19 wxASSERT_MSG (
masterDB ==
nullptr,
"There can be only one instance of yatishDBsqlite.");
21 wxString databasePath = wxStandardPaths::Get().GetUserLocalDataDir() + wxFILE_SEP_PATH +
"yatish.sqlite";
22 databasePath.Replace (
"\\",
"\\\\" );
24 wxString databasePath =
"yatish.sqlite";
28 wxMessageBox (_(
"You are probably using Yatish for the first time...\n"
29 "Please ignore error messages and read\n"
30 "\"Getting started\" in the User guide (F1)"),
31 _(
"Startup procedure completed?"), wxOK);
35 if (
masterDB->GetTypeName() !=
"SQLITE")
36 throw ( wxDatabaseException (-4, _(
"Master DB must be SQLite") ) );
38 throw ( wxDatabaseException (-5, _(
"Yatish tables not found (SQLite)") ) );
39 masterDB->RunQuery (
"PRAGMA foreign_keys = ON;");
41 catch (wxDatabaseException& e) {
42 wxLogError (
"[%d] %s", e.GetErrorCode(), e.GetErrorMessage() );
65 wxString sql (
"SELECT id,name FROM yatish_");
68 sql +=
"client WHERE sync <> 'D' ORDER BY name;";
72 sql +=
"project WHERE sync <> 'D' ORDER BY name;";
76 sql +=
"task WHERE sync <> 'D' ORDER BY name;";
80 sql +=
"tool WHERE sync <> 'D' ORDER BY name;";
89 wxDatabaseResultSet * results =
masterDB->RunQueryWithResults (sql);
90 while ( results->Next() ) {
91 ids->Add ( results->GetResultLong (1) );
92 choices->Append ( results->GetResultString (2) );
135 sql.Printf (
"SELECT %s_id FROM yatish_%s WHERE id = %ld;",
137 refered_id =
masterDB->GetSingleResultLong (sql, 1);
140 return ids->Index (refered_id);
154 sql.Printf (
"SELECT id FROM yatish_activity"
155 " WHERE project_id = %d AND task_id = %d AND tool_id = %d;",
156 projectID, taskID, toolID);
158 wxDatabaseResultSet * results =
masterDB->RunQueryWithResults (sql);
159 if ( !results->Next() ) {
161 sql2.Printf (
"INSERT INTO yatish_activity (project_id,task_id,tool_id,sync)"
162 " VALUES (%d,%d,%d,'I');",
163 projectID, taskID, toolID);
166 results =
masterDB->RunQueryWithResults (sql);
169 long activityID = results->GetResultLong (1);
171 sql.Printf (
"INSERT INTO yatish_timeslot (start,activity_id,sync)"
172 " VALUES (datetime('now'),%ld,'I');", activityID);
174 results =
masterDB->RunQueryWithResults (
"SELECT id FROM yatish_timeslot WHERE stop IS NULL;");
190 sql.Printf (
"UPDATE yatish_timeslot SET stop=datetime('now')"
205 wxString sql (
"SELECT id,COUNT(*) FROM yatish_timeslot WHERE stop IS NULL;");
207 n =
masterDB->GetSingleResultInt (sql, 2);
221 int colmin, colmax = lst->GetColumnCount();
222 wxString sql (
"SELECT ");
225 if (colmax != 1)
return false;
226 sql +=
"id,name FROM yatish_client WHERE sync <> 'D';";
229 if (colmax != 2)
return false;
230 sql +=
"p.id,p.name,c.name FROM yatish_project AS p"
231 " INNER JOIN yatish_client AS c ON p.client_id = c.id"
232 " WHERE p.sync <> 'D'";
237 if (colmax != 1)
return false;
238 sql +=
"id,name FROM yatish_task WHERE sync <> 'D';";
241 if (colmax != 1)
return false;
242 sql +=
"id,name FROM yatish_tool WHERE sync <> 'D';";
245 if (colmax != 4)
return false;
246 sql +=
"a.id,p.name,c.name,tk.name,tl.name"
247 " FROM (((yatish_activity AS a"
248 " INNER JOIN yatish_project AS p ON a.project_id = p.id)"
249 " INNER JOIN yatish_client AS c ON p.client_id = c.id)"
250 " INNER JOIN yatish_task AS tk ON a.task_id = tk.id)"
251 " INNER JOIN yatish_tool AS tl ON a.tool_id = tl.id"
252 " WHERE a.sync <> 'D'";
257 if (colmax != 6)
return false;
258 sql.Printf (
"SELECT t.id,t.start,t.stop,p.name,c.name,tk.name,tl.name"
259 " FROM ((((yatish_timeslot AS t"
260 " INNER JOIN yatish_activity AS a ON t.activity_id = a.id)"
261 " INNER JOIN yatish_project AS p ON a.project_id = p.id)"
262 " INNER JOIN yatish_client AS c ON p.client_id = c.id)"
263 " INNER JOIN yatish_task AS tk ON a.task_id = tk.id)"
264 " INNER JOIN yatish_tool AS tl ON a.tool_id = tl.id"
265 " WHERE t.sync <> 'D' AND t.start BETWEEN '%s' AND '%s'"
269 sql += wxString::Format (
" ORDER BY t.id DESC LIMIT %d;",
rowLimit);
271 sql +=
" ORDER BY t.id DESC;";
282 wxDatabaseResultSet * results =
masterDB->RunQueryWithResults (sql);
285 while ( results->Next() ) {
287 wxDateTime start = results->GetResultDate (2), stop = results->GetResultDate (3);
290 row_index = lst->InsertItem ( row++, start.FormatISODate() );
291 if ( stop.IsValid() ) {
296 lst->SetItem ( row_index, 1, span.Format (
"%H:%M:%S") );
301 row_index = lst->InsertItem ( row++, results->GetResultString (2) );
305 for (
int col = colmin; col < colmax; col++)
307 lst->SetItem ( row_index, col, results->GetResultString (col + 2) );
309 lst->SetItemData ( row_index, results->GetResultLong (1) );
323 wxString sql (
"SELECT t.start,t.stop,p.name,c.name,tk.name,tl.name FROM yatish_timeslot t"
324 " INNER JOIN yatish_activity a ON t.activity_id = a.id"
325 " INNER JOIN yatish_project p ON a.project_id = p.id"
326 " INNER JOIN yatish_client c ON p.client_id = c.id"
327 " INNER JOIN yatish_task tk ON a.task_id = tk.id"
328 " INNER JOIN yatish_tool tl ON a.tool_id = tl.id"
329 " WHERE stop IS NOT NULL ORDER BY start;");
331 wxDatabaseResultSet * results =
masterDB->RunQueryWithResults (sql);
333 while ( results->Next() ) {
334 record.
start = results->GetResultDate (1);
335 record.
stop = results->GetResultDate (2);
336 record.
project = results->GetResultString (3);
337 record.
client = results->GetResultString (4);
338 record.
task = results->GetResultString (5);
339 record.
tool = results->GetResultString (6);
340 v.push_back (record);
356 sqlAnd.Printf (
" AND c.id = %ld",
clientIDs[choice]);
359 sqlAnd.Printf (
" AND p.id = %ld",
projectIDs[choice]);
362 sqlAnd.Printf (
" AND tk.id = %ld",
taskIDs[choice]);
365 sqlAnd.Printf (
" AND tl.id = %ld",
toolIDs[choice]);
379 sqlAnd.Printf (
" AND activity_id = %ld", activityID);
391 ts = wxTimeSpan::Hours (0);
402 double totalSeconds =
totalSpan.GetSeconds().ToDouble();
403 double totalDays, averageHours;
404 totalDays = totalSeconds / 3600.;
407 return wxString::Format(_(
"SUM: %.1f days | AVG: %.1f hours (x%ld)"),
416 firstDay = dt.ToUTC().FormatISOCombined();
426 lastDay = ( dt + wxDateSpan::Day() ).ToUTC().FormatISOCombined();
434 wxDateTime dt = wxDateTime::Now();
438 wxString sql (
"SELECT MIN(start) FROM ((((yatish_timeslot AS t"
439 " INNER JOIN yatish_activity AS a ON t.activity_id = a.id)"
440 " INNER JOIN yatish_project AS p ON a.project_id = p.id)"
441 " INNER JOIN yatish_client AS c ON p.client_id = c.id)"
442 " INNER JOIN yatish_task AS tk ON a.task_id = tk.id)"
443 " INNER JOIN yatish_tool AS tl ON a.tool_id = tl.id"
444 " WHERE t.sync <> 'D'");
447 ans =
masterDB->GetSingleResultString (sql, 1);
450 dt.ParseISOCombined (ans,
' ');
451 return dt.MakeFromUTC();
459 wxDateTime dt = wxDateTime::Now();
463 wxString sql (
"SELECT MAX(start) FROM ((((yatish_timeslot AS t"
464 " INNER JOIN yatish_activity AS a ON t.activity_id = a.id)"
465 " INNER JOIN yatish_project AS p ON a.project_id = p.id)"
466 " INNER JOIN yatish_client AS c ON p.client_id = c.id)"
467 " INNER JOIN yatish_task AS tk ON a.task_id = tk.id)"
468 " INNER JOIN yatish_tool AS tl ON a.tool_id = tl.id"
469 " WHERE t.sync <> 'D'");
472 ans =
masterDB->GetSingleResultString (sql, 1);
475 dt.ParseISOCombined (ans,
' ');
476 return dt.MakeFromUTC();
483 long id = wxNOT_FOUND;
486 wxString sql (
"SELECT activity_id FROM yatish_timeslot ORDER BY start DESC LIMIT 1;");
487 wxDatabaseResultSet * results =
masterDB->RunQueryWithResults (sql);
488 if ( results->Next() )
id = results->GetResultLong (1);
500 long rid = wxNOT_FOUND;
503 wxString sql = wxString::Format(
"SELECT activity_id FROM yatish_timeslot WHERE id=%ld;",
id);
504 rid =
masterDB->GetSingleResultLong (sql, 1);
519 wxMessageBox ( _(
"Owing to foreign key constraints\nin the Yatish database,"
520 "\nrecords refering to this item\nwill also be deleted."),
521 _(
"Confirm deletion"),
522 wxICON_EXCLAMATION|wxOK|wxCANCEL|wxCANCEL_DEFAULT )
523 == wxCANCEL )
return false;
524 wxString sql, status;
526 sql.Printf (
"SELECT sync FROM yatish_%s WHERE id = %ld;",
tableName[tid],
id);
527 status =
masterDB->GetSingleResultString(sql, 1);
531 sql.Printf (
"DELETE FROM yatish_%s WHERE id = %ld;",
tableName[tid],
id);
533 sql.Printf (
"UPDATE yatish_%s SET sync='D' WHERE id = %ld;",
tableName[tid],
id);
550 sql.Printf (
"SELECT name FROM yatish_%s WHERE id = %ld;",
tableName[tid],
id);
551 return masterDB->GetSingleResultString (sql, 1);
566 sql.Printf (
"SELECT start,stop FROM yatish_timeslot WHERE id = %ld;",
id);
567 wxDatabaseResultSet * results =
masterDB->RunQueryWithResults (sql);
568 if ( !results->Next() ) {
572 dt1 = results->GetResultDate (1); dt2 = results->GetResultDate (2);
577 if ( !dt2.IsValid() )
return false;
592 wxString sql, tbl =
tableName[tid], sync;
593 if (
id == wxNOT_FOUND)
594 sql.Printf (
"INSERT INTO yatish_%s (name,sync) VALUES ('%s','I');", tbl, str);
596 sql.Printf (
"SELECT sync FROM yatish_%s WHERE id = %ld", tbl,
id);
597 sync =
masterDB->GetSingleResultString (sql, 1);
599 sql.Printf (
"UPDATE yatish_%s SET name='%s'"
600 " WHERE id = %ld;", tbl, str,
id);
602 sql.Printf (
"UPDATE yatish_%s SET name='%s',sync='U'"
603 "WHERE id = %ld;", tbl, str,
id);
622 if (
id == wxNOT_FOUND)
623 sql.Printf (
"INSERT INTO yatish_project (name,client_id,sync) VALUES ('%s',%d,'I');",
626 sql.Printf (
"SELECT sync FROM yatish_project WHERE id = %ld",
id);
627 sync =
masterDB->GetSingleResultString (sql, 1);
629 sql.Printf (
"UPDATE yatish_project SET name='%s',client_id=%d"
630 " WHERE id = %ld;", str, clientID,
id);
632 sql.Printf (
"UPDATE yatish_project SET name='%s',client_id=%d,sync='U'"
633 " WHERE id = %ld;", str, clientID,
id);
653 if (
id == wxNOT_FOUND)
654 sql.Printf (
"INSERT INTO yatish_activity (project_id,task_id,tool_id,sync)"
655 " VALUES (%d,%d,%d,'I');", projectID, taskID, toolID);
657 sql.Printf (
"SELECT sync FROM yatish_activity WHERE id = %ld",
id);
658 sync =
masterDB->GetSingleResultString (sql, 1);
660 sql.Printf (
"UPDATE yatish_activity SET project_id=%d,task_id=%d,tool_id=%d"
661 " WHERE id = %ld;", projectID, taskID, toolID,
id);
663 sql.Printf (
"UPDATE yatish_activity SET project_id=%d,task_id=%d,tool_id=%d,sync='U'"
664 " WHERE id = %ld;", projectID, taskID, toolID,
id);
681 const wxDateTime& dt1,
const wxDateTime& dt2,
682 int project,
int task,
int tool) {
684 wxString start = dt1.ToUTC().FormatISOCombined(
' ');
685 wxString stop = dt2.ToUTC().FormatISOCombined(
' ');
690 sql.Printf (
"SELECT id FROM yatish_activity"
691 " WHERE project_id = %d AND task_id = %d AND tool_id = %d;",
692 projectID, taskID, toolID);
694 wxDatabaseResultSet * results =
masterDB->RunQueryWithResults (sql);
695 if ( !results->Next() ) {
697 sql2.Printf (
"INSERT INTO yatish_activity (project_id,task_id,tool_id,sync)"
698 " VALUES (%d,%d,%d,'I');",
699 projectID, taskID, toolID);
702 results =
masterDB->RunQueryWithResults (sql);
705 long activityID = results->GetResultLong (1);
707 if (
id == wxNOT_FOUND)
708 sql.Printf (
"INSERT INTO yatish_timeslot (start,stop,activity_id,sync)"
709 " VALUES ('%s','%s',%ld,'I');", start, stop, activityID);
711 sql.Printf (
"SELECT sync FROM yatish_timeslot WHERE id = %ld",
id);
712 sync =
masterDB->GetSingleResultString (sql, 1);
714 sql.Printf (
"UPDATE yatish_timeslot SET start='%s',stop='%s',activity_id=%ld"
715 " WHERE id = %ld;", start, stop, activityID,
id);
717 sql.Printf (
"UPDATE yatish_timeslot SET start='%s',stop='%s',activity_id=%ld,sync='U'"
718 " WHERE id = %ld;", start, stop, activityID,
id);
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().
bool ReadDates(long, wxDateTime &, wxDateTime &)
Reads the start and stop fields of a yatish_timeslot record.
bool FillChoice(wxChoice *, tableID)
Fills a wxChoice.
bool RecordActivity(long, int, int, int)
Modifies or creates a record in the yatish_activity table.
void SetFirstDay(const wxDateTime &)
Sets private member firstDay (for future SQL queries).
bool RecordTimeslot(long, const wxDateTime &, const wxDateTime &, int, int, int)
Modifies or creates a record in the yatish_timeslot table.
bool FillList(wxListCtrl *, tableID)
Fills a wxListCtrl.
long FilteredTotal(wxTimeSpan &) const
Returns the total duration of currently viewed timeslots (and their count).
bool FillPlotData(RawData &)
Fills a RawData (typedefined in yatishTypes.h).
bool RecordName(tableID, long, const wxString &)
Modifies or creates a record with only one field (called name).
wxString FilteredTotalFormatted()
Writes the total and average durations of currently viewed timeslots.
yatishDBsqlite()
Mainly connects to the yatish.sqlite database.
wxString ReadName(tableID, long)
Reads a wxString from the name column.
wxDateTime Last()
Obtains the maximum time in column start from currently selected rows of table yatish_timeslot.
int RunningTimeslots()
Determines the number n of unfinished timeslots.
wxDateTime First()
Obtains the minimum time in column start from currently selected rows of table yatish_timeslot.
void SetLastDay(const wxDateTime &)
Sets private member lastDay (for future SQL queries).
bool Delete(tableID, long)
Marks a local record for deletion.
void AddToFilter(tableID, int)
Adds a condition to the WHERE clause of FillList().
bool RecordProject(long, const wxString &, int)
Modifies or creates a record in the yatish_project table.
bool StartTimeslot(int, int, int)
Starts a new row in the timeslot table.
int ChoiceSelector(tableID, long)
Finds the proper index for selection in a wxChoice.
long Activity(long)
Reads the activity_id in a record of yatish_timeslot.
bool StopTimeslot()
Stops current timeslot.
long LastActivity()
Reads the last activity_id in yatish_timeslot.
Element of typefined std:vector RawData (yatishPlot.big_data).
#define CATCH(returnValue)
vector< RawRecord > RawData