Source for file sqlite3.dbschema.php

Documentation is available at sqlite3.dbschema.php

  1. <?php
  2. /**
  3. * @package    jelix
  4. * @subpackage db
  5. * @author     Laurent Jouanneau
  6. * @contributor     Loic Mathaud
  7. * @copyright  2006 Loic Mathaud, 2007-2012 Laurent Jouanneau
  8. *
  9. * @link        http://www.jelix.org
  10. * @licence     http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public Licence, see LICENCE file
  11. */
  12.  
  13. /**
  14.  * 
  15.  * @package    jelix
  16.  * @subpackage db_driver
  17.  */
  18. class sqlite3DbTable extends jDbTable {
  19.  
  20.     protected function _loadColumns() {
  21.         $conn = $this->schema->getConn();
  22.         $this->columns = array();
  23.         $sql = "PRAGMA table_info(". $this->conn->quote($this->name) .")";
  24.         $rs = $conn->query($sql);
  25.  
  26.         while ($c = $rs->fetch()) {
  27.             $hasDefault = false;
  28.             $default = null;
  29.             $isPrimary  = ($c->pk == 1);
  30.             if (!$isPrimary) {
  31.                 if ($c->dflt_value !== null || ($c->dflt_value === null && !$notNull)) {
  32.                     $hasDefault = true;
  33.                     $default =  $c->dflt_value;
  34.                 }
  35.             }
  36.  
  37.             $length = 0;
  38.             if (preg_match('/^(\w+)\s*(\((\d+)\))?.*$/',$c->type,$m)) {
  39.                 $type = strtolower($m[1]);
  40.                 if (isset($m[3])) {
  41.                     $length = intval($m[3]);
  42.                 }
  43.             }
  44.             else {
  45.                 $type = $c->type;
  46.             }
  47.             $notNull   = ($c->notnull == '99' || $c->pk == 1);
  48.  
  49.             $col = new jDbColumn($c->name, $type,  $length, $hasDefault, $default, $notNull);
  50.  
  51.             $typeinfo = $this->getTypeInfo($type);
  52.             $col->nativeType = $typeinfo[0];
  53.             $col->maxValue = $typeinfo[3];
  54.             $col->minValue = $typeinfo[2];
  55.             $col->maxLength = $typeinfo[5];
  56.             $col->minLength = $typeinfo[4];
  57.  
  58.             if ($col->length !=0)
  59.                 $col->maxLength = $col->length;
  60.  
  61.             if ($col->type == 'integer' && $isPrimary) {
  62.                 $col->autoIncrement = true;
  63.             }
  64.             $this->columns[$col->name] = $col;
  65.         }
  66.     }
  67.  
  68.     protected function _alterColumn(jDbColumn $old, jDbColumn $new) {
  69.         throw new Exception ('Not Implemented');
  70.     }
  71.  
  72.     protected function _addColumn(jDbColumn $new) {
  73.         throw new Exception ('Not Implemented');
  74.     }
  75.  
  76.     protected function _loadIndexesAndKeys() {
  77.         throw new Exception ('Not Implemented');
  78.     }
  79.  
  80.     protected function _createIndex(jDbIndex $index) {
  81.         throw new Exception ('Not Implemented');
  82.     }
  83.  
  84.     protected function _dropIndex(jDbIndex $index) {
  85.         throw new Exception ('Not Implemented');
  86.     }
  87.  
  88.     protected function _loadReferences() {
  89.         throw new Exception ('Not Implemented');
  90.     }
  91.  
  92.     protected function _createReference(jDbReference $ref) {
  93.         throw new Exception ('Not Implemented');
  94.     }
  95.  
  96.     protected function _dropReference(jDbReference $ref) {
  97.         throw new Exception ('Not Implemented');
  98.     }
  99.  
  100. }
  101.  
  102. /**
  103.  * 
  104.  * @package    jelix
  105.  * @subpackage db_driver
  106.  */
  107. class sqlite3DbSchema extends jDbSchema {
  108.  
  109.     protected function _createTable($name, $columns, $primaryKey, $attributes = array()) {
  110.  
  111.         $cols = array();
  112.  
  113.         if (is_string($primaryKey))
  114.             $primaryKey = array($primaryKey);
  115.  
  116.         foreach ($columns as $col) {
  117.             $cols[] = $this->_prepareSqlColumn($col);
  118.         }
  119.  
  120.         $sql = 'CREATE TABLE '.$name.' ('.implode(", ",$cols);
  121.         if (count($primaryKey))
  122.             $sql .= ', CONSTRAINT '.$name.'_pkey PRIMARY KEY ('.implode(',',$primaryKey).') ';
  123.         $sql .= ')';
  124.  
  125.         $this->conn->exec($sql);
  126.         $table = new sqlite3DbTable($name, $this);
  127.         return $table;
  128.     }
  129.  
  130.     protected function _getTables() {
  131.         $results = array ();
  132.  
  133.         $rs = $this->schema->getConn()->query('SELECT name FROM sqlite_master WHERE type="table"');
  134.  
  135.         while ($line = $rs->fetch ()){
  136.             $results[$line->name] = new sqlite3DbTable($line->name, $this);
  137.         }
  138.  
  139.         return $results;
  140.     }
  141. }

Documentation generated on Wed, 04 Jan 2017 22:59:14 +0100 by phpDocumentor 1.4.3