Source for file Sqlite.php

Documentation is available at Sqlite.php

  1. <?php
  2. /*
  3.  *  $Id: Sqlite.php 1889 2007-06-28 12:11:55Z zYne $
  4.  *
  5.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6.  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8.  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9.  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11.  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15.  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16.  *
  17.  * This software consists of voluntary contributions made by many individuals
  18.  * and is licensed under the LGPL. For more information, see
  19.  * <http://www.phpdoctrine.com>.
  20.  */
  21. Doctrine::autoload('Doctrine_Import');
  22. /**
  23.  * @package     Doctrine
  24.  * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
  25.  * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
  26.  * @author      Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
  27.  * @version     $Revision: 1889 $
  28.  * @category    Object Relational Mapping
  29.  * @link        www.phpdoctrine.com
  30.  * @since       1.0
  31.  */
  32. {
  33.     /**
  34.      * lists all databases
  35.      *
  36.      * @return array 
  37.      */
  38.     public function listDatabases()
  39.     {
  40.  
  41.     }
  42.     /**
  43.      * lists all availible database functions
  44.      *
  45.      * @return array 
  46.      */
  47.     public function listFunctions()
  48.     {
  49.  
  50.     }
  51.     /**
  52.      * lists all database triggers
  53.      *
  54.      * @param string|null$database 
  55.      * @return array 
  56.      */
  57.     public function listTriggers($database null)
  58.     {
  59.  
  60.     }
  61.     /**
  62.      * lists all database sequences
  63.      *
  64.      * @param string|null$database 
  65.      * @return array 
  66.      */
  67.     public function listSequences($database null)
  68.     {
  69.         $query      "SELECT name FROM sqlite_master WHERE type='table' AND sql NOT NULL ORDER BY name";
  70.         $tableNames $this->conn->fetchColumn($query);
  71.  
  72.         $result array();
  73.         foreach ($tableNames as $tableName{
  74.             if ($sqn $this->conn->fixSequenceName($tableNametrue)) {
  75.                 $result[$sqn;
  76.             }
  77.         }
  78.         if ($this->conn->getAttribute(Doctrine::ATTR_PORTABILITYDoctrine::PORTABILITY_FIX_CASE{
  79.             $result array_map(($this->conn->getAttribute(Doctrine::ATTR_FIELD_CASE== CASE_LOWER 'strtolower' 'strtoupper')$result);
  80.         }
  81.         return $result;
  82.     }
  83.     /**
  84.      * lists table constraints
  85.      *
  86.      * @param string $table     database table name
  87.      * @return array 
  88.      */
  89.     public function listTableConstraints($table)
  90.     {
  91.         $table $this->conn->quote($table'text');
  92.  
  93.         $query "SELECT sql FROM sqlite_master WHERE type='index' AND ";
  94.  
  95.         if ($this->conn->getAttribute(Doctrine::ATTR_PORTABILITYDoctrine::PORTABILITY_FIX_CASE{
  96.             $query .= 'LOWER(tbl_name) = ' strtolower($table);
  97.         else {
  98.             $query .= 'tbl_name = ' $table;
  99.         }
  100.         $query  .= ' AND sql NOT NULL ORDER BY name';
  101.         $indexes $this->conn->fetchColumn($query);
  102.  
  103.         $result array();
  104.         foreach ($indexes as $sql{
  105.             if (preg_match("/^create unique index ([^ ]+) on /i"$sql$tmp)) {
  106.                 $index $this->conn->fixIndexName($tmp[1]);
  107.                 if empty($index)) {
  108.                     $result[$indextrue;
  109.                 }
  110.             }
  111.         }
  112.  
  113.         if ($this->conn->getAttribute(Doctrine::ATTR_PORTABILITYDoctrine::PORTABILITY_FIX_CASE{
  114.             $result array_change_key_case($result$this->conn->getAttribute(Doctrine::ATTR_FIELD_CASE));
  115.         }
  116.         return array_keys($result);
  117.     }
  118.     /**
  119.      * lists table constraints
  120.      *
  121.      * @param string $table     database table name
  122.      * @return array 
  123.      */
  124.     public function listTableColumns($table)
  125.     {
  126.         $sql    'PRAGMA table_info(' $table ')';
  127.         $result $this->conn->fetchAll($sql);
  128.  
  129.         $description array();
  130.         $columns     array();
  131.         foreach ($result as $key => $val{
  132.             $val array_change_key_case($valCASE_LOWER);
  133.             $decl $this->conn->dataDict->getPortableDeclaration($val);
  134.  
  135.             $description array(
  136.                     'name'      => $val['name'],
  137.                     'ntype'     => $val['type'],
  138.                     'type'      => $decl['type'][0],
  139.                     'alltypes'  => $decl['type'],
  140.                     'notnull'   => (bool) $val['notnull'],
  141.                     'default'   => $val['dflt_value'],
  142.                     'primary'   => (bool) $val['pk'],
  143.                     'length'    => null,
  144.                     'scale'     => null,
  145.                     'precision' => null,
  146.                     'unsigned'  => null,
  147.                     );
  148.             $columns[$val['name']] $description;
  149.         }
  150.         return $columns;
  151.     }
  152.     /**
  153.      * lists table constraints
  154.      *
  155.      * @param string $table     database table name
  156.      * @return array 
  157.      */
  158.     public function listTableIndexes($table)
  159.     {
  160.         $sql  'PRAGMA index_list(' $table ')';
  161.         return $this->conn->fetchColumn($sql);
  162.    }
  163.     /**
  164.      * lists tables
  165.      *
  166.      * @param string|null$database 
  167.      * @return array 
  168.      */
  169.     public function listTables($database null)
  170.     {
  171.         $sql "SELECT name FROM sqlite_master WHERE type = 'table' "
  172.              . "UNION ALL SELECT name FROM sqlite_temp_master "
  173.              . "WHERE type = 'table' ORDER BY name";
  174.  
  175.         return $this->conn->fetchColumn($sql);
  176.     }
  177.     /**
  178.      * lists table triggers
  179.      *
  180.      * @param string $table     database table name
  181.      * @return array 
  182.      */
  183.     public function listTableTriggers($table)
  184.     {
  185.  
  186.     }
  187.     /**
  188.      * lists table views
  189.      *
  190.      * @param string $table     database table name
  191.      * @return array 
  192.      */
  193.     public function listTableViews($table)
  194.     {
  195.         $query "SELECT name, sql FROM sqlite_master WHERE type='view' AND sql NOT NULL";
  196.         $views $db->fetchAll($query);
  197.  
  198.         $result array();
  199.         foreach ($views as $row{
  200.             if (preg_match("/^create view .* \bfrom\b\s+\b{$table}\b /i"$row['sql'])) {
  201.                 if empty($row['name'])) {
  202.                     $result[$row['name']] true;
  203.                 }
  204.             }
  205.         }
  206.         return $result;
  207.     }
  208.     /**
  209.      * lists database users
  210.      *
  211.      * @return array 
  212.      */
  213.     public function listUsers()
  214.     {
  215.  
  216.     }
  217.     /**
  218.      * lists database views
  219.      *
  220.      * @param string|null$database 
  221.      * @return array 
  222.      */
  223.     public function listViews($database null)
  224.     {
  225.         $query "SELECT name FROM sqlite_master WHERE type='view' AND sql NOT NULL";
  226.  
  227.         return $this->conn->fetchColumn($query);
  228.     }
  229. }