mirror of
https://github.com/dg/dibi.git
synced 2025-08-30 17:29:53 +02:00
Compare commits
31 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
697ffcd44a | ||
|
4535bc5458 | ||
|
bf36cf96ee | ||
|
1912a15831 | ||
|
d4429546e2 | ||
|
a007ea95f9 | ||
|
247f38bb6b | ||
|
ed95fc2ddf | ||
|
2dcf08a90c | ||
|
204cb75bf0 | ||
|
8112fe10d0 | ||
|
6f99db544f | ||
|
c399d79ab8 | ||
|
67f8468a38 | ||
|
72944ae012 | ||
|
4843882e61 | ||
|
669ce73096 | ||
|
6e4a6474cd | ||
|
2dc30747e5 | ||
|
23531a0f3d | ||
|
22c6f2dfda | ||
|
f51ac0b67e | ||
|
43f5e08296 | ||
|
997f5a98f8 | ||
|
e4b3cfb12c | ||
|
26082294b6 | ||
|
b3052b4ce2 | ||
|
efa3a48232 | ||
|
24511a1d96 | ||
|
a20ba29b13 | ||
|
a606be0efa |
@@ -10,15 +10,6 @@
|
||||
"homepage": "http://davidgrudl.com"
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"php": ">=5.2.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"nette/tester": "~1.1"
|
||||
},
|
||||
"replace": {
|
||||
"dg/dibi": "self.version"
|
||||
},
|
||||
"autoload": {
|
||||
"classmap": ["dibi/"]
|
||||
}
|
||||
|
@@ -11,13 +11,13 @@
|
||||
|
||||
|
||||
/**
|
||||
* Dibi extension for Nette Framework 2.0. Creates 'connection' service.
|
||||
* Dibi extension for Nette Framework. Creates 'connection' service.
|
||||
*
|
||||
* @author David Grudl
|
||||
* @package dibi\nette
|
||||
* @phpversion 5.3
|
||||
*/
|
||||
class DibiNette20Extension extends Nette\Config\CompilerExtension
|
||||
class DibiNetteExtension extends Nette\Config\CompilerExtension
|
||||
{
|
||||
|
||||
public function loadConfiguration()
|
||||
@@ -46,7 +46,7 @@ class DibiNette20Extension extends Nette\Config\CompilerExtension
|
||||
$panel = $container->addDefinition($this->prefix('panel'))
|
||||
->setClass('DibiNettePanel')
|
||||
->addSetup('Nette\Diagnostics\Debugger::$bar->addPanel(?)', array('@self'))
|
||||
->addSetup('Nette\Diagnostics\Debugger::$blueScreen->addPanel(?)', array('DibiNettePanel::renderException'));
|
||||
->addSetup('Nette\Diagnostics\Debugger::$blueScreen->addPanel(?)', array(array('@self', 'renderException')));
|
||||
|
||||
$connection->addSetup('$service->onEvent[] = ?', array(array($panel, 'logEvent')));
|
||||
}
|
@@ -44,19 +44,13 @@ class DibiNettePanel extends DibiObject implements IBarPanel
|
||||
if (is_callable('Nette\Diagnostics\Debugger::enable') && !class_exists('NDebugger')) {
|
||||
class_alias('Nette\Diagnostics\Debugger', 'NDebugger'); // PHP 5.2 code compatibility
|
||||
}
|
||||
if (is_callable('NDebugger::enable') && is_callable('NDebugger::getBlueScreen')) { // Nette Framework 2.1
|
||||
NDebugger::getBar()->addPanel($this);
|
||||
NDebugger::getBlueScreen()->addPanel(array(__CLASS__, 'renderException'));
|
||||
$connection->onEvent[] = array($this, 'logEvent');
|
||||
|
||||
} elseif (is_callable('NDebugger::enable')) { // Nette Framework 2.0 (for PHP 5.3 or PHP 5.2 prefixed)
|
||||
if (is_callable('NDebugger::enable')) {
|
||||
NDebugger::$bar && NDebugger::$bar->addPanel($this);
|
||||
NDebugger::$blueScreen && NDebugger::$blueScreen->addPanel(array(__CLASS__, 'renderException'), __CLASS__);
|
||||
NDebugger::$blueScreen && NDebugger::$blueScreen->addPanel(array($this, 'renderException'), __CLASS__);
|
||||
$connection->onEvent[] = array($this, 'logEvent');
|
||||
|
||||
} elseif (is_callable('Debugger::enable') && !is_callable('Debugger::getBlueScreen')) { // Nette Framework 2.0 for PHP 5.2 non-prefixed
|
||||
} elseif (is_callable('Debugger::enable')) {
|
||||
Debugger::$bar && Debugger::$bar->addPanel($this);
|
||||
Debugger::$blueScreen && Debugger::$blueScreen->addPanel(array(__CLASS__, 'renderException'), __CLASS__);
|
||||
Debugger::$blueScreen && Debugger::$blueScreen->addPanel(array($this, 'renderException'), __CLASS__);
|
||||
$connection->onEvent[] = array($this, 'logEvent');
|
||||
}
|
||||
}
|
||||
@@ -79,7 +73,7 @@ class DibiNettePanel extends DibiObject implements IBarPanel
|
||||
* Returns blue-screen custom tab.
|
||||
* @return mixed
|
||||
*/
|
||||
public static function renderException($e)
|
||||
public function renderException($e)
|
||||
{
|
||||
if ($e instanceof DibiException && $e->getSql()) {
|
||||
return array(
|
||||
@@ -132,7 +126,7 @@ class DibiNettePanel extends DibiObject implements IBarPanel
|
||||
if ($explain) {
|
||||
static $counter;
|
||||
$counter++;
|
||||
$s .= "<br /><a href='#nette-debug-DibiProfiler-row-$counter' class='nette-toggler nette-toggle-collapsed' rel='#nette-debug-DibiProfiler-row-$counter'>explain</a>";
|
||||
$s .= "<br /><a href='#' class='nette-toggler' rel='#nette-debug-DibiProfiler-row-$counter'>explain ►</a>";
|
||||
}
|
||||
|
||||
$s .= '</td><td class="nette-DibiProfiler-sql">' . dibi::dump(strlen($event->sql) > self::$maxLength ? substr($event->sql, 0, self::$maxLength) . '...' : $event->sql, TRUE);
|
@@ -1,17 +0,0 @@
|
||||
# Requires Nette Framework 2.0 for PHP 5.3
|
||||
#
|
||||
# In bootstrap.php append these lines after line $configurator = new Nette\Config\Configurator;
|
||||
#
|
||||
# $configurator->onCompile[] = function($configurator, $compiler) {
|
||||
# $compiler->addExtension('dibi', new DibiNette20Extension);
|
||||
# };
|
||||
#
|
||||
# This will create service named 'dibi.connection'.
|
||||
|
||||
common:
|
||||
dibi:
|
||||
host: localhost
|
||||
username: root
|
||||
password: ***
|
||||
database: foo
|
||||
lazy: TRUE
|
@@ -1,51 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the "dibi" - smart database abstraction layer.
|
||||
* Copyright (c) 2005 David Grudl (http://davidgrudl.com)
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* Dibi extension for Nette Framework 2.1. Creates 'connection' service.
|
||||
*
|
||||
* @author David Grudl
|
||||
* @package dibi\nette
|
||||
* @phpversion 5.3
|
||||
*/
|
||||
class DibiNette21Extension extends Nette\DI\CompilerExtension
|
||||
{
|
||||
|
||||
public function loadConfiguration()
|
||||
{
|
||||
$container = $this->getContainerBuilder();
|
||||
$config = $this->getConfig();
|
||||
|
||||
$useProfiler = isset($config['profiler'])
|
||||
? $config['profiler']
|
||||
: $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('DibiNettePanel')
|
||||
->addSetup('Nette\Diagnostics\Debugger::getBar()->addPanel(?)', array('@self'))
|
||||
->addSetup('Nette\Diagnostics\Debugger::getBlueScreen()->addPanel(?)', array('DibiNettePanel::renderException'));
|
||||
|
||||
$connection->addSetup('$service->onEvent[] = ?', array(array($panel, 'logEvent')));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -1,12 +0,0 @@
|
||||
# This will create service named 'dibi.connection'.
|
||||
# Requires Nette Framework 2.1
|
||||
|
||||
extensions:
|
||||
dibi: DibiNette21Extension
|
||||
|
||||
dibi:
|
||||
host: localhost
|
||||
username: root
|
||||
password: ***
|
||||
database: foo
|
||||
lazy: TRUE
|
@@ -33,7 +33,7 @@ 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';
|
||||
require_once dirname(__FILE__) . '/bridges/Nette-2.0/DibiNettePanel.php';
|
||||
}
|
||||
|
||||
|
||||
@@ -71,8 +71,8 @@ class dibi
|
||||
FIELD_TIME = dibi::TIME;
|
||||
|
||||
/** version */
|
||||
const VERSION = '2.1.2',
|
||||
REVISION = '$WCREV$ released on $WCDATE$';
|
||||
const VERSION = '2.0.5',
|
||||
REVISION = 'released on 2015-01-13';
|
||||
|
||||
/** sorting order */
|
||||
const ASC = 'ASC',
|
||||
@@ -478,7 +478,7 @@ class dibi
|
||||
*/
|
||||
public static function datetime($time = NULL)
|
||||
{
|
||||
trigger_error(__METHOD__ . '() is deprecated; create DateTime object instead.', E_USER_WARNING);
|
||||
trigger_error(__METHOD__ . '() is deprecated; create DibiDateTime object instead.', E_USER_WARNING);
|
||||
return new DibiDateTime($time);
|
||||
}
|
||||
|
||||
@@ -488,7 +488,7 @@ class dibi
|
||||
*/
|
||||
public static function date($date = NULL)
|
||||
{
|
||||
trigger_error(__METHOD__ . '() is deprecated; create DateTime object instead.', E_USER_WARNING);
|
||||
trigger_error(__METHOD__ . '() is deprecated; create DibiDateTime object instead.', E_USER_WARNING);
|
||||
return new DibiDateTime($date);
|
||||
}
|
||||
|
||||
@@ -570,18 +570,13 @@ class dibi
|
||||
$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 {
|
||||
// syntax highlight
|
||||
$sql = htmlSpecialChars($sql);
|
||||
$sql = preg_replace_callback($highlighter, array('dibi', 'highlightCallback'), $sql);
|
||||
echo '<pre class="dump">', trim($sql), "</pre>\n\n";
|
||||
$sql = preg_replace_callback("#(/\\*.+?\\*/)|(\\*\\*.+?\\*\\*)|(?<=[\\s,(])($keywords1)(?=[\\s,)])|(?<=[\\s,(=])($keywords2)(?=[\\s,)=])#is", array('dibi', 'highlightCallback'), $sql);
|
||||
echo '<pre class="dump">', trim($sql), "</pre>\n";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -609,21 +604,4 @@ class dibi
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,133 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the "dibi" - smart database abstraction layer.
|
||||
* Copyright (c) 2005 David Grudl (http://davidgrudl.com)
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* The dibi reflector for MSSQL2005 databases.
|
||||
*
|
||||
* @author Daniel Kouba
|
||||
* @package dibi\drivers
|
||||
* @internal
|
||||
*/
|
||||
class DibiMsSql2005Reflector extends DibiObject implements IDibiReflector
|
||||
{
|
||||
/** @var IDibiDriver */
|
||||
private $driver;
|
||||
|
||||
|
||||
public function __construct(IDibiDriver $driver)
|
||||
{
|
||||
$this->driver = $driver;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns list of tables.
|
||||
* @return array
|
||||
*/
|
||||
public function getTables()
|
||||
{
|
||||
$res = $this->driver->query('SELECT TABLE_NAME, TABLE_TYPE FROM INFORMATION_SCHEMA.TABLES');
|
||||
$tables = array();
|
||||
while ($row = $res->fetch(FALSE)) {
|
||||
$tables[] = array(
|
||||
'name' => $row[0],
|
||||
'view' => isset($row[1]) && $row[1] === 'VIEW',
|
||||
);
|
||||
}
|
||||
return $tables;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns metadata for all columns in a table.
|
||||
* @param string
|
||||
* @return array
|
||||
*/
|
||||
public function getColumns($table)
|
||||
{
|
||||
$res = $this->driver->query("
|
||||
SELECT c.name as COLUMN_NAME, c.is_identity AS AUTO_INCREMENT
|
||||
FROM sys.columns c
|
||||
INNER JOIN sys.tables t ON c.object_id = t.object_id
|
||||
WHERE t.name = {$this->driver->escape($table, dibi::TEXT)}
|
||||
");
|
||||
|
||||
$autoIncrements = array();
|
||||
while ($row = $res->fetch(TRUE)) {
|
||||
$autoIncrements[$row['COLUMN_NAME']] = (bool) $row['AUTO_INCREMENT'];
|
||||
}
|
||||
|
||||
$res = $this->driver->query("
|
||||
SELECT C.COLUMN_NAME, C.DATA_TYPE, C.CHARACTER_MAXIMUM_LENGTH , C.COLUMN_DEFAULT , C.NUMERIC_PRECISION, C.NUMERIC_SCALE , C.IS_NULLABLE, Case When Z.CONSTRAINT_NAME Is Null Then 0 Else 1 End As IsPartOfPrimaryKey
|
||||
FROM INFORMATION_SCHEMA.COLUMNS As C
|
||||
Outer Apply (
|
||||
SELECT CCU.CONSTRAINT_NAME
|
||||
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS As TC
|
||||
Join INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE As CCU
|
||||
On CCU.CONSTRAINT_NAME = TC.CONSTRAINT_NAME
|
||||
WHERE TC.TABLE_SCHEMA = C.TABLE_SCHEMA
|
||||
And TC.TABLE_NAME = C.TABLE_NAME
|
||||
And TC.CONSTRAINT_TYPE = 'PRIMARY KEY'
|
||||
And CCU.COLUMN_NAME = C.COLUMN_NAME
|
||||
) As Z
|
||||
WHERE C.TABLE_NAME = {$this->driver->escape($table, dibi::TEXT)}
|
||||
");
|
||||
$columns = array();
|
||||
while ($row = $res->fetch(TRUE)) {
|
||||
$columns[] = array(
|
||||
'name' => $row['COLUMN_NAME'],
|
||||
'table' => $table,
|
||||
'nativetype' => strtoupper($row['DATA_TYPE']),
|
||||
'size' => $row['CHARACTER_MAXIMUM_LENGTH'],
|
||||
'unsigned' => TRUE,
|
||||
'nullable' => $row['IS_NULLABLE'] === 'YES',
|
||||
'default' => $row['COLUMN_DEFAULT'],
|
||||
'autoincrement' => $autoIncrements[$row['COLUMN_NAME']],
|
||||
'vendor' => $row,
|
||||
);
|
||||
}
|
||||
return $columns;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns metadata for all indexes in a table.
|
||||
* @param string
|
||||
* @return array
|
||||
*/
|
||||
public function getIndexes($table)
|
||||
{
|
||||
$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) ) {
|
||||
$keyUsages[$row['CONSTRAINT_NAME']][(int) $row['ORDINAL_POSITION'] - 1] = $row['COLUMN_NAME'];
|
||||
}
|
||||
|
||||
$res = $this->driver->query("SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE TABLE_NAME = {$this->driver->escape($table, dibi::TEXT)}");
|
||||
$indexes = array();
|
||||
while ($row = $res->fetch(TRUE)) {
|
||||
$indexes[$row['CONSTRAINT_NAME']]['name'] = $row['CONSTRAINT_NAME'];
|
||||
$indexes[$row['CONSTRAINT_NAME']]['unique'] = $row['CONSTRAINT_TYPE'] === 'UNIQUE';
|
||||
$indexes[$row['CONSTRAINT_NAME']]['primary'] = $row['CONSTRAINT_TYPE'] === 'PRIMARY KEY';
|
||||
$indexes[$row['CONSTRAINT_NAME']]['columns'] = isset($keyUsages[$row['CONSTRAINT_NAME']]) ? $keyUsages[$row['CONSTRAINT_NAME']] : array();
|
||||
}
|
||||
return array_values($indexes);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns metadata for all foreign keys in a table.
|
||||
* @param string
|
||||
* @return array
|
||||
*/
|
||||
public function getForeignKeys($table)
|
||||
{
|
||||
throw new DibiNotImplementedException;
|
||||
}
|
||||
|
||||
}
|
@@ -66,13 +66,11 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD
|
||||
|
||||
} else {
|
||||
// default values
|
||||
$config += array(
|
||||
'username' => ini_get('ibase.default_password'),
|
||||
'password' => ini_get('ibase.default_user'),
|
||||
'database' => ini_get('ibase.default_db'),
|
||||
'charset' => ini_get('ibase.default_charset'),
|
||||
'buffers' => 0,
|
||||
);
|
||||
if (!isset($config['username'])) $config['username'] = ini_get('ibase.default_password');
|
||||
if (!isset($config['password'])) $config['password'] = ini_get('ibase.default_user');
|
||||
if (!isset($config['database'])) $config['database'] = ini_get('ibase.default_db');
|
||||
if (!isset($config['charset'])) $config['charset'] = ini_get('ibase.default_charset');
|
||||
if (!isset($config['buffers'])) $config['buffers'] = 0;
|
||||
|
||||
DibiDriverException::tryError();
|
||||
if (empty($config['persistent'])) {
|
||||
@@ -348,7 +346,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.");
|
||||
return ibase_num_fields($this->resultSet);
|
||||
}
|
||||
|
||||
|
||||
@@ -364,7 +362,7 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD
|
||||
|
||||
if (DibiDriverException::catchError($msg)) {
|
||||
if (ibase_errcode() == self::ERROR_EXCEPTION_THROWN) {
|
||||
preg_match('/exception (\d+) (\w+) (.*)/is', ibase_errmsg(), $match);
|
||||
preg_match('/exception (\d+) (\w+) (.*)/i', ibase_errmsg(), $match);
|
||||
throw new DibiProcedureException($match[3], $match[1], $match[2], dibi::$sql);
|
||||
|
||||
} else {
|
@@ -5,7 +5,7 @@
|
||||
* Copyright (c) 2005 David Grudl (http://davidgrudl.com)
|
||||
*/
|
||||
|
||||
require_once dirname(__FILE__) . '/DibiMsSqlReflector.php';
|
||||
require_once dirname(__FILE__) . '/mssql.reflector.php';
|
||||
|
||||
/**
|
||||
* The dibi driver for MS SQL database.
|
432
dibi/drivers/DibiMsSqlReflector.php → dibi/drivers/mssql.reflector.php
Normal file → Executable file
432
dibi/drivers/DibiMsSqlReflector.php → dibi/drivers/mssql.reflector.php
Normal file → Executable file
@@ -1,216 +1,216 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the "dibi" - smart database abstraction layer.
|
||||
*
|
||||
* Copyright (c) 2005, 2010 David Grudl (http://davidgrudl.com)
|
||||
*
|
||||
* @package dibi\drivers
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* The dibi reflector for MsSQL databases.
|
||||
*
|
||||
* @author Steven Bredenberg
|
||||
* @package dibi\drivers
|
||||
* @internal
|
||||
*/
|
||||
class DibiMsSqlReflector extends DibiObject implements IDibiReflector
|
||||
{
|
||||
/** @var IDibiDriver */
|
||||
private $driver;
|
||||
|
||||
|
||||
public function __construct(IDibiDriver $driver)
|
||||
{
|
||||
$this->driver = $driver;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns list of tables.
|
||||
* @return array
|
||||
*/
|
||||
public function getTables()
|
||||
{
|
||||
$res = $this->driver->query("
|
||||
SELECT TABLE_NAME, TABLE_TYPE
|
||||
FROM INFORMATION_SCHEMA.TABLES
|
||||
");
|
||||
$tables = array();
|
||||
while ($row = $res->fetch(FALSE)) {
|
||||
$tables[] = array(
|
||||
'name' => $row[0],
|
||||
'view' => isset($row[1]) && $row[1] === 'VIEW',
|
||||
);
|
||||
}
|
||||
return $tables;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns count of rows in a table
|
||||
* @param string
|
||||
* @return integer
|
||||
*/
|
||||
public function getTableCount($table, $fallback=true)
|
||||
{
|
||||
if (empty($table)) {
|
||||
return false;
|
||||
}
|
||||
$result = $this->driver->query("
|
||||
SELECT MAX(rowcnt)
|
||||
FROM sys.sysindexes
|
||||
WHERE id=OBJECT_ID({$this->driver->escape($table, dibi::IDENTIFIER)})
|
||||
");
|
||||
$row = $result->fetch(FALSE);
|
||||
|
||||
if (!is_array($row) || count($row) < 1) {
|
||||
if ($fallback) {
|
||||
$row = $this->driver->query("SELECT COUNT(*) FROM {$this->driver->escape($table, dibi::IDENTIFIER)}")->fetch(FALSE);
|
||||
$count = intval($row[0]);
|
||||
} else {
|
||||
$count = false;
|
||||
}
|
||||
} else {
|
||||
$count = intval($row[0]);
|
||||
}
|
||||
|
||||
return $count;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns metadata for all columns in a table.
|
||||
* @param string
|
||||
* @return array
|
||||
*/
|
||||
public function getColumns($table)
|
||||
{
|
||||
$res = $this->driver->query("
|
||||
SELECT * FROM
|
||||
INFORMATION_SCHEMA.COLUMNS
|
||||
WHERE TABLE_NAME = {$this->driver->escape($table, dibi::TEXT)}
|
||||
ORDER BY TABLE_NAME, ORDINAL_POSITION
|
||||
");
|
||||
$columns = array();
|
||||
while ($row = $res->fetch(TRUE)) {
|
||||
$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'
|
||||
);
|
||||
|
||||
if (isset($size_cols[$type])) {
|
||||
if ($size_cols[$type]) {
|
||||
$size = $row[$size_cols[$type]];
|
||||
}
|
||||
}
|
||||
|
||||
$columns[] = array(
|
||||
'name' => $row['COLUMN_NAME'],
|
||||
'table' => $table,
|
||||
'nativetype' => $type,
|
||||
'size' => $size,
|
||||
'unsigned' => NULL,
|
||||
'nullable' => $row['IS_NULLABLE'] === 'YES',
|
||||
'default' => $row['COLUMN_DEFAULT'],
|
||||
'autoincrement' => false,
|
||||
'vendor' => $row,
|
||||
);
|
||||
}
|
||||
|
||||
return $columns;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns metadata for all indexes in a table.
|
||||
* @param string
|
||||
* @return array
|
||||
*/
|
||||
public function getIndexes($table)
|
||||
{
|
||||
$res = $this->driver->query(
|
||||
"SELECT ind.name index_name, ind.index_id, ic.index_column_id,
|
||||
col.name column_name, ind.is_unique, ind.is_primary_key
|
||||
FROM sys.indexes ind
|
||||
INNER JOIN sys.index_columns ic ON
|
||||
(ind.object_id = ic.object_id AND ind.index_id = ic.index_id)
|
||||
INNER JOIN sys.columns col ON
|
||||
(ic.object_id = col.object_id and ic.column_id = col.column_id)
|
||||
INNER JOIN sys.tables t ON
|
||||
(ind.object_id = t.object_id)
|
||||
WHERE t.name = {$this->driver->escape($table, dibi::TEXT)}
|
||||
AND t.is_ms_shipped = 0
|
||||
ORDER BY
|
||||
t.name, ind.name, ind.index_id, ic.index_column_id
|
||||
");
|
||||
|
||||
$indexes = array();
|
||||
while ($row = $res->fetch(TRUE)) {
|
||||
$index_name = $row['index_name'];
|
||||
|
||||
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]['columns'] = array();
|
||||
}
|
||||
$indexes[$index_name]['columns'][] = $row['column_name'];
|
||||
}
|
||||
|
||||
return array_values($indexes);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns metadata for all foreign keys in a table.
|
||||
* @param string
|
||||
* @return array
|
||||
*/
|
||||
public function getForeignKeys($table)
|
||||
{
|
||||
$res = $this->driver->query("
|
||||
SELECT f.name AS foreign_key,
|
||||
OBJECT_NAME(f.parent_object_id) AS table_name,
|
||||
COL_NAME(fc.parent_object_id,
|
||||
fc.parent_column_id) AS column_name,
|
||||
OBJECT_NAME (f.referenced_object_id) AS reference_table_name,
|
||||
COL_NAME(fc.referenced_object_id,
|
||||
fc.referenced_column_id) AS reference_column_name,
|
||||
fc.*
|
||||
FROM sys.foreign_keys AS f
|
||||
INNER JOIN sys.foreign_key_columns AS fc
|
||||
ON f.OBJECT_ID = fc.constraint_object_id
|
||||
WHERE OBJECT_NAME(f.parent_object_id) = {$this->driver->escape($table, dibi::TEXT)}
|
||||
");
|
||||
|
||||
$keys = array();
|
||||
while ($row = $res->fetch(TRUE)) {
|
||||
$key_name = $row['foreign_key'];
|
||||
|
||||
if (!isset($keys[$key_name])) {
|
||||
$keys[$key_name]['name'] = $row['foreign_key']; // foreign key name
|
||||
$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;
|
||||
} else {
|
||||
$keys[$key_name]['local'][] = $row['column_name']; // local columns
|
||||
$keys[$key_name]['foreign'][] = $row['reference_column_name']; // referenced columns
|
||||
}
|
||||
}
|
||||
return array_values($keys);
|
||||
}
|
||||
|
||||
}
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the "dibi" - smart database abstraction layer.
|
||||
*
|
||||
* Copyright (c) 2005, 2010 David Grudl (http://davidgrudl.com)
|
||||
*
|
||||
* @package dibi\drivers
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* The dibi reflector for MsSQL databases.
|
||||
*
|
||||
* @author Steven Bredenberg
|
||||
* @package dibi\drivers
|
||||
* @internal
|
||||
*/
|
||||
class DibiMsSqlReflector extends DibiObject implements IDibiReflector
|
||||
{
|
||||
/** @var IDibiDriver */
|
||||
private $driver;
|
||||
|
||||
|
||||
public function __construct(IDibiDriver $driver)
|
||||
{
|
||||
$this->driver = $driver;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns list of tables.
|
||||
* @return array
|
||||
*/
|
||||
public function getTables()
|
||||
{
|
||||
$res = $this->driver->query("
|
||||
SELECT TABLE_NAME, TABLE_TYPE
|
||||
FROM INFORMATION_SCHEMA.TABLES
|
||||
");
|
||||
$tables = array();
|
||||
while ($row = $res->fetch(FALSE)) {
|
||||
$tables[] = array(
|
||||
'name' => $row[0],
|
||||
'view' => isset($row[1]) && $row[1] === 'VIEW',
|
||||
);
|
||||
}
|
||||
return $tables;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns count of rows in a table
|
||||
* @param string
|
||||
* @return integer
|
||||
*/
|
||||
public function getTableCount($table, $fallback=true)
|
||||
{
|
||||
if (empty($table)) {
|
||||
return false;
|
||||
}
|
||||
$result = $this->driver->query("
|
||||
SELECT MAX(rowcnt)
|
||||
FROM sys.sysindexes
|
||||
WHERE id=OBJECT_ID({$this->driver->escape($table, dibi::IDENTIFIER)})
|
||||
");
|
||||
$row = $result->fetch(FALSE);
|
||||
|
||||
if (!is_array($row) || count($row) < 1) {
|
||||
if ($fallback) {
|
||||
$row = $this->driver->query("SELECT COUNT(*) FROM {$this->driver->escape($table, dibi::IDENTIFIER)}")->fetch(FALSE);
|
||||
$count = intval($row[0]);
|
||||
} else {
|
||||
$count = false;
|
||||
}
|
||||
} else {
|
||||
$count = intval($row[0]);
|
||||
}
|
||||
|
||||
return $count;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns metadata for all columns in a table.
|
||||
* @param string
|
||||
* @return array
|
||||
*/
|
||||
public function getColumns($table)
|
||||
{
|
||||
$res = $this->driver->query("
|
||||
SELECT * FROM
|
||||
INFORMATION_SCHEMA.COLUMNS
|
||||
WHERE TABLE_NAME = {$this->driver->escape($table, dibi::TEXT)}
|
||||
ORDER BY TABLE_NAME, ORDINAL_POSITION
|
||||
");
|
||||
$columns = array();
|
||||
while ($row = $res->fetch(TRUE)) {
|
||||
$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'
|
||||
);
|
||||
|
||||
if (isset($size_cols[$type])) {
|
||||
if ($size_cols[$type]) {
|
||||
$size = $row[$size_cols[$type]];
|
||||
}
|
||||
}
|
||||
|
||||
$columns[] = array(
|
||||
'name' => $row['COLUMN_NAME'],
|
||||
'table' => $table,
|
||||
'nativetype' => $type,
|
||||
'size' => $size,
|
||||
'unsigned' => NULL,
|
||||
'nullable' => $row['IS_NULLABLE'] === 'YES',
|
||||
'default' => $row['COLUMN_DEFAULT'],
|
||||
'autoincrement' => false,
|
||||
'vendor' => $row,
|
||||
);
|
||||
}
|
||||
|
||||
return $columns;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns metadata for all indexes in a table.
|
||||
* @param string
|
||||
* @return array
|
||||
*/
|
||||
public function getIndexes($table)
|
||||
{
|
||||
$res = $this->driver->query(
|
||||
"SELECT ind.name index_name, ind.index_id, ic.index_column_id,
|
||||
col.name column_name, ind.is_unique, ind.is_primary_key
|
||||
FROM sys.indexes ind
|
||||
INNER JOIN sys.index_columns ic ON
|
||||
(ind.object_id = ic.object_id AND ind.index_id = ic.index_id)
|
||||
INNER JOIN sys.columns col ON
|
||||
(ic.object_id = col.object_id and ic.column_id = col.column_id)
|
||||
INNER JOIN sys.tables t ON
|
||||
(ind.object_id = t.object_id)
|
||||
WHERE t.name = {$this->driver->escape($table, dibi::TEXT)}
|
||||
AND t.is_ms_shipped = 0
|
||||
ORDER BY
|
||||
t.name, ind.name, ind.index_id, ic.index_column_id
|
||||
");
|
||||
|
||||
$indexes = array();
|
||||
while ($row = $res->fetch(TRUE)) {
|
||||
$index_name = $row['index_name'];
|
||||
|
||||
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]['columns'] = array();
|
||||
}
|
||||
$indexes[$index_name]['columns'][] = $row['column_name'];
|
||||
}
|
||||
|
||||
return array_values($indexes);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns metadata for all foreign keys in a table.
|
||||
* @param string
|
||||
* @return array
|
||||
*/
|
||||
public function getForeignKeys($table)
|
||||
{
|
||||
$res = $this->driver->query("
|
||||
SELECT f.name AS foreign_key,
|
||||
OBJECT_NAME(f.parent_object_id) AS table_name,
|
||||
COL_NAME(fc.parent_object_id,
|
||||
fc.parent_column_id) AS column_name,
|
||||
OBJECT_NAME (f.referenced_object_id) AS reference_table_name,
|
||||
COL_NAME(fc.referenced_object_id,
|
||||
fc.referenced_column_id) AS reference_column_name,
|
||||
fc.*
|
||||
FROM sys.foreign_keys AS f
|
||||
INNER JOIN sys.foreign_key_columns AS fc
|
||||
ON f.OBJECT_ID = fc.constraint_object_id
|
||||
WHERE OBJECT_NAME(f.parent_object_id) = {$this->driver->escape($table, dibi::TEXT)}
|
||||
");
|
||||
|
||||
$keys = array();
|
||||
while ($row = $res->fetch(TRUE)) {
|
||||
$key_name = $row['foreign_key'];
|
||||
|
||||
if (!isset($keys[$key_name])) {
|
||||
$keys[$key_name]['name'] = $row['foreign_key']; // foreign key name
|
||||
$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;
|
||||
} else {
|
||||
$keys[$key_name]['local'][] = $row['column_name']; // local columns
|
||||
$keys[$key_name]['foreign'][] = $row['reference_column_name']; // referenced columns
|
||||
}
|
||||
}
|
||||
return array_values($keys);
|
||||
}
|
||||
|
||||
}
|
@@ -6,9 +6,6 @@
|
||||
*/
|
||||
|
||||
|
||||
require_once dirname(__FILE__) . '/DibiMsSql2005Reflector.php';
|
||||
|
||||
|
||||
/**
|
||||
* The dibi driver for MS SQL Driver 2005 database.
|
||||
*
|
||||
@@ -67,11 +64,6 @@ class DibiMsSql2005Driver extends DibiObject implements IDibiDriver, IDibiResult
|
||||
$this->connection = $config['resource'];
|
||||
|
||||
} else {
|
||||
// Default values
|
||||
if (!isset($config['options']['CharacterSet'])) {
|
||||
$config['options']['CharacterSet'] = 'UTF-8';
|
||||
}
|
||||
|
||||
$this->connection = sqlsrv_connect($config['host'], (array) $config['options']);
|
||||
}
|
||||
|
||||
@@ -191,7 +183,7 @@ class DibiMsSql2005Driver extends DibiObject implements IDibiDriver, IDibiResult
|
||||
*/
|
||||
public function getReflector()
|
||||
{
|
||||
return new DibiMssql2005Reflector($this);
|
||||
throw new DibiNotSupportedException;
|
||||
}
|
||||
|
||||
|
||||
@@ -227,7 +219,7 @@ class DibiMsSql2005Driver extends DibiObject implements IDibiDriver, IDibiResult
|
||||
|
||||
case dibi::IDENTIFIER:
|
||||
// @see http://msdn.microsoft.com/en-us/library/ms176027.aspx
|
||||
return '[' . str_replace(']', ']]', $value) . ']';
|
||||
return '[' . str_replace(array('[', ']'), array('[[', ']]'), $value) . ']';
|
||||
|
||||
case dibi::BOOL:
|
||||
return $value ? 1 : 0;
|
||||
@@ -281,7 +273,7 @@ class DibiMsSql2005Driver extends DibiObject implements IDibiDriver, IDibiResult
|
||||
{
|
||||
// offset support is missing
|
||||
if ($limit >= 0) {
|
||||
$sql = 'SELECT TOP ' . (int) $limit . ' * FROM (' . $sql . ') AS T ';
|
||||
$sql = 'SELECT TOP ' . (int) $limit . ' * FROM (' . $sql . ')';
|
||||
}
|
||||
|
||||
if ($offset) {
|
@@ -6,7 +6,7 @@
|
||||
*/
|
||||
|
||||
|
||||
require_once dirname(__FILE__) . '/DibiMySqlReflector.php';
|
||||
require_once dirname(__FILE__) . '/mysql.reflector.php';
|
||||
|
||||
|
||||
/**
|
||||
@@ -73,12 +73,9 @@ class DibiMySqlDriver extends DibiObject implements IDibiDriver, IDibiResultDriv
|
||||
} else {
|
||||
// default values
|
||||
DibiConnection::alias($config, 'flags', 'options');
|
||||
$config += array(
|
||||
'charset' => 'utf8',
|
||||
'timezone' => date('P'),
|
||||
'username' => ini_get('mysql.default_user'),
|
||||
'password' => ini_get('mysql.default_password'),
|
||||
);
|
||||
if (!isset($config['charset'])) $config['charset'] = 'utf8';
|
||||
if (!isset($config['username'])) $config['username'] = ini_get('mysql.default_user');
|
||||
if (!isset($config['password'])) $config['password'] = ini_get('mysql.default_password');
|
||||
if (!isset($config['host'])) {
|
||||
$host = ini_get('mysql.default_host');
|
||||
if ($host) {
|
||||
@@ -130,9 +127,7 @@ class DibiMySqlDriver extends DibiObject implements IDibiDriver, IDibiResultDriv
|
||||
$this->query("SET sql_mode='$config[sqlmode]'");
|
||||
}
|
||||
|
||||
if (isset($config['timezone'])) {
|
||||
$this->query("SET time_zone='$config[timezone]'");
|
||||
}
|
||||
$this->query("SET time_zone='" . date('P') . "'");
|
||||
|
||||
$this->buffered = empty($config['unbuffered']);
|
||||
}
|
@@ -6,7 +6,7 @@
|
||||
*/
|
||||
|
||||
|
||||
require_once dirname(__FILE__) . '/DibiMySqlReflector.php';
|
||||
require_once dirname(__FILE__) . '/mysql.reflector.php';
|
||||
|
||||
|
||||
/**
|
||||
@@ -74,14 +74,11 @@ class DibiMySqliDriver extends DibiObject implements IDibiDriver, IDibiResultDri
|
||||
|
||||
} else {
|
||||
// default values
|
||||
$config += array(
|
||||
'charset' => 'utf8',
|
||||
'timezone' => date('P'),
|
||||
'username' => ini_get('mysqli.default_user'),
|
||||
'password' => ini_get('mysqli.default_pw'),
|
||||
'socket' => ini_get('mysqli.default_socket'),
|
||||
'port' => NULL,
|
||||
);
|
||||
if (!isset($config['charset'])) $config['charset'] = 'utf8';
|
||||
if (!isset($config['username'])) $config['username'] = ini_get('mysqli.default_user');
|
||||
if (!isset($config['password'])) $config['password'] = ini_get('mysqli.default_pw');
|
||||
if (!isset($config['socket'])) $config['socket'] = ini_get('mysqli.default_socket');
|
||||
if (!isset($config['port'])) $config['port'] = NULL;
|
||||
if (!isset($config['host'])) {
|
||||
$host = ini_get('mysqli.default_host');
|
||||
if ($host) {
|
||||
@@ -129,9 +126,7 @@ class DibiMySqliDriver extends DibiObject implements IDibiDriver, IDibiResultDri
|
||||
$this->query("SET sql_mode='$config[sqlmode]'");
|
||||
}
|
||||
|
||||
if (isset($config['timezone'])) {
|
||||
$this->query("SET time_zone='$config[timezone]'");
|
||||
}
|
||||
$this->query("SET time_zone='" . date('P') . "'");
|
||||
|
||||
$this->buffered = empty($config['unbuffered']);
|
||||
}
|
@@ -60,11 +60,9 @@ class DibiOdbcDriver extends DibiObject implements IDibiDriver, IDibiResultDrive
|
||||
$this->connection = $config['resource'];
|
||||
} else {
|
||||
// default values
|
||||
$config += array(
|
||||
'username' => ini_get('odbc.default_user'),
|
||||
'password' => ini_get('odbc.default_pw'),
|
||||
'dsn' => ini_get('odbc.default_db'),
|
||||
);
|
||||
if (!isset($config['username'])) $config['username'] = ini_get('odbc.default_user');
|
||||
if (!isset($config['password'])) $config['password'] = ini_get('odbc.default_pw');
|
||||
if (!isset($config['dsn'])) $config['dsn'] = ini_get('odbc.default_db');
|
||||
|
||||
if (empty($config['persistent'])) {
|
||||
$this->connection = @odbc_connect($config['dsn'], $config['username'], $config['password']); // intentionally @
|
@@ -17,7 +17,6 @@
|
||||
* - formatDate => how to format date in SQL (@see date)
|
||||
* - formatDateTime => how to format datetime in SQL (@see date)
|
||||
* - resource (resource) => existing connection resource
|
||||
* - persistent => Creates persistent connections with oci_pconnect instead of oci_new_connect
|
||||
* - lazy, profiler, result, substitutes, ... => see DibiConnection options
|
||||
*
|
||||
* @author David Grudl
|
||||
@@ -65,10 +64,8 @@ class DibiOracleDriver extends DibiObject implements IDibiDriver, IDibiResultDri
|
||||
|
||||
if (isset($config['resource'])) {
|
||||
$this->connection = $config['resource'];
|
||||
} elseif (empty($config['persistent'])) {
|
||||
$this->connection = @oci_new_connect($config['username'], $config['password'], $config['database'], $config['charset']); // intentionally @
|
||||
} else {
|
||||
$this->connection = @oci_pconnect($config['username'], $config['password'], $config['database'], $config['charset']); // intentionally @
|
||||
$this->connection = @oci_new_connect($config['username'], $config['password'], $config['database'], $config['charset']); // intentionally @
|
||||
}
|
||||
|
||||
if (!$this->connection) {
|
||||
@@ -284,9 +281,7 @@ class DibiOracleDriver extends DibiObject implements IDibiDriver, IDibiResultDri
|
||||
{
|
||||
if ($offset > 0) {
|
||||
// see http://www.oracle.com/technology/oramag/oracle/06-sep/o56asktom.html
|
||||
$sql = 'SELECT * FROM (SELECT t.*, ROWNUM AS "__rnum" FROM (' . $sql . ') t '
|
||||
. ($limit >= 0 ? 'WHERE ROWNUM <= ' . ((int) $offset + (int) $limit) : '')
|
||||
. ') WHERE "__rnum" > '. (int) $offset;
|
||||
$sql = 'SELECT * FROM (SELECT t.*, ROWNUM AS "__rnum" FROM (' . $sql . ') t ' . ($limit >= 0 ? 'WHERE ROWNUM <= ' . ((int) $offset + (int) $limit) : '') . ') WHERE "__rnum" > '. (int) $offset;
|
||||
|
||||
} elseif ($limit >= 0) {
|
||||
$sql = 'SELECT * FROM (' . $sql . ') WHERE ROWNUM <= ' . (int) $limit;
|
@@ -6,8 +6,8 @@
|
||||
*/
|
||||
|
||||
|
||||
require_once dirname(__FILE__) . '/DibiMySqlReflector.php';
|
||||
require_once dirname(__FILE__) . '/DibiSqliteReflector.php';
|
||||
require_once dirname(__FILE__) . '/mysql.reflector.php';
|
||||
require_once dirname(__FILE__) . '/sqlite.reflector.php';
|
||||
|
||||
|
||||
/**
|
||||
@@ -255,6 +255,7 @@ class DibiPdoDriver extends DibiObject implements IDibiDriver, IDibiResultDriver
|
||||
case 'mysql':
|
||||
return '`' . str_replace('`', '``', $value) . '`';
|
||||
|
||||
case 'oci':
|
||||
case 'pgsql':
|
||||
return '"' . str_replace('"', '""', $value) . '"';
|
||||
|
||||
@@ -263,13 +264,9 @@ class DibiPdoDriver extends DibiObject implements IDibiDriver, IDibiResultDriver
|
||||
return '[' . strtr($value, '[]', ' ') . ']';
|
||||
|
||||
case 'odbc':
|
||||
case 'oci': // TODO: not tested
|
||||
case 'mssql':
|
||||
return '[' . str_replace(array('[', ']'), array('[[', ']]'), $value) . ']';
|
||||
|
||||
case 'sqlsrv':
|
||||
return '[' . str_replace(']', ']]', $value) . ']';
|
||||
|
||||
default:
|
||||
return $value;
|
||||
}
|
||||
@@ -349,9 +346,7 @@ class DibiPdoDriver extends DibiObject implements IDibiDriver, IDibiResultDriver
|
||||
|
||||
case 'oci':
|
||||
if ($offset > 0) {
|
||||
$sql = 'SELECT * FROM (SELECT t.*, ROWNUM AS "__rnum" FROM (' . $sql . ') t '
|
||||
. ($limit >= 0 ? 'WHERE ROWNUM <= ' . ((int) $offset + (int) $limit) : '')
|
||||
. ') WHERE "__rnum" > '. (int) $offset;
|
||||
$sql = 'SELECT * FROM (SELECT t.*, ROWNUM AS "__rnum" FROM (' . $sql . ') t ' . ($limit >= 0 ? 'WHERE ROWNUM <= ' . ((int) $offset + (int) $limit) : '') . ') WHERE "__rnum" > '. (int) $offset;
|
||||
} elseif ($limit >= 0) {
|
||||
$sql = 'SELECT * FROM (' . $sql . ') WHERE ROWNUM <= ' . (int) $limit;
|
||||
}
|
||||
@@ -359,9 +354,8 @@ class DibiPdoDriver extends DibiObject implements IDibiDriver, IDibiResultDriver
|
||||
|
||||
case 'odbc':
|
||||
case 'mssql':
|
||||
case 'sqlsrv':
|
||||
if ($offset < 1) {
|
||||
$sql = 'SELECT TOP ' . (int) $limit . ' * FROM (' . $sql . ') t';
|
||||
$sql = 'SELECT TOP ' . (int) $limit . ' * FROM (' . $sql . ')';
|
||||
break;
|
||||
}
|
||||
// intentionally break omitted
|
@@ -61,9 +61,7 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver, IDibiResultDr
|
||||
$this->connection = $config['resource'];
|
||||
|
||||
} else {
|
||||
$config += array(
|
||||
'charset' => 'utf8',
|
||||
);
|
||||
if (!isset($config['charset'])) $config['charset'] = 'utf8';
|
||||
if (isset($config['string'])) {
|
||||
$string = $config['string'];
|
||||
} else {
|
||||
@@ -482,7 +480,7 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver, IDibiResultDr
|
||||
*/
|
||||
public function getColumns($table)
|
||||
{
|
||||
$_table = $this->escape($table, dibi::TEXT);
|
||||
$_table = $this->escape($this->escape($table, dibi::IDENTIFIER), dibi::TEXT);
|
||||
$res = $this->query("
|
||||
SELECT indkey
|
||||
FROM pg_class
|
||||
@@ -522,7 +520,7 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver, IDibiResultDr
|
||||
*/
|
||||
public function getIndexes($table)
|
||||
{
|
||||
$_table = $this->escape($table, dibi::TEXT);
|
||||
$_table = $this->escape($this->escape($table, dibi::IDENTIFIER), dibi::TEXT);
|
||||
$res = $this->query("
|
||||
SELECT ordinal_position, column_name
|
||||
FROM information_schema.columns
|
||||
@@ -563,7 +561,7 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver, IDibiResultDr
|
||||
*/
|
||||
public function getForeignKeys($table)
|
||||
{
|
||||
$_table = $this->escape($table, dibi::TEXT);
|
||||
$_table = $this->escape($this->escape($table, dibi::IDENTIFIER), dibi::TEXT);
|
||||
|
||||
$res = $this->query("
|
||||
SELECT
|
@@ -6,7 +6,7 @@
|
||||
*/
|
||||
|
||||
|
||||
require_once dirname(__FILE__) . '/DibiSqliteReflector.php';
|
||||
require_once dirname(__FILE__) . '/sqlite.reflector.php';
|
||||
|
||||
|
||||
/**
|
||||
@@ -285,7 +285,7 @@ class DibiSqliteDriver extends DibiObject implements IDibiDriver, IDibiResultDri
|
||||
public function applyLimit(& $sql, $limit, $offset)
|
||||
{
|
||||
if ($limit >= 0 || $offset > 0) {
|
||||
$sql .= ' LIMIT ' . (int) $limit . ($offset > 0 ? ' OFFSET ' . (int) $offset : '');
|
||||
$sql .= ' LIMIT ' . $limit . ($offset > 0 ? ' OFFSET ' . (int) $offset : '');
|
||||
}
|
||||
}
|
||||
|
@@ -6,7 +6,7 @@
|
||||
*/
|
||||
|
||||
|
||||
require_once dirname(__FILE__) . '/DibiSqliteReflector.php';
|
||||
require_once dirname(__FILE__) . '/sqlite.reflector.php';
|
||||
|
||||
|
||||
/**
|
||||
@@ -281,7 +281,7 @@ class DibiSqlite3Driver extends DibiObject implements IDibiDriver, IDibiResultDr
|
||||
public function applyLimit(& $sql, $limit, $offset)
|
||||
{
|
||||
if ($limit >= 0 || $offset > 0) {
|
||||
$sql .= ' LIMIT ' . (int) $limit . ($offset > 0 ? ' OFFSET ' . (int) $offset : '');
|
||||
$sql .= ' LIMIT ' . $limit . ($offset > 0 ? ' OFFSET ' . (int) $offset : '');
|
||||
}
|
||||
}
|
||||
|
@@ -83,10 +83,10 @@ class DibiConnection extends DibiObject
|
||||
$config['driver'] = dibi::$defaultDriver;
|
||||
}
|
||||
|
||||
$class = preg_replace(array('#\W#', '#sql#'), array('_', 'Sql'), ucfirst(strtolower($config['driver'])));
|
||||
$class = "Dibi{$class}Driver";
|
||||
$driver = preg_replace('#[^a-z0-9_]#', '_', strtolower($config['driver']));
|
||||
$class = "Dibi" . $driver . "Driver";
|
||||
if (!class_exists($class, FALSE)) {
|
||||
include_once dirname(__FILE__) . "/../drivers/$class.php";
|
||||
include_once dirname(__FILE__) . "/../drivers/$driver.php";
|
||||
|
||||
if (!class_exists($class, FALSE)) {
|
||||
throw new DibiException("Unable to create instance of dibi driver '$class'.");
|
||||
|
@@ -569,13 +569,13 @@ class DibiColumnInfo extends DibiObject
|
||||
static $patterns = array(
|
||||
'^_' => dibi::TEXT, // PostgreSQL arrays
|
||||
'BYTEA|BLOB|BIN' => dibi::BINARY,
|
||||
'TEXT|CHAR|POINT|INTERVAL' => dibi::TEXT,
|
||||
'YEAR|BYTE|COUNTER|SERIAL|INT|LONG|SHORT' => dibi::INTEGER,
|
||||
'TEXT|CHAR' => dibi::TEXT,
|
||||
'YEAR|BYTE|COUNTER|SERIAL|INT|LONG' => dibi::INTEGER,
|
||||
'CURRENCY|REAL|MONEY|FLOAT|DOUBLE|DECIMAL|NUMERIC|NUMBER' => dibi::FLOAT,
|
||||
'^TIME$' => dibi::TIME,
|
||||
'TIME' => dibi::DATETIME, // DATETIME, TIMESTAMP
|
||||
'DATE' => dibi::DATE,
|
||||
'BOOL' => dibi::BOOL,
|
||||
'BOOL|BIT' => dibi::BOOL,
|
||||
);
|
||||
|
||||
foreach ($patterns as $s => $val) {
|
||||
|
@@ -49,9 +49,6 @@ class DibiResult extends DibiObject implements IDataSource
|
||||
/** @var string returned object class */
|
||||
private $rowClass = 'DibiRow';
|
||||
|
||||
/** @var Callback returned object factory*/
|
||||
private $rowFactory;
|
||||
|
||||
/** @var array format */
|
||||
private $formats = array();
|
||||
|
||||
@@ -187,18 +184,6 @@ class DibiResult extends DibiObject implements IDataSource
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set a factory to create fetched object instances. These should extend the DibiRow class.
|
||||
* @param callback
|
||||
* @return self
|
||||
*/
|
||||
public function setRowFactory($callback)
|
||||
{
|
||||
$this->rowFactory = $callback;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Fetches the row at current position, process optional type conversion.
|
||||
* and moves the internal cursor to the next position
|
||||
@@ -212,9 +197,7 @@ class DibiResult extends DibiObject implements IDataSource
|
||||
}
|
||||
$this->fetched = TRUE;
|
||||
$this->normalize($row);
|
||||
if ($this->rowFactory) {
|
||||
return call_user_func($this->rowFactory, $row);
|
||||
} elseif ($this->rowClass) {
|
||||
if ($this->rowClass) {
|
||||
$row = new $this->rowClass($row);
|
||||
}
|
||||
return $row;
|
||||
@@ -471,7 +454,7 @@ class DibiResult extends DibiObject implements IDataSource
|
||||
}
|
||||
|
||||
do {
|
||||
$data[ (string) $row[$key] ] = $row[$value];
|
||||
$data[ $row[$key] ] = $row[$value];
|
||||
} while ($row = $this->fetch());
|
||||
|
||||
return $data;
|
||||
@@ -626,70 +609,37 @@ class DibiResult extends DibiObject implements IDataSource
|
||||
|
||||
|
||||
/**
|
||||
* Displays complete result set as HTML or text table for debug purposes.
|
||||
* Displays complete result set as HTML table for debug purposes.
|
||||
* @return void
|
||||
*/
|
||||
final public function dump()
|
||||
{
|
||||
$i = 0;
|
||||
$this->seek(0);
|
||||
if (PHP_SAPI === 'cli') {
|
||||
$hasColors = (substr(getenv('TERM'), 0, 5) === 'xterm');
|
||||
$maxLen = 0;
|
||||
while ($row = $this->fetch()) {
|
||||
if ($i === 0) {
|
||||
foreach ($row as $col => $foo) {
|
||||
$len = mb_strlen($col);
|
||||
$maxLen = max($len, $maxLen);
|
||||
}
|
||||
}
|
||||
|
||||
if ($hasColors) {
|
||||
echo "\033[1;37m#row: $i\033[0m\n";
|
||||
} else {
|
||||
echo "#row: $i\n";
|
||||
}
|
||||
|
||||
foreach ($row as $col => $val) {
|
||||
$spaces = $maxLen - mb_strlen($col) + 2;
|
||||
echo "$col" . str_repeat(" ", $spaces) . "$val\n";
|
||||
}
|
||||
|
||||
echo "\n";
|
||||
$i++;
|
||||
}
|
||||
|
||||
while ($row = $this->fetch()) {
|
||||
if ($i === 0) {
|
||||
echo "empty result set\n";
|
||||
}
|
||||
echo "\n";
|
||||
echo "\n<table class=\"dump\">\n<thead>\n\t<tr>\n\t\t<th>#row</th>\n";
|
||||
|
||||
foreach ($row as $col => $foo) {
|
||||
echo "\t\t<th>" . htmlSpecialChars($col) . "</th>\n";
|
||||
}
|
||||
|
||||
echo "\t</tr>\n</thead>\n<tbody>\n";
|
||||
}
|
||||
|
||||
echo "\t<tr>\n\t\t<th>", $i, "</th>\n";
|
||||
foreach ($row as $col) {
|
||||
//if (is_object($col)) $col = $col->__toString();
|
||||
echo "\t\t<td>", htmlSpecialChars($col), "</td>\n";
|
||||
}
|
||||
echo "\t</tr>\n";
|
||||
$i++;
|
||||
}
|
||||
|
||||
if ($i === 0) {
|
||||
echo '<p><em>empty result set</em></p>';
|
||||
} else {
|
||||
while ($row = $this->fetch()) {
|
||||
if ($i === 0) {
|
||||
echo "\n<table class=\"dump\">\n<thead>\n\t<tr>\n\t\t<th>#row</th>\n";
|
||||
|
||||
foreach ($row as $col => $foo) {
|
||||
echo "\t\t<th>" . htmlSpecialChars($col) . "</th>\n";
|
||||
}
|
||||
|
||||
echo "\t</tr>\n</thead>\n<tbody>\n";
|
||||
}
|
||||
|
||||
echo "\t<tr>\n\t\t<th>", $i, "</th>\n";
|
||||
foreach ($row as $col) {
|
||||
//if (is_object($col)) $col = $col->__toString();
|
||||
echo "\t\t<td>", htmlSpecialChars($col), "</td>\n";
|
||||
}
|
||||
echo "\t</tr>\n";
|
||||
$i++;
|
||||
}
|
||||
|
||||
if ($i === 0) {
|
||||
echo '<p><em>empty result set</em></p>';
|
||||
} else {
|
||||
echo "</tbody>\n</table>\n";
|
||||
}
|
||||
echo "</tbody>\n</table>\n";
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -4,16 +4,16 @@
|
||||
|
||||
<?php
|
||||
|
||||
require dirname(__FILE__) . '/Nette/Debugger.php';
|
||||
require dirname(__FILE__) . '/../dibi/dibi.php';
|
||||
require_once 'Nette/Debugger.php';
|
||||
require_once '../dibi/dibi.php';
|
||||
|
||||
|
||||
// connects to SQlite using dibi class
|
||||
echo '<p>Connecting to Sqlite: ';
|
||||
try {
|
||||
dibi::connect(array(
|
||||
'driver' => 'sqlite3',
|
||||
'database' => 'data/sample.s3db',
|
||||
'driver' => 'sqlite',
|
||||
'database' => 'data/sample.sdb',
|
||||
));
|
||||
echo 'OK';
|
||||
|
||||
@@ -27,8 +27,8 @@ echo "</p>\n";
|
||||
echo '<p>Connecting to Sqlite: ';
|
||||
try {
|
||||
$connection = new DibiConnection(array(
|
||||
'driver' => 'sqlite3',
|
||||
'database' => 'data/sample.s3db',
|
||||
'driver' => 'sqlite',
|
||||
'database' => 'data/sample.sdb',
|
||||
));
|
||||
echo 'OK';
|
||||
|
||||
|
Binary file not shown.
Before Width: | Height: | Size: 1.1 KiB |
@@ -4,13 +4,13 @@
|
||||
|
||||
<?php
|
||||
|
||||
require dirname(__FILE__) . '/Nette/Debugger.php';
|
||||
require dirname(__FILE__) . '/../dibi/dibi.php';
|
||||
require_once 'Nette/Debugger.php';
|
||||
require_once '../dibi/dibi.php';
|
||||
|
||||
|
||||
dibi::connect(array(
|
||||
'driver' => 'sqlite3',
|
||||
'database' => 'data/sample.s3db',
|
||||
'driver' => 'sqlite',
|
||||
'database' => 'data/sample.sdb',
|
||||
));
|
||||
|
||||
|
||||
|
@@ -4,13 +4,13 @@
|
||||
|
||||
<?php
|
||||
|
||||
require dirname(__FILE__) . '/Nette/Debugger.php';
|
||||
require dirname(__FILE__) . '/../dibi/dibi.php';
|
||||
require_once 'Nette/Debugger.php';
|
||||
require_once '../dibi/dibi.php';
|
||||
|
||||
|
||||
dibi::connect(array(
|
||||
'driver' => 'sqlite3',
|
||||
'database' => 'data/sample.s3db',
|
||||
'driver' => 'sqlite',
|
||||
'database' => 'data/sample.sdb',
|
||||
));
|
||||
|
||||
|
||||
|
@@ -4,14 +4,14 @@
|
||||
|
||||
<?php
|
||||
|
||||
require dirname(__FILE__) . '/Nette/Debugger.php';
|
||||
require dirname(__FILE__) . '/../dibi/dibi.php';
|
||||
require_once 'Nette/Debugger.php';
|
||||
require_once '../dibi/dibi.php';
|
||||
|
||||
ndebug();
|
||||
|
||||
dibi::connect(array(
|
||||
'driver' => 'sqlite3',
|
||||
'database' => 'data/sample.s3db',
|
||||
'driver' => 'sqlite',
|
||||
'database' => 'data/sample.sdb',
|
||||
));
|
||||
|
||||
|
||||
|
@@ -4,13 +4,13 @@
|
||||
|
||||
<?php
|
||||
|
||||
require dirname(__FILE__) . '/Nette/Debugger.php';
|
||||
require dirname(__FILE__) . '/../dibi/dibi.php';
|
||||
require_once 'Nette/Debugger.php';
|
||||
require_once '../dibi/dibi.php';
|
||||
|
||||
|
||||
dibi::connect(array(
|
||||
'driver' => 'sqlite3',
|
||||
'database' => 'data/sample.s3db',
|
||||
'driver' => 'sqlite',
|
||||
'database' => 'data/sample.sdb',
|
||||
));
|
||||
|
||||
|
||||
|
@@ -10,8 +10,8 @@
|
||||
|
||||
<?php
|
||||
|
||||
require dirname(__FILE__) . '/Nette/Debugger.php';
|
||||
require dirname(__FILE__) . '/../dibi/dibi.php';
|
||||
require_once 'Nette/Debugger.php';
|
||||
require_once '../dibi/dibi.php';
|
||||
|
||||
|
||||
// enable Nette Debugger
|
||||
@@ -19,8 +19,8 @@ ndebug();
|
||||
|
||||
|
||||
dibi::connect(array(
|
||||
'driver' => 'sqlite3',
|
||||
'database' => 'data/sample.s3db',
|
||||
'driver' => 'sqlite',
|
||||
'database' => 'data/sample.sdb',
|
||||
'profiler' => array(
|
||||
'run' => TRUE,
|
||||
)
|
||||
@@ -32,8 +32,8 @@ dibi::query('SELECT * FROM customers WHERE customer_id < ?', 38);
|
||||
|
||||
|
||||
dibi::connect(array(
|
||||
'driver' => 'sqlite3',
|
||||
'database' => 'data/sample.s3db',
|
||||
'driver' => 'sqlite',
|
||||
'database' => 'data/sample.sdb',
|
||||
'profiler' => array(
|
||||
'run' => TRUE,
|
||||
)
|
||||
|
@@ -1,7 +1,5 @@
|
||||
<!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>
|
||||
@@ -12,8 +10,8 @@
|
||||
|
||||
<?php
|
||||
|
||||
require dirname(__FILE__) . '/Nette/Debugger.php';
|
||||
require dirname(__FILE__) . '/../dibi/dibi.php';
|
||||
require_once 'Nette/Debugger.php';
|
||||
require_once '../dibi/dibi.php';
|
||||
|
||||
|
||||
// enable Nette Debugger
|
||||
@@ -21,8 +19,8 @@ NDebugger::enable();
|
||||
|
||||
|
||||
dibi::connect(array(
|
||||
'driver' => 'sqlite3',
|
||||
'database' => 'data/sample.s3db',
|
||||
'driver' => 'sqlite',
|
||||
'database' => 'data/sample.sdb',
|
||||
'profiler' => array(
|
||||
'run' => TRUE,
|
||||
)
|
||||
|
@@ -4,13 +4,13 @@
|
||||
|
||||
<?php
|
||||
|
||||
require dirname(__FILE__) . '/Nette/Debugger.php';
|
||||
require dirname(__FILE__) . '/../dibi/dibi.php';
|
||||
require_once 'Nette/Debugger.php';
|
||||
require_once '../dibi/dibi.php';
|
||||
|
||||
|
||||
dibi::connect(array(
|
||||
'driver' => 'sqlite3',
|
||||
'database' => 'data/sample.s3db',
|
||||
'driver' => 'sqlite',
|
||||
'database' => 'data/sample.sdb',
|
||||
));
|
||||
|
||||
|
||||
@@ -53,10 +53,3 @@ dibi::test('
|
||||
%else 1 LIMIT 10 %end'
|
||||
);
|
||||
// -> SELECT * FROM customers WHERE LIMIT 10
|
||||
|
||||
|
||||
// IF()
|
||||
dibi::test('UPDATE products SET', array(
|
||||
'price' => array('IF(price_fixed, price, ?)', 123),
|
||||
));
|
||||
// -> SELECT * FROM customers WHERE LIMIT 10
|
||||
|
@@ -4,15 +4,15 @@
|
||||
|
||||
<?php
|
||||
|
||||
require dirname(__FILE__) . '/Nette/Debugger.php';
|
||||
require dirname(__FILE__) . '/../dibi/dibi.php';
|
||||
require_once 'Nette/Debugger.php';
|
||||
require_once '../dibi/dibi.php';
|
||||
|
||||
date_default_timezone_set('Europe/Prague');
|
||||
|
||||
|
||||
dibi::connect(array(
|
||||
'driver' => 'sqlite3',
|
||||
'database' => 'data/sample.s3db',
|
||||
'driver' => 'sqlite',
|
||||
'database' => 'data/sample.sdb',
|
||||
));
|
||||
|
||||
|
||||
|
@@ -4,16 +4,16 @@
|
||||
|
||||
<?php
|
||||
|
||||
require dirname(__FILE__) . '/Nette/Debugger.php';
|
||||
require dirname(__FILE__) . '/../dibi/dibi.php';
|
||||
require_once 'Nette/Debugger.php';
|
||||
require_once '../dibi/dibi.php';
|
||||
|
||||
ndebug();
|
||||
date_default_timezone_set('Europe/Prague');
|
||||
|
||||
|
||||
dibi::connect(array(
|
||||
'driver' => 'sqlite3',
|
||||
'database' => 'data/sample.s3db',
|
||||
'driver' => 'sqlite',
|
||||
'database' => 'data/sample.sdb',
|
||||
));
|
||||
|
||||
|
||||
@@ -27,10 +27,11 @@ $res->setType('customer_id', Dibi::INTEGER)
|
||||
|
||||
dump( $res->fetch() );
|
||||
// outputs:
|
||||
// DibiRow(3) {
|
||||
// customer_id => 1
|
||||
// name => "Dave Lister" (11)
|
||||
// added => "2007-03-11 17:20:03" (19)
|
||||
// object(DibiRow)#3 (3) {
|
||||
// customer_id => int(1)
|
||||
// name => string(11) "Dave Lister"
|
||||
// added => object(DateTime53) {}
|
||||
// }
|
||||
|
||||
|
||||
// using auto-detection (works well with MySQL or other strictly typed databases)
|
||||
@@ -38,7 +39,8 @@ $res = dibi::query('SELECT * FROM [customers]');
|
||||
|
||||
dump( $res->fetch() );
|
||||
// outputs:
|
||||
// DibiRow(3) {
|
||||
// customer_id => 1
|
||||
// name => "Dave Lister" (11)
|
||||
// added => "2007-03-11 17:20:03" (19)
|
||||
// object(DibiRow)#3 (3) {
|
||||
// customer_id => int(1)
|
||||
// name => string(11) "Dave Lister"
|
||||
// added => string(15) "17:20 11.3.2007"
|
||||
// }
|
||||
|
@@ -4,16 +4,16 @@
|
||||
|
||||
<?php
|
||||
|
||||
require dirname(__FILE__) . '/Nette/Debugger.php';
|
||||
require dirname(__FILE__) . '/../dibi/dibi.php';
|
||||
require_once 'Nette/Debugger.php';
|
||||
require_once '../dibi/dibi.php';
|
||||
|
||||
date_default_timezone_set('Europe/Prague');
|
||||
|
||||
|
||||
// CHANGE TO REAL PARAMETERS!
|
||||
dibi::connect(array(
|
||||
'driver' => 'sqlite3',
|
||||
'database' => 'data/sample.s3db',
|
||||
'driver' => 'sqlite',
|
||||
'database' => 'data/sample.sdb',
|
||||
'formatDate' => "'Y-m-d'",
|
||||
'formatDateTime' => "'Y-m-d H-i-s'",
|
||||
));
|
||||
|
@@ -4,14 +4,14 @@
|
||||
|
||||
<?php
|
||||
|
||||
require dirname(__FILE__) . '/Nette/Debugger.php';
|
||||
require dirname(__FILE__) . '/../dibi/dibi.php';
|
||||
require_once 'Nette/Debugger.php';
|
||||
require_once '../dibi/dibi.php';
|
||||
|
||||
ndebug();
|
||||
|
||||
dibi::connect(array(
|
||||
'driver' => 'sqlite3',
|
||||
'database' => 'data/sample.s3db',
|
||||
'driver' => 'sqlite',
|
||||
'database' => 'data/sample.sdb',
|
||||
));
|
||||
|
||||
|
||||
|
@@ -4,15 +4,15 @@
|
||||
|
||||
<?php
|
||||
|
||||
require dirname(__FILE__) . '/Nette/Debugger.php';
|
||||
require dirname(__FILE__) . '/../dibi/dibi.php';
|
||||
require_once 'Nette/Debugger.php';
|
||||
require_once '../dibi/dibi.php';
|
||||
|
||||
date_default_timezone_set('Europe/Prague');
|
||||
|
||||
|
||||
dibi::connect(array(
|
||||
'driver' => 'sqlite3',
|
||||
'database' => 'data/sample.s3db',
|
||||
'driver' => 'sqlite',
|
||||
'database' => 'data/sample.sdb',
|
||||
));
|
||||
|
||||
|
||||
|
@@ -4,13 +4,13 @@
|
||||
|
||||
<?php
|
||||
|
||||
require dirname(__FILE__) . '/Nette/Debugger.php';
|
||||
require dirname(__FILE__) . '/../dibi/dibi.php';
|
||||
require_once 'Nette/Debugger.php';
|
||||
require_once '../dibi/dibi.php';
|
||||
|
||||
|
||||
dibi::connect(array(
|
||||
'driver' => 'sqlite3',
|
||||
'database' => 'data/sample.s3db',
|
||||
'driver' => 'sqlite',
|
||||
'database' => 'data/sample.sdb',
|
||||
));
|
||||
|
||||
|
||||
|
@@ -4,15 +4,15 @@
|
||||
|
||||
<?php
|
||||
|
||||
require dirname(__FILE__) . '/Nette/Debugger.php';
|
||||
require dirname(__FILE__) . '/../dibi/dibi.php';
|
||||
require_once 'Nette/Debugger.php';
|
||||
require_once '../dibi/dibi.php';
|
||||
|
||||
date_default_timezone_set('Europe/Prague');
|
||||
|
||||
|
||||
dibi::connect(array(
|
||||
'driver' => 'sqlite3',
|
||||
'database' => 'data/sample.s3db',
|
||||
'driver' => 'sqlite',
|
||||
'database' => 'data/sample.sdb',
|
||||
// enable query logging to this file
|
||||
'profiler' => array(
|
||||
'run' => TRUE,
|
||||
|
@@ -1,4 +1,4 @@
|
||||
<?php ob_start() // needed by FirePHP ?>
|
||||
<?php ob_start(1) // needed by FirePHP ?>
|
||||
|
||||
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
|
||||
|
||||
@@ -6,13 +6,13 @@
|
||||
|
||||
<?php
|
||||
|
||||
require dirname(__FILE__) . '/Nette/Debugger.php';
|
||||
require dirname(__FILE__) . '/../dibi/dibi.php';
|
||||
require_once 'Nette/Debugger.php';
|
||||
require_once '../dibi/dibi.php';
|
||||
|
||||
|
||||
dibi::connect(array(
|
||||
'driver' => 'sqlite3',
|
||||
'database' => 'data/sample.s3db',
|
||||
'driver' => 'sqlite',
|
||||
'database' => 'data/sample.sdb',
|
||||
'profiler' => array(
|
||||
'run' => TRUE,
|
||||
)
|
||||
|
@@ -4,13 +4,13 @@
|
||||
|
||||
<?php
|
||||
|
||||
require dirname(__FILE__) . '/Nette/Debugger.php';
|
||||
require dirname(__FILE__) . '/../dibi/dibi.php';
|
||||
require_once 'Nette/Debugger.php';
|
||||
require_once '../dibi/dibi.php';
|
||||
|
||||
|
||||
dibi::connect(array(
|
||||
'driver' => 'sqlite3',
|
||||
'database' => 'data/sample.s3db',
|
||||
'driver' => 'sqlite',
|
||||
'database' => 'data/sample.sdb',
|
||||
));
|
||||
|
||||
|
||||
|
@@ -4,13 +4,13 @@
|
||||
|
||||
<?php
|
||||
|
||||
require dirname(__FILE__) . '/Nette/Debugger.php';
|
||||
require dirname(__FILE__) . '/../dibi/dibi.php';
|
||||
require_once 'Nette/Debugger.php';
|
||||
require_once '../dibi/dibi.php';
|
||||
|
||||
|
||||
dibi::connect(array(
|
||||
'driver' => 'sqlite3',
|
||||
'database' => 'data/sample.s3db',
|
||||
'driver' => 'sqlite',
|
||||
'database' => 'data/sample.sdb',
|
||||
));
|
||||
|
||||
|
||||
|
55
license.md
55
license.md
@@ -1,55 +0,0 @@
|
||||
Licenses
|
||||
========
|
||||
|
||||
Good news! You may use Dibi 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 Dibi in commercial projects as long as the copyright header
|
||||
remains intact.
|
||||
|
||||
|
||||
New BSD License
|
||||
---------------
|
||||
|
||||
Copyright (c) 2004, 2014 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 "Dibi" 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)
|
61
license.txt
Normal file
61
license.txt
Normal file
@@ -0,0 +1,61 @@
|
||||
Licenses
|
||||
========
|
||||
|
||||
Good news! You may use Dibi 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 library. If the GPL
|
||||
fits better to your project, you can use the Dibi under this license.
|
||||
|
||||
You don't have to notify anyone which license you are using. You can freely
|
||||
use Dibi in commercial projects as long as the copyright header
|
||||
remains intact.
|
||||
|
||||
Please do not use word "Dibi" 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) 2005, 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 "Dibi" 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
|
126
readme.md
126
readme.md
@@ -1,126 +0,0 @@
|
||||
[Dibi](http://dibiphp.com) - smart database layer for PHP
|
||||
=========================================================
|
||||
|
||||
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):
|
||||
|
||||
php composer.phar require dibi/dibi
|
||||
|
||||
Or you can download the latest package from http://dibiphp.com. In this
|
||||
package is also `Dibi.minified`, shrinked single-file version of whole Dibi,
|
||||
useful when you don't want to modify the library, but just use it.
|
||||
|
||||
Dibi requires PHP 5.2.0 or later. It has been tested with PHP 5.5 too.
|
||||
|
||||
|
||||
Examples
|
||||
--------
|
||||
|
||||
Refer to the `examples` directory for examples. Dibi documentation is
|
||||
available on the [homepage](http://dibiphp.com).
|
||||
|
||||
Connect to database:
|
||||
|
||||
```php
|
||||
// connect to database (static way)
|
||||
dibi::connect(array(
|
||||
'driver' => 'mysql',
|
||||
'host' => 'localhost',
|
||||
'username' => 'root',
|
||||
'password' => '***',
|
||||
));
|
||||
|
||||
// or object way; in all other examples use $connection-> instead of dibi::
|
||||
$connection = new DibiConnection($options);
|
||||
```
|
||||
|
||||
SELECT, INSERT, UPDATE
|
||||
|
||||
```php
|
||||
dibi::query('SELECT * FROM users WHERE id = ?', $id);
|
||||
|
||||
$arr = array(
|
||||
'name' => 'John',
|
||||
'is_admin' => TRUE,
|
||||
);
|
||||
dibi::query('INSERT INTO users', $arr);
|
||||
// INSERT INTO users (`name`, `is_admin`) VALUES ('John', 1)
|
||||
|
||||
dibi::query('UPDATE users SET', $arr, 'WHERE `id`=?', $x);
|
||||
// UPDATE users SET `name`='John', `is_admin`=1 WHERE `id` = 123
|
||||
|
||||
dibi::query('UPDATE users SET', array(
|
||||
'title' => array('SHA1(?)', 'tajneheslo'),
|
||||
));
|
||||
// UPDATE users SET 'title' = SHA1('tajneheslo')
|
||||
```
|
||||
|
||||
Getting results
|
||||
|
||||
```php
|
||||
$result = dibi::query('SELECT * FROM users');
|
||||
|
||||
$value = $result->fetchSingle(); // single value
|
||||
$all = $result->fetchAll(); // all rows
|
||||
$assoc = $result->fetchAssoc('id'); // all rows as associative array
|
||||
$pairs = $result->fetchPairs('customerID', 'name'); // all rows as key => value pairs
|
||||
|
||||
// iterating
|
||||
foreach ($result as $n => $row) {
|
||||
print_r($row);
|
||||
}
|
||||
```
|
||||
|
||||
Modifiers for arrays:
|
||||
|
||||
```php
|
||||
dibi::query('SELECT * FROM users WHERE %and', array(
|
||||
array('number > ?', 10),
|
||||
array('number < ?', 100),
|
||||
));
|
||||
// SELECT * FROM users WHERE (number > 10) AND (number < 100)
|
||||
```
|
||||
|
||||
<table>
|
||||
<tr><td> %and </td><td> </td><td> `[key]=val AND [key2]="val2" AND ...` </td></tr>
|
||||
<tr><td> %or </td><td> </td><td> `[key]=val OR [key2]="val2" OR ...` </td></tr>
|
||||
<tr><td> %a </td><td> assoc </td><td> `[key]=val, [key2]="val2", ...` </td></tr>
|
||||
<tr><td> %l %in </td><td> list </td><td> `(val, "val2", ...)` </td></tr>
|
||||
<tr><td> %v </td><td> values </td><td> `([key], [key2], ...) VALUES (val, "val2", ...)` </td></tr>
|
||||
<tr><td> %m </td><td> multivalues </td><td> `([key], [key2], ...) VALUES (val, "val2", ...), (val, "val2", ...), ...` </td></tr>
|
||||
<tr><td> %by </td><td> ordering </td><td> `[key] ASC, [key2] DESC ...` </td></tr>
|
||||
<tr><td> %n </td><td> identifiers </td><td> `[key], [key2] AS alias, ...` </td></tr>
|
||||
<tr><td> other </td><td> - </td><td> `val, val2, ...` </td></tr>
|
||||
</table>
|
||||
|
||||
|
||||
Modifiers for LIKE
|
||||
|
||||
```php
|
||||
dibi::query("SELECT * FROM table WHERE name LIKE %like~", $query);
|
||||
```
|
||||
|
||||
<table>
|
||||
<tr><td> %like~ </td><td> begins with </td></tr>
|
||||
<tr><td> %~like </td><td> ends with </td></tr>
|
||||
<tr><td> %~like~ </td><td> contains </td></tr>
|
||||
</table>
|
||||
|
||||
DateTime:
|
||||
|
||||
```php
|
||||
dibi::query('UPDATE users SET', array(
|
||||
'time' => new DateTime,
|
||||
));
|
||||
// UPDATE users SET ('2008-01-01 01:08:10')
|
||||
```
|
||||
|
||||
Testing:
|
||||
|
||||
```php
|
||||
echo dibi::$sql; // last SQL query
|
||||
echo dibi::$elapsedTime;
|
||||
echo dibi::$numOfQueries;
|
||||
echo dibi::$totalTime;
|
31
readme.txt
Normal file
31
readme.txt
Normal file
@@ -0,0 +1,31 @@
|
||||
Introduction
|
||||
------------
|
||||
|
||||
Thank you for downloading Dibi!
|
||||
|
||||
Database access functions in PHP are not standardised. This is class library
|
||||
to hide the differences between the different databases access.
|
||||
|
||||
|
||||
Documentation and Examples
|
||||
--------------------------
|
||||
|
||||
Refer to the 'examples' directory for examples. Dibi documentation is
|
||||
available on the homepage:
|
||||
|
||||
http://dibiphp.com
|
||||
|
||||
|
||||
Dibi.minified
|
||||
-------------
|
||||
|
||||
This is shrinked single-file version of whole Dibi, useful when you don't
|
||||
want to modify library, but just use it.
|
||||
|
||||
This is exactly the same as normal version, just only comments and
|
||||
whitespaces are removed.
|
||||
|
||||
|
||||
-----
|
||||
For more information, visit the author's weblog (in czech language):
|
||||
http://phpfashion.com
|
@@ -4,14 +4,16 @@
|
||||
* Test: Cloning of DibiFluent
|
||||
*
|
||||
* @author David Grudl
|
||||
* @category Dibi
|
||||
* @subpackage UnitTests
|
||||
*/
|
||||
|
||||
|
||||
require dirname(__FILE__) . '/bootstrap.php';
|
||||
require dirname(__FILE__) . '/initialize.php';
|
||||
|
||||
|
||||
|
||||
dibi::connect($config['sqlite3']);
|
||||
dibi::connect($config['sqlite']);
|
||||
|
||||
|
||||
$fluent = new DibiFluent(dibi::getConnection());
|
||||
@@ -20,21 +22,48 @@ $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->test();
|
||||
$dolly->test();
|
||||
|
||||
|
||||
$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->test();
|
||||
$dolly->test();
|
||||
|
||||
|
||||
$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 );
|
||||
$fluent->test();
|
||||
$dolly->test();
|
||||
|
||||
|
||||
|
||||
__halt_compiler() ?>
|
||||
|
||||
------EXPECT------
|
||||
SELECT *
|
||||
FROM [table]
|
||||
WHERE x=1
|
||||
|
||||
SELECT *
|
||||
FROM [table]
|
||||
WHERE x=1 AND y=1 FOO
|
||||
|
||||
SELECT [id]
|
||||
FROM [table]
|
||||
WHERE id = 1
|
||||
|
||||
SELECT [id]
|
||||
FROM [table]
|
||||
WHERE id = 1 AND cd = 5
|
||||
|
||||
SELECT *
|
||||
FROM [table]
|
||||
|
||||
SELECT count(*)
|
||||
FROM [table]
|
||||
|
138
tests/NetteTest/Assert.php
Normal file
138
tests/NetteTest/Assert.php
Normal file
@@ -0,0 +1,138 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Nette Framework
|
||||
*
|
||||
* @copyright Copyright (c) 2004 David Grudl
|
||||
* @license http://nette.org/license Nette license
|
||||
* @link http://nette.org
|
||||
* @category Nette
|
||||
* @package Nette\Test
|
||||
*/
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Asseratation test helpers.
|
||||
*
|
||||
* @author David Grudl
|
||||
* @package Nette\Test
|
||||
*/
|
||||
class Assert
|
||||
{
|
||||
|
||||
/**
|
||||
* Checks assertation.
|
||||
* @param mixed expected
|
||||
* @param mixed actual
|
||||
* @return void
|
||||
*/
|
||||
public static function same($expected, $actual)
|
||||
{
|
||||
if ($actual !== $expected) {
|
||||
self::note('Failed asserting that ' . self::dump($actual) . ' is not identical to ' . self::dump($expected));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Checks TRUE assertation.
|
||||
* @param mixed actual
|
||||
* @return void
|
||||
*/
|
||||
public static function true($actual)
|
||||
{
|
||||
if ($actual !== TRUE) {
|
||||
self::note('Failed asserting that ' . self::dump($actual) . ' is not TRUE');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Checks FALSE assertation.
|
||||
* @param mixed actual
|
||||
* @return void
|
||||
*/
|
||||
public static function false($actual)
|
||||
{
|
||||
if ($actual !== FALSE) {
|
||||
self::note('Failed asserting that ' . self::dump($actual) . ' is not FALSE');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Checks NULL assertation.
|
||||
* @param mixed actual
|
||||
* @return void
|
||||
*/
|
||||
public static function null($actual)
|
||||
{
|
||||
if ($actual !== NULL) {
|
||||
self::note('Failed asserting that ' . self::dump($actual) . ' is not NULL');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Dumps information about a variable in readable format.
|
||||
* @param mixed variable to dump
|
||||
* @return void
|
||||
*/
|
||||
private static function dump($var)
|
||||
{
|
||||
if (is_bool($var)) {
|
||||
return $var ? 'TRUE' : 'FALSE';
|
||||
|
||||
} elseif ($var === NULL) {
|
||||
return "NULL";
|
||||
|
||||
} elseif (is_int($var)) {
|
||||
return "$var";
|
||||
|
||||
} elseif (is_float($var)) {
|
||||
return "$var";
|
||||
|
||||
} elseif (is_string($var)) {
|
||||
return var_export($var, TRUE);
|
||||
|
||||
} elseif (is_array($var)) {
|
||||
return "array(" . count($var) . ")";
|
||||
|
||||
} elseif ($var instanceof Exception) {
|
||||
return 'Exception ' . get_class($var) . ': ' . ($var->getCode() ? '#' . $var->getCode() . ' ' : '') . $var->getMessage();
|
||||
|
||||
} elseif (is_object($var)) {
|
||||
$arr = (array) $var;
|
||||
return "object(" . get_class($var) . ") (" . count($arr) . ")";
|
||||
|
||||
} elseif (is_resource($var)) {
|
||||
return "resource(" . get_resource_type($var) . ")";
|
||||
|
||||
} else {
|
||||
return "unknown type";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Returns message and file and line from call stack.
|
||||
* @param string
|
||||
* @return void
|
||||
*/
|
||||
private static function note($message)
|
||||
{
|
||||
echo $message;
|
||||
$trace = debug_backtrace();
|
||||
if (isset($trace[1]['file'], $trace[1]['line'])) {
|
||||
echo ' in file ' . $trace[1]['file'] . ' on line ' . $trace[1]['line'];
|
||||
}
|
||||
echo "\n\n";
|
||||
}
|
||||
|
||||
}
|
43
tests/NetteTest/RunTests.php
Normal file
43
tests/NetteTest/RunTests.php
Normal file
@@ -0,0 +1,43 @@
|
||||
|
||||
Nette Test Framework (v0.3)
|
||||
---------------------------
|
||||
<?php
|
||||
|
||||
require_once dirname(__FILE__) . '/TestRunner.php';
|
||||
|
||||
/**
|
||||
* Help
|
||||
*/
|
||||
if (!isset($_SERVER['argv'][1])) { ?>
|
||||
Usage:
|
||||
php RunTests.php [options] [file or directory]
|
||||
|
||||
Options:
|
||||
-p <php> Specify PHP-CGI executable to run.
|
||||
-c <path> Look for php.ini in directory <path> or use <path> as php.ini.
|
||||
-d key=val Define INI entry 'key' with value 'val'.
|
||||
-l <path> Specify path to shared library files (LD_LIBRARY_PATH)
|
||||
-e <name> Load php environment <name>
|
||||
-s Show information about skipped tests
|
||||
|
||||
<?php
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Execute tests
|
||||
*/
|
||||
try {
|
||||
@unlink(dirname(__FILE__) . '/coverage.tmp'); // @ - file may not exist
|
||||
|
||||
$manager = new TestRunner;
|
||||
$manager->parseConfigFile();
|
||||
$manager->parseArguments();
|
||||
$res = $manager->run();
|
||||
die($res ? 0 : 1);
|
||||
|
||||
} catch (Exception $e) {
|
||||
echo 'Error: ', $e->getMessage(), "\n";
|
||||
die(2);
|
||||
}
|
400
tests/NetteTest/TestCase.php
Normal file
400
tests/NetteTest/TestCase.php
Normal file
@@ -0,0 +1,400 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Nette Framework
|
||||
*
|
||||
* @copyright Copyright (c) 2004 David Grudl
|
||||
* @license http://nette.org/license Nette license
|
||||
* @link http://nette.org
|
||||
* @category Nette
|
||||
* @package Nette\Test
|
||||
*/
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Single test case.
|
||||
*
|
||||
* @author David Grudl
|
||||
* @package Nette\Test
|
||||
*/
|
||||
class TestCase
|
||||
{
|
||||
/** @var string test file */
|
||||
private $file;
|
||||
|
||||
/** @var array test file multiparts */
|
||||
private $sections;
|
||||
|
||||
/** @var string test output */
|
||||
private $output;
|
||||
|
||||
/** @var string output headers in raw format */
|
||||
private $headers;
|
||||
|
||||
/** @var string PHP command line */
|
||||
private $cmdLine;
|
||||
|
||||
/** @var string PHP command line */
|
||||
private $phpVersion;
|
||||
|
||||
/** @var array */
|
||||
private static $cachedPhp;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @param string test file name
|
||||
* @param string PHP command line
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($testFile)
|
||||
{
|
||||
$this->file = (string) $testFile;
|
||||
$this->sections = self::parseSections($this->file);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Runs single test.
|
||||
* @return void
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
// pre-skip?
|
||||
$options = $this->sections['options'];
|
||||
if (isset($options['skip'])) {
|
||||
$message = $options['skip'] ? $options['skip'] : 'No message.';
|
||||
throw new TestCaseException($message, TestCaseException::SKIPPED);
|
||||
|
||||
} elseif (isset($options['phpversion'])) {
|
||||
$operator = '>=';
|
||||
if (preg_match('#^(<=|le|<|lt|==|=|eq|!=|<>|ne|>=|ge|>|gt)#', $options['phpversion'], $matches)) {
|
||||
$options['phpversion'] = trim(substr($options['phpversion'], strlen($matches[1])));
|
||||
$operator = $matches[1];
|
||||
}
|
||||
if (version_compare($options['phpversion'], $this->phpVersion, $operator)) {
|
||||
throw new TestCaseException("Requires PHP $operator $options[phpversion].", TestCaseException::SKIPPED);
|
||||
}
|
||||
}
|
||||
|
||||
$this->execute();
|
||||
$output = $this->output;
|
||||
$headers = array_change_key_case(self::parseLines($this->headers, ':'), CASE_LOWER);
|
||||
$tests = 0;
|
||||
|
||||
// post-skip?
|
||||
if (isset($headers['x-nette-test-skip'])) {
|
||||
throw new TestCaseException($headers['x-nette-test-skip'], TestCaseException::SKIPPED);
|
||||
}
|
||||
|
||||
// compare output
|
||||
$expectedOutput = $this->getExpectedOutput();
|
||||
if ($expectedOutput !== NULL) {
|
||||
$tests++;
|
||||
$binary = (bool) preg_match('#[\x00-\x08\x0B\x0C\x0E-\x1F]#', $expectedOutput);
|
||||
if ($binary) {
|
||||
if ($expectedOutput !== $output) {
|
||||
throw new TestCaseException("Binary output doesn't match.");
|
||||
}
|
||||
} else {
|
||||
$output = self::normalize($output, isset($options['keeptrailingspaces']));
|
||||
$expectedOutput = self::normalize($expectedOutput, isset($options['keeptrailingspaces']));
|
||||
if (!$this->compare($output, $expectedOutput)) {
|
||||
throw new TestCaseException("Output doesn't match.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// compare headers
|
||||
$expectedHeaders = $this->getExpectedHeaders();
|
||||
if ($expectedHeaders !== NULL) {
|
||||
$tests++;
|
||||
$expectedHeaders = self::normalize($expectedHeaders, FALSE);
|
||||
$expectedHeaders = array_change_key_case(self::parseLines($expectedHeaders, ':'), CASE_LOWER);
|
||||
foreach ($expectedHeaders as $name => $header) {
|
||||
if (!isset($headers[$name])) {
|
||||
throw new TestCaseException("Missing header '$name'.");
|
||||
|
||||
} elseif (!$this->compare($headers[$name], $header)) {
|
||||
throw new TestCaseException("Header '$name' doesn't match.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!$tests) { // expecting no output
|
||||
if (trim($output) !== '') {
|
||||
throw new TestCaseException("Empty output doesn't match.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Sets PHP command line.
|
||||
* @param string
|
||||
* @param string
|
||||
* @param string
|
||||
* @return TestCase provides a fluent interface
|
||||
*/
|
||||
public function setPhp($binary, $args, $environment)
|
||||
{
|
||||
if (isset(self::$cachedPhp[$binary])) {
|
||||
$this->phpVersion = self::$cachedPhp[$binary];
|
||||
|
||||
} else {
|
||||
exec($environment . escapeshellarg($binary) . ' -v', $output, $res);
|
||||
if ($res !== 0 && $res !== 255) {
|
||||
throw new Exception("Unable to execute '$binary -v'.");
|
||||
}
|
||||
|
||||
if (!preg_match('#^PHP (\S+).*cli#i', $output[0], $matches)) {
|
||||
throw new Exception("Unable to detect PHP version (output: $output[0]).");
|
||||
}
|
||||
|
||||
$this->phpVersion = self::$cachedPhp[$binary] = $matches[1];
|
||||
}
|
||||
|
||||
$this->cmdLine = $environment . escapeshellarg($binary) . $args;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Execute test.
|
||||
* @return array
|
||||
*/
|
||||
private function execute()
|
||||
{
|
||||
$this->headers = $this->output = NULL;
|
||||
|
||||
$tempFile = tempnam('', 'tmp');
|
||||
if (!$tempFile) {
|
||||
throw new Exception("Unable to create temporary file.");
|
||||
}
|
||||
|
||||
$command = $this->cmdLine;
|
||||
if (isset($this->sections['options']['phpini'])) {
|
||||
foreach (explode(';', $this->sections['options']['phpini']) as $item) {
|
||||
$command .= " -d " . escapeshellarg(trim($item));
|
||||
}
|
||||
}
|
||||
$command .= ' ' . escapeshellarg($this->file) . ' > ' . escapeshellarg($tempFile);
|
||||
|
||||
chdir(dirname($this->file));
|
||||
exec($command, $foo, $res);
|
||||
if ($res === 255) {
|
||||
// exit_status 255 => parse or fatal error
|
||||
|
||||
} elseif ($res !== 0) {
|
||||
throw new Exception("Unable to execute '$command'.");
|
||||
|
||||
}
|
||||
|
||||
$this->output = file_get_contents($tempFile);
|
||||
unlink($tempFile);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Returns test file section.
|
||||
* @return string
|
||||
*/
|
||||
public function getSection($name)
|
||||
{
|
||||
return isset($this->sections[$name]) ? $this->sections[$name] : NULL;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Returns test name.
|
||||
* @return string
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return $this->sections['options']['name'];
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Returns test output.
|
||||
* @return string
|
||||
*/
|
||||
public function getOutput()
|
||||
{
|
||||
return $this->output;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Returns output headers.
|
||||
* @return string
|
||||
*/
|
||||
public function getHeaders()
|
||||
{
|
||||
return $this->headers;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Returns expected output.
|
||||
* @return string
|
||||
*/
|
||||
public function getExpectedOutput()
|
||||
{
|
||||
if (isset($this->sections['expect'])) {
|
||||
return $this->sections['expect'];
|
||||
|
||||
} elseif (is_file($expFile = str_replace('.phpt', '', $this->file) . '.expect')) {
|
||||
return file_get_contents($expFile);
|
||||
|
||||
} else {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Returns expected headers.
|
||||
* @return string
|
||||
*/
|
||||
public function getExpectedHeaders()
|
||||
{
|
||||
return $this->getSection('expectheaders');
|
||||
}
|
||||
|
||||
|
||||
|
||||
/********************* helpers ****************d*g**/
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Splits file into sections.
|
||||
* @param string file
|
||||
* @return array
|
||||
*/
|
||||
public static function parseSections($testFile)
|
||||
{
|
||||
$content = file_get_contents($testFile);
|
||||
$sections = array(
|
||||
'options' => array(),
|
||||
);
|
||||
|
||||
// phpDoc
|
||||
$phpDoc = preg_match('#^/\*\*(.*?)\*/#ms', $content, $matches) ? trim($matches[1]) : '';
|
||||
preg_match_all('#^\s*\*\s*@(\S+)(.*)#mi', $phpDoc, $matches, PREG_SET_ORDER);
|
||||
foreach ($matches as $match) {
|
||||
$sections['options'][strtolower($match[1])] = isset($match[2]) ? trim($match[2]) : TRUE;
|
||||
}
|
||||
$sections['options']['name'] = preg_match('#^\s*\*\s*TEST:(.*)#mi', $phpDoc, $matches) ? trim($matches[1]) : $testFile;
|
||||
|
||||
// file parts
|
||||
$tmp = preg_split('#^-{3,}([^\s-]+)-{1,}(?:\r?\n|$)#m', $content, -1, PREG_SPLIT_DELIM_CAPTURE);
|
||||
$i = 1;
|
||||
while (isset($tmp[$i])) {
|
||||
$sections[strtolower($tmp[$i])] = $tmp[$i+1];
|
||||
$i += 2;
|
||||
}
|
||||
return $sections;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Splits HTTP headers into array.
|
||||
* @param string
|
||||
* @param string
|
||||
* @return array
|
||||
*/
|
||||
public static function parseLines($raw, $separator)
|
||||
{
|
||||
$headers = array();
|
||||
foreach (explode("\r\n", $raw) as $header) {
|
||||
$a = strpos($header, $separator);
|
||||
if ($a !== FALSE) {
|
||||
$headers[trim(substr($header, 0, $a))] = (string) trim(substr($header, $a + 1));
|
||||
}
|
||||
}
|
||||
return $headers;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Compares results.
|
||||
* @param string
|
||||
* @param string
|
||||
* @return bool
|
||||
*/
|
||||
public static function compare($left, $right)
|
||||
{
|
||||
$right = strtr($right, array(
|
||||
'%a%' => '[^\r\n]+', // one or more of anything except the end of line characters
|
||||
'%a?%'=> '[^\r\n]*', // zero or more of anything except the end of line characters
|
||||
'%A%' => '.+', // one or more of anything including the end of line characters
|
||||
'%A?%'=> '.*', // zero or more of anything including the end of line characters
|
||||
'%s%' => '[\t ]+', // one or more white space characters except the end of line characters
|
||||
'%s?%'=> '[\t ]*', // zero or more white space characters except the end of line characters
|
||||
'%S%' => '\S+', // one or more of characters except the white space
|
||||
'%S?%'=> '\S*', // zero or more of characters except the white space
|
||||
'%c%' => '[^\r\n]', // a single character of any sort (except the end of line)
|
||||
'%d%' => '[0-9]+', // one or more digits
|
||||
'%d?%'=> '[0-9]*', // zero or more digits
|
||||
'%i%' => '[+-]?[0-9]+', // signed integer value
|
||||
'%f%' => '[+-]?\.?\d+\.?\d*(?:[Ee][+-]?\d+)?', // floating point number
|
||||
'%h%' => '[0-9a-fA-F]+',// one or more HEX digits
|
||||
'%ns%'=> '(?:[_0-9a-zA-Z\\\\]+\\\\|N)?',// PHP namespace
|
||||
'%[^' => '[^', // reg-exp
|
||||
'%[' => '[', // reg-exp
|
||||
']%' => ']+', // reg-exp
|
||||
|
||||
'.' => '\.', '\\' => '\\\\', '+' => '\+', '*' => '\*', '?' => '\?', '[' => '\[', '^' => '\^', ']' => '\]', '$' => '\$', '(' => '\(', ')' => '\)', // preg quote
|
||||
'{' => '\{', '}' => '\}', '=' => '\=', '!' => '\!', '>' => '\>', '<' => '\<', '|' => '\|', ':' => '\:', '-' => '\-', "\x00" => '\000', '#' => '\#', // preg quote
|
||||
));
|
||||
|
||||
return (bool) preg_match("#^$right$#s", $left);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Normalizes whitespace
|
||||
* @param string
|
||||
* @param bool
|
||||
* @return string
|
||||
*/
|
||||
public static function normalize($s, $keepTrailingSpaces)
|
||||
{
|
||||
$s = str_replace("\n", PHP_EOL, str_replace("\r\n", "\n", $s)); // normalize EOL
|
||||
if (!$keepTrailingSpaces) {
|
||||
$s = preg_replace("#[\t ]+(\r?\n)#", '$1', $s); // multiline right trim
|
||||
$s = rtrim($s); // ending trim
|
||||
}
|
||||
return $s;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Single test exception.
|
||||
*
|
||||
* @author David Grudl
|
||||
* @package Nette\Test
|
||||
*/
|
||||
class TestCaseException extends Exception
|
||||
{
|
||||
const SKIPPED = 1;
|
||||
|
||||
}
|
312
tests/NetteTest/TestHelpers.php
Normal file
312
tests/NetteTest/TestHelpers.php
Normal file
@@ -0,0 +1,312 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Nette Framework
|
||||
*
|
||||
* @copyright Copyright (c) 2004 David Grudl
|
||||
* @license http://nette.org/license Nette license
|
||||
* @link http://nette.org
|
||||
* @category Nette
|
||||
* @package Nette\Test
|
||||
*/
|
||||
|
||||
require dirname(__FILE__) . '/TestCase.php';
|
||||
|
||||
|
||||
/**
|
||||
* Test helpers.
|
||||
*
|
||||
* @author David Grudl
|
||||
* @package Nette\Test
|
||||
*/
|
||||
class TestHelpers
|
||||
{
|
||||
/** @var int */
|
||||
static public $maxDepth = 5;
|
||||
|
||||
/** @var array */
|
||||
private static $sections;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Configures PHP and environment.
|
||||
* @return void
|
||||
*/
|
||||
public static function startup()
|
||||
{
|
||||
error_reporting(E_ALL | E_STRICT);
|
||||
ini_set('display_errors', TRUE);
|
||||
ini_set('html_errors', FALSE);
|
||||
ini_set('log_errors', FALSE);
|
||||
|
||||
|
||||
$_SERVER = array_intersect_key($_SERVER, array_flip(array('PHP_SELF', 'SCRIPT_NAME', 'SERVER_ADDR', 'SERVER_SOFTWARE', 'HTTP_HOST', 'DOCUMENT_ROOT', 'OS')));
|
||||
$_SERVER['REQUEST_TIME'] = 1234567890;
|
||||
$_ENV = array();
|
||||
|
||||
if (PHP_SAPI !== 'cli') {
|
||||
header('Content-Type: text/plain; charset=utf-8');
|
||||
}
|
||||
|
||||
if (extension_loaded('xdebug')) {
|
||||
xdebug_disable();
|
||||
xdebug_start_code_coverage(XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE);
|
||||
register_shutdown_function(array(__CLASS__, 'prepareSaveCoverage'));
|
||||
}
|
||||
|
||||
set_exception_handler(array(__CLASS__, 'exceptionHandler'));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Purges directory.
|
||||
* @param string
|
||||
* @return void
|
||||
*/
|
||||
public static function purge($dir)
|
||||
{
|
||||
@mkdir($dir); // @ - directory may already exist
|
||||
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir), RecursiveIteratorIterator::CHILD_FIRST) as $entry) {
|
||||
if ($entry->getBasename() === '.gitignore') {
|
||||
// ignore
|
||||
} elseif ($entry->isDir()) {
|
||||
rmdir($entry);
|
||||
} else {
|
||||
unlink($entry);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Returns current test section.
|
||||
* @param string
|
||||
* @param string
|
||||
* @return mixed
|
||||
*/
|
||||
public static function getSection($file, $section)
|
||||
{
|
||||
if (!isset(self::$sections[$file])) {
|
||||
self::$sections[$file] = TestCase::parseSections($file);
|
||||
}
|
||||
|
||||
$lowerSection = strtolower($section);
|
||||
if (!isset(self::$sections[$file][$lowerSection])) {
|
||||
throw new Exception("Missing section '$section' in file '$file'.");
|
||||
}
|
||||
|
||||
if (in_array($section, array('GET', 'POST', 'SERVER'), TRUE)) {
|
||||
return TestCase::parseLines(self::$sections[$file][$lowerSection], '=');
|
||||
} else {
|
||||
return self::$sections[$file][$lowerSection];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Writes new message.
|
||||
* @param string
|
||||
* @return void
|
||||
*/
|
||||
public static function note($message = NULL)
|
||||
{
|
||||
echo $message ? "$message\n\n" : "===\n\n";
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Dumps information about a variable in readable format.
|
||||
* @param mixed variable to dump
|
||||
* @param string
|
||||
* @return mixed variable itself or dump
|
||||
*/
|
||||
public static function dump($var, $message = NULL)
|
||||
{
|
||||
if ($message) {
|
||||
echo $message . (preg_match('#[.:?]$#', $message) ? ' ' : ': ');
|
||||
}
|
||||
|
||||
self::_dump($var, 0);
|
||||
echo "\n";
|
||||
return $var;
|
||||
}
|
||||
|
||||
|
||||
|
||||
private static function _dump(& $var, $level = 0)
|
||||
{
|
||||
static $tableUtf, $tableBin, $reBinary = '#[^\x09\x0A\x0D\x20-\x7E\xA0-\x{10FFFF}]#u';
|
||||
if ($tableUtf === NULL) {
|
||||
foreach (range("\x00", "\xFF") as $ch) {
|
||||
if (ord($ch) < 32 && strpos("\r\n\t", $ch) === FALSE) $tableUtf[$ch] = $tableBin[$ch] = '\\x' . str_pad(dechex(ord($ch)), 2, '0', STR_PAD_LEFT);
|
||||
elseif (ord($ch) < 127) $tableUtf[$ch] = $tableBin[$ch] = $ch;
|
||||
else { $tableUtf[$ch] = $ch; $tableBin[$ch] = '\\x' . dechex(ord($ch)); }
|
||||
}
|
||||
$tableBin["\\"] = '\\\\';
|
||||
$tableBin["\r"] = '\\r';
|
||||
$tableBin["\n"] = '\\n';
|
||||
$tableBin["\t"] = '\\t';
|
||||
$tableUtf['\\x'] = $tableBin['\\x'] = '\\\\x';
|
||||
}
|
||||
|
||||
if (is_bool($var)) {
|
||||
echo ($var ? 'TRUE' : 'FALSE') . "\n";
|
||||
|
||||
} elseif ($var === NULL) {
|
||||
echo "NULL\n";
|
||||
|
||||
} elseif (is_int($var)) {
|
||||
echo "$var\n";
|
||||
|
||||
} elseif (is_float($var)) {
|
||||
$var = (string) $var;
|
||||
if (strpos($var, '.') === FALSE) $var .= '.0';
|
||||
echo "$var\n";
|
||||
|
||||
} elseif (is_string($var)) {
|
||||
$s = strtr($var, preg_match($reBinary, $var) || preg_last_error() ? $tableBin : $tableUtf);
|
||||
echo "\"$s\"\n";
|
||||
|
||||
} elseif (is_array($var)) {
|
||||
echo "array(";
|
||||
$space = str_repeat("\t", $level);
|
||||
|
||||
static $marker;
|
||||
if ($marker === NULL) $marker = uniqid("\x00", TRUE);
|
||||
if (empty($var)) {
|
||||
|
||||
} elseif (isset($var[$marker])) {
|
||||
echo " *RECURSION* ";
|
||||
|
||||
} elseif ($level < self::$maxDepth) {
|
||||
echo "\n";
|
||||
$vector = range(0, count($var) - 1) === array_keys($var);
|
||||
$var[$marker] = 0;
|
||||
foreach ($var as $k => &$v) {
|
||||
if ($k === $marker) continue;
|
||||
if ($vector) {
|
||||
echo "$space\t";
|
||||
} else {
|
||||
$k = is_int($k) ? $k : '"' . strtr($k, preg_match($reBinary, $k) || preg_last_error() ? $tableBin : $tableUtf) . '"';
|
||||
echo "$space\t$k => ";
|
||||
}
|
||||
self::_dump($v, $level + 1);
|
||||
}
|
||||
unset($var[$marker]);
|
||||
echo "$space";
|
||||
|
||||
} else {
|
||||
echo " ... ";
|
||||
}
|
||||
echo ")\n";
|
||||
|
||||
} elseif ($var instanceof Exception) {
|
||||
echo 'Exception ', get_class($var), ': ', ($var->getCode() ? '#' . $var->getCode() . ' ' : '') . $var->getMessage(), "\n";
|
||||
|
||||
} elseif (is_object($var)) {
|
||||
$arr = (array) $var;
|
||||
echo get_class($var) . "(";
|
||||
$space = str_repeat("\t", $level);
|
||||
|
||||
static $list = array();
|
||||
if (empty($arr)) {
|
||||
|
||||
} elseif (in_array($var, $list, TRUE)) {
|
||||
echo " *RECURSION* ";
|
||||
|
||||
} elseif ($level < self::$maxDepth) {
|
||||
echo "\n";
|
||||
$list[] = $var;
|
||||
foreach ($arr as $k => &$v) {
|
||||
$m = '';
|
||||
if ($k[0] === "\x00") {
|
||||
$m = $k[1] === '*' ? ' protected' : ' private';
|
||||
$k = substr($k, strrpos($k, "\x00") + 1);
|
||||
}
|
||||
$k = strtr($k, preg_match($reBinary, $k) || preg_last_error() ? $tableBin : $tableUtf);
|
||||
echo "$space\t\"$k\"$m => ";
|
||||
echo self::_dump($v, $level + 1);
|
||||
}
|
||||
array_pop($list);
|
||||
echo "$space";
|
||||
|
||||
} else {
|
||||
echo " ... ";
|
||||
}
|
||||
echo ")\n";
|
||||
|
||||
} elseif (is_resource($var)) {
|
||||
echo get_resource_type($var) . " resource\n";
|
||||
|
||||
} else {
|
||||
echo "unknown type\n";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Custom exception handler.
|
||||
* @param Exception
|
||||
* @return void
|
||||
*/
|
||||
public static function exceptionHandler(Exception $exception)
|
||||
{
|
||||
echo 'Error: Uncaught ';
|
||||
echo $exception;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Coverage saving helper.
|
||||
* @return void
|
||||
*/
|
||||
public static function prepareSaveCoverage()
|
||||
{
|
||||
register_shutdown_function(array(__CLASS__, 'saveCoverage'));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Saves information about code coverage.
|
||||
* @return void
|
||||
*/
|
||||
public static function saveCoverage()
|
||||
{
|
||||
$file = dirname(__FILE__) . '/coverage.tmp';
|
||||
$coverage = @unserialize(file_get_contents($file));
|
||||
$root = realpath(dirname(__FILE__) . '/../../Nette') . DIRECTORY_SEPARATOR;
|
||||
|
||||
foreach (xdebug_get_code_coverage() as $filename => $lines) {
|
||||
if (strncmp($root, $filename, strlen($root))) continue;
|
||||
|
||||
foreach ($lines as $num => $val) {
|
||||
if (empty($coverage[$filename][$num]) || $val > 0) {
|
||||
$coverage[$filename][$num] = $val; // -1 => untested; -2 => dead code
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
file_put_contents($file, serialize($coverage));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Skips this test.
|
||||
* @return void
|
||||
*/
|
||||
public static function skip($message = 'No message.')
|
||||
{
|
||||
header('X-Nette-Test-Skip: '. $message);
|
||||
exit;
|
||||
}
|
||||
|
||||
}
|
230
tests/NetteTest/TestRunner.php
Normal file
230
tests/NetteTest/TestRunner.php
Normal file
@@ -0,0 +1,230 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Nette Framework
|
||||
*
|
||||
* @copyright Copyright (c) 2004 David Grudl
|
||||
* @license http://nette.org/license Nette license
|
||||
* @link http://nette.org
|
||||
* @category Nette
|
||||
* @package Nette\Test
|
||||
*/
|
||||
|
||||
require dirname(__FILE__) . '/TestCase.php';
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Test runner.
|
||||
*
|
||||
* @author David Grudl
|
||||
* @package Nette\Test
|
||||
*/
|
||||
class TestRunner
|
||||
{
|
||||
const OUTPUT = 'output';
|
||||
const EXPECTED = 'expect';
|
||||
const HEADERS = 'headers';
|
||||
|
||||
/** @var string path to test file/directory */
|
||||
public $path;
|
||||
|
||||
/** @var string php-cgi binary */
|
||||
public $phpBinary;
|
||||
|
||||
/** @var string php-cgi command-line arguments */
|
||||
public $phpArgs;
|
||||
|
||||
/** @var string php-cgi environment variables */
|
||||
public $phpEnvironment;
|
||||
|
||||
/** @var bool display skipped tests information? */
|
||||
public $displaySkipped = FALSE;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Runs all tests.
|
||||
* @return void
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
$count = 0;
|
||||
$failed = $passed = $skipped = array();
|
||||
|
||||
if (is_file($this->path)) {
|
||||
$files = array($this->path);
|
||||
} else {
|
||||
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->path));
|
||||
}
|
||||
|
||||
foreach ($files as $entry) {
|
||||
$entry = (string) $entry;
|
||||
$info = pathinfo($entry);
|
||||
if (!isset($info['extension']) || $info['extension'] !== 'phpt') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$count++;
|
||||
$testCase = new TestCase($entry);
|
||||
$testCase->setPhp($this->phpBinary, $this->phpArgs, $this->phpEnvironment);
|
||||
|
||||
try {
|
||||
$testCase->run();
|
||||
echo '.';
|
||||
$passed[] = array($testCase->getName(), $entry);
|
||||
|
||||
} catch (TestCaseException $e) {
|
||||
if ($e->getCode() === TestCaseException::SKIPPED) {
|
||||
echo 's';
|
||||
$skipped[] = array($testCase->getName(), $entry, $e->getMessage());
|
||||
|
||||
} else {
|
||||
echo 'F';
|
||||
$failed[] = array($testCase->getName(), $entry, $e->getMessage());
|
||||
|
||||
$this->log($entry, $testCase->getOutput(), self::OUTPUT);
|
||||
$this->log($entry, $testCase->getExpectedOutput(), self::EXPECTED);
|
||||
|
||||
if ($testCase->getExpectedHeaders() !== NULL) {
|
||||
$this->log($entry, $testCase->getHeaders(), self::OUTPUT, self::HEADERS);
|
||||
$this->log($entry, $testCase->getExpectedHeaders(), self::EXPECTED, self::HEADERS);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$failedCount = count($failed);
|
||||
$skippedCount = count($skipped);
|
||||
|
||||
if ($this->displaySkipped && $skippedCount) {
|
||||
echo "\n\nSkipped:\n";
|
||||
foreach ($skipped as $i => $item) {
|
||||
list($name, $file, $message) = $item;
|
||||
echo "\n", ($i + 1), ") $name\n $message\n $file\n";
|
||||
}
|
||||
}
|
||||
|
||||
if (!$count) {
|
||||
echo "No tests found\n";
|
||||
|
||||
} elseif ($failedCount) {
|
||||
echo "\n\nFailures:\n";
|
||||
foreach ($failed as $i => $item) {
|
||||
list($name, $file, $message) = $item;
|
||||
echo "\n", ($i + 1), ") $name\n $message\n $file\n";
|
||||
}
|
||||
echo "\nFAILURES! ($count tests, $failedCount failures, $skippedCount skipped)\n";
|
||||
return FALSE;
|
||||
|
||||
} else {
|
||||
echo "\n\nOK ($count tests, $skippedCount skipped)\n";
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Returns output file for logging.
|
||||
* @param string
|
||||
* @param string
|
||||
* @param string
|
||||
* @param string
|
||||
* @return void
|
||||
*/
|
||||
public function log($testFile, $content, $type, $section = '')
|
||||
{
|
||||
$file = dirname($testFile) . '/' . $type . '/' . basename($testFile, '.phpt') . ($section ? ".$section" : '') . '.raw';
|
||||
@mkdir(dirname($file)); // @ - directory may already exist
|
||||
file_put_contents($file, $content);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Parses configuration file.
|
||||
* @return void
|
||||
*/
|
||||
public function parseConfigFile()
|
||||
{
|
||||
$configFile = dirname(__FILE__) . '/config.ini';
|
||||
if (file_exists($configFile)) {
|
||||
$this->config = parse_ini_file($configFile, TRUE);
|
||||
if ($this->config === FALSE) {
|
||||
throw new Exception('Config file parsing failed.');
|
||||
}
|
||||
foreach ($this->config as & $environment) {
|
||||
$environment += array(
|
||||
'binary' => 'php-cgi',
|
||||
'args' => '',
|
||||
'environment' => '',
|
||||
);
|
||||
// shorthand options
|
||||
if (isset($environment['php.ini'])) {
|
||||
$environment['args'] .= ' -c '. escapeshellarg($environment['php.ini']);
|
||||
}
|
||||
if (isset($environment['libraries'])) {
|
||||
$environment['environment'] .= 'LD_LIBRARY_PATH='. escapeshellarg($environment['libraries']) .' ';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Parses command line arguments.
|
||||
* @return void
|
||||
*/
|
||||
public function parseArguments()
|
||||
{
|
||||
$this->phpBinary = 'php-cgi';
|
||||
$this->phpArgs = '';
|
||||
$this->phpEnvironment = '';
|
||||
$this->path = getcwd(); // current directory
|
||||
|
||||
$args = new ArrayIterator(array_slice(isset($_SERVER['argv']) ? $_SERVER['argv'] : array(), 1));
|
||||
foreach ($args as $arg) {
|
||||
if (!preg_match('#^[-/][a-z]$#', $arg)) {
|
||||
if ($path = realpath($arg)) {
|
||||
$this->path = $path;
|
||||
} else {
|
||||
throw new Exception("Invalid path '$arg'.");
|
||||
}
|
||||
|
||||
} else switch ($arg[1]) {
|
||||
case 'p':
|
||||
$args->next();
|
||||
$this->phpBinary = $args->current();
|
||||
break;
|
||||
case 'c':
|
||||
case 'd':
|
||||
$args->next();
|
||||
$this->phpArgs .= " -$arg[1] " . escapeshellarg($args->current());
|
||||
break;
|
||||
case 'l':
|
||||
$args->next();
|
||||
$this->phpEnvironment .= 'LD_LIBRARY_PATH='. escapeshellarg($args->current()) . ' ';
|
||||
break;
|
||||
case 'e':
|
||||
$args->next();
|
||||
$name = $args->current();
|
||||
if (!isset($this->config[$name])) {
|
||||
throw new Exception("Unknown environment name '$name'.");
|
||||
}
|
||||
$this->phpBinary = $this->config[$name]['binary'];
|
||||
$this->phpArgs = $this->config[$name]['args'];
|
||||
$this->phpEnvironment = $this->config[$name]['environment'];
|
||||
break;
|
||||
case 's':
|
||||
$this->displaySkipped = TRUE;
|
||||
break;
|
||||
default:
|
||||
throw new Exception("Unknown option -$arg[1].");
|
||||
exit;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -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);
|
26
tests/initialize.php
Normal file
26
tests/initialize.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Test initialization and helpers.
|
||||
*
|
||||
* @author David Grudl
|
||||
* @package Nette\Test
|
||||
*/
|
||||
|
||||
require dirname(__FILE__) . '/NetteTest/TestHelpers.php';
|
||||
require dirname(__FILE__) . '/NetteTest/Assert.php';
|
||||
require dirname(__FILE__) . '/../dibi/dibi.php';
|
||||
|
||||
date_default_timezone_set('Europe/Prague');
|
||||
|
||||
TestHelpers::startup();
|
||||
|
||||
if (function_exists('class_alias')) {
|
||||
class_alias('TestHelpers', 'T');
|
||||
} else {
|
||||
class T extends TestHelpers {}
|
||||
}
|
||||
|
||||
// load connections
|
||||
define('DIR', dirname(__FILE__));
|
||||
$config = parse_ini_file('config.ini', TRUE);
|
@@ -1 +1 @@
|
||||
Dibi 2.1.2 (revision $WCREV$ released on $WCDATE$)
|
||||
Dibi 2.0.5 (released on 2015-01-13)
|
||||
|
Reference in New Issue
Block a user