From 1aad1c8da9ba46d9a093634c987eda0e134f7f71 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Wed, 28 Nov 2007 15:56:57 +0000 Subject: [PATCH] * 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 --- dibi/dibi.php | 8 +-- dibi/drivers/mssql.php | 17 ++++-- dibi/drivers/mysql.php | 30 ++++++++--- dibi/drivers/mysqli.php | 33 ++++++++---- dibi/drivers/odbc.php | 17 ++++-- dibi/drivers/oracle.php | 16 ++++-- dibi/drivers/pdo.php | 16 ++++-- dibi/drivers/postgre.php | 15 ++++-- dibi/drivers/sqlite.php | 20 ++++--- dibi/libs/DibiResult.php | 30 +++++------ dibi/libs/DibiResultIterator.php | 1 + examples/datetime.demo.php | 4 +- examples/sql-builder.php | 90 ++++++++++++-------------------- 13 files changed, 170 insertions(+), 127 deletions(-) diff --git a/dibi/dibi.php b/dibi/dibi.php index 9054874..1c1e460 100644 --- a/dibi/dibi.php +++ b/dibi/dibi.php @@ -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 '
', $sql, "
\n"; } diff --git a/dibi/drivers/mssql.php b/dibi/drivers/mssql.php index aa7d82e..32913e8 100644 --- a/dibi/drivers/mssql.php +++ b/dibi/drivers/mssql.php @@ -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 { diff --git a/dibi/drivers/mysql.php b/dibi/drivers/mysql.php index 6f38a09..7aec643 100644 --- a/dibi/drivers/mysql.php +++ b/dibi/drivers/mysql.php @@ -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']); diff --git a/dibi/drivers/mysqli.php b/dibi/drivers/mysqli.php index 65bfcd0..c98b4e3 100644 --- a/dibi/drivers/mysqli.php +++ b/dibi/drivers/mysqli.php @@ -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); } diff --git a/dibi/drivers/odbc.php b/dibi/drivers/odbc.php index d147725..9ef40ac 100644 --- a/dibi/drivers/odbc.php +++ b/dibi/drivers/odbc.php @@ -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 { diff --git a/dibi/drivers/oracle.php b/dibi/drivers/oracle.php index b6c85d8..c86b945 100644 --- a/dibi/drivers/oracle.php +++ b/dibi/drivers/oracle.php @@ -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) { diff --git a/dibi/drivers/pdo.php b/dibi/drivers/pdo.php index dc02b77..bd3555f 100644 --- a/dibi/drivers/pdo.php +++ b/dibi/drivers/pdo.php @@ -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) { diff --git a/dibi/drivers/postgre.php b/dibi/drivers/postgre.php index c24e7c0..4b3d1c8 100644 --- a/dibi/drivers/postgre.php +++ b/dibi/drivers/postgre.php @@ -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 { diff --git a/dibi/drivers/sqlite.php b/dibi/drivers/sqlite.php index 3f6ccc1..4f723dc 100644 --- a/dibi/drivers/sqlite.php +++ b/dibi/drivers/sqlite.php @@ -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); diff --git a/dibi/libs/DibiResult.php b/dibi/libs/DibiResult.php index 4cfca23..66c1bad 100644 --- a/dibi/libs/DibiResult.php +++ b/dibi/libs/DibiResult.php @@ -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']; } } } diff --git a/dibi/libs/DibiResultIterator.php b/dibi/libs/DibiResultIterator.php index 6d98734..cb226e6 100644 --- a/dibi/libs/DibiResultIterator.php +++ b/dibi/libs/DibiResultIterator.php @@ -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++; } diff --git a/examples/datetime.demo.php b/examples/datetime.demo.php index 3e7016c..592f482 100644 --- a/examples/datetime.demo.php +++ b/examples/datetime.demo.php @@ -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'); diff --git a/examples/sql-builder.php b/examples/sql-builder.php index b177bab..bc4d964 100644 --- a/examples/sql-builder.php +++ b/examples/sql-builder.php @@ -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, ")");