1
0
mirror of https://github.com/dg/dibi.git synced 2025-07-31 19:30:30 +02:00

* better datetime converting in DibiResult (see http://forum.dibiphp.com/viewtopic.php?pid=2331)

* added support for Nette_Debug
* renamed NObject -> Nette_Object (Nette::Object in PHP 5.3)
This commit is contained in:
David Grudl
2008-04-03 12:40:04 +00:00
parent 2632953541
commit 24bf999cd9
23 changed files with 121 additions and 35 deletions

View File

@@ -23,6 +23,13 @@
/**
* Custom output for Nette::Debug.
*/
interface IDebuggable
interface Nette_IDebuggable
{
/**
* Returns custom panels.
* @return array
*/
function getPanels();
}

View File

@@ -61,7 +61,7 @@
* @package Nette
* @version $Revision$ $Date$
*/
abstract class NObject
abstract class Nette_Object
{
/**

View File

@@ -112,7 +112,7 @@ class DirectoryNotFoundException extends IOException
/**
* User attempt to terminate the current script
* User attempt to terminate the current script.
*/
class AbortException extends RuntimeException
{

View File

@@ -32,8 +32,8 @@ if (version_compare(PHP_VERSION, '5.1.0', '<')) {
// nette libraries
if (!class_exists('NotImplementedException', FALSE)) { require_once dirname(__FILE__) . '/Nette/exceptions.php'; }
if (!class_exists('NObject', FALSE)) { require_once dirname(__FILE__) . '/Nette/Object.php'; }
if (!interface_exists('IDebuggable', FALSE)) { require_once dirname(__FILE__) . '/Nette/IDebuggable.php'; }
if (!class_exists('Nette_Object', FALSE)) { require_once dirname(__FILE__) . '/Nette/Object.php'; }
if (!interface_exists('Nette_IDebuggable', FALSE)) { require_once dirname(__FILE__) . '/Nette/IDebuggable.php'; }
// dibi libraries
require_once dirname(__FILE__) . '/libs/interfaces.php';
@@ -154,6 +154,10 @@ class dibi
/********************* connections handling ****************d*g**/
/**
* Creates a new DibiConnection object and connects it to specified database.
*
@@ -164,7 +168,11 @@ class dibi
*/
public static function connect($config = array(), $name = 0)
{
if (is_array($config) || $config instanceof IMap) {
if (class_exists('Nette_Debug', FALSE) && Nette_Debug::isEnabled()) {
Nette_Debug::addColophon(array(__CLASS__, 'getColophon'));
}
if (is_array($config) || $config instanceof Nette_Collections_IMap) {
$config['name'] = $name;
} else {
$config .= '&name=' . urlencode($name);
@@ -238,6 +246,10 @@ class dibi
/********************* monostate for active connection ****************d*g**/
/**
* Generates and executes SQL query - Monostate for DibiConnection::query().
*
@@ -413,6 +425,10 @@ class dibi
/********************* data types ****************d*g**/
/**
* Pseudotype for timestamp representation.
*
@@ -448,6 +464,10 @@ class dibi
/********************* substitutions ****************d*g**/
/**
* Create a new substitution pair for indentifiers.
*
@@ -491,6 +511,10 @@ class dibi
/********************* event handling ****************d*g**/
/**
* Add new event handler.
*
@@ -543,6 +567,10 @@ class dibi
/********************* misc tools ****************d*g**/
/**
* Prints out a syntax highlighted version of the SQL command or DibiResult.
*
@@ -604,4 +632,17 @@ class dibi
}
/**
* Returns brief descriptions.
* @return array
*/
public static function getColophon()
{
return array(
'dibi version: ' . dibi::VERSION,
'Number or queries: ' . dibi::$numOfQueries . (dibi::$totalTime === NULL ? '' : ' (elapsed time: ' . sprintf('%0.3f', dibi::$totalTime * 1000) . ' ms)'),
);
}
}

View File

@@ -34,7 +34,7 @@
* @package dibi
* @version $Revision$ $Date$
*/
class DibiMsSqlDriver extends NObject implements IDibiDriver
class DibiMsSqlDriver extends Nette_Object implements IDibiDriver
{
/**

View File

@@ -40,7 +40,7 @@
* @package dibi
* @version $Revision$ $Date$
*/
class DibiMySqlDriver extends NObject implements IDibiDriver
class DibiMySqlDriver extends Nette_Object implements IDibiDriver
{
/**

View File

@@ -40,7 +40,7 @@
* @package dibi
* @version $Revision$ $Date$
*/
class DibiMySqliDriver extends NObject implements IDibiDriver
class DibiMySqliDriver extends Nette_Object implements IDibiDriver
{
/**

View File

@@ -33,7 +33,7 @@
* @package dibi
* @version $Revision$ $Date$
*/
class DibiOdbcDriver extends NObject implements IDibiDriver
class DibiOdbcDriver extends Nette_Object implements IDibiDriver
{
/**

View File

@@ -33,7 +33,7 @@
* @package dibi
* @version $Revision$ $Date$
*/
class DibiOracleDriver extends NObject implements IDibiDriver
class DibiOracleDriver extends Nette_Object implements IDibiDriver
{
/**

View File

@@ -33,7 +33,7 @@
* @package dibi
* @version $Revision$ $Date$
*/
class DibiPdoDriver extends NObject implements IDibiDriver
class DibiPdoDriver extends Nette_Object implements IDibiDriver
{
/**

View File

@@ -34,7 +34,7 @@
* @package dibi
* @version $Revision$ $Date$
*/
class DibiPostgreDriver extends NObject implements IDibiDriver
class DibiPostgreDriver extends Nette_Object implements IDibiDriver
{
/**

View File

@@ -34,7 +34,7 @@
* @package dibi
* @version $Revision$ $Date$
*/
class DibiSqliteDriver extends NObject implements IDibiDriver
class DibiSqliteDriver extends Nette_Object implements IDibiDriver
{
/**

View File

@@ -27,7 +27,7 @@
* @package dibi
* @version $Revision$ $Date$
*/
class DibiConnection extends NObject
class DibiConnection extends Nette_Object
{
/**
* Current connection configuration.
@@ -58,7 +58,7 @@ class DibiConnection extends NObject
/**
* Creates object and (optionally) connects to a database.
*
* @param array|string|IMap connection parameters
* @param array|string|Nette_Collections_IMap connection parameters
* @throws DibiException
*/
public function __construct($config)
@@ -67,7 +67,7 @@ class DibiConnection extends NObject
if (is_string($config)) {
parse_str($config, $config);
} elseif ($config instanceof IMap) {
} elseif ($config instanceof Nette_Collections_IMap) {
$config = $config->toArray();
}

View File

@@ -27,7 +27,7 @@
* @package dibi
* @version $Revision$ $Date$
*/
class DibiDataSource extends NObject implements IDataSource
class DibiDataSource extends Nette_Object implements IDataSource
{
/** @var DibiConnection */
private $connection;

View File

@@ -42,7 +42,7 @@ class DibiException extends Exception
* @package dibi
* @version $Revision$ $Date$
*/
class DibiDriverException extends DibiException implements IDebuggable
class DibiDriverException extends DibiException implements Nette_IDebuggable
{
/** @var string */
private static $errorMsg;
@@ -87,6 +87,27 @@ class DibiDriverException extends DibiException implements IDebuggable
/********************* interface Nette_IDebuggable ****************d*g**/
/**
* Returns custom panels.
* @return array
*/
public function getPanels()
{
$panels = array();
if ($this->sql !== NULL) {
$panels['SQL'] = array(
'expanded' => TRUE,
'content' => dibi::dump($this->sql, TRUE),
);
}
return $panels;
}
/********************* error catching ****************d*g**/

View File

@@ -27,7 +27,7 @@
* @package dibi
* @version $Revision$ $Date$
*/
final class DibiLogger extends NObject
final class DibiLogger extends Nette_Object
{
/** @var string Name of the file where SQL errors should be logged */
private $file;

View File

@@ -41,7 +41,7 @@
* @package dibi
* @version $Revision$ $Date$
*/
class DibiResult extends NObject implements IDataSource
class DibiResult extends Nette_Object implements IDataSource
{
/**
* IDibiDriver.
@@ -240,7 +240,7 @@ class DibiResult extends NObject implements IDataSource
if ($this->xlat !== NULL) {
foreach ($this->xlat as $col => $type) {
if (isset($row[$col])) {
$row[$col] = $this->convert($row[$col], $type);
$row[$col] = $this->convert($row[$col], $type[0], $type[1]);
}
}
}
@@ -269,7 +269,8 @@ class DibiResult extends NObject implements IDataSource
// types-converting?
$key = key($row);
if (isset($this->xlat[$key])) {
return $this->convert($value, $this->xlat[$key]);
$type = $this->xlat[$key];
return $this->convert($value, $type[0], $type[1]);
}
return $value;
@@ -447,14 +448,9 @@ class DibiResult extends NObject implements IDataSource
final public function setType($col, $type = NULL)
final public function setType($col, $type = NULL, $format = NULL)
{
if (is_array($col)) {
$this->xlat = $col;
} else {
$this->xlat[$col] = $type;
}
$this->xlat[$col] = array($type, $format);
}
@@ -466,7 +462,7 @@ class DibiResult extends NObject implements IDataSource
final public function convert($value, $type)
final public function convert($value, $type, $format = NULL)
{
if ($value === NULL || $value === FALSE) {
return $value;
@@ -478,7 +474,7 @@ class DibiResult extends NObject implements IDataSource
}
if ($type === dibi::FIELD_DATE || $type === dibi::FIELD_DATETIME) {
return strtotime($value);
return $format === NULL ? strtotime($value) : date($format, strtotime($value));
}
if ($type === dibi::FIELD_BOOL) {

View File

@@ -26,7 +26,7 @@
* @package dibi
* @version $Revision$ $Date$
*/
abstract class DibiTable extends NObject
abstract class DibiTable extends Nette_Object
{
/** @var string primary key mask */
public static $primaryMask = 'id';

View File

@@ -27,7 +27,7 @@
* @package dibi
* @version $Revision$ $Date$
*/
final class DibiTranslator extends NObject
final class DibiTranslator extends Nette_Object
{
/** @var string */
public $sql;

View File

@@ -23,7 +23,7 @@
* Default implemenation of IDibiVariable.
* @package dibi
*/
class DibiVariable extends NObject implements IDibiVariable
class DibiVariable extends Nette_Object implements IDibiVariable
{
/** @var mixed */
public $value;

View File

@@ -15,5 +15,7 @@ $res = dibi::query('SELECT * FROM [customers]');
// auto-convert this column to integer
$res->setType('customer_id', Dibi::FIELD_INTEGER);
$res->setType('added', Dibi::FIELD_DATETIME, 'H:i j.n.Y');
$row = $res->fetch();
var_dump($row);

19
examples/nette-debug.php Normal file
View File

@@ -0,0 +1,19 @@
<h1>Nette::Debug && dibi example</h1>
<?php
require_once '../dibi/dibi.php';
require_once 'Nette/Debug.php';
Nette_Debug::enable();
dibi::connect(array(
'driver' => 'sqlite',
'database' => 'sample.sdb',
));
// throws error
dibi::query('SELECT FROM [customers] WHERE [customer_id] < %i', 38);

Binary file not shown.