SQLiteTableInfo.php

Go to the documentation of this file.
00001 <?php 00002 /* 00003 * $Id: SQLiteTableInfo.php,v 1.5 2004/03/20 04:16:50 hlellelid Exp $ 00004 * 00005 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00006 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00007 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00008 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00009 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00010 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00011 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00012 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00013 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00014 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00015 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00016 * 00017 * This software consists of voluntary contributions made by many individuals 00018 * and is licensed under the LGPL. For more information please see 00019 * <http://creole.phpdb.org>. 00020 */ 00021 00022 require_once 'creole/metadata/TableInfo.php'; 00023 00031 class SQLiteTableInfo extends TableInfo { 00032 00034 protected function initColumns() 00035 { 00036 00037 include_once 'creole/metadata/ColumnInfo.php'; 00038 include_once 'creole/metadata/PrimaryKeyInfo.php'; 00039 include_once 'creole/drivers/sqlite/SQLiteTypes.php'; 00040 00041 // To get all of the attributes we need, we'll actually do 00042 // two separate queries. The first gets names and default values 00043 // the second will fill in some more details. 00044 00045 $sql = 'PRAGMA table_info('.$this->name.')'; 00046 00047 $res = sqlite_query($this->conn->getResource(), $sql); 00048 00049 00050 while($row = sqlite_fetch_array($res, SQLITE_ASSOC)) { 00051 00052 $name = $row['name']; 00053 00054 $fulltype = $row['type']; 00055 $size = null; 00056 $scale = null; 00057 if (preg_match('/^([^\(]+)\(\s*(\d+)\s*,\s*(\d+)\s*\)$/', $fulltype, $matches)) { 00058 $type = $matches[1]; 00059 $size = $matches[2]; 00060 $scale = $matches[3]; // aka precision 00061 } elseif (preg_match('/^([^\(]+)\(\s*(\d+)\s*\)$/', $fulltype, $matches)) { 00062 $type = $matches[1]; 00063 $size = $matches[2]; 00064 } else { 00065 $type = $fulltype; 00066 } 00067 00068 $not_null = $row['notnull']; 00069 $default_val = $row['dflt_value']; 00070 00071 $this->columns[$name] = new ColumnInfo($this, $name, SQLiteTypes::getType($type), $type, $size, $scale, $is_nullable, $default); 00072 00073 if (strtolower($type) == 'integer primary key') { 00074 if ($this->primaryKey === null) { 00075 $this->primaryKey = new PrimaryKeyInfo($name); 00076 } 00077 $this->primaryKey->addColumn($this->columns[ $name ]); 00078 } 00079 00080 } 00081 00082 $this->colsLoaded = true; 00083 } 00084 00086 protected function initPrimaryKey() 00087 { 00088 // columns have to be loaded first 00089 if (!$this->colsLoaded) $this->initColumns(); 00090 // keys are loaded by initColumns() in this class. 00091 $this->pkLoaded = true; 00092 } 00093 00095 protected function initIndexes() { 00096 00097 include_once 'creole/metadata/IndexInfo.php'; 00098 00099 // columns have to be loaded first 00100 if (!$this->colsLoaded) $this->initColumns(); 00101 00102 $sql = 'PRAGMA index_list('.$this->name.')'; 00103 $res = sqlite_query($this->conn->getResource(), $sql); 00104 00105 while($row = sqlite_fetch_array($res, SQLITE_ASSOC)) { 00106 $name = $row['name']; 00107 $this->indexes[$name] = new IndexInfo($name); 00108 00109 // get columns for that index 00110 $res2 = sqlite_query($this->conn->getResource(), 'PRAGMA index_info('.$name.')'); 00111 while($row2 = sqlite_fetch_array($res2, SQLITE_ASSOC)) { 00112 $colname = $row2['name']; 00113 $this->indexes[$name]->addColumn($this->columns[ $colname ]); 00114 } 00115 } 00116 00117 $this->indexesLoaded = true; 00118 } 00119 00121 protected function initForeignKeys() { 00122 00123 // columns have to be loaded first 00124 if (!$this->colsLoaded) $this->initColumns(); 00125 00126 // No fkeys in SQLite 00127 00128 $this->fksLoaded = true; 00129 } 00130 00131 }

This file is part of the Creole[php5] library.


Copyright © 2004 Hans Lellelid  
Creole[php5] CVS