database.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. /**
  3. * Elgg database procedural code.
  4. *
  5. * Includes functions for reading data, writing data, and escaping queries.
  6. *
  7. * @package Elgg.Core
  8. * @subpackage Database
  9. */
  10. /**
  11. * Queue a query for running during shutdown that writes to the database
  12. *
  13. * @param string $query The query to execute
  14. * @param string $handler The optional handler for processing the result
  15. *
  16. * @return boolean
  17. */
  18. function execute_delayed_write_query($query, $handler = "") {
  19. return _elgg_services()->db->registerDelayedQuery($query, 'write', $handler);
  20. }
  21. /**
  22. * Queue a query for running during shutdown that reads from the database
  23. *
  24. * @param string $query The query to execute
  25. * @param string $handler The optional handler for processing the result
  26. *
  27. * @return boolean
  28. */
  29. function execute_delayed_read_query($query, $handler = "") {
  30. return _elgg_services()->db->registerDelayedQuery($query, 'read', $handler);
  31. }
  32. /**
  33. * Retrieve rows from the database.
  34. *
  35. * Queries are executed with {@link execute_query()} and results
  36. * are retrieved with {@link mysql_fetch_object()}. If a callback
  37. * function $callback is defined, each row will be passed as the single
  38. * argument to $callback. If no callback function is defined, the
  39. * entire result set is returned as an array.
  40. *
  41. * @param mixed $query The query being passed.
  42. * @param string $callback Optionally, the name of a function to call back to on each row
  43. *
  44. * @return array An array of database result objects or callback function results. If the query
  45. * returned nothing, an empty array.
  46. */
  47. function get_data($query, $callback = "") {
  48. return _elgg_services()->db->getData($query, $callback);
  49. }
  50. /**
  51. * Retrieve a single row from the database.
  52. *
  53. * Similar to {@link get_data()} but returns only the first row
  54. * matched. If a callback function $callback is specified, the row will be passed
  55. * as the only argument to $callback.
  56. *
  57. * @param mixed $query The query to execute.
  58. * @param string $callback A callback function
  59. *
  60. * @return mixed A single database result object or the result of the callback function.
  61. */
  62. function get_data_row($query, $callback = "") {
  63. return _elgg_services()->db->getDataRow($query, $callback);
  64. }
  65. /**
  66. * Insert a row into the database.
  67. *
  68. * @note Altering the DB invalidates all queries in {@link $DB_QUERY_CACHE}.
  69. *
  70. * @param mixed $query The query to execute.
  71. *
  72. * @return int|false The database id of the inserted row if a AUTO_INCREMENT field is
  73. * defined, 0 if not, and false on failure.
  74. */
  75. function insert_data($query) {
  76. return _elgg_services()->db->insertData($query);
  77. }
  78. /**
  79. * Update a row in the database.
  80. *
  81. * @note Altering the DB invalidates all queries in {@link $DB_QUERY_CACHE}.
  82. *
  83. * @param string $query The query to run.
  84. *
  85. * @return bool
  86. */
  87. function update_data($query) {
  88. return _elgg_services()->db->updateData($query);
  89. }
  90. /**
  91. * Remove a row from the database.
  92. *
  93. * @note Altering the DB invalidates all queries in {@link $DB_QUERY_CACHE}.
  94. *
  95. * @param string $query The SQL query to run
  96. *
  97. * @return int|false The number of affected rows or false on failure
  98. */
  99. function delete_data($query) {
  100. return _elgg_services()->db->deleteData($query);
  101. }
  102. /**
  103. * Runs a full database script from disk.
  104. *
  105. * The file specified should be a standard SQL file as created by
  106. * mysqldump or similar. Statements must be terminated with ;
  107. * and a newline character (\n or \r\n) with only one statement per line.
  108. *
  109. * The special string 'prefix_' is replaced with the database prefix
  110. * as defined in {@link $CONFIG->dbprefix}.
  111. *
  112. * @warning Errors do not halt execution of the script. If a line
  113. * generates an error, the error message is saved and the
  114. * next line is executed. After the file is run, any errors
  115. * are displayed as a {@link DatabaseException}
  116. *
  117. * @param string $scriptlocation The full path to the script
  118. *
  119. * @return void
  120. * @throws DatabaseException
  121. */
  122. function run_sql_script($scriptlocation) {
  123. _elgg_services()->db->runSqlScript($scriptlocation);
  124. }
  125. /**
  126. * Sanitize a string for database use.
  127. *
  128. * @param string $string The string to sanitize
  129. * @return string
  130. */
  131. function sanitize_string($string) {
  132. return _elgg_services()->db->sanitizeString($string);
  133. }
  134. /**
  135. * Wrapper function for alternate English spelling (@see sanitize_string)
  136. *
  137. * @param string $string The string to sanitize
  138. * @return string
  139. */
  140. function sanitise_string($string) {
  141. return sanitize_string($string);
  142. }
  143. /**
  144. * Sanitizes an integer for database use.
  145. *
  146. * @param int $int Value to be sanitized
  147. * @param bool $signed Whether negative values should be allowed (true)
  148. * @return int
  149. */
  150. function sanitize_int($int, $signed = true) {
  151. return _elgg_services()->db->sanitizeInt($int, $signed);
  152. }
  153. /**
  154. * Sanitizes an integer for database use.
  155. * Wrapper function for alternate English spelling (@see sanitize_int)
  156. *
  157. * @param int $int Value to be sanitized
  158. * @param bool $signed Whether negative values should be allowed (true)
  159. * @return int
  160. */
  161. function sanitise_int($int, $signed = true) {
  162. return sanitize_int($int, $signed);
  163. }
  164. /**
  165. * Log db profiling information at NOTICE debug level upon shutdown.
  166. *
  167. * @return void
  168. * @access private
  169. */
  170. function _elgg_db_log_profiling_data() {
  171. $db_calls = _elgg_services()->db->getQueryCount();
  172. // demoted to NOTICE as it corrupts javascript at DEBUG
  173. elgg_log("DB Queries for this page: $db_calls", 'INFO');
  174. }
  175. /**
  176. * Get a new query counter that will begin counting from 0. For profiling isolated
  177. * sections of code.
  178. *
  179. * <code>
  180. * $counter = _elgg_db_get_query_counter();
  181. *
  182. * ... code to profile
  183. *
  184. * $counter->setDeltaHeader();
  185. * </code>
  186. *
  187. * @return \Elgg\Database\QueryCounter
  188. * @access private
  189. */
  190. function _elgg_db_get_query_counter() {
  191. return _elgg_services()->queryCounter;
  192. }
  193. /**
  194. * Execute any delayed queries upon shutdown.
  195. *
  196. * @return void
  197. * @access private
  198. */
  199. function _elgg_db_run_delayed_queries() {
  200. _elgg_services()->db->executeDelayedQueries();
  201. }
  202. /**
  203. * Registers shutdown functions for database profiling and delayed queries.
  204. *
  205. * @access private
  206. */
  207. function _elgg_db_init() {
  208. register_shutdown_function('_elgg_db_run_delayed_queries');
  209. register_shutdown_function('_elgg_db_log_profiling_data');
  210. }
  211. return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) {
  212. $events->registerHandler('init', 'system', '_elgg_db_init');
  213. };