00001 <?php
00027 require_once 'Zend/Db/Adapter/Pdo/Abstract.php';
00028
00029
00039 class Zend_Db_Adapter_Pdo_Sqlite extends Zend_Db_Adapter_Pdo_Abstract
00040 {
00041
00047 protected $_pdoType = 'sqlite';
00048
00060 protected $_numericDataTypes = array(
00061 Zend_Db::INT_TYPE => Zend_Db::INT_TYPE,
00062 Zend_Db::BIGINT_TYPE => Zend_Db::BIGINT_TYPE,
00063 Zend_Db::FLOAT_TYPE => Zend_Db::FLOAT_TYPE,
00064 'INTEGER' => Zend_Db::BIGINT_TYPE,
00065 'REAL' => Zend_Db::FLOAT_TYPE
00066 );
00067
00084 public function __construct(array $config = array())
00085 {
00086 if (isset($config['sqlite2']) && $config['sqlite2']) {
00087 $this->_pdoType = 'sqlite2';
00088 }
00089
00090
00091 $this->_config['username'] = null;
00092 $this->_config['password'] = null;
00093
00094 return parent::__construct($config);
00095 }
00096
00104 protected function _checkRequiredOptions(array $config)
00105 {
00106
00107 if (! array_key_exists('dbname', $config)) {
00109 require_once 'Zend/Db/Adapter/Exception.php';
00110 throw new Zend_Db_Adapter_Exception("Configuration array must have a key for 'dbname' that names the database instance");
00111 }
00112 }
00113
00117 protected function _dsn()
00118 {
00119 return $this->_pdoType .':'. $this->_config['dbname'];
00120 }
00121
00128 protected function _connect()
00129 {
00133 if ($this->_connection) {
00134 return;
00135 }
00136
00137 parent::_connect();
00138
00139 $retval = $this->_connection->exec('PRAGMA full_column_names=0');
00140 if ($retval === false) {
00141 $error = $this->_connection->errorInfo();
00143 require_once 'Zend/Db/Adapter/Exception.php';
00144 throw new Zend_Db_Adapter_Exception($error[2]);
00145 }
00146
00147 $retval = $this->_connection->exec('PRAGMA short_column_names=1');
00148 if ($retval === false) {
00149 $error = $this->_connection->errorInfo();
00151 require_once 'Zend/Db/Adapter/Exception.php';
00152 throw new Zend_Db_Adapter_Exception($error[2]);
00153 }
00154 }
00155
00161 public function listTables()
00162 {
00163 $sql = "SELECT name FROM sqlite_master WHERE type='table' "
00164 . "UNION ALL SELECT name FROM sqlite_temp_master "
00165 . "WHERE type='table' ORDER BY name";
00166
00167 return $this->fetchCol($sql);
00168 }
00169
00198 public function describeTable($tableName, $schemaName = null)
00199 {
00200 $sql = 'PRAGMA ';
00201
00202 if ($schemaName) {
00203 $sql .= $this->quoteIdentifier($schemaName) . '.';
00204 }
00205
00206 $sql .= 'table_info('.$this->quoteIdentifier($tableName).')';
00207
00208 $stmt = $this->query($sql);
00209
00213 $result = $stmt->fetchAll(Zend_Db::FETCH_NUM);
00214
00215 $cid = 0;
00216 $name = 1;
00217 $type = 2;
00218 $notnull = 3;
00219 $dflt_value = 4;
00220 $pk = 5;
00221
00222 $desc = array();
00223
00224 $p = 1;
00225 foreach ($result as $key => $row) {
00226 list($length, $scale, $precision, $primary, $primaryPosition, $identity) =
00227 array(null, null, null, false, null, false);
00228 if (preg_match('/^((?:var)?char)\((\d+)\)/i', $row[$type], $matches)) {
00229 $row[$type] = $matches[1];
00230 $length = $matches[2];
00231 } else if (preg_match('/^decimal\((\d+),(\d+)\)/i', $row[$type], $matches)) {
00232 $row[$type] = 'DECIMAL';
00233 $precision = $matches[1];
00234 $scale = $matches[2];
00235 }
00236 if ((bool) $row[$pk]) {
00237 $primary = true;
00238 $primaryPosition = $p;
00242 $identity = (bool) ($row[$type] == 'INTEGER');
00243 ++$p;
00244 }
00245 $desc[$this->foldCase($row[$name])] = array(
00246 'SCHEMA_NAME' => $this->foldCase($schemaName),
00247 'TABLE_NAME' => $this->foldCase($tableName),
00248 'COLUMN_NAME' => $this->foldCase($row[$name]),
00249 'COLUMN_POSITION' => $row[$cid]+1,
00250 'DATA_TYPE' => $row[$type],
00251 'DEFAULT' => $row[$dflt_value],
00252 'NULLABLE' => ! (bool) $row[$notnull],
00253 'LENGTH' => $length,
00254 'SCALE' => $scale,
00255 'PRECISION' => $precision,
00256 'UNSIGNED' => null,
00257 'PRIMARY' => $primary,
00258 'PRIMARY_POSITION' => $primaryPosition,
00259 'IDENTITY' => $identity
00260 );
00261 }
00262 return $desc;
00263 }
00264
00273 public function limit($sql, $count, $offset = 0)
00274 {
00275 $count = intval($count);
00276 if ($count <= 0) {
00278 require_once 'Zend/Db/Adapter/Exception.php';
00279 throw new Zend_Db_Adapter_Exception("LIMIT argument count=$count is not valid");
00280 }
00281
00282 $offset = intval($offset);
00283 if ($offset < 0) {
00285 require_once 'Zend/Db/Adapter/Exception.php';
00286 throw new Zend_Db_Adapter_Exception("LIMIT argument offset=$offset is not valid");
00287 }
00288
00289 $sql .= " LIMIT $count";
00290 if ($offset > 0) {
00291 $sql .= " OFFSET $offset";
00292 }
00293
00294 return $sql;
00295 }
00296
00297 }