campo-sirio/src/ps/ps1004100.cpp
Mattia Tollari 3e6707c33e Patch level : 12.0 no-patch
Files correlati     : ps
Commento            : Modifiche Diana 2000:
- Aggiunta modalità auto senza finestra
- Aggiunto lancio eseguibile post-esportazione
- Aggiunto prefisso tabelle
2018-10-19 12:10:12 +02:00

352 lines
8.2 KiB
C++
Raw Blame History

#include <applicat.h>
#include <automask.h>
#include <tsdb.h>
#include <defmask.h>
#include <execp.h>
#include <modaut.h>
#include <progind.h>
#include <relation.h>
#include <tabutil.h>
#include <clifo.h>
#include <cfven.h>
#include <partite.h>
#include <rmov.h>
#include <doc.h>
#include <mov.h>
#include "../ca/calib01.h"
#include "../cg/cgsaldac.h"
#include "../ef/ef0301.h"
#include "ps1004.h"
#include "ps1004100a.h"
#include "utility.h"
///////////////////////////////////////////////////////////
// Utilities
///////////////////////////////////////////////////////////
void set_connection(SSimple_query& s);
SSimple_query& diana_db()
{
static SSimple_query* db = nullptr;
if (db == nullptr)
{
db = new SSimple_query();
set_connection(*db);
// Non utilizzo l'autocommit, viene gestito manualmente
db->sq_set_autocommit(false);
}
return *db;
}
void set_connection(SSimple_query& s)
{
if (s.sq_connect(
TString() << ini_get_string(CONFIG_DITTA, "ps1004", "ip") << "@" << ini_get_string(CONFIG_DITTA, "ps1004", "db"),
ini_get_string(CONFIG_DITTA, "ps1004", "usr"),
decode(ini_get_string(CONFIG_DITTA, "ps1004", "psw")),
TSDB_MSSQL) != NOERR)
fatal_box("Impossibile connettersi al DB esterno");
}
const TString to_escape(const TString& val)
{
TString& app = get_tmp_string();
for (int k = 0; k < val.len(); k++)
{
switch (val[k])
{
case '\'':
app << "''";
break;
default:
app << val[k];
break;
}
}
return app;
}
const TString query_to_null(TString& val)
{
int lastpos = val.find("(\'\'", 0); // Devo trovarne uno tra virgole
while (lastpos != -1)
{
lastpos++;
val[lastpos] = ' ';
val[lastpos + 1] = ' ';
val.insert("NULL", lastpos);
lastpos = val.find(",\'\'", lastpos);
}
lastpos = val.find(",\'\'", 0); // Devo trovarne uno tra virgole
while (lastpos != -1)
{
lastpos++;
val[lastpos] = ' ';
val[lastpos + 1] = ' ';
val.insert("NULL", lastpos);
lastpos = val.find(",\'\'", lastpos);
}
return val;
}
///////////////////////////////////////////////////////////
// Main Mask
///////////////////////////////////////////////////////////
class TDianaExport_mask : public TAutomask
{
protected:
virtual bool on_field_event(TOperable_field& o, TField_event e, long jolly);
void load_all();
public:
void save_all() const;
TDianaExport_mask();
virtual ~TDianaExport_mask(){}
};
bool TDianaExport_mask::on_field_event(TOperable_field& o, TField_event e, long jolly)
{
return TRUE;
}
TDianaExport_mask::TDianaExport_mask() : TAutomask("ps1004100a")
{
load_all();
}
void TDianaExport_mask::save_all() const
{
ini_set_string(CONFIG_DITTA, "ps1004", "ip", get(F_INDIRIZZO));
ini_set_string(CONFIG_DITTA, "ps1004", "db", get(F_DATABASE));
ini_set_string(CONFIG_DITTA, "ps1004", "usr", get(F_USER));
ini_set_string(CONFIG_DITTA, "ps1004", "psw", encode(get(F_PASSWORD)));
ini_set_string(CONFIG_DITTA, "ps1004", "ext", get(F_EXTERNAL));
}
void TDianaExport_mask::load_all()
{
set(F_INDIRIZZO, ini_get_string(CONFIG_DITTA, "ps1004", "ip"));
set(F_DATABASE, ini_get_string(CONFIG_DITTA, "ps1004", "db"));
set(F_USER, ini_get_string(CONFIG_DITTA, "ps1004", "usr"));
set(F_PASSWORD, decode(ini_get_string(CONFIG_DITTA, "ps1004", "psw")));
set(F_EXTERNAL, ini_get_string(CONFIG_DITTA, "ps1004", "ext"));
}
///////////////////////////////////////////////////////////
// Main Program
///////////////////////////////////////////////////////////
class TDianaExport_app : public TSkeleton_application
{
bool _visual;
bool export_all();
bool export_table(const int logicnum);
bool create_table(const int logicnum);
bool deploy_data(const int logicnum);
inline bool rko_outta_nowhere(const int logicnum);
bool launch_ext();
public:
virtual bool create();
virtual void main_loop();
TDianaExport_app() : _visual(false) {}
};
bool TDianaExport_app::export_all()
{
return
export_table(LF_TAB) &&
export_table(LF_CLIFO) &&
export_table(LF_ANAMAG) &&
export_table(LF_COMUNI) &&
export_table(LF_LVPASPLAN) &&
export_table(LF_LVRCONDV);
}
bool TDianaExport_app::export_table(const int logicnum)
{
return rko_outta_nowhere(logicnum) && create_table(logicnum) && deploy_data(logicnum);
}
bool TDianaExport_app::create_table(const int logicnum)
{
const TString& table = logic2table(logicnum);
const RecDes& rd = prefix().get_recdes(logicnum);
// Create new table
TString sql; sql.cut(0) << "CREATE TABLE CAMPO_" << table << "\n(";
for (int i = 0; i < rd.NFields; i++)
{
if (i > 0) sql << ',';
sql << rd.Fd[i].Name << ' ';
switch (rd.Fd[i].TypeF)
{
case _charfld:
case _alfafld:
sql << "VARCHAR(" << rd.Fd[i].Len << ")";
break;
case _memofld:
sql << "TEXT"; break;
case _datefld:
sql << "DATE"; break;
case _boolfld:
sql << "BIT DEFAULT 0"; break;
case _realfld:
case _wordfld:
case _intfld:
case _longfld:
case _intzerofld:
case _longzerofld:
default: sql << "NUMERIC(" << rd.Fd[i].Len << "," << rd.Fd[i].Dec << ")"; break;
}
}
sql << ");";
return diana_db().sq_set_exec(sql) && diana_db().sq_commit();;
}
bool TDianaExport_app::deploy_data(const int logicnum)
{
const RecDes& rd = prefix().get_recdes(logicnum);
TLocalisamfile table(logicnum);
TString queryF, queryV;
TProgress_monitor p(table.items(), TString("Esportazione tabella ") << logic2table(logicnum));
bool export_status = true;
for(bool ok = table.first() == NOERR; ok && export_status; ok = table.next() == NOERR)
{
if (_visual && !p.add_status())
return false;
queryF.cut(0) << "INSERT INTO [CAMPO_" << logic2table(logicnum) << "] ("; // Tolgo il simbolo iniziale
queryV.cut(0) << "(";
for (int k = 0; k < rd.NFields; k++)
{
queryF << "[" << rd.Fd[k].Name << "]";
if (table.get(rd.Fd[k].Name).empty()) // Vuoto, causa errori se non controllato
queryV << "''";
else
{
switch (rd.Fd[k].TypeF)
{
case _intfld:
case _wordfld:
case _intzerofld:
queryV << "'" << table.get_int(rd.Fd[k].Name) << "'";
break;
case _longfld:
case _longzerofld:
queryV << "'" << table.get_long(rd.Fd[k].Name) << "'";
break;
// Real
case _realfld:
queryV << "'" << table.get_real(rd.Fd[k].Name).string() << "'";
break;
case _datefld:
queryV << "'" << table.get_date(rd.Fd[k].Name).date2ansi() << "'";
break;
case _boolfld:
if (table.get_bool(rd.Fd[k].Name))
queryV << "'1'";
else
queryV << "'0'";
break;
case _charfld:
case _alfafld:
default:
queryV << "'" << to_escape(table.get(rd.Fd[k].Name)) << "'";
break;
}
}
if (k + 1 < rd.NFields) // Modo pi<70> comodo
{
queryF << ",";
queryV << ",";
}
}
queryV << ")";
queryF << ") VALUES " << queryV;
export_status &= diana_db().sq_set_exec(query_to_null(queryF));
}
return export_status && diana_db().sq_commit();
}
bool TDianaExport_app::rko_outta_nowhere(const int logicnum)
{
diana_db().sq_set_exec(TString().cut(0) << "DROP TABLE CAMPO_" << logic2table(logicnum) << ';') && diana_db().sq_commit();
return true;
}
bool TDianaExport_app::launch_ext()
{
TString ext = ini_get_string(CONFIG_DITTA, "ps1004", "ext");
if(ext.full())
{
TExternal_app e(ext);
e.run(true);
}
return true;
}
bool TDianaExport_app::create()
{
open_files(LF_TAB, LF_CLIFO, LF_ANAMAG,
LF_COMUNI, LF_LVPASPLAN, LF_LVRCONDV, 0);
return TSkeleton_application::create();
}
void TDianaExport_app::main_loop()
{
const TFixed_string arg = argv(2);
if(arg.starts_with("-a") || arg.starts_with("-A"))
{
const WINDOW task = TASK_WIN;
xvt_vobj_set_visible(task, FALSE);
export_all() && launch_ext();
}
else
{
TDianaExport_mask msk;
while (msk.run() == K_ENTER)
{
bool connected = false;
_visual = true;
{
TString ad; ad << msk.get(F_INDIRIZZO) << "@" << msk.get(F_DATABASE);
SSimple_query s(ad, msk.get(F_USER), msk.get(F_PASSWORD), TSDB_MSSQL);
connected = s.sq_is_connect();
if (connected)
{
msk.save_all();
message_box("Dati salvati correttamente!");
}
else
{
error_box("Impossibile stabilire una connessione al database");
}
}
connected && yesno_box("Vuoi eseguire l'esportazione dei dati?") && export_all() && launch_ext();
}
}
}
int ps1004100(int argc, char* argv[])
{
int n = 0;
TDianaExport_app pe;
pe.run(argc, argv, TR("Esportazione tabelle per terminalino"));
return 0;
}