YATiSh
Yet Another TIme SHeet
yatishDB.cpp
Go to the documentation of this file.
1 /********************************************************************
2  * Name: yatishDB.cpp
3  * Purpose: Implements the base class of yatish database controllers
4  * Author: Nicolas PĂ©renne (nicolas.perenne@eif-services.eu)
5  * Created: 2020-04-15
6  * Copyright: EIF-services (https://www.eif-services.eu/yatish)
7  * License: GPLv3
8  ********************************************************************/
9 
10 #include "wx_pch.h"
11 #include "yatishDB.h"
12 
17 const char * yatishDB::tableName [] = {
18  "client",
19  "task",
20  "tool",
21  "project",
22  "activity",
23  "timeslot"
24 };
25 
26 wxDatabase * yatishDB::masterDB = nullptr;
27 
48 wxDatabase * yatishDB::GetDatabase (const wxString& configString) {
49  wxInputStream * configStream;
50  wxDatabase * pDatabase;
51  try {
52  if ( configString.IsEmpty() ) {
53  wxString configPath = wxStandardPaths::Get().GetUserLocalDataDir() + wxFILE_SEP_PATH + "yatish.auth";
54  if ( !wxFileName::FileExists (configPath) ) {
55  wxString msg;
56  msg.Printf (_("Authentication file '%s' not found"), configPath);
57  throw ( wxDatabaseException (-1, msg) );
58  }
59  configStream = new wxFileInputStream (configPath);
60  if ( !configStream->IsOk() ) {
61  wxString msg;
62  msg.Printf (_("Authentication file '%s' cannot be opened"), configPath);
63  throw ( wxDatabaseException (-2, msg) );
64  }
65  } else {
66  configStream = new wxStringInputStream (configString);
67  }
68  wxFileConfig config (*configStream);
69  wxString err = wxEmptyString;
70  pDatabase = wxDatabase::GetDatabase (config, &err);
71  if (!pDatabase) {
72  wxString msg;
73  msg.Printf (_("Error while reading the authentication file:\n%s"), err);
74  throw ( wxDatabaseException (-3, msg) );
75  }
76  }
77  CATCH (nullptr)
78  delete configStream;
79  return pDatabase;
80 }
81 
83 bool yatishDB::TablesOk (wxDatabase * pDatabase) {
84  return pDatabase->TableExists ("yatish_client") &&
85  pDatabase->TableExists ("yatish_project") &&
86  pDatabase->TableExists ("yatish_task") &&
87  pDatabase->TableExists ("yatish_tool") &&
88  pDatabase->TableExists ("yatish_activity")&&
89  pDatabase->TableExists ("yatish_timeslot");
90 }
static wxDatabase * masterDB
Definition: yatishDB.h:46
bool TablesOk(wxDatabase *)
Returns false if any of the yatish table is missings.
Definition: yatishDB.cpp:83
static const char * tableName[]
Must be defined in the same order as enum tableID.
Definition: yatishDB.h:44
wxDatabase * GetDatabase(const wxString &)
Sets up a (configuration) flux using its argument then calls wxDatabase::GetDatabase().
Definition: yatishDB.cpp:48
#define CATCH(returnValue)
Definition: yatishDB.h:22