QxDaoThrowable.h 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  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_THROWABLE_H_
  32. #define _QX_DAO_THROWABLE_H_
  33. #ifdef _MSC_VER
  34. #pragma once
  35. #endif
  36. /*!
  37. * \file QxDaoThrowable.h
  38. * \author Lionel Marty
  39. * \ingroup QxDao
  40. * \brief Same functions as qx::dao namespace, but throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred (instead of returning a QSqlError instance)
  41. */
  42. #include <QxDao/QxDao.h>
  43. #include <QxDao/QxSqlError.h>
  44. namespace qx {
  45. namespace dao {
  46. /*!
  47. * \ingroup QxDao
  48. * \brief Same functions as qx::dao namespace, but throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred (instead of returning a QSqlError instance)
  49. */
  50. namespace throwable {
  51. /// @cond TO_AVOID_DOXYGEN_WARNINGS
  52. /*!
  53. * \ingroup QxDao
  54. * \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
  55. * \param lCount Output parameter with the number of lines in the table associated to the SQL query
  56. * \param query Define a user SQL query added to default SQL query builded by QxOrm library (optional parameter)
  57. * \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)
  58. *
  59. * qx::dao::throwable::count<T>() execute following SQL query :<br>
  60. * <i>SELECT COUNT(*) FROM my_table</i> + <i>WHERE my_query...</i>
  61. */
  62. template <class T>
  63. inline void count(long & lCount, const qx::QxSqlQuery & query = qx::QxSqlQuery(), QSqlDatabase * pDatabase = NULL)
  64. { QSqlError err = qx::dao::detail::QxDao_Count<T>::count(lCount, query, pDatabase); if (err.isValid()) { throw qx::dao::sql_error(err); } }
  65. /*!
  66. * \ingroup QxDao
  67. * \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)
  68. * \param lCount Output parameter with the number of lines in the table associated to the SQL query
  69. * \param relation List of relationships keys to be fetched (eager fetch instead of default lazy fetch for a relation)
  70. * \param query Define a user SQL query added to default SQL query builded by QxOrm library (optional parameter)
  71. * \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)
  72. *
  73. * qx::dao::throwable::count_with_relation<T>() execute following SQL query :<br>
  74. * <i>SELECT COUNT(*) FROM my_table</i> + <i>XXX_JOINS_XXX</i> + <i>WHERE my_query...</i>
  75. */
  76. template <class T>
  77. inline void count_with_relation(long & lCount, const QStringList & relation, const qx::QxSqlQuery & query = qx::QxSqlQuery(), QSqlDatabase * pDatabase = NULL)
  78. { QSqlError err = qx::dao::detail::QxDao_Count_WithRelation<T>::count(lCount, relation, query, pDatabase); if (err.isValid()) { throw qx::dao::sql_error(err); } }
  79. /*!
  80. * \ingroup QxDao
  81. * \brief Insert an element or a list of elements into database
  82. * \param t Element (or list of elements) to be inserted into database
  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. * \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
  85. *
  86. * qx::dao::throwable::insert<T>() execute following SQL query :<br>
  87. * <i>INSERT INTO my_table (my_column_1, my_column_2, etc.) VALUES (?, ?, etc.)</i>
  88. */
  89. template <class T>
  90. inline void insert(T & t, QSqlDatabase * pDatabase = NULL)
  91. { QSqlError err = qx::dao::detail::QxDao_Insert<T>::insert(t, pDatabase); if (err.isValid()) { throw qx::dao::sql_error(err); } }
  92. /*!
  93. * \ingroup QxDao
  94. * \brief Insert (if no exist) or update (if already exist) an element or a list of elements into database
  95. * \param t Element (or list of elements) to be inserted (if no exist) or updated (if already exist) into database
  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. * \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
  98. *
  99. * qx::dao::throwable::save<T>() execute following SQL query :<br>
  100. * <i>INSERT INTO my_table (my_column_1, my_column_2, etc.) VALUES (?, ?, etc.)</i>
  101. * <br>or (if already exist into database) :<br>
  102. * <i>UPDATE my_table SET my_column_1 = ?, my_column_2 = ?, etc.</i>
  103. */
  104. template <class T>
  105. inline void save(T & t, QSqlDatabase * pDatabase = NULL)
  106. { QSqlError err = qx::dao::detail::QxDao_Save<T>::save(t, pDatabase); if (err.isValid()) { throw qx::dao::sql_error(err); } }
  107. /*!
  108. * \ingroup QxDao
  109. * \brief Delete a line (or list of lines) of a table (database) mapped to a C++ object of type T (registered into QxOrm context)
  110. * \param t Element (or list of elements) to be deleted into database
  111. * \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)
  112. * \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
  113. *
  114. * qx::dao::throwable::delete_by_id<T>() execute following SQL query :<br>
  115. * <i>DELETE FROM my_table WHERE my_id = ?</i><br>
  116. * <br>
  117. * If a soft delete behavior is defined for class T, qx::dao::throwable::delete_by_id<T>() execute following SQL query :<br>
  118. * <i>UPDATE my_table SET is_deleted='1' WHERE my_id = ?</i>
  119. */
  120. template <class T>
  121. inline void delete_by_id(T & t, QSqlDatabase * pDatabase = NULL)
  122. { QSqlError err = qx::dao::detail::QxDao_DeleteById<T>::deleteById(t, pDatabase, true); if (err.isValid()) { throw qx::dao::sql_error(err); } }
  123. /*!
  124. * \ingroup QxDao
  125. * \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
  126. * \param t Element (or list of elements) to be destroyed into database
  127. * \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)
  128. * \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
  129. *
  130. * qx::dao::throwable::destroy_by_id<T>() execute following SQL query :<br>
  131. * <i>DELETE FROM my_table WHERE my_id = ?</i>
  132. */
  133. template <class T>
  134. inline void destroy_by_id(T & t, QSqlDatabase * pDatabase = NULL)
  135. { QSqlError err = qx::dao::detail::QxDao_DeleteById<T>::deleteById(t, pDatabase, false); if (err.isValid()) { throw qx::dao::sql_error(err); } }
  136. /*!
  137. * \ingroup QxDao
  138. * \brief Delete all lines of a table (database) mapped to a C++ class T (registered into QxOrm context)
  139. * \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)
  140. * \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
  141. *
  142. * qx::dao::throwable::delete_all<T>() execute following SQL query :<br>
  143. * <i>DELETE FROM my_table</i><br>
  144. * <br>
  145. * If a soft delete behavior is defined for class T, qx::dao::throwable::delete_all<T>() execute following SQL query :<br>
  146. * <i>UPDATE my_table SET is_deleted='1'</i>
  147. */
  148. template <class T>
  149. inline void delete_all(QSqlDatabase * pDatabase = NULL)
  150. { QSqlError err = qx::dao::detail::QxDao_DeleteAll<T>::deleteAll("", pDatabase, true); if (err.isValid()) { throw qx::dao::sql_error(err); } }
  151. /*!
  152. * \ingroup QxDao
  153. * \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
  154. * \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)
  155. * \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
  156. *
  157. * qx::dao::throwable::destroy_all<T>() execute following SQL query :<br>
  158. * <i>DELETE FROM my_table</i>
  159. */
  160. template <class T>
  161. inline void destroy_all(QSqlDatabase * pDatabase = NULL)
  162. { QSqlError err = qx::dao::detail::QxDao_DeleteAll<T>::deleteAll("", pDatabase, false); if (err.isValid()) { throw qx::dao::sql_error(err); } }
  163. /*!
  164. * \ingroup QxDao
  165. * \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
  166. * \param query Define a user SQL query added to default SQL query builded by QxOrm library
  167. * \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)
  168. * \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
  169. *
  170. * qx::dao::throwable::delete_by_query<T>() execute following SQL query :<br>
  171. * <i>DELETE FROM my_table</i> + <i>WHERE my_query...</i><br>
  172. * <br>
  173. * If a soft delete behavior is defined for class T, qx::dao::throwable::delete_by_query<T>() execute following SQL query :<br>
  174. * <i>UPDATE my_table SET is_deleted='1'</i> + <i>WHERE my_query...</i>
  175. */
  176. template <class T>
  177. inline void delete_by_query(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL)
  178. { QSqlError err = qx::dao::detail::QxDao_DeleteAll<T>::deleteAll(query, pDatabase, true); if (err.isValid()) { throw qx::dao::sql_error(err); } }
  179. /*!
  180. * \ingroup QxDao
  181. * \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
  182. * \param query Define a user SQL query added to default SQL query builded by QxOrm library
  183. * \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)
  184. * \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
  185. *
  186. * qx::dao::throwable::destroy_by_query<T>() execute following SQL query :<br>
  187. * <i>DELETE FROM my_table</i> + <i>WHERE my_query...</i>
  188. */
  189. template <class T>
  190. inline void destroy_by_query(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL)
  191. { QSqlError err = qx::dao::detail::QxDao_DeleteAll<T>::deleteAll(query, pDatabase, false); if (err.isValid()) { throw qx::dao::sql_error(err); } }
  192. /*!
  193. * \ingroup QxDao
  194. * \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...)
  195. * \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)
  196. * \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
  197. *
  198. * qx::dao::throwable::create_table<T>() execute following SQL query :<br>
  199. * <i>CREATE TABLE my_table (my_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, my_column_1 TEXT, my_column_2 TEXT, etc.)</i>
  200. */
  201. template <class T>
  202. inline void create_table(QSqlDatabase * pDatabase = NULL)
  203. { QSqlError err = qx::dao::detail::QxDao_CreateTable<T>::createTable(pDatabase); if (err.isValid()) { throw qx::dao::sql_error(err); } }
  204. /*!
  205. * \ingroup QxDao
  206. * \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)
  207. * \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
  208. * \param t Instance (with a valid id) to be fetched (retrieve all properties from database)
  209. * \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)
  210. * \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
  211. *
  212. * qx::dao::throwable::fetch_by_id_with_relation<T>() execute following SQL query :<br>
  213. * <i>SELECT * FROM my_table WHERE my_id = ?</i>
  214. */
  215. template <class T>
  216. inline void fetch_by_id_with_relation(const QString & relation, T & t, QSqlDatabase * pDatabase = NULL)
  217. { QSqlError err = qx::dao::detail::QxDao_FetchById_WithRelation<T>::fetchById(relation, t, pDatabase); if (err.isValid()) { throw qx::dao::sql_error(err); } }
  218. /*!
  219. * \ingroup QxDao
  220. * \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)
  221. * \param relation List of relationships keys to be fetched (eager fetch instead of default lazy fetch for a relation)
  222. * \param t Instance (with a valid id) to be fetched (retrieve all properties from database)
  223. * \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)
  224. * \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
  225. *
  226. * qx::dao::throwable::fetch_by_id_with_relation<T>() execute following SQL query :<br>
  227. * <i>SELECT * FROM my_table WHERE my_id = ?</i>
  228. */
  229. template <class T>
  230. inline void fetch_by_id_with_relation(const QStringList & relation, T & t, QSqlDatabase * pDatabase = NULL)
  231. { QSqlError err = qx::dao::detail::QxDao_FetchById_WithRelation<T>::fetchById(relation, t, pDatabase); if (err.isValid()) { throw qx::dao::sql_error(err); } }
  232. /*!
  233. * \ingroup QxDao
  234. * \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)
  235. * \param t Instance (with a valid id) to be fetched (retrieve all properties and relationships from database)
  236. * \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)
  237. * \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
  238. *
  239. * qx::dao::throwable::fetch_by_id_with_all_relation<T>() execute following SQL query :<br>
  240. * <i>SELECT * FROM my_table WHERE my_id = ?</i>
  241. */
  242. template <class T>
  243. inline void fetch_by_id_with_all_relation(T & t, QSqlDatabase * pDatabase = NULL)
  244. { QSqlError err = qx::dao::detail::QxDao_FetchById_WithRelation<T>::fetchById("*", t, pDatabase); if (err.isValid()) { throw qx::dao::sql_error(err); } }
  245. /*!
  246. * \ingroup QxDao
  247. * \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
  248. * \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
  249. * \param t Container to be fetched (retrieve all elements and properties associated); t is cleared before executing SQL query
  250. * \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)
  251. * \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
  252. *
  253. * qx::dao::throwable::fetch_all_with_relation<T>() execute following SQL query :<br>
  254. * <i>SELECT * FROM my_table</i>
  255. */
  256. template <class T>
  257. inline void fetch_all_with_relation(const QString & relation, T & t, QSqlDatabase * pDatabase = NULL)
  258. { QSqlError err = qx::dao::detail::QxDao_FetchAll_WithRelation<T>::fetchAll(relation, "", t, pDatabase); if (err.isValid()) { throw qx::dao::sql_error(err); } }
  259. /*!
  260. * \ingroup QxDao
  261. * \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
  262. * \param relation List of relationships keys to be fetched (eager fetch instead of default lazy fetch for a relation)
  263. * \param t Container to be fetched (retrieve all elements and properties associated); t is cleared before executing SQL query
  264. * \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)
  265. * \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
  266. *
  267. * qx::dao::throwable::fetch_all_with_relation<T>() execute following SQL query :<br>
  268. * <i>SELECT * FROM my_table</i>
  269. */
  270. template <class T>
  271. inline void fetch_all_with_relation(const QStringList & relation, T & t, QSqlDatabase * pDatabase = NULL)
  272. { QSqlError err = qx::dao::detail::QxDao_FetchAll_WithRelation<T>::fetchAll(relation, "", t, pDatabase); if (err.isValid()) { throw qx::dao::sql_error(err); } }
  273. /*!
  274. * \ingroup QxDao
  275. * \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
  276. * \param t Container to be fetched (retrieve all elements and properties + all relationships associated); t is cleared before executing SQL query
  277. * \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)
  278. * \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
  279. *
  280. * qx::dao::throwable::fetch_all_with_all_relation<T>() execute following SQL query :<br>
  281. * <i>SELECT * FROM my_table</i>
  282. */
  283. template <class T>
  284. inline void fetch_all_with_all_relation(T & t, QSqlDatabase * pDatabase = NULL)
  285. { QSqlError err = qx::dao::detail::QxDao_FetchAll_WithRelation<T>::fetchAll("*", "", t, pDatabase); if (err.isValid()) { throw qx::dao::sql_error(err); } }
  286. /*!
  287. * \ingroup QxDao
  288. * \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
  289. * \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
  290. * \param query Define a user SQL query added to default SQL query builded by QxOrm library
  291. * \param t Container to be fetched (retrieve all elements and properties associated); t is cleared before executing SQL query
  292. * \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)
  293. * \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
  294. *
  295. * qx::dao::throwable::fetch_by_query_with_relation<T>() execute following SQL query :<br>
  296. * <i>SELECT * FROM my_table</i> + <i>WHERE my_query...</i>
  297. */
  298. template <class T>
  299. inline void fetch_by_query_with_relation(const QString & relation, const qx::QxSqlQuery & query, T & t, QSqlDatabase * pDatabase = NULL)
  300. { QSqlError err = qx::dao::detail::QxDao_FetchAll_WithRelation<T>::fetchAll(relation, query, t, pDatabase); if (err.isValid()) { throw qx::dao::sql_error(err); } }
  301. /*!
  302. * \ingroup QxDao
  303. * \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
  304. * \param relation List of relationships keys to be fetched (eager fetch instead of default lazy fetch for a relation)
  305. * \param query Define a user SQL query added to default SQL query builded by QxOrm library
  306. * \param t Container to be fetched (retrieve all elements and properties associated); t is cleared before executing SQL query
  307. * \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)
  308. * \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
  309. *
  310. * qx::dao::throwable::fetch_by_query_with_relation<T>() execute following SQL query :<br>
  311. * <i>SELECT * FROM my_table</i> + <i>WHERE my_query...</i>
  312. */
  313. template <class T>
  314. inline void fetch_by_query_with_relation(const QStringList & relation, const qx::QxSqlQuery & query, T & t, QSqlDatabase * pDatabase = NULL)
  315. { QSqlError err = qx::dao::detail::QxDao_FetchAll_WithRelation<T>::fetchAll(relation, query, t, pDatabase); if (err.isValid()) { throw qx::dao::sql_error(err); } }
  316. /*!
  317. * \ingroup QxDao
  318. * \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
  319. * \param query Define a user SQL query added to default SQL query builded by QxOrm library
  320. * \param t Container to be fetched (retrieve all elements and properties + all relationships associated); t is cleared before executing SQL query
  321. * \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)
  322. * \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
  323. *
  324. * qx::dao::throwable::fetch_by_query_with_all_relation<T>() execute following SQL query :<br>
  325. * <i>SELECT * FROM my_table</i> + <i>WHERE my_query...</i>
  326. */
  327. template <class T>
  328. inline void fetch_by_query_with_all_relation(const qx::QxSqlQuery & query, T & t, QSqlDatabase * pDatabase = NULL)
  329. { QSqlError err = qx::dao::detail::QxDao_FetchAll_WithRelation<T>::fetchAll("*", query, t, pDatabase); if (err.isValid()) { throw qx::dao::sql_error(err); } }
  330. /*!
  331. * \ingroup QxDao
  332. * \brief Insert an element and its relationships (or a list of elements + relationships) into database
  333. * \param relation List of relationships keys to be inserted in others tables of database : use "|" separator to put many relationships keys into this parameter
  334. * \param t Element (or list of elements) to be inserted into database
  335. * \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)
  336. * \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
  337. *
  338. * qx::dao::throwable::insert_with_relation<T>() execute following SQL query :<br>
  339. * <i>INSERT INTO my_table (my_column_1, my_column_2, etc.) VALUES (?, ?, etc.)</i>
  340. */
  341. template <class T>
  342. inline void insert_with_relation(const QString & relation, T & t, QSqlDatabase * pDatabase = NULL)
  343. { QSqlError err = qx::dao::detail::QxDao_Insert_WithRelation<T>::insert(relation, t, pDatabase); if (err.isValid()) { throw qx::dao::sql_error(err); } }
  344. /*!
  345. * \ingroup QxDao
  346. * \brief Insert an element and its relationships (or a list of elements + relationships) into database
  347. * \param relation List of relationships keys to be inserted in others tables of database
  348. * \param t Element (or list of elements) to be inserted into database
  349. * \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)
  350. * \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
  351. *
  352. * qx::dao::throwable::insert_with_relation<T>() execute following SQL query :<br>
  353. * <i>INSERT INTO my_table (my_column_1, my_column_2, etc.) VALUES (?, ?, etc.)</i>
  354. */
  355. template <class T>
  356. inline void insert_with_relation(const QStringList & relation, T & t, QSqlDatabase * pDatabase = NULL)
  357. { QSqlError err = qx::dao::detail::QxDao_Insert_WithRelation<T>::insert(relation, t, pDatabase); if (err.isValid()) { throw qx::dao::sql_error(err); } }
  358. /*!
  359. * \ingroup QxDao
  360. * \brief Insert an element and all its relationships (or a list of elements + all relationships) into database
  361. * \param t Element (or list of elements) to be inserted into database
  362. * \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)
  363. * \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
  364. *
  365. * qx::dao::throwable::insert_with_all_relation<T>() execute following SQL query :<br>
  366. * <i>INSERT INTO my_table (my_column_1, my_column_2, etc.) VALUES (?, ?, etc.)</i>
  367. */
  368. template <class T>
  369. inline void insert_with_all_relation(T & t, QSqlDatabase * pDatabase = NULL)
  370. { QSqlError err = qx::dao::detail::QxDao_Insert_WithRelation<T>::insert("*", t, pDatabase); if (err.isValid()) { throw qx::dao::sql_error(err); } }
  371. /*!
  372. * \ingroup QxDao
  373. * \brief Update an element and its relationships (or a list of elements + relationships) into database
  374. * \param relation List of relationships keys to be updated in others tables of database : use "|" separator to put many relationships keys into this parameter
  375. * \param t Element (or list of elements) to be updated into database
  376. * \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)
  377. * \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
  378. *
  379. * qx::dao::throwable::update_with_relation<T>() execute following SQL query :<br>
  380. * <i>UPDATE my_table SET my_column_1 = ?, my_column_2 = ?, etc.</i>
  381. */
  382. template <class T>
  383. inline void update_with_relation(const QString & relation, T & t, QSqlDatabase * pDatabase = NULL)
  384. { QSqlError err = qx::dao::detail::QxDao_Update_WithRelation<T>::update(relation, "", t, pDatabase); if (err.isValid()) { throw qx::dao::sql_error(err); } }
  385. /*!
  386. * \ingroup QxDao
  387. * \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)
  388. * \param relation List of relationships keys to be updated in others tables of database : use "|" separator to put many relationships keys into this parameter
  389. * \param query Define a user SQL query added to default SQL query builded by QxOrm library
  390. * \param t Element (or list of elements) to be updated into database
  391. * \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)
  392. * \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
  393. *
  394. * qx::dao::throwable::update_by_query_with_relation<T>() execute following SQL query :<br>
  395. * <i>UPDATE my_table SET my_column_1 = ?, my_column_2 = ?, etc.</i> + <i>WHERE my_query...</i>
  396. */
  397. template <class T>
  398. inline void update_by_query_with_relation(const QString & relation, const qx::QxSqlQuery & query, T & t, QSqlDatabase * pDatabase = NULL)
  399. { QSqlError err = qx::dao::detail::QxDao_Update_WithRelation<T>::update(relation, query, t, pDatabase); if (err.isValid()) { throw qx::dao::sql_error(err); } }
  400. /*!
  401. * \ingroup QxDao
  402. * \brief Update an element and its relationships (or a list of elements + relationships) into database
  403. * \param relation List of relationships keys to be updated in others tables of database
  404. * \param t Element (or list of elements) to be updated into database
  405. * \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)
  406. * \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
  407. *
  408. * qx::dao::throwable::update_with_relation<T>() execute following SQL query :<br>
  409. * <i>UPDATE my_table SET my_column_1 = ?, my_column_2 = ?, etc.</i>
  410. */
  411. template <class T>
  412. inline void update_with_relation(const QStringList & relation, T & t, QSqlDatabase * pDatabase = NULL)
  413. { QSqlError err = qx::dao::detail::QxDao_Update_WithRelation<T>::update(relation, "", t, pDatabase); if (err.isValid()) { throw qx::dao::sql_error(err); } }
  414. /*!
  415. * \ingroup QxDao
  416. * \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)
  417. * \param relation List of relationships keys to be updated in others tables of database
  418. * \param query Define a user SQL query added to default SQL query builded by QxOrm library
  419. * \param t Element (or list of elements) to be updated into database
  420. * \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)
  421. * \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
  422. *
  423. * qx::dao::throwable::update_by_query_with_relation<T>() execute following SQL query :<br>
  424. * <i>UPDATE my_table SET my_column_1 = ?, my_column_2 = ?, etc.</i> + <i>WHERE my_query...</i>
  425. */
  426. template <class T>
  427. inline void update_by_query_with_relation(const QStringList & relation, const qx::QxSqlQuery & query, T & t, QSqlDatabase * pDatabase = NULL)
  428. { QSqlError err = qx::dao::detail::QxDao_Update_WithRelation<T>::update(relation, query, t, pDatabase); if (err.isValid()) { throw qx::dao::sql_error(err); } }
  429. /*!
  430. * \ingroup QxDao
  431. * \brief Update an element and all its relationships (or a list of elements + all relationships) into database
  432. * \param t Element (or list of elements) to be updated into database
  433. * \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)
  434. * \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
  435. *
  436. * qx::dao::throwable::update_with_all_relation<T>() execute following SQL query :<br>
  437. * <i>UPDATE my_table SET my_column_1 = ?, my_column_2 = ?, etc.</i>
  438. */
  439. template <class T>
  440. inline void update_with_all_relation(T & t, QSqlDatabase * pDatabase = NULL)
  441. { QSqlError err = qx::dao::detail::QxDao_Update_WithRelation<T>::update("*", "", t, pDatabase); if (err.isValid()) { throw qx::dao::sql_error(err); } }
  442. /*!
  443. * \ingroup QxDao
  444. * \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)
  445. * \param query Define a user SQL query added to default SQL query builded by QxOrm library
  446. * \param t Element (or list of elements) to be updated into database
  447. * \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)
  448. * \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
  449. *
  450. * qx::dao::throwable::update_by_query_with_all_relation<T>() execute following SQL query :<br>
  451. * <i>UPDATE my_table SET my_column_1 = ?, my_column_2 = ?, etc.</i> + <i>WHERE my_query...</i>
  452. */
  453. template <class T>
  454. inline void update_by_query_with_all_relation(const qx::QxSqlQuery & query, T & t, QSqlDatabase * pDatabase = NULL)
  455. { QSqlError err = qx::dao::detail::QxDao_Update_WithRelation<T>::update("*", query, t, pDatabase); if (err.isValid()) { throw qx::dao::sql_error(err); } }
  456. /*!
  457. * \ingroup QxDao
  458. * \brief Insert (if no exist) or update (if already exist) an element and its relationships (or a list of elements + relationships) into database
  459. * \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
  460. * \param t Element (or list of elements) to be inserted (if no exist) or updated (if already exist) into database
  461. * \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)
  462. * \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
  463. *
  464. * qx::dao::throwable::save_with_relation<T>() execute following SQL query :<br>
  465. * <i>INSERT INTO my_table (my_column_1, my_column_2, etc.) VALUES (?, ?, etc.)</i>
  466. * <br>or (if already exist into database) :<br>
  467. * <i>UPDATE my_table SET my_column_1 = ?, my_column_2 = ?, etc.</i>
  468. */
  469. template <class T>
  470. inline void save_with_relation(const QString & relation, T & t, QSqlDatabase * pDatabase = NULL)
  471. { QSqlError err = qx::dao::detail::QxDao_Save_WithRelation<T>::save(relation, t, pDatabase); if (err.isValid()) { throw qx::dao::sql_error(err); } }
  472. /*!
  473. * \ingroup QxDao
  474. * \brief Insert (if no exist) or update (if already exist) an element and its relationships (or a list of elements + relationships) into database
  475. * \param relation List of relationships keys to be inserted or updated in others tables of database
  476. * \param t Element (or list of elements) to be inserted (if no exist) or updated (if already exist) into database
  477. * \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)
  478. * \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
  479. *
  480. * qx::dao::throwable::save_with_relation<T>() execute following SQL query :<br>
  481. * <i>INSERT INTO my_table (my_column_1, my_column_2, etc.) VALUES (?, ?, etc.)</i>
  482. * <br>or (if already exist into database) :<br>
  483. * <i>UPDATE my_table SET my_column_1 = ?, my_column_2 = ?, etc.</i>
  484. */
  485. template <class T>
  486. inline void save_with_relation(const QStringList & relation, T & t, QSqlDatabase * pDatabase = NULL)
  487. { QSqlError err = qx::dao::detail::QxDao_Save_WithRelation<T>::save(relation, t, pDatabase); if (err.isValid()) { throw qx::dao::sql_error(err); } }
  488. /*!
  489. * \ingroup QxDao
  490. * \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
  491. * \param t Element (or list of elements) to be inserted (if no exist) or updated (if already exist) into database
  492. * \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)
  493. * \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
  494. *
  495. * qx::dao::throwable::save_with_all_relation<T>() execute following SQL query :<br>
  496. * <i>INSERT INTO my_table (my_column_1, my_column_2, etc.) VALUES (?, ?, etc.)</i>
  497. * <br>or (if already exist into database) :<br>
  498. * <i>UPDATE my_table SET my_column_1 = ?, my_column_2 = ?, etc.</i>
  499. */
  500. template <class T>
  501. inline void save_with_all_relation(T & t, QSqlDatabase * pDatabase = NULL)
  502. { QSqlError err = qx::dao::detail::QxDao_Save_WithRelation<T>::save("*", t, pDatabase); if (err.isValid()) { throw qx::dao::sql_error(err); } }
  503. /*!
  504. * \ingroup QxDao
  505. * \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
  506. * \param t Element (or list of elements) to be inserted (if no exist) or updated (if already exist) into database
  507. * \param eSaveMode To improve performance, use this parameter to indicate if you just want to insert or update all elements in database
  508. * \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)
  509. * \param pRelationParams Keep this parameter as NULL, it is used internally by QxOrm library to iterate over each level of relationship
  510. * \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
  511. *
  512. * qx::dao::throwable::save_with_relation_recursive<T>() execute following SQL query :<br>
  513. * <i>INSERT INTO my_table (my_column_1, my_column_2, etc.) VALUES (?, ?, etc.)</i>
  514. * <br>or (if already exist into database) :<br>
  515. * <i>UPDATE my_table SET my_column_1 = ?, my_column_2 = ?, etc.</i><br>
  516. * <br>
  517. * <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>
  518. * - <i>qx::dao::save_mode::e_check_insert_or_update</i> : check before saving if item already exists in database ;<br>
  519. * - <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>
  520. * - <i>qx::dao::save_mode::e_update_only</i> : only update items in database (use only 1 SQL query to update collection of items).
  521. */
  522. template <class T>
  523. inline void 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)
  524. { QSqlError err = qx::dao::detail::QxDao_Save_WithRelation_Recursive<T>::save(t, eSaveMode, pDatabase, pRelationParams); if (err.isValid()) { throw qx::dao::sql_error(err); } }
  525. /*!
  526. * \ingroup QxDao
  527. * \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)
  528. * \param t Instance (with a valid id) to be fetched (retrieve all properties from 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. * \param columns List of database table columns (mapped to properties of C++ class T) to be fetched (optional parameter)
  531. * \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
  532. *
  533. * qx::dao::throwable::fetch_by_id<T>() execute following SQL query :<br>
  534. * <i>SELECT * FROM my_table WHERE my_id = ?</i>
  535. */
  536. template <class T>
  537. inline void fetch_by_id(T & t, QSqlDatabase * pDatabase = NULL, const QStringList & columns = QStringList())
  538. { QSqlError err = qx::dao::detail::QxDao_FetchById<T>::fetchById(t, pDatabase, columns); if (err.isValid()) { throw qx::dao::sql_error(err); } }
  539. /*!
  540. * \ingroup QxDao
  541. * \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
  542. * \param t Container to be fetched (retrieve all elements and properties associated); t is cleared before executing SQL query
  543. * \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)
  544. * \param columns List of database table columns (mapped to properties of C++ class T) to be fetched (optional parameter)
  545. * \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
  546. *
  547. * qx::dao::throwable::fetch_all<T>() execute following SQL query :<br>
  548. * <i>SELECT * FROM my_table</i>
  549. */
  550. template <class T>
  551. inline void fetch_all(T & t, QSqlDatabase * pDatabase = NULL, const QStringList & columns = QStringList())
  552. { QSqlError err = qx::dao::detail::QxDao_FetchAll<T>::fetchAll("", t, pDatabase, columns); if (err.isValid()) { throw qx::dao::sql_error(err); } }
  553. /*!
  554. * \ingroup QxDao
  555. * \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
  556. * \param query Define a user SQL query added to default SQL query builded by QxOrm library
  557. * \param t Container to be fetched (retrieve all elements and properties associated); t is cleared before executing SQL query
  558. * \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)
  559. * \param columns List of database table columns (mapped to properties of C++ class T) to be fetched (optional parameter)
  560. * \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
  561. *
  562. * qx::dao::throwable::fetch_by_query<T>() execute following SQL query :<br>
  563. * <i>SELECT * FROM my_table</i> + <i>WHERE my_query...</i>
  564. */
  565. template <class T>
  566. inline void fetch_by_query(const qx::QxSqlQuery & query, T & t, QSqlDatabase * pDatabase = NULL, const QStringList & columns = QStringList())
  567. { QSqlError err = qx::dao::detail::QxDao_FetchAll<T>::fetchAll(query, t, pDatabase, columns); if (err.isValid()) { throw qx::dao::sql_error(err); } }
  568. /*!
  569. * \ingroup QxDao
  570. * \brief Update an element or a list of elements into database
  571. * \param t Element (or list of elements) to be updated into database
  572. * \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)
  573. * \param columns List of database table columns (mapped to properties of C++ class T) to be updated (optional parameter)
  574. * \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
  575. *
  576. * qx::dao::throwable::update<T>() execute following SQL query :<br>
  577. * <i>UPDATE my_table SET my_column_1 = ?, my_column_2 = ?, etc.</i>
  578. */
  579. template <class T>
  580. inline void update(T & t, QSqlDatabase * pDatabase = NULL, const QStringList & columns = QStringList())
  581. { QSqlError err = qx::dao::detail::QxDao_Update<T>::update("", t, pDatabase, columns); if (err.isValid()) { throw qx::dao::sql_error(err); } }
  582. /*!
  583. * \ingroup QxDao
  584. * \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)
  585. * \param query Define a user SQL query added to default SQL query builded by QxOrm library
  586. * \param t Element (or list of elements) to be updated into database
  587. * \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)
  588. * \param columns List of database table columns (mapped to properties of C++ class T) to be updated (optional parameter)
  589. * \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
  590. *
  591. * qx::dao::throwable::update_by_query<T>() execute following SQL query :<br>
  592. * <i>UPDATE my_table SET my_column_1 = ?, my_column_2 = ?, etc.</i> + <i>WHERE my_query...</i>
  593. */
  594. template <class T>
  595. inline void update_by_query(const qx::QxSqlQuery & query, T & t, QSqlDatabase * pDatabase = NULL, const QStringList & columns = QStringList())
  596. { QSqlError err = qx::dao::detail::QxDao_Update<T>::update(query, t, pDatabase, columns); if (err.isValid()) { throw qx::dao::sql_error(err); } }
  597. /*!
  598. * \ingroup QxDao
  599. * \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)
  600. * \param ptr Element (or list of elements) to be updated into database
  601. * \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)
  602. * \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
  603. *
  604. * qx::dao::throwable::update_optimized<T>() execute following SQL query :<br>
  605. * <i>UPDATE my_table SET my_column_1 = ?, my_column_2 = ?, etc.</i>
  606. */
  607. template <class T>
  608. inline void update_optimized(qx::dao::ptr<T> & ptr, QSqlDatabase * pDatabase = NULL)
  609. { QSqlError err = qx::dao::detail::QxDao_Update_Optimized<T>::update_optimized("", ptr, pDatabase); if (err.isValid()) { throw qx::dao::sql_error(err); } }
  610. /*!
  611. * \ingroup QxDao
  612. * \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
  613. * \param query Define a user SQL query added to default SQL query builded by QxOrm library
  614. * \param ptr Element (or list of elements) to be updated into database
  615. * \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)
  616. * \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
  617. *
  618. * qx::dao::throwable::update_optimized_by_query<T>() execute following SQL query :<br>
  619. * <i>UPDATE my_table SET my_column_1 = ?, my_column_2 = ?, etc.</i> + <i>WHERE my_query...</i>
  620. */
  621. template <class T>
  622. inline void update_optimized_by_query(const qx::QxSqlQuery & query, qx::dao::ptr<T> & ptr, QSqlDatabase * pDatabase = NULL)
  623. { QSqlError err = qx::dao::detail::QxDao_Update_Optimized<T>::update_optimized(query, ptr, pDatabase); if (err.isValid()) { throw qx::dao::sql_error(err); } }
  624. /*!
  625. * \ingroup QxDao
  626. * \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
  627. * \param query Define a custom SQL query or a stored procedure to call
  628. * \param t Instance of type T, all columns that can be mapped to this instance will be fetched automatically
  629. * \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)
  630. * \return Throw a <i>qx::dao::sql_error</i> exception when a SQL error occurred
  631. */
  632. template <class T>
  633. inline void execute_query(qx::QxSqlQuery & query, T & t, QSqlDatabase * pDatabase = NULL)
  634. { QSqlError err = qx::dao::detail::QxDao_ExecuteQuery<T>::executeQuery(query, t, pDatabase); if (err.isValid()) { throw qx::dao::sql_error(err); } }
  635. inline void call_query(qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL)
  636. { QSqlError err = qx::dao::call_query(query, pDatabase); if (err.isValid()) { throw qx::dao::sql_error(err); } }
  637. /// @endcond
  638. } // namespace throwable
  639. } // namespace dao
  640. } // namespace qx
  641. #endif // _QX_DAO_THROWABLE_H_