PageRenderTime 60ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/joomla/database/database.php

https://bitbucket.org/izubizarreta/https-bitbucket.org-bityvip
PHP | 1902 lines | 868 code | 203 blank | 831 comment | 86 complexity | 1519d1a5b0da56aff50d6953491fc310 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.0, JSON, GPL-2.0, BSD-3-Clause, LGPL-2.1, MIT
  1. <?php
  2. /**
  3. * @package Joomla.Platform
  4. * @subpackage Database
  5. *
  6. * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE
  8. */
  9. defined('JPATH_PLATFORM') or die;
  10. jimport('joomla.filesystem.folder');
  11. /**
  12. * Database interface class.
  13. *
  14. * @package Joomla.Platform
  15. * @subpackage Database
  16. * @since 11.2
  17. */
  18. interface JDatabaseInterface
  19. {
  20. /**
  21. * Test to see if the connector is available.
  22. *
  23. * @return boolean True on success, false otherwise.
  24. *
  25. * @since 11.2
  26. */
  27. public static function test();
  28. }
  29. /**
  30. * Database connector class.
  31. *
  32. * @package Joomla.Platform
  33. * @subpackage Database
  34. * @since 11.1
  35. */
  36. abstract class JDatabase implements JDatabaseInterface
  37. {
  38. /**
  39. * The name of the database.
  40. *
  41. * @var string
  42. * @since 11.4
  43. */
  44. private $_database;
  45. /**
  46. * The name of the database driver.
  47. *
  48. * @var string
  49. * @since 11.1
  50. */
  51. public $name;
  52. /**
  53. * @var resource The database connection resource.
  54. * @since 11.1
  55. */
  56. protected $connection;
  57. /**
  58. * @var integer The number of SQL statements executed by the database driver.
  59. * @since 11.1
  60. */
  61. protected $count = 0;
  62. /**
  63. * @var resource The database connection cursor from the last query.
  64. * @since 11.1
  65. */
  66. protected $cursor;
  67. /**
  68. * @var boolean The database driver debugging state.
  69. * @since 11.1
  70. */
  71. protected $debug = false;
  72. /**
  73. * @var integer The affected row limit for the current SQL statement.
  74. * @since 11.1
  75. */
  76. protected $limit = 0;
  77. /**
  78. * @var array The log of executed SQL statements by the database driver.
  79. * @since 11.1
  80. */
  81. protected $log = array();
  82. /**
  83. * @var string The character(s) used to quote SQL statement names such as table names or field names,
  84. * etc. The child classes should define this as necessary. If a single character string the
  85. * same character is used for both sides of the quoted name, else the first character will be
  86. * used for the opening quote and the second for the closing quote.
  87. * @since 11.1
  88. */
  89. protected $nameQuote;
  90. /**
  91. * @var string The null or zero representation of a timestamp for the database driver. This should be
  92. * defined in child classes to hold the appropriate value for the engine.
  93. * @since 11.1
  94. */
  95. protected $nullDate;
  96. /**
  97. * @var integer The affected row offset to apply for the current SQL statement.
  98. * @since 11.1
  99. */
  100. protected $offset = 0;
  101. /**
  102. * @var mixed The current SQL statement to execute.
  103. * @since 11.1
  104. */
  105. protected $sql;
  106. /**
  107. * @var string The common database table prefix.
  108. * @since 11.1
  109. */
  110. protected $tablePrefix;
  111. /**
  112. * @var boolean True if the database engine supports UTF-8 character encoding.
  113. * @since 11.1
  114. */
  115. protected $utf = true;
  116. /**
  117. * @var integer The database error number
  118. * @since 11.1
  119. * @deprecated 12.1
  120. */
  121. protected $errorNum = 0;
  122. /**
  123. * @var string The database error message
  124. * @since 11.1
  125. * @deprecated 12.1
  126. */
  127. protected $errorMsg;
  128. /**
  129. * @var boolean If true then there are fields to be quoted for the query.
  130. * @since 11.1
  131. * @deprecated 12.1
  132. */
  133. protected $hasQuoted = false;
  134. /**
  135. * @var array The fields that are to be quoted.
  136. * @since 11.1
  137. * @deprecated 12.1
  138. */
  139. protected $quoted = array();
  140. /**
  141. * @var array JDatabase instances container.
  142. * @since 11.1
  143. */
  144. protected static $instances = array();
  145. /**
  146. * @var string The minimum supported database version.
  147. * @since 12.1
  148. */
  149. protected $dbMinimum;
  150. /**
  151. * Get a list of available database connectors. The list will only be populated with connectors that both
  152. * the class exists and the static test method returns true. This gives us the ability to have a multitude
  153. * of connector classes that are self-aware as to whether or not they are able to be used on a given system.
  154. *
  155. * @return array An array of available database connectors.
  156. *
  157. * @since 11.1
  158. */
  159. public static function getConnectors()
  160. {
  161. // Instantiate variables.
  162. $connectors = array();
  163. // Get a list of types.
  164. $types = JFolder::files(dirname(__FILE__) . '/database');
  165. // Loop through the types and find the ones that are available.
  166. foreach ($types as $type)
  167. {
  168. // Ignore some files.
  169. if (($type == 'index.html') || stripos($type, 'importer') || stripos($type, 'exporter') || stripos($type, 'query') || stripos($type, 'exception'))
  170. {
  171. continue;
  172. }
  173. // Derive the class name from the type.
  174. $class = str_ireplace(array('.php', 'sql'), array('', 'SQL'), 'JDatabase' . ucfirst(trim($type)));
  175. // If the class doesn't exist, let's look for it and register it.
  176. if (!class_exists($class))
  177. {
  178. // Derive the file path for the driver class.
  179. $path = dirname(__FILE__) . '/database/' . $type;
  180. // If the file exists register the class with our class loader.
  181. if (file_exists($path))
  182. {
  183. JLoader::register($class, $path);
  184. }
  185. // If it doesn't exist we are at an impasse so move on to the next type.
  186. else
  187. {
  188. continue;
  189. }
  190. }
  191. // If the class still doesn't exist we have nothing left to do but look at the next type. We did our best.
  192. if (!class_exists($class))
  193. {
  194. continue;
  195. }
  196. // Sweet! Our class exists, so now we just need to know if it passes it's test method.
  197. if (call_user_func_array(array($class, 'test'), array()))
  198. {
  199. // Connector names should not have file extensions.
  200. $connectors[] = str_ireplace('.php', '', $type);
  201. }
  202. }
  203. return $connectors;
  204. }
  205. /**
  206. * Method to return a JDatabase instance based on the given options. There are three global options and then
  207. * the rest are specific to the database driver. The 'driver' option defines which JDatabaseDriver class is
  208. * used for the connection -- the default is 'mysql'. The 'database' option determines which database is to
  209. * be used for the connection. The 'select' option determines whether the connector should automatically select
  210. * the chosen database.
  211. *
  212. * Instances are unique to the given options and new objects are only created when a unique options array is
  213. * passed into the method. This ensures that we don't end up with unnecessary database connection resources.
  214. *
  215. * @param array $options Parameters to be passed to the database driver.
  216. *
  217. * @return JDatabase A database object.
  218. *
  219. * @since 11.1
  220. */
  221. public static function getInstance($options = array())
  222. {
  223. // Sanitize the database connector options.
  224. $options['driver'] = (isset($options['driver'])) ? preg_replace('/[^A-Z0-9_\.-]/i', '', $options['driver']) : 'mysql';
  225. $options['database'] = (isset($options['database'])) ? $options['database'] : null;
  226. $options['select'] = (isset($options['select'])) ? $options['select'] : true;
  227. // Get the options signature for the database connector.
  228. $signature = md5(serialize($options));
  229. // If we already have a database connector instance for these options then just use that.
  230. if (empty(self::$instances[$signature]))
  231. {
  232. // Derive the class name from the driver.
  233. $class = 'JDatabase' . ucfirst($options['driver']);
  234. // If the class doesn't exist, let's look for it and register it.
  235. if (!class_exists($class))
  236. {
  237. // Derive the file path for the driver class.
  238. $path = dirname(__FILE__) . '/database/' . $options['driver'] . '.php';
  239. // If the file exists register the class with our class loader.
  240. if (file_exists($path))
  241. {
  242. JLoader::register($class, $path);
  243. }
  244. // If it doesn't exist we are at an impasse so throw an exception.
  245. else
  246. {
  247. // Legacy error handling switch based on the JError::$legacy switch.
  248. // @deprecated 12.1
  249. if (JError::$legacy)
  250. {
  251. // Deprecation warning.
  252. JLog::add('JError is deprecated.', JLog::WARNING, 'deprecated');
  253. JError::setErrorHandling(E_ERROR, 'die');
  254. return JError::raiseError(500, JText::sprintf('JLIB_DATABASE_ERROR_LOAD_DATABASE_DRIVER', $options['driver']));
  255. }
  256. else
  257. {
  258. throw new JDatabaseException(JText::sprintf('JLIB_DATABASE_ERROR_LOAD_DATABASE_DRIVER', $options['driver']));
  259. }
  260. }
  261. }
  262. // If the class still doesn't exist we have nothing left to do but throw an exception. We did our best.
  263. if (!class_exists($class))
  264. {
  265. // Legacy error handling switch based on the JError::$legacy switch.
  266. // @deprecated 12.1
  267. if (JError::$legacy)
  268. {
  269. // Deprecation warning.
  270. JLog::add('JError() is deprecated.', JLog::WARNING, 'deprecated');
  271. JError::setErrorHandling(E_ERROR, 'die');
  272. return JError::raiseError(500, JText::sprintf('JLIB_DATABASE_ERROR_LOAD_DATABASE_DRIVER', $options['driver']));
  273. }
  274. else
  275. {
  276. throw new JDatabaseException(JText::sprintf('JLIB_DATABASE_ERROR_LOAD_DATABASE_DRIVER', $options['driver']));
  277. }
  278. }
  279. // Create our new JDatabase connector based on the options given.
  280. try
  281. {
  282. $instance = new $class($options);
  283. }
  284. catch (JDatabaseException $e)
  285. {
  286. // Legacy error handling switch based on the JError::$legacy switch.
  287. // @deprecated 12.1
  288. if (JError::$legacy)
  289. {
  290. // Deprecation warning.
  291. JLog::add('JError() is deprecated.', JLog::WARNING, 'deprecated');
  292. JError::setErrorHandling(E_ERROR, 'ignore');
  293. return JError::raiseError(500, JText::sprintf('JLIB_DATABASE_ERROR_CONNECT_DATABASE', $e->getMessage()));
  294. }
  295. else
  296. {
  297. throw new JDatabaseException(JText::sprintf('JLIB_DATABASE_ERROR_CONNECT_DATABASE', $e->getMessage()));
  298. }
  299. }
  300. // Set the new connector to the global instances based on signature.
  301. self::$instances[$signature] = $instance;
  302. }
  303. return self::$instances[$signature];
  304. }
  305. /**
  306. * Splits a string of multiple queries into an array of individual queries.
  307. *
  308. * @param string $sql Input SQL string with which to split into individual queries.
  309. *
  310. * @return array The queries from the input string separated into an array.
  311. *
  312. * @since 11.1
  313. */
  314. public static function splitSql($sql)
  315. {
  316. $start = 0;
  317. $open = false;
  318. $char = '';
  319. $end = strlen($sql);
  320. $queries = array();
  321. for ($i = 0; $i < $end; $i++)
  322. {
  323. $current = substr($sql, $i, 1);
  324. if (($current == '"' || $current == '\''))
  325. {
  326. $n = 2;
  327. while (substr($sql, $i - $n + 1, 1) == '\\' && $n < $i)
  328. {
  329. $n++;
  330. }
  331. if ($n % 2 == 0)
  332. {
  333. if ($open)
  334. {
  335. if ($current == $char)
  336. {
  337. $open = false;
  338. $char = '';
  339. }
  340. }
  341. else
  342. {
  343. $open = true;
  344. $char = $current;
  345. }
  346. }
  347. }
  348. if (($current == ';' && !$open) || $i == $end - 1)
  349. {
  350. $queries[] = substr($sql, $start, ($i - $start + 1));
  351. $start = $i + 1;
  352. }
  353. }
  354. return $queries;
  355. }
  356. /**
  357. * Magic method to provide method alias support for quote() and quoteName().
  358. *
  359. * @param string $method The called method.
  360. * @param array $args The array of arguments passed to the method.
  361. *
  362. * @return string The aliased method's return value or null.
  363. *
  364. * @since 11.1
  365. */
  366. public function __call($method, $args)
  367. {
  368. if (empty($args))
  369. {
  370. return;
  371. }
  372. switch ($method)
  373. {
  374. case 'q':
  375. return $this->quote($args[0], isset($args[1]) ? $args[1] : true);
  376. break;
  377. case 'nq':
  378. case 'qn':
  379. return $this->quoteName($args[0], isset($args[1]) ? $args[1] : null);
  380. break;
  381. }
  382. }
  383. /**
  384. * Constructor.
  385. *
  386. * @param array $options List of options used to configure the connection
  387. *
  388. * @since 11.1
  389. */
  390. protected function __construct($options)
  391. {
  392. // Initialise object variables.
  393. $this->_database = (isset($options['database'])) ? $options['database'] : '';
  394. $this->tablePrefix = (isset($options['prefix'])) ? $options['prefix'] : 'jos_';
  395. $this->count = 0;
  396. $this->errorNum = 0;
  397. $this->log = array();
  398. $this->quoted = array();
  399. $this->hasQuoted = false;
  400. // Set charactersets (needed for MySQL 4.1.2+).
  401. $this->setUTF();
  402. }
  403. /**
  404. * Adds a field or array of field names to the list that are to be quoted.
  405. *
  406. * @param mixed $quoted Field name or array of names.
  407. *
  408. * @return void
  409. *
  410. * @deprecated 12.1
  411. * @since 11.1
  412. */
  413. public function addQuoted($quoted)
  414. {
  415. // Deprecation warning.
  416. JLog::add('JDatabase::addQuoted() is deprecated.', JLog::WARNING, 'deprecated');
  417. if (is_string($quoted))
  418. {
  419. $this->quoted[] = $quoted;
  420. }
  421. else
  422. {
  423. $this->quoted = array_merge($this->quoted, (array) $quoted);
  424. }
  425. $this->hasQuoted = true;
  426. }
  427. /**
  428. * Determines if the connection to the server is active.
  429. *
  430. * @return boolean True if connected to the database engine.
  431. *
  432. * @since 11.1
  433. */
  434. abstract public function connected();
  435. /**
  436. * Drops a table from the database.
  437. *
  438. * @param string $table The name of the database table to drop.
  439. * @param boolean $ifExists Optionally specify that the table must exist before it is dropped.
  440. *
  441. * @return JDatabase Returns this object to support chaining.
  442. *
  443. * @since 11.4
  444. * @throws JDatabaseException
  445. */
  446. public abstract function dropTable($table, $ifExists = true);
  447. /**
  448. * Method to escape a string for usage in an SQL statement.
  449. *
  450. * @param string $text The string to be escaped.
  451. * @param boolean $extra Optional parameter to provide extra escaping.
  452. *
  453. * @return string The escaped string.
  454. *
  455. * @since 11.1
  456. */
  457. abstract public function escape($text, $extra = false);
  458. /**
  459. * Method to fetch a row from the result set cursor as an array.
  460. *
  461. * @param mixed $cursor The optional result set cursor from which to fetch the row.
  462. *
  463. * @return mixed Either the next row from the result set or false if there are no more rows.
  464. *
  465. * @since 11.1
  466. */
  467. abstract protected function fetchArray($cursor = null);
  468. /**
  469. * Method to fetch a row from the result set cursor as an associative array.
  470. *
  471. * @param mixed $cursor The optional result set cursor from which to fetch the row.
  472. *
  473. * @return mixed Either the next row from the result set or false if there are no more rows.
  474. *
  475. * @since 11.1
  476. */
  477. abstract protected function fetchAssoc($cursor = null);
  478. /**
  479. * Method to fetch a row from the result set cursor as an object.
  480. *
  481. * @param mixed $cursor The optional result set cursor from which to fetch the row.
  482. * @param string $class The class name to use for the returned row object.
  483. *
  484. * @return mixed Either the next row from the result set or false if there are no more rows.
  485. *
  486. * @since 11.1
  487. */
  488. abstract protected function fetchObject($cursor = null, $class = 'stdClass');
  489. /**
  490. * Method to free up the memory used for the result set.
  491. *
  492. * @param mixed $cursor The optional result set cursor from which to fetch the row.
  493. *
  494. * @return void
  495. *
  496. * @since 11.1
  497. */
  498. abstract protected function freeResult($cursor = null);
  499. /**
  500. * Get the number of affected rows for the previous executed SQL statement.
  501. *
  502. * @return integer The number of affected rows.
  503. *
  504. * @since 11.1
  505. */
  506. abstract public function getAffectedRows();
  507. /**
  508. * Method to get the database collation in use by sampling a text field of a table in the database.
  509. *
  510. * @return mixed The collation in use by the database or boolean false if not supported.
  511. *
  512. * @since 11.1
  513. */
  514. abstract public function getCollation();
  515. /**
  516. * Method that provides access to the underlying database connection. Useful for when you need to call a
  517. * proprietary method such as postgresql's lo_* methods.
  518. *
  519. * @return resource The underlying database connection resource.
  520. *
  521. * @since 11.1
  522. */
  523. public function getConnection()
  524. {
  525. return $this->connection;
  526. }
  527. /**
  528. * Get the total number of SQL statements executed by the database driver.
  529. *
  530. * @return integer
  531. *
  532. * @since 11.1
  533. */
  534. public function getCount()
  535. {
  536. return $this->count;
  537. }
  538. /**
  539. * Gets the name of the database used by this conneciton.
  540. *
  541. * @return string
  542. *
  543. * @since 11.4
  544. */
  545. protected function getDatabase()
  546. {
  547. return $this->_database;
  548. }
  549. /**
  550. * Returns a PHP date() function compliant date format for the database driver.
  551. *
  552. * @return string The format string.
  553. *
  554. * @since 11.1
  555. */
  556. public function getDateFormat()
  557. {
  558. return 'Y-m-d H:i:s';
  559. }
  560. /**
  561. * Get the database driver SQL statement log.
  562. *
  563. * @return array SQL statements executed by the database driver.
  564. *
  565. * @since 11.1
  566. */
  567. public function getLog()
  568. {
  569. return $this->log;
  570. }
  571. /**
  572. * Get the minimum supported database version.
  573. *
  574. * @return string The minimum version number for the database driver.
  575. *
  576. * @since 12.1
  577. */
  578. public function getMinimum()
  579. {
  580. return $this->dbMinimum;
  581. }
  582. /**
  583. * Get the null or zero representation of a timestamp for the database driver.
  584. *
  585. * @return string Null or zero representation of a timestamp.
  586. *
  587. * @since 11.1
  588. */
  589. public function getNullDate()
  590. {
  591. return $this->nullDate;
  592. }
  593. /**
  594. * Get the number of returned rows for the previous executed SQL statement.
  595. *
  596. * @param resource $cursor An optional database cursor resource to extract the row count from.
  597. *
  598. * @return integer The number of returned rows.
  599. *
  600. * @since 11.1
  601. */
  602. abstract public function getNumRows($cursor = null);
  603. /**
  604. * Get the common table prefix for the database driver.
  605. *
  606. * @return string The common database table prefix.
  607. *
  608. * @since 11.1
  609. */
  610. public function getPrefix()
  611. {
  612. return $this->tablePrefix;
  613. }
  614. /**
  615. * Get the current query object or a new JDatabaseQuery object.
  616. *
  617. * @param boolean $new False to return the current query object, True to return a new JDatabaseQuery object.
  618. *
  619. * @return JDatabaseQuery The current query object or a new object extending the JDatabaseQuery class.
  620. *
  621. * @since 11.1
  622. * @throws JDatabaseException
  623. */
  624. abstract public function getQuery($new = false);
  625. /**
  626. * Retrieves field information about the given tables.
  627. *
  628. * @param string $table The name of the database table.
  629. * @param boolean $typeOnly True (default) to only return field types.
  630. *
  631. * @return array An array of fields by table.
  632. *
  633. * @since 11.1
  634. * @throws JDatabaseException
  635. */
  636. abstract public function getTableColumns($table, $typeOnly = true);
  637. /**
  638. * Shows the table CREATE statement that creates the given tables.
  639. *
  640. * @param mixed $tables A table name or a list of table names.
  641. *
  642. * @return array A list of the create SQL for the tables.
  643. *
  644. * @since 11.1
  645. * @throws JDatabaseException
  646. */
  647. abstract public function getTableCreate($tables);
  648. /**
  649. * Retrieves field information about the given tables.
  650. *
  651. * @param mixed $tables A table name or a list of table names.
  652. *
  653. * @return array An array of keys for the table(s).
  654. *
  655. * @since 11.1
  656. * @throws JDatabaseException
  657. */
  658. abstract public function getTableKeys($tables);
  659. /**
  660. * Method to get an array of all tables in the database.
  661. *
  662. * @return array An array of all the tables in the database.
  663. *
  664. * @since 11.1
  665. * @throws JDatabaseException
  666. */
  667. abstract public function getTableList();
  668. /**
  669. * Determine whether or not the database engine supports UTF-8 character encoding.
  670. *
  671. * @return boolean True if the database engine supports UTF-8 character encoding.
  672. *
  673. * @since 11.1
  674. */
  675. public function getUTFSupport()
  676. {
  677. return $this->utf;
  678. }
  679. /**
  680. * Get the version of the database connector
  681. *
  682. * @return string The database connector version.
  683. *
  684. * @since 11.1
  685. */
  686. abstract public function getVersion();
  687. /**
  688. * Determines if the database engine supports UTF-8 character encoding.
  689. *
  690. * @return boolean True if supported.
  691. *
  692. * @since 11.1
  693. *
  694. * @deprecated 12.1
  695. */
  696. abstract public function hasUTF();
  697. /**
  698. * Method to get the auto-incremented value from the last INSERT statement.
  699. *
  700. * @return integer The value of the auto-increment field from the last inserted row.
  701. *
  702. * @since 11.1
  703. */
  704. abstract public function insertid();
  705. /**
  706. * Inserts a row into a table based on an object's properties.
  707. *
  708. * @param string $table The name of the database table to insert into.
  709. * @param object &$object A reference to an object whose public properties match the table fields.
  710. * @param string $key The name of the primary key. If provided the object property is updated.
  711. *
  712. * @return boolean True on success.
  713. *
  714. * @since 11.1
  715. * @throws JDatabaseException
  716. */
  717. public function insertObject($table, &$object, $key = null)
  718. {
  719. // Initialise variables.
  720. $fields = array();
  721. $values = array();
  722. // Create the base insert statement.
  723. $statement = 'INSERT INTO ' . $this->quoteName($table) . ' (%s) VALUES (%s)';
  724. // Iterate over the object variables to build the query fields and values.
  725. foreach (get_object_vars($object) as $k => $v)
  726. {
  727. // Only process non-null scalars.
  728. if (is_array($v) or is_object($v) or $v === null)
  729. {
  730. continue;
  731. }
  732. // Ignore any internal fields.
  733. if ($k[0] == '_')
  734. {
  735. continue;
  736. }
  737. // Prepare and sanitize the fields and values for the database query.
  738. $fields[] = $this->quoteName($k);
  739. $values[] = $this->quote($v);
  740. }
  741. // Set the query and execute the insert.
  742. $this->setQuery(sprintf($statement, implode(',', $fields), implode(',', $values)));
  743. if (!$this->execute())
  744. {
  745. return false;
  746. }
  747. // Update the primary key if it exists.
  748. $id = $this->insertid();
  749. if ($key && $id)
  750. {
  751. $object->$key = $id;
  752. }
  753. return true;
  754. }
  755. /**
  756. * Method to check whether the installed database version is supported by the database driver
  757. *
  758. * @return boolean True if the database version is supported
  759. *
  760. * @since 12.1
  761. */
  762. public function isMinimumVersion()
  763. {
  764. return version_compare($this->getVersion(), $this->dbMinimum) >= 0;
  765. }
  766. /**
  767. * Method to get the first row of the result set from the database query as an associative array
  768. * of ['field_name' => 'row_value'].
  769. *
  770. * @return mixed The return value or null if the query failed.
  771. *
  772. * @since 11.1
  773. * @throws JDatabaseException
  774. */
  775. public function loadAssoc()
  776. {
  777. // Initialise variables.
  778. $ret = null;
  779. // Execute the query and get the result set cursor.
  780. if (!($cursor = $this->execute()))
  781. {
  782. return null;
  783. }
  784. // Get the first row from the result set as an associative array.
  785. if ($array = $this->fetchAssoc($cursor))
  786. {
  787. $ret = $array;
  788. }
  789. // Free up system resources and return.
  790. $this->freeResult($cursor);
  791. return $ret;
  792. }
  793. /**
  794. * Method to get an array of the result set rows from the database query where each row is an associative array
  795. * of ['field_name' => 'row_value']. The array of rows can optionally be keyed by a field name, but defaults to
  796. * a sequential numeric array.
  797. *
  798. * NOTE: Chosing to key the result array by a non-unique field name can result in unwanted
  799. * behavior and should be avoided.
  800. *
  801. * @param string $key The name of a field on which to key the result array.
  802. * @param string $column An optional column name. Instead of the whole row, only this column value will be in
  803. * the result array.
  804. *
  805. * @return mixed The return value or null if the query failed.
  806. *
  807. * @since 11.1
  808. * @throws JDatabaseException
  809. */
  810. public function loadAssocList($key = null, $column = null)
  811. {
  812. // Initialise variables.
  813. $array = array();
  814. // Execute the query and get the result set cursor.
  815. if (!($cursor = $this->execute()))
  816. {
  817. return null;
  818. }
  819. // Get all of the rows from the result set.
  820. while ($row = $this->fetchAssoc($cursor))
  821. {
  822. $value = ($column) ? (isset($row[$column]) ? $row[$column] : $row) : $row;
  823. if ($key)
  824. {
  825. $array[$row[$key]] = $value;
  826. }
  827. else
  828. {
  829. $array[] = $value;
  830. }
  831. }
  832. // Free up system resources and return.
  833. $this->freeResult($cursor);
  834. return $array;
  835. }
  836. /**
  837. * Method to get an array of values from the <var>$offset</var> field in each row of the result set from
  838. * the database query.
  839. *
  840. * @param integer $offset The row offset to use to build the result array.
  841. *
  842. * @return mixed The return value or null if the query failed.
  843. *
  844. * @since 11.1
  845. * @throws JDatabaseException
  846. */
  847. public function loadColumn($offset = 0)
  848. {
  849. // Initialise variables.
  850. $array = array();
  851. // Execute the query and get the result set cursor.
  852. if (!($cursor = $this->execute()))
  853. {
  854. return null;
  855. }
  856. // Get all of the rows from the result set as arrays.
  857. while ($row = $this->fetchArray($cursor))
  858. {
  859. $array[] = $row[$offset];
  860. }
  861. // Free up system resources and return.
  862. $this->freeResult($cursor);
  863. return $array;
  864. }
  865. /**
  866. * Method to get the next row in the result set from the database query as an object.
  867. *
  868. * @param string $class The class name to use for the returned row object.
  869. *
  870. * @return mixed The result of the query as an array, false if there are no more rows.
  871. *
  872. * @since 11.1
  873. * @throws JDatabaseException
  874. */
  875. public function loadNextObject($class = 'stdClass')
  876. {
  877. static $cursor;
  878. // Execute the query and get the result set cursor.
  879. if (!($cursor = $this->execute()))
  880. {
  881. return $this->errorNum ? null : false;
  882. }
  883. // Get the next row from the result set as an object of type $class.
  884. if ($row = $this->fetchObject($cursor, $class))
  885. {
  886. return $row;
  887. }
  888. // Free up system resources and return.
  889. $this->freeResult($cursor);
  890. $cursor = null;
  891. return false;
  892. }
  893. /**
  894. * Method to get the next row in the result set from the database query as an array.
  895. *
  896. * @return mixed The result of the query as an array, false if there are no more rows.
  897. *
  898. * @since 11.1
  899. * @throws JDatabaseException
  900. */
  901. public function loadNextRow()
  902. {
  903. static $cursor;
  904. // Execute the query and get the result set cursor.
  905. if (!($cursor = $this->execute()))
  906. {
  907. return $this->errorNum ? null : false;
  908. }
  909. // Get the next row from the result set as an object of type $class.
  910. if ($row = $this->fetchArray($cursor))
  911. {
  912. return $row;
  913. }
  914. // Free up system resources and return.
  915. $this->freeResult($cursor);
  916. $cursor = null;
  917. return false;
  918. }
  919. /**
  920. * Method to get the first row of the result set from the database query as an object.
  921. *
  922. * @param string $class The class name to use for the returned row object.
  923. *
  924. * @return mixed The return value or null if the query failed.
  925. *
  926. * @since 11.1
  927. * @throws JDatabaseException
  928. */
  929. public function loadObject($class = 'stdClass')
  930. {
  931. // Initialise variables.
  932. $ret = null;
  933. // Execute the query and get the result set cursor.
  934. if (!($cursor = $this->execute()))
  935. {
  936. return null;
  937. }
  938. // Get the first row from the result set as an object of type $class.
  939. if ($object = $this->fetchObject($cursor, $class))
  940. {
  941. $ret = $object;
  942. }
  943. // Free up system resources and return.
  944. $this->freeResult($cursor);
  945. return $ret;
  946. }
  947. /**
  948. * Method to get an array of the result set rows from the database query where each row is an object. The array
  949. * of objects can optionally be keyed by a field name, but defaults to a sequential numeric array.
  950. *
  951. * NOTE: Choosing to key the result array by a non-unique field name can result in unwanted
  952. * behavior and should be avoided.
  953. *
  954. * @param string $key The name of a field on which to key the result array.
  955. * @param string $class The class name to use for the returned row objects.
  956. *
  957. * @return mixed The return value or null if the query failed.
  958. *
  959. * @since 11.1
  960. * @throws JDatabaseException
  961. */
  962. public function loadObjectList($key = '', $class = 'stdClass')
  963. {
  964. // Initialise variables.
  965. $array = array();
  966. // Execute the query and get the result set cursor.
  967. if (!($cursor = $this->execute()))
  968. {
  969. return null;
  970. }
  971. // Get all of the rows from the result set as objects of type $class.
  972. while ($row = $this->fetchObject($cursor, $class))
  973. {
  974. if ($key)
  975. {
  976. $array[$row->$key] = $row;
  977. }
  978. else
  979. {
  980. $array[] = $row;
  981. }
  982. }
  983. // Free up system resources and return.
  984. $this->freeResult($cursor);
  985. return $array;
  986. }
  987. /**
  988. * Method to get the first field of the first row of the result set from the database query.
  989. *
  990. * @return mixed The return value or null if the query failed.
  991. *
  992. * @since 11.1
  993. * @throws JDatabaseException
  994. */
  995. public function loadResult()
  996. {
  997. // Initialise variables.
  998. $ret = null;
  999. // Execute the query and get the result set cursor.
  1000. if (!($cursor = $this->execute()))
  1001. {
  1002. return null;
  1003. }
  1004. // Get the first row from the result set as an array.
  1005. if ($row = $this->fetchArray($cursor))
  1006. {
  1007. $ret = $row[0];
  1008. }
  1009. // Free up system resources and return.
  1010. $this->freeResult($cursor);
  1011. return $ret;
  1012. }
  1013. /**
  1014. * Method to get the first row of the result set from the database query as an array. Columns are indexed
  1015. * numerically so the first column in the result set would be accessible via <var>$row[0]</var>, etc.
  1016. *
  1017. * @return mixed The return value or null if the query failed.
  1018. *
  1019. * @since 11.1
  1020. * @throws JDatabaseException
  1021. */
  1022. public function loadRow()
  1023. {
  1024. // Initialise variables.
  1025. $ret = null;
  1026. // Execute the query and get the result set cursor.
  1027. if (!($cursor = $this->execute()))
  1028. {
  1029. return null;
  1030. }
  1031. // Get the first row from the result set as an array.
  1032. if ($row = $this->fetchArray($cursor))
  1033. {
  1034. $ret = $row;
  1035. }
  1036. // Free up system resources and return.
  1037. $this->freeResult($cursor);
  1038. return $ret;
  1039. }
  1040. /**
  1041. * Method to get an array of the result set rows from the database query where each row is an array. The array
  1042. * of objects can optionally be keyed by a field offset, but defaults to a sequential numeric array.
  1043. *
  1044. * NOTE: Choosing to key the result array by a non-unique field can result in unwanted
  1045. * behavior and should be avoided.
  1046. *
  1047. * @param string $key The name of a field on which to key the result array.
  1048. *
  1049. * @return mixed The return value or null if the query failed.
  1050. *
  1051. * @since 11.1
  1052. * @throws JDatabaseException
  1053. */
  1054. public function loadRowList($key = null)
  1055. {
  1056. // Initialise variables.
  1057. $array = array();
  1058. // Execute the query and get the result set cursor.
  1059. if (!($cursor = $this->execute()))
  1060. {
  1061. return null;
  1062. }
  1063. // Get all of the rows from the result set as arrays.
  1064. while ($row = $this->fetchArray($cursor))
  1065. {
  1066. if ($key !== null)
  1067. {
  1068. $array[$row[$key]] = $row;
  1069. }
  1070. else
  1071. {
  1072. $array[] = $row;
  1073. }
  1074. }
  1075. // Free up system resources and return.
  1076. $this->freeResult($cursor);
  1077. return $array;
  1078. }
  1079. /**
  1080. * Locks a table in the database.
  1081. *
  1082. * @param string $tableName The name of the table to unlock.
  1083. *
  1084. * @return JDatabase Returns this object to support chaining.
  1085. *
  1086. * @since 11.4
  1087. * @throws JDatabaseException
  1088. */
  1089. public abstract function lockTable($tableName);
  1090. /**
  1091. * Execute the SQL statement.
  1092. *
  1093. * @return mixed A database cursor resource on success, boolean false on failure.
  1094. *
  1095. * @since 11.1
  1096. * @throws JDatabaseException
  1097. */
  1098. public function query()
  1099. {
  1100. return $this->execute();
  1101. }
  1102. /**
  1103. * Execute the SQL statement.
  1104. *
  1105. * @return mixed A database cursor resource on success, boolean false on failure.
  1106. *
  1107. * @since 12.1
  1108. * @throws JDatabaseException
  1109. */
  1110. abstract public function execute();
  1111. /**
  1112. * Method to quote and optionally escape a string to database requirements for insertion into the database.
  1113. *
  1114. * @param string $text The string to quote.
  1115. * @param boolean $escape True (default) to escape the string, false to leave it unchanged.
  1116. *
  1117. * @return string The quoted input string.
  1118. *
  1119. * @since 11.1
  1120. */
  1121. public function quote($text, $escape = true)
  1122. {
  1123. return '\'' . ($escape ? $this->escape($text) : $text) . '\'';
  1124. }
  1125. /**
  1126. * Wrap an SQL statement identifier name such as column, table or database names in quotes to prevent injection
  1127. * risks and reserved word conflicts.
  1128. *
  1129. * @param mixed $name The identifier name to wrap in quotes, or an array of identifier names to wrap in quotes.
  1130. * Each type supports dot-notation name.
  1131. * @param mixed $as The AS query part associated to $name. It can be string or array, in latter case it has to be
  1132. * same length of $name; if is null there will not be any AS part for string or array element.
  1133. *
  1134. * @return mixed The quote wrapped name, same type of $name.
  1135. *
  1136. * @since 11.1
  1137. */
  1138. public function quoteName($name, $as = null)
  1139. {
  1140. if (is_string($name))
  1141. {
  1142. $quotedName = $this->quoteNameStr(explode('.', $name));
  1143. $quotedAs = '';
  1144. if (!is_null($as))
  1145. {
  1146. settype($as, 'array');
  1147. $quotedAs .= ' AS ' . $this->quoteNameStr($as);
  1148. }
  1149. return $quotedName . $quotedAs;
  1150. }
  1151. else
  1152. {
  1153. $fin = array();
  1154. if (is_null($as))
  1155. {
  1156. foreach ($name as $str)
  1157. {
  1158. $fin[] = $this->quoteName($str);
  1159. }
  1160. }
  1161. elseif (is_array($name) && (count($name) == count($as)))
  1162. {
  1163. for ($i = 0; $i < count($name); $i++)
  1164. {
  1165. $fin[] = $this->quoteName($name[$i], $as[$i]);
  1166. }
  1167. }
  1168. return $fin;
  1169. }
  1170. }
  1171. /**
  1172. * Quote strings coming from quoteName call.
  1173. *
  1174. * @param array $strArr Array of strings coming from quoteName dot-explosion.
  1175. *
  1176. * @return string Dot-imploded string of quoted parts.
  1177. *
  1178. * @since 11.3
  1179. */
  1180. protected function quoteNameStr($strArr)
  1181. {
  1182. $parts = array();
  1183. $q = $this->nameQuote;
  1184. foreach ($strArr as $part)
  1185. {
  1186. if (is_null($part))
  1187. {
  1188. continue;
  1189. }
  1190. if (strlen($q) == 1)
  1191. {
  1192. $parts[] = $q . $part . $q;
  1193. }
  1194. else
  1195. {
  1196. $parts[] = $q{0} . $part . $q{1};
  1197. }
  1198. }
  1199. return implode('.', $parts);
  1200. }
  1201. /**
  1202. * This function replaces a string identifier <var>$prefix</var> with the string held is the
  1203. * <var>tablePrefix</var> class variable.
  1204. *
  1205. * @param string $sql The SQL statement to prepare.
  1206. * @param string $prefix The common table prefix.
  1207. *
  1208. * @return string The processed SQL statement.
  1209. *
  1210. * @since 11.1
  1211. */
  1212. public function replacePrefix($sql, $prefix = '#__')
  1213. {
  1214. // Initialize variables.
  1215. $escaped = false;
  1216. $startPos = 0;
  1217. $quoteChar = '';
  1218. $literal = '';
  1219. $sql = trim($sql);
  1220. $n = strlen($sql);
  1221. while ($startPos < $n)
  1222. {
  1223. $ip = strpos($sql, $prefix, $startPos);
  1224. if ($ip === false)
  1225. {
  1226. break;
  1227. }
  1228. $j = strpos($sql, "'", $startPos);
  1229. $k = strpos($sql, '"', $startPos);
  1230. if (($k !== false) && (($k < $j) || ($j === false)))
  1231. {
  1232. $quoteChar = '"';
  1233. $j = $k;
  1234. }
  1235. else
  1236. {
  1237. $quoteChar = "'";
  1238. }
  1239. if ($j === false)
  1240. {
  1241. $j = $n;
  1242. }
  1243. $literal .= str_replace($prefix, $this->tablePrefix, substr($sql, $startPos, $j - $startPos));
  1244. $startPos = $j;
  1245. $j = $startPos + 1;
  1246. if ($j >= $n)
  1247. {
  1248. break;
  1249. }
  1250. // quote comes first, find end of quote
  1251. while (true)
  1252. {
  1253. $k = strpos($sql, $quoteChar, $j);
  1254. $escaped = false;
  1255. if ($k === false)
  1256. {
  1257. break;
  1258. }
  1259. $l = $k - 1;
  1260. while ($l >= 0 && $sql{$l} == '\\')
  1261. {
  1262. $l--;
  1263. $escaped = !$escaped;
  1264. }
  1265. if ($escaped)
  1266. {
  1267. $j = $k + 1;
  1268. continue;
  1269. }
  1270. break;
  1271. }
  1272. if ($k === false)
  1273. {
  1274. // error in the query - no end quote; ignore it
  1275. break;
  1276. }
  1277. $literal .= substr($sql, $startPos, $k - $startPos + 1);
  1278. $startPos = $k + 1;
  1279. }
  1280. if ($startPos < $n)
  1281. {
  1282. $literal .= substr($sql, $startPos, $n - $startPos);
  1283. }
  1284. return $literal;
  1285. }
  1286. /**
  1287. * Renames a table in the database.
  1288. *
  1289. * @param string $oldTable The name of the table to be renamed
  1290. * @param string $newTable The new name for the table.
  1291. * @param string $backup Table prefix
  1292. * @param string $prefix For the table - used to rename constraints in non-mysql databases
  1293. *
  1294. * @return JDatabase Returns this object to support chaining.
  1295. *
  1296. * @since 11.4
  1297. * @throws JDatabaseException
  1298. */
  1299. public abstract function renameTable($oldTable, $newTable, $backup = null, $prefix = null);
  1300. /**
  1301. * Select a database for use.
  1302. *
  1303. * @param string $database The name of the database to select for use.
  1304. *
  1305. * @return boolean True if the database was successfully selected.
  1306. *
  1307. * @since 11.1
  1308. * @throws JDatabaseException
  1309. */
  1310. abstract public function select($database);
  1311. /**
  1312. * Sets the database debugging state for the driver.
  1313. *
  1314. * @param boolean $level True to enable debugging.
  1315. *
  1316. * @return boolean The old debugging level.
  1317. *
  1318. * @since 11.1
  1319. */
  1320. public function setDebug($level)
  1321. {
  1322. $previous = $this->debug;
  1323. $this->debug = (bool) $level;
  1324. return $previous;
  1325. }
  1326. /**
  1327. * Sets the SQL statement string for later execution.
  1328. *
  1329. * @param mixed $query The SQL statement to set either as a JDatabaseQuery object or a string.
  1330. * @param integer $offset The affected row offset to set.
  1331. * @param integer $limit The maximum affected rows to set.
  1332. *
  1333. * @return JDatabase This object to support method chaining.
  1334. *
  1335. * @since 11.1
  1336. */
  1337. public function setQuery($query, $offset = 0, $limit = 0)
  1338. {
  1339. $this->sql = $query;
  1340. $this->limit = (int) max(0, $limit);
  1341. $this->offset = (int) max(0, $offset);
  1342. return $this;
  1343. }
  1344. /**
  1345. * Set the connection to use UTF-8 character encoding.
  1346. *
  1347. * @return boolean True on success.
  1348. *
  1349. * @since 11.1
  1350. */
  1351. abstract public function setUTF();
  1352. /**
  1353. * Method to commit a transaction.
  1354. *
  1355. * @return void
  1356. *
  1357. * @since 11.1
  1358. * @throws JDatabaseException
  1359. */
  1360. abstract public function transactionCommit();
  1361. /**
  1362. * Method to roll back a transaction.
  1363. *
  1364. * @return void
  1365. *
  1366. * @since 11.1
  1367. * @throws JDatabaseException
  1368. */
  1369. abstract public function transactionRollback();
  1370. /**
  1371. * Method to initialize a transaction.
  1372. *
  1373. * @return void
  1374. *
  1375. * @since 11.1
  1376. * @throws JDatabaseException
  1377. */
  1378. abstract public function transactionStart();
  1379. /**
  1380. * Method to truncate a table.
  1381. *
  1382. * @param string $table The table to truncate
  1383. *
  1384. * @return void
  1385. *
  1386. * @since 11.3
  1387. * @throws JDatabaseException
  1388. */
  1389. public function truncateTable($table)
  1390. {
  1391. $this->setQuery('TRUNCATE TABLE ' . $this->quoteName($table));
  1392. $this->execute();
  1393. }
  1394. /**
  1395. * Updates a row in a table based on an object's properties.
  1396. *
  1397. * @param string $table The name of the database table to update.
  1398. * @param object &$object A reference to an object whose public properties match the table fields.
  1399. * @param string $key The name of the primary key.
  1400. * @param boolean $nulls True to update null fields or false to ignore them.
  1401. *
  1402. * @return boolean True on success.
  1403. *
  1404. * @since 11.1
  1405. * @throws JDatabaseException
  1406. */
  1407. public function updateObject($table, &$object, $key, $nulls = false)
  1408. {
  1409. // Initialise variables.
  1410. $fields = array();
  1411. $where = '';
  1412. // Create the base update statement.
  1413. $statement = 'UPDATE ' . $this->quoteName($table) . ' SET %s WHERE %s';
  1414. // Iterate over the object variables to build the query fields/value pairs.
  1415. foreach (get_object_vars($object) as $k => $v)
  1416. {
  1417. // Only process scalars that are not internal fields.
  1418. if (is_array($v) or is_object($v) or $k[0] == '_')
  1419. {
  1420. continue;
  1421. }
  1422. // Set the primary key to the WHERE clause instead of a field to update.
  1423. if ($k == $key)
  1424. {
  1425. $where = $this->quoteName($k) . '=' . $this->quote($v);
  1426. continue;
  1427. }
  1428. // Prepare and sanitize the fields and values for the database query.
  1429. if ($v === null)
  1430. {
  1431. // If the value is null and we want to update nulls then set it.
  1432. if ($nulls)
  1433. {
  1434. $val = 'NULL';
  1435. }
  1436. // If the value is null and we do not want to update nulls then ignore this field.
  1437. else
  1438. {
  1439. continue;
  1440. }
  1441. }
  1442. // The field is not null so we prep it for update.
  1443. else
  1444. {
  1445. $val = $this->quote($v);
  1446. }
  1447. // Add the field to be updated.
  1448. $fields[] = $this->quoteName($k) . '=' . $val;
  1449. }
  1450. // We don't have any fields to update.
  1451. if (empty($fields))
  1452. {
  1453. return true;
  1454. }
  1455. // Set the query and execute the update.
  1456. $this->setQuery(sprintf($statement, implode(",", $fields), $where));
  1457. return $this->execute();
  1458. }
  1459. /**
  1460. * Unlocks tables in the database.
  1461. *
  1462. * @return JDatabase Returns this object to support chaining.
  1463. *
  1464. * @since 11.4
  1465. * @throws JDatabaseException
  1466. */
  1467. public abstract function unlockTables();
  1468. //
  1469. // Deprecated methods.
  1470. //
  1471. /**
  1472. * Sets the debug level on or off
  1473. *
  1474. * @param integer $level 0 to disable debugging and 1 to enable it.
  1475. *
  1476. * @return void
  1477. *
  1478. * @deprecated 12.1
  1479. * @since 11.1
  1480. */
  1481. public function debug($level)
  1482. {
  1483. // Deprecation warning.
  1484. JLog::add('JDatabase::debug() is deprecated, use JDatabase::setDebug() instead.', JLog::NOTICE, 'deprecated');
  1485. $this->setDebug(($level == 0) ? false : true);
  1486. }
  1487. /**
  1488. * Diagnostic method to return explain information for a query.
  1489. *
  1490. * @return string The explain output.
  1491. *
  1492. * @deprecated 12.1
  1493. * @since 11.1
  1494. */
  1495. abstract public function explain();
  1496. /**
  1497. * Gets the error message from the database connection.
  1498. *
  1499. * @param boolean $escaped True to escape the message string for use in JavaScript.
  1500. *
  1501. * @return string The error message for the most recent query.
  1502. *
  1503. * @deprecated 12.1
  1504. * @since 11.1
  1505. */
  1506. public function getErrorMsg($escaped = false)
  1507. {
  1508. // Deprecation warning.
  1509. JLog::add('JDatabase::getErrorMsg() is deprecated, use exception handling instead.', JLog::WARNING, 'deprecated');
  1510. if ($escaped)
  1511. {
  1512. return addslashes($this->errorMsg);
  1513. }
  1514. else
  1515. {
  1516. return $this->errorMsg;
  1517. }
  1518. }
  1519. /**
  1520. * Gets the error number from the database connection.
  1521. *
  1522. * @return integer The error number for the most recent query.
  1523. *
  1524. * @since 11.1
  1525. * @deprecated 12.1
  1526. */
  1527. public function getErrorNum()
  1528. {
  1529. // Deprecation warning.
  1530. JLog::add('JDatabase::getErrorNum() is deprecated, use exception handling instead.', JLog::WARNING, 'deprecated');
  1531. return $this->errorNum;
  1532. }
  1533. /**
  1534. * Method to escape a string for usage in an SQL statement.
  1535. *
  1536. * @param string $text The string to be escaped.
  1537. * @param boolean $extra Optional parameter to provide extra escaping.
  1538. *
  1539. * @return string The escaped string.
  1540. *
  1541. * @since 11.1
  1542. * @deprecated 12.1
  1543. */
  1544. public function getEscaped($text, $extra = false)
  1545. {
  1546. // Deprecation warning.
  1547. JLog::add('JDatabase::getEscaped() is deprecated. Use JDatabase::escape().', JLog::WARNING, 'deprecated');
  1548. return $this->escape($text, $extra);
  1549. }
  1550. /**
  1551. * Retrieves field information about the given tables.
  1552. *
  1553. * @param mixed $tables A table name or a list of table names.
  1554. * @param boolean $typeOnly True to only return field types.
  1555. *
  1556. * @return array An array of fields by table.
  1557. *
  1558. * @since 11.1
  1559. * @throws JDatabaseException
  1560. * @deprecated 12.1
  1561. */
  1562. public function getTableFields($tables, $typeOnly = true)
  1563. {
  1564. // Deprecation warning.
  1565. JLog::add('JDatabase::getTableFields() is deprecated. Use JDatabase::getTableColumns().', JLog::WARNING, 'deprecated');
  1566. $results = array();
  1567. settype($tables, 'array');
  1568. foreach ($tables as $table)
  1569. {
  1570. $results[$table] = $this->getTableColumns($table, $typeOnly);
  1571. }
  1572. return $results;
  1573. }
  1574. /**
  1575. * Get the total number of SQL statements executed by the database driver.
  1576. *
  1577. * @return integer
  1578. *
  1579. * @since 11.1
  1580. * @deprecated 12.1
  1581. */
  1582. public function getTicker()
  1583. {
  1584. // Deprecation warning.
  1585. JLog::add('JDatabase::getTicker() is deprecated, use JDatabase::getCount() instead.', JLog::NOTICE, 'deprecated');
  1586. return $this->count;
  1587. }
  1588. /**
  1589. * Checks if field name needs to be quoted.
  1590. *
  1591. * @param string $field The field name to be checked.
  1592. *
  1593. * @return bool
  1594. *
  1595. * @deprecated 12.1
  1596. * @since 11.1
  1597. */
  1598. public function isQuoted($field)
  1599. {
  1600. // Deprecation warning.
  1601. JLog::add('JDatabase::isQuoted() is deprecated.', JLog::WARNING, 'deprecated');
  1602. if ($this->hasQuoted)
  1603. {
  1604. return in_array($field, $this->quoted);
  1605. }
  1606. else
  1607. {
  1608. return true;
  1609. }
  1610. }
  1611. /**
  1612. * Method to get an array of values from the <var>$offset</var> field in each row of the result set from
  1613. * the database query.
  1614. *
  1615. * @param integer $offset The row offset to use to build the result array.
  1616. *
  1617. * @return mixed The return value or null if the query failed.
  1618. *
  1619. * @since 11.1
  1620. * @throws JDatabaseException
  1621. * @deprecated 12.1
  1622. */
  1623. public function loadResultArray($offset = 0)
  1624. {
  1625. // Deprecation warning.
  1626. JLog::add('JDatabase::loadResultArray() is deprecated. Use JDatabase::loadColumn().', JLog::WARNING, 'deprecated');
  1627. return $this->loadColumn($offset);
  1628. }
  1629. /**
  1630. * Wrap an SQL statement identifier name such as column, table or database names in quotes to prevent injection
  1631. * risks and reserved word conflicts.
  1632. *
  1633. * @param string $name The identifier name to wrap in quotes.
  1634. *
  1635. * @return string The quote wrapped name.
  1636. *
  1637. * @since 11.1
  1638. * @deprecated 12.1
  1639. */
  1640. public function nameQuote($name)
  1641. {
  1642. // Deprecation warning.
  1643. JLog::add('JDatabase::nameQuote() is deprecated. Use JDatabase::quoteName().', JLog::WARNING, 'deprecated');
  1644. return $this->quoteName($name);
  1645. }
  1646. /**
  1647. * Execute a query batch.
  1648. *
  1649. * @param boolean $abortOnError Abort on error.
  1650. * @param boolean $transactionSafe Transaction safe queries.
  1651. *
  1652. * @return mixed A database resource if successful, false if not.
  1653. *
  1654. * @deprecated 12.1
  1655. * @since 11.1
  1656. */
  1657. abstract public function queryBatch($abortOnError = true, $transactionSafe = false);
  1658. /**
  1659. * Return the most recent error message for the database connector.
  1660. *
  1661. * @param boolean $showSQL True to display the SQL statement sent to the database as well as the error.
  1662. *
  1663. * @return string The error message for the most recent query.
  1664. *
  1665. * @deprecated 12.1
  1666. * @since 11.1
  1667. */
  1668. public function stderr($showSQL = false)
  1669. {
  1670. // Deprecation warning.
  1671. JLog::add('JDatabase::stderr() is deprecated.', JLog::WARNING, 'deprecated');
  1672. if ($this->errorNum != 0)
  1673. {
  1674. return JText::sprintf('JLIB_DATABASE_ERROR_FUNCTION_FAILED', $this->errorNum, $this->errorMsg)
  1675. . ($showSQL ? "<br />SQL = <pre>$this->sql</pre>" : '');
  1676. }
  1677. else
  1678. {
  1679. return JText::_('JLIB_DATABASE_FUNCTION_NOERROR');
  1680. }
  1681. }
  1682. }