PageRenderTime 51ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/libraries/joomla/database/database.php

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