PageRenderTime 53ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/system/classes/db.php

https://github.com/HabariMag/habarimag-old
PHP | 471 lines | 202 code | 44 blank | 225 comment | 21 complexity | 5352c172d63d45d2f7ea90249f06ca2f MD5 | raw file
Possible License(s): Apache-2.0
  1. <?php
  2. /**
  3. * @package Habari
  4. *
  5. */
  6. /**
  7. * Habari DB Class
  8. *
  9. * Singleton class for database connection and manipulation
  10. *
  11. */
  12. class DB extends Singleton
  13. {
  14. private $connection = null;
  15. /**
  16. * Enables singleton working properly
  17. *
  18. * @see singleton.php
  19. */
  20. protected static function instance()
  21. {
  22. return self::getInstanceOf( __CLASS__ );
  23. }
  24. /**
  25. * Connects to the database server. If no arguments are
  26. * supplied, then the connection is attempted for the
  27. * database authentication variables in config.php.
  28. *
  29. * @param (optional) connection_string a PDO connection string
  30. * @param (optional) db_user the database user name
  31. * @param (optional) db_pass the database user password
  32. * @return bool
  33. */
  34. public static function connect()
  35. {
  36. /*
  37. if connection has been instantiated (ie: not null), check if is already connected
  38. */
  39. if ( null != DB::instance()->connection ) {
  40. if ( (func_num_args() == 0) && false != DB::instance()->connection->is_connected() ) {
  41. return true;
  42. }
  43. }
  44. if ( func_num_args() > 0 ) {
  45. $connect_string = func_get_arg( 0 );
  46. $db_user = func_get_arg( 1 );
  47. $db_pass = func_get_arg( 2 );
  48. }
  49. else {
  50. /* We use the config.php variables */
  51. $connect_string = Config::get( 'db_connection' )->connection_string;
  52. $db_user = Config::get( 'db_connection' )->username;
  53. $db_pass = Config::get( 'db_connection' )->password;
  54. }
  55. DB::instance()->connection = DatabaseConnection::ConnectionFactory( $connect_string );
  56. if ( null != DB::instance()->connection ) {
  57. return DB::instance()->connection->connect( $connect_string, $db_user, $db_pass );
  58. }
  59. else {
  60. // do some error handling here. The connect string does not have a corresponding DB connection object
  61. print _t( 'Panic! No database connection class appears to be found for the connection string specified. Please check config.php' );
  62. }
  63. }
  64. public static function disconnect()
  65. {
  66. if ( null == DB::instance()->connection ) {
  67. return true;
  68. }
  69. return DB::instance()->connection->disconnect();
  70. }
  71. /**
  72. * Helper function to naturally return table names
  73. *
  74. * @param table name of the table
  75. */
  76. public static function table( $name )
  77. {
  78. return DB::instance()->connection->table( $name );
  79. }
  80. /**
  81. * Adds a table to the list of tables known to Habari. Used
  82. * by Theme and Plugin classes to inform the DB class about
  83. * custom tables used by the plugin
  84. *
  85. * @param name the table name
  86. **/
  87. public static function register_table( $name )
  88. {
  89. DB::instance()->connection->register_table( $name );
  90. }
  91. /**
  92. * Sets the fetch mode for return calls from PDOStatement
  93. *
  94. * @param mode One of the PDO::FETCH_MODE integers
  95. */
  96. public static function set_fetch_mode( $mode )
  97. {
  98. DB::instance()->connection->set_fetch_mode( $mode );
  99. }
  100. /**
  101. * Sets the class to fetch, if fetch mode is PDO::FETCH_CLASS
  102. *
  103. * @param class_name Name of class to create during fetch
  104. */
  105. public static function set_fetch_class( $class_name )
  106. {
  107. DB::instance()->connection->set_fetch_class( $class_name );
  108. }
  109. public static function exec( $query )
  110. {
  111. return DB::instance()->connection->exec( $query );
  112. }
  113. /**
  114. * Queries the database for a given SQL command.
  115. * @param query the SQL query text
  116. * @param args array of values to use for placeholder replacement
  117. * @param class_name (optional) name of class name to wrangle returned data to
  118. * @return bool
  119. */
  120. public static function query( $query, $args = array() )
  121. {
  122. return DB::instance()->connection->query( $query, $args );
  123. }
  124. /**
  125. * Executes a stored procedure against the database
  126. *
  127. * @param procedure name of the stored procedure
  128. * @param args arguments for the procedure
  129. * @return mixed whatever the procedure returns...
  130. * @experimental
  131. * @todo EVERYTHING... :)
  132. */
  133. public static function execute_procedure( $procedure, $args = array() )
  134. {
  135. return DB::instance()->connection->execute_procedure( $procedure, $args );
  136. }
  137. /**
  138. * Start a transaction against the RDBMS in order to wrap multiple
  139. * statements in a safe ACID-compliant container
  140. */
  141. public static function begin_transaction()
  142. {
  143. DB::instance()->connection->begin_transaction();
  144. }
  145. /**
  146. * Rolls a currently running transaction back to the
  147. * prexisting state, or, if the RDBMS supports it, whenever
  148. * a savepoint was committed.
  149. */
  150. public static function rollback()
  151. {
  152. DB::instance()->connection->rollback();
  153. }
  154. /**
  155. * Commit a currently running transaction
  156. */
  157. public static function commit()
  158. {
  159. DB::instance()->connection->commit();
  160. }
  161. /**
  162. * Returns query profiles
  163. *
  164. * @return array an array of query profiles
  165. */
  166. public static function get_profiles()
  167. {
  168. return DB::instance()->connection->get_profiles();
  169. }
  170. /**
  171. * Adds an error to the internal collection
  172. *
  173. * @param error array('query'=>query, 'error'=>errorInfo)
  174. */
  175. private static function add_error( $error )
  176. {
  177. DB::instance()->connection->add_error( $error );
  178. }
  179. /**
  180. * Returns error data gathered from database connection
  181. * @return array An array of error data
  182. */
  183. public static function get_errors()
  184. {
  185. return DB::instance()->connection->get_errors();
  186. }
  187. /**
  188. * Determines if there have been errors since the last clear_errors() call
  189. * @return boolean True if there were errors, false if not
  190. **/
  191. public static function has_errors()
  192. {
  193. return DB::instance()->connection->has_errors();
  194. }
  195. /**
  196. * Updates the last error pointer to simulate resetting the error array
  197. **/
  198. public static function clear_errors()
  199. {
  200. DB::instance()->connection->clear_errors();
  201. }
  202. /**
  203. * Returns only the last error info
  204. * @return array Data for the last error
  205. **/
  206. public static function get_last_error()
  207. {
  208. return DB::instance()->connection->get_last_error();
  209. }
  210. /**
  211. * Execute a query and return the results as an array of objects
  212. * @param query the query to execute
  213. * @param args array of arguments to pass for prepared statements
  214. * @param string Optional class name for row result objects
  215. * @return array An array of QueryRecord or the named class each containing the row data
  216. * <code>$ary = DB::get_results( 'SELECT * FROM tablename WHERE foo = ?', array('fieldvalue'), 'extendedQueryRecord' );</code>
  217. **/
  218. public static function get_results( $query, $args = array() )
  219. {
  220. if ( func_num_args() == 3 ) {
  221. $class_name = func_get_arg( 2 );
  222. return DB::instance()->connection->get_results( $query, $args, $class_name );
  223. }
  224. else {
  225. return DB::instance()->connection->get_results( $query, $args );
  226. }
  227. }
  228. /**
  229. * Returns a single row (the first in a multi-result set) object for a query
  230. * @param string The query to execute
  231. * @param array Arguments to pass for prepared statements
  232. * @param string Optional class name for row result object
  233. * @return object A QueryRecord or an instance of the named class containing the row data
  234. * <code>$obj = DB::get_row( 'SELECT * FROM tablename WHERE foo = ?', array('fieldvalue'), 'extendedQueryRecord' );</code>
  235. **/
  236. public static function get_row( $query, $args = array() )
  237. {
  238. if ( func_num_args() == 3 ) {
  239. $class_name = func_get_arg( 2 );
  240. return DB::instance()->connection->get_row( $query, $args, $class_name );
  241. }
  242. else {
  243. return DB::instance()->connection->get_row( $query, $args );
  244. }
  245. }
  246. /**
  247. * Returns all values for a column for a query
  248. *
  249. * @param string The query to execute
  250. * @param array Arguments to pass for prepared statements
  251. * @return array An array containing the column data
  252. * <code>$ary = DB::get_column( 'SELECT col1 FROM tablename WHERE foo = ?', array('fieldvalue') );</code>
  253. **/
  254. public static function get_column( $query, $args = array() )
  255. {
  256. return DB::instance()->connection->get_column( $query, $args );
  257. }
  258. /**
  259. * Return a single value from the database
  260. *
  261. * @param string the query to execute
  262. * @param array Arguments to pass for prepared statements
  263. * @return mixed a single value (int, string)
  264. **/
  265. public static function get_value( $query, $args = array() )
  266. {
  267. return DB::instance()->connection->get_value( $query, $args );
  268. }
  269. /**
  270. * Returns an associative array using the first returned column as the array key and the second as the array value
  271. *
  272. * @param string The query to execute
  273. * @param array Arguments to pass for prepared statements
  274. * @return array An array containing the associative data
  275. * <code>$ary= DB::get_keyvalue( 'SELECT keyfield, valuefield FROM tablename');</code>
  276. **/
  277. public static function get_keyvalue( $query, $args = array() )
  278. {
  279. return DB::instance()->connection->get_keyvalue( $query, $args );
  280. }
  281. /**
  282. * Inserts into the specified table values associated to the key fields
  283. * @param string The table name
  284. * @param array An associative array of fields and values to insert
  285. * @return boolean True on success, false if not
  286. * <code>DB::insert( 'mytable', array( 'fieldname' => 'value' ) );</code>
  287. **/
  288. public static function insert( $table, $fieldvalues )
  289. {
  290. return DB::instance()->connection->insert( $table, $fieldvalues );
  291. }
  292. /**
  293. * Checks for a record that matches the specific criteria
  294. * @param string Table to check
  295. * @param array Associative array of field values to match
  296. * @return boolean True if any matching record exists, false if not
  297. * <code>DB::exists( 'mytable', array( 'fieldname' => 'value' ) );</code>
  298. **/
  299. public static function exists( $table, $keyfieldvalues )
  300. {
  301. return DB::instance()->connection->exists( $table, $keyfieldvalues );
  302. }
  303. /**
  304. * function update
  305. * Updates any record that matches the specific criteria
  306. * A new row is inserted if no existing record matches the criteria
  307. * @param string Table to update
  308. * @param array Associative array of field values to set
  309. * @param array Associative array of field values to match
  310. * @return boolean True on success, false if not
  311. * <code>DB::update( 'mytable', array( 'fieldname' => 'newvalue' ), array( 'fieldname' => 'value' ) );</code>
  312. **/
  313. public static function update( $table, $fieldvalues, $keyfields )
  314. {
  315. return DB::instance()->connection->update( $table, $fieldvalues, $keyfields );
  316. }
  317. /**
  318. * Deletes any record that matches the specific criteria
  319. * @param string Table to delete from
  320. * @param array Associative array of field values to match
  321. * @return boolean True on success, false if not
  322. * <code>DB::delete( 'mytable', array( 'fieldname' => 'value' ) );</code>
  323. */
  324. public static function delete( $table, $keyfields )
  325. {
  326. return DB::instance()->connection->delete( $table, $keyfields );
  327. }
  328. /**
  329. * Helper function to return the last inserted sequence or
  330. * auto_increment field. Useful when doing multiple inserts
  331. * within a single transaction -- for example, adding dependent
  332. * related rows.
  333. *
  334. * @return mixed The last sequence value (RDBMS-dependent!)
  335. * @see http://us2.php.net/manual/en/function.pdo-lastinsertid.php
  336. */
  337. public static function last_insert_id()
  338. {
  339. return DB::instance()->connection->last_insert_id( func_num_args() == 1 ? func_get_arg( 0 ) : '' );
  340. }
  341. /**
  342. * Returns number of rows affected by the last DELETE, INSERT, or UPDATE
  343. *
  344. * @return int The number of rows affected.
  345. */
  346. public static function row_count()
  347. {
  348. return DB::instance()->connection->row_count();
  349. }
  350. /**
  351. * Automatic database diffing function, used for determining required database upgrades.
  352. *
  353. * @param queries array of create table and insert statements which constitute a fresh install
  354. * @param (optional) execute should the queries be executed against the database or just simulated. default = true
  355. * @param (optional) silent silent running with no messages printed? default = true
  356. * @return string translated SQL string
  357. */
  358. public static function dbdelta( $queries, $execute = true, $silent = true, $doinserts = false )
  359. {
  360. return DB::instance()->connection->dbdelta( $queries, $execute, $silent, $doinserts );
  361. }
  362. /**
  363. * Upgrade data in the database between database revisions
  364. *
  365. * @param integer $old_version Optional version to upgrade to
  366. */
  367. public static function upgrade( $old_version )
  368. {
  369. return DB::instance()->connection->upgrade( $old_version );
  370. }
  371. public static function upgrade_pre( $old_version )
  372. {
  373. return DB::instance()->connection->upgrade_pre( $old_version );
  374. }
  375. public static function upgrade_post( $old_version )
  376. {
  377. return DB::instance()->connection->upgrade_post( $old_version );
  378. }
  379. public static function get_driver_name()
  380. {
  381. return DB::instance()->connection->get_driver_name();
  382. }
  383. public static function get_driver_version()
  384. {
  385. return DB::instance()->connection->get_driver_version();
  386. }
  387. /**
  388. * Returns a list of tables the DB currently knows about.
  389. *
  390. * @return array The list of tables.
  391. */
  392. public static function list_tables()
  393. {
  394. return DB::instance()->connection->list_tables();
  395. }
  396. /**
  397. * Check whether there is an existing connection to a database.
  398. *
  399. * @return boolean
  400. */
  401. public static function is_connected()
  402. {
  403. return (DB::instance()->connection instanceof DatabaseConnection && DB::instance()->connection->is_connected());
  404. }
  405. /**
  406. * Check whether there is a transaction underway.
  407. *
  408. * @return boolean
  409. */
  410. public static function in_transaction()
  411. {
  412. return DB::instance()->connection->in_transaction();
  413. }
  414. /**
  415. * Return a PDO-quoted string appropriate for the DB backend we're using.
  416. *
  417. * If you're using this then there's 99+% probability you're building your queries the wrong way!
  418. *
  419. * @param string $string The string to quote.
  420. * @return string A DB-safe quoted string.
  421. */
  422. public static function quote( $string )
  423. {
  424. return DB::instance()->connection->quote( $string );
  425. }
  426. }
  427. ?>