mirror of
https://github.com/dg/dibi.git
synced 2025-08-31 17:51:43 +02:00
Compare commits
81 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
0a2d12c47a | ||
|
19748f7c5b | ||
|
ee5f1dd293 | ||
|
f4ae1e692f | ||
|
443661f0e9 | ||
|
ac0ab9041b | ||
|
d7885921e6 | ||
|
ce459f440a | ||
|
df37a500fa | ||
|
8a5eddfabc | ||
|
a57f3dfc83 | ||
|
2702de6ccb | ||
|
73e1f366d4 | ||
|
656cbfc40c | ||
|
e778096641 | ||
|
68521d69e3 | ||
|
a68886f51d | ||
|
f534c15f0e | ||
|
985f59a2b2 | ||
|
6fc99254ab | ||
|
dc688f3ee7 | ||
|
cde5af7cbe | ||
|
7c35e49a1c | ||
|
6b08cf0711 | ||
|
4b0ebc76b0 | ||
|
5993ea8aaf | ||
|
f89a2310cc | ||
|
a118c2cf96 | ||
|
9b0e64220b | ||
|
1c386d5582 | ||
|
c9944b3886 | ||
|
d0fd009dda | ||
|
c32251357d | ||
|
c23d9c2866 | ||
|
60893a1c11 | ||
|
f31d4a9afa | ||
|
abd3e2116c | ||
|
e4e767048f | ||
|
1ebb2deb83 | ||
|
47f8a6f88d | ||
|
fee5c294d8 | ||
|
b14a4efbbb | ||
|
6949f37a7a | ||
|
e99ce9d053 | ||
|
d4c72bbd4d | ||
|
c802f9343a | ||
|
771bdbe124 | ||
|
1a4fca41a7 | ||
|
176b1a8895 | ||
|
0071b80938 | ||
|
4f64bd726b | ||
|
7fc3d76072 | ||
|
a6cc588d91 | ||
|
6666d71e5b | ||
|
5082282e35 | ||
|
7cee7997e2 | ||
|
738c0c91ed | ||
|
2769f1ae0a | ||
|
bf8fb69b9a | ||
|
3780a42971 | ||
|
791d001bfd | ||
|
367b115ad2 | ||
|
7318658017 | ||
|
ddf7b74bf0 | ||
|
30b5290c9d | ||
|
a36678d3db | ||
|
530e7d30c9 | ||
|
3b1e9e2632 | ||
|
1b38d13422 | ||
|
f348828223 | ||
|
5599dde525 | ||
|
32518eca54 | ||
|
a47395d16a | ||
|
a1f7413c9f | ||
|
1728437f5d | ||
|
cf942c68ce | ||
|
e87c112d71 | ||
|
9e23730cb0 | ||
|
a388767848 | ||
|
bc59eb6a14 | ||
|
44aba8a986 |
2
.gitattributes
vendored
2
.gitattributes
vendored
@@ -1,4 +1,2 @@
|
||||
.gitattributes export-ignore
|
||||
.gitignore export-ignore
|
||||
.travis.yml export-ignore
|
||||
tests/ export-ignore
|
||||
|
@@ -1,20 +1,20 @@
|
||||
{
|
||||
"name": "dibi/dibi",
|
||||
"description": "Dibi is Database Abstraction Library for PHP 5.",
|
||||
"description": "Dibi is Database Abstraction Library for PHP",
|
||||
"keywords": ["database", "dbal", "mysql", "postgresql", "sqlite", "mssql", "oracle", "access", "pdo", "odbc"],
|
||||
"homepage": "http://dibiphp.com/",
|
||||
"homepage": "http://dibiphp.com",
|
||||
"license": ["BSD-3-Clause", "GPL-2.0", "GPL-3.0"],
|
||||
"authors": [
|
||||
{
|
||||
"name": "David Grudl",
|
||||
"homepage": "http://davidgrudl.com"
|
||||
"homepage": "https://davidgrudl.com"
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"php": ">=5.2.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"nette/tester": "~1.1"
|
||||
"tracy/tracy": "~2.2"
|
||||
},
|
||||
"replace": {
|
||||
"dg/dibi": "self.version"
|
||||
|
@@ -3,7 +3,7 @@
|
||||
/**
|
||||
* This file is part of the "dibi" - smart database abstraction layer.
|
||||
*
|
||||
* Copyright (c) 2005 David Grudl (http://davidgrudl.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the file license.txt that was distributed with this source code.
|
||||
|
@@ -2,7 +2,7 @@
|
||||
|
||||
/**
|
||||
* This file is part of the "dibi" - smart database abstraction layer.
|
||||
* Copyright (c) 2005 David Grudl (http://davidgrudl.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
|
||||
|
@@ -2,7 +2,7 @@
|
||||
|
||||
/**
|
||||
* This file is part of the "dibi" - smart database abstraction layer.
|
||||
* Copyright (c) 2005 David Grudl (http://davidgrudl.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
|
||||
@@ -124,7 +124,8 @@ class DibiNettePanel extends DibiObject implements IBarPanel
|
||||
$event->connection->onEvent = NULL;
|
||||
$cmd = is_string($this->explain) ? $this->explain : ($event->connection->getConfig('driver') === 'oracle' ? 'EXPLAIN PLAN' : 'EXPLAIN');
|
||||
$explain = dibi::dump($event->connection->nativeQuery("$cmd $event->sql"), TRUE);
|
||||
} catch (DibiException $e) {}
|
||||
} catch (DibiException $e) {
|
||||
}
|
||||
list($event->connection->onEvent, dibi::$numOfQueries, dibi::$totalTime) = $backup;
|
||||
}
|
||||
|
||||
|
52
dibi/bridges/Nette-2.2/DibiNette22Extension.php
Normal file
52
dibi/bridges/Nette-2.2/DibiNette22Extension.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the "dibi" - smart database abstraction layer.
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
namespace Dibi\Bridges\Nette;
|
||||
|
||||
use dibi;
|
||||
use Nette;
|
||||
|
||||
|
||||
/**
|
||||
* Dibi extension for Nette Framework 2.2. Creates 'connection' & 'panel' services.
|
||||
*
|
||||
* @author David Grudl
|
||||
* @package dibi\nette
|
||||
*/
|
||||
class DibiExtension22 extends Nette\DI\CompilerExtension
|
||||
{
|
||||
|
||||
public function loadConfiguration()
|
||||
{
|
||||
$container = $this->getContainerBuilder();
|
||||
$config = $this->getConfig();
|
||||
|
||||
$useProfiler = isset($config['profiler'])
|
||||
? $config['profiler']
|
||||
: class_exists('Tracy\Debugger') && $container->parameters['debugMode'];
|
||||
|
||||
unset($config['profiler']);
|
||||
|
||||
if (isset($config['flags'])) {
|
||||
$flags = 0;
|
||||
foreach ((array) $config['flags'] as $flag) {
|
||||
$flags |= constant($flag);
|
||||
}
|
||||
$config['flags'] = $flags;
|
||||
}
|
||||
|
||||
$connection = $container->addDefinition($this->prefix('connection'))
|
||||
->setClass('DibiConnection', array($config));
|
||||
|
||||
if ($useProfiler) {
|
||||
$panel = $container->addDefinition($this->prefix('panel'))
|
||||
->setClass('Dibi\Bridges\Tracy\Panel');
|
||||
$connection->addSetup(array($panel, 'register'), array($connection));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
12
dibi/bridges/Nette-2.2/config.sample.neon
Normal file
12
dibi/bridges/Nette-2.2/config.sample.neon
Normal file
@@ -0,0 +1,12 @@
|
||||
# This will create service named 'dibi.connection'.
|
||||
# Requires Nette Framework 2.2
|
||||
|
||||
extensions:
|
||||
dibi: Dibi\Bridges\Nette\DibiExtension22
|
||||
|
||||
dibi:
|
||||
host: localhost
|
||||
username: root
|
||||
password: ***
|
||||
database: foo
|
||||
lazy: TRUE
|
146
dibi/bridges/Tracy/Panel.php
Normal file
146
dibi/bridges/Tracy/Panel.php
Normal file
@@ -0,0 +1,146 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the "dibi" - smart database abstraction layer.
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
namespace Dibi\Bridges\Tracy;
|
||||
|
||||
use dibi;
|
||||
use Tracy;
|
||||
|
||||
|
||||
/**
|
||||
* Dibi panel for Tracy.
|
||||
* @package dibi\nette
|
||||
* @author David Grudl
|
||||
*/
|
||||
class Panel extends \DibiObject implements Tracy\IBarPanel
|
||||
{
|
||||
/** @var int maximum SQL length */
|
||||
static public $maxLength = 1000;
|
||||
|
||||
/** @var bool explain queries? */
|
||||
public $explain;
|
||||
|
||||
/** @var int */
|
||||
public $filter;
|
||||
|
||||
/** @var array */
|
||||
private $events = array();
|
||||
|
||||
|
||||
public function __construct($explain = TRUE, $filter = NULL)
|
||||
{
|
||||
$this->filter = $filter ? (int) $filter : \DibiEvent::QUERY;
|
||||
$this->explain = $explain;
|
||||
}
|
||||
|
||||
|
||||
public function register(\DibiConnection $connection)
|
||||
{
|
||||
Tracy\Debugger::getBar()->addPanel($this);
|
||||
Tracy\Debugger::getBlueScreen()->addPanel(array(__CLASS__, 'renderException'));
|
||||
$connection->onEvent[] = array($this, 'logEvent');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* After event notification.
|
||||
* @return void
|
||||
*/
|
||||
public function logEvent(\DibiEvent $event)
|
||||
{
|
||||
if (($event->type & $this->filter) === 0) {
|
||||
return;
|
||||
}
|
||||
$this->events[] = $event;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns blue-screen custom tab.
|
||||
* @return mixed
|
||||
*/
|
||||
public static function renderException($e)
|
||||
{
|
||||
if ($e instanceof \DibiException && $e->getSql()) {
|
||||
return array(
|
||||
'tab' => 'SQL',
|
||||
'panel' => dibi::dump($e->getSql(), TRUE),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns HTML code for custom tab. (Tracy\IBarPanel)
|
||||
* @return mixed
|
||||
*/
|
||||
public function getTab()
|
||||
{
|
||||
$totalTime = 0;
|
||||
foreach ($this->events as $event) {
|
||||
$totalTime += $event->time;
|
||||
}
|
||||
return '<span title="dibi"><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAQAAAC1+jfqAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAEYSURBVBgZBcHPio5hGAfg6/2+R980k6wmJgsJ5U/ZOAqbSc2GnXOwUg7BESgLUeIQ1GSjLFnMwsKGGg1qxJRmPM97/1zXFAAAAEADdlfZzr26miup2svnelq7d2aYgt3rebl585wN6+K3I1/9fJe7O/uIePP2SypJkiRJ0vMhr55FLCA3zgIAOK9uQ4MS361ZOSX+OrTvkgINSjS/HIvhjxNNFGgQsbSmabohKDNoUGLohsls6BaiQIMSs2FYmnXdUsygQYmumy3Nhi6igwalDEOJEjPKP7CA2aFNK8Bkyy3fdNCg7r9/fW3jgpVJbDmy5+PB2IYp4MXFelQ7izPrhkPHB+P5/PjhD5gCgCenx+VR/dODEwD+A3T7nqbxwf1HAAAAAElFTkSuQmCC" />'
|
||||
. count($this->events) . ' queries'
|
||||
. ($totalTime ? sprintf(' / %0.1f ms', $totalTime * 1000) : '')
|
||||
. '</span>';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns HTML code for custom panel. (Tracy\IBarPanel)
|
||||
* @return mixed
|
||||
*/
|
||||
public function getPanel()
|
||||
{
|
||||
$totalTime = $s = NULL;
|
||||
$h = 'htmlSpecialChars';
|
||||
foreach ($this->events as $event) {
|
||||
$totalTime += $event->time;
|
||||
$explain = NULL; // EXPLAIN is called here to work SELECT FOUND_ROWS()
|
||||
if ($this->explain && $event->type === \DibiEvent::SELECT) {
|
||||
try {
|
||||
$backup = array($event->connection->onEvent, dibi::$numOfQueries, dibi::$totalTime);
|
||||
$event->connection->onEvent = NULL;
|
||||
$cmd = is_string($this->explain) ? $this->explain : ($event->connection->getConfig('driver') === 'oracle' ? 'EXPLAIN PLAN' : 'EXPLAIN');
|
||||
$explain = dibi::dump($event->connection->nativeQuery("$cmd $event->sql"), TRUE);
|
||||
} catch (\DibiException $e) {
|
||||
}
|
||||
list($event->connection->onEvent, dibi::$numOfQueries, dibi::$totalTime) = $backup;
|
||||
}
|
||||
|
||||
$s .= '<tr><td>' . sprintf('%0.3f', $event->time * 1000);
|
||||
if ($explain) {
|
||||
static $counter;
|
||||
$counter++;
|
||||
$s .= "<br /><a href='#tracy-debug-DibiProfiler-row-$counter' class='tracy-toggle tracy-collapsed' rel='#tracy-debug-DibiProfiler-row-$counter'>explain</a>";
|
||||
}
|
||||
|
||||
$s .= '</td><td class="tracy-DibiProfiler-sql">' . dibi::dump(strlen($event->sql) > self::$maxLength ? substr($event->sql, 0, self::$maxLength) . '...' : $event->sql, TRUE);
|
||||
if ($explain) {
|
||||
$s .= "<div id='tracy-debug-DibiProfiler-row-$counter' class='tracy-collapsed'>{$explain}</div>";
|
||||
}
|
||||
if ($event->source) {
|
||||
$s .= Tracy\Helpers::editorLink($event->source[0], $event->source[1]);//->class('tracy-DibiProfiler-source');
|
||||
}
|
||||
|
||||
$s .= "</td><td>{$event->count}</td><td>{$h($event->connection->getConfig('driver') . '/' . $event->connection->getConfig('name'))}</td></tr>";
|
||||
}
|
||||
|
||||
return empty($this->events) ? '' :
|
||||
'<style> #tracy-debug td.tracy-DibiProfiler-sql { background: white !important }
|
||||
#tracy-debug .tracy-DibiProfiler-source { color: #999 !important }
|
||||
#tracy-debug tracy-DibiProfiler tr table { margin: 8px 0; max-height: 150px; overflow:auto } </style>
|
||||
<h1>Queries: ' . count($this->events) . ($totalTime === NULL ? '' : sprintf(', time: %0.3f ms', $totalTime * 1000)) . '</h1>
|
||||
<div class="tracy-inner tracy-DibiProfiler">
|
||||
<table>
|
||||
<tr><th>Time ms</th><th>SQL Statement</th><th>Rows</th><th>Connection</th></tr>' . $s . '
|
||||
</table>
|
||||
</div>';
|
||||
}
|
||||
|
||||
}
|
596
dibi/dibi.php
596
dibi/dibi.php
@@ -3,7 +3,7 @@
|
||||
/**
|
||||
* dibi - smart database abstraction layer (http://dibiphp.com)
|
||||
*
|
||||
* Copyright (c) 2005, 2012 David Grudl (http://davidgrudl.com)
|
||||
* Copyright (c) 2005, 2012 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@ if (version_compare(PHP_VERSION, '5.2.0', '<')) {
|
||||
|
||||
|
||||
require_once dirname(__FILE__) . '/libs/interfaces.php';
|
||||
require_once dirname(__FILE__) . '/libs/Dibi.php';
|
||||
require_once dirname(__FILE__) . '/libs/DibiDateTime.php';
|
||||
require_once dirname(__FILE__) . '/libs/DibiObject.php';
|
||||
require_once dirname(__FILE__) . '/libs/DibiLiteral.php';
|
||||
@@ -32,598 +33,7 @@ require_once dirname(__FILE__) . '/libs/DibiDatabaseInfo.php';
|
||||
require_once dirname(__FILE__) . '/libs/DibiEvent.php';
|
||||
require_once dirname(__FILE__) . '/libs/DibiFileLogger.php';
|
||||
require_once dirname(__FILE__) . '/libs/DibiFirePhpLogger.php';
|
||||
|
||||
if (interface_exists('Nette\Diagnostics\IBarPanel') || interface_exists('IBarPanel')) {
|
||||
require_once dirname(__FILE__) . '/bridges/Nette-2.1/DibiNettePanel.php';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Interface for database drivers.
|
||||
*
|
||||
* This class is static container class for creating DB objects and
|
||||
* store connections info.
|
||||
*
|
||||
* @author David Grudl
|
||||
* @package dibi
|
||||
*/
|
||||
class dibi
|
||||
{
|
||||
/** column type */
|
||||
const TEXT = 's', // as 'string'
|
||||
BINARY = 'bin',
|
||||
BOOL = 'b',
|
||||
INTEGER = 'i',
|
||||
FLOAT = 'f',
|
||||
DATE = 'd',
|
||||
DATETIME = 't',
|
||||
TIME = 't';
|
||||
|
||||
const IDENTIFIER = 'n';
|
||||
|
||||
/** @deprecated */
|
||||
const FIELD_TEXT = dibi::TEXT,
|
||||
FIELD_BINARY = dibi::BINARY,
|
||||
FIELD_BOOL = dibi::BOOL,
|
||||
FIELD_INTEGER = dibi::INTEGER,
|
||||
FIELD_FLOAT = dibi::FLOAT,
|
||||
FIELD_DATE = dibi::DATE,
|
||||
FIELD_DATETIME = dibi::DATETIME,
|
||||
FIELD_TIME = dibi::TIME;
|
||||
|
||||
/** version */
|
||||
const VERSION = '2.1.3',
|
||||
REVISION = 'released on 2015-01-13';
|
||||
|
||||
/** sorting order */
|
||||
const ASC = 'ASC',
|
||||
DESC = 'DESC';
|
||||
|
||||
/** @var DibiConnection[] Connection registry storage for DibiConnection objects */
|
||||
private static $registry = array();
|
||||
|
||||
/** @var DibiConnection Current connection */
|
||||
private static $connection;
|
||||
|
||||
/** @var array @see addHandler */
|
||||
private static $handlers = array();
|
||||
|
||||
/** @var string Last SQL command @see dibi::query() */
|
||||
public static $sql;
|
||||
|
||||
/** @var int Elapsed time for last query */
|
||||
public static $elapsedTime;
|
||||
|
||||
/** @var int Elapsed time for all queries */
|
||||
public static $totalTime;
|
||||
|
||||
/** @var int Number or queries */
|
||||
public static $numOfQueries = 0;
|
||||
|
||||
/** @var string Default dibi driver */
|
||||
public static $defaultDriver = 'mysql';
|
||||
|
||||
|
||||
/**
|
||||
* Static class - cannot be instantiated.
|
||||
*/
|
||||
final public function __construct()
|
||||
{
|
||||
throw new LogicException("Cannot instantiate static class " . get_class($this));
|
||||
}
|
||||
|
||||
|
||||
/********************* connections handling ****************d*g**/
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new DibiConnection object and connects it to specified database.
|
||||
* @param mixed connection parameters
|
||||
* @param string connection name
|
||||
* @return DibiConnection
|
||||
* @throws DibiException
|
||||
*/
|
||||
public static function connect($config = array(), $name = 0)
|
||||
{
|
||||
return self::$connection = self::$registry[$name] = new DibiConnection($config, $name);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Disconnects from database (doesn't destroy DibiConnection object).
|
||||
* @return void
|
||||
*/
|
||||
public static function disconnect()
|
||||
{
|
||||
self::getConnection()->disconnect();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns TRUE when connection was established.
|
||||
* @return bool
|
||||
*/
|
||||
public static function isConnected()
|
||||
{
|
||||
return (self::$connection !== NULL) && self::$connection->isConnected();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Retrieve active connection.
|
||||
* @param string connection registy name
|
||||
* @return DibiConnection
|
||||
* @throws DibiException
|
||||
*/
|
||||
public static function getConnection($name = NULL)
|
||||
{
|
||||
if ($name === NULL) {
|
||||
if (self::$connection === NULL) {
|
||||
throw new DibiException('Dibi is not connected to database.');
|
||||
}
|
||||
|
||||
return self::$connection;
|
||||
}
|
||||
|
||||
if (!isset(self::$registry[$name])) {
|
||||
throw new DibiException("There is no connection named '$name'.");
|
||||
}
|
||||
|
||||
return self::$registry[$name];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sets connection.
|
||||
* @param DibiConnection
|
||||
* @return DibiConnection
|
||||
*/
|
||||
public static function setConnection(DibiConnection $connection)
|
||||
{
|
||||
return self::$connection = $connection;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Change active connection.
|
||||
* @param string connection registy name
|
||||
* @return void
|
||||
* @throws DibiException
|
||||
*/
|
||||
public static function activate($name)
|
||||
{
|
||||
self::$connection = self::getConnection($name);
|
||||
}
|
||||
|
||||
|
||||
/********************* monostate for active connection ****************d*g**/
|
||||
|
||||
|
||||
/**
|
||||
* Generates and executes SQL query - Monostate for DibiConnection::query().
|
||||
* @param array|mixed one or more arguments
|
||||
* @return DibiResult|int result set object (if any)
|
||||
* @throws DibiException
|
||||
*/
|
||||
public static function query($args)
|
||||
{
|
||||
$args = func_get_args();
|
||||
return self::getConnection()->query($args);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Executes the SQL query - Monostate for DibiConnection::nativeQuery().
|
||||
* @param string SQL statement.
|
||||
* @return DibiResult|int result set object (if any)
|
||||
*/
|
||||
public static function nativeQuery($sql)
|
||||
{
|
||||
return self::getConnection()->nativeQuery($sql);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Generates and prints SQL query - Monostate for DibiConnection::test().
|
||||
* @param array|mixed one or more arguments
|
||||
* @return bool
|
||||
*/
|
||||
public static function test($args)
|
||||
{
|
||||
$args = func_get_args();
|
||||
return self::getConnection()->test($args);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Generates and returns SQL query as DibiDataSource - Monostate for DibiConnection::test().
|
||||
* @param array|mixed one or more arguments
|
||||
* @return DibiDataSource
|
||||
*/
|
||||
public static function dataSource($args)
|
||||
{
|
||||
$args = func_get_args();
|
||||
return self::getConnection()->dataSource($args);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Executes SQL query and fetch result - Monostate for DibiConnection::query() & fetch().
|
||||
* @param array|mixed one or more arguments
|
||||
* @return DibiRow
|
||||
* @throws DibiException
|
||||
*/
|
||||
public static function fetch($args)
|
||||
{
|
||||
$args = func_get_args();
|
||||
return self::getConnection()->query($args)->fetch();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Executes SQL query and fetch results - Monostate for DibiConnection::query() & fetchAll().
|
||||
* @param array|mixed one or more arguments
|
||||
* @return DibiRow[]
|
||||
* @throws DibiException
|
||||
*/
|
||||
public static function fetchAll($args)
|
||||
{
|
||||
$args = func_get_args();
|
||||
return self::getConnection()->query($args)->fetchAll();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Executes SQL query and fetch first column - Monostate for DibiConnection::query() & fetchSingle().
|
||||
* @param array|mixed one or more arguments
|
||||
* @return string
|
||||
* @throws DibiException
|
||||
*/
|
||||
public static function fetchSingle($args)
|
||||
{
|
||||
$args = func_get_args();
|
||||
return self::getConnection()->query($args)->fetchSingle();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Executes SQL query and fetch pairs - Monostate for DibiConnection::query() & fetchPairs().
|
||||
* @param array|mixed one or more arguments
|
||||
* @return string
|
||||
* @throws DibiException
|
||||
*/
|
||||
public static function fetchPairs($args)
|
||||
{
|
||||
$args = func_get_args();
|
||||
return self::getConnection()->query($args)->fetchPairs();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets the number of affected rows.
|
||||
* Monostate for DibiConnection::getAffectedRows()
|
||||
* @return int number of rows
|
||||
* @throws DibiException
|
||||
*/
|
||||
public static function getAffectedRows()
|
||||
{
|
||||
return self::getConnection()->getAffectedRows();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets the number of affected rows. Alias for getAffectedRows().
|
||||
* @return int number of rows
|
||||
* @throws DibiException
|
||||
*/
|
||||
public static function affectedRows()
|
||||
{
|
||||
return self::getConnection()->getAffectedRows();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query.
|
||||
* Monostate for DibiConnection::getInsertId()
|
||||
* @param string optional sequence name
|
||||
* @return int
|
||||
* @throws DibiException
|
||||
*/
|
||||
public static function getInsertId($sequence=NULL)
|
||||
{
|
||||
return self::getConnection()->getInsertId($sequence);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Retrieves the ID generated for an AUTO_INCREMENT column. Alias for getInsertId().
|
||||
* @param string optional sequence name
|
||||
* @return int
|
||||
* @throws DibiException
|
||||
*/
|
||||
public static function insertId($sequence=NULL)
|
||||
{
|
||||
return self::getConnection()->getInsertId($sequence);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Begins a transaction - Monostate for DibiConnection::begin().
|
||||
* @param string optional savepoint name
|
||||
* @return void
|
||||
* @throws DibiException
|
||||
*/
|
||||
public static function begin($savepoint = NULL)
|
||||
{
|
||||
self::getConnection()->begin($savepoint);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Commits statements in a transaction - Monostate for DibiConnection::commit($savepoint = NULL).
|
||||
* @param string optional savepoint name
|
||||
* @return void
|
||||
* @throws DibiException
|
||||
*/
|
||||
public static function commit($savepoint = NULL)
|
||||
{
|
||||
self::getConnection()->commit($savepoint);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Rollback changes in a transaction - Monostate for DibiConnection::rollback().
|
||||
* @param string optional savepoint name
|
||||
* @return void
|
||||
* @throws DibiException
|
||||
*/
|
||||
public static function rollback($savepoint = NULL)
|
||||
{
|
||||
self::getConnection()->rollback($savepoint);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets a information about the current database - Monostate for DibiConnection::getDatabaseInfo().
|
||||
* @return DibiDatabaseInfo
|
||||
*/
|
||||
public static function getDatabaseInfo()
|
||||
{
|
||||
return self::getConnection()->getDatabaseInfo();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Import SQL dump from file - extreme fast!
|
||||
* @param string filename
|
||||
* @return int count of sql commands
|
||||
*/
|
||||
public static function loadFile($file)
|
||||
{
|
||||
return self::getConnection()->loadFile($file);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Replacement for majority of dibi::methods() in future.
|
||||
*/
|
||||
public static function __callStatic($name, $args)
|
||||
{
|
||||
//if ($name = 'select', 'update', ...') {
|
||||
// return self::command()->$name($args);
|
||||
//}
|
||||
return call_user_func_array(array(self::getConnection(), $name), $args);
|
||||
}
|
||||
|
||||
|
||||
/********************* fluent SQL builders ****************d*g**/
|
||||
|
||||
|
||||
/**
|
||||
* @return DibiFluent
|
||||
*/
|
||||
public static function command()
|
||||
{
|
||||
return self::getConnection()->command();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string column name
|
||||
* @return DibiFluent
|
||||
*/
|
||||
public static function select($args)
|
||||
{
|
||||
$args = func_get_args();
|
||||
return call_user_func_array(array(self::getConnection(), 'select'), $args);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string table
|
||||
* @param array
|
||||
* @return DibiFluent
|
||||
*/
|
||||
public static function update($table, $args)
|
||||
{
|
||||
return self::getConnection()->update($table, $args);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string table
|
||||
* @param array
|
||||
* @return DibiFluent
|
||||
*/
|
||||
public static function insert($table, $args)
|
||||
{
|
||||
return self::getConnection()->insert($table, $args);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string table
|
||||
* @return DibiFluent
|
||||
*/
|
||||
public static function delete($table)
|
||||
{
|
||||
return self::getConnection()->delete($table);
|
||||
}
|
||||
|
||||
|
||||
/********************* data types ****************d*g**/
|
||||
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
*/
|
||||
public static function datetime($time = NULL)
|
||||
{
|
||||
trigger_error(__METHOD__ . '() is deprecated; create DateTime object instead.', E_USER_WARNING);
|
||||
return new DibiDateTime($time);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
*/
|
||||
public static function date($date = NULL)
|
||||
{
|
||||
trigger_error(__METHOD__ . '() is deprecated; create DateTime object instead.', E_USER_WARNING);
|
||||
return new DibiDateTime($date);
|
||||
}
|
||||
|
||||
|
||||
/********************* substitutions ****************d*g**/
|
||||
|
||||
|
||||
/**
|
||||
* Returns substitution hashmap - Monostate for DibiConnection::getSubstitutes().
|
||||
* @return DibiHashMap
|
||||
*/
|
||||
public static function getSubstitutes()
|
||||
{
|
||||
return self::getConnection()->getSubstitutes();
|
||||
}
|
||||
|
||||
|
||||
/** @deprecated */
|
||||
public static function addSubst($expr, $subst)
|
||||
{
|
||||
trigger_error(__METHOD__ . '() is deprecated; use dibi::getSubstitutes()->expr = val; instead.', E_USER_WARNING);
|
||||
self::getSubstitutes()->$expr = $subst;
|
||||
}
|
||||
|
||||
|
||||
/** @deprecated */
|
||||
public static function removeSubst($expr)
|
||||
{
|
||||
trigger_error(__METHOD__ . '() is deprecated; use unset(dibi::getSubstitutes()->expr) instead.', E_USER_WARNING);
|
||||
$substitutes = self::getSubstitutes();
|
||||
if ($expr === TRUE) {
|
||||
foreach ($substitutes as $expr => $foo) {
|
||||
unset($substitutes->$expr);
|
||||
}
|
||||
} else {
|
||||
unset($substitutes->$expr);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/** @deprecated */
|
||||
public static function setSubstFallback($callback)
|
||||
{
|
||||
trigger_error(__METHOD__ . '() is deprecated; use dibi::getSubstitutes()->setCallback() instead.', E_USER_WARNING);
|
||||
self::getSubstitutes()->setCallback($callback);
|
||||
}
|
||||
|
||||
|
||||
/********************* misc tools ****************d*g**/
|
||||
|
||||
|
||||
/**
|
||||
* Prints out a syntax highlighted version of the SQL command or DibiResult.
|
||||
* @param string|DibiResult
|
||||
* @param bool return output instead of printing it?
|
||||
* @return string
|
||||
*/
|
||||
public static function dump($sql = NULL, $return = FALSE)
|
||||
{
|
||||
ob_start();
|
||||
if ($sql instanceof DibiResult) {
|
||||
$sql->dump();
|
||||
|
||||
} else {
|
||||
if ($sql === NULL) {
|
||||
$sql = self::$sql;
|
||||
}
|
||||
|
||||
static $keywords1 = 'SELECT|(?:ON\s+DUPLICATE\s+KEY)?UPDATE|INSERT(?:\s+INTO)?|REPLACE(?:\s+INTO)?|DELETE|CALL|UNION|FROM|WHERE|HAVING|GROUP\s+BY|ORDER\s+BY|LIMIT|OFFSET|SET|VALUES|LEFT\s+JOIN|INNER\s+JOIN|TRUNCATE';
|
||||
static $keywords2 = 'ALL|DISTINCT|DISTINCTROW|IGNORE|AS|USING|ON|AND|OR|IN|IS|NOT|NULL|LIKE|RLIKE|REGEXP|TRUE|FALSE';
|
||||
|
||||
// insert new lines
|
||||
$sql = " $sql ";
|
||||
$sql = preg_replace("#(?<=[\\s,(])($keywords1)(?=[\\s,)])#i", "\n\$1", $sql);
|
||||
|
||||
// reduce spaces
|
||||
$sql = preg_replace('#[ \t]{2,}#', " ", $sql);
|
||||
|
||||
$sql = wordwrap($sql, 100);
|
||||
$sql = preg_replace("#([ \t]*\r?\n){2,}#", "\n", $sql);
|
||||
|
||||
// syntax highlight
|
||||
$highlighter = "#(/\\*.+?\\*/)|(\\*\\*.+?\\*\\*)|(?<=[\\s,(])($keywords1)(?=[\\s,)])|(?<=[\\s,(=])($keywords2)(?=[\\s,)=])#is";
|
||||
if (PHP_SAPI === 'cli') {
|
||||
if (substr(getenv('TERM'), 0, 5) === 'xterm') {
|
||||
$sql = preg_replace_callback($highlighter, array('dibi', 'cliHighlightCallback'), $sql);
|
||||
}
|
||||
echo trim($sql) . "\n\n";
|
||||
|
||||
} else {
|
||||
$sql = htmlSpecialChars($sql);
|
||||
$sql = preg_replace_callback($highlighter, array('dibi', 'highlightCallback'), $sql);
|
||||
echo '<pre class="dump">', trim($sql), "</pre>\n\n";
|
||||
}
|
||||
}
|
||||
|
||||
if ($return) {
|
||||
return ob_get_clean();
|
||||
} else {
|
||||
ob_end_flush();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static function highlightCallback($matches)
|
||||
{
|
||||
if (!empty($matches[1])) { // comment
|
||||
return '<em style="color:gray">' . $matches[1] . '</em>';
|
||||
|
||||
} elseif (!empty($matches[2])) { // error
|
||||
return '<strong style="color:red">' . $matches[2] . '</strong>';
|
||||
|
||||
} elseif (!empty($matches[3])) { // most important keywords
|
||||
return '<strong style="color:blue">' . $matches[3] . '</strong>';
|
||||
|
||||
} elseif (!empty($matches[4])) { // other keywords
|
||||
return '<strong style="color:green">' . $matches[4] . '</strong>';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static function cliHighlightCallback($matches)
|
||||
{
|
||||
if (!empty($matches[1])) { // comment
|
||||
return "\033[1;30m" . $matches[1] . "\033[0m";
|
||||
|
||||
} elseif (!empty($matches[2])) { // error
|
||||
return "\033[1;31m" . $matches[2] . "\033[0m";
|
||||
|
||||
} elseif (!empty($matches[3])) { // most important keywords
|
||||
return "\033[1;34m" . $matches[3] . "\033[0m";
|
||||
|
||||
} elseif (!empty($matches[4])) { // other keywords
|
||||
return "\033[1;32m" . $matches[4] . "\033[0m";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -2,7 +2,7 @@
|
||||
|
||||
/**
|
||||
* This file is part of the "dibi" - smart database abstraction layer.
|
||||
* Copyright (c) 2005 David Grudl (http://davidgrudl.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
|
||||
@@ -88,7 +88,6 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD
|
||||
throw new DibiDriverException(ibase_errmsg(), ibase_errcode());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -277,10 +276,11 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD
|
||||
return $value ? 1 : 0;
|
||||
|
||||
case dibi::DATE:
|
||||
return $value instanceof DateTime ? $value->format("'Y-m-d'") : date("'Y-m-d'", $value);
|
||||
|
||||
case dibi::DATETIME:
|
||||
return $value instanceof DateTime ? $value->format("'Y-m-d H:i:s'") : date("'Y-m-d H:i:s'", $value);
|
||||
if (!$value instanceof DateTime && !$value instanceof DateTimeInterface) {
|
||||
$value = new DibiDateTime($value);
|
||||
}
|
||||
return $value->format($type === dibi::DATETIME ? "'Y-m-d H:i:s'" : "'Y-m-d'");
|
||||
|
||||
default:
|
||||
throw new InvalidArgumentException('Unsupported type.');
|
||||
@@ -348,7 +348,7 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD
|
||||
*/
|
||||
public function getRowCount()
|
||||
{
|
||||
throw new DibiNotSupportedException("Firebird/Interbase do not support returning number of rows in result set.");
|
||||
throw new DibiNotSupportedException('Firebird/Interbase do not support returning number of rows in result set.');
|
||||
}
|
||||
|
||||
|
||||
@@ -378,13 +378,13 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @param int the 0-based cursor pos to seek to
|
||||
* @return bool TRUE on success, FALSE if unable to seek to specified record
|
||||
* @throws DibiException
|
||||
*/
|
||||
public function seek($row)
|
||||
{
|
||||
throw new DibiNotSupportedException("Firebird/Interbase do not support seek in result set.");
|
||||
throw new DibiNotSupportedException('Firebird/Interbase do not support seek in result set.');
|
||||
}
|
||||
|
||||
|
||||
@@ -655,7 +655,7 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD
|
||||
END AS TRIGGER_ENABLED
|
||||
FROM RDB\$TRIGGERS
|
||||
WHERE RDB\$SYSTEM_FLAG = 0"
|
||||
. ($table === NULL ? ";" : " AND RDB\$RELATION_NAME = UPPER('$table');")
|
||||
. ($table === NULL ? ';' : " AND RDB\$RELATION_NAME = UPPER('$table');")
|
||||
);
|
||||
$triggers = array();
|
||||
while ($row = $res->fetch(TRUE)) {
|
||||
@@ -682,7 +682,7 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD
|
||||
$q = "SELECT TRIM(RDB\$TRIGGER_NAME)
|
||||
FROM RDB\$TRIGGERS
|
||||
WHERE RDB\$SYSTEM_FLAG = 0";
|
||||
$q .= $table === NULL ? ";" : " AND RDB\$RELATION_NAME = UPPER('$table')";
|
||||
$q .= $table === NULL ? ';' : " AND RDB\$RELATION_NAME = UPPER('$table')";
|
||||
|
||||
$res = $this->query($q);
|
||||
$triggers = array();
|
||||
|
@@ -2,7 +2,7 @@
|
||||
|
||||
/**
|
||||
* This file is part of the "dibi" - smart database abstraction layer.
|
||||
* Copyright (c) 2005 David Grudl (http://davidgrudl.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ require_once dirname(__FILE__) . '/DibiMsSql2005Reflector.php';
|
||||
* - username (or user)
|
||||
* - password (or pass)
|
||||
* - database => the database name to select
|
||||
* - options (array) => connection options {@link http://msdn.microsoft.com/en-us/library/cc296161(SQL.90).aspx}
|
||||
* - options (array) => connection options {@link https://msdn.microsoft.com/en-us/library/cc296161(SQL.90).aspx}
|
||||
* - charset => character encoding to set (default is UTF-8)
|
||||
* - resource (resource) => existing connection resource
|
||||
* - lazy, profiler, result, substitutes, ... => see DibiConnection options
|
||||
@@ -226,17 +226,18 @@ class DibiMsSql2005Driver extends DibiObject implements IDibiDriver, IDibiResult
|
||||
return "'" . str_replace("'", "''", $value) . "'";
|
||||
|
||||
case dibi::IDENTIFIER:
|
||||
// @see http://msdn.microsoft.com/en-us/library/ms176027.aspx
|
||||
// @see https://msdn.microsoft.com/en-us/library/ms176027.aspx
|
||||
return '[' . str_replace(']', ']]', $value) . ']';
|
||||
|
||||
case dibi::BOOL:
|
||||
return $value ? 1 : 0;
|
||||
|
||||
case dibi::DATE:
|
||||
return $value instanceof DateTime ? $value->format("'Y-m-d'") : date("'Y-m-d'", $value);
|
||||
|
||||
case dibi::DATETIME:
|
||||
return $value instanceof DateTime ? $value->format("'Y-m-d H:i:s'") : date("'Y-m-d H:i:s'", $value);
|
||||
if (!$value instanceof DateTime && !$value instanceof DateTimeInterface) {
|
||||
$value = new DibiDateTime($value);
|
||||
}
|
||||
return $value->format($type === dibi::DATETIME ? "'Y-m-d H:i:s'" : "'Y-m-d'");
|
||||
|
||||
default:
|
||||
throw new InvalidArgumentException('Unsupported type.');
|
||||
@@ -326,8 +327,8 @@ class DibiMsSql2005Driver extends DibiObject implements IDibiDriver, IDibiResult
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @param int the 0-based cursor pos to seek to
|
||||
* @return bool TRUE on success, FALSE if unable to seek to specified record
|
||||
*/
|
||||
public function seek($row)
|
||||
{
|
||||
|
@@ -2,7 +2,7 @@
|
||||
|
||||
/**
|
||||
* This file is part of the "dibi" - smart database abstraction layer.
|
||||
* Copyright (c) 2005 David Grudl (http://davidgrudl.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
|
||||
@@ -104,7 +104,7 @@ class DibiMsSql2005Reflector extends DibiObject implements IDibiReflector
|
||||
{
|
||||
$keyUsagesRes = $this->driver->query("SELECT * FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE TABLE_NAME = {$this->driver->escape($table, dibi::TEXT)}");
|
||||
$keyUsages = array();
|
||||
while( $row = $keyUsagesRes->fetch(TRUE) ) {
|
||||
while ($row = $keyUsagesRes->fetch(TRUE)) {
|
||||
$keyUsages[$row['CONSTRAINT_NAME']][(int) $row['ORDINAL_POSITION'] - 1] = $row['COLUMN_NAME'];
|
||||
}
|
||||
|
||||
|
@@ -2,7 +2,7 @@
|
||||
|
||||
/**
|
||||
* This file is part of the "dibi" - smart database abstraction layer.
|
||||
* Copyright (c) 2005 David Grudl (http://davidgrudl.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
require_once dirname(__FILE__) . '/DibiMsSqlReflector.php';
|
||||
@@ -211,17 +211,18 @@ class DibiMsSqlDriver extends DibiObject implements IDibiDriver, IDibiResultDriv
|
||||
return "'" . str_replace("'", "''", $value) . "'";
|
||||
|
||||
case dibi::IDENTIFIER:
|
||||
// @see http://msdn.microsoft.com/en-us/library/ms176027.aspx
|
||||
// @see https://msdn.microsoft.com/en-us/library/ms176027.aspx
|
||||
return '[' . str_replace(array('[', ']'), array('[[', ']]'), $value) . ']';
|
||||
|
||||
case dibi::BOOL:
|
||||
return $value ? 1 : 0;
|
||||
|
||||
case dibi::DATE:
|
||||
return $value instanceof DateTime ? $value->format("'Y-m-d'") : date("'Y-m-d'", $value);
|
||||
|
||||
case dibi::DATETIME:
|
||||
return $value instanceof DateTime ? $value->format("'Y-m-d H:i:s'") : date("'Y-m-d H:i:s'", $value);
|
||||
if (!$value instanceof DateTime && !$value instanceof DateTimeInterface) {
|
||||
$value = new DibiDateTime($value);
|
||||
}
|
||||
return $value->format($type === dibi::DATETIME ? "'Y-m-d H:i:s'" : "'Y-m-d'");
|
||||
|
||||
default:
|
||||
throw new InvalidArgumentException('Unsupported type.');
|
||||
@@ -362,5 +363,4 @@ class DibiMsSqlDriver extends DibiObject implements IDibiDriver, IDibiResultDriv
|
||||
return is_resource($this->resultSet) ? $this->resultSet : NULL;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@@ -3,7 +3,7 @@
|
||||
/**
|
||||
* This file is part of the "dibi" - smart database abstraction layer.
|
||||
*
|
||||
* Copyright (c) 2005, 2010 David Grudl (http://davidgrudl.com)
|
||||
* Copyright (c) 2005, 2010 David Grudl (https://davidgrudl.com)
|
||||
*
|
||||
* @package dibi\drivers
|
||||
*/
|
||||
@@ -34,10 +34,10 @@ class DibiMsSqlReflector extends DibiObject implements IDibiReflector
|
||||
*/
|
||||
public function getTables()
|
||||
{
|
||||
$res = $this->driver->query("
|
||||
$res = $this->driver->query('
|
||||
SELECT TABLE_NAME, TABLE_TYPE
|
||||
FROM INFORMATION_SCHEMA.TABLES
|
||||
");
|
||||
');
|
||||
$tables = array();
|
||||
while ($row = $res->fetch(FALSE)) {
|
||||
$tables[] = array(
|
||||
@@ -52,12 +52,12 @@ class DibiMsSqlReflector extends DibiObject implements IDibiReflector
|
||||
/**
|
||||
* Returns count of rows in a table
|
||||
* @param string
|
||||
* @return integer
|
||||
* @return int
|
||||
*/
|
||||
public function getTableCount($table, $fallback=true)
|
||||
public function getTableCount($table, $fallback = TRUE)
|
||||
{
|
||||
if (empty($table)) {
|
||||
return false;
|
||||
return FALSE;
|
||||
}
|
||||
$result = $this->driver->query("
|
||||
SELECT MAX(rowcnt)
|
||||
@@ -71,7 +71,7 @@ class DibiMsSqlReflector extends DibiObject implements IDibiReflector
|
||||
$row = $this->driver->query("SELECT COUNT(*) FROM {$this->driver->escape($table, dibi::IDENTIFIER)}")->fetch(FALSE);
|
||||
$count = intval($row[0]);
|
||||
} else {
|
||||
$count = false;
|
||||
$count = FALSE;
|
||||
}
|
||||
} else {
|
||||
$count = intval($row[0]);
|
||||
@@ -96,16 +96,16 @@ class DibiMsSqlReflector extends DibiObject implements IDibiReflector
|
||||
");
|
||||
$columns = array();
|
||||
while ($row = $res->fetch(TRUE)) {
|
||||
$size = false;
|
||||
$size = FALSE;
|
||||
$type = strtoupper($row['DATA_TYPE']);
|
||||
|
||||
$size_cols = array(
|
||||
'DATETIME'=>'DATETIME_PRECISION',
|
||||
'DECIMAL'=>'NUMERIC_PRECISION',
|
||||
'CHAR'=>'CHARACTER_MAXIMUM_LENGTH',
|
||||
'NCHAR'=>'CHARACTER_OCTET_LENGTH',
|
||||
'NVARCHAR'=>'CHARACTER_OCTET_LENGTH',
|
||||
'VARCHAR'=>'CHARACTER_OCTET_LENGTH'
|
||||
'DATETIME' => 'DATETIME_PRECISION',
|
||||
'DECIMAL' => 'NUMERIC_PRECISION',
|
||||
'CHAR' => 'CHARACTER_MAXIMUM_LENGTH',
|
||||
'NCHAR' => 'CHARACTER_OCTET_LENGTH',
|
||||
'NVARCHAR' => 'CHARACTER_OCTET_LENGTH',
|
||||
'VARCHAR' => 'CHARACTER_OCTET_LENGTH',
|
||||
);
|
||||
|
||||
if (isset($size_cols[$type])) {
|
||||
@@ -122,7 +122,7 @@ class DibiMsSqlReflector extends DibiObject implements IDibiReflector
|
||||
'unsigned' => NULL,
|
||||
'nullable' => $row['IS_NULLABLE'] === 'YES',
|
||||
'default' => $row['COLUMN_DEFAULT'],
|
||||
'autoincrement' => false,
|
||||
'autoincrement' => FALSE,
|
||||
'vendor' => $row,
|
||||
);
|
||||
}
|
||||
@@ -161,8 +161,8 @@ class DibiMsSqlReflector extends DibiObject implements IDibiReflector
|
||||
if (!isset($indexes[$index_name])) {
|
||||
$indexes[$index_name] = array();
|
||||
$indexes[$index_name]['name'] = $index_name;
|
||||
$indexes[$index_name]['unique'] = (bool)$row['is_unique'];
|
||||
$indexes[$index_name]['primary'] = (bool)$row['is_primary_key'];
|
||||
$indexes[$index_name]['unique'] = (bool) $row['is_unique'];
|
||||
$indexes[$index_name]['primary'] = (bool) $row['is_primary_key'];
|
||||
$indexes[$index_name]['columns'] = array();
|
||||
}
|
||||
$indexes[$index_name]['columns'][] = $row['column_name'];
|
||||
@@ -203,8 +203,8 @@ class DibiMsSqlReflector extends DibiObject implements IDibiReflector
|
||||
$keys[$key_name]['local'] = array($row['column_name']); // local columns
|
||||
$keys[$key_name]['table'] = $row['reference_table_name']; // referenced table
|
||||
$keys[$key_name]['foreign'] = array($row['reference_column_name']); // referenced columns
|
||||
$keys[$key_name]['onDelete'] = false;
|
||||
$keys[$key_name]['onUpdate'] = false;
|
||||
$keys[$key_name]['onDelete'] = FALSE;
|
||||
$keys[$key_name]['onUpdate'] = FALSE;
|
||||
} else {
|
||||
$keys[$key_name]['local'][] = $row['column_name']; // local columns
|
||||
$keys[$key_name]['foreign'][] = $row['reference_column_name']; // referenced columns
|
||||
|
@@ -2,7 +2,7 @@
|
||||
|
||||
/**
|
||||
* This file is part of the "dibi" - smart database abstraction layer.
|
||||
* Copyright (c) 2005 David Grudl (http://davidgrudl.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
|
||||
@@ -312,10 +312,11 @@ class DibiMySqlDriver extends DibiObject implements IDibiDriver, IDibiResultDriv
|
||||
return $value ? 1 : 0;
|
||||
|
||||
case dibi::DATE:
|
||||
return $value instanceof DateTime ? $value->format("'Y-m-d'") : date("'Y-m-d'", $value);
|
||||
|
||||
case dibi::DATETIME:
|
||||
return $value instanceof DateTime ? $value->format("'Y-m-d H:i:s'") : date("'Y-m-d H:i:s'", $value);
|
||||
if (!$value instanceof DateTime && !$value instanceof DateTimeInterface) {
|
||||
$value = new DibiDateTime($value);
|
||||
}
|
||||
return $value->format($type === dibi::DATETIME ? "'Y-m-d H:i:s'" : "'Y-m-d'");
|
||||
|
||||
default:
|
||||
throw new InvalidArgumentException('Unsupported type.');
|
||||
@@ -405,8 +406,8 @@ class DibiMySqlDriver extends DibiObject implements IDibiDriver, IDibiResultDriv
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @param int the 0-based cursor pos to seek to
|
||||
* @return bool TRUE on success, FALSE if unable to seek to specified record
|
||||
* @throws DibiException
|
||||
*/
|
||||
public function seek($row)
|
||||
|
@@ -2,7 +2,7 @@
|
||||
|
||||
/**
|
||||
* This file is part of the "dibi" - smart database abstraction layer.
|
||||
* Copyright (c) 2005 David Grudl (http://davidgrudl.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
|
||||
@@ -36,7 +36,7 @@ class DibiMySqlReflector extends DibiObject implements IDibiReflector
|
||||
FROM INFORMATION_SCHEMA.TABLES
|
||||
WHERE TABLE_SCHEMA = DATABASE()
|
||||
");*/
|
||||
$res = $this->driver->query("SHOW FULL TABLES");
|
||||
$res = $this->driver->query('SHOW FULL TABLES');
|
||||
$tables = array();
|
||||
while ($row = $res->fetch(FALSE)) {
|
||||
$tables[] = array(
|
||||
@@ -111,10 +111,40 @@ class DibiMySqlReflector extends DibiObject implements IDibiReflector
|
||||
* Returns metadata for all foreign keys in a table.
|
||||
* @param string
|
||||
* @return array
|
||||
* @throws DibiNotSupportedException
|
||||
*/
|
||||
public function getForeignKeys($table)
|
||||
{
|
||||
throw new DibiNotImplementedException;
|
||||
$data = $this->driver->query("SELECT `ENGINE` FROM information_schema.TABLES WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = {$this->driver->escape($table, dibi::TEXT)}")->fetch(TRUE);
|
||||
if ($data['ENGINE'] !== 'InnoDB') {
|
||||
throw new DibiNotSupportedException("Foreign keys are not supported in {$data['ENGINE']} tables.");
|
||||
}
|
||||
|
||||
$res = $this->driver->query("
|
||||
SELECT rc.CONSTRAINT_NAME, rc.UPDATE_RULE, rc.DELETE_RULE, kcu.REFERENCED_TABLE_NAME,
|
||||
GROUP_CONCAT(kcu.REFERENCED_COLUMN_NAME ORDER BY kcu.ORDINAL_POSITION) AS REFERENCED_COLUMNS,
|
||||
GROUP_CONCAT(kcu.COLUMN_NAME ORDER BY kcu.ORDINAL_POSITION) AS COLUMNS
|
||||
FROM information_schema.REFERENTIAL_CONSTRAINTS rc
|
||||
INNER JOIN information_schema.KEY_COLUMN_USAGE kcu ON
|
||||
kcu.CONSTRAINT_NAME = rc.CONSTRAINT_NAME
|
||||
AND kcu.CONSTRAINT_SCHEMA = rc.CONSTRAINT_SCHEMA
|
||||
WHERE rc.CONSTRAINT_SCHEMA = DATABASE()
|
||||
AND rc.TABLE_NAME = {$this->driver->escape($table, dibi::TEXT)}
|
||||
GROUP BY rc.CONSTRAINT_NAME
|
||||
");
|
||||
|
||||
$foreignKeys = array();
|
||||
while ($row = $res->fetch(TRUE)) {
|
||||
$keyName = $row['CONSTRAINT_NAME'];
|
||||
|
||||
$foreignKeys[$keyName]['name'] = $keyName;
|
||||
$foreignKeys[$keyName]['local'] = explode(',', $row['COLUMNS']);
|
||||
$foreignKeys[$keyName]['table'] = $row['REFERENCED_TABLE_NAME'];
|
||||
$foreignKeys[$keyName]['foreign'] = explode(',', $row['REFERENCED_COLUMNS']);
|
||||
$foreignKeys[$keyName]['onDelete'] = $row['DELETE_RULE'];
|
||||
$foreignKeys[$keyName]['onUpdate'] = $row['UPDATE_RULE'];
|
||||
}
|
||||
return array_values($foreignKeys);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -2,7 +2,7 @@
|
||||
|
||||
/**
|
||||
* This file is part of the "dibi" - smart database abstraction layer.
|
||||
* Copyright (c) 2005 David Grudl (http://davidgrudl.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
|
||||
@@ -115,12 +115,7 @@ class DibiMySqliDriver extends DibiObject implements IDibiDriver, IDibiResultDri
|
||||
}
|
||||
|
||||
if (isset($config['charset'])) {
|
||||
$ok = FALSE;
|
||||
if (version_compare(PHP_VERSION , '5.1.5', '>=')) {
|
||||
// affects the character set used by mysql_real_escape_string() (was added in MySQL 5.0.7 and PHP 5.0.5, fixed in PHP 5.1.5)
|
||||
$ok = @mysqli_set_charset($this->connection, $config['charset']); // intentionally @
|
||||
}
|
||||
if (!$ok) {
|
||||
if (!@mysqli_set_charset($this->connection, $config['charset'])) {
|
||||
$this->query("SET NAMES '$config[charset]'");
|
||||
}
|
||||
}
|
||||
@@ -300,10 +295,11 @@ class DibiMySqliDriver extends DibiObject implements IDibiDriver, IDibiResultDri
|
||||
return $value ? 1 : 0;
|
||||
|
||||
case dibi::DATE:
|
||||
return $value instanceof DateTime ? $value->format("'Y-m-d'") : date("'Y-m-d'", $value);
|
||||
|
||||
case dibi::DATETIME:
|
||||
return $value instanceof DateTime ? $value->format("'Y-m-d H:i:s'") : date("'Y-m-d H:i:s'", $value);
|
||||
if (!$value instanceof DateTime && !$value instanceof DateTimeInterface) {
|
||||
$value = new DibiDateTime($value);
|
||||
}
|
||||
return $value->format($type === dibi::DATETIME ? "'Y-m-d H:i:s'" : "'Y-m-d'");
|
||||
|
||||
default:
|
||||
throw new InvalidArgumentException('Unsupported type.');
|
||||
@@ -393,8 +389,8 @@ class DibiMySqliDriver extends DibiObject implements IDibiDriver, IDibiResultDri
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @param int the 0-based cursor pos to seek to
|
||||
* @return bool TRUE on success, FALSE if unable to seek to specified record
|
||||
* @throws DibiException
|
||||
*/
|
||||
public function seek($row)
|
||||
|
@@ -2,7 +2,7 @@
|
||||
|
||||
/**
|
||||
* This file is part of the "dibi" - smart database abstraction layer.
|
||||
* Copyright (c) 2005 David Grudl (http://davidgrudl.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
|
||||
@@ -241,10 +241,11 @@ class DibiOdbcDriver extends DibiObject implements IDibiDriver, IDibiResultDrive
|
||||
return $value ? 1 : 0;
|
||||
|
||||
case dibi::DATE:
|
||||
return $value instanceof DateTime ? $value->format("#m/d/Y#") : date("#m/d/Y#", $value);
|
||||
|
||||
case dibi::DATETIME:
|
||||
return $value instanceof DateTime ? $value->format("#m/d/Y H:i:s#") : date("#m/d/Y H:i:s#", $value);
|
||||
if (!$value instanceof DateTime && !$value instanceof DateTimeInterface) {
|
||||
$value = new DibiDateTime($value);
|
||||
}
|
||||
return $value->format($type === dibi::DATETIME ? "#m/d/Y H:i:s#" : "#m/d/Y#");
|
||||
|
||||
default:
|
||||
throw new InvalidArgumentException('Unsupported type.');
|
||||
@@ -338,7 +339,9 @@ class DibiOdbcDriver extends DibiObject implements IDibiDriver, IDibiResultDrive
|
||||
}
|
||||
$count = odbc_num_fields($set);
|
||||
$cols = array();
|
||||
for ($i = 1; $i <= $count; $i++) $cols[] = odbc_result($set, $i);
|
||||
for ($i = 1; $i <= $count; $i++) {
|
||||
$cols[] = odbc_result($set, $i);
|
||||
}
|
||||
return $cols;
|
||||
}
|
||||
}
|
||||
@@ -346,8 +349,8 @@ class DibiOdbcDriver extends DibiObject implements IDibiDriver, IDibiResultDrive
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @param int the 0-based cursor pos to seek to
|
||||
* @return bool TRUE on success, FALSE if unable to seek to specified record
|
||||
*/
|
||||
public function seek($row)
|
||||
{
|
||||
@@ -377,10 +380,10 @@ class DibiOdbcDriver extends DibiObject implements IDibiDriver, IDibiResultDrive
|
||||
$columns = array();
|
||||
for ($i = 1; $i <= $count; $i++) {
|
||||
$columns[] = array(
|
||||
'name' => odbc_field_name($this->resultSet, $i),
|
||||
'table' => NULL,
|
||||
'fullname' => odbc_field_name($this->resultSet, $i),
|
||||
'nativetype'=> odbc_field_type($this->resultSet, $i),
|
||||
'name' => odbc_field_name($this->resultSet, $i),
|
||||
'table' => NULL,
|
||||
'fullname' => odbc_field_name($this->resultSet, $i),
|
||||
'nativetype' => odbc_field_type($this->resultSet, $i),
|
||||
);
|
||||
}
|
||||
return $columns;
|
||||
|
@@ -2,7 +2,7 @@
|
||||
|
||||
/**
|
||||
* This file is part of the "dibi" - smart database abstraction layer.
|
||||
* Copyright (c) 2005 David Grudl (http://davidgrudl.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
|
||||
@@ -235,10 +235,11 @@ class DibiOracleDriver extends DibiObject implements IDibiDriver, IDibiResultDri
|
||||
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);
|
||||
if (!$value instanceof DateTime && !$value instanceof DateTimeInterface) {
|
||||
$value = new DibiDateTime($value);
|
||||
}
|
||||
return $value->format($type === dibi::DATETIME ? $this->fmtDateTime : $this->fmtDate);
|
||||
|
||||
default:
|
||||
throw new InvalidArgumentException('Unsupported type.');
|
||||
@@ -330,8 +331,8 @@ class DibiOracleDriver extends DibiObject implements IDibiDriver, IDibiResultDri
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @param int the 0-based cursor pos to seek to
|
||||
* @return bool TRUE on success, FALSE if unable to seek to specified record
|
||||
*/
|
||||
public function seek($row)
|
||||
{
|
||||
|
@@ -2,7 +2,7 @@
|
||||
|
||||
/**
|
||||
* This file is part of the "dibi" - smart database abstraction layer.
|
||||
* Copyright (c) 2005 David Grudl (http://davidgrudl.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
|
||||
@@ -64,12 +64,13 @@ class DibiPdoDriver extends DibiObject implements IDibiDriver, IDibiResultDriver
|
||||
if ($config['resource'] instanceof PDO) {
|
||||
$this->connection = $config['resource'];
|
||||
|
||||
} else try {
|
||||
} else {
|
||||
try {
|
||||
$this->connection = new PDO($config['dsn'], $config['username'], $config['password'], $config['options']);
|
||||
|
||||
} catch (PDOException $e) {
|
||||
throw new DibiDriverException($e->getMessage(), $e->getCode());
|
||||
}
|
||||
}
|
||||
|
||||
if (!$this->connection) {
|
||||
throw new DibiDriverException('Connecting error.');
|
||||
@@ -278,10 +279,11 @@ class DibiPdoDriver extends DibiObject implements IDibiDriver, IDibiResultDriver
|
||||
return $this->connection->quote($value, PDO::PARAM_BOOL);
|
||||
|
||||
case dibi::DATE:
|
||||
return $value instanceof DateTime ? $value->format("'Y-m-d'") : date("'Y-m-d'", $value);
|
||||
|
||||
case dibi::DATETIME:
|
||||
return $value instanceof DateTime ? $value->format("'Y-m-d H:i:s'") : date("'Y-m-d H:i:s'", $value);
|
||||
if (!$value instanceof DateTime && !$value instanceof DateTimeInterface) {
|
||||
$value = new DibiDateTime($value);
|
||||
}
|
||||
return $value->format($type === dibi::DATETIME ? "'Y-m-d H:i:s'" : "'Y-m-d'");
|
||||
|
||||
default:
|
||||
throw new InvalidArgumentException('Unsupported type.');
|
||||
@@ -358,6 +360,7 @@ class DibiPdoDriver extends DibiObject implements IDibiDriver, IDibiResultDriver
|
||||
break;
|
||||
|
||||
case 'odbc':
|
||||
case 'dblib':
|
||||
case 'mssql':
|
||||
case 'sqlsrv':
|
||||
if ($offset < 1) {
|
||||
@@ -399,7 +402,7 @@ class DibiPdoDriver extends DibiObject implements IDibiDriver, IDibiResultDriver
|
||||
/**
|
||||
* 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
|
||||
* @return bool TRUE on success, FALSE if unable to seek to specified record
|
||||
*/
|
||||
public function seek($row)
|
||||
{
|
||||
|
@@ -2,7 +2,7 @@
|
||||
|
||||
/**
|
||||
* This file is part of the "dibi" - smart database abstraction layer.
|
||||
* Copyright (c) 2005 David Grudl (http://davidgrudl.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
|
||||
@@ -35,9 +35,6 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver, IDibiResultDr
|
||||
/** @var int|FALSE Affected rows */
|
||||
private $affectedRows = FALSE;
|
||||
|
||||
/** @var bool Escape method */
|
||||
private $escMethod = FALSE;
|
||||
|
||||
|
||||
/**
|
||||
* @throws DibiNotSupportedException
|
||||
@@ -103,8 +100,6 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver, IDibiResultDr
|
||||
if (isset($config['schema'])) {
|
||||
$this->query('SET search_path TO "' . $config['schema'] . '"');
|
||||
}
|
||||
|
||||
$this->escMethod = version_compare(PHP_VERSION , '5.2.0', '>=');
|
||||
}
|
||||
|
||||
|
||||
@@ -159,7 +154,7 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver, IDibiResultDr
|
||||
{
|
||||
if ($sequence === NULL) {
|
||||
// PostgreSQL 8.1 is needed
|
||||
$res = $this->query("SELECT LASTVAL()");
|
||||
$res = $this->query('SELECT LASTVAL()');
|
||||
} else {
|
||||
$res = $this->query("SELECT CURRVAL('$sequence')");
|
||||
}
|
||||
@@ -266,24 +261,16 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver, IDibiResultDr
|
||||
{
|
||||
switch ($type) {
|
||||
case dibi::TEXT:
|
||||
if ($this->escMethod) {
|
||||
if (!is_resource($this->connection)) {
|
||||
throw new DibiException('Lost connection to server.');
|
||||
}
|
||||
return "'" . pg_escape_string($this->connection, $value) . "'";
|
||||
} else {
|
||||
return "'" . pg_escape_string($value) . "'";
|
||||
if (!is_resource($this->connection)) {
|
||||
throw new DibiException('Lost connection to server.');
|
||||
}
|
||||
return "'" . pg_escape_string($this->connection, $value) . "'";
|
||||
|
||||
case dibi::BINARY:
|
||||
if ($this->escMethod) {
|
||||
if (!is_resource($this->connection)) {
|
||||
throw new DibiException('Lost connection to server.');
|
||||
}
|
||||
return "'" . pg_escape_bytea($this->connection, $value) . "'";
|
||||
} else {
|
||||
return "'" . pg_escape_bytea($value) . "'";
|
||||
if (!is_resource($this->connection)) {
|
||||
throw new DibiException('Lost connection to server.');
|
||||
}
|
||||
return "'" . pg_escape_bytea($this->connection, $value) . "'";
|
||||
|
||||
case dibi::IDENTIFIER:
|
||||
// @see http://www.postgresql.org/docs/8.2/static/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS
|
||||
@@ -293,10 +280,11 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver, IDibiResultDr
|
||||
return $value ? 'TRUE' : 'FALSE';
|
||||
|
||||
case dibi::DATE:
|
||||
return $value instanceof DateTime ? $value->format("'Y-m-d'") : date("'Y-m-d'", $value);
|
||||
|
||||
case dibi::DATETIME:
|
||||
return $value instanceof DateTime ? $value->format("'Y-m-d H:i:s'") : date("'Y-m-d H:i:s'", $value);
|
||||
if (!$value instanceof DateTime && !$value instanceof DateTimeInterface) {
|
||||
$value = new DibiDateTime($value);
|
||||
}
|
||||
return $value->format($type === dibi::DATETIME ? "'Y-m-d H:i:s'" : "'Y-m-d'");
|
||||
|
||||
default:
|
||||
throw new InvalidArgumentException('Unsupported type.');
|
||||
@@ -312,13 +300,9 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver, IDibiResultDr
|
||||
*/
|
||||
public function escapeLike($value, $pos)
|
||||
{
|
||||
if ($this->escMethod) {
|
||||
$value = pg_escape_string($this->connection, $value);
|
||||
} else {
|
||||
$value = pg_escape_string($value);
|
||||
}
|
||||
|
||||
$value = strtr($value, array( '%' => '\\\\%', '_' => '\\\\_'));
|
||||
$bs = pg_escape_string($this->connection, '\\'); // standard_conforming_strings = on/off
|
||||
$value = pg_escape_string($this->connection, $value);
|
||||
$value = strtr($value, array('%' => $bs . '%', '_' => $bs . '_', '\\' => '\\\\'));
|
||||
return ($pos <= 0 ? "'%" : "'") . $value . ($pos >= 0 ? "%'" : "'");
|
||||
}
|
||||
|
||||
@@ -392,7 +376,7 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver, IDibiResultDr
|
||||
/**
|
||||
* 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
|
||||
* @return bool TRUE on success, FALSE if unable to seek to specified record
|
||||
*/
|
||||
public function seek($row)
|
||||
{
|
||||
@@ -417,13 +401,12 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver, IDibiResultDr
|
||||
*/
|
||||
public function getResultColumns()
|
||||
{
|
||||
$hasTable = version_compare(PHP_VERSION , '5.2.0', '>=');
|
||||
$count = pg_num_fields($this->resultSet);
|
||||
$columns = array();
|
||||
for ($i = 0; $i < $count; $i++) {
|
||||
$row = array(
|
||||
'name' => pg_field_name($this->resultSet, $i),
|
||||
'table' => $hasTable ? pg_field_table($this->resultSet, $i) : NULL,
|
||||
'table' => pg_field_table($this->resultSet, $i),
|
||||
'nativetype'=> pg_field_type($this->resultSet, $i),
|
||||
);
|
||||
$row['fullname'] = $row['table'] ? $row['table'] . '.' . $row['name'] : $row['name'];
|
||||
@@ -458,7 +441,7 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver, IDibiResultDr
|
||||
throw new DibiDriverException('Reflection requires PostgreSQL 7.4 and newer.');
|
||||
}
|
||||
|
||||
$res = $this->query("
|
||||
$query = "
|
||||
SELECT
|
||||
table_name AS name,
|
||||
CASE table_type
|
||||
@@ -468,8 +451,20 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver, IDibiResultDr
|
||||
FROM
|
||||
information_schema.tables
|
||||
WHERE
|
||||
table_schema = current_schema()
|
||||
");
|
||||
table_schema = ANY (current_schemas(false))";
|
||||
|
||||
if ($version >= 9.3) {
|
||||
$query .= '
|
||||
UNION ALL
|
||||
SELECT
|
||||
matviewname, 1
|
||||
FROM
|
||||
pg_matviews
|
||||
WHERE
|
||||
schemaname = ANY (current_schemas(false))';
|
||||
}
|
||||
|
||||
$res = $this->query($query);
|
||||
$tables = pg_fetch_all($res->resultSet);
|
||||
return $tables ? $tables : array();
|
||||
}
|
||||
@@ -487,16 +482,43 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver, IDibiResultDr
|
||||
SELECT indkey
|
||||
FROM pg_class
|
||||
LEFT JOIN pg_index on pg_class.oid = pg_index.indrelid AND pg_index.indisprimary
|
||||
WHERE pg_class.relname = $_table
|
||||
WHERE pg_class.oid = $_table::regclass
|
||||
");
|
||||
$primary = (int) pg_fetch_object($res->resultSet)->indkey;
|
||||
|
||||
$res = $this->query("
|
||||
SELECT *
|
||||
FROM information_schema.columns
|
||||
WHERE table_name = $_table AND table_schema = current_schema()
|
||||
ORDER BY ordinal_position
|
||||
FROM information_schema.columns c
|
||||
JOIN pg_class ON pg_class.relname = c.table_name
|
||||
JOIN pg_namespace nsp ON nsp.oid = pg_class.relnamespace AND nsp.nspname = c.table_schema
|
||||
WHERE pg_class.oid = $_table::regclass
|
||||
ORDER BY c.ordinal_position
|
||||
");
|
||||
|
||||
if (!$res->getRowCount()) {
|
||||
$res = $this->query("
|
||||
SELECT
|
||||
a.attname AS column_name,
|
||||
pg_type.typname AS udt_name,
|
||||
a.attlen AS numeric_precision,
|
||||
a.atttypmod-4 AS character_maximum_length,
|
||||
NOT a.attnotnull AS is_nullable,
|
||||
a.attnum AS ordinal_position,
|
||||
adef.adsrc AS column_default
|
||||
FROM
|
||||
pg_attribute a
|
||||
JOIN pg_type ON a.atttypid = pg_type.oid
|
||||
JOIN pg_class cls ON a.attrelid = cls.oid
|
||||
LEFT JOIN pg_attrdef adef ON adef.adnum = a.attnum AND adef.adrelid = a.attrelid
|
||||
WHERE
|
||||
cls.relkind IN ('r', 'v', 'mv')
|
||||
AND a.attrelid = $_table::regclass
|
||||
AND a.attnum > 0
|
||||
AND NOT a.attisdropped
|
||||
ORDER BY ordinal_position
|
||||
");
|
||||
}
|
||||
|
||||
$columns = array();
|
||||
while ($row = $res->fetch(TRUE)) {
|
||||
$size = (int) max($row['character_maximum_length'], $row['numeric_precision']);
|
||||
@@ -504,8 +526,8 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver, IDibiResultDr
|
||||
'name' => $row['column_name'],
|
||||
'table' => $table,
|
||||
'nativetype' => strtoupper($row['udt_name']),
|
||||
'size' => $size ? $size : NULL,
|
||||
'nullable' => $row['is_nullable'] === 'YES',
|
||||
'size' => $size > 0 ? $size : NULL,
|
||||
'nullable' => $row['is_nullable'] === 'YES' || $row['is_nullable'] === 't',
|
||||
'default' => $row['column_default'],
|
||||
'autoincrement' => (int) $row['ordinal_position'] === $primary && substr($row['column_default'], 0, 7) === 'nextval',
|
||||
'vendor' => $row,
|
||||
@@ -524,9 +546,16 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver, IDibiResultDr
|
||||
{
|
||||
$_table = $this->escape($this->escape($table, dibi::IDENTIFIER), dibi::TEXT);
|
||||
$res = $this->query("
|
||||
SELECT ordinal_position, column_name
|
||||
FROM information_schema.columns
|
||||
WHERE table_name = $_table AND table_schema = current_schema()
|
||||
SELECT
|
||||
a.attnum AS ordinal_position,
|
||||
a.attname AS column_name
|
||||
FROM
|
||||
pg_attribute a
|
||||
JOIN pg_class cls ON a.attrelid = cls.oid
|
||||
WHERE
|
||||
a.attrelid = $_table::regclass
|
||||
AND a.attnum > 0
|
||||
AND NOT a.attisdropped
|
||||
ORDER BY ordinal_position
|
||||
");
|
||||
|
||||
@@ -540,7 +569,7 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver, IDibiResultDr
|
||||
FROM pg_class
|
||||
LEFT JOIN pg_index on pg_class.oid = pg_index.indrelid
|
||||
INNER JOIN pg_class as pg_class2 on pg_class2.oid = pg_index.indexrelid
|
||||
WHERE pg_class.relname = $_table
|
||||
WHERE pg_class.oid = $_table::regclass
|
||||
");
|
||||
|
||||
$indexes = array();
|
||||
|
@@ -2,7 +2,7 @@
|
||||
|
||||
/**
|
||||
* This file is part of the "dibi" - smart database abstraction layer.
|
||||
* Copyright (c) 2005 David Grudl (http://davidgrudl.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
|
||||
@@ -66,11 +66,12 @@ class DibiSqlite3Driver extends DibiObject implements IDibiDriver, IDibiResultDr
|
||||
|
||||
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());
|
||||
} 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'];
|
||||
@@ -82,7 +83,7 @@ class DibiSqlite3Driver extends DibiObject implements IDibiDriver, IDibiResultDr
|
||||
// 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");
|
||||
$this->query('PRAGMA foreign_keys = ON');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -234,10 +235,11 @@ class DibiSqlite3Driver extends DibiObject implements IDibiDriver, IDibiResultDr
|
||||
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);
|
||||
if (!$value instanceof DateTime && !$value instanceof DateTimeInterface) {
|
||||
$value = new DibiDateTime($value);
|
||||
}
|
||||
return $value->format($type === dibi::DATETIME ? $this->fmtDateTime : $this->fmtDate);
|
||||
|
||||
default:
|
||||
throw new InvalidArgumentException('Unsupported type.');
|
||||
@@ -335,8 +337,8 @@ class DibiSqlite3Driver extends DibiObject implements IDibiDriver, IDibiResultDr
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @param int the 0-based cursor pos to seek to
|
||||
* @return bool TRUE on success, FALSE if unable to seek to specified record
|
||||
* @throws DibiNotSupportedException
|
||||
*/
|
||||
public function seek($row)
|
||||
@@ -367,7 +369,7 @@ class DibiSqlite3Driver extends DibiObject implements IDibiDriver, IDibiResultDr
|
||||
static $types = array(SQLITE3_INTEGER => 'int', SQLITE3_FLOAT => 'float', SQLITE3_TEXT => 'text', SQLITE3_BLOB => 'blob', SQLITE3_NULL => 'null');
|
||||
for ($i = 0; $i < $count; $i++) {
|
||||
$columns[] = array(
|
||||
'name' => $this->resultSet->columnName($i),
|
||||
'name' => $this->resultSet->columnName($i),
|
||||
'table' => NULL,
|
||||
'fullname' => $this->resultSet->columnName($i),
|
||||
'nativetype' => $types[$this->resultSet->columnType($i)],
|
||||
|
@@ -2,7 +2,7 @@
|
||||
|
||||
/**
|
||||
* This file is part of the "dibi" - smart database abstraction layer.
|
||||
* Copyright (c) 2005 David Grudl (http://davidgrudl.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
|
||||
@@ -239,10 +239,11 @@ class DibiSqliteDriver extends DibiObject implements IDibiDriver, IDibiResultDri
|
||||
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);
|
||||
if (!$value instanceof DateTime && !$value instanceof DateTimeInterface) {
|
||||
$value = new DibiDateTime($value);
|
||||
}
|
||||
return $value->format($type === dibi::DATETIME ? $this->fmtDateTime : $this->fmtDate);
|
||||
|
||||
default:
|
||||
throw new InvalidArgumentException('Unsupported type.');
|
||||
@@ -331,8 +332,8 @@ class DibiSqliteDriver extends DibiObject implements IDibiDriver, IDibiResultDri
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @param int the 0-based cursor pos to seek to
|
||||
* @return bool TRUE on success, FALSE if unable to seek to specified record
|
||||
* @throws DibiException
|
||||
*/
|
||||
public function seek($row)
|
||||
@@ -366,7 +367,7 @@ class DibiSqliteDriver extends DibiObject implements IDibiDriver, IDibiResultDri
|
||||
$name = str_replace(array('[', ']'), '', sqlite_field_name($this->resultSet, $i));
|
||||
$pair = explode('.', $name);
|
||||
$columns[] = array(
|
||||
'name' => isset($pair[1]) ? $pair[1] : $pair[0],
|
||||
'name' => isset($pair[1]) ? $pair[1] : $pair[0],
|
||||
'table' => isset($pair[1]) ? $pair[0] : NULL,
|
||||
'fullname' => $name,
|
||||
'nativetype' => NULL,
|
||||
|
@@ -2,7 +2,7 @@
|
||||
|
||||
/**
|
||||
* This file is part of the "dibi" - smart database abstraction layer.
|
||||
* Copyright (c) 2005 David Grudl (http://davidgrudl.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
|
||||
|
543
dibi/libs/Dibi.php
Normal file
543
dibi/libs/Dibi.php
Normal file
@@ -0,0 +1,543 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the "dibi" - smart database abstraction layer.
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* This class is static container class for creating DB objects and
|
||||
* store connections info.
|
||||
*
|
||||
* @author David Grudl
|
||||
* @package dibi
|
||||
*/
|
||||
class dibi
|
||||
{
|
||||
/** column type */
|
||||
const TEXT = 's', // as 'string'
|
||||
BINARY = 'bin',
|
||||
BOOL = 'b',
|
||||
INTEGER = 'i',
|
||||
FLOAT = 'f',
|
||||
DATE = 'd',
|
||||
DATETIME = 't',
|
||||
TIME = 't';
|
||||
|
||||
const IDENTIFIER = 'n',
|
||||
AFFECTED_ROWS = 'a';
|
||||
|
||||
/** @deprecated */
|
||||
const FIELD_TEXT = self::TEXT,
|
||||
FIELD_BINARY = self::BINARY,
|
||||
FIELD_BOOL = self::BOOL,
|
||||
FIELD_INTEGER = self::INTEGER,
|
||||
FIELD_FLOAT = self::FLOAT,
|
||||
FIELD_DATE = self::DATE,
|
||||
FIELD_DATETIME = self::DATETIME,
|
||||
FIELD_TIME = self::TIME;
|
||||
|
||||
/** version */
|
||||
const VERSION = '2.2.4',
|
||||
REVISION = 'released on 2015-10-22';
|
||||
|
||||
/** sorting order */
|
||||
const ASC = 'ASC',
|
||||
DESC = 'DESC';
|
||||
|
||||
/** @var DibiConnection[] Connection registry storage for DibiConnection objects */
|
||||
private static $registry = array();
|
||||
|
||||
/** @var DibiConnection Current connection */
|
||||
private static $connection;
|
||||
|
||||
/** @var array @see addHandler */
|
||||
private static $handlers = array();
|
||||
|
||||
/** @var string Last SQL command @see dibi::query() */
|
||||
public static $sql;
|
||||
|
||||
/** @var int Elapsed time for last query */
|
||||
public static $elapsedTime;
|
||||
|
||||
/** @var int Elapsed time for all queries */
|
||||
public static $totalTime;
|
||||
|
||||
/** @var int Number or queries */
|
||||
public static $numOfQueries = 0;
|
||||
|
||||
/** @var string Default dibi driver */
|
||||
public static $defaultDriver = 'mysql';
|
||||
|
||||
|
||||
/**
|
||||
* Static class - cannot be instantiated.
|
||||
*/
|
||||
final public function __construct()
|
||||
{
|
||||
throw new LogicException('Cannot instantiate static class ' . get_class($this));
|
||||
}
|
||||
|
||||
|
||||
/********************* connections handling ****************d*g**/
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new DibiConnection object and connects it to specified database.
|
||||
* @param mixed connection parameters
|
||||
* @param string connection name
|
||||
* @return DibiConnection
|
||||
* @throws DibiException
|
||||
*/
|
||||
public static function connect($config = array(), $name = 0)
|
||||
{
|
||||
return self::$connection = self::$registry[$name] = new DibiConnection($config, $name);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Disconnects from database (doesn't destroy DibiConnection object).
|
||||
* @return void
|
||||
*/
|
||||
public static function disconnect()
|
||||
{
|
||||
self::getConnection()->disconnect();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns TRUE when connection was established.
|
||||
* @return bool
|
||||
*/
|
||||
public static function isConnected()
|
||||
{
|
||||
return (self::$connection !== NULL) && self::$connection->isConnected();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Retrieve active connection.
|
||||
* @param string connection registy name
|
||||
* @return DibiConnection
|
||||
* @throws DibiException
|
||||
*/
|
||||
public static function getConnection($name = NULL)
|
||||
{
|
||||
if ($name === NULL) {
|
||||
if (self::$connection === NULL) {
|
||||
throw new DibiException('Dibi is not connected to database.');
|
||||
}
|
||||
|
||||
return self::$connection;
|
||||
}
|
||||
|
||||
if (!isset(self::$registry[$name])) {
|
||||
throw new DibiException("There is no connection named '$name'.");
|
||||
}
|
||||
|
||||
return self::$registry[$name];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sets connection.
|
||||
* @param DibiConnection
|
||||
* @return DibiConnection
|
||||
*/
|
||||
public static function setConnection(DibiConnection $connection)
|
||||
{
|
||||
return self::$connection = $connection;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Change active connection.
|
||||
* @param string connection registy name
|
||||
* @return void
|
||||
* @throws DibiException
|
||||
*/
|
||||
public static function activate($name)
|
||||
{
|
||||
self::$connection = self::getConnection($name);
|
||||
}
|
||||
|
||||
|
||||
/********************* monostate for active connection ****************d*g**/
|
||||
|
||||
|
||||
/**
|
||||
* Generates and executes SQL query - Monostate for DibiConnection::query().
|
||||
* @param array|mixed one or more arguments
|
||||
* @return DibiResult|int result set object (if any)
|
||||
* @throws DibiException
|
||||
*/
|
||||
public static function query($args)
|
||||
{
|
||||
$args = func_get_args();
|
||||
return self::getConnection()->query($args);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Executes the SQL query - Monostate for DibiConnection::nativeQuery().
|
||||
* @param string SQL statement.
|
||||
* @return DibiResult|int result set object (if any)
|
||||
*/
|
||||
public static function nativeQuery($sql)
|
||||
{
|
||||
return self::getConnection()->nativeQuery($sql);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Generates and prints SQL query - Monostate for DibiConnection::test().
|
||||
* @param array|mixed one or more arguments
|
||||
* @return bool
|
||||
*/
|
||||
public static function test($args)
|
||||
{
|
||||
$args = func_get_args();
|
||||
return self::getConnection()->test($args);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Generates and returns SQL query as DibiDataSource - Monostate for DibiConnection::test().
|
||||
* @param array|mixed one or more arguments
|
||||
* @return DibiDataSource
|
||||
*/
|
||||
public static function dataSource($args)
|
||||
{
|
||||
$args = func_get_args();
|
||||
return self::getConnection()->dataSource($args);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Executes SQL query and fetch result - Monostate for DibiConnection::query() & fetch().
|
||||
* @param array|mixed one or more arguments
|
||||
* @return DibiRow
|
||||
* @throws DibiException
|
||||
*/
|
||||
public static function fetch($args)
|
||||
{
|
||||
$args = func_get_args();
|
||||
return self::getConnection()->query($args)->fetch();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Executes SQL query and fetch results - Monostate for DibiConnection::query() & fetchAll().
|
||||
* @param array|mixed one or more arguments
|
||||
* @return DibiRow[]
|
||||
* @throws DibiException
|
||||
*/
|
||||
public static function fetchAll($args)
|
||||
{
|
||||
$args = func_get_args();
|
||||
return self::getConnection()->query($args)->fetchAll();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Executes SQL query and fetch first column - Monostate for DibiConnection::query() & fetchSingle().
|
||||
* @param array|mixed one or more arguments
|
||||
* @return string
|
||||
* @throws DibiException
|
||||
*/
|
||||
public static function fetchSingle($args)
|
||||
{
|
||||
$args = func_get_args();
|
||||
return self::getConnection()->query($args)->fetchSingle();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Executes SQL query and fetch pairs - Monostate for DibiConnection::query() & fetchPairs().
|
||||
* @param array|mixed one or more arguments
|
||||
* @return string
|
||||
* @throws DibiException
|
||||
*/
|
||||
public static function fetchPairs($args)
|
||||
{
|
||||
$args = func_get_args();
|
||||
return self::getConnection()->query($args)->fetchPairs();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets the number of affected rows.
|
||||
* Monostate for DibiConnection::getAffectedRows()
|
||||
* @return int number of rows
|
||||
* @throws DibiException
|
||||
*/
|
||||
public static function getAffectedRows()
|
||||
{
|
||||
return self::getConnection()->getAffectedRows();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets the number of affected rows. Alias for getAffectedRows().
|
||||
* @return int number of rows
|
||||
* @throws DibiException
|
||||
*/
|
||||
public static function affectedRows()
|
||||
{
|
||||
return self::getConnection()->getAffectedRows();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query.
|
||||
* Monostate for DibiConnection::getInsertId()
|
||||
* @param string optional sequence name
|
||||
* @return int
|
||||
* @throws DibiException
|
||||
*/
|
||||
public static function getInsertId($sequence = NULL)
|
||||
{
|
||||
return self::getConnection()->getInsertId($sequence);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Retrieves the ID generated for an AUTO_INCREMENT column. Alias for getInsertId().
|
||||
* @param string optional sequence name
|
||||
* @return int
|
||||
* @throws DibiException
|
||||
*/
|
||||
public static function insertId($sequence = NULL)
|
||||
{
|
||||
return self::getConnection()->getInsertId($sequence);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Begins a transaction - Monostate for DibiConnection::begin().
|
||||
* @param string optional savepoint name
|
||||
* @return void
|
||||
* @throws DibiException
|
||||
*/
|
||||
public static function begin($savepoint = NULL)
|
||||
{
|
||||
self::getConnection()->begin($savepoint);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Commits statements in a transaction - Monostate for DibiConnection::commit($savepoint = NULL).
|
||||
* @param string optional savepoint name
|
||||
* @return void
|
||||
* @throws DibiException
|
||||
*/
|
||||
public static function commit($savepoint = NULL)
|
||||
{
|
||||
self::getConnection()->commit($savepoint);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Rollback changes in a transaction - Monostate for DibiConnection::rollback().
|
||||
* @param string optional savepoint name
|
||||
* @return void
|
||||
* @throws DibiException
|
||||
*/
|
||||
public static function rollback($savepoint = NULL)
|
||||
{
|
||||
self::getConnection()->rollback($savepoint);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets a information about the current database - Monostate for DibiConnection::getDatabaseInfo().
|
||||
* @return DibiDatabaseInfo
|
||||
*/
|
||||
public static function getDatabaseInfo()
|
||||
{
|
||||
return self::getConnection()->getDatabaseInfo();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Import SQL dump from file - extreme fast!
|
||||
* @param string filename
|
||||
* @return int count of sql commands
|
||||
*/
|
||||
public static function loadFile($file)
|
||||
{
|
||||
return self::getConnection()->loadFile($file);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Replacement for majority of dibi::methods() in future.
|
||||
*/
|
||||
public static function __callStatic($name, $args)
|
||||
{
|
||||
//if ($name = 'select', 'update', ...') {
|
||||
// return self::command()->$name($args);
|
||||
//}
|
||||
return call_user_func_array(array(self::getConnection(), $name), $args);
|
||||
}
|
||||
|
||||
|
||||
/********************* fluent SQL builders ****************d*g**/
|
||||
|
||||
|
||||
/**
|
||||
* @return DibiFluent
|
||||
*/
|
||||
public static function command()
|
||||
{
|
||||
return self::getConnection()->command();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string column name
|
||||
* @return DibiFluent
|
||||
*/
|
||||
public static function select($args)
|
||||
{
|
||||
$args = func_get_args();
|
||||
return call_user_func_array(array(self::getConnection(), 'select'), $args);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string table
|
||||
* @param array
|
||||
* @return DibiFluent
|
||||
*/
|
||||
public static function update($table, $args)
|
||||
{
|
||||
return self::getConnection()->update($table, $args);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string table
|
||||
* @param array
|
||||
* @return DibiFluent
|
||||
*/
|
||||
public static function insert($table, $args)
|
||||
{
|
||||
return self::getConnection()->insert($table, $args);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string table
|
||||
* @return DibiFluent
|
||||
*/
|
||||
public static function delete($table)
|
||||
{
|
||||
return self::getConnection()->delete($table);
|
||||
}
|
||||
|
||||
|
||||
/********************* substitutions ****************d*g**/
|
||||
|
||||
|
||||
/**
|
||||
* Returns substitution hashmap - Monostate for DibiConnection::getSubstitutes().
|
||||
* @return DibiHashMap
|
||||
*/
|
||||
public static function getSubstitutes()
|
||||
{
|
||||
return self::getConnection()->getSubstitutes();
|
||||
}
|
||||
|
||||
|
||||
/********************* misc tools ****************d*g**/
|
||||
|
||||
|
||||
/**
|
||||
* Prints out a syntax highlighted version of the SQL command or DibiResult.
|
||||
* @param string|DibiResult
|
||||
* @param bool return output instead of printing it?
|
||||
* @return string
|
||||
*/
|
||||
public static function dump($sql = NULL, $return = FALSE)
|
||||
{
|
||||
ob_start();
|
||||
if ($sql instanceof DibiResult) {
|
||||
$sql->dump();
|
||||
|
||||
} else {
|
||||
if ($sql === NULL) {
|
||||
$sql = self::$sql;
|
||||
}
|
||||
|
||||
static $keywords1 = 'SELECT|(?:ON\s+DUPLICATE\s+KEY)?UPDATE|INSERT(?:\s+INTO)?|REPLACE(?:\s+INTO)?|DELETE|CALL|UNION|FROM|WHERE|HAVING|GROUP\s+BY|ORDER\s+BY|LIMIT|OFFSET|SET|VALUES|LEFT\s+JOIN|INNER\s+JOIN|TRUNCATE';
|
||||
static $keywords2 = 'ALL|DISTINCT|DISTINCTROW|IGNORE|AS|USING|ON|AND|OR|IN|IS|NOT|NULL|LIKE|RLIKE|REGEXP|TRUE|FALSE';
|
||||
|
||||
// insert new lines
|
||||
$sql = " $sql ";
|
||||
$sql = preg_replace("#(?<=[\\s,(])($keywords1)(?=[\\s,)])#i", "\n\$1", $sql);
|
||||
|
||||
// reduce spaces
|
||||
$sql = preg_replace('#[ \t]{2,}#', ' ', $sql);
|
||||
|
||||
$sql = wordwrap($sql, 100);
|
||||
$sql = preg_replace("#([ \t]*\r?\n){2,}#", "\n", $sql);
|
||||
|
||||
// syntax highlight
|
||||
$highlighter = "#(/\\*.+?\\*/)|(\\*\\*.+?\\*\\*)|(?<=[\\s,(])($keywords1)(?=[\\s,)])|(?<=[\\s,(=])($keywords2)(?=[\\s,)=])#is";
|
||||
if (PHP_SAPI === 'cli') {
|
||||
if (substr(getenv('TERM'), 0, 5) === 'xterm') {
|
||||
$sql = preg_replace_callback($highlighter, array('dibi', 'cliHighlightCallback'), $sql);
|
||||
}
|
||||
echo trim($sql) . "\n\n";
|
||||
|
||||
} else {
|
||||
$sql = htmlSpecialChars($sql);
|
||||
$sql = preg_replace_callback($highlighter, array('dibi', 'highlightCallback'), $sql);
|
||||
echo '<pre class="dump">', trim($sql), "</pre>\n\n";
|
||||
}
|
||||
}
|
||||
|
||||
if ($return) {
|
||||
return ob_get_clean();
|
||||
} else {
|
||||
ob_end_flush();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static function highlightCallback($matches)
|
||||
{
|
||||
if (!empty($matches[1])) { // comment
|
||||
return '<em style="color:gray">' . $matches[1] . '</em>';
|
||||
|
||||
} elseif (!empty($matches[2])) { // error
|
||||
return '<strong style="color:red">' . $matches[2] . '</strong>';
|
||||
|
||||
} elseif (!empty($matches[3])) { // most important keywords
|
||||
return '<strong style="color:blue">' . $matches[3] . '</strong>';
|
||||
|
||||
} elseif (!empty($matches[4])) { // other keywords
|
||||
return '<strong style="color:green">' . $matches[4] . '</strong>';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static function cliHighlightCallback($matches)
|
||||
{
|
||||
if (!empty($matches[1])) { // comment
|
||||
return "\033[1;30m" . $matches[1] . "\033[0m";
|
||||
|
||||
} elseif (!empty($matches[2])) { // error
|
||||
return "\033[1;31m" . $matches[2] . "\033[0m";
|
||||
|
||||
} elseif (!empty($matches[3])) { // most important keywords
|
||||
return "\033[1;34m" . $matches[3] . "\033[0m";
|
||||
|
||||
} elseif (!empty($matches[4])) { // other keywords
|
||||
return "\033[1;32m" . $matches[4] . "\033[0m";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -2,7 +2,7 @@
|
||||
|
||||
/**
|
||||
* This file is part of the "dibi" - smart database abstraction layer.
|
||||
* Copyright (c) 2005 David Grudl (http://davidgrudl.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
|
||||
@@ -12,16 +12,12 @@
|
||||
* @author David Grudl
|
||||
* @package dibi
|
||||
*
|
||||
* @property-read bool $connected
|
||||
* @property-read mixed $config
|
||||
* @property-read IDibiDriver $driver
|
||||
* @property-read int $affectedRows
|
||||
* @property-read int $insertId
|
||||
* @property-read DibiDatabaseInfo $databaseInfo
|
||||
*/
|
||||
class DibiConnection extends DibiObject
|
||||
{
|
||||
/** @var array of function(DibiEvent $event); Occurs after query is executed */
|
||||
/** @var array of function (DibiEvent $event); Occurs after query is executed */
|
||||
public $onEvent;
|
||||
|
||||
/** @var array Current connection configuration */
|
||||
@@ -56,9 +52,6 @@ class DibiConnection extends DibiObject
|
||||
*/
|
||||
public function __construct($config, $name = NULL)
|
||||
{
|
||||
class_exists('dibi'); // ensure class dibi is loaded
|
||||
|
||||
// DSN string
|
||||
if (is_string($config)) {
|
||||
parse_str($config, $config);
|
||||
|
||||
@@ -85,7 +78,7 @@ class DibiConnection extends DibiObject
|
||||
|
||||
$class = preg_replace(array('#\W#', '#sql#'), array('_', 'Sql'), ucfirst(strtolower($config['driver'])));
|
||||
$class = "Dibi{$class}Driver";
|
||||
if (!class_exists($class, FALSE)) {
|
||||
if (!class_exists($class)) {
|
||||
include_once dirname(__FILE__) . "/../drivers/$class.php";
|
||||
|
||||
if (!class_exists($class, FALSE)) {
|
||||
@@ -114,7 +107,7 @@ class DibiConnection extends DibiObject
|
||||
$this->onEvent[] = array(new DibiFirePhpLogger($filter), 'logEvent');
|
||||
}
|
||||
|
||||
if (class_exists('DibiNettePanel', FALSE)) {
|
||||
if (!interface_exists('Tracy\IBarPanel') && (interface_exists('Nette\Diagnostics\IBarPanel') || interface_exists('IBarPanel'))) {
|
||||
$panel = new DibiNettePanel(isset($profilerCfg['explain']) ? $profilerCfg['explain'] : TRUE, $filter);
|
||||
$panel->register($this);
|
||||
}
|
||||
@@ -641,14 +634,21 @@ class DibiConnection extends DibiObject
|
||||
}
|
||||
|
||||
$count = 0;
|
||||
$delimiter = ';';
|
||||
$sql = '';
|
||||
while (!feof($handle)) {
|
||||
$s = fgets($handle);
|
||||
$sql .= $s;
|
||||
if (substr(rtrim($s), -1) === ';') {
|
||||
$s = rtrim(fgets($handle));
|
||||
if (substr($s, 0, 10) === 'DELIMITER ') {
|
||||
$delimiter = substr($s, 10);
|
||||
|
||||
} elseif (substr($s, -strlen($delimiter)) === $delimiter) {
|
||||
$sql .= substr($s, 0, -strlen($delimiter));
|
||||
$this->driver->query($sql);
|
||||
$sql = '';
|
||||
$count++;
|
||||
|
||||
} else {
|
||||
$sql .= $s . "\n";
|
||||
}
|
||||
}
|
||||
if (trim($sql) !== '') {
|
||||
|
@@ -2,7 +2,7 @@
|
||||
|
||||
/**
|
||||
* This file is part of the "dibi" - smart database abstraction layer.
|
||||
* Copyright (c) 2005 David Grudl (http://davidgrudl.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
|
||||
@@ -11,11 +11,6 @@
|
||||
*
|
||||
* @author David Grudl
|
||||
* @package dibi
|
||||
*
|
||||
* @property-read DibiConnection $connection
|
||||
* @property-read DibiResult $result
|
||||
* @property-read DibiResultIterator $iterator
|
||||
* @property-read int $totalCount
|
||||
*/
|
||||
class DibiDataSource extends DibiObject implements IDataSource
|
||||
{
|
||||
|
@@ -2,7 +2,7 @@
|
||||
|
||||
/**
|
||||
* This file is part of the "dibi" - smart database abstraction layer.
|
||||
* Copyright (c) 2005 David Grudl (http://davidgrudl.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
|
||||
|
@@ -2,7 +2,7 @@
|
||||
|
||||
/**
|
||||
* This file is part of the "dibi" - smart database abstraction layer.
|
||||
* Copyright (c) 2005 David Grudl (http://davidgrudl.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
|
||||
@@ -18,9 +18,9 @@ class DibiDateTime extends DateTime
|
||||
public function __construct($time = 'now', DateTimeZone $timezone = NULL)
|
||||
{
|
||||
if (is_numeric($time)) {
|
||||
$time = date('Y-m-d H:i:s', $time);
|
||||
}
|
||||
if ($timezone === NULL) {
|
||||
parent::__construct('@' . $time);
|
||||
$this->setTimeZone($timezone ? $timezone : new DateTimeZone(date_default_timezone_get()));
|
||||
} elseif ($timezone === NULL) {
|
||||
parent::__construct($time);
|
||||
} else {
|
||||
parent::__construct($time, $timezone);
|
||||
@@ -42,29 +42,19 @@ class DibiDateTime extends DateTime
|
||||
}
|
||||
|
||||
|
||||
public function __sleep()
|
||||
public function setTimestamp($timestamp)
|
||||
{
|
||||
$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);
|
||||
$zone = PHP_VERSION_ID === 50206 ? new DateTimeZone($this->getTimezone()->getName()) : $this->getTimezone();
|
||||
$this->__construct('@' . $timestamp);
|
||||
$this->setTimeZone($zone);
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
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
|
||||
$ts = $this->format('U');
|
||||
return is_float($tmp = $ts * 1) ? $ts : $tmp;
|
||||
}
|
||||
|
||||
|
||||
@@ -73,4 +63,27 @@ class DibiDateTime extends DateTime
|
||||
return $this->format('Y-m-d H:i:s');
|
||||
}
|
||||
|
||||
|
||||
public function __sleep()
|
||||
{
|
||||
$zone = $this->getTimezone()->getName();
|
||||
if ($zone[0] === '+') {
|
||||
$this->fix = array($this->format('Y-m-d H:i:sP'));
|
||||
} else {
|
||||
$this->fix = array($this->format('Y-m-d H:i:s'), $zone);
|
||||
}
|
||||
return array('fix');
|
||||
}
|
||||
|
||||
|
||||
public function __wakeup()
|
||||
{
|
||||
if (isset($this->fix[1])) {
|
||||
$this->__construct($this->fix[0], new DateTimeZone($this->fix[1]));
|
||||
} else {
|
||||
$this->__construct($this->fix[0]);
|
||||
}
|
||||
unset($this->fix);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -2,7 +2,7 @@
|
||||
|
||||
/**
|
||||
* This file is part of the "dibi" - smart database abstraction layer.
|
||||
* Copyright (c) 2005 David Grudl (http://davidgrudl.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
|
||||
|
@@ -2,7 +2,7 @@
|
||||
|
||||
/**
|
||||
* This file is part of the "dibi" - smart database abstraction layer.
|
||||
* Copyright (c) 2005 David Grudl (http://davidgrudl.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
|
||||
@@ -117,8 +117,8 @@ class DibiDriverException extends DibiException
|
||||
* @author David Grudl
|
||||
* @package dibi
|
||||
*/
|
||||
class DibiPcreException extends Exception {
|
||||
|
||||
class DibiPcreException extends Exception
|
||||
{
|
||||
public function __construct($message = '%msg.')
|
||||
{
|
||||
static $messages = array(
|
||||
|
@@ -2,7 +2,7 @@
|
||||
|
||||
/**
|
||||
* This file is part of the "dibi" - smart database abstraction layer.
|
||||
* Copyright (c) 2005 David Grudl (http://davidgrudl.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
|
||||
@@ -58,7 +58,7 @@ class DibiFileLogger extends DibiObject
|
||||
);
|
||||
} else {
|
||||
fwrite($handle,
|
||||
"OK: " . $event->sql
|
||||
'OK: ' . $event->sql
|
||||
. ($event->count ? ";\n-- rows: " . $event->count : '')
|
||||
. "\n-- takes: " . sprintf('%0.3f ms', $event->time * 1000)
|
||||
. "\n-- source: " . implode(':', $event->source)
|
||||
|
@@ -2,7 +2,7 @@
|
||||
|
||||
/**
|
||||
* This file is part of the "dibi" - smart database abstraction layer.
|
||||
* Copyright (c) 2005 David Grudl (http://davidgrudl.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
|
||||
@@ -64,7 +64,7 @@ class DibiFirePhpLogger extends DibiObject
|
||||
sprintf('%0.3f', $event->time * 1000),
|
||||
strlen($event->sql) > self::$maxLength ? substr($event->sql, 0, self::$maxLength) . '...' : $event->sql,
|
||||
$event->result instanceof Exception ? 'ERROR' : (string) $event->count,
|
||||
$event->connection->getConfig('driver') . '/' . $event->connection->getConfig('name')
|
||||
$event->connection->getConfig('driver') . '/' . $event->connection->getConfig('name'),
|
||||
);
|
||||
|
||||
header('X-Wf-Protocol-dibi: http://meta.wildfirehq.org/Protocol/JsonStream/0.2');
|
||||
|
@@ -2,7 +2,7 @@
|
||||
|
||||
/**
|
||||
* This file is part of the "dibi" - smart database abstraction layer.
|
||||
* Copyright (c) 2005 David Grudl (http://davidgrudl.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
|
||||
@@ -12,9 +12,6 @@
|
||||
* @author David Grudl
|
||||
* @package dibi
|
||||
*
|
||||
* @property-read string $command
|
||||
* @property-read DibiConnection $connection
|
||||
* @property-read DibiResultIterator $iterator
|
||||
* @method DibiFluent select($field)
|
||||
* @method DibiFluent distinct()
|
||||
* @method DibiFluent from($table)
|
||||
@@ -24,6 +21,8 @@
|
||||
* @method DibiFluent orderBy($field)
|
||||
* @method DibiFluent limit(int $limit)
|
||||
* @method DibiFluent offset(int $offset)
|
||||
* @method DibiFluent leftJoin($table)
|
||||
* @method DibiFluent on($cond)
|
||||
*/
|
||||
class DibiFluent extends DibiObject implements IDataSource
|
||||
{
|
||||
@@ -203,15 +202,10 @@ class DibiFluent extends DibiObject implements IDataSource
|
||||
* @param string clause name
|
||||
* @return self
|
||||
*/
|
||||
public function clause($clause, $remove = FALSE)
|
||||
public function clause($clause)
|
||||
{
|
||||
$this->cursor = & $this->clauses[self::$normalizer->$clause];
|
||||
|
||||
if ($remove) { // deprecated, use removeClause
|
||||
trigger_error(__METHOD__ . '(..., TRUE) is deprecated; use removeClause() instead.', E_USER_NOTICE);
|
||||
$this->cursor = NULL;
|
||||
|
||||
} elseif ($this->cursor === NULL) {
|
||||
if ($this->cursor === NULL) {
|
||||
$this->cursor = array();
|
||||
}
|
||||
|
||||
@@ -305,7 +299,14 @@ class DibiFluent extends DibiObject implements IDataSource
|
||||
public function execute($return = NULL)
|
||||
{
|
||||
$res = $this->query($this->_export());
|
||||
return $return === dibi::IDENTIFIER ? $this->connection->getInsertId() : $res;
|
||||
switch ($return) {
|
||||
case dibi::IDENTIFIER:
|
||||
return $this->connection->getInsertId();
|
||||
case dibi::AFFECTED_ROWS:
|
||||
return $this->connection->getAffectedRows();
|
||||
default:
|
||||
return $res;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -315,11 +316,12 @@ class DibiFluent extends DibiObject implements IDataSource
|
||||
*/
|
||||
public function fetch()
|
||||
{
|
||||
if ($this->command === 'SELECT') {
|
||||
return $this->query($this->_export(NULL, array('%lmt', 1)))->fetch();
|
||||
} else {
|
||||
return $this->query($this->_export())->fetch();
|
||||
if ($this->command === 'SELECT' && !$this->clauses['LIMIT']) {
|
||||
$result = $this->query($this->limit(1)->_export())->fetch();
|
||||
$this->removeClause('LIMIT');
|
||||
return $result;
|
||||
}
|
||||
return $this->query($this->_export())->fetch();
|
||||
}
|
||||
|
||||
|
||||
@@ -329,11 +331,12 @@ class DibiFluent extends DibiObject implements IDataSource
|
||||
*/
|
||||
public function fetchSingle()
|
||||
{
|
||||
if ($this->command === 'SELECT') {
|
||||
return $this->query($this->_export(NULL, array('%lmt', 1)))->fetchSingle();
|
||||
} else {
|
||||
return $this->query($this->_export())->fetchSingle();
|
||||
if ($this->command === 'SELECT' && !$this->clauses['LIMIT']) {
|
||||
$result = $this->query($this->limit(1)->_export())->fetchSingle();
|
||||
$this->removeClause('LIMIT');
|
||||
return $result;
|
||||
}
|
||||
return $this->query($this->_export())->fetchSingle();
|
||||
}
|
||||
|
||||
|
||||
@@ -401,7 +404,7 @@ class DibiFluent extends DibiObject implements IDataSource
|
||||
public function count()
|
||||
{
|
||||
return (int) $this->query(array(
|
||||
'SELECT COUNT(*) FROM (%ex', $this->_export(), ') AS [data]'
|
||||
'SELECT COUNT(*) FROM (%ex', $this->_export(), ') [data]',
|
||||
))->fetchSingle();
|
||||
}
|
||||
|
||||
|
@@ -2,7 +2,7 @@
|
||||
|
||||
/**
|
||||
* This file is part of the "dibi" - smart database abstraction layer.
|
||||
* Copyright (c) 2005 David Grudl (http://davidgrudl.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
|
||||
|
@@ -2,7 +2,7 @@
|
||||
|
||||
/**
|
||||
* This file is part of the "dibi" - smart database abstraction layer.
|
||||
* Copyright (c) 2005 David Grudl (http://davidgrudl.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
|
||||
|
@@ -2,14 +2,14 @@
|
||||
|
||||
/**
|
||||
* This file is part of the "dibi" - smart database abstraction layer.
|
||||
* Copyright (c) 2005 David Grudl (http://davidgrudl.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* DibiObject is the ultimate ancestor of all instantiable classes.
|
||||
*
|
||||
* DibiObject is copy of Nette\Object from Nette Framework (http://nette.org).
|
||||
* DibiObject is copy of Nette\Object from Nette Framework (https://nette.org).
|
||||
*
|
||||
* It defines some handful methods and enhances object core of PHP:
|
||||
* - access to undeclared members throws exceptions
|
||||
@@ -39,7 +39,7 @@
|
||||
* Adding method to class (i.e. to all instances) works similar to JavaScript
|
||||
* prototype property. The syntax for adding a new method is:
|
||||
* <code>
|
||||
* MyClass::extensionMethod('newMethod', function(MyClass $obj, $arg, ...) { ... });
|
||||
* MyClass::extensionMethod('newMethod', function (MyClass $obj, $arg, ...) { ... });
|
||||
* $obj = new MyClass;
|
||||
* $obj->newMethod($x);
|
||||
* </code>
|
||||
@@ -209,8 +209,8 @@ abstract class DibiObject
|
||||
}
|
||||
|
||||
// property getter support
|
||||
$name[0] = $name[0] & "\xDF"; // case-sensitive checking, capitalize first character
|
||||
$m = 'get' . $name;
|
||||
$uname = ucfirst($name);
|
||||
$m = 'get' . $uname;
|
||||
if (self::hasAccessor($class, $m)) {
|
||||
// ampersands:
|
||||
// - uses & __get() because declaration should be forward compatible (e.g. with Nette\Web\Html)
|
||||
@@ -219,13 +219,12 @@ abstract class DibiObject
|
||||
return $val;
|
||||
}
|
||||
|
||||
$m = 'is' . $name;
|
||||
$m = 'is' . $uname;
|
||||
if (self::hasAccessor($class, $m)) {
|
||||
$val = $this->$m();
|
||||
return $val;
|
||||
}
|
||||
|
||||
$name = func_get_arg(0);
|
||||
throw new LogicException("Cannot read an undeclared property $class::\$$name.");
|
||||
}
|
||||
|
||||
@@ -246,20 +245,18 @@ abstract class DibiObject
|
||||
}
|
||||
|
||||
// property setter support
|
||||
$name[0] = $name[0] & "\xDF"; // case-sensitive checking, capitalize first character
|
||||
if (self::hasAccessor($class, 'get' . $name) || self::hasAccessor($class, 'is' . $name)) {
|
||||
$uname = ucfirst($name);
|
||||
if (self::hasAccessor($class, 'get' . $uname) || self::hasAccessor($class, 'is' . $uname)) {
|
||||
$m = 'set' . $name;
|
||||
if (self::hasAccessor($class, $m)) {
|
||||
$this->$m($value);
|
||||
return;
|
||||
|
||||
} else {
|
||||
$name = func_get_arg(0);
|
||||
throw new LogicException("Cannot assign to a read-only property $class::\$$name.");
|
||||
}
|
||||
}
|
||||
|
||||
$name = func_get_arg(0);
|
||||
throw new LogicException("Cannot assign to an undeclared property $class::\$$name.");
|
||||
}
|
||||
|
||||
@@ -271,8 +268,7 @@ abstract class DibiObject
|
||||
*/
|
||||
public function __isset($name)
|
||||
{
|
||||
$name[0] = $name[0] & "\xDF";
|
||||
return $name !== '' && self::hasAccessor(get_class($this), 'get' . $name);
|
||||
return $name !== '' && self::hasAccessor(get_class($this), 'get' . ucfirst($name));
|
||||
}
|
||||
|
||||
|
||||
|
@@ -2,7 +2,7 @@
|
||||
|
||||
/**
|
||||
* This file is part of the "dibi" - smart database abstraction layer.
|
||||
* Copyright (c) 2005 David Grudl (http://davidgrudl.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
|
||||
@@ -25,12 +25,7 @@
|
||||
* @author David Grudl
|
||||
* @package dibi
|
||||
*
|
||||
* @property-read mixed $resource
|
||||
* @property-read IDibiResultDriver $driver
|
||||
* @property-read int $rowCount
|
||||
* @property-read DibiResultIterator $iterator
|
||||
* @property string $rowClass
|
||||
* @property-read DibiResultInfo $info
|
||||
*/
|
||||
class DibiResult extends DibiObject implements IDataSource
|
||||
{
|
||||
@@ -109,7 +104,7 @@ class DibiResult extends DibiObject implements IDataSource
|
||||
/**
|
||||
* 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
|
||||
* @return bool TRUE on success, FALSE if unable to seek to specified record
|
||||
* @throws DibiException
|
||||
*/
|
||||
final public function seek($row)
|
||||
@@ -138,26 +133,12 @@ class DibiResult extends DibiObject implements IDataSource
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the number of rows in a result set. Alias for getRowCount().
|
||||
* @deprecated
|
||||
*/
|
||||
final public function rowCount()
|
||||
{
|
||||
trigger_error(__METHOD__ . '() is deprecated; use count($res) or $res->getRowCount() instead.', E_USER_WARNING);
|
||||
return $this->getResultDriver()->getRowCount();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Required by the IteratorAggregate interface.
|
||||
* @return DibiResultIterator
|
||||
*/
|
||||
final public function getIterator()
|
||||
{
|
||||
if (func_num_args()) {
|
||||
trigger_error(__METHOD__ . ' arguments $offset & $limit have been dropped; use SQL clauses instead.', E_USER_WARNING);
|
||||
}
|
||||
return new DibiResultIterator($this);
|
||||
}
|
||||
|
||||
@@ -202,7 +183,7 @@ class DibiResult extends DibiObject implements IDataSource
|
||||
/**
|
||||
* Fetches the row at current position, process optional type conversion.
|
||||
* and moves the internal cursor to the next position
|
||||
* @return DibiRow|FALSE array on success, FALSE if no next record
|
||||
* @return DibiRow|FALSE array on success, FALSE if no next record
|
||||
*/
|
||||
final public function fetch()
|
||||
{
|
||||
@@ -223,7 +204,7 @@ class DibiResult extends DibiObject implements IDataSource
|
||||
|
||||
/**
|
||||
* Like fetch(), but returns only first field.
|
||||
* @return mixed value on success, FALSE if no next record
|
||||
* @return mixed value on success, FALSE if no next record
|
||||
*/
|
||||
final public function fetchSingle()
|
||||
{
|
||||
@@ -317,16 +298,16 @@ class DibiResult extends DibiObject implements IDataSource
|
||||
$x = & $x[];
|
||||
|
||||
} elseif ($as === '=') { // "value" node
|
||||
$x = $row->{$assoc[$i+1]};
|
||||
$x = $row->{$assoc[$i + 1]};
|
||||
continue 2;
|
||||
|
||||
} elseif ($as === '->') { // "object" node
|
||||
if ($x === NULL) {
|
||||
$x = clone $row;
|
||||
$x = & $x->{$assoc[$i+1]};
|
||||
$x = & $x->{$assoc[$i + 1]};
|
||||
$x = NULL; // prepare child node
|
||||
} else {
|
||||
$x = & $x->{$assoc[$i+1]};
|
||||
$x = & $x->{$assoc[$i + 1]};
|
||||
}
|
||||
|
||||
} elseif ($as !== '|') { // associative-array node
|
||||
@@ -383,22 +364,21 @@ class DibiResult extends DibiObject implements IDataSource
|
||||
} elseif ($as === '=') { // "record" node
|
||||
if ($x === NULL) {
|
||||
$x = $row->toArray();
|
||||
$x = & $x[ $assoc[$i+1] ];
|
||||
$x = & $x[ $assoc[$i + 1] ];
|
||||
$x = NULL; // prepare child node
|
||||
} else {
|
||||
$x = & $x[ $assoc[$i+1] ];
|
||||
$x = & $x[ $assoc[$i + 1] ];
|
||||
}
|
||||
|
||||
} elseif ($as === '@') { // "object" node
|
||||
if ($x === NULL) {
|
||||
$x = clone $row;
|
||||
$x = & $x->{$assoc[$i+1]};
|
||||
$x = & $x->{$assoc[$i + 1]};
|
||||
$x = NULL; // prepare child node
|
||||
} else {
|
||||
$x = & $x->{$assoc[$i+1]};
|
||||
$x = & $x->{$assoc[$i + 1]};
|
||||
}
|
||||
|
||||
|
||||
} else { // associative-array node
|
||||
$x = & $x[$row->$as];
|
||||
}
|
||||
@@ -438,7 +418,7 @@ class DibiResult extends DibiObject implements IDataSource
|
||||
|
||||
if ($value === NULL) {
|
||||
if ($key !== NULL) {
|
||||
throw new InvalidArgumentException("Either none or both columns must be specified.");
|
||||
throw new InvalidArgumentException('Either none or both columns must be specified.');
|
||||
}
|
||||
|
||||
// autodetect
|
||||
@@ -492,7 +472,8 @@ class DibiResult extends DibiObject implements IDataSource
|
||||
foreach ($this->getResultDriver()->getResultColumns() as $col) {
|
||||
$this->types[$col['name']] = $cache->{$col['nativetype']};
|
||||
}
|
||||
} catch (DibiNotSupportedException $e) {}
|
||||
} catch (DibiNotSupportedException $e) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -511,29 +492,30 @@ class DibiResult extends DibiObject implements IDataSource
|
||||
if ($value === FALSE || $type === dibi::TEXT) {
|
||||
|
||||
} elseif ($type === dibi::INTEGER) {
|
||||
$row[$key] = is_float($tmp = $value * 1) ? $value : $tmp;
|
||||
$row[$key] = is_float($tmp = $value * 1)
|
||||
? (is_string($value) ? $value : (int) $value)
|
||||
: $tmp;
|
||||
|
||||
} elseif ($type === dibi::FLOAT) {
|
||||
$row[$key] = str_replace(',', '.', ltrim((string) ($tmp = (float) $value), '0')) === ltrim(rtrim(rtrim($value, '0'), '.'), '0') ? $tmp : $value;
|
||||
$value = ltrim($value, '0');
|
||||
$p = strpos($value, '.');
|
||||
if ($p !== FALSE) {
|
||||
$value = rtrim(rtrim($value, '0'), '.');
|
||||
}
|
||||
if ($value === '' || $value[0] === '.') {
|
||||
$value = '0' . $value;
|
||||
}
|
||||
$row[$key] = $value === str_replace(',', '.', (string) ($float = (float) $value))
|
||||
? $float
|
||||
: $value;
|
||||
|
||||
} elseif ($type === dibi::BOOL) {
|
||||
$row[$key] = ((bool) $value) && $value !== 'f' && $value !== 'F';
|
||||
|
||||
} elseif ($type === dibi::DATE || $type === dibi::DATETIME) {
|
||||
if ((int) $value === 0 && substr((string) $value, 0, 3) !== '00:') { // '', NULL, FALSE, '0000-00-00', ...
|
||||
|
||||
} elseif (empty($this->formats[$type])) { // return DateTime object (default)
|
||||
$row[$key] = new DibiDateTime(is_numeric($value) ? date('Y-m-d H:i:s', $value) : $value);
|
||||
|
||||
} elseif ($this->formats[$type] === 'U') { // return timestamp
|
||||
$row[$key] = is_numeric($value) ? (int) $value : strtotime($value);
|
||||
|
||||
} elseif (is_numeric($value)) { // formatted date
|
||||
$row[$key] = date($this->formats[$type], $value);
|
||||
|
||||
} else {
|
||||
if ((int) $value !== 0 || substr((string) $value, 0, 3) === '00:') { // '', NULL, FALSE, '0000-00-00', ...
|
||||
$value = new DibiDateTime($value);
|
||||
$row[$key] = $value->format($this->formats[$type]);
|
||||
$row[$key] = empty($this->formats[$type]) ? $value : $value->format($this->formats[$type]);
|
||||
}
|
||||
|
||||
} elseif ($type === dibi::BINARY) {
|
||||
@@ -614,14 +596,6 @@ class DibiResult extends DibiObject implements IDataSource
|
||||
}
|
||||
|
||||
|
||||
/** @deprecated */
|
||||
public function getColumnNames($fullNames = FALSE)
|
||||
{
|
||||
trigger_error(__METHOD__ . '() is deprecated; use $res->getInfo()->getColumnNames() instead.', E_USER_WARNING);
|
||||
return $this->getInfo()->getColumnNames($fullNames);
|
||||
}
|
||||
|
||||
|
||||
/********************* misc tools ****************d*g**/
|
||||
|
||||
|
||||
@@ -652,7 +626,7 @@ class DibiResult extends DibiObject implements IDataSource
|
||||
|
||||
foreach ($row as $col => $val) {
|
||||
$spaces = $maxLen - mb_strlen($col) + 2;
|
||||
echo "$col" . str_repeat(" ", $spaces) . "$val\n";
|
||||
echo "$col" . str_repeat(' ', $spaces) . "$val\n";
|
||||
}
|
||||
|
||||
echo "\n";
|
||||
|
@@ -2,7 +2,7 @@
|
||||
|
||||
/**
|
||||
* This file is part of the "dibi" - smart database abstraction layer.
|
||||
* Copyright (c) 2005 David Grudl (http://davidgrudl.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
|
||||
|
@@ -2,7 +2,7 @@
|
||||
|
||||
/**
|
||||
* This file is part of the "dibi" - smart database abstraction layer.
|
||||
* Copyright (c) 2005 David Grudl (http://davidgrudl.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
|
||||
@@ -42,48 +42,12 @@ class DibiRow implements ArrayAccess, IteratorAggregate, Countable
|
||||
if ((int) $time === 0 && substr((string) $time, 0, 3) !== '00:') { // '', NULL, FALSE, '0000-00-00', ...
|
||||
return NULL;
|
||||
}
|
||||
$time = new DibiDateTime(is_numeric($time) ? date('Y-m-d H:i:s', $time) : $time);
|
||||
$time = new DibiDateTime($time);
|
||||
}
|
||||
return $format === NULL ? $time : $time->format($format);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Converts value to UNIX timestamp.
|
||||
* @param string key
|
||||
* @return int
|
||||
*/
|
||||
public function asTimestamp($key)
|
||||
{
|
||||
trigger_error(__METHOD__ . '() is deprecated.', E_USER_WARNING);
|
||||
return $this->asDateTime($key, 'U');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Converts value to boolean.
|
||||
* @param string key
|
||||
* @return mixed
|
||||
*/
|
||||
public function asBool($key)
|
||||
{
|
||||
trigger_error(__METHOD__ . '() is deprecated.', E_USER_WARNING);
|
||||
return $this[$key];
|
||||
}
|
||||
|
||||
|
||||
/** @deprecated */
|
||||
public function asDate($key, $format = NULL)
|
||||
{
|
||||
trigger_error(__METHOD__ . '() is deprecated.', E_USER_WARNING);
|
||||
if ($format === NULL) {
|
||||
return $this->asTimestamp($key);
|
||||
} else {
|
||||
return $this->asDateTime($key, $format === TRUE ? NULL : $format);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/********************* interfaces ArrayAccess, Countable & IteratorAggregate ****************d*g**/
|
||||
|
||||
|
||||
|
@@ -2,7 +2,7 @@
|
||||
|
||||
/**
|
||||
* This file is part of the "dibi" - smart database abstraction layer.
|
||||
* Copyright (c) 2005 David Grudl (http://davidgrudl.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
|
||||
@@ -51,6 +51,7 @@ final class DibiTranslator extends DibiObject
|
||||
public function __construct(DibiConnection $connection)
|
||||
{
|
||||
$this->connection = $connection;
|
||||
$this->identifiers = new DibiHashMap(array($this, 'delimite'));
|
||||
}
|
||||
|
||||
|
||||
@@ -62,8 +63,9 @@ final class DibiTranslator extends DibiObject
|
||||
*/
|
||||
public function translate(array $args)
|
||||
{
|
||||
$this->identifiers = new DibiHashMap(array($this, 'delimite'));
|
||||
$this->driver = $this->connection->getDriver();
|
||||
if (!$this->driver) {
|
||||
$this->driver = $this->connection->getDriver();
|
||||
}
|
||||
|
||||
$args = array_values($args);
|
||||
while (count($args) === 1 && is_array($args[0])) { // implicit array expansion
|
||||
@@ -158,7 +160,7 @@ final class DibiTranslator extends DibiObject
|
||||
|
||||
|
||||
if ($comment) {
|
||||
$sql[] = "*/";
|
||||
$sql[] = '*/';
|
||||
}
|
||||
|
||||
$sql = implode(' ', $sql);
|
||||
@@ -185,7 +187,11 @@ final class DibiTranslator extends DibiObject
|
||||
public function formatValue($value, $modifier)
|
||||
{
|
||||
if ($this->comment) {
|
||||
return "...";
|
||||
return '...';
|
||||
}
|
||||
|
||||
if (!$this->driver) {
|
||||
$this->driver = $this->connection->getDriver();
|
||||
}
|
||||
|
||||
// array processing (with or without modifier)
|
||||
@@ -327,7 +333,7 @@ final class DibiTranslator extends DibiObject
|
||||
|
||||
// with modifier procession
|
||||
if ($modifier) {
|
||||
if ($value !== NULL && !is_scalar($value) && !($value instanceof DateTime)) { // array is already processed
|
||||
if ($value !== NULL && !is_scalar($value) && !$value instanceof DateTime && !$value instanceof DateTimeInterface) { // array is already processed
|
||||
$this->hasError = TRUE;
|
||||
return '**Unexpected type ' . gettype($value) . '**';
|
||||
}
|
||||
@@ -445,7 +451,7 @@ final class DibiTranslator extends DibiObject
|
||||
} elseif ($value === NULL) {
|
||||
return 'NULL';
|
||||
|
||||
} elseif ($value instanceof DateTime) {
|
||||
} elseif ($value instanceof DateTime || $value instanceof DateTimeInterface) {
|
||||
return $this->driver->escape($value, dibi::DATETIME);
|
||||
|
||||
} elseif ($value instanceof DibiLiteral) {
|
||||
@@ -483,7 +489,7 @@ final class DibiTranslator extends DibiObject
|
||||
|
||||
if ($cursor >= count($this->args)) {
|
||||
$this->hasError = TRUE;
|
||||
return "**Extra placeholder**";
|
||||
return '**Extra placeholder**';
|
||||
}
|
||||
|
||||
$cursor++;
|
||||
@@ -506,7 +512,7 @@ final class DibiTranslator extends DibiObject
|
||||
// open comment
|
||||
$this->ifLevelStart = $this->ifLevel;
|
||||
$this->comment = TRUE;
|
||||
return "/*";
|
||||
return '/*';
|
||||
}
|
||||
return '';
|
||||
|
||||
@@ -514,11 +520,11 @@ final class DibiTranslator extends DibiObject
|
||||
if ($this->ifLevelStart === $this->ifLevel) {
|
||||
$this->ifLevelStart = 0;
|
||||
$this->comment = FALSE;
|
||||
return "*/";
|
||||
return '*/';
|
||||
} elseif (!$this->comment) {
|
||||
$this->ifLevelStart = $this->ifLevel;
|
||||
$this->comment = TRUE;
|
||||
return "/*";
|
||||
return '/*';
|
||||
}
|
||||
|
||||
} elseif ($mod === 'end') {
|
||||
@@ -527,7 +533,7 @@ final class DibiTranslator extends DibiObject
|
||||
// close comment
|
||||
$this->ifLevelStart = 0;
|
||||
$this->comment = FALSE;
|
||||
return "*/";
|
||||
return '*/';
|
||||
}
|
||||
return '';
|
||||
|
||||
@@ -566,10 +572,10 @@ final class DibiTranslator extends DibiObject
|
||||
return $this->identifiers->{$matches[2]};
|
||||
|
||||
} elseif ($matches[3]) { // SQL strings: '...'
|
||||
return $this->driver->escape( str_replace("''", "'", $matches[4]), dibi::TEXT);
|
||||
return $this->driver->escape(str_replace("''", "'", $matches[4]), dibi::TEXT);
|
||||
|
||||
} elseif ($matches[5]) { // SQL strings: "..."
|
||||
return $this->driver->escape( str_replace('""', '"', $matches[6]), dibi::TEXT);
|
||||
return $this->driver->escape(str_replace('""', '"', $matches[6]), dibi::TEXT);
|
||||
|
||||
} elseif ($matches[7]) { // string quote
|
||||
$this->hasError = TRUE;
|
||||
|
@@ -2,7 +2,7 @@
|
||||
|
||||
/**
|
||||
* This file is part of the "dibi" - smart database abstraction layer.
|
||||
* Copyright (c) 2005 David Grudl (http://davidgrudl.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
|
||||
|
1
examples/.gitignore
vendored
1
examples/.gitignore
vendored
@@ -1,3 +1,4 @@
|
||||
_test.bat
|
||||
ref
|
||||
output
|
||||
log
|
||||
|
File diff suppressed because one or more lines are too long
@@ -1,60 +0,0 @@
|
||||
Licenses
|
||||
========
|
||||
|
||||
Good news! You may use Nette Framework under the terms of either
|
||||
the New BSD License or the GNU General Public License (GPL) version 2 or 3.
|
||||
|
||||
The BSD License is recommended for most projects. It is easy to understand and it
|
||||
places almost no restrictions on what you can do with the framework. If the GPL
|
||||
fits better to your project, you can use the framework under this license.
|
||||
|
||||
You don't have to notify anyone which license you are using. You can freely
|
||||
use Nette Framework in commercial projects as long as the copyright header
|
||||
remains intact.
|
||||
|
||||
Please be advised that the name "Nette Framework" is a protected trademark and its
|
||||
usage has some limitations. So please do not use word "Nette" in the name of your
|
||||
project or top-level domain, and choose a name that stands on its own merits.
|
||||
If your stuff is good, it will not take long to establish a reputation for yourselves.
|
||||
|
||||
|
||||
New BSD License
|
||||
---------------
|
||||
|
||||
Copyright (c) 2004, 2012 David Grudl (http://davidgrudl.com)
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification,
|
||||
are permitted provided that the following conditions are met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
* Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
* Neither the name of "Nette Framework" nor the names of its contributors
|
||||
may be used to endorse or promote products derived from this software
|
||||
without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
|
||||
GNU General Public License
|
||||
--------------------------
|
||||
|
||||
GPL licenses are very very long, so instead of including them here we offer
|
||||
you URLs with full text:
|
||||
|
||||
- GPL version 2: http://www.gnu.org/licenses/gpl-2.0.html
|
||||
- GPL version 3: http://www.gnu.org/licenses/gpl-3.0.html
|
@@ -1,3 +0,0 @@
|
||||
This file is part of Nette Framework
|
||||
|
||||
For more information please see http://nette.org
|
@@ -4,19 +4,17 @@
|
||||
|
||||
<?php
|
||||
|
||||
require dirname(__FILE__) . '/Nette/Debugger.php';
|
||||
require dirname(__FILE__) . '/../dibi/dibi.php';
|
||||
require __DIR__ . '/../dibi/dibi.php';
|
||||
|
||||
|
||||
// connects to SQlite using dibi class
|
||||
echo '<p>Connecting to Sqlite: ';
|
||||
try {
|
||||
dibi::connect(array(
|
||||
'driver' => 'sqlite3',
|
||||
'driver' => 'sqlite3',
|
||||
'database' => 'data/sample.s3db',
|
||||
));
|
||||
echo 'OK';
|
||||
|
||||
} catch (DibiException $e) {
|
||||
echo get_class($e), ': ', $e->getMessage(), "\n";
|
||||
}
|
||||
@@ -27,11 +25,10 @@ echo "</p>\n";
|
||||
echo '<p>Connecting to Sqlite: ';
|
||||
try {
|
||||
$connection = new DibiConnection(array(
|
||||
'driver' => 'sqlite3',
|
||||
'driver' => 'sqlite3',
|
||||
'database' => 'data/sample.s3db',
|
||||
));
|
||||
echo 'OK';
|
||||
|
||||
} catch (DibiException $e) {
|
||||
echo get_class($e), ': ', $e->getMessage(), "\n";
|
||||
}
|
||||
@@ -43,7 +40,6 @@ echo '<p>Connecting to MySQL: ';
|
||||
try {
|
||||
dibi::connect('driver=mysql&host=localhost&username=root&password=xxx&database=test&charset=cp1250');
|
||||
echo 'OK';
|
||||
|
||||
} catch (DibiException $e) {
|
||||
echo get_class($e), ': ', $e->getMessage(), "\n";
|
||||
}
|
||||
@@ -54,18 +50,17 @@ echo "</p>\n";
|
||||
echo '<p>Connecting to MySQLi: ';
|
||||
try {
|
||||
dibi::connect(array(
|
||||
'driver' => 'mysqli',
|
||||
'host' => 'localhost',
|
||||
'driver' => 'mysqli',
|
||||
'host' => 'localhost',
|
||||
'username' => 'root',
|
||||
'password' => 'xxx',
|
||||
'database' => 'dibi',
|
||||
'options' => array(
|
||||
MYSQLI_OPT_CONNECT_TIMEOUT => 30
|
||||
'options' => array(
|
||||
MYSQLI_OPT_CONNECT_TIMEOUT => 30,
|
||||
),
|
||||
'flags' => MYSQLI_CLIENT_COMPRESS,
|
||||
'flags' => MYSQLI_CLIENT_COMPRESS,
|
||||
));
|
||||
echo 'OK';
|
||||
|
||||
} catch (DibiException $e) {
|
||||
echo get_class($e), ': ', $e->getMessage(), "\n";
|
||||
}
|
||||
@@ -76,13 +71,12 @@ echo "</p>\n";
|
||||
echo '<p>Connecting to ODBC: ';
|
||||
try {
|
||||
dibi::connect(array(
|
||||
'driver' => 'odbc',
|
||||
'driver' => 'odbc',
|
||||
'username' => 'root',
|
||||
'password' => '***',
|
||||
'dsn' => 'Driver={Microsoft Access Driver (*.mdb)};Dbq='.dirname(__FILE__).'/data/sample.mdb',
|
||||
'dsn' => 'Driver={Microsoft Access Driver (*.mdb)};Dbq='.__DIR__.'/data/sample.mdb',
|
||||
));
|
||||
echo 'OK';
|
||||
|
||||
} catch (DibiException $e) {
|
||||
echo get_class($e), ': ', $e->getMessage(), "\n";
|
||||
}
|
||||
@@ -93,12 +87,11 @@ echo "</p>\n";
|
||||
echo '<p>Connecting to PostgreSql: ';
|
||||
try {
|
||||
dibi::connect(array(
|
||||
'driver' => 'postgre',
|
||||
'string' => 'host=localhost port=5432 dbname=mary',
|
||||
'driver' => 'postgre',
|
||||
'string' => 'host=localhost port=5432 dbname=mary',
|
||||
'persistent' => TRUE,
|
||||
));
|
||||
echo 'OK';
|
||||
|
||||
} catch (DibiException $e) {
|
||||
echo get_class($e), ': ', $e->getMessage(), "\n";
|
||||
}
|
||||
@@ -109,11 +102,10 @@ echo "</p>\n";
|
||||
echo '<p>Connecting to Sqlite via PDO: ';
|
||||
try {
|
||||
dibi::connect(array(
|
||||
'driver' => 'pdo',
|
||||
'dsn' => 'sqlite2::memory:',
|
||||
'driver' => 'pdo',
|
||||
'dsn' => 'sqlite2::memory:',
|
||||
));
|
||||
echo 'OK';
|
||||
|
||||
} catch (DibiException $e) {
|
||||
echo get_class($e), ': ', $e->getMessage(), "\n";
|
||||
}
|
||||
@@ -124,13 +116,12 @@ echo "</p>\n";
|
||||
echo '<p>Connecting to MS SQL: ';
|
||||
try {
|
||||
dibi::connect(array(
|
||||
'driver' => 'mssql',
|
||||
'host' => 'localhost',
|
||||
'driver' => 'mssql',
|
||||
'host' => 'localhost',
|
||||
'username' => 'root',
|
||||
'password' => 'xxx',
|
||||
));
|
||||
echo 'OK';
|
||||
|
||||
} catch (DibiException $e) {
|
||||
echo get_class($e), ': ', $e->getMessage(), "\n";
|
||||
}
|
||||
@@ -141,14 +132,13 @@ echo "</p>\n";
|
||||
echo '<p>Connecting to MS SQL 2005: ';
|
||||
try {
|
||||
dibi::connect(array(
|
||||
'driver' => 'mssql2005',
|
||||
'host' => '(local)',
|
||||
'driver' => 'mssql2005',
|
||||
'host' => '(local)',
|
||||
'username' => 'Administrator',
|
||||
'password' => 'xxx',
|
||||
'database' => 'main',
|
||||
));
|
||||
echo 'OK';
|
||||
|
||||
} catch (DibiException $e) {
|
||||
echo get_class($e), ': ', $e->getMessage(), "\n";
|
||||
}
|
||||
@@ -159,13 +149,12 @@ echo "</p>\n";
|
||||
echo '<p>Connecting to Oracle: ';
|
||||
try {
|
||||
dibi::connect(array(
|
||||
'driver' => 'oracle',
|
||||
'driver' => 'oracle',
|
||||
'username' => 'root',
|
||||
'password' => 'xxx',
|
||||
'database' => 'db',
|
||||
));
|
||||
echo 'OK';
|
||||
|
||||
} catch (DibiException $e) {
|
||||
echo get_class($e), ': ', $e->getMessage(), "\n";
|
||||
}
|
||||
|
@@ -41,24 +41,24 @@ table.dump th {
|
||||
}
|
||||
|
||||
/* dump() */
|
||||
pre.nette-dump, pre.dump {
|
||||
pre.tracy-dump, pre.dump {
|
||||
color: #444; background: white;
|
||||
border: 1px solid silver;
|
||||
padding: 1em;
|
||||
margin: 1em 0;
|
||||
}
|
||||
pre.nette-dump .php-array, pre.nette-dump .php-object {
|
||||
pre.tracy-dump .php-array, pre.tracy-dump .php-object {
|
||||
color: #C22;
|
||||
}
|
||||
pre.nette-dump .php-string {
|
||||
pre.tracy-dump .php-string {
|
||||
color: #080;
|
||||
}
|
||||
pre.nette-dump .php-int, pre.nette-dump .php-float {
|
||||
pre.tracy-dump .php-int, pre.tracy-dump .php-float {
|
||||
color: #37D;
|
||||
}
|
||||
pre.nette-dump .php-null, pre.nette-dump .php-bool {
|
||||
pre.tracy-dump .php-null, pre.tracy-dump .php-bool {
|
||||
color: black;
|
||||
}
|
||||
pre.nette-dump .php-visibility {
|
||||
pre.tracy-dump .php-visibility {
|
||||
font-size: 85%; color: #999;
|
||||
}
|
||||
|
@@ -4,12 +4,11 @@
|
||||
|
||||
<?php
|
||||
|
||||
require dirname(__FILE__) . '/Nette/Debugger.php';
|
||||
require dirname(__FILE__) . '/../dibi/dibi.php';
|
||||
require __DIR__ . '/../dibi/dibi.php';
|
||||
|
||||
|
||||
dibi::connect(array(
|
||||
'driver' => 'sqlite3',
|
||||
'driver' => 'sqlite3',
|
||||
'database' => 'data/sample.s3db',
|
||||
));
|
||||
|
||||
@@ -38,7 +37,7 @@ foreach ($table->getColumns() as $column) {
|
||||
echo "</ul>\n";
|
||||
|
||||
|
||||
echo "Indexes";
|
||||
echo 'Indexes';
|
||||
echo "<ul>\n";
|
||||
foreach ($table->getIndexes() as $index) {
|
||||
echo "<li>{$index->name} " . ($index->primary ? 'primary ' : '') . ($index->unique ? 'unique' : '') . ' (';
|
||||
|
@@ -4,12 +4,11 @@
|
||||
|
||||
<?php
|
||||
|
||||
require dirname(__FILE__) . '/Nette/Debugger.php';
|
||||
require dirname(__FILE__) . '/../dibi/dibi.php';
|
||||
require __DIR__ . '/../dibi/dibi.php';
|
||||
|
||||
|
||||
dibi::connect(array(
|
||||
'driver' => 'sqlite3',
|
||||
'driver' => 'sqlite3',
|
||||
'database' => 'data/sample.s3db',
|
||||
));
|
||||
|
||||
|
@@ -4,13 +4,15 @@
|
||||
|
||||
<?php
|
||||
|
||||
require dirname(__FILE__) . '/Nette/Debugger.php';
|
||||
require dirname(__FILE__) . '/../dibi/dibi.php';
|
||||
if (@!include __DIR__ . '/../vendor/autoload.php') {
|
||||
die('Install dependencies using `composer install --dev`');
|
||||
}
|
||||
|
||||
Tracy\Debugger::enable();
|
||||
|
||||
ndebug();
|
||||
|
||||
dibi::connect(array(
|
||||
'driver' => 'sqlite3',
|
||||
'driver' => 'sqlite3',
|
||||
'database' => 'data/sample.s3db',
|
||||
));
|
||||
|
||||
@@ -30,38 +32,40 @@ product_id | title
|
||||
// fetch a single row
|
||||
echo "<h2>fetch()</h2>\n";
|
||||
$row = dibi::fetch('SELECT title FROM products');
|
||||
dump($row); // Chair
|
||||
Tracy\Dumper::dump($row); // Chair
|
||||
|
||||
|
||||
// fetch a single value
|
||||
echo "<h2>fetchSingle()</h2>\n";
|
||||
$value = dibi::fetchSingle('SELECT title FROM products');
|
||||
dump($value); // Chair
|
||||
Tracy\Dumper::dump($value); // Chair
|
||||
|
||||
|
||||
// fetch complete result set
|
||||
echo "<h2>fetchAll()</h2>\n";
|
||||
$all = dibi::fetchAll('SELECT * FROM products');
|
||||
dump($all);
|
||||
Tracy\Dumper::dump($all);
|
||||
|
||||
|
||||
// fetch complete result set like association array
|
||||
echo "<h2>fetchAssoc('title')</h2>\n";
|
||||
$res = dibi::query('SELECT * FROM products');
|
||||
$assoc = $res->fetchAssoc('title'); // key
|
||||
dump($assoc);
|
||||
Tracy\Dumper::dump($assoc);
|
||||
|
||||
|
||||
// fetch complete result set like pairs key => value
|
||||
echo "<h2>fetchPairs('product_id', 'title')</h2>\n";
|
||||
$res = dibi::query('SELECT * FROM products');
|
||||
$pairs = $res->fetchPairs('product_id', 'title');
|
||||
dump($pairs);
|
||||
Tracy\Dumper::dump($pairs);
|
||||
|
||||
|
||||
// fetch row by row
|
||||
echo "<h2>using foreach</h2>\n";
|
||||
$res = dibi::query('SELECT * FROM products');
|
||||
foreach ($res as $n => $row) {
|
||||
dump($row);
|
||||
Tracy\Dumper::dump($row);
|
||||
}
|
||||
|
||||
|
||||
@@ -73,14 +77,16 @@ $res = dibi::query('
|
||||
INNER JOIN customers USING (customer_id)
|
||||
');
|
||||
|
||||
echo "<h2>fetchAssoc('customers.name|products.title')</h2>\n";
|
||||
$assoc = $res->fetchAssoc('customers.name|products.title'); // key
|
||||
dump($assoc);
|
||||
echo "<h2>fetchAssoc('name|title')</h2>\n";
|
||||
$assoc = $res->fetchAssoc('name|title'); // key
|
||||
Tracy\Dumper::dump($assoc);
|
||||
|
||||
echo "<h2>fetchAssoc('customers.name[]products.title')</h2>\n";
|
||||
$assoc = $res->fetchAssoc('customers.name[]products.title'); // key
|
||||
dump($assoc);
|
||||
echo "<h2>fetchAssoc('name[]title')</h2>\n";
|
||||
$res = dibi::query('SELECT * FROM products INNER JOIN orders USING (product_id) INNER JOIN customers USING (customer_id)');
|
||||
$assoc = $res->fetchAssoc('name[]title'); // key
|
||||
Tracy\Dumper::dump($assoc);
|
||||
|
||||
echo "<h2>fetchAssoc('customers.name->products.title')</h2>\n";
|
||||
$assoc = $res->fetchAssoc('customers.name->products.title'); // key
|
||||
dump($assoc);
|
||||
echo "<h2>fetchAssoc('name->title')</h2>\n";
|
||||
$res = dibi::query('SELECT * FROM products INNER JOIN orders USING (product_id) INNER JOIN customers USING (customer_id)');
|
||||
$assoc = $res->fetchAssoc('name->title'); // key
|
||||
Tracy\Dumper::dump($assoc);
|
||||
|
@@ -4,12 +4,11 @@
|
||||
|
||||
<?php
|
||||
|
||||
require dirname(__FILE__) . '/Nette/Debugger.php';
|
||||
require dirname(__FILE__) . '/../dibi/dibi.php';
|
||||
require __DIR__ . '/../dibi/dibi.php';
|
||||
|
||||
|
||||
dibi::connect(array(
|
||||
'driver' => 'sqlite3',
|
||||
'driver' => 'sqlite3',
|
||||
'database' => 'data/sample.s3db',
|
||||
));
|
||||
|
||||
|
@@ -1,44 +0,0 @@
|
||||
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
|
||||
|
||||
<h1>Nette Debugger & SQL Exceptions | dibi</h1>
|
||||
|
||||
<p>Dibi can display and log exceptions via Nette Debugger, part of Nette Framework.</p>
|
||||
|
||||
<ul>
|
||||
<li>Nette Framework: http://nette.org
|
||||
</ul>
|
||||
|
||||
<?php
|
||||
|
||||
require dirname(__FILE__) . '/Nette/Debugger.php';
|
||||
require dirname(__FILE__) . '/../dibi/dibi.php';
|
||||
|
||||
|
||||
// enable Nette Debugger
|
||||
ndebug();
|
||||
|
||||
|
||||
dibi::connect(array(
|
||||
'driver' => 'sqlite3',
|
||||
'database' => 'data/sample.s3db',
|
||||
'profiler' => array(
|
||||
'run' => TRUE,
|
||||
)
|
||||
));
|
||||
|
||||
|
||||
// throws error because SQL is bad
|
||||
dibi::query('SELECT * FROM customers WHERE customer_id < ?', 38);
|
||||
|
||||
|
||||
dibi::connect(array(
|
||||
'driver' => 'sqlite3',
|
||||
'database' => 'data/sample.s3db',
|
||||
'profiler' => array(
|
||||
'run' => TRUE,
|
||||
)
|
||||
));
|
||||
|
||||
|
||||
// throws error because SQL is bad
|
||||
dibi::query('SELECT FROM customers WHERE customer_id < ?', 38);
|
@@ -1,32 +0,0 @@
|
||||
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
|
||||
|
||||
<style> html { background: url(data/arrow.png) no-repeat bottom right; height: 100%; } </style>
|
||||
|
||||
<h1>Nette Debugger & Variables | dibi</h1>
|
||||
|
||||
<p>Dibi can dump variables via Nette Debugger, part of Nette Framework.</p>
|
||||
|
||||
<ul>
|
||||
<li>Nette Framework: http://nette.org
|
||||
</ul>
|
||||
|
||||
<?php
|
||||
|
||||
require dirname(__FILE__) . '/Nette/Debugger.php';
|
||||
require dirname(__FILE__) . '/../dibi/dibi.php';
|
||||
|
||||
|
||||
// enable Nette Debugger
|
||||
NDebugger::enable();
|
||||
|
||||
|
||||
dibi::connect(array(
|
||||
'driver' => 'sqlite3',
|
||||
'database' => 'data/sample.s3db',
|
||||
'profiler' => array(
|
||||
'run' => TRUE,
|
||||
)
|
||||
));
|
||||
|
||||
|
||||
NDebugger::barDump( dibi::fetchAll('SELECT * FROM customers WHERE customer_id < ?', 38), '[customers]' );
|
@@ -4,12 +4,11 @@
|
||||
|
||||
<?php
|
||||
|
||||
require dirname(__FILE__) . '/Nette/Debugger.php';
|
||||
require dirname(__FILE__) . '/../dibi/dibi.php';
|
||||
require __DIR__ . '/../dibi/dibi.php';
|
||||
|
||||
|
||||
dibi::connect(array(
|
||||
'driver' => 'sqlite3',
|
||||
'driver' => 'sqlite3',
|
||||
'database' => 'data/sample.s3db',
|
||||
));
|
||||
|
||||
@@ -33,13 +32,13 @@ dibi::test('
|
||||
|
||||
|
||||
// if & else & (optional) end
|
||||
dibi::test("
|
||||
dibi::test('
|
||||
SELECT *
|
||||
FROM people
|
||||
WHERE id > 0
|
||||
%if", ($foo > 0), "AND foo=?", $foo, "
|
||||
%else %if", ($bar > 0), "AND bar=?", $bar, "
|
||||
");
|
||||
%if', ($foo > 0), 'AND foo=?', $foo, '
|
||||
%else %if', ($bar > 0), 'AND bar=?', $bar, '
|
||||
');
|
||||
// -> SELECT * FROM people WHERE id > 0 AND bar=2
|
||||
|
||||
|
||||
|
@@ -4,14 +4,13 @@
|
||||
|
||||
<?php
|
||||
|
||||
require dirname(__FILE__) . '/Nette/Debugger.php';
|
||||
require dirname(__FILE__) . '/../dibi/dibi.php';
|
||||
require __DIR__ . '/../dibi/dibi.php';
|
||||
|
||||
date_default_timezone_set('Europe/Prague');
|
||||
|
||||
|
||||
dibi::connect(array(
|
||||
'driver' => 'sqlite3',
|
||||
'driver' => 'sqlite3',
|
||||
'database' => 'data/sample.s3db',
|
||||
));
|
||||
|
||||
@@ -46,26 +45,26 @@ $array = array(
|
||||
'brand' => NULL,
|
||||
'created' => new DateTime,
|
||||
);
|
||||
dibi::test("INSERT INTO products", $array, $array, $array);
|
||||
dibi::test('INSERT INTO products', $array, $array, $array);
|
||||
// -> INSERT INTO products ([title], [price], [brand], [created]) VALUES ('Super Product', ...) , (...) , (...)
|
||||
|
||||
|
||||
// dibi detects UPDATE command
|
||||
dibi::test("
|
||||
UPDATE colors SET", array(
|
||||
dibi::test('
|
||||
UPDATE colors SET', array(
|
||||
'color' => 'blue',
|
||||
'order' => 12,
|
||||
), "
|
||||
WHERE id=?", 123);
|
||||
), '
|
||||
WHERE id=?', 123);
|
||||
// -> UPDATE colors SET [color]='blue', [order]=12 WHERE id=123
|
||||
|
||||
|
||||
// modifier applied to array
|
||||
$array = array(1, 2, 3);
|
||||
dibi::test("
|
||||
dibi::test('
|
||||
SELECT *
|
||||
FROM people
|
||||
WHERE id IN (?)", $array
|
||||
WHERE id IN (?)', $array
|
||||
);
|
||||
// -> SELECT * FROM people WHERE id IN ( 1, 2, 3 )
|
||||
|
||||
@@ -75,11 +74,11 @@ $order = array(
|
||||
'field1' => 'asc',
|
||||
'field2' => 'desc',
|
||||
);
|
||||
dibi::test("
|
||||
dibi::test('
|
||||
SELECT *
|
||||
FROM people
|
||||
ORDER BY %by", $order, "
|
||||
");
|
||||
ORDER BY %by', $order, '
|
||||
');
|
||||
// -> SELECT * FROM people ORDER BY [field1] ASC, [field2] DESC
|
||||
|
||||
|
||||
|
@@ -1,18 +1,20 @@
|
||||
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
|
||||
|
||||
<h1>Result Set Data Types | dibi</h1>
|
||||
<h1>Result Set Data Types | dibi</h1>
|
||||
|
||||
<?php
|
||||
|
||||
require dirname(__FILE__) . '/Nette/Debugger.php';
|
||||
require dirname(__FILE__) . '/../dibi/dibi.php';
|
||||
if (@!include __DIR__ . '/../vendor/autoload.php') {
|
||||
die('Install dependencies using `composer install --dev`');
|
||||
}
|
||||
|
||||
Tracy\Debugger::enable();
|
||||
|
||||
ndebug();
|
||||
date_default_timezone_set('Europe/Prague');
|
||||
|
||||
|
||||
dibi::connect(array(
|
||||
'driver' => 'sqlite3',
|
||||
'driver' => 'sqlite3',
|
||||
'database' => 'data/sample.s3db',
|
||||
));
|
||||
|
||||
@@ -25,7 +27,7 @@ $res->setType('customer_id', Dibi::INTEGER)
|
||||
->setFormat(dibi::DATETIME, 'Y-m-d H:i:s');
|
||||
|
||||
|
||||
dump( $res->fetch() );
|
||||
Tracy\Dumper::dump($res->fetch());
|
||||
// outputs:
|
||||
// DibiRow(3) {
|
||||
// customer_id => 1
|
||||
@@ -36,7 +38,7 @@ dump( $res->fetch() );
|
||||
// using auto-detection (works well with MySQL or other strictly typed databases)
|
||||
$res = dibi::query('SELECT * FROM [customers]');
|
||||
|
||||
dump( $res->fetch() );
|
||||
Tracy\Dumper::dump($res->fetch());
|
||||
// outputs:
|
||||
// DibiRow(3) {
|
||||
// customer_id => 1
|
||||
|
33
examples/tracy-and-exceptions.php
Normal file
33
examples/tracy-and-exceptions.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
|
||||
|
||||
<h1>Tracy & SQL Exceptions | dibi</h1>
|
||||
|
||||
<p>Dibi can display and log exceptions via <a href="https://tracy.nette.org">Tracy</a>.</p>
|
||||
|
||||
<?php
|
||||
|
||||
if (@!include __DIR__ . '/../vendor/autoload.php') {
|
||||
die('Install dependencies using `composer install --dev`');
|
||||
}
|
||||
|
||||
|
||||
// enable Tracy
|
||||
Tracy\Debugger::enable();
|
||||
|
||||
|
||||
$connection = dibi::connect(array(
|
||||
'driver' => 'sqlite3',
|
||||
'database' => 'data/sample.s3db',
|
||||
'profiler' => array(
|
||||
'run' => TRUE,
|
||||
),
|
||||
));
|
||||
|
||||
|
||||
// add panel to debug bar
|
||||
$panel = new Dibi\Bridges\Tracy\Panel;
|
||||
$panel->register($connection);
|
||||
|
||||
|
||||
// throws error because SQL is bad
|
||||
dibi::query('SELECT FROM customers WHERE customer_id < ?', 38);
|
38
examples/tracy.php
Normal file
38
examples/tracy.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
|
||||
|
||||
<style> html { background: url(data/arrow.png) no-repeat bottom right; height: 100%; } </style>
|
||||
|
||||
<h1>Tracy | dibi</h1>
|
||||
|
||||
<p>Dibi can log queries and dump variables to the <a href="https://tracy.nette.org">Tracy</a>.</p>
|
||||
|
||||
<?php
|
||||
|
||||
if (@!include __DIR__ . '/../vendor/autoload.php') {
|
||||
die('Install dependencies using `composer install --dev`');
|
||||
}
|
||||
|
||||
|
||||
// enable Tracy
|
||||
Tracy\Debugger::enable();
|
||||
|
||||
|
||||
$connection = dibi::connect(array(
|
||||
'driver' => 'sqlite3',
|
||||
'database' => 'data/sample.s3db',
|
||||
'profiler' => array(
|
||||
'run' => TRUE,
|
||||
),
|
||||
));
|
||||
|
||||
|
||||
// add panel to debug bar
|
||||
$panel = new Dibi\Bridges\Tracy\Panel;
|
||||
$panel->register($connection);
|
||||
|
||||
|
||||
// query will be logged
|
||||
dibi::query('SELECT 123');
|
||||
|
||||
// result set will be dumped
|
||||
Tracy\Debugger::barDump(dibi::fetchAll('SELECT * FROM customers WHERE customer_id < ?', 38), '[customers]');
|
@@ -4,15 +4,14 @@
|
||||
|
||||
<?php
|
||||
|
||||
require dirname(__FILE__) . '/Nette/Debugger.php';
|
||||
require dirname(__FILE__) . '/../dibi/dibi.php';
|
||||
require __DIR__ . '/../dibi/dibi.php';
|
||||
|
||||
date_default_timezone_set('Europe/Prague');
|
||||
|
||||
|
||||
// CHANGE TO REAL PARAMETERS!
|
||||
dibi::connect(array(
|
||||
'driver' => 'sqlite3',
|
||||
'driver' => 'sqlite3',
|
||||
'database' => 'data/sample.s3db',
|
||||
'formatDate' => "'Y-m-d'",
|
||||
'formatDateTime' => "'Y-m-d H-i-s'",
|
||||
@@ -20,10 +19,10 @@ dibi::connect(array(
|
||||
|
||||
|
||||
// generate and dump SQL
|
||||
dibi::test("
|
||||
INSERT INTO [mytable]", array(
|
||||
'id' => 123,
|
||||
'date' => new DateTime('12.3.2007'),
|
||||
dibi::test('
|
||||
INSERT INTO [mytable]', array(
|
||||
'id' => 123,
|
||||
'date' => new DateTime('12.3.2007'),
|
||||
'stamp' => new DateTime('23.1.2007 10:23'),
|
||||
)
|
||||
);
|
||||
|
@@ -4,27 +4,28 @@
|
||||
|
||||
<?php
|
||||
|
||||
require dirname(__FILE__) . '/Nette/Debugger.php';
|
||||
require dirname(__FILE__) . '/../dibi/dibi.php';
|
||||
if (@!include __DIR__ . '/../vendor/autoload.php') {
|
||||
die('Install dependencies using `composer install --dev`');
|
||||
}
|
||||
|
||||
Tracy\Debugger::enable();
|
||||
|
||||
ndebug();
|
||||
|
||||
dibi::connect(array(
|
||||
'driver' => 'sqlite3',
|
||||
'driver' => 'sqlite3',
|
||||
'database' => 'data/sample.s3db',
|
||||
));
|
||||
|
||||
|
||||
// using the "prototype" to add custom method to class DibiResult
|
||||
function DibiResult_prototype_fetchShuffle(DibiResult $obj)
|
||||
{
|
||||
DibiResult::extensionMethod('fetchShuffle', function (DibiResult $obj) {
|
||||
$all = $obj->fetchAll();
|
||||
shuffle($all);
|
||||
return $all;
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
// fetch complete result set shuffled
|
||||
$res = dibi::query('SELECT * FROM [customers]');
|
||||
$all = $res->fetchShuffle();
|
||||
dump($all);
|
||||
Tracy\Dumper::dump($all);
|
||||
|
@@ -4,22 +4,21 @@
|
||||
|
||||
<?php
|
||||
|
||||
require dirname(__FILE__) . '/Nette/Debugger.php';
|
||||
require dirname(__FILE__) . '/../dibi/dibi.php';
|
||||
require __DIR__ . '/../dibi/dibi.php';
|
||||
|
||||
date_default_timezone_set('Europe/Prague');
|
||||
|
||||
|
||||
dibi::connect(array(
|
||||
'driver' => 'sqlite3',
|
||||
'driver' => 'sqlite3',
|
||||
'database' => 'data/sample.s3db',
|
||||
));
|
||||
|
||||
|
||||
$id = 10;
|
||||
$record = array(
|
||||
'title' => 'Super product',
|
||||
'price' => 318,
|
||||
'title' => 'Super product',
|
||||
'price' => 318,
|
||||
'active' => TRUE,
|
||||
);
|
||||
|
||||
|
@@ -4,12 +4,11 @@
|
||||
|
||||
<?php
|
||||
|
||||
require dirname(__FILE__) . '/Nette/Debugger.php';
|
||||
require dirname(__FILE__) . '/../dibi/dibi.php';
|
||||
require __DIR__ . '/../dibi/dibi.php';
|
||||
|
||||
|
||||
dibi::connect(array(
|
||||
'driver' => 'sqlite3',
|
||||
'driver' => 'sqlite3',
|
||||
'database' => 'data/sample.s3db',
|
||||
));
|
||||
|
||||
|
@@ -4,14 +4,13 @@
|
||||
|
||||
<?php
|
||||
|
||||
require dirname(__FILE__) . '/Nette/Debugger.php';
|
||||
require dirname(__FILE__) . '/../dibi/dibi.php';
|
||||
require __DIR__ . '/../dibi/dibi.php';
|
||||
|
||||
date_default_timezone_set('Europe/Prague');
|
||||
|
||||
|
||||
dibi::connect(array(
|
||||
'driver' => 'sqlite3',
|
||||
'driver' => 'sqlite3',
|
||||
'database' => 'data/sample.s3db',
|
||||
// enable query logging to this file
|
||||
'profiler' => array(
|
||||
@@ -27,13 +26,12 @@ try {
|
||||
$res = dibi::query('SELECT * FROM [customers] WHERE [customer_id] < ?', 5);
|
||||
|
||||
$res = dibi::query('SELECT FROM [customers] WHERE [customer_id] < ?', 38);
|
||||
|
||||
} catch (DibiException $e) {
|
||||
echo '<p>', get_class($e), ': ', $e->getMessage(), '</p>';
|
||||
}
|
||||
|
||||
|
||||
// outputs a log file
|
||||
echo "<h2>File data/log.sql:</h2>";
|
||||
echo '<h2>File data/log.sql:</h2>';
|
||||
|
||||
echo '<pre>', file_get_contents('data/log.sql'), '</pre>';
|
||||
|
@@ -6,21 +6,20 @@
|
||||
|
||||
<?php
|
||||
|
||||
require dirname(__FILE__) . '/Nette/Debugger.php';
|
||||
require dirname(__FILE__) . '/../dibi/dibi.php';
|
||||
require __DIR__ . '/../dibi/dibi.php';
|
||||
|
||||
|
||||
dibi::connect(array(
|
||||
'driver' => 'sqlite3',
|
||||
'driver' => 'sqlite3',
|
||||
'database' => 'data/sample.s3db',
|
||||
'profiler' => array(
|
||||
'run' => TRUE,
|
||||
)
|
||||
),
|
||||
));
|
||||
|
||||
|
||||
// execute some queries...
|
||||
for ($i=0; $i<20; $i++) {
|
||||
for ($i = 0; $i < 20; $i++) {
|
||||
$res = dibi::query('SELECT * FROM [customers] WHERE [customer_id] < ?', $i);
|
||||
}
|
||||
|
||||
|
@@ -4,12 +4,11 @@
|
||||
|
||||
<?php
|
||||
|
||||
require dirname(__FILE__) . '/Nette/Debugger.php';
|
||||
require dirname(__FILE__) . '/../dibi/dibi.php';
|
||||
require __DIR__ . '/../dibi/dibi.php';
|
||||
|
||||
|
||||
dibi::connect(array(
|
||||
'driver' => 'sqlite3',
|
||||
'driver' => 'sqlite3',
|
||||
'database' => 'data/sample.s3db',
|
||||
));
|
||||
|
||||
@@ -17,7 +16,7 @@ dibi::connect(array(
|
||||
// create new substitution :blog: ==> wp_
|
||||
dibi::getSubstitutes()->blog = 'wp_';
|
||||
|
||||
dibi::test("SELECT * FROM [:blog:items]");
|
||||
dibi::test('SELECT * FROM [:blog:items]');
|
||||
// -> SELECT * FROM [wp_items]
|
||||
|
||||
|
||||
|
@@ -4,12 +4,11 @@
|
||||
|
||||
<?php
|
||||
|
||||
require dirname(__FILE__) . '/Nette/Debugger.php';
|
||||
require dirname(__FILE__) . '/../dibi/dibi.php';
|
||||
require __DIR__ . '/../dibi/dibi.php';
|
||||
|
||||
|
||||
dibi::connect(array(
|
||||
'driver' => 'sqlite3',
|
||||
'driver' => 'sqlite3',
|
||||
'database' => 'data/sample.s3db',
|
||||
));
|
||||
|
||||
|
@@ -16,7 +16,7 @@ remains intact.
|
||||
New BSD License
|
||||
---------------
|
||||
|
||||
Copyright (c) 2004, 2014 David Grudl (http://davidgrudl.com)
|
||||
Copyright (c) 2004, 2014 David Grudl (https://davidgrudl.com)
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification,
|
||||
|
@@ -1,10 +1,12 @@
|
||||
[Dibi](http://dibiphp.com) - smart database layer for PHP
|
||||
[Dibi](http://dibiphp.com) - smart database layer for PHP [](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=9XXL5ZJHAYQUN)
|
||||
=========================================================
|
||||
|
||||
[](https://packagist.org/packages/dibi/dibi)
|
||||
|
||||
Database access functions in PHP are not standardised. This library
|
||||
hides the differences between them, and above all, it gives you a very handy interface.
|
||||
|
||||
The best way to install Dibi is to use a [Composer](http://getcomposer.org/download):
|
||||
The best way to install Dibi is to use a [Composer](https://getcomposer.org/download):
|
||||
|
||||
php composer.phar require dibi/dibi
|
||||
|
||||
@@ -124,3 +126,4 @@ echo dibi::$sql; // last SQL query
|
||||
echo dibi::$elapsedTime;
|
||||
echo dibi::$numOfQueries;
|
||||
echo dibi::$totalTime;
|
||||
```
|
||||
|
@@ -1,40 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Test: Cloning of DibiFluent
|
||||
*
|
||||
* @author David Grudl
|
||||
*/
|
||||
|
||||
|
||||
require dirname(__FILE__) . '/bootstrap.php';
|
||||
|
||||
|
||||
|
||||
dibi::connect($config['sqlite3']);
|
||||
|
||||
|
||||
$fluent = new DibiFluent(dibi::getConnection());
|
||||
$fluent->select('*')->from('table')->where('x=1');
|
||||
$dolly = clone $fluent;
|
||||
$dolly->where('y=1');
|
||||
$dolly->clause('FOO');
|
||||
|
||||
Assert::same( 'SELECT * FROM [table] WHERE x=1', (string) $fluent );
|
||||
Assert::same( 'SELECT * FROM [table] WHERE x=1 AND y=1 FOO', (string) $dolly );
|
||||
|
||||
|
||||
$fluent = dibi::select('id')->from('table')->where('id = %i',1);
|
||||
$dolly = clone $fluent;
|
||||
$dolly->where('cd = %i',5);
|
||||
|
||||
Assert::same( 'SELECT [id] FROM [table] WHERE id = 1', (string) $fluent );
|
||||
Assert::same( 'SELECT [id] FROM [table] WHERE id = 1 AND cd = 5', (string) $dolly );
|
||||
|
||||
|
||||
$fluent = dibi::select("*")->from("table");
|
||||
$dolly = clone $fluent;
|
||||
$dolly->removeClause("select")->select("count(*)");
|
||||
|
||||
Assert::same( 'SELECT * FROM [table]', (string) $fluent );
|
||||
Assert::same( 'SELECT count(*) FROM [table]', (string) $dolly );
|
@@ -1,20 +0,0 @@
|
||||
<?php
|
||||
|
||||
// The Nette Tester command-line runner can be
|
||||
// invoked through the command: ../../vendor/bin/tester .
|
||||
|
||||
if (@!include __DIR__ . '/../vendor/autoload.php') {
|
||||
echo 'Install Nette Tester using `composer update --dev`';
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
||||
// configure environment
|
||||
Tester\Environment::setup();
|
||||
date_default_timezone_set('Europe/Prague');
|
||||
class_alias('Tester\Assert', 'Assert');
|
||||
|
||||
|
||||
// load connections
|
||||
define('DIR', dirname(__FILE__));
|
||||
$config = parse_ini_file(dirname(__FILE__) . '/config.ini', TRUE);
|
@@ -1,67 +0,0 @@
|
||||
[mysql]
|
||||
driver = mysql
|
||||
host = localhost
|
||||
username = dibi
|
||||
password = dibi
|
||||
database = dibi_test
|
||||
charset = utf8
|
||||
|
||||
[mysqli]
|
||||
driver = mysqli
|
||||
host = localhost
|
||||
username = dibi
|
||||
password = dibi
|
||||
database = dibi_test
|
||||
charset = utf8
|
||||
|
||||
[sqlite]
|
||||
driver = sqlite
|
||||
database = DIR "/data/sample.sdb"
|
||||
|
||||
[sqlite3]
|
||||
driver = sqlite3
|
||||
database = DIR "/data/sample.sdb3"
|
||||
|
||||
[odbc]
|
||||
driver = odbc
|
||||
username = dibi
|
||||
password = dibi
|
||||
dsn = "Driver={Microsoft Access Driver (*.mdb)};Dbq=" DIR "/data/sample.mdb"
|
||||
|
||||
[postgresql]
|
||||
driver = postgre
|
||||
host = localhost
|
||||
port = 5432
|
||||
username = dibi
|
||||
password = dibi
|
||||
database = dibi_test
|
||||
persistent = TRUE
|
||||
|
||||
[sqlite-pdo]
|
||||
driver = pdo
|
||||
dsn = "sqlite2::" DIR "/data/sample.sdb"
|
||||
|
||||
[mysql-pdo]
|
||||
driver = pdo
|
||||
dsn = "mysql:dbname=dibi_test;host=localhost"
|
||||
username = dibi
|
||||
password = dibi
|
||||
|
||||
[mssql]
|
||||
driver = mssql
|
||||
host = localhost
|
||||
username = dibi
|
||||
password = dibi
|
||||
|
||||
[mssql2005]
|
||||
driver = mssql2005
|
||||
host = "(local)"
|
||||
username = dibi
|
||||
password = dibi
|
||||
database = dibi_test
|
||||
|
||||
[oracle]
|
||||
driver = oracle
|
||||
username = dibi
|
||||
password = dibi
|
||||
database = dibi_test
|
Binary file not shown.
@@ -1,149 +0,0 @@
|
||||
-- phpMyAdmin SQL Dump
|
||||
-- version 2.11.1.2
|
||||
-- http://www.phpmyadmin.net
|
||||
--
|
||||
-- Po<50><6F>ta<74>: localhost
|
||||
-- Vygenerov<6F>no: Ned<65>le 02. prosince 2007, 19:49
|
||||
-- Verze MySQL: 5.0.45
|
||||
-- Verze PHP: 5.2.1
|
||||
|
||||
SET FOREIGN_KEY_CHECKS=0;
|
||||
|
||||
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
|
||||
|
||||
--
|
||||
-- Datab<61>ze: `dibi`
|
||||
--
|
||||
|
||||
-- --------------------------------------------------------
|
||||
|
||||
--
|
||||
-- Struktura tabulky `customers`
|
||||
--
|
||||
|
||||
DROP TABLE IF EXISTS `customers`;
|
||||
CREATE TABLE IF NOT EXISTS `customers` (
|
||||
`customer_id` int(11) NOT NULL auto_increment,
|
||||
`name` varchar(100) default NULL,
|
||||
PRIMARY KEY (`customer_id`)
|
||||
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=7 ;
|
||||
|
||||
--
|
||||
-- Vypisuji data pro tabulku `customers`
|
||||
--
|
||||
|
||||
INSERT INTO `customers` (`customer_id`, `name`) VALUES
|
||||
(1, 'Dave Lister'),
|
||||
(2, 'Arnold Rimmer'),
|
||||
(3, 'The Cat'),
|
||||
(4, 'Holly'),
|
||||
(5, 'Kryten'),
|
||||
(6, 'Kristine Kochanski');
|
||||
|
||||
-- --------------------------------------------------------
|
||||
|
||||
--
|
||||
-- Struktura tabulky `enumtest`
|
||||
--
|
||||
|
||||
DROP TABLE IF EXISTS `enumtest`;
|
||||
CREATE TABLE IF NOT EXISTS `enumtest` (
|
||||
`id` int(11) NOT NULL auto_increment,
|
||||
`test` enum('a','b','c') NOT NULL,
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
|
||||
|
||||
--
|
||||
-- Vypisuji data pro tabulku `enumtest`
|
||||
--
|
||||
|
||||
|
||||
-- --------------------------------------------------------
|
||||
|
||||
--
|
||||
-- Struktura tabulky `orders`
|
||||
--
|
||||
|
||||
DROP TABLE IF EXISTS `orders`;
|
||||
CREATE TABLE IF NOT EXISTS `orders` (
|
||||
`order_id` int(11) NOT NULL,
|
||||
`customer_id` int(11) NOT NULL,
|
||||
`product_id` int(11) NOT NULL,
|
||||
`amount` float NOT NULL
|
||||
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
|
||||
|
||||
--
|
||||
-- Vypisuji data pro tabulku `orders`
|
||||
--
|
||||
|
||||
INSERT INTO `orders` (`order_id`, `customer_id`, `product_id`, `amount`) VALUES
|
||||
(1, 2, 1, 7),
|
||||
(2, 2, 3, 2),
|
||||
(3, 1, 2, 3),
|
||||
(4, 6, 3, 5);
|
||||
|
||||
-- --------------------------------------------------------
|
||||
|
||||
--
|
||||
-- Struktura tabulky `products`
|
||||
--
|
||||
|
||||
DROP TABLE IF EXISTS `products`;
|
||||
CREATE TABLE IF NOT EXISTS `products` (
|
||||
`product_id` int(11) NOT NULL auto_increment,
|
||||
`title` varchar(100) default NULL,
|
||||
PRIMARY KEY (`product_id`)
|
||||
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
|
||||
|
||||
--
|
||||
-- Vypisuji data pro tabulku `products`
|
||||
--
|
||||
|
||||
INSERT INTO `products` (`product_id`, `title`) VALUES
|
||||
(1, 'Chair'),
|
||||
(2, 'Table'),
|
||||
(3, 'Computer');
|
||||
|
||||
-- --------------------------------------------------------
|
||||
|
||||
--
|
||||
-- Struktura tabulky `settest`
|
||||
--
|
||||
|
||||
DROP TABLE IF EXISTS `settest`;
|
||||
CREATE TABLE IF NOT EXISTS `settest` (
|
||||
`id` int(11) NOT NULL auto_increment,
|
||||
`test` set('a','b','c') NOT NULL,
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
|
||||
|
||||
--
|
||||
-- Vypisuji data pro tabulku `settest`
|
||||
--
|
||||
|
||||
|
||||
-- --------------------------------------------------------
|
||||
|
||||
--
|
||||
-- Struktura tabulky `where`
|
||||
--
|
||||
|
||||
DROP TABLE IF EXISTS `where`;
|
||||
CREATE TABLE IF NOT EXISTS `where` (
|
||||
`select` int(11) NOT NULL,
|
||||
`dot.dot` int(11) NOT NULL,
|
||||
`is` int(11) NOT NULL,
|
||||
`quot'n' space` int(11) NOT NULL
|
||||
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
|
||||
|
||||
--
|
||||
-- Vypisuji data pro tabulku `where`
|
||||
--
|
||||
|
||||
INSERT INTO `where` (`select`, `dot.dot`, `is`, `quot'n' space`) VALUES
|
||||
(1, 2, 3, 4);
|
||||
|
||||
SET FOREIGN_KEY_CHECKS=1;
|
||||
|
||||
SET SQL_MODE="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION";
|
||||
|
Binary file not shown.
Binary file not shown.
70
tests/dibi/DibiObject.phpt
Normal file
70
tests/dibi/DibiObject.phpt
Normal file
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
use Tester\Assert;
|
||||
|
||||
require __DIR__ . '/bootstrap.php';
|
||||
|
||||
|
||||
class TestClass extends DibiObject
|
||||
{
|
||||
public function getBar()
|
||||
{
|
||||
return 123;
|
||||
}
|
||||
|
||||
public function isFoo()
|
||||
{
|
||||
return 456;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// calling
|
||||
Assert::exception(function () {
|
||||
$obj = new TestClass;
|
||||
$obj->undeclared();
|
||||
}, 'LogicException', 'Call to undefined method TestClass::undeclared().');
|
||||
|
||||
Assert::exception(function () {
|
||||
TestClass::undeclared();
|
||||
}, 'LogicException', 'Call to undefined static method TestClass::undeclared().');
|
||||
|
||||
|
||||
// writing
|
||||
Assert::exception(function () {
|
||||
$obj = new TestClass;
|
||||
$obj->undeclared = 'value';
|
||||
}, 'LogicException', 'Cannot assign to an undeclared property TestClass::$undeclared.');
|
||||
|
||||
|
||||
// property getter
|
||||
$obj = new TestClass;
|
||||
Assert::true(isset($obj->bar));
|
||||
Assert::same(123, $obj->bar);
|
||||
Assert::false(isset($obj->foo));
|
||||
Assert::same(456, $obj->foo);
|
||||
|
||||
|
||||
// reading
|
||||
Assert::exception(function () {
|
||||
$obj = new TestClass;
|
||||
$val = $obj->undeclared;
|
||||
}, 'LogicException', 'Cannot read an undeclared property TestClass::$undeclared.');
|
||||
|
||||
|
||||
// unset/isset
|
||||
Assert::exception(function () {
|
||||
$obj = new TestClass;
|
||||
unset($obj->undeclared);
|
||||
}, 'LogicException', 'Cannot unset the property TestClass::$undeclared.');
|
||||
|
||||
Assert::false(isset($obj->undeclared));
|
||||
|
||||
|
||||
// extension method
|
||||
TestClass::extensionMethod('join', $func = function (TestClass $that, $separator) {
|
||||
return $that->foo . $separator . $that->bar;
|
||||
});
|
||||
|
||||
$obj = new TestClass;
|
||||
Assert::same('456*123', $obj->join('*'));
|
@@ -1 +1 @@
|
||||
Dibi 2.1.3 (released on 2015-01-13)
|
||||
Dibi 2.2.3 (released on 2015-01-13)
|
||||
|
Reference in New Issue
Block a user