Patch level : 12.0 766
Files correlati : ba2900 Commento : - Aggiunta esportazione archivi campo su MSSQLSERVER - [Da implementrare] esportazione dirgen
This commit is contained in:
parent
1d6b7a426e
commit
6364ac9540
@ -118,7 +118,7 @@
|
||||
</Midl>
|
||||
<ClCompile>
|
||||
<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>
|
||||
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
|
||||
<BrowseInformation>false</BrowseInformation>
|
||||
|
@ -8,9 +8,32 @@
|
||||
#include <real.h>
|
||||
|
||||
#include "ba2900.h"
|
||||
#include "../xvtdb/xvtdb.h"
|
||||
//#include "tsdb.h"
|
||||
#include "codeb.h"
|
||||
|
||||
#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
|
||||
{
|
||||
protected:
|
||||
@ -18,6 +41,7 @@ protected:
|
||||
virtual bool on_field_event(TOperable_field& o, TField_event e, long jolly);
|
||||
public:
|
||||
TMSSQLExport_msk();
|
||||
~TMSSQLExport_msk();
|
||||
};
|
||||
|
||||
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)
|
||||
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:
|
||||
check_range_tab(*this);
|
||||
break;
|
||||
}
|
||||
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_USR, ini_get_string(CONFIG_DITTA, "Campo_MSSQL_Export", "User"));
|
||||
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_EXPORTGEN, "X");
|
||||
//set(CHK_EXPORTGEN, "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
|
||||
{
|
||||
protected:
|
||||
TToken_string tables;
|
||||
TString DSN, usr, psw;
|
||||
TToken_string _tables;
|
||||
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 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
|
||||
//*****************************************************************
|
||||
bool emptyTables() const;
|
||||
bool exportGen() const;
|
||||
bool exportTables() const;
|
||||
bool createGen() const;
|
||||
bool createTables() const;
|
||||
bool createIndexes() const;
|
||||
bool empty_tables() const;
|
||||
bool expor_gen() const;
|
||||
bool file_valid(int logicnum) const;
|
||||
bool export_tables() const;
|
||||
bool create_gen() 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
|
||||
bool dir_gen() const;
|
||||
bool trc_gen() const;
|
||||
bool exportManager(const TString generalErrors) const;
|
||||
bool export_manager(const TString generalErrors) const;
|
||||
|
||||
public:
|
||||
bool my_range();
|
||||
virtual void main_loop();
|
||||
void setTable(TToken_string s) { tables = s; }
|
||||
bool setParameters(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 testFieldSeq(int val, int arr[]) const;
|
||||
void set_table(TToken_string s) { _tables = s; }
|
||||
bool set_parameters(TString dsn, TString utente, TString password);
|
||||
bool connect(const TString& _DSN, const TString& _usr, const TString& _psw);
|
||||
//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 ****************************************************************************************************************************
|
||||
@ -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
|
||||
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", "Password", password);
|
||||
DSN = dsn;
|
||||
usr = utente;
|
||||
psw = password;
|
||||
_DSN = dsn;
|
||||
_usr = utente;
|
||||
_psw = password;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TMSSQLExport_app::emptyTables() const
|
||||
bool TMSSQLExport_app::empty_tables() const
|
||||
{
|
||||
TODBC_recordset sqlset("");
|
||||
TString table;
|
||||
|
||||
if (!sqlset.connect(DSN, usr, psw))
|
||||
if (!sqlset.connect(_DSN, _usr, _psw))
|
||||
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
|
||||
|
||||
tables.get(i, table);
|
||||
_tables.get(i, table);
|
||||
theQuery << "DELETE FROM "<< table << ";";
|
||||
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
|
||||
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++)
|
||||
if(val == arr[i] || val < arr[i]) return false;
|
||||
@ -179,12 +237,8 @@ bool TMSSQLExport_app::testFieldSeq(int val, int arr[]) const
|
||||
}
|
||||
|
||||
// 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
|
||||
|
||||
// Prima le tabelle gen
|
||||
@ -200,7 +254,7 @@ bool TMSSQLExport_app::createGen() const
|
||||
gensql << aganaye;
|
||||
}
|
||||
queryFile.close();
|
||||
if(sqlset.exec(gensql) != 1) // EXEC
|
||||
if(!_db->sq_exec(gensql)) // EXEC
|
||||
return false;
|
||||
}
|
||||
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
|
||||
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("");
|
||||
// Controllo la connessione di nuovo per essere scrupolosi
|
||||
if (!sqlset.connect(DSN, usr, psw))
|
||||
return false;
|
||||
ifstream queryFile; // stream dei files con le query
|
||||
|
||||
// Le tabelle comuni
|
||||
TFilename studio("sql\\studiosql.sql");
|
||||
queryFile.open(studio);
|
||||
if(queryFile.is_open())
|
||||
const TString& table = logic2table(logicnum);
|
||||
const bool studio_tab = TDir(logicnum).is_com();
|
||||
return TString("[") << (!studio_tab ? ditta_name : "") << table << "]";
|
||||
}
|
||||
|
||||
void TMSSQLExport_app::set_range_tab(int& logicnum, int& endtab) const
|
||||
{
|
||||
logicnum = 2;
|
||||
endtab = prefix().items();
|
||||
if (_msk->get_bool(CHK_ONLYTAB))
|
||||
{
|
||||
TString studiosql; // Qua metterò le query
|
||||
while(!queryFile.eof())
|
||||
{
|
||||
char aganaye[10000];
|
||||
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;
|
||||
check_range_tab(*_msk);
|
||||
logicnum = _msk->get_int(F_FROMTAB);
|
||||
endtab = _msk->get_int(F_TOTAB);
|
||||
endtab++;
|
||||
}
|
||||
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;
|
||||
message_box(msg);
|
||||
return false;
|
||||
return _db->sq_connect(_DSN, _usr, _psw, TSDB_MSSQL) == NOERR;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/************************************************************************************************
|
||||
* Le tabelle di ditta, caso particolare: *
|
||||
* In questi file è presente una sequenza di escape (%%%%%) che andrà sostituita con la ditta, *
|
||||
* quindi prima leggo tutto e poi sostituisco *
|
||||
************************************************************************************************/
|
||||
TFilename ditta("sql\\dittesql.sql");
|
||||
TString appsql;
|
||||
queryFile.open(ditta);
|
||||
bool TMSSQLExport_app::create_tables() const
|
||||
{
|
||||
bool ok = true;
|
||||
TString ditta_name; ditta_name << prefix().name() << "_";
|
||||
TString last_col_name = " ";
|
||||
|
||||
// Creo il cursore delle ditte
|
||||
TRelation relTDitte(LF_NDITTE);
|
||||
TCursor curDitte(&relTDitte);
|
||||
int itemsDitte = curDitte.items();
|
||||
int logicnum;
|
||||
int endtab;
|
||||
set_range_tab(logicnum, endtab);
|
||||
|
||||
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
|
||||
appsql << aganaye;
|
||||
}
|
||||
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)
|
||||
|
||||
if (i > 0 && !last_err) sql << ",\n";
|
||||
column_name.cut(0) << "[" << rd.Fd[i].Name << "] ";
|
||||
if (column_name == last_col_name)
|
||||
{
|
||||
// Siccome so che ci sono 5 caratteri faccio che farlo a mano da vero gangster
|
||||
queryFirm[lastpos] = nomeDitta[0];
|
||||
queryFirm[lastpos + 1] = nomeDitta[1];
|
||||
queryFirm[lastpos + 2] = nomeDitta[2];
|
||||
queryFirm[lastpos + 3] = nomeDitta[3];
|
||||
queryFirm[lastpos + 4] = nomeDitta[4];
|
||||
lastpos = queryFirm.find("%%%%%", lastpos); // find(x, lastpos) si velocizza partendo già dalla ultima posizione trovata
|
||||
}
|
||||
if(sqlset.exec(queryFirm) != 1) // EXEC
|
||||
return false;
|
||||
last_err = true;
|
||||
continue;
|
||||
}
|
||||
last_err = false;
|
||||
last_col_name = column_name;
|
||||
sql << column_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;
|
||||
}
|
||||
}
|
||||
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
|
||||
{
|
||||
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();
|
||||
return ok;
|
||||
}
|
||||
|
||||
/*
|
||||
@ -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])
|
||||
*
|
||||
*/
|
||||
bool TMSSQLExport_app::createIndexes() const
|
||||
bool TMSSQLExport_app::create_indexes() const
|
||||
{
|
||||
TODBC_recordset sqlset("");
|
||||
if (!sqlset.connect(DSN, usr, psw))
|
||||
if (!sqlset.connect(_DSN, _usr, _psw))
|
||||
return "Connessione fallita!";
|
||||
int items = TABELLE_CAMPO; // Numero totale di tabelle
|
||||
COpenDir((int)_manulock, NORDIR); // Apro dir.gen
|
||||
@ -392,42 +457,166 @@ bool TMSSQLExport_app::createIndexes() const
|
||||
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();
|
||||
}
|
||||
|
||||
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
|
||||
if(!emptyTables()) return false;
|
||||
|
||||
TODBC_recordset sqlset("");
|
||||
if (!sqlset.connect(DSN, usr, psw))
|
||||
return "Connessione fallita!";
|
||||
try
|
||||
bool check = true;
|
||||
TString ditta_name; ditta_name << prefix().name() << "_";
|
||||
|
||||
int logicnum;
|
||||
int endtab;
|
||||
set_range_tab(logicnum, endtab);
|
||||
|
||||
for (; logicnum < endtab && check; logicnum++)
|
||||
{
|
||||
bool wat = false;
|
||||
// Inizio con dir.gen
|
||||
TString generalErrors;
|
||||
exportManager(generalErrors);
|
||||
TString nome_tab = tab_name(logicnum, ditta_name);
|
||||
const RecDes& rd = prefix().get_recdes(logicnum);
|
||||
TLocalisamfile table(logicnum);
|
||||
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)
|
||||
{
|
||||
message_box(orrore);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
return check;
|
||||
}
|
||||
|
||||
bool TMSSQLExport_app::dir_gen() const
|
||||
{
|
||||
TODBC_recordset sqlset("", true);
|
||||
// Controllo la connessione
|
||||
if (!sqlset.connect(DSN, usr, psw))
|
||||
return false;
|
||||
int items = TABELLE_CAMPO; // Numero totale di tabelle
|
||||
TString msg("Esportazione elenco directory");
|
||||
const int items = prefix().items()-1; // Numero totale di tabelle
|
||||
const TString msg("Esportazione elenco directory");
|
||||
COpenDir((int)_manulock, NORDIR); // Apro dir.gen
|
||||
TProgress_monitor p(items, msg);
|
||||
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.FCalc) << "','"
|
||||
<< toEscape(d.GenPrompt) << "')";
|
||||
if(sqlset.exec(queryToNull(sqlQuery)) != 1)
|
||||
if(!_db->sq_exec(queryToNull(sqlQuery)))
|
||||
return false;
|
||||
}
|
||||
return sqlset.commit() == -1 ? false : true;
|
||||
return _db->sq_commit();
|
||||
}
|
||||
|
||||
bool TMSSQLExport_app::trc_gen() const
|
||||
{
|
||||
TODBC_recordset sqlset("", true);
|
||||
// Controllo la connessione
|
||||
if (!sqlset.connect(DSN, usr, psw))
|
||||
return false;
|
||||
int items = TABELLE_CAMPO; // Numero totale di tabelle
|
||||
TString msg("Esportazione tracciati record");
|
||||
const int items = prefix().items()-1; // Numero totale di tabelle
|
||||
const TString msg("Esportazione tracciati record");
|
||||
COpenDir((int)_manulock, NORDIR); // Apro dir.gen
|
||||
TProgress_monitor p(items-1, msg);
|
||||
for (int i = 2; i <= items && !p.is_cancelled(); i++)
|
||||
@ -487,7 +672,7 @@ bool TMSSQLExport_app::trc_gen() const
|
||||
<< SortFd << "','"
|
||||
<< d.NKeys << "','"
|
||||
<< i << "')";
|
||||
if(sqlset.exec(queryToNull(sqlQueryTrc)) != 1)
|
||||
if(!_db->sq_exec(queryToNull(sqlQueryTrc)))
|
||||
return false;
|
||||
|
||||
// Poi trc_recfdes
|
||||
@ -504,7 +689,7 @@ bool TMSSQLExport_app::trc_gen() const
|
||||
<< r.Dec << "','"
|
||||
<< r.RecOff << "','"
|
||||
<< i << "')";
|
||||
if(sqlset.exec(queryToNull(sqlQueryRFD)) != 1)
|
||||
if(!_db->sq_exec(queryToNull(sqlQueryRFD)))
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -540,18 +725,18 @@ bool TMSSQLExport_app::trc_gen() const
|
||||
<< FromCh << "','"
|
||||
<< ToCh << "','"
|
||||
<< i << "')";
|
||||
if(sqlset.exec(queryToNull(sqlQueryKD)) != 1)
|
||||
if(!_db->sq_exec(queryToNull(sqlQueryKD)))
|
||||
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);
|
||||
// Controllo la connessione
|
||||
if (!sqlset.connect(DSN, usr, psw))
|
||||
if (!sqlset.connect(_DSN, _usr, _psw))
|
||||
return false;
|
||||
|
||||
COpenDir((int)_manulock, NORDIR); // Apro dir.gen
|
||||
@ -662,31 +847,70 @@ bool TMSSQLExport_app::exportManager(TString generalErrors) const
|
||||
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()
|
||||
{
|
||||
TMSSQLExport_msk m;
|
||||
while (m.run() == K_ENTER)
|
||||
TMSSQLExport_msk msk;
|
||||
_msk = &msk;
|
||||
|
||||
while (msk.run() == K_ENTER)
|
||||
{
|
||||
const TString& DSN = m.get(F_DSN);
|
||||
const TString& usr = m.get(F_USR);
|
||||
const TString& psw = m.get(F_PWD);
|
||||
#ifdef DBG
|
||||
const TString& _DSN = "TESTCAMPO2012\\MSSQLSERVER2019@campo13";
|
||||
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
|
||||
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!
|
||||
if(m.get_bool(CHK_CREATEGEN))
|
||||
if(!createGen())
|
||||
message_box("ERROR: Creazione tabelle gen fallita");
|
||||
if(m.get_bool(CHK_CREATETABLES))
|
||||
if(!createTables())
|
||||
message_box("ERROR: Creazione tabelle campo fallita");
|
||||
if(m.get_bool(CHK_EXPORTGEN))
|
||||
if (!exportGen())
|
||||
message_box("CERROR: Esportazione tabelle gen fallita");
|
||||
if(m.get_bool(CHK_EXPORTTABLES))
|
||||
if(!exportTables())
|
||||
message_box("ERROR: Esportazione tabelle campo fallita");
|
||||
message_box("Migrazione effettuata correttamente!");
|
||||
while (true)
|
||||
{
|
||||
bool loop = my_range();
|
||||
// Per comodità utilizzo gli AND short circuits, solo se i bool danno true eseguo la funzione dopo!
|
||||
if (msk.get_bool(CHK_CREATEGEN))
|
||||
if (!create_gen())
|
||||
message_box("ERROR: Creazione tabelle gen fallita");
|
||||
if (msk.get_bool(CHK_CREATETABLES))
|
||||
if (!create_tables())
|
||||
message_box("ERROR: Creazione tabelle campo fallita");
|
||||
if (msk.get_bool(CHK_EXPORTGEN))
|
||||
if (!expor_gen())
|
||||
message_box("CERROR: Esportazione tabelle gen fallita");
|
||||
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
|
||||
message_box("Fallita connessione");
|
||||
@ -698,7 +922,7 @@ int ba2900(int argc, char* argv[])
|
||||
{
|
||||
TMSSQLExport_app app;
|
||||
// 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"));
|
||||
return 0;
|
||||
}
|
||||
|
@ -5,3 +5,8 @@
|
||||
#define CHK_CREATETABLES 254
|
||||
#define CHK_EXPORTGEN 255
|
||||
#define CHK_EXPORTTABLES 256
|
||||
|
||||
#define CHK_ONLYTAB 257
|
||||
#define F_FROMTAB 258
|
||||
#define F_TOTAB 259
|
||||
#define F_MYRANGE 260
|
@ -10,7 +10,7 @@ PAGE "Trasferimento DB su MSSQL" 0 2 0 0
|
||||
|
||||
STRING F_DSN 260 50
|
||||
BEGIN
|
||||
PROMPT 1 0 "ODBC DSN "
|
||||
PROMPT 1 1 "ODBC DSN "
|
||||
CHECKTYPE REQUIRED
|
||||
END
|
||||
|
||||
@ -30,6 +30,7 @@ END
|
||||
BOOLEAN CHK_CREATEGEN
|
||||
BEGIN
|
||||
PROMPT 1 5 "Elimina e ricrea tutte le tabelle gen, Attenzione! I dati presenti verranno eliminati!"
|
||||
FLAGS "H"
|
||||
END
|
||||
|
||||
BOOLEAN CHK_CREATETABLES
|
||||
@ -40,6 +41,7 @@ END
|
||||
BOOLEAN CHK_EXPORTGEN
|
||||
BEGIN
|
||||
PROMPT 1 7 "Esporta tabelle gen"
|
||||
FLAGS "H"
|
||||
END
|
||||
|
||||
BOOLEAN CHK_EXPORTTABLES
|
||||
@ -47,6 +49,29 @@ BEGIN
|
||||
PROMPT 1 8 "Esporta tabelle dati"
|
||||
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
|
||||
|
||||
ENDPAGE
|
||||
|
Loading…
x
Reference in New Issue
Block a user