Source for file driver.php

Documentation is available at driver.php

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

Documentation generated on Tue, 19 Nov 2013 15:01:42 +0100 by phpDocumentor 1.4.3