1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-31 17:51:43 +02:00

Compare commits

...

85 Commits
v2.3 ... v3.0.2

Author SHA1 Message Date
David Grudl
8948cc293c Released version 3.0.2 2016-01-29 15:46:23 +01:00
David Grudl
193252dc5f PdoDriver::applyLimit() is the same as SqlsrvDriver 2016-01-28 20:13:49 +01:00
Mira Paulik
6b2a46bcb8 appveyor: testing with SQL Server 2012 & 2014 2016-01-28 19:55:55 +01:00
Mira Paulik
2c01d993d0 SqlsrvReflector::getTables(): gets list of all tables from dbo schema only 2016-01-28 19:54:46 +01:00
Mira Paulik
e415157206 SqlsrvDriver::applyLimit(): fixed limit and offset behaviour for odbc 11+
SqlsrvReflector: changed constrains metadata loading from INFORMATION_SCHEMA to sys schema to get complete list of all constraints, not PK only
2016-01-28 19:48:49 +01:00
David Grudl
8a7dbcba86 Revert "removed MsSqlDriver (is not available with PHP 5.3 or later; replaced with SqlsrvDriver)"
This reverts commit ac1ab26e7a.
2016-01-24 22:52:18 +01:00
David Grudl
4f2a0dac97 Released version 3.0.1 2015-12-16 15:15:56 +01:00
David Grudl
a13eb6f3d1 Merge pull request #200 from michal-kocarek/master
Allow disconnecting when passing PDO instance.
2015-12-16 15:02:43 +01:00
David Grudl
e3f01c879f uses https 2015-12-14 14:26:09 +01:00
Michal Kočárek
efae5808f0 PdoDriver: unset remaining references to PDO to allow disconnection 2015-12-14 12:40:41 +01:00
David Grudl
6a8a136c0a Merge pull request #199 from janlanger/patch-1
Helpers::detectType now resolves tinyint as integer
2015-12-06 23:30:38 +01:00
Jan Langer
bbb654fc27 Helpers::detectType resolves tinyint as integer 2015-12-06 22:03:06 +01:00
David Grudl
b6933815c7 Translator: added modifier %dt [Closes #198] 2015-12-05 11:33:28 +01:00
David Grudl
a8691eb8f5 Connection::substitute() fixed [Closes #197] 2015-11-26 12:24:57 +01:00
David Grudl
0cce3b9916 Strict: added support for the old way of adding extension methods [Closes #195] 2015-11-11 13:34:23 +01:00
David Grudl
98d1b2a519 MySqliDriver::createException() removed undefined ConnectionException [Closes #194] 2015-11-10 17:51:50 +01:00
David Grudl
f327212b57 Released version 3.0.0 2015-11-07 18:55:58 +01:00
David Grudl
ff22e8de09 Helpers::getSuggestion() better balance. Replacement is more expensive than insertion/deletion. 2015-11-07 02:31:14 +01:00
Petr Soukup
044fe4fc2a Fluent: added missing annotations [Closes #191] 2015-11-06 23:31:57 +01:00
David Grudl
e9c677c750 typo, docs 2015-11-06 23:31:56 +01:00
David Grudl
607ec8ae77 tests: not supported drivers are not skipped (except for 'mysql' on PHP 7) 2015-11-04 23:36:34 +01:00
David Grudl
deb56bb166 added appveyor.yml (thanks @mhujer) 2015-11-04 23:36:33 +01:00
David Grudl
5d44e55527 travis: uses own databases.travis.ini 2015-11-04 17:40:28 +01:00
David Grudl
78d24a0e74 tests: fixes 2015-11-04 17:40:19 +01:00
David Grudl
5aab1ff023 tests: improved ini quering, removed duplicated tests 2015-11-04 17:29:51 +01:00
David Grudl
6730fb4633 tests: added missing items to databases.sample.ini & etc 2015-11-04 17:29:51 +01:00
David Grudl
ac1ab26e7a removed MsSqlDriver (is not available with PHP 5.3 or later; replaced with SqlsrvDriver) 2015-11-04 03:59:21 +01:00
David Grudl
26a66c92d9 used type callable 2015-11-03 19:01:16 +01:00
David Grudl
2bb99ef3a0 DibiExtension22: bluescreen panel is registered in production mode too 2015-11-03 19:01:16 +01:00
David Grudl
b090ec802b differentiated Type::TIME and Type::DATETIME 2015-11-03 19:01:15 +01:00
David Grudl
89e92d24a2 Connection: added literal() 2015-11-02 18:23:59 +01:00
David Grudl
2627e23701 Fluent: fixed combination of modifier and inner fluent [Closes #192] 2015-11-02 18:00:29 +01:00
David Grudl
65d6f62a5b Translator::translate() can be called only once (BC break) 2015-11-02 18:00:28 +01:00
David Grudl
0c22f3b43f Translator: DateTime can be used only with %d or %t modifiers (BC break?) 2015-11-02 18:00:28 +01:00
David Grudl
b7d922d992 Translator: Literal can be used with %sql or %SQL modifiers 2015-11-02 18:00:27 +01:00
David Grudl
a9a45f0c4c Translator: better error messages 2015-11-02 18:00:27 +01:00
David Grudl
654e345921 added test, whitespace 2015-11-02 18:00:26 +01:00
David Grudl
f2a400084f Reflection\Result: fixed case insensitivity 2015-11-02 18:00:26 +01:00
David Grudl
a3325cac4d added deprecation notices 2015-11-02 18:00:25 +01:00
David Grudl
f19dd9208a Connection::alias() & loadFile() moved to Helpers 2015-10-26 19:20:59 +01:00
David Grudl
fd5cfaa9d3 drivers: applyLimit throws exception for negative values (BC break) 2015-10-26 19:20:58 +01:00
David Grudl
1333d07833 SqlsrvDriver: added support for LIMIT & OFFSET on SQL Server 2012 2015-10-26 19:20:58 +01:00
David Grudl
c26201c75d Fluent: exports limit & offset as %lmt and %ofs [Closes #82]
Also fixes 20f2093 on MSSQL
2015-10-26 19:20:57 +01:00
David Grudl
70c68402ea Fluent: prevents doubled processing 2015-10-26 19:20:57 +01:00
Pavel Zelezny
5fac432272 Connection: option 'driver' can contain driver instance or class name [Closes #153] 2015-10-26 19:20:56 +01:00
David Grudl
b3696f9beb FirebirdDriver: removed $sql from exception in fetch() 2015-10-23 17:24:01 +02:00
David Grudl
11027e2573 MsSql2005 driver renamed to Sqlsrv 2015-10-22 02:05:32 +02:00
David Grudl
d74908402d fixes 2015-10-22 02:05:31 +02:00
David Grudl
31cd9594a1 removed unused code 2015-10-22 02:05:30 +02:00
David Grudl
52f0793991 Result::dump() moved to Helpers 2015-10-13 21:16:49 +02:00
David Grudl
c79f4a1475 MySQL drivers: type TIME is returned as DateInterval (BC break) [Closes #168] 2015-10-13 21:16:49 +02:00
David Grudl
fbb63a9cc3 implemented DriverException descendants:
- ConstraintViolationException
- ForeignKeyConstraintViolationException
- NotNullConstraintViolationException
- UniqueConstraintViolationException
2015-10-13 21:16:48 +02:00
David Grudl
3f44e96353 tests: improved sql dumps, renamed pgsql -> postgre 2015-10-13 20:09:18 +02:00
David Grudl
6994f6d11a Dibi\Exception: code can be string 2015-10-13 20:09:12 +02:00
David Grudl
e849be486f DriverException: removed tryError & catchError (BC break) 2015-10-13 20:08:26 +02:00
David Grudl
8649366b1f PostgreDriver: removed usage of tryError & catchError 2015-10-13 20:04:45 +02:00
David Grudl
1a027c75ab FirebirdDriver: removed usage of tryError & catchError (not tested!) 2015-10-13 20:03:27 +02:00
David Grudl
ee4cd0d6ef Column::detectTypes() moved to Helpers 2015-10-13 15:48:24 +02:00
David Grudl
4630e1818f Result: normalizes invalid date to NULL 2015-10-13 15:04:22 +02:00
David Grudl
c0416bf176 Result: improved normalization of type INT 2015-10-13 15:04:14 +02:00
David Grudl
407a6f7550 Result: fixed normalization of float when ends with "0" [Closes #189] 2015-10-13 15:03:59 +02:00
David Grudl
91c2be2a84 Result: normalize always converts type TEXT to string 2015-10-13 15:03:48 +02:00
David Grudl
6f273ef601 Column::detectType() return NULL when type is unknown instead of TEXT 2015-10-13 15:03:18 +02:00
David Grudl
8bfb39f790 Merge pull request #190 from bckp/patch-1
Updated docs
2015-10-13 11:54:55 +02:00
Radovan Kepák
617e5ca6da Updated docs
Docs updated to new syntax, so people use new and not old deprecated one
2015-10-13 10:36:24 +02:00
castamir
20f2093aa5 Fluent::fetch(): fixed limit clause duplication [Closes #188][Closes #186][Closes #185] 2015-10-09 12:20:15 +02:00
David Grudl
0c4a28935d Fluent: removed keyword AS from SQL [Closes #172] 2015-10-09 12:20:14 +02:00
David Grudl
2e09a559f2 moved to namespace Dibi
added loader for old class names
2015-10-09 12:20:14 +02:00
David Grudl
6222e966c7 typos 2015-10-09 12:20:13 +02:00
David Grudl
8e2feec9fb removed @package 2015-10-09 12:20:12 +02:00
David Grudl
80e2fd6cc3 DibiRow: shows suggestions for missing columns 2015-10-09 12:20:12 +02:00
David Grudl
a119921832 DibiObject: shows suggestions for undeclared members 2015-10-09 12:20:11 +02:00
David Grudl
f42d1b1611 DibiObject replaced with trait DibiStrict 2015-10-09 12:20:11 +02:00
David Grudl
76396ab250 DibiObject: simplified to minimum (BC break)
Removed support for setters, onEvent(), getClass() & getReflection().
Retained support for getters and extension methods.
2015-10-09 12:20:10 +02:00
David Grudl
ae68965710 type constants dibi::* moved to new class Type 2015-10-09 12:20:09 +02:00
David Grudl
785a021b8d dibi::dump() moved to class Helpers 2015-10-09 12:20:09 +02:00
David Grudl
59223d937d removed usage of magic properties 2015-10-09 12:20:08 +02:00
David Grudl
7c1f735f9b used PHP 5.4 syntax 2015-10-09 12:20:08 +02:00
David Grudl
a32e24262f loader.php: uses SPL autoloader 2015-10-09 12:20:07 +02:00
David Grudl
3e010c0f4d removed PHP < 5.4 stuff 2015-10-09 12:20:06 +02:00
David Grudl
96daa02525 Minimal required PHP version changed to 5.4.4 2015-10-09 12:20:06 +02:00
David Grudl
cbebcba37d IDibiDriver: escape() & unescape() replaced with escape*() & unescapeBinary() (BC break!) 2015-10-09 12:20:05 +02:00
David Grudl
7f76a8b2f4 separation of some classes into own files 2015-10-09 12:20:05 +02:00
David Grudl
2522dcd7fe new directory structure, moved to /src 2015-10-09 12:20:04 +02:00
David Grudl
c96271c09b opened 2.4-dev 2015-10-08 22:23:03 +02:00
122 changed files with 5470 additions and 4444 deletions

View File

@@ -1,6 +1,5 @@
language: php language: php
php: php:
- 5.3.3
- 5.4 - 5.4
- 5.5 - 5.5
- 5.6 - 5.6
@@ -9,12 +8,11 @@ php:
matrix: matrix:
allow_failures: allow_failures:
- php: 7.0
- php: hhvm - php: hhvm
script: script:
- vendor/bin/tester tests -s -p php -c tests/php-unix.ini - vendor/bin/tester tests -s -p php -c tests/php-unix.ini
- php code-checker/src/code-checker.php - php temp/code-checker/src/code-checker.php --short-arrays
after_failure: after_failure:
# Print *.actual content # Print *.actual content
@@ -23,10 +21,10 @@ after_failure:
before_script: before_script:
# Install Nette Tester & Code Checker # Install Nette Tester & Code Checker
- travis_retry composer install --no-interaction - travis_retry composer install --no-interaction
- travis_retry composer create-project nette/code-checker code-checker ~2.5 --no-interaction - travis_retry composer create-project nette/code-checker temp/code-checker ~2.5 --no-interaction
# Create databases.ini # Create databases.ini
- cp ./tests/databases.sample.ini ./tests/databases.ini - cp ./tests/databases.travis.ini ./tests/databases.ini
# Create Postgre database # Create Postgre database
- psql -c 'CREATE DATABASE dibi_test' -U postgres - psql -c 'CREATE DATABASE dibi_test' -U postgres

45
appveyor.yml Normal file
View File

@@ -0,0 +1,45 @@
build: off
cache:
- c:\php -> appveyor.yml
- '%LOCALAPPDATA%\Composer\files -> appveyor.yml'
clone_folder: c:\projects\dibi
services:
- mssql2012sp1
- mssql2014
- mysql
init:
- SET PATH=c:\php;%PATH%
- SET PHP=1
- SET ANSICON=121x90 (121x90)
install:
# Install PHP
- IF EXIST c:\php (SET PHP=0) ELSE (mkdir c:\php)
- IF %PHP%==1 cd c:\php
- IF %PHP%==1 appveyor DownloadFile http://windows.php.net/downloads/releases/archives/php-5.6.14-Win32-VC11-x86.zip
- IF %PHP%==1 7z x php-5.6.14-Win32-VC11-x86.zip >nul
- IF %PHP%==1 echo extension_dir=ext >> php.ini
- IF %PHP%==1 echo extension=php_openssl.dll >> php.ini
- IF %PHP%==1 appveyor DownloadFile https://files.nette.org/misc/php-sqlsrv.zip
- IF %PHP%==1 7z x php-sqlsrv.zip >nul
- IF %PHP%==1 copy SQLSRV\php_sqlsrv_56_ts.dll ext\php_sqlsrv_ts.dll
- IF %PHP%==1 copy SQLSRV\php_pdo_sqlsrv_56_ts.dll ext\php_pdo_sqlsrv_ts.dll
- IF %PHP%==1 del /Q *.zip
- cd c:\projects\dibi
# Install Nette Tester
- appveyor DownloadFile https://getcomposer.org/composer.phar
- php composer.phar install --prefer-dist --no-interaction --no-progress
# Create databases.ini
- copy tests\databases.appveyor.ini tests\databases.ini
test_script:
- vendor\bin\tester tests -s -p php -c tests\php-win.ini
on_failure:
# Print *.actual content
- type tests\dibi\output\*.actual

View File

@@ -1,8 +1,8 @@
{ {
"name": "dibi/dibi", "name": "dibi/dibi",
"description": "Dibi is Database Abstraction Library for PHP", "description": "Dibi is Database Abstraction Library for PHP",
"keywords": ["database", "dbal", "mysql", "postgresql", "sqlite", "mssql", "oracle", "access", "pdo", "odbc"], "keywords": ["database", "dbal", "mysql", "postgresql", "sqlite", "mssql", "sqlsrv", "oracle", "access", "pdo", "odbc"],
"homepage": "http://dibiphp.com", "homepage": "https://dibiphp.com",
"license": ["BSD-3-Clause", "GPL-2.0", "GPL-3.0"], "license": ["BSD-3-Clause", "GPL-2.0", "GPL-3.0"],
"authors": [ "authors": [
{ {
@@ -11,7 +11,7 @@
} }
], ],
"require": { "require": {
"php": ">=5.2.0" "php": ">=5.4.4"
}, },
"require-dev": { "require-dev": {
"tracy/tracy": "~2.2", "tracy/tracy": "~2.2",
@@ -21,11 +21,12 @@
"dg/dibi": "self.version" "dg/dibi": "self.version"
}, },
"autoload": { "autoload": {
"classmap": ["dibi/"] "classmap": ["src/"],
"files": ["src/loader.php"]
}, },
"extra": { "extra": {
"branch-alias": { "branch-alias": {
"dev-master": "2.3-dev" "dev-master": "3.0-dev"
} }
} }
} }

View File

@@ -5,7 +5,7 @@ The issue tracker is the preferred channel for bug reports, features requests
and submitting pull requests, but please respect the following restrictions: and submitting pull requests, but please respect the following restrictions:
* Please **do not** use the issue tracker for personal support requests (use * Please **do not** use the issue tracker for personal support requests (use
[dibi forum](http://forum.dibiphp.com) or [Stack Overflow](http://stackoverflow.com)). [dibi forum](https://forum.dibiphp.com) or [Stack Overflow](http://stackoverflow.com)).
* Please **do not** derail or troll issues. Keep the discussion on topic and * Please **do not** derail or troll issues. Keep the discussion on topic and
respect the opinions of others. respect the opinions of others.

View File

@@ -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

View File

@@ -1,35 +1,3 @@
<?php <?php
trigger_error('Dibi was moved to /src/loader.php', E_USER_WARNING);
/** require __DIR__ . '/../src/loader.php';
* dibi - smart database abstraction layer (http://dibiphp.com)
*
* Copyright (c) 2005, 2012 David Grudl (https://davidgrudl.com)
*/
/**
* Check PHP configuration.
*/
if (version_compare(PHP_VERSION, '5.2.0', '<')) {
throw new Exception('dibi needs PHP 5.2.0 or newer.');
}
require_once dirname(__FILE__) . '/libs/interfaces.php';
require_once dirname(__FILE__) . '/libs/Dibi.php';
require_once dirname(__FILE__) . '/libs/DibiDateTime.php';
require_once dirname(__FILE__) . '/libs/DibiObject.php';
require_once dirname(__FILE__) . '/libs/DibiLiteral.php';
require_once dirname(__FILE__) . '/libs/DibiHashMap.php';
require_once dirname(__FILE__) . '/libs/DibiException.php';
require_once dirname(__FILE__) . '/libs/DibiConnection.php';
require_once dirname(__FILE__) . '/libs/DibiResult.php';
require_once dirname(__FILE__) . '/libs/DibiResultIterator.php';
require_once dirname(__FILE__) . '/libs/DibiRow.php';
require_once dirname(__FILE__) . '/libs/DibiTranslator.php';
require_once dirname(__FILE__) . '/libs/DibiDataSource.php';
require_once dirname(__FILE__) . '/libs/DibiFluent.php';
require_once dirname(__FILE__) . '/libs/DibiDatabaseInfo.php';
require_once dirname(__FILE__) . '/libs/DibiEvent.php';
require_once dirname(__FILE__) . '/libs/DibiFileLogger.php';
require_once dirname(__FILE__) . '/libs/DibiFirePhpLogger.php';

View File

@@ -1,419 +0,0 @@
<?php
/**
* This file is part of the "dibi" - smart database abstraction layer.
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/
require_once dirname(__FILE__) . '/DibiSqliteReflector.php';
/**
* The dibi driver for SQLite database.
*
* Driver options:
* - database (or file) => the filename of the SQLite database
* - persistent (bool) => try to find a persistent link?
* - unbuffered (bool) => sends query without fetching and buffering the result rows automatically?
* - formatDate => how to format date in SQL (@see date)
* - formatDateTime => how to format datetime in SQL (@see date)
* - dbcharset => database character encoding (will be converted to 'charset')
* - charset => character encoding to set (default is UTF-8)
* - resource (resource) => existing connection resource
* - lazy, profiler, result, substitutes, ... => see DibiConnection options
*
* @author David Grudl
* @package dibi\drivers
*/
class DibiSqliteDriver extends DibiObject implements IDibiDriver, IDibiResultDriver
{
/** @var resource Connection resource */
private $connection;
/** @var resource Resultset resource */
private $resultSet;
/** @var bool Is buffered (seekable and countable)? */
private $buffered;
/** @var string Date and datetime format */
private $fmtDate, $fmtDateTime;
/** @var string character encoding */
private $dbcharset, $charset;
/**
* @throws DibiNotSupportedException
*/
public function __construct()
{
if (!extension_loaded('sqlite')) {
throw new DibiNotSupportedException("PHP extension 'sqlite' is not loaded.");
}
}
/**
* Connects to a database.
* @return void
* @throws DibiException
*/
public function connect(array & $config)
{
DibiConnection::alias($config, 'database', 'file');
$this->fmtDate = isset($config['formatDate']) ? $config['formatDate'] : 'U';
$this->fmtDateTime = isset($config['formatDateTime']) ? $config['formatDateTime'] : 'U';
$errorMsg = '';
if (isset($config['resource'])) {
$this->connection = $config['resource'];
} elseif (empty($config['persistent'])) {
$this->connection = @sqlite_open($config['database'], 0666, $errorMsg); // intentionally @
} else {
$this->connection = @sqlite_popen($config['database'], 0666, $errorMsg); // intentionally @
}
if (!$this->connection) {
throw new DibiDriverException($errorMsg);
}
$this->buffered = empty($config['unbuffered']);
$this->dbcharset = empty($config['dbcharset']) ? 'UTF-8' : $config['dbcharset'];
$this->charset = empty($config['charset']) ? 'UTF-8' : $config['charset'];
if (strcasecmp($this->dbcharset, $this->charset) === 0) {
$this->dbcharset = $this->charset = NULL;
}
}
/**
* Disconnects from a database.
* @return void
*/
public function disconnect()
{
sqlite_close($this->connection);
}
/**
* Executes the SQL query.
* @param string SQL statement.
* @return IDibiResultDriver|NULL
* @throws DibiDriverException
*/
public function query($sql)
{
if ($this->dbcharset !== NULL) {
$sql = iconv($this->charset, $this->dbcharset . '//IGNORE', $sql);
}
DibiDriverException::tryError();
if ($this->buffered) {
$res = sqlite_query($this->connection, $sql);
} else {
$res = sqlite_unbuffered_query($this->connection, $sql);
}
if (DibiDriverException::catchError($msg)) {
throw new DibiDriverException($msg, sqlite_last_error($this->connection), $sql);
} elseif (is_resource($res)) {
return $this->createResultDriver($res);
}
}
/**
* Gets the number of affected rows by the last INSERT, UPDATE or DELETE query.
* @return int|FALSE number of rows or FALSE on error
*/
public function getAffectedRows()
{
return sqlite_changes($this->connection);
}
/**
* Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query.
* @return int|FALSE int on success or FALSE on failure
*/
public function getInsertId($sequence)
{
return sqlite_last_insert_rowid($this->connection);
}
/**
* Begins a transaction (if supported).
* @param string optional savepoint name
* @return void
* @throws DibiDriverException
*/
public function begin($savepoint = NULL)
{
$this->query('BEGIN');
}
/**
* Commits statements in a transaction.
* @param string optional savepoint name
* @return void
* @throws DibiDriverException
*/
public function commit($savepoint = NULL)
{
$this->query('COMMIT');
}
/**
* Rollback changes in a transaction.
* @param string optional savepoint name
* @return void
* @throws DibiDriverException
*/
public function rollback($savepoint = NULL)
{
$this->query('ROLLBACK');
}
/**
* Returns the connection resource.
* @return mixed
*/
public function getResource()
{
return is_resource($this->connection) ? $this->connection : NULL;
}
/**
* Returns the connection reflector.
* @return IDibiReflector
*/
public function getReflector()
{
return new DibiSqliteReflector($this);
}
/**
* Result set driver factory.
* @param resource
* @return IDibiResultDriver
*/
public function createResultDriver($resource)
{
$res = clone $this;
$res->resultSet = $resource;
return $res;
}
/********************* SQL ****************d*g**/
/**
* Encodes data for use in a SQL statement.
* @param mixed value
* @param string type (dibi::TEXT, dibi::BOOL, ...)
* @return string encoded value
* @throws InvalidArgumentException
*/
public function escape($value, $type)
{
switch ($type) {
case dibi::TEXT:
case dibi::BINARY:
return "'" . sqlite_escape_string($value) . "'";
case dibi::IDENTIFIER:
return '[' . strtr($value, '[]', ' ') . ']';
case dibi::BOOL:
return $value ? 1 : 0;
case dibi::DATE:
case dibi::DATETIME:
if (!$value instanceof DateTime && !$value instanceof DateTimeInterface) {
$value = new DibiDateTime($value);
}
return $value->format($type === dibi::DATETIME ? $this->fmtDateTime : $this->fmtDate);
default:
throw new InvalidArgumentException('Unsupported type.');
}
}
/**
* Encodes string for use in a LIKE statement.
* @param string
* @param int
* @return string
*/
public function escapeLike($value, $pos)
{
throw new DibiNotSupportedException;
}
/**
* Decodes data from result set.
* @param string value
* @param string type (dibi::BINARY)
* @return string decoded value
* @throws InvalidArgumentException
*/
public function unescape($value, $type)
{
if ($type === dibi::BINARY) {
return $value;
}
throw new InvalidArgumentException('Unsupported type.');
}
/**
* Injects LIMIT/OFFSET to the SQL query.
* @return void
*/
public function applyLimit(& $sql, $limit, $offset)
{
if ($limit >= 0 || $offset > 0) {
$sql .= ' LIMIT ' . (int) $limit . ($offset > 0 ? ' OFFSET ' . (int) $offset : '');
}
}
/********************* result set ****************d*g**/
/**
* Returns the number of rows in a result set.
* @return int
*/
public function getRowCount()
{
if (!$this->buffered) {
throw new DibiNotSupportedException('Row count is not available for unbuffered queries.');
}
return sqlite_num_rows($this->resultSet);
}
/**
* Fetches the row at current position and moves the internal cursor to the next position.
* @param bool TRUE for associative array, FALSE for numeric
* @return array array on success, nonarray if no next record
*/
public function fetch($assoc)
{
$row = sqlite_fetch_array($this->resultSet, $assoc ? SQLITE_ASSOC : SQLITE_NUM);
$charset = $this->charset === NULL ? NULL : $this->charset . '//TRANSLIT';
if ($row && ($assoc || $charset)) {
$tmp = array();
foreach ($row as $k => $v) {
if ($charset !== NULL && is_string($v)) {
$v = iconv($this->dbcharset, $charset, $v);
}
$tmp[str_replace(array('[', ']'), '', $k)] = $v;
}
return $tmp;
}
return $row;
}
/**
* Moves cursor position without fetching row.
* @param int the 0-based cursor pos to seek to
* @return bool TRUE on success, FALSE if unable to seek to specified record
* @throws DibiException
*/
public function seek($row)
{
if (!$this->buffered) {
throw new DibiNotSupportedException('Cannot seek an unbuffered result set.');
}
return sqlite_seek($this->resultSet, $row);
}
/**
* Frees the resources allocated for this result set.
* @return void
*/
public function free()
{
$this->resultSet = NULL;
}
/**
* Returns metadata for all columns in a result set.
* @return array
*/
public function getResultColumns()
{
$count = sqlite_num_fields($this->resultSet);
$columns = array();
for ($i = 0; $i < $count; $i++) {
$name = str_replace(array('[', ']'), '', sqlite_field_name($this->resultSet, $i));
$pair = explode('.', $name);
$columns[] = array(
'name' => isset($pair[1]) ? $pair[1] : $pair[0],
'table' => isset($pair[1]) ? $pair[0] : NULL,
'fullname' => $name,
'nativetype' => NULL,
);
}
return $columns;
}
/**
* Returns the result set resource.
* @return mixed
*/
public function getResultResource()
{
return is_resource($this->resultSet) ? $this->resultSet : NULL;
}
/********************* user defined functions ****************d*g**/
/**
* Registers an user defined function for use in SQL statements.
* @param string function name
* @param mixed callback
* @param int num of arguments
* @return void
*/
public function registerFunction($name, $callback, $numArgs = -1)
{
sqlite_create_function($this->connection, $name, $callback, $numArgs);
}
/**
* Registers an aggregating user defined function for use in SQL statements.
* @param string function name
* @param mixed callback called for each row of the result set
* @param mixed callback called to aggregate the "stepped" data from each row
* @param int num of arguments
* @return void
*/
public function registerAggregateFunction($name, $rowCallback, $agrCallback, $numArgs = -1)
{
sqlite_create_aggregate($this->connection, $name, $rowCallback, $agrCallback, $numArgs);
}
}

View File

@@ -1,702 +0,0 @@
<?php
/**
* This file is part of the "dibi" - smart database abstraction layer.
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/
/**
* Reflection metadata class for a database.
*
* @package dibi\reflection
*
* @property-read string $name
* @property-read array $tables
* @property-read array $tableNames
*/
class DibiDatabaseInfo extends DibiObject
{
/** @var IDibiReflector */
private $reflector;
/** @var string */
private $name;
/** @var array */
private $tables;
public function __construct(IDibiReflector $reflector, $name)
{
$this->reflector = $reflector;
$this->name = $name;
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @return DibiTableInfo[]
*/
public function getTables()
{
$this->init();
return array_values($this->tables);
}
/**
* @return string[]
*/
public function getTableNames()
{
$this->init();
$res = array();
foreach ($this->tables as $table) {
$res[] = $table->getName();
}
return $res;
}
/**
* @param string
* @return DibiTableInfo
*/
public function getTable($name)
{
$this->init();
$l = strtolower($name);
if (isset($this->tables[$l])) {
return $this->tables[$l];
} else {
throw new DibiException("Database '$this->name' has no table '$name'.");
}
}
/**
* @param string
* @return bool
*/
public function hasTable($name)
{
$this->init();
return isset($this->tables[strtolower($name)]);
}
/**
* @return void
*/
protected function init()
{
if ($this->tables === NULL) {
$this->tables = array();
foreach ($this->reflector->getTables() as $info) {
$this->tables[strtolower($info['name'])] = new DibiTableInfo($this->reflector, $info);
}
}
}
}
/**
* Reflection metadata class for a database table.
*
* @package dibi\reflection
*
* @property-read string $name
* @property-read bool $view
* @property-read array $columns
* @property-read array $columnNames
* @property-read array $foreignKeys
* @property-read array $indexes
* @property-read DibiIndexInfo $primaryKey
*/
class DibiTableInfo extends DibiObject
{
/** @var IDibiReflector */
private $reflector;
/** @var string */
private $name;
/** @var bool */
private $view;
/** @var array */
private $columns;
/** @var array */
private $foreignKeys;
/** @var array */
private $indexes;
/** @var DibiIndexInfo */
private $primaryKey;
public function __construct(IDibiReflector $reflector, array $info)
{
$this->reflector = $reflector;
$this->name = $info['name'];
$this->view = !empty($info['view']);
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @return bool
*/
public function isView()
{
return $this->view;
}
/**
* @return DibiColumnInfo[]
*/
public function getColumns()
{
$this->initColumns();
return array_values($this->columns);
}
/**
* @return string[]
*/
public function getColumnNames()
{
$this->initColumns();
$res = array();
foreach ($this->columns as $column) {
$res[] = $column->getName();
}
return $res;
}
/**
* @param string
* @return DibiColumnInfo
*/
public function getColumn($name)
{
$this->initColumns();
$l = strtolower($name);
if (isset($this->columns[$l])) {
return $this->columns[$l];
} else {
throw new DibiException("Table '$this->name' has no column '$name'.");
}
}
/**
* @param string
* @return bool
*/
public function hasColumn($name)
{
$this->initColumns();
return isset($this->columns[strtolower($name)]);
}
/**
* @return DibiForeignKeyInfo[]
*/
public function getForeignKeys()
{
$this->initForeignKeys();
return $this->foreignKeys;
}
/**
* @return DibiIndexInfo[]
*/
public function getIndexes()
{
$this->initIndexes();
return $this->indexes;
}
/**
* @return DibiIndexInfo
*/
public function getPrimaryKey()
{
$this->initIndexes();
return $this->primaryKey;
}
/**
* @return void
*/
protected function initColumns()
{
if ($this->columns === NULL) {
$this->columns = array();
foreach ($this->reflector->getColumns($this->name) as $info) {
$this->columns[strtolower($info['name'])] = new DibiColumnInfo($this->reflector, $info);
}
}
}
/**
* @return void
*/
protected function initIndexes()
{
if ($this->indexes === NULL) {
$this->initColumns();
$this->indexes = array();
foreach ($this->reflector->getIndexes($this->name) as $info) {
foreach ($info['columns'] as $key => $name) {
$info['columns'][$key] = $this->columns[strtolower($name)];
}
$this->indexes[strtolower($info['name'])] = new DibiIndexInfo($info);
if (!empty($info['primary'])) {
$this->primaryKey = $this->indexes[strtolower($info['name'])];
}
}
}
}
/**
* @return void
*/
protected function initForeignKeys()
{
throw new DibiNotImplementedException;
}
}
/**
* Reflection metadata class for a result set.
*
* @package dibi\reflection
*
* @property-read array $columns
* @property-read array $columnNames
*/
class DibiResultInfo extends DibiObject
{
/** @var IDibiResultDriver */
private $driver;
/** @var array */
private $columns;
/** @var array */
private $names;
public function __construct(IDibiResultDriver $driver)
{
$this->driver = $driver;
}
/**
* @return DibiColumnInfo[]
*/
public function getColumns()
{
$this->initColumns();
return array_values($this->columns);
}
/**
* @param bool
* @return string[]
*/
public function getColumnNames($fullNames = FALSE)
{
$this->initColumns();
$res = array();
foreach ($this->columns as $column) {
$res[] = $fullNames ? $column->getFullName() : $column->getName();
}
return $res;
}
/**
* @param string
* @return DibiColumnInfo
*/
public function getColumn($name)
{
$this->initColumns();
$l = strtolower($name);
if (isset($this->names[$l])) {
return $this->names[$l];
} else {
throw new DibiException("Result set has no column '$name'.");
}
}
/**
* @param string
* @return bool
*/
public function hasColumn($name)
{
$this->initColumns();
return isset($this->names[strtolower($name)]);
}
/**
* @return void
*/
protected function initColumns()
{
if ($this->columns === NULL) {
$this->columns = array();
$reflector = $this->driver instanceof IDibiReflector ? $this->driver : NULL;
foreach ($this->driver->getResultColumns() as $info) {
$this->columns[] = $this->names[$info['name']] = new DibiColumnInfo($reflector, $info);
}
}
}
}
/**
* Reflection metadata class for a table or result set column.
*
* @package dibi\reflection
*
* @property-read string $name
* @property-read string $fullName
* @property-read DibiTableInfo $table
* @property-read string $type
* @property-read mixed $nativeType
* @property-read int $size
* @property-read bool $unsigned
* @property-read bool $nullable
* @property-read bool $autoIncrement
* @property-read mixed $default
*/
class DibiColumnInfo extends DibiObject
{
/** @var array */
private static $types;
/** @var IDibiReflector|NULL when created by DibiResultInfo */
private $reflector;
/** @var array (name, nativetype, [table], [fullname], [size], [nullable], [default], [autoincrement], [vendor]) */
private $info;
public function __construct(IDibiReflector $reflector = NULL, array $info)
{
$this->reflector = $reflector;
$this->info = $info;
}
/**
* @return string
*/
public function getName()
{
return $this->info['name'];
}
/**
* @return string
*/
public function getFullName()
{
return isset($this->info['fullname']) ? $this->info['fullname'] : NULL;
}
/**
* @return bool
*/
public function hasTable()
{
return !empty($this->info['table']);
}
/**
* @return DibiTableInfo
*/
public function getTable()
{
if (empty($this->info['table']) || !$this->reflector) {
throw new DibiException("Table is unknown or not available.");
}
return new DibiTableInfo($this->reflector, array('name' => $this->info['table']));
}
/**
* @return string
*/
public function getTableName()
{
return isset($this->info['table']) && $this->info['table'] != NULL ? $this->info['table'] : NULL; // intentionally ==
}
/**
* @return string
*/
public function getType()
{
return self::getTypeCache()->{$this->info['nativetype']};
}
/**
* @return mixed
*/
public function getNativeType()
{
return $this->info['nativetype'];
}
/**
* @return int
*/
public function getSize()
{
return isset($this->info['size']) ? (int) $this->info['size'] : NULL;
}
/**
* @return bool
*/
public function isUnsigned()
{
return isset($this->info['unsigned']) ? (bool) $this->info['unsigned'] : NULL;
}
/**
* @return bool
*/
public function isNullable()
{
return isset($this->info['nullable']) ? (bool) $this->info['nullable'] : NULL;
}
/**
* @return bool
*/
public function isAutoIncrement()
{
return isset($this->info['autoincrement']) ? (bool) $this->info['autoincrement'] : NULL;
}
/**
* @return mixed
*/
public function getDefault()
{
return isset($this->info['default']) ? $this->info['default'] : NULL;
}
/**
* @param string
* @return mixed
*/
public function getVendorInfo($key)
{
return isset($this->info['vendor'][$key]) ? $this->info['vendor'][$key] : NULL;
}
/**
* Heuristic type detection.
* @param string
* @return string
* @internal
*/
public static function detectType($type)
{
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,
'CURRENCY|REAL|MONEY|FLOAT|DOUBLE|DECIMAL|NUMERIC|NUMBER' => dibi::FLOAT,
'^TIME$' => dibi::TIME,
'TIME' => dibi::DATETIME, // DATETIME, TIMESTAMP
'DATE' => dibi::DATE,
'BOOL' => dibi::BOOL,
);
foreach ($patterns as $s => $val) {
if (preg_match("#$s#i", $type)) {
return $val;
}
}
return dibi::TEXT;
}
/**
* @internal
*/
public static function getTypeCache()
{
if (self::$types === NULL) {
self::$types = new DibiHashMap(array(__CLASS__, 'detectType'));
}
return self::$types;
}
}
/**
* Reflection metadata class for a foreign key.
*
* @package dibi\reflection
* @todo
*
* @property-read string $name
* @property-read array $references
*/
class DibiForeignKeyInfo extends DibiObject
{
/** @var string */
private $name;
/** @var array of array(local, foreign, onDelete, onUpdate) */
private $references;
public function __construct($name, array $references)
{
$this->name = $name;
$this->references = $references;
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @return array
*/
public function getReferences()
{
return $this->references;
}
}
/**
* Reflection metadata class for a index or primary key.
*
* @package dibi\reflection
*
* @property-read string $name
* @property-read array $columns
* @property-read bool $unique
* @property-read bool $primary
*/
class DibiIndexInfo extends DibiObject
{
/** @var array (name, columns, [unique], [primary]) */
private $info;
public function __construct(array $info)
{
$this->info = $info;
}
/**
* @return string
*/
public function getName()
{
return $this->info['name'];
}
/**
* @return array
*/
public function getColumns()
{
return $this->info['columns'];
}
/**
* @return bool
*/
public function isUnique()
{
return !empty($this->info['unique']);
}
/**
* @return bool
*/
public function isPrimary()
{
return !empty($this->info['primary']);
}
}

View File

@@ -1,88 +0,0 @@
<?php
/**
* This file is part of the "dibi" - smart database abstraction layer.
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/
/**
* DateTime with serialization and timestamp support for PHP 5.2.
*
* @package dibi
*/
class DibiDateTime extends DateTime
{
public function __construct($time = 'now', DateTimeZone $timezone = NULL)
{
if (is_numeric($time)) {
parent::__construct('@' . $time);
$this->setTimeZone($timezone ? $timezone : new DateTimeZone(date_default_timezone_get()));
} elseif ($timezone === NULL) {
parent::__construct($time);
} else {
parent::__construct($time, $timezone);
}
}
public function modifyClone($modify = '')
{
$dolly = clone($this);
return $modify ? $dolly->modify($modify) : $dolly;
}
public function modify($modify)
{
parent::modify($modify);
return $this;
}
public function setTimestamp($timestamp)
{
$zone = PHP_VERSION_ID === 50206 ? new DateTimeZone($this->getTimezone()->getName()) : $this->getTimezone();
$this->__construct('@' . $timestamp);
$this->setTimeZone($zone);
return $this;
}
public function getTimestamp()
{
$ts = $this->format('U');
return is_float($tmp = $ts * 1) ? $ts : $tmp;
}
public function __toString()
{
return $this->format('Y-m-d H:i:s');
}
public function __sleep()
{
$zone = $this->getTimezone()->getName();
if ($zone[0] === '+') {
$this->fix = array($this->format('Y-m-d H:i:sP'));
} else {
$this->fix = array($this->format('Y-m-d H:i:s'), $zone);
}
return array('fix');
}
public function __wakeup()
{
if (isset($this->fix[1])) {
$this->__construct($this->fix[0], new DateTimeZone($this->fix[1]));
} else {
$this->__construct($this->fix[0]);
}
unset($this->fix);
}
}

View File

@@ -1,307 +0,0 @@
<?php
/**
* This file is part of the "dibi" - smart database abstraction layer.
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/
/**
* DibiObject is the ultimate ancestor of all instantiable classes.
*
* DibiObject is copy of Nette\Object from Nette Framework (https://nette.org).
*
* It defines some handful methods and enhances object core of PHP:
* - access to undeclared members throws exceptions
* - support for conventional properties with getters and setters
* - support for event raising functionality
* - ability to add new methods to class (extension methods)
*
* Properties is a syntactic sugar which allows access public getter and setter
* methods as normal object variables. A property is defined by a getter method
* and optional setter method (no setter method means read-only property).
* <code>
* $val = $obj->label; // equivalent to $val = $obj->getLabel();
* $obj->label = 'Nette'; // equivalent to $obj->setLabel('Nette');
* </code>
* Property names are case-sensitive, and they are written in the camelCaps
* or PascalCaps.
*
* Event functionality is provided by declaration of property named 'on{Something}'
* Multiple handlers are allowed.
* <code>
* public $onClick; // declaration in class
* $this->onClick[] = 'callback'; // attaching event handler
* if (!empty($this->onClick)) ... // are there any handlers?
* $this->onClick($sender, $arg); // raises the event with arguments
* </code>
*
* Adding method to class (i.e. to all instances) works similar to JavaScript
* prototype property. The syntax for adding a new method is:
* <code>
* MyClass::extensionMethod('newMethod', function (MyClass $obj, $arg, ...) { ... });
* $obj = new MyClass;
* $obj->newMethod($x);
* </code>
*
* @package dibi
*/
abstract class DibiObject
{
/** @var array (method => array(type => callback)) */
private static $extMethods;
/**
* Returns the name of the class of this object.
* @return string
*/
final public /*static*/ function getClass()
{
return /*get_called_class()*/ /**/get_class($this)/**/;
}
/**
* Access to reflection.
* @return \ReflectionObject
*/
final public function getReflection()
{
return new ReflectionObject($this);
}
/**
* Call to undefined method.
* @param string method name
* @param array arguments
* @return mixed
* @throws \LogicException
*/
public function __call($name, $args)
{
$class = get_class($this);
if ($name === '') {
throw new LogicException("Call to class '$class' method without name.");
}
// event functionality
if (preg_match('#^on[A-Z]#', $name)) {
$rp = new ReflectionProperty($class, $name);
if ($rp->isPublic() && !$rp->isStatic()) {
$list = $this->$name;
if (is_array($list) || $list instanceof Traversable) {
foreach ($list as $handler) {
/**/if (is_object($handler)) {
call_user_func_array(array($handler, '__invoke'), $args);
} else /**/{
call_user_func_array($handler, $args);
}
}
}
return NULL;
}
}
// extension methods
if ($cb = self::extensionMethod("$class::$name")) {
array_unshift($args, $this);
return call_user_func_array($cb, $args);
}
throw new LogicException("Call to undefined method $class::$name().");
}
/**
* Call to undefined static method.
* @param string method name (in lower case!)
* @param array arguments
* @return mixed
* @throws \LogicException
*/
public static function __callStatic($name, $args)
{
$class = get_called_class();
throw new LogicException("Call to undefined static method $class::$name().");
}
/**
* Adding method to class.
* @param string method name
* @param mixed callback or closure
* @return mixed
*/
public static function extensionMethod($name, $callback = NULL)
{
if (self::$extMethods === NULL || $name === NULL) { // for backwards compatibility
$list = get_defined_functions();
foreach ($list['user'] as $fce) {
$pair = explode('_prototype_', $fce);
if (count($pair) === 2) {
self::$extMethods[$pair[1]][$pair[0]] = $fce;
self::$extMethods[$pair[1]][''] = NULL;
}
}
if ($name === NULL) {
return NULL;
}
}
$name = strtolower($name);
$a = strrpos($name, ':'); // search ::
if ($a === FALSE) {
$class = strtolower(get_called_class());
$l = & self::$extMethods[$name];
} else {
$class = substr($name, 0, $a - 1);
$l = & self::$extMethods[substr($name, $a + 1)];
}
if ($callback !== NULL) { // works as setter
$l[$class] = $callback;
$l[''] = NULL;
return NULL;
}
// works as getter
if (empty($l)) {
return FALSE;
} elseif (isset($l[''][$class])) { // cached value
return $l[''][$class];
}
$cl = $class;
do {
$cl = strtolower($cl);
if (isset($l[$cl])) {
return $l[''][$class] = $l[$cl];
}
} while (($cl = get_parent_class($cl)) !== FALSE);
foreach (class_implements($class) as $cl) {
$cl = strtolower($cl);
if (isset($l[$cl])) {
return $l[''][$class] = $l[$cl];
}
}
return $l[''][$class] = FALSE;
}
/**
* Returns property value. Do not call directly.
* @param string property name
* @return mixed property value
* @throws \LogicException if the property is not defined.
*/
public function & __get($name)
{
$class = get_class($this);
if ($name === '') {
throw new LogicException("Cannot read a class '$class' property without name.");
}
// property getter support
$uname = ucfirst($name);
$m = 'get' . $uname;
if (self::hasAccessor($class, $m)) {
// ampersands:
// - uses & __get() because declaration should be forward compatible (e.g. with Nette\Web\Html)
// - doesn't call & $this->$m because user could bypass property setter by: $x = & $obj->property; $x = 'new value';
$val = $this->$m();
return $val;
}
$m = 'is' . $uname;
if (self::hasAccessor($class, $m)) {
$val = $this->$m();
return $val;
}
throw new LogicException("Cannot read an undeclared property $class::\$$name.");
}
/**
* Sets value of a property. Do not call directly.
* @param string property name
* @param mixed property value
* @return void
* @throws \LogicException if the property is not defined or is read-only
*/
public function __set($name, $value)
{
$class = get_class($this);
if ($name === '') {
throw new LogicException("Cannot assign to a class '$class' property without name.");
}
// property setter support
$uname = ucfirst($name);
if (self::hasAccessor($class, 'get' . $uname) || self::hasAccessor($class, 'is' . $uname)) {
$m = 'set' . $name;
if (self::hasAccessor($class, $m)) {
$this->$m($value);
return;
} else {
throw new LogicException("Cannot assign to a read-only property $class::\$$name.");
}
}
throw new LogicException("Cannot assign to an undeclared property $class::\$$name.");
}
/**
* Is property defined?
* @param string property name
* @return bool
*/
public function __isset($name)
{
return $name !== '' && self::hasAccessor(get_class($this), 'get' . ucfirst($name));
}
/**
* Access to undeclared property.
* @param string property name
* @return void
* @throws \LogicException
*/
public function __unset($name)
{
$class = get_class($this);
throw new LogicException("Cannot unset the property $class::\$$name.");
}
/**
* Has property an accessor?
* @param string class name
* @param string method name
* @return bool
*/
private static function hasAccessor($c, $m)
{
static $cache;
if (!isset($cache[$c])) {
// get_class_methods returns private, protected and public methods of Object (doesn't matter)
// and ONLY PUBLIC methods of descendants (perfect!)
// but returns static methods too (nothing doing...)
// and is much faster than reflection
// (works good since 5.0.4)
$cache[$c] = array_flip(get_class_methods($c));
}
return isset($cache[$c][$m]);
}
}

View File

@@ -4,32 +4,32 @@
<?php <?php
require __DIR__ . '/../dibi/dibi.php'; require __DIR__ . '/../src/loader.php';
// connects to SQlite using dibi class // connects to SQlite using dibi class
echo '<p>Connecting to Sqlite: '; echo '<p>Connecting to Sqlite: ';
try { try {
dibi::connect(array( dibi::connect([
'driver' => 'sqlite3', 'driver' => 'sqlite3',
'database' => 'data/sample.s3db', 'database' => 'data/sample.s3db',
)); ]);
echo 'OK'; echo 'OK';
} catch (DibiException $e) { } catch (Dibi\Exception $e) {
echo get_class($e), ': ', $e->getMessage(), "\n"; echo get_class($e), ': ', $e->getMessage(), "\n";
} }
echo "</p>\n"; echo "</p>\n";
// connects to SQlite using DibiConnection object // connects to SQlite using Dibi\Connection object
echo '<p>Connecting to Sqlite: '; echo '<p>Connecting to Sqlite: ';
try { try {
$connection = new DibiConnection(array( $connection = new Dibi\Connection([
'driver' => 'sqlite3', 'driver' => 'sqlite3',
'database' => 'data/sample.s3db', 'database' => 'data/sample.s3db',
)); ]);
echo 'OK'; echo 'OK';
} catch (DibiException $e) { } catch (Dibi\Exception $e) {
echo get_class($e), ': ', $e->getMessage(), "\n"; echo get_class($e), ': ', $e->getMessage(), "\n";
} }
echo "</p>\n"; echo "</p>\n";
@@ -40,7 +40,7 @@ echo '<p>Connecting to MySQL: ';
try { try {
dibi::connect('driver=mysql&host=localhost&username=root&password=xxx&database=test&charset=cp1250'); dibi::connect('driver=mysql&host=localhost&username=root&password=xxx&database=test&charset=cp1250');
echo 'OK'; echo 'OK';
} catch (DibiException $e) { } catch (Dibi\Exception $e) {
echo get_class($e), ': ', $e->getMessage(), "\n"; echo get_class($e), ': ', $e->getMessage(), "\n";
} }
echo "</p>\n"; echo "</p>\n";
@@ -49,19 +49,19 @@ echo "</p>\n";
// connects to MySQLi using array // connects to MySQLi using array
echo '<p>Connecting to MySQLi: '; echo '<p>Connecting to MySQLi: ';
try { try {
dibi::connect(array( dibi::connect([
'driver' => 'mysqli', 'driver' => 'mysqli',
'host' => 'localhost', 'host' => 'localhost',
'username' => 'root', 'username' => 'root',
'password' => 'xxx', 'password' => 'xxx',
'database' => 'dibi', 'database' => 'dibi',
'options' => array( 'options' => [
MYSQLI_OPT_CONNECT_TIMEOUT => 30, MYSQLI_OPT_CONNECT_TIMEOUT => 30,
), ],
'flags' => MYSQLI_CLIENT_COMPRESS, 'flags' => MYSQLI_CLIENT_COMPRESS,
)); ]);
echo 'OK'; echo 'OK';
} catch (DibiException $e) { } catch (Dibi\Exception $e) {
echo get_class($e), ': ', $e->getMessage(), "\n"; echo get_class($e), ': ', $e->getMessage(), "\n";
} }
echo "</p>\n"; echo "</p>\n";
@@ -70,14 +70,14 @@ echo "</p>\n";
// connects to ODBC // connects to ODBC
echo '<p>Connecting to ODBC: '; echo '<p>Connecting to ODBC: ';
try { try {
dibi::connect(array( dibi::connect([
'driver' => 'odbc', 'driver' => 'odbc',
'username' => 'root', 'username' => 'root',
'password' => '***', 'password' => '***',
'dsn' => 'Driver={Microsoft Access Driver (*.mdb)};Dbq='.__DIR__.'/data/sample.mdb', 'dsn' => 'Driver={Microsoft Access Driver (*.mdb)};Dbq='.__DIR__.'/data/sample.mdb',
)); ]);
echo 'OK'; echo 'OK';
} catch (DibiException $e) { } catch (Dibi\Exception $e) {
echo get_class($e), ': ', $e->getMessage(), "\n"; echo get_class($e), ': ', $e->getMessage(), "\n";
} }
echo "</p>\n"; echo "</p>\n";
@@ -86,13 +86,13 @@ echo "</p>\n";
// connects to PostgreSql // connects to PostgreSql
echo '<p>Connecting to PostgreSql: '; echo '<p>Connecting to PostgreSql: ';
try { try {
dibi::connect(array( dibi::connect([
'driver' => 'postgre', 'driver' => 'postgre',
'string' => 'host=localhost port=5432 dbname=mary', 'string' => 'host=localhost port=5432 dbname=mary',
'persistent' => TRUE, 'persistent' => TRUE,
)); ]);
echo 'OK'; echo 'OK';
} catch (DibiException $e) { } catch (Dibi\Exception $e) {
echo get_class($e), ': ', $e->getMessage(), "\n"; echo get_class($e), ': ', $e->getMessage(), "\n";
} }
echo "</p>\n"; echo "</p>\n";
@@ -101,12 +101,12 @@ echo "</p>\n";
// connects to PDO // connects to PDO
echo '<p>Connecting to Sqlite via PDO: '; echo '<p>Connecting to Sqlite via PDO: ';
try { try {
dibi::connect(array( dibi::connect([
'driver' => 'pdo', 'driver' => 'pdo',
'dsn' => 'sqlite2::memory:', 'dsn' => 'sqlite::memory:',
)); ]);
echo 'OK'; echo 'OK';
} catch (DibiException $e) { } catch (Dibi\Exception $e) {
echo get_class($e), ': ', $e->getMessage(), "\n"; echo get_class($e), ': ', $e->getMessage(), "\n";
} }
echo "</p>\n"; echo "</p>\n";
@@ -115,31 +115,31 @@ echo "</p>\n";
// connects to MS SQL // connects to MS SQL
echo '<p>Connecting to MS SQL: '; echo '<p>Connecting to MS SQL: ';
try { try {
dibi::connect(array( dibi::connect([
'driver' => 'mssql', 'driver' => 'mssql',
'host' => 'localhost', 'host' => 'localhost',
'username' => 'root', 'username' => 'root',
'password' => 'xxx', 'password' => 'xxx',
)); ]);
echo 'OK'; echo 'OK';
} catch (DibiException $e) { } catch (Dibi\Exception $e) {
echo get_class($e), ': ', $e->getMessage(), "\n"; echo get_class($e), ': ', $e->getMessage(), "\n";
} }
echo "</p>\n"; echo "</p>\n";
// connects to MS SQL 2005 // connects to SQLSRV
echo '<p>Connecting to MS SQL 2005: '; echo '<p>Connecting to Microsoft SQL Server: ';
try { try {
dibi::connect(array( dibi::connect([
'driver' => 'mssql2005', 'driver' => 'sqlsrv',
'host' => '(local)', 'host' => '(local)',
'username' => 'Administrator', 'username' => 'Administrator',
'password' => 'xxx', 'password' => 'xxx',
'database' => 'main', 'database' => 'main',
)); ]);
echo 'OK'; echo 'OK';
} catch (DibiException $e) { } catch (Dibi\Exception $e) {
echo get_class($e), ': ', $e->getMessage(), "\n"; echo get_class($e), ': ', $e->getMessage(), "\n";
} }
echo "</p>\n"; echo "</p>\n";
@@ -148,14 +148,14 @@ echo "</p>\n";
// connects to Oracle // connects to Oracle
echo '<p>Connecting to Oracle: '; echo '<p>Connecting to Oracle: ';
try { try {
dibi::connect(array( dibi::connect([
'driver' => 'oracle', 'driver' => 'oracle',
'username' => 'root', 'username' => 'root',
'password' => 'xxx', 'password' => 'xxx',
'database' => 'db', 'database' => 'db',
)); ]);
echo 'OK'; echo 'OK';
} catch (DibiException $e) { } catch (Dibi\Exception $e) {
echo get_class($e), ': ', $e->getMessage(), "\n"; echo get_class($e), ': ', $e->getMessage(), "\n";
} }
echo "</p>\n"; echo "</p>\n";

Binary file not shown.

View File

@@ -4,22 +4,22 @@
<?php <?php
require __DIR__ . '/../dibi/dibi.php'; require __DIR__ . '/../src/loader.php';
dibi::connect(array( dibi::connect([
'driver' => 'sqlite3', 'driver' => 'sqlite3',
'database' => 'data/sample.s3db', 'database' => 'data/sample.s3db',
)); ]);
// retrieve database reflection // retrieve database reflection
$database = dibi::getDatabaseInfo(); $database = dibi::getDatabaseInfo();
echo "<h2>Database '{$database->name}'</h2>\n"; echo "<h2>Database '{$database->getName()}'</h2>\n";
echo "<ul>\n"; echo "<ul>\n";
foreach ($database->getTables() as $table) { foreach ($database->getTables() as $table) {
echo '<li>', ($table->view ? 'view' : 'table') . " $table->name</li>\n"; echo '<li>', ($table->isView() ? 'view' : 'table') . " {$table->getName()}</li>\n";
} }
echo "</ul>\n"; echo "</ul>\n";
@@ -27,12 +27,12 @@ echo "</ul>\n";
// table reflection // table reflection
$table = $database->getTable('products'); $table = $database->getTable('products');
echo "<h2>Table '{$table->name}'</h2>\n"; echo "<h2>Table '{$table->getName()}'</h2>\n";
echo "Columns\n"; echo "Columns\n";
echo "<ul>\n"; echo "<ul>\n";
foreach ($table->getColumns() as $column) { foreach ($table->getColumns() as $column) {
echo "<li>{$column->name} <i>{$column->nativeType}</i> <code>{$column->default}</code></li>\n"; echo "<li>{$column->getName()} <i>{$column->getNativeType()}</i> <code>{$column->getDefault()}</code></li>\n";
} }
echo "</ul>\n"; echo "</ul>\n";
@@ -40,9 +40,9 @@ echo "</ul>\n";
echo 'Indexes'; echo 'Indexes';
echo "<ul>\n"; echo "<ul>\n";
foreach ($table->getIndexes() as $index) { foreach ($table->getIndexes() as $index) {
echo "<li>{$index->name} " . ($index->primary ? 'primary ' : '') . ($index->unique ? 'unique' : '') . ' ('; echo "<li>{$index->getName()} " . ($index->isPrimary() ? 'primary ' : '') . ($index->isUnique() ? 'unique' : '') . ' (';
foreach ($index->getColumns() as $column) { foreach ($index->getColumns() as $column) {
echo "$column->name, "; echo $column->getName(), ', ';
} }
echo ")</li>\n"; echo ")</li>\n";
} }

View File

@@ -4,13 +4,13 @@
<?php <?php
require __DIR__ . '/../dibi/dibi.php'; require __DIR__ . '/../src/loader.php';
dibi::connect(array( dibi::connect([
'driver' => 'sqlite3', 'driver' => 'sqlite3',
'database' => 'data/sample.s3db', 'database' => 'data/sample.s3db',
)); ]);
$res = dibi::query(' $res = dibi::query('
@@ -27,6 +27,6 @@ dibi::dump();
// dump result table // dump result table
echo '<h2>DibiResult::dump()</h2>'; echo '<h2>Dibi\Result::dump()</h2>';
$res->dump(); $res->dump();

View File

@@ -11,10 +11,10 @@ if (@!include __DIR__ . '/../vendor/autoload.php') {
Tracy\Debugger::enable(); Tracy\Debugger::enable();
dibi::connect(array( dibi::connect([
'driver' => 'sqlite3', 'driver' => 'sqlite3',
'database' => 'data/sample.s3db', 'database' => 'data/sample.s3db',
)); ]);
/* /*

View File

@@ -4,13 +4,13 @@
<?php <?php
require __DIR__ . '/../dibi/dibi.php'; require __DIR__ . '/../src/loader.php';
dibi::connect(array( dibi::connect([
'driver' => 'sqlite3', 'driver' => 'sqlite3',
'database' => 'data/sample.s3db', 'database' => 'data/sample.s3db',
)); ]);
$count = dibi::loadFile('compress.zlib://data/sample.dump.sql.gz'); $count = dibi::loadFile('compress.zlib://data/sample.dump.sql.gz');

View File

@@ -4,13 +4,13 @@
<?php <?php
require __DIR__ . '/../dibi/dibi.php'; require __DIR__ . '/../src/loader.php';
dibi::connect(array( dibi::connect([
'driver' => 'sqlite3', 'driver' => 'sqlite3',
'database' => 'data/sample.s3db', 'database' => 'data/sample.s3db',
)); ]);
// some variables // some variables
@@ -55,7 +55,7 @@ dibi::test('
// IF() // IF()
dibi::test('UPDATE products SET', array( dibi::test('UPDATE products SET', [
'price' => array('IF(price_fixed, price, ?)', 123), 'price' => ['IF(price_fixed, price, ?)', 123],
)); ]);
// -> SELECT * FROM customers WHERE LIMIT 10 // -> SELECT * FROM customers WHERE LIMIT 10

View File

@@ -4,15 +4,15 @@
<?php <?php
require __DIR__ . '/../dibi/dibi.php'; require __DIR__ . '/../src/loader.php';
date_default_timezone_set('Europe/Prague'); date_default_timezone_set('Europe/Prague');
dibi::connect(array( dibi::connect([
'driver' => 'sqlite3', 'driver' => 'sqlite3',
'database' => 'data/sample.s3db', 'database' => 'data/sample.s3db',
)); ]);
// SELECT // SELECT
@@ -23,44 +23,44 @@ dibi::test('
SELECT COUNT(*) as [count] SELECT COUNT(*) as [count]
FROM [comments] FROM [comments]
WHERE [ip] LIKE ?', $ipMask, ' WHERE [ip] LIKE ?', $ipMask, '
AND [date] > ', new DibiDateTime($timestamp) AND [date] > ', new Dibi\DateTime($timestamp)
); );
// -> SELECT COUNT(*) as [count] FROM [comments] WHERE [ip] LIKE '192.168.%' AND [date] > 876693600 // -> SELECT COUNT(*) as [count] FROM [comments] WHERE [ip] LIKE '192.168.%' AND [date] > 876693600
// dibi detects INSERT or REPLACE command // dibi detects INSERT or REPLACE command
dibi::test(' dibi::test('
REPLACE INTO products', array( REPLACE INTO products', [
'title' => 'Super product', 'title' => 'Super product',
'price' => 318, 'price' => 318,
'active' => TRUE, 'active' => TRUE,
)); ]);
// -> REPLACE INTO products ([title], [price], [active]) VALUES ('Super product', 318, 1) // -> REPLACE INTO products ([title], [price], [active]) VALUES ('Super product', 318, 1)
// multiple INSERT command // multiple INSERT command
$array = array( $array = [
'title' => 'Super Product', 'title' => 'Super Product',
'price' => 12, 'price' => 12,
'brand' => NULL, 'brand' => NULL,
'created' => new DateTime, 'created' => new DateTime,
); ];
dibi::test('INSERT INTO products', $array, $array, $array); dibi::test('INSERT INTO products', $array, $array, $array);
// -> INSERT INTO products ([title], [price], [brand], [created]) VALUES ('Super Product', ...) , (...) , (...) // -> INSERT INTO products ([title], [price], [brand], [created]) VALUES ('Super Product', ...) , (...) , (...)
// dibi detects UPDATE command // dibi detects UPDATE command
dibi::test(' dibi::test('
UPDATE colors SET', array( UPDATE colors SET', [
'color' => 'blue', 'color' => 'blue',
'order' => 12, 'order' => 12,
), ' ], '
WHERE id=?', 123); WHERE id=?', 123);
// -> UPDATE colors SET [color]='blue', [order]=12 WHERE id=123 // -> UPDATE colors SET [color]='blue', [order]=12 WHERE id=123
// modifier applied to array // modifier applied to array
$array = array(1, 2, 3); $array = [1, 2, 3];
dibi::test(' dibi::test('
SELECT * SELECT *
FROM people FROM people
@@ -70,10 +70,10 @@ dibi::test('
// modifier %by for ORDER BY // modifier %by for ORDER BY
$order = array( $order = [
'field1' => 'asc', 'field1' => 'asc',
'field2' => 'desc', 'field2' => 'desc',
); ];
dibi::test(' dibi::test('
SELECT * SELECT *
FROM people FROM people

View File

@@ -4,6 +4,8 @@
<?php <?php
use Dibi\Type;
if (@!include __DIR__ . '/../vendor/autoload.php') { if (@!include __DIR__ . '/../vendor/autoload.php') {
die('Install dependencies using `composer install --dev`'); die('Install dependencies using `composer install --dev`');
} }
@@ -13,23 +15,23 @@ Tracy\Debugger::enable();
date_default_timezone_set('Europe/Prague'); date_default_timezone_set('Europe/Prague');
dibi::connect(array( dibi::connect([
'driver' => 'sqlite3', 'driver' => 'sqlite3',
'database' => 'data/sample.s3db', 'database' => 'data/sample.s3db',
)); ]);
// using manual hints // using manual hints
$res = dibi::query('SELECT * FROM [customers]'); $res = dibi::query('SELECT * FROM [customers]');
$res->setType('customer_id', Dibi::INTEGER) $res->setType('customer_id', Type::INTEGER)
->setType('added', Dibi::DATETIME) ->setType('added', Type::DATETIME)
->setFormat(dibi::DATETIME, 'Y-m-d H:i:s'); ->setFormat(Type::DATETIME, 'Y-m-d H:i:s');
Tracy\Dumper::dump($res->fetch()); Tracy\Dumper::dump($res->fetch());
// outputs: // outputs:
// DibiRow(3) { // Dibi\Row(3) {
// customer_id => 1 // customer_id => 1
// name => "Dave Lister" (11) // name => "Dave Lister" (11)
// added => "2007-03-11 17:20:03" (19) // added => "2007-03-11 17:20:03" (19)
@@ -40,7 +42,7 @@ $res = dibi::query('SELECT * FROM [customers]');
Tracy\Dumper::dump($res->fetch()); Tracy\Dumper::dump($res->fetch());
// outputs: // outputs:
// DibiRow(3) { // Dibi\Row(3) {
// customer_id => 1 // customer_id => 1
// name => "Dave Lister" (11) // name => "Dave Lister" (11)
// added => "2007-03-11 17:20:03" (19) // added => "2007-03-11 17:20:03" (19)

View File

@@ -15,13 +15,13 @@ if (@!include __DIR__ . '/../vendor/autoload.php') {
Tracy\Debugger::enable(); Tracy\Debugger::enable();
$connection = dibi::connect(array( $connection = dibi::connect([
'driver' => 'sqlite3', 'driver' => 'sqlite3',
'database' => 'data/sample.s3db', 'database' => 'data/sample.s3db',
'profiler' => array( 'profiler' => [
'run' => TRUE, 'run' => TRUE,
), ],
)); ]);
// add panel to debug bar // add panel to debug bar

View File

@@ -17,13 +17,13 @@ if (@!include __DIR__ . '/../vendor/autoload.php') {
Tracy\Debugger::enable(); Tracy\Debugger::enable();
$connection = dibi::connect(array( $connection = dibi::connect([
'driver' => 'sqlite3', 'driver' => 'sqlite3',
'database' => 'data/sample.s3db', 'database' => 'data/sample.s3db',
'profiler' => array( 'profiler' => [
'run' => TRUE, 'run' => TRUE,
), ],
)); ]);
// add panel to debug bar // add panel to debug bar

View File

@@ -4,26 +4,26 @@
<?php <?php
require __DIR__ . '/../dibi/dibi.php'; require __DIR__ . '/../src/loader.php';
date_default_timezone_set('Europe/Prague'); date_default_timezone_set('Europe/Prague');
// CHANGE TO REAL PARAMETERS! // CHANGE TO REAL PARAMETERS!
dibi::connect(array( dibi::connect([
'driver' => 'sqlite3', 'driver' => 'sqlite3',
'database' => 'data/sample.s3db', 'database' => 'data/sample.s3db',
'formatDate' => "'Y-m-d'", 'formatDate' => "'Y-m-d'",
'formatDateTime' => "'Y-m-d H-i-s'", 'formatDateTime' => "'Y-m-d H-i-s'",
)); ]);
// generate and dump SQL // generate and dump SQL
dibi::test(' dibi::test('
INSERT INTO [mytable]', array( INSERT INTO [mytable]', [
'id' => 123, 'id' => 123,
'date' => new DateTime('12.3.2007'), 'date' => new DateTime('12.3.2007'),
'stamp' => new DateTime('23.1.2007 10:23'), 'stamp' => new DateTime('23.1.2007 10:23'),
) ]
); );
// -> INSERT INTO [mytable] ([id], [date], [stamp]) VALUES (123, '2007-03-12', '2007-01-23 10-23-00') // -> INSERT INTO [mytable] ([id], [date], [stamp]) VALUES (123, '2007-03-12', '2007-01-23 10-23-00')

View File

@@ -11,14 +11,14 @@ if (@!include __DIR__ . '/../vendor/autoload.php') {
Tracy\Debugger::enable(); Tracy\Debugger::enable();
dibi::connect(array( dibi::connect([
'driver' => 'sqlite3', 'driver' => 'sqlite3',
'database' => 'data/sample.s3db', 'database' => 'data/sample.s3db',
)); ]);
// using the "prototype" to add custom method to class DibiResult // using the "prototype" to add custom method to class Dibi\Result
DibiResult::extensionMethod('fetchShuffle', function (DibiResult $obj) { Dibi\Result::extensionMethod('fetchShuffle', function (Dibi\Result $obj) {
$all = $obj->fetchAll(); $all = $obj->fetchAll();
shuffle($all); shuffle($all);
return $all; return $all;

View File

@@ -4,23 +4,23 @@
<?php <?php
require __DIR__ . '/../dibi/dibi.php'; require __DIR__ . '/../src/loader.php';
date_default_timezone_set('Europe/Prague'); date_default_timezone_set('Europe/Prague');
dibi::connect(array( dibi::connect([
'driver' => 'sqlite3', 'driver' => 'sqlite3',
'database' => 'data/sample.s3db', 'database' => 'data/sample.s3db',
)); ]);
$id = 10; $id = 10;
$record = array( $record = [
'title' => 'Super product', 'title' => 'Super product',
'price' => 318, 'price' => 318,
'active' => TRUE, 'active' => TRUE,
); ];
// SELECT ... // SELECT ...
dibi::select('product_id')->as('id') dibi::select('product_id')->as('id')

View File

@@ -4,13 +4,13 @@
<?php <?php
require __DIR__ . '/../dibi/dibi.php'; require __DIR__ . '/../src/loader.php';
dibi::connect(array( dibi::connect([
'driver' => 'sqlite3', 'driver' => 'sqlite3',
'database' => 'data/sample.s3db', 'database' => 'data/sample.s3db',
)); ]);
// no limit // no limit

View File

@@ -4,20 +4,20 @@
<?php <?php
require __DIR__ . '/../dibi/dibi.php'; require __DIR__ . '/../src/loader.php';
date_default_timezone_set('Europe/Prague'); date_default_timezone_set('Europe/Prague');
dibi::connect(array( dibi::connect([
'driver' => 'sqlite3', 'driver' => 'sqlite3',
'database' => 'data/sample.s3db', 'database' => 'data/sample.s3db',
// enable query logging to this file // enable query logging to this file
'profiler' => array( 'profiler' => [
'run' => TRUE, 'run' => TRUE,
'file' => 'data/log.sql', 'file' => 'data/log.sql',
), ],
)); ]);
try { try {
@@ -26,7 +26,7 @@ try {
$res = dibi::query('SELECT * FROM [customers] WHERE [customer_id] < ?', 5); $res = dibi::query('SELECT * FROM [customers] WHERE [customer_id] < ?', 5);
$res = dibi::query('SELECT FROM [customers] WHERE [customer_id] < ?', 38); $res = dibi::query('SELECT FROM [customers] WHERE [customer_id] < ?', 38);
} catch (DibiException $e) { } catch (Dibi\Exception $e) {
echo '<p>', get_class($e), ': ', $e->getMessage(), '</p>'; echo '<p>', get_class($e), ': ', $e->getMessage(), '</p>';
} }

View File

@@ -6,16 +6,16 @@
<?php <?php
require __DIR__ . '/../dibi/dibi.php'; require __DIR__ . '/../src/loader.php';
dibi::connect(array( dibi::connect([
'driver' => 'sqlite3', 'driver' => 'sqlite3',
'database' => 'data/sample.s3db', 'database' => 'data/sample.s3db',
'profiler' => array( 'profiler' => [
'run' => TRUE, 'run' => TRUE,
), ],
)); ]);
// execute some queries... // execute some queries...

View File

@@ -4,13 +4,13 @@
<?php <?php
require __DIR__ . '/../dibi/dibi.php'; require __DIR__ . '/../src/loader.php';
dibi::connect(array( dibi::connect([
'driver' => 'sqlite3', 'driver' => 'sqlite3',
'database' => 'data/sample.s3db', 'database' => 'data/sample.s3db',
)); ]);
// create new substitution :blog: ==> wp_ // create new substitution :blog: ==> wp_

View File

@@ -4,13 +4,13 @@
<?php <?php
require __DIR__ . '/../dibi/dibi.php'; require __DIR__ . '/../src/loader.php';
dibi::connect(array( dibi::connect([
'driver' => 'sqlite3', 'driver' => 'sqlite3',
'database' => 'data/sample.s3db', 'database' => 'data/sample.s3db',
)); ]);
echo "<h2>Before</h2>\n"; echo "<h2>Before</h2>\n";
@@ -19,9 +19,9 @@ dibi::query('SELECT * FROM [products]')->dump();
dibi::begin(); dibi::begin();
dibi::query('INSERT INTO [products]', array( dibi::query('INSERT INTO [products]', [
'title' => 'Test product', 'title' => 'Test product',
)); ]);
echo "<h2>After INSERT</h2>\n"; echo "<h2>After INSERT</h2>\n";
dibi::query('SELECT * FROM [products]')->dump(); dibi::query('SELECT * FROM [products]')->dump();

View File

@@ -1,8 +1,9 @@
[Dibi](http://dibiphp.com) - smart database layer for PHP [![Buy me a coffee](https://files.nette.org/images/coffee1s.png)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=9XXL5ZJHAYQUN) [Dibi](https://dibiphp.com) - smart database layer for PHP [![Buy me a coffee](https://files.nette.org/images/coffee1s.png)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=9XXL5ZJHAYQUN)
========================================================= =========================================================
[![Downloads this Month](https://img.shields.io/packagist/dm/dibi/dibi.svg)](https://packagist.org/packages/dibi/dibi) [![Downloads this Month](https://img.shields.io/packagist/dm/dibi/dibi.svg)](https://packagist.org/packages/dibi/dibi)
[![Build Status](https://travis-ci.org/dg/dibi.svg?branch=master)](https://travis-ci.org/dg/dibi) [![Build Status](https://travis-ci.org/dg/dibi.svg?branch=master)](https://travis-ci.org/dg/dibi)
[![Build Status Windows](https://ci.appveyor.com/api/projects/status/github/dg/dibi?branch=master&svg=true)](https://ci.appveyor.com/project/dg/dibi/branch/master)
[![Latest Stable Version](https://poser.pugx.org/dibi/dibi/v/stable)](https://github.com/dg/dibi/releases) [![Latest Stable Version](https://poser.pugx.org/dibi/dibi/v/stable)](https://github.com/dg/dibi/releases)
[![License](https://img.shields.io/badge/license-New%20BSD-blue.svg)](https://github.com/dg/dibi/blob/master/license.md) [![License](https://img.shields.io/badge/license-New%20BSD-blue.svg)](https://github.com/dg/dibi/blob/master/license.md)
@@ -13,29 +14,29 @@ The best way to install Dibi is to use a [Composer](https://getcomposer.org/down
php composer.phar require dibi/dibi php composer.phar require dibi/dibi
Or you can download the latest package from http://dibiphp.com. In this Or you can download the latest package from https://dibiphp.com. In this
package is also `Dibi.minified`, shrinked single-file version of whole Dibi, 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. 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. Dibi requires PHP 5.4.4 or later. It has been tested with PHP 7 too.
Examples Examples
-------- --------
Refer to the `examples` directory for examples. Dibi documentation is Refer to the `examples` directory for examples. Dibi documentation is
available on the [homepage](http://dibiphp.com). available on the [homepage](https://dibiphp.com).
Connect to database: Connect to database:
```php ```php
// connect to database (static way) // connect to database (static way)
dibi::connect(array( dibi::connect([
'driver' => 'mysql', 'driver' => 'mysql',
'host' => 'localhost', 'host' => 'localhost',
'username' => 'root', 'username' => 'root',
'password' => '***', 'password' => '***',
)); ]);
// or object way; in all other examples use $connection-> instead of dibi:: // or object way; in all other examples use $connection-> instead of dibi::
$connection = new DibiConnection($options); $connection = new DibiConnection($options);
@@ -46,19 +47,19 @@ SELECT, INSERT, UPDATE
```php ```php
dibi::query('SELECT * FROM users WHERE id = ?', $id); dibi::query('SELECT * FROM users WHERE id = ?', $id);
$arr = array( $arr = [
'name' => 'John', 'name' => 'John',
'is_admin' => TRUE, 'is_admin' => TRUE,
); ];
dibi::query('INSERT INTO users', $arr); dibi::query('INSERT INTO users', $arr);
// INSERT INTO users (`name`, `is_admin`) VALUES ('John', 1) // INSERT INTO users (`name`, `is_admin`) VALUES ('John', 1)
dibi::query('UPDATE users SET', $arr, 'WHERE `id`=?', $x); dibi::query('UPDATE users SET', $arr, 'WHERE `id`=?', $x);
// UPDATE users SET `name`='John', `is_admin`=1 WHERE `id` = 123 // UPDATE users SET `name`='John', `is_admin`=1 WHERE `id` = 123
dibi::query('UPDATE users SET', array( dibi::query('UPDATE users SET', [
'title' => array('SHA1(?)', 'tajneheslo'), 'title' => array('SHA1(?)', 'tajneheslo'),
)); ]);
// UPDATE users SET 'title' = SHA1('tajneheslo') // UPDATE users SET 'title' = SHA1('tajneheslo')
``` ```
@@ -81,10 +82,10 @@ foreach ($result as $n => $row) {
Modifiers for arrays: Modifiers for arrays:
```php ```php
dibi::query('SELECT * FROM users WHERE %and', array( dibi::query('SELECT * FROM users WHERE %and', [
array('number > ?', 10), array('number > ?', 10),
array('number < ?', 100), array('number < ?', 100),
)); ]);
// SELECT * FROM users WHERE (number > 10) AND (number < 100) // SELECT * FROM users WHERE (number > 10) AND (number < 100)
``` ```
@@ -116,9 +117,9 @@ dibi::query("SELECT * FROM table WHERE name LIKE %like~", $query);
DateTime: DateTime:
```php ```php
dibi::query('UPDATE users SET', array( dibi::query('UPDATE users SET', [
'time' => new DateTime, 'time' => new DateTime,
)); ]);
// UPDATE users SET ('2008-01-01 01:08:10') // UPDATE users SET ('2008-01-01 01:08:10')
``` ```

View File

@@ -5,14 +5,15 @@
* Copyright (c) 2005 David Grudl (https://davidgrudl.com) * Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/ */
namespace Dibi\Bridges\Nette;
use Nette;
/** /**
* Dibi extension for Nette Framework 2.1. Creates 'connection' service. * Dibi extension for Nette Framework 2.1. Creates 'connection' service.
*
* @package dibi\nette
* @phpversion 5.3
*/ */
class DibiNette21Extension extends Nette\DI\CompilerExtension class DibiExtension21 extends Nette\DI\CompilerExtension
{ {
public function loadConfiguration() public function loadConfiguration()
@@ -35,16 +36,16 @@ class DibiNette21Extension extends Nette\DI\CompilerExtension
} }
$connection = $container->addDefinition($this->prefix('connection')) $connection = $container->addDefinition($this->prefix('connection'))
->setClass('DibiConnection', array($config)) ->setClass('Dibi\Connection', [$config])
->setAutowired(isset($config['autowired']) ? $config['autowired'] : TRUE); ->setAutowired(isset($config['autowired']) ? $config['autowired'] : TRUE);
if ($useProfiler) { if ($useProfiler) {
$panel = $container->addDefinition($this->prefix('panel')) $panel = $container->addDefinition($this->prefix('panel'))
->setClass('DibiNettePanel') ->setClass('Dibi\Bridges\Nette\Panel')
->addSetup('Nette\Diagnostics\Debugger::getBar()->addPanel(?)', array('@self')) ->addSetup('Nette\Diagnostics\Debugger::getBar()->addPanel(?)', ['@self'])
->addSetup('Nette\Diagnostics\Debugger::getBlueScreen()->addPanel(?)', array('DibiNettePanel::renderException')); ->addSetup('Nette\Diagnostics\Debugger::getBlueScreen()->addPanel(?)', ['Dibi\Bridges\Nette\Panel::renderException']);
$connection->addSetup('$service->onEvent[] = ?', array(array($panel, 'logEvent'))); $connection->addSetup('$service->onEvent[] = ?', [[$panel, 'logEvent']]);
} }
} }

View File

@@ -7,14 +7,11 @@
namespace Dibi\Bridges\Nette; namespace Dibi\Bridges\Nette;
use dibi;
use Nette; use Nette;
/** /**
* Dibi extension for Nette Framework 2.2. Creates 'connection' & 'panel' services. * Dibi extension for Nette Framework 2.2. Creates 'connection' & 'panel' services.
*
* @package dibi\nette
*/ */
class DibiExtension22 extends Nette\DI\CompilerExtension class DibiExtension22 extends Nette\DI\CompilerExtension
{ {
@@ -39,13 +36,19 @@ class DibiExtension22 extends Nette\DI\CompilerExtension
} }
$connection = $container->addDefinition($this->prefix('connection')) $connection = $container->addDefinition($this->prefix('connection'))
->setClass('DibiConnection', array($config)) ->setClass('Dibi\Connection', [$config])
->setAutowired(isset($config['autowired']) ? $config['autowired'] : TRUE); ->setAutowired(isset($config['autowired']) ? $config['autowired'] : TRUE);
if (class_exists('Tracy\Debugger')) {
$connection->addSetup(
[new Nette\DI\Statement('Tracy\Debugger::getBlueScreen'), 'addPanel'],
[['Dibi\Bridges\Tracy\Panel', 'renderException']]
);
}
if ($useProfiler) { if ($useProfiler) {
$panel = $container->addDefinition($this->prefix('panel')) $panel = $container->addDefinition($this->prefix('panel'))
->setClass('Dibi\Bridges\Tracy\Panel'); ->setClass('Dibi\Bridges\Tracy\Panel');
$connection->addSetup(array($panel, 'register'), array($connection)); $connection->addSetup([$panel, 'register'], [$connection]);
} }
} }

View File

@@ -5,18 +5,24 @@
* Copyright (c) 2005 David Grudl (https://davidgrudl.com) * Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/ */
namespace Dibi\Bridges\Nette;
use Dibi;
use Dibi\Event;
use Dibi\Helpers;
use Nette;
use Nette\Diagnostics\Debugger; use Nette\Diagnostics\Debugger;
/** /**
* Dibi panel for Nette\Diagnostics. * Dibi panel for Nette\Diagnostics.
*
* @package dibi\nette
*/ */
class DibiNettePanel extends DibiObject implements Nette\Diagnostics\IBarPanel class Panel implements Nette\Diagnostics\IBarPanel
{ {
use Dibi\Strict;
/** @var int maximum SQL length */ /** @var int maximum SQL length */
static public $maxLength = 1000; public static $maxLength = 1000;
/** @var bool explain queries? */ /** @var bool explain queries? */
public $explain; public $explain;
@@ -25,21 +31,21 @@ class DibiNettePanel extends DibiObject implements Nette\Diagnostics\IBarPanel
public $filter; public $filter;
/** @var array */ /** @var array */
private $events = array(); private $events = [];
public function __construct($explain = TRUE, $filter = NULL) public function __construct($explain = TRUE, $filter = NULL)
{ {
$this->filter = $filter ? (int) $filter : DibiEvent::QUERY; $this->filter = $filter ? (int) $filter : Event::QUERY;
$this->explain = $explain; $this->explain = $explain;
} }
public function register(DibiConnection $connection) public function register(Dibi\Connection $connection)
{ {
Debugger::getBar()->addPanel($this); Debugger::getBar()->addPanel($this);
Debugger::getBlueScreen()->addPanel(array(__CLASS__, 'renderException')); Debugger::getBlueScreen()->addPanel([__CLASS__, 'renderException']);
$connection->onEvent[] = array($this, 'logEvent'); $connection->onEvent[] = [$this, 'logEvent'];
} }
@@ -47,7 +53,7 @@ class DibiNettePanel extends DibiObject implements Nette\Diagnostics\IBarPanel
* After event notification. * After event notification.
* @return void * @return void
*/ */
public function logEvent(DibiEvent $event) public function logEvent(Event $event)
{ {
if (($event->type & $this->filter) === 0) { if (($event->type & $this->filter) === 0) {
return; return;
@@ -62,11 +68,11 @@ class DibiNettePanel extends DibiObject implements Nette\Diagnostics\IBarPanel
*/ */
public static function renderException($e) public static function renderException($e)
{ {
if ($e instanceof DibiException && $e->getSql()) { if ($e instanceof Dibi\Exception && $e->getSql()) {
return array( return [
'tab' => 'SQL', 'tab' => 'SQL',
'panel' => dibi::dump($e->getSql(), TRUE), 'panel' => Helpers::dump($e->getSql(), TRUE),
); ];
} }
} }
@@ -99,15 +105,15 @@ class DibiNettePanel extends DibiObject implements Nette\Diagnostics\IBarPanel
foreach ($this->events as $event) { foreach ($this->events as $event) {
$totalTime += $event->time; $totalTime += $event->time;
$explain = NULL; // EXPLAIN is called here to work SELECT FOUND_ROWS() $explain = NULL; // EXPLAIN is called here to work SELECT FOUND_ROWS()
if ($this->explain && $event->type === DibiEvent::SELECT) { if ($this->explain && $event->type === Event::SELECT) {
try { try {
$backup = array($event->connection->onEvent, dibi::$numOfQueries, dibi::$totalTime); $backup = [$event->connection->onEvent, \dibi::$numOfQueries, \dibi::$totalTime];
$event->connection->onEvent = NULL; $event->connection->onEvent = NULL;
$cmd = is_string($this->explain) ? $this->explain : ($event->connection->getConfig('driver') === 'oracle' ? 'EXPLAIN PLAN FOR' : 'EXPLAIN'); $cmd = is_string($this->explain) ? $this->explain : ($event->connection->getConfig('driver') === 'oracle' ? 'EXPLAIN PLAN FOR' : 'EXPLAIN');
$explain = dibi::dump($event->connection->nativeQuery("$cmd $event->sql"), TRUE); $explain = Helpers::dump($event->connection->nativeQuery("$cmd $event->sql"), TRUE);
} catch (DibiException $e) { } catch (Dibi\Exception $e) {
} }
list($event->connection->onEvent, dibi::$numOfQueries, dibi::$totalTime) = $backup; list($event->connection->onEvent, \dibi::$numOfQueries, \dibi::$totalTime) = $backup;
} }
$s .= '<tr><td>' . sprintf('%0.3f', $event->time * 1000); $s .= '<tr><td>' . sprintf('%0.3f', $event->time * 1000);
@@ -117,7 +123,7 @@ class DibiNettePanel extends DibiObject implements Nette\Diagnostics\IBarPanel
$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='#nette-debug-DibiProfiler-row-$counter' class='nette-toggler nette-toggle-collapsed' 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); $s .= '</td><td class="nette-DibiProfiler-sql">' . Helpers::dump(strlen($event->sql) > self::$maxLength ? substr($event->sql, 0, self::$maxLength) . '...' : $event->sql, TRUE);
if ($explain) { if ($explain) {
$s .= "<div id='nette-debug-DibiProfiler-row-$counter' class='nette-collapsed'>{$explain}</div>"; $s .= "<div id='nette-debug-DibiProfiler-row-$counter' class='nette-collapsed'>{$explain}</div>";
} }
@@ -126,7 +132,7 @@ class DibiNettePanel extends DibiObject implements Nette\Diagnostics\IBarPanel
if (!class_exists($helpers)) { if (!class_exists($helpers)) {
$helpers = class_exists('NDebugHelpers') ? 'NDebugHelpers' : 'DebugHelpers'; $helpers = class_exists('NDebugHelpers') ? 'NDebugHelpers' : 'DebugHelpers';
} }
$s .= call_user_func(array($helpers, 'editorLink'), $event->source[0], $event->source[1])->class('nette-DibiProfiler-source'); $s .= call_user_func([$helpers, 'editorLink'], $event->source[0], $event->source[1])->class('nette-DibiProfiler-source');
} }
$s .= "</td><td>{$event->count}</td><td>{$h($event->connection->getConfig('driver') . '/' . $event->connection->getConfig('name'))}</td></tr>"; $s .= "</td><td>{$event->count}</td><td>{$h($event->connection->getConfig('driver') . '/' . $event->connection->getConfig('name'))}</td></tr>";

View File

@@ -7,18 +7,21 @@
namespace Dibi\Bridges\Tracy; namespace Dibi\Bridges\Tracy;
use dibi; use Dibi;
use Dibi\Event;
use Dibi\Helpers;
use Tracy; use Tracy;
/** /**
* Dibi panel for Tracy. * Dibi panel for Tracy.
* @package dibi\nette
*/ */
class Panel extends \DibiObject implements Tracy\IBarPanel class Panel implements Tracy\IBarPanel
{ {
use Dibi\Strict;
/** @var int maximum SQL length */ /** @var int maximum SQL length */
static public $maxLength = 1000; public static $maxLength = 1000;
/** @var bool explain queries? */ /** @var bool explain queries? */
public $explain; public $explain;
@@ -27,21 +30,21 @@ class Panel extends \DibiObject implements Tracy\IBarPanel
public $filter; public $filter;
/** @var array */ /** @var array */
private $events = array(); private $events = [];
public function __construct($explain = TRUE, $filter = NULL) public function __construct($explain = TRUE, $filter = NULL)
{ {
$this->filter = $filter ? (int) $filter : \DibiEvent::QUERY; $this->filter = $filter ? (int) $filter : Event::QUERY;
$this->explain = $explain; $this->explain = $explain;
} }
public function register(\DibiConnection $connection) public function register(Dibi\Connection $connection)
{ {
Tracy\Debugger::getBar()->addPanel($this); Tracy\Debugger::getBar()->addPanel($this);
Tracy\Debugger::getBlueScreen()->addPanel(array(__CLASS__, 'renderException')); Tracy\Debugger::getBlueScreen()->addPanel([__CLASS__, 'renderException']);
$connection->onEvent[] = array($this, 'logEvent'); $connection->onEvent[] = [$this, 'logEvent'];
} }
@@ -49,7 +52,7 @@ class Panel extends \DibiObject implements Tracy\IBarPanel
* After event notification. * After event notification.
* @return void * @return void
*/ */
public function logEvent(\DibiEvent $event) public function logEvent(Event $event)
{ {
if (($event->type & $this->filter) === 0) { if (($event->type & $this->filter) === 0) {
return; return;
@@ -64,11 +67,11 @@ class Panel extends \DibiObject implements Tracy\IBarPanel
*/ */
public static function renderException($e) public static function renderException($e)
{ {
if ($e instanceof \DibiException && $e->getSql()) { if ($e instanceof Dibi\Exception && $e->getSql()) {
return array( return [
'tab' => 'SQL', 'tab' => 'SQL',
'panel' => dibi::dump($e->getSql(), TRUE), 'panel' => Helpers::dump($e->getSql(), TRUE),
); ];
} }
} }
@@ -102,15 +105,15 @@ class Panel extends \DibiObject implements Tracy\IBarPanel
foreach ($this->events as $event) { foreach ($this->events as $event) {
$totalTime += $event->time; $totalTime += $event->time;
$explain = NULL; // EXPLAIN is called here to work SELECT FOUND_ROWS() $explain = NULL; // EXPLAIN is called here to work SELECT FOUND_ROWS()
if ($this->explain && $event->type === \DibiEvent::SELECT) { if ($this->explain && $event->type === Event::SELECT) {
try { try {
$backup = array($event->connection->onEvent, dibi::$numOfQueries, dibi::$totalTime); $backup = [$event->connection->onEvent, \dibi::$numOfQueries, \dibi::$totalTime];
$event->connection->onEvent = NULL; $event->connection->onEvent = NULL;
$cmd = is_string($this->explain) ? $this->explain : ($event->connection->getConfig('driver') === 'oracle' ? 'EXPLAIN PLAN FOR' : 'EXPLAIN'); $cmd = is_string($this->explain) ? $this->explain : ($event->connection->getConfig('driver') === 'oracle' ? 'EXPLAIN PLAN FOR' : 'EXPLAIN');
$explain = dibi::dump($event->connection->nativeQuery("$cmd $event->sql"), TRUE); $explain = Helpers::dump($event->connection->nativeQuery("$cmd $event->sql"), TRUE);
} catch (\DibiException $e) { } catch (Dibi\Exception $e) {
} }
list($event->connection->onEvent, dibi::$numOfQueries, dibi::$totalTime) = $backup; list($event->connection->onEvent, \dibi::$numOfQueries, \dibi::$totalTime) = $backup;
} }
$s .= '<tr><td>' . sprintf('%0.3f', $event->time * 1000); $s .= '<tr><td>' . sprintf('%0.3f', $event->time * 1000);
@@ -120,7 +123,7 @@ class Panel extends \DibiObject implements Tracy\IBarPanel
$s .= "<br /><a href='#tracy-debug-DibiProfiler-row-$counter' class='tracy-toggle tracy-collapsed' rel='#tracy-debug-DibiProfiler-row-$counter'>explain</a>"; $s .= "<br /><a href='#tracy-debug-DibiProfiler-row-$counter' class='tracy-toggle tracy-collapsed' rel='#tracy-debug-DibiProfiler-row-$counter'>explain</a>";
} }
$s .= '</td><td class="tracy-DibiProfiler-sql">' . dibi::dump(strlen($event->sql) > self::$maxLength ? substr($event->sql, 0, self::$maxLength) . '...' : $event->sql, TRUE); $s .= '</td><td class="tracy-DibiProfiler-sql">' . Helpers::dump(strlen($event->sql) > self::$maxLength ? substr($event->sql, 0, self::$maxLength) . '...' : $event->sql, TRUE);
if ($explain) { if ($explain) {
$s .= "<div id='tracy-debug-DibiProfiler-row-$counter' class='tracy-collapsed'>{$explain}</div>"; $s .= "<div id='tracy-debug-DibiProfiler-row-$counter' class='tracy-collapsed'>{$explain}</div>";
} }

View File

@@ -5,33 +5,37 @@
* Copyright (c) 2005 David Grudl (https://davidgrudl.com) * Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/ */
namespace Dibi;
use Traversable;
/** /**
* dibi connection. * dibi connection.
* *
* @package dibi
*
* @property-read int $affectedRows * @property-read int $affectedRows
* @property-read int $insertId * @property-read int $insertId
*/ */
class DibiConnection extends DibiObject class Connection
{ {
/** @var array of function (DibiEvent $event); Occurs after query is executed */ use Strict;
/** @var array of function (Event $event); Occurs after query is executed */
public $onEvent; public $onEvent;
/** @var array Current connection configuration */ /** @var array Current connection configuration */
private $config; private $config;
/** @var IDibiDriver */ /** @var Driver */
private $driver; private $driver;
/** @var DibiTranslator */ /** @var Translator */
private $translator; private $translator;
/** @var bool Is connected? */ /** @var bool Is connected? */
private $connected = FALSE; private $connected = FALSE;
/** @var DibiHashMap Substitutes for identifiers */ /** @var HashMap Substitutes for identifiers */
private $substitutes; private $substitutes;
@@ -47,7 +51,7 @@ class DibiConnection extends DibiObject
* @param mixed connection parameters * @param mixed connection parameters
* @param string connection name * @param string connection name
* @throws DibiException * @throws Exception
*/ */
public function __construct($config, $name = NULL) public function __construct($config, $name = NULL)
{ {
@@ -55,64 +59,66 @@ class DibiConnection extends DibiObject
parse_str($config, $config); parse_str($config, $config);
} elseif ($config instanceof Traversable) { } elseif ($config instanceof Traversable) {
$tmp = array(); $tmp = [];
foreach ($config as $key => $val) { foreach ($config as $key => $val) {
$tmp[$key] = $val instanceof Traversable ? iterator_to_array($val) : $val; $tmp[$key] = $val instanceof Traversable ? iterator_to_array($val) : $val;
} }
$config = $tmp; $config = $tmp;
} elseif (!is_array($config)) { } elseif (!is_array($config)) {
throw new InvalidArgumentException('Configuration must be array, string or object.'); throw new \InvalidArgumentException('Configuration must be array, string or object.');
} }
self::alias($config, 'username', 'user'); Helpers::alias($config, 'username', 'user');
self::alias($config, 'password', 'pass'); Helpers::alias($config, 'password', 'pass');
self::alias($config, 'host', 'hostname'); Helpers::alias($config, 'host', 'hostname');
self::alias($config, 'result|formatDate', 'resultDate'); Helpers::alias($config, 'result|formatDate', 'resultDate');
self::alias($config, 'result|formatDateTime', 'resultDateTime'); Helpers::alias($config, 'result|formatDateTime', 'resultDateTime');
if (!isset($config['driver'])) { if (!isset($config['driver'])) {
$config['driver'] = dibi::$defaultDriver; $config['driver'] = \dibi::$defaultDriver;
} }
$class = preg_replace(array('#\W#', '#sql#'), array('_', 'Sql'), ucfirst(strtolower($config['driver']))); if ($config['driver'] instanceof Driver) {
$class = "Dibi{$class}Driver"; $this->driver = $config['driver'];
if (!class_exists($class)) { $config['driver'] = get_class($this->driver);
include_once dirname(__FILE__) . "/../drivers/$class.php"; } elseif (is_subclass_of($config['driver'], 'Dibi\Driver')) {
$this->driver = new $config['driver'];
if (!class_exists($class, FALSE)) { } else {
throw new DibiException("Unable to create instance of dibi driver '$class'."); $class = preg_replace(['#\W#', '#sql#'], ['_', 'Sql'], ucfirst(strtolower($config['driver'])));
$class = "Dibi\\Drivers\\{$class}Driver";
if (!class_exists($class)) {
throw new Exception("Unable to create instance of dibi driver '$class'.");
} }
$this->driver = new $class;
} }
$config['name'] = $name; $config['name'] = $name;
$this->config = $config; $this->config = $config;
$this->driver = new $class;
$this->translator = new DibiTranslator($this);
// profiler // profiler
$profilerCfg = & $config['profiler']; $profilerCfg = & $config['profiler'];
if (is_scalar($profilerCfg)) { if (is_scalar($profilerCfg)) {
$profilerCfg = array('run' => (bool) $profilerCfg); $profilerCfg = ['run' => (bool) $profilerCfg];
} }
if (!empty($profilerCfg['run'])) { if (!empty($profilerCfg['run'])) {
$filter = isset($profilerCfg['filter']) ? $profilerCfg['filter'] : DibiEvent::QUERY; $filter = isset($profilerCfg['filter']) ? $profilerCfg['filter'] : Event::QUERY;
if (isset($profilerCfg['file'])) { if (isset($profilerCfg['file'])) {
$this->onEvent[] = array(new DibiFileLogger($profilerCfg['file'], $filter), 'logEvent'); $this->onEvent[] = [new Loggers\FileLogger($profilerCfg['file'], $filter), 'logEvent'];
} }
if (DibiFirePhpLogger::isAvailable()) { if (Loggers\FirePhpLogger::isAvailable()) {
$this->onEvent[] = array(new DibiFirePhpLogger($filter), 'logEvent'); $this->onEvent[] = [new Loggers\FirePhpLogger($filter), 'logEvent'];
} }
if (!interface_exists('Tracy\IBarPanel') && interface_exists('Nette\Diagnostics\IBarPanel') && class_exists('DibiNettePanel')) { if (!interface_exists('Tracy\IBarPanel') && interface_exists('Nette\Diagnostics\IBarPanel') && class_exists('Dibi\Bridges\Nette\Panel')) {
$panel = new DibiNettePanel(isset($profilerCfg['explain']) ? $profilerCfg['explain'] : TRUE, $filter); $panel = new Bridges\Nette\Panel(isset($profilerCfg['explain']) ? $profilerCfg['explain'] : TRUE, $filter);
$panel->register($this); $panel->register($this);
} }
} }
$this->substitutes = new DibiHashMap(create_function('$expr', 'return ":$expr:";')); $this->substitutes = new HashMap(function ($expr) { return ":$expr:"; });
if (!empty($config['substitutes'])) { if (!empty($config['substitutes'])) {
foreach ($config['substitutes'] as $key => $value) { foreach ($config['substitutes'] as $key => $value) {
$this->substitutes->$key = $value; $this->substitutes->$key = $value;
@@ -142,13 +148,13 @@ class DibiConnection extends DibiObject
*/ */
final public function connect() final public function connect()
{ {
$event = $this->onEvent ? new DibiEvent($this, DibiEvent::CONNECT) : NULL; $event = $this->onEvent ? new Event($this, Event::CONNECT) : NULL;
try { try {
$this->driver->connect($this->config); $this->driver->connect($this->config);
$this->connected = TRUE; $this->connected = TRUE;
$event && $this->onEvent($event->done()); $event && $this->onEvent($event->done());
} catch (DibiException $e) { } catch (Exception $e) {
$event && $this->onEvent($event->done($e)); $event && $this->onEvent($event->done($e));
throw $e; throw $e;
} }
@@ -197,30 +203,17 @@ class DibiConnection extends DibiObject
} }
/** /** @deprecated */
* Apply configuration alias or default values.
* @param array connect configuration
* @param string key
* @param string alias key
* @return void
*/
public static function alias(& $config, $key, $alias) public static function alias(& $config, $key, $alias)
{ {
$foo = & $config; trigger_error(__METHOD__ . '() is deprecated, use Helpers::alias().', E_USER_DEPRECATED);
foreach (explode('|', $key) as $key) { Helpers::alias($config, $key, $alias);
$foo = & $foo[$key];
}
if (!isset($foo) && isset($config[$alias])) {
$foo = $config[$alias];
unset($config[$alias]);
}
} }
/** /**
* Returns the driver and connects to a database in lazy mode. * Returns the driver and connects to a database in lazy mode.
* @return IDibiDriver * @return Driver
*/ */
final public function getDriver() final public function getDriver()
{ {
@@ -232,8 +225,8 @@ class DibiConnection extends DibiObject
/** /**
* Generates (translates) and executes SQL query. * Generates (translates) and executes SQL query.
* @param array|mixed one or more arguments * @param array|mixed one or more arguments
* @return DibiResult|int result set object (if any) * @return Result|int result set object (if any)
* @throws DibiException * @throws Exception
*/ */
final public function query($args) final public function query($args)
{ {
@@ -246,7 +239,7 @@ class DibiConnection extends DibiObject
* Generates SQL query. * Generates SQL query.
* @param array|mixed one or more arguments * @param array|mixed one or more arguments
* @return string * @return string
* @throws DibiException * @throws Exception
*/ */
final public function translate($args) final public function translate($args)
{ {
@@ -264,12 +257,12 @@ class DibiConnection extends DibiObject
{ {
$args = func_get_args(); $args = func_get_args();
try { try {
dibi::dump($this->translateArgs($args)); Helpers::dump($this->translateArgs($args));
return TRUE; return TRUE;
} catch (DibiException $e) { } catch (Exception $e) {
if ($e->getSql()) { if ($e->getSql()) {
dibi::dump($e->getSql()); Helpers::dump($e->getSql());
} else { } else {
echo get_class($e) . ': ' . $e->getMessage() . (PHP_SAPI === 'cli' ? "\n" : '<br>'); echo get_class($e) . ': ' . $e->getMessage() . (PHP_SAPI === 'cli' ? "\n" : '<br>');
} }
@@ -279,15 +272,15 @@ class DibiConnection extends DibiObject
/** /**
* Generates (translates) and returns SQL query as DibiDataSource. * Generates (translates) and returns SQL query as DataSource.
* @param array|mixed one or more arguments * @param array|mixed one or more arguments
* @return DibiDataSource * @return DataSource
* @throws DibiException * @throws Exception
*/ */
final public function dataSource($args) final public function dataSource($args)
{ {
$args = func_get_args(); $args = func_get_args();
return new DibiDataSource($this->translateArgs($args), $this); return new DataSource($this->translateArgs($args), $this);
} }
@@ -299,26 +292,30 @@ class DibiConnection extends DibiObject
private function translateArgs($args) private function translateArgs($args)
{ {
$this->connected || $this->connect(); $this->connected || $this->connect();
return $this->translator->translate($args); if (!$this->translator) {
$this->translator = new Translator($this);
}
$translator = clone $this->translator;
return $translator->translate($args);
} }
/** /**
* Executes the SQL query. * Executes the SQL query.
* @param string SQL statement. * @param string SQL statement.
* @return DibiResult|int result set object (if any) * @return Result|int result set object (if any)
* @throws DibiException * @throws Exception
*/ */
final public function nativeQuery($sql) final public function nativeQuery($sql)
{ {
$this->connected || $this->connect(); $this->connected || $this->connect();
dibi::$sql = $sql; \dibi::$sql = $sql;
$event = $this->onEvent ? new DibiEvent($this, DibiEvent::QUERY, $sql) : NULL; $event = $this->onEvent ? new Event($this, Event::QUERY, $sql) : NULL;
try { try {
$res = $this->driver->query($sql); $res = $this->driver->query($sql);
} catch (DibiException $e) { } catch (Exception $e) {
$event && $this->onEvent($event->done($e)); $event && $this->onEvent($event->done($e));
throw $e; throw $e;
} }
@@ -337,14 +334,14 @@ class DibiConnection extends DibiObject
/** /**
* Gets the number of affected rows by the last INSERT, UPDATE or DELETE query. * Gets the number of affected rows by the last INSERT, UPDATE or DELETE query.
* @return int number of rows * @return int number of rows
* @throws DibiException * @throws Exception
*/ */
public function getAffectedRows() public function getAffectedRows()
{ {
$this->connected || $this->connect(); $this->connected || $this->connect();
$rows = $this->driver->getAffectedRows(); $rows = $this->driver->getAffectedRows();
if (!is_int($rows) || $rows < 0) { if (!is_int($rows) || $rows < 0) {
throw new DibiException('Cannot retrieve number of affected rows.'); throw new Exception('Cannot retrieve number of affected rows.');
} }
return $rows; return $rows;
} }
@@ -353,7 +350,7 @@ class DibiConnection extends DibiObject
/** /**
* Gets the number of affected rows. Alias for getAffectedRows(). * Gets the number of affected rows. Alias for getAffectedRows().
* @return int number of rows * @return int number of rows
* @throws DibiException * @throws Exception
*/ */
public function affectedRows() public function affectedRows()
{ {
@@ -365,14 +362,14 @@ class DibiConnection extends DibiObject
* Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query. * Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query.
* @param string optional sequence name * @param string optional sequence name
* @return int * @return int
* @throws DibiException * @throws Exception
*/ */
public function getInsertId($sequence = NULL) public function getInsertId($sequence = NULL)
{ {
$this->connected || $this->connect(); $this->connected || $this->connect();
$id = $this->driver->getInsertId($sequence); $id = $this->driver->getInsertId($sequence);
if ($id < 1) { if ($id < 1) {
throw new DibiException('Cannot retrieve last generated ID.'); throw new Exception('Cannot retrieve last generated ID.');
} }
return (int) $id; return (int) $id;
} }
@@ -382,7 +379,7 @@ class DibiConnection extends DibiObject
* Retrieves the ID generated for an AUTO_INCREMENT column. Alias for getInsertId(). * Retrieves the ID generated for an AUTO_INCREMENT column. Alias for getInsertId().
* @param string optional sequence name * @param string optional sequence name
* @return int * @return int
* @throws DibiException * @throws Exception
*/ */
public function insertId($sequence = NULL) public function insertId($sequence = NULL)
{ {
@@ -398,12 +395,12 @@ class DibiConnection extends DibiObject
public function begin($savepoint = NULL) public function begin($savepoint = NULL)
{ {
$this->connected || $this->connect(); $this->connected || $this->connect();
$event = $this->onEvent ? new DibiEvent($this, DibiEvent::BEGIN, $savepoint) : NULL; $event = $this->onEvent ? new Event($this, Event::BEGIN, $savepoint) : NULL;
try { try {
$this->driver->begin($savepoint); $this->driver->begin($savepoint);
$event && $this->onEvent($event->done()); $event && $this->onEvent($event->done());
} catch (DibiException $e) { } catch (Exception $e) {
$event && $this->onEvent($event->done($e)); $event && $this->onEvent($event->done($e));
throw $e; throw $e;
} }
@@ -418,12 +415,12 @@ class DibiConnection extends DibiObject
public function commit($savepoint = NULL) public function commit($savepoint = NULL)
{ {
$this->connected || $this->connect(); $this->connected || $this->connect();
$event = $this->onEvent ? new DibiEvent($this, DibiEvent::COMMIT, $savepoint) : NULL; $event = $this->onEvent ? new Event($this, Event::COMMIT, $savepoint) : NULL;
try { try {
$this->driver->commit($savepoint); $this->driver->commit($savepoint);
$event && $this->onEvent($event->done()); $event && $this->onEvent($event->done());
} catch (DibiException $e) { } catch (Exception $e) {
$event && $this->onEvent($event->done($e)); $event && $this->onEvent($event->done($e));
throw $e; throw $e;
} }
@@ -438,12 +435,12 @@ class DibiConnection extends DibiObject
public function rollback($savepoint = NULL) public function rollback($savepoint = NULL)
{ {
$this->connected || $this->connect(); $this->connected || $this->connect();
$event = $this->onEvent ? new DibiEvent($this, DibiEvent::ROLLBACK, $savepoint) : NULL; $event = $this->onEvent ? new Event($this, Event::ROLLBACK, $savepoint) : NULL;
try { try {
$this->driver->rollback($savepoint); $this->driver->rollback($savepoint);
$event && $this->onEvent($event->done()); $event && $this->onEvent($event->done());
} catch (DibiException $e) { } catch (Exception $e) {
$event && $this->onEvent($event->done($e)); $event && $this->onEvent($event->done($e));
throw $e; throw $e;
} }
@@ -452,14 +449,14 @@ class DibiConnection extends DibiObject
/** /**
* Result set factory. * Result set factory.
* @param IDibiResultDriver * @param ResultDriver
* @return DibiResult * @return Result
*/ */
public function createResultSet(IDibiResultDriver $resultDriver) public function createResultSet(ResultDriver $resultDriver)
{ {
$res = new DibiResult($resultDriver); $res = new Result($resultDriver);
return $res->setFormat(dibi::DATE, $this->config['result']['formatDate']) return $res->setFormat(Type::DATE, $this->config['result']['formatDate'])
->setFormat(dibi::DATETIME, $this->config['result']['formatDateTime']); ->setFormat(Type::DATETIME, $this->config['result']['formatDateTime']);
} }
@@ -467,17 +464,17 @@ class DibiConnection extends DibiObject
/** /**
* @return DibiFluent * @return Fluent
*/ */
public function command() public function command()
{ {
return new DibiFluent($this); return new Fluent($this);
} }
/** /**
* @param string column name * @param string column name
* @return DibiFluent * @return Fluent
*/ */
public function select($args) public function select($args)
{ {
@@ -489,12 +486,12 @@ class DibiConnection extends DibiObject
/** /**
* @param string table * @param string table
* @param array * @param array
* @return DibiFluent * @return Fluent
*/ */
public function update($table, $args) public function update($table, $args)
{ {
if (!(is_array($args) || $args instanceof Traversable)) { if (!(is_array($args) || $args instanceof Traversable)) {
throw new InvalidArgumentException('Arguments must be array or Traversable.'); throw new \InvalidArgumentException('Arguments must be array or Traversable.');
} }
return $this->command()->update('%n', $table)->set($args); return $this->command()->update('%n', $table)->set($args);
} }
@@ -503,14 +500,14 @@ class DibiConnection extends DibiObject
/** /**
* @param string table * @param string table
* @param array * @param array
* @return DibiFluent * @return Fluent
*/ */
public function insert($table, $args) public function insert($table, $args)
{ {
if ($args instanceof Traversable) { if ($args instanceof Traversable) {
$args = iterator_to_array($args); $args = iterator_to_array($args);
} elseif (!is_array($args)) { } elseif (!is_array($args)) {
throw new InvalidArgumentException('Arguments must be array or Traversable.'); throw new \InvalidArgumentException('Arguments must be array or Traversable.');
} }
return $this->command()->insert() return $this->command()->insert()
->into('%n', $table, '(%n)', array_keys($args))->values('%l', $args); ->into('%n', $table, '(%n)', array_keys($args))->values('%l', $args);
@@ -519,7 +516,7 @@ class DibiConnection extends DibiObject
/** /**
* @param string table * @param string table
* @return DibiFluent * @return Fluent
*/ */
public function delete($table) public function delete($table)
{ {
@@ -532,7 +529,7 @@ class DibiConnection extends DibiObject
/** /**
* Returns substitution hashmap. * Returns substitution hashmap.
* @return DibiHashMap * @return HashMap
*/ */
public function getSubstitutes() public function getSubstitutes()
{ {
@@ -546,16 +543,9 @@ class DibiConnection extends DibiObject
*/ */
public function substitute($value) public function substitute($value)
{ {
return strpos($value, ':') === FALSE ? $value : preg_replace_callback('#:([^:\s]*):#', array($this, 'subCb'), $value); return strpos($value, ':') === FALSE
} ? $value
: preg_replace_callback('#:([^:\s]*):#', function ($m) { return $this->substitutes->{$m[1]}; }, $value);
/**
* Substitution callback.
*/
private function subCb($m)
{
return $this->substitutes->{$m[1]};
} }
@@ -565,8 +555,8 @@ class DibiConnection extends DibiObject
/** /**
* Executes SQL query and fetch result - shortcut for query() & fetch(). * Executes SQL query and fetch result - shortcut for query() & fetch().
* @param array|mixed one or more arguments * @param array|mixed one or more arguments
* @return DibiRow * @return Row
* @throws DibiException * @throws Exception
*/ */
public function fetch($args) public function fetch($args)
{ {
@@ -578,8 +568,8 @@ class DibiConnection extends DibiObject
/** /**
* Executes SQL query and fetch results - shortcut for query() & fetchAll(). * Executes SQL query and fetch results - shortcut for query() & fetchAll().
* @param array|mixed one or more arguments * @param array|mixed one or more arguments
* @return DibiRow[] * @return Row[]
* @throws DibiException * @throws Exception
*/ */
public function fetchAll($args) public function fetchAll($args)
{ {
@@ -592,7 +582,7 @@ class DibiConnection extends DibiObject
* Executes SQL query and fetch first column - shortcut for query() & fetchSingle(). * Executes SQL query and fetch first column - shortcut for query() & fetchSingle().
* @param array|mixed one or more arguments * @param array|mixed one or more arguments
* @return string * @return string
* @throws DibiException * @throws Exception
*/ */
public function fetchSingle($args) public function fetchSingle($args)
{ {
@@ -605,7 +595,7 @@ class DibiConnection extends DibiObject
* Executes SQL query and fetch pairs - shortcut for query() & fetchPairs(). * Executes SQL query and fetch pairs - shortcut for query() & fetchPairs().
* @param array|mixed one or more arguments * @param array|mixed one or more arguments
* @return string * @return string
* @throws DibiException * @throws Exception
*/ */
public function fetchPairs($args) public function fetchPairs($args)
{ {
@@ -614,59 +604,37 @@ class DibiConnection extends DibiObject
} }
/**
* @return Literal
*/
public static function literal($value)
{
return new Literal($value);
}
/********************* misc ****************d*g**/ /********************* misc ****************d*g**/
/** /**
* Import SQL dump from file - extreme fast! * Import SQL dump from file.
* @param string filename * @param string filename
* @return int count of sql commands * @return int count of sql commands
*/ */
public function loadFile($file) public function loadFile($file)
{ {
$this->connected || $this->connect(); return Helpers::loadFromFile($this, $file);
@set_time_limit(0); // intentionally @
$handle = @fopen($file, 'r'); // intentionally @
if (!$handle) {
throw new RuntimeException("Cannot open file '$file'.");
}
$count = 0;
$delimiter = ';';
$sql = '';
while (!feof($handle)) {
$s = rtrim(fgets($handle));
if (substr($s, 0, 10) === 'DELIMITER ') {
$delimiter = substr($s, 10);
} elseif (substr($s, -strlen($delimiter)) === $delimiter) {
$sql .= substr($s, 0, -strlen($delimiter));
$this->driver->query($sql);
$sql = '';
$count++;
} else {
$sql .= $s . "\n";
}
}
if (trim($sql) !== '') {
$this->driver->query($sql);
$count++;
}
fclose($handle);
return $count;
} }
/** /**
* Gets a information about the current database. * Gets a information about the current database.
* @return DibiDatabaseInfo * @return Reflection\Database
*/ */
public function getDatabaseInfo() public function getDatabaseInfo()
{ {
$this->connected || $this->connect(); $this->connected || $this->connect();
return new DibiDatabaseInfo($this->driver->getReflector(), isset($this->config['database']) ? $this->config['database'] : NULL); return new Reflection\Database($this->driver->getReflector(), isset($this->config['database']) ? $this->config['database'] : NULL);
} }
@@ -675,7 +643,7 @@ class DibiConnection extends DibiObject
*/ */
public function __wakeup() public function __wakeup()
{ {
throw new DibiNotSupportedException('You cannot serialize or unserialize ' . $this->getClass() . ' instances.'); throw new NotSupportedException('You cannot serialize or unserialize ' . get_class($this) . ' instances.');
} }
@@ -684,7 +652,15 @@ class DibiConnection extends DibiObject
*/ */
public function __sleep() public function __sleep()
{ {
throw new DibiNotSupportedException('You cannot serialize or unserialize ' . $this->getClass() . ' instances.'); throw new NotSupportedException('You cannot serialize or unserialize ' . get_class($this) . ' instances.');
}
protected function onEvent($arg)
{
foreach ($this->onEvent ?: [] as $handler) {
call_user_func($handler, $arg);
}
} }
} }

View File

@@ -5,21 +5,24 @@
* Copyright (c) 2005 David Grudl (https://davidgrudl.com) * Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/ */
namespace Dibi;
/** /**
* Default implementation of IDataSource for dibi. * Default implementation of IDataSource for dibi.
* *
* @package dibi
*/ */
class DibiDataSource extends DibiObject implements IDataSource class DataSource implements IDataSource
{ {
/** @var DibiConnection */ use Strict;
/** @var Connection */
private $connection; private $connection;
/** @var string */ /** @var string */
private $sql; private $sql;
/** @var DibiResult */ /** @var Result */
private $result; private $result;
/** @var int */ /** @var int */
@@ -29,13 +32,13 @@ class DibiDataSource extends DibiObject implements IDataSource
private $totalCount; private $totalCount;
/** @var array */ /** @var array */
private $cols = array(); private $cols = [];
/** @var array */ /** @var array */
private $sorting = array(); private $sorting = [];
/** @var array */ /** @var array */
private $conds = array(); private $conds = [];
/** @var int */ /** @var int */
private $offset; private $offset;
@@ -46,12 +49,12 @@ class DibiDataSource extends DibiObject implements IDataSource
/** /**
* @param string SQL command or table or view name, as data source * @param string SQL command or table or view name, as data source
* @param DibiConnection connection * @param Connection connection
*/ */
public function __construct($sql, DibiConnection $connection) public function __construct($sql, Connection $connection)
{ {
if (strpbrk($sql, " \t\r\n") === FALSE) { if (strpbrk($sql, " \t\r\n") === FALSE) {
$this->sql = $connection->getDriver()->escape($sql, dibi::IDENTIFIER); // table name $this->sql = $connection->getDriver()->escapeIdentifier($sql); // table name
} else { } else {
$this->sql = '(' . $sql . ') t'; // SQL command $this->sql = '(' . $sql . ') t'; // SQL command
} }
@@ -130,7 +133,7 @@ class DibiDataSource extends DibiObject implements IDataSource
/** /**
* Returns the dibi connection. * Returns the dibi connection.
* @return DibiConnection * @return Connection
*/ */
final public function getConnection() final public function getConnection()
{ {
@@ -142,8 +145,8 @@ class DibiDataSource extends DibiObject implements IDataSource
/** /**
* Returns (and queries) DibiResult. * Returns (and queries) Result.
* @return DibiResult * @return Result
*/ */
public function getResult() public function getResult()
{ {
@@ -155,7 +158,7 @@ class DibiDataSource extends DibiObject implements IDataSource
/** /**
* @return DibiResultIterator * @return ResultIterator
*/ */
public function getIterator() public function getIterator()
{ {
@@ -165,7 +168,7 @@ class DibiDataSource extends DibiObject implements IDataSource
/** /**
* Generates, executes SQL query and fetches the single row. * Generates, executes SQL query and fetches the single row.
* @return DibiRow|FALSE array on success, FALSE if no next record * @return Row|FALSE array on success, FALSE if no next record
*/ */
public function fetch() public function fetch()
{ {
@@ -230,8 +233,8 @@ class DibiDataSource extends DibiObject implements IDataSource
/** /**
* Returns this data source wrapped in DibiFluent object. * Returns this data source wrapped in Fluent object.
* @return DibiFluent * @return Fluent
*/ */
public function toFluent() public function toFluent()
{ {
@@ -240,8 +243,8 @@ class DibiDataSource extends DibiObject implements IDataSource
/** /**
* Returns this data source wrapped in DibiDataSource object. * Returns this data source wrapped in DataSource object.
* @return DibiDataSource * @return DataSource
*/ */
public function toDataSource() public function toDataSource()
{ {
@@ -259,11 +262,11 @@ class DibiDataSource extends DibiObject implements IDataSource
return $this->connection->translate(' return $this->connection->translate('
SELECT %n', (empty($this->cols) ? '*' : $this->cols), ' SELECT %n', (empty($this->cols) ? '*' : $this->cols), '
FROM %SQL', $this->sql, ' FROM %SQL', $this->sql, '
%ex', $this->conds ? array('WHERE %and', $this->conds) : NULL, ' %ex', $this->conds ? ['WHERE %and', $this->conds] : NULL, '
%ex', $this->sorting ? array('ORDER BY %by', $this->sorting) : NULL, ' %ex', $this->sorting ? ['ORDER BY %by', $this->sorting] : NULL, '
%ofs %lmt', $this->offset, $this->limit %ofs %lmt', $this->offset, $this->limit
); );
} catch (Exception $e) { } catch (\Exception $e) {
trigger_error($e->getMessage(), E_USER_ERROR); trigger_error($e->getMessage(), E_USER_ERROR);
} }
} }

58
src/Dibi/DateTime.php Normal file
View File

@@ -0,0 +1,58 @@
<?php
/**
* This file is part of the "dibi" - smart database abstraction layer.
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/
namespace Dibi;
/**
* DateTime.
*/
class DateTime extends \DateTime
{
use Strict;
public function __construct($time = 'now', \DateTimeZone $timezone = NULL)
{
if (is_numeric($time)) {
parent::__construct('@' . $time);
$this->setTimeZone($timezone ? $timezone : new \DateTimeZone(date_default_timezone_get()));
} elseif ($timezone === NULL) {
parent::__construct($time);
} else {
parent::__construct($time, $timezone);
}
}
public function modifyClone($modify = '')
{
$dolly = clone($this);
return $modify ? $dolly->modify($modify) : $dolly;
}
public function setTimestamp($timestamp)
{
$zone = $this->getTimezone();
$this->__construct('@' . $timestamp);
return $this->setTimeZone($zone);
}
public function getTimestamp()
{
$ts = $this->format('U');
return is_float($tmp = $ts * 1) ? $ts : $tmp;
}
public function __toString()
{
return $this->format('Y-m-d H:i:s');
}
}

View File

@@ -5,6 +5,10 @@
* Copyright (c) 2005 David Grudl (https://davidgrudl.com) * Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/ */
namespace Dibi\Drivers;
use Dibi;
/** /**
* The dibi driver for Firebird/InterBase database. * The dibi driver for Firebird/InterBase database.
@@ -16,12 +20,12 @@
* - charset => character encoding to set * - charset => character encoding to set
* - buffers (int) => buffers is the number of database buffers to allocate for the server-side cache. If 0 or omitted, server chooses its own default. * - buffers (int) => buffers is the number of database buffers to allocate for the server-side cache. If 0 or omitted, server chooses its own default.
* - resource (resource) => existing connection resource * - resource (resource) => existing connection resource
* - lazy, profiler, result, substitutes, ... => see DibiConnection options * - lazy, profiler, result, substitutes, ... => see Dibi\Connection options
*
* @package dibi\drivers
*/ */
class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultDriver, IDibiReflector class FirebirdDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
{ {
use Dibi\Strict;
const ERROR_EXCEPTION_THROWN = -836; const ERROR_EXCEPTION_THROWN = -836;
/** @var resource Connection resource */ /** @var resource Connection resource */
@@ -41,12 +45,12 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD
/** /**
* @throws DibiNotSupportedException * @throws Dibi\NotSupportedException
*/ */
public function __construct() public function __construct()
{ {
if (!extension_loaded('interbase')) { if (!extension_loaded('interbase')) {
throw new DibiNotSupportedException("PHP extension 'interbase' is not loaded."); throw new Dibi\NotSupportedException("PHP extension 'interbase' is not loaded.");
} }
} }
@@ -54,37 +58,33 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD
/** /**
* Connects to a database. * Connects to a database.
* @return void * @return void
* @throws DibiException * @throws Dibi\Exception
*/ */
public function connect(array & $config) public function connect(array & $config)
{ {
DibiConnection::alias($config, 'database', 'db'); Dibi\Helpers::alias($config, 'database', 'db');
if (isset($config['resource'])) { if (isset($config['resource'])) {
$this->connection = $config['resource']; $this->connection = $config['resource'];
} else { } else {
// default values // default values
$config += array( $config += [
'username' => ini_get('ibase.default_password'), 'username' => ini_get('ibase.default_password'),
'password' => ini_get('ibase.default_user'), 'password' => ini_get('ibase.default_user'),
'database' => ini_get('ibase.default_db'), 'database' => ini_get('ibase.default_db'),
'charset' => ini_get('ibase.default_charset'), 'charset' => ini_get('ibase.default_charset'),
'buffers' => 0, 'buffers' => 0,
); ];
DibiDriverException::tryError();
if (empty($config['persistent'])) { if (empty($config['persistent'])) {
$this->connection = ibase_connect($config['database'], $config['username'], $config['password'], $config['charset'], $config['buffers']); // intentionally @ $this->connection = ibase_connect($config['database'], $config['username'], $config['password'], $config['charset'], $config['buffers']); // intentionally @
} else { } else {
$this->connection = ibase_pconnect($config['database'], $config['username'], $config['password'], $config['charset'], $config['buffers']); // intentionally @ $this->connection = ibase_pconnect($config['database'], $config['username'], $config['password'], $config['charset'], $config['buffers']); // intentionally @
} }
if (DibiDriverException::catchError($msg)) {
throw new DibiDriverException($msg, ibase_errcode());
}
if (!is_resource($this->connection)) { if (!is_resource($this->connection)) {
throw new DibiDriverException(ibase_errmsg(), ibase_errcode()); throw new Dibi\DriverException(ibase_errmsg(), ibase_errcode());
} }
} }
} }
@@ -103,27 +103,22 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD
/** /**
* Executes the SQL query. * Executes the SQL query.
* @param string SQL statement. * @param string SQL statement.
* @return IDibiResultDriver|NULL * @return Dibi\ResultDriver|NULL
* @throws DibiDriverException|DibiException * @throws Dibi\DriverException|Dibi\Exception
*/ */
public function query($sql) public function query($sql)
{ {
DibiDriverException::tryError();
$resource = $this->inTransaction ? $this->transaction : $this->connection; $resource = $this->inTransaction ? $this->transaction : $this->connection;
$res = ibase_query($resource, $sql); $res = ibase_query($resource, $sql);
if (DibiDriverException::catchError($msg)) { if ($res === FALSE) {
if (ibase_errcode() == self::ERROR_EXCEPTION_THROWN) { if (ibase_errcode() == self::ERROR_EXCEPTION_THROWN) {
preg_match('/exception (\d+) (\w+) (.*)/i', ibase_errmsg(), $match); preg_match('/exception (\d+) (\w+) (.*)/i', ibase_errmsg(), $match);
throw new DibiProcedureException($match[3], $match[1], $match[2], dibi::$sql); throw new Dibi\ProcedureException($match[3], $match[1], $match[2], $sql);
} else { } else {
throw new DibiDriverException(ibase_errmsg(), ibase_errcode(), dibi::$sql); throw new Dibi\DriverException(ibase_errmsg(), ibase_errcode(), $sql);
} }
}
if ($res === FALSE) {
throw new DibiDriverException(ibase_errmsg(), ibase_errcode(), $sql);
} elseif (is_resource($res)) { } elseif (is_resource($res)) {
return $this->createResultDriver($res); return $this->createResultDriver($res);
@@ -156,14 +151,14 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD
* Begins a transaction (if supported). * Begins a transaction (if supported).
* @param string optional savepoint name * @param string optional savepoint name
* @return void * @return void
* @throws DibiDriverException * @throws Dibi\DriverException
*/ */
public function begin($savepoint = NULL) public function begin($savepoint = NULL)
{ {
if ($savepoint !== NULL) { if ($savepoint !== NULL) {
throw new DibiNotSupportedException('Savepoints are not supported in Firebird/Interbase.'); throw new Dibi\NotSupportedException('Savepoints are not supported in Firebird/Interbase.');
} }
$this->transaction = ibase_trans($this->resource); $this->transaction = ibase_trans($this->getResource());
$this->inTransaction = TRUE; $this->inTransaction = TRUE;
} }
@@ -172,16 +167,16 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD
* Commits statements in a transaction. * Commits statements in a transaction.
* @param string optional savepoint name * @param string optional savepoint name
* @return void * @return void
* @throws DibiDriverException * @throws Dibi\DriverException
*/ */
public function commit($savepoint = NULL) public function commit($savepoint = NULL)
{ {
if ($savepoint !== NULL) { if ($savepoint !== NULL) {
throw new DibiNotSupportedException('Savepoints are not supported in Firebird/Interbase.'); throw new Dibi\NotSupportedException('Savepoints are not supported in Firebird/Interbase.');
} }
if (!ibase_commit($this->transaction)) { if (!ibase_commit($this->transaction)) {
throw new DibiDriverException('Unable to handle operation - failure when commiting transaction.'); throw new Dibi\DriverException('Unable to handle operation - failure when commiting transaction.');
} }
$this->inTransaction = FALSE; $this->inTransaction = FALSE;
@@ -192,16 +187,16 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD
* Rollback changes in a transaction. * Rollback changes in a transaction.
* @param string optional savepoint name * @param string optional savepoint name
* @return void * @return void
* @throws DibiDriverException * @throws Dibi\DriverException
*/ */
public function rollback($savepoint = NULL) public function rollback($savepoint = NULL)
{ {
if ($savepoint !== NULL) { if ($savepoint !== NULL) {
throw new DibiNotSupportedException('Savepoints are not supported in Firebird/Interbase.'); throw new Dibi\NotSupportedException('Savepoints are not supported in Firebird/Interbase.');
} }
if (!ibase_rollback($this->transaction)) { if (!ibase_rollback($this->transaction)) {
throw new DibiDriverException('Unable to handle operation - failure when rolbacking transaction.'); throw new Dibi\DriverException('Unable to handle operation - failure when rolbacking transaction.');
} }
$this->inTransaction = FALSE; $this->inTransaction = FALSE;
@@ -230,7 +225,7 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD
/** /**
* Returns the connection reflector. * Returns the connection reflector.
* @return IDibiReflector * @return Dibi\Reflector
*/ */
public function getReflector() public function getReflector()
{ {
@@ -241,7 +236,7 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD
/** /**
* Result set driver factory. * Result set driver factory.
* @param resource * @param resource
* @return IDibiResultDriver * @return Dibi\ResultDriver
*/ */
public function createResultDriver($resource) public function createResultDriver($resource)
{ {
@@ -256,34 +251,48 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD
/** /**
* Encodes data for use in a SQL statement. * Encodes data for use in a SQL statement.
* @param mixed value * @param string
* @param string type (dibi::TEXT, dibi::BOOL, ...) * @return string
* @return string encoded value
* @throws InvalidArgumentException
*/ */
public function escape($value, $type) public function escapeText($value)
{ {
switch ($type) { return "'" . str_replace("'", "''", $value) . "'";
case dibi::TEXT: }
case dibi::BINARY:
return "'" . str_replace("'", "''", $value) . "'";
case dibi::IDENTIFIER:
return $value;
case dibi::BOOL: public function escapeBinary($value)
return $value ? 1 : 0; {
return "'" . str_replace("'", "''", $value) . "'";
}
case dibi::DATE:
case dibi::DATETIME:
if (!$value instanceof DateTime && !$value instanceof DateTimeInterface) {
$value = new DibiDateTime($value);
}
return $value->format($type === dibi::DATETIME ? "'Y-m-d H:i:s'" : "'Y-m-d'");
default: public function escapeIdentifier($value)
throw new InvalidArgumentException('Unsupported type.'); {
return $value;
}
public function escapeBool($value)
{
return $value ? 1 : 0;
}
public function escapeDate($value)
{
if (!$value instanceof \DateTime && !$value instanceof \DateTimeInterface) {
$value = new Dibi\DateTime($value);
} }
return $value->format("'Y-m-d'");
}
public function escapeDateTime($value)
{
if (!$value instanceof \DateTime && !$value instanceof \DateTimeInterface) {
$value = new Dibi\DateTime($value);
}
return $value->format("'Y-m-d H:i:s'");
} }
@@ -295,23 +304,26 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD
*/ */
public function escapeLike($value, $pos) public function escapeLike($value, $pos)
{ {
throw new DibiNotImplementedException; throw new Dibi\NotImplementedException;
} }
/** /**
* Decodes data from result set. * Decodes data from result set.
* @param string value * @param string
* @param string type (dibi::BINARY) * @return string
* @return string decoded value
* @throws InvalidArgumentException
*/ */
public function unescape($value, $type) public function unescapeBinary($value)
{ {
if ($type === dibi::BINARY) { return $value;
return $value; }
}
throw new InvalidArgumentException('Unsupported type.');
/** @deprecated */
public function escape($value, $type)
{
trigger_error(__METHOD__ . '() is deprecated.', E_USER_DEPRECATED);
return Dibi\Helpers::escape($this, $value, $type);
} }
@@ -347,7 +359,7 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD
*/ */
public function getRowCount() public function getRowCount()
{ {
throw new DibiNotSupportedException('Firebird/Interbase do not support returning number of rows in result set.'); throw new Dibi\NotSupportedException('Firebird/Interbase do not support returning number of rows in result set.');
} }
@@ -358,16 +370,15 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD
*/ */
public function fetch($assoc) public function fetch($assoc)
{ {
DibiDriverException::tryError();
$result = $assoc ? ibase_fetch_assoc($this->resultSet, IBASE_TEXT) : ibase_fetch_row($this->resultSet, IBASE_TEXT); // intentionally @ $result = $assoc ? ibase_fetch_assoc($this->resultSet, IBASE_TEXT) : ibase_fetch_row($this->resultSet, IBASE_TEXT); // intentionally @
if (DibiDriverException::catchError($msg)) { if (ibase_errcode()) {
if (ibase_errcode() == self::ERROR_EXCEPTION_THROWN) { if (ibase_errcode() == self::ERROR_EXCEPTION_THROWN) {
preg_match('/exception (\d+) (\w+) (.*)/is', ibase_errmsg(), $match); preg_match('/exception (\d+) (\w+) (.*)/is', ibase_errmsg(), $match);
throw new DibiProcedureException($match[3], $match[1], $match[2], dibi::$sql); throw new Dibi\ProcedureException($match[3], $match[1], $match[2]);
} else { } else {
throw new DibiDriverException($msg, ibase_errcode(), dibi::$sql); throw new Dibi\DriverException($msg, ibase_errcode());
} }
} }
@@ -379,11 +390,11 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD
* Moves cursor position without fetching row. * Moves cursor position without fetching row.
* @param int the 0-based cursor pos to seek to * @param int the 0-based cursor pos to seek to
* @return bool TRUE on success, FALSE if unable to seek to specified record * @return bool TRUE on success, FALSE if unable to seek to specified record
* @throws DibiException * @throws Dibi\Exception
*/ */
public function seek($row) public function seek($row)
{ {
throw new DibiNotSupportedException('Firebird/Interbase do not support seek in result set.'); throw new Dibi\NotSupportedException('Firebird/Interbase do not support seek in result set.');
} }
@@ -416,21 +427,21 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD
public function getResultColumns() public function getResultColumns()
{ {
$count = ibase_num_fields($this->resultSet); $count = ibase_num_fields($this->resultSet);
$columns = array(); $columns = [];
for ($i = 0; $i < $count; $i++) { for ($i = 0; $i < $count; $i++) {
$row = (array) ibase_field_info($this->resultSet, $i); $row = (array) ibase_field_info($this->resultSet, $i);
$columns[] = array( $columns[] = [
'name' => $row['name'], 'name' => $row['name'],
'fullname' => $row['name'], 'fullname' => $row['name'],
'table' => $row['relation'], 'table' => $row['relation'],
'nativetype' => $row['type'], 'nativetype' => $row['type'],
); ];
} }
return $columns; return $columns;
} }
/********************* IDibiReflector ********************/ /********************* Dibi\Reflector ********************/
/** /**
@@ -445,12 +456,12 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD
FROM RDB\$RELATIONS FROM RDB\$RELATIONS
WHERE RDB\$SYSTEM_FLAG = 0;" WHERE RDB\$SYSTEM_FLAG = 0;"
); );
$tables = array(); $tables = [];
while ($row = $res->fetch(FALSE)) { while ($row = $res->fetch(FALSE)) {
$tables[] = array( $tables[] = [
'name' => $row[0], 'name' => $row[0],
'view' => $row[1] === 'TRUE', 'view' => $row[1] === 'TRUE',
); ];
} }
return $tables; return $tables;
} }
@@ -494,10 +505,10 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD
ORDER BY r.RDB\$FIELD_POSITION;" ORDER BY r.RDB\$FIELD_POSITION;"
); );
$columns = array(); $columns = [];
while ($row = $res->fetch(TRUE)) { while ($row = $res->fetch(TRUE)) {
$key = $row['FIELD_NAME']; $key = $row['FIELD_NAME'];
$columns[$key] = array( $columns[$key] = [
'name' => $key, 'name' => $key,
'table' => $table, 'table' => $table,
'nativetype' => trim($row['FIELD_TYPE']), 'nativetype' => trim($row['FIELD_TYPE']),
@@ -505,7 +516,7 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD
'nullable' => $row['NULLABLE'] === 'TRUE', 'nullable' => $row['NULLABLE'] === 'TRUE',
'default' => $row['DEFAULT_VALUE'], 'default' => $row['DEFAULT_VALUE'],
'autoincrement' => FALSE, 'autoincrement' => FALSE,
); ];
} }
return $columns; return $columns;
} }
@@ -532,7 +543,7 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD
WHERE UPPER(i.RDB\$RELATION_NAME) = '$table' WHERE UPPER(i.RDB\$RELATION_NAME) = '$table'
ORDER BY s.RDB\$FIELD_POSITION" ORDER BY s.RDB\$FIELD_POSITION"
); );
$indexes = array(); $indexes = [];
while ($row = $res->fetch(TRUE)) { while ($row = $res->fetch(TRUE)) {
$key = $row['INDEX_NAME']; $key = $row['INDEX_NAME'];
$indexes[$key]['name'] = $key; $indexes[$key]['name'] = $key;
@@ -562,14 +573,14 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD
AND r.RDB\$CONSTRAINT_TYPE = 'FOREIGN KEY' AND r.RDB\$CONSTRAINT_TYPE = 'FOREIGN KEY'
ORDER BY s.RDB\$FIELD_POSITION" ORDER BY s.RDB\$FIELD_POSITION"
); );
$keys = array(); $keys = [];
while ($row = $res->fetch(TRUE)) { while ($row = $res->fetch(TRUE)) {
$key = $row['INDEX_NAME']; $key = $row['INDEX_NAME'];
$keys[$key] = array( $keys[$key] = [
'name' => $key, 'name' => $key,
'column' => $row['FIELD_NAME'], 'column' => $row['FIELD_NAME'],
'table' => $table, 'table' => $table,
); ];
} }
return $keys; return $keys;
} }
@@ -589,7 +600,7 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD
AND RDB\$UNIQUE_FLAG IS NULL AND RDB\$UNIQUE_FLAG IS NULL
AND RDB\$FOREIGN_KEY IS NULL;" AND RDB\$FOREIGN_KEY IS NULL;"
); );
$indices = array(); $indices = [];
while ($row = $res->fetch(FALSE)) { while ($row = $res->fetch(FALSE)) {
$indices[] = $row[0]; $indices[] = $row[0];
} }
@@ -613,7 +624,7 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD
OR RDB\$FOREIGN_KEY IS NOT NULL OR RDB\$FOREIGN_KEY IS NOT NULL
);" );"
); );
$constraints = array(); $constraints = [];
while ($row = $res->fetch(FALSE)) { while ($row = $res->fetch(FALSE)) {
$constraints[] = $row[0]; $constraints[] = $row[0];
} }
@@ -656,15 +667,15 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD
WHERE RDB\$SYSTEM_FLAG = 0" WHERE RDB\$SYSTEM_FLAG = 0"
. ($table === NULL ? ';' : " AND RDB\$RELATION_NAME = UPPER('$table');") . ($table === NULL ? ';' : " AND RDB\$RELATION_NAME = UPPER('$table');")
); );
$triggers = array(); $triggers = [];
while ($row = $res->fetch(TRUE)) { while ($row = $res->fetch(TRUE)) {
$triggers[$row['TRIGGER_NAME']] = array( $triggers[$row['TRIGGER_NAME']] = [
'name' => $row['TRIGGER_NAME'], 'name' => $row['TRIGGER_NAME'],
'table' => $row['TABLE_NAME'], 'table' => $row['TABLE_NAME'],
'type' => trim($row['TRIGGER_TYPE']), 'type' => trim($row['TRIGGER_TYPE']),
'event' => trim($row['TRIGGER_EVENT']), 'event' => trim($row['TRIGGER_EVENT']),
'enabled' => trim($row['TRIGGER_ENABLED']) === 'TRUE', 'enabled' => trim($row['TRIGGER_ENABLED']) === 'TRUE',
); ];
} }
return $triggers; return $triggers;
} }
@@ -684,7 +695,7 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD
$q .= $table === NULL ? ';' : " AND RDB\$RELATION_NAME = UPPER('$table')"; $q .= $table === NULL ? ';' : " AND RDB\$RELATION_NAME = UPPER('$table')";
$res = $this->query($q); $res = $this->query($q);
$triggers = array(); $triggers = [];
while ($row = $res->fetch(FALSE)) { while ($row = $res->fetch(FALSE)) {
$triggers[] = $row[0]; $triggers[] = $row[0];
} }
@@ -731,7 +742,7 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD
LEFT JOIN RDB\$FIELDS f ON f.RDB\$FIELD_NAME = p.RDB\$FIELD_SOURCE LEFT JOIN RDB\$FIELDS f ON f.RDB\$FIELD_NAME = p.RDB\$FIELD_SOURCE
ORDER BY p.RDB\$PARAMETER_TYPE, p.RDB\$PARAMETER_NUMBER;" ORDER BY p.RDB\$PARAMETER_TYPE, p.RDB\$PARAMETER_NUMBER;"
); );
$procedures = array(); $procedures = [];
while ($row = $res->fetch(TRUE)) { while ($row = $res->fetch(TRUE)) {
$key = $row['PROCEDURE_NAME']; $key = $row['PROCEDURE_NAME'];
$io = trim($row['PARAMETER_TYPE']); $io = trim($row['PARAMETER_TYPE']);
@@ -755,7 +766,7 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD
SELECT TRIM(RDB\$PROCEDURE_NAME) SELECT TRIM(RDB\$PROCEDURE_NAME)
FROM RDB\$PROCEDURES;" FROM RDB\$PROCEDURES;"
); );
$procedures = array(); $procedures = [];
while ($row = $res->fetch(FALSE)) { while ($row = $res->fetch(FALSE)) {
$procedures[] = $row[0]; $procedures[] = $row[0];
} }
@@ -774,7 +785,7 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD
FROM RDB\$GENERATORS FROM RDB\$GENERATORS
WHERE RDB\$SYSTEM_FLAG = 0;" WHERE RDB\$SYSTEM_FLAG = 0;"
); );
$generators = array(); $generators = [];
while ($row = $res->fetch(FALSE)) { while ($row = $res->fetch(FALSE)) {
$generators[] = $row[0]; $generators[] = $row[0];
} }
@@ -793,7 +804,7 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD
FROM RDB\$FUNCTIONS FROM RDB\$FUNCTIONS
WHERE RDB\$SYSTEM_FLAG = 0;" WHERE RDB\$SYSTEM_FLAG = 0;"
); );
$functions = array(); $functions = [];
while ($row = $res->fetch(FALSE)) { while ($row = $res->fetch(FALSE)) {
$functions[] = $row[0]; $functions[] = $row[0];
} }
@@ -801,39 +812,3 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD
} }
} }
/**
* Database procedure exception.
*
* @package dibi\drivers
*/
class DibiProcedureException extends DibiException
{
/** @var string */
protected $severity;
/**
* Construct the exception.
* @param string Message describing the exception
* @param int Some code
* @param string SQL command
*/
public function __construct($message = NULL, $code = 0, $severity = NULL, $sql = NULL)
{
parent::__construct($message, (int) $code, $sql);
$this->severity = $severity;
}
/**
* Gets the exception severity.
* @return string
*/
public function getSeverity()
{
$this->severity;
}
}

View File

@@ -5,7 +5,10 @@
* Copyright (c) 2005 David Grudl (https://davidgrudl.com) * Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/ */
require_once dirname(__FILE__) . '/DibiMsSqlReflector.php'; namespace Dibi\Drivers;
use Dibi;
/** /**
* The dibi driver for MS SQL database. * The dibi driver for MS SQL database.
@@ -17,12 +20,12 @@ require_once dirname(__FILE__) . '/DibiMsSqlReflector.php';
* - database => the database name to select * - database => the database name to select
* - persistent (bool) => try to find a persistent link? * - persistent (bool) => try to find a persistent link?
* - resource (resource) => existing connection resource * - resource (resource) => existing connection resource
* - lazy, profiler, result, substitutes, ... => see DibiConnection options * - lazy, profiler, result, substitutes, ... => see Dibi\Connection options
*
* @package dibi\drivers
*/ */
class DibiMsSqlDriver extends DibiObject implements IDibiDriver, IDibiResultDriver class MsSqlDriver implements Dibi\Driver, Dibi\ResultDriver
{ {
use Dibi\Strict;
/** @var resource Connection resource */ /** @var resource Connection resource */
private $connection; private $connection;
@@ -34,12 +37,12 @@ class DibiMsSqlDriver extends DibiObject implements IDibiDriver, IDibiResultDriv
/** /**
* @throws DibiNotSupportedException * @throws Dibi\NotSupportedException
*/ */
public function __construct() public function __construct()
{ {
if (!extension_loaded('mssql')) { if (!extension_loaded('mssql')) {
throw new DibiNotSupportedException("PHP extension 'mssql' is not loaded."); throw new Dibi\NotSupportedException("PHP extension 'mssql' is not loaded.");
} }
} }
@@ -47,7 +50,7 @@ class DibiMsSqlDriver extends DibiObject implements IDibiDriver, IDibiResultDriv
/** /**
* Connects to a database. * Connects to a database.
* @return void * @return void
* @throws DibiException * @throws Dibi\Exception
*/ */
public function connect(array & $config) public function connect(array & $config)
{ {
@@ -60,11 +63,11 @@ class DibiMsSqlDriver extends DibiObject implements IDibiDriver, IDibiResultDriv
} }
if (!is_resource($this->connection)) { if (!is_resource($this->connection)) {
throw new DibiDriverException("Can't connect to DB."); throw new Dibi\DriverException("Can't connect to DB.");
} }
if (isset($config['database']) && !@mssql_select_db($this->escape($config['database'], dibi::IDENTIFIER), $this->connection)) { // intentionally @ if (isset($config['database']) && !@mssql_select_db($this->escapeIdentifier($config['database']), $this->connection)) { // intentionally @
throw new DibiDriverException("Can't select DB '$config[database]'."); throw new Dibi\DriverException("Can't select DB '$config[database]'.");
} }
} }
@@ -82,15 +85,15 @@ class DibiMsSqlDriver extends DibiObject implements IDibiDriver, IDibiResultDriv
/** /**
* Executes the SQL query. * Executes the SQL query.
* @param string SQL statement. * @param string SQL statement.
* @return IDibiResultDriver|NULL * @return Dibi\ResultDriver|NULL
* @throws DibiDriverException * @throws Dibi\DriverException
*/ */
public function query($sql) public function query($sql)
{ {
$res = @mssql_query($sql, $this->connection); // intentionally @ $res = @mssql_query($sql, $this->connection); // intentionally @
if ($res === FALSE) { if ($res === FALSE) {
throw new DibiDriverException(mssql_get_last_message(), 0, $sql); throw new Dibi\DriverException(mssql_get_last_message(), 0, $sql);
} elseif (is_resource($res)) { } elseif (is_resource($res)) {
return $this->createResultDriver($res); return $this->createResultDriver($res);
@@ -127,7 +130,7 @@ class DibiMsSqlDriver extends DibiObject implements IDibiDriver, IDibiResultDriv
* Begins a transaction (if supported). * Begins a transaction (if supported).
* @param string optional savepoint name * @param string optional savepoint name
* @return void * @return void
* @throws DibiDriverException * @throws Dibi\DriverException
*/ */
public function begin($savepoint = NULL) public function begin($savepoint = NULL)
{ {
@@ -139,7 +142,7 @@ class DibiMsSqlDriver extends DibiObject implements IDibiDriver, IDibiResultDriv
* Commits statements in a transaction. * Commits statements in a transaction.
* @param string optional savepoint name * @param string optional savepoint name
* @return void * @return void
* @throws DibiDriverException * @throws Dibi\DriverException
*/ */
public function commit($savepoint = NULL) public function commit($savepoint = NULL)
{ {
@@ -151,7 +154,7 @@ class DibiMsSqlDriver extends DibiObject implements IDibiDriver, IDibiResultDriv
* Rollback changes in a transaction. * Rollback changes in a transaction.
* @param string optional savepoint name * @param string optional savepoint name
* @return void * @return void
* @throws DibiDriverException * @throws Dibi\DriverException
*/ */
public function rollback($savepoint = NULL) public function rollback($savepoint = NULL)
{ {
@@ -171,18 +174,18 @@ class DibiMsSqlDriver extends DibiObject implements IDibiDriver, IDibiResultDriv
/** /**
* Returns the connection reflector. * Returns the connection reflector.
* @return IDibiReflector * @return Dibi\Reflector
*/ */
public function getReflector() public function getReflector()
{ {
return new DibiMsSqlReflector($this); return new MsSqlReflector($this);
} }
/** /**
* Result set driver factory. * Result set driver factory.
* @param resource * @param resource
* @return IDibiResultDriver * @return Dibi\ResultDriver
*/ */
public function createResultDriver($resource) public function createResultDriver($resource)
{ {
@@ -198,34 +201,48 @@ class DibiMsSqlDriver extends DibiObject implements IDibiDriver, IDibiResultDriv
/** /**
* Encodes data for use in a SQL statement. * Encodes data for use in a SQL statement.
* @param mixed value * @param mixed value
* @param string type (dibi::TEXT, dibi::BOOL, ...)
* @return string encoded value * @return string encoded value
* @throws InvalidArgumentException
*/ */
public function escape($value, $type) public function escapeText($value)
{ {
switch ($type) { return "'" . str_replace("'", "''", $value) . "'";
case dibi::TEXT: }
case dibi::BINARY:
return "'" . str_replace("'", "''", $value) . "'";
case dibi::IDENTIFIER:
// @see https://msdn.microsoft.com/en-us/library/ms176027.aspx
return '[' . str_replace(array('[', ']'), array('[[', ']]'), $value) . ']';
case dibi::BOOL: public function escapeBinary($value)
return $value ? 1 : 0; {
return "'" . str_replace("'", "''", $value) . "'";
}
case dibi::DATE:
case dibi::DATETIME:
if (!$value instanceof DateTime && !$value instanceof DateTimeInterface) {
$value = new DibiDateTime($value);
}
return $value->format($type === dibi::DATETIME ? "'Y-m-d H:i:s'" : "'Y-m-d'");
default: public function escapeIdentifier($value)
throw new InvalidArgumentException('Unsupported type.'); {
// @see https://msdn.microsoft.com/en-us/library/ms176027.aspx
return '[' . str_replace(['[', ']'], ['[[', ']]'], $value) . ']';
}
public function escapeBool($value)
{
return $value ? 1 : 0;
}
public function escapeDate($value)
{
if (!$value instanceof \DateTime && !$value instanceof \DateTimeInterface) {
$value = new Dibi\DateTime($value);
} }
return $value->format("'Y-m-d'");
}
public function escapeDateTime($value)
{
if (!$value instanceof \DateTime && !$value instanceof \DateTimeInterface) {
$value = new Dibi\DateTime($value);
}
return $value->format("'Y-m-d H:i:s'");
} }
@@ -237,24 +254,27 @@ class DibiMsSqlDriver extends DibiObject implements IDibiDriver, IDibiResultDriv
*/ */
public function escapeLike($value, $pos) public function escapeLike($value, $pos)
{ {
$value = strtr($value, array("'" => "''", '%' => '[%]', '_' => '[_]', '[' => '[[]')); $value = strtr($value, ["'" => "''", '%' => '[%]', '_' => '[_]', '[' => '[[]']);
return ($pos <= 0 ? "'%" : "'") . $value . ($pos >= 0 ? "%'" : "'"); return ($pos <= 0 ? "'%" : "'") . $value . ($pos >= 0 ? "%'" : "'");
} }
/** /**
* Decodes data from result set. * Decodes data from result set.
* @param string value * @param string
* @param string type (dibi::BINARY) * @return string
* @return string decoded value
* @throws InvalidArgumentException
*/ */
public function unescape($value, $type) public function unescapeBinary($value)
{ {
if ($type === dibi::BINARY) { return $value;
return $value; }
}
throw new InvalidArgumentException('Unsupported type.');
/** @deprecated */
public function escape($value, $type)
{
trigger_error(__METHOD__ . '() is deprecated.', E_USER_DEPRECATED);
return Dibi\Helpers::escape($this, $value, $type);
} }
@@ -264,13 +284,14 @@ class DibiMsSqlDriver extends DibiObject implements IDibiDriver, IDibiResultDriv
*/ */
public function applyLimit(& $sql, $limit, $offset) public function applyLimit(& $sql, $limit, $offset)
{ {
// offset support is missing
if ($limit >= 0) {
$sql = 'SELECT TOP ' . (int) $limit . ' * FROM (' . $sql . ') t';
}
if ($offset) { if ($offset) {
throw new DibiNotImplementedException('Offset is not implemented.'); throw new Dibi\NotSupportedException('Offset is not supported by this database.');
} elseif ($limit < 0) {
throw new Dibi\NotSupportedException('Negative offset or limit.');
} elseif ($limit !== NULL) {
$sql = 'SELECT TOP ' . (int) $limit . ' * FROM (' . $sql . ') t';
} }
} }
@@ -338,15 +359,15 @@ class DibiMsSqlDriver extends DibiObject implements IDibiDriver, IDibiResultDriv
public function getResultColumns() public function getResultColumns()
{ {
$count = mssql_num_fields($this->resultSet); $count = mssql_num_fields($this->resultSet);
$columns = array(); $columns = [];
for ($i = 0; $i < $count; $i++) { for ($i = 0; $i < $count; $i++) {
$row = (array) mssql_fetch_field($this->resultSet, $i); $row = (array) mssql_fetch_field($this->resultSet, $i);
$columns[] = array( $columns[] = [
'name' => $row['name'], 'name' => $row['name'],
'fullname' => $row['column_source'] ? $row['column_source'] . '.' . $row['name'] : $row['name'], 'fullname' => $row['column_source'] ? $row['column_source'] . '.' . $row['name'] : $row['name'],
'table' => $row['column_source'], 'table' => $row['column_source'],
'nativetype' => $row['type'], 'nativetype' => $row['type'],
); ];
} }
return $columns; return $columns;
} }

View File

@@ -5,20 +5,24 @@
* Copyright (c) 2005 David Grudl (https://davidgrudl.com) * Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/ */
namespace Dibi\Drivers;
use Dibi;
/** /**
* The dibi reflector for MsSQL databases. * The dibi reflector for MS SQL databases.
*
* @package dibi\drivers
* @internal * @internal
*/ */
class DibiMsSqlReflector extends DibiObject implements IDibiReflector class MsSqlReflector implements Dibi\Reflector
{ {
/** @var IDibiDriver */ use Dibi\Strict;
/** @var Dibi\Driver */
private $driver; private $driver;
public function __construct(IDibiDriver $driver) public function __construct(Dibi\Driver $driver)
{ {
$this->driver = $driver; $this->driver = $driver;
} }
@@ -34,12 +38,12 @@ class DibiMsSqlReflector extends DibiObject implements IDibiReflector
SELECT TABLE_NAME, TABLE_TYPE SELECT TABLE_NAME, TABLE_TYPE
FROM INFORMATION_SCHEMA.TABLES FROM INFORMATION_SCHEMA.TABLES
'); ');
$tables = array(); $tables = [];
while ($row = $res->fetch(FALSE)) { while ($row = $res->fetch(FALSE)) {
$tables[] = array( $tables[] = [
'name' => $row[0], 'name' => $row[0],
'view' => isset($row[1]) && $row[1] === 'VIEW', 'view' => isset($row[1]) && $row[1] === 'VIEW',
); ];
} }
return $tables; return $tables;
} }
@@ -58,13 +62,13 @@ class DibiMsSqlReflector extends DibiObject implements IDibiReflector
$result = $this->driver->query(" $result = $this->driver->query("
SELECT MAX(rowcnt) SELECT MAX(rowcnt)
FROM sys.sysindexes FROM sys.sysindexes
WHERE id=OBJECT_ID({$this->driver->escape($table, dibi::IDENTIFIER)}) WHERE id=OBJECT_ID({$this->driver->escapeIdentifier($table)})
"); ");
$row = $result->fetch(FALSE); $row = $result->fetch(FALSE);
if (!is_array($row) || count($row) < 1) { if (!is_array($row) || count($row) < 1) {
if ($fallback) { if ($fallback) {
$row = $this->driver->query("SELECT COUNT(*) FROM {$this->driver->escape($table, dibi::IDENTIFIER)}")->fetch(FALSE); $row = $this->driver->query("SELECT COUNT(*) FROM {$this->driver->escapeIdentifier($table)}")->fetch(FALSE);
$count = intval($row[0]); $count = intval($row[0]);
} else { } else {
$count = FALSE; $count = FALSE;
@@ -87,22 +91,22 @@ class DibiMsSqlReflector extends DibiObject implements IDibiReflector
$res = $this->driver->query(" $res = $this->driver->query("
SELECT * FROM SELECT * FROM
INFORMATION_SCHEMA.COLUMNS INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = {$this->driver->escape($table, dibi::TEXT)} WHERE TABLE_NAME = {$this->driver->escapeText($table)}
ORDER BY TABLE_NAME, ORDINAL_POSITION ORDER BY TABLE_NAME, ORDINAL_POSITION
"); ");
$columns = array(); $columns = [];
while ($row = $res->fetch(TRUE)) { while ($row = $res->fetch(TRUE)) {
$size = FALSE; $size = FALSE;
$type = strtoupper($row['DATA_TYPE']); $type = strtoupper($row['DATA_TYPE']);
$size_cols = array( $size_cols = [
'DATETIME' => 'DATETIME_PRECISION', 'DATETIME' => 'DATETIME_PRECISION',
'DECIMAL' => 'NUMERIC_PRECISION', 'DECIMAL' => 'NUMERIC_PRECISION',
'CHAR' => 'CHARACTER_MAXIMUM_LENGTH', 'CHAR' => 'CHARACTER_MAXIMUM_LENGTH',
'NCHAR' => 'CHARACTER_OCTET_LENGTH', 'NCHAR' => 'CHARACTER_OCTET_LENGTH',
'NVARCHAR' => 'CHARACTER_OCTET_LENGTH', 'NVARCHAR' => 'CHARACTER_OCTET_LENGTH',
'VARCHAR' => 'CHARACTER_OCTET_LENGTH', 'VARCHAR' => 'CHARACTER_OCTET_LENGTH',
); ];
if (isset($size_cols[$type])) { if (isset($size_cols[$type])) {
if ($size_cols[$type]) { if ($size_cols[$type]) {
@@ -110,7 +114,7 @@ class DibiMsSqlReflector extends DibiObject implements IDibiReflector
} }
} }
$columns[] = array( $columns[] = [
'name' => $row['COLUMN_NAME'], 'name' => $row['COLUMN_NAME'],
'table' => $table, 'table' => $table,
'nativetype' => $type, 'nativetype' => $type,
@@ -120,7 +124,7 @@ class DibiMsSqlReflector extends DibiObject implements IDibiReflector
'default' => $row['COLUMN_DEFAULT'], 'default' => $row['COLUMN_DEFAULT'],
'autoincrement' => FALSE, 'autoincrement' => FALSE,
'vendor' => $row, 'vendor' => $row,
); ];
} }
return $columns; return $columns;
@@ -144,22 +148,22 @@ class DibiMsSqlReflector extends DibiObject implements IDibiReflector
(ic.object_id = col.object_id and ic.column_id = col.column_id) (ic.object_id = col.object_id and ic.column_id = col.column_id)
INNER JOIN sys.tables t ON INNER JOIN sys.tables t ON
(ind.object_id = t.object_id) (ind.object_id = t.object_id)
WHERE t.name = {$this->driver->escape($table, dibi::TEXT)} WHERE t.name = {$this->driver->escapeText($table)}
AND t.is_ms_shipped = 0 AND t.is_ms_shipped = 0
ORDER BY ORDER BY
t.name, ind.name, ind.index_id, ic.index_column_id t.name, ind.name, ind.index_id, ic.index_column_id
"); ");
$indexes = array(); $indexes = [];
while ($row = $res->fetch(TRUE)) { while ($row = $res->fetch(TRUE)) {
$index_name = $row['index_name']; $index_name = $row['index_name'];
if (!isset($indexes[$index_name])) { if (!isset($indexes[$index_name])) {
$indexes[$index_name] = array(); $indexes[$index_name] = [];
$indexes[$index_name]['name'] = $index_name; $indexes[$index_name]['name'] = $index_name;
$indexes[$index_name]['unique'] = (bool) $row['is_unique']; $indexes[$index_name]['unique'] = (bool) $row['is_unique'];
$indexes[$index_name]['primary'] = (bool) $row['is_primary_key']; $indexes[$index_name]['primary'] = (bool) $row['is_primary_key'];
$indexes[$index_name]['columns'] = array(); $indexes[$index_name]['columns'] = [];
} }
$indexes[$index_name]['columns'][] = $row['column_name']; $indexes[$index_name]['columns'][] = $row['column_name'];
} }
@@ -187,18 +191,18 @@ class DibiMsSqlReflector extends DibiObject implements IDibiReflector
FROM sys.foreign_keys AS f FROM sys.foreign_keys AS f
INNER JOIN sys.foreign_key_columns AS fc INNER JOIN sys.foreign_key_columns AS fc
ON f.OBJECT_ID = fc.constraint_object_id ON f.OBJECT_ID = fc.constraint_object_id
WHERE OBJECT_NAME(f.parent_object_id) = {$this->driver->escape($table, dibi::TEXT)} WHERE OBJECT_NAME(f.parent_object_id) = {$this->driver->escapeText($table)}
"); ");
$keys = array(); $keys = [];
while ($row = $res->fetch(TRUE)) { while ($row = $res->fetch(TRUE)) {
$key_name = $row['foreign_key']; $key_name = $row['foreign_key'];
if (!isset($keys[$key_name])) { if (!isset($keys[$key_name])) {
$keys[$key_name]['name'] = $row['foreign_key']; // foreign 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]['local'] = [$row['column_name']]; // local columns
$keys[$key_name]['table'] = $row['reference_table_name']; // referenced table $keys[$key_name]['table'] = $row['reference_table_name']; // referenced table
$keys[$key_name]['foreign'] = array($row['reference_column_name']); // referenced columns $keys[$key_name]['foreign'] = [$row['reference_column_name']]; // referenced columns
$keys[$key_name]['onDelete'] = FALSE; $keys[$key_name]['onDelete'] = FALSE;
$keys[$key_name]['onUpdate'] = FALSE; $keys[$key_name]['onUpdate'] = FALSE;
} else { } else {

View File

@@ -5,8 +5,9 @@
* Copyright (c) 2005 David Grudl (https://davidgrudl.com) * Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/ */
namespace Dibi\Drivers;
require_once dirname(__FILE__) . '/DibiMySqlReflector.php'; use Dibi;
/** /**
@@ -25,12 +26,12 @@ require_once dirname(__FILE__) . '/DibiMySqlReflector.php';
* - unbuffered (bool) => sends query without fetching and buffering the result rows automatically? * - unbuffered (bool) => sends query without fetching and buffering the result rows automatically?
* - sqlmode => see http://dev.mysql.com/doc/refman/5.0/en/server-sql-mode.html * - sqlmode => see http://dev.mysql.com/doc/refman/5.0/en/server-sql-mode.html
* - resource (resource) => existing connection resource * - resource (resource) => existing connection resource
* - lazy, profiler, result, substitutes, ... => see DibiConnection options * - lazy, profiler, result, substitutes, ... => see Dibi\Connection options
*
* @package dibi\drivers
*/ */
class DibiMySqlDriver extends DibiObject implements IDibiDriver, IDibiResultDriver class MySqlDriver implements Dibi\Driver, Dibi\ResultDriver
{ {
use Dibi\Strict;
const ERROR_ACCESS_DENIED = 1045; const ERROR_ACCESS_DENIED = 1045;
const ERROR_DUPLICATE_ENTRY = 1062; const ERROR_DUPLICATE_ENTRY = 1062;
const ERROR_DATA_TRUNCATED = 1265; const ERROR_DATA_TRUNCATED = 1265;
@@ -49,12 +50,12 @@ class DibiMySqlDriver extends DibiObject implements IDibiDriver, IDibiResultDriv
/** /**
* @throws DibiNotSupportedException * @throws Dibi\NotSupportedException
*/ */
public function __construct() public function __construct()
{ {
if (!extension_loaded('mysql')) { if (!extension_loaded('mysql')) {
throw new DibiNotSupportedException("PHP extension 'mysql' is not loaded."); throw new Dibi\NotSupportedException("PHP extension 'mysql' is not loaded.");
} }
} }
@@ -62,7 +63,7 @@ class DibiMySqlDriver extends DibiObject implements IDibiDriver, IDibiResultDriv
/** /**
* Connects to a database. * Connects to a database.
* @return void * @return void
* @throws DibiException * @throws Dibi\Exception
*/ */
public function connect(array & $config) public function connect(array & $config)
{ {
@@ -71,13 +72,13 @@ class DibiMySqlDriver extends DibiObject implements IDibiDriver, IDibiResultDriv
} else { } else {
// default values // default values
DibiConnection::alias($config, 'flags', 'options'); Dibi\Helpers::alias($config, 'flags', 'options');
$config += array( $config += [
'charset' => 'utf8', 'charset' => 'utf8',
'timezone' => date('P'), 'timezone' => date('P'),
'username' => ini_get('mysql.default_user'), 'username' => ini_get('mysql.default_user'),
'password' => ini_get('mysql.default_password'), 'password' => ini_get('mysql.default_password'),
); ];
if (!isset($config['host'])) { if (!isset($config['host'])) {
$host = ini_get('mysql.default_host'); $host = ini_get('mysql.default_host');
if ($host) { if ($host) {
@@ -105,23 +106,18 @@ class DibiMySqlDriver extends DibiObject implements IDibiDriver, IDibiResultDriv
} }
if (!is_resource($this->connection)) { if (!is_resource($this->connection)) {
throw new DibiDriverException(mysql_error(), mysql_errno()); throw new Dibi\DriverException(mysql_error(), mysql_errno());
} }
if (isset($config['charset'])) { if (isset($config['charset'])) {
$ok = FALSE; if (!@mysql_set_charset($config['charset'], $this->connection)) { // intentionally @
if (function_exists('mysql_set_charset')) {
// affects the character set used by mysql_real_escape_string() (was added in MySQL 5.0.7 and PHP 5.2.3)
$ok = @mysql_set_charset($config['charset'], $this->connection); // intentionally @
}
if (!$ok) {
$this->query("SET NAMES '$config[charset]'"); $this->query("SET NAMES '$config[charset]'");
} }
} }
if (isset($config['database'])) { if (isset($config['database'])) {
if (!@mysql_select_db($config['database'], $this->connection)) { // intentionally @ if (!@mysql_select_db($config['database'], $this->connection)) { // intentionally @
throw new DibiDriverException(mysql_error($this->connection), mysql_errno($this->connection)); throw new Dibi\DriverException(mysql_error($this->connection), mysql_errno($this->connection));
} }
} }
@@ -150,8 +146,8 @@ class DibiMySqlDriver extends DibiObject implements IDibiDriver, IDibiResultDriv
/** /**
* Executes the SQL query. * Executes the SQL query.
* @param string SQL statement. * @param string SQL statement.
* @return IDibiResultDriver|NULL * @return Dibi\ResultDriver|NULL
* @throws DibiDriverException * @throws Dibi\DriverException
*/ */
public function query($sql) public function query($sql)
{ {
@@ -161,8 +157,8 @@ class DibiMySqlDriver extends DibiObject implements IDibiDriver, IDibiResultDriv
$res = @mysql_unbuffered_query($sql, $this->connection); // intentionally @ $res = @mysql_unbuffered_query($sql, $this->connection); // intentionally @
} }
if (mysql_errno($this->connection)) { if ($code = mysql_errno($this->connection)) {
throw new DibiDriverException(mysql_error($this->connection), mysql_errno($this->connection), $sql); throw MySqliDriver::createException(mysql_error($this->connection), $code, $sql);
} elseif (is_resource($res)) { } elseif (is_resource($res)) {
return $this->createResultDriver($res); return $this->createResultDriver($res);
@@ -176,10 +172,10 @@ class DibiMySqlDriver extends DibiObject implements IDibiDriver, IDibiResultDriv
*/ */
public function getInfo() public function getInfo()
{ {
$res = array(); $res = [];
preg_match_all('#(.+?): +(\d+) *#', mysql_info($this->connection), $matches, PREG_SET_ORDER); preg_match_all('#(.+?): +(\d+) *#', mysql_info($this->connection), $matches, PREG_SET_ORDER);
if (preg_last_error()) { if (preg_last_error()) {
throw new DibiPcreException; throw new Dibi\PcreException;
} }
foreach ($matches as $m) { foreach ($matches as $m) {
@@ -213,7 +209,7 @@ class DibiMySqlDriver extends DibiObject implements IDibiDriver, IDibiResultDriv
* Begins a transaction (if supported). * Begins a transaction (if supported).
* @param string optional savepoint name * @param string optional savepoint name
* @return void * @return void
* @throws DibiDriverException * @throws Dibi\DriverException
*/ */
public function begin($savepoint = NULL) public function begin($savepoint = NULL)
{ {
@@ -225,7 +221,7 @@ class DibiMySqlDriver extends DibiObject implements IDibiDriver, IDibiResultDriv
* Commits statements in a transaction. * Commits statements in a transaction.
* @param string optional savepoint name * @param string optional savepoint name
* @return void * @return void
* @throws DibiDriverException * @throws Dibi\DriverException
*/ */
public function commit($savepoint = NULL) public function commit($savepoint = NULL)
{ {
@@ -237,7 +233,7 @@ class DibiMySqlDriver extends DibiObject implements IDibiDriver, IDibiResultDriv
* Rollback changes in a transaction. * Rollback changes in a transaction.
* @param string optional savepoint name * @param string optional savepoint name
* @return void * @return void
* @throws DibiDriverException * @throws Dibi\DriverException
*/ */
public function rollback($savepoint = NULL) public function rollback($savepoint = NULL)
{ {
@@ -257,18 +253,18 @@ class DibiMySqlDriver extends DibiObject implements IDibiDriver, IDibiResultDriv
/** /**
* Returns the connection reflector. * Returns the connection reflector.
* @return IDibiReflector * @return Dibi\Reflector
*/ */
public function getReflector() public function getReflector()
{ {
return new DibiMySqlReflector($this); return new MySqlReflector($this);
} }
/** /**
* Result set driver factory. * Result set driver factory.
* @param resource * @param resource
* @return IDibiResultDriver * @return Dibi\ResultDriver
*/ */
public function createResultDriver($resource) public function createResultDriver($resource)
{ {
@@ -284,42 +280,54 @@ class DibiMySqlDriver extends DibiObject implements IDibiDriver, IDibiResultDriv
/** /**
* Encodes data for use in a SQL statement. * Encodes data for use in a SQL statement.
* @param mixed value * @param mixed value
* @param string type (dibi::TEXT, dibi::BOOL, ...)
* @return string encoded value * @return string encoded value
* @throws InvalidArgumentException
*/ */
public function escape($value, $type) public function escapeText($value)
{ {
switch ($type) { if (!is_resource($this->connection)) {
case dibi::TEXT: throw new Dibi\Exception('Lost connection to server.');
if (!is_resource($this->connection)) {
throw new DibiException('Lost connection to server.');
}
return "'" . mysql_real_escape_string($value, $this->connection) . "'";
case dibi::BINARY:
if (!is_resource($this->connection)) {
throw new DibiException('Lost connection to server.');
}
return "_binary'" . mysql_real_escape_string($value, $this->connection) . "'";
case dibi::IDENTIFIER:
// @see http://dev.mysql.com/doc/refman/5.0/en/identifiers.html
return '`' . str_replace('`', '``', $value) . '`';
case dibi::BOOL:
return $value ? 1 : 0;
case dibi::DATE:
case dibi::DATETIME:
if (!$value instanceof DateTime && !$value instanceof DateTimeInterface) {
$value = new DibiDateTime($value);
}
return $value->format($type === dibi::DATETIME ? "'Y-m-d H:i:s'" : "'Y-m-d'");
default:
throw new InvalidArgumentException('Unsupported type.');
} }
return "'" . mysql_real_escape_string($value, $this->connection) . "'";
}
public function escapeBinary($value)
{
if (!is_resource($this->connection)) {
throw new Dibi\Exception('Lost connection to server.');
}
return "_binary'" . mysql_real_escape_string($value, $this->connection) . "'";
}
public function escapeIdentifier($value)
{
// @see http://dev.mysql.com/doc/refman/5.0/en/identifiers.html
return '`' . str_replace('`', '``', $value) . '`';
}
public function escapeBool($value)
{
return $value ? 1 : 0;
}
public function escapeDate($value)
{
if (!$value instanceof \DateTime && !$value instanceof \DateTimeInterface) {
$value = new Dibi\DateTime($value);
}
return $value->format("'Y-m-d'");
}
public function escapeDateTime($value)
{
if (!$value instanceof \DateTime && !$value instanceof \DateTimeInterface) {
$value = new Dibi\DateTime($value);
}
return $value->format("'Y-m-d H:i:s'");
} }
@@ -338,17 +346,20 @@ class DibiMySqlDriver extends DibiObject implements IDibiDriver, IDibiResultDriv
/** /**
* Decodes data from result set. * Decodes data from result set.
* @param string value * @param string
* @param string type (dibi::BINARY) * @return string
* @return string decoded value
* @throws InvalidArgumentException
*/ */
public function unescape($value, $type) public function unescapeBinary($value)
{ {
if ($type === dibi::BINARY) { return $value;
return $value; }
}
throw new InvalidArgumentException('Unsupported type.');
/** @deprecated */
public function escape($value, $type)
{
trigger_error(__METHOD__ . '() is deprecated.', E_USER_DEPRECATED);
return Dibi\Helpers::escape($this, $value, $type);
} }
@@ -358,10 +369,13 @@ class DibiMySqlDriver extends DibiObject implements IDibiDriver, IDibiResultDriv
*/ */
public function applyLimit(& $sql, $limit, $offset) public function applyLimit(& $sql, $limit, $offset)
{ {
if ($limit >= 0 || $offset > 0) { if ($limit < 0 || $offset < 0) {
throw new Dibi\NotSupportedException('Negative offset or limit.');
} elseif ($limit !== NULL || $offset) {
// see http://dev.mysql.com/doc/refman/5.0/en/select.html // see http://dev.mysql.com/doc/refman/5.0/en/select.html
$sql .= ' LIMIT ' . ($limit < 0 ? '18446744073709551615' : (int) $limit) $sql .= ' LIMIT ' . ($limit === NULL ? '18446744073709551615' : (int) $limit)
. ($offset > 0 ? ' OFFSET ' . (int) $offset : ''); . ($offset ? ' OFFSET ' . (int) $offset : '');
} }
} }
@@ -386,7 +400,7 @@ class DibiMySqlDriver extends DibiObject implements IDibiDriver, IDibiResultDriv
public function getRowCount() public function getRowCount()
{ {
if (!$this->buffered) { if (!$this->buffered) {
throw new DibiNotSupportedException('Row count is not available for unbuffered queries.'); throw new Dibi\NotSupportedException('Row count is not available for unbuffered queries.');
} }
return mysql_num_rows($this->resultSet); return mysql_num_rows($this->resultSet);
} }
@@ -407,12 +421,12 @@ class DibiMySqlDriver extends DibiObject implements IDibiDriver, IDibiResultDriv
* Moves cursor position without fetching row. * Moves cursor position without fetching row.
* @param int the 0-based cursor pos to seek to * @param int the 0-based cursor pos to seek to
* @return bool TRUE on success, FALSE if unable to seek to specified record * @return bool TRUE on success, FALSE if unable to seek to specified record
* @throws DibiException * @throws Dibi\Exception
*/ */
public function seek($row) public function seek($row)
{ {
if (!$this->buffered) { if (!$this->buffered) {
throw new DibiNotSupportedException('Cannot seek an unbuffered result set.'); throw new Dibi\NotSupportedException('Cannot seek an unbuffered result set.');
} }
return mysql_data_seek($this->resultSet, $row); return mysql_data_seek($this->resultSet, $row);
@@ -437,16 +451,17 @@ class DibiMySqlDriver extends DibiObject implements IDibiDriver, IDibiResultDriv
public function getResultColumns() public function getResultColumns()
{ {
$count = mysql_num_fields($this->resultSet); $count = mysql_num_fields($this->resultSet);
$columns = array(); $columns = [];
for ($i = 0; $i < $count; $i++) { for ($i = 0; $i < $count; $i++) {
$row = (array) mysql_fetch_field($this->resultSet, $i); $row = (array) mysql_fetch_field($this->resultSet, $i);
$columns[] = array( $columns[] = [
'name' => $row['name'], 'name' => $row['name'],
'table' => $row['table'], 'table' => $row['table'],
'fullname' => $row['table'] ? $row['table'] . '.' . $row['name'] : $row['name'], 'fullname' => $row['table'] ? $row['table'] . '.' . $row['name'] : $row['name'],
'nativetype' => strtoupper($row['type']), 'nativetype' => strtoupper($row['type']),
'type' => $row['type'] === 'time' ? Dibi\Type::TIME_INTERVAL : NULL,
'vendor' => $row, 'vendor' => $row,
); ];
} }
return $columns; return $columns;
} }

View File

@@ -5,20 +5,24 @@
* Copyright (c) 2005 David Grudl (https://davidgrudl.com) * Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/ */
namespace Dibi\Drivers;
use Dibi;
/** /**
* The dibi reflector for MySQL databases. * The dibi reflector for MySQL databases.
*
* @package dibi\drivers
* @internal * @internal
*/ */
class DibiMySqlReflector extends DibiObject implements IDibiReflector class MySqlReflector implements Dibi\Reflector
{ {
/** @var IDibiDriver */ use Dibi\Strict;
/** @var Dibi\Driver */
private $driver; private $driver;
public function __construct(IDibiDriver $driver) public function __construct(Dibi\Driver $driver)
{ {
$this->driver = $driver; $this->driver = $driver;
} }
@@ -30,18 +34,13 @@ class DibiMySqlReflector extends DibiObject implements IDibiReflector
*/ */
public function getTables() public function getTables()
{ {
/*$this->query("
SELECT TABLE_NAME as name, TABLE_TYPE = 'VIEW' as view
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = DATABASE()
");*/
$res = $this->driver->query('SHOW FULL TABLES'); $res = $this->driver->query('SHOW FULL TABLES');
$tables = array(); $tables = [];
while ($row = $res->fetch(FALSE)) { while ($row = $res->fetch(FALSE)) {
$tables[] = array( $tables[] = [
'name' => $row[0], 'name' => $row[0],
'view' => isset($row[1]) && $row[1] === 'VIEW', 'view' => isset($row[1]) && $row[1] === 'VIEW',
); ];
} }
return $tables; return $tables;
} }
@@ -54,17 +53,11 @@ class DibiMySqlReflector extends DibiObject implements IDibiReflector
*/ */
public function getColumns($table) public function getColumns($table)
{ {
/*$table = $this->escape($table, dibi::TEXT); $res = $this->driver->query("SHOW FULL COLUMNS FROM {$this->driver->escapeIdentifier($table)}");
$this->query(" $columns = [];
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = $table AND TABLE_SCHEMA = DATABASE()
");*/
$res = $this->driver->query("SHOW FULL COLUMNS FROM {$this->driver->escape($table, dibi::IDENTIFIER)}");
$columns = array();
while ($row = $res->fetch(TRUE)) { while ($row = $res->fetch(TRUE)) {
$type = explode('(', $row['Type']); $type = explode('(', $row['Type']);
$columns[] = array( $columns[] = [
'name' => $row['Field'], 'name' => $row['Field'],
'table' => $table, 'table' => $table,
'nativetype' => strtoupper($type[0]), 'nativetype' => strtoupper($type[0]),
@@ -74,7 +67,7 @@ class DibiMySqlReflector extends DibiObject implements IDibiReflector
'default' => $row['Default'], 'default' => $row['Default'],
'autoincrement' => $row['Extra'] === 'auto_increment', 'autoincrement' => $row['Extra'] === 'auto_increment',
'vendor' => $row, 'vendor' => $row,
); ];
} }
return $columns; return $columns;
} }
@@ -87,15 +80,8 @@ class DibiMySqlReflector extends DibiObject implements IDibiReflector
*/ */
public function getIndexes($table) public function getIndexes($table)
{ {
/*$table = $this->escape($table, dibi::TEXT); $res = $this->driver->query("SHOW INDEX FROM {$this->driver->escapeIdentifier($table)}");
$this->query(" $indexes = [];
SELECT *
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE TABLE_NAME = $table AND TABLE_SCHEMA = DATABASE()
AND REFERENCED_COLUMN_NAME IS NULL
");*/
$res = $this->driver->query("SHOW INDEX FROM {$this->driver->escape($table, dibi::IDENTIFIER)}");
$indexes = array();
while ($row = $res->fetch(TRUE)) { while ($row = $res->fetch(TRUE)) {
$indexes[$row['Key_name']]['name'] = $row['Key_name']; $indexes[$row['Key_name']]['name'] = $row['Key_name'];
$indexes[$row['Key_name']]['unique'] = !$row['Non_unique']; $indexes[$row['Key_name']]['unique'] = !$row['Non_unique'];
@@ -110,13 +96,13 @@ class DibiMySqlReflector extends DibiObject implements IDibiReflector
* Returns metadata for all foreign keys in a table. * Returns metadata for all foreign keys in a table.
* @param string * @param string
* @return array * @return array
* @throws DibiNotSupportedException * @throws Dibi\NotSupportedException
*/ */
public function getForeignKeys($table) public function getForeignKeys($table)
{ {
$data = $this->driver->query("SELECT `ENGINE` FROM information_schema.TABLES WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = {$this->driver->escape($table, dibi::TEXT)}")->fetch(TRUE); $data = $this->driver->query("SELECT `ENGINE` FROM information_schema.TABLES WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = {$this->driver->escapeText($table)}")->fetch(TRUE);
if ($data['ENGINE'] !== 'InnoDB') { if ($data['ENGINE'] !== 'InnoDB') {
throw new DibiNotSupportedException("Foreign keys are not supported in {$data['ENGINE']} tables."); throw new Dibi\NotSupportedException("Foreign keys are not supported in {$data['ENGINE']} tables.");
} }
$res = $this->driver->query(" $res = $this->driver->query("
@@ -128,11 +114,11 @@ class DibiMySqlReflector extends DibiObject implements IDibiReflector
kcu.CONSTRAINT_NAME = rc.CONSTRAINT_NAME kcu.CONSTRAINT_NAME = rc.CONSTRAINT_NAME
AND kcu.CONSTRAINT_SCHEMA = rc.CONSTRAINT_SCHEMA AND kcu.CONSTRAINT_SCHEMA = rc.CONSTRAINT_SCHEMA
WHERE rc.CONSTRAINT_SCHEMA = DATABASE() WHERE rc.CONSTRAINT_SCHEMA = DATABASE()
AND rc.TABLE_NAME = {$this->driver->escape($table, dibi::TEXT)} AND rc.TABLE_NAME = {$this->driver->escapeText($table)}
GROUP BY rc.CONSTRAINT_NAME GROUP BY rc.CONSTRAINT_NAME
"); ");
$foreignKeys = array(); $foreignKeys = [];
while ($row = $res->fetch(TRUE)) { while ($row = $res->fetch(TRUE)) {
$keyName = $row['CONSTRAINT_NAME']; $keyName = $row['CONSTRAINT_NAME'];

View File

@@ -5,8 +5,9 @@
* Copyright (c) 2005 David Grudl (https://davidgrudl.com) * Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/ */
namespace Dibi\Drivers;
require_once dirname(__FILE__) . '/DibiMySqlReflector.php'; use Dibi;
/** /**
@@ -26,12 +27,12 @@ require_once dirname(__FILE__) . '/DibiMySqlReflector.php';
* - unbuffered (bool) => sends query without fetching and buffering the result rows automatically? * - unbuffered (bool) => sends query without fetching and buffering the result rows automatically?
* - sqlmode => see http://dev.mysql.com/doc/refman/5.0/en/server-sql-mode.html * - sqlmode => see http://dev.mysql.com/doc/refman/5.0/en/server-sql-mode.html
* - resource (mysqli) => existing connection resource * - resource (mysqli) => existing connection resource
* - lazy, profiler, result, substitutes, ... => see DibiConnection options * - lazy, profiler, result, substitutes, ... => see Dibi\Connection options
*
* @package dibi\drivers
*/ */
class DibiMySqliDriver extends DibiObject implements IDibiDriver, IDibiResultDriver class MySqliDriver implements Dibi\Driver, Dibi\ResultDriver
{ {
use Dibi\Strict;
const ERROR_ACCESS_DENIED = 1045; const ERROR_ACCESS_DENIED = 1045;
const ERROR_DUPLICATE_ENTRY = 1062; const ERROR_DUPLICATE_ENTRY = 1062;
const ERROR_DATA_TRUNCATED = 1265; const ERROR_DATA_TRUNCATED = 1265;
@@ -50,12 +51,12 @@ class DibiMySqliDriver extends DibiObject implements IDibiDriver, IDibiResultDri
/** /**
* @throws DibiNotSupportedException * @throws Dibi\NotSupportedException
*/ */
public function __construct() public function __construct()
{ {
if (!extension_loaded('mysqli')) { if (!extension_loaded('mysqli')) {
throw new DibiNotSupportedException("PHP extension 'mysqli' is not loaded."); throw new Dibi\NotSupportedException("PHP extension 'mysqli' is not loaded.");
} }
} }
@@ -63,7 +64,7 @@ class DibiMySqliDriver extends DibiObject implements IDibiDriver, IDibiResultDri
/** /**
* Connects to a database. * Connects to a database.
* @return void * @return void
* @throws DibiException * @throws Dibi\Exception
*/ */
public function connect(array & $config) public function connect(array & $config)
{ {
@@ -73,14 +74,14 @@ class DibiMySqliDriver extends DibiObject implements IDibiDriver, IDibiResultDri
} else { } else {
// default values // default values
$config += array( $config += [
'charset' => 'utf8', 'charset' => 'utf8',
'timezone' => date('P'), 'timezone' => date('P'),
'username' => ini_get('mysqli.default_user'), 'username' => ini_get('mysqli.default_user'),
'password' => ini_get('mysqli.default_pw'), 'password' => ini_get('mysqli.default_pw'),
'socket' => (string) ini_get('mysqli.default_socket'), 'socket' => (string) ini_get('mysqli.default_socket'),
'port' => NULL, 'port' => NULL,
); ];
if (!isset($config['host'])) { if (!isset($config['host'])) {
$host = ini_get('mysqli.default_host'); $host = ini_get('mysqli.default_host');
if ($host) { if ($host) {
@@ -109,7 +110,7 @@ class DibiMySqliDriver extends DibiObject implements IDibiDriver, IDibiResultDri
@mysqli_real_connect($this->connection, (empty($config['persistent']) ? '' : 'p:') . $config['host'], $config['username'], $config['password'], $config['database'], $config['port'], $config['socket'], $config['flags']); // intentionally @ @mysqli_real_connect($this->connection, (empty($config['persistent']) ? '' : 'p:') . $config['host'], $config['username'], $config['password'], $config['database'], $config['port'], $config['socket'], $config['flags']); // intentionally @
if ($errno = mysqli_connect_errno()) { if ($errno = mysqli_connect_errno()) {
throw new DibiDriverException(mysqli_connect_error(), $errno); throw new Dibi\DriverException(mysqli_connect_error(), $errno);
} }
} }
@@ -144,15 +145,15 @@ class DibiMySqliDriver extends DibiObject implements IDibiDriver, IDibiResultDri
/** /**
* Executes the SQL query. * Executes the SQL query.
* @param string SQL statement. * @param string SQL statement.
* @return IDibiResultDriver|NULL * @return Dibi\ResultDriver|NULL
* @throws DibiDriverException * @throws Dibi\DriverException
*/ */
public function query($sql) public function query($sql)
{ {
$res = @mysqli_query($this->connection, $sql, $this->buffered ? MYSQLI_STORE_RESULT : MYSQLI_USE_RESULT); // intentionally @ $res = @mysqli_query($this->connection, $sql, $this->buffered ? MYSQLI_STORE_RESULT : MYSQLI_USE_RESULT); // intentionally @
if (mysqli_errno($this->connection)) { if ($code = mysqli_errno($this->connection)) {
throw new DibiDriverException(mysqli_error($this->connection), mysqli_errno($this->connection), $sql); throw self::createException(mysqli_error($this->connection), $code, $sql);
} elseif (is_object($res)) { } elseif (is_object($res)) {
return $this->createResultDriver($res); return $this->createResultDriver($res);
@@ -160,16 +161,36 @@ class DibiMySqliDriver extends DibiObject implements IDibiDriver, IDibiResultDri
} }
/**
* @return Dibi\DriverException
*/
public static function createException($message, $code, $sql)
{
if (in_array($code, [1216, 1217, 1451, 1452, 1701], TRUE)) {
return new Dibi\ForeignKeyConstraintViolationException($message, $code, $sql);
} elseif (in_array($code, [1062, 1557, 1569, 1586], TRUE)) {
return new Dibi\UniqueConstraintViolationException($message, $code, $sql);
} elseif (in_array($code, [1048, 1121, 1138, 1171, 1252, 1263, 1566], TRUE)) {
return new Dibi\NotNullConstraintViolationException($message, $code, $sql);
} else {
return new Dibi\DriverException($message, $code, $sql);
}
}
/** /**
* Retrieves information about the most recently executed query. * Retrieves information about the most recently executed query.
* @return array * @return array
*/ */
public function getInfo() public function getInfo()
{ {
$res = array(); $res = [];
preg_match_all('#(.+?): +(\d+) *#', mysqli_info($this->connection), $matches, PREG_SET_ORDER); preg_match_all('#(.+?): +(\d+) *#', mysqli_info($this->connection), $matches, PREG_SET_ORDER);
if (preg_last_error()) { if (preg_last_error()) {
throw new DibiPcreException; throw new Dibi\PcreException;
} }
foreach ($matches as $m) { foreach ($matches as $m) {
@@ -203,7 +224,7 @@ class DibiMySqliDriver extends DibiObject implements IDibiDriver, IDibiResultDri
* Begins a transaction (if supported). * Begins a transaction (if supported).
* @param string optional savepoint name * @param string optional savepoint name
* @return void * @return void
* @throws DibiDriverException * @throws Dibi\DriverException
*/ */
public function begin($savepoint = NULL) public function begin($savepoint = NULL)
{ {
@@ -215,7 +236,7 @@ class DibiMySqliDriver extends DibiObject implements IDibiDriver, IDibiResultDri
* Commits statements in a transaction. * Commits statements in a transaction.
* @param string optional savepoint name * @param string optional savepoint name
* @return void * @return void
* @throws DibiDriverException * @throws Dibi\DriverException
*/ */
public function commit($savepoint = NULL) public function commit($savepoint = NULL)
{ {
@@ -227,7 +248,7 @@ class DibiMySqliDriver extends DibiObject implements IDibiDriver, IDibiResultDri
* Rollback changes in a transaction. * Rollback changes in a transaction.
* @param string optional savepoint name * @param string optional savepoint name
* @return void * @return void
* @throws DibiDriverException * @throws Dibi\DriverException
*/ */
public function rollback($savepoint = NULL) public function rollback($savepoint = NULL)
{ {
@@ -247,20 +268,20 @@ class DibiMySqliDriver extends DibiObject implements IDibiDriver, IDibiResultDri
/** /**
* Returns the connection reflector. * Returns the connection reflector.
* @return IDibiReflector * @return Dibi\Reflector
*/ */
public function getReflector() public function getReflector()
{ {
return new DibiMySqlReflector($this); return new MySqlReflector($this);
} }
/** /**
* Result set driver factory. * Result set driver factory.
* @param mysqli_result * @param mysqli_result
* @return IDibiResultDriver * @return Dibi\ResultDriver
*/ */
public function createResultDriver(mysqli_result $resource) public function createResultDriver(\mysqli_result $resource)
{ {
$res = clone $this; $res = clone $this;
$res->resultSet = $resource; $res->resultSet = $resource;
@@ -274,35 +295,47 @@ class DibiMySqliDriver extends DibiObject implements IDibiDriver, IDibiResultDri
/** /**
* Encodes data for use in a SQL statement. * Encodes data for use in a SQL statement.
* @param mixed value * @param mixed value
* @param string type (dibi::TEXT, dibi::BOOL, ...)
* @return string encoded value * @return string encoded value
* @throws InvalidArgumentException
*/ */
public function escape($value, $type) public function escapeText($value)
{ {
switch ($type) { return "'" . mysqli_real_escape_string($this->connection, $value) . "'";
case dibi::TEXT: }
return "'" . mysqli_real_escape_string($this->connection, $value) . "'";
case dibi::BINARY:
return "_binary'" . mysqli_real_escape_string($this->connection, $value) . "'";
case dibi::IDENTIFIER: public function escapeBinary($value)
return '`' . str_replace('`', '``', $value) . '`'; {
return "_binary'" . mysqli_real_escape_string($this->connection, $value) . "'";
}
case dibi::BOOL:
return $value ? 1 : 0;
case dibi::DATE: public function escapeIdentifier($value)
case dibi::DATETIME: {
if (!$value instanceof DateTime && !$value instanceof DateTimeInterface) { return '`' . str_replace('`', '``', $value) . '`';
$value = new DibiDateTime($value); }
}
return $value->format($type === dibi::DATETIME ? "'Y-m-d H:i:s'" : "'Y-m-d'");
default:
throw new InvalidArgumentException('Unsupported type.'); public function escapeBool($value)
{
return $value ? 1 : 0;
}
public function escapeDate($value)
{
if (!$value instanceof \DateTime && !$value instanceof \DateTimeInterface) {
$value = new Dibi\DateTime($value);
} }
return $value->format("'Y-m-d'");
}
public function escapeDateTime($value)
{
if (!$value instanceof \DateTime && !$value instanceof \DateTimeInterface) {
$value = new Dibi\DateTime($value);
}
return $value->format("'Y-m-d H:i:s'");
} }
@@ -321,17 +354,20 @@ class DibiMySqliDriver extends DibiObject implements IDibiDriver, IDibiResultDri
/** /**
* Decodes data from result set. * Decodes data from result set.
* @param string value * @param string
* @param string type (dibi::BINARY) * @return string
* @return string decoded value
* @throws InvalidArgumentException
*/ */
public function unescape($value, $type) public function unescapeBinary($value)
{ {
if ($type === dibi::BINARY) { return $value;
return $value; }
}
throw new InvalidArgumentException('Unsupported type.');
/** @deprecated */
public function escape($value, $type)
{
trigger_error(__METHOD__ . '() is deprecated.', E_USER_DEPRECATED);
return Dibi\Helpers::escape($this, $value, $type);
} }
@@ -341,10 +377,13 @@ class DibiMySqliDriver extends DibiObject implements IDibiDriver, IDibiResultDri
*/ */
public function applyLimit(& $sql, $limit, $offset) public function applyLimit(& $sql, $limit, $offset)
{ {
if ($limit >= 0 || $offset > 0) { if ($limit < 0 || $offset < 0) {
throw new Dibi\NotSupportedException('Negative offset or limit.');
} elseif ($limit !== NULL || $offset) {
// see http://dev.mysql.com/doc/refman/5.0/en/select.html // see http://dev.mysql.com/doc/refman/5.0/en/select.html
$sql .= ' LIMIT ' . ($limit < 0 ? '18446744073709551615' : (int) $limit) $sql .= ' LIMIT ' . ($limit === NULL ? '18446744073709551615' : (int) $limit)
. ($offset > 0 ? ' OFFSET ' . (int) $offset : ''); . ($offset ? ' OFFSET ' . (int) $offset : '');
} }
} }
@@ -369,7 +408,7 @@ class DibiMySqliDriver extends DibiObject implements IDibiDriver, IDibiResultDri
public function getRowCount() public function getRowCount()
{ {
if (!$this->buffered) { if (!$this->buffered) {
throw new DibiNotSupportedException('Row count is not available for unbuffered queries.'); throw new Dibi\NotSupportedException('Row count is not available for unbuffered queries.');
} }
return mysqli_num_rows($this->resultSet); return mysqli_num_rows($this->resultSet);
} }
@@ -390,12 +429,12 @@ class DibiMySqliDriver extends DibiObject implements IDibiDriver, IDibiResultDri
* Moves cursor position without fetching row. * Moves cursor position without fetching row.
* @param int the 0-based cursor pos to seek to * @param int the 0-based cursor pos to seek to
* @return bool TRUE on success, FALSE if unable to seek to specified record * @return bool TRUE on success, FALSE if unable to seek to specified record
* @throws DibiException * @throws Dibi\Exception
*/ */
public function seek($row) public function seek($row)
{ {
if (!$this->buffered) { if (!$this->buffered) {
throw new DibiNotSupportedException('Cannot seek an unbuffered result set.'); throw new Dibi\NotSupportedException('Cannot seek an unbuffered result set.');
} }
return mysqli_data_seek($this->resultSet, $row); return mysqli_data_seek($this->resultSet, $row);
} }
@@ -421,8 +460,8 @@ class DibiMySqliDriver extends DibiObject implements IDibiDriver, IDibiResultDri
static $types; static $types;
if ($types === NULL) { if ($types === NULL) {
$consts = get_defined_constants(TRUE); $consts = get_defined_constants(TRUE);
$types = array(); $types = [];
foreach (isset($consts['mysqli']) ? $consts['mysqli'] : array() as $key => $value) { foreach (isset($consts['mysqli']) ? $consts['mysqli'] : [] as $key => $value) {
if (strncmp($key, 'MYSQLI_TYPE_', 12) === 0) { if (strncmp($key, 'MYSQLI_TYPE_', 12) === 0) {
$types[$value] = substr($key, 12); $types[$value] = substr($key, 12);
} }
@@ -431,16 +470,17 @@ class DibiMySqliDriver extends DibiObject implements IDibiDriver, IDibiResultDri
} }
$count = mysqli_num_fields($this->resultSet); $count = mysqli_num_fields($this->resultSet);
$columns = array(); $columns = [];
for ($i = 0; $i < $count; $i++) { for ($i = 0; $i < $count; $i++) {
$row = (array) mysqli_fetch_field_direct($this->resultSet, $i); $row = (array) mysqli_fetch_field_direct($this->resultSet, $i);
$columns[] = array( $columns[] = [
'name' => $row['name'], 'name' => $row['name'],
'table' => $row['orgtable'], 'table' => $row['orgtable'],
'fullname' => $row['table'] ? $row['table'] . '.' . $row['name'] : $row['name'], 'fullname' => $row['table'] ? $row['table'] . '.' . $row['name'] : $row['name'],
'nativetype' => isset($types[$row['type']]) ? $types[$row['type']] : $row['type'], 'nativetype' => isset($types[$row['type']]) ? $types[$row['type']] : $row['type'],
'type' => $row['type'] === MYSQLI_TYPE_TIME ? Dibi\Type::TIME_INTERVAL : NULL,
'vendor' => $row, 'vendor' => $row,
); ];
} }
return $columns; return $columns;
} }

View File

@@ -5,6 +5,10 @@
* Copyright (c) 2005 David Grudl (https://davidgrudl.com) * Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/ */
namespace Dibi\Drivers;
use Dibi;
/** /**
* The dibi driver interacting with databases via ODBC connections. * The dibi driver interacting with databases via ODBC connections.
@@ -15,12 +19,12 @@
* - password (or pass) * - password (or pass)
* - persistent (bool) => try to find a persistent link? * - persistent (bool) => try to find a persistent link?
* - resource (resource) => existing connection resource * - resource (resource) => existing connection resource
* - lazy, profiler, result, substitutes, ... => see DibiConnection options * - lazy, profiler, result, substitutes, ... => see Dibi\Connection options
*
* @package dibi\drivers
*/ */
class DibiOdbcDriver extends DibiObject implements IDibiDriver, IDibiResultDriver, IDibiReflector class OdbcDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
{ {
use Dibi\Strict;
/** @var resource Connection resource */ /** @var resource Connection resource */
private $connection; private $connection;
@@ -38,12 +42,12 @@ class DibiOdbcDriver extends DibiObject implements IDibiDriver, IDibiResultDrive
/** /**
* @throws DibiNotSupportedException * @throws Dibi\NotSupportedException
*/ */
public function __construct() public function __construct()
{ {
if (!extension_loaded('odbc')) { if (!extension_loaded('odbc')) {
throw new DibiNotSupportedException("PHP extension 'odbc' is not loaded."); throw new Dibi\NotSupportedException("PHP extension 'odbc' is not loaded.");
} }
} }
@@ -51,7 +55,7 @@ class DibiOdbcDriver extends DibiObject implements IDibiDriver, IDibiResultDrive
/** /**
* Connects to a database. * Connects to a database.
* @return void * @return void
* @throws DibiException * @throws Dibi\Exception
*/ */
public function connect(array & $config) public function connect(array & $config)
{ {
@@ -59,11 +63,11 @@ class DibiOdbcDriver extends DibiObject implements IDibiDriver, IDibiResultDrive
$this->connection = $config['resource']; $this->connection = $config['resource'];
} else { } else {
// default values // default values
$config += array( $config += [
'username' => ini_get('odbc.default_user'), 'username' => ini_get('odbc.default_user'),
'password' => ini_get('odbc.default_pw'), 'password' => ini_get('odbc.default_pw'),
'dsn' => ini_get('odbc.default_db'), 'dsn' => ini_get('odbc.default_db'),
); ];
if (empty($config['persistent'])) { if (empty($config['persistent'])) {
$this->connection = @odbc_connect($config['dsn'], $config['username'], $config['password']); // intentionally @ $this->connection = @odbc_connect($config['dsn'], $config['username'], $config['password']); // intentionally @
@@ -73,7 +77,7 @@ class DibiOdbcDriver extends DibiObject implements IDibiDriver, IDibiResultDrive
} }
if (!is_resource($this->connection)) { if (!is_resource($this->connection)) {
throw new DibiDriverException(odbc_errormsg() . ' ' . odbc_error()); throw new Dibi\DriverException(odbc_errormsg() . ' ' . odbc_error());
} }
} }
@@ -91,8 +95,8 @@ class DibiOdbcDriver extends DibiObject implements IDibiDriver, IDibiResultDrive
/** /**
* Executes the SQL query. * Executes the SQL query.
* @param string SQL statement. * @param string SQL statement.
* @return IDibiResultDriver|NULL * @return Dibi\ResultDriver|NULL
* @throws DibiDriverException * @throws Dibi\DriverException
*/ */
public function query($sql) public function query($sql)
{ {
@@ -100,7 +104,7 @@ class DibiOdbcDriver extends DibiObject implements IDibiDriver, IDibiResultDrive
$res = @odbc_exec($this->connection, $sql); // intentionally @ $res = @odbc_exec($this->connection, $sql); // intentionally @
if ($res === FALSE) { if ($res === FALSE) {
throw new DibiDriverException(odbc_errormsg($this->connection) . ' ' . odbc_error($this->connection), 0, $sql); throw new Dibi\DriverException(odbc_errormsg($this->connection) . ' ' . odbc_error($this->connection), 0, $sql);
} elseif (is_resource($res)) { } elseif (is_resource($res)) {
$this->affectedRows = odbc_num_rows($res); $this->affectedRows = odbc_num_rows($res);
@@ -125,7 +129,7 @@ class DibiOdbcDriver extends DibiObject implements IDibiDriver, IDibiResultDrive
*/ */
public function getInsertId($sequence) public function getInsertId($sequence)
{ {
throw new DibiNotSupportedException('ODBC does not support autoincrementing.'); throw new Dibi\NotSupportedException('ODBC does not support autoincrementing.');
} }
@@ -133,12 +137,12 @@ class DibiOdbcDriver extends DibiObject implements IDibiDriver, IDibiResultDrive
* Begins a transaction (if supported). * Begins a transaction (if supported).
* @param string optional savepoint name * @param string optional savepoint name
* @return void * @return void
* @throws DibiDriverException * @throws Dibi\DriverException
*/ */
public function begin($savepoint = NULL) public function begin($savepoint = NULL)
{ {
if (!odbc_autocommit($this->connection, FALSE)) { if (!odbc_autocommit($this->connection, FALSE)) {
throw new DibiDriverException(odbc_errormsg($this->connection) . ' ' . odbc_error($this->connection)); throw new Dibi\DriverException(odbc_errormsg($this->connection) . ' ' . odbc_error($this->connection));
} }
} }
@@ -147,12 +151,12 @@ class DibiOdbcDriver extends DibiObject implements IDibiDriver, IDibiResultDrive
* Commits statements in a transaction. * Commits statements in a transaction.
* @param string optional savepoint name * @param string optional savepoint name
* @return void * @return void
* @throws DibiDriverException * @throws Dibi\DriverException
*/ */
public function commit($savepoint = NULL) public function commit($savepoint = NULL)
{ {
if (!odbc_commit($this->connection)) { if (!odbc_commit($this->connection)) {
throw new DibiDriverException(odbc_errormsg($this->connection) . ' ' . odbc_error($this->connection)); throw new Dibi\DriverException(odbc_errormsg($this->connection) . ' ' . odbc_error($this->connection));
} }
odbc_autocommit($this->connection, TRUE); odbc_autocommit($this->connection, TRUE);
} }
@@ -162,12 +166,12 @@ class DibiOdbcDriver extends DibiObject implements IDibiDriver, IDibiResultDrive
* Rollback changes in a transaction. * Rollback changes in a transaction.
* @param string optional savepoint name * @param string optional savepoint name
* @return void * @return void
* @throws DibiDriverException * @throws Dibi\DriverException
*/ */
public function rollback($savepoint = NULL) public function rollback($savepoint = NULL)
{ {
if (!odbc_rollback($this->connection)) { if (!odbc_rollback($this->connection)) {
throw new DibiDriverException(odbc_errormsg($this->connection) . ' ' . odbc_error($this->connection)); throw new Dibi\DriverException(odbc_errormsg($this->connection) . ' ' . odbc_error($this->connection));
} }
odbc_autocommit($this->connection, TRUE); odbc_autocommit($this->connection, TRUE);
} }
@@ -195,7 +199,7 @@ class DibiOdbcDriver extends DibiObject implements IDibiDriver, IDibiResultDrive
/** /**
* Returns the connection reflector. * Returns the connection reflector.
* @return IDibiReflector * @return Dibi\Reflector
*/ */
public function getReflector() public function getReflector()
{ {
@@ -206,7 +210,7 @@ class DibiOdbcDriver extends DibiObject implements IDibiDriver, IDibiResultDrive
/** /**
* Result set driver factory. * Result set driver factory.
* @param resource * @param resource
* @return IDibiResultDriver * @return Dibi\ResultDriver
*/ */
public function createResultDriver($resource) public function createResultDriver($resource)
{ {
@@ -222,33 +226,47 @@ class DibiOdbcDriver extends DibiObject implements IDibiDriver, IDibiResultDrive
/** /**
* Encodes data for use in a SQL statement. * Encodes data for use in a SQL statement.
* @param mixed value * @param mixed value
* @param string type (dibi::TEXT, dibi::BOOL, ...)
* @return string encoded value * @return string encoded value
* @throws InvalidArgumentException
*/ */
public function escape($value, $type) public function escapeText($value)
{ {
switch ($type) { return "'" . str_replace("'", "''", $value) . "'";
case dibi::TEXT: }
case dibi::BINARY:
return "'" . str_replace("'", "''", $value) . "'";
case dibi::IDENTIFIER:
return '[' . str_replace(array('[', ']'), array('[[', ']]'), $value) . ']';
case dibi::BOOL: public function escapeBinary($value)
return $value ? 1 : 0; {
return "'" . str_replace("'", "''", $value) . "'";
}
case dibi::DATE:
case dibi::DATETIME:
if (!$value instanceof DateTime && !$value instanceof DateTimeInterface) {
$value = new DibiDateTime($value);
}
return $value->format($type === dibi::DATETIME ? "#m/d/Y H:i:s#" : "#m/d/Y#");
default: public function escapeIdentifier($value)
throw new InvalidArgumentException('Unsupported type.'); {
return '[' . str_replace(['[', ']'], ['[[', ']]'], $value) . ']';
}
public function escapeBool($value)
{
return $value ? 1 : 0;
}
public function escapeDate($value)
{
if (!$value instanceof \DateTime && !$value instanceof \DateTimeInterface) {
$value = new Dibi\DateTime($value);
} }
return $value->format("#m/d/Y#");
}
public function escapeDateTime($value)
{
if (!$value instanceof \DateTime && !$value instanceof \DateTimeInterface) {
$value = new Dibi\DateTime($value);
}
return $value->format("#m/d/Y H:i:s#");
} }
@@ -260,24 +278,27 @@ class DibiOdbcDriver extends DibiObject implements IDibiDriver, IDibiResultDrive
*/ */
public function escapeLike($value, $pos) public function escapeLike($value, $pos)
{ {
$value = strtr($value, array("'" => "''", '%' => '[%]', '_' => '[_]', '[' => '[[]')); $value = strtr($value, ["'" => "''", '%' => '[%]', '_' => '[_]', '[' => '[[]']);
return ($pos <= 0 ? "'%" : "'") . $value . ($pos >= 0 ? "%'" : "'"); return ($pos <= 0 ? "'%" : "'") . $value . ($pos >= 0 ? "%'" : "'");
} }
/** /**
* Decodes data from result set. * Decodes data from result set.
* @param string value * @param string
* @param string type (dibi::BINARY) * @return string
* @return string decoded value
* @throws InvalidArgumentException
*/ */
public function unescape($value, $type) public function unescapeBinary($value)
{ {
if ($type === dibi::BINARY) { return $value;
return $value; }
}
throw new InvalidArgumentException('Unsupported type.');
/** @deprecated */
public function escape($value, $type)
{
trigger_error(__METHOD__ . '() is deprecated.', E_USER_DEPRECATED);
return Dibi\Helpers::escape($this, $value, $type);
} }
@@ -287,13 +308,14 @@ class DibiOdbcDriver extends DibiObject implements IDibiDriver, IDibiResultDrive
*/ */
public function applyLimit(& $sql, $limit, $offset) public function applyLimit(& $sql, $limit, $offset)
{ {
// offset support is missing
if ($limit >= 0) {
$sql = 'SELECT TOP ' . (int) $limit . ' * FROM (' . $sql . ') t';
}
if ($offset) { if ($offset) {
throw new DibiNotSupportedException('Offset is not implemented in driver odbc.'); throw new Dibi\NotSupportedException('Offset is not supported by this database.');
} elseif ($limit < 0) {
throw new Dibi\NotSupportedException('Negative offset or limit.');
} elseif ($limit !== NULL) {
$sql = 'SELECT TOP ' . (int) $limit . ' * FROM (' . $sql . ') t';
} }
} }
@@ -337,7 +359,7 @@ class DibiOdbcDriver extends DibiObject implements IDibiDriver, IDibiResultDrive
return FALSE; return FALSE;
} }
$count = odbc_num_fields($set); $count = odbc_num_fields($set);
$cols = array(); $cols = [];
for ($i = 1; $i <= $count; $i++) { for ($i = 1; $i <= $count; $i++) {
$cols[] = odbc_result($set, $i); $cols[] = odbc_result($set, $i);
} }
@@ -376,14 +398,14 @@ class DibiOdbcDriver extends DibiObject implements IDibiDriver, IDibiResultDrive
public function getResultColumns() public function getResultColumns()
{ {
$count = odbc_num_fields($this->resultSet); $count = odbc_num_fields($this->resultSet);
$columns = array(); $columns = [];
for ($i = 1; $i <= $count; $i++) { for ($i = 1; $i <= $count; $i++) {
$columns[] = array( $columns[] = [
'name' => odbc_field_name($this->resultSet, $i), 'name' => odbc_field_name($this->resultSet, $i),
'table' => NULL, 'table' => NULL,
'fullname' => odbc_field_name($this->resultSet, $i), 'fullname' => odbc_field_name($this->resultSet, $i),
'nativetype' => odbc_field_type($this->resultSet, $i), 'nativetype' => odbc_field_type($this->resultSet, $i),
); ];
} }
return $columns; return $columns;
} }
@@ -400,7 +422,7 @@ class DibiOdbcDriver extends DibiObject implements IDibiDriver, IDibiResultDrive
} }
/********************* IDibiReflector ****************d*g**/ /********************* Dibi\Reflector ****************d*g**/
/** /**
@@ -410,13 +432,13 @@ class DibiOdbcDriver extends DibiObject implements IDibiDriver, IDibiResultDrive
public function getTables() public function getTables()
{ {
$res = odbc_tables($this->connection); $res = odbc_tables($this->connection);
$tables = array(); $tables = [];
while ($row = odbc_fetch_array($res)) { while ($row = odbc_fetch_array($res)) {
if ($row['TABLE_TYPE'] === 'TABLE' || $row['TABLE_TYPE'] === 'VIEW') { if ($row['TABLE_TYPE'] === 'TABLE' || $row['TABLE_TYPE'] === 'VIEW') {
$tables[] = array( $tables[] = [
'name' => $row['TABLE_NAME'], 'name' => $row['TABLE_NAME'],
'view' => $row['TABLE_TYPE'] === 'VIEW', 'view' => $row['TABLE_TYPE'] === 'VIEW',
); ];
} }
} }
odbc_free_result($res); odbc_free_result($res);
@@ -432,17 +454,17 @@ class DibiOdbcDriver extends DibiObject implements IDibiDriver, IDibiResultDrive
public function getColumns($table) public function getColumns($table)
{ {
$res = odbc_columns($this->connection); $res = odbc_columns($this->connection);
$columns = array(); $columns = [];
while ($row = odbc_fetch_array($res)) { while ($row = odbc_fetch_array($res)) {
if ($row['TABLE_NAME'] === $table) { if ($row['TABLE_NAME'] === $table) {
$columns[] = array( $columns[] = [
'name' => $row['COLUMN_NAME'], 'name' => $row['COLUMN_NAME'],
'table' => $table, 'table' => $table,
'nativetype' => $row['TYPE_NAME'], 'nativetype' => $row['TYPE_NAME'],
'size' => $row['COLUMN_SIZE'], 'size' => $row['COLUMN_SIZE'],
'nullable' => (bool) $row['NULLABLE'], 'nullable' => (bool) $row['NULLABLE'],
'default' => $row['COLUMN_DEF'], 'default' => $row['COLUMN_DEF'],
); ];
} }
} }
odbc_free_result($res); odbc_free_result($res);
@@ -457,7 +479,7 @@ class DibiOdbcDriver extends DibiObject implements IDibiDriver, IDibiResultDrive
*/ */
public function getIndexes($table) public function getIndexes($table)
{ {
throw new DibiNotImplementedException; throw new Dibi\NotImplementedException;
} }
@@ -468,7 +490,7 @@ class DibiOdbcDriver extends DibiObject implements IDibiDriver, IDibiResultDrive
*/ */
public function getForeignKeys($table) public function getForeignKeys($table)
{ {
throw new DibiNotImplementedException; throw new Dibi\NotImplementedException;
} }
} }

View File

@@ -5,6 +5,10 @@
* Copyright (c) 2005 David Grudl (https://davidgrudl.com) * Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/ */
namespace Dibi\Drivers;
use Dibi;
/** /**
* The dibi driver for Oracle database. * The dibi driver for Oracle database.
@@ -19,12 +23,12 @@
* - formatDateTime => how to format datetime in SQL (@see date) * - formatDateTime => how to format datetime in SQL (@see date)
* - resource (resource) => existing connection resource * - resource (resource) => existing connection resource
* - persistent => Creates persistent connections with oci_pconnect instead of oci_new_connect * - persistent => Creates persistent connections with oci_pconnect instead of oci_new_connect
* - lazy, profiler, result, substitutes, ... => see DibiConnection options * - lazy, profiler, result, substitutes, ... => see Dibi\Connection options
*
* @package dibi\drivers
*/ */
class DibiOracleDriver extends DibiObject implements IDibiDriver, IDibiResultDriver, IDibiReflector class OracleDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
{ {
use Dibi\Strict;
/** @var resource Connection resource */ /** @var resource Connection resource */
private $connection; private $connection;
@@ -42,12 +46,12 @@ class DibiOracleDriver extends DibiObject implements IDibiDriver, IDibiResultDri
/** /**
* @throws DibiNotSupportedException * @throws Dibi\NotSupportedException
*/ */
public function __construct() public function __construct()
{ {
if (!extension_loaded('oci8')) { if (!extension_loaded('oci8')) {
throw new DibiNotSupportedException("PHP extension 'oci8' is not loaded."); throw new Dibi\NotSupportedException("PHP extension 'oci8' is not loaded.");
} }
} }
@@ -55,7 +59,7 @@ class DibiOracleDriver extends DibiObject implements IDibiDriver, IDibiResultDri
/** /**
* Connects to a database. * Connects to a database.
* @return void * @return void
* @throws DibiException * @throws Dibi\Exception
*/ */
public function connect(array & $config) public function connect(array & $config)
{ {
@@ -73,7 +77,7 @@ class DibiOracleDriver extends DibiObject implements IDibiDriver, IDibiResultDri
if (!$this->connection) { if (!$this->connection) {
$err = oci_error(); $err = oci_error();
throw new DibiDriverException($err['message'], $err['code']); throw new Dibi\DriverException($err['message'], $err['code']);
} }
if (isset($config['schema'])) { if (isset($config['schema'])) {
@@ -95,8 +99,8 @@ class DibiOracleDriver extends DibiObject implements IDibiDriver, IDibiResultDri
/** /**
* Executes the SQL query. * Executes the SQL query.
* @param string SQL statement. * @param string SQL statement.
* @return IDibiResultDriver|NULL * @return Dibi\ResultDriver|NULL
* @throws DibiDriverException * @throws Dibi\DriverException
*/ */
public function query($sql) public function query($sql)
{ {
@@ -105,14 +109,34 @@ class DibiOracleDriver extends DibiObject implements IDibiDriver, IDibiResultDri
@oci_execute($res, $this->autocommit ? OCI_COMMIT_ON_SUCCESS : OCI_DEFAULT); @oci_execute($res, $this->autocommit ? OCI_COMMIT_ON_SUCCESS : OCI_DEFAULT);
$err = oci_error($res); $err = oci_error($res);
if ($err) { if ($err) {
throw new DibiDriverException($err['message'], $err['code'], $sql); throw self::createException($err['message'], $err['code'], $sql);
} elseif (is_resource($res)) { } elseif (is_resource($res)) {
return $this->createResultDriver($res); return $this->createResultDriver($res);
} }
} else { } else {
$err = oci_error($this->connection); $err = oci_error($this->connection);
throw new DibiDriverException($err['message'], $err['code'], $sql); throw new Dibi\DriverException($err['message'], $err['code'], $sql);
}
}
/**
* @return Dibi\DriverException
*/
public static function createException($message, $code, $sql)
{
if (in_array($code, [1, 2299, 38911], TRUE)) {
return new Dibi\UniqueConstraintViolationException($message, $code, $sql);
} elseif (in_array($code, [1400], TRUE)) {
return new Dibi\NotNullConstraintViolationException($message, $code, $sql);
} elseif (in_array($code, [2266, 2291, 2292], TRUE)) {
return new Dibi\ForeignKeyConstraintViolationException($message, $code, $sql);
} else {
return new Dibi\DriverException($message, $code, $sql);
} }
} }
@@ -123,7 +147,7 @@ class DibiOracleDriver extends DibiObject implements IDibiDriver, IDibiResultDri
*/ */
public function getAffectedRows() public function getAffectedRows()
{ {
throw new DibiNotImplementedException; throw new Dibi\NotImplementedException;
} }
@@ -153,13 +177,13 @@ class DibiOracleDriver extends DibiObject implements IDibiDriver, IDibiResultDri
* Commits statements in a transaction. * Commits statements in a transaction.
* @param string optional savepoint name * @param string optional savepoint name
* @return void * @return void
* @throws DibiDriverException * @throws Dibi\DriverException
*/ */
public function commit($savepoint = NULL) public function commit($savepoint = NULL)
{ {
if (!oci_commit($this->connection)) { if (!oci_commit($this->connection)) {
$err = oci_error($this->connection); $err = oci_error($this->connection);
throw new DibiDriverException($err['message'], $err['code']); throw new Dibi\DriverException($err['message'], $err['code']);
} }
$this->autocommit = TRUE; $this->autocommit = TRUE;
} }
@@ -169,13 +193,13 @@ class DibiOracleDriver extends DibiObject implements IDibiDriver, IDibiResultDri
* Rollback changes in a transaction. * Rollback changes in a transaction.
* @param string optional savepoint name * @param string optional savepoint name
* @return void * @return void
* @throws DibiDriverException * @throws Dibi\DriverException
*/ */
public function rollback($savepoint = NULL) public function rollback($savepoint = NULL)
{ {
if (!oci_rollback($this->connection)) { if (!oci_rollback($this->connection)) {
$err = oci_error($this->connection); $err = oci_error($this->connection);
throw new DibiDriverException($err['message'], $err['code']); throw new Dibi\DriverException($err['message'], $err['code']);
} }
$this->autocommit = TRUE; $this->autocommit = TRUE;
} }
@@ -193,7 +217,7 @@ class DibiOracleDriver extends DibiObject implements IDibiDriver, IDibiResultDri
/** /**
* Returns the connection reflector. * Returns the connection reflector.
* @return IDibiReflector * @return Dibi\Reflector
*/ */
public function getReflector() public function getReflector()
{ {
@@ -204,7 +228,7 @@ class DibiOracleDriver extends DibiObject implements IDibiDriver, IDibiResultDri
/** /**
* Result set driver factory. * Result set driver factory.
* @param resource * @param resource
* @return IDibiResultDriver * @return Dibi\ResultDriver
*/ */
public function createResultDriver($resource) public function createResultDriver($resource)
{ {
@@ -220,34 +244,48 @@ class DibiOracleDriver extends DibiObject implements IDibiDriver, IDibiResultDri
/** /**
* Encodes data for use in a SQL statement. * Encodes data for use in a SQL statement.
* @param mixed value * @param mixed value
* @param string type (dibi::TEXT, dibi::BOOL, ...)
* @return string encoded value * @return string encoded value
* @throws InvalidArgumentException
*/ */
public function escape($value, $type) public function escapeText($value)
{ {
switch ($type) { return "'" . str_replace("'", "''", $value) . "'"; // TODO: not tested
case dibi::TEXT: }
case dibi::BINARY:
return "'" . str_replace("'", "''", $value) . "'"; // TODO: not tested
case dibi::IDENTIFIER:
// @see http://download.oracle.com/docs/cd/B10500_01/server.920/a96540/sql_elements9a.htm
return '"' . str_replace('"', '""', $value) . '"';
case dibi::BOOL: public function escapeBinary($value)
return $value ? 1 : 0; {
return "'" . str_replace("'", "''", $value) . "'"; // TODO: not tested
}
case dibi::DATE:
case dibi::DATETIME:
if (!$value instanceof DateTime && !$value instanceof DateTimeInterface) {
$value = new DibiDateTime($value);
}
return $value->format($type === dibi::DATETIME ? $this->fmtDateTime : $this->fmtDate);
default: public function escapeIdentifier($value)
throw new InvalidArgumentException('Unsupported type.'); {
// @see http://download.oracle.com/docs/cd/B10500_01/server.920/a96540/sql_elements9a.htm
return '"' . str_replace('"', '""', $value) . '"';
}
public function escapeBool($value)
{
return $value ? 1 : 0;
}
public function escapeDate($value)
{
if (!$value instanceof \DateTime && !$value instanceof \DateTimeInterface) {
$value = new Dibi\DateTime($value);
} }
return $value->format($this->fmtDate);
}
public function escapeDateTime($value)
{
if (!$value instanceof \DateTime && !$value instanceof \DateTimeInterface) {
$value = new Dibi\DateTime($value);
}
return $value->format($this->fmtDateTime);
} }
@@ -267,17 +305,20 @@ class DibiOracleDriver extends DibiObject implements IDibiDriver, IDibiResultDri
/** /**
* Decodes data from result set. * Decodes data from result set.
* @param string value * @param string
* @param string type (dibi::BINARY) * @return string
* @return string decoded value
* @throws InvalidArgumentException
*/ */
public function unescape($value, $type) public function unescapeBinary($value)
{ {
if ($type === dibi::BINARY) { return $value;
return $value; }
}
throw new InvalidArgumentException('Unsupported type.');
/** @deprecated */
public function escape($value, $type)
{
trigger_error(__METHOD__ . '() is deprecated.', E_USER_DEPRECATED);
return Dibi\Helpers::escape($this, $value, $type);
} }
@@ -287,13 +328,16 @@ class DibiOracleDriver extends DibiObject implements IDibiDriver, IDibiResultDri
*/ */
public function applyLimit(& $sql, $limit, $offset) public function applyLimit(& $sql, $limit, $offset)
{ {
if ($offset > 0) { if ($limit < 0 || $offset < 0) {
throw new Dibi\NotSupportedException('Negative offset or limit.');
} elseif ($offset) {
// see http://www.oracle.com/technology/oramag/oracle/06-sep/o56asktom.html // see http://www.oracle.com/technology/oramag/oracle/06-sep/o56asktom.html
$sql = 'SELECT * FROM (SELECT t.*, ROWNUM AS "__rnum" FROM (' . $sql . ') t ' $sql = 'SELECT * FROM (SELECT t.*, ROWNUM AS "__rnum" FROM (' . $sql . ') t '
. ($limit >= 0 ? 'WHERE ROWNUM <= ' . ((int) $offset + (int) $limit) : '') . ($limit !== NULL ? 'WHERE ROWNUM <= ' . ((int) $offset + (int) $limit) : '')
. ') WHERE "__rnum" > '. (int) $offset; . ') WHERE "__rnum" > '. (int) $offset;
} elseif ($limit >= 0) { } elseif ($limit !== NULL) {
$sql = 'SELECT * FROM (' . $sql . ') WHERE ROWNUM <= ' . (int) $limit; $sql = 'SELECT * FROM (' . $sql . ') WHERE ROWNUM <= ' . (int) $limit;
} }
} }
@@ -318,7 +362,7 @@ class DibiOracleDriver extends DibiObject implements IDibiDriver, IDibiResultDri
*/ */
public function getRowCount() public function getRowCount()
{ {
throw new DibiNotSupportedException('Row count is not available for unbuffered queries.'); throw new Dibi\NotSupportedException('Row count is not available for unbuffered queries.');
} }
@@ -340,7 +384,7 @@ class DibiOracleDriver extends DibiObject implements IDibiDriver, IDibiResultDri
*/ */
public function seek($row) public function seek($row)
{ {
throw new DibiNotImplementedException; throw new Dibi\NotImplementedException;
} }
@@ -362,15 +406,15 @@ class DibiOracleDriver extends DibiObject implements IDibiDriver, IDibiResultDri
public function getResultColumns() public function getResultColumns()
{ {
$count = oci_num_fields($this->resultSet); $count = oci_num_fields($this->resultSet);
$columns = array(); $columns = [];
for ($i = 1; $i <= $count; $i++) { for ($i = 1; $i <= $count; $i++) {
$type = oci_field_type($this->resultSet, $i); $type = oci_field_type($this->resultSet, $i);
$columns[] = array( $columns[] = [
'name' => oci_field_name($this->resultSet, $i), 'name' => oci_field_name($this->resultSet, $i),
'table' => NULL, 'table' => NULL,
'fullname' => oci_field_name($this->resultSet, $i), 'fullname' => oci_field_name($this->resultSet, $i),
'nativetype' => $type === 'NUMBER' && oci_field_scale($this->resultSet, $i) === 0 ? 'INTEGER' : $type, 'nativetype' => $type === 'NUMBER' && oci_field_scale($this->resultSet, $i) === 0 ? 'INTEGER' : $type,
); ];
} }
return $columns; return $columns;
} }
@@ -387,7 +431,7 @@ class DibiOracleDriver extends DibiObject implements IDibiDriver, IDibiResultDri
} }
/********************* IDibiReflector ****************d*g**/ /********************* Dibi\Reflector ****************d*g**/
/** /**
@@ -397,13 +441,13 @@ class DibiOracleDriver extends DibiObject implements IDibiDriver, IDibiResultDri
public function getTables() public function getTables()
{ {
$res = $this->query('SELECT * FROM cat'); $res = $this->query('SELECT * FROM cat');
$tables = array(); $tables = [];
while ($row = $res->fetch(FALSE)) { while ($row = $res->fetch(FALSE)) {
if ($row[1] === 'TABLE' || $row[1] === 'VIEW') { if ($row[1] === 'TABLE' || $row[1] === 'VIEW') {
$tables[] = array( $tables[] = [
'name' => $row[0], 'name' => $row[0],
'view' => $row[1] === 'VIEW', 'view' => $row[1] === 'VIEW',
); ];
} }
} }
return $tables; return $tables;
@@ -417,7 +461,7 @@ class DibiOracleDriver extends DibiObject implements IDibiDriver, IDibiResultDri
*/ */
public function getColumns($table) public function getColumns($table)
{ {
throw new DibiNotImplementedException; throw new Dibi\NotImplementedException;
} }
@@ -428,7 +472,7 @@ class DibiOracleDriver extends DibiObject implements IDibiDriver, IDibiResultDri
*/ */
public function getIndexes($table) public function getIndexes($table)
{ {
throw new DibiNotImplementedException; throw new Dibi\NotImplementedException;
} }
@@ -439,7 +483,7 @@ class DibiOracleDriver extends DibiObject implements IDibiDriver, IDibiResultDri
*/ */
public function getForeignKeys($table) public function getForeignKeys($table)
{ {
throw new DibiNotImplementedException; throw new Dibi\NotImplementedException;
} }
} }

View File

@@ -5,9 +5,10 @@
* Copyright (c) 2005 David Grudl (https://davidgrudl.com) * Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/ */
namespace Dibi\Drivers;
require_once dirname(__FILE__) . '/DibiMySqlReflector.php'; use Dibi;
require_once dirname(__FILE__) . '/DibiSqliteReflector.php'; use PDO;
/** /**
@@ -20,16 +21,16 @@ require_once dirname(__FILE__) . '/DibiSqliteReflector.php';
* - options (array) => driver specific options {@see PDO::__construct} * - options (array) => driver specific options {@see PDO::__construct}
* - resource (PDO) => existing connection * - resource (PDO) => existing connection
* - version * - version
* - lazy, profiler, result, substitutes, ... => see DibiConnection options * - lazy, profiler, result, substitutes, ... => see Dibi\Connection options
*
* @package dibi\drivers
*/ */
class DibiPdoDriver extends DibiObject implements IDibiDriver, IDibiResultDriver class PdoDriver implements Dibi\Driver, Dibi\ResultDriver
{ {
use Dibi\Strict;
/** @var PDO Connection resource */ /** @var PDO Connection resource */
private $connection; private $connection;
/** @var PDOStatement Resultset resource */ /** @var \PDOStatement Resultset resource */
private $resultSet; private $resultSet;
/** @var int|FALSE Affected rows */ /** @var int|FALSE Affected rows */
@@ -43,12 +44,12 @@ class DibiPdoDriver extends DibiObject implements IDibiDriver, IDibiResultDriver
/** /**
* @throws DibiNotSupportedException * @throws Dibi\NotSupportedException
*/ */
public function __construct() public function __construct()
{ {
if (!extension_loaded('pdo')) { if (!extension_loaded('pdo')) {
throw new DibiNotSupportedException("PHP extension 'pdo' is not loaded."); throw new Dibi\NotSupportedException("PHP extension 'pdo' is not loaded.");
} }
} }
@@ -56,25 +57,25 @@ class DibiPdoDriver extends DibiObject implements IDibiDriver, IDibiResultDriver
/** /**
* Connects to a database. * Connects to a database.
* @return void * @return void
* @throws DibiException * @throws Dibi\Exception
*/ */
public function connect(array & $config) public function connect(array & $config)
{ {
$foo = & $config['dsn']; $foo = & $config['dsn'];
$foo = & $config['options']; $foo = & $config['options'];
DibiConnection::alias($config, 'resource', 'pdo'); Dibi\Helpers::alias($config, 'resource', 'pdo');
if ($config['resource'] instanceof PDO) { if ($config['resource'] instanceof PDO) {
$this->connection = $config['resource']; $this->connection = $config['resource'];
unset($config['resource'], $config['pdo']);
} else { } else {
try { try {
$this->connection = new PDO($config['dsn'], $config['username'], $config['password'], $config['options']); $this->connection = new PDO($config['dsn'], $config['username'], $config['password'], $config['options']);
} catch (PDOException $e) { } catch (\PDOException $e) {
if ($e->getMessage() === 'could not find driver') { if ($e->getMessage() === 'could not find driver') {
throw new DibiNotSupportedException('PHP extension for PDO is not loaded.'); throw new Dibi\NotSupportedException('PHP extension for PDO is not loaded.');
} }
throw new DibiDriverException($e->getMessage(), $e->getCode()); throw new Dibi\DriverException($e->getMessage(), $e->getCode());
} }
} }
@@ -98,34 +99,46 @@ class DibiPdoDriver extends DibiObject implements IDibiDriver, IDibiResultDriver
/** /**
* Executes the SQL query. * Executes the SQL query.
* @param string SQL statement. * @param string SQL statement.
* @return IDibiResultDriver|NULL * @return Dibi\ResultDriver|NULL
* @throws DibiDriverException * @throws Dibi\DriverException
*/ */
public function query($sql) public function query($sql)
{ {
// must detect if SQL returns result set or num of affected rows // must detect if SQL returns result set or num of affected rows
$cmd = strtoupper(substr(ltrim($sql), 0, 6)); $cmd = strtoupper(substr(ltrim($sql), 0, 6));
static $list = array('UPDATE' => 1, 'DELETE' => 1, 'INSERT' => 1, 'REPLAC' => 1); static $list = ['UPDATE' => 1, 'DELETE' => 1, 'INSERT' => 1, 'REPLAC' => 1];
$this->affectedRows = FALSE; $this->affectedRows = FALSE;
if (isset($list[$cmd])) { if (isset($list[$cmd])) {
$this->affectedRows = $this->connection->exec($sql); $this->affectedRows = $this->connection->exec($sql);
if ($this->affectedRows !== FALSE) {
if ($this->affectedRows === FALSE) { return;
$err = $this->connection->errorInfo();
throw new DibiDriverException("SQLSTATE[$err[0]]: $err[2]", $err[1], $sql);
} }
} else { } else {
$res = $this->connection->query($sql); $res = $this->connection->query($sql);
if ($res) {
if ($res === FALSE) {
$err = $this->connection->errorInfo();
throw new DibiDriverException("SQLSTATE[$err[0]]: $err[2]", $err[1], $sql);
} else {
return $this->createResultDriver($res); return $this->createResultDriver($res);
} }
} }
list($sqlState, $code, $message) = $this->connection->errorInfo();
$message = "SQLSTATE[$sqlState]: $message";
switch ($this->driverName) {
case 'mysql':
throw MySqliDriver::createException($message, $code, $sql);
case 'oci':
throw OracleDriver::createException($message, $code, $sql);
case 'pgsql':
throw PostgreDriver::createException($message, $sqlState, $sql);
case 'sqlite':
throw Sqlite3Driver::createException($message, $code, $sql);
default:
throw new Dibi\DriverException($message, $code, $sql);
}
} }
@@ -153,13 +166,13 @@ class DibiPdoDriver extends DibiObject implements IDibiDriver, IDibiResultDriver
* Begins a transaction (if supported). * Begins a transaction (if supported).
* @param string optional savepoint name * @param string optional savepoint name
* @return void * @return void
* @throws DibiDriverException * @throws Dibi\DriverException
*/ */
public function begin($savepoint = NULL) public function begin($savepoint = NULL)
{ {
if (!$this->connection->beginTransaction()) { if (!$this->connection->beginTransaction()) {
$err = $this->connection->errorInfo(); $err = $this->connection->errorInfo();
throw new DibiDriverException("SQLSTATE[$err[0]]: $err[2]", $err[1]); throw new Dibi\DriverException("SQLSTATE[$err[0]]: $err[2]", $err[1]);
} }
} }
@@ -168,13 +181,13 @@ class DibiPdoDriver extends DibiObject implements IDibiDriver, IDibiResultDriver
* Commits statements in a transaction. * Commits statements in a transaction.
* @param string optional savepoint name * @param string optional savepoint name
* @return void * @return void
* @throws DibiDriverException * @throws Dibi\DriverException
*/ */
public function commit($savepoint = NULL) public function commit($savepoint = NULL)
{ {
if (!$this->connection->commit()) { if (!$this->connection->commit()) {
$err = $this->connection->errorInfo(); $err = $this->connection->errorInfo();
throw new DibiDriverException("SQLSTATE[$err[0]]: $err[2]", $err[1]); throw new Dibi\DriverException("SQLSTATE[$err[0]]: $err[2]", $err[1]);
} }
} }
@@ -183,13 +196,13 @@ class DibiPdoDriver extends DibiObject implements IDibiDriver, IDibiResultDriver
* Rollback changes in a transaction. * Rollback changes in a transaction.
* @param string optional savepoint name * @param string optional savepoint name
* @return void * @return void
* @throws DibiDriverException * @throws Dibi\DriverException
*/ */
public function rollback($savepoint = NULL) public function rollback($savepoint = NULL)
{ {
if (!$this->connection->rollBack()) { if (!$this->connection->rollBack()) {
$err = $this->connection->errorInfo(); $err = $this->connection->errorInfo();
throw new DibiDriverException("SQLSTATE[$err[0]]: $err[2]", $err[1]); throw new Dibi\DriverException("SQLSTATE[$err[0]]: $err[2]", $err[1]);
} }
} }
@@ -206,30 +219,29 @@ class DibiPdoDriver extends DibiObject implements IDibiDriver, IDibiResultDriver
/** /**
* Returns the connection reflector. * Returns the connection reflector.
* @return IDibiReflector * @return Dibi\Reflector
*/ */
public function getReflector() public function getReflector()
{ {
switch ($this->driverName) { switch ($this->driverName) {
case 'mysql': case 'mysql':
return new DibiMySqlReflector($this); return new MySqlReflector($this);
case 'sqlite': case 'sqlite':
case 'sqlite2': return new SqliteReflector($this);
return new DibiSqliteReflector($this);
default: default:
throw new DibiNotSupportedException; throw new Dibi\NotSupportedException;
} }
} }
/** /**
* Result set driver factory. * Result set driver factory.
* @param PDOStatement * @param \PDOStatement
* @return IDibiResultDriver * @return Dibi\ResultDriver
*/ */
public function createResultDriver(PDOStatement $resource) public function createResultDriver(\PDOStatement $resource)
{ {
$res = clone $this; $res = clone $this;
$res->resultSet = $resource; $res->resultSet = $resource;
@@ -243,70 +255,83 @@ class DibiPdoDriver extends DibiObject implements IDibiDriver, IDibiResultDriver
/** /**
* Encodes data for use in a SQL statement. * Encodes data for use in a SQL statement.
* @param mixed value * @param mixed value
* @param string type (dibi::TEXT, dibi::BOOL, ...)
* @return string encoded value * @return string encoded value
* @throws InvalidArgumentException
*/ */
public function escape($value, $type) public function escapeText($value)
{ {
switch ($type) { if ($this->driverName === 'odbc') {
case dibi::TEXT: return "'" . str_replace("'", "''", $value) . "'";
case dibi::BINARY: } else {
if ($this->driverName === 'odbc') { return $this->connection->quote($value, PDO::PARAM_STR);
return "'" . str_replace("'", "''", $value) . "'"; }
} else { }
return $this->connection->quote($value, $type === dibi::TEXT ? PDO::PARAM_STR : PDO::PARAM_LOB);
}
case dibi::IDENTIFIER:
switch ($this->driverName) {
case 'mysql':
return '`' . str_replace('`', '``', $value) . '`';
case 'oci': public function escapeBinary($value)
case 'pgsql': {
return '"' . str_replace('"', '""', $value) . '"'; if ($this->driverName === 'odbc') {
return "'" . str_replace("'", "''", $value) . "'";
} else {
return $this->connection->quote($value, PDO::PARAM_LOB);
}
}
case 'sqlite':
case 'sqlite2':
return '[' . strtr($value, '[]', ' ') . ']';
case 'odbc': public function escapeIdentifier($value)
case 'mssql': {
return '[' . str_replace(array('[', ']'), array('[[', ']]'), $value) . ']'; switch ($this->driverName) {
case 'mysql':
return '`' . str_replace('`', '``', $value) . '`';
case 'dblib': case 'oci':
case 'sqlsrv': case 'pgsql':
return '[' . str_replace(']', ']]', $value) . ']'; return '"' . str_replace('"', '""', $value) . '"';
default: case 'sqlite':
return $value; return '[' . strtr($value, '[]', ' ') . ']';
}
case dibi::BOOL: case 'odbc':
if ($this->driverName === 'pgsql') { case 'mssql':
return $value ? 'TRUE' : 'FALSE'; return '[' . str_replace(['[', ']'], ['[[', ']]'], $value) . ']';
} else {
return $value ? 1 : 0;
}
case dibi::DATE: case 'dblib':
case dibi::DATETIME: case 'sqlsrv':
if (!$value instanceof DateTime && !$value instanceof DateTimeInterface) { return '[' . str_replace(']', ']]', $value) . ']';
$value = new DibiDateTime($value);
}
if ($this->driverName === 'odbc') {
return $value->format($type === dibi::DATETIME ? '#m/d/Y H:i:s#' : '#m/d/Y#');
} else {
return $value->format($type === dibi::DATETIME ? "'Y-m-d H:i:s'" : "'Y-m-d'");
}
default: default:
throw new InvalidArgumentException('Unsupported type.'); return $value;
} }
} }
public function escapeBool($value)
{
if ($this->driverName === 'pgsql') {
return $value ? 'TRUE' : 'FALSE';
} else {
return $value ? 1 : 0;
}
}
public function escapeDate($value)
{
if (!$value instanceof \DateTime && !$value instanceof \DateTimeInterface) {
$value = new Dibi\DateTime($value);
}
return $value->format($this->driverName === 'odbc' ? '#m/d/Y#' : "'Y-m-d'");
}
public function escapeDateTime($value)
{
if (!$value instanceof \DateTime && !$value instanceof \DateTimeInterface) {
$value = new Dibi\DateTime($value);
}
return $value->format($this->driverName === 'odbc' ? "#m/d/Y H:i:s#" : "'Y-m-d H:i:s'");
}
/** /**
* Encodes string for use in a LIKE statement. * Encodes string for use in a LIKE statement.
* @param string * @param string
@@ -328,11 +353,10 @@ class DibiPdoDriver extends DibiObject implements IDibiDriver, IDibiResultDriver
case 'pgsql': case 'pgsql':
$bs = substr($this->connection->quote('\\', PDO::PARAM_STR), 1, -1); // standard_conforming_strings = on/off $bs = substr($this->connection->quote('\\', PDO::PARAM_STR), 1, -1); // standard_conforming_strings = on/off
$value = substr($this->connection->quote($value, PDO::PARAM_STR), 1, -1); $value = substr($this->connection->quote($value, PDO::PARAM_STR), 1, -1);
$value = strtr($value, array('%' => $bs . '%', '_' => $bs . '_', '\\' => '\\\\')); $value = strtr($value, ['%' => $bs . '%', '_' => $bs . '_', '\\' => '\\\\']);
return ($pos <= 0 ? "'%" : "'") . $value . ($pos >= 0 ? "%'" : "'"); return ($pos <= 0 ? "'%" : "'") . $value . ($pos >= 0 ? "%'" : "'");
case 'sqlite': case 'sqlite':
case 'sqlite2':
$value = addcslashes(substr($this->connection->quote($value, PDO::PARAM_STR), 1, -1), '%_\\'); $value = addcslashes(substr($this->connection->quote($value, PDO::PARAM_STR), 1, -1), '%_\\');
return ($pos <= 0 ? "'%" : "'") . $value . ($pos >= 0 ? "%'" : "'") . " ESCAPE '\\'"; return ($pos <= 0 ? "'%" : "'") . $value . ($pos >= 0 ? "%'" : "'") . " ESCAPE '\\'";
@@ -340,28 +364,31 @@ class DibiPdoDriver extends DibiObject implements IDibiDriver, IDibiResultDriver
case 'mssql': case 'mssql':
case 'dblib': case 'dblib':
case 'sqlsrv': case 'sqlsrv':
$value = strtr($value, array("'" => "''", '%' => '[%]', '_' => '[_]', '[' => '[[]')); $value = strtr($value, ["'" => "''", '%' => '[%]', '_' => '[_]', '[' => '[[]']);
return ($pos <= 0 ? "'%" : "'") . $value . ($pos >= 0 ? "%'" : "'"); return ($pos <= 0 ? "'%" : "'") . $value . ($pos >= 0 ? "%'" : "'");
default: default:
throw new DibiNotImplementedException; throw new Dibi\NotImplementedException;
} }
} }
/** /**
* Decodes data from result set. * Decodes data from result set.
* @param string value * @param string
* @param string type (dibi::BINARY) * @return string
* @return string decoded value
* @throws InvalidArgumentException
*/ */
public function unescape($value, $type) public function unescapeBinary($value)
{ {
if ($type === dibi::BINARY) { return $value;
return $value; }
}
throw new InvalidArgumentException('Unsupported type.');
/** @deprecated */
public function escape($value, $type)
{
trigger_error(__METHOD__ . '() is deprecated.', E_USER_DEPRECATED);
return Dibi\Helpers::escape($this, $value, $type);
} }
@@ -371,36 +398,43 @@ class DibiPdoDriver extends DibiObject implements IDibiDriver, IDibiResultDriver
*/ */
public function applyLimit(& $sql, $limit, $offset) public function applyLimit(& $sql, $limit, $offset)
{ {
if ($limit < 0 && $offset < 1) { if ($limit < 0 || $offset < 0) {
return; throw new Dibi\NotSupportedException('Negative offset or limit.');
} }
switch ($this->driverName) { switch ($this->driverName) {
case 'mysql': case 'mysql':
$sql .= ' LIMIT ' . ($limit < 0 ? '18446744073709551615' : (int) $limit) if ($limit !== NULL || $offset) {
. ($offset > 0 ? ' OFFSET ' . (int) $offset : ''); // see http://dev.mysql.com/doc/refman/5.0/en/select.html
$sql .= ' LIMIT ' . ($limit === NULL ? '18446744073709551615' : (int) $limit)
. ($offset ? ' OFFSET ' . (int) $offset : '');
}
break; break;
case 'pgsql': case 'pgsql':
if ($limit >= 0) { if ($limit !== NULL) {
$sql .= ' LIMIT ' . (int) $limit; $sql .= ' LIMIT ' . (int) $limit;
} }
if ($offset > 0) { if ($offset) {
$sql .= ' OFFSET ' . (int) $offset; $sql .= ' OFFSET ' . (int) $offset;
} }
break; break;
case 'sqlite': case 'sqlite':
case 'sqlite2': if ($limit !== NULL || $offset) {
$sql .= ' LIMIT ' . $limit . ($offset > 0 ? ' OFFSET ' . (int) $offset : ''); $sql .= ' LIMIT ' . ($limit === NULL ? '-1' : (int) $limit)
. ($offset ? ' OFFSET ' . (int) $offset : '');
}
break; break;
case 'oci': case 'oci':
if ($offset > 0) { if ($offset) {
// see http://www.oracle.com/technology/oramag/oracle/06-sep/o56asktom.html
$sql = 'SELECT * FROM (SELECT t.*, ROWNUM AS "__rnum" FROM (' . $sql . ') t ' $sql = 'SELECT * FROM (SELECT t.*, ROWNUM AS "__rnum" FROM (' . $sql . ') t '
. ($limit >= 0 ? 'WHERE ROWNUM <= ' . ((int) $offset + (int) $limit) : '') . ($limit !== NULL ? 'WHERE ROWNUM <= ' . ((int) $offset + (int) $limit) : '')
. ') WHERE "__rnum" > '. (int) $offset; . ') WHERE "__rnum" > '. (int) $offset;
} elseif ($limit >= 0) {
} elseif ($limit !== NULL) {
$sql = 'SELECT * FROM (' . $sql . ') WHERE ROWNUM <= ' . (int) $limit; $sql = 'SELECT * FROM (' . $sql . ') WHERE ROWNUM <= ' . (int) $limit;
} }
break; break;
@@ -408,24 +442,29 @@ class DibiPdoDriver extends DibiObject implements IDibiDriver, IDibiResultDriver
case 'mssql': case 'mssql':
case 'sqlsrv': case 'sqlsrv':
case 'dblib': case 'dblib':
if (version_compare($this->serverVersion, '11.0') >= 0) { if (version_compare($this->serverVersion, '11.0') >= 0) { // 11 == SQL Server 2012
if ($offset >= 0 || $limit >= 0) { // requires ORDER BY, see https://technet.microsoft.com/en-us/library/gg699618(v=sql.110).aspx
$sql .= ' OFFSET ' . (int) $offset . ' ROWS' if ($limit !== NULL) {
. ($limit > 0 ? ' FETCH NEXT ' . (int) $limit . ' ROWS ONLY' : ''); $sql = sprintf('%s OFFSET %d ROWS FETCH NEXT %d ROWS ONLY', rtrim($sql), $offset, $limit);
} elseif ($offset) {
$sql = sprintf('%s OFFSET %d ROWS', rtrim($sql), $offset);
} }
break; break;
} }
// intentionally break omitted // intentionally break omitted
case 'odbc': case 'odbc':
if ($offset < 1) { if ($offset) {
throw new Dibi\NotSupportedException('Offset is not supported by this database.');
} elseif ($limit !== NULL) {
$sql = 'SELECT TOP ' . (int) $limit . ' * FROM (' . $sql . ') t'; $sql = 'SELECT TOP ' . (int) $limit . ' * FROM (' . $sql . ') t';
break; break;
} }
// intentionally break omitted // intentionally break omitted
default: default:
throw new DibiNotSupportedException('PDO or driver does not support applying limit or offset.'); throw new Dibi\NotSupportedException('PDO or driver does not support applying limit or offset.');
} }
} }
@@ -461,7 +500,7 @@ class DibiPdoDriver extends DibiObject implements IDibiDriver, IDibiResultDriver
*/ */
public function seek($row) public function seek($row)
{ {
throw new DibiNotSupportedException('Cannot seek an unbuffered result set.'); throw new Dibi\NotSupportedException('Cannot seek an unbuffered result set.');
} }
@@ -478,31 +517,30 @@ class DibiPdoDriver extends DibiObject implements IDibiDriver, IDibiResultDriver
/** /**
* Returns metadata for all columns in a result set. * Returns metadata for all columns in a result set.
* @return array * @return array
* @throws DibiException * @throws Dibi\Exception
*/ */
public function getResultColumns() public function getResultColumns()
{ {
$count = $this->resultSet->columnCount(); $count = $this->resultSet->columnCount();
$columns = array(); $columns = [];
for ($i = 0; $i < $count; $i++) { for ($i = 0; $i < $count; $i++) {
$row = @$this->resultSet->getColumnMeta($i); // intentionally @ $row = @$this->resultSet->getColumnMeta($i); // intentionally @
if ($row === FALSE) { if ($row === FALSE) {
throw new DibiNotSupportedException('Driver does not support meta data.'); throw new Dibi\NotSupportedException('Driver does not support meta data.');
} }
// PHP < 5.2.3 compatibility $row = $row + [
// @see: http://php.net/manual/en/pdostatement.getcolumnmeta.php#pdostatement.getcolumnmeta.changelog
$row = $row + array(
'table' => NULL, 'table' => NULL,
'native_type' => 'VAR_STRING', 'native_type' => 'VAR_STRING',
); ];
$columns[] = array( $columns[] = [
'name' => $row['name'], 'name' => $row['name'],
'table' => $row['table'], 'table' => $row['table'],
'nativetype' => $row['native_type'], 'nativetype' => $row['native_type'],
'type' => $row['native_type'] === 'TIME' && $this->driverName === 'mysql' ? Dibi\Type::TIME_INTERVAL : NULL,
'fullname' => $row['table'] ? $row['table'] . '.' . $row['name'] : $row['name'], 'fullname' => $row['table'] ? $row['table'] . '.' . $row['name'] : $row['name'],
'vendor' => $row, 'vendor' => $row,
); ];
} }
return $columns; return $columns;
} }
@@ -510,7 +548,7 @@ class DibiPdoDriver extends DibiObject implements IDibiDriver, IDibiResultDriver
/** /**
* Returns the result set resource. * Returns the result set resource.
* @return PDOStatement * @return \PDOStatement
*/ */
public function getResultResource() public function getResultResource()
{ {

View File

@@ -5,6 +5,10 @@
* Copyright (c) 2005 David Grudl (https://davidgrudl.com) * Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/ */
namespace Dibi\Drivers;
use Dibi;
/** /**
* The dibi driver for PostgreSQL database. * The dibi driver for PostgreSQL database.
@@ -16,12 +20,12 @@
* - charset => character encoding to set (default is utf8) * - charset => character encoding to set (default is utf8)
* - persistent (bool) => try to find a persistent link? * - persistent (bool) => try to find a persistent link?
* - resource (resource) => existing connection resource * - resource (resource) => existing connection resource
* - lazy, profiler, result, substitutes, ... => see DibiConnection options * - lazy, profiler, result, substitutes, ... => see Dibi\Connection options
*
* @package dibi\drivers
*/ */
class DibiPostgreDriver extends DibiObject implements IDibiDriver, IDibiResultDriver, IDibiReflector class PostgreDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
{ {
use Dibi\Strict;
/** @var resource Connection resource */ /** @var resource Connection resource */
private $connection; private $connection;
@@ -36,12 +40,12 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver, IDibiResultDr
/** /**
* @throws DibiNotSupportedException * @throws Dibi\NotSupportedException
*/ */
public function __construct() public function __construct()
{ {
if (!extension_loaded('pgsql')) { if (!extension_loaded('pgsql')) {
throw new DibiNotSupportedException("PHP extension 'pgsql' is not loaded."); throw new Dibi\NotSupportedException("PHP extension 'pgsql' is not loaded.");
} }
} }
@@ -49,51 +53,50 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver, IDibiResultDr
/** /**
* Connects to a database. * Connects to a database.
* @return void * @return void
* @throws DibiException * @throws Dibi\Exception
*/ */
public function connect(array & $config) public function connect(array & $config)
{ {
$error = NULL;
if (isset($config['resource'])) { if (isset($config['resource'])) {
$this->connection = $config['resource']; $this->connection = $config['resource'];
} else { } else {
$config += array( $config += [
'charset' => 'utf8', 'charset' => 'utf8',
); ];
if (isset($config['string'])) { if (isset($config['string'])) {
$string = $config['string']; $string = $config['string'];
} else { } else {
$string = ''; $string = '';
DibiConnection::alias($config, 'user', 'username'); Dibi\Helpers::alias($config, 'user', 'username');
DibiConnection::alias($config, 'dbname', 'database'); Dibi\Helpers::alias($config, 'dbname', 'database');
foreach (array('host', 'hostaddr', 'port', 'dbname', 'user', 'password', 'connect_timeout', 'options', 'sslmode', 'service') as $key) { foreach (['host', 'hostaddr', 'port', 'dbname', 'user', 'password', 'connect_timeout', 'options', 'sslmode', 'service'] as $key) {
if (isset($config[$key])) { if (isset($config[$key])) {
$string .= $key . '=' . $config[$key] . ' '; $string .= $key . '=' . $config[$key] . ' ';
} }
} }
} }
DibiDriverException::tryError(); set_error_handler(function($severity, $message) use (& $error) {
$error = $message;
});
if (empty($config['persistent'])) { if (empty($config['persistent'])) {
$this->connection = pg_connect($string, PGSQL_CONNECT_FORCE_NEW); $this->connection = pg_connect($string, PGSQL_CONNECT_FORCE_NEW);
} else { } else {
$this->connection = pg_pconnect($string, PGSQL_CONNECT_FORCE_NEW); $this->connection = pg_pconnect($string, PGSQL_CONNECT_FORCE_NEW);
} }
if (DibiDriverException::catchError($msg)) { restore_error_handler();
throw new DibiDriverException($msg, 0);
}
} }
if (!is_resource($this->connection)) { if (!is_resource($this->connection)) {
throw new DibiDriverException('Connecting error.'); throw new Dibi\DriverException($error ?: 'Connecting error.');
} }
if (isset($config['charset'])) { pg_set_error_verbosity($this->connection, PGSQL_ERRORS_VERBOSE);
DibiDriverException::tryError();
pg_set_client_encoding($this->connection, $config['charset']); if (isset($config['charset']) && pg_set_client_encoding($this->connection, $config['charset'])) {
if (DibiDriverException::catchError($msg)) { throw self::createException(pg_last_error($this->connection));
throw new DibiDriverException($msg, 0);
}
} }
if (isset($config['schema'])) { if (isset($config['schema'])) {
@@ -125,8 +128,8 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver, IDibiResultDr
/** /**
* Executes the SQL query. * Executes the SQL query.
* @param string SQL statement. * @param string SQL statement.
* @return IDibiResultDriver|NULL * @return Dibi\ResultDriver|NULL
* @throws DibiDriverException * @throws Dibi\DriverException
*/ */
public function query($sql) public function query($sql)
{ {
@@ -134,7 +137,7 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver, IDibiResultDr
$res = @pg_query($this->connection, $sql); // intentionally @ $res = @pg_query($this->connection, $sql); // intentionally @
if ($res === FALSE) { if ($res === FALSE) {
throw new DibiDriverException(pg_last_error($this->connection), 0, $sql); throw self::createException(pg_last_error($this->connection), NULL, $sql);
} elseif (is_resource($res)) { } elseif (is_resource($res)) {
$this->affectedRows = pg_affected_rows($res); $this->affectedRows = pg_affected_rows($res);
@@ -145,6 +148,34 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver, IDibiResultDr
} }
/**
* @return Dibi\DriverException
*/
public static function createException($message, $code = NULL, $sql = NULL)
{
if ($code === NULL && preg_match('#^ERROR:\s+(\S+):\s*#', $message, $m)) {
$code = $m[1];
$message = substr($message, strlen($m[0]));
}
if ($code === '0A000' && strpos($message, 'truncate') !== FALSE) {
return new Dibi\ForeignKeyConstraintViolationException($message, $code, $sql);
} elseif ($code === '23502') {
return new Dibi\NotNullConstraintViolationException($message, $code, $sql);
} elseif ($code === '23503') {
return new Dibi\ForeignKeyConstraintViolationException($message, $code, $sql);
} elseif ($code === '23505') {
return new Dibi\UniqueConstraintViolationException($message, $code, $sql);
} else {
return new Dibi\DriverException($message, $code, $sql);
}
}
/** /**
* Gets the number of affected rows by the last INSERT, UPDATE or DELETE query. * Gets the number of affected rows by the last INSERT, UPDATE or DELETE query.
* @return int|FALSE number of rows or FALSE on error * @return int|FALSE number of rows or FALSE on error
@@ -181,7 +212,7 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver, IDibiResultDr
* Begins a transaction (if supported). * Begins a transaction (if supported).
* @param string optional savepoint name * @param string optional savepoint name
* @return void * @return void
* @throws DibiDriverException * @throws Dibi\DriverException
*/ */
public function begin($savepoint = NULL) public function begin($savepoint = NULL)
{ {
@@ -193,7 +224,7 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver, IDibiResultDr
* Commits statements in a transaction. * Commits statements in a transaction.
* @param string optional savepoint name * @param string optional savepoint name
* @return void * @return void
* @throws DibiDriverException * @throws Dibi\DriverException
*/ */
public function commit($savepoint = NULL) public function commit($savepoint = NULL)
{ {
@@ -205,7 +236,7 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver, IDibiResultDr
* Rollback changes in a transaction. * Rollback changes in a transaction.
* @param string optional savepoint name * @param string optional savepoint name
* @return void * @return void
* @throws DibiDriverException * @throws Dibi\DriverException
*/ */
public function rollback($savepoint = NULL) public function rollback($savepoint = NULL)
{ {
@@ -219,7 +250,7 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver, IDibiResultDr
*/ */
public function inTransaction() public function inTransaction()
{ {
return !in_array(pg_transaction_status($this->connection), array(PGSQL_TRANSACTION_UNKNOWN, PGSQL_TRANSACTION_IDLE), TRUE); return !in_array(pg_transaction_status($this->connection), [PGSQL_TRANSACTION_UNKNOWN, PGSQL_TRANSACTION_IDLE], TRUE);
} }
@@ -235,7 +266,7 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver, IDibiResultDr
/** /**
* Returns the connection reflector. * Returns the connection reflector.
* @return IDibiReflector * @return Dibi\Reflector
*/ */
public function getReflector() public function getReflector()
{ {
@@ -246,7 +277,7 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver, IDibiResultDr
/** /**
* Result set driver factory. * Result set driver factory.
* @param resource * @param resource
* @return IDibiResultDriver * @return Dibi\ResultDriver
*/ */
public function createResultDriver($resource) public function createResultDriver($resource)
{ {
@@ -262,42 +293,54 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver, IDibiResultDr
/** /**
* Encodes data for use in a SQL statement. * Encodes data for use in a SQL statement.
* @param mixed value * @param mixed value
* @param string type (dibi::TEXT, dibi::BOOL, ...)
* @return string encoded value * @return string encoded value
* @throws InvalidArgumentException
*/ */
public function escape($value, $type) public function escapeText($value)
{ {
switch ($type) { if (!is_resource($this->connection)) {
case dibi::TEXT: throw new Dibi\Exception('Lost connection to server.');
if (!is_resource($this->connection)) {
throw new DibiException('Lost connection to server.');
}
return "'" . pg_escape_string($this->connection, $value) . "'";
case dibi::BINARY:
if (!is_resource($this->connection)) {
throw new DibiException('Lost connection to server.');
}
return "'" . pg_escape_bytea($this->connection, $value) . "'";
case dibi::IDENTIFIER:
// @see http://www.postgresql.org/docs/8.2/static/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS
return '"' . str_replace('"', '""', $value) . '"';
case dibi::BOOL:
return $value ? 'TRUE' : 'FALSE';
case dibi::DATE:
case dibi::DATETIME:
if (!$value instanceof DateTime && !$value instanceof DateTimeInterface) {
$value = new DibiDateTime($value);
}
return $value->format($type === dibi::DATETIME ? "'Y-m-d H:i:s'" : "'Y-m-d'");
default:
throw new InvalidArgumentException('Unsupported type.');
} }
return "'" . pg_escape_string($this->connection, $value) . "'";
}
public function escapeBinary($value)
{
if (!is_resource($this->connection)) {
throw new Dibi\Exception('Lost connection to server.');
}
return "'" . pg_escape_bytea($this->connection, $value) . "'";
}
public function escapeIdentifier($value)
{
// @see http://www.postgresql.org/docs/8.2/static/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS
return '"' . str_replace('"', '""', $value) . '"';
}
public function escapeBool($value)
{
return $value ? 'TRUE' : 'FALSE';
}
public function escapeDate($value)
{
if (!$value instanceof \DateTime && !$value instanceof \DateTimeInterface) {
$value = new Dibi\DateTime($value);
}
return $value->format("'Y-m-d'");
}
public function escapeDateTime($value)
{
if (!$value instanceof \DateTime && !$value instanceof \DateTimeInterface) {
$value = new Dibi\DateTime($value);
}
return $value->format("'Y-m-d H:i:s'");
} }
@@ -311,24 +354,27 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver, IDibiResultDr
{ {
$bs = pg_escape_string($this->connection, '\\'); // standard_conforming_strings = on/off $bs = pg_escape_string($this->connection, '\\'); // standard_conforming_strings = on/off
$value = pg_escape_string($this->connection, $value); $value = pg_escape_string($this->connection, $value);
$value = strtr($value, array('%' => $bs . '%', '_' => $bs . '_', '\\' => '\\\\')); $value = strtr($value, ['%' => $bs . '%', '_' => $bs . '_', '\\' => '\\\\']);
return ($pos <= 0 ? "'%" : "'") . $value . ($pos >= 0 ? "%'" : "'"); return ($pos <= 0 ? "'%" : "'") . $value . ($pos >= 0 ? "%'" : "'");
} }
/** /**
* Decodes data from result set. * Decodes data from result set.
* @param string value * @param string
* @param string type (dibi::BINARY) * @return string
* @return string decoded value
* @throws InvalidArgumentException
*/ */
public function unescape($value, $type) public function unescapeBinary($value)
{ {
if ($type === dibi::BINARY) { return pg_unescape_bytea($value);
return pg_unescape_bytea($value); }
}
throw new InvalidArgumentException('Unsupported type.');
/** @deprecated */
public function escape($value, $type)
{
trigger_error(__METHOD__ . '() is deprecated.', E_USER_DEPRECATED);
return Dibi\Helpers::escape($this, $value, $type);
} }
@@ -338,11 +384,13 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver, IDibiResultDr
*/ */
public function applyLimit(& $sql, $limit, $offset) public function applyLimit(& $sql, $limit, $offset)
{ {
if ($limit >= 0) { if ($limit < 0 || $offset < 0) {
throw new Dibi\NotSupportedException('Negative offset or limit.');
}
if ($limit !== NULL) {
$sql .= ' LIMIT ' . (int) $limit; $sql .= ' LIMIT ' . (int) $limit;
} }
if ($offset) {
if ($offset > 0) {
$sql .= ' OFFSET ' . (int) $offset; $sql .= ' OFFSET ' . (int) $offset;
} }
} }
@@ -411,13 +459,13 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver, IDibiResultDr
public function getResultColumns() public function getResultColumns()
{ {
$count = pg_num_fields($this->resultSet); $count = pg_num_fields($this->resultSet);
$columns = array(); $columns = [];
for ($i = 0; $i < $count; $i++) { for ($i = 0; $i < $count; $i++) {
$row = array( $row = [
'name' => pg_field_name($this->resultSet, $i), 'name' => pg_field_name($this->resultSet, $i),
'table' => pg_field_table($this->resultSet, $i), 'table' => pg_field_table($this->resultSet, $i),
'nativetype' => pg_field_type($this->resultSet, $i), 'nativetype' => pg_field_type($this->resultSet, $i),
); ];
$row['fullname'] = $row['table'] ? $row['table'] . '.' . $row['name'] : $row['name']; $row['fullname'] = $row['table'] ? $row['table'] . '.' . $row['name'] : $row['name'];
$columns[] = $row; $columns[] = $row;
} }
@@ -436,7 +484,7 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver, IDibiResultDr
} }
/********************* IDibiReflector ****************d*g**/ /********************* Dibi\Reflector ****************d*g**/
/** /**
@@ -445,9 +493,9 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver, IDibiResultDr
*/ */
public function getTables() public function getTables()
{ {
$version = pg_parameter_status($this->resource, 'server_version'); $version = pg_parameter_status($this->getResource(), 'server_version');
if ($version < 7.4) { if ($version < 7.4) {
throw new DibiDriverException('Reflection requires PostgreSQL 7.4 and newer.'); throw new Dibi\DriverException('Reflection requires PostgreSQL 7.4 and newer.');
} }
$query = " $query = "
@@ -475,7 +523,7 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver, IDibiResultDr
$res = $this->query($query); $res = $this->query($query);
$tables = pg_fetch_all($res->resultSet); $tables = pg_fetch_all($res->resultSet);
return $tables ? $tables : array(); return $tables ? $tables : [];
} }
@@ -486,7 +534,7 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver, IDibiResultDr
*/ */
public function getColumns($table) public function getColumns($table)
{ {
$_table = $this->escape($this->escape($table, dibi::IDENTIFIER), dibi::TEXT); $_table = $this->escapeText($this->escapeIdentifier($table));
$res = $this->query(" $res = $this->query("
SELECT indkey SELECT indkey
FROM pg_class FROM pg_class
@@ -528,10 +576,10 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver, IDibiResultDr
"); ");
} }
$columns = array(); $columns = [];
while ($row = $res->fetch(TRUE)) { while ($row = $res->fetch(TRUE)) {
$size = (int) max($row['character_maximum_length'], $row['numeric_precision']); $size = (int) max($row['character_maximum_length'], $row['numeric_precision']);
$columns[] = array( $columns[] = [
'name' => $row['column_name'], 'name' => $row['column_name'],
'table' => $table, 'table' => $table,
'nativetype' => strtoupper($row['udt_name']), 'nativetype' => strtoupper($row['udt_name']),
@@ -540,7 +588,7 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver, IDibiResultDr
'default' => $row['column_default'], 'default' => $row['column_default'],
'autoincrement' => (int) $row['ordinal_position'] === $primary && substr($row['column_default'], 0, 7) === 'nextval', 'autoincrement' => (int) $row['ordinal_position'] === $primary && substr($row['column_default'], 0, 7) === 'nextval',
'vendor' => $row, 'vendor' => $row,
); ];
} }
return $columns; return $columns;
} }
@@ -553,7 +601,7 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver, IDibiResultDr
*/ */
public function getIndexes($table) public function getIndexes($table)
{ {
$_table = $this->escape($this->escape($table, dibi::IDENTIFIER), dibi::TEXT); $_table = $this->escapeText($this->escapeIdentifier($table));
$res = $this->query(" $res = $this->query("
SELECT SELECT
a.attnum AS ordinal_position, a.attnum AS ordinal_position,
@@ -568,7 +616,7 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver, IDibiResultDr
ORDER BY ordinal_position ORDER BY ordinal_position
"); ");
$columns = array(); $columns = [];
while ($row = $res->fetch(TRUE)) { while ($row = $res->fetch(TRUE)) {
$columns[$row['ordinal_position']] = $row['column_name']; $columns[$row['ordinal_position']] = $row['column_name'];
} }
@@ -581,7 +629,7 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver, IDibiResultDr
WHERE pg_class.oid = $_table::regclass WHERE pg_class.oid = $_table::regclass
"); ");
$indexes = array(); $indexes = [];
while ($row = $res->fetch(TRUE)) { while ($row = $res->fetch(TRUE)) {
$indexes[$row['relname']]['name'] = $row['relname']; $indexes[$row['relname']]['name'] = $row['relname'];
$indexes[$row['relname']]['unique'] = $row['indisunique'] === 't'; $indexes[$row['relname']]['unique'] = $row['indisunique'] === 't';
@@ -601,7 +649,7 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver, IDibiResultDr
*/ */
public function getForeignKeys($table) public function getForeignKeys($table)
{ {
$_table = $this->escape($this->escape($table, dibi::IDENTIFIER), dibi::TEXT); $_table = $this->escapeText($this->escapeIdentifier($table));
$res = $this->query(" $res = $this->query("
SELECT SELECT
@@ -642,17 +690,17 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver, IDibiResultDr
c.conrelid = $_table::regclass c.conrelid = $_table::regclass
"); ");
$fKeys = $references = array(); $fKeys = $references = [];
while ($row = $res->fetch(TRUE)) { while ($row = $res->fetch(TRUE)) {
if (!isset($fKeys[$row['name']])) { if (!isset($fKeys[$row['name']])) {
$fKeys[$row['name']] = array( $fKeys[$row['name']] = [
'name' => $row['name'], 'name' => $row['name'],
'table' => $row['table'], 'table' => $row['table'],
'local' => array(), 'local' => [],
'foreign' => array(), 'foreign' => [],
'onUpdate' => $row['onUpdate'], 'onUpdate' => $row['onUpdate'],
'onDelete' => $row['onDelete'], 'onDelete' => $row['onDelete'],
); ];
$l = explode(',', trim($row['conkey'], '{}')); $l = explode(',', trim($row['conkey'], '{}'));
$f = explode(',', trim($row['confkey'], '{}')); $f = explode(',', trim($row['confkey'], '{}'));

View File

@@ -5,8 +5,10 @@
* Copyright (c) 2005 David Grudl (https://davidgrudl.com) * Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/ */
namespace Dibi\Drivers;
require_once dirname(__FILE__) . '/DibiSqliteReflector.php'; use Dibi;
use SQLite3;
/** /**
@@ -19,16 +21,16 @@ require_once dirname(__FILE__) . '/DibiSqliteReflector.php';
* - dbcharset => database character encoding (will be converted to 'charset') * - dbcharset => database character encoding (will be converted to 'charset')
* - charset => character encoding to set (default is UTF-8) * - charset => character encoding to set (default is UTF-8)
* - resource (SQLite3) => existing connection resource * - resource (SQLite3) => existing connection resource
* - lazy, profiler, result, substitutes, ... => see DibiConnection options * - lazy, profiler, result, substitutes, ... => see Dibi\Connection options
*
* @package dibi\drivers
*/ */
class DibiSqlite3Driver extends DibiObject implements IDibiDriver, IDibiResultDriver class Sqlite3Driver implements Dibi\Driver, Dibi\ResultDriver
{ {
use Dibi\Strict;
/** @var SQLite3 Connection resource */ /** @var SQLite3 Connection resource */
private $connection; private $connection;
/** @var SQLite3Result Resultset resource */ /** @var \SQLite3Result Resultset resource */
private $resultSet; private $resultSet;
/** @var bool */ /** @var bool */
@@ -42,12 +44,12 @@ class DibiSqlite3Driver extends DibiObject implements IDibiDriver, IDibiResultDr
/** /**
* @throws DibiNotSupportedException * @throws Dibi\NotSupportedException
*/ */
public function __construct() public function __construct()
{ {
if (!extension_loaded('sqlite3')) { if (!extension_loaded('sqlite3')) {
throw new DibiNotSupportedException("PHP extension 'sqlite3' is not loaded."); throw new Dibi\NotSupportedException("PHP extension 'sqlite3' is not loaded.");
} }
} }
@@ -55,11 +57,11 @@ class DibiSqlite3Driver extends DibiObject implements IDibiDriver, IDibiResultDr
/** /**
* Connects to a database. * Connects to a database.
* @return void * @return void
* @throws DibiException * @throws Dibi\Exception
*/ */
public function connect(array & $config) public function connect(array & $config)
{ {
DibiConnection::alias($config, 'database', 'file'); Dibi\Helpers::alias($config, 'database', 'file');
$this->fmtDate = isset($config['formatDate']) ? $config['formatDate'] : 'U'; $this->fmtDate = isset($config['formatDate']) ? $config['formatDate'] : 'U';
$this->fmtDateTime = isset($config['formatDateTime']) ? $config['formatDateTime'] : 'U'; $this->fmtDateTime = isset($config['formatDateTime']) ? $config['formatDateTime'] : 'U';
@@ -68,8 +70,8 @@ class DibiSqlite3Driver extends DibiObject implements IDibiDriver, IDibiResultDr
} else { } else {
try { try {
$this->connection = new SQLite3($config['database']); $this->connection = new SQLite3($config['database']);
} catch (Exception $e) { } catch (\Exception $e) {
throw new DibiDriverException($e->getMessage(), $e->getCode()); throw new Dibi\DriverException($e->getMessage(), $e->getCode());
} }
} }
@@ -100,8 +102,8 @@ class DibiSqlite3Driver extends DibiObject implements IDibiDriver, IDibiResultDr
/** /**
* Executes the SQL query. * Executes the SQL query.
* @param string SQL statement. * @param string SQL statement.
* @return IDibiResultDriver|NULL * @return Dibi\ResultDriver|NULL
* @throws DibiDriverException * @throws Dibi\DriverException
*/ */
public function query($sql) public function query($sql)
{ {
@@ -110,15 +112,45 @@ class DibiSqlite3Driver extends DibiObject implements IDibiDriver, IDibiResultDr
} }
$res = @$this->connection->query($sql); // intentionally @ $res = @$this->connection->query($sql); // intentionally @
if ($this->connection->lastErrorCode()) { if ($code = $this->connection->lastErrorCode()) {
throw new DibiDriverException($this->connection->lastErrorMsg(), $this->connection->lastErrorCode(), $sql); throw self::createException($this->connection->lastErrorMsg(), $code, $sql);
} elseif ($res instanceof SQLite3Result) { } elseif ($res instanceof \SQLite3Result) {
return $this->createResultDriver($res); return $this->createResultDriver($res);
} }
} }
/**
* @return Dibi\DriverException
*/
public static function createException($message, $code, $sql)
{
if ($code !== 19) {
return new Dibi\DriverException($message, $code, $sql);
} elseif (strpos($message, 'must be unique') !== FALSE
|| strpos($message, 'is not unique') !== FALSE
|| strpos($message, 'UNIQUE constraint failed') !== FALSE
) {
return new Dibi\UniqueConstraintViolationException($message, $code, $sql);
} elseif (strpos($message, 'may not be NULL') !== FALSE
|| strpos($message, 'NOT NULL constraint failed') !== FALSE
) {
return new Dibi\NotNullConstraintViolationException($message, $code, $sql);
} elseif (strpos($message, 'foreign key constraint failed') !== FALSE
|| strpos($message, 'FOREIGN KEY constraint failed') !== FALSE
) {
return new Dibi\ForeignKeyConstraintViolationException($message, $code, $sql);
} else {
return new Dibi\ConstraintViolationException($message, $code, $sql);
}
}
/** /**
* Gets the number of affected rows by the last INSERT, UPDATE or DELETE query. * Gets the number of affected rows by the last INSERT, UPDATE or DELETE query.
* @return int|FALSE number of rows or FALSE on error * @return int|FALSE number of rows or FALSE on error
@@ -143,7 +175,7 @@ class DibiSqlite3Driver extends DibiObject implements IDibiDriver, IDibiResultDr
* Begins a transaction (if supported). * Begins a transaction (if supported).
* @param string optional savepoint name * @param string optional savepoint name
* @return void * @return void
* @throws DibiDriverException * @throws Dibi\DriverException
*/ */
public function begin($savepoint = NULL) public function begin($savepoint = NULL)
{ {
@@ -155,7 +187,7 @@ class DibiSqlite3Driver extends DibiObject implements IDibiDriver, IDibiResultDr
* Commits statements in a transaction. * Commits statements in a transaction.
* @param string optional savepoint name * @param string optional savepoint name
* @return void * @return void
* @throws DibiDriverException * @throws Dibi\DriverException
*/ */
public function commit($savepoint = NULL) public function commit($savepoint = NULL)
{ {
@@ -167,7 +199,7 @@ class DibiSqlite3Driver extends DibiObject implements IDibiDriver, IDibiResultDr
* Rollback changes in a transaction. * Rollback changes in a transaction.
* @param string optional savepoint name * @param string optional savepoint name
* @return void * @return void
* @throws DibiDriverException * @throws Dibi\DriverException
*/ */
public function rollback($savepoint = NULL) public function rollback($savepoint = NULL)
{ {
@@ -187,20 +219,20 @@ class DibiSqlite3Driver extends DibiObject implements IDibiDriver, IDibiResultDr
/** /**
* Returns the connection reflector. * Returns the connection reflector.
* @return IDibiReflector * @return Dibi\Reflector
*/ */
public function getReflector() public function getReflector()
{ {
return new DibiSqliteReflector($this); return new SqliteReflector($this);
} }
/** /**
* Result set driver factory. * Result set driver factory.
* @param SQLite3Result * @param \SQLite3Result
* @return IDibiResultDriver * @return Dibi\ResultDriver
*/ */
public function createResultDriver(SQLite3Result $resource) public function createResultDriver(\SQLite3Result $resource)
{ {
$res = clone $this; $res = clone $this;
$res->resultSet = $resource; $res->resultSet = $resource;
@@ -214,35 +246,47 @@ class DibiSqlite3Driver extends DibiObject implements IDibiDriver, IDibiResultDr
/** /**
* Encodes data for use in a SQL statement. * Encodes data for use in a SQL statement.
* @param mixed value * @param mixed value
* @param string type (dibi::TEXT, dibi::BOOL, ...)
* @return string encoded value * @return string encoded value
* @throws InvalidArgumentException
*/ */
public function escape($value, $type) public function escapeText($value)
{ {
switch ($type) { return "'" . $this->connection->escapeString($value) . "'";
case dibi::TEXT: }
return "'" . $this->connection->escapeString($value) . "'";
case dibi::BINARY:
return "X'" . bin2hex((string) $value) . "'";
case dibi::IDENTIFIER: public function escapeBinary($value)
return '[' . strtr($value, '[]', ' ') . ']'; {
return "X'" . bin2hex((string) $value) . "'";
}
case dibi::BOOL:
return $value ? 1 : 0;
case dibi::DATE: public function escapeIdentifier($value)
case dibi::DATETIME: {
if (!$value instanceof DateTime && !$value instanceof DateTimeInterface) { return '[' . strtr($value, '[]', ' ') . ']';
$value = new DibiDateTime($value); }
}
return $value->format($type === dibi::DATETIME ? $this->fmtDateTime : $this->fmtDate);
default:
throw new InvalidArgumentException('Unsupported type.'); public function escapeBool($value)
{
return $value ? 1 : 0;
}
public function escapeDate($value)
{
if (!$value instanceof \DateTime && !$value instanceof \DateTimeInterface) {
$value = new Dibi\DateTime($value);
} }
return $value->format($this->fmtDate);
}
public function escapeDateTime($value)
{
if (!$value instanceof \DateTime && !$value instanceof \DateTimeInterface) {
$value = new Dibi\DateTime($value);
}
return $value->format($this->fmtDateTime);
} }
@@ -261,17 +305,20 @@ class DibiSqlite3Driver extends DibiObject implements IDibiDriver, IDibiResultDr
/** /**
* Decodes data from result set. * Decodes data from result set.
* @param string value * @param string
* @param string type (dibi::BINARY) * @return string
* @return string decoded value
* @throws InvalidArgumentException
*/ */
public function unescape($value, $type) public function unescapeBinary($value)
{ {
if ($type === dibi::BINARY) { return $value;
return $value; }
}
throw new InvalidArgumentException('Unsupported type.');
/** @deprecated */
public function escape($value, $type)
{
trigger_error(__METHOD__ . '() is deprecated.', E_USER_DEPRECATED);
return Dibi\Helpers::escape($this, $value, $type);
} }
@@ -281,8 +328,12 @@ class DibiSqlite3Driver extends DibiObject implements IDibiDriver, IDibiResultDr
*/ */
public function applyLimit(& $sql, $limit, $offset) public function applyLimit(& $sql, $limit, $offset)
{ {
if ($limit >= 0 || $offset > 0) { if ($limit < 0 || $offset < 0) {
$sql .= ' LIMIT ' . (int) $limit . ($offset > 0 ? ' OFFSET ' . (int) $offset : ''); throw new Dibi\NotSupportedException('Negative offset or limit.');
} elseif ($limit !== NULL || $offset) {
$sql .= ' LIMIT ' . ($limit === NULL ? '-1' : (int) $limit)
. ($offset ? ' OFFSET ' . (int) $offset : '');
} }
} }
@@ -303,11 +354,11 @@ class DibiSqlite3Driver extends DibiObject implements IDibiDriver, IDibiResultDr
/** /**
* Returns the number of rows in a result set. * Returns the number of rows in a result set.
* @return int * @return int
* @throws DibiNotSupportedException * @throws Dibi\NotSupportedException
*/ */
public function getRowCount() public function getRowCount()
{ {
throw new DibiNotSupportedException('Row count is not available for unbuffered queries.'); throw new Dibi\NotSupportedException('Row count is not available for unbuffered queries.');
} }
@@ -321,12 +372,12 @@ class DibiSqlite3Driver extends DibiObject implements IDibiDriver, IDibiResultDr
$row = $this->resultSet->fetchArray($assoc ? SQLITE3_ASSOC : SQLITE3_NUM); $row = $this->resultSet->fetchArray($assoc ? SQLITE3_ASSOC : SQLITE3_NUM);
$charset = $this->charset === NULL ? NULL : $this->charset . '//TRANSLIT'; $charset = $this->charset === NULL ? NULL : $this->charset . '//TRANSLIT';
if ($row && ($assoc || $charset)) { if ($row && ($assoc || $charset)) {
$tmp = array(); $tmp = [];
foreach ($row as $k => $v) { foreach ($row as $k => $v) {
if ($charset !== NULL && is_string($v)) { if ($charset !== NULL && is_string($v)) {
$v = iconv($this->dbcharset, $charset, $v); $v = iconv($this->dbcharset, $charset, $v);
} }
$tmp[str_replace(array('[', ']'), '', $k)] = $v; $tmp[str_replace(['[', ']'], '', $k)] = $v;
} }
return $tmp; return $tmp;
} }
@@ -338,11 +389,11 @@ class DibiSqlite3Driver extends DibiObject implements IDibiDriver, IDibiResultDr
* Moves cursor position without fetching row. * Moves cursor position without fetching row.
* @param int the 0-based cursor pos to seek to * @param int the 0-based cursor pos to seek to
* @return bool TRUE on success, FALSE if unable to seek to specified record * @return bool TRUE on success, FALSE if unable to seek to specified record
* @throws DibiNotSupportedException * @throws Dibi\NotSupportedException
*/ */
public function seek($row) public function seek($row)
{ {
throw new DibiNotSupportedException('Cannot seek an unbuffered result set.'); throw new Dibi\NotSupportedException('Cannot seek an unbuffered result set.');
} }
@@ -364,15 +415,15 @@ class DibiSqlite3Driver extends DibiObject implements IDibiDriver, IDibiResultDr
public function getResultColumns() public function getResultColumns()
{ {
$count = $this->resultSet->numColumns(); $count = $this->resultSet->numColumns();
$columns = array(); $columns = [];
static $types = array(SQLITE3_INTEGER => 'int', SQLITE3_FLOAT => 'float', SQLITE3_TEXT => 'text', SQLITE3_BLOB => 'blob', SQLITE3_NULL => 'null'); static $types = [SQLITE3_INTEGER => 'int', SQLITE3_FLOAT => 'float', SQLITE3_TEXT => 'text', SQLITE3_BLOB => 'blob', SQLITE3_NULL => 'null'];
for ($i = 0; $i < $count; $i++) { for ($i = 0; $i < $count; $i++) {
$columns[] = array( $columns[] = [
'name' => $this->resultSet->columnName($i), 'name' => $this->resultSet->columnName($i),
'table' => NULL, 'table' => NULL,
'fullname' => $this->resultSet->columnName($i), 'fullname' => $this->resultSet->columnName($i),
'nativetype' => $types[$this->resultSet->columnType($i)], 'nativetype' => $types[$this->resultSet->columnType($i)],
); ];
} }
return $columns; return $columns;
} }
@@ -399,7 +450,7 @@ class DibiSqlite3Driver extends DibiObject implements IDibiDriver, IDibiResultDr
* @param int num of arguments * @param int num of arguments
* @return void * @return void
*/ */
public function registerFunction($name, $callback, $numArgs = -1) public function registerFunction($name, callable $callback, $numArgs = -1)
{ {
$this->connection->createFunction($name, $callback, $numArgs); $this->connection->createFunction($name, $callback, $numArgs);
} }
@@ -413,7 +464,7 @@ class DibiSqlite3Driver extends DibiObject implements IDibiDriver, IDibiResultDr
* @param int num of arguments * @param int num of arguments
* @return void * @return void
*/ */
public function registerAggregateFunction($name, $rowCallback, $agrCallback, $numArgs = -1) public function registerAggregateFunction($name, callable $rowCallback, callable $agrCallback, $numArgs = -1)
{ {
$this->connection->createAggregate($name, $rowCallback, $agrCallback, $numArgs); $this->connection->createAggregate($name, $rowCallback, $agrCallback, $numArgs);
} }

View File

@@ -5,20 +5,24 @@
* Copyright (c) 2005 David Grudl (https://davidgrudl.com) * Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/ */
namespace Dibi\Drivers;
use Dibi;
/** /**
* The dibi reflector for SQLite database. * The dibi reflector for SQLite database.
*
* @package dibi\drivers
* @internal * @internal
*/ */
class DibiSqliteReflector extends DibiObject implements IDibiReflector class SqliteReflector implements Dibi\Reflector
{ {
/** @var IDibiDriver */ use Dibi\Strict;
/** @var Dibi\Driver */
private $driver; private $driver;
public function __construct(IDibiDriver $driver) public function __construct(Dibi\Driver $driver)
{ {
$this->driver = $driver; $this->driver = $driver;
} }
@@ -36,7 +40,7 @@ class DibiSqliteReflector extends DibiObject implements IDibiReflector
SELECT name, type = 'view' as view FROM sqlite_temp_master WHERE type IN ('table', 'view') SELECT name, type = 'view' as view FROM sqlite_temp_master WHERE type IN ('table', 'view')
ORDER BY name ORDER BY name
"); ");
$tables = array(); $tables = [];
while ($row = $res->fetch(TRUE)) { while ($row = $res->fetch(TRUE)) {
$tables[] = $row; $tables[] = $row;
} }
@@ -51,18 +55,12 @@ class DibiSqliteReflector extends DibiObject implements IDibiReflector
*/ */
public function getColumns($table) public function getColumns($table)
{ {
$meta = $this->driver->query(" $res = $this->driver->query("PRAGMA table_info({$this->driver->escapeIdentifier($table)})");
SELECT sql FROM sqlite_master WHERE type = 'table' AND name = {$this->driver->escape($table, dibi::TEXT)} $columns = [];
UNION ALL
SELECT sql FROM sqlite_temp_master WHERE type = 'table' AND name = {$this->driver->escape($table, dibi::TEXT)}
")->fetch(TRUE);
$res = $this->driver->query("PRAGMA table_info({$this->driver->escape($table, dibi::IDENTIFIER)})");
$columns = array();
while ($row = $res->fetch(TRUE)) { while ($row = $res->fetch(TRUE)) {
$column = $row['name']; $column = $row['name'];
$type = explode('(', $row['type']); $type = explode('(', $row['type']);
$columns[] = array( $columns[] = [
'name' => $column, 'name' => $column,
'table' => $table, 'table' => $table,
'fullname' => "$table.$column", 'fullname' => "$table.$column",
@@ -72,7 +70,7 @@ class DibiSqliteReflector extends DibiObject implements IDibiReflector
'default' => $row['dflt_value'], 'default' => $row['dflt_value'],
'autoincrement' => $row['pk'] && $type[0] === 'INTEGER', 'autoincrement' => $row['pk'] && $type[0] === 'INTEGER',
'vendor' => $row, 'vendor' => $row,
); ];
} }
return $columns; return $columns;
} }
@@ -85,15 +83,15 @@ class DibiSqliteReflector extends DibiObject implements IDibiReflector
*/ */
public function getIndexes($table) public function getIndexes($table)
{ {
$res = $this->driver->query("PRAGMA index_list({$this->driver->escape($table, dibi::IDENTIFIER)})"); $res = $this->driver->query("PRAGMA index_list({$this->driver->escapeIdentifier($table)})");
$indexes = array(); $indexes = [];
while ($row = $res->fetch(TRUE)) { while ($row = $res->fetch(TRUE)) {
$indexes[$row['name']]['name'] = $row['name']; $indexes[$row['name']]['name'] = $row['name'];
$indexes[$row['name']]['unique'] = (bool) $row['unique']; $indexes[$row['name']]['unique'] = (bool) $row['unique'];
} }
foreach ($indexes as $index => $values) { foreach ($indexes as $index => $values) {
$res = $this->driver->query("PRAGMA index_info({$this->driver->escape($index, dibi::IDENTIFIER)})"); $res = $this->driver->query("PRAGMA index_info({$this->driver->escapeIdentifier($index)})");
while ($row = $res->fetch(TRUE)) { while ($row = $res->fetch(TRUE)) {
$indexes[$index]['columns'][$row['seqno']] = $row['name']; $indexes[$index]['columns'][$row['seqno']] = $row['name'];
} }
@@ -114,12 +112,12 @@ class DibiSqliteReflector extends DibiObject implements IDibiReflector
if (!$indexes) { // @see http://www.sqlite.org/lang_createtable.html#rowid if (!$indexes) { // @see http://www.sqlite.org/lang_createtable.html#rowid
foreach ($columns as $column) { foreach ($columns as $column) {
if ($column['vendor']['pk']) { if ($column['vendor']['pk']) {
$indexes[] = array( $indexes[] = [
'name' => 'ROWID', 'name' => 'ROWID',
'unique' => TRUE, 'unique' => TRUE,
'primary' => TRUE, 'primary' => TRUE,
'columns' => array($column['name']), 'columns' => [$column['name']],
); ];
break; break;
} }
} }
@@ -136,11 +134,8 @@ class DibiSqliteReflector extends DibiObject implements IDibiReflector
*/ */
public function getForeignKeys($table) public function getForeignKeys($table)
{ {
if (!($this->driver instanceof DibiSqlite3Driver)) { $res = $this->driver->query("PRAGMA foreign_key_list({$this->driver->escapeIdentifier($table)})");
// throw new DibiNotSupportedException; // @see http://www.sqlite.org/foreignkeys.html $keys = [];
}
$res = $this->driver->query("PRAGMA foreign_key_list({$this->driver->escape($table, dibi::IDENTIFIER)})");
$keys = array();
while ($row = $res->fetch(TRUE)) { while ($row = $res->fetch(TRUE)) {
$keys[$row['id']]['name'] = $row['id']; // foreign key name $keys[$row['id']]['name'] = $row['id']; // foreign key name
$keys[$row['id']]['local'][$row['seq']] = $row['from']; // local columns $keys[$row['id']]['local'][$row['seq']] = $row['from']; // local columns

View File

@@ -5,12 +5,15 @@
* Copyright (c) 2005 David Grudl (https://davidgrudl.com) * Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/ */
namespace Dibi\Drivers;
require_once dirname(__FILE__) . '/DibiMsSql2005Reflector.php'; use Dibi;
use Dibi\Connection;
use Dibi\Helpers;
/** /**
* The dibi driver for MS SQL Driver 2005 database. * The dibi driver for Microsoft SQL Server and SQL Azure databases.
* *
* Driver options: * Driver options:
* - host => the MS SQL server host name. It can also include a port number (hostname:port) * - host => the MS SQL server host name. It can also include a port number (hostname:port)
@@ -20,12 +23,12 @@ require_once dirname(__FILE__) . '/DibiMsSql2005Reflector.php';
* - options (array) => connection options {@link https://msdn.microsoft.com/en-us/library/cc296161(SQL.90).aspx} * - options (array) => connection options {@link https://msdn.microsoft.com/en-us/library/cc296161(SQL.90).aspx}
* - charset => character encoding to set (default is UTF-8) * - charset => character encoding to set (default is UTF-8)
* - resource (resource) => existing connection resource * - resource (resource) => existing connection resource
* - lazy, profiler, result, substitutes, ... => see DibiConnection options * - lazy, profiler, result, substitutes, ... => see Dibi\Connection options
*
* @package dibi\drivers
*/ */
class DibiMsSql2005Driver extends DibiObject implements IDibiDriver, IDibiResultDriver class SqlsrvDriver implements Dibi\Driver, Dibi\ResultDriver
{ {
use Dibi\Strict;
/** @var resource Connection resource */ /** @var resource Connection resource */
private $connection; private $connection;
@@ -38,14 +41,17 @@ class DibiMsSql2005Driver extends DibiObject implements IDibiDriver, IDibiResult
/** @var int|FALSE Affected rows */ /** @var int|FALSE Affected rows */
private $affectedRows = FALSE; private $affectedRows = FALSE;
/** @var string */
private $version;
/** /**
* @throws DibiNotSupportedException * @throws Dibi\NotSupportedException
*/ */
public function __construct() public function __construct()
{ {
if (!extension_loaded('sqlsrv')) { if (!extension_loaded('sqlsrv')) {
throw new DibiNotSupportedException("PHP extension 'sqlsrv' is not loaded."); throw new Dibi\NotSupportedException("PHP extension 'sqlsrv' is not loaded.");
} }
} }
@@ -53,14 +59,14 @@ class DibiMsSql2005Driver extends DibiObject implements IDibiDriver, IDibiResult
/** /**
* Connects to a database. * Connects to a database.
* @return void * @return void
* @throws DibiException * @throws Dibi\Exception
*/ */
public function connect(array & $config) public function connect(array & $config)
{ {
DibiConnection::alias($config, 'options|UID', 'username'); Helpers::alias($config, 'options|UID', 'username');
DibiConnection::alias($config, 'options|PWD', 'password'); Helpers::alias($config, 'options|PWD', 'password');
DibiConnection::alias($config, 'options|Database', 'database'); Helpers::alias($config, 'options|Database', 'database');
DibiConnection::alias($config, 'options|CharacterSet', 'charset'); Helpers::alias($config, 'options|CharacterSet', 'charset');
if (isset($config['resource'])) { if (isset($config['resource'])) {
$this->connection = $config['resource']; $this->connection = $config['resource'];
@@ -76,8 +82,9 @@ class DibiMsSql2005Driver extends DibiObject implements IDibiDriver, IDibiResult
if (!is_resource($this->connection)) { if (!is_resource($this->connection)) {
$info = sqlsrv_errors(); $info = sqlsrv_errors();
throw new DibiDriverException($info[0]['message'], $info[0]['code']); throw new Dibi\DriverException($info[0]['message'], $info[0]['code']);
} }
$this->version = sqlsrv_server_info($this->connection)['SQLServerVersion'];
} }
@@ -94,8 +101,8 @@ class DibiMsSql2005Driver extends DibiObject implements IDibiDriver, IDibiResult
/** /**
* Executes the SQL query. * Executes the SQL query.
* @param string SQL statement. * @param string SQL statement.
* @return IDibiResultDriver|NULL * @return Dibi\ResultDriver|NULL
* @throws DibiDriverException * @throws Dibi\DriverException
*/ */
public function query($sql) public function query($sql)
{ {
@@ -104,7 +111,7 @@ class DibiMsSql2005Driver extends DibiObject implements IDibiDriver, IDibiResult
if ($res === FALSE) { if ($res === FALSE) {
$info = sqlsrv_errors(); $info = sqlsrv_errors();
throw new DibiDriverException($info[0]['message'], $info[0]['code'], $sql); throw new Dibi\DriverException($info[0]['message'], $info[0]['code'], $sql);
} elseif (is_resource($res)) { } elseif (is_resource($res)) {
$this->affectedRows = sqlsrv_rows_affected($res); $this->affectedRows = sqlsrv_rows_affected($res);
@@ -142,7 +149,7 @@ class DibiMsSql2005Driver extends DibiObject implements IDibiDriver, IDibiResult
* Begins a transaction (if supported). * Begins a transaction (if supported).
* @param string optional savepoint name * @param string optional savepoint name
* @return void * @return void
* @throws DibiDriverException * @throws Dibi\DriverException
*/ */
public function begin($savepoint = NULL) public function begin($savepoint = NULL)
{ {
@@ -154,7 +161,7 @@ class DibiMsSql2005Driver extends DibiObject implements IDibiDriver, IDibiResult
* Commits statements in a transaction. * Commits statements in a transaction.
* @param string optional savepoint name * @param string optional savepoint name
* @return void * @return void
* @throws DibiDriverException * @throws Dibi\DriverException
*/ */
public function commit($savepoint = NULL) public function commit($savepoint = NULL)
{ {
@@ -166,7 +173,7 @@ class DibiMsSql2005Driver extends DibiObject implements IDibiDriver, IDibiResult
* Rollback changes in a transaction. * Rollback changes in a transaction.
* @param string optional savepoint name * @param string optional savepoint name
* @return void * @return void
* @throws DibiDriverException * @throws Dibi\DriverException
*/ */
public function rollback($savepoint = NULL) public function rollback($savepoint = NULL)
{ {
@@ -186,18 +193,18 @@ class DibiMsSql2005Driver extends DibiObject implements IDibiDriver, IDibiResult
/** /**
* Returns the connection reflector. * Returns the connection reflector.
* @return IDibiReflector * @return Dibi\Reflector
*/ */
public function getReflector() public function getReflector()
{ {
return new DibiMssql2005Reflector($this); return new SqlsrvReflector($this);
} }
/** /**
* Result set driver factory. * Result set driver factory.
* @param resource * @param resource
* @return IDibiResultDriver * @return Dibi\ResultDriver
*/ */
public function createResultDriver($resource) public function createResultDriver($resource)
{ {
@@ -213,34 +220,48 @@ class DibiMsSql2005Driver extends DibiObject implements IDibiDriver, IDibiResult
/** /**
* Encodes data for use in a SQL statement. * Encodes data for use in a SQL statement.
* @param mixed value * @param mixed value
* @param string type (dibi::TEXT, dibi::BOOL, ...)
* @return string encoded value * @return string encoded value
* @throws InvalidArgumentException
*/ */
public function escape($value, $type) public function escapeText($value)
{ {
switch ($type) { return "'" . str_replace("'", "''", $value) . "'";
case dibi::TEXT: }
case dibi::BINARY:
return "'" . str_replace("'", "''", $value) . "'";
case dibi::IDENTIFIER:
// @see https://msdn.microsoft.com/en-us/library/ms176027.aspx
return '[' . str_replace(']', ']]', $value) . ']';
case dibi::BOOL: public function escapeBinary($value)
return $value ? 1 : 0; {
return "'" . str_replace("'", "''", $value) . "'";
}
case dibi::DATE:
case dibi::DATETIME:
if (!$value instanceof DateTime && !$value instanceof DateTimeInterface) {
$value = new DibiDateTime($value);
}
return $value->format($type === dibi::DATETIME ? "'Y-m-d H:i:s'" : "'Y-m-d'");
default: public function escapeIdentifier($value)
throw new InvalidArgumentException('Unsupported type.'); {
// @see https://msdn.microsoft.com/en-us/library/ms176027.aspx
return '[' . str_replace(']', ']]', $value) . ']';
}
public function escapeBool($value)
{
return $value ? 1 : 0;
}
public function escapeDate($value)
{
if (!$value instanceof \DateTime && !$value instanceof \DateTimeInterface) {
$value = new Dibi\DateTime($value);
} }
return $value->format("'Y-m-d'");
}
public function escapeDateTime($value)
{
if (!$value instanceof \DateTime && !$value instanceof \DateTimeInterface) {
$value = new Dibi\DateTime($value);
}
return $value->format("'Y-m-d H:i:s'");
} }
@@ -252,24 +273,27 @@ class DibiMsSql2005Driver extends DibiObject implements IDibiDriver, IDibiResult
*/ */
public function escapeLike($value, $pos) public function escapeLike($value, $pos)
{ {
$value = strtr($value, array("'" => "''", '%' => '[%]', '_' => '[_]', '[' => '[[]')); $value = strtr($value, ["'" => "''", '%' => '[%]', '_' => '[_]', '[' => '[[]']);
return ($pos <= 0 ? "'%" : "'") . $value . ($pos >= 0 ? "%'" : "'"); return ($pos <= 0 ? "'%" : "'") . $value . ($pos >= 0 ? "%'" : "'");
} }
/** /**
* Decodes data from result set. * Decodes data from result set.
* @param string value * @param string
* @param string type (dibi::BINARY) * @return string
* @return string decoded value
* @throws InvalidArgumentException
*/ */
public function unescape($value, $type) public function unescapeBinary($value)
{ {
if ($type === dibi::BINARY) { return $value;
return $value; }
}
throw new InvalidArgumentException('Unsupported type.');
/** @deprecated */
public function escape($value, $type)
{
trigger_error(__METHOD__ . '() is deprecated.', E_USER_DEPRECATED);
return Helpers::escape($this, $value, $type);
} }
@@ -279,13 +303,23 @@ class DibiMsSql2005Driver extends DibiObject implements IDibiDriver, IDibiResult
*/ */
public function applyLimit(& $sql, $limit, $offset) public function applyLimit(& $sql, $limit, $offset)
{ {
// offset support is missing if ($limit < 0 || $offset < 0) {
if ($limit >= 0) { throw new Dibi\NotSupportedException('Negative offset or limit.');
$sql = 'SELECT TOP ' . (int) $limit . ' * FROM (' . $sql . ') AS T ';
}
if ($offset) { } elseif (version_compare($this->version, 11, '<')) { // 11 == SQL Server 2012
throw new DibiNotImplementedException('Offset is not implemented.'); if ($offset) {
throw new Dibi\NotSupportedException('Offset is not supported by this database.');
} elseif ($limit !== NULL) {
$sql = sprintf('SELECT TOP (%d) * FROM (%s) t', $limit, $sql);
}
} elseif ($limit !== NULL) {
// requires ORDER BY, see https://technet.microsoft.com/en-us/library/gg699618(v=sql.110).aspx
$sql = sprintf('%s OFFSET %d ROWS FETCH NEXT %d ROWS ONLY', rtrim($sql), $offset, $limit);
} elseif ($offset) {
// requires ORDER BY, see https://technet.microsoft.com/en-us/library/gg699618(v=sql.110).aspx
$sql = sprintf('%s OFFSET %d ROWS', rtrim($sql), $offset);
} }
} }
@@ -309,7 +343,7 @@ class DibiMsSql2005Driver extends DibiObject implements IDibiDriver, IDibiResult
*/ */
public function getRowCount() public function getRowCount()
{ {
throw new DibiNotSupportedException('Row count is not available for unbuffered queries.'); throw new Dibi\NotSupportedException('Row count is not available for unbuffered queries.');
} }
@@ -331,7 +365,7 @@ class DibiMsSql2005Driver extends DibiObject implements IDibiDriver, IDibiResult
*/ */
public function seek($row) public function seek($row)
{ {
throw new DibiNotSupportedException('Cannot seek an unbuffered result set.'); throw new Dibi\NotSupportedException('Cannot seek an unbuffered result set.');
} }
@@ -352,13 +386,13 @@ class DibiMsSql2005Driver extends DibiObject implements IDibiDriver, IDibiResult
*/ */
public function getResultColumns() public function getResultColumns()
{ {
$columns = array(); $columns = [];
foreach ((array) sqlsrv_field_metadata($this->resultSet) as $fieldMetadata) { foreach ((array) sqlsrv_field_metadata($this->resultSet) as $fieldMetadata) {
$columns[] = array( $columns[] = [
'name' => $fieldMetadata['Name'], 'name' => $fieldMetadata['Name'],
'fullname' => $fieldMetadata['Name'], 'fullname' => $fieldMetadata['Name'],
'nativetype' => $fieldMetadata['Type'], 'nativetype' => $fieldMetadata['Type'],
); ];
} }
return $columns; return $columns;
} }

View File

@@ -5,20 +5,24 @@
* Copyright (c) 2005 David Grudl (https://davidgrudl.com) * Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/ */
namespace Dibi\Drivers;
use Dibi;
/** /**
* The dibi reflector for MSSQL2005 databases. * The dibi reflector for Microsoft SQL Server and SQL Azure databases.
*
* @package dibi\drivers
* @internal * @internal
*/ */
class DibiMsSql2005Reflector extends DibiObject implements IDibiReflector class SqlsrvReflector implements Dibi\Reflector
{ {
/** @var IDibiDriver */ use Dibi\Strict;
/** @var Dibi\Driver */
private $driver; private $driver;
public function __construct(IDibiDriver $driver) public function __construct(Dibi\Driver $driver)
{ {
$this->driver = $driver; $this->driver = $driver;
} }
@@ -30,13 +34,13 @@ class DibiMsSql2005Reflector extends DibiObject implements IDibiReflector
*/ */
public function getTables() public function getTables()
{ {
$res = $this->driver->query('SELECT TABLE_NAME, TABLE_TYPE FROM INFORMATION_SCHEMA.TABLES'); $res = $this->driver->query("SELECT TABLE_NAME, TABLE_TYPE FROM INFORMATION_SCHEMA.TABLES WHERE [TABLE_SCHEMA] = 'dbo'");
$tables = array(); $tables = [];
while ($row = $res->fetch(FALSE)) { while ($row = $res->fetch(FALSE)) {
$tables[] = array( $tables[] = [
'name' => $row[0], 'name' => $row[0],
'view' => isset($row[1]) && $row[1] === 'VIEW', 'view' => isset($row[1]) && $row[1] === 'VIEW',
); ];
} }
return $tables; return $tables;
} }
@@ -53,10 +57,10 @@ class DibiMsSql2005Reflector extends DibiObject implements IDibiReflector
SELECT c.name as COLUMN_NAME, c.is_identity AS AUTO_INCREMENT SELECT c.name as COLUMN_NAME, c.is_identity AS AUTO_INCREMENT
FROM sys.columns c FROM sys.columns c
INNER JOIN sys.tables t ON c.object_id = t.object_id INNER JOIN sys.tables t ON c.object_id = t.object_id
WHERE t.name = {$this->driver->escape($table, dibi::TEXT)} WHERE t.name = {$this->driver->escapeText($table)}
"); ");
$autoIncrements = array(); $autoIncrements = [];
while ($row = $res->fetch(TRUE)) { while ($row = $res->fetch(TRUE)) {
$autoIncrements[$row['COLUMN_NAME']] = (bool) $row['AUTO_INCREMENT']; $autoIncrements[$row['COLUMN_NAME']] = (bool) $row['AUTO_INCREMENT'];
} }
@@ -74,11 +78,11 @@ class DibiMsSql2005Reflector extends DibiObject implements IDibiReflector
And TC.CONSTRAINT_TYPE = 'PRIMARY KEY' And TC.CONSTRAINT_TYPE = 'PRIMARY KEY'
And CCU.COLUMN_NAME = C.COLUMN_NAME And CCU.COLUMN_NAME = C.COLUMN_NAME
) As Z ) As Z
WHERE C.TABLE_NAME = {$this->driver->escape($table, dibi::TEXT)} WHERE C.TABLE_NAME = {$this->driver->escapeText($table)}
"); ");
$columns = array(); $columns = [];
while ($row = $res->fetch(TRUE)) { while ($row = $res->fetch(TRUE)) {
$columns[] = array( $columns[] = [
'name' => $row['COLUMN_NAME'], 'name' => $row['COLUMN_NAME'],
'table' => $table, 'table' => $table,
'nativetype' => strtoupper($row['DATA_TYPE']), 'nativetype' => strtoupper($row['DATA_TYPE']),
@@ -88,7 +92,7 @@ class DibiMsSql2005Reflector extends DibiObject implements IDibiReflector
'default' => $row['COLUMN_DEFAULT'], 'default' => $row['COLUMN_DEFAULT'],
'autoincrement' => $autoIncrements[$row['COLUMN_NAME']], 'autoincrement' => $autoIncrements[$row['COLUMN_NAME']],
'vendor' => $row, 'vendor' => $row,
); ];
} }
return $columns; return $columns;
} }
@@ -101,19 +105,19 @@ class DibiMsSql2005Reflector extends DibiObject implements IDibiReflector
*/ */
public function getIndexes($table) public function getIndexes($table)
{ {
$keyUsagesRes = $this->driver->query("SELECT * FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE TABLE_NAME = {$this->driver->escape($table, dibi::TEXT)}"); $keyUsagesRes = $this->driver->query(sprintf("EXEC [sys].[sp_helpindex] @objname = N%s", $this->driver->escapeText($table)));
$keyUsages = array(); $keyUsages = [];
while ($row = $keyUsagesRes->fetch(TRUE)) { while ($row = $keyUsagesRes->fetch(TRUE)) {
$keyUsages[$row['CONSTRAINT_NAME']][(int) $row['ORDINAL_POSITION'] - 1] = $row['COLUMN_NAME']; $keyUsages[$row['index_name']] = explode(',', $row['index_keys']);
} }
$res = $this->driver->query("SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE TABLE_NAME = {$this->driver->escape($table, dibi::TEXT)}"); $res = $this->driver->query("SELECT [i].* FROM [sys].[indexes] [i] INNER JOIN [sys].[tables] [t] ON [i].[object_id] = [t].[object_id] WHERE [t].[name] = {$this->driver->escapeText($table)}");
$indexes = array(); $indexes = [];
while ($row = $res->fetch(TRUE)) { while ($row = $res->fetch(TRUE)) {
$indexes[$row['CONSTRAINT_NAME']]['name'] = $row['CONSTRAINT_NAME']; $indexes[$row['name']]['name'] = $row['name'];
$indexes[$row['CONSTRAINT_NAME']]['unique'] = $row['CONSTRAINT_TYPE'] === 'UNIQUE'; $indexes[$row['name']]['unique'] = $row['is_unique'] === 1;
$indexes[$row['CONSTRAINT_NAME']]['primary'] = $row['CONSTRAINT_TYPE'] === 'PRIMARY KEY'; $indexes[$row['name']]['primary'] = $row['is_primary_key'] === 1;
$indexes[$row['CONSTRAINT_NAME']]['columns'] = isset($keyUsages[$row['CONSTRAINT_NAME']]) ? $keyUsages[$row['CONSTRAINT_NAME']] : array(); $indexes[$row['name']]['columns'] = isset($keyUsages[$row['name']]) ? $keyUsages[$row['name']] : [];
} }
return array_values($indexes); return array_values($indexes);
} }
@@ -126,7 +130,7 @@ class DibiMsSql2005Reflector extends DibiObject implements IDibiReflector
*/ */
public function getForeignKeys($table) public function getForeignKeys($table)
{ {
throw new DibiNotImplementedException; throw new Dibi\NotImplementedException;
} }
} }

View File

@@ -5,14 +5,16 @@
* Copyright (c) 2005 David Grudl (https://davidgrudl.com) * Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/ */
namespace Dibi;
/** /**
* Profiler & logger event. * Profiler & logger event.
*
* @package dibi
*/ */
class DibiEvent class Event
{ {
use Strict;
/** event type */ /** event type */
const CONNECT = 1, const CONNECT = 1,
SELECT = 4, SELECT = 4,
@@ -26,7 +28,7 @@ class DibiEvent
TRANSACTION = 448, // BEGIN | COMMIT | ROLLBACK TRANSACTION = 448, // BEGIN | COMMIT | ROLLBACK
ALL = 1023; ALL = 1023;
/** @var DibiConnection */ /** @var Connection */
public $connection; public $connection;
/** @var int */ /** @var int */
@@ -35,7 +37,7 @@ class DibiEvent
/** @var string */ /** @var string */
public $sql; public $sql;
/** @var DibiResult|DibiDriverException|NULL */ /** @var Result|DriverException|NULL */
public $result; public $result;
/** @var float */ /** @var float */
@@ -48,7 +50,7 @@ class DibiEvent
public $source; public $source;
public function __construct(DibiConnection $connection, $type, $sql = NULL) public function __construct(Connection $connection, $type, $sql = NULL)
{ {
$this->connection = $connection; $this->connection = $connection;
$this->type = $type; $this->type = $type;
@@ -56,25 +58,25 @@ class DibiEvent
$this->time = -microtime(TRUE); $this->time = -microtime(TRUE);
if ($type === self::QUERY && preg_match('#\(?\s*(SELECT|UPDATE|INSERT|DELETE)#iA', $this->sql, $matches)) { if ($type === self::QUERY && preg_match('#\(?\s*(SELECT|UPDATE|INSERT|DELETE)#iA', $this->sql, $matches)) {
static $types = array( static $types = [
'SELECT' => self::SELECT, 'UPDATE' => self::UPDATE, 'SELECT' => self::SELECT, 'UPDATE' => self::UPDATE,
'INSERT' => self::INSERT, 'DELETE' => self::DELETE, 'INSERT' => self::INSERT, 'DELETE' => self::DELETE,
); ];
$this->type = $types[strtoupper($matches[1])]; $this->type = $types[strtoupper($matches[1])];
} }
$rc = new ReflectionClass('dibi'); $rc = new \ReflectionClass('dibi');
$dibiDir = dirname($rc->getFileName()) . DIRECTORY_SEPARATOR; $dibiDir = dirname($rc->getFileName()) . DIRECTORY_SEPARATOR;
foreach (debug_backtrace(FALSE) as $row) { foreach (debug_backtrace(FALSE) as $row) {
if (isset($row['file']) && is_file($row['file']) && strpos($row['file'], $dibiDir) !== 0) { if (isset($row['file']) && is_file($row['file']) && strpos($row['file'], $dibiDir) !== 0) {
$this->source = array($row['file'], (int) $row['line']); $this->source = [$row['file'], (int) $row['line']];
break; break;
} }
} }
dibi::$elapsedTime = FALSE; \dibi::$elapsedTime = FALSE;
dibi::$numOfQueries++; \dibi::$numOfQueries++;
dibi::$sql = $sql; \dibi::$sql = $sql;
} }
@@ -82,14 +84,14 @@ class DibiEvent
{ {
$this->result = $result; $this->result = $result;
try { try {
$this->count = $result instanceof DibiResult ? count($result) : NULL; $this->count = $result instanceof Result ? count($result) : NULL;
} catch (DibiException $e) { } catch (Exception $e) {
$this->count = NULL; $this->count = NULL;
} }
$this->time += microtime(TRUE); $this->time += microtime(TRUE);
dibi::$elapsedTime = $this->time; \dibi::$elapsedTime = $this->time;
dibi::$totalTime += $this->time; \dibi::$totalTime += $this->time;
return $this; return $this;
} }

View File

@@ -5,39 +5,46 @@
* Copyright (c) 2005 David Grudl (https://davidgrudl.com) * Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/ */
namespace Dibi;
/** /**
* dibi SQL builder via fluent interfaces. EXPERIMENTAL! * dibi SQL builder via fluent interfaces. EXPERIMENTAL!
* *
* @package dibi * @method Fluent select(...$field)
* * @method Fluent distinct()
* @method DibiFluent select($field) * @method Fluent from($table)
* @method DibiFluent distinct() * @method Fluent where(...$cond)
* @method DibiFluent from($table) * @method Fluent groupBy(...$field)
* @method DibiFluent where($cond) * @method Fluent having(...$cond)
* @method DibiFluent groupBy($field) * @method Fluent orderBy(...$field)
* @method DibiFluent having($cond) * @method Fluent limit(int $limit)
* @method DibiFluent orderBy($field) * @method Fluent offset(int $offset)
* @method DibiFluent limit(int $limit) * @method Fluent join(...$table)
* @method DibiFluent offset(int $offset) * @method Fluent leftJoin(...$table)
* @method DibiFluent leftJoin($table) * @method Fluent innerJoin(...$table)
* @method DibiFluent on($cond) * @method Fluent rightJoin(...$table)
* @method Fluent outerJoin(...$table)
* @method Fluent on(...$cond)
* @method Fluent using(...$cond)
*/ */
class DibiFluent extends DibiObject implements IDataSource class Fluent implements IDataSource
{ {
use Strict;
const REMOVE = FALSE; const REMOVE = FALSE;
/** @var array */ /** @var array */
public static $masks = array( public static $masks = [
'SELECT' => array('SELECT', 'DISTINCT', 'FROM', 'WHERE', 'GROUP BY', 'SELECT' => ['SELECT', 'DISTINCT', 'FROM', 'WHERE', 'GROUP BY',
'HAVING', 'ORDER BY', 'LIMIT', 'OFFSET'), 'HAVING', 'ORDER BY', 'LIMIT', 'OFFSET'],
'UPDATE' => array('UPDATE', 'SET', 'WHERE', 'ORDER BY', 'LIMIT'), 'UPDATE' => ['UPDATE', 'SET', 'WHERE', 'ORDER BY', 'LIMIT'],
'INSERT' => array('INSERT', 'INTO', 'VALUES', 'SELECT'), 'INSERT' => ['INSERT', 'INTO', 'VALUES', 'SELECT'],
'DELETE' => array('DELETE', 'FROM', 'USING', 'WHERE', 'ORDER BY', 'LIMIT'), 'DELETE' => ['DELETE', 'FROM', 'USING', 'WHERE', 'ORDER BY', 'LIMIT'],
); ];
/** @var array default modifiers for arrays */ /** @var array default modifiers for arrays */
public static $modifiers = array( public static $modifiers = [
'SELECT' => '%n', 'SELECT' => '%n',
'FROM' => '%n', 'FROM' => '%n',
'IN' => '%in', 'IN' => '%in',
@@ -47,10 +54,10 @@ class DibiFluent extends DibiObject implements IDataSource
'HAVING' => '%and', 'HAVING' => '%and',
'ORDER BY' => '%by', 'ORDER BY' => '%by',
'GROUP BY' => '%by', 'GROUP BY' => '%by',
); ];
/** @var array clauses separators */ /** @var array clauses separators */
public static $separators = array( public static $separators = [
'SELECT' => ',', 'SELECT' => ',',
'FROM' => ',', 'FROM' => ',',
'WHERE' => 'AND', 'WHERE' => 'AND',
@@ -62,47 +69,47 @@ class DibiFluent extends DibiObject implements IDataSource
'SET' => ',', 'SET' => ',',
'VALUES' => ',', 'VALUES' => ',',
'INTO' => FALSE, 'INTO' => FALSE,
); ];
/** @var array clauses */ /** @var array clauses */
public static $clauseSwitches = array( public static $clauseSwitches = [
'JOIN' => 'FROM', 'JOIN' => 'FROM',
'INNER JOIN' => 'FROM', 'INNER JOIN' => 'FROM',
'LEFT JOIN' => 'FROM', 'LEFT JOIN' => 'FROM',
'RIGHT JOIN' => 'FROM', 'RIGHT JOIN' => 'FROM',
); ];
/** @var DibiConnection */ /** @var Connection */
private $connection; private $connection;
/** @var array */ /** @var array */
private $setups = array(); private $setups = [];
/** @var string */ /** @var string */
private $command; private $command;
/** @var array */ /** @var array */
private $clauses = array(); private $clauses = [];
/** @var array */ /** @var array */
private $flags = array(); private $flags = [];
/** @var array */ /** @var array */
private $cursor; private $cursor;
/** @var DibiHashMap normalized clauses */ /** @var HashMap normalized clauses */
private static $normalizer; private static $normalizer;
/** /**
* @param DibiConnection * @param Connection
*/ */
public function __construct(DibiConnection $connection) public function __construct(Connection $connection)
{ {
$this->connection = $connection; $this->connection = $connection;
if (self::$normalizer === NULL) { if (self::$normalizer === NULL) {
self::$normalizer = new DibiHashMap(array(__CLASS__, '_formatClause')); self::$normalizer = new HashMap([__CLASS__, '_formatClause']);
} }
} }
@@ -123,7 +130,7 @@ class DibiFluent extends DibiObject implements IDataSource
$this->clauses = array_fill_keys(self::$masks[$clause], NULL); $this->clauses = array_fill_keys(self::$masks[$clause], NULL);
} }
$this->cursor = & $this->clauses[$clause]; $this->cursor = & $this->clauses[$clause];
$this->cursor = array(); $this->cursor = [];
$this->command = $clause; $this->command = $clause;
} }
@@ -137,7 +144,7 @@ class DibiFluent extends DibiObject implements IDataSource
$this->cursor = & $this->clauses[$clause]; $this->cursor = & $this->clauses[$clause];
// TODO: really delete? // TODO: really delete?
if ($args === array(self::REMOVE)) { if ($args === [self::REMOVE]) {
$this->cursor = NULL; $this->cursor = NULL;
return $this; return $this;
} }
@@ -145,7 +152,7 @@ class DibiFluent extends DibiObject implements IDataSource
if (isset(self::$separators[$clause])) { if (isset(self::$separators[$clause])) {
$sep = self::$separators[$clause]; $sep = self::$separators[$clause];
if ($sep === FALSE) { // means: replace if ($sep === FALSE) { // means: replace
$this->cursor = array(); $this->cursor = [];
} elseif (!empty($this->cursor)) { } elseif (!empty($this->cursor)) {
$this->cursor[] = $sep; $this->cursor[] = $sep;
@@ -154,7 +161,7 @@ class DibiFluent extends DibiObject implements IDataSource
} else { } else {
// append to currect flow // append to currect flow
if ($args === array(self::REMOVE)) { if ($args === [self::REMOVE]) {
return $this; return $this;
} }
@@ -162,7 +169,7 @@ class DibiFluent extends DibiObject implements IDataSource
} }
if ($this->cursor === NULL) { if ($this->cursor === NULL) {
$this->cursor = array(); $this->cursor = [];
} }
// special types or argument // special types or argument
@@ -173,21 +180,21 @@ class DibiFluent extends DibiObject implements IDataSource
return $this; return $this;
} elseif (is_string($arg) && preg_match('#^[a-z:_][a-z0-9_.:]*\z#i', $arg)) { // identifier } elseif (is_string($arg) && preg_match('#^[a-z:_][a-z0-9_.:]*\z#i', $arg)) { // identifier
$args = array('%n', $arg); $args = ['%n', $arg];
} elseif (is_array($arg) || ($arg instanceof Traversable && !$arg instanceof self)) { // any array } elseif (is_array($arg) || ($arg instanceof \Traversable && !$arg instanceof self)) { // any array
if (isset(self::$modifiers[$clause])) { if (isset(self::$modifiers[$clause])) {
$args = array(self::$modifiers[$clause], $arg); $args = [self::$modifiers[$clause], $arg];
} elseif (is_string(key($arg))) { // associative array } elseif (is_string(key($arg))) { // associative array
$args = array('%a', $arg); $args = ['%a', $arg];
} }
} // case $arg === FALSE is handled above } // case $arg === FALSE is handled above
} }
foreach ($args as $arg) { foreach ($args as $arg) {
if ($arg instanceof self) { if ($arg instanceof self) {
$arg = "($arg)"; $arg = new Literal("($arg)");
} }
$this->cursor[] = $arg; $this->cursor[] = $arg;
} }
@@ -205,7 +212,7 @@ class DibiFluent extends DibiObject implements IDataSource
{ {
$this->cursor = & $this->clauses[self::$normalizer->$clause]; $this->cursor = & $this->clauses[self::$normalizer->$clause];
if ($this->cursor === NULL) { if ($this->cursor === NULL) {
$this->cursor = array(); $this->cursor = [];
} }
return $this; return $this;
@@ -265,7 +272,7 @@ class DibiFluent extends DibiObject implements IDataSource
/** /**
* Returns the dibi connection. * Returns the dibi connection.
* @return DibiConnection * @return Connection
*/ */
final public function getConnection() final public function getConnection()
{ {
@@ -274,7 +281,7 @@ class DibiFluent extends DibiObject implements IDataSource
/** /**
* Adds DibiResult setup. * Adds Result setup.
* @param string method * @param string method
* @param mixed args * @param mixed args
* @return self * @return self
@@ -292,16 +299,16 @@ class DibiFluent extends DibiObject implements IDataSource
/** /**
* Generates and executes SQL query. * Generates and executes SQL query.
* @param mixed what to return? * @param mixed what to return?
* @return DibiResult|int result set object (if any) * @return Result|int result set object (if any)
* @throws DibiException * @throws Exception
*/ */
public function execute($return = NULL) public function execute($return = NULL)
{ {
$res = $this->query($this->_export()); $res = $this->query($this->_export());
switch ($return) { switch ($return) {
case dibi::IDENTIFIER: case \dibi::IDENTIFIER:
return $this->connection->getInsertId(); return $this->connection->getInsertId();
case dibi::AFFECTED_ROWS: case \dibi::AFFECTED_ROWS:
return $this->connection->getAffectedRows(); return $this->connection->getAffectedRows();
default: default:
return $res; return $res;
@@ -311,12 +318,12 @@ class DibiFluent extends DibiObject implements IDataSource
/** /**
* Generates, executes SQL query and fetches the single row. * Generates, executes SQL query and fetches the single row.
* @return DibiRow|FALSE array on success, FALSE if no next record * @return Row|FALSE array on success, FALSE if no next record
*/ */
public function fetch() public function fetch()
{ {
if ($this->command === 'SELECT') { if ($this->command === 'SELECT' && !$this->clauses['LIMIT']) {
return $this->query($this->_export(NULL, array('%lmt', 1)))->fetch(); return $this->query($this->_export(NULL, ['%lmt', 1]))->fetch();
} else { } else {
return $this->query($this->_export())->fetch(); return $this->query($this->_export())->fetch();
} }
@@ -329,8 +336,8 @@ class DibiFluent extends DibiObject implements IDataSource
*/ */
public function fetchSingle() public function fetchSingle()
{ {
if ($this->command === 'SELECT') { if ($this->command === 'SELECT' && !$this->clauses['LIMIT']) {
return $this->query($this->_export(NULL, array('%lmt', 1)))->fetchSingle(); return $this->query($this->_export(NULL, ['%lmt', 1]))->fetchSingle();
} else { } else {
return $this->query($this->_export())->fetchSingle(); return $this->query($this->_export())->fetchSingle();
} }
@@ -345,7 +352,7 @@ class DibiFluent extends DibiObject implements IDataSource
*/ */
public function fetchAll($offset = NULL, $limit = NULL) public function fetchAll($offset = NULL, $limit = NULL)
{ {
return $this->query($this->_export(NULL, array('%ofs %lmt', $offset, $limit)))->fetchAll(); return $this->query($this->_export(NULL, ['%ofs %lmt', $offset, $limit]))->fetchAll();
} }
@@ -376,11 +383,11 @@ class DibiFluent extends DibiObject implements IDataSource
* Required by the IteratorAggregate interface. * Required by the IteratorAggregate interface.
* @param int offset * @param int offset
* @param int limit * @param int limit
* @return DibiResultIterator * @return ResultIterator
*/ */
public function getIterator($offset = NULL, $limit = NULL) public function getIterator($offset = NULL, $limit = NULL)
{ {
return $this->query($this->_export(NULL, array('%ofs %lmt', $offset, $limit)))->getIterator(); return $this->query($this->_export(NULL, ['%ofs %lmt', $offset, $limit]))->getIterator();
} }
@@ -400,20 +407,20 @@ class DibiFluent extends DibiObject implements IDataSource
*/ */
public function count() public function count()
{ {
return (int) $this->query(array( return (int) $this->query([
'SELECT COUNT(*) FROM (%ex', $this->_export(), ') AS [data]', 'SELECT COUNT(*) FROM (%ex', $this->_export(), ') [data]',
))->fetchSingle(); ])->fetchSingle();
} }
/** /**
* @return DibiResult * @return Result
*/ */
private function query($args) private function query($args)
{ {
$res = $this->connection->query($args); $res = $this->connection->query($args);
foreach ($this->setups as $setup) { foreach ($this->setups as $setup) {
call_user_func_array(array($res, array_shift($setup)), $setup); call_user_func_array([$res, array_shift($setup)], $setup);
} }
return $res; return $res;
} }
@@ -423,11 +430,11 @@ class DibiFluent extends DibiObject implements IDataSource
/** /**
* @return DibiDataSource * @return DataSource
*/ */
public function toDataSource() public function toDataSource()
{ {
return new DibiDataSource($this->connection->translate($this->_export()), $this->connection); return new DataSource($this->connection->translate($this->_export()), $this->connection);
} }
@@ -439,28 +446,32 @@ class DibiFluent extends DibiObject implements IDataSource
{ {
try { try {
return $this->connection->translate($this->_export()); return $this->connection->translate($this->_export());
} catch (Exception $e) { } catch (\Exception $e) {
trigger_error($e->getMessage(), E_USER_ERROR); trigger_error($e->getMessage(), E_USER_ERROR);
} }
} }
/** /**
* Generates parameters for DibiTranslator. * Generates parameters for Translator.
* @param string clause name * @param string clause name
* @return array * @return array
*/ */
protected function _export($clause = NULL, $args = array()) protected function _export($clause = NULL, $args = [])
{ {
if ($clause === NULL) { if ($clause === NULL) {
$data = $this->clauses; $data = $this->clauses;
if ($this->command === 'SELECT' && ($data['LIMIT'] || $data['OFFSET'])) {
$args = array_merge(['%lmt %ofs', $data['LIMIT'][0], $data['OFFSET'][0]], $args);
unset($data['LIMIT'], $data['OFFSET']);
}
} else { } else {
$clause = self::$normalizer->$clause; $clause = self::$normalizer->$clause;
if (array_key_exists($clause, $this->clauses)) { if (array_key_exists($clause, $this->clauses)) {
$data = array($clause => $this->clauses[$clause]); $data = [$clause => $this->clauses[$clause]];
} else { } else {
return array(); return [];
} }
} }

View File

@@ -5,30 +5,26 @@
* Copyright (c) 2005 David Grudl (https://davidgrudl.com) * Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/ */
namespace Dibi;
/** /**
* Lazy cached storage. * Lazy cached storage.
*
* @package dibi
* @internal * @internal
*/ */
abstract class DibiHashMapBase abstract class HashMapBase
{ {
private $callback; private $callback;
public function __construct($callback) public function __construct(callable $callback)
{ {
$this->setCallback($callback); $this->callback = $callback;
} }
public function setCallback($callback) public function setCallback(callable $callback)
{ {
if (!is_callable($callback)) {
$able = is_callable($callback, TRUE, $textual);
throw new InvalidArgumentException("Handler '$textual' is not " . ($able ? 'callable.' : 'valid PHP callback.'));
}
$this->callback = $callback; $this->callback = $callback;
} }
@@ -46,7 +42,7 @@ abstract class DibiHashMapBase
* *
* @internal * @internal
*/ */
final class DibiHashMap extends DibiHashMapBase final class HashMap extends HashMapBase
{ {
public function __set($nm, $val) public function __set($nm, $val)

277
src/Dibi/Helpers.php Normal file
View File

@@ -0,0 +1,277 @@
<?php
/**
* This file is part of the "dibi" - smart database abstraction layer.
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/
namespace Dibi;
class Helpers
{
use Strict;
/** @var array */
private static $types;
/**
* Prints out a syntax highlighted version of the SQL command or Result.
* @param string|Result
* @param bool return output instead of printing it?
* @return string
*/
public static function dump($sql = NULL, $return = FALSE)
{
ob_start();
if ($sql instanceof Result && PHP_SAPI === 'cli') {
$hasColors = (substr(getenv('TERM'), 0, 5) === 'xterm');
$maxLen = 0;
foreach ($sql as $i => $row) {
if ($i === 0) {
foreach ($row as $col => $foo) {
$len = mb_strlen($col);
$maxLen = max($len, $maxLen);
}
}
echo $hasColors ? "\033[1;37m#row: $i\033[0m\n" : "#row: $i\n";
foreach ($row as $col => $val) {
$spaces = $maxLen - mb_strlen($col) + 2;
echo "$col" . str_repeat(' ', $spaces) . "$val\n";
}
echo "\n";
}
echo empty($row) ? "empty result set\n\n" : "\n";
} elseif ($sql instanceof Result) {
foreach ($sql as $i => $row) {
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) {
echo "\t\t<td>", htmlSpecialChars($col), "</td>\n";
}
echo "\t</tr>\n";
}
echo empty($row)
? '<p><em>empty result set</em></p>'
: "</tbody>\n</table>\n";
} else {
if ($sql === NULL) {
$sql = \dibi::$sql;
}
static $keywords1 = 'SELECT|(?:ON\s+DUPLICATE\s+KEY)?UPDATE|INSERT(?:\s+INTO)?|REPLACE(?:\s+INTO)?|DELETE|CALL|UNION|FROM|WHERE|HAVING|GROUP\s+BY|ORDER\s+BY|LIMIT|OFFSET|FETCH\s+NEXT|SET|VALUES|LEFT\s+JOIN|INNER\s+JOIN|TRUNCATE|START\s+TRANSACTION|BEGIN|COMMIT|ROLLBACK(?:\s+TO\s+SAVEPOINT)?|(?:RELEASE\s+)?SAVEPOINT';
static $keywords2 = 'ALL|DISTINCT|DISTINCTROW|IGNORE|AS|USING|ON|AND|OR|IN|IS|NOT|NULL|LIKE|RLIKE|REGEXP|TRUE|FALSE';
// insert new lines
$sql = " $sql ";
$sql = preg_replace("#(?<=[\\s,(])($keywords1)(?=[\\s,)])#i", "\n\$1", $sql);
// reduce spaces
$sql = preg_replace('#[ \t]{2,}#', ' ', $sql);
$sql = wordwrap($sql, 100);
$sql = preg_replace("#([ \t]*\r?\n){2,}#", "\n", $sql);
// syntax highlight
$highlighter = "#(/\\*.+?\\*/)|(\\*\\*.+?\\*\\*)|(?<=[\\s,(])($keywords1)(?=[\\s,)])|(?<=[\\s,(=])($keywords2)(?=[\\s,)=])#is";
if (PHP_SAPI === 'cli') {
if (substr(getenv('TERM'), 0, 5) === 'xterm') {
$sql = preg_replace_callback($highlighter, function ($m) {
if (!empty($m[1])) { // comment
return "\033[1;30m" . $m[1] . "\033[0m";
} elseif (!empty($m[2])) { // error
return "\033[1;31m" . $m[2] . "\033[0m";
} elseif (!empty($m[3])) { // most important keywords
return "\033[1;34m" . $m[3] . "\033[0m";
} elseif (!empty($m[4])) { // other keywords
return "\033[1;32m" . $m[4] . "\033[0m";
}
}, $sql);
}
echo trim($sql) . "\n\n";
} else {
$sql = htmlSpecialChars($sql);
$sql = preg_replace_callback($highlighter, function ($m) {
if (!empty($m[1])) { // comment
return '<em style="color:gray">' . $m[1] . '</em>';
} elseif (!empty($m[2])) { // error
return '<strong style="color:red">' . $m[2] . '</strong>';
} elseif (!empty($m[3])) { // most important keywords
return '<strong style="color:blue">' . $m[3] . '</strong>';
} elseif (!empty($m[4])) { // other keywords
return '<strong style="color:green">' . $m[4] . '</strong>';
}
}, $sql);
echo '<pre class="dump">', trim($sql), "</pre>\n\n";
}
}
if ($return) {
return ob_get_clean();
} else {
ob_end_flush();
}
}
/**
* Finds the best suggestion.
* @return string|NULL
* @internal
*/
public static function getSuggestion(array $items, $value)
{
$best = NULL;
$min = (strlen($value) / 4 + 1) * 10 + .1;
foreach (array_unique($items, SORT_REGULAR) as $item) {
$item = is_object($item) ? $item->getName() : $item;
if (($len = levenshtein($item, $value, 10, 11, 10)) > 0 && $len < $min) {
$min = $len;
$best = $item;
}
}
return $best;
}
/** @internal */
public static function escape(Driver $driver, $value, $type)
{
static $types = [
Type::TEXT => 'text',
Type::BINARY => 'binary',
Type::BOOL => 'bool',
Type::DATE => 'date',
Type::DATETIME => 'datetime',
\dibi::IDENTIFIER => 'identifier',
];
if (isset($types[$type])) {
return $driver->{'escape' . $types[$type]}($value);
} else {
throw new \InvalidArgumentException('Unsupported type.');
}
}
/**
* Heuristic type detection.
* @param string
* @return string|NULL
* @internal
*/
public static function detectType($type)
{
static $patterns = [
'^_' => Type::TEXT, // PostgreSQL arrays
'BYTEA|BLOB|BIN' => Type::BINARY,
'TEXT|CHAR|POINT|INTERVAL' => Type::TEXT,
'YEAR|BYTE|COUNTER|SERIAL|INT|LONG|SHORT|^TINY$' => Type::INTEGER,
'CURRENCY|REAL|MONEY|FLOAT|DOUBLE|DECIMAL|NUMERIC|NUMBER' => Type::FLOAT,
'^TIME$' => Type::TIME,
'TIME' => Type::DATETIME, // DATETIME, TIMESTAMP
'DATE' => Type::DATE,
'BOOL' => Type::BOOL,
];
foreach ($patterns as $s => $val) {
if (preg_match("#$s#i", $type)) {
return $val;
}
}
return NULL;
}
/**
* @internal
*/
public static function getTypeCache()
{
if (self::$types === NULL) {
self::$types = new HashMap([__CLASS__, 'detectType']);
}
return self::$types;
}
/**
* Apply configuration alias or default values.
* @param array connect configuration
* @param string key
* @param string alias key
* @return void
*/
public static function alias(& $config, $key, $alias)
{
$foo = & $config;
foreach (explode('|', $key) as $key) {
$foo = & $foo[$key];
}
if (!isset($foo) && isset($config[$alias])) {
$foo = $config[$alias];
unset($config[$alias]);
}
}
/**
* Import SQL dump from file.
* @return int count of sql commands
*/
public static function loadFromFile(Connection $connection, $file)
{
@set_time_limit(0); // intentionally @
$handle = @fopen($file, 'r'); // intentionally @
if (!$handle) {
throw new \RuntimeException("Cannot open file '$file'.");
}
$count = 0;
$delimiter = ';';
$sql = '';
$driver = $connection->getDriver();
while (!feof($handle)) {
$s = rtrim(fgets($handle));
if (substr($s, 0, 10) === 'DELIMITER ') {
$delimiter = substr($s, 10);
} elseif (substr($s, -strlen($delimiter)) === $delimiter) {
$sql .= substr($s, 0, -strlen($delimiter));
$driver->query($sql);
$sql = '';
$count++;
} else {
$sql .= $s . "\n";
}
}
if (trim($sql) !== '') {
$driver->query($sql);
$count++;
}
fclose($handle);
return $count;
}
}

View File

@@ -5,14 +5,16 @@
* Copyright (c) 2005 David Grudl (https://davidgrudl.com) * Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/ */
namespace Dibi;
/** /**
* SQL literal value. * SQL literal value.
*
* @package dibi
*/ */
class DibiLiteral extends DibiObject class Literal
{ {
use Strict;
/** @var string */ /** @var string */
private $value; private $value;

View File

@@ -5,14 +5,18 @@
* Copyright (c) 2005 David Grudl (https://davidgrudl.com) * Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/ */
namespace Dibi\Loggers;
use Dibi;
/** /**
* dibi file logger. * dibi file logger.
*
* @package dibi
*/ */
class DibiFileLogger extends DibiObject class FileLogger
{ {
use Dibi\Strict;
/** @var string Name of the file where SQL errors should be logged */ /** @var string Name of the file where SQL errors should be logged */
public $file; public $file;
@@ -23,7 +27,7 @@ class DibiFileLogger extends DibiObject
public function __construct($file, $filter = NULL) public function __construct($file, $filter = NULL)
{ {
$this->file = $file; $this->file = $file;
$this->filter = $filter ? (int) $filter : DibiEvent::QUERY; $this->filter = $filter ? (int) $filter : Dibi\Event::QUERY;
} }
@@ -31,7 +35,7 @@ class DibiFileLogger extends DibiObject
* After event notification. * After event notification.
* @return void * @return void
*/ */
public function logEvent(DibiEvent $event) public function logEvent(Dibi\Event $event)
{ {
if (($event->type & $this->filter) === 0) { if (($event->type & $this->filter) === 0) {
return; return;
@@ -43,7 +47,7 @@ class DibiFileLogger extends DibiObject
} }
flock($handle, LOCK_EX); flock($handle, LOCK_EX);
if ($event->result instanceof Exception) { if ($event->result instanceof \Exception) {
$message = $event->result->getMessage(); $message = $event->result->getMessage();
if ($code = $event->result->getCode()) { if ($code = $event->result->getCode()) {
$message = "[$code] $message"; $message = "[$code] $message";

View File

@@ -5,22 +5,26 @@
* Copyright (c) 2005 David Grudl (https://davidgrudl.com) * Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/ */
namespace Dibi\Loggers;
use Dibi;
/** /**
* dibi FirePHP logger. * dibi FirePHP logger.
*
* @package dibi
*/ */
class DibiFirePhpLogger extends DibiObject class FirePhpLogger
{ {
use Dibi\Strict;
/** maximum number of rows */ /** maximum number of rows */
static public $maxQueries = 30; public static $maxQueries = 30;
/** maximum SQL length */ /** maximum SQL length */
static public $maxLength = 1000; public static $maxLength = 1000;
/** size of json stream chunk */ /** size of json stream chunk */
static public $streamChunkSize = 4990; public static $streamChunkSize = 4990;
/** @var int */ /** @var int */
public $filter; public $filter;
@@ -32,7 +36,7 @@ class DibiFirePhpLogger extends DibiObject
public $numOfQueries = 0; public $numOfQueries = 0;
/** @var array */ /** @var array */
private static $fireTable = array(array('Time', 'SQL Statement', 'Rows', 'Connection')); private static $fireTable = [['Time', 'SQL Statement', 'Rows', 'Connection']];
/** /**
@@ -46,7 +50,7 @@ class DibiFirePhpLogger extends DibiObject
public function __construct($filter = NULL) public function __construct($filter = NULL)
{ {
$this->filter = $filter ? (int) $filter : DibiEvent::QUERY; $this->filter = $filter ? (int) $filter : Dibi\Event::QUERY;
} }
@@ -54,7 +58,7 @@ class DibiFirePhpLogger extends DibiObject
* After event notification. * After event notification.
* @return void * @return void
*/ */
public function logEvent(DibiEvent $event) public function logEvent(Dibi\Event $event)
{ {
if (headers_sent() || ($event->type & $this->filter) === 0 || count(self::$fireTable) > self::$maxQueries) { if (headers_sent() || ($event->type & $this->filter) === 0 || count(self::$fireTable) > self::$maxQueries) {
return; return;
@@ -67,20 +71,20 @@ class DibiFirePhpLogger extends DibiObject
} }
$this->totalTime += $event->time; $this->totalTime += $event->time;
$this->numOfQueries++; $this->numOfQueries++;
self::$fireTable[] = array( self::$fireTable[] = [
sprintf('%0.3f', $event->time * 1000), sprintf('%0.3f', $event->time * 1000),
strlen($event->sql) > self::$maxLength ? substr($event->sql, 0, self::$maxLength) . '...' : $event->sql, strlen($event->sql) > self::$maxLength ? substr($event->sql, 0, self::$maxLength) . '...' : $event->sql,
$event->result instanceof Exception ? 'ERROR' : (string) $event->count, $event->result instanceof \Exception ? 'ERROR' : (string) $event->count,
$event->connection->getConfig('driver') . '/' . $event->connection->getConfig('name'), $event->connection->getConfig('driver') . '/' . $event->connection->getConfig('name'),
); ];
$payload = json_encode(array( $payload = json_encode([
array( [
'Type' => 'TABLE', 'Type' => 'TABLE',
'Label' => 'dibi profiler (' . $this->numOfQueries . ' SQL queries took ' . sprintf('%0.3f', $this->totalTime * 1000) . ' ms)', 'Label' => 'dibi profiler (' . $this->numOfQueries . ' SQL queries took ' . sprintf('%0.3f', $this->totalTime * 1000) . ' ms)',
), ],
self::$fireTable, self::$fireTable,
)); ]);
foreach (str_split($payload, self::$streamChunkSize) as $num => $s) { foreach (str_split($payload, self::$streamChunkSize) as $num => $s) {
$num++; $num++;
header("X-Wf-dibi-1-1-d$num: |$s|\\"); // protocol-, structure-, plugin-, message-index header("X-Wf-dibi-1-1-d$num: |$s|\\"); // protocol-, structure-, plugin-, message-index

View File

@@ -0,0 +1,166 @@
<?php
/**
* This file is part of the "dibi" - smart database abstraction layer.
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/
namespace Dibi\Reflection;
use Dibi;
use Dibi\Type;
/**
* Reflection metadata class for a table or result set column.
*
* @property-read string $name
* @property-read string $fullName
* @property-read Table $table
* @property-read string $type
* @property-read mixed $nativeType
* @property-read int $size
* @property-read bool $unsigned
* @property-read bool $nullable
* @property-read bool $autoIncrement
* @property-read mixed $default
*/
class Column
{
use Dibi\Strict;
/** @var Dibi\Reflector|NULL when created by Result */
private $reflector;
/** @var array (name, nativetype, [table], [fullname], [size], [nullable], [default], [autoincrement], [vendor]) */
private $info;
public function __construct(Dibi\Reflector $reflector = NULL, array $info)
{
$this->reflector = $reflector;
$this->info = $info;
}
/**
* @return string
*/
public function getName()
{
return $this->info['name'];
}
/**
* @return string
*/
public function getFullName()
{
return isset($this->info['fullname']) ? $this->info['fullname'] : NULL;
}
/**
* @return bool
*/
public function hasTable()
{
return !empty($this->info['table']);
}
/**
* @return Table
*/
public function getTable()
{
if (empty($this->info['table']) || !$this->reflector) {
throw new Dibi\Exception("Table is unknown or not available.");
}
return new Table($this->reflector, ['name' => $this->info['table']]);
}
/**
* @return string
*/
public function getTableName()
{
return isset($this->info['table']) && $this->info['table'] != NULL ? $this->info['table'] : NULL; // intentionally ==
}
/**
* @return string
*/
public function getType()
{
return Dibi\Helpers::getTypeCache()->{$this->info['nativetype']};
}
/**
* @return mixed
*/
public function getNativeType()
{
return $this->info['nativetype'];
}
/**
* @return int
*/
public function getSize()
{
return isset($this->info['size']) ? (int) $this->info['size'] : NULL;
}
/**
* @return bool
*/
public function isUnsigned()
{
return isset($this->info['unsigned']) ? (bool) $this->info['unsigned'] : NULL;
}
/**
* @return bool
*/
public function isNullable()
{
return isset($this->info['nullable']) ? (bool) $this->info['nullable'] : NULL;
}
/**
* @return bool
*/
public function isAutoIncrement()
{
return isset($this->info['autoincrement']) ? (bool) $this->info['autoincrement'] : NULL;
}
/**
* @return mixed
*/
public function getDefault()
{
return isset($this->info['default']) ? $this->info['default'] : NULL;
}
/**
* @param string
* @return mixed
*/
public function getVendorInfo($key)
{
return isset($this->info['vendor'][$key]) ? $this->info['vendor'][$key] : NULL;
}
}

View File

@@ -0,0 +1,115 @@
<?php
/**
* This file is part of the "dibi" - smart database abstraction layer.
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/
namespace Dibi\Reflection;
use Dibi;
/**
* Reflection metadata class for a database.
*
* @property-read string $name
* @property-read array $tables
* @property-read array $tableNames
*/
class Database
{
use Dibi\Strict;
/** @var Dibi\Reflector */
private $reflector;
/** @var string */
private $name;
/** @var array */
private $tables;
public function __construct(Dibi\Reflector $reflector, $name)
{
$this->reflector = $reflector;
$this->name = $name;
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @return Table[]
*/
public function getTables()
{
$this->init();
return array_values($this->tables);
}
/**
* @return string[]
*/
public function getTableNames()
{
$this->init();
$res = [];
foreach ($this->tables as $table) {
$res[] = $table->getName();
}
return $res;
}
/**
* @param string
* @return Table
*/
public function getTable($name)
{
$this->init();
$l = strtolower($name);
if (isset($this->tables[$l])) {
return $this->tables[$l];
} else {
throw new Dibi\Exception("Database '$this->name' has no table '$name'.");
}
}
/**
* @param string
* @return bool
*/
public function hasTable($name)
{
$this->init();
return isset($this->tables[strtolower($name)]);
}
/**
* @return void
*/
protected function init()
{
if ($this->tables === NULL) {
$this->tables = [];
foreach ($this->reflector->getTables() as $info) {
$this->tables[strtolower($info['name'])] = new Table($this->reflector, $info);
}
}
}
}

View File

@@ -0,0 +1,54 @@
<?php
/**
* This file is part of the "dibi" - smart database abstraction layer.
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/
namespace Dibi\Reflection;
use Dibi;
/**
* Reflection metadata class for a foreign key.
*
* @property-read string $name
* @property-read array $references
*/
class ForeignKey
{
use Dibi\Strict;
/** @var string */
private $name;
/** @var array of [local, foreign, onDelete, onUpdate] */
private $references;
public function __construct($name, array $references)
{
$this->name = $name;
$this->references = $references;
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @return array
*/
public function getReferences()
{
return $this->references;
}
}

View File

@@ -0,0 +1,70 @@
<?php
/**
* This file is part of the "dibi" - smart database abstraction layer.
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/
namespace Dibi\Reflection;
use Dibi;
/**
* Reflection metadata class for a index or primary key.
*
* @property-read string $name
* @property-read array $columns
* @property-read bool $unique
* @property-read bool $primary
*/
class Index
{
use Dibi\Strict;
/** @var array (name, columns, [unique], [primary]) */
private $info;
public function __construct(array $info)
{
$this->info = $info;
}
/**
* @return string
*/
public function getName()
{
return $this->info['name'];
}
/**
* @return array
*/
public function getColumns()
{
return $this->info['columns'];
}
/**
* @return bool
*/
public function isUnique()
{
return !empty($this->info['unique']);
}
/**
* @return bool
*/
public function isPrimary()
{
return !empty($this->info['primary']);
}
}

View File

@@ -0,0 +1,106 @@
<?php
/**
* This file is part of the "dibi" - smart database abstraction layer.
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/
namespace Dibi\Reflection;
use Dibi;
/**
* Reflection metadata class for a result set.
*
* @property-read array $columns
* @property-read array $columnNames
*/
class Result
{
use Dibi\Strict;
/** @var Dibi\ResultDriver */
private $driver;
/** @var array */
private $columns;
/** @var array */
private $names;
public function __construct(Dibi\ResultDriver $driver)
{
$this->driver = $driver;
}
/**
* @return Column[]
*/
public function getColumns()
{
$this->initColumns();
return array_values($this->columns);
}
/**
* @param bool
* @return string[]
*/
public function getColumnNames($fullNames = FALSE)
{
$this->initColumns();
$res = [];
foreach ($this->columns as $column) {
$res[] = $fullNames ? $column->getFullName() : $column->getName();
}
return $res;
}
/**
* @param string
* @return Column
*/
public function getColumn($name)
{
$this->initColumns();
$l = strtolower($name);
if (isset($this->names[$l])) {
return $this->names[$l];
} else {
throw new Dibi\Exception("Result set has no column '$name'.");
}
}
/**
* @param string
* @return bool
*/
public function hasColumn($name)
{
$this->initColumns();
return isset($this->names[strtolower($name)]);
}
/**
* @return void
*/
protected function initColumns()
{
if ($this->columns === NULL) {
$this->columns = [];
$reflector = $this->driver instanceof Dibi\Reflector ? $this->driver : NULL;
foreach ($this->driver->getResultColumns() as $info) {
$this->columns[] = $this->names[strtolower($info['name'])] = new Column($reflector, $info);
}
}
}
}

View File

@@ -0,0 +1,201 @@
<?php
/**
* This file is part of the "dibi" - smart database abstraction layer.
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/
namespace Dibi\Reflection;
use Dibi;
/**
* Reflection metadata class for a database table.
*
* @property-read string $name
* @property-read bool $view
* @property-read array $columns
* @property-read array $columnNames
* @property-read array $foreignKeys
* @property-read array $indexes
* @property-read Index $primaryKey
*/
class Table
{
use Dibi\Strict;
/** @var Dibi\Reflector */
private $reflector;
/** @var string */
private $name;
/** @var bool */
private $view;
/** @var array */
private $columns;
/** @var array */
private $foreignKeys;
/** @var array */
private $indexes;
/** @var Index */
private $primaryKey;
public function __construct(Dibi\Reflector $reflector, array $info)
{
$this->reflector = $reflector;
$this->name = $info['name'];
$this->view = !empty($info['view']);
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @return bool
*/
public function isView()
{
return $this->view;
}
/**
* @return Column[]
*/
public function getColumns()
{
$this->initColumns();
return array_values($this->columns);
}
/**
* @return string[]
*/
public function getColumnNames()
{
$this->initColumns();
$res = [];
foreach ($this->columns as $column) {
$res[] = $column->getName();
}
return $res;
}
/**
* @param string
* @return Column
*/
public function getColumn($name)
{
$this->initColumns();
$l = strtolower($name);
if (isset($this->columns[$l])) {
return $this->columns[$l];
} else {
throw new Dibi\Exception("Table '$this->name' has no column '$name'.");
}
}
/**
* @param string
* @return bool
*/
public function hasColumn($name)
{
$this->initColumns();
return isset($this->columns[strtolower($name)]);
}
/**
* @return ForeignKey[]
*/
public function getForeignKeys()
{
$this->initForeignKeys();
return $this->foreignKeys;
}
/**
* @return Index[]
*/
public function getIndexes()
{
$this->initIndexes();
return $this->indexes;
}
/**
* @return Index
*/
public function getPrimaryKey()
{
$this->initIndexes();
return $this->primaryKey;
}
/**
* @return void
*/
protected function initColumns()
{
if ($this->columns === NULL) {
$this->columns = [];
foreach ($this->reflector->getColumns($this->name) as $info) {
$this->columns[strtolower($info['name'])] = new Column($this->reflector, $info);
}
}
}
/**
* @return void
*/
protected function initIndexes()
{
if ($this->indexes === NULL) {
$this->initColumns();
$this->indexes = [];
foreach ($this->reflector->getIndexes($this->name) as $info) {
foreach ($info['columns'] as $key => $name) {
$info['columns'][$key] = $this->columns[strtolower($name)];
}
$this->indexes[strtolower($info['name'])] = new Index($info);
if (!empty($info['primary'])) {
$this->primaryKey = $this->indexes[strtolower($info['name'])];
}
}
}
}
/**
* @return void
*/
protected function initForeignKeys()
{
throw new Dibi\NotImplementedException;
}
}

View File

@@ -5,6 +5,8 @@
* Copyright (c) 2005 David Grudl (https://davidgrudl.com) * Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/ */
namespace Dibi;
/** /**
* dibi result set. * dibi result set.
@@ -16,42 +18,42 @@
* $value = $result->fetchSingle(); * $value = $result->fetchSingle();
* $table = $result->fetchAll(); * $table = $result->fetchAll();
* $pairs = $result->fetchPairs(); * $pairs = $result->fetchPairs();
* $assoc = $result->fetchAssoc('id'); * $assoc = $result->fetchAssoc('col1');
* $assoc = $result->fetchAssoc('active,#,id'); * $assoc = $result->fetchAssoc('col1[]col2->col3');
* *
* unset($result); * unset($result);
* </code> * </code>
* *
* @package dibi
*
* @property-read int $rowCount * @property-read int $rowCount
*/ */
class DibiResult extends DibiObject implements IDataSource class Result implements IDataSource
{ {
/** @var array IDibiResultDriver */ use Strict;
/** @var array ResultDriver */
private $driver; private $driver;
/** @var array Translate table */ /** @var array Translate table */
private $types = array(); private $types = [];
/** @var DibiResultInfo */ /** @var Reflection\Result */
private $meta; private $meta;
/** @var bool Already fetched? Used for allowance for first seek(0) */ /** @var bool Already fetched? Used for allowance for first seek(0) */
private $fetched = FALSE; private $fetched = FALSE;
/** @var string returned object class */ /** @var string returned object class */
private $rowClass = 'DibiRow'; private $rowClass = 'Dibi\Row';
/** @var Callback returned object factory*/ /** @var callable returned object factory*/
private $rowFactory; private $rowFactory;
/** @var array format */ /** @var array format */
private $formats = array(); private $formats = [];
/** /**
* @param IDibiResultDriver * @param ResultDriver
*/ */
public function __construct($driver) public function __construct($driver)
{ {
@@ -65,6 +67,7 @@ class DibiResult extends DibiObject implements IDataSource
*/ */
final public function getResource() final public function getResource()
{ {
trigger_error(__METHOD__ . '() is deprecated, use getResultDriver()->getResultResource().', E_USER_DEPRECATED);
return $this->getResultDriver()->getResultResource(); return $this->getResultDriver()->getResultResource();
} }
@@ -84,13 +87,13 @@ class DibiResult extends DibiObject implements IDataSource
/** /**
* Safe access to property $driver. * Safe access to property $driver.
* @return IDibiResultDriver * @return ResultDriver
* @throws RuntimeException * @throws \RuntimeException
*/ */
final public function getResultDriver() final public function getResultDriver()
{ {
if ($this->driver === NULL) { if ($this->driver === NULL) {
throw new RuntimeException('Result-set was released from memory.'); throw new \RuntimeException('Result-set was released from memory.');
} }
return $this->driver; return $this->driver;
@@ -104,7 +107,7 @@ class DibiResult extends DibiObject implements IDataSource
* Moves cursor position without fetching row. * Moves cursor position without fetching row.
* @param int the 0-based cursor pos to seek to * @param int the 0-based cursor pos to seek to
* @return bool TRUE on success, FALSE if unable to seek to specified record * @return bool TRUE on success, FALSE if unable to seek to specified record
* @throws DibiException * @throws Exception
*/ */
final public function seek($row) final public function seek($row)
{ {
@@ -134,11 +137,11 @@ class DibiResult extends DibiObject implements IDataSource
/** /**
* Required by the IteratorAggregate interface. * Required by the IteratorAggregate interface.
* @return DibiResultIterator * @return ResultIterator
*/ */
final public function getIterator() final public function getIterator()
{ {
return new DibiResultIterator($this); return new ResultIterator($this);
} }
@@ -146,7 +149,7 @@ class DibiResult extends DibiObject implements IDataSource
/** /**
* Set fetched object class. This class should extend the DibiRow class. * Set fetched object class. This class should extend the Row class.
* @param string * @param string
* @return self * @return self
*/ */
@@ -168,11 +171,10 @@ class DibiResult extends DibiObject implements IDataSource
/** /**
* Set a factory to create fetched object instances. These should extend the DibiRow class. * Set a factory to create fetched object instances. These should extend the Row class.
* @param callback
* @return self * @return self
*/ */
public function setRowFactory($callback) public function setRowFactory(callable $callback)
{ {
$this->rowFactory = $callback; $this->rowFactory = $callback;
return $this; return $this;
@@ -182,7 +184,7 @@ class DibiResult extends DibiObject implements IDataSource
/** /**
* Fetches the row at current position, process optional type conversion. * Fetches the row at current position, process optional type conversion.
* and moves the internal cursor to the next position * and moves the internal cursor to the next position
* @return DibiRow|FALSE array on success, FALSE if no next record * @return Row|FALSE array on success, FALSE if no next record
*/ */
final public function fetch() final public function fetch()
{ {
@@ -221,7 +223,7 @@ class DibiResult extends DibiObject implements IDataSource
* Fetches all records from table. * Fetches all records from table.
* @param int offset * @param int offset
* @param int limit * @param int limit
* @return DibiRow[] * @return Row[]
*/ */
final public function fetchAll($offset = NULL, $limit = NULL) final public function fetchAll($offset = NULL, $limit = NULL)
{ {
@@ -229,10 +231,10 @@ class DibiResult extends DibiObject implements IDataSource
$this->seek((int) $offset); $this->seek((int) $offset);
$row = $this->fetch(); $row = $this->fetch();
if (!$row) { if (!$row) {
return array(); // empty result set return []; // empty result set
} }
$data = array(); $data = [];
do { do {
if ($limit === 0) { if ($limit === 0) {
break; break;
@@ -253,8 +255,8 @@ class DibiResult extends DibiObject implements IDataSource
* - associative descriptor: col1|col2->col3=col4 * - associative descriptor: col1|col2->col3=col4
* builds a tree: $tree[$val1][$val2]->col3[$val3] = val4 * builds a tree: $tree[$val1][$val2]->col3[$val3] = val4
* @param string associative descriptor * @param string associative descriptor
* @return DibiRow * @return array
* @throws InvalidArgumentException * @throws \InvalidArgumentException
*/ */
final public function fetchAssoc($assoc) final public function fetchAssoc($assoc)
{ {
@@ -265,7 +267,7 @@ class DibiResult extends DibiObject implements IDataSource
$this->seek(0); $this->seek(0);
$row = $this->fetch(); $row = $this->fetch();
if (!$row) { if (!$row) {
return array(); // empty result set return []; // empty result set
} }
$data = NULL; $data = NULL;
@@ -275,7 +277,7 @@ class DibiResult extends DibiObject implements IDataSource
foreach ($assoc as $as) { foreach ($assoc as $as) {
// offsetExists ignores NULL in PHP 5.2.1, isset() surprisingly NULL accepts // offsetExists ignores NULL in PHP 5.2.1, isset() surprisingly NULL accepts
if ($as !== '[]' && $as !== '=' && $as !== '->' && $as !== '|' && !property_exists($row, $as)) { if ($as !== '[]' && $as !== '=' && $as !== '->' && $as !== '|' && !property_exists($row, $as)) {
throw new InvalidArgumentException("Unknown column '$as' in associative descriptor."); throw new \InvalidArgumentException("Unknown column '$as' in associative descriptor.");
} }
} }
@@ -333,7 +335,7 @@ class DibiResult extends DibiObject implements IDataSource
$this->seek(0); $this->seek(0);
$row = $this->fetch(); $row = $this->fetch();
if (!$row) { if (!$row) {
return array(); // empty result set return []; // empty result set
} }
$data = NULL; $data = NULL;
@@ -403,21 +405,21 @@ class DibiResult extends DibiObject implements IDataSource
* @param string associative key * @param string associative key
* @param string value * @param string value
* @return array * @return array
* @throws InvalidArgumentException * @throws \InvalidArgumentException
*/ */
final public function fetchPairs($key = NULL, $value = NULL) final public function fetchPairs($key = NULL, $value = NULL)
{ {
$this->seek(0); $this->seek(0);
$row = $this->fetch(); $row = $this->fetch();
if (!$row) { if (!$row) {
return array(); // empty result set return []; // empty result set
} }
$data = array(); $data = [];
if ($value === NULL) { if ($value === NULL) {
if ($key !== NULL) { if ($key !== NULL) {
throw new InvalidArgumentException('Either none or both columns must be specified.'); throw new \InvalidArgumentException('Either none or both columns must be specified.');
} }
// autodetect // autodetect
@@ -434,7 +436,7 @@ class DibiResult extends DibiObject implements IDataSource
} else { } else {
if (!property_exists($row, $value)) { if (!property_exists($row, $value)) {
throw new InvalidArgumentException("Unknown value column '$value'."); throw new \InvalidArgumentException("Unknown value column '$value'.");
} }
if ($key === NULL) { // indexed-array if ($key === NULL) { // indexed-array
@@ -445,7 +447,7 @@ class DibiResult extends DibiObject implements IDataSource
} }
if (!property_exists($row, $key)) { if (!property_exists($row, $key)) {
throw new InvalidArgumentException("Unknown key column '$key'."); throw new \InvalidArgumentException("Unknown key column '$key'.");
} }
} }
@@ -466,12 +468,12 @@ class DibiResult extends DibiObject implements IDataSource
*/ */
private function detectTypes() private function detectTypes()
{ {
$cache = DibiColumnInfo::getTypeCache(); $cache = Helpers::getTypeCache();
try { try {
foreach ($this->getResultDriver()->getResultColumns() as $col) { foreach ($this->getResultDriver()->getResultColumns() as $col) {
$this->types[$col['name']] = $cache->{$col['nativetype']}; $this->types[$col['name']] = isset($col['type']) ? $col['type'] : $cache->{$col['nativetype']};
} }
} catch (DibiNotSupportedException $e) { } catch (NotSupportedException $e) {
} }
} }
@@ -488,25 +490,45 @@ class DibiResult extends DibiObject implements IDataSource
continue; continue;
} }
$value = $row[$key]; $value = $row[$key];
if ($value === FALSE || $type === dibi::TEXT) { if ($type === Type::TEXT) {
$row[$key] = (string) $value;
} elseif ($type === dibi::INTEGER) { } elseif ($type === Type::INTEGER) {
$row[$key] = is_float($tmp = $value * 1) ? $value : $tmp; $row[$key] = is_float($tmp = $value * 1)
? (is_string($value) ? $value : (int) $value)
: $tmp;
} elseif ($type === dibi::FLOAT) { } elseif ($type === Type::FLOAT) {
$row[$key] = str_replace(',', '.', ltrim((string) ($tmp = (float) $value), '0')) === ltrim(rtrim(rtrim($value, '0'), '.'), '0') ? $tmp : $value; $value = ltrim($value, '0');
$p = strpos($value, '.');
if ($p !== FALSE) {
$value = rtrim(rtrim($value, '0'), '.');
}
if ($value === '' || $value[0] === '.') {
$value = '0' . $value;
}
$row[$key] = $value === str_replace(',', '.', (string) ($float = (float) $value))
? $float
: $value;
} elseif ($type === dibi::BOOL) { } elseif ($type === Type::BOOL) {
$row[$key] = ((bool) $value) && $value !== 'f' && $value !== 'F'; $row[$key] = ((bool) $value) && $value !== 'f' && $value !== 'F';
} elseif ($type === dibi::DATE || $type === dibi::DATETIME) { } elseif ($type === Type::DATETIME || $type === Type::DATE || $type === Type::TIME) {
if ((int) $value !== 0 || substr((string) $value, 0, 3) === '00:') { // '', NULL, FALSE, '0000-00-00', ... if ((int) $value !== 0 || substr((string) $value, 0, 3) === '00:') { // '', NULL, FALSE, '0000-00-00', ...
$value = new DibiDateTime($value); $value = new DateTime($value);
$row[$key] = empty($this->formats[$type]) ? $value : $value->format($this->formats[$type]); $row[$key] = empty($this->formats[$type]) ? $value : $value->format($this->formats[$type]);
} else {
$row[$key] = NULL;
} }
} elseif ($type === dibi::BINARY) { } elseif ($type === Type::TIME_INTERVAL) {
$row[$key] = $this->getResultDriver()->unescape($value, $type); preg_match('#^(-?)(\d+)\D(\d+)\D(\d+)\z#', $value, $m);
$row[$key] = new \DateInterval("PT$m[2]H$m[3]M$m[4]S");
$row[$key]->invert = (int) (bool) $m[1];
} elseif ($type === Type::BINARY) {
$row[$key] = $this->getResultDriver()->unescapeBinary($value);
} }
} }
} }
@@ -515,7 +537,7 @@ class DibiResult extends DibiObject implements IDataSource
/** /**
* Define column type. * Define column type.
* @param string column * @param string column
* @param string type (use constant Dibi::*) * @param string type (use constant Type::*)
* @return self * @return self
*/ */
final public function setType($col, $type) final public function setType($col, $type)
@@ -537,7 +559,7 @@ class DibiResult extends DibiObject implements IDataSource
/** /**
* Sets data format. * Sets data format.
* @param string type (use constant Dibi::*) * @param string type (use constant Type::*)
* @param string format * @param string format
* @return self * @return self
*/ */
@@ -563,19 +585,19 @@ class DibiResult extends DibiObject implements IDataSource
/** /**
* Returns a meta information about the current result set. * Returns a meta information about the current result set.
* @return DibiResultInfo * @return Reflection\Result
*/ */
public function getInfo() public function getInfo()
{ {
if ($this->meta === NULL) { if ($this->meta === NULL) {
$this->meta = new DibiResultInfo($this->getResultDriver()); $this->meta = new Reflection\Result($this->getResultDriver());
} }
return $this->meta; return $this->meta;
} }
/** /**
* @deprecated * @return Reflection\Column[]
*/ */
final public function getColumns() final public function getColumns()
{ {
@@ -592,66 +614,7 @@ class DibiResult extends DibiObject implements IDataSource
*/ */
final public function dump() final public function dump()
{ {
$i = 0; echo Helpers::dump($this);
$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++;
}
if ($i === 0) {
echo "empty result set\n";
}
echo "\n";
} 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";
}
}
} }
} }

View File

@@ -5,11 +5,13 @@
* Copyright (c) 2005 David Grudl (https://davidgrudl.com) * Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/ */
namespace Dibi;
/** /**
* External result set iterator. * External result set iterator.
* *
* This can be returned by DibiResult::getIterator() method or using foreach * This can be returned by Result::getIterator() method or using foreach
* <code> * <code>
* $result = dibi::query('SELECT * FROM table'); * $result = dibi::query('SELECT * FROM table');
* foreach ($result as $row) { * foreach ($result as $row) {
@@ -17,12 +19,12 @@
* } * }
* unset($result); * unset($result);
* </code> * </code>
*
* @package dibi
*/ */
class DibiResultIterator implements Iterator, Countable class ResultIterator implements \Iterator, \Countable
{ {
/** @var DibiResult */ use Strict;
/** @var Result */
private $result; private $result;
/** @var int */ /** @var int */
@@ -33,9 +35,9 @@ class DibiResultIterator implements Iterator, Countable
/** /**
* @param DibiResult * @param Result
*/ */
public function __construct(DibiResult $result) public function __construct(Result $result)
{ {
$this->result = $result; $this->result = $result;
} }

View File

@@ -5,13 +5,13 @@
* Copyright (c) 2005 David Grudl (https://davidgrudl.com) * Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/ */
namespace Dibi;
/** /**
* Result set single row. * Result set single row.
*
* @package dibi
*/ */
class DibiRow implements ArrayAccess, IteratorAggregate, Countable class Row implements \ArrayAccess, \IteratorAggregate, \Countable
{ {
public function __construct($arr) public function __construct($arr)
@@ -32,21 +32,28 @@ class DibiRow implements ArrayAccess, IteratorAggregate, Countable
* Converts value to DateTime object. * Converts value to DateTime object.
* @param string key * @param string key
* @param string format * @param string format
* @return DateTime * @return \DateTime
*/ */
public function asDateTime($key, $format = NULL) public function asDateTime($key, $format = NULL)
{ {
$time = $this[$key]; $time = $this[$key];
if (!$time instanceof DibiDateTime) { if (!$time instanceof DateTime) {
if ((int) $time === 0 && substr((string) $time, 0, 3) !== '00:') { // '', NULL, FALSE, '0000-00-00', ... if ((int) $time === 0 && substr((string) $time, 0, 3) !== '00:') { // '', NULL, FALSE, '0000-00-00', ...
return NULL; return NULL;
} }
$time = new DibiDateTime($time); $time = new DateTime($time);
} }
return $format === NULL ? $time : $time->format($format); return $format === NULL ? $time : $time->format($format);
} }
public function __get($key)
{
$hint = Helpers::getSuggestion(array_keys((array) $this), $key);
trigger_error("Attempt to read missing column '$key'" . ($hint ? ", did you mean '$hint'?" : '.'), E_USER_NOTICE);
}
/********************* interfaces ArrayAccess, Countable & IteratorAggregate ****************d*g**/ /********************* interfaces ArrayAccess, Countable & IteratorAggregate ****************d*g**/
@@ -58,7 +65,7 @@ class DibiRow implements ArrayAccess, IteratorAggregate, Countable
final public function getIterator() final public function getIterator()
{ {
return new ArrayIterator($this); return new \ArrayIterator($this);
} }

152
src/Dibi/Strict.php Normal file
View File

@@ -0,0 +1,152 @@
<?php
/**
* This file is part of the "dibi" - smart database abstraction layer.
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/
namespace Dibi;
use ReflectionClass;
use ReflectionMethod;
use ReflectionProperty;
/**
* Better OOP experience.
*/
trait Strict
{
/** @var array [method => [type => callback]] */
private static $extMethods;
/**
* Call to undefined method.
* @throws \LogicException
*/
public function __call($name, $args)
{
if ($cb = self::extensionMethod(get_class($this) . '::' . $name)) { // back compatiblity
array_unshift($args, $this);
return call_user_func_array($cb, $args);
}
$class = method_exists($this, $name) ? 'parent' : get_class($this);
$items = (new ReflectionClass($this))->getMethods(ReflectionMethod::IS_PUBLIC);
$hint = ($t = Helpers::getSuggestion($items, $name)) ? ", did you mean $t()?" : '.';
throw new \LogicException("Call to undefined method $class::$name()$hint");
}
/**
* Call to undefined static method.
* @throws \LogicException
*/
public static function __callStatic($name, $args)
{
$rc = new ReflectionClass(get_called_class());
$items = array_intersect($rc->getMethods(ReflectionMethod::IS_PUBLIC), $rc->getMethods(ReflectionMethod::IS_STATIC));
$hint = ($t = Helpers::getSuggestion($items, $name)) ? ", did you mean $t()?" : '.';
throw new \LogicException("Call to undefined static method {$rc->getName()}::$name()$hint");
}
/**
* Access to undeclared property.
* @throws \LogicException
*/
public function &__get($name)
{
if ((method_exists($this, $m = 'get' . $name) || method_exists($this, $m = 'is' . $name))
&& (new ReflectionMethod($this, $m))->isPublic()
) { // back compatiblity
$ret = $this->$m();
return $ret;
}
$rc = new ReflectionClass($this);
$items = array_diff($rc->getProperties(ReflectionProperty::IS_PUBLIC), $rc->getProperties(ReflectionProperty::IS_STATIC));
$hint = ($t = Helpers::getSuggestion($items, $name)) ? ", did you mean $$t?" : '.';
throw new \LogicException("Attempt to read undeclared property {$rc->getName()}::$$name$hint");
}
/**
* Access to undeclared property.
* @throws \LogicException
*/
public function __set($name, $value)
{
$rc = new ReflectionClass($this);
$items = array_diff($rc->getProperties(ReflectionProperty::IS_PUBLIC), $rc->getProperties(ReflectionProperty::IS_STATIC));
$hint = ($t = Helpers::getSuggestion($items, $name)) ? ", did you mean $$t?" : '.';
throw new \LogicException("Attempt to write to undeclared property {$rc->getName()}::$$name$hint");
}
/**
* @return bool
*/
public function __isset($name)
{
return FALSE;
}
/**
* Access to undeclared property.
* @throws \LogicException
*/
public function __unset($name)
{
$class = get_class($this);
throw new \LogicException("Attempt to unset undeclared property $class::$$name.");
}
/**
* @param string method name
* @param callabke
* @return mixed
*/
public static function extensionMethod($name, $callback = NULL)
{
if (strpos($name, '::') === FALSE) {
$class = get_called_class();
} else {
list($class, $name) = explode('::', $name);
$class = (new ReflectionClass($class))->getName();
}
if (self::$extMethods === NULL) { // for backwards compatibility
$list = get_defined_functions();
foreach ($list['user'] as $fce) {
$pair = explode('_prototype_', $fce);
if (count($pair) === 2) {
trigger_error("Extension method defined as $fce() is deprecated, use $class::extensionMethod('$name', ...).", E_USER_DEPRECATED);
self::$extMethods[$pair[1]][(new ReflectionClass($pair[0]))->getName()] = $fce;
self::$extMethods[$pair[1]][''] = NULL;
}
}
}
$list = & self::$extMethods[strtolower($name)];
if ($callback === NULL) { // getter
$cache = & $list[''][$class];
if (isset($cache)) {
return $cache;
}
foreach ([$class] + class_parents($class) + class_implements($class) as $cl) {
if (isset($list[$cl])) {
return $cache = $list[$cl];
}
}
return $cache = FALSE;
} else { // setter
$list[$class] = $callback;
$list[''] = NULL;
}
}
}

View File

@@ -5,37 +5,39 @@
* Copyright (c) 2005 David Grudl (https://davidgrudl.com) * Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/ */
namespace Dibi;
/** /**
* dibi SQL translator. * dibi SQL translator.
*
* @package dibi
*/ */
final class DibiTranslator extends DibiObject final class Translator
{ {
/** @var DibiConnection */ use Strict;
/** @var Connection */
private $connection; private $connection;
/** @var IDibiDriver */ /** @var Driver */
private $driver; private $driver;
/** @var int */ /** @var int */
private $cursor; private $cursor = 0;
/** @var array */ /** @var array */
private $args; private $args;
/** @var bool */ /** @var string[] */
private $hasError; private $errors;
/** @var bool */ /** @var bool */
private $comment; private $comment = FALSE;
/** @var int */ /** @var int */
private $ifLevel; private $ifLevel = 0;
/** @var int */ /** @var int */
private $ifLevelStart; private $ifLevelStart = 0;
/** @var int */ /** @var int */
private $limit; private $limit;
@@ -43,51 +45,39 @@ final class DibiTranslator extends DibiObject
/** @var int */ /** @var int */
private $offset; private $offset;
/** @var DibiHashMap */ /** @var HashMap */
private $identifiers; private $identifiers;
public function __construct(DibiConnection $connection) public function __construct(Connection $connection)
{ {
$this->connection = $connection; $this->connection = $connection;
$this->identifiers = new DibiHashMap(array($this, 'delimite')); $this->driver = $connection->getDriver();
$this->identifiers = new HashMap([$this, 'delimite']);
} }
/** /**
* Generates SQL. * Generates SQL. Can be called only once.
* @param array * @param array
* @return string * @return string
* @throws DibiException * @throws Exception
*/ */
public function translate(array $args) public function translate(array $args)
{ {
if (!$this->driver) {
$this->driver = $this->connection->getDriver();
}
$args = array_values($args); $args = array_values($args);
while (count($args) === 1 && is_array($args[0])) { // implicit array expansion while (count($args) === 1 && is_array($args[0])) { // implicit array expansion
$args = array_values($args[0]); $args = array_values($args[0]);
} }
$this->args = $args; $this->args = $args;
$this->limit = -1;
$this->offset = 0;
$this->hasError = FALSE;
$commandIns = NULL; $commandIns = NULL;
$lastArr = NULL; $lastArr = NULL;
// shortcuts
$cursor = & $this->cursor; $cursor = & $this->cursor;
$cursor = 0;
// conditional sql
$this->ifLevel = $this->ifLevelStart = 0;
$comment = & $this->comment; $comment = & $this->comment;
$comment = FALSE;
// iterate // iterate
$sql = array(); $sql = [];
while ($cursor < count($this->args)) { while ($cursor < count($this->args)) {
$arg = $this->args[$cursor]; $arg = $this->args[$cursor];
$cursor++; $cursor++;
@@ -116,11 +106,11 @@ final class DibiTranslator extends DibiObject
)/xs', )/xs',
*/ // note: this can change $this->args & $this->cursor & ... */ // note: this can change $this->args & $this->cursor & ...
. preg_replace_callback('/(?=[`[\'":%?])(?:`(.+?)`|\[(.+?)\]|(\')((?:\'\'|[^\'])*)\'|(")((?:""|[^"])*)"|(\'|")|:(\S*?:)([a-zA-Z0-9._]?)|%([a-zA-Z~][a-zA-Z0-9~]{0,5})|(\?))/s', . preg_replace_callback('/(?=[`[\'":%?])(?:`(.+?)`|\[(.+?)\]|(\')((?:\'\'|[^\'])*)\'|(")((?:""|[^"])*)"|(\'|")|:(\S*?:)([a-zA-Z0-9._]?)|%([a-zA-Z~][a-zA-Z0-9~]{0,5})|(\?))/s',
array($this, 'cb'), [$this, 'cb'],
substr($arg, $toSkip) substr($arg, $toSkip)
); );
if (preg_last_error()) { if (preg_last_error()) {
throw new DibiPcreException; throw new PcreException;
} }
} }
continue; continue;
@@ -131,26 +121,24 @@ final class DibiTranslator extends DibiObject
continue; continue;
} }
if ($arg instanceof Traversable) { if ($arg instanceof \Traversable) {
$arg = iterator_to_array($arg); $arg = iterator_to_array($arg);
} }
if (is_array($arg)) { if (is_array($arg) && is_string(key($arg))) {
if (is_string(key($arg))) { // associative array -> autoselect between SET or VALUES & LIST
// associative array -> autoselect between SET or VALUES & LIST if ($commandIns === NULL) {
if ($commandIns === NULL) { $commandIns = strtoupper(substr(ltrim($this->args[0]), 0, 6));
$commandIns = strtoupper(substr(ltrim($this->args[0]), 0, 6)); $commandIns = $commandIns === 'INSERT' || $commandIns === 'REPLAC';
$commandIns = $commandIns === 'INSERT' || $commandIns === 'REPLAC'; $sql[] = $this->formatValue($arg, $commandIns ? 'v' : 'a');
$sql[] = $this->formatValue($arg, $commandIns ? 'v' : 'a'); } else {
} else { if ($lastArr === $cursor - 1) {
if ($lastArr === $cursor - 1) { $sql[] = ',';
$sql[] = ',';
}
$sql[] = $this->formatValue($arg, $commandIns ? 'l' : 'a');
} }
$lastArr = $cursor; $sql[] = $this->formatValue($arg, $commandIns ? 'l' : 'a');
continue;
} }
$lastArr = $cursor;
continue;
} }
// default processing // default processing
@@ -164,12 +152,12 @@ final class DibiTranslator extends DibiObject
$sql = implode(' ', $sql); $sql = implode(' ', $sql);
if ($this->hasError) { if ($this->errors) {
throw new DibiException('SQL translate error', 0, $sql); throw new Exception('SQL translate error: ' . trim(reset($this->errors), '*'), 0, $sql);
} }
// apply limit // apply limit
if ($this->limit > -1 || $this->offset > 0) { if ($this->limit !== NULL || $this->offset !== NULL) {
$this->driver->applyLimit($sql, $this->limit, $this->offset); $this->driver->applyLimit($sql, $this->limit, $this->offset);
} }
@@ -189,17 +177,13 @@ final class DibiTranslator extends DibiObject
return '...'; return '...';
} }
if (!$this->driver) {
$this->driver = $this->connection->getDriver();
}
// array processing (with or without modifier) // array processing (with or without modifier)
if ($value instanceof Traversable) { if ($value instanceof \Traversable) {
$value = iterator_to_array($value); $value = iterator_to_array($value);
} }
if (is_array($value)) { if (is_array($value)) {
$vx = $kx = array(); $vx = $kx = [];
switch ($modifier) { switch ($modifier) {
case 'and': case 'and':
case 'or': // key=val AND key IS NULL AND ... case 'or': // key=val AND key IS NULL AND ...
@@ -215,7 +199,7 @@ final class DibiTranslator extends DibiObject
$v = $this->formatValue($v, FALSE); $v = $this->formatValue($v, FALSE);
$vx[] = $k . ($v === 'NULL' ? 'IS ' : '= ') . $v; $vx[] = $k . ($v === 'NULL' ? 'IS ' : '= ') . $v;
} elseif ($pair[1] === 'ex') { // TODO: this will be removed } elseif ($pair[1] === 'ex') {
$vx[] = $k . $this->formatValue($v, 'ex'); $vx[] = $k . $this->formatValue($v, 'ex');
} else { } else {
@@ -281,15 +265,13 @@ final class DibiTranslator extends DibiObject
if (is_array($v)) { if (is_array($v)) {
if (isset($proto)) { if (isset($proto)) {
if ($proto !== array_keys($v)) { if ($proto !== array_keys($v)) {
$this->hasError = TRUE; return $this->errors[] = '**Multi-insert array "' . $k . '" is different**';
return '**Multi-insert array "' . $k . '" is different.**';
} }
} else { } else {
$proto = array_keys($v); $proto = array_keys($v);
} }
} else { } else {
$this->hasError = TRUE; return $this->errors[] = '**Unexpected type ' . (is_object($v) ? get_class($v) : gettype($v)) . '**';
return '**Unexpected type ' . gettype($v) . '**';
} }
$pair = explode('%', $k, 2); // split into identifier & modifier $pair = explode('%', $k, 2); // split into identifier & modifier
@@ -318,8 +300,7 @@ final class DibiTranslator extends DibiObject
case 'ex': case 'ex':
case 'sql': case 'sql':
$translator = new self($this->connection); return call_user_func_array([$this->connection, 'translate'], $value);
return $translator->translate($value);
default: // value, value, value - all with the same modifier default: // value, value, value - all with the same modifier
foreach ($value as $v) { foreach ($value as $v) {
@@ -332,23 +313,36 @@ final class DibiTranslator extends DibiObject
// with modifier procession // with modifier procession
if ($modifier) { if ($modifier) {
if ($value !== NULL && !is_scalar($value) && !$value instanceof DateTime && !$value instanceof DateTimeInterface) { // array is already processed if ($value !== NULL && !is_scalar($value)) { // array is already processed
$this->hasError = TRUE; if ($value instanceof Literal && ($modifier === 'sql' || $modifier === 'SQL')) {
return '**Unexpected type ' . gettype($value) . '**'; $modifier = 'SQL';
} elseif (($value instanceof \DateTime || $value instanceof \DateTimeInterface) && ($modifier === 'd' || $modifier === 't')) {
// continue
} else {
$type = is_object($value) ? get_class($value) : gettype($value);
return $this->errors[] = "**Invalid combination of type $type and modifier %$modifier**";
}
} }
switch ($modifier) { switch ($modifier) {
case 's': // string case 's': // string
return $value === NULL ? 'NULL' : $this->driver->escapeText($value);
case 'bin':// binary case 'bin':// binary
return $value === NULL ? 'NULL' : $this->driver->escapeBinary($value);
case 'b': // boolean case 'b': // boolean
return $value === NULL ? 'NULL' : $this->driver->escape($value, $modifier); return $value === NULL ? 'NULL' : $this->driver->escapeBool($value);
case 'sN': // string or NULL case 'sN': // string or NULL
case 'sn': case 'sn':
return $value == '' ? 'NULL' : $this->driver->escape($value, dibi::TEXT); // notice two equal signs return $value == '' ? 'NULL' : $this->driver->escapeText($value); // notice two equal signs
case 'in': // deprecated
trigger_error('Modifier %in is deprecated, use %iN.', E_USER_DEPRECATED);
// intentionally break omitted
case 'iN': // signed int or NULL case 'iN': // signed int or NULL
case 'in': // deprecated
if ($value == '') { if ($value == '') {
$value = NULL; $value = NULL;
} }
@@ -378,16 +372,11 @@ final class DibiTranslator extends DibiObject
case 'd': // date case 'd': // date
case 't': // datetime case 't': // datetime
case 'dt': // datetime
if ($value === NULL) { if ($value === NULL) {
return 'NULL'; return 'NULL';
} else { } else {
if (is_numeric($value)) { return $modifier === 'd' ? $this->driver->escapeDate($value) : $this->driver->escapeDateTime($value);
$value = (int) $value; // timestamp
} elseif (is_string($value)) {
$value = new DateTime($value);
}
return $this->driver->escape($value, $modifier);
} }
case 'by': case 'by':
@@ -403,11 +392,11 @@ final class DibiTranslator extends DibiObject
$value = substr($value, 0, $toSkip) $value = substr($value, 0, $toSkip)
. preg_replace_callback( . preg_replace_callback(
'/(?=[`[\'":])(?:`(.+?)`|\[(.+?)\]|(\')((?:\'\'|[^\'])*)\'|(")((?:""|[^"])*)"|(\'|")|:(\S*?:)([a-zA-Z0-9._]?))/s', '/(?=[`[\'":])(?:`(.+?)`|\[(.+?)\]|(\')((?:\'\'|[^\'])*)\'|(")((?:""|[^"])*)"|(\'|")|:(\S*?:)([a-zA-Z0-9._]?))/s',
array($this, 'cb'), [$this, 'cb'],
substr($value, $toSkip) substr($value, $toSkip)
); );
if (preg_last_error()) { if (preg_last_error()) {
throw new DibiPcreException; throw new PcreException;
} }
} }
return $value; return $value;
@@ -429,19 +418,18 @@ final class DibiTranslator extends DibiObject
case 'a': case 'a':
case 'l': case 'l':
case 'v': case 'v':
$this->hasError = TRUE; $type = gettype($value);
return '**Unexpected type ' . gettype($value) . '**'; return $this->errors[] = "**Invalid combination of type $type and modifier %$modifier**";
default: default:
$this->hasError = TRUE; return $this->errors[] = "**Unknown or unexpected modifier %$modifier**";
return "**Unknown or invalid modifier %$modifier**";
} }
} }
// without modifier procession // without modifier procession
if (is_string($value)) { if (is_string($value)) {
return $this->driver->escape($value, dibi::TEXT); return $this->driver->escapeText($value);
} elseif (is_int($value)) { } elseif (is_int($value)) {
return (string) $value; return (string) $value;
@@ -450,20 +438,20 @@ final class DibiTranslator extends DibiObject
return rtrim(rtrim(number_format($value, 10, '.', ''), '0'), '.'); return rtrim(rtrim(number_format($value, 10, '.', ''), '0'), '.');
} elseif (is_bool($value)) { } elseif (is_bool($value)) {
return $this->driver->escape($value, dibi::BOOL); return $this->driver->escapeBool($value);
} elseif ($value === NULL) { } elseif ($value === NULL) {
return 'NULL'; return 'NULL';
} elseif ($value instanceof DateTime || $value instanceof DateTimeInterface) { } elseif ($value instanceof \DateTime || $value instanceof \DateTimeInterface) {
return $this->driver->escape($value, dibi::DATETIME); return $this->driver->escapeDateTime($value);
} elseif ($value instanceof DibiLiteral) { } elseif ($value instanceof Literal) {
return (string) $value; return (string) $value;
} else { } else {
$this->hasError = TRUE; $type = is_object($value) ? get_class($value) : gettype($value);
return '**Unexpected ' . gettype($value) . '**'; return $this->errors[] = "**Unexpected $type**";
} }
} }
@@ -492,8 +480,7 @@ final class DibiTranslator extends DibiObject
$cursor = & $this->cursor; $cursor = & $this->cursor;
if ($cursor >= count($this->args)) { if ($cursor >= count($this->args)) {
$this->hasError = TRUE; return $this->errors[] = '**Extra placeholder**';
return '**Extra placeholder**';
} }
$cursor++; $cursor++;
@@ -505,8 +492,7 @@ final class DibiTranslator extends DibiObject
$cursor = & $this->cursor; $cursor = & $this->cursor;
if ($cursor >= count($this->args) && $mod !== 'else' && $mod !== 'end') { if ($cursor >= count($this->args) && $mod !== 'else' && $mod !== 'end') {
$this->hasError = TRUE; return $this->errors[] = "**Extra modifier %$mod**";
return "**Extra modifier %$mod**";
} }
if ($mod === 'if') { if ($mod === 'if') {
@@ -582,14 +568,13 @@ final class DibiTranslator extends DibiObject
return $this->identifiers->{$matches[2]}; return $this->identifiers->{$matches[2]};
} elseif ($matches[3]) { // SQL strings: '...' } elseif ($matches[3]) { // SQL strings: '...'
return $this->driver->escape(str_replace("''", "'", $matches[4]), dibi::TEXT); return $this->driver->escapeText(str_replace("''", "'", $matches[4]));
} elseif ($matches[5]) { // SQL strings: "..." } elseif ($matches[5]) { // SQL strings: "..."
return $this->driver->escape(str_replace('""', '"', $matches[6]), dibi::TEXT); return $this->driver->escapeText(str_replace('""', '"', $matches[6]));
} elseif ($matches[7]) { // string quote } elseif ($matches[7]) { // string quote
$this->hasError = TRUE; return $this->errors[] = '**Alone quote**';
return '**Alone quote**';
} }
if ($matches[8]) { // SQL identifier substitution if ($matches[8]) { // SQL identifier substitution
@@ -598,7 +583,7 @@ final class DibiTranslator extends DibiObject
return $matches[9] == '' ? $this->formatValue($m, FALSE) : $m . $matches[9]; // value or identifier return $matches[9] == '' ? $this->formatValue($m, FALSE) : $m . $matches[9]; // value or identifier
} }
die('this should be never executed'); throw new \Exception('this should be never executed');
} }
@@ -614,7 +599,7 @@ final class DibiTranslator extends DibiObject
$parts = explode('.', $value); $parts = explode('.', $value);
foreach ($parts as & $v) { foreach ($parts as & $v) {
if ($v !== '*') { if ($v !== '*') {
$v = $this->driver->escape($v, dibi::IDENTIFIER); $v = $this->driver->escapeIdentifier($v);
} }
} }
return implode('.', $parts); return implode('.', $parts);

32
src/Dibi/Type.php Normal file
View File

@@ -0,0 +1,32 @@
<?php
/**
* This file is part of the "dibi" - smart database abstraction layer.
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/
namespace Dibi;
/**
* Data types.
*/
class Type
{
const
TEXT = 's', // as 'string'
BINARY = 'bin',
BOOL = 'b',
INTEGER = 'i',
FLOAT = 'f',
DATE = 'd',
DATETIME = 'dt',
TIME = 't',
TIME_INTERVAL = 'ti';
final public function __construct()
{
throw new \LogicException('Cannot instantiate static class ' . __CLASS__);
}
}

View File

@@ -5,55 +5,56 @@
* Copyright (c) 2005 David Grudl (https://davidgrudl.com) * Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/ */
use Dibi\Type;
/** /**
* This class is static container class for creating DB objects and * This class is static container class for creating DB objects and
* store connections info. * store connections info.
*
* @package dibi
*/ */
class dibi class dibi
{ {
/** column type */ use Dibi\Strict;
const TEXT = 's', // as 'string'
BINARY = 'bin',
BOOL = 'b',
INTEGER = 'i',
FLOAT = 'f',
DATE = 'd',
DATETIME = 't',
TIME = 't';
const IDENTIFIER = 'n', const
AFFECTED_ROWS = 'a'; AFFECTED_ROWS = 'a',
IDENTIFIER = 'n';
/** @deprecated */
const FIELD_TEXT = self::TEXT,
FIELD_BINARY = self::BINARY,
FIELD_BOOL = self::BOOL,
FIELD_INTEGER = self::INTEGER,
FIELD_FLOAT = self::FLOAT,
FIELD_DATE = self::DATE,
FIELD_DATETIME = self::DATETIME,
FIELD_TIME = self::TIME;
/** version */ /** version */
const VERSION = '2.3.2', const
REVISION = 'released on 2015-04-18'; VERSION = '3.0.2',
REVISION = 'released on 2016-01-29';
/** sorting order */ /** sorting order */
const ASC = 'ASC', const
ASC = 'ASC',
DESC = 'DESC'; DESC = 'DESC';
/** @var DibiConnection[] Connection registry storage for DibiConnection objects */ /** @deprecated */
private static $registry = array(); const
TEXT = Type::TEXT,
BINARY = Type::BINARY,
BOOL = Type::BOOL,
INTEGER = Type::INTEGER,
FLOAT = Type::FLOAT,
DATE = Type::DATE,
DATETIME = Type::DATETIME,
TIME = Type::TIME,
FIELD_TEXT = Type::TEXT,
FIELD_BINARY = Type::BINARY,
FIELD_BOOL = Type::BOOL,
FIELD_INTEGER = Type::INTEGER,
FIELD_FLOAT = Type::FLOAT,
FIELD_DATE = Type::DATE,
FIELD_DATETIME = Type::DATETIME,
FIELD_TIME = Type::TIME;
/** @var DibiConnection Current connection */ /** @var Dibi\Connection[] Connection registry storage for DibiConnection objects */
private static $registry = [];
/** @var Dibi\Connection Current connection */
private static $connection; private static $connection;
/** @var array @see addHandler */
private static $handlers = array();
/** @var string Last SQL command @see dibi::query() */ /** @var string Last SQL command @see dibi::query() */
public static $sql; public static $sql;
@@ -83,20 +84,20 @@ class dibi
/** /**
* Creates a new DibiConnection object and connects it to specified database. * Creates a new Connection object and connects it to specified database.
* @param mixed connection parameters * @param mixed connection parameters
* @param string connection name * @param string connection name
* @return DibiConnection * @return Dibi\Connection
* @throws DibiException * @throws Dibi\Exception
*/ */
public static function connect($config = array(), $name = 0) public static function connect($config = [], $name = 0)
{ {
return self::$connection = self::$registry[$name] = new DibiConnection($config, $name); return self::$connection = self::$registry[$name] = new Dibi\Connection($config, $name);
} }
/** /**
* Disconnects from database (doesn't destroy DibiConnection object). * Disconnects from database (doesn't destroy Connection object).
* @return void * @return void
*/ */
public static function disconnect() public static function disconnect()
@@ -118,21 +119,21 @@ class dibi
/** /**
* Retrieve active connection. * Retrieve active connection.
* @param string connection registy name * @param string connection registy name
* @return DibiConnection * @return Dibi\Connection
* @throws DibiException * @throws Dibi\Exception
*/ */
public static function getConnection($name = NULL) public static function getConnection($name = NULL)
{ {
if ($name === NULL) { if ($name === NULL) {
if (self::$connection === NULL) { if (self::$connection === NULL) {
throw new DibiException('Dibi is not connected to database.'); throw new Dibi\Exception('Dibi is not connected to database.');
} }
return self::$connection; return self::$connection;
} }
if (!isset(self::$registry[$name])) { if (!isset(self::$registry[$name])) {
throw new DibiException("There is no connection named '$name'."); throw new Dibi\Exception("There is no connection named '$name'.");
} }
return self::$registry[$name]; return self::$registry[$name];
@@ -141,10 +142,10 @@ class dibi
/** /**
* Sets connection. * Sets connection.
* @param DibiConnection * @param Dibi\Connection
* @return DibiConnection * @return Dibi\Connection
*/ */
public static function setConnection(DibiConnection $connection) public static function setConnection(Dibi\Connection $connection)
{ {
return self::$connection = $connection; return self::$connection = $connection;
} }
@@ -164,10 +165,10 @@ class dibi
/** /**
* Generates and executes SQL query - Monostate for DibiConnection::query(). * Generates and executes SQL query - Monostate for Dibi\Connection::query().
* @param array|mixed one or more arguments * @param array|mixed one or more arguments
* @return DibiResult|int result set object (if any) * @return Dibi\Result|int result set object (if any)
* @throws DibiException * @throws Dibi\Exception
*/ */
public static function query($args) public static function query($args)
{ {
@@ -177,9 +178,9 @@ class dibi
/** /**
* Executes the SQL query - Monostate for DibiConnection::nativeQuery(). * Executes the SQL query - Monostate for Dibi\Connection::nativeQuery().
* @param string SQL statement. * @param string SQL statement.
* @return DibiResult|int result set object (if any) * @return Dibi\Result|int result set object (if any)
*/ */
public static function nativeQuery($sql) public static function nativeQuery($sql)
{ {
@@ -188,7 +189,7 @@ class dibi
/** /**
* Generates and prints SQL query - Monostate for DibiConnection::test(). * Generates and prints SQL query - Monostate for Dibi\Connection::test().
* @param array|mixed one or more arguments * @param array|mixed one or more arguments
* @return bool * @return bool
*/ */
@@ -200,9 +201,9 @@ class dibi
/** /**
* Generates and returns SQL query as DibiDataSource - Monostate for DibiConnection::test(). * Generates and returns SQL query as DataSource - Monostate for Dibi\Connection::test().
* @param array|mixed one or more arguments * @param array|mixed one or more arguments
* @return DibiDataSource * @return Dibi\DataSource
*/ */
public static function dataSource($args) public static function dataSource($args)
{ {
@@ -212,10 +213,10 @@ class dibi
/** /**
* Executes SQL query and fetch result - Monostate for DibiConnection::query() & fetch(). * Executes SQL query and fetch result - Monostate for Dibi\Connection::query() & fetch().
* @param array|mixed one or more arguments * @param array|mixed one or more arguments
* @return DibiRow * @return Dibi\Row
* @throws DibiException * @throws Dibi\Exception
*/ */
public static function fetch($args) public static function fetch($args)
{ {
@@ -225,10 +226,10 @@ class dibi
/** /**
* Executes SQL query and fetch results - Monostate for DibiConnection::query() & fetchAll(). * Executes SQL query and fetch results - Monostate for Dibi\Connection::query() & fetchAll().
* @param array|mixed one or more arguments * @param array|mixed one or more arguments
* @return DibiRow[] * @return Dibi\Row[]
* @throws DibiException * @throws Dibi\Exception
*/ */
public static function fetchAll($args) public static function fetchAll($args)
{ {
@@ -238,10 +239,10 @@ class dibi
/** /**
* Executes SQL query and fetch first column - Monostate for DibiConnection::query() & fetchSingle(). * Executes SQL query and fetch first column - Monostate for Dibi\Connection::query() & fetchSingle().
* @param array|mixed one or more arguments * @param array|mixed one or more arguments
* @return string * @return string
* @throws DibiException * @throws Dibi\Exception
*/ */
public static function fetchSingle($args) public static function fetchSingle($args)
{ {
@@ -251,10 +252,10 @@ class dibi
/** /**
* Executes SQL query and fetch pairs - Monostate for DibiConnection::query() & fetchPairs(). * Executes SQL query and fetch pairs - Monostate for Dibi\Connection::query() & fetchPairs().
* @param array|mixed one or more arguments * @param array|mixed one or more arguments
* @return string * @return string
* @throws DibiException * @throws Dibi\Exception
*/ */
public static function fetchPairs($args) public static function fetchPairs($args)
{ {
@@ -265,9 +266,9 @@ class dibi
/** /**
* Gets the number of affected rows. * Gets the number of affected rows.
* Monostate for DibiConnection::getAffectedRows() * Monostate for Dibi\Connection::getAffectedRows()
* @return int number of rows * @return int number of rows
* @throws DibiException * @throws Dibi\Exception
*/ */
public static function getAffectedRows() public static function getAffectedRows()
{ {
@@ -278,7 +279,7 @@ class dibi
/** /**
* Gets the number of affected rows. Alias for getAffectedRows(). * Gets the number of affected rows. Alias for getAffectedRows().
* @return int number of rows * @return int number of rows
* @throws DibiException * @throws Dibi\Exception
*/ */
public static function affectedRows() public static function affectedRows()
{ {
@@ -288,10 +289,10 @@ class dibi
/** /**
* Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query. * Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query.
* Monostate for DibiConnection::getInsertId() * Monostate for Dibi\Connection::getInsertId()
* @param string optional sequence name * @param string optional sequence name
* @return int * @return int
* @throws DibiException * @throws Dibi\Exception
*/ */
public static function getInsertId($sequence = NULL) public static function getInsertId($sequence = NULL)
{ {
@@ -303,7 +304,7 @@ class dibi
* Retrieves the ID generated for an AUTO_INCREMENT column. Alias for getInsertId(). * Retrieves the ID generated for an AUTO_INCREMENT column. Alias for getInsertId().
* @param string optional sequence name * @param string optional sequence name
* @return int * @return int
* @throws DibiException * @throws Dibi\Exception
*/ */
public static function insertId($sequence = NULL) public static function insertId($sequence = NULL)
{ {
@@ -312,10 +313,10 @@ class dibi
/** /**
* Begins a transaction - Monostate for DibiConnection::begin(). * Begins a transaction - Monostate for Dibi\Connection::begin().
* @param string optional savepoint name * @param string optional savepoint name
* @return void * @return void
* @throws DibiException * @throws Dibi\Exception
*/ */
public static function begin($savepoint = NULL) public static function begin($savepoint = NULL)
{ {
@@ -324,10 +325,10 @@ class dibi
/** /**
* Commits statements in a transaction - Monostate for DibiConnection::commit($savepoint = NULL). * Commits statements in a transaction - Monostate for Dibi\Connection::commit($savepoint = NULL).
* @param string optional savepoint name * @param string optional savepoint name
* @return void * @return void
* @throws DibiException * @throws Dibi\Exception
*/ */
public static function commit($savepoint = NULL) public static function commit($savepoint = NULL)
{ {
@@ -336,10 +337,10 @@ class dibi
/** /**
* Rollback changes in a transaction - Monostate for DibiConnection::rollback(). * Rollback changes in a transaction - Monostate for Dibi\Connection::rollback().
* @param string optional savepoint name * @param string optional savepoint name
* @return void * @return void
* @throws DibiException * @throws Dibi\Exception
*/ */
public static function rollback($savepoint = NULL) public static function rollback($savepoint = NULL)
{ {
@@ -348,8 +349,8 @@ class dibi
/** /**
* Gets a information about the current database - Monostate for DibiConnection::getDatabaseInfo(). * Gets a information about the current database - Monostate for Dibi\Connection::getDatabaseInfo().
* @return DibiDatabaseInfo * @return Dibi\Reflection\Database
*/ */
public static function getDatabaseInfo() public static function getDatabaseInfo()
{ {
@@ -364,19 +365,7 @@ class dibi
*/ */
public static function loadFile($file) public static function loadFile($file)
{ {
return self::getConnection()->loadFile($file); return Dibi\Helpers::loadFromFile(self::getConnection(), $file);
}
/**
* Replacement for majority of dibi::methods() in future.
*/
public static function __callStatic($name, $args)
{
//if ($name = 'select', 'update', ...') {
// return self::command()->$name($args);
//}
return call_user_func_array(array(self::getConnection(), $name), $args);
} }
@@ -384,7 +373,7 @@ class dibi
/** /**
* @return DibiFluent * @return Dibi\Fluent
*/ */
public static function command() public static function command()
{ {
@@ -394,19 +383,19 @@ class dibi
/** /**
* @param string column name * @param string column name
* @return DibiFluent * @return Dibi\Fluent
*/ */
public static function select($args) public static function select($args)
{ {
$args = func_get_args(); $args = func_get_args();
return call_user_func_array(array(self::getConnection(), 'select'), $args); return call_user_func_array([self::getConnection(), 'select'], $args);
} }
/** /**
* @param string table * @param string table
* @param array * @param array
* @return DibiFluent * @return Dibi\Fluent
*/ */
public static function update($table, $args) public static function update($table, $args)
{ {
@@ -417,7 +406,7 @@ class dibi
/** /**
* @param string table * @param string table
* @param array * @param array
* @return DibiFluent * @return Dibi\Fluent
*/ */
public static function insert($table, $args) public static function insert($table, $args)
{ {
@@ -427,7 +416,7 @@ class dibi
/** /**
* @param string table * @param string table
* @return DibiFluent * @return Dibi\Fluent
*/ */
public static function delete($table) public static function delete($table)
{ {
@@ -439,8 +428,8 @@ class dibi
/** /**
* Returns substitution hashmap - Monostate for DibiConnection::getSubstitutes(). * Returns substitution hashmap - Monostate for Dibi\Connection::getSubstitutes().
* @return DibiHashMap * @return Dibi\HashMap
*/ */
public static function getSubstitutes() public static function getSubstitutes()
{ {
@@ -452,89 +441,14 @@ class dibi
/** /**
* Prints out a syntax highlighted version of the SQL command or DibiResult. * Prints out a syntax highlighted version of the SQL command or Result.
* @param string|DibiResult * @param string|Result
* @param bool return output instead of printing it? * @param bool return output instead of printing it?
* @return string * @return string
*/ */
public static function dump($sql = NULL, $return = FALSE) public static function dump($sql = NULL, $return = FALSE)
{ {
ob_start(); return Dibi\Helpers::dump($sql, $return);
if ($sql instanceof DibiResult) {
$sql->dump();
} else {
if ($sql === NULL) {
$sql = self::$sql;
}
static $keywords1 = 'SELECT|(?:ON\s+DUPLICATE\s+KEY)?UPDATE|INSERT(?:\s+INTO)?|REPLACE(?:\s+INTO)?|DELETE|CALL|UNION|FROM|WHERE|HAVING|GROUP\s+BY|ORDER\s+BY|LIMIT|OFFSET|FETCH\s+NEXT|SET|VALUES|LEFT\s+JOIN|INNER\s+JOIN|TRUNCATE|START\s+TRANSACTION|BEGIN|COMMIT|ROLLBACK(?:\s+TO\s+SAVEPOINT)?|(?:RELEASE\s+)?SAVEPOINT';
static $keywords2 = 'ALL|DISTINCT|DISTINCTROW|IGNORE|AS|USING|ON|AND|OR|IN|IS|NOT|NULL|LIKE|RLIKE|REGEXP|TRUE|FALSE';
// insert new lines
$sql = " $sql ";
$sql = preg_replace("#(?<=[\\s,(])($keywords1)(?=[\\s,)])#i", "\n\$1", $sql);
// reduce spaces
$sql = preg_replace('#[ \t]{2,}#', ' ', $sql);
$sql = wordwrap($sql, 100);
$sql = preg_replace("#([ \t]*\r?\n){2,}#", "\n", $sql);
// syntax highlight
$highlighter = "#(/\\*.+?\\*/)|(\\*\\*.+?\\*\\*)|(?<=[\\s,(])($keywords1)(?=[\\s,)])|(?<=[\\s,(=])($keywords2)(?=[\\s,)=])#is";
if (PHP_SAPI === 'cli') {
if (substr(getenv('TERM'), 0, 5) === 'xterm') {
$sql = preg_replace_callback($highlighter, array('dibi', 'cliHighlightCallback'), $sql);
}
echo trim($sql) . "\n\n";
} else {
$sql = htmlSpecialChars($sql);
$sql = preg_replace_callback($highlighter, array('dibi', 'highlightCallback'), $sql);
echo '<pre class="dump">', trim($sql), "</pre>\n\n";
}
}
if ($return) {
return ob_get_clean();
} else {
ob_end_flush();
}
}
private static function highlightCallback($matches)
{
if (!empty($matches[1])) { // comment
return '<em style="color:gray">' . $matches[1] . '</em>';
} elseif (!empty($matches[2])) { // error
return '<strong style="color:red">' . $matches[2] . '</strong>';
} elseif (!empty($matches[3])) { // most important keywords
return '<strong style="color:blue">' . $matches[3] . '</strong>';
} elseif (!empty($matches[4])) { // other keywords
return '<strong style="color:green">' . $matches[4] . '</strong>';
}
}
private static function cliHighlightCallback($matches)
{
if (!empty($matches[1])) { // comment
return "\033[1;30m" . $matches[1] . "\033[0m";
} elseif (!empty($matches[2])) { // error
return "\033[1;31m" . $matches[2] . "\033[0m";
} elseif (!empty($matches[3])) { // most important keywords
return "\033[1;34m" . $matches[3] . "\033[0m";
} elseif (!empty($matches[4])) { // other keywords
return "\033[1;32m" . $matches[4] . "\033[0m";
}
} }
} }

View File

@@ -5,27 +5,30 @@
* Copyright (c) 2005 David Grudl (https://davidgrudl.com) * Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/ */
namespace Dibi;
/** /**
* dibi common exception. * dibi common exception.
*
* @package dibi
*/ */
class DibiException extends Exception class Exception extends \Exception
{ {
/** @var string */ use Strict;
/** @var string|NULL */
private $sql; private $sql;
/** /**
* Construct a dibi exception. * Construct a dibi exception.
* @param string Message describing the exception * @param string Message describing the exception
* @param int Some code * @param mixed
* @param string SQL command * @param string SQL command
*/ */
public function __construct($message = NULL, $code = 0, $sql = NULL) public function __construct($message = NULL, $code = 0, $sql = NULL)
{ {
parent::__construct($message, (int) $code); parent::__construct($message);
$this->code = $code;
$this->sql = $sql; $this->sql = $sql;
} }
@@ -52,94 +55,105 @@ class DibiException extends Exception
/** /**
* database server exception. * database server exception.
*
* @package dibi
*/ */
class DibiDriverException extends DibiException class DriverException extends Exception
{ {
/********************* error catching ****************d*g**/
/** @var string */
private static $errorMsg;
/**
* Starts catching potential errors/warnings.
* @return void
*/
public static function tryError()
{
set_error_handler(array(__CLASS__, '_errorHandler'), E_ALL);
self::$errorMsg = NULL;
}
/**
* Returns catched error/warning message.
* @param string catched message
* @return bool
*/
public static function catchError(& $message)
{
restore_error_handler();
$message = self::$errorMsg;
self::$errorMsg = NULL;
return $message !== NULL;
}
/**
* Internal error handler. Do not call directly.
* @internal
*/
public static function _errorHandler($code, $message)
{
restore_error_handler();
if (ini_get('html_errors')) {
$message = strip_tags($message);
$message = html_entity_decode($message);
}
self::$errorMsg = $message;
}
} }
/** /**
* PCRE exception. * PCRE exception.
*
* @package dibi
*/ */
class DibiPcreException extends Exception class PcreException extends Exception
{ {
use Strict;
public function __construct($message = '%msg.') public function __construct($message = '%msg.')
{ {
static $messages = array( static $messages = [
PREG_INTERNAL_ERROR => 'Internal error', PREG_INTERNAL_ERROR => 'Internal error',
PREG_BACKTRACK_LIMIT_ERROR => 'Backtrack limit was exhausted', PREG_BACKTRACK_LIMIT_ERROR => 'Backtrack limit was exhausted',
PREG_RECURSION_LIMIT_ERROR => 'Recursion limit was exhausted', PREG_RECURSION_LIMIT_ERROR => 'Recursion limit was exhausted',
PREG_BAD_UTF8_ERROR => 'Malformed UTF-8 data', PREG_BAD_UTF8_ERROR => 'Malformed UTF-8 data',
5 => 'Offset didn\'t correspond to the begin of a valid UTF-8 code point', // PREG_BAD_UTF8_OFFSET_ERROR 5 => 'Offset didn\'t correspond to the begin of a valid UTF-8 code point', // PREG_BAD_UTF8_OFFSET_ERROR
); ];
$code = preg_last_error(); $code = preg_last_error();
parent::__construct(str_replace('%msg', isset($messages[$code]) ? $messages[$code] : 'Unknown error', $message), $code); parent::__construct(str_replace('%msg', isset($messages[$code]) ? $messages[$code] : 'Unknown error', $message), $code);
} }
} }
/** class NotImplementedException extends Exception
* @package dibi {
*/ }
class DibiNotImplementedException extends DibiException
{}
class NotSupportedException extends Exception
{
}
/** /**
* @package dibi * Database procedure exception.
*/ */
class DibiNotSupportedException extends DibiException class ProcedureException extends Exception
{} {
/** @var string */
protected $severity;
/**
* Construct the exception.
* @param string Message describing the exception
* @param int Some code
* @param string SQL command
*/
public function __construct($message = NULL, $code = 0, $severity = NULL, $sql = NULL)
{
parent::__construct($message, (int) $code, $sql);
$this->severity = $severity;
}
/**
* Gets the exception severity.
* @return string
*/
public function getSeverity()
{
$this->severity;
}
}
/**
* Base class for all constraint violation related exceptions.
*/
class ConstraintViolationException extends DriverException
{
}
/**
* Exception for a foreign key constraint violation.
*/
class ForeignKeyConstraintViolationException extends ConstraintViolationException
{
}
/**
* Exception for a NOT NULL constraint violation.
*/
class NotNullConstraintViolationException extends ConstraintViolationException
{
}
/**
* Exception for a unique constraint violation.
*/
class UniqueConstraintViolationException extends ConstraintViolationException
{
}

View File

@@ -5,45 +5,45 @@
* Copyright (c) 2005 David Grudl (https://davidgrudl.com) * Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/ */
namespace Dibi;
/** /**
* Provides an interface between a dataset and data-aware components. * Provides an interface between a dataset and data-aware components.
* @package dibi
*/ */
interface IDataSource extends Countable, IteratorAggregate interface IDataSource extends \Countable, \IteratorAggregate
{ {
//function IteratorAggregate::getIterator(); //function \IteratorAggregate::getIterator();
//function Countable::count(); //function \Countable::count();
} }
/** /**
* dibi driver interface. * dibi driver interface.
* @package dibi
*/ */
interface IDibiDriver interface Driver
{ {
/** /**
* Connects to a database. * Connects to a database.
* @param array * @param array
* @return void * @return void
* @throws DibiException * @throws Exception
*/ */
function connect(array & $config); function connect(array & $config);
/** /**
* Disconnects from a database. * Disconnects from a database.
* @return void * @return void
* @throws DibiException * @throws Exception
*/ */
function disconnect(); function disconnect();
/** /**
* Internal: Executes the SQL query. * Internal: Executes the SQL query.
* @param string SQL statement. * @param string SQL statement.
* @return IDibiResultDriver|NULL * @return ResultDriver|NULL
* @throws DibiDriverException * @throws DriverException
*/ */
function query($sql); function query($sql);
@@ -63,7 +63,7 @@ interface IDibiDriver
* Begins a transaction (if supported). * Begins a transaction (if supported).
* @param string optional savepoint name * @param string optional savepoint name
* @return void * @return void
* @throws DibiDriverException * @throws DriverException
*/ */
function begin($savepoint = NULL); function begin($savepoint = NULL);
@@ -71,7 +71,7 @@ interface IDibiDriver
* Commits statements in a transaction. * Commits statements in a transaction.
* @param string optional savepoint name * @param string optional savepoint name
* @return void * @return void
* @throws DibiDriverException * @throws DriverException
*/ */
function commit($savepoint = NULL); function commit($savepoint = NULL);
@@ -79,7 +79,7 @@ interface IDibiDriver
* Rollback changes in a transaction. * Rollback changes in a transaction.
* @param string optional savepoint name * @param string optional savepoint name
* @return void * @return void
* @throws DibiDriverException * @throws DriverException
*/ */
function rollback($savepoint = NULL); function rollback($savepoint = NULL);
@@ -91,18 +91,26 @@ interface IDibiDriver
/** /**
* Returns the connection reflector. * Returns the connection reflector.
* @return IDibiReflector * @return Reflector
*/ */
function getReflector(); function getReflector();
/** /**
* Encodes data for use in a SQL statement. * Encodes data for use in a SQL statement.
* @param string value * @param mixed value
* @param string type (dibi::TEXT, dibi::BOOL, ...)
* @return string encoded value * @return string encoded value
* @throws InvalidArgumentException
*/ */
function escape($value, $type); function escapeText($value);
function escapeBinary($value);
function escapeIdentifier($value);
function escapeBool($value);
function escapeDate($value);
function escapeDateTime($value);
/** /**
* Encodes string for use in a LIKE statement. * Encodes string for use in a LIKE statement.
@@ -123,9 +131,8 @@ interface IDibiDriver
/** /**
* dibi result set driver interface. * dibi result set driver interface.
* @package dibi
*/ */
interface IDibiResultDriver interface ResultDriver
{ {
/** /**
@@ -138,7 +145,7 @@ interface IDibiResultDriver
* Moves cursor position without fetching row. * Moves cursor position without fetching row.
* @param int the 0-based cursor pos to seek to * @param int the 0-based cursor pos to seek to
* @return boolean TRUE on success, FALSE if unable to seek to specified record * @return boolean TRUE on success, FALSE if unable to seek to specified record
* @throws DibiException * @throws Exception
*/ */
function seek($row); function seek($row);
@@ -171,22 +178,18 @@ interface IDibiResultDriver
/** /**
* Decodes data from result set. * Decodes data from result set.
* @param string value * @param string
* @param string type (dibi::BINARY) * @return string
* @return string decoded value
* @throws InvalidArgumentException
*/ */
function unescape($value, $type); function unescapeBinary($value);
} }
/** /**
* dibi driver reflection. * dibi driver reflection.
*
* @package dibi
*/ */
interface IDibiReflector interface Reflector
{ {
/** /**

144
src/loader.php Normal file
View File

@@ -0,0 +1,144 @@
<?php
/**
* This file is part of the "dibi" - smart database abstraction layer.
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/
if (PHP_VERSION_ID < 50404) {
throw new Exception('Dibi requires PHP 5.4.4 or newer.');
}
spl_autoload_register(function ($class) {
static $map = [
'dibi' => 'dibi.php',
'Dibi\Bridges\Nette\DibiExtension22' => 'Bridges/Nette/DibiExtension22.php',
'Dibi\Bridges\Nette\DibiExtension21' => 'Bridges/Nette/DibiExtension21.php',
'Dibi\Bridges\Nette\Panel' => 'Bridges/Nette/Panel.php',
'Dibi\Bridges\Tracy\Panel' => 'Bridges/Tracy/Panel.php',
'Dibi\Connection' => 'Connection.php',
'Dibi\DataSource' => 'DataSource.php',
'Dibi\DateTime' => 'DateTime.php',
'Dibi\Driver' => 'interfaces.php',
'Dibi\DriverException' => 'exceptions.php',
'Dibi\Drivers\FirebirdDriver' => 'Drivers/FirebirdDriver.php',
'Dibi\Drivers\SqlsrvDriver' => 'Drivers/SqlsrvDriver.php',
'Dibi\Drivers\SqlsrvReflector' => 'Drivers/SqlsrvReflector.php',
'Dibi\Drivers\MsSqlDriver' => 'Drivers/MsSqlDriver.php',
'Dibi\Drivers\MsSqlReflector' => 'Drivers/MsSqlReflector.php',
'Dibi\Drivers\MySqlDriver' => 'Drivers/MySqlDriver.php',
'Dibi\Drivers\MySqliDriver' => 'Drivers/MySqliDriver.php',
'Dibi\Drivers\MySqlReflector' => 'Drivers/MySqlReflector.php',
'Dibi\Drivers\OdbcDriver' => 'Drivers/OdbcDriver.php',
'Dibi\Drivers\OracleDriver' => 'Drivers/OracleDriver.php',
'Dibi\Drivers\PdoDriver' => 'Drivers/PdoDriver.php',
'Dibi\Drivers\PostgreDriver' => 'Drivers/PostgreDriver.php',
'Dibi\Drivers\Sqlite3Driver' => 'Drivers/Sqlite3Driver.php',
'Dibi\Drivers\SqliteReflector' => 'Drivers/SqliteReflector.php',
'Dibi\Event' => 'Event.php',
'Dibi\Exception' => 'exceptions.php',
'Dibi\Fluent' => 'Fluent.php',
'Dibi\HashMap' => 'HashMap.php',
'Dibi\HashMapBase' => 'HashMap.php',
'Dibi\Helpers' => 'Helpers.php',
'Dibi\IDataSource' => 'interfaces.php',
'Dibi\Literal' => 'Literal.php',
'Dibi\Loggers\FileLogger' => 'Loggers/FileLogger.php',
'Dibi\Loggers\FirePhpLogger' => 'Loggers/FirePhpLogger.php',
'Dibi\NotImplementedException' => 'exceptions.php',
'Dibi\NotSupportedException' => 'exceptions.php',
'Dibi\PcreException' => 'exceptions.php',
'Dibi\ProcedureException' => 'exceptions.php',
'Dibi\Reflection\Column' => 'Reflection/Column.php',
'Dibi\Reflection\Database' => 'Reflection/Database.php',
'Dibi\Reflection\ForeignKey' => 'Reflection/ForeignKey.php',
'Dibi\Reflection\Index' => 'Reflection/Index.php',
'Dibi\Reflection\Result' => 'Reflection/Result.php',
'Dibi\Reflection\Table' => 'Reflection/Table.php',
'Dibi\Reflector' => 'interfaces.php',
'Dibi\Result' => 'Result.php',
'Dibi\ResultDriver' => 'interfaces.php',
'Dibi\ResultIterator' => 'ResultIterator.php',
'Dibi\Row' => 'Row.php',
'Dibi\Strict' => 'Strict.php',
'Dibi\Translator' => 'Translator.php',
'Dibi\Type' => 'Type.php',
], $old2new = [
'Dibi' => 'dibi.php',
'DibiColumnInfo' => 'Dibi\Reflection\Column',
'DibiConnection' => 'Dibi\Connection',
'DibiDatabaseInfo' => 'Dibi\Reflection\Database',
'DibiDataSource' => 'Dibi\DataSource',
'DibiDateTime' => 'Dibi\DateTime',
'DibiDriverException' => 'Dibi\DriverException',
'DibiEvent' => 'Dibi\Event',
'DibiException' => 'Dibi\Exception',
'DibiFileLogger' => 'Dibi\Loggers\FileLogger',
'DibiFirebirdDriver' => 'Dibi\Drivers\FirebirdDriver',
'DibiFirePhpLogger' => 'Dibi\Loggers\FirePhpLogger',
'DibiFluent' => 'Dibi\Fluent',
'DibiForeignKeyInfo' => 'Dibi\Reflection\ForeignKey',
'DibiHashMap' => 'Dibi\HashMap',
'DibiHashMapBase' => 'Dibi\HashMapBase',
'DibiIndexInfo' => 'Dibi\Reflection\Index',
'DibiLiteral' => 'Dibi\Literal',
'DibiMsSql2005Driver' => 'Dibi\Drivers\SqlsrvDriver',
'DibiMsSql2005Reflector' => 'Dibi\Drivers\SqlsrvReflector',
'DibiMsSqlDriver' => 'Dibi\Drivers\MsSqlDriver',
'DibiMsSqlReflector' => 'Dibi\Drivers\MsSqlReflector',
'DibiMySqlDriver' => 'Dibi\Drivers\MySqlDriver',
'DibiMySqliDriver' => 'Dibi\Drivers\MySqliDriver',
'DibiMySqlReflector' => 'Dibi\Drivers\MySqlReflector',
'DibiNette21Extension' => 'Dibi\Bridges\Nette\DibiExtension21',
'DibiNettePanel' => 'Dibi\Bridges\Nette\Panel',
'DibiNotImplementedException' => 'Dibi\NotImplementedException',
'DibiNotSupportedException' => 'Dibi\NotSupportedException',
'DibiOdbcDriver' => 'Dibi\Drivers\OdbcDriver',
'DibiOracleDriver' => 'Dibi\Drivers\OracleDriver',
'DibiPcreException' => 'Dibi\PcreException',
'DibiPdoDriver' => 'Dibi\Drivers\PdoDriver',
'DibiPostgreDriver' => 'Dibi\Drivers\PostgreDriver',
'DibiProcedureException' => 'Dibi\ProcedureException',
'DibiResult' => 'Dibi\Result',
'DibiResultInfo' => 'Dibi\Reflection\Result',
'DibiResultIterator' => 'Dibi\ResultIterator',
'DibiRow' => 'Dibi\Row',
'DibiSqlite3Driver' => 'Dibi\Drivers\Sqlite3Driver',
'DibiSqliteReflector' => 'Dibi\Drivers\SqliteReflector',
'DibiTableInfo' => 'Dibi\Reflection\Table',
'DibiTranslator' => 'Dibi\Translator',
'IDataSource' => 'Dibi\IDataSource',
'IDibiDriver' => 'Dibi\Driver',
'IDibiReflector' => 'Dibi\Reflector',
'IDibiResultDriver' => 'Dibi\ResultDriver',
'Dibi\Drivers\MsSql2005Driver' => 'Dibi\Drivers\SqlsrvDriver',
'Dibi\Drivers\MsSql2005Reflector' => 'Dibi\Drivers\SqlsrvReflector',
];
if (isset($map[$class])) {
require __DIR__ . '/Dibi/' . $map[$class];
} elseif (isset($old2new[$class])) {
class_alias($old2new[$class], $class);
}
});
// preload for compatiblity
array_map('class_exists', [
'DibiConnection',
'DibiDateTime',
'DibiDriverException',
'DibiEvent',
'DibiException',
'DibiFluent',
'DibiLiteral',
'DibiNotImplementedException',
'DibiNotSupportedException',
'DibiPcreException',
'DibiProcedureException',
'DibiResult',
'DibiRow',
'IDataSource',
'IDibiDriver',
]);

View File

@@ -0,0 +1,89 @@
[sqlite] ; default
driver = sqlite3
database = :memory:
system = sqlite
[sqlite pdo]
driver = pdo
dsn = "sqlite::memory:"
system = sqlite
[mysql]
driver = mysql
host = 127.0.0.1
username = root
password = "Password12!"
charset = utf8
system = mysql
[mysql improved]
driver = mysqli
host = 127.0.0.1
username = root
password = "Password12!"
charset = utf8
system = mysql
[mysql pdo]
driver = pdo
dsn = "mysql:host=127.0.0.1"
username = root
password = "Password12!"
system = mysql
[odbc]
driver = odbc
dsn = "Driver={Microsoft Access Driver (*.mdb)};Dbq=data/odbc.mdb"
system = odbc
[odbc pdo]
driver = pdo
dsn = "odbc:Driver={Microsoft Access Driver (*.mdb)};Dbq=data/odbc.mdb"
username =
password =
system = odbc
;[sqlsrv 2008]
;driver = sqlsrv
;host = "(local)\SQL2008R2SP2"
;database = master
;username = sa
;password = "Password12!"
;system = sqlsrv
;[sqlsrv 2008-pdo]
;driver = pdo
;dsn = "sqlsrv:Server=(local)\SQL2008R2SP2;Database=master"
;username = sa
;password = "Password12!"
;system = sqlsrv
[sqlsrv 2012]
driver = sqlsrv
host = "(local)\SQL2012SP1"
database = master
username = sa
password = "Password12!"
system = sqlsrv
;[sqlsrv 2012-pdo]
;driver = pdo
;dsn = "sqlsrv:Server=(local)\SQL2012SP1;Database=master"
;username = sa
;password = "Password12!"
;system = sqlsrv
[sqlsrv 2014]
driver = sqlsrv
host = "(local)\SQL2014"
database = master
username = sa
password = "Password12!"
system = sqlsrv
;[sqlsrv 2014-pdo]
;driver = pdo
;dsn = "sqlsrv:Server=(local)\SQL2014;Database=master"
;username = sa
;password = "Password12!"
;system = sqlsrv

View File

@@ -1,3 +1,13 @@
[sqlite] ; default
driver = sqlite3
database = :memory:
system = sqlite
[sqlite pdo]
driver = pdo
dsn = "sqlite::memory:"
system = sqlite
[mysql] [mysql]
driver = mysql driver = mysql
host = 127.0.0.1 host = 127.0.0.1
@@ -6,7 +16,7 @@ password =
charset = utf8 charset = utf8
system = mysql system = mysql
[mysqli] [mysql improved]
driver = mysqli driver = mysqli
host = 127.0.0.1 host = 127.0.0.1
username = root username = root
@@ -14,26 +24,37 @@ password =
charset = utf8 charset = utf8
system = mysql system = mysql
[sqlite2] [mysql pdo]
driver = sqlite driver = pdo
database = :memory: dsn = "mysql:host=127.0.0.1"
system = sqlite username = root
password =
system = mysql
[sqlite3] ; default [postgre]
driver = sqlite3
database = :memory:
system = sqlite
[pgsql]
driver = postgre driver = postgre
host = 127.0.0.1 host = 127.0.0.1
username = postgres username = postgres
password = password =
system = pgsql system = postgre
[postgre pdo]
driver = pdo
dsn = "pgsql:host=127.0.0.1;dbname=dibi_test"
username = postgres
password =
system = postgre
[odbc] [odbc]
driver = odbc driver = odbc
dsn = "Driver={Microsoft Access Driver (*.mdb)}Dbq=data/odbc_tmp.mdb" dsn = "Driver={Microsoft Access Driver (*.mdb)};Dbq=data/odbc.mdb"
system = odbc
[odbc pdo]
driver = pdo
dsn = "odbc:Driver={Microsoft Access Driver (*.mdb)};Dbq=data/odbc.mdb"
username =
password =
system = odbc system = odbc
[mssql] [mssql]
@@ -43,34 +64,43 @@ username = dibi
password = password =
system = mssql system = mssql
[mssql2005] [mssql pdo]
driver = mssql2005 driver = pdo
host = (local) host = mssql:host=127.0.0.1;dbname=dibi_test
username = dibi username = dibi
password = password =
system = mssql system = mssql
[sqlsrv]
driver = sqlsrv
host = (local)
username = dibi
password =
system = sqlsrv
[sqlsrv pdo]
driver = pdo
dsn = "sqlsrv:Server=127.0.0.1"
username = dibi
password =
system = sqlsrv
[oracle] [oracle]
driver = oracle driver = oracle
username = dibi username = dibi
password = password =
system = oracle system = oracle
[sqlite-pdo] [oracle pdo]
driver = pdo driver = pdo
dsn = "sqlite::memory:" dsn = "oci:dbname=dibi"
system = sqlite username = dibi
[mysql-pdo]
driver = pdo
dsn = "mysql:host=127.0.0.1"
username = root
password = password =
system = mysql system = oracle
[pgsql-pdo] [firebird]
driver = pdo driver = firebird
dsn = "pgsql:host=127.0.0.1;dbname=dibi_test" database = database.fdb
username = postgres username = dibi
password = password =
system = pgsql system = firebird

View File

@@ -0,0 +1,46 @@
[sqlite] ; default
driver = sqlite3
database = :memory:
system = sqlite
[sqlite pdo]
driver = pdo
dsn = "sqlite::memory:"
system = sqlite
[mysql]
driver = mysql
host = 127.0.0.1
username = root
password =
charset = utf8
system = mysql
[mysql improved]
driver = mysqli
host = 127.0.0.1
username = root
password =
charset = utf8
system = mysql
[mysql pdo]
driver = pdo
dsn = "mysql:host=127.0.0.1"
username = root
password =
system = mysql
[postgre]
driver = postgre
host = 127.0.0.1
username = postgres
password =
system = postgre
[postgre pdo]
driver = pdo
dsn = "pgsql:host=127.0.0.1;dbname=dibi_test"
username = postgres
password =
system = postgre

View File

@@ -8,13 +8,13 @@ use Tester\Assert;
require __DIR__ . '/bootstrap.php'; require __DIR__ . '/bootstrap.php';
$conn = new DibiConnection($config); $conn = new Dibi\Connection($config);
$conn->loadFile(__DIR__ . "/data/$config[system].sql"); $conn->loadFile(__DIR__ . "/data/$config[system].sql");
$conn->query('INSERT INTO products', array( $conn->query('INSERT INTO products', [
'title' => 'Test product', 'title' => 'Test product',
)); ]);
Assert::same(1, $conn->getAffectedRows()); Assert::same(1, $conn->getAffectedRows());

View File

@@ -5,12 +5,13 @@
*/ */
use Tester\Assert; use Tester\Assert;
use Dibi\Connection;
require __DIR__ . '/bootstrap.php'; require __DIR__ . '/bootstrap.php';
test(function () use ($config) { test(function () use ($config) {
$conn = new DibiConnection($config); $conn = new Connection($config);
Assert::true($conn->isConnected()); Assert::true($conn->isConnected());
$conn->disconnect(); $conn->disconnect();
@@ -19,7 +20,7 @@ test(function () use ($config) {
test(function () use ($config) { // lazy test(function () use ($config) { // lazy
$conn = new DibiConnection($config + array('lazy' => TRUE)); $conn = new Connection($config + ['lazy' => TRUE]);
Assert::false($conn->isConnected()); Assert::false($conn->isConnected());
$conn->query('SELECT 1'); $conn->query('SELECT 1');
@@ -28,10 +29,10 @@ test(function () use ($config) { // lazy
test(function () use ($config) { // query string test(function () use ($config) { // query string
$conn = new DibiConnection(http_build_query($config, NULL, '&')); $conn = new Connection(http_build_query($config, NULL, '&'));
Assert::true($conn->isConnected()); Assert::true($conn->isConnected());
Assert::null($conn->getConfig('lazy')); Assert::null($conn->getConfig('lazy'));
Assert::same($config['driver'], $conn->getConfig('driver')); Assert::same($config['driver'], $conn->getConfig('driver'));
Assert::type('IDibiDriver', $conn->getDriver()); Assert::type('Dibi\Driver', $conn->getDriver());
}); });

View File

@@ -0,0 +1,320 @@
<?php
/**
* @dataProvider ../databases.ini
*/
use Tester\Assert;
use Dibi\Row;
require __DIR__ . '/bootstrap.php';
$conn = new Dibi\Connection($config);
$conn->loadFile(__DIR__ . "/data/$config[system].sql");
// fetch a single value
$res = $conn->query('SELECT [title] FROM [products] ORDER BY [product_id]');
Assert::same('Chair', $res->fetchSingle());
// fetch complete result set
$res = $conn->query('SELECT * FROM [products] ORDER BY product_id');
Assert::equal([
new Row(['product_id' => num(1), 'title' => 'Chair']),
new Row(['product_id' => num(2), 'title' => 'Table']),
new Row(['product_id' => num(3), 'title' => 'Computer']),
], $res->fetchAll());
// fetch complete result set like pairs key => value
$res = $conn->query('SELECT * FROM [products] ORDER BY product_id');
Assert::same(
[1 => 'Chair', 'Table', 'Computer'],
$res->fetchPairs('product_id', 'title')
);
$res = $conn->query('SELECT * FROM [products] ORDER BY product_id');
Assert::same(
[1 => 'Chair', 'Table', 'Computer'],
$res->fetchPairs()
);
// fetch row by row
$res = $conn->query('SELECT * FROM [products] ORDER BY product_id');
Assert::equal([
new Row(['product_id' => num(1), 'title' => 'Chair']),
new Row(['product_id' => num(2), 'title' => 'Table']),
new Row(['product_id' => num(3), 'title' => 'Computer']),
], iterator_to_array($res));
// fetch complete result set like association array
$res = $conn->query('SELECT * FROM [products] ORDER BY product_id');
Assert::equal([
'Chair' => new Row(['product_id' => num(1), 'title' => 'Chair']),
'Table' => new Row(['product_id' => num(2), 'title' => 'Table']),
'Computer' => new Row(['product_id' => num(3), 'title' => 'Computer']),
], $res->fetchAssoc('title'));
// more complex association array
function query($conn) {
return $conn->query(in_array($conn->getConfig('system'), ['odbc', 'sqlsrv']) ? '
SELECT products.title, customers.name, orders.amount
FROM ([products]
INNER JOIN [orders] ON [products.product_id] = [orders.product_id])
INNER JOIN [customers] ON [orders.customer_id] = [customers.customer_id]
ORDER BY orders.order_id
' : '
SELECT products.title AS title, customers.name AS name, orders.amount AS amount
FROM [products]
INNER JOIN [orders] USING ([product_id])
INNER JOIN [customers] USING ([customer_id])
ORDER BY orders.order_id
');
}
Assert::equal([
'Arnold Rimmer' => [
'Chair' => new Row(['title' => 'Chair', 'name' => 'Arnold Rimmer', 'amount' => num(7.0)]),
'Computer' => new Row(['title' => 'Computer', 'name' => 'Arnold Rimmer', 'amount' => num(2.0)]),
],
'Dave Lister' => [
'Table' => new Row(['title' => 'Table', 'name' => 'Dave Lister', 'amount' => num(3.0)]),
],
'Kristine Kochanski' => [
'Computer' => new Row(['title' => 'Computer', 'name' => 'Kristine Kochanski', 'amount' => num(5.0)]),
],
], query($conn)->fetchAssoc('name,title'));
Assert::equal([
'Arnold Rimmer' => [
[
'Chair' => new Row(['title' => 'Chair', 'name' => 'Arnold Rimmer', 'amount' => num(7.0)]),
],
[
'Computer' => new Row(['title' => 'Computer', 'name' => 'Arnold Rimmer', 'amount' => num(2.0)]),
],
],
'Dave Lister' => [
[
'Table' => new Row(['title' => 'Table', 'name' => 'Dave Lister', 'amount' => num(3.0)]),
],
],
'Kristine Kochanski' => [
[
'Computer' => new Row(['title' => 'Computer', 'name' => 'Kristine Kochanski', 'amount' => num(5.0)]),
],
],
], query($conn)->fetchAssoc('name,#,title'));
Assert::equal([
'Arnold Rimmer' => [
'title' => [
'Chair' => new Row(['title' => 'Chair', 'name' => 'Arnold Rimmer', 'amount' => num(7.0)]),
'Computer' => new Row(['title' => 'Computer', 'name' => 'Arnold Rimmer', 'amount' => num(2.0)]),
],
'name' => 'Arnold Rimmer',
'amount' => num(7.0),
],
'Dave Lister' => [
'title' => [
'Table' => new Row(['title' => 'Table', 'name' => 'Dave Lister', 'amount' => num(3.0)]),
],
'name' => 'Dave Lister',
'amount' => num(3.0),
],
'Kristine Kochanski' => [
'title' => [
'Computer' => new Row(['title' => 'Computer', 'name' => 'Kristine Kochanski', 'amount' => num(5.0)]),
],
'name' => 'Kristine Kochanski',
'amount' => num(5.0),
],
], query($conn)->fetchAssoc('name,=,title'));
Assert::equal([
'Arnold Rimmer' => new Row([
'title' => [
'Chair' => new Row(['title' => 'Chair', 'name' => 'Arnold Rimmer', 'amount' => num(7.0)]),
'Computer' => new Row(['title' => 'Computer', 'name' => 'Arnold Rimmer', 'amount' => num(2.0)]),
],
'name' => 'Arnold Rimmer',
'amount' => num(7.0),
]),
'Dave Lister' => new Row([
'title' => [
'Table' => new Row(['title' => 'Table', 'name' => 'Dave Lister', 'amount' => num(3.0)]),
],
'name' => 'Dave Lister',
'amount' => num(3.0),
]),
'Kristine Kochanski' => new Row([
'title' => [
'Computer' => new Row(['title' => 'Computer', 'name' => 'Kristine Kochanski', 'amount' => num(5.0)]),
],
'name' => 'Kristine Kochanski',
'amount' => num(5.0),
]),
], query($conn)->fetchAssoc('name,@,title'));
Assert::equal([
new Row(['title' => 'Chair', 'name' => 'Arnold Rimmer', 'amount' => num(7.0)]),
new Row([
'title' => 'Computer', 'name' => 'Arnold Rimmer', 'amount' => num(2.0)]),
new Row([
'title' => 'Table', 'name' => 'Dave Lister', 'amount' => num(3.0)]),
new Row([
'title' => 'Computer', 'name' => 'Kristine Kochanski', 'amount' => num(5.0)]),
], query($conn)->fetchAssoc('@,='));
Assert::equal([
'Arnold Rimmer' => [
'title' => [
'Chair' => new Row(['title' => 'Chair', 'name' => 'Arnold Rimmer', 'amount' => num(7.0)]),
'Computer' => new Row(['title' => 'Computer', 'name' => 'Arnold Rimmer', 'amount' => num(2.0)]),
],
'name' => 'Arnold Rimmer',
'amount' => num(7.0),
],
'Dave Lister' => [
'title' => [
'Table' => new Row(['title' => 'Table', 'name' => 'Dave Lister', 'amount' => num(3.0)]),
],
'name' => 'Dave Lister',
'amount' => num(3.0),
],
'Kristine Kochanski' => [
'title' => [
'Computer' => new Row(['title' => 'Computer', 'name' => 'Kristine Kochanski', 'amount' => num(5.0)]),
],
'name' => 'Kristine Kochanski',
'amount' => num(5.0),
],
], query($conn)->fetchAssoc('name,=,title,@'));
// old syntax
Assert::equal([
'Arnold Rimmer' => [
'Chair' => new Row(['title' => 'Chair', 'name' => 'Arnold Rimmer', 'amount' => num(7.0)]),
'Computer' => new Row(['title' => 'Computer', 'name' => 'Arnold Rimmer', 'amount' => num(2.0)]),
],
'Dave Lister' => [
'Table' => new Row(['title' => 'Table', 'name' => 'Dave Lister', 'amount' => num(3.0)]),
],
'Kristine Kochanski' => [
'Computer' => new Row(['title' => 'Computer', 'name' => 'Kristine Kochanski', 'amount' => num(5.0)]),
],
], query($conn)->fetchAssoc('name|title'));
Assert::equal([
'Arnold Rimmer' => [
[
'Chair' => new Row(['title' => 'Chair', 'name' => 'Arnold Rimmer', 'amount' => num(7.0)]),
],
[
'Computer' => new Row(['title' => 'Computer', 'name' => 'Arnold Rimmer', 'amount' => num(2.0)]),
],
],
'Dave Lister' => [
[
'Table' => new Row(['title' => 'Table', 'name' => 'Dave Lister', 'amount' => num(3.0)]),
],
],
'Kristine Kochanski' => [
[
'Computer' => new Row(['title' => 'Computer', 'name' => 'Kristine Kochanski', 'amount' => num(5.0)]),
],
],
], query($conn)->fetchAssoc('name[]title'));
Assert::equal([
'Arnold Rimmer' => new Row([
'title' => [
'Chair' => new Row(['title' => 'Chair', 'name' => 'Arnold Rimmer', 'amount' => num(7.0)]),
'Computer' => new Row(['title' => 'Computer', 'name' => 'Arnold Rimmer', 'amount' => num(2.0)]),
],
'name' => 'Arnold Rimmer',
'amount' => num(7.0),
]),
'Dave Lister' => new Row([
'title' => [
'Table' => new Row(['title' => 'Table', 'name' => 'Dave Lister', 'amount' => num(3.0)]),
],
'name' => 'Dave Lister',
'amount' => num(3.0),
]),
'Kristine Kochanski' => new Row([
'title' => [
'Computer' => new Row(['title' => 'Computer', 'name' => 'Kristine Kochanski', 'amount' => num(5.0)]),
],
'name' => 'Kristine Kochanski',
'amount' => num(5.0),
]),
], query($conn)->fetchAssoc('name->title'));
Assert::equal([
'Arnold Rimmer' => new Row([
'title' => ['Chair' => 'Arnold Rimmer', 'Computer' => 'Arnold Rimmer'],
'name' => 'Arnold Rimmer',
'amount' => num(7.0),
]),
'Dave Lister' => new Row([
'title' => ['Table' => 'Dave Lister'],
'name' => 'Dave Lister',
'amount' => num(3.0),
]),
'Kristine Kochanski' => new Row([
'title' => ['Computer' => 'Kristine Kochanski'],
'name' => 'Kristine Kochanski',
'amount' => num(5.0),
]),
], query($conn)->fetchAssoc('name->title=name'));
Assert::equal([
new Row(['title' => 'Chair', 'name' => 'Arnold Rimmer', 'amount' => num(7.0)]),
new Row(['title' => 'Computer', 'name' => 'Arnold Rimmer', 'amount' => num(2.0)]),
new Row(['title' => 'Table', 'name' => 'Dave Lister', 'amount' => num(3.0)]),
new Row(['title' => 'Computer', 'name' => 'Kristine Kochanski', 'amount' => num(5.0)]),
], query($conn)->fetchAssoc('[]'));
Assert::equal([
'Arnold Rimmer' => new Row([
'title' => [
'Chair' => new Row(['title' => 'Chair', 'name' => 'Arnold Rimmer', 'amount' => num(7.0)]),
'Computer' => new Row(['title' => 'Computer', 'name' => 'Arnold Rimmer', 'amount' => num(2.0)]),
],
'name' => 'Arnold Rimmer',
'amount' => num(7.0),
]),
'Dave Lister' => new Row([
'title' => [
'Table' => new Row(['title' => 'Table', 'name' => 'Dave Lister', 'amount' => num(3.0)]),
],
'name' => 'Dave Lister',
'amount' => num(3.0),
]),
'Kristine Kochanski' => new Row([
'title' => [
'Computer' => new Row(['title' => 'Computer', 'name' => 'Kristine Kochanski', 'amount' => num(5.0)]),
],
'name' => 'Kristine Kochanski',
'amount' => num(5.0),
]),
], query($conn)->fetchAssoc('name->title->'));

View File

@@ -0,0 +1,56 @@
<?php
use Tester\Assert;
require __DIR__ . '/bootstrap.php';
$conn = new Dibi\Connection($config);
// create new substitution :blog: ==> wp_
$conn->getSubstitutes()->blog = 'wp_';
Assert::same(
reformat('UPDATE wp_items SET [val]=1'),
$conn->translate('UPDATE :blog:items SET [val]=1')
);
Assert::same(
reformat('UPDATE [wp_items] SET [val]=1'),
$conn->translate('UPDATE [:blog:items] SET [val]=1')
);
Assert::same(
reformat("UPDATE 'wp_' SET [val]=1"),
$conn->translate('UPDATE :blog: SET [val]=1')
);
Assert::same(
reformat("UPDATE ':blg:' SET [val]=1"),
$conn->translate('UPDATE :blg: SET [val]=1')
);
Assert::same(
reformat("UPDATE table SET [text]=':blog:a'"),
$conn->translate("UPDATE table SET [text]=':blog:a'")
);
// create new substitution :: (empty) ==> my_
$conn->getSubstitutes()->{''} = 'my_';
Assert::same(
reformat('UPDATE my_table SET [val]=1'),
$conn->translate('UPDATE ::table SET [val]=1')
);
// create substitutions using fallback callback
$conn->getSubstitutes()->setCallback(function ($expr) {
return '_' . $expr . '_';
});
Assert::same(
reformat('UPDATE _account_user SET [val]=1'),
$conn->translate('UPDATE :account:user SET [val]=1')
);

View File

@@ -9,30 +9,30 @@ use Tester\Assert;
require __DIR__ . '/bootstrap.php'; require __DIR__ . '/bootstrap.php';
$conn = new DibiConnection($config); $conn = new Dibi\Connection($config);
$conn->loadFile(__DIR__ . "/data/$config[system].sql"); $conn->loadFile(__DIR__ . "/data/$config[system].sql");
/*Assert::exception(function () use ($conn) { /*Assert::exception(function () use ($conn) {
$conn->rollback(); $conn->rollback();
}, 'DibiException'); }, 'Dibi\Exception');
Assert::exception(function () use ($conn) { Assert::exception(function () use ($conn) {
$conn->commit(); $conn->commit();
}, 'DibiException'); }, 'Dibi\Exception');
$conn->begin(); $conn->begin();
Assert::exception(function () use ($conn) { Assert::exception(function () use ($conn) {
$conn->begin(); $conn->begin();
}, 'DibiException'); }, 'Dibi\Exception');
*/ */
$conn->begin(); $conn->begin();
Assert::same(3, (int) $conn->query('SELECT COUNT(*) FROM [products]')->fetchSingle()); Assert::same(3, (int) $conn->query('SELECT COUNT(*) FROM [products]')->fetchSingle());
$conn->query('INSERT INTO [products]', array( $conn->query('INSERT INTO [products]', [
'title' => 'Test product', 'title' => 'Test product',
)); ]);
Assert::same(4, (int) $conn->query('SELECT COUNT(*) FROM [products]')->fetchSingle()); Assert::same(4, (int) $conn->query('SELECT COUNT(*) FROM [products]')->fetchSingle());
$conn->rollback(); $conn->rollback();
Assert::same(3, (int) $conn->query('SELECT COUNT(*) FROM [products]')->fetchSingle()); Assert::same(3, (int) $conn->query('SELECT COUNT(*) FROM [products]')->fetchSingle());
@@ -41,8 +41,8 @@ Assert::same(3, (int) $conn->query('SELECT COUNT(*) FROM [products]')->fetchSing
$conn->begin(); $conn->begin();
$conn->query('INSERT INTO [products]', array( $conn->query('INSERT INTO [products]', [
'title' => 'Test product', 'title' => 'Test product',
)); ]);
$conn->commit(); $conn->commit();
Assert::same(4, (int) $conn->query('SELECT COUNT(*) FROM [products]')->fetchSingle()); Assert::same(4, (int) $conn->query('SELECT COUNT(*) FROM [products]')->fetchSingle());

View File

@@ -1,11 +1,12 @@
<?php <?php
use Tester\Assert; use Tester\Assert;
use Dibi\Row;
require __DIR__ . '/bootstrap.php'; require __DIR__ . '/bootstrap.php';
$conn = new DibiConnection($config); $conn = new Dibi\Connection($config);
$conn->loadFile(__DIR__ . "/data/$config[system].sql"); $conn->loadFile(__DIR__ . "/data/$config[system].sql");
@@ -54,9 +55,9 @@ FROM (SELECT * FROM products) t
); );
$ds->select(array('product_id')); $ds->select(['product_id']);
$ds->orderBy(array('product_id' => dibi::ASC)); $ds->orderBy(['product_id' => dibi::ASC]);
$ds->where(array('product_id = 1')); $ds->where(['product_id = 1']);
Assert::match( Assert::match(
reformat(" reformat("
SELECT [product_id] SELECT [product_id]
@@ -79,11 +80,11 @@ FROM (SELECT * FROM products) t
Assert::same(1, $ds->toDataSource()->count()); Assert::same(1, $ds->toDataSource()->count());
Assert::equal(array( Assert::equal([
new DibiRow(array( new Row([
'product_id' => 1, 'product_id' => 1,
)), ]),
), iterator_to_array($ds)); ], iterator_to_array($ds));
Assert::match( Assert::match(
reformat(" reformat("
@@ -117,35 +118,35 @@ FROM (SELECT [title] FROM [products]) t'),
(string) $ds (string) $ds
); );
Assert::equal(new DibiRow(array( Assert::equal(new Row([
'product_id' => 1, 'product_id' => 1,
'title' => 'Chair', 'title' => 'Chair',
)), $conn->dataSource('SELECT * FROM products ORDER BY product_id')->fetch()); ]), $conn->dataSource('SELECT * FROM products ORDER BY product_id')->fetch());
Assert::same(1, $conn->dataSource('SELECT * FROM products ORDER BY product_id')->fetchSingle()); Assert::same(1, $conn->dataSource('SELECT * FROM products ORDER BY product_id')->fetchSingle());
Assert::same( Assert::same(
array(1 => 'Chair', 'Table', 'Computer'), [1 => 'Chair', 'Table', 'Computer'],
$conn->dataSource('SELECT * FROM products ORDER BY product_id')->fetchPairs() $conn->dataSource('SELECT * FROM products ORDER BY product_id')->fetchPairs()
); );
Assert::equal(array( Assert::equal([
1 => new DibiRow(array( 1 => new Row([
'product_id' => 1, 'product_id' => 1,
'title' => 'Chair', 'title' => 'Chair',
)), ]),
new DibiRow(array( new Row([
'product_id' => 2, 'product_id' => 2,
'title' => 'Table', 'title' => 'Table',
)), ]),
new DibiRow(array( new Row([
'product_id' => 3, 'product_id' => 3,
'title' => 'Computer', 'title' => 'Computer',
)), ]),
), $conn->dataSource('SELECT * FROM products ORDER BY product_id')->fetchAssoc('product_id')); ], $conn->dataSource('SELECT * FROM products ORDER BY product_id')->fetchAssoc('product_id'));
$ds = new DibiDataSource('products', $conn); $ds = new Dibi\DataSource('products', $conn);
Assert::match( Assert::match(
reformat(' reformat('

View File

@@ -1,329 +0,0 @@
<?php
/**
* @dataProvider ../databases.ini
*/
use Tester\Assert;
require __DIR__ . '/bootstrap.php';
$conn = new DibiConnection($config);
$conn->loadFile(__DIR__ . "/data/$config[system].sql");
function num($n)
{
global $config;
if (substr(@$config['dsn'], 0, 5) === 'odbc:' || $config['driver'] === 'sqlite') {
$n = is_float($n) ? "$n.0" : (string) $n;
}
return $n;
}
// fetch a single value
$res = $conn->query('SELECT [title] FROM [products]');
Assert::same('Chair', $res->fetchSingle());
// fetch complete result set
$res = $conn->query('SELECT * FROM [products] ORDER BY product_id');
Assert::equal(array(
new DibiRow(array('product_id' => num(1), 'title' => 'Chair')),
new DibiRow(array('product_id' => num(2), 'title' => 'Table')),
new DibiRow(array('product_id' => num(3), 'title' => 'Computer')),
), $res->fetchAll());
// fetch complete result set like pairs key => value
$res = $conn->query('SELECT * FROM [products] ORDER BY product_id');
Assert::same(
array(1 => 'Chair', 'Table', 'Computer'),
$res->fetchPairs('product_id', 'title')
);
$res = $conn->query('SELECT * FROM [products] ORDER BY product_id');
Assert::same(
array(1 => 'Chair', 'Table', 'Computer'),
$res->fetchPairs()
);
// fetch row by row
$res = $conn->query('SELECT * FROM [products] ORDER BY product_id');
Assert::equal(array(
new DibiRow(array('product_id' => num(1), 'title' => 'Chair')),
new DibiRow(array('product_id' => num(2), 'title' => 'Table')),
new DibiRow(array('product_id' => num(3), 'title' => 'Computer')),
), iterator_to_array($res));
// fetch complete result set like association array
$res = $conn->query('SELECT * FROM [products] ORDER BY product_id');
Assert::equal(array(
'Chair' => new DibiRow(array('product_id' => num(1), 'title' => 'Chair')),
'Table' => new DibiRow(array('product_id' => num(2), 'title' => 'Table')),
'Computer' => new DibiRow(array('product_id' => num(3), 'title' => 'Computer')),
), $res->fetchAssoc('title'));
// more complex association array
function query($conn) {
return $conn->query($conn->getConfig('system') === 'odbc' ? '
SELECT products.title, customers.name, orders.amount
FROM ([products]
INNER JOIN [orders] ON [products.product_id] = [orders.product_id])
INNER JOIN [customers] ON [orders.customer_id] = [customers.customer_id]
ORDER BY orders.order_id
' : '
SELECT products.title AS title, customers.name AS name, orders.amount AS amount
FROM [products]
INNER JOIN [orders] USING ([product_id])
INNER JOIN [customers] USING ([customer_id])
ORDER BY orders.order_id
');
}
Assert::equal(array(
'Arnold Rimmer' => array(
'Chair' => new DibiRow(array('title' => 'Chair', 'name' => 'Arnold Rimmer', 'amount' => num(7.0))),
'Computer' => new DibiRow(array('title' => 'Computer', 'name' => 'Arnold Rimmer', 'amount' => num(2.0))),
),
'Dave Lister' => array(
'Table' => new DibiRow(array('title' => 'Table', 'name' => 'Dave Lister', 'amount' => num(3.0))),
),
'Kristine Kochanski' => array(
'Computer' => new DibiRow(array('title' => 'Computer', 'name' => 'Kristine Kochanski', 'amount' => num(5.0))),
),
), query($conn)->fetchAssoc('name,title'));
Assert::equal(array(
'Arnold Rimmer' => array(
array(
'Chair' => new DibiRow(array('title' => 'Chair', 'name' => 'Arnold Rimmer', 'amount' => num(7.0))),
),
array(
'Computer' => new DibiRow(array('title' => 'Computer', 'name' => 'Arnold Rimmer', 'amount' => num(2.0))),
),
),
'Dave Lister' => array(
array(
'Table' => new DibiRow(array('title' => 'Table', 'name' => 'Dave Lister', 'amount' => num(3.0))),
),
),
'Kristine Kochanski' => array(
array(
'Computer' => new DibiRow(array('title' => 'Computer', 'name' => 'Kristine Kochanski', 'amount' => num(5.0))),
),
),
), query($conn)->fetchAssoc('name,#,title'));
Assert::equal(array(
'Arnold Rimmer' => array(
'title' => array(
'Chair' => new DibiRow(array('title' => 'Chair', 'name' => 'Arnold Rimmer', 'amount' => num(7.0))),
'Computer' => new DibiRow(array('title' => 'Computer', 'name' => 'Arnold Rimmer', 'amount' => num(2.0))),
),
'name' => 'Arnold Rimmer',
'amount' => num(7.0),
),
'Dave Lister' => array(
'title' => array(
'Table' => new DibiRow(array('title' => 'Table', 'name' => 'Dave Lister', 'amount' => num(3.0))),
),
'name' => 'Dave Lister',
'amount' => num(3.0),
),
'Kristine Kochanski' => array(
'title' => array(
'Computer' => new DibiRow(array('title' => 'Computer', 'name' => 'Kristine Kochanski', 'amount' => num(5.0))),
),
'name' => 'Kristine Kochanski',
'amount' => num(5.0),
),
), query($conn)->fetchAssoc('name,=,title'));
Assert::equal(array(
'Arnold Rimmer' => new DibiRow(array(
'title' => array(
'Chair' => new DibiRow(array('title' => 'Chair', 'name' => 'Arnold Rimmer', 'amount' => num(7.0))),
'Computer' => new DibiRow(array('title' => 'Computer', 'name' => 'Arnold Rimmer', 'amount' => num(2.0))),
),
'name' => 'Arnold Rimmer',
'amount' => num(7.0),
)),
'Dave Lister' => new DibiRow(array(
'title' => array(
'Table' => new DibiRow(array('title' => 'Table', 'name' => 'Dave Lister', 'amount' => num(3.0))),
),
'name' => 'Dave Lister',
'amount' => num(3.0),
)),
'Kristine Kochanski' => new DibiRow(array(
'title' => array(
'Computer' => new DibiRow(array('title' => 'Computer', 'name' => 'Kristine Kochanski', 'amount' => num(5.0))),
),
'name' => 'Kristine Kochanski',
'amount' => num(5.0),
)),
), query($conn)->fetchAssoc('name,@,title'));
Assert::equal(array(
new DibiRow(array('title' => 'Chair', 'name' => 'Arnold Rimmer', 'amount' => num(7.0))),
new DibiRow(array(
'title' => 'Computer', 'name' => 'Arnold Rimmer', 'amount' => num(2.0))),
new DibiRow(array(
'title' => 'Table', 'name' => 'Dave Lister', 'amount' => num(3.0))),
new DibiRow(array(
'title' => 'Computer', 'name' => 'Kristine Kochanski', 'amount' => num(5.0))),
), query($conn)->fetchAssoc('@,='));
Assert::equal(array(
'Arnold Rimmer' => array(
'title' => array(
'Chair' => new DibiRow(array('title' => 'Chair', 'name' => 'Arnold Rimmer', 'amount' => num(7.0))),
'Computer' => new DibiRow(array('title' => 'Computer', 'name' => 'Arnold Rimmer', 'amount' => num(2.0))),
),
'name' => 'Arnold Rimmer',
'amount' => num(7.0),
),
'Dave Lister' => array(
'title' => array(
'Table' => new DibiRow(array('title' => 'Table', 'name' => 'Dave Lister', 'amount' => num(3.0))),
),
'name' => 'Dave Lister',
'amount' => num(3.0),
),
'Kristine Kochanski' => array(
'title' => array(
'Computer' => new DibiRow(array('title' => 'Computer', 'name' => 'Kristine Kochanski', 'amount' => num(5.0))),
),
'name' => 'Kristine Kochanski',
'amount' => num(5.0),
),
), query($conn)->fetchAssoc('name,=,title,@'));
// old syntax
Assert::equal(array(
'Arnold Rimmer' => array(
'Chair' => new DibiRow(array('title' => 'Chair', 'name' => 'Arnold Rimmer', 'amount' => num(7.0))),
'Computer' => new DibiRow(array('title' => 'Computer', 'name' => 'Arnold Rimmer', 'amount' => num(2.0))),
),
'Dave Lister' => array(
'Table' => new DibiRow(array('title' => 'Table', 'name' => 'Dave Lister', 'amount' => num(3.0))),
),
'Kristine Kochanski' => array(
'Computer' => new DibiRow(array('title' => 'Computer', 'name' => 'Kristine Kochanski', 'amount' => num(5.0))),
),
), query($conn)->fetchAssoc('name|title'));
Assert::equal(array(
'Arnold Rimmer' => array(
array(
'Chair' => new DibiRow(array('title' => 'Chair', 'name' => 'Arnold Rimmer', 'amount' => num(7.0))),
),
array(
'Computer' => new DibiRow(array('title' => 'Computer', 'name' => 'Arnold Rimmer', 'amount' => num(2.0))),
),
),
'Dave Lister' => array(
array(
'Table' => new DibiRow(array('title' => 'Table', 'name' => 'Dave Lister', 'amount' => num(3.0))),
),
),
'Kristine Kochanski' => array(
array(
'Computer' => new DibiRow(array('title' => 'Computer', 'name' => 'Kristine Kochanski', 'amount' => num(5.0))),
),
),
), query($conn)->fetchAssoc('name[]title'));
Assert::equal(array(
'Arnold Rimmer' => new DibiRow(array(
'title' => array(
'Chair' => new DibiRow(array('title' => 'Chair', 'name' => 'Arnold Rimmer', 'amount' => num(7.0))),
'Computer' => new DibiRow(array('title' => 'Computer', 'name' => 'Arnold Rimmer', 'amount' => num(2.0))),
),
'name' => 'Arnold Rimmer',
'amount' => num(7.0),
)),
'Dave Lister' => new DibiRow(array(
'title' => array(
'Table' => new DibiRow(array('title' => 'Table', 'name' => 'Dave Lister', 'amount' => num(3.0))),
),
'name' => 'Dave Lister',
'amount' => num(3.0),
)),
'Kristine Kochanski' => new DibiRow(array(
'title' => array(
'Computer' => new DibiRow(array('title' => 'Computer', 'name' => 'Kristine Kochanski', 'amount' => num(5.0))),
),
'name' => 'Kristine Kochanski',
'amount' => num(5.0),
)),
), query($conn)->fetchAssoc('name->title'));
Assert::equal(array(
'Arnold Rimmer' => new DibiRow(array(
'title' => array('Chair' => 'Arnold Rimmer', 'Computer' => 'Arnold Rimmer'),
'name' => 'Arnold Rimmer',
'amount' => num(7.0),
)),
'Dave Lister' => new DibiRow(array(
'title' => array('Table' => 'Dave Lister'),
'name' => 'Dave Lister',
'amount' => num(3.0),
)),
'Kristine Kochanski' => new DibiRow(array(
'title' => array('Computer' => 'Kristine Kochanski'),
'name' => 'Kristine Kochanski',
'amount' => num(5.0),
)),
), query($conn)->fetchAssoc('name->title=name'));
Assert::equal(array(
new DibiRow(array('title' => 'Chair', 'name' => 'Arnold Rimmer', 'amount' => num(7.0))),
new DibiRow(array('title' => 'Computer', 'name' => 'Arnold Rimmer', 'amount' => num(2.0))),
new DibiRow(array('title' => 'Table', 'name' => 'Dave Lister', 'amount' => num(3.0))),
new DibiRow(array('title' => 'Computer', 'name' => 'Kristine Kochanski', 'amount' => num(5.0))),
), query($conn)->fetchAssoc('[]'));
Assert::equal(array(
'Arnold Rimmer' => new DibiRow(array(
'title' => array(
'Chair' => new DibiRow(array('title' => 'Chair', 'name' => 'Arnold Rimmer', 'amount' => num(7.0))),
'Computer' => new DibiRow(array('title' => 'Computer', 'name' => 'Arnold Rimmer', 'amount' => num(2.0))),
),
'name' => 'Arnold Rimmer',
'amount' => num(7.0),
)),
'Dave Lister' => new DibiRow(array(
'title' => array(
'Table' => new DibiRow(array('title' => 'Table', 'name' => 'Dave Lister', 'amount' => num(3.0))),
),
'name' => 'Dave Lister',
'amount' => num(3.0),
)),
'Kristine Kochanski' => new DibiRow(array(
'title' => array(
'Computer' => new DibiRow(array('title' => 'Computer', 'name' => 'Kristine Kochanski', 'amount' => num(5.0))),
),
'name' => 'Kristine Kochanski',
'amount' => num(5.0),
)),
), query($conn)->fetchAssoc('name->title->'));

View File

@@ -1,26 +0,0 @@
<?php
use Tester\Assert;
require __DIR__ . '/bootstrap.php';
$conn = new DibiConnection($config);
// create new substitution :blog: ==> wp_
$conn->getSubstitutes()->blog = 'wp_';
Assert::same(
reformat('UPDATE wp_items SET [text]=\'Hello World\''),
$conn->translate("UPDATE :blog:items SET [text]='Hello World'")
);
Assert::same(
reformat('UPDATE \'wp_\' SET [text]=\'Hello World\''),
$conn->translate("UPDATE :blog: SET [text]='Hello World'")
);
Assert::same(
reformat('UPDATE \':blg:\' SET [text]=\'Hello World\''),
$conn->translate("UPDATE :blg: SET [text]='Hello World'")
);

View File

@@ -1,59 +0,0 @@
<?php
/**
* @dataProvider ../databases.ini
*/
use Tester\Assert;
require __DIR__ . '/bootstrap.php';
$conn = new DibiConnection($config);
$conn->loadFile(__DIR__ . "/data/$config[system].sql");
function num($n)
{
global $config;
if (substr(@$config['dsn'], 0, 5) === 'odbc:' || $config['driver'] === 'sqlite') {
$n = is_float($n) ? "$n.0" : (string) $n;
}
return $n;
}
// fetch a single value
$res = $conn->select('title')->from('products')->orderBy('product_id');
Assert::equal('Chair', $res->fetchSingle());
// fetch complete result set
$res = $conn->select('*')->from('products')->orderBy('product_id');
Assert::equal(array(
new DibiRow(array('product_id' => num(1), 'title' => 'Chair')),
new DibiRow(array('product_id' => num(2), 'title' => 'Table')),
new DibiRow(array('product_id' => num(3), 'title' => 'Computer')),
), $res->fetchAll());
// more complex association array
if ($config['system'] !== 'odbc') {
$res = $conn->select(array('products.title' => 'title', 'customers.name' => 'name'))->select('orders.amount')->as('amount')
->from('products')
->innerJoin('orders')->using('(product_id)')
->innerJoin('customers')->using('([customer_id])')
->orderBy('order_id');
Assert::equal(array(
'Arnold Rimmer' => array(
'Chair' => new DibiRow(array('title' => 'Chair', 'name' => 'Arnold Rimmer', 'amount' => num(7.0))),
'Computer' => new DibiRow(array('title' => 'Computer', 'name' => 'Arnold Rimmer', 'amount' => num(2.0))),
),
'Dave Lister' => array(
'Table' => new DibiRow(array('title' => 'Table', 'name' => 'Dave Lister', 'amount' => num(3.0))),
),
'Kristine Kochanski' => array(
'Computer' => new DibiRow(array('title' => 'Computer', 'name' => 'Kristine Kochanski', 'amount' => num(5.0))),
),
), $res->fetchAssoc('name,title'));
}

View File

@@ -1,70 +0,0 @@
<?php
use Tester\Assert;
require __DIR__ . '/bootstrap.php';
class TestClass extends DibiObject
{
public function getBar()
{
return 123;
}
public function isFoo()
{
return 456;
}
}
// calling
Assert::exception(function () {
$obj = new TestClass;
$obj->undeclared();
}, 'LogicException', 'Call to undefined method TestClass::undeclared().');
Assert::exception(function () {
TestClass::undeclared();
}, 'LogicException', 'Call to undefined static method TestClass::undeclared().');
// writing
Assert::exception(function () {
$obj = new TestClass;
$obj->undeclared = 'value';
}, 'LogicException', 'Cannot assign to an undeclared property TestClass::$undeclared.');
// property getter
$obj = new TestClass;
Assert::true(isset($obj->bar));
Assert::same(123, $obj->bar);
Assert::false(isset($obj->foo));
Assert::same(456, $obj->foo);
// reading
Assert::exception(function () {
$obj = new TestClass;
$val = $obj->undeclared;
}, 'LogicException', 'Cannot read an undeclared property TestClass::$undeclared.');
// unset/isset
Assert::exception(function () {
$obj = new TestClass;
unset($obj->undeclared);
}, 'LogicException', 'Cannot unset the property TestClass::$undeclared.');
Assert::false(isset($obj->undeclared));
// extension method
TestClass::extensionMethod('join', $func = function (TestClass $that, $separator) {
return $that->foo . $separator . $that->bar;
});
$obj = new TestClass;
Assert::same('456*123', $obj->join('*'));

View File

@@ -1,57 +0,0 @@
<?php
/**
* @dataProvider ../databases.ini
*/
use Tester\Assert;
require __DIR__ . '/bootstrap.php';
if ($config['system'] === 'odbc') {
Tester\Environment::skip('Not supported.');
}
$conn = new DibiConnection($config);
$conn->loadFile(__DIR__ . "/data/$config[system].sql");
$info = $conn->query('
SELECT products.product_id, orders.order_id, customers.name, products.product_id + 1 AS xxx
FROM products
INNER JOIN orders USING (product_id)
INNER JOIN customers USING (customer_id)
')->getInfo();
Assert::same(
array('product_id', 'order_id', 'name', 'xxx'),
$info->getColumnNames()
);
if ($config['driver'] !== 'sqlite3' && $config['driver'] !== 'pdo') {
Assert::same(
array('products.product_id', 'orders.order_id', 'customers.name', 'xxx'),
$info->getColumnNames(TRUE)
);
}
$columns = $info->getColumns();
Assert::same('product_id', $columns[0]->name);
if ($config['driver'] !== 'sqlite3' && $config['driver'] !== 'pdo') {
Assert::same('products', $columns[0]->tableName);
}
Assert::null($columns[0]->getVendorInfo('xxx'));
if ($config['system'] !== 'sqlite') {
Assert::same('i', $columns[0]->type);
}
Assert::null($columns[0]->nullable);
Assert::same('xxx', $columns[3]->name);
Assert::null($columns[3]->tableName);
if ($config['system'] !== 'sqlite') {
Assert::same('i', $columns[0]->type);
}
Assert::null($columns[3]->nullable);

View File

@@ -1,13 +1,14 @@
<?php <?php
use Tester\Assert; use Tester\Assert;
use Dibi\Fluent;
require __DIR__ . '/bootstrap.php'; require __DIR__ . '/bootstrap.php';
$conn = new DibiConnection($config); $conn = new Dibi\Connection($config);
$fluent = new DibiFluent($conn); $fluent = new Fluent($conn);
$fluent->select('*')->from('table')->where('x=1'); $fluent->select('*')->from('table')->where('x=1');
$dolly = clone $fluent; $dolly = clone $fluent;
$dolly->where('y=1'); $dolly->where('y=1');
@@ -17,7 +18,7 @@ Assert::same(reformat('SELECT * FROM [table] WHERE x=1'), (string) $fluent);
Assert::same(reformat('SELECT * FROM [table] WHERE x=1 AND y=1 FOO'), (string) $dolly); Assert::same(reformat('SELECT * FROM [table] WHERE x=1 AND y=1 FOO'), (string) $dolly);
$fluent = new DibiFluent($conn); $fluent = new Fluent($conn);
$fluent->select('id')->from('table')->where('id = %i', 1); $fluent->select('id')->from('table')->where('id = %i', 1);
$dolly = clone $fluent; $dolly = clone $fluent;
$dolly->where('cd = %i', 5); $dolly->where('cd = %i', 5);
@@ -26,7 +27,7 @@ Assert::same(reformat('SELECT [id] FROM [table] WHERE id = 1'), (string) $fluent
Assert::same(reformat('SELECT [id] FROM [table] WHERE id = 1 AND cd = 5'), (string) $dolly); Assert::same(reformat('SELECT [id] FROM [table] WHERE id = 1 AND cd = 5'), (string) $dolly);
$fluent = new DibiFluent($conn); $fluent = new Fluent($conn);
$fluent->select('*')->from('table'); $fluent->select('*')->from('table');
$dolly = clone $fluent; $dolly = clone $fluent;
$dolly->removeClause('select')->select('count(*)'); $dolly->removeClause('select')->select('count(*)');

View File

@@ -5,7 +5,7 @@ use Tester\Assert;
require __DIR__ . '/bootstrap.php'; require __DIR__ . '/bootstrap.php';
$conn = new DibiConnection($config); $conn = new Dibi\Connection($config);
$fluent = $conn->delete('table')->as('bAlias') $fluent = $conn->delete('table')->as('bAlias')

View File

@@ -0,0 +1,101 @@
<?php
use Tester\Assert;
require __DIR__ . '/bootstrap.php';
class MockDriver extends Dibi\Drivers\SqlsrvDriver
{
function __construct()
{}
function connect(array & $config)
{}
function query($sql)
{
return $this;
}
function getResultColumns()
{
return [];
}
function fetch($assoc)
{
return FALSE;
}
}
$config['driver'] = new MockDriver;
$conn = new Dibi\Connection($config);
// fetch & limit
$fluent = $conn->select('*')
->from('customers')
->limit(1)
->orderBy('customer_id');
Assert::same(
reformat('SELECT TOP (1) * FROM ( SELECT * FROM [customers] ORDER BY [customer_id]) t'),
(string) $fluent
);
$fluent->fetch();
Assert::same(
'SELECT TOP (1) * FROM ( SELECT * FROM [customers] ORDER BY [customer_id]) t',
dibi::$sql
);
$fluent->fetchSingle();
Assert::same(
reformat('SELECT TOP (1) * FROM ( SELECT * FROM [customers] ORDER BY [customer_id]) t'),
dibi::$sql
);
$fluent->fetchAll(0, 3);
Assert::same(
reformat('SELECT TOP (3) * FROM ( SELECT * FROM [customers] ORDER BY [customer_id]) t'),
dibi::$sql
);
Assert::same(
reformat('SELECT TOP (1) * FROM ( SELECT * FROM [customers] ORDER BY [customer_id]) t'),
(string) $fluent
);
$fluent->limit(0);
$fluent->fetch();
Assert::same(
reformat('SELECT TOP (0) * FROM ( SELECT * FROM [customers] ORDER BY [customer_id]) t'),
dibi::$sql
);
$fluent->fetchSingle();
Assert::same(
reformat('SELECT TOP (0) * FROM ( SELECT * FROM [customers] ORDER BY [customer_id]) t'),
dibi::$sql
);
Assert::same(
reformat('SELECT TOP (0) * FROM ( SELECT * FROM [customers] ORDER BY [customer_id]) t'),
(string) $fluent
);
$fluent->removeClause('limit');
$fluent->removeClause('offset');
$fluent->fetch();
Assert::same(
reformat('SELECT TOP (1) * FROM ( SELECT * FROM [customers] ORDER BY [customer_id]) t'),
dibi::$sql
);
$fluent->fetchSingle();
Assert::same(
reformat('SELECT TOP (1) * FROM ( SELECT * FROM [customers] ORDER BY [customer_id]) t'),
dibi::$sql
);
Assert::same(
reformat('SELECT * FROM [customers] ORDER BY [customer_id]'),
(string) $fluent
);

View File

@@ -0,0 +1,97 @@
<?php
/**
* @dataProvider? ../databases.ini mysql
*/
use Tester\Assert;
require __DIR__ . '/bootstrap.php';
$conn = new Dibi\Connection($config);
$conn->loadFile(__DIR__ . "/data/$config[system].sql");
// fetch & limit
$fluent = $conn->select('*')
->from('customers')
->limit(1)
->offset(3)
->orderBy('customer_id');
Assert::same(
reformat(' SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 1 OFFSET 3'),
(string) $fluent
);
$fluent->fetch();
Assert::same(
reformat(' SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 1 OFFSET 3'),
dibi::$sql
);
$fluent->fetchSingle();
Assert::same(
reformat(' SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 1 OFFSET 3'),
dibi::$sql
);
$fluent->fetchAll(2, 3);
Assert::same(
reformat(' SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 3 OFFSET 2'),
dibi::$sql
);
Assert::same(
reformat(' SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 1 OFFSET 3'),
(string) $fluent
);
$fluent->limit(0);
$fluent->fetch();
Assert::same(
reformat(' SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 0 OFFSET 3'),
dibi::$sql
);
$fluent->fetchSingle();
Assert::same(
reformat(' SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 0 OFFSET 3'),
dibi::$sql
);
Assert::same(
reformat(' SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 0 OFFSET 3'),
(string) $fluent
);
$fluent->removeClause('limit');
$fluent->fetch();
Assert::same(
reformat(' SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 1 OFFSET 3'),
dibi::$sql
);
$fluent->fetchSingle();
Assert::same(
reformat(' SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 1 OFFSET 3'),
dibi::$sql
);
Assert::same(
reformat(' SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 18446744073709551615 OFFSET 3'),
(string) $fluent
);
$fluent->removeClause('offset');
$fluent->fetch();
Assert::same(
reformat(' SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 1'),
dibi::$sql
);
$fluent->fetchSingle();
Assert::same(
reformat(' SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 1'),
dibi::$sql
);
Assert::same(
reformat('SELECT * FROM [customers] ORDER BY [customer_id]'),
(string) $fluent
);

View File

@@ -0,0 +1,50 @@
<?php
/**
* @dataProvider ../databases.ini
*/
use Tester\Assert;
use Dibi\Row;
require __DIR__ . '/bootstrap.php';
$conn = new Dibi\Connection($config);
$conn->loadFile(__DIR__ . "/data/$config[system].sql");
// fetch a single value
$res = $conn->select('title')->from('products')->orderBy('product_id');
Assert::equal('Chair', $res->fetchSingle());
// fetch complete result set
$res = $conn->select('*')->from('products')->orderBy('product_id');
Assert::equal([
new Row(['product_id' => num(1), 'title' => 'Chair']),
new Row(['product_id' => num(2), 'title' => 'Table']),
new Row(['product_id' => num(3), 'title' => 'Computer']),
], $res->fetchAll());
// more complex association array
if (!in_array($config['system'], ['odbc', 'sqlsrv'])) {
$res = $conn->select(['products.title' => 'title', 'customers.name' => 'name'])->select('orders.amount')->as('amount')
->from('products')
->innerJoin('orders')->using('(product_id)')
->innerJoin('customers')->using('([customer_id])')
->orderBy('order_id');
Assert::equal([
'Arnold Rimmer' => [
'Chair' => new Row(['title' => 'Chair', 'name' => 'Arnold Rimmer', 'amount' => num(7.0)]),
'Computer' => new Row(['title' => 'Computer', 'name' => 'Arnold Rimmer', 'amount' => num(2.0)]),
],
'Dave Lister' => [
'Table' => new Row(['title' => 'Table', 'name' => 'Dave Lister', 'amount' => num(3.0)]),
],
'Kristine Kochanski' => [
'Computer' => new Row(['title' => 'Computer', 'name' => 'Kristine Kochanski', 'amount' => num(5.0)]),
],
], $res->fetchAssoc('name,title'));
}

View File

@@ -5,14 +5,14 @@ use Tester\Assert;
require __DIR__ . '/bootstrap.php'; require __DIR__ . '/bootstrap.php';
$conn = new DibiConnection($config); $conn = new Dibi\Connection($config);
$arr = array( $arr = [
'title' => 'Super Product', 'title' => 'Super Product',
'price' => 12, 'price' => 12,
'brand' => NULL, 'brand' => NULL,
); ];
$fluent = $conn->insert('table', $arr) $fluent = $conn->insert('table', $arr)
->setFlag('IGNORE')->setFlag('DELAYED'); ->setFlag('IGNORE')->setFlag('DELAYED');

View File

@@ -1,11 +1,15 @@
<?php <?php
/**
* @dataProvider ../databases.ini
*/
use Tester\Assert; use Tester\Assert;
require __DIR__ . '/bootstrap.php'; require __DIR__ . '/bootstrap.php';
$conn = new DibiConnection($config); $conn = new Dibi\Connection($config);
$max = 10; $max = 10;
@@ -14,7 +18,7 @@ $min = 5;
$fluent = $conn->select('*') $fluent = $conn->select('*')
->select('a') ->select('a')
->select('b')->as('bAlias') ->select('b')->as('bAlias')
->select(array('c', 'd', 'e')) ->select(['c', 'd', 'e'])
->select('%n', 'd'); ->select('%n', 'd');
Assert::same( Assert::same(
@@ -58,17 +62,17 @@ Assert::same(
$fluent->where('col > %i', $max) $fluent->where('col > %i', $max)
->or('col < %i', $min) ->or('col < %i', $min)
->where('active = 1') ->where('active = 1')
->where('col')->in(array(1, 2, 3)) ->where('col')->in([1, 2, 3])
->orderBy('val')->asc() ->orderBy('val')->asc()
->orderBy('[val2] DESC') ->orderBy('[val2] DESC')
->orderBy(array('val3' => -1)); ->orderBy(['val3' => -1]);
Assert::same( Assert::same(
reformat('SELECT * , [a] , [b] AS [bAlias] , [c], [d], [e] , [d] FROM [anotherTable] AS [anotherAlias] INNER JOIN [table3] ON table.col = table3.col WHERE col > 10 OR col < 5 AND active = 1 AND [col] IN (1, 2, 3) ORDER BY [val] ASC , [val2] DESC , [val3] DESC'), reformat('SELECT * , [a] , [b] AS [bAlias] , [c], [d], [e] , [d] FROM [anotherTable] AS [anotherAlias] INNER JOIN [table3] ON table.col = table3.col WHERE col > 10 OR col < 5 AND active = 1 AND [col] IN (1, 2, 3) ORDER BY [val] ASC , [val2] DESC , [val3] DESC'),
(string) $fluent (string) $fluent
); );
$fluent->orderBy(DibiFluent::REMOVE); $fluent->orderBy(Dibi\Fluent::REMOVE);
Assert::same( Assert::same(
reformat('SELECT * , [a] , [b] AS [bAlias] , [c], [d], [e] , [d] FROM [anotherTable] AS [anotherAlias] INNER JOIN [table3] ON table.col = table3.col WHERE col > 10 OR col < 5 AND active = 1 AND [col] IN (1, 2, 3)'), reformat('SELECT * , [a] , [b] AS [bAlias] , [c], [d], [e] , [d] FROM [anotherTable] AS [anotherAlias] INNER JOIN [table3] ON table.col = table3.col WHERE col > 10 OR col < 5 AND active = 1 AND [col] IN (1, 2, 3)'),
@@ -76,16 +80,6 @@ Assert::same(
); );
try {
$fluent = $conn->select('*')->from('table')->fetch();
} catch (Exception $e) {
}
Assert::same(
reformat(' SELECT * FROM [table] LIMIT 1'),
dibi::$sql
);
$fluent = $conn->select('*') $fluent = $conn->select('*')
->select( ->select(
$conn->select('count(*)') $conn->select('count(*)')
@@ -98,17 +92,21 @@ $fluent = $conn->select('*')
->offset(0); ->offset(0);
Assert::same( Assert::same(
reformat('SELECT * , (SELECT count(*) FROM [precteni] AS [P] WHERE P.id_clanku = C.id_clanku) FROM [clanky] AS [C] WHERE id_clanku=123 LIMIT 1 OFFSET 0'), reformat([
'odbc' => 'SELECT TOP 1 * FROM ( SELECT * , (SELECT count(*) FROM [precteni] AS [P] WHERE P.id_clanku = C.id_clanku) FROM [clanky] AS [C] WHERE id_clanku=123) t',
'sqlsrv' => ' SELECT * , (SELECT count(*) FROM [precteni] AS [P] WHERE P.id_clanku = C.id_clanku) FROM [clanky] AS [C] WHERE id_clanku=123 OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY',
' SELECT * , (SELECT count(*) FROM [precteni] AS [P] WHERE P.id_clanku = C.id_clanku) FROM [clanky] AS [C] WHERE id_clanku=123 LIMIT 1',
]),
(string) $fluent (string) $fluent
); );
$fluent = $conn->select('*') $fluent = $conn->select('*')
->select(array('x' => 'xAlias')) ->select(['x' => 'xAlias'])
->from('products') ->from('products')
->innerJoin('orders')->using('(product_id)') ->innerJoin('orders')->using('(product_id)')
->innerJoin('customers')->using('([customer_id])') ->innerJoin('customers')->using('([customer_id])')
->innerJoin('items')->using('(%n)', array('customer_id', 'order_id')); ->innerJoin('items')->using('(%n)', ['customer_id', 'order_id']);
Assert::same( Assert::same(
reformat('SELECT * , [x] AS [xAlias] FROM [products] INNER JOIN [orders] USING (product_id) INNER JOIN [customers] USING ([customer_id]) INNER JOIN [items] USING ([customer_id], [order_id])'), reformat('SELECT * , [x] AS [xAlias] FROM [products] INNER JOIN [orders] USING (product_id) INNER JOIN [customers] USING ([customer_id]) INNER JOIN [items] USING ([customer_id], [order_id])'),
@@ -129,11 +127,32 @@ Assert::same(
$fluent = $conn->select('*') $fluent = $conn->select('*')
->from(array('me' => 't')) ->from(['me' => 't'])
->where('col > %i', $max) ->where('col > %i', $max)
->where(array('x' => 'a', 'b', 'c')); ->where(['x' => 'a', 'b', 'c']);
Assert::same( Assert::same(
reformat('SELECT * FROM [me] AS [t] WHERE col > 10 AND ([x] = \'a\') AND (b) AND (c)'), reformat('SELECT * FROM [me] AS [t] WHERE col > 10 AND ([x] = \'a\') AND (b) AND (c)'),
(string) $fluent (string) $fluent
); );
if ($config['system'] === 'mysql') {
$fluent = $conn->select('*')
->limit(' 1; DROP TABLE users')
->offset(' 1; DROP TABLE users');
Assert::same(
reformat(' SELECT * LIMIT 1 OFFSET 1'),
(string) $fluent
);
}
$fluent = $conn->select('*')->from('abc')
->where('x IN (%SQL)', $conn->select('id')->from('xyz'));
Assert::same(
reformat('SELECT * FROM [abc] WHERE x IN ((SELECT [id] FROM [xyz]))'),
(string) $fluent
);

View File

@@ -5,14 +5,14 @@ use Tester\Assert;
require __DIR__ . '/bootstrap.php'; require __DIR__ . '/bootstrap.php';
$conn = new DibiConnection($config); $conn = new Dibi\Connection($config);
$arr = array( $arr = [
'title' => 'Super Product', 'title' => 'Super Product',
'price' => 12, 'price' => 12,
'brand' => NULL, 'brand' => NULL,
); ];
$fluent = $conn->update('table', $arr) $fluent = $conn->update('table', $arr)
->setFlag('IGNORE')->setFlag('DELAYED'); ->setFlag('IGNORE')->setFlag('DELAYED');
@@ -22,7 +22,7 @@ Assert::same(
(string) $fluent (string) $fluent
); );
$fluent->set(array('another' => 123)); $fluent->set(['another' => 123]);
Assert::same( Assert::same(
reformat('UPDATE IGNORE DELAYED [table] SET [title]=\'Super Product\', [price]=12, [brand]=NULL , [another]=123'), reformat('UPDATE IGNORE DELAYED [table] SET [title]=\'Super Product\', [price]=12, [brand]=NULL , [another]=123'),

View File

@@ -0,0 +1,36 @@
<?php
use Dibi\Helpers;
use Tester\Assert;
require __DIR__ . '/bootstrap.php';
Assert::same(NULL, Helpers::getSuggestion([], ''));
Assert::same(NULL, Helpers::getSuggestion([], 'a'));
Assert::same(NULL, Helpers::getSuggestion(['a'], 'a'));
Assert::same('a', Helpers::getSuggestion(['a', 'b'], ''));
Assert::same('b', Helpers::getSuggestion(['a', 'b'], 'a')); // ignore 100% match
Assert::same('a1', Helpers::getSuggestion(['a1', 'a2'], 'a')); // take first
Assert::same(NULL, Helpers::getSuggestion(['aaa', 'bbb'], 'a'));
Assert::same(NULL, Helpers::getSuggestion(['aaa', 'bbb'], 'ab'));
Assert::same(NULL, Helpers::getSuggestion(['aaa', 'bbb'], 'abc'));
Assert::same('bar', Helpers::getSuggestion(['foo', 'bar', 'baz'], 'baz'));
Assert::same('abcd', Helpers::getSuggestion(['abcd'], 'acbd'));
Assert::same('abcd', Helpers::getSuggestion(['abcd'], 'axbd'));
Assert::same(NULL, Helpers::getSuggestion(['abcd'], 'axyd'));
/*
length allowed ins/del replacements
-------------------------------------
0 1 0
1 1 1
2 1 1
3 1 1
4 2 1
5 2 2
6 2 2
7 2 2
8 3 2
*/

View File

@@ -1,80 +0,0 @@
<?php
/**
* @dataProvider ../databases.ini
*/
use Tester\Assert;
require __DIR__ . '/bootstrap.php';
if ($config['system'] !== 'mssql' || $config['driver'] !== 'pdo') {
Tester\Environment::skip("Not supported system '$config[system]'.");
}
$tests = function ($conn) {
$version = $conn->getDriver()->getResource()->getAttribute(PDO::ATTR_SERVER_VERSION);
// MsSQL2012+
if (version_compare($version, '11.0') >= 0) {
// Limit and offset
Assert::same(
'SELECT 1 OFFSET 10 ROWS FETCH NEXT 10 ROWS ONLY',
$conn->translate('SELECT 1 %ofs %lmt', 10, 10)
);
// Limit only
Assert::same(
'SELECT 1 OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY',
$conn->translate('SELECT 1 %lmt', 10)
);
// Offset only
Assert::same(
'SELECT 1 OFFSET 10 ROWS',
$conn->translate('SELECT 1 %ofs', 10)
);
// Offset invalid
Assert::same(
'SELECT 1',
$conn->translate('SELECT 1 %ofs', -10)
);
// Limit invalid
Assert::same(
'SELECT 1',
$conn->translate('SELECT 1 %lmt', -10)
);
// Limit invalid, offset valid
Assert::same(
'SELECT 1',
$conn->translate('SELECT 1 %ofs %lmt', 10, -10)
);
// Limit valid, offset invalid
Assert::same(
'SELECT 1',
$conn->translate('SELECT 1 %ofs %lmt', -10, 10)
);
} else {
Assert::same(
'SELECT TOP 1 * FROM (SELECT 1) t',
$conn->translate('SELECT 1 %lmt', 1)
);
Assert::same(
'SELECT 1',
$conn->translate('SELECT 1 %lmt', -10)
);
Assert::exception(
$conn->translate('SELECT 1 %ofs %lmt', 10, 10),
'DibiNotSupportedException'
);
}
};
$conn = new DibiConnection($config);
$tests($conn);

View File

@@ -1,17 +1,13 @@
<?php <?php
/** /**
* @dataProvider ../databases.ini * @dataProvider? ../databases.ini postgre
*/ */
use Tester\Assert; use Tester\Assert;
require __DIR__ . '/bootstrap.php'; require __DIR__ . '/bootstrap.php';
if ($config['system'] !== 'pgsql') {
Tester\Environment::skip("Not supported system '$config[system]'.");
}
$tests = function ($conn) { $tests = function ($conn) {
Assert::false($conn->query("SELECT 'AAxBB' LIKE %~like~", 'A_B')->fetchSingle()); Assert::false($conn->query("SELECT 'AAxBB' LIKE %~like~", 'A_B')->fetchSingle());
@@ -25,7 +21,7 @@ $tests = function ($conn) {
Assert::true($conn->query("SELECT 'AA\\BB' LIKE %~like~", 'A\\B')->fetchSingle()); Assert::true($conn->query("SELECT 'AA\\BB' LIKE %~like~", 'A\\B')->fetchSingle());
}; };
$conn = new DibiConnection($config); $conn = new Dibi\Connection($config);
$conn->query('SET escape_string_warning = off'); // do not log warnings $conn->query('SET escape_string_warning = off'); // do not log warnings
$conn->query('SET standard_conforming_strings = on'); $conn->query('SET standard_conforming_strings = on');

Some files were not shown because too many files have changed in this diff Show More