QxDao.h 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  1. /****************************************************************************
  2. **
  3. ** https://www.qxorm.com/
  4. ** Copyright (C) 2013 Lionel Marty (contact@qxorm.com)
  5. **
  6. ** This file is part of the QxOrm library
  7. **
  8. ** This software is provided 'as-is', without any express or implied
  9. ** warranty. In no event will the authors be held liable for any
  10. ** damages arising from the use of this software
  11. **
  12. ** Commercial Usage
  13. ** Licensees holding valid commercial QxOrm licenses may use this file in
  14. ** accordance with the commercial license agreement provided with the
  15. ** Software or, alternatively, in accordance with the terms contained in
  16. ** a written agreement between you and Lionel Marty
  17. **
  18. ** GNU General Public License Usage
  19. ** Alternatively, this file may be used under the terms of the GNU
  20. ** General Public License version 3.0 as published by the Free Software
  21. ** Foundation and appearing in the file 'license.gpl3.txt' included in the
  22. ** packaging of this file. Please review the following information to
  23. ** ensure the GNU General Public License version 3.0 requirements will be
  24. ** met : http://www.gnu.org/copyleft/gpl.html
  25. **
  26. ** If you are unsure which license is appropriate for your use, or
  27. ** if you have questions regarding the use of this file, please contact :
  28. ** contact@qxorm.com
  29. **
  30. ****************************************************************************/
  31. #ifndef _QX_DAO_H_
  32. #define _QX_DAO_H_
  33. #ifdef _MSC_VER
  34. #pragma once
  35. #endif
  36. /*!
  37. * \file QxDao.h
  38. * \author Lionel Marty
  39. * \ingroup QxDao
  40. * \brief Provide template functions to map C++ class registered into QxOrm context with table database (ORM - Object Relational Mapping)
  41. */
  42. #include <QtSql/qsqldatabase.h>
  43. #include <QtSql/qsqlquery.h>
  44. #include <QtSql/qsqlrecord.h>
  45. #include <QtSql/qsqlfield.h>
  46. #include <QtSql/qsqlerror.h>
  47. #include <QtSql/qsqldriver.h>
  48. #include <QxCommon/QxBool.h>
  49. #include <QxDao/QxSoftDelete.h>
  50. #include <QxDao/QxDaoPointer.h>
  51. #include <QxDao/QxSqlQuery.h>
  52. #include <QxDao/QxSqlSaveMode.h>
  53. namespace qx {
  54. class QxSqlRelationParams;
  55. namespace dao {
  56. namespace detail {
  57. class IxDao_Helper;
  58. template <class T> struct QxDao_Count;
  59. template <class T> struct QxDao_Count_WithRelation;
  60. template <class T> struct QxDao_FetchById;
  61. template <class T> struct QxDao_FetchById_WithRelation;
  62. template <class T> struct QxDao_FetchAll;
  63. template <class T> struct QxDao_FetchAll_WithRelation;
  64. template <class T> struct QxDao_Insert;
  65. template <class T> struct QxDao_Insert_WithRelation;
  66. template <class T> struct QxDao_Update;
  67. template <class T> struct QxDao_Update_Optimized;
  68. template <class T> struct QxDao_Update_WithRelation;
  69. template <class T> struct QxDao_Save;
  70. template <class T> struct QxDao_Save_WithRelation;
  71. template <class T> struct QxDao_Save_WithRelation_Recursive;
  72. template <class T> struct QxDao_DeleteById;
  73. template <class T> struct QxDao_DeleteAll;
  74. template <class T> struct QxDao_Exist;
  75. template <class T> struct QxDao_CreateTable;
  76. template <class T> struct QxDao_Trigger;
  77. template <class T> struct QxDao_ExecuteQuery;
  78. } // namespace detail
  79. /*!
  80. * \ingroup QxDao
  81. * \brief Return the number of lines in the table (database) mapped to the C++ class T (registered into QxOrm context) and filtered by a user SQL query
  82. * \param query Define a user SQL query added to default SQL query builded by QxOrm library (optional parameter)
  83. * \param pDatabase Connection to database (you can manage your own connection pool for example, you can also define a transaction, etc.); if NULL, a valid connection for the current thread is provided by qx::QxSqlDatabase singleton class (optional parameter)
  84. *
  85. * qx::dao::count<T>() execute following SQL query :<br>
  86. * <i>SELECT COUNT(*) FROM my_table</i> + <i>WHERE my_query...</i>
  87. */
  88. template <class T>
  89. inline long count(const qx::QxSqlQuery & query = qx::QxSqlQuery(), QSqlDatabase * pDatabase = NULL)
  90. { return qx::dao::detail::QxDao_Count<T>::count(query, pDatabase); }
  91. /*!
  92. * \ingroup QxDao
  93. * \brief Return the number of lines in the table (database) mapped to the C++ class T (registered into QxOrm context) and filtered by a user SQL query
  94. * \param lCount Output parameter with the number of lines in the table associated to the SQL query
  95. * \param query Define a user SQL query added to default SQL query builded by QxOrm library (optional parameter)
  96. * \param pDatabase Connection to database (you can manage your own connection pool for example, you can also define a transaction, etc.); if NULL, a valid connection for the current thread is provided by qx::QxSqlDatabase singleton class (optional parameter)
  97. *
  98. * qx::dao::count<T>() execute following SQL query :<br>
  99. * <i>SELECT COUNT(*) FROM my_table</i> + <i>WHERE my_query...</i>
  100. */
  101. template <class T>
  102. inline QSqlError count(long & lCount, const qx::QxSqlQuery & query = qx::QxSqlQuery(), QSqlDatabase * pDatabase = NULL)
  103. { return qx::dao::detail::QxDao_Count<T>::count(lCount, query, pDatabase); }
  104. /*!
  105. * \ingroup QxDao
  106. * \brief Return the number of lines in the table (database) mapped to the C++ class T (registered into QxOrm context) and filtered by a user SQL query (with possibility to add relations to SQL query)
  107. * \param lCount Output parameter with the number of lines in the table associated to the SQL query
  108. * \param relation List of relationships keys to be fetched (eager fetch instead of default lazy fetch for a relation)
  109. * \param query Define a user SQL query added to default SQL query builded by QxOrm library (optional parameter)
  110. * \param pDatabase Connection to database (you can manage your own connection pool for example, you can also define a transaction, etc.); if NULL, a valid connection for the current thread is provided by qx::QxSqlDatabase singleton class (optional parameter)
  111. *
  112. * qx::dao::count_with_relation<T>() execute following SQL query :<br>
  113. * <i>SELECT COUNT(*) FROM my_table</i> + <i>XXX_JOINS_XXX</i> + <i>WHERE my_query...</i>
  114. */
  115. template <class T>
  116. inline QSqlError count_with_relation(long & lCount, const QStringList & relation, const qx::QxSqlQuery & query = qx::QxSqlQuery(), QSqlDatabase * pDatabase = NULL)
  117. { return qx::dao::detail::QxDao_Count_WithRelation<T>::count(lCount, relation, query, pDatabase); }
  118. /*!
  119. * \ingroup QxDao
  120. * \brief Insert an element or a list of elements into database
  121. * \param t Element (or list of elements) to be inserted into database
  122. * \param pDatabase Connection to database (you can manage your own connection pool for example, you can also define a transaction, etc.); if NULL, a valid connection for the current thread is provided by qx::QxSqlDatabase singleton class (optional parameter)
  123. * \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
  124. *
  125. * qx::dao::insert<T>() execute following SQL query :<br>
  126. * <i>INSERT INTO my_table (my_column_1, my_column_2, etc.) VALUES (?, ?, etc.)</i>
  127. */
  128. template <class T>
  129. inline QSqlError insert(T & t, QSqlDatabase * pDatabase = NULL)
  130. { return qx::dao::detail::QxDao_Insert<T>::insert(t, pDatabase); }
  131. /*!
  132. * \ingroup QxDao
  133. * \brief Insert (if no exist) or update (if already exist) an element or a list of elements into database
  134. * \param t Element (or list of elements) to be inserted (if no exist) or updated (if already exist) into database
  135. * \param pDatabase Connection to database (you can manage your own connection pool for example, you can also define a transaction, etc.); if NULL, a valid connection for the current thread is provided by qx::QxSqlDatabase singleton class (optional parameter)
  136. * \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
  137. *
  138. * qx::dao::save<T>() execute following SQL query :<br>
  139. * <i>INSERT INTO my_table (my_column_1, my_column_2, etc.) VALUES (?, ?, etc.)</i>
  140. * <br>or (if already exist into database) :<br>
  141. * <i>UPDATE my_table SET my_column_1 = ?, my_column_2 = ?, etc.</i>
  142. */
  143. template <class T>
  144. inline QSqlError save(T & t, QSqlDatabase * pDatabase = NULL)
  145. { return qx::dao::detail::QxDao_Save<T>::save(t, pDatabase); }
  146. /*!
  147. * \ingroup QxDao
  148. * \brief Delete a line (or list of lines) of a table (database) mapped to a C++ object of type T (registered into QxOrm context)
  149. * \param t Element (or list of elements) to be deleted into database
  150. * \param pDatabase Connection to database (you can manage your own connection pool for example, you can also define a transaction, etc.); if NULL, a valid connection for the current thread is provided by qx::QxSqlDatabase singleton class (optional parameter)
  151. * \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
  152. *
  153. * qx::dao::delete_by_id<T>() execute following SQL query :<br>
  154. * <i>DELETE FROM my_table WHERE my_id = ?</i><br>
  155. * <br>
  156. * If a soft delete behavior is defined for class T, qx::dao::delete_by_id<T>() execute following SQL query :<br>
  157. * <i>UPDATE my_table SET is_deleted='1' WHERE my_id = ?</i>
  158. */
  159. template <class T>
  160. inline QSqlError delete_by_id(T & t, QSqlDatabase * pDatabase = NULL)
  161. { return qx::dao::detail::QxDao_DeleteById<T>::deleteById(t, pDatabase, true); }
  162. /*!
  163. * \ingroup QxDao
  164. * \brief Destroy a line (or list of lines) of a table (database) mapped to a C++ object of type T (registered into QxOrm context), even if a soft delete behavior is defined for class T
  165. * \param t Element (or list of elements) to be destroyed into database
  166. * \param pDatabase Connection to database (you can manage your own connection pool for example, you can also define a transaction, etc.); if NULL, a valid connection for the current thread is provided by qx::QxSqlDatabase singleton class (optional parameter)
  167. * \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
  168. *
  169. * qx::dao::destroy_by_id<T>() execute following SQL query :<br>
  170. * <i>DELETE FROM my_table WHERE my_id = ?</i>
  171. */
  172. template <class T>
  173. inline QSqlError destroy_by_id(T & t, QSqlDatabase * pDatabase = NULL)
  174. { return qx::dao::detail::QxDao_DeleteById<T>::deleteById(t, pDatabase, false); }
  175. /*!
  176. * \ingroup QxDao
  177. * \brief Delete all lines of a table (database) mapped to a C++ class T (registered into QxOrm context)
  178. * \param pDatabase Connection to database (you can manage your own connection pool for example, you can also define a transaction, etc.); if NULL, a valid connection for the current thread is provided by qx::QxSqlDatabase singleton class (optional parameter)
  179. * \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
  180. *
  181. * qx::dao::delete_all<T>() execute following SQL query :<br>
  182. * <i>DELETE FROM my_table</i><br>
  183. * <br>
  184. * If a soft delete behavior is defined for class T, qx::dao::delete_all<T>() execute following SQL query :<br>
  185. * <i>UPDATE my_table SET is_deleted='1'</i>
  186. */
  187. template <class T>
  188. inline QSqlError delete_all(QSqlDatabase * pDatabase = NULL)
  189. { return qx::dao::detail::QxDao_DeleteAll<T>::deleteAll("", pDatabase, true); }
  190. /*!
  191. * \ingroup QxDao
  192. * \brief Destroy all lines of a table (database) mapped to a C++ class T (registered into QxOrm context), even if a soft delete behavior is defined for class T
  193. * \param pDatabase Connection to database (you can manage your own connection pool for example, you can also define a transaction, etc.); if NULL, a valid connection for the current thread is provided by qx::QxSqlDatabase singleton class (optional parameter)
  194. * \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
  195. *
  196. * qx::dao::destroy_all<T>() execute following SQL query :<br>
  197. * <i>DELETE FROM my_table</i>
  198. */
  199. template <class T>
  200. inline QSqlError destroy_all(QSqlDatabase * pDatabase = NULL)
  201. { return qx::dao::detail::QxDao_DeleteAll<T>::deleteAll("", pDatabase, false); }
  202. /*!
  203. * \ingroup QxDao
  204. * \brief Delete all lines of a table (database) mapped to a C++ class T (registered into QxOrm context) and filtered by a user SQL query
  205. * \param query Define a user SQL query added to default SQL query builded by QxOrm library
  206. * \param pDatabase Connection to database (you can manage your own connection pool for example, you can also define a transaction, etc.); if NULL, a valid connection for the current thread is provided by qx::QxSqlDatabase singleton class (optional parameter)
  207. * \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
  208. *
  209. * qx::dao::delete_by_query<T>() execute following SQL query :<br>
  210. * <i>DELETE FROM my_table</i> + <i>WHERE my_query...</i><br>
  211. * <br>
  212. * If a soft delete behavior is defined for class T, qx::dao::delete_by_query<T>() execute following SQL query :<br>
  213. * <i>UPDATE my_table SET is_deleted='1'</i> + <i>WHERE my_query...</i>
  214. */
  215. template <class T>
  216. inline QSqlError delete_by_query(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL)
  217. { return qx::dao::detail::QxDao_DeleteAll<T>::deleteAll(query, pDatabase, true); }
  218. /*!
  219. * \ingroup QxDao
  220. * \brief Destroy all lines of a table (database) mapped to a C++ class T (registered into QxOrm context) and filtered by a user SQL query, even if a soft delete behavior is defined for class T
  221. * \param query Define a user SQL query added to default SQL query builded by QxOrm library
  222. * \param pDatabase Connection to database (you can manage your own connection pool for example, you can also define a transaction, etc.); if NULL, a valid connection for the current thread is provided by qx::QxSqlDatabase singleton class (optional parameter)
  223. * \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
  224. *
  225. * qx::dao::destroy_by_query<T>() execute following SQL query :<br>
  226. * <i>DELETE FROM my_table</i> + <i>WHERE my_query...</i>
  227. */
  228. template <class T>
  229. inline QSqlError destroy_by_query(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL)
  230. { return qx::dao::detail::QxDao_DeleteAll<T>::deleteAll(query, pDatabase, false); }
  231. /*!
  232. * \ingroup QxDao
  233. * \brief Create a table into database (with all columns) mapped to a C++ class T (registered into QxOrm context) : be careful, this function can be used only with a SQLite database to create examples or prototypes; For other databases, it is recommended to use QxEntityEditor application or to manage the database schema with an external tool provided by the SGBD (SQLite Manager for SQLite, pgAdmin for PostgreSQL, MySQL Workbench for MySQL, etc...)
  234. * \param pDatabase Connection to database (you can manage your own connection pool for example, you can also define a transaction, etc.); if NULL, a valid connection for the current thread is provided by qx::QxSqlDatabase singleton class (optional parameter)
  235. * \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
  236. *
  237. * qx::dao::create_table<T>() execute following SQL query :<br>
  238. * <i>CREATE TABLE my_table (my_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, my_column_1 TEXT, my_column_2 TEXT, etc.)</i>
  239. */
  240. template <class T>
  241. inline QSqlError create_table(QSqlDatabase * pDatabase = NULL)
  242. { return qx::dao::detail::QxDao_CreateTable<T>::createTable(pDatabase); }
  243. /*!
  244. * \ingroup QxDao
  245. * \brief Search if an element (or list of elements) already exists into database
  246. * \param t Element (or list of elements) to be searched into database
  247. * \param pDatabase Connection to database (you can manage your own connection pool for example, you can also define a transaction, etc.); if NULL, a valid connection for the current thread is provided by qx::QxSqlDatabase singleton class (optional parameter)
  248. * \return Return true if element already exists into database; otherwise return false; if an error occurred, qx_bool object contains a description of database error executing SQL query
  249. *
  250. * qx::dao::exist<T>() execute following SQL query :<br>
  251. * <i>SELECT * FROM my_table WHERE my_id = ?</i>
  252. */
  253. template <class T>
  254. inline qx_bool exist(T & t, QSqlDatabase * pDatabase = NULL)
  255. { return qx::dao::detail::QxDao_Exist<T>::exist(t, pDatabase); }
  256. /*!
  257. * \ingroup QxDao
  258. * \brief Fetch an object t (retrieve all its properties) of type T (registered into QxOrm context) mapped to a table in the database (t must have a valid id before to be fetched without error)
  259. * \param relation List of relationships keys to be fetched (eager fetch instead of default lazy fetch for a relation) : use "|" separator to put many relationships keys into this parameter
  260. * \param t Instance (with a valid id) to be fetched (retrieve all properties from database)
  261. * \param pDatabase Connection to database (you can manage your own connection pool for example, you can also define a transaction, etc.); if NULL, a valid connection for the current thread is provided by qx::QxSqlDatabase singleton class (optional parameter)
  262. * \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
  263. *
  264. * qx::dao::fetch_by_id_with_relation<T>() execute following SQL query :<br>
  265. * <i>SELECT * FROM my_table WHERE my_id = ?</i>
  266. */
  267. template <class T>
  268. inline QSqlError fetch_by_id_with_relation(const QString & relation, T & t, QSqlDatabase * pDatabase = NULL)
  269. { return qx::dao::detail::QxDao_FetchById_WithRelation<T>::fetchById(relation, t, pDatabase); }
  270. /*!
  271. * \ingroup QxDao
  272. * \brief Fetch an object t (retrieve all its properties) of type T (registered into QxOrm context) mapped to a table in the database (t must have a valid id before to be fetched without error)
  273. * \param relation List of relationships keys to be fetched (eager fetch instead of default lazy fetch for a relation)
  274. * \param t Instance (with a valid id) to be fetched (retrieve all properties from database)
  275. * \param pDatabase Connection to database (you can manage your own connection pool for example, you can also define a transaction, etc.); if NULL, a valid connection for the current thread is provided by qx::QxSqlDatabase singleton class (optional parameter)
  276. * \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
  277. *
  278. * qx::dao::fetch_by_id_with_relation<T>() execute following SQL query :<br>
  279. * <i>SELECT * FROM my_table WHERE my_id = ?</i>
  280. */
  281. template <class T>
  282. inline QSqlError fetch_by_id_with_relation(const QStringList & relation, T & t, QSqlDatabase * pDatabase = NULL)
  283. { return qx::dao::detail::QxDao_FetchById_WithRelation<T>::fetchById(relation, t, pDatabase); }
  284. /*!
  285. * \ingroup QxDao
  286. * \brief Fetch an object t (retrieve all its properties and relationships) of type T (registered into QxOrm context) mapped to a table in the database (t must have a valid id before to be fetched without error)
  287. * \param t Instance (with a valid id) to be fetched (retrieve all properties and relationships from database)
  288. * \param pDatabase Connection to database (you can manage your own connection pool for example, you can also define a transaction, etc.); if NULL, a valid connection for the current thread is provided by qx::QxSqlDatabase singleton class (optional parameter)
  289. * \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
  290. *
  291. * qx::dao::fetch_by_id_with_all_relation<T>() execute following SQL query :<br>
  292. * <i>SELECT * FROM my_table WHERE my_id = ?</i>
  293. */
  294. template <class T>
  295. inline QSqlError fetch_by_id_with_all_relation(T & t, QSqlDatabase * pDatabase = NULL)
  296. { return qx::dao::detail::QxDao_FetchById_WithRelation<T>::fetchById("*", t, pDatabase); }
  297. /*!
  298. * \ingroup QxDao
  299. * \brief Fetch a list of objects (retrieve all elements and properties associated) of type T (container registered into QxOrm context) mapped to a table in the database
  300. * \param relation List of relationships keys to be fetched (eager fetch instead of default lazy fetch for a relation) : use "|" separator to put many relationships keys into this parameter
  301. * \param t Container to be fetched (retrieve all elements and properties associated); t is cleared before executing SQL query
  302. * \param pDatabase Connection to database (you can manage your own connection pool for example, you can also define a transaction, etc.); if NULL, a valid connection for the current thread is provided by qx::QxSqlDatabase singleton class (optional parameter)
  303. * \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
  304. *
  305. * qx::dao::fetch_all_with_relation<T>() execute following SQL query :<br>
  306. * <i>SELECT * FROM my_table</i>
  307. */
  308. template <class T>
  309. inline QSqlError fetch_all_with_relation(const QString & relation, T & t, QSqlDatabase * pDatabase = NULL)
  310. { return qx::dao::detail::QxDao_FetchAll_WithRelation<T>::fetchAll(relation, "", t, pDatabase); }
  311. /*!
  312. * \ingroup QxDao
  313. * \brief Fetch a list of objects (retrieve all elements and properties associated) of type T (container registered into QxOrm context) mapped to a table in the database
  314. * \param relation List of relationships keys to be fetched (eager fetch instead of default lazy fetch for a relation)
  315. * \param t Container to be fetched (retrieve all elements and properties associated); t is cleared before executing SQL query
  316. * \param pDatabase Connection to database (you can manage your own connection pool for example, you can also define a transaction, etc.); if NULL, a valid connection for the current thread is provided by qx::QxSqlDatabase singleton class (optional parameter)
  317. * \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
  318. *
  319. * qx::dao::fetch_all_with_relation<T>() execute following SQL query :<br>
  320. * <i>SELECT * FROM my_table</i>
  321. */
  322. template <class T>
  323. inline QSqlError fetch_all_with_relation(const QStringList & relation, T & t, QSqlDatabase * pDatabase = NULL)
  324. { return qx::dao::detail::QxDao_FetchAll_WithRelation<T>::fetchAll(relation, "", t, pDatabase); }
  325. /*!
  326. * \ingroup QxDao
  327. * \brief Fetch a list of objects (retrieve all elements and properties + all relationships associated) of type T (container registered into QxOrm context) mapped to a table in the database
  328. * \param t Container to be fetched (retrieve all elements and properties + all relationships associated); t is cleared before executing SQL query
  329. * \param pDatabase Connection to database (you can manage your own connection pool for example, you can also define a transaction, etc.); if NULL, a valid connection for the current thread is provided by qx::QxSqlDatabase singleton class (optional parameter)
  330. * \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
  331. *
  332. * qx::dao::fetch_all_with_all_relation<T>() execute following SQL query :<br>
  333. * <i>SELECT * FROM my_table</i>
  334. */
  335. template <class T>
  336. inline QSqlError fetch_all_with_all_relation(T & t, QSqlDatabase * pDatabase = NULL)
  337. { return qx::dao::detail::QxDao_FetchAll_WithRelation<T>::fetchAll("*", "", t, pDatabase); }
  338. /*!
  339. * \ingroup QxDao
  340. * \brief Fetch a list of objects (retrieve all elements and properties associated) of type T (container registered into QxOrm context) mapped to a table in the database and filtered by a user SQL query
  341. * \param relation List of relationships keys to be fetched (eager fetch instead of default lazy fetch for a relation) : use "|" separator to put many relationships keys into this parameter
  342. * \param query Define a user SQL query added to default SQL query builded by QxOrm library
  343. * \param t Container to be fetched (retrieve all elements and properties associated); t is cleared before executing SQL query
  344. * \param pDatabase Connection to database (you can manage your own connection pool for example, you can also define a transaction, etc.); if NULL, a valid connection for the current thread is provided by qx::QxSqlDatabase singleton class (optional parameter)
  345. * \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
  346. *
  347. * qx::dao::fetch_by_query_with_relation<T>() execute following SQL query :<br>
  348. * <i>SELECT * FROM my_table</i> + <i>WHERE my_query...</i>
  349. */
  350. template <class T>
  351. inline QSqlError fetch_by_query_with_relation(const QString & relation, const qx::QxSqlQuery & query, T & t, QSqlDatabase * pDatabase = NULL)
  352. { return qx::dao::detail::QxDao_FetchAll_WithRelation<T>::fetchAll(relation, query, t, pDatabase); }
  353. /*!
  354. * \ingroup QxDao
  355. * \brief Fetch a list of objects (retrieve all elements and properties associated) of type T (container registered into QxOrm context) mapped to a table in the database and filtered by a user SQL query
  356. * \param relation List of relationships keys to be fetched (eager fetch instead of default lazy fetch for a relation)
  357. * \param query Define a user SQL query added to default SQL query builded by QxOrm library
  358. * \param t Container to be fetched (retrieve all elements and properties associated); t is cleared before executing SQL query
  359. * \param pDatabase Connection to database (you can manage your own connection pool for example, you can also define a transaction, etc.); if NULL, a valid connection for the current thread is provided by qx::QxSqlDatabase singleton class (optional parameter)
  360. * \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
  361. *
  362. * qx::dao::fetch_by_query_with_relation<T>() execute following SQL query :<br>
  363. * <i>SELECT * FROM my_table</i> + <i>WHERE my_query...</i>
  364. */
  365. template <class T>
  366. inline QSqlError fetch_by_query_with_relation(const QStringList & relation, const qx::QxSqlQuery & query, T & t, QSqlDatabase * pDatabase = NULL)
  367. { return qx::dao::detail::QxDao_FetchAll_WithRelation<T>::fetchAll(relation, query, t, pDatabase); }
  368. /*!
  369. * \ingroup QxDao
  370. * \brief Fetch a list of objects (retrieve all elements and properties + all relationships associated) of type T (container registered into QxOrm context) mapped to a table in the database and filtered by a user SQL query
  371. * \param query Define a user SQL query added to default SQL query builded by QxOrm library
  372. * \param t Container to be fetched (retrieve all elements and properties + all relationships associated); t is cleared before executing SQL query
  373. * \param pDatabase Connection to database (you can manage your own connection pool for example, you can also define a transaction, etc.); if NULL, a valid connection for the current thread is provided by qx::QxSqlDatabase singleton class (optional parameter)
  374. * \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
  375. *
  376. * qx::dao::fetch_by_query_with_all_relation<T>() execute following SQL query :<br>
  377. * <i>SELECT * FROM my_table</i> + <i>WHERE my_query...</i>
  378. */
  379. template <class T>
  380. inline QSqlError fetch_by_query_with_all_relation(const qx::QxSqlQuery & query, T & t, QSqlDatabase * pDatabase = NULL)
  381. { return qx::dao::detail::QxDao_FetchAll_WithRelation<T>::fetchAll("*", query, t, pDatabase); }
  382. /*!
  383. * \ingroup QxDao
  384. * \brief Insert an element and its relationships (or a list of elements + relationships) into database
  385. * \param relation List of relationships keys to be inserted in others tables of database : use "|" separator to put many relationships keys into this parameter
  386. * \param t Element (or list of elements) to be inserted into database
  387. * \param pDatabase Connection to database (you can manage your own connection pool for example, you can also define a transaction, etc.); if NULL, a valid connection for the current thread is provided by qx::QxSqlDatabase singleton class (optional parameter)
  388. * \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
  389. *
  390. * qx::dao::insert_with_relation<T>() execute following SQL query :<br>
  391. * <i>INSERT INTO my_table (my_column_1, my_column_2, etc.) VALUES (?, ?, etc.)</i>
  392. */
  393. template <class T>
  394. inline QSqlError insert_with_relation(const QString & relation, T & t, QSqlDatabase * pDatabase = NULL)
  395. { return qx::dao::detail::QxDao_Insert_WithRelation<T>::insert(relation, t, pDatabase); }
  396. /*!
  397. * \ingroup QxDao
  398. * \brief Insert an element and its relationships (or a list of elements + relationships) into database
  399. * \param relation List of relationships keys to be inserted in others tables of database
  400. * \param t Element (or list of elements) to be inserted into database
  401. * \param pDatabase Connection to database (you can manage your own connection pool for example, you can also define a transaction, etc.); if NULL, a valid connection for the current thread is provided by qx::QxSqlDatabase singleton class (optional parameter)
  402. * \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
  403. *
  404. * qx::dao::insert_with_relation<T>() execute following SQL query :<br>
  405. * <i>INSERT INTO my_table (my_column_1, my_column_2, etc.) VALUES (?, ?, etc.)</i>
  406. */
  407. template <class T>
  408. inline QSqlError insert_with_relation(const QStringList & relation, T & t, QSqlDatabase * pDatabase = NULL)
  409. { return qx::dao::detail::QxDao_Insert_WithRelation<T>::insert(relation, t, pDatabase); }
  410. /*!
  411. * \ingroup QxDao
  412. * \brief Insert an element and all its relationships (or a list of elements + all relationships) into database
  413. * \param t Element (or list of elements) to be inserted into database
  414. * \param pDatabase Connection to database (you can manage your own connection pool for example, you can also define a transaction, etc.); if NULL, a valid connection for the current thread is provided by qx::QxSqlDatabase singleton class (optional parameter)
  415. * \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
  416. *
  417. * qx::dao::insert_with_all_relation<T>() execute following SQL query :<br>
  418. * <i>INSERT INTO my_table (my_column_1, my_column_2, etc.) VALUES (?, ?, etc.)</i>
  419. */
  420. template <class T>
  421. inline QSqlError insert_with_all_relation(T & t, QSqlDatabase * pDatabase = NULL)
  422. { return qx::dao::detail::QxDao_Insert_WithRelation<T>::insert("*", t, pDatabase); }
  423. /*!
  424. * \ingroup QxDao
  425. * \brief Update an element and its relationships (or a list of elements + relationships) into database
  426. * \param relation List of relationships keys to be updated in others tables of database : use "|" separator to put many relationships keys into this parameter
  427. * \param t Element (or list of elements) to be updated into database
  428. * \param pDatabase Connection to database (you can manage your own connection pool for example, you can also define a transaction, etc.); if NULL, a valid connection for the current thread is provided by qx::QxSqlDatabase singleton class (optional parameter)
  429. * \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
  430. *
  431. * qx::dao::update_with_relation<T>() execute following SQL query :<br>
  432. * <i>UPDATE my_table SET my_column_1 = ?, my_column_2 = ?, etc.</i>
  433. */
  434. template <class T>
  435. inline QSqlError update_with_relation(const QString & relation, T & t, QSqlDatabase * pDatabase = NULL)
  436. { return qx::dao::detail::QxDao_Update_WithRelation<T>::update(relation, "", t, pDatabase); }
  437. /*!
  438. * \ingroup QxDao
  439. * \brief Update an element and its relationships (or a list of elements + relationships) into database (adding a user SQL query to the default SQL query builded by QxOrm library)
  440. * \param relation List of relationships keys to be updated in others tables of database : use "|" separator to put many relationships keys into this parameter
  441. * \param query Define a user SQL query added to default SQL query builded by QxOrm library
  442. * \param t Element (or list of elements) to be updated into database
  443. * \param pDatabase Connection to database (you can manage your own connection pool for example, you can also define a transaction, etc.); if NULL, a valid connection for the current thread is provided by qx::QxSqlDatabase singleton class (optional parameter)
  444. * \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
  445. *
  446. * qx::dao::update_by_query_with_relation<T>() execute following SQL query :<br>
  447. * <i>UPDATE my_table SET my_column_1 = ?, my_column_2 = ?, etc.</i> + <i>WHERE my_query...</i>
  448. */
  449. template <class T>
  450. inline QSqlError update_by_query_with_relation(const QString & relation, const qx::QxSqlQuery & query, T & t, QSqlDatabase * pDatabase = NULL)
  451. { return qx::dao::detail::QxDao_Update_WithRelation<T>::update(relation, query, t, pDatabase); }
  452. /*!
  453. * \ingroup QxDao
  454. * \brief Update an element and its relationships (or a list of elements + relationships) into database
  455. * \param relation List of relationships keys to be updated in others tables of database
  456. * \param t Element (or list of elements) to be updated into database
  457. * \param pDatabase Connection to database (you can manage your own connection pool for example, you can also define a transaction, etc.); if NULL, a valid connection for the current thread is provided by qx::QxSqlDatabase singleton class (optional parameter)
  458. * \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
  459. *
  460. * qx::dao::update_with_relation<T>() execute following SQL query :<br>
  461. * <i>UPDATE my_table SET my_column_1 = ?, my_column_2 = ?, etc.</i>
  462. */
  463. template <class T>
  464. inline QSqlError update_with_relation(const QStringList & relation, T & t, QSqlDatabase * pDatabase = NULL)
  465. { return qx::dao::detail::QxDao_Update_WithRelation<T>::update(relation, "", t, pDatabase); }
  466. /*!
  467. * \ingroup QxDao
  468. * \brief Update an element and its relationships (or a list of elements + relationships) into database (adding a user SQL query to the default SQL query builded by QxOrm library)
  469. * \param relation List of relationships keys to be updated in others tables of database
  470. * \param query Define a user SQL query added to default SQL query builded by QxOrm library
  471. * \param t Element (or list of elements) to be updated into database
  472. * \param pDatabase Connection to database (you can manage your own connection pool for example, you can also define a transaction, etc.); if NULL, a valid connection for the current thread is provided by qx::QxSqlDatabase singleton class (optional parameter)
  473. * \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
  474. *
  475. * qx::dao::update_by_query_with_relation<T>() execute following SQL query :<br>
  476. * <i>UPDATE my_table SET my_column_1 = ?, my_column_2 = ?, etc.</i> + <i>WHERE my_query...</i>
  477. */
  478. template <class T>
  479. inline QSqlError update_by_query_with_relation(const QStringList & relation, const qx::QxSqlQuery & query, T & t, QSqlDatabase * pDatabase = NULL)
  480. { return qx::dao::detail::QxDao_Update_WithRelation<T>::update(relation, query, t, pDatabase); }
  481. /*!
  482. * \ingroup QxDao
  483. * \brief Update an element and all its relationships (or a list of elements + all relationships) into database
  484. * \param t Element (or list of elements) to be updated into database
  485. * \param pDatabase Connection to database (you can manage your own connection pool for example, you can also define a transaction, etc.); if NULL, a valid connection for the current thread is provided by qx::QxSqlDatabase singleton class (optional parameter)
  486. * \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
  487. *
  488. * qx::dao::update_with_all_relation<T>() execute following SQL query :<br>
  489. * <i>UPDATE my_table SET my_column_1 = ?, my_column_2 = ?, etc.</i>
  490. */
  491. template <class T>
  492. inline QSqlError update_with_all_relation(T & t, QSqlDatabase * pDatabase = NULL)
  493. { return qx::dao::detail::QxDao_Update_WithRelation<T>::update("*", "", t, pDatabase); }
  494. /*!
  495. * \ingroup QxDao
  496. * \brief Update an element and all its relationships (or a list of elements + all relationships) into database (adding a user SQL query to the default SQL query builded by QxOrm library)
  497. * \param query Define a user SQL query added to default SQL query builded by QxOrm library
  498. * \param t Element (or list of elements) to be updated into database
  499. * \param pDatabase Connection to database (you can manage your own connection pool for example, you can also define a transaction, etc.); if NULL, a valid connection for the current thread is provided by qx::QxSqlDatabase singleton class (optional parameter)
  500. * \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
  501. *
  502. * qx::dao::update_by_query_with_all_relation<T>() execute following SQL query :<br>
  503. * <i>UPDATE my_table SET my_column_1 = ?, my_column_2 = ?, etc.</i> + <i>WHERE my_query...</i>
  504. */
  505. template <class T>
  506. inline QSqlError update_by_query_with_all_relation(const qx::QxSqlQuery & query, T & t, QSqlDatabase * pDatabase = NULL)
  507. { return qx::dao::detail::QxDao_Update_WithRelation<T>::update("*", query, t, pDatabase); }
  508. /*!
  509. * \ingroup QxDao
  510. * \brief Insert (if no exist) or update (if already exist) an element and its relationships (or a list of elements + relationships) into database
  511. * \param relation List of relationships keys to be inserted or updated in others tables of database : use "|" separator to put many relationships keys into this parameter
  512. * \param t Element (or list of elements) to be inserted (if no exist) or updated (if already exist) into database
  513. * \param pDatabase Connection to database (you can manage your own connection pool for example, you can also define a transaction, etc.); if NULL, a valid connection for the current thread is provided by qx::QxSqlDatabase singleton class (optional parameter)
  514. * \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
  515. *
  516. * qx::dao::save_with_relation<T>() execute following SQL query :<br>
  517. * <i>INSERT INTO my_table (my_column_1, my_column_2, etc.) VALUES (?, ?, etc.)</i>
  518. * <br>or (if already exist into database) :<br>
  519. * <i>UPDATE my_table SET my_column_1 = ?, my_column_2 = ?, etc.</i>
  520. */
  521. template <class T>
  522. inline QSqlError save_with_relation(const QString & relation, T & t, QSqlDatabase * pDatabase = NULL)
  523. { return qx::dao::detail::QxDao_Save_WithRelation<T>::save(relation, t, pDatabase); }
  524. /*!
  525. * \ingroup QxDao
  526. * \brief Insert (if no exist) or update (if already exist) an element and its relationships (or a list of elements + relationships) into database
  527. * \param relation List of relationships keys to be inserted or updated in others tables of database
  528. * \param t Element (or list of elements) to be inserted (if no exist) or updated (if already exist) into database
  529. * \param pDatabase Connection to database (you can manage your own connection pool for example, you can also define a transaction, etc.); if NULL, a valid connection for the current thread is provided by qx::QxSqlDatabase singleton class (optional parameter)
  530. * \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
  531. *
  532. * qx::dao::save_with_relation<T>() execute following SQL query :<br>
  533. * <i>INSERT INTO my_table (my_column_1, my_column_2, etc.) VALUES (?, ?, etc.)</i>
  534. * <br>or (if already exist into database) :<br>
  535. * <i>UPDATE my_table SET my_column_1 = ?, my_column_2 = ?, etc.</i>
  536. */
  537. template <class T>
  538. inline QSqlError save_with_relation(const QStringList & relation, T & t, QSqlDatabase * pDatabase = NULL)
  539. { return qx::dao::detail::QxDao_Save_WithRelation<T>::save(relation, t, pDatabase); }
  540. /*!
  541. * \ingroup QxDao
  542. * \brief Insert (if no exist) or update (if already exist) an element and all its relationships (or a list of elements + all relationships) into database
  543. * \param t Element (or list of elements) to be inserted (if no exist) or updated (if already exist) into database
  544. * \param pDatabase Connection to database (you can manage your own connection pool for example, you can also define a transaction, etc.); if NULL, a valid connection for the current thread is provided by qx::QxSqlDatabase singleton class (optional parameter)
  545. * \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
  546. *
  547. * qx::dao::save_with_all_relation<T>() execute following SQL query :<br>
  548. * <i>INSERT INTO my_table (my_column_1, my_column_2, etc.) VALUES (?, ?, etc.)</i>
  549. * <br>or (if already exist into database) :<br>
  550. * <i>UPDATE my_table SET my_column_1 = ?, my_column_2 = ?, etc.</i>
  551. */
  552. template <class T>
  553. inline QSqlError save_with_all_relation(T & t, QSqlDatabase * pDatabase = NULL)
  554. { return qx::dao::detail::QxDao_Save_WithRelation<T>::save("*", t, pDatabase); }
  555. /*!
  556. * \ingroup QxDao
  557. * \brief Insert (if no exist) or update (if already exist) recursively an element and all levels of relationships (or a list of elements + all levels of relationships) into database, useful to save a tree structure for example
  558. * \param t Element (or list of elements) to be inserted (if no exist) or updated (if already exist) into database
  559. * \param eSaveMode To improve performance, use this parameter to indicate if you just want to insert or update all elements in database
  560. * \param pDatabase Connection to database (you can manage your own connection pool for example, you can also define a transaction, etc.); if NULL, a valid connection for the current thread is provided by qx::QxSqlDatabase singleton class (optional parameter)
  561. * \param pRelationParams Keep this parameter as NULL, it is used internally by QxOrm library to iterate over each level of relationship
  562. * \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
  563. *
  564. * qx::dao::save_with_relation_recursive<T>() execute following SQL query :<br>
  565. * <i>INSERT INTO my_table (my_column_1, my_column_2, etc.) VALUES (?, ?, etc.)</i>
  566. * <br>or (if already exist into database) :<br>
  567. * <i>UPDATE my_table SET my_column_1 = ?, my_column_2 = ?, etc.</i><br>
  568. * <br>
  569. * <b>Note :</b> to improve performance, and if you know that you are just inserting or updating items in database, you can use the parameter <i>eSaveMode</i> :<br>
  570. * - <i>qx::dao::save_mode::e_check_insert_or_update</i> : check before saving if item already exists in database ;<br>
  571. * - <i>qx::dao::save_mode::e_insert_only</i> : only insert items in database (use only 1 SQL query to insert collection of items) ;<br>
  572. * - <i>qx::dao::save_mode::e_update_only</i> : only update items in database (use only 1 SQL query to update collection of items).
  573. */
  574. template <class T>
  575. inline QSqlError save_with_relation_recursive(T & t, qx::dao::save_mode::e_save_mode eSaveMode = qx::dao::save_mode::e_check_insert_or_update, QSqlDatabase * pDatabase = NULL, qx::QxSqlRelationParams * pRelationParams = NULL)
  576. { return qx::dao::detail::QxDao_Save_WithRelation_Recursive<T>::save(t, eSaveMode, pDatabase, pRelationParams); }
  577. /*!
  578. * \ingroup QxDao
  579. * \brief Fetch an object t (retrieve all its properties) of type T (registered into QxOrm context) mapped to a table in the database (t must have a valid id before to be fetched without error)
  580. * \param t Instance (with a valid id) to be fetched (retrieve all properties from database)
  581. * \param pDatabase Connection to database (you can manage your own connection pool for example, you can also define a transaction, etc.); if NULL, a valid connection for the current thread is provided by qx::QxSqlDatabase singleton class (optional parameter)
  582. * \param columns List of database table columns (mapped to properties of C++ class T) to be fetched (optional parameter)
  583. * \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
  584. *
  585. * qx::dao::fetch_by_id<T>() execute following SQL query :<br>
  586. * <i>SELECT * FROM my_table WHERE my_id = ?</i>
  587. */
  588. template <class T>
  589. inline QSqlError fetch_by_id(T & t, QSqlDatabase * pDatabase = NULL, const QStringList & columns = QStringList())
  590. { return qx::dao::detail::QxDao_FetchById<T>::fetchById(t, pDatabase, columns); }
  591. /*!
  592. * \ingroup QxDao
  593. * \brief Fetch a list of objects (retrieve all elements and properties associated) of type T (container registered into QxOrm context) mapped to a table in the database
  594. * \param t Container to be fetched (retrieve all elements and properties associated); t is cleared before executing SQL query
  595. * \param pDatabase Connection to database (you can manage your own connection pool for example, you can also define a transaction, etc.); if NULL, a valid connection for the current thread is provided by qx::QxSqlDatabase singleton class (optional parameter)
  596. * \param columns List of database table columns (mapped to properties of C++ class T) to be fetched (optional parameter)
  597. * \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
  598. *
  599. * qx::dao::fetch_all<T>() execute following SQL query :<br>
  600. * <i>SELECT * FROM my_table</i>
  601. */
  602. template <class T>
  603. inline QSqlError fetch_all(T & t, QSqlDatabase * pDatabase = NULL, const QStringList & columns = QStringList())
  604. { return qx::dao::detail::QxDao_FetchAll<T>::fetchAll("", t, pDatabase, columns); }
  605. /*!
  606. * \ingroup QxDao
  607. * \brief Fetch a list of objects (retrieve all elements and properties associated) of type T (container registered into QxOrm context) mapped to a table in the database and filtered by a user SQL query
  608. * \param query Define a user SQL query added to default SQL query builded by QxOrm library
  609. * \param t Container to be fetched (retrieve all elements and properties associated); t is cleared before executing SQL query
  610. * \param pDatabase Connection to database (you can manage your own connection pool for example, you can also define a transaction, etc.); if NULL, a valid connection for the current thread is provided by qx::QxSqlDatabase singleton class (optional parameter)
  611. * \param columns List of database table columns (mapped to properties of C++ class T) to be fetched (optional parameter)
  612. * \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
  613. *
  614. * qx::dao::fetch_by_query<T>() execute following SQL query :<br>
  615. * <i>SELECT * FROM my_table</i> + <i>WHERE my_query...</i>
  616. */
  617. template <class T>
  618. inline QSqlError fetch_by_query(const qx::QxSqlQuery & query, T & t, QSqlDatabase * pDatabase = NULL, const QStringList & columns = QStringList())
  619. { return qx::dao::detail::QxDao_FetchAll<T>::fetchAll(query, t, pDatabase, columns); }
  620. /*!
  621. * \ingroup QxDao
  622. * \brief Update an element or a list of elements into database
  623. * \param t Element (or list of elements) to be updated into database
  624. * \param pDatabase Connection to database (you can manage your own connection pool for example, you can also define a transaction, etc.); if NULL, a valid connection for the current thread is provided by qx::QxSqlDatabase singleton class (optional parameter)
  625. * \param columns List of database table columns (mapped to properties of C++ class T) to be updated (optional parameter)
  626. * \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
  627. *
  628. * qx::dao::update<T>() execute following SQL query :<br>
  629. * <i>UPDATE my_table SET my_column_1 = ?, my_column_2 = ?, etc.</i>
  630. */
  631. template <class T>
  632. inline QSqlError update(T & t, QSqlDatabase * pDatabase = NULL, const QStringList & columns = QStringList())
  633. { return qx::dao::detail::QxDao_Update<T>::update("", t, pDatabase, columns); }
  634. /*!
  635. * \ingroup QxDao
  636. * \brief Update an element or a list of elements into database (adding a user SQL query to the default SQL query builded by QxOrm library)
  637. * \param query Define a user SQL query added to default SQL query builded by QxOrm library
  638. * \param t Element (or list of elements) to be updated into database
  639. * \param pDatabase Connection to database (you can manage your own connection pool for example, you can also define a transaction, etc.); if NULL, a valid connection for the current thread is provided by qx::QxSqlDatabase singleton class (optional parameter)
  640. * \param columns List of database table columns (mapped to properties of C++ class T) to be updated (optional parameter)
  641. * \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
  642. *
  643. * qx::dao::update_by_query<T>() execute following SQL query :<br>
  644. * <i>UPDATE my_table SET my_column_1 = ?, my_column_2 = ?, etc.</i> + <i>WHERE my_query...</i>
  645. */
  646. template <class T>
  647. inline QSqlError update_by_query(const qx::QxSqlQuery & query, T & t, QSqlDatabase * pDatabase = NULL, const QStringList & columns = QStringList())
  648. { return qx::dao::detail::QxDao_Update<T>::update(query, t, pDatabase, columns); }
  649. /*!
  650. * \ingroup QxDao
  651. * \brief Update only modified fields/properties of an element or a list of elements into database (using is dirty pattern and qx::dao::ptr<T> smart-pointer)
  652. * \param ptr Element (or list of elements) to be updated into database
  653. * \param pDatabase Connection to database (you can manage your own connection pool for example, you can also define a transaction, etc.); if NULL, a valid connection for the current thread is provided by qx::QxSqlDatabase singleton class (optional parameter)
  654. * \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
  655. *
  656. * qx::dao::update_optimized<T>() execute following SQL query :<br>
  657. * <i>UPDATE my_table SET my_column_1 = ?, my_column_2 = ?, etc.</i>
  658. */
  659. template <class T>
  660. inline QSqlError update_optimized(qx::dao::ptr<T> & ptr, QSqlDatabase * pDatabase = NULL)
  661. { return qx::dao::detail::QxDao_Update_Optimized<T>::update_optimized("", ptr, pDatabase); }
  662. /*!
  663. * \ingroup QxDao
  664. * \brief Update only modified fields/properties of an element or a list of elements into database (using is dirty pattern and qx::dao::ptr<T> smart-pointer), adding a user SQL query to the default SQL query builded by QxOrm library
  665. * \param query Define a user SQL query added to default SQL query builded by QxOrm library
  666. * \param ptr Element (or list of elements) to be updated into database
  667. * \param pDatabase Connection to database (you can manage your own connection pool for example, you can also define a transaction, etc.); if NULL, a valid connection for the current thread is provided by qx::QxSqlDatabase singleton class (optional parameter)
  668. * \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
  669. *
  670. * qx::dao::update_optimized_by_query<T>() execute following SQL query :<br>
  671. * <i>UPDATE my_table SET my_column_1 = ?, my_column_2 = ?, etc.</i> + <i>WHERE my_query...</i>
  672. */
  673. template <class T>
  674. inline QSqlError update_optimized_by_query(const qx::QxSqlQuery & query, qx::dao::ptr<T> & ptr, QSqlDatabase * pDatabase = NULL)
  675. { return qx::dao::detail::QxDao_Update_Optimized<T>::update_optimized(query, ptr, pDatabase); }
  676. /*!
  677. * \ingroup QxDao
  678. * \brief Execute a custom SQL query or a stored procedure, all columns that can be mapped to the instance of type T will be fetched automatically
  679. * \param query Define a custom SQL query or a stored procedure to call
  680. * \param t Instance of type T, all columns that can be mapped to this instance will be fetched automatically
  681. * \param pDatabase Connection to database (you can manage your own connection pool for example, you can also define a transaction, etc.); if NULL, a valid connection for the current thread is provided by qx::QxSqlDatabase singleton class (optional parameter)
  682. * \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
  683. */
  684. template <class T>
  685. inline QSqlError execute_query(qx::QxSqlQuery & query, T & t, QSqlDatabase * pDatabase = NULL)
  686. { return qx::dao::detail::QxDao_ExecuteQuery<T>::executeQuery(query, t, pDatabase); }
  687. /*!
  688. * \ingroup QxDao
  689. * \brief Callback before inserting an object into database (<a href="https://www.qxorm.com/qxorm_en/faq.html#faq_130" target="_blank">here is an example using QxOrm Trigger</a>)
  690. */
  691. template <class T>
  692. inline void on_before_insert(T * t, qx::dao::detail::IxDao_Helper * dao)
  693. { qx::dao::detail::QxDao_Trigger<T>::onBeforeInsert(t, dao); }
  694. /*!
  695. * \ingroup QxDao
  696. * \brief Callback before updating an object into database (<a href="https://www.qxorm.com/qxorm_en/faq.html#faq_130" target="_blank">here is an example using QxOrm Trigger</a>)
  697. */
  698. template <class T>
  699. inline void on_before_update(T * t, qx::dao::detail::IxDao_Helper * dao)
  700. { qx::dao::detail::QxDao_Trigger<T>::onBeforeUpdate(t, dao); }
  701. /*!
  702. * \ingroup QxDao
  703. * \brief Callback before deleting an object into database (<a href="https://www.qxorm.com/qxorm_en/faq.html#faq_130" target="_blank">here is an example using QxOrm Trigger</a>)
  704. */
  705. template <class T>
  706. inline void on_before_delete(T * t, qx::dao::detail::IxDao_Helper * dao)
  707. { qx::dao::detail::QxDao_Trigger<T>::onBeforeDelete(t, dao); }
  708. /*!
  709. * \ingroup QxDao
  710. * \brief Callback before fetching an object from database (<a href="https://www.qxorm.com/qxorm_en/faq.html#faq_130" target="_blank">here is an example using QxOrm Trigger</a>)
  711. */
  712. template <class T>
  713. inline void on_before_fetch(T * t, qx::dao::detail::IxDao_Helper * dao)
  714. { qx::dao::detail::QxDao_Trigger<T>::onBeforeFetch(t, dao); }
  715. /*!
  716. * \ingroup QxDao
  717. * \brief Callback after inserting an object into database (<a href="https://www.qxorm.com/qxorm_en/faq.html#faq_130" target="_blank">here is an example using QxOrm Trigger</a>)
  718. */
  719. template <class T>
  720. inline void on_after_insert(T * t, qx::dao::detail::IxDao_Helper * dao)
  721. { qx::dao::detail::QxDao_Trigger<T>::onAfterInsert(t, dao); }
  722. /*!
  723. * \ingroup QxDao
  724. * \brief Callback after updating an object into database (<a href="https://www.qxorm.com/qxorm_en/faq.html#faq_130" target="_blank">here is an example using QxOrm Trigger</a>)
  725. */
  726. template <class T>
  727. inline void on_after_update(T * t, qx::dao::detail::IxDao_Helper * dao)
  728. { qx::dao::detail::QxDao_Trigger<T>::onAfterUpdate(t, dao); }
  729. /*!
  730. * \ingroup QxDao
  731. * \brief Callback after deleting an object into database (<a href="https://www.qxorm.com/qxorm_en/faq.html#faq_130" target="_blank">here is an example using QxOrm trigger</a>)
  732. */
  733. template <class T>
  734. inline void on_after_delete(T * t, qx::dao::detail::IxDao_Helper * dao)
  735. { qx::dao::detail::QxDao_Trigger<T>::onAfterDelete(t, dao); }
  736. /*!
  737. * \ingroup QxDao
  738. * \brief Callback after fetching an object from database (<a href="https://www.qxorm.com/qxorm_en/faq.html#faq_130" target="_blank">here is an example using QxOrm trigger</a>)
  739. */
  740. template <class T>
  741. inline void on_after_fetch(T * t, qx::dao::detail::IxDao_Helper * dao)
  742. { qx::dao::detail::QxDao_Trigger<T>::onAfterFetch(t, dao); }
  743. } // namespace dao
  744. } // namespace qx
  745. #endif // _QX_DAO_H_