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