1
0
mirror of https://github.com/dg/dibi.git synced 2025-02-25 03:12:50 +01:00
php-dibi/dibi/drivers/sqlite.php
David Grudl af7c4de14a * removed static factory DibiDriver::connect()
* added support for lazy connections
2007-04-25 06:18:06 +00:00

228 lines
4.7 KiB
PHP

<?php
/**
* This file is part of the "dibi" project (http://dibi.texy.info/)
*
* Copyright (c) 2005-2007 David Grudl aka -dgx- <dave@dgx.cz>
*
* @version $Revision$ $Date$
* @package dibi
*/
// security - include dibi.php, not this file
if (!defined('DIBI')) die();
/**
* The dibi driver for SQlite database
*
*/
class DibiSqliteDriver extends DibiDriver
{
private
$insertId = FALSE,
$affectedRows = FALSE;
public
$formats = array(
'TRUE' => "1",
'FALSE' => "0",
'date' => "'Y-m-d'",
'datetime' => "'Y-m-d H:i:s'",
);
/**
* @param array connect configuration
* @throw DibiException
*/
public function __construct($config)
{
if (!extension_loaded('sqlite'))
throw new DibiException("PHP extension 'sqlite' is not loaded");
if (empty($config['database']))
throw new DibiException("Database must be specified");
if (!isset($config['mode']))
$config['mode'] = 0666;
parent::__construct($config);
}
protected function connect()
{
$config = $this->config;
$errorMsg = '';
if (empty($config['persistent']))
$conn = @sqlite_open($config['database'], $config['mode'], $errorMsg);
else
$conn = @sqlite_popen($config['database'], $config['mode'], $errorMsg);
if (!$conn)
throw new DibiException("Connecting error", array(
'message' => $errorMsg,
));
return $conn;
}
public function nativeQuery($sql)
{
$this->insertId = $this->affectedRows = FALSE;
$conn = $this->getResource();
$res = @sqlite_query($conn, $sql, SQLITE_ASSOC);
if ($res === FALSE) return FALSE;
$this->affectedRows = sqlite_changes($conn);
if ($this->affectedRows < 0) $this->affectedRows = FALSE;
$this->insertId = sqlite_last_insert_rowid($conn);
if ($this->insertId < 1) $this->insertId = FALSE;
if (is_resource($res))
return new DibiSqliteResult($res);
return TRUE;
}
public function affectedRows()
{
return $this->affectedRows;
}
public function insertId()
{
return $this->insertId;
}
public function begin()
{
return sqlite_query($this->getResource(), 'BEGIN');
}
public function commit()
{
return sqlite_query($this->getResource(), 'COMMIT');
}
public function rollback()
{
return sqlite_query($this->getResource(), 'ROLLBACK');
}
public function errorInfo()
{
$code = sqlite_last_error($this->getResource());
return array(
'message' => sqlite_error_string($code),
'code' => $code,
);
}
public function escape($value, $appendQuotes=TRUE)
{
return $appendQuotes
? "'" . sqlite_escape_string($value) . "'"
: sqlite_escape_string($value);
}
public function delimite($value)
{
return '[' . str_replace('.', '].[', $value) . ']';
}
public function getMetaData()
{
trigger_error('Meta is not implemented yet.', E_USER_WARNING);
}
/**
* @see DibiDriver::applyLimit()
*/
public function applyLimit(&$sql, $limit, $offset = 0)
{
if ($limit < 0 && $offset < 1) return;
$sql .= ' LIMIT ' . $limit . ($offset > 0 ? ' OFFSET ' . (int) $offset : '');
}
} // class DibiSqliteDriver
class DibiSqliteResult extends DibiResult
{
private $resource;
public function __construct($resource)
{
$this->resource = $resource;
}
public function rowCount()
{
return sqlite_num_rows($this->resource);
}
protected function doFetch()
{
return sqlite_fetch_array($this->resource, SQLITE_ASSOC);
}
public function seek($row)
{
return sqlite_seek($this->resource, $row);
}
protected function free()
{
}
/** this is experimental */
protected function buildMeta()
{
$count = sqlite_num_fields($this->resource);
$this->meta = $this->convert = array();
for ($index = 0; $index < $count; $index++) {
$name = sqlite_field_name($this->resource, $index);
$this->meta[$name] = array('type' => dibi::FIELD_UNKNOWN);
$this->convert[$name] = dibi::FIELD_UNKNOWN;
}
}
} // class DibiSqliteResult