GnuCashew ~ GnuCash Enabled Web
GCW
SessionGnuCashew.cpp
Go to the documentation of this file.
1 #line 2 "src/Dbo/SessionGnuCashew.cpp"
2 
3 #include <iostream>
4 
5 #include <Wt/Dbo/backend/Sqlite3.h>
6 
7 #include <gcwglobal.h>
8 
9 #include "Accounts/Accounts.h"
10 #include "Customers/Customers.h"
12 #include "Slots/Slots.h"
13 #include "Splits/Splits.h"
15 #include "Vars/Vars.h"
16 #include "SessionGnuCashew.h"
17 
18 bool
20 open( const std::string & _path )
21 {
22  /*
23  ** Call the base class.
24  **
25  */
27 
28  /*
29  ** Setup authentication services
30  **
31  ** This module needs initialization if it is going to be used. If it is
32  ** not going to be used, then no damage is done here. Once the database
33  ** is opened then we can determine if it supports user authentication.
34  **
35  */
37 
39 
40  m_users = std::make_unique< GCW::Dbo::Users::UserDatabase >( *this );
41 
42  /*
43  ** Try to get the sqlite3 file open
44  **
45  */
46  try
47  {
48  auto connection = std::make_unique< Wt::Dbo::backend::Sqlite3 >( _path );
49 
50 // connection-> setProperty( "show-queries", "true" );
51 
52  setConnection( std::move( connection ) );
53 
54  }
55 
56  /*
57  ** Handle the error
58  **
59  */
60  catch( std::exception & e )
61  {
62  std::cout << __FILE__ << ":" << __LINE__ << " " << e.what() << std::endl;
63  }
64 
65  m_isOpen = true;
66 
67 // std::cout << __FILE__ << ":" << __LINE__ << " " << tableCreationSql() << std::endl;
68 
69  return isOpen();
70 
71 } // endopen( const std::string & _path )
72 
73 bool
76 {
77  /*
78  ** assume no extensions
79  */
80  bool retVal = false;
81 
82  using ResultRow = std::tuple< std::string, std::string, std::string, int, std::string >;
83 
84  /*
85  ** query the list of tables
86  */
87  Wt::Dbo::Transaction t( *this );
88  auto results =
89  query< ResultRow >
90  (
91  "select type,name,tbl_name,rootpage,sql from sqlite_master"
92  )
93  .resultList()
94  ;
95 
96  /*
97  ** loop the list, looking for the extension tables
98  */
99  for( auto result : results )
100  {
101  std::string _type;
102  std::string _name;
103  std::string tbl_name;
104  int rootpage;
105  std::string _text;
106 
107  std::tie( _type, _name, tbl_name, rootpage, _text ) = result;
108 
109  if( _type != "table" )
110  continue;
111 
112  if( _name == GCW::Dbo::Vars::s_tableName )
113  {
114  retVal = true;
115  break;
116  }
117 
118 #ifdef NEVER
119  std::cout << __FILE__ << ":" << __LINE__
120  << " type:" << _type
121  << " name:" << _name
122 // << " tbln:" << tbl_name
123 // << " root:" << rootpage
124 // << " text:" << _text
125  << std::endl
126  ;
127 #endif
128 
129  } // endfor( auto result : results )
130 
131  return retVal;
132 
133 } // endhasGnuCashewExtensions() const
134 
135 bool
138 {
139  if( hasGnuCashewExtensions() )
140  return true;
141 
142  Wt::Dbo::Transaction t( *this );
143 
144  /*
145  ** Add a table to contain the vars
146  **
147  */
148  for( int i=0; i< 5; i++ )
149  {
150  auto key = Wt::WString( "gcw_sql.create.{1}").arg( i ).toUTF8();
151  auto sql = TR( key ).toUTF8();
152  execute( sql );
153  }
154 
155  /*
156  ** poke some initial values in to the vars table
157  **
158  */
159  auto item = GCW::Dbo::Vars::get( "gnucashew","sys" );
160 
161  item.modify()-> setVar( "createdOn", Wt::WDateTime::currentDateTime().toString( ISO_DATE_FORMAT ) );
162  item.modify()-> setVar( "gcwVersion", 1 );
163 
164  return true;
165 
166 } // endaddGnuCashewExtensions()
167 
168 
169 
auto isOpen() const -> bool
auto open(const std::string &_path) -> bool
Open a database.
bool addGnuCashewExtensions()
Add GnuCashew Extensions.
bool open(const std::string &_path)
Open a database.
std::unique_ptr< GCW::Dbo::Users::UserDatabase > m_users
bool hasGnuCashewExtensions()
Check GnuCashew Extensions.
#define TR(X)
Definition: define.h:17
#define ISO_DATE_FORMAT
Definition: gcwglobal.h:11
auto currentDateTime() -> Wt::WDateTime
Current Date/Time.
Definition: Core.cpp:260
auto configure() -> void
Definition: Auth.cpp:58
auto mapClasses(Wt::Dbo::Session &session) -> void
Definition: Auth.cpp:24
GCW::Dbo::Vars::Item::Ptr get(const std::string &_keyValue, const std::string &_cfyValue="*", bool _add=true)
Definition: Vars.cpp:16
const char * s_tableName
Definition: Vars.cpp:11
auto toString(int _value) -> std::string
Convert Integer to String.
Definition: BillPay.cpp:41