77#ifndef BOT_ORM_H
88#define BOT_ORM_H
99
10- #include < functional>
10+ // Container
11+ #include < tuple>
1112#include < vector>
1213#include < list>
1314#include < string>
14- #include < tuple>
1515#include < unordered_map>
16- # include < cctype >
17- # include < thread >
16+
17+ // Serialization
1818#include < sstream>
19+
20+ // Type Traits
1921#include < type_traits>
22+
23+ // std::nullptr_t
2024#include < cstddef>
2125
26+ // std::shared_ptr
27+ #include < memory>
28+
29+ // for Field Name Extractor
30+ #include < cctype>
31+
32+ // for SQL Connector
33+ #include < thread>
34+ #include < functional>
35+
36+ // SQLite 3 Dependency
2237#include " sqlite3.h"
2338
2439// Public Macro
@@ -179,8 +194,7 @@ namespace BOT_ORM_Impl
179194 public:
180195 SQLConnector (const std::string &fileName)
181196 {
182- auto rc = sqlite3_open (fileName.c_str (), &db);
183- if (rc)
197+ if (sqlite3_open (fileName.c_str (), &db))
184198 {
185199 sqlite3_close (db);
186200 throw std::runtime_error (
@@ -236,8 +250,7 @@ namespace BOT_ORM_Impl
236250
237251 private:
238252 sqlite3 *db;
239- static void _callback (int argc, char **argv, char **azColName)
240- { return ; }
253+ static inline void _callback (int , char **, char **) { return ; }
241254 };
242255
243256 // Checking Injection
@@ -859,7 +872,7 @@ namespace BOT_ORM
859872 class Queryable
860873 {
861874 protected:
862- BOT_ORM_Impl::SQLConnector & _connector;
875+ std::shared_ptr< BOT_ORM_Impl::SQLConnector> _connector;
863876 QueryResult _queryHelper;
864877
865878 std::string _sqlFrom;
@@ -874,19 +887,20 @@ namespace BOT_ORM
874887 std::string _sqlLimit;
875888 std::string _sqlOffset;
876889
877- Queryable (BOT_ORM_Impl::SQLConnector &connector,
878- QueryResult queryHelper,
879- std::string sqlFrom,
880- std::string sqlSelect = " select " ,
881- std::string sqlTarget = " *" ,
882- std::string sqlWhere = std::string {},
883- std::string sqlGroupBy = std::string {},
884- std::string sqlHaving = std::string {},
885- std::string sqlOrderBy = std::string {},
886- std::string sqlLimit = std::string {},
887- std::string sqlOffset = std::string {})
890+ Queryable (
891+ std::shared_ptr<BOT_ORM_Impl::SQLConnector> connector,
892+ QueryResult queryHelper,
893+ std::string sqlFrom,
894+ std::string sqlSelect = " select " ,
895+ std::string sqlTarget = " *" ,
896+ std::string sqlWhere = std::string {},
897+ std::string sqlGroupBy = std::string {},
898+ std::string sqlHaving = std::string {},
899+ std::string sqlOrderBy = std::string {},
900+ std::string sqlLimit = std::string {},
901+ std::string sqlOffset = std::string {})
888902 :
889- _connector (connector),
903+ _connector (std::move ( connector) ),
890904 _queryHelper (std::move (queryHelper)),
891905 _sqlFrom (std::move (sqlFrom)),
892906 _sqlSelect (std::move (sqlSelect)),
@@ -1019,9 +1033,9 @@ namespace BOT_ORM
10191033 Nullable<T> Select (const Expression::Aggregate<T> &agg) const
10201034 {
10211035 Nullable<T> ret;
1022- _connector. Execute (_sqlSelect + agg.fieldName +
1023- _GetFromSql () + _GetLimit () + " ;" ,
1024- [&] (int argc, char **argv, char **)
1036+ _connector-> Execute (_sqlSelect + agg.fieldName +
1037+ _GetFromSql () + _GetLimit () + " ;" ,
1038+ [&] (int argc, char **argv, char **)
10251039 {
10261040 BOT_ORM_Impl::DeserializeValue (ret, argv[0 ]);
10271041 });
@@ -1052,18 +1066,16 @@ namespace BOT_ORM
10521066
10531067 // Return a new Queryable Object
10541068 template <typename ... Args>
1055- auto _NewQuery (std::string sqlTarget,
1056- std::string sqlFrom,
1057- std::tuple<Args...> &&newQueryHelper) const
1069+ inline auto _NewQuery (std::string sqlTarget,
1070+ std::string sqlFrom,
1071+ std::tuple<Args...> &&newQueryHelper) const
10581072 {
1059- return Queryable<std::tuple<Args...>>
1060- {
1073+ return Queryable<std::tuple<Args...>> (
10611074 _connector, newQueryHelper,
1062- std::move (sqlFrom),
1063- _sqlSelect, std::move (sqlTarget),
1064- _sqlWhere, _sqlGroupBy, _sqlHaving,
1065- _sqlOrderBy, _sqlLimit, _sqlOffset
1066- };
1075+ std::move (sqlFrom),
1076+ _sqlSelect, std::move (sqlTarget),
1077+ _sqlWhere, _sqlGroupBy, _sqlHaving,
1078+ _sqlOrderBy, _sqlLimit, _sqlOffset);
10671079 }
10681080
10691081 // Return a new Join Queryable Object
@@ -1113,9 +1125,9 @@ namespace BOT_ORM
11131125 void _Select (const C &, Out &out) const
11141126 {
11151127 auto copy = _queryHelper;
1116- _connector. Execute (_sqlSelect + _sqlTarget +
1117- _GetFromSql () + _GetLimit () + " ;" ,
1118- [&] (int , char **argv, char **)
1128+ _connector-> Execute (_sqlSelect + _sqlTarget +
1129+ _GetFromSql () + _GetLimit () + " ;" ,
1130+ [&] (int , char **argv, char **)
11191131 {
11201132 size_t index = 1 ;
11211133 BOT_ORM_Impl::DeserializeValue (copy.__PrimaryKey (), argv[0 ]);
@@ -1132,9 +1144,9 @@ namespace BOT_ORM
11321144 void _Select (const std::tuple<Args...> &, Out &out) const
11331145 {
11341146 auto copy = _queryHelper;
1135- _connector. Execute (_sqlSelect + _sqlTarget +
1136- _GetFromSql () + _GetLimit () + " ;" ,
1137- [&] (int , char **argv, char **)
1147+ _connector-> Execute (_sqlSelect + _sqlTarget +
1148+ _GetFromSql () + _GetLimit () + " ;" ,
1149+ [&] (int , char **argv, char **)
11381150 {
11391151 size_t index = 0 ;
11401152 BOT_ORM_Impl::QueryableHelper::TupleVisit (
@@ -1153,23 +1165,24 @@ namespace BOT_ORM
11531165 {
11541166 public:
11551167 ORMapper (const std::string &connectionString)
1156- : _connector (connectionString)
1168+ : _connector (std::make_shared<BOT_ORM_Impl::SQLConnector> (
1169+ connectionString))
11571170 {
1158- _connector. Execute (" PRAGMA foreign_keys = ON;" );
1171+ _connector-> Execute (" PRAGMA foreign_keys = ON;" );
11591172 }
11601173
11611174 template <typename Fn>
11621175 void Transaction (Fn fn)
11631176 {
11641177 try
11651178 {
1166- _connector. Execute (" begin transaction;" );
1179+ _connector-> Execute (" begin transaction;" );
11671180 fn ();
1168- _connector. Execute (" commit transaction;" );
1181+ _connector-> Execute (" commit transaction;" );
11691182 }
11701183 catch (...)
11711184 {
1172- _connector. Execute (" rollback transaction;" );
1185+ _connector-> Execute (" rollback transaction;" );
11731186 throw ;
11741187 }
11751188 }
@@ -1205,9 +1218,9 @@ namespace BOT_ORM
12051218 strFmt += std::move (tableFixes);
12061219 strFmt.pop_back ();
12071220
1208- _connector. Execute (" create table " +
1209- std::string (C::__TableName) +
1210- " (" + strFmt + " );" );
1221+ _connector-> Execute (" create table " +
1222+ std::string (C::__TableName) +
1223+ " (" + strFmt + " );" );
12111224 }
12121225
12131226 template <typename C>
@@ -1220,9 +1233,9 @@ namespace BOT_ORM
12201233 std::enable_if_t <BOT_ORM_Impl::HasInjected<C>::value>
12211234 DropTbl (const C &)
12221235 {
1223- _connector. Execute (" drop table " +
1224- std::string (C::__TableName) +
1225- " ;" );
1236+ _connector-> Execute (" drop table " +
1237+ std::string (C::__TableName) +
1238+ " ;" );
12261239 }
12271240
12281241 template <typename C>
@@ -1237,7 +1250,7 @@ namespace BOT_ORM
12371250 {
12381251 std::ostringstream os;
12391252 _GetInsert (os, entity, withId);
1240- _connector. Execute (os.str ());
1253+ _connector-> Execute (os.str ());
12411254 }
12421255
12431256 template <typename In, typename C = typename In::value_type>
@@ -1259,7 +1272,7 @@ namespace BOT_ORM
12591272 anyEntity = true ;
12601273 }
12611274 if (anyEntity)
1262- _connector. Execute (os.str ());
1275+ _connector-> Execute (os.str ());
12631276 }
12641277
12651278 template <typename C>
@@ -1274,7 +1287,7 @@ namespace BOT_ORM
12741287 {
12751288 std::ostringstream os;
12761289 if (_GetUpdate (os, entity))
1277- _connector. Execute (os.str ());
1290+ _connector-> Execute (os.str ());
12781291 }
12791292
12801293 template <typename In, typename C = typename In::value_type>
@@ -1295,7 +1308,7 @@ namespace BOT_ORM
12951308 osTmp.str (std::string {}); // Flush the previous one
12961309 }
12971310 auto sql = os.str ();
1298- if (!sql.empty ()) _connector. Execute (sql);
1311+ if (!sql.empty ()) _connector-> Execute (sql);
12991312 }
13001313
13011314 template <typename C>
@@ -1312,11 +1325,11 @@ namespace BOT_ORM
13121325 const Expression::SetExpr &setExpr,
13131326 const Expression::Expr &whereExpr)
13141327 {
1315- _connector. Execute (" update " +
1316- std::string (C::__TableName) +
1317- " set " + setExpr.ToString () +
1318- " where " +
1319- whereExpr.ToString (false ) + " ;" );
1328+ _connector-> Execute (" update " +
1329+ std::string (C::__TableName) +
1330+ " set " + setExpr.ToString () +
1331+ " where " +
1332+ whereExpr.ToString (false ) + " ;" );
13201333 }
13211334
13221335 template <typename C>
@@ -1335,7 +1348,7 @@ namespace BOT_ORM
13351348 BOT_ORM_Impl::SerializeValue (os, entity.__PrimaryKey ());
13361349 os << " ;" ;
13371350
1338- _connector. Execute (os.str ());
1351+ _connector-> Execute (os.str ());
13391352 }
13401353
13411354 template <typename C>
@@ -1350,10 +1363,10 @@ namespace BOT_ORM
13501363 Delete (const C &,
13511364 const Expression::Expr &whereExpr)
13521365 {
1353- _connector. Execute (" delete from " +
1354- std::string (C::__TableName) +
1355- " where " +
1356- whereExpr.ToString (false ) + " ;" );
1366+ _connector-> Execute (" delete from " +
1367+ std::string (C::__TableName) +
1368+ " where " +
1369+ whereExpr.ToString (false ) + " ;" );
13571370 }
13581371
13591372 template <typename C>
@@ -1366,13 +1379,13 @@ namespace BOT_ORM
13661379 std::enable_if_t <BOT_ORM_Impl::HasInjected<C>::value, Queryable<C>>
13671380 Query (C queryHelper)
13681381 {
1369- return Queryable<C> { _connector,
1370- std::move (queryHelper),
1371- std::string (" from " ) + C::__TableName } ;
1382+ return Queryable<C> ( _connector,
1383+ std::move (queryHelper),
1384+ std::string (" from " ) + C::__TableName) ;
13721385 }
13731386
13741387 protected:
1375- BOT_ORM_Impl::SQLConnector _connector;
1388+ std::shared_ptr< BOT_ORM_Impl::SQLConnector> _connector;
13761389
13771390 template <typename T>
13781391 static inline const char *_TypeString (const T &)
0 commit comments