14 #define CATCH(returnValue) catch (wxDatabaseException& e) {\
15 errorCode = e.GetErrorCode();\
16 wxLogError ( "[%d] %s", errorCode, e.GetErrorMessage() );\
27 wxASSERT_MSG (
slaveDB ==
nullptr,
"There can be only one instance of yatishDBmysql.");
31 if (
slaveDB->GetTypeName() !=
"MYSQL")
32 throw ( wxDatabaseException (-4, _(
"Slave DB must be MySQL") ) );
34 throw ( wxDatabaseException (-5, _(
"Yatish tables not created (MySQL)") ) );
36 catch (wxDatabaseException& e) {
37 wxLogError (
"[%d] %s", e.GetErrorCode(), e.GetErrorMessage() );
52 if (
slaveDB->TableExists (
"yatish_client") )
53 slaveDB->RunQuery (
"DELETE FROM yatish_client;");
55 slaveDB->RunQuery (
"CREATE TABLE yatish_client ("
56 "id INT NOT NULL AUTO_INCREMENT,"
57 "name VARCHAR (20) NOT NULL UNIQUE,"
58 "PRIMARY KEY (id));");
59 if (
slaveDB->TableExists (
"yatish_project") )
60 slaveDB->RunQuery (
"DELETE FROM yatish_project;");
62 slaveDB->RunQuery (
"CREATE TABLE yatish_project ("
63 "id INT NOT NULL AUTO_INCREMENT,"
64 "name VARCHAR (20) NOT NULL UNIQUE,"
65 "client_id INT NOT NULL,"
67 "FOREIGN KEY (client_id) REFERENCES yatish_client (id)"
68 "ON DELETE CASCADE ON UPDATE CASCADE);");
69 if (
slaveDB->TableExists (
"yatish_task") )
70 slaveDB->RunQuery (
"DELETE FROM yatish_task;");
72 slaveDB->RunQuery (
"CREATE TABLE yatish_task ("
73 "id INT NOT NULL AUTO_INCREMENT,"
74 "name VARCHAR (20) NOT NULL UNIQUE,"
75 "PRIMARY KEY (id));");
76 if (
slaveDB->TableExists (
"yatish_tool") )
77 slaveDB->RunQuery (
"DELETE FROM yatish_tool;");
79 slaveDB->RunQuery (
"CREATE TABLE yatish_tool ("
80 "id INT NOT NULL AUTO_INCREMENT,"
81 "name VARCHAR (20) NOT NULL UNIQUE,"
82 "PRIMARY KEY (id));");
83 if (
slaveDB->TableExists (
"yatish_activity") )
84 slaveDB->RunQuery (
"DELETE FROM yatish_activity;");
86 slaveDB->RunQuery (
"CREATE TABLE yatish_activity ("
87 "id INT NOT NULL AUTO_INCREMENT,"
88 "project_id INT NOT NULL,"
89 "task_id INT NOT NULL,"
90 "tool_id INT NOT NULL,"
92 "UNIQUE (project_id,task_id,tool_id),"
93 "FOREIGN KEY (project_id) REFERENCES yatish_project (id)"
94 "ON DELETE CASCADE ON UPDATE CASCADE,"
95 "FOREIGN KEY (task_id) REFERENCES yatish_task (id)"
96 "ON DELETE CASCADE ON UPDATE CASCADE,"
97 "FOREIGN KEY (tool_id) REFERENCES yatish_tool (id)"
98 "ON DELETE CASCADE ON UPDATE CASCADE);");
99 if (
slaveDB->TableExists (
"yatish_timeslot") )
100 slaveDB->RunQuery (
"DELETE FROM yatish_timeslot;");
102 slaveDB->RunQuery (
"CREATE TABLE yatish_timeslot ("
103 "id INT NOT NULL AUTO_INCREMENT,"
106 "activity_id INT NOT NULL,"
108 "FOREIGN KEY (activity_id) REFERENCES yatish_activity (id)"
109 "ON DELETE CASCADE ON UPDATE CASCADE);");
164 slaveDB->RunQuery (
"DELETE FROM yatish_client;");
165 slaveDB->RunQuery (
"DELETE FROM yatish_project;");
166 slaveDB->RunQuery (
"DELETE FROM yatish_task;");
167 slaveDB->RunQuery (
"DELETE FROM yatish_tool;");
168 slaveDB->RunQuery (
"DELETE FROM yatish_activity;");
169 slaveDB->RunQuery (
"DELETE FROM yatish_timeslot;");
183 masterDB->RunQuery (
"DELETE FROM yatish_client;");
184 masterDB->RunQuery (
"DELETE FROM yatish_project;");
185 masterDB->RunQuery (
"DELETE FROM yatish_task;");
186 masterDB->RunQuery (
"DELETE FROM yatish_tool;");
187 masterDB->RunQuery (
"DELETE FROM yatish_activity;");
188 masterDB->RunQuery (
"DELETE FROM yatish_timeslot;");
202 masterDB->RunQuery (
"UPDATE yatish_client SET sync = 'I';");
203 masterDB->RunQuery (
"UPDATE yatish_project SET sync = 'I';");
204 masterDB->RunQuery (
"UPDATE yatish_task SET sync = 'I';");
205 masterDB->RunQuery (
"UPDATE yatish_tool SET sync = 'I';");
206 masterDB->RunQuery (
"UPDATE yatish_activity SET sync = 'I';");
207 masterDB->RunQuery (
"UPDATE yatish_timeslot SET sync = 'I';");
223 for (
int i = 0; i < 6; i++) {
226 sql.Printf(
"SELECT id FROM yatish_%s WHERE sync = 'D';", n);
227 wxDatabaseResultSet * results =
masterDB->RunQueryWithResults (sql);
228 while ( results->Next() ) {
229 id = results->GetResultLong (1);
230 sql.Printf (
"DELETE FROM yatish_%s WHERE id = %ld;", n,
id);
234 sql.Printf(
"DELETE FROM yatish_%s WHERE sync = 'D';", n);
254 for (
int i = 0; i < 3; i++) {
257 sql.Printf(
"SELECT id,name FROM yatish_%s WHERE sync = 'I';", n);
258 wxDatabaseResultSet * results =
masterDB->RunQueryWithResults (sql);
259 while ( results->Next() ) {
260 id = results->GetResultLong (1);
261 name = results->GetResultString (2);
262 sql.Printf (
"INSERT INTO yatish_%s (id,name) VALUES (%ld,'%s');", n,
id, name);
266 sql.Printf(
"UPDATE yatish_%s SET sync='S' WHERE sync = 'I';", n);
286 for (
int i = 0; i < 3; i++) {
290 sql.Printf(
"SELECT id,name FROM yatish_%s;", n);
291 wxDatabaseResultSet * results =
slaveDB->RunQueryWithResults (sql);
292 while ( results->Next() ) {
293 id = results->GetResultLong (1);
294 name = results->GetResultString (2);
295 sql.Printf (
"INSERT INTO yatish_%s (id,name,sync) VALUES (%ld,'%s','S');", n,
id, name);
298 slaveDB->CloseResultSet (results);
318 for (
int i = 0; i < 3; i++) {
321 sql.Printf(
"SELECT id,name FROM yatish_%s WHERE sync = 'U';", n);
322 wxDatabaseResultSet * results =
masterDB->RunQueryWithResults (sql);
323 while ( results->Next() ) {
324 id = results->GetResultLong (1);
325 name = results->GetResultString (2);
326 sql.Printf (
"UPDATE yatish_%s SET name='%s' WHERE id = %ld;", n, name,
id);
330 sql.Printf (
"UPDATE yatish_%s SET sync='S' WHERE sync = 'U';", n);
348 sql.Printf(
"SELECT id,name,client_id FROM yatish_project WHERE sync = 'I';");
349 wxDatabaseResultSet * results =
masterDB->RunQueryWithResults (sql);
350 while ( results->Next() ) {
351 id = results->GetResultLong (1);
352 name = results->GetResultString (2);
353 client_id = results->GetResultLong (3);
354 sql.Printf (
"INSERT INTO yatish_project (id,name,client_id)"
355 " VALUES (%ld,'%s',%ld);",
id, name, client_id);
359 sql.Printf (
"UPDATE yatish_project SET sync='S' WHERE sync = 'I';");
377 sql.Printf(
"SELECT id,name,client_id FROM yatish_project;");
378 wxDatabaseResultSet * results =
slaveDB->RunQueryWithResults (sql);
379 while ( results->Next() ) {
380 id = results->GetResultLong (1);
381 name = results->GetResultString (2);
382 client_id = results->GetResultLong (3);
383 sql.Printf (
"INSERT INTO yatish_project (id,name,client_id,sync)"
384 " VALUES (%ld,'%s',%ld,'S');",
id, name, client_id);
387 slaveDB->CloseResultSet (results);
404 sql.Printf(
"SELECT id,name,client_id FROM yatish_project WHERE sync = 'U';");
405 wxDatabaseResultSet * results =
masterDB->RunQueryWithResults (sql);
406 while ( results->Next() ) {
407 id = results->GetResultLong (1);
408 name = results->GetResultString (2);
409 client_id = results->GetResultLong (3);
410 sql.Printf (
"UPDATE yatish_project SET name='%s',client_id=%ld"
411 " WHERE id = %ld;", name, client_id,
id);
415 sql.Printf (
"UPDATE yatish_project SET sync='S' WHERE sync = 'U';");
430 long id1, id2, id3, id4;
432 sql.Printf(
"SELECT id,project_id,task_id,tool_id FROM yatish_activity WHERE sync = 'I';");
433 wxDatabaseResultSet * results =
masterDB->RunQueryWithResults (sql);
434 while ( results->Next() ) {
435 id1 = results->GetResultLong (1);
436 id2 = results->GetResultLong (2);
437 id3 = results->GetResultLong (3);
438 id4 = results->GetResultLong (4);
439 sql.Printf (
"INSERT INTO yatish_activity (id,project_id,task_id,tool_id)"
440 " VALUES (%ld,%ld,%ld,%ld);", id1, id2, id3, id4);
444 sql.Printf (
"UPDATE yatish_activity SET sync='S' WHERE sync = 'I';");
459 long id1, id2, id3, id4;
462 sql.Printf(
"SELECT id,project_id,task_id,tool_id FROM yatish_activity;");
463 wxDatabaseResultSet * results =
slaveDB->RunQueryWithResults (sql);
464 while ( results->Next() ) {
465 id1 = results->GetResultLong (1);
466 id2 = results->GetResultLong (2);
467 id3 = results->GetResultLong (3);
468 id4 = results->GetResultLong (4);
469 sql.Printf (
"INSERT INTO yatish_activity (id,project_id,task_id,tool_id,sync)"
470 " VALUES (%ld,%ld,%ld,%ld,'S');", id1, id2, id3, id4);
473 slaveDB->CloseResultSet (results);
488 long id1, id2, id3, id4;
490 sql.Printf(
"SELECT id,project_id,task_id,tool_id FROM yatish_activity WHERE sync = 'U';");
491 wxDatabaseResultSet * results =
masterDB->RunQueryWithResults (sql);
492 while ( results->Next() ) {
493 id1 = results->GetResultLong (1);
494 id2 = results->GetResultLong (2);
495 id3 = results->GetResultLong (3);
496 id4 = results->GetResultLong (4);
497 sql.Printf (
"UPDATE yatish_activity SET project_id=%ld,task_id=%ld,tool_id=%ld"
498 " WHERE id = %ld;", id2, id3, id4, id1);
502 sql.Printf (
"UPDATE yatish_activity SET sync='S' WHERE sync = 'U';");
516 wxString sql, start, stop;
517 long id, activity_id;
519 sql.Printf(
"SELECT id,start,stop,activity_id FROM yatish_timeslot WHERE sync = 'I';");
523 wxDatabaseResultSet * results =
masterDB->RunQueryWithResults (sql);
524 wxPreparedStatement * prepareSlave =
slaveDB->PrepareStatement (
525 "INSERT INTO yatish_timeslot (id,start,stop,activity_id) VALUES (?,?,?,?);" );
526 while ( results->Next() ) {
527 id = results->GetResultLong (1);
528 start = results->GetResultString (2);
529 stop = results->GetResultString (3);
530 activity_id = results->GetResultLong (4);
531 prepareSlave->SetParamInt (1,
id);
532 prepareSlave->SetParamString (2, start);
533 prepareSlave->SetParamString (3, stop);
534 prepareSlave->SetParamInt (4, activity_id);
535 prepareSlave->RunQuery();
537 slaveDB->CloseStatement (prepareSlave);
539 sql.Printf (
"UPDATE yatish_timeslot SET sync='S' WHERE sync='I';");
553 wxString sql, start, stop;
554 long id, activity_id;
557 sql.Printf(
"SELECT id,start,stop,activity_id FROM yatish_timeslot;");
558 wxDatabaseResultSet * results =
slaveDB->RunQueryWithResults (sql);
559 while ( results->Next() ) {
560 id = results->GetResultLong (1);
561 start = results->GetResultDate (2) .FormatISOCombined (
' ');
562 stop = results->GetResultDate (3) .FormatISOCombined (
' ');
563 activity_id = results->GetResultLong (4);
564 sql.Printf (
"INSERT INTO yatish_timeslot (id,start,stop,activity_id,sync)"
565 " VALUES (%ld,'%s','%s',%ld,'S');",
id, start, stop, activity_id);
568 slaveDB->CloseResultSet (results);
582 wxString sql, start, stop;
583 long id, activity_id;
585 sql.Printf(
"SELECT id,start,stop,activity_id FROM yatish_timeslot WHERE sync = 'U';");
586 wxDatabaseResultSet * results =
masterDB->RunQueryWithResults (sql);
587 while ( results->Next() ) {
588 id = results->GetResultLong (1);
589 start = results->GetResultString (2);
590 stop = results->GetResultString (3);
591 activity_id = results->GetResultLong (4);
592 sql.Printf (
"UPDATE yatish_timeslot SET start='%s',stop='%s',activity_id=%ld"
593 " WHERE id = %ld;", start, stop, activity_id,
id);
597 sql.Printf (
"UPDATE yatish_timeslot SET sync='S' WHERE sync = 'U';");
static wxDatabase * masterDB
bool TablesOk(wxDatabase *)
Returns false if any of the yatish table is missings.
static const char * tableName[]
Must be defined in the same order as enum tableID.
wxDatabase * GetDatabase(const wxString &)
Sets up a (configuration) flux using its argument then calls wxDatabase::GetDatabase().
int Download()
Downloads slaveDB into masterDB.
bool UpdateTimeslot()
Takes care of UPDATEs in yatish_timeslot.
bool Insert()
Takes care of INSERTs in yatish tables with a single name column.
bool DownloadProject()
Downloads yatish_project.
bool InsertActivity()
Takes care of INSERTs in yatish_activity.
bool DownloadNames()
Downloads yatish tables with a single name column.
bool UpdateProject()
Takes care of UPDATEs in yatish_project.
bool DownloadActivity()
Downloads yatish_activity.
bool EmptyMasterTables()
Empties all yatish tables in masterDB.
bool Delete()
Takes care of DELETEs in yatish tables.
bool EmptySlaveTables()
Empties all yatish tables in slaveDB.
bool NewTables()
Creates the yatish tables.
bool UpdateActivity()
Takes care of UPDATEs in yatish_activity.
bool InsertTimeslot()
Takes care of INSERTs in yatish_timeslot.
bool DownloadTimeslot()
Download yatish_timeslot.
bool OutdateMasterTables()
Sets the sync field to 'I' in all records of all yatish tables in masterDB.
bool Update()
Takes care of UPDATEs in yatish tables with a single name column.
yatishDBmysql()
Mainly connects to the MySQL database specified in yatish.auth
int Upload()
Uploads masterDB into slaveDB.
int Commit()
Updates slaveDB with records of masterDB where the sync field is not 'S'.
static wxDatabase * slaveDB
bool InsertProject()
Takes care of INSERTs in yatish_project.
#define CATCH(returnValue)