Source for file sqlite.php

Documentation is available at sqlite.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.  * SQLite database driver
  14.  *
  15.  * @package     Joomla.Platform
  16.  * @subpackage  Database
  17.  * @see         http://php.net/pdo
  18.  * @since       12.1
  19.  */
  20. {
  21.     /**
  22.      * The name of the database driver.
  23.      *
  24.      * @var    string 
  25.      * @since  12.1
  26.      */
  27.     public $name = 'sqlite';
  28.  
  29.     /**
  30.      * The character(s) used to quote SQL statement names such as table names or field names,
  31.      * etc. The child classes should define this as necessary.  If a single character string the
  32.      * same character is used for both sides of the quoted name, else the first character will be
  33.      * used for the opening quote and the second for the closing quote.
  34.      *
  35.      * @var    string 
  36.      * @since  12.1
  37.      */
  38.     protected $nameQuote = '`';
  39.  
  40.     /**
  41.      * Destructor.
  42.      *
  43.      * @since   12.1
  44.      */
  45.     public function __destruct()
  46.     {
  47.         $this->freeResult();
  48.         unset($this->connection);
  49.     }
  50.  
  51.     /**
  52.      * Disconnects the database.
  53.      *
  54.      * @return  void 
  55.      *
  56.      * @since   12.1
  57.      */
  58.     public function disconnect()
  59.     {
  60.         $this->freeResult();
  61.         unset($this->connection);
  62.     }
  63.  
  64.     /**
  65.      * Drops a table from the database.
  66.      *
  67.      * @param   string   $tableName  The name of the database table to drop.
  68.      * @param   boolean  $ifExists   Optionally specify that the table must exist before it is dropped.
  69.      *
  70.      * @return  JDatabaseDriverSqlite  Returns this object to support chaining.
  71.      *
  72.      * @since   12.1
  73.      */
  74.     public function dropTable($tableName$ifExists true)
  75.     {
  76.         $this->connect();
  77.  
  78.         $query $this->getQuery(true);
  79.  
  80.         $this->setQuery('DROP TABLE ' ($ifExists 'IF EXISTS ' ''$query->quoteName($tableName));
  81.  
  82.         $this->execute();
  83.  
  84.         return $this;
  85.     }
  86.  
  87.     /**
  88.      * Method to escape a string for usage in an SQLite statement.
  89.      *
  90.      * Note: Using query objects with bound variables is
  91.      * preferable to the below.
  92.      *
  93.      * @param   string   $text   The string to be escaped.
  94.      * @param   boolean  $extra  Unused optional parameter to provide extra escaping.
  95.      *
  96.      * @return  string  The escaped string.
  97.      *
  98.      * @since   12.1
  99.      */
  100.     public function escape($text$extra false)
  101.     {
  102.         if (is_int($text|| is_float($text))
  103.         {
  104.             return $text;
  105.         }
  106.  
  107.         return SQLite3::escapeString($text);
  108.     }
  109.  
  110.     /**
  111.      * Method to get the database collation in use by sampling a text field of a table in the database.
  112.      *
  113.      * @return  mixed  The collation in use by the database or boolean false if not supported.
  114.      *
  115.      * @since   12.1
  116.      */
  117.     public function getCollation()
  118.     {
  119.         return $this->charset;
  120.     }
  121.  
  122.     /**
  123.      * Shows the table CREATE statement that creates the given tables.
  124.      *
  125.      * Note: Doesn't appear to have support in SQLite
  126.      *
  127.      * @param   mixed  $tables  A table name or a list of table names.
  128.      *
  129.      * @return  array  A list of the create SQL for the tables.
  130.      *
  131.      * @since   12.1
  132.      * @throws  RuntimeException
  133.      */
  134.     public function getTableCreate($tables)
  135.     {
  136.         $this->connect();
  137.  
  138.         // Sanitize input to an array and iterate over the list.
  139.         settype($tables'array');
  140.  
  141.         return $tables;
  142.     }
  143.  
  144.     /**
  145.      * Retrieves field information about a given table.
  146.      *
  147.      * @param   string   $table     The name of the database table.
  148.      * @param   boolean  $typeOnly  True to only return field types.
  149.      *
  150.      * @return  array  An array of fields for the database table.
  151.      *
  152.      * @since   12.1
  153.      * @throws  RuntimeException
  154.      */
  155.     public function getTableColumns($table$typeOnly true)
  156.     {
  157.         $this->connect();
  158.  
  159.         $columns array();
  160.         $query $this->getQuery(true);
  161.  
  162.         $fieldCasing $this->getOption(PDO::ATTR_CASE);
  163.  
  164.         $this->setOption(PDO::ATTR_CASEPDO::CASE_UPPER);
  165.  
  166.         $table strtoupper($table);
  167.  
  168.         $query->setQuery('pragma table_info(' $table ')');
  169.  
  170.         $this->setQuery($query);
  171.         $fields $this->loadObjectList();
  172.  
  173.         if ($typeOnly)
  174.         {
  175.             foreach ($fields as $field)
  176.             {
  177.                 $columns[$field->NAME$field->TYPE;
  178.             }
  179.         }
  180.         else
  181.         {
  182.             foreach ($fields as $field)
  183.             {
  184.                 // Do some dirty translation to MySQL output.
  185.                 // TODO: Come up with and implement a standard across databases.
  186.                 $columns[$field->NAME= (object) array(
  187.                     'Field' => $field->NAME,
  188.                     'Type' => $field->TYPE,
  189.                     'Null' => ($field->NOTNULL == '1' 'NO' 'YES'),
  190.                     'Default' => $field->DFLT_VALUE,
  191.                     'Key' => ($field->PK == '1' 'PRI' '')
  192.                 );
  193.             }
  194.         }
  195.  
  196.         $this->setOption(PDO::ATTR_CASE$fieldCasing);
  197.  
  198.         return $columns;
  199.     }
  200.  
  201.     /**
  202.      * Get the details list of keys for a table.
  203.      *
  204.      * @param   string  $table  The name of the table.
  205.      *
  206.      * @return  array  An array of the column specification for the table.
  207.      *
  208.      * @since   12.1
  209.      * @throws  RuntimeException
  210.      */
  211.     public function getTableKeys($table)
  212.     {
  213.         $this->connect();
  214.  
  215.         $keys array();
  216.         $query $this->getQuery(true);
  217.  
  218.         $fieldCasing $this->getOption(PDO::ATTR_CASE);
  219.  
  220.         $this->setOption(PDO::ATTR_CASEPDO::CASE_UPPER);
  221.  
  222.         $table strtoupper($table);
  223.         $query->setQuery('pragma table_info( ' $table ')');
  224.  
  225.         // $query->bind(':tableName', $table);
  226.  
  227.         $this->setQuery($query);
  228.         $rows $this->loadObjectList();
  229.  
  230.         foreach ($rows as $column)
  231.         {
  232.             if ($column->PK == 1)
  233.             {
  234.                 $keys[$column->NAME$column;
  235.             }
  236.         }
  237.  
  238.         $this->setOption(PDO::ATTR_CASE$fieldCasing);
  239.  
  240.         return $keys;
  241.     }
  242.  
  243.     /**
  244.      * Method to get an array of all tables in the database (schema).
  245.      *
  246.      * @return  array   An array of all the tables in the database.
  247.      *
  248.      * @since   12.1
  249.      * @throws  RuntimeException
  250.      */
  251.     public function getTableList()
  252.     {
  253.         $this->connect();
  254.  
  255.         $type 'table';
  256.  
  257.         $query $this->getQuery(true)
  258.             ->select('name')
  259.             ->from('sqlite_master')
  260.             ->where('type = :type')
  261.             ->bind(':type'$type)
  262.             ->order('name');
  263.  
  264.         $this->setQuery($query);
  265.  
  266.         $tables $this->loadColumn();
  267.  
  268.         return $tables;
  269.     }
  270.  
  271.     /**
  272.      * Get the version of the database connector.
  273.      *
  274.      * @return  string  The database connector version.
  275.      *
  276.      * @since   12.1
  277.      */
  278.     public function getVersion()
  279.     {
  280.         $this->connect();
  281.  
  282.         $this->setQuery("SELECT sqlite_version()");
  283.  
  284.         return $this->loadResult();
  285.     }
  286.  
  287.     /**
  288.      * Select a database for use.
  289.      *
  290.      * @param   string  $database  The name of the database to select for use.
  291.      *
  292.      * @return  boolean  True if the database was successfully selected.
  293.      *
  294.      * @since   12.1
  295.      * @throws  RuntimeException
  296.      */
  297.     public function select($database)
  298.     {
  299.         $this->connect();
  300.  
  301.         return true;
  302.     }
  303.  
  304.     /**
  305.      * Set the connection to use UTF-8 character encoding.
  306.      *
  307.      * Returns false automatically for the Oracle driver since
  308.      * you can only set the character set when the connection
  309.      * is created.
  310.      *
  311.      * @return  boolean  True on success.
  312.      *
  313.      * @since   12.1
  314.      */
  315.     public function setUTF()
  316.     {
  317.         $this->connect();
  318.  
  319.         return false;
  320.     }
  321.  
  322.     /**
  323.      * Locks a table in the database.
  324.      *
  325.      * @param   string  $table  The name of the table to unlock.
  326.      *
  327.      * @return  JDatabaseDriverSqlite  Returns this object to support chaining.
  328.      *
  329.      * @since   12.1
  330.      * @throws  RuntimeException
  331.      */
  332.     public function lockTable($table)
  333.     {
  334.         return $this;
  335.     }
  336.  
  337.     /**
  338.      * Renames a table in the database.
  339.      *
  340.      * @param   string  $oldTable  The name of the table to be renamed
  341.      * @param   string  $newTable  The new name for the table.
  342.      * @param   string  $backup    Not used by Sqlite.
  343.      * @param   string  $prefix    Not used by Sqlite.
  344.      *
  345.      * @return  JDatabaseDriverSqlite  Returns this object to support chaining.
  346.      *
  347.      * @since   12.1
  348.      * @throws  RuntimeException
  349.      */
  350.     public function renameTable($oldTable$newTable$backup null$prefix null)
  351.     {
  352.         $this->setQuery('ALTER TABLE ' $oldTable ' RENAME TO ' $newTable)->execute();
  353.  
  354.         return $this;
  355.     }
  356.  
  357.     /**
  358.      * Unlocks tables in the database.
  359.      *
  360.      * @return  JDatabaseDriverSqlite  Returns this object to support chaining.
  361.      *
  362.      * @since   12.1
  363.      * @throws  RuntimeException
  364.      */
  365.     public function unlockTables()
  366.     {
  367.         return $this;
  368.     }
  369.  
  370.     /**
  371.      * Test to see if the PDO ODBC connector is available.
  372.      *
  373.      * @return  boolean  True on success, false otherwise.
  374.      *
  375.      * @since   12.1
  376.      */
  377.     public static function isSupported()
  378.     {
  379.         return class_exists('PDO'&& in_array('sqlite'PDO::getAvailableDrivers());
  380.     }
  381.  
  382.     /**
  383.      * Method to commit a transaction.
  384.      *
  385.      * @param   boolean  $toSavepoint  If true, commit to the last savepoint.
  386.      *
  387.      * @return  void 
  388.      *
  389.      * @since   12.3
  390.      * @throws  RuntimeException
  391.      */
  392.     public function transactionCommit($toSavepoint false)
  393.     {
  394.         $this->connect();
  395.  
  396.         if (!$toSavepoint || $this->transactionDepth <= 1)
  397.         {
  398.             parent::transactionCommit($toSavepoint);
  399.         }
  400.         else
  401.         {
  402.             $this->transactionDepth--;
  403.         }
  404.     }
  405.  
  406.     /**
  407.      * Method to roll back a transaction.
  408.      *
  409.      * @param   boolean  $toSavepoint  If true, rollback to the last savepoint.
  410.      *
  411.      * @return  void 
  412.      *
  413.      * @since   12.3
  414.      * @throws  RuntimeException
  415.      */
  416.     public function transactionRollback($toSavepoint false)
  417.     {
  418.         $this->connect();
  419.  
  420.         if (!$toSavepoint || $this->transactionDepth <= 1)
  421.         {
  422.             parent::transactionRollback($toSavepoint);
  423.         }
  424.         else
  425.         {
  426.             $savepoint 'SP_' ($this->transactionDepth - 1);
  427.             $this->setQuery('ROLLBACK TO ' $this->quoteName($savepoint));
  428.  
  429.             if ($this->execute())
  430.             {
  431.                 $this->transactionDepth--;
  432.             }
  433.         }
  434.     }
  435.  
  436.     /**
  437.      * Method to initialize a transaction.
  438.      *
  439.      * @param   boolean  $asSavepoint  If true and a transaction is already active, a savepoint will be created.
  440.      *
  441.      * @return  void 
  442.      *
  443.      * @since   12.3
  444.      * @throws  RuntimeException
  445.      */
  446.     public function transactionStart($asSavepoint false)
  447.     {
  448.         $this->connect();
  449.  
  450.         if (!$asSavepoint || !$this->transactionDepth)
  451.         {
  452.             parent::transactionStart($asSavepoint);
  453.         }
  454.  
  455.         $savepoint 'SP_' $this->transactionDepth;
  456.         $this->setQuery('SAVEPOINT ' $this->quoteName($savepoint));
  457.  
  458.         if ($this->execute())
  459.         {
  460.             $this->transactionDepth++;
  461.         }
  462.     }
  463. }

Documentation generated on Tue, 19 Nov 2013 15:14:08 +0100 by phpDocumentor 1.4.3