Patch level : 12.0 no-patch
Files correlati : ba2900 Commento : Creazione e esportazione tabelle gen
This commit is contained in:
parent
31375592f6
commit
be0d3050c8
@ -8,9 +8,10 @@
|
||||
#include <real.h>
|
||||
|
||||
#include "ba2900.h"
|
||||
#include "../xvtdb/xvtdb.h"
|
||||
#include <xvtdb.h>
|
||||
//#include "tsdb.h"
|
||||
#include "codeb.h"
|
||||
#include "reputils.h"
|
||||
|
||||
#define TABELLE_CAMPO 171
|
||||
|
||||
@ -69,8 +70,8 @@ bool TMSSQLExport_msk::on_field_event(TOperable_field& o, TField_event e, long j
|
||||
{
|
||||
get_bool(CHK_ONLYTAB) ? enable(F_FROMTAB) : disable(F_FROMTAB);
|
||||
get_bool(CHK_ONLYTAB) ? enable(F_TOTAB) : disable(F_TOTAB);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case CHK_EXPORTTABLES:
|
||||
if (e == fe_modify)
|
||||
{
|
||||
@ -111,15 +112,19 @@ protected:
|
||||
TString _DSN, _usr, _psw;
|
||||
SSimple_query* _db;
|
||||
TMSSQLExport_msk* _msk;
|
||||
TLog_report* _log;
|
||||
TString _logstr;
|
||||
|
||||
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 empty_tables() const;
|
||||
bool rko_gen() const;
|
||||
bool expor_gen() const;
|
||||
bool file_valid(int logicnum) const;
|
||||
bool export_tables() const;
|
||||
int export_tables();
|
||||
bool create_gen() const;
|
||||
static TString tab_name(int logicnum, TString& ditta_name);
|
||||
void set_range_tab(int& logicnum, int& endtab) const;
|
||||
@ -130,6 +135,8 @@ protected:
|
||||
bool dir_gen() const;
|
||||
bool trc_gen() const;
|
||||
bool export_manager(const TString generalErrors) const;
|
||||
bool show_log();
|
||||
void log(int severity, const char* msg);
|
||||
|
||||
public:
|
||||
bool my_range();
|
||||
@ -139,6 +146,7 @@ public:
|
||||
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;
|
||||
bool create_gen_ms() const;
|
||||
TMSSQLExport_app() : _visual(false) { }
|
||||
~TMSSQLExport_app() { delete _db; }
|
||||
};
|
||||
@ -228,6 +236,32 @@ bool TMSSQLExport_app::empty_tables() const
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TMSSQLExport_app::rko_gen() const
|
||||
{
|
||||
TString query;
|
||||
query <<
|
||||
"DROP TABLE [dir_gen];";
|
||||
if (!_db->sq_set_exec(query))
|
||||
return false;
|
||||
|
||||
query.cut(0) <<
|
||||
"DROP TABLE [trc_gen];";
|
||||
if (!_db->sq_set_exec(query))
|
||||
return false;
|
||||
|
||||
query.cut(0) <<
|
||||
"DROP TABLE [trc_keydes];";
|
||||
if (!_db->sq_set_exec(query))
|
||||
return false;
|
||||
|
||||
query.cut(0) <<
|
||||
"DROP TABLE [trc_recfdes];";
|
||||
if (!_db->sq_set_exec(query))
|
||||
return false;
|
||||
|
||||
return _db->sq_commit();
|
||||
}
|
||||
|
||||
// Controllo che il valore passato sia > e diverso da quelli presenti nell'array
|
||||
bool TMSSQLExport_app::test_field_seq(int val, int arr[]) const
|
||||
{
|
||||
@ -237,6 +271,78 @@ bool TMSSQLExport_app::test_field_seq(int val, int arr[]) const
|
||||
}
|
||||
|
||||
// Main Program *****************************************************************************************************************************************************************
|
||||
bool TMSSQLExport_app::create_gen_ms() const
|
||||
{
|
||||
|
||||
rko_gen();
|
||||
TString query;
|
||||
query <<
|
||||
"IF OBJECT_ID('dbo.dir_gen', 'U') IS NOT NULL DROP TABLE dbo.dir_gen;\n" <<
|
||||
"CREATE TABLE[dbo].[dir_gen](\n" <<
|
||||
"[_ID_][int] IDENTITY(1, 1) NOT NULL,\n" <<
|
||||
"[NUMERO][int] NOT NULL,\n" <<
|
||||
"[SYSNAME] VARCHAR(MAX) NOT NULL,\n" <<
|
||||
"[EOD][int] NULL,\n" <<
|
||||
"[EOX][int] NULL,\n" <<
|
||||
"[FLAGS][int] NULL,\n" <<
|
||||
"[LENR][int] NULL,\n" <<
|
||||
"[DES] VARCHAR(MAX) NULL,\n" <<
|
||||
"[CALC] VARCHAR(MAX) NULL,\n" <<
|
||||
"[GENPROMPT] VARCHAR(MAX) NULL,\n" <<
|
||||
"PRIMARY KEY CLUSTERED\n" <<
|
||||
"([_ID_] ASC) WITH\n" <<
|
||||
"(PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON[PRIMARY]\n" <<
|
||||
") ON[PRIMARY] TEXTIMAGE_ON[PRIMARY];";
|
||||
|
||||
if (!_db->sq_set_exec(query))
|
||||
return false;
|
||||
|
||||
query.cut(0) <<
|
||||
"IF OBJECT_ID('dbo.trc_gen', 'U') IS NOT NULL DROP TABLE dbo.trc_gen;\n" <<
|
||||
"CREATE TABLE[dbo].[trc_gen](\n" <<
|
||||
"[_ID_][int] IDENTITY(1, 1) NOT NULL,\n" <<
|
||||
"[NFields][int] NOT NULL,\n" <<
|
||||
"[SortFd] VARCHAR(MAX) NULL,\n" <<
|
||||
"[NKeys][int] NOT NULL,\n" <<
|
||||
"[logicname][int] NOT NULL\n" <<
|
||||
") ON[PRIMARY] TEXTIMAGE_ON[PRIMARY];";
|
||||
|
||||
if (!_db->sq_set_exec(query))
|
||||
return false;
|
||||
|
||||
query.cut(0) <<
|
||||
"IF OBJECT_ID('dbo.trc_keydes', 'U') IS NOT NULL DROP TABLE dbo.trc_keydes;\n" <<
|
||||
"CREATE TABLE[dbo].[trc_keydes](\n" <<
|
||||
"[_ID_][int] IDENTITY(1, 1) NOT NULL,\n" <<
|
||||
"[DupKeys][int] NOT NULL,\n" <<
|
||||
"[NkFields][int] NOT NULL,\n" <<
|
||||
"[FieldSeq] VARCHAR(MAX) NULL,\n" <<
|
||||
"[FromCh] VARCHAR(MAX) NULL,\n" <<
|
||||
"[ToCh] VARCHAR(MAX) NULL,\n" <<
|
||||
"[logicname][int] NOT NULL\n" <<
|
||||
") ON[PRIMARY] TEXTIMAGE_ON[PRIMARY];";
|
||||
|
||||
if (!_db->sq_set_exec(query))
|
||||
return false;
|
||||
|
||||
query.cut(0) <<
|
||||
"IF OBJECT_ID('dbo.trc_recfdes', 'U') IS NOT NULL DROP TABLE dbo.trc_recfdes;\n" <<
|
||||
"CREATE TABLE[dbo].[trc_recfdes](\n" <<
|
||||
"[_ID_][int] IDENTITY(1, 1) NOT NULL,\n" <<
|
||||
"[Name] VARCHAR(MAX) NOT NULL,\n" <<
|
||||
"[TypeF][int] NOT NULL,\n" <<
|
||||
"[Len][int] NULL,\n" <<
|
||||
"[Dec][int] NULL,\n" <<
|
||||
"[RecOff][int] NULL,\n" <<
|
||||
"[logicname][int] NOT NULL\n" <<
|
||||
") ON[PRIMARY] TEXTIMAGE_ON[PRIMARY];";
|
||||
|
||||
if (!_db->sq_set_exec(query))
|
||||
return false;
|
||||
|
||||
return _db->sq_commit();
|
||||
|
||||
}
|
||||
bool TMSSQLExport_app::create_gen() const
|
||||
{
|
||||
ifstream queryFile; // stream dei files con le query
|
||||
@ -292,12 +398,13 @@ bool TMSSQLExport_app::connect(const TString& _DSN, const TString& _usr, const T
|
||||
{
|
||||
if (_db == nullptr)
|
||||
_db = new SSimple_query();
|
||||
|
||||
if (!_db->sq_is_connect())
|
||||
bool connected = _db->sq_is_connect();
|
||||
if (!connected)
|
||||
{
|
||||
return _db->sq_connect(_DSN, _usr, _psw, TSDB_MSSQL) == NOERR;
|
||||
_db->sq_set_con_option("UseAPI", "OLEDB");
|
||||
connected = _db->sq_connect(_DSN, _usr, _psw, TSDB_MSSQL) == NOERR;
|
||||
}
|
||||
return true;
|
||||
return connected;
|
||||
}
|
||||
|
||||
bool TMSSQLExport_app::create_tables() const
|
||||
@ -323,6 +430,7 @@ bool TMSSQLExport_app::create_tables() const
|
||||
if (rd.NFields == 0)
|
||||
continue;
|
||||
TString sql; sql.cut(0) << "CREATE TABLE " << table_name << "(\n";
|
||||
sql << "[_ID_][int] IDENTITY(1, 1) NOT NULL,\n";
|
||||
for (int i = 0; i < rd.NFields; i++)
|
||||
{
|
||||
|
||||
@ -459,13 +567,13 @@ bool TMSSQLExport_app::create_indexes() 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;
|
||||
_db->sq_set_exec(TString().cut(0) << "DROP TABLE " << table_name << ';');
|
||||
return _db->sq_commit();
|
||||
}
|
||||
|
||||
bool TMSSQLExport_app::expor_gen() const
|
||||
{
|
||||
return dir_gen() && trc_gen();
|
||||
return rko_gen() && create_gen_ms() && dir_gen() && trc_gen();
|
||||
}
|
||||
|
||||
bool TMSSQLExport_app::file_valid(int logicnum) const
|
||||
@ -520,7 +628,7 @@ const TString query_to_null(TString& val)
|
||||
return val;
|
||||
}
|
||||
|
||||
bool TMSSQLExport_app::export_tables() const
|
||||
int TMSSQLExport_app::export_tables()
|
||||
{
|
||||
// Innanzitutto svuoto le tabelle
|
||||
|
||||
@ -530,6 +638,7 @@ bool TMSSQLExport_app::export_tables() const
|
||||
int logicnum;
|
||||
int endtab;
|
||||
set_range_tab(logicnum, endtab);
|
||||
int last_tab_err = -1;
|
||||
|
||||
for (; logicnum < endtab && check; logicnum++)
|
||||
{
|
||||
@ -541,7 +650,7 @@ bool TMSSQLExport_app::export_tables() const
|
||||
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;
|
||||
@ -549,7 +658,7 @@ bool TMSSQLExport_app::export_tables() const
|
||||
for (; ok && export_status; ok = table.next() == NOERR)
|
||||
{
|
||||
if (_visual && !p.add_status())
|
||||
return false;
|
||||
return logicnum;
|
||||
queryF.cut(0) << "INSERT INTO " << nome_tab << " (";
|
||||
queryV.cut(0) << "(";
|
||||
for (int k = 0; k < rd.NFields; k++)
|
||||
@ -606,11 +715,23 @@ bool TMSSQLExport_app::export_tables() const
|
||||
|
||||
queryV << ")";
|
||||
queryF << ") VALUES " << queryV;
|
||||
export_status &= _db->sq_set_exec(query_to_null(queryF));
|
||||
//export_status &= _db->sq_set_exec(query_to_null(queryF));
|
||||
if(!_db->sq_set_exec(query_to_null(queryF), false))
|
||||
{
|
||||
if (last_tab_err != logicnum)
|
||||
{
|
||||
log(2, TString("Errore esportazione tabella ") << logicnum << "\n\tQuery:" << queryF);
|
||||
last_tab_err = logicnum;
|
||||
}
|
||||
else
|
||||
log(2, TString("\tQuery: ") << queryF);
|
||||
log(2, _db->sq_get_string_error());
|
||||
log(2, _db->sq_get_text_error());
|
||||
}
|
||||
}
|
||||
check = export_status && _db->sq_commit();
|
||||
}
|
||||
return check;
|
||||
return check ? 0 : -1;
|
||||
}
|
||||
|
||||
bool TMSSQLExport_app::dir_gen() const
|
||||
@ -627,17 +748,17 @@ bool TMSSQLExport_app::dir_gen() const
|
||||
CGetFile(i, &d, _nolock, NORDIR);
|
||||
TString sqlQuery;
|
||||
sqlQuery
|
||||
<< "INSERT INTO DIR_GEN (NUMERO,SYSNAME,EOD,EOX,FLAGS,LENR,DES,CALC,GENPROMPT) VALUES ('"
|
||||
<< "INSERT INTO DIR_GEN (NUMERO,SYSNAME,EOD,EOX,LENR,FLAGS,DES,CALC,GENPROMPT) VALUES ('"
|
||||
<< i << "','"
|
||||
<< d.SysName << "','"
|
||||
<< d.EOD << "','"
|
||||
<< d.EOX << "','"
|
||||
<< d.Flags << "','"
|
||||
<< d.LenR << "','"
|
||||
<< d.Flags << "','"
|
||||
<< toEscape(d.Des) << "','"
|
||||
<< toEscape(d.FCalc) << "','"
|
||||
<< toEscape(d.GenPrompt) << "')";
|
||||
if(!_db->sq_exec(queryToNull(sqlQuery)))
|
||||
if(!_db->sq_set_exec(queryToNull(sqlQuery), false))
|
||||
return false;
|
||||
}
|
||||
return _db->sq_commit();
|
||||
@ -672,7 +793,7 @@ bool TMSSQLExport_app::trc_gen() const
|
||||
<< SortFd << "','"
|
||||
<< d.NKeys << "','"
|
||||
<< i << "')";
|
||||
if(!_db->sq_exec(queryToNull(sqlQueryTrc)))
|
||||
if(!_db->sq_set_exec(queryToNull(sqlQueryTrc), false))
|
||||
return false;
|
||||
|
||||
// Poi trc_recfdes
|
||||
@ -681,15 +802,14 @@ bool TMSSQLExport_app::trc_gen() const
|
||||
RecFieldDes r = d.Fd[j];
|
||||
TString sqlQueryRFD;
|
||||
sqlQueryRFD
|
||||
<< "INSERT INTO TRC_RECFDES (NumField, Name,TypeF,Len,Dec,RecOff,logicname) VALUES ('"
|
||||
<< j << "','"
|
||||
<< "INSERT INTO TRC_RECFDES (Name,TypeF,Len,Dec,RecOff,logicname) VALUES ('"
|
||||
<< r.Name << "','"
|
||||
<< (int)r.TypeF << "','"
|
||||
<< r.Len << "','"
|
||||
<< r.Dec << "','"
|
||||
<< r.RecOff << "','"
|
||||
<< i << "')";
|
||||
if(!_db->sq_exec(queryToNull(sqlQueryRFD)))
|
||||
if(!_db->sq_set_exec(queryToNull(sqlQueryRFD), false))
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -725,7 +845,7 @@ bool TMSSQLExport_app::trc_gen() const
|
||||
<< FromCh << "','"
|
||||
<< ToCh << "','"
|
||||
<< i << "')";
|
||||
if(!_db->sq_exec(queryToNull(sqlQueryKD)))
|
||||
if(!_db->sq_set_exec(queryToNull(sqlQueryKD), false))
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@ -847,6 +967,49 @@ bool TMSSQLExport_app::export_manager(TString generalErrors) const
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TMSSQLExport_app::show_log()
|
||||
{
|
||||
if (_log)
|
||||
{
|
||||
_log->preview();
|
||||
delete _log;
|
||||
_log = NULL;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void TMSSQLExport_app::log(int severity, const char* msg)
|
||||
{
|
||||
if (_log == nullptr)
|
||||
{
|
||||
_log = new TLog_report;
|
||||
// Tento l'eliminazione del file
|
||||
std::remove("ba_export.log");
|
||||
}
|
||||
if (severity < 0)
|
||||
{
|
||||
_logstr = msg;
|
||||
}
|
||||
else
|
||||
{
|
||||
static TString txt;
|
||||
txt.cut(0);
|
||||
if (_logstr.full())
|
||||
{
|
||||
txt << _logstr << ": " << msg;
|
||||
}
|
||||
else
|
||||
txt << msg;
|
||||
_log->log(severity, txt);
|
||||
// Scrivo anche su file
|
||||
std::filebuf fb;
|
||||
fb.open("ba_export.log", std::ios::out | std::ios::app);
|
||||
std::ostream os(&fb);
|
||||
os << txt << std::endl;
|
||||
fb.close();
|
||||
}
|
||||
}
|
||||
|
||||
bool TMSSQLExport_app::my_range()
|
||||
{
|
||||
TString myrange(_msk->get(F_MYRANGE));
|
||||
@ -891,12 +1054,13 @@ void TMSSQLExport_app::main_loop()
|
||||
// Chiamo la funzione globale esporta
|
||||
if(connect(_DSN, _usr, _psw))
|
||||
{
|
||||
while (true)
|
||||
bool loop;
|
||||
do
|
||||
{
|
||||
bool loop = my_range();
|
||||
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())
|
||||
if (!create_gen_ms())
|
||||
message_box("ERROR: Creazione tabelle gen fallita");
|
||||
if (msk.get_bool(CHK_CREATETABLES))
|
||||
if (!create_tables())
|
||||
@ -905,12 +1069,13 @@ void TMSSQLExport_app::main_loop()
|
||||
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");
|
||||
{
|
||||
int err;
|
||||
if ((err = export_tables()))
|
||||
message_box(TString("ERROR: Esportazione tabelle campo fallita") << (err >= 2? TString(":\n Errorre tabella ") << err : ""));
|
||||
}
|
||||
message_box("Migrazione effettuata correttamente!");
|
||||
if (!loop)
|
||||
break;
|
||||
}
|
||||
} while (!loop);
|
||||
}
|
||||
else
|
||||
message_box("Fallita connessione");
|
||||
|
@ -30,7 +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"
|
||||
FLAGS ""
|
||||
END
|
||||
|
||||
BOOLEAN CHK_CREATETABLES
|
||||
@ -41,7 +41,7 @@ END
|
||||
BOOLEAN CHK_EXPORTGEN
|
||||
BEGIN
|
||||
PROMPT 1 7 "Esporta tabelle gen"
|
||||
FLAGS "H"
|
||||
FLAGS ""
|
||||
END
|
||||
|
||||
BOOLEAN CHK_EXPORTTABLES
|
||||
@ -51,7 +51,7 @@ END
|
||||
|
||||
BOOLEAN CHK_ONLYTAB
|
||||
BEGIN
|
||||
PROMPT 1 9 "Esporta\crea tabelle in un intervallo"
|
||||
PROMPT 1 9 "Esporta/crea tabelle in un intervallo"
|
||||
END
|
||||
|
||||
NUMBER F_FROMTAB 4 0
|
||||
|
Loading…
x
Reference in New Issue
Block a user