1
0
mirror of https://github.com/dg/dibi.git synced 2025-02-21 09:23:57 +01:00

* better syntax highlighting

* all drivers checks for extension in constructor
* DibiMySqlDriver - charset is set by mysql_set_charset
* DibiMySqliDriver - charset is set by mysqli_set_charset
This commit is contained in:
David Grudl 2007-11-28 15:56:57 +00:00
parent 1a9abfb326
commit 1aad1c8da9
13 changed files with 170 additions and 127 deletions

View File

@ -539,11 +539,11 @@ class dibi extends NClass
} else {
if ($sql === NULL) $sql = self::$sql;
static $keywords2 = 'ALL|DISTINCT|AS|ON|INTO|AND|OR|AS';
static $keywords1 = 'SELECT|UPDATE|INSERT|DELETE|FROM|WHERE|HAVING|GROUP\s+BY|ORDER\s+BY|LIMIT|SET|VALUES|LEFT\s+JOIN|INNER\s+JOIN';
static $keywords2 = 'ALL|DISTINCT|DISTINCTROW|AS|USING|ON|AND|OR|IN|IS|NOT|NULL|LIKE|TRUE|FALSE';
static $keywords1 = 'SELECT|UPDATE|INSERT(?:\s+INTO)|REPLACE(?:\s+INTO)|DELETE|FROM|WHERE|HAVING|GROUP\s+BY|ORDER\s+BY|LIMIT|SET|VALUES|LEFT\s+JOIN|INNER\s+JOIN';
// insert new lines
$sql = preg_replace("#\\b(?:$keywords1)\\b#", "\n\$0", $sql);
$sql = preg_replace("#\\b(?:$keywords1)\\b#i", "\n\$0", $sql);
$sql = trim($sql);
// reduce spaces
@ -554,7 +554,7 @@ class dibi extends NClass
$sql = preg_replace("#\n{2,}#", "\n", $sql);
// syntax highlight
$sql = preg_replace_callback("#(/\\*.+?\\*/)|(\\*\\*.+?\\*\\*)|\\b($keywords1)\\b|\\b($keywords2)\\b#", array('dibi', 'highlightCallback'), $sql);
$sql = preg_replace_callback("#(/\\*.+?\\*/)|(\\*\\*.+?\\*\\*)|\\b($keywords1)\\b|\\b($keywords2)\\b#i", array('dibi', 'highlightCallback'), $sql);
echo '<pre class="dump">', $sql, "</pre>\n";
}

View File

@ -52,6 +52,18 @@ class DibiMsSqlDriver extends NObject implements DibiDriverInterface
/**
* @throws DibiException
*/
public function __construct()
{
if (!extension_loaded('mssql')) {
throw new DibiDriverException("PHP extension 'mssql' is not loaded");
}
}
/**
* Connects to a database
*
@ -64,11 +76,6 @@ class DibiMsSqlDriver extends NObject implements DibiDriverInterface
DibiConnection::alias($config, 'password', 'pass');
DibiConnection::alias($config, 'host');
if (!extension_loaded('mssql')) {
throw new DibiException("PHP extension 'mssql' is not loaded");
}
if (empty($config['persistent'])) {
$this->connection = @mssql_connect($config['host'], $config['username'], $config['password'], TRUE);
} else {

View File

@ -63,6 +63,19 @@ class DibiMySqlDriver extends NObject implements DibiDriverInterface
private $buffered;
/**
* @throws DibiException
*/
public function __construct()
{
if (!extension_loaded('mysql')) {
throw new DibiDriverException("PHP extension 'mysql' is not loaded");
}
}
/**
* Connects to a database
*
@ -71,10 +84,6 @@ class DibiMySqlDriver extends NObject implements DibiDriverInterface
*/
public function connect(array &$config)
{
if (!extension_loaded('mysql')) {
throw new DibiException("PHP extension 'mysql' is not loaded");
}
DibiConnection::alias($config, 'username', 'user');
DibiConnection::alias($config, 'password', 'pass');
DibiConnection::alias($config, 'options');
@ -110,12 +119,17 @@ class DibiMySqlDriver extends NObject implements DibiDriverInterface
}
if (isset($config['charset'])) {
@mysql_query("SET NAMES '" . $config['charset'] . "'", $this->connection);
// don't handle this error...
$ok = FALSE;
if (function_exists('mysql_set_charset')) {
// affects the character set used by mysql_real_escape_string() (was added in MySQL 5.0.7 and PHP 5.2.3)
$ok = @mysql_set_charset($config['charset'], $this->connection);
}
if (!$ok) $ok = @mysql_query("SET NAMES '" . $config['charset'] . "'", $this->connection);
if (!$ok) $this->throwException();
}
if (isset($config['database']) && !@mysql_select_db($config['database'], $this->connection)) {
$this->throwException();
if (isset($config['database'])) {
@mysql_select_db($config['database'], $this->connection) or $this->throwException();
}
$this->buffered = empty($config['unbuffered']);

View File

@ -63,6 +63,19 @@ class DibiMySqliDriver extends NObject implements DibiDriverInterface
private $buffered;
/**
* @throws DibiException
*/
public function __construct()
{
if (!extension_loaded('mysqli')) {
throw new DibiDriverException("PHP extension 'mysqli' is not loaded");
}
}
/**
* Connects to a database
*
@ -82,15 +95,10 @@ class DibiMySqliDriver extends NObject implements DibiDriverInterface
if (!isset($config['socket'])) $config['socket'] = ini_get('mysqli.default_socket');
if (!isset($config['host'])) {
$config['host'] = ini_get('mysqli.default_host');
if (!isset($config['port'])) ini_get('mysqli.default_port');
if (!isset($config['port'])) $config['port'] = ini_get('mysqli.default_port');
if (!isset($config['host'])) $config['host'] = 'localhost';
}
if (!extension_loaded('mysqli')) {
throw new DibiException("PHP extension 'mysqli' is not loaded");
}
$this->connection = mysqli_init();
@mysqli_real_connect($this->connection, $config['host'], $config['username'], $config['password'], $config['database'], $config['port'], $config['socket'], $config['options']);
@ -99,7 +107,10 @@ class DibiMySqliDriver extends NObject implements DibiDriverInterface
}
if (isset($config['charset'])) {
mysqli_query($this->connection, "SET NAMES '" . $config['charset'] . "'");
// affects the character set used by mysql_real_escape_string() (was added in MySQL 5.0.7 and PHP 5.0.5)
$ok = @mysqli_set_charset($this->connection, $config['charset']);
if (!$ok) $ok = @mysqli_query($this->connection, "SET NAMES '" . $config['charset'] . "'");
if (!$ok) $this->throwException();
}
$this->buffered = empty($config['unbuffered']);
@ -129,7 +140,7 @@ class DibiMySqliDriver extends NObject implements DibiDriverInterface
{
$this->resultset = @mysqli_query($this->connection, $sql, $this->buffered ? MYSQLI_STORE_RESULT : MYSQLI_USE_RESULT);
if ($errno = mysqli_errno($this->connection)) {
if (mysqli_errno($this->connection)) {
$this->throwException($sql);
}
@ -170,7 +181,7 @@ class DibiMySqliDriver extends NObject implements DibiDriverInterface
public function begin()
{
if (!mysqli_autocommit($this->connection, FALSE)) {
$this->throwException($sql);
$this->throwException();
}
}
@ -184,7 +195,7 @@ class DibiMySqliDriver extends NObject implements DibiDriverInterface
public function commit()
{
if (!mysqli_commit($this->connection)) {
$this->throwException($sql);
$this->throwException();
}
mysqli_autocommit($this->connection, TRUE);
}
@ -199,7 +210,7 @@ class DibiMySqliDriver extends NObject implements DibiDriverInterface
public function rollback()
{
if (!mysqli_rollback($this->connection)) {
$this->throwException($sql);
$this->throwException();
}
mysqli_autocommit($this->connection, TRUE);
}

View File

@ -58,6 +58,18 @@ class DibiOdbcDriver extends NObject implements DibiDriverInterface
/**
* @throws DibiException
*/
public function __construct()
{
if (!extension_loaded('odbc')) {
throw new DibiDriverException("PHP extension 'odbc' is not loaded");
}
}
/**
* Connects to a database
*
@ -74,11 +86,6 @@ class DibiOdbcDriver extends NObject implements DibiDriverInterface
if (!isset($config['password'])) $config['password'] = ini_get('odbc.default_pw');
if (!isset($config['dsn'])) $config['dsn'] = ini_get('odbc.default_db');
if (!extension_loaded('odbc')) {
throw new DibiException("PHP extension 'odbc' is not loaded");
}
if (empty($config['persistent'])) {
$this->connection = @odbc_connect($config['dsn'], $config['username'], $config['password']);
} else {

View File

@ -57,6 +57,18 @@ class DibiOracleDriver extends NObject implements DibiDriverInterface
/**
* @throws DibiException
*/
public function __construct()
{
if (!extension_loaded('oci8')) {
throw new DibiDriverException("PHP extension 'oci8' is not loaded");
}
}
/**
* Connects to a database
*
@ -70,10 +82,6 @@ class DibiOracleDriver extends NObject implements DibiDriverInterface
DibiConnection::alias($config, 'database', 'db');
DibiConnection::alias($config, 'charset');
if (!extension_loaded('oci8')) {
throw new DibiException("PHP extension 'oci8' is not loaded");
}
$this->connection = @oci_new_connect($config['username'], $config['password'], $config['database'], $config['charset']);
if (!$this->connection) {

View File

@ -58,6 +58,18 @@ class DibiPdoDriver extends NObject implements DibiDriverInterface
/**
* @throws DibiException
*/
public function __construct()
{
if (!extension_loaded('pdo')) {
throw new DibiDriverException("PHP extension 'pdo' is not loaded");
}
}
/**
* Connects to a database
*
@ -71,10 +83,6 @@ class DibiPdoDriver extends NObject implements DibiDriverInterface
DibiConnection::alias($config, 'dsn');
DibiConnection::alias($config, 'options');
if (!extension_loaded('pdo')) {
throw new DibiException("PHP extension 'pdo' is not loaded");
}
try {
$this->connection = new PDO($config['dsn'], $config['username'], $config['password'], $config['options']);
} catch (PDOException $e) {

View File

@ -51,6 +51,17 @@ class DibiPostgreDriver extends NObject implements DibiDriverInterface
/**
* @throws DibiException
*/
public function __construct()
{
if (!extension_loaded('pgsql')) {
throw new DibiDriverException("PHP extension 'pgsql' is not loaded");
}
}
/**
* Connects to a database
*
@ -59,10 +70,6 @@ class DibiPostgreDriver extends NObject implements DibiDriverInterface
*/
public function connect(array &$config)
{
if (!extension_loaded('pgsql')) {
throw new DibiException("PHP extension 'pgsql' is not loaded");
}
if (isset($config['string'])) {
$string = $config['string'];
} else {

View File

@ -57,6 +57,7 @@ class DibiSqliteDriver extends NObject implements DibiDriverInterface
*/
private $buffered;
/**
* Date and datetime format
* @var string
@ -64,6 +65,19 @@ class DibiSqliteDriver extends NObject implements DibiDriverInterface
private $fmtDate, $fmtDateTime;
/**
* @throws DibiException
*/
public function __construct()
{
if (!extension_loaded('sqlite')) {
throw new DibiDriverException("PHP extension 'sqlite' is not loaded");
}
}
/**
* Connects to a database
*
@ -76,10 +90,6 @@ class DibiSqliteDriver extends NObject implements DibiDriverInterface
$this->fmtDate = isset($config['format:date']) ? $config['format:date'] : 'U';
$this->fmtDateTime = isset($config['format:datetime']) ? $config['format:datetime'] : 'U';
if (!extension_loaded('sqlite')) {
throw new DibiException("PHP extension 'sqlite' is not loaded");
}
$errorMsg = '';
if (empty($config['persistent'])) {
$this->connection = @sqlite_open($config['database'], 0666, $errorMsg);
@ -116,8 +126,6 @@ class DibiSqliteDriver extends NObject implements DibiDriverInterface
*/
public function query($sql)
{
$errorMsg = NULL;
DibiDriverException::catchError();
if ($this->buffered) {
$this->resultset = sqlite_query($this->connection, $sql);

View File

@ -49,10 +49,10 @@ class DibiResult extends NObject implements IteratorAggregate, Countable
private $driver;
/**
* Describes columns types
* Translate table
* @var array
*/
private $convert;
private $xlat;
/**
* Describes columns types
@ -119,9 +119,7 @@ class DibiResult extends NObject implements IteratorAggregate, Countable
*/
final public function seek($row)
{
if ($row !== 0 || $this->fetched) {
return (bool) $this->getDriver()->seek($row);
}
return ($row !== 0 || $this->fetched) ? (bool) $this->getDriver()->seek($row) : TRUE;
}
@ -166,10 +164,11 @@ class DibiResult extends NObject implements IteratorAggregate, Countable
$this->fetched = TRUE;
// types-converting?
if ($t = $this->convert) { // little speed-up
if ($this->xlat) {
$xlat = $this->xlat; // little speed-up
foreach ($row as $key => $value) {
if (isset($t[$key])) {
$row[$key] = $this->convert($value, $t[$key]);
if (isset($xlat[$key])) {
$row[$key] = $this->convert($value, $xlat[$key]);
}
}
}
@ -191,11 +190,12 @@ class DibiResult extends NObject implements IteratorAggregate, Countable
$this->fetched = TRUE;
// types-converting?
if ($t = $this->convert) { // little speed-up
if ($this->xlat) {
$xlat = $this->xlat; // little speed-up
$value = reset($row);
$key = key($row);
return isset($t[$key])
? $this->convert($value, $t[$key])
return isset($xlat[$key])
? $this->convert($value, $xlat[$key])
: $value;
}
@ -365,10 +365,10 @@ class DibiResult extends NObject implements IteratorAggregate, Countable
$this->buildMeta();
} elseif (is_array($field)) {
$this->convert = $field;
$this->xlat = $field;
} else {
$this->convert[$field] = $type;
$this->xlat[$field] = $type;
}
}
@ -377,7 +377,7 @@ class DibiResult extends NObject implements IteratorAggregate, Countable
/** is this needed? */
final public function getType($field)
{
return isset($this->convert[$field]) ? $this->convert[$field] : NULL;
return isset($this->xlat[$field]) ? $this->xlat[$field] : NULL;
}
@ -439,7 +439,7 @@ class DibiResult extends NObject implements IteratorAggregate, Countable
if ($this->meta === NULL) {
$this->meta = $this->getDriver()->buildMeta();
foreach ($this->meta as $name => $info) {
$this->convert[$name] = $info['type'];
$this->xlat[$name] = $info['type'];
}
}
}

View File

@ -116,6 +116,7 @@ final class DibiResultIterator implements Iterator
*/
public function next()
{
//$this->result->seek($this->offset + $this->pointer + 1);
$this->row = $this->result->fetch();
$this->pointer++;
}

View File

@ -5,9 +5,7 @@ require_once '../dibi/dibi.php';
// required since PHP 5.1.0
if (function_exists('date_default_timezone_set')) {
date_default_timezone_set('Europe/Prague');
}
date_default_timezone_set('Europe/Prague');

View File

@ -8,9 +8,7 @@ pre.dibi { padding-bottom: 10px; }
require_once '../dibi/dibi.php';
// required since PHP 5.1.0
if (function_exists('date_default_timezone_set')) {
date_default_timezone_set('Europe/Prague');
}
date_default_timezone_set('Europe/Prague');
dibi::connect(array(
@ -19,68 +17,44 @@ dibi::connect(array(
));
$array1 = array(1, 2, 3);
$array2 = array('one', 'two', 'three');
$array3 = array(
'col1' => 'one',
'col2' => 'two',
'col3' => 'three',
);
$array4 = array(
'a' => 12,
'b' => NULL,
'c' => dibi::datetime(),
'd' => 'any string',
);
$array5 = array('RAND()', '[col1] > [col2]');
dibi::test("
SELECT *
FROM [db.table]
WHERE ([test.a] LIKE %d", '1995-03-01', "
OR [b1] IN (", $array1, ")
OR [b2] IN (%s", $array1, ")
OR [b3] IN (", $array2, ")
OR [b4] IN (%n", $array3, ")
OR [b5] IN (%sql", $array5, ")
OR [b6] IN (", array(), ")
AND [c] = 'embedded '' string'
OR [d]=%i", 10.3, "
OR [e]=%i", NULL, "
OR [true]=", TRUE, "
OR [false]=", FALSE, "
OR [str_null]=%sn", '', "
OR [str_not_null]=%sn", 'hello', "
LIMIT 10");
// dibi detects INSERT or REPLACE command
dibi::test("INSERT INTO [mytable]", $array4);
dibi::test('
REPLACE INTO [products]', array(
'title' => 'Drtička na trávu',
'price' => 318,
'active' => TRUE,
));
// dibi detects MULTI INSERT or REPLACE command
dibi::test("REPLACE INTO [mytable]", $array4, $array4, $array4);
// multiple INSERT command
$array = array(
'title' => 'Super Product',
'price' => 12,
'brand' => NULL,
'created' => dibi::datetime(),
);
dibi::test("INSERT INTO [products]", $array, $array, $array);
// dibi detects UPDATE command
$n = 123;
dibi::test("UPDATE [mytable] SET", $array4, " WHERE [id]=%i", $n);
dibi::test("
UPDATE [colors] SET", array(
'color' => 'blue',
'order' => 12,
), "WHERE [id]=%i", 123);
// array with modifier %a - assoc
dibi::test("UPDATE [mytable] SET%a", $array4, " WHERE [id]=%i", $n);
// SELECT
$ipMask = '192.168.%';
$timestamp = mktime(0, 0, 0, 10, 13, 1997);
dibi::test('
SELECT COUNT(*) as [count]
FROM [comments]
WHERE [ip] LIKE %s', $ipMask, 'AND [date] > ', dibi::date($timestamp)
);
// long numbers
dibi::test("SELECT %i", '-123456789123456789123456789');
// long float numbers
dibi::test("SELECT %f", '-.12345678912345678912345678e10');
// hex numbers
dibi::test("SELECT %i", '0x11');
// invalid input
dibi::test("SELECT %s", (object) array(123), ', %m', 123);
// IN array
$array = array(1, 2, 3);
dibi::test("SELECT * FROM [people] WHERE [id] IN (", $array, ")");