Patch level : 12.0 766

Files correlati     : ba2900
Commento            :
- Aggiunta esportazione archivi campo su MSSQLSERVER
- [Da implementrare] esportazione dirgen
This commit is contained in:
Simone Palacino 2019-04-09 18:02:00 +02:00
parent 1d6b7a426e
commit 6364ac9540
4 changed files with 421 additions and 167 deletions

View File

@ -118,7 +118,7 @@
</Midl> </Midl>
<ClCompile> <ClCompile>
<Optimization>Disabled</Optimization> <Optimization>Disabled</Optimization>
<AdditionalIncludeDirectories>..\src\include;..\src\xvaga;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> <AdditionalIncludeDirectories>..\src\include;..\src\xvaga;..\src\xvtdb;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<PreprocessorDefinitions>_DEBUG;WIN32;__LONGDOUBLE__;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions> <PreprocessorDefinitions>_DEBUG;WIN32;__LONGDOUBLE__;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary> <RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
<BrowseInformation>false</BrowseInformation> <BrowseInformation>false</BrowseInformation>

View File

@ -8,9 +8,32 @@
#include <real.h> #include <real.h>
#include "ba2900.h" #include "ba2900.h"
#include "../xvtdb/xvtdb.h"
//#include "tsdb.h"
#include "codeb.h"
#define TABELLE_CAMPO 171 #define TABELLE_CAMPO 171
void check_range_tab(TMask& msk)
{
if (msk.get_int(F_FROMTAB) == 7466)
{
msk.show(F_MYRANGE);
msk.set(F_FROMTAB, 2);
}
else
{
if (msk.get_int(F_FROMTAB) < 2)
msk.set(F_FROMTAB, 2);
if (msk.get_int(F_FROMTAB) > prefix().items())
msk.set(F_FROMTAB, prefix().items());
if (msk.get_int(F_TOTAB) < msk.get_int(F_FROMTAB))
msk.set(F_TOTAB, msk.get_int(F_FROMTAB));
if (msk.get_int(F_TOTAB) > prefix().items())
msk.set(F_TOTAB, prefix().items());
}
}
class TMSSQLExport_msk : public TAutomask class TMSSQLExport_msk : public TAutomask
{ {
protected: protected:
@ -18,6 +41,7 @@ protected:
virtual bool on_field_event(TOperable_field& o, TField_event e, long jolly); virtual bool on_field_event(TOperable_field& o, TField_event e, long jolly);
public: public:
TMSSQLExport_msk(); TMSSQLExport_msk();
~TMSSQLExport_msk();
}; };
long TMSSQLExport_msk::handler(WINDOW task, EVENT* ep) long TMSSQLExport_msk::handler(WINDOW task, EVENT* ep)
@ -40,7 +64,21 @@ bool TMSSQLExport_msk::on_field_event(TOperable_field& o, TField_event e, long j
if (e == se_query_add || e == se_query_del) if (e == se_query_add || e == se_query_del)
return false; return false;
*/ */
case CHK_ONLYTAB:
if (e == fe_modify)
{
get_bool(CHK_ONLYTAB) ? enable(F_FROMTAB) : disable(F_FROMTAB);
get_bool(CHK_ONLYTAB) ? enable(F_TOTAB) : disable(F_TOTAB);
break;
}
case CHK_EXPORTTABLES:
if (e == fe_modify)
{
get_bool(CHK_EXPORTTABLES) ? enable(CHK_ONLYTAB) : disable(CHK_ONLYTAB);
break;
}
default: default:
check_range_tab(*this);
break; break;
} }
return true; return true;
@ -51,38 +89,58 @@ TMSSQLExport_msk::TMSSQLExport_msk() : TAutomask("ba2900a")
set(F_DSN, ini_get_string(CONFIG_DITTA, "Campo_MSSQL_Export", "DSN")); set(F_DSN, ini_get_string(CONFIG_DITTA, "Campo_MSSQL_Export", "DSN"));
set(F_USR, ini_get_string(CONFIG_DITTA, "Campo_MSSQL_Export", "User")); set(F_USR, ini_get_string(CONFIG_DITTA, "Campo_MSSQL_Export", "User"));
set(F_PWD, ini_get_string(CONFIG_DITTA, "Campo_MSSQL_Export", "Password")); set(F_PWD, ini_get_string(CONFIG_DITTA, "Campo_MSSQL_Export", "Password"));
set(CHK_CREATEGEN, "X"); //set(CHK_CREATEGEN, "X");
set(CHK_CREATETABLES, "X"); set(CHK_CREATETABLES, "X");
set(CHK_EXPORTGEN, "X"); //set(CHK_EXPORTGEN, "X");
set(CHK_EXPORTTABLES, "X"); set(CHK_EXPORTTABLES, "X");
set(F_FROMTAB, 2);
set(F_TOTAB, prefix().items());
}
TMSSQLExport_msk::~TMSSQLExport_msk()
{
ini_set_string(CONFIG_DITTA, "Campo_MSSQL_Export", "DSN", TMask::get(F_DSN));
ini_set_string(CONFIG_DITTA, "Campo_MSSQL_Export", "User", TMask::get(F_USR));
ini_set_string(CONFIG_DITTA, "Campo_MSSQL_Export", "Password", TMask::get(F_PWD));
} }
class TMSSQLExport_app : public TSkeleton_application class TMSSQLExport_app : public TSkeleton_application
{ {
protected: protected:
TToken_string tables; TToken_string _tables;
TString DSN, usr, psw; TString _DSN, _usr, _psw;
SSimple_query* _db;
TMSSQLExport_msk* _msk;
bool _visual;
const TString toEscape (TString val) const; // Prende una stringa e sistema i caratteri di escape const TString toEscape (TString val) const; // Prende una stringa e sistema i caratteri di escape
const TString toDate (TString val) const; // Prende una stringa e la trasforma in una data di mssql const TString toDate (TString val) const; // Prende una stringa e la trasforma in una data di mssql
const TString queryToNull (TString val) const; // Da una query sostituisce tutti i valori vuoti ('') con null const TString queryToNull (TString val) const; // Da una query sostituisce tutti i valori vuoti ('') con null
//***************************************************************** //*****************************************************************
bool emptyTables() const; bool empty_tables() const;
bool exportGen() const; bool expor_gen() const;
bool exportTables() const; bool file_valid(int logicnum) const;
bool createGen() const; bool export_tables() const;
bool createTables() const; bool create_gen() const;
bool createIndexes() const; static TString tab_name(int logicnum, TString& ditta_name);
void set_range_tab(int& logicnum, int& endtab) const;
bool create_tables() const;
bool create_indexes() const;
bool rko_outta_nowhere(const char* table_name) const;
// Funzioni di esportazione // Funzioni di esportazione
bool dir_gen() const; bool dir_gen() const;
bool trc_gen() const; bool trc_gen() const;
bool exportManager(const TString generalErrors) const; bool export_manager(const TString generalErrors) const;
public: public:
bool my_range();
virtual void main_loop(); virtual void main_loop();
void setTable(TToken_string s) { tables = s; } void set_table(TToken_string s) { _tables = s; }
bool setParameters(TString dsn, TString utente, TString password); bool set_parameters(TString dsn, TString utente, TString password);
bool checkParameters(const TString& DSN, const TString& usr, const TString& psw) { TODBC_recordset connTest(""); return connTest.connect(DSN, usr, psw) ? setParameters(DSN, usr, psw) : false; } bool connect(const TString& _DSN, const TString& _usr, const TString& _psw);
bool testFieldSeq(int val, int arr[]) const; //bool checkParameters() { TODBC_recordset connTest(""); return connTest.connect(_DSN, _usr, _psw) ? set_parameters(_DSN, _usr, _psw) : false; }
bool test_field_seq(int val, int arr[]) const;
TMSSQLExport_app() : _visual(false) { }
~TMSSQLExport_app() { delete _db; }
}; };
// Funzioni Utility **************************************************************************************************************************** // Funzioni Utility ****************************************************************************************************************************
@ -137,31 +195,31 @@ const TString TMSSQLExport_app::toDate(TString val) const
} }
bool TMSSQLExport_app::setParameters(TString dsn, TString utente, TString password) bool TMSSQLExport_app::set_parameters(TString dsn, TString utente, TString password)
{ {
// Salvo i parametri // Salvo i parametri
ini_set_string(CONFIG_DITTA, "Campo_MSSQL_Export", "DSN", dsn); ini_set_string(CONFIG_DITTA, "Campo_MSSQL_Export", "_DSN", dsn);
ini_set_string(CONFIG_DITTA, "Campo_MSSQL_Export", "User", utente); ini_set_string(CONFIG_DITTA, "Campo_MSSQL_Export", "User", utente);
ini_set_string(CONFIG_DITTA, "Campo_MSSQL_Export", "Password", password); ini_set_string(CONFIG_DITTA, "Campo_MSSQL_Export", "Password", password);
DSN = dsn; _DSN = dsn;
usr = utente; _usr = utente;
psw = password; _psw = password;
return true; return true;
} }
bool TMSSQLExport_app::emptyTables() const bool TMSSQLExport_app::empty_tables() const
{ {
TODBC_recordset sqlset(""); TODBC_recordset sqlset("");
TString table; TString table;
if (!sqlset.connect(DSN, usr, psw)) if (!sqlset.connect(_DSN, _usr, _psw))
return false; return false;
for (int i = 0; i < tables.size(); i++) for (int i = 0; i < _tables.size(); i++)
{ {
TString theQuery; // The query: una tabella (donna) per cui uccidere TString theQuery; // The query: una tabella (donna) per cui uccidere
tables.get(i, table); _tables.get(i, table);
theQuery << "DELETE FROM "<< table << ";"; theQuery << "DELETE FROM "<< table << ";";
sqlset.exec(theQuery); sqlset.exec(theQuery);
} }
@ -171,7 +229,7 @@ bool TMSSQLExport_app::emptyTables() const
} }
// Controllo che il valore passato sia > e diverso da quelli presenti nell'array // Controllo che il valore passato sia > e diverso da quelli presenti nell'array
bool TMSSQLExport_app::testFieldSeq(int val, int arr[]) const bool TMSSQLExport_app::test_field_seq(int val, int arr[]) const
{ {
for (int i = 0; i < MKFields; i++) for (int i = 0; i < MKFields; i++)
if(val == arr[i] || val < arr[i]) return false; if(val == arr[i] || val < arr[i]) return false;
@ -179,12 +237,8 @@ bool TMSSQLExport_app::testFieldSeq(int val, int arr[]) const
} }
// Main Program ***************************************************************************************************************************************************************** // Main Program *****************************************************************************************************************************************************************
bool TMSSQLExport_app::createGen() const bool TMSSQLExport_app::create_gen() const
{ {
TODBC_recordset sqlset("");
// Controllo la connessione di nuovo per essere scrupolosi
if (!sqlset.connect(DSN, usr, psw))
return false;
ifstream queryFile; // stream dei files con le query ifstream queryFile; // stream dei files con le query
// Prima le tabelle gen // Prima le tabelle gen
@ -200,7 +254,7 @@ bool TMSSQLExport_app::createGen() const
gensql << aganaye; gensql << aganaye;
} }
queryFile.close(); queryFile.close();
if(sqlset.exec(gensql) != 1) // EXEC if(!_db->sq_exec(gensql)) // EXEC
return false; return false;
} }
else else
@ -211,94 +265,105 @@ bool TMSSQLExport_app::createGen() const
} }
// Committo la creazione iniziale delle tabelle custom fondamentali per il passaggio a DB di Campo // Committo la creazione iniziale delle tabelle custom fondamentali per il passaggio a DB di Campo
return sqlset.commit() != -1; return _db->sq_commit();
} }
bool TMSSQLExport_app::createTables() const
TString TMSSQLExport_app::tab_name(int logicnum, TString& ditta_name)
{ {
TODBC_recordset sqlset(""); const TString& table = logic2table(logicnum);
// Controllo la connessione di nuovo per essere scrupolosi const bool studio_tab = TDir(logicnum).is_com();
if (!sqlset.connect(DSN, usr, psw)) return TString("[") << (!studio_tab ? ditta_name : "") << table << "]";
return false; }
ifstream queryFile; // stream dei files con le query
void TMSSQLExport_app::set_range_tab(int& logicnum, int& endtab) const
// Le tabelle comuni {
TFilename studio("sql\\studiosql.sql"); logicnum = 2;
queryFile.open(studio); endtab = prefix().items();
if(queryFile.is_open()) if (_msk->get_bool(CHK_ONLYTAB))
{ {
TString studiosql; // Qua metterò le query check_range_tab(*_msk);
while(!queryFile.eof()) logicnum = _msk->get_int(F_FROMTAB);
{ endtab = _msk->get_int(F_TOTAB);
char aganaye[10000]; endtab++;
queryFile.getline(aganaye, 10000); // Sei stronzo se fai una riga con più di 10.000 caratteri
studiosql << aganaye;
}
queryFile.close();
if(sqlset.exec(studiosql) != 1) // EXEC
return false;
} }
else }
bool TMSSQLExport_app::connect(const TString& _DSN, const TString& _usr, const TString& _psw)
{
if (_db == nullptr)
_db = new SSimple_query();
if (!_db->sq_is_connect())
{ {
TString msg("Errore apertura file "); msg << studio; return _db->sq_connect(_DSN, _usr, _psw, TSDB_MSSQL) == NOERR;
message_box(msg);
return false;
} }
return true;
}
/************************************************************************************************ bool TMSSQLExport_app::create_tables() const
* Le tabelle di ditta, caso particolare: * {
* In questi file è presente una sequenza di escape (%%%%%) che andrà sostituita con la ditta, * bool ok = true;
* quindi prima leggo tutto e poi sostituisco * TString ditta_name; ditta_name << prefix().name() << "_";
************************************************************************************************/ TString last_col_name = " ";
TFilename ditta("sql\\dittesql.sql");
TString appsql;
queryFile.open(ditta);
// Creo il cursore delle ditte int logicnum;
TRelation relTDitte(LF_NDITTE); int endtab;
TCursor curDitte(&relTDitte); set_range_tab(logicnum, endtab);
int itemsDitte = curDitte.items();
if(queryFile.is_open()) for(; logicnum < endtab && ok; logicnum++)
{ {
while(!queryFile.eof()) last_col_name = " ";
const RecDes& rd = prefix().get_recdes(logicnum);
TString table_name = tab_name(logicnum, ditta_name);
TString column_name;
bool last_err = false;
// Droppo la tabella prima di crearla
rko_outta_nowhere(table_name);
// Create new table
if (rd.NFields == 0)
continue;
TString sql; sql.cut(0) << "CREATE TABLE " << table_name << "(\n";
for (int i = 0; i < rd.NFields; i++)
{ {
char aganaye[10000];
queryFile.getline(aganaye, 10000); // Sei stronzo se fai una riga con più di 10.000 caratteri if (i > 0 && !last_err) sql << ",\n";
appsql << aganaye; column_name.cut(0) << "[" << rd.Fd[i].Name << "] ";
} if (column_name == last_col_name)
for(curDitte = 0; curDitte.pos() < curDitte.items(); ++curDitte)
{
TRectype dittaCurr = curDitte.curr();
TString nomeDitta(dittaCurr.get("CODDITTA"));
for (int i = 0, applen = nomeDitta.len(); i < 5 - applen; i++)
nomeDitta.insert("0");
// Non esiste nulla che rimpiazzi tutte occorrenze (stringhe) in una TString, quindi mi faccio io l'algoritmo
TString queryFirm(appsql);
int lastpos = queryFirm.find("%%%%%", 0);
while(lastpos != -1)
{ {
// Siccome so che ci sono 5 caratteri faccio che farlo a mano da vero gangster last_err = true;
queryFirm[lastpos] = nomeDitta[0]; continue;
queryFirm[lastpos + 1] = nomeDitta[1]; }
queryFirm[lastpos + 2] = nomeDitta[2]; last_err = false;
queryFirm[lastpos + 3] = nomeDitta[3]; last_col_name = column_name;
queryFirm[lastpos + 4] = nomeDitta[4]; sql << column_name;
lastpos = queryFirm.find("%%%%%", lastpos); // find(x, lastpos) si velocizza partendo già dalla ultima posizione trovata switch (rd.Fd[i].TypeF)
} {
if(sqlset.exec(queryFirm) != 1) // EXEC case _charfld:
return false; 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;
}
} }
queryFile.close(); sql << "\n);";
ok = _db->sq_set_exec(sql) && _db->sq_commit();
TString err = _db->sq_get_string_error();
TString err2 = _db->sq_get_text_error();
} }
else return ok;
{
TString msg("Errore apertura file "); msg << ditta;
message_box(msg);
return false;
}
// Committo la creazione iniziale delle tabelle custom fondamentali per il passaggio a DB di Campo
return sqlset.commit() != -1 && createIndexes();
} }
/* /*
@ -306,10 +371,10 @@ bool TMSSQLExport_app::createTables() const
* Sintassi: CREATE NONCLUSTERED INDEX nome_indice ON nome_tabella (colonna1 [ASC | DESC], colonna2 [ASC | DESC][,...n]) * Sintassi: CREATE NONCLUSTERED INDEX nome_indice ON nome_tabella (colonna1 [ASC | DESC], colonna2 [ASC | DESC][,...n])
* *
*/ */
bool TMSSQLExport_app::createIndexes() const bool TMSSQLExport_app::create_indexes() const
{ {
TODBC_recordset sqlset(""); TODBC_recordset sqlset("");
if (!sqlset.connect(DSN, usr, psw)) if (!sqlset.connect(_DSN, _usr, _psw))
return "Connessione fallita!"; return "Connessione fallita!";
int items = TABELLE_CAMPO; // Numero totale di tabelle int items = TABELLE_CAMPO; // Numero totale di tabelle
COpenDir((int)_manulock, NORDIR); // Apro dir.gen COpenDir((int)_manulock, NORDIR); // Apro dir.gen
@ -392,42 +457,166 @@ bool TMSSQLExport_app::createIndexes() const
return true; return true;
} }
bool TMSSQLExport_app::exportGen() const bool TMSSQLExport_app::rko_outta_nowhere(const char* table_name) const
{
_db->sq_set_exec(TString().cut(0) << "DROP TABLE " << table_name << ';') && _db->sq_commit();
return true;
}
bool TMSSQLExport_app::expor_gen() const
{ {
return dir_gen() && trc_gen(); return dir_gen() && trc_gen();
} }
bool TMSSQLExport_app::exportTables() const bool TMSSQLExport_app::file_valid(int logicnum) const
{
TFilename filename;
const TIsam_handle isam_handle = prefix().open_isamfile(logicnum, filename, false, true);
const TCodeb_handle fhnd = isam_handle > 0 ? prefix().get_handle(isam_handle, 1) : isam_handle;
const int trcreclen = prefix().get_reclen(logicnum);
const int dbfreclen = DB_reclen(fhnd);
return dbfreclen == trcreclen;
}
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;
}
bool TMSSQLExport_app::export_tables() const
{ {
// Innanzitutto svuoto le tabelle // Innanzitutto svuoto le tabelle
if(!emptyTables()) return false;
TODBC_recordset sqlset(""); bool check = true;
if (!sqlset.connect(DSN, usr, psw)) TString ditta_name; ditta_name << prefix().name() << "_";
return "Connessione fallita!";
try int logicnum;
int endtab;
set_range_tab(logicnum, endtab);
for (; logicnum < endtab && check; logicnum++)
{ {
bool wat = false; TString nome_tab = tab_name(logicnum, ditta_name);
// Inizio con dir.gen const RecDes& rd = prefix().get_recdes(logicnum);
TString generalErrors; TLocalisamfile table(logicnum);
exportManager(generalErrors); if(!file_valid(logicnum))
{
warning_box(TString("Impossibile esportare il file ") << logicnum);
continue;
}
TString queryF, queryV;
TProgress_monitor p(table.items(), TString("Esportazione tabella ") << logic2table(logicnum));
bool export_status = true;
bool ok = table.first() == NOERR;
for (; ok && export_status; ok = table.next() == NOERR)
{
if (_visual && !p.add_status())
return false;
queryF.cut(0) << "INSERT INTO " << nome_tab << " (";
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ù comodo
{
queryF << ",";
queryV << ",";
}
}
queryV << ")";
queryF << ") VALUES " << queryV;
export_status &= _db->sq_set_exec(query_to_null(queryF));
}
check = export_status && _db->sq_commit();
} }
catch (TString orrore) return check;
{
message_box(orrore);
return false;
}
return true;
} }
bool TMSSQLExport_app::dir_gen() const bool TMSSQLExport_app::dir_gen() const
{ {
TODBC_recordset sqlset("", true); const int items = prefix().items()-1; // Numero totale di tabelle
// Controllo la connessione const TString msg("Esportazione elenco directory");
if (!sqlset.connect(DSN, usr, psw))
return false;
int items = TABELLE_CAMPO; // Numero totale di tabelle
TString msg("Esportazione elenco directory");
COpenDir((int)_manulock, NORDIR); // Apro dir.gen COpenDir((int)_manulock, NORDIR); // Apro dir.gen
TProgress_monitor p(items, msg); TProgress_monitor p(items, msg);
for (int i = 1; i <= items && !p.is_cancelled(); i++) for (int i = 1; i <= items && !p.is_cancelled(); i++)
@ -448,20 +637,16 @@ bool TMSSQLExport_app::dir_gen() const
<< toEscape(d.Des) << "','" << toEscape(d.Des) << "','"
<< toEscape(d.FCalc) << "','" << toEscape(d.FCalc) << "','"
<< toEscape(d.GenPrompt) << "')"; << toEscape(d.GenPrompt) << "')";
if(sqlset.exec(queryToNull(sqlQuery)) != 1) if(!_db->sq_exec(queryToNull(sqlQuery)))
return false; return false;
} }
return sqlset.commit() == -1 ? false : true; return _db->sq_commit();
} }
bool TMSSQLExport_app::trc_gen() const bool TMSSQLExport_app::trc_gen() const
{ {
TODBC_recordset sqlset("", true); const int items = prefix().items()-1; // Numero totale di tabelle
// Controllo la connessione const TString msg("Esportazione tracciati record");
if (!sqlset.connect(DSN, usr, psw))
return false;
int items = TABELLE_CAMPO; // Numero totale di tabelle
TString msg("Esportazione tracciati record");
COpenDir((int)_manulock, NORDIR); // Apro dir.gen COpenDir((int)_manulock, NORDIR); // Apro dir.gen
TProgress_monitor p(items-1, msg); TProgress_monitor p(items-1, msg);
for (int i = 2; i <= items && !p.is_cancelled(); i++) for (int i = 2; i <= items && !p.is_cancelled(); i++)
@ -487,7 +672,7 @@ bool TMSSQLExport_app::trc_gen() const
<< SortFd << "','" << SortFd << "','"
<< d.NKeys << "','" << d.NKeys << "','"
<< i << "')"; << i << "')";
if(sqlset.exec(queryToNull(sqlQueryTrc)) != 1) if(!_db->sq_exec(queryToNull(sqlQueryTrc)))
return false; return false;
// Poi trc_recfdes // Poi trc_recfdes
@ -504,7 +689,7 @@ bool TMSSQLExport_app::trc_gen() const
<< r.Dec << "','" << r.Dec << "','"
<< r.RecOff << "','" << r.RecOff << "','"
<< i << "')"; << i << "')";
if(sqlset.exec(queryToNull(sqlQueryRFD)) != 1) if(!_db->sq_exec(queryToNull(sqlQueryRFD)))
return false; return false;
} }
@ -540,18 +725,18 @@ bool TMSSQLExport_app::trc_gen() const
<< FromCh << "','" << FromCh << "','"
<< ToCh << "','" << ToCh << "','"
<< i << "')"; << i << "')";
if(sqlset.exec(queryToNull(sqlQueryKD)) != 1) if(!_db->sq_exec(queryToNull(sqlQueryKD)))
return false; return false;
} }
} }
return sqlset.commit() == -1 ? false : true; return _db->sq_commit();
} }
bool TMSSQLExport_app::exportManager(TString generalErrors) const bool TMSSQLExport_app::export_manager(TString generalErrors) const
{ {
TODBC_recordset sqlset("", true); TODBC_recordset sqlset("", true);
// Controllo la connessione // Controllo la connessione
if (!sqlset.connect(DSN, usr, psw)) if (!sqlset.connect(_DSN, _usr, _psw))
return false; return false;
COpenDir((int)_manulock, NORDIR); // Apro dir.gen COpenDir((int)_manulock, NORDIR); // Apro dir.gen
@ -662,31 +847,70 @@ bool TMSSQLExport_app::exportManager(TString generalErrors) const
return true; return true;
} }
bool TMSSQLExport_app::my_range()
{
TString myrange(_msk->get(F_MYRANGE));
if (myrange.empty())
return false;
TToken_string range_tok(myrange, ',');
if(range_tok.items() > 0)
{
TToken_string one_range(range_tok.get(0), '-');
_msk->set(F_FROMTAB, one_range.get(0));
one_range.items() == 0 ? _msk->set(F_TOTAB, one_range.get(0)) : _msk->set(F_TOTAB, one_range.get(1));
range_tok.destroy(0);
TString rewrite;
for(int i=0; i < range_tok.items(); i++)
{
if (i > 0)
rewrite << ",";
rewrite << range_tok.get();
}
_msk->set(F_MYRANGE, rewrite);
}
return !TString(_msk->get(F_MYRANGE)).empty(); // se vuoto anche solo la prima volta interrompo il loop
}
void TMSSQLExport_app::main_loop() void TMSSQLExport_app::main_loop()
{ {
TMSSQLExport_msk m; TMSSQLExport_msk msk;
while (m.run() == K_ENTER) _msk = &msk;
while (msk.run() == K_ENTER)
{ {
const TString& DSN = m.get(F_DSN); #ifdef DBG
const TString& usr = m.get(F_USR); const TString& _DSN = "TESTCAMPO2012\\MSSQLSERVER2019@campo13";
const TString& psw = m.get(F_PWD); const TString& _usr = "campo";
const TString& _psw = "campo";
#else
const TString& _DSN = msk.get(F_DSN);
const TString& _usr = msk.get(F_USR);
const TString& _psw = msk.get(F_PWD);
#endif
_visual = true;
// Chiamo la funzione globale esporta // Chiamo la funzione globale esporta
if(checkParameters(DSN, usr, psw)) if(connect(_DSN, _usr, _psw))
{ {
// Per comodità utilizzo gli AND short circuits, solo se i bool danno true eseguo la funzione dopo! while (true)
if(m.get_bool(CHK_CREATEGEN)) {
if(!createGen()) bool loop = my_range();
message_box("ERROR: Creazione tabelle gen fallita"); // Per comodità utilizzo gli AND short circuits, solo se i bool danno true eseguo la funzione dopo!
if(m.get_bool(CHK_CREATETABLES)) if (msk.get_bool(CHK_CREATEGEN))
if(!createTables()) if (!create_gen())
message_box("ERROR: Creazione tabelle campo fallita"); message_box("ERROR: Creazione tabelle gen fallita");
if(m.get_bool(CHK_EXPORTGEN)) if (msk.get_bool(CHK_CREATETABLES))
if (!exportGen()) if (!create_tables())
message_box("CERROR: Esportazione tabelle gen fallita"); message_box("ERROR: Creazione tabelle campo fallita");
if(m.get_bool(CHK_EXPORTTABLES)) if (msk.get_bool(CHK_EXPORTGEN))
if(!exportTables()) if (!expor_gen())
message_box("ERROR: Esportazione tabelle campo fallita"); message_box("CERROR: Esportazione tabelle gen fallita");
message_box("Migrazione effettuata correttamente!"); if (msk.get_bool(CHK_EXPORTTABLES))
if (!export_tables())
message_box("ERROR: Esportazione tabelle campo fallita");
message_box("Migrazione effettuata correttamente!");
if (!loop)
break;
}
} }
else else
message_box("Fallita connessione"); message_box("Fallita connessione");
@ -698,7 +922,7 @@ int ba2900(int argc, char* argv[])
{ {
TMSSQLExport_app app; TMSSQLExport_app app;
// Imposto le tabelle da utilizzare // Imposto le tabelle da utilizzare
app.setTable("TRC_GEN|TRC_KEYDES|TRC_RECFDES"); app.set_table("TRC_GEN|TRC_KEYDES|TRC_RECFDES");
app.run(argc, argv, TR("Migrazione DB a MSSQL")); app.run(argc, argv, TR("Migrazione DB a MSSQL"));
return 0; return 0;
} }

View File

@ -5,3 +5,8 @@
#define CHK_CREATETABLES 254 #define CHK_CREATETABLES 254
#define CHK_EXPORTGEN 255 #define CHK_EXPORTGEN 255
#define CHK_EXPORTTABLES 256 #define CHK_EXPORTTABLES 256
#define CHK_ONLYTAB 257
#define F_FROMTAB 258
#define F_TOTAB 259
#define F_MYRANGE 260

View File

@ -10,7 +10,7 @@ PAGE "Trasferimento DB su MSSQL" 0 2 0 0
STRING F_DSN 260 50 STRING F_DSN 260 50
BEGIN BEGIN
PROMPT 1 0 "ODBC DSN " PROMPT 1 1 "ODBC DSN "
CHECKTYPE REQUIRED CHECKTYPE REQUIRED
END END
@ -30,6 +30,7 @@ END
BOOLEAN CHK_CREATEGEN BOOLEAN CHK_CREATEGEN
BEGIN BEGIN
PROMPT 1 5 "Elimina e ricrea tutte le tabelle gen, Attenzione! I dati presenti verranno eliminati!" PROMPT 1 5 "Elimina e ricrea tutte le tabelle gen, Attenzione! I dati presenti verranno eliminati!"
FLAGS "H"
END END
BOOLEAN CHK_CREATETABLES BOOLEAN CHK_CREATETABLES
@ -40,6 +41,7 @@ END
BOOLEAN CHK_EXPORTGEN BOOLEAN CHK_EXPORTGEN
BEGIN BEGIN
PROMPT 1 7 "Esporta tabelle gen" PROMPT 1 7 "Esporta tabelle gen"
FLAGS "H"
END END
BOOLEAN CHK_EXPORTTABLES BOOLEAN CHK_EXPORTTABLES
@ -47,6 +49,29 @@ BEGIN
PROMPT 1 8 "Esporta tabelle dati" PROMPT 1 8 "Esporta tabelle dati"
END END
BOOLEAN CHK_ONLYTAB
BEGIN
PROMPT 1 9 "Esporta\crea tabelle in un intervallo"
END
NUMBER F_FROMTAB 4 0
BEGIN
PROMPT 1 10 "Da "
FLAGS "D"
END
NUMBER F_TOTAB 3 0
BEGIN
PROMPT 10 10 "A "
FLAGS "D"
END
STRING F_MYRANGE 50
BEGIN
PROMPT 1 11 "MY RANGE "
FLAGS "H"
END
END END
ENDPAGE ENDPAGE