Skip to content

Commit

Permalink
DibSQLite3Driver: improved by Roman Sklenar
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Dec 18, 2009
1 parent 686b60f commit 7688dd8
Showing 1 changed file with 87 additions and 8 deletions.
95 changes: 87 additions & 8 deletions dibi/drivers/sqlite3.php
Expand Up @@ -87,6 +87,12 @@ public function connect(array &$config)
if (strcasecmp($this->dbcharset, $this->charset) === 0) {
$this->dbcharset = $this->charset = NULL;
}

// enable foreign keys support (defaultly disabled; if disabled then foreign key constraints are not enforced)
$version = SQLite3::version();
if ($version['versionNumber'] >= '3006019') {
$this->query("PRAGMA foreign_keys = ON");
}
}


Expand Down Expand Up @@ -154,7 +160,7 @@ public function getInsertId($sequence)
*/
public function begin($savepoint = NULL)
{
$this->query('BEGIN');
$this->query($savepoint ? "SAVEPOINT $savepoint" : 'BEGIN');
}


Expand All @@ -167,7 +173,7 @@ public function begin($savepoint = NULL)
*/
public function commit($savepoint = NULL)
{
$this->query('COMMIT');
$this->query($savepoint ? "RELEASE SAVEPOINT $savepoint" : 'COMMIT');
}


Expand All @@ -180,7 +186,7 @@ public function commit($savepoint = NULL)
*/
public function rollback($savepoint = NULL)
{
$this->query('ROLLBACK');
$this->query($savepoint ? "ROLLBACK TO SAVEPOINT $savepoint" : 'ROLLBACK');
}


Expand Down Expand Up @@ -274,6 +280,7 @@ public function applyLimit(&$sql, $limit, $offset)
/**
* Returns the number of rows in a result set.
* @return int
* @throws NotSupportedException
*/
public function getRowCount()
{
Expand Down Expand Up @@ -311,11 +318,11 @@ public function fetch($assoc)
* Moves cursor position without fetching row.
* @param int the 0-based cursor pos to seek to
* @return boolean TRUE on success, FALSE if unable to seek to specified record
* @throws DibiException
* @throws NotSupportedException
*/
public function seek($row)
{
throw new DibiDriverException('Cannot seek an unbuffered result set.');
throw new NotSupportedException('Cannot seek an unbuffered result set.');
}


Expand Down Expand Up @@ -397,7 +404,35 @@ public function getTables()
*/
public function getColumns($table)
{
throw new NotImplementedException;
$this->query("
SELECT sql FROM sqlite_master WHERE type = 'table' AND name = '$table'
UNION ALL
SELECT sql FROM sqlite_temp_master WHERE type = 'table' AND name = '$table'"
);
$meta = $this->fetch(TRUE);
$this->free();

$this->query("PRAGMA table_info([$table])");
$res = array();
while ($row = $this->fetch(TRUE)) {
$column = $row['name'];
$pattern = "/(\"$column\"|\[$column\]|$column)\s+[^,]+\s+PRIMARY\s+KEY\s+AUTOINCREMENT/Ui";
$type = explode('(', $row['type']);

$res[] = array(
'name' => $column,
'table' => $table,
'fullname' => "$table.$column",
'nativetype' => strtoupper($type[0]),
'size' => isset($type[1]) ? (int) $type[1] : NULL,
'nullable' => $row['notnull'] == '0',
'default' => $row['dflt_value'],
'autoincrement' => (bool) preg_match($pattern, $meta['sql']),
'vendor' => $row,
);
}
$this->free();
return $res;
}


Expand All @@ -409,7 +444,36 @@ public function getColumns($table)
*/
public function getIndexes($table)
{
throw new NotImplementedException;
$this->query("PRAGMA index_list([$table])");
$res = array();
while ($row = $this->fetch(TRUE)) {
$res[$row['name']]['name'] = $row['name'];
$res[$row['name']]['unique'] = (bool) $row['unique'];
}
$this->free();

foreach ($res as $index => $values) {
$this->query("PRAGMA index_info([$index])");
while ($row = $this->fetch(TRUE)) {
$res[$index]['columns'][$row['seqno']] = $row['name'];
}
}
$this->free();

$columns = $this->getColumns($table);
foreach ($res as $index => $values) {
$column = $res[$index]['columns'][0];
$primary = FALSE;
foreach ($columns as $info) {
if ($column == $info['name']) {
$primary = $info['vendor']['pk'];
break;
}
}
$res[$index]['primary'] = (bool) $primary;
}

return array_values($res);
}


Expand All @@ -421,7 +485,22 @@ public function getIndexes($table)
*/
public function getForeignKeys($table)
{
throw new NotImplementedException;
$this->query("PRAGMA foreign_key_list([$table])");
$res = array();
while ($row = $this->fetch(TRUE)) {
$res[$row['id']]['name'] = $row['id']; // foreign key name
$res[$row['id']]['local'][$row['seq']] = $row['from']; // local columns
$res[$row['id']]['table'] = $row['table']; // referenced table
$res[$row['id']]['foreign'][$row['seq']] = $row['to']; // referenced columns
$res[$row['id']]['onDelete'] = $row['on_delete'];
$res[$row['id']]['onUpdate'] = $row['on_update'];

if ($res[$row['id']]['foreign'][0] == NULL) {
$res[$row['id']]['foreign'] = NULL;
}
}
$this->free();
return array_values($res);
}


Expand Down

0 comments on commit 7688dd8

Please sign in to comment.