2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* SQLite layer for DBO
|
|
|
|
*
|
2010-10-03 16:38:58 +00:00
|
|
|
* PHP 5
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-11-06 06:46:59 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2012-03-13 02:46:07 +00:00
|
|
|
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2012-03-13 02:46:07 +00:00
|
|
|
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2009-11-06 06:00:11 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Model.Datasource.Database
|
2008-10-30 17:30:26 +00:00
|
|
|
* @since CakePHP(tm) v 0.9.0
|
2009-11-06 06:51:51 +00:00
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-12-08 00:29:07 +00:00
|
|
|
App::uses('DboSource', 'Model/Datasource');
|
2011-10-27 01:17:20 +00:00
|
|
|
App::uses('String', 'Utility');
|
2010-12-08 00:29:07 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-11-27 04:16:34 +00:00
|
|
|
* DBO implementation for the SQLite3 DBMS.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-11-27 04:16:34 +00:00
|
|
|
* A DboSource adapter for SQLite 3 using PDO
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Model.Datasource.Database
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-12-08 00:29:07 +00:00
|
|
|
class Sqlite extends DboSource {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-05-29 15:10:48 +00:00
|
|
|
* Datasource Description
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-05-29 15:10:48 +00:00
|
|
|
* @var string
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-08-19 02:16:16 +00:00
|
|
|
public $description = "SQLite DBO Driver";
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-11-27 04:16:34 +00:00
|
|
|
* Quote Start
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2011-08-19 02:16:16 +00:00
|
|
|
public $startQuote = '"';
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-11-27 04:16:34 +00:00
|
|
|
* Quote End
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2011-08-19 02:16:16 +00:00
|
|
|
public $endQuote = '"';
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-11-27 04:16:34 +00:00
|
|
|
* Base configuration settings for SQLite3 driver
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2011-08-19 02:16:16 +00:00
|
|
|
protected $_baseConfig = array(
|
2010-11-27 04:16:34 +00:00
|
|
|
'persistent' => false,
|
2009-12-24 10:03:23 +00:00
|
|
|
'database' => null
|
2008-05-30 11:40:08 +00:00
|
|
|
);
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-11-27 04:16:34 +00:00
|
|
|
* SQLite3 column definition
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2011-08-19 02:16:16 +00:00
|
|
|
public $columns = array(
|
2010-11-27 04:16:34 +00:00
|
|
|
'primary_key' => array('name' => 'integer primary key autoincrement'),
|
2008-05-30 11:40:08 +00:00
|
|
|
'string' => array('name' => 'varchar', 'limit' => '255'),
|
|
|
|
'text' => array('name' => 'text'),
|
2010-11-27 04:16:34 +00:00
|
|
|
'integer' => array('name' => 'integer', 'limit' => null, 'formatter' => 'intval'),
|
2012-08-30 13:26:17 +00:00
|
|
|
'biginteger' => array('name' => 'bigint', 'limit' => 20),
|
2008-05-30 11:40:08 +00:00
|
|
|
'float' => array('name' => 'float', 'formatter' => 'floatval'),
|
|
|
|
'datetime' => array('name' => 'datetime', 'format' => 'Y-m-d H:i:s', 'formatter' => 'date'),
|
|
|
|
'timestamp' => array('name' => 'timestamp', 'format' => 'Y-m-d H:i:s', 'formatter' => 'date'),
|
|
|
|
'time' => array('name' => 'time', 'format' => 'H:i:s', 'formatter' => 'date'),
|
|
|
|
'date' => array('name' => 'date', 'format' => 'Y-m-d', 'formatter' => 'date'),
|
|
|
|
'binary' => array('name' => 'blob'),
|
|
|
|
'boolean' => array('name' => 'boolean')
|
|
|
|
);
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-10-29 05:36:15 +00:00
|
|
|
/**
|
|
|
|
* List of engine specific additional field parameters used on table creating
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2011-08-19 02:16:16 +00:00
|
|
|
public $fieldParameters = array(
|
2009-10-29 06:03:50 +00:00
|
|
|
'collate' => array(
|
|
|
|
'value' => 'COLLATE',
|
|
|
|
'quote' => false,
|
2010-11-27 04:16:34 +00:00
|
|
|
'join' => ' ',
|
|
|
|
'column' => 'Collate',
|
2009-10-29 06:03:50 +00:00
|
|
|
'position' => 'afterDefault',
|
|
|
|
'options' => array(
|
|
|
|
'BINARY', 'NOCASE', 'RTRIM'
|
|
|
|
)
|
|
|
|
),
|
2009-10-29 05:36:15 +00:00
|
|
|
);
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Connects to the database using config['database'] as a filename.
|
|
|
|
*
|
2011-07-30 22:38:57 +00:00
|
|
|
* @return boolean
|
2011-07-31 20:55:52 +00:00
|
|
|
* @throws MissingConnectionException
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-05-28 20:38:46 +00:00
|
|
|
public function connect() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$config = $this->config;
|
2011-09-03 16:47:00 +00:00
|
|
|
$flags = array(
|
|
|
|
PDO::ATTR_PERSISTENT => $config['persistent'],
|
|
|
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
|
|
|
|
);
|
2010-11-27 04:16:34 +00:00
|
|
|
try {
|
2010-11-27 05:07:13 +00:00
|
|
|
$this->_connection = new PDO('sqlite:' . $config['database'], null, null, $flags);
|
2010-11-27 04:16:34 +00:00
|
|
|
$this->connected = true;
|
2011-01-04 04:34:22 +00:00
|
|
|
} catch(PDOException $e) {
|
2012-09-13 02:31:07 +00:00
|
|
|
throw new MissingConnectionException(array(
|
|
|
|
'class' => get_class($this),
|
|
|
|
'message' => $e->getMessage()
|
|
|
|
));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
return $this->connected;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-10-20 20:11:31 +00:00
|
|
|
/**
|
2012-01-25 04:11:24 +00:00
|
|
|
* Check whether the SQLite extension is installed/loaded
|
2009-10-20 20:11:31 +00:00
|
|
|
*
|
|
|
|
* @return boolean
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2011-05-28 20:38:46 +00:00
|
|
|
public function enabled() {
|
2010-11-27 04:16:34 +00:00
|
|
|
return in_array('sqlite', PDO::getAvailableDrivers());
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns an array of tables in the database. If there are no tables, an error is raised and the application exits.
|
|
|
|
*
|
2011-07-30 22:38:57 +00:00
|
|
|
* @param mixed $data
|
2011-12-02 05:58:09 +00:00
|
|
|
* @return array Array of table names in the database
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-05-28 20:38:46 +00:00
|
|
|
public function listSources($data = null) {
|
2010-11-27 04:16:34 +00:00
|
|
|
$cache = parent::listSources();
|
2012-09-21 22:30:43 +00:00
|
|
|
if ($cache) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return $cache;
|
|
|
|
}
|
2010-11-27 04:16:34 +00:00
|
|
|
|
2008-07-30 19:18:48 +00:00
|
|
|
$result = $this->fetchAll("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name;", false);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-11-27 04:16:34 +00:00
|
|
|
if (!$result || empty($result)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return array();
|
|
|
|
}
|
2012-09-21 22:30:43 +00:00
|
|
|
|
|
|
|
$tables = array();
|
|
|
|
foreach ($result as $table) {
|
|
|
|
$tables[] = $table[0]['name'];
|
|
|
|
}
|
|
|
|
parent::listSources($tables);
|
|
|
|
return $tables;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns an array of the fields in given table name.
|
|
|
|
*
|
2011-10-15 01:25:14 +00:00
|
|
|
* @param Model|string $model Either the model or table name you want described.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return array Fields in table. Keys are name and type
|
|
|
|
*/
|
2011-10-15 01:25:14 +00:00
|
|
|
public function describe($model) {
|
2012-03-11 14:24:05 +00:00
|
|
|
$table = $this->fullTableName($model, false, false);
|
|
|
|
$cache = parent::describe($table);
|
2012-09-21 22:30:43 +00:00
|
|
|
if ($cache) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return $cache;
|
|
|
|
}
|
|
|
|
$fields = array();
|
2012-03-22 00:56:08 +00:00
|
|
|
$result = $this->_execute(
|
|
|
|
'PRAGMA table_info(' . $this->value($table, 'string') . ')'
|
|
|
|
);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
foreach ($result as $column) {
|
2012-03-04 19:18:04 +00:00
|
|
|
$column = (array)$column;
|
2010-11-27 05:07:13 +00:00
|
|
|
$default = ($column['dflt_value'] === 'NULL') ? null : trim($column['dflt_value'], "'");
|
|
|
|
|
|
|
|
$fields[$column['name']] = array(
|
2012-03-04 19:18:04 +00:00
|
|
|
'type' => $this->column($column['type']),
|
|
|
|
'null' => !$column['notnull'],
|
|
|
|
'default' => $default,
|
|
|
|
'length' => $this->length($column['type'])
|
2008-05-30 11:40:08 +00:00
|
|
|
);
|
2010-12-03 19:11:39 +00:00
|
|
|
if ($column['pk'] == 1) {
|
|
|
|
$fields[$column['name']]['key'] = $this->index['PRI'];
|
|
|
|
$fields[$column['name']]['null'] = false;
|
|
|
|
if (empty($fields[$column['name']]['length'])) {
|
|
|
|
$fields[$column['name']]['length'] = 11;
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-27 05:07:13 +00:00
|
|
|
$result->closeCursor();
|
2011-10-15 01:25:14 +00:00
|
|
|
$this->_cacheDescription($table, $fields);
|
2008-05-30 11:40:08 +00:00
|
|
|
return $fields;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Generates and executes an SQL UPDATE statement for given model, fields, and values.
|
|
|
|
*
|
|
|
|
* @param Model $model
|
|
|
|
* @param array $fields
|
|
|
|
* @param array $values
|
|
|
|
* @param mixed $conditions
|
|
|
|
* @return array
|
|
|
|
*/
|
2011-07-30 23:17:20 +00:00
|
|
|
public function update(Model $model, $fields = array(), $values = null, $conditions = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (empty($values) && !empty($fields)) {
|
|
|
|
foreach ($fields as $field => $value) {
|
|
|
|
if (strpos($field, $model->alias . '.') !== false) {
|
|
|
|
unset($fields[$field]);
|
|
|
|
$field = str_replace($model->alias . '.', "", $field);
|
|
|
|
$field = str_replace($model->alias . '.', "", $field);
|
|
|
|
$fields[$field] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-11-27 04:16:34 +00:00
|
|
|
return parent::update($model, $fields, $values, $conditions);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Deletes all the records in a table and resets the count of the auto-incrementing
|
|
|
|
* primary key, where applicable.
|
|
|
|
*
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param string|Model $table A string or model class representing the table to be truncated
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return boolean SQL TRUNCATE TABLE statement, false if not applicable.
|
|
|
|
*/
|
2011-05-28 20:38:46 +00:00
|
|
|
public function truncate($table) {
|
2011-11-05 10:57:08 +00:00
|
|
|
$this->_execute('DELETE FROM sqlite_sequence where name=' . $this->startQuote . $this->fullTableName($table, false, false) . $this->endQuote);
|
2011-03-19 17:16:12 +00:00
|
|
|
return $this->execute('DELETE FROM ' . $this->fullTableName($table));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Converts database-layer column types to basic types
|
|
|
|
*
|
|
|
|
* @param string $real Real database-layer column type (i.e. "varchar(255)")
|
|
|
|
* @return string Abstract column type (i.e. "string")
|
|
|
|
*/
|
2011-05-28 20:38:46 +00:00
|
|
|
public function column($real) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (is_array($real)) {
|
|
|
|
$col = $real['name'];
|
|
|
|
if (isset($real['limit'])) {
|
2011-12-16 06:52:07 +00:00
|
|
|
$col .= '(' . $real['limit'] . ')';
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
return $col;
|
|
|
|
}
|
|
|
|
|
|
|
|
$col = strtolower(str_replace(')', '', $real));
|
|
|
|
$limit = null;
|
2010-11-27 04:16:34 +00:00
|
|
|
@list($col, $limit) = explode('(', $col);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2012-08-30 13:26:17 +00:00
|
|
|
$standard = array(
|
|
|
|
'text',
|
|
|
|
'integer',
|
|
|
|
'float',
|
|
|
|
'boolean',
|
|
|
|
'timestamp',
|
|
|
|
'date',
|
|
|
|
'datetime',
|
|
|
|
'time'
|
|
|
|
);
|
|
|
|
if (in_array($col, $standard)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return $col;
|
|
|
|
}
|
2012-08-30 13:26:17 +00:00
|
|
|
if ($col === 'bigint') {
|
|
|
|
return 'biginteger';
|
|
|
|
}
|
2011-11-08 04:16:45 +00:00
|
|
|
if (strpos($col, 'char') !== false) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return 'string';
|
|
|
|
}
|
|
|
|
if (in_array($col, array('blob', 'clob'))) {
|
|
|
|
return 'binary';
|
|
|
|
}
|
2010-11-27 04:16:34 +00:00
|
|
|
if (strpos($col, 'numeric') !== false || strpos($col, 'decimal') !== false) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return 'float';
|
|
|
|
}
|
|
|
|
return 'text';
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-11-27 04:16:34 +00:00
|
|
|
* Generate ResultSet
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-11-27 04:16:34 +00:00
|
|
|
* @param mixed $results
|
2011-07-30 22:38:57 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-05-28 20:38:46 +00:00
|
|
|
public function resultSet($results) {
|
2010-11-27 04:16:34 +00:00
|
|
|
$this->results = $results;
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->map = array();
|
2012-03-04 19:18:04 +00:00
|
|
|
$numFields = $results->columnCount();
|
2010-11-27 04:16:34 +00:00
|
|
|
$index = 0;
|
|
|
|
$j = 0;
|
|
|
|
|
|
|
|
//PDO::getColumnMeta is experimental and does not work with sqlite3,
|
|
|
|
// so try to figure it out based on the querystring
|
|
|
|
$querystring = $results->queryString;
|
|
|
|
if (stripos($querystring, 'SELECT') === 0) {
|
2011-01-04 22:14:33 +00:00
|
|
|
$last = strripos($querystring, 'FROM');
|
2010-11-27 04:16:34 +00:00
|
|
|
if ($last !== false) {
|
|
|
|
$selectpart = substr($querystring, 7, $last - 8);
|
2011-10-27 01:17:20 +00:00
|
|
|
$selects = String::tokenize($selectpart, ',', '(', ')');
|
2010-11-27 04:16:34 +00:00
|
|
|
}
|
|
|
|
} elseif (strpos($querystring, 'PRAGMA table_info') === 0) {
|
|
|
|
$selects = array('cid', 'name', 'type', 'notnull', 'dflt_value', 'pk');
|
2011-11-30 15:44:11 +00:00
|
|
|
} elseif (strpos($querystring, 'PRAGMA index_list') === 0) {
|
2010-11-27 04:16:34 +00:00
|
|
|
$selects = array('seq', 'name', 'unique');
|
2011-11-30 15:44:11 +00:00
|
|
|
} elseif (strpos($querystring, 'PRAGMA index_info') === 0) {
|
2010-11-27 04:16:34 +00:00
|
|
|
$selects = array('seqno', 'cid', 'name');
|
|
|
|
}
|
2012-03-04 19:18:04 +00:00
|
|
|
while ($j < $numFields) {
|
2011-09-03 21:07:45 +00:00
|
|
|
if (!isset($selects[$j])) {
|
|
|
|
$j++;
|
|
|
|
continue;
|
|
|
|
}
|
2010-11-27 04:16:34 +00:00
|
|
|
if (preg_match('/\bAS\s+(.*)/i', $selects[$j], $matches)) {
|
2011-12-01 07:21:31 +00:00
|
|
|
$columnName = trim($matches[1], '"');
|
2010-11-27 04:16:34 +00:00
|
|
|
} else {
|
|
|
|
$columnName = trim(str_replace('"', '', $selects[$j]));
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-11-27 04:16:34 +00:00
|
|
|
if (strpos($selects[$j], 'DISTINCT') === 0) {
|
|
|
|
$columnName = str_ireplace('DISTINCT', '', $columnName);
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2011-01-02 02:41:28 +00:00
|
|
|
$metaType = false;
|
|
|
|
try {
|
|
|
|
$metaData = (array)$results->getColumnMeta($j);
|
|
|
|
if (!empty($metaData['sqlite:decl_type'])) {
|
|
|
|
$metaType = trim($metaData['sqlite:decl_type']);
|
|
|
|
}
|
2012-03-04 19:18:04 +00:00
|
|
|
} catch (Exception $e) {
|
|
|
|
}
|
2011-01-02 02:41:28 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
if (strpos($columnName, '.')) {
|
|
|
|
$parts = explode('.', $columnName);
|
2011-01-02 02:41:28 +00:00
|
|
|
$this->map[$index++] = array(trim($parts[0]), trim($parts[1]), $metaType);
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
2011-01-02 02:41:28 +00:00
|
|
|
$this->map[$index++] = array(0, $columnName, $metaType);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
$j++;
|
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Fetches the next row from the current result set
|
|
|
|
*
|
2010-11-27 04:16:34 +00:00
|
|
|
* @return mixed array with results fetched and mapped to column names or false if there is no results left to fetch
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-05-28 20:38:46 +00:00
|
|
|
public function fetchResult() {
|
2011-11-25 18:05:32 +00:00
|
|
|
if ($row = $this->_result->fetch(PDO::FETCH_NUM)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$resultRow = array();
|
2010-11-27 04:16:34 +00:00
|
|
|
foreach ($this->map as $col => $meta) {
|
2011-01-04 21:55:23 +00:00
|
|
|
list($table, $column, $type) = $meta;
|
2010-11-27 04:16:34 +00:00
|
|
|
$resultRow[$table][$column] = $row[$col];
|
2011-01-04 21:30:10 +00:00
|
|
|
if ($type == 'boolean' && !is_null($row[$col])) {
|
2011-01-02 05:56:57 +00:00
|
|
|
$resultRow[$table][$column] = $this->boolean($resultRow[$table][$column]);
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
return $resultRow;
|
|
|
|
} else {
|
2010-11-27 04:16:34 +00:00
|
|
|
$this->_result->closeCursor();
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns a limit statement in the correct format for the particular database.
|
|
|
|
*
|
|
|
|
* @param integer $limit Limit of results returned
|
|
|
|
* @param integer $offset Offset from which to start results
|
|
|
|
* @return string SQL limit/offset statement
|
|
|
|
*/
|
2011-05-28 20:38:46 +00:00
|
|
|
public function limit($limit, $offset = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($limit) {
|
|
|
|
$rt = '';
|
|
|
|
if (!strpos(strtolower($limit), 'limit') || strpos(strtolower($limit), 'limit') === 0) {
|
|
|
|
$rt = ' LIMIT';
|
|
|
|
}
|
|
|
|
$rt .= ' ' . $limit;
|
|
|
|
if ($offset) {
|
|
|
|
$rt .= ' OFFSET ' . $offset;
|
|
|
|
}
|
|
|
|
return $rt;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Generate a database-native column schema string
|
|
|
|
*
|
|
|
|
* @param array $column An array structured like the following: array('name'=>'value', 'type'=>'value'[, options]),
|
2010-11-27 04:16:34 +00:00
|
|
|
* where options can be 'default', 'length', or 'key'.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2011-05-28 20:38:46 +00:00
|
|
|
public function buildColumn($column) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$name = $type = null;
|
|
|
|
$column = array_merge(array('null' => true), $column);
|
|
|
|
extract($column);
|
|
|
|
|
|
|
|
if (empty($name) || empty($type)) {
|
2011-03-20 15:35:43 +00:00
|
|
|
trigger_error(__d('cake_dev', 'Column name or type not defined in schema'), E_USER_WARNING);
|
2008-05-30 11:40:08 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($this->columns[$type])) {
|
2011-03-20 15:35:43 +00:00
|
|
|
trigger_error(__d('cake_dev', 'Column type %s does not exist', $type), E_USER_WARNING);
|
2008-05-30 11:40:08 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2009-06-02 03:03:51 +00:00
|
|
|
if (isset($column['key']) && $column['key'] == 'primary' && $type == 'integer') {
|
|
|
|
return $this->name($name) . ' ' . $this->columns['primary_key']['name'];
|
|
|
|
}
|
2009-10-29 05:36:15 +00:00
|
|
|
return parent::buildColumn($column);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Sets the database encoding
|
|
|
|
*
|
|
|
|
* @param string $enc Database encoding
|
2011-07-30 22:38:57 +00:00
|
|
|
* @return boolean
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-05-28 20:38:46 +00:00
|
|
|
public function setEncoding($enc) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!in_array($enc, array("UTF-8", "UTF-16", "UTF-16le", "UTF-16be"))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return $this->_execute("PRAGMA encoding = \"{$enc}\"") !== false;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Gets the database encoding
|
|
|
|
*
|
|
|
|
* @return string The database encoding
|
|
|
|
*/
|
2011-05-28 20:38:46 +00:00
|
|
|
public function getEncoding() {
|
2008-05-30 11:40:08 +00:00
|
|
|
return $this->fetchRow('PRAGMA encoding');
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Removes redundant primary key indexes, as they are handled in the column def of the key.
|
|
|
|
*
|
|
|
|
* @param array $indexes
|
|
|
|
* @param string $table
|
|
|
|
* @return string
|
|
|
|
*/
|
2011-05-28 20:38:46 +00:00
|
|
|
public function buildIndex($indexes, $table = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$join = array();
|
|
|
|
|
2011-11-05 10:57:08 +00:00
|
|
|
$table = str_replace('"', '', $table);
|
|
|
|
list($dbname, $table) = explode('.', $table);
|
|
|
|
$dbname = $this->name($dbname);
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
foreach ($indexes as $name => $value) {
|
|
|
|
|
|
|
|
if ($name == 'PRIMARY') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$out = 'CREATE ';
|
|
|
|
|
|
|
|
if (!empty($value['unique'])) {
|
|
|
|
$out .= 'UNIQUE ';
|
|
|
|
}
|
|
|
|
if (is_array($value['column'])) {
|
2012-09-15 10:06:02 +00:00
|
|
|
$value['column'] = implode(', ', array_map(array(&$this, 'name'), $value['column']));
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
|
|
|
$value['column'] = $this->name($value['column']);
|
|
|
|
}
|
2010-12-03 19:55:04 +00:00
|
|
|
$t = trim($table, '"');
|
2012-03-04 19:18:04 +00:00
|
|
|
$indexname = $this->name($t . '_' . $name);
|
2011-11-05 10:57:08 +00:00
|
|
|
$table = $this->name($table);
|
|
|
|
$out .= "INDEX {$dbname}.{$indexname} ON {$table}({$value['column']});";
|
2008-05-30 11:40:08 +00:00
|
|
|
$join[] = $out;
|
|
|
|
}
|
|
|
|
return $join;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-12-10 03:38:10 +00:00
|
|
|
/**
|
2011-12-02 05:58:09 +00:00
|
|
|
* Overrides DboSource::index to handle SQLite index introspection
|
2008-12-10 03:38:10 +00:00
|
|
|
* Returns an array of the indexes in given table name.
|
|
|
|
*
|
|
|
|
* @param string $model Name of model to inspect
|
|
|
|
* @return array Fields in table. Keys are column and unique
|
|
|
|
*/
|
2011-05-28 20:38:46 +00:00
|
|
|
public function index($model) {
|
2008-12-10 03:38:10 +00:00
|
|
|
$index = array();
|
2011-11-05 10:57:08 +00:00
|
|
|
$table = $this->fullTableName($model, false, false);
|
2008-12-10 03:38:10 +00:00
|
|
|
if ($table) {
|
|
|
|
$indexes = $this->query('PRAGMA index_list(' . $table . ')');
|
2011-03-19 17:16:12 +00:00
|
|
|
|
2012-03-04 19:18:04 +00:00
|
|
|
if (is_bool($indexes)) {
|
2010-12-02 04:49:43 +00:00
|
|
|
return array();
|
|
|
|
}
|
2012-09-05 22:22:30 +00:00
|
|
|
foreach ($indexes as $info) {
|
2008-12-10 03:38:10 +00:00
|
|
|
$key = array_pop($info);
|
|
|
|
$keyInfo = $this->query('PRAGMA index_info("' . $key['name'] . '")');
|
|
|
|
foreach ($keyInfo as $keyCol) {
|
|
|
|
if (!isset($index[$key['name']])) {
|
|
|
|
$col = array();
|
|
|
|
if (preg_match('/autoindex/', $key['name'])) {
|
|
|
|
$key['name'] = 'PRIMARY';
|
|
|
|
}
|
|
|
|
$index[$key['name']]['column'] = $keyCol[0]['name'];
|
|
|
|
$index[$key['name']]['unique'] = intval($key['unique'] == 1);
|
|
|
|
} else {
|
|
|
|
if (!is_array($index[$key['name']]['column'])) {
|
|
|
|
$col[] = $index[$key['name']]['column'];
|
|
|
|
}
|
|
|
|
$col[] = $keyCol[0]['name'];
|
|
|
|
$index[$key['name']]['column'] = $col;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $index;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Overrides DboSource::renderStatement to handle schema generation with SQLite-style indexes
|
|
|
|
*
|
|
|
|
* @param string $type
|
|
|
|
* @param array $data
|
|
|
|
* @return string
|
|
|
|
*/
|
2011-05-28 20:38:46 +00:00
|
|
|
public function renderStatement($type, $data) {
|
2008-05-30 11:40:08 +00:00
|
|
|
switch (strtolower($type)) {
|
|
|
|
case 'schema':
|
|
|
|
extract($data);
|
2010-12-02 04:49:43 +00:00
|
|
|
if (is_array($columns)) {
|
2012-09-15 10:06:02 +00:00
|
|
|
$columns = "\t" . implode(",\n\t", array_filter($columns));
|
2010-12-02 04:49:43 +00:00
|
|
|
}
|
|
|
|
if (is_array($indexes)) {
|
2012-09-15 10:06:02 +00:00
|
|
|
$indexes = "\t" . implode("\n\t", array_filter($indexes));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
return "CREATE TABLE {$table} (\n{$columns});\n{$indexes}";
|
|
|
|
default:
|
|
|
|
return parent::renderStatement($type, $data);
|
|
|
|
}
|
|
|
|
}
|
2010-11-27 04:16:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* PDO deals in objects, not resources, so overload accordingly.
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2011-05-28 20:38:46 +00:00
|
|
|
public function hasResult() {
|
2010-11-27 04:16:34 +00:00
|
|
|
return is_object($this->_result);
|
|
|
|
}
|
2010-12-02 04:50:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate a "drop table" statement for the given Schema object
|
|
|
|
*
|
2011-07-31 22:03:28 +00:00
|
|
|
* @param CakeSchema $schema An instance of a subclass of CakeSchema
|
2010-12-02 04:50:08 +00:00
|
|
|
* @param string $table Optional. If specified only the table name given will be generated.
|
|
|
|
* Otherwise, all tables defined in the schema are generated.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function dropSchema(CakeSchema $schema, $table = null) {
|
|
|
|
$out = '';
|
|
|
|
foreach ($schema->tables as $curTable => $columns) {
|
|
|
|
if (!$table || $table == $curTable) {
|
|
|
|
$out .= 'DROP TABLE IF EXISTS ' . $this->fullTableName($curTable) . ";\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $out;
|
|
|
|
}
|
2011-11-05 10:57:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the schema name
|
|
|
|
*
|
|
|
|
* @return string The schema name
|
|
|
|
*/
|
|
|
|
public function getSchemaName() {
|
|
|
|
return "main"; // Sqlite Datasource does not support multidb
|
|
|
|
}
|
|
|
|
|
2012-04-01 02:39:18 +00:00
|
|
|
/**
|
|
|
|
* Check if the server support nested transactions
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2012-04-27 00:19:03 +00:00
|
|
|
public function nestedTransactionSupported() {
|
2012-04-27 00:53:18 +00:00
|
|
|
return $this->useNestedTransactions && version_compare($this->getVersion(), '3.6.8', '>=');
|
2012-04-01 02:39:18 +00:00
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|