20 wxASSERT_MSG (
masterDB ==
nullptr,
"There can be only one instance of yatishDBsqlite.");
22 wxString databasePath = wxStandardPaths::Get().GetUserLocalDataDir() + wxFILE_SEP_PATH +
"yatish.sqlite";
23 databasePath.Replace (
"\\",
"\\\\" );
25 wxString databasePath =
"yatish.sqlite";
29 wxMessageBox (_(
"You are probably using Yatish for the first time...\n" 30 "Please ignore error messages and read\n" 31 "\"Getting started\" in the User guide (F1)"),
32 _(
"Startup procedure completed?"), wxOK);
36 if (
masterDB->GetTypeName() !=
"SQLITE")
37 throw ( wxDatabaseException (-4, _(
"Master DB must be SQLite") ) );
39 throw ( wxDatabaseException (-5, _(
"Yatish tables not found (SQLite)") ) );
40 masterDB->RunQuery (
"PRAGMA foreign_keys = ON;");
42 catch (wxDatabaseException& e) {
43 wxLogError (
"[%d] %s", e.GetErrorCode(), e.GetErrorMessage() );
66 wxString sql (
"SELECT id,name FROM yatish_");
69 sql +=
"client WHERE sync <> 'D' ORDER BY name;";
73 sql +=
"project WHERE sync <> 'D' ORDER BY name;";
77 sql +=
"task WHERE sync <> 'D' ORDER BY name;";
81 sql +=
"tool WHERE sync <> 'D' ORDER BY name;";
90 wxDatabaseResultSet * results =
masterDB->RunQueryWithResults (sql);
91 while ( results->Next() ) {
92 ids->Add ( results->GetResultLong (1) );
93 choices->Append ( results->GetResultString (2) );
136 sql.Printf (
"SELECT %s_id FROM yatish_%s WHERE id = %ld;",
138 refered_id =
masterDB->GetSingleResultLong (sql, 1);
141 return ids->Index (refered_id);
155 sql.Printf (
"SELECT id FROM yatish_activity" 156 " WHERE project_id = %d AND task_id = %d AND tool_id = %d;",
157 projectID, taskID, toolID);
159 wxDatabaseResultSet * results =
masterDB->RunQueryWithResults (sql);
160 if ( !results->Next() ) {
162 sql2.Printf (
"INSERT INTO yatish_activity (project_id,task_id,tool_id,sync)" 163 " VALUES (%d,%d,%d,'I');",
164 projectID, taskID, toolID);
167 results =
masterDB->RunQueryWithResults (sql);
170 long activityID = results->GetResultLong (1);
172 sql.Printf (
"INSERT INTO yatish_timeslot (start,activity_id,sync)" 173 " VALUES (datetime('now'),%ld,'I');", activityID);
175 results =
masterDB->RunQueryWithResults (
"SELECT id FROM yatish_timeslot WHERE stop IS NULL;");
191 sql.Printf (
"UPDATE yatish_timeslot SET stop=datetime('now')" 206 wxString sql (
"SELECT id,COUNT(*) FROM yatish_timeslot WHERE stop IS NULL;");
208 n =
masterDB->GetSingleResultInt (sql, 2);
222 int colmin, colmax = lst->GetColumnCount();
223 wxString sql (
"SELECT ");
226 if (colmax != 1)
return false;
227 sql +=
"id,name FROM yatish_client WHERE sync <> 'D';";
230 if (colmax != 2)
return false;
231 sql +=
"p.id,p.name,c.name FROM yatish_project AS p" 232 " INNER JOIN yatish_client AS c ON p.client_id = c.id" 233 " WHERE p.sync <> 'D'";
238 if (colmax != 1)
return false;
239 sql +=
"id,name FROM yatish_task WHERE sync <> 'D';";
242 if (colmax != 1)
return false;
243 sql +=
"id,name FROM yatish_tool WHERE sync <> 'D';";
246 if (colmax != 4)
return false;
247 sql +=
"a.id,p.name,c.name,tk.name,tl.name" 248 " FROM (((yatish_activity AS a" 249 " INNER JOIN yatish_project AS p ON a.project_id = p.id)" 250 " INNER JOIN yatish_client AS c ON p.client_id = c.id)" 251 " INNER JOIN yatish_task AS tk ON a.task_id = tk.id)" 252 " INNER JOIN yatish_tool AS tl ON a.tool_id = tl.id" 253 " WHERE a.sync <> 'D'";
258 if (colmax != 6)
return false;
259 sql.Printf (
"SELECT t.id,t.start,t.stop,p.name,c.name,tk.name,tl.name" 260 " FROM ((((yatish_timeslot AS t" 261 " INNER JOIN yatish_activity AS a ON t.activity_id = a.id)" 262 " INNER JOIN yatish_project AS p ON a.project_id = p.id)" 263 " INNER JOIN yatish_client AS c ON p.client_id = c.id)" 264 " INNER JOIN yatish_task AS tk ON a.task_id = tk.id)" 265 " INNER JOIN yatish_tool AS tl ON a.tool_id = tl.id" 266 " WHERE t.sync <> 'D' AND t.start BETWEEN '%s' AND '%s'" 270 sql += wxString::Format (
" ORDER BY t.id DESC LIMIT %d;",
rowLimit);
272 sql +=
" ORDER BY t.id DESC;";
283 wxDatabaseResultSet * results =
masterDB->RunQueryWithResults (sql);
286 while ( results->Next() ) {
288 wxDateTime start = results->GetResultDate (2), stop = results->GetResultDate (3);
291 row_index = lst->InsertItem ( row++, start.FormatISODate() );
292 if ( stop.IsValid() ) {
297 lst->SetItem ( row_index, 1, span.Format (
"%H:%M:%S") );
302 row_index = lst->InsertItem ( row++, results->GetResultString (2) );
306 for (
int col = colmin; col < colmax; col++)
308 lst->SetItem ( row_index, col, results->GetResultString (col + 2) );
310 lst->SetItemData ( row_index, results->GetResultLong (1) );
326 sqlAnd.Printf (
" AND c.id = %ld",
clientIDs[choice]);
329 sqlAnd.Printf (
" AND p.id = %ld",
projectIDs[choice]);
332 sqlAnd.Printf (
" AND tk.id = %ld",
taskIDs[choice]);
335 sqlAnd.Printf (
" AND tl.id = %ld",
toolIDs[choice]);
350 unsigned long totalSeconds =
totalSpan.GetSeconds().ToLong();
351 double totalDays, averageHours;
352 totalDays = totalSeconds / 3600.;
355 return wxString::Format(_(
"SUM: %.1f days | AVG: %.1f hours (x%ld)"),
364 firstDay = dt.ToUTC().FormatISOCombined();
372 lastDay = dt.ToUTC().FormatISOCombined();
380 wxDateTime dt = wxDateTime::Now();
384 wxString sql (
"SELECT MIN(start) FROM ((((yatish_timeslot AS t" 385 " INNER JOIN yatish_activity AS a ON t.activity_id = a.id)" 386 " INNER JOIN yatish_project AS p ON a.project_id = p.id)" 387 " INNER JOIN yatish_client AS c ON p.client_id = c.id)" 388 " INNER JOIN yatish_task AS tk ON a.task_id = tk.id)" 389 " INNER JOIN yatish_tool AS tl ON a.tool_id = tl.id" 390 " WHERE t.sync <> 'D'");
393 ans =
masterDB->GetSingleResultString (sql, 1);
396 dt.ParseISOCombined (ans,
' ');
397 return dt.MakeFromUTC();
405 wxDateTime dt = wxDateTime::Now();
409 wxString sql (
"SELECT MAX(start) FROM ((((yatish_timeslot AS t" 410 " INNER JOIN yatish_activity AS a ON t.activity_id = a.id)" 411 " INNER JOIN yatish_project AS p ON a.project_id = p.id)" 412 " INNER JOIN yatish_client AS c ON p.client_id = c.id)" 413 " INNER JOIN yatish_task AS tk ON a.task_id = tk.id)" 414 " INNER JOIN yatish_tool AS tl ON a.tool_id = tl.id" 415 " WHERE t.sync <> 'D'");
418 ans =
masterDB->GetSingleResultString (sql, 1);
421 dt.ParseISOCombined (ans,
' ');
422 return dt.MakeFromUTC();
429 long id = wxNOT_FOUND;
432 wxString sql (
"SELECT activity_id FROM yatish_timeslot ORDER BY start DESC LIMIT 1;");
433 wxDatabaseResultSet * results =
masterDB->RunQueryWithResults (sql);
434 if ( results->Next() )
id = results->GetResultLong (1);
446 long rid = wxNOT_FOUND;
449 wxString sql = wxString::Format(
"SELECT activity_id FROM yatish_timeslot WHERE id=%ld;",
id);
450 rid =
masterDB->GetSingleResultLong (sql, 1);
465 wxMessageBox ( _(
"Owing to foreign key constraints\nin the Yatish database," 466 "\nrecords refering to this item\nwill also be deleted."),
467 _(
"Confirm deletion"),
468 wxICON_EXCLAMATION|wxOK|wxCANCEL|wxCANCEL_DEFAULT )
469 == wxCANCEL )
return false;
470 wxString sql, status;
472 sql.Printf (
"SELECT sync FROM yatish_%s WHERE id = %ld;",
tableName[tid],
id);
473 status =
masterDB->GetSingleResultString(sql, 1);
477 sql.Printf (
"DELETE FROM yatish_%s WHERE id = %ld;",
tableName[tid],
id);
479 sql.Printf (
"UPDATE yatish_%s SET sync='D' WHERE id = %ld;",
tableName[tid],
id);
496 sql.Printf (
"SELECT name FROM yatish_%s WHERE id = %ld;",
tableName[tid],
id);
497 return masterDB->GetSingleResultString (sql, 1);
524 sql.Printf (
"SELECT start,stop FROM yatish_timeslot WHERE id = %ld;",
id);
525 wxDatabaseResultSet * results =
masterDB->RunQueryWithResults (sql);
526 if ( !results->Next() ) {
530 dt1 = results->GetResultDate (1); dt2 = results->GetResultDate (2);
535 if ( !dt2.IsValid() )
return false;
550 wxString sql, tbl =
tableName[tid], sync;
551 if (
id == wxNOT_FOUND)
552 sql.Printf (
"INSERT INTO yatish_%s (name,sync) VALUES ('%s','I');", tbl, str);
554 sql.Printf (
"SELECT sync FROM yatish_%s WHERE id = %ld", tbl,
id);
555 sync =
masterDB->GetSingleResultString (sql, 1);
557 sql.Printf (
"UPDATE yatish_%s SET name='%s'" 558 " WHERE id = %ld;", tbl, str,
id);
560 sql.Printf (
"UPDATE yatish_%s SET name='%s',sync='U'" 561 "WHERE id = %ld;", tbl, str,
id);
580 if (
id == wxNOT_FOUND)
581 sql.Printf (
"INSERT INTO yatish_project (name,client_id,sync) VALUES ('%s',%d,'I');",
584 sql.Printf (
"SELECT sync FROM yatish_project WHERE id = %ld",
id);
585 sync =
masterDB->GetSingleResultString (sql, 1);
587 sql.Printf (
"UPDATE yatish_project SET name='%s',client_id=%d" 588 " WHERE id = %ld;", str, clientID,
id);
590 sql.Printf (
"UPDATE yatish_project SET name='%s',client_id=%d,sync='U'" 591 " WHERE id = %ld;", str, clientID,
id);
611 if (
id == wxNOT_FOUND)
612 sql.Printf (
"INSERT INTO yatish_activity (project_id,task_id,tool_id,sync)" 613 " VALUES (%d,%d,%d,'I');", projectID, taskID, toolID);
615 sql.Printf (
"SELECT sync FROM yatish_activity WHERE id = %ld",
id);
616 sync =
masterDB->GetSingleResultString (sql, 1);
618 sql.Printf (
"UPDATE yatish_activity SET project_id=%d,task_id=%d,tool_id=%d" 619 " WHERE id = %ld;", projectID, taskID, toolID,
id);
621 sql.Printf (
"UPDATE yatish_activity SET project_id=%d,task_id=%d,tool_id=%d,sync='U'" 622 " WHERE id = %ld;", projectID, taskID, toolID,
id);
639 const wxDateTime& dt1,
const wxDateTime& dt2,
640 int project,
int task,
int tool) {
642 wxString start = dt1.ToUTC().FormatISOCombined(
' ');
643 wxString stop = dt2.ToUTC().FormatISOCombined(
' ');
648 sql.Printf (
"SELECT id FROM yatish_activity" 649 " WHERE project_id = %d AND task_id = %d AND tool_id = %d;",
650 projectID, taskID, toolID);
652 wxDatabaseResultSet * results =
masterDB->RunQueryWithResults (sql);
653 if ( !results->Next() ) {
655 sql2.Printf (
"INSERT INTO yatish_activity (project_id,task_id,tool_id,sync)" 656 " VALUES (%d,%d,%d,'I');",
657 projectID, taskID, toolID);
660 results =
masterDB->RunQueryWithResults (sql);
663 long activityID = results->GetResultLong (1);
665 if (
id == wxNOT_FOUND)
666 sql.Printf (
"INSERT INTO yatish_timeslot (start,stop,activity_id,sync)" 667 " VALUES ('%s','%s',%ld,'I');", start, stop, activityID);
669 sql.Printf (
"SELECT sync FROM yatish_timeslot WHERE id = %ld",
id);
670 sync =
masterDB->GetSingleResultString (sql, 1);
672 sql.Printf (
"UPDATE yatish_timeslot SET start='%s',stop='%s',activity_id=%ld" 673 " WHERE id = %ld;", start, stop, activityID,
id);
675 sql.Printf (
"UPDATE yatish_timeslot SET start='%s',stop='%s',activity_id=%ld,sync='U'" 676 " WHERE id = %ld;", start, stop, activityID,
id);
bool StopTimeslot()
Stops current timeslot.
long Activity(long)
Reads the activity_id in a record of yatish_timeslot.
wxDateTime First()
Obtains the minimum time in column start from currently selected rows of table yatish_timeslot.
int RunningTimeslots()
Determines the number n of unfinished timeslots.
void SetLastDay(const wxDateTime &)
Sets private member lastDay (for future SQL queries).
long LastActivity()
Reads the last activity_id in yatish_timeslot.
wxDatabase * GetDatabase(const wxString &)
Sets up a (configuration) flux using its argument then calls wxDatabase::GetDatabase().
bool ReadDates(long, wxDateTime &, wxDateTime &)
Reads the start and stop fields of a yatish_timeslot record.
static wxDatabase * masterDB
bool RecordActivity(long, int, int, int)
Modifies or creates a record in the yatish_activity table.
void AddToFilter(tableID, int)
Add a condition to the WHERE clause of FillList().
yatishDBsqlite()
Mainly connects to the yatish.sqlite database.
wxString FilteredTotal()
Writes the total and average durations of currently viewed timeslots.
bool StartTimeslot(int, int, int)
Starts a new row in the timeslot table.
void SetFirstDay(const wxDateTime &)
Sets private member firstDay (for future SQL queries).
bool Delete(tableID, long)
Marks a local record for deletion.
bool RecordTimeslot(long, const wxDateTime &, const wxDateTime &, int, int, int)
Modifies or creates a record in the yatish_timeslot table.
int ChoiceSelector(tableID, long)
Finds the proper index for selection in a wxChoice.
bool TablesOk(wxDatabase *)
Returns false if any of the yatish table is missings.
#define CATCH(returnValue)
bool RecordProject(long, const wxString &, int)
Modifies or creates a record in the yatish_project table.
bool FillList(wxListCtrl *, tableID)
Fills a wxListCtrl.
bool RecordName(tableID, long, const wxString &)
Modifies or creates a record with only one field (called name).
wxDateTime Last()
Obtains the maximum time in column start from currently selected rows of table yatish_timeslot.
bool FillChoice(wxChoice *, tableID)
Fills a wxChoice.
wxString ReadName(tableID, long)
Reads a wxString from the name column.
static const char * tableName[]
Must be defined in the same order as enum tableID.