Permalink
Browse files

Merge branch 'master' of github.com:nette/dibi

  • Loading branch information...
2 parents d7b312d + 550be3b commit 18455aab851f4bcf1098cd3389498b9b3caf1964 Jakub Vrana committed Apr 21, 2010
View
54 dibi/Nette/DateTime53.php
@@ -0,0 +1,54 @@
+<?php
+
+/**
+ * Nette Framework
+ *
+ * @copyright Copyright (c) 2004, 2010 David Grudl
+ * @license http://nettephp.com/license Nette license
+ * @link http://nettephp.com
+ * @category Nette
+ * @package Nette
+ */
+
+// no namespace
+
+
+
+/**
+ * DateTime with serialization and timestamp support for PHP 5.2.
+ *
+ * @copyright Copyright (c) 2004, 2010 David Grudl
+ * @package Nette
+ */
+class DateTime53 extends DateTime
+{
+
+ public function __sleep()
+ {
+ $this->fix = array($this->format('Y-m-d H:i:s'), $this->getTimezone()->getName());
+ return array('fix');
+ }
+
+
+
+ public function __wakeup()
+ {
+ $this->__construct($this->fix[0], new DateTimeZone($this->fix[1]));
+ unset($this->fix);
+ }
+
+
+
+ public function getTimestamp()
+ {
+ return (int) $this->format('U');
+ }
+
+
+
+ public function setTimestamp($timestamp)
+ {
+ return $this->__construct(date('Y-m-d H:i:s', $timestamp), new DateTimeZone($this->getTimezone()->getName())); // getTimeZone() crashes in PHP 5.2.6
+ }
+
+}
View
44 dibi/Nette/IDebugPanel.php
@@ -0,0 +1,44 @@
+<?php
+
+/**
+ * Nette Framework
+ *
+ * @copyright Copyright (c) 2004, 2010 David Grudl
+ * @license http://nettephp.com/license Nette license
+ * @link http://nettephp.com
+ * @category Nette
+ * @package Nette
+ */
+
+/*namespace Nette;*/
+
+
+
+/**
+ * Custom output for Nette\Debug.
+ *
+ * @copyright Copyright (c) 2004, 2010 David Grudl
+ * @package Nette
+ */
+interface IDebugPanel
+{
+
+ /**
+ * Renders HTML code for custom tab.
+ * @return void
+ */
+ function getTab();
+
+ /**
+ * Renders HTML code for custom panel.
+ * @return void
+ */
+ function getPanel();
+
+ /**
+ * Returns panel ID.
+ * @return string
+ */
+ function getId();
+
+}
View
40 dibi/Nette/IDebuggable.php
@@ -1,40 +0,0 @@
-<?php
-
-/**
- * Nette Framework
- *
- * Copyright (c) 2004, 2009 David Grudl (http://davidgrudl.com)
- *
- * This source file is subject to the "Nette license" that is bundled
- * with this package in the file license.txt.
- *
- * For more information please see http://nettephp.com
- *
- * @copyright Copyright (c) 2004, 2009 David Grudl
- * @license http://nettephp.com/license Nette license
- * @link http://nettephp.com
- * @category Nette
- * @package Nette
- */
-
-/*namespace Nette;*/
-
-
-
-/**
- * Custom output for Nette\Debug.
- *
- * @author David Grudl
- * @copyright Copyright (c) 2004, 2009 David Grudl
- * @package Nette
- */
-interface IDebuggable
-{
-
- /**
- * Returns custom panels.
- * @return array
- */
- function getPanels();
-
-}
View
93 dibi/dibi.php
@@ -4,14 +4,14 @@
* dibi - tiny'n'smart database abstraction layer
* ----------------------------------------------
*
- * Copyright (c) 2005, 2009 David Grudl (http://davidgrudl.com)
+ * Copyright (c) 2005, 2010 David Grudl (http://davidgrudl.com)
*
* This source file is subject to the "dibi license" that is bundled
- * with this package in the file license.txt.
+ * with this package in the file license.txt, and/or GPL license.
*
* For more information please see http://dibiphp.com
*
- * @copyright Copyright (c) 2005, 2009 David Grudl
+ * @copyright Copyright (c) 2005, 2010 David Grudl
* @license http://dibiphp.com/license dibi license
* @link http://dibiphp.com
* @package dibi
@@ -21,8 +21,8 @@
/**
* Check PHP configuration.
*/
-if (version_compare(PHP_VERSION, '5.1.0', '<')) {
- throw new Exception('dibi needs PHP 5.1.0 or newer.');
+if (version_compare(PHP_VERSION, '5.2.0', '<')) {
+ throw new Exception('dibi needs PHP 5.2.0 or newer.');
}
@set_magic_quotes_runtime(FALSE); // intentionally @
@@ -33,33 +33,58 @@
* Compatibility with Nette
*/
if (!class_exists('NotImplementedException', FALSE)) {
+ /** @package exceptions */
class NotImplementedException extends LogicException {}
}
if (!class_exists('NotSupportedException', FALSE)) {
+ /** @package exceptions */
class NotSupportedException extends LogicException {}
}
if (!class_exists('MemberAccessException', FALSE)) {
+ /** @package exceptions */
class MemberAccessException extends LogicException {}
}
if (!class_exists('InvalidStateException', FALSE)) {
+ /** @package exceptions */
class InvalidStateException extends RuntimeException {}
}
if (!class_exists('IOException', FALSE)) {
+ /** @package exceptions */
class IOException extends RuntimeException {}
}
if (!class_exists('FileNotFoundException', FALSE)) {
+ /** @package exceptions */
class FileNotFoundException extends IOException {}
}
-if (!interface_exists(/*Nette\*/'IDebuggable', FALSE)) {
- require_once dirname(__FILE__) . '/Nette/IDebuggable.php';
+if (!interface_exists(/*Nette\*/'IDebugPanel', FALSE)) {
+ require_once dirname(__FILE__) . '/Nette/IDebugPanel.php';
}
+if (!class_exists('DateTime53', FALSE)) {
+ require_once dirname(__FILE__) . '/Nette/DateTime53.php';
+}
+
+
+
+/**
+ * @deprecated
+ */
+class DibiVariable extends DateTime53
+{
+ function __construct($val)
+ {
+ parent::__construct($val);
+ }
+}
+
+
+
// dibi libraries
require_once dirname(__FILE__) . '/libs/interfaces.php';
require_once dirname(__FILE__) . '/libs/DibiObject.php';
@@ -69,7 +94,6 @@ class FileNotFoundException extends IOException {}
require_once dirname(__FILE__) . '/libs/DibiResultIterator.php';
require_once dirname(__FILE__) . '/libs/DibiRow.php';
require_once dirname(__FILE__) . '/libs/DibiTranslator.php';
-require_once dirname(__FILE__) . '/libs/DibiVariable.php';
require_once dirname(__FILE__) . '/libs/DibiDataSource.php';
require_once dirname(__FILE__) . '/libs/DibiFluent.php';
require_once dirname(__FILE__) . '/libs/DibiDatabaseInfo.php';
@@ -85,8 +109,7 @@ class FileNotFoundException extends IOException {}
* This class is static container class for creating DB objects and
* store connections info.
*
- * @author David Grudl
- * @copyright Copyright (c) 2005, 2009 David Grudl
+ * @copyright Copyright (c) 2005, 2010 David Grudl
* @package dibi
*/
class dibi
@@ -121,15 +144,15 @@ class dibi
/**#@+
* dibi version
*/
- const VERSION = '1.2-dev';
+ const VERSION = '1.3-dev';
const REVISION = '$WCREV$ released on $WCDATE$';
/**#@-*/
/**#@+
* Configuration options
*/
- const RESULT_WITH_TABLES = 'resultWithTables'; // for MySQL
- const ROW_CLASS = 'rowClass';
+ const RESULT_DETECT_TYPES = 'resultDetectTypes';
+ const RESULT_DATE_TIME = 'resultDateTime';
const ASC = 'ASC', DESC = 'DESC';
/**#@-*/
@@ -597,36 +620,21 @@ public static function delete($table)
/**
- * Pseudotype for timestamp representation.
- * @param mixed datetime
- * @return DibiVariable
+ * @deprecated
*/
public static function datetime($time = NULL)
{
- if ($time === NULL) {
- $time = time(); // current time
-
- } elseif (is_numeric($time)) {
- $time = (int) $time; // timestamp
-
- } elseif (is_string($time)) {
- $time = class_exists('DateTime', FALSE) ? new DateTime($time) : strtotime($time); // DateTime is since PHP 5.2
- }
- return new DibiVariable($time, dibi::DATETIME);
+ return new DateTime53(is_numeric($time) ? date('Y-m-d H:i:s', $time) : $time);
}
/**
- * Pseudotype for date representation.
- * @param mixed date
- * @return DibiVariable
+ * @deprecated
*/
public static function date($date = NULL)
{
- $var = self::datetime($date);
- $var->modifier = dibi::DATE;
- return $var;
+ return new DateTime53(is_numeric($date) ? date('Y-m-d', $date) : $date);
}
@@ -724,7 +732,7 @@ public static function dump($sql = NULL, $return = FALSE)
$sql = wordwrap($sql, 100);
$sql = htmlSpecialChars($sql);
- $sql = preg_replace("#\n{2,}#", "\n", $sql);
+ $sql = preg_replace("#([ \t]*\r?\n){2,}#", "\n", $sql);
// syntax highlight
$sql = preg_replace_callback("#(/\\*.+?\\*/)|(\\*\\*.+?\\*\\*)|(?<=[\\s,(])($keywords1)(?=[\\s,)])|(?<=[\\s,(=])($keywords2)(?=[\\s,)=])#is", array('dibi', 'highlightCallback'), $sql);
@@ -756,23 +764,4 @@ private static function highlightCallback($matches)
return '<strong style="color:green">' . $matches[4] . '</strong>';
}
-
-
- /**
- * Returns brief descriptions.
- * @return string
- * @return array
- */
- public static function getColophon($sender = NULL)
- {
- $arr = array(
- 'Number of SQL queries: ' . dibi::$numOfQueries
- . (dibi::$totalTime === NULL ? '' : ', elapsed time: ' . sprintf('%0.3f', dibi::$totalTime * 1000) . ' ms'),
- );
- if ($sender === 'bluescreen') {
- $arr[] = 'dibi ' . dibi::VERSION . ' (revision ' . dibi::REVISION . ')';
- }
- return $arr;
- }
-
}
View
34 dibi/drivers/firebird.php
@@ -4,17 +4,10 @@
* dibi - tiny'n'smart database abstraction layer
* ----------------------------------------------
*
- * Copyright (c) 2005, 2009 David Grudl (http://davidgrudl.com)
- *
- * This source file is subject to the "dibi license" that is bundled
- * with this package in the file license.txt.
- *
- * For more information please see http://dibiphp.com
- *
- * @copyright Copyright (c) 2005, 2009 David Grudl
+ * @copyright Copyright (c) 2005, 2010 David Grudl
* @license http://dibiphp.com/license dibi license
* @link http://dibiphp.com
- * @package dibi
+ * @package dibi\drivers
*/
@@ -31,8 +24,8 @@
* - 'resource' - connection resource (optional)
*
* @author Tomáš Kraina, Roman Sklenář
- * @copyright Copyright (c) 2009
- * @package dibi
+ * @copyright Copyright (c) 2010
+ * @package dibi\drivers
*/
class DibiFirebirdDriver extends DibiObject implements IDibiDriver
{
@@ -163,7 +156,6 @@ public function getAffectedRows()
public function getInsertId($sequence)
{
return ibase_gen_id($sequence, 0, $this->connection);
- //throw new NotSupportedException('Firebird/InterBase does not support autoincrementing.');
}
@@ -228,6 +220,17 @@ public function rollback($savepoint = NULL)
/**
+ * Is in transaction?
+ * @return bool
+ */
+ public function inTransaction()
+ {
+ return $this->inTransaction;
+ }
+
+
+
+ /**
* Returns the connection resource.
* @return resource
*/
@@ -333,7 +336,7 @@ public function getRowCount()
public function fetch($assoc)
{
DibiDriverException::tryError();
- $result = $assoc ? ibase_fetch_assoc($this->resultSet) : ibase_fetch_row($this->resultSet); // intentionally @
+ $result = $assoc ? ibase_fetch_assoc($this->resultSet, IBASE_TEXT) : ibase_fetch_row($this->resultSet, IBASE_TEXT); // intentionally @
if (DibiDriverException::catchError($msg)) {
if (ibase_errcode() == self::ERROR_EXCEPTION_THROWN) {
@@ -800,8 +803,8 @@ public function getFunctions()
* Database procedure exception.
*
* @author Roman Sklenář
- * @copyright Copyright (c) 2009
- * @package dibi
+ * @copyright Copyright (c) 2010
+ * @package dibi\drivers
*/
class DibiProcedureException extends DibiException
{
@@ -822,6 +825,7 @@ public function __construct($message = NULL, $code = 0, $severity = NULL, $sql =
}
+
/**
* Gets the exception severity.
* @return string
View
29 dibi/drivers/mssql.php
@@ -4,17 +4,10 @@
* dibi - tiny'n'smart database abstraction layer
* ----------------------------------------------
*
- * Copyright (c) 2005, 2009 David Grudl (http://davidgrudl.com)
- *
- * This source file is subject to the "dibi license" that is bundled
- * with this package in the file license.txt.
- *
- * For more information please see http://dibiphp.com
- *
- * @copyright Copyright (c) 2005, 2009 David Grudl
+ * @copyright Copyright (c) 2005, 2010 David Grudl
* @license http://dibiphp.com/license dibi license
* @link http://dibiphp.com
- * @package dibi
+ * @package dibi\drivers
*/
@@ -30,9 +23,8 @@
* - 'lazy' - if TRUE, connection will be established only when required
* - 'resource' - connection resource (optional)
*
- * @author David Grudl
- * @copyright Copyright (c) 2005, 2009 David Grudl
- * @package dibi
+ * @copyright Copyright (c) 2005, 2010 David Grudl
+ * @package dibi\drivers
*/
class DibiMsSqlDriver extends DibiObject implements IDibiDriver
{
@@ -179,6 +171,17 @@ public function rollback($savepoint = NULL)
/**
+ * Is in transaction?
+ * @return bool
+ */
+ public function inTransaction()
+ {
+ throw new NotSupportedException('MSSQL driver does not support transaction testing.');
+ }
+
+
+
+ /**
* Returns the connection resource.
* @return mixed
*/
@@ -256,7 +259,7 @@ public function applyLimit(&$sql, $limit, $offset)
{
// offset support is missing
if ($limit >= 0) {
- $sql = 'SELECT TOP ' . (int) $limit . ' * FROM (' . $sql . ')';
+ $sql = 'SELECT TOP ' . (int) $limit . ' * FROM (' . $sql . ') t';
}
if ($offset) {
View
45 dibi/drivers/mssql2005.php
@@ -4,17 +4,10 @@
* dibi - tiny'n'smart database abstraction layer
* ----------------------------------------------
*
- * Copyright (c) 2005, 2009 David Grudl (http://davidgrudl.com)
- *
- * This source file is subject to the "dibi license" that is bundled
- * with this package in the file license.txt.
- *
- * For more information please see http://dibiphp.com
- *
- * @copyright Copyright (c) 2005, 2009 David Grudl
+ * @copyright Copyright (c) 2005, 2010 David Grudl
* @license http://dibiphp.com/license dibi license
* @link http://dibiphp.com
- * @package dibi
+ * @package dibi\drivers
*/
@@ -28,9 +21,8 @@
* - 'charset' - character encoding to set (default is UTF-8)
* - 'resource' - connection resource (optional)
*
- * @author David Grudl
- * @copyright Copyright (c) 2005, 2009 David Grudl
- * @package dibi
+ * @copyright Copyright (c) 2005, 2010 David Grudl
+ * @package dibi\drivers
*/
class DibiMsSql2005Driver extends DibiObject implements IDibiDriver
{
@@ -40,9 +32,6 @@ class DibiMsSql2005Driver extends DibiObject implements IDibiDriver
/** @var resource Resultset resource */
private $resultSet;
- /** @var string character encoding */
- private $charset;
-
/**
@@ -66,8 +55,10 @@ public function connect(array &$config)
{
if (isset($config['resource'])) {
$this->connection = $config['resource'];
+
} elseif (isset($config['options'])) {
- $this->connection = sqlsrv_connect($config['host'], $config['options']);
+ $this->connection = sqlsrv_connect($config['host'], (array) $config['options']);
+
} else {
$this->connection = sqlsrv_connect($config['host']);
}
@@ -76,8 +67,6 @@ public function connect(array &$config)
$info = sqlsrv_errors();
throw new DibiDriverException($info[0]['message'], $info[0]['code']);
}
-
- $this->charset = empty($config['charset']) ? 'UTF-8' : $config['charset'];
}
@@ -101,7 +90,6 @@ public function disconnect()
*/
public function query($sql)
{
- $sql = iconv($this->charset, 'UTF-16LE', $sql);
$this->resultSet = sqlsrv_query($this->connection, $sql);
if ($this->resultSet === FALSE) {
@@ -181,6 +169,17 @@ public function rollback($savepoint = NULL)
/**
+ * Is in transaction?
+ * @return bool
+ */
+ public function inTransaction()
+ {
+ throw new NotSupportedException('MSSQL 2005 driver does not support transaction testing.');
+ }
+
+
+
+ /**
* Returns the connection resource.
* @return mixed
*/
@@ -291,13 +290,7 @@ public function getRowCount()
*/
public function fetch($assoc)
{
- $row = sqlsrv_fetch_array($this->resultSet, $assoc ? SQLSRV_FETCH_ASSOC : SQLSRV_FETCH_NUMERIC);
- foreach ($row as $k => $v) {
- if (is_string($v)) {
- $row[$k] = iconv('UTF-16LE', $this->charset, $v);
- }
- }
- return $row;
+ return sqlsrv_fetch_array($this->resultSet, $assoc ? SQLSRV_FETCH_ASSOC : SQLSRV_FETCH_NUMERIC);
}
View
41 dibi/drivers/mysql.php
@@ -4,17 +4,10 @@
* dibi - tiny'n'smart database abstraction layer
* ----------------------------------------------
*
- * Copyright (c) 2005, 2009 David Grudl (http://davidgrudl.com)
- *
- * This source file is subject to the "dibi license" that is bundled
- * with this package in the file license.txt.
- *
- * For more information please see http://dibiphp.com
- *
- * @copyright Copyright (c) 2005, 2009 David Grudl
+ * @copyright Copyright (c) 2005, 2010 David Grudl
* @license http://dibiphp.com/license dibi license
* @link http://dibiphp.com
- * @package dibi
+ * @package dibi\drivers
*/
@@ -36,9 +29,8 @@
* - 'lazy' - if TRUE, connection will be established only when required
* - 'resource' - connection resource (optional)
*
- * @author David Grudl
- * @copyright Copyright (c) 2005, 2009 David Grudl
- * @package dibi
+ * @copyright Copyright (c) 2005, 2010 David Grudl
+ * @package dibi\drivers
*/
class DibiMySqlDriver extends DibiObject implements IDibiDriver
{
@@ -120,10 +112,7 @@ public function connect(array &$config)
$ok = @mysql_set_charset($config['charset'], $this->connection); // intentionally @
}
if (!$ok) {
- $ok = @mysql_query("SET NAMES '$config[charset]'", $this->connection); // intentionally @
- if (!$ok) {
- throw new DibiDriverException(mysql_error($this->connection), mysql_errno($this->connection));
- }
+ $this->query("SET NAMES '$config[charset]'");
}
}
@@ -134,11 +123,11 @@ public function connect(array &$config)
}
if (isset($config['sqlmode'])) {
- if (!@mysql_query("SET sql_mode='$config[sqlmode]'", $this->connection)) { // intentionally @
- throw new DibiDriverException(mysql_error($this->connection), mysql_errno($this->connection));
- }
+ $this->query("SET sql_mode='$config[sqlmode]'");
}
+ $this->query("SET time_zone='" . date('P') . "'");
+
$this->buffered = empty($config['unbuffered']);
}
@@ -261,6 +250,17 @@ public function rollback($savepoint = NULL)
/**
+ * Is in transaction?
+ * @return bool
+ */
+ public function inTransaction()
+ {
+ return (bool) mysql_fetch_field(mysql_query('SELECT @@autocommit', $this->connection));
+ }
+
+
+
+ /**
* Returns the connection resource.
* @return mixed
*/
@@ -472,7 +472,7 @@ public function getTables()
*/
public function getColumns($table)
{
- $this->query("SHOW COLUMNS FROM `$table`");
+ $this->query("SHOW FULL COLUMNS FROM `$table`");
$res = array();
while ($row = $this->fetch(TRUE)) {
$type = explode('(', $row['Type']);
@@ -484,6 +484,7 @@ public function getColumns($table)
'nullable' => $row['Null'] === 'YES',
'default' => $row['Default'],
'autoincrement' => $row['Extra'] === 'auto_increment',
+ 'vendor' => $row,
);
}
$this->free();
View
41 dibi/drivers/mysqli.php
@@ -4,17 +4,10 @@
* dibi - tiny'n'smart database abstraction layer
* ----------------------------------------------
*
- * Copyright (c) 2005, 2009 David Grudl (http://davidgrudl.com)
- *
- * This source file is subject to the "dibi license" that is bundled
- * with this package in the file license.txt.
- *
- * For more information please see http://dibiphp.com
- *
- * @copyright Copyright (c) 2005, 2009 David Grudl
+ * @copyright Copyright (c) 2005, 2010 David Grudl
* @license http://dibiphp.com/license dibi license
* @link http://dibiphp.com
- * @package dibi
+ * @package dibi\drivers
*/
@@ -36,9 +29,8 @@
* - 'lazy' - if TRUE, connection will be established only when required
* - 'resource' - connection resource (optional)
*
- * @author David Grudl
- * @copyright Copyright (c) 2005, 2009 David Grudl
- * @package dibi
+ * @copyright Copyright (c) 2005, 2010 David Grudl
+ * @package dibi\drivers
*/
class DibiMySqliDriver extends DibiObject implements IDibiDriver
{
@@ -114,19 +106,16 @@ public function connect(array &$config)
$ok = @mysqli_set_charset($this->connection, $config['charset']); // intentionally @
}
if (!$ok) {
- $ok = @mysqli_query($this->connection, "SET NAMES '$config[charset]'"); // intentionally @
- if (!$ok) {
- throw new DibiDriverException(mysqli_error($this->connection), mysqli_errno($this->connection));
- }
+ $this->query("SET NAMES '$config[charset]'");
}
}
if (isset($config['sqlmode'])) {
- if (!@mysqli_query($this->connection, "SET sql_mode='$config[sqlmode]'")) { // intentionally @
- throw new DibiDriverException(mysqli_error($this->connection), mysqli_errno($this->connection));
- }
+ $this->query("SET sql_mode='$config[sqlmode]'");
}
+ $this->query("SET time_zone='" . date('P') . "'");
+
$this->buffered = empty($config['unbuffered']);
}
@@ -245,6 +234,17 @@ public function rollback($savepoint = NULL)
/**
+ * Is in transaction?
+ * @return bool
+ */
+ public function inTransaction()
+ {
+ return (bool) mysqli_fetch_field_direct(mysqli_query($this->connection, 'SELECT @@autocommit'), 0);
+ }
+
+
+
+ /**
* Returns the connection resource.
* @return mysqli
*/
@@ -475,7 +475,7 @@ public function getColumns($table)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = $table AND TABLE_SCHEMA = DATABASE()
");*/
- $this->query("SHOW COLUMNS FROM `$table`");
+ $this->query("SHOW FULL COLUMNS FROM `$table`");
$res = array();
while ($row = $this->fetch(TRUE)) {
$type = explode('(', $row['Type']);
@@ -487,6 +487,7 @@ public function getColumns($table)
'nullable' => $row['Null'] === 'YES',
'default' => $row['Default'],
'autoincrement' => $row['Extra'] === 'auto_increment',
+ 'vendor' => $row,
);
}
$this->free();
View
27 dibi/drivers/odbc.php
@@ -4,17 +4,10 @@
* dibi - tiny'n'smart database abstraction layer
* ----------------------------------------------
*
- * Copyright (c) 2005, 2009 David Grudl (http://davidgrudl.com)
- *
- * This source file is subject to the "dibi license" that is bundled
- * with this package in the file license.txt.
- *
- * For more information please see http://dibiphp.com
- *
- * @copyright Copyright (c) 2005, 2009 David Grudl
+ * @copyright Copyright (c) 2005, 2010 David Grudl
* @license http://dibiphp.com/license dibi license
* @link http://dibiphp.com
- * @package dibi
+ * @package dibi\drivers
*/
@@ -29,9 +22,8 @@
* - 'lazy' - if TRUE, connection will be established only when required
* - 'resource' - connection resource (optional)
*
- * @author David Grudl
- * @copyright Copyright (c) 2005, 2009 David Grudl
- * @package dibi
+ * @copyright Copyright (c) 2005, 2010 David Grudl
+ * @package dibi\drivers
*/
class DibiOdbcDriver extends DibiObject implements IDibiDriver
{
@@ -187,6 +179,17 @@ public function rollback($savepoint = NULL)
/**
+ * Is in transaction?
+ * @return bool
+ */
+ public function inTransaction()
+ {
+ return (bool) odbc_autocommit($this->connection);
+ }
+
+
+
+ /**
* Returns the connection resource.
* @return mixed
*/
View
32 dibi/drivers/oracle.php
@@ -4,17 +4,10 @@
* dibi - tiny'n'smart database abstraction layer
* ----------------------------------------------
*
- * Copyright (c) 2005, 2009 David Grudl (http://davidgrudl.com)
- *
- * This source file is subject to the "dibi license" that is bundled
- * with this package in the file license.txt.
- *
- * For more information please see http://dibiphp.com
- *
- * @copyright Copyright (c) 2005, 2009 David Grudl
+ * @copyright Copyright (c) 2005, 2010 David Grudl
* @license http://dibiphp.com/license dibi license
* @link http://dibiphp.com
- * @package dibi
+ * @package dibi\drivers
*/
@@ -31,9 +24,8 @@
* - 'charset' - character encoding to set
* - 'resource' - connection resource (optional)
*
- * @author David Grudl
- * @copyright Copyright (c) 2005, 2009 David Grudl
- * @package dibi
+ * @copyright Copyright (c) 2005, 2010 David Grudl
+ * @package dibi\drivers
*/
class DibiOracleDriver extends DibiObject implements IDibiDriver
{
@@ -107,7 +99,6 @@ public function disconnect()
*/
public function query($sql)
{
-
$this->resultSet = oci_parse($this->connection, $sql);
if ($this->resultSet) {
oci_execute($this->resultSet, $this->autocommit ? OCI_COMMIT_ON_SUCCESS : OCI_DEFAULT);
@@ -142,7 +133,9 @@ public function getAffectedRows()
*/
public function getInsertId($sequence)
{
- throw new NotSupportedException('Oracle does not support autoincrementing.');
+ $this->query("SELECT $sequence.CURRVAL AS ID FROM DUAL");
+ $row = $this->fetch(TRUE);
+ return isset($row['ID']) ? (int) $row['ID'] : FALSE;
}
@@ -194,6 +187,17 @@ public function rollback($savepoint = NULL)
/**
+ * Is in transaction?
+ * @return bool
+ */
+ public function inTransaction()
+ {
+ throw new NotSupportedException('Oracle driver does not support transaction testing.');
+ }
+
+
+
+ /**
* Returns the connection resource.
* @return mixed
*/
View
29 dibi/drivers/pdo.php
@@ -4,17 +4,10 @@
* dibi - tiny'n'smart database abstraction layer
* ----------------------------------------------
*
- * Copyright (c) 2005, 2009 David Grudl (http://davidgrudl.com)
- *
- * This source file is subject to the "dibi license" that is bundled
- * with this package in the file license.txt.
- *
- * For more information please see http://dibiphp.com
- *
- * @copyright Copyright (c) 2005, 2009 David Grudl
+ * @copyright Copyright (c) 2005, 2010 David Grudl
* @license http://dibiphp.com/license dibi license
* @link http://dibiphp.com
- * @package dibi
+ * @package dibi\drivers
*/
@@ -29,9 +22,8 @@
* - 'resource' - PDO object (optional)
* - 'lazy' - if TRUE, connection will be established only when required
*
- * @author David Grudl
- * @copyright Copyright (c) 2005, 2009 David Grudl
- * @package dibi
+ * @copyright Copyright (c) 2005, 2010 David Grudl
+ * @package dibi\drivers
*/
class DibiPdoDriver extends DibiObject implements IDibiDriver
{
@@ -206,6 +198,17 @@ public function rollback($savepoint = NULL)
/**
+ * Is in transaction?
+ * @return bool
+ */
+ public function inTransaction()
+ {
+ return (bool) $this->connection->getAttribute(PDO::ATTR_AUTOCOMMIT);
+ }
+
+
+
+ /**
* Returns the connection resource.
* @return PDO
*/
@@ -356,7 +359,7 @@ public function applyLimit(&$sql, $limit, $offset)
*/
public function getRowCount()
{
- throw new NotSupportedException('Row count is not available for unbuffered queries.');
+ return $this->resultSet->rowCount();
}
View
29 dibi/drivers/postgre.php
@@ -4,17 +4,10 @@
* dibi - tiny'n'smart database abstraction layer
* ----------------------------------------------
*
- * Copyright (c) 2005, 2009 David Grudl (http://davidgrudl.com)
- *
- * This source file is subject to the "dibi license" that is bundled
- * with this package in the file license.txt.
- *
- * For more information please see http://dibiphp.com
- *
- * @copyright Copyright (c) 2005, 2009 David Grudl
+ * @copyright Copyright (c) 2005, 2010 David Grudl
* @license http://dibiphp.com/license dibi license
* @link http://dibiphp.com
- * @package dibi
+ * @package dibi\drivers
*/
@@ -30,9 +23,8 @@
* - 'lazy' - if TRUE, connection will be established only when required
* - 'resource' - connection resource (optional)
*
- * @author David Grudl
- * @copyright Copyright (c) 2005, 2009 David Grudl
- * @package dibi
+ * @copyright Copyright (c) 2005, 2010 David Grudl
+ * @package dibi\drivers
*/
class DibiPostgreDriver extends DibiObject implements IDibiDriver
{
@@ -216,6 +208,17 @@ public function rollback($savepoint = NULL)
/**
+ * Is in transaction?
+ * @return bool
+ */
+ public function inTransaction()
+ {
+ throw new NotSupportedException('PostgreSQL driver does not support transaction testing.');
+ }
+
+
+
+ /**
* Returns the connection resource.
* @return mixed
*/
@@ -423,7 +426,7 @@ public function getTables()
");
$res = pg_fetch_all($this->resultSet);
$this->free();
- return $res;
+ return $res ? $res : array();
}
View
30 dibi/drivers/sqlite.php
@@ -4,17 +4,10 @@
* dibi - tiny'n'smart database abstraction layer
* ----------------------------------------------
*
- * Copyright (c) 2005, 2009 David Grudl (http://davidgrudl.com)
- *
- * This source file is subject to the "dibi license" that is bundled
- * with this package in the file license.txt.
- *
- * For more information please see http://dibiphp.com
- *
- * @copyright Copyright (c) 2005, 2009 David Grudl
+ * @copyright Copyright (c) 2005, 2010 David Grudl
* @license http://dibiphp.com/license dibi license
* @link http://dibiphp.com
- * @package dibi
+ * @package dibi\drivers
*/
@@ -32,9 +25,8 @@
* - 'charset' - character encoding to set (default is UTF-8)
* - 'resource' - connection resource (optional)
*
- * @author David Grudl
- * @copyright Copyright (c) 2005, 2009 David Grudl
- * @package dibi
+ * @copyright Copyright (c) 2005, 2010 David Grudl
+ * @package dibi\drivers
*/
class DibiSqliteDriver extends DibiObject implements IDibiDriver
{
@@ -202,6 +194,17 @@ public function rollback($savepoint = NULL)
/**
+ * Is in transaction?
+ * @return bool
+ */
+ public function inTransaction()
+ {
+ throw new NotSupportedException('SQLite driver does not support transaction testing.');
+ }
+
+
+
+ /**
* Returns the connection resource.
* @return mixed
*/
@@ -230,9 +233,6 @@ public function escape($value, $type)
case dibi::BINARY:
return "'" . sqlite_escape_string($value) . "'";
- /*case dibi::BINARY: // SQLite 3
- return "X'" . bin2hex((string) $value) . "'";*/
-
case dibi::IDENTIFIER:
return '[' . str_replace('.', '].[', strtr($value, '[]', ' ')) . ']';
View
542 dibi/drivers/sqlite3.php
@@ -0,0 +1,542 @@
+<?php
+
+/**
+ * dibi - tiny'n'smart database abstraction layer
+ * ----------------------------------------------
+ *
+ * @copyright Copyright (c) 2005, 2010 David Grudl
+ * @license http://dibiphp.com/license dibi license
+ * @link http://dibiphp.com
+ * @package dibi\drivers
+ */
+
+
+/**
+ * The dibi driver for SQLite3 database.
+ *
+ * Connection options:
+ * - 'database' (or 'file') - the filename of the SQLite3 database
+ * - 'lazy' - if TRUE, connection will be established only when required
+ * - 'formatDate' - how to format date in SQL (@see date)
+ * - 'formatDateTime' - how to format datetime in SQL (@see date)
+ * - 'dbcharset' - database character encoding (will be converted to 'charset')
+ * - 'charset' - character encoding to set (default is UTF-8)
+ * - 'resource' - connection resource (optional)
+ *
+ * @copyright Copyright (c) 2005, 2010 David Grudl
+ * @package dibi\drivers
+ */
+class DibiSqlite3Driver extends DibiObject implements IDibiDriver
+{
+ /** @var SQLite3 Connection resource */
+ private $connection;
+
+ /** @var SQLite3Result Resultset resource */
+ private $resultSet;
+
+ /** @var string Date and datetime format */
+ private $fmtDate, $fmtDateTime;
+
+ /** @var string character encoding */
+ private $dbcharset, $charset;
+
+
+
+ /**
+ * @throws DibiException
+ */
+ public function __construct()
+ {
+ if (!extension_loaded('sqlite3')) {
+ throw new DibiDriverException("PHP extension 'sqlite3' is not loaded.");
+ }
+ }
+
+
+
+ /**
+ * Connects to a database.
+ * @return void
+ * @throws DibiException
+ */
+ public function connect(array &$config)
+ {
+ DibiConnection::alias($config, 'database', 'file');
+ $this->fmtDate = isset($config['formatDate']) ? $config['formatDate'] : 'U';
+ $this->fmtDateTime = isset($config['formatDateTime']) ? $config['formatDateTime'] : 'U';
+
+ if (isset($config['resource']) && $config['resource'] instanceof SQLite3) {
+ $this->connection = $config['resource'];
+ } else try {
+ $this->connection = new SQLite3($config['database']);
+
+ } catch (Exception $e) {
+ throw new DibiDriverException($e->getMessage(), $e->getCode());
+ }
+
+ $this->dbcharset = empty($config['dbcharset']) ? 'UTF-8' : $config['dbcharset'];
+ $this->charset = empty($config['charset']) ? 'UTF-8' : $config['charset'];
+ 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");
+ }
+ }
+
+
+
+ /**
+ * Disconnects from a database.
+ * @return void
+ */
+ public function disconnect()
+ {
+ $this->connection->close();
+ }
+
+
+
+ /**
+ * Executes the SQL query.
+ * @param string SQL statement.
+ * @return IDibiDriver|NULL
+ * @throws DibiDriverException
+ */
+ public function query($sql)
+ {
+ if ($this->dbcharset !== NULL) {
+ $sql = iconv($this->charset, $this->dbcharset . '//IGNORE', $sql);
+ }
+
+ $this->resultSet = @$this->connection->query($sql); // intentionally @
+ if ($this->connection->lastErrorCode()) {
+ throw new DibiDriverException($this->connection->lastErrorMsg(), $this->connection->lastErrorCode(), $sql);
+ }
+
+ return $this->resultSet instanceof SQLite3Result ? clone $this : NULL;
+ }
+
+
+
+ /**
+ * Gets the number of affected rows by the last INSERT, UPDATE or DELETE query.
+ * @return int|FALSE number of rows or FALSE on error
+ */
+ public function getAffectedRows()
+ {
+ return $this->connection->changes();
+ }
+
+
+
+ /**
+ * Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query.
+ * @return int|FALSE int on success or FALSE on failure
+ */
+ public function getInsertId($sequence)
+ {
+ return $this->connection->lastInsertRowID();
+ }
+
+
+
+ /**
+ * Begins a transaction (if supported).
+ * @param string optional savepoint name
+ * @return void
+ * @throws DibiDriverException
+ */
+ public function begin($savepoint = NULL)
+ {
+ $this->query($savepoint ? "SAVEPOINT $savepoint" : 'BEGIN');
+ }
+
+
+
+ /**
+ * Commits statements in a transaction.
+ * @param string optional savepoint name
+ * @return void
+ * @throws DibiDriverException
+ */
+ public function commit($savepoint = NULL)
+ {
+ $this->query($savepoint ? "RELEASE SAVEPOINT $savepoint" : 'COMMIT');
+ }
+
+
+
+ /**
+ * Rollback changes in a transaction.
+ * @param string optional savepoint name
+ * @return void
+ * @throws DibiDriverException
+ */
+ public function rollback($savepoint = NULL)
+ {
+ $this->query($savepoint ? "ROLLBACK TO SAVEPOINT $savepoint" : 'ROLLBACK');
+ }
+
+
+
+ /**
+ * Is in transaction?
+ * @return bool
+ */
+ public function inTransaction()
+ {
+ throw new NotSupportedException('SQLite3 driver does not support transaction testing.');
+ }
+
+
+
+ /**
+ * Returns the connection resource.
+ * @return mixed
+ */
+ public function getResource()
+ {
+ return $this->connection;
+ }
+
+
+
+ /********************* SQL ****************d*g**/
+
+
+
+ /**
+ * Encodes data for use in a SQL statement.
+ * @param mixed value
+ * @param string type (dibi::TEXT, dibi::BOOL, ...)
+ * @return string encoded value
+ * @throws InvalidArgumentException
+ */
+ public function escape($value, $type)
+ {
+ switch ($type) {
+ case dibi::TEXT:
+ return "'" . $this->connection->escapeString($value) . "'";
+
+ case dibi::BINARY:
+ return "X'" . bin2hex((string) $value) . "'";
+
+ case dibi::IDENTIFIER:
+ return '[' . str_replace('.', '].[', strtr($value, '[]', ' ')) . ']';
+
+ case dibi::BOOL:
+ return $value ? 1 : 0;
+
+ case dibi::DATE:
+ return $value instanceof DateTime ? $value->format($this->fmtDate) : date($this->fmtDate, $value);
+
+ case dibi::DATETIME:
+ return $value instanceof DateTime ? $value->format($this->fmtDateTime) : date($this->fmtDateTime, $value);
+
+ default:
+ throw new InvalidArgumentException('Unsupported type.');
+ }
+ }
+
+
+
+ /**
+ * Decodes data from result set.
+ * @param string value
+ * @param string type (dibi::BINARY)
+ * @return string decoded value
+ * @throws InvalidArgumentException
+ */
+ public function unescape($value, $type)
+ {
+ if ($type === dibi::BINARY) {
+ return $value;
+ }
+ throw new InvalidArgumentException('Unsupported type.');
+ }
+
+
+
+ /**
+ * Injects LIMIT/OFFSET to the SQL query.
+ * @param string &$sql The SQL query that will be modified.
+ * @param int $limit
+ * @param int $offset
+ * @return void
+ */
+ public function applyLimit(&$sql, $limit, $offset)
+ {
+ if ($limit < 0 && $offset < 1) return;
+ $sql .= ' LIMIT ' . $limit . ($offset > 0 ? ' OFFSET ' . (int) $offset : '');
+ }
+
+
+
+ /********************* result set ****************d*g**/
+
+
+
+ /**
+ * Returns the number of rows in a result set.
+ * @return int
+ * @throws NotSupportedException
+ */
+ public function getRowCount()
+ {
+ throw new NotSupportedException('Row count is not available for unbuffered queries.');
+ }
+
+
+
+ /**
+ * Fetches the row at current position and moves the internal cursor to the next position.
+ * @param bool TRUE for associative array, FALSE for numeric
+ * @return array array on success, nonarray if no next record
+ * @internal
+ */
+ public function fetch($assoc)
+ {
+ $row = $this->resultSet->fetchArray($assoc ? SQLITE3_ASSOC : SQLITE3_NUM);
+ $charset = $this->charset === NULL ? NULL : $this->charset . '//TRANSLIT';
+ if ($row && ($assoc || $charset)) {
+ $tmp = array();
+ foreach ($row as $k => $v) {
+ if ($charset !== NULL && is_string($v)) {
+ $v = iconv($this->dbcharset, $charset, $v);
+ }
+ $tmp[str_replace(array('[', ']'), '', $k)] = $v;
+ }
+ return $tmp;
+ }
+ return $row;
+ }
+
+
+
+ /**
+ * 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 NotSupportedException
+ */
+ public function seek($row)
+ {
+ throw new NotSupportedException('Cannot seek an unbuffered result set.');
+ }
+
+
+
+ /**
+ * Frees the resources allocated for this result set.
+ * @return void
+ */
+ public function free()
+ {
+ $this->resultSet = NULL;
+ }
+
+
+
+ /**
+ * Returns metadata for all columns in a result set.
+ * @return array
+ */
+ public function getColumnsMeta()
+ {
+ $count = $this->resultSet->numColumns();
+ $res = array();
+ static $types = array(SQLITE3_INTEGER => 'int', SQLITE3_FLOAT => 'float', SQLITE3_TEXT => 'text', SQLITE3_BLOB => 'blob', SQLITE3_NULL => 'null');
+ for ($i = 0; $i < $count; $i++) {
+ $res[] = array(
+ 'name' => $this->resultSet->columnName($i),
+ 'table' => NULL,
+ 'fullname' => $this->resultSet->columnName($i),
+ 'nativetype' => $types[$this->resultSet->columnType($i)],
+ );
+ }
+ return $res;
+ }
+
+
+
+ /**
+ * Returns the result set resource.
+ * @return mixed
+ */
+ public function getResultResource()
+ {
+ return $this->resultSet;
+ }
+
+
+
+ /********************* reflection ****************d*g**/
+
+
+
+ /**
+ * Returns list of tables.
+ * @return array
+ */
+ public function getTables()
+ {
+ $this->query("
+ SELECT name, type = 'view' as view FROM sqlite_master WHERE type IN ('table', 'view')
+ UNION ALL
+ SELECT name, type = 'view' as view FROM sqlite_temp_master WHERE type IN ('table', 'view')
+ ORDER BY name
+ ");
+ $res = array();
+ while ($row = $this->fetch(TRUE)) {
+ $res[] = $row;
+ }
+ $this->free();
+ return $res;
+ }
+
+
+
+ /**
+ * Returns metadata for all columns in a table.
+ * @param string
+ * @return array
+ */
+ public function getColumns($table)
+ {
+ $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;
+ }
+
+
+
+ /**
+ * Returns metadata for all indexes in a table.
+ * @param string
+ * @return array
+ */
+ public function getIndexes($table)
+ {
+ $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);
+ }
+
+
+
+ /**
+ * Returns metadata for all foreign keys in a table.
+ * @param string
+ * @return array
+ */
+ public function getForeignKeys($table)
+ {
+ $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);
+ }
+
+
+
+ /********************* user defined functions ****************d*g**/
+
+
+
+ /**
+ * Registers an user defined function for use in SQL statements.
+ * @param string function name
+ * @param mixed callback
+ * @param int num of arguments
+ * @return void
+ */
+ public function registerFunction($name, $callback, $numArgs = -1)
+ {
+ $this->connection->createFunction($name, $callback, $numArgs);
+ }
+
+
+
+ /**
+ * Registers an aggregating user defined function for use in SQL statements.
+ * @param string function name
+ * @param mixed callback called for each row of the result set
+ * @param mixed callback called to aggregate the "stepped" data from each row
+ * @param int num of arguments
+ * @return void
+ */
+ public function registerAggregateFunction($name, $rowCallback, $agrCallback, $numArgs = -1)
+ {
+ $this->connection->createAggregate($name, $rowCallback, $agrCallback, $numArgs);
+ }
+
+}
View
54 dibi/libs/DibiConnection.php
@@ -4,14 +4,7 @@
* dibi - tiny'n'smart database abstraction layer
* ----------------------------------------------
*
- * Copyright (c) 2005, 2009 David Grudl (http://davidgrudl.com)
- *
- * This source file is subject to the "dibi license" that is bundled
- * with this package in the file license.txt.
- *
- * For more information please see http://dibiphp.com
- *
- * @copyright Copyright (c) 2005, 2009 David Grudl
+ * @copyright Copyright (c) 2005, 2010 David Grudl
* @license http://dibiphp.com/license dibi license
* @link http://dibiphp.com
* @package dibi
@@ -22,8 +15,7 @@
/**
* dibi connection.
*
- * @author David Grudl
- * @copyright Copyright (c) 2005, 2009 David Grudl
+ * @copyright Copyright (c) 2005, 2010 David Grudl
* @package dibi
*/
class DibiConnection extends DibiObject
@@ -40,14 +32,11 @@ class DibiConnection extends DibiObject
/** @var bool Is connected? */
private $connected = FALSE;
- /** @var bool Is in transaction? */
- private $inTxn = FALSE;
/** @var array Commands in current transaction */
private $transaction = array();
-
/**
* Creates object and (optionally) connects to a database.
* @param array|string|ArrayObject connection parameters
@@ -56,10 +45,6 @@ class DibiConnection extends DibiObject
*/
public function __construct($config, $name = NULL)
{
- if (class_exists(/*Nette\*/'Debug', FALSE)) {
- /*Nette\*/Debug::addColophon(array('dibi', 'getColophon'));
- }
-
// DSN string
if (is_string($config)) {
parse_str($config, $config);
@@ -156,9 +141,6 @@ public function __destruct()
final public function disconnect()
{
if ($this->connected) {
- if ($this->inTxn) {
- $this->rollback();
- }
$this->driver->disconnect();
$this->connected = FALSE;
}
@@ -335,22 +317,14 @@ public static function alias(&$config, $key, $alias=NULL)
}
$ticket = $this->profiler->before($this, $event, $sql);
}
- // TODO: move to profiler?
- dibi::$numOfQueries++;
- dibi::$sql = $sql;
- dibi::$elapsedTime = FALSE;
- $time = -microtime(TRUE);
+ dibi::$sql = $sql;
if ($res = $this->driver->query($sql)) { // intentionally =
$res = new DibiResult($res, $this->config);
} else {
$res = $this->driver->getAffectedRows();
}
- $time += microtime(TRUE);
- dibi::$elapsedTime = $time;
- dibi::$totalTime += $time;
-
if (isset($ticket)) {
$this->profiler->after($ticket, $res);
}
@@ -472,18 +446,10 @@ public function insertId($sequence = NULL)
public function begin($savepoint = NULL)
{
$this->connect();
- if (!$savepoint && $this->inTxn) {
- throw new DibiException('There is already an active transaction.');
- }
if ($this->profiler !== NULL) {
$ticket = $this->profiler->before($this, IDibiProfiler::BEGIN, $savepoint);
}
- if ($savepoint && !$this->inTxn) {
- $this->driver->begin();
- }
$this->driver->begin($savepoint);
-
- $this->inTxn = TRUE;
if (isset($ticket)) {
$this->profiler->after($ticket);
}
@@ -499,14 +465,10 @@ public function begin($savepoint = NULL)
*/
public function commit($savepoint = NULL)
{
- if (!$this->inTxn) {
- throw new DibiException('There is no active transaction.');
- }
if ($this->profiler !== NULL) {
$ticket = $this->profiler->before($this, IDibiProfiler::COMMIT, $savepoint);
}
$this->driver->commit($savepoint);
- $this->inTxn = (bool) $savepoint;
if (isset($ticket)) {
$this->profiler->after($ticket);
}
@@ -522,14 +484,10 @@ public function commit($savepoint = NULL)
*/
public function rollback($savepoint = NULL)
{
- if (!$this->inTxn) {
- throw new DibiException('There is no active transaction.');
- }
if ($this->profiler !== NULL) {
$ticket = $this->profiler->before($this, IDibiProfiler::ROLLBACK, $savepoint);
}
$this->driver->rollback($savepoint);
- $this->inTxn = (bool) $savepoint;
if (isset($ticket)) {
$this->profiler->after($ticket);
}
@@ -544,7 +502,8 @@ public function rollback($savepoint = NULL)
*/
public function inTransaction()
{
- return $this->inTxn;
+ $this->connect();
+ return $this->driver->inTransaction();
}
@@ -686,11 +645,12 @@ public function delete($table)
/**
* @param IDibiProfiler
- * @return void
+ * @return DibiConnection provides a fluent interface
*/
public function setProfiler(IDibiProfiler $profiler = NULL)
{
$this->profiler = $profiler;
+ return $this;
}
View
16 dibi/libs/DibiDataSource.php
@@ -4,14 +4,7 @@
* dibi - tiny'n'smart database abstraction layer
* ----------------------------------------------
*
- * Copyright (c) 2005, 2009 David Grudl (http://davidgrudl.com)
- *
- * This source file is subject to the "dibi license" that is bundled
- * with this package in the file license.txt.
- *
- * For more information please see http://dibiphp.com
- *
- * @copyright Copyright (c) 2005, 2009 David Grudl
+ * @copyright Copyright (c) 2005, 2010 David Grudl
* @license http://dibiphp.com/license dibi license
* @link http://dibiphp.com
* @package dibi
@@ -22,8 +15,7 @@
/**
* Default implementation of IDataSource for dibi.
*
- * @author David Grudl
- * @copyright Copyright (c) 2005, 2009 David Grudl
+ * @copyright Copyright (c) 2005, 2010 David Grudl
* @package dibi
*/
class DibiDataSource extends DibiObject implements IDataSource
@@ -66,7 +58,7 @@ class DibiDataSource extends DibiObject implements IDataSource
*/
public function __construct($sql, DibiConnection $connection)
{
- if (strpos($sql, ' ') === FALSE) {
+ if (strpbrk($sql, " \t\r\n") === FALSE) {
$this->sql = $connection->getDriver()->escape($sql, dibi::IDENTIFIER); // table name
} else {
$this->sql = '(' . $sql . ') t'; // SQL command
@@ -287,7 +279,7 @@ public function toDataSource()
* Returns SQL query.
* @return string
*/
- final public function __toString()
+ public function __toString()
{
return $this->connection->sql('
SELECT %n', (empty($this->cols) ? '*' : $this->cols), '
View
143 dibi/libs/DibiDatabaseInfo.php
@@ -4,14 +4,7 @@
* dibi - tiny'n'smart database abstraction layer
* ----------------------------------------------
*
- * Copyright (c) 2005, 2009 David Grudl (http://davidgrudl.com)
- *
- * This source file is subject to the "dibi license" that is bundled
- * with this package in the file license.txt.
- *
- * For more information please see http://dibiphp.com
- *
- * @copyright Copyright (c) 2005, 2009 David Grudl
+ * @copyright Copyright (c) 2005, 2010 David Grudl
* @license http://dibiphp.com/license dibi license
* @link http://dibiphp.com
* @package dibi
@@ -22,8 +15,7 @@
/**
* Reflection metadata class for a database.
*
- * @author David Grudl
- * @copyright Copyright (c) 2005, 2009 David Grudl
+ * @copyright Copyright (c) 2005, 2010 David Grudl
* @package dibi
*/
class DibiDatabaseInfo extends DibiObject
@@ -134,8 +126,7 @@ protected function init()
/**
* Reflection metadata class for a database table.
*
- * @author David Grudl
- * @copyright Copyright (c) 2005, 2009 David Grudl
+ * @copyright Copyright (c) 2005, 2010 David Grudl
* @package dibi
*/
class DibiTableInfo extends DibiObject
@@ -332,10 +323,110 @@ protected function initForeignKeys()
/**
- * Reflection metadata class for a table column.
+ * Reflection metadata class for a result set.
*
- * @author David Grudl
- * @copyright Copyright (c) 2005, 2009 David Grudl
+ * @copyright Copyright (c) 2005, 2010 David Grudl
+ * @package dibi
+ */
+class DibiResultInfo extends DibiObject
+{
+ /** @var IDibiDriver */
+ private $driver;
+
+ /** @var array */
+ private $columns;
+
+ /** @var array */
+ private $names;
+
+
+
+ public function __construct(IDibiDriver $driver)
+ {
+ $this->driver = $driver;
+ }
+
+
+
+ /**
+ * @return array of DibiColumnInfo
+ */
+ public function getColumns()
+ {
+ $this->initColumns();
+ return array_values($this->columns);
+ }
+
+
+
+ /**
+ * @param bool
+ * @return array of string
+ */
+ public function getColumnNames($fullNames = FALSE)
+ {
+ $this->initColumns();
+ $res = array();
+ foreach ($this->columns as $column) {
+ $res[] = $fullNames ? $column->getFullName() : $column->getName();
+ }
+ return $res;
+ }