mirror of
https://github.com/dg/dibi.git
synced 2025-08-30 09:19:48 +02:00
Compare commits
1 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
3383676830 |
10
.gitattributes
vendored
10
.gitattributes
vendored
@@ -1,10 +0,0 @@
|
||||
.gitattributes export-ignore
|
||||
.gitignore export-ignore
|
||||
.github export-ignore
|
||||
.travis.yml export-ignore
|
||||
ecs.php export-ignore
|
||||
phpstan.neon export-ignore
|
||||
tests/ export-ignore
|
||||
|
||||
*.sh eol=lf
|
||||
*.php* diff=php linguist-language=PHP
|
19
.github/ISSUE_TEMPLATE/Bug_report.md
vendored
19
.github/ISSUE_TEMPLATE/Bug_report.md
vendored
@@ -1,19 +0,0 @@
|
||||
---
|
||||
name: "\U0001F41B Bug Report"
|
||||
about: "If something isn't working as expected \U0001F914"
|
||||
|
||||
---
|
||||
|
||||
Version: ?.?.?
|
||||
|
||||
### Bug Description
|
||||
... A clear and concise description of what the bug is. A good bug report shouldn't leave others needing to chase you up for more information.
|
||||
|
||||
### Steps To Reproduce
|
||||
... If possible a minimal demo of the problem ...
|
||||
|
||||
### Expected Behavior
|
||||
... A clear and concise description of what you expected to happen.
|
||||
|
||||
### Possible Solution
|
||||
... Only if you have suggestions on a fix for the bug
|
9
.github/ISSUE_TEMPLATE/Feature_request.md
vendored
9
.github/ISSUE_TEMPLATE/Feature_request.md
vendored
@@ -1,9 +0,0 @@
|
||||
---
|
||||
name: "\U0001F680 Feature Request"
|
||||
about: "I have a suggestion (and may want to implement it \U0001F642)"
|
||||
|
||||
---
|
||||
|
||||
- Is your feature request related to a problem? Please describe.
|
||||
- Explain your intentions.
|
||||
- It's up to you to make a strong case to convince the project's developers of the merits of this feature.
|
17
.github/ISSUE_TEMPLATE/Support_us.md
vendored
17
.github/ISSUE_TEMPLATE/Support_us.md
vendored
@@ -1,17 +0,0 @@
|
||||
---
|
||||
name: "❤️ Support us"
|
||||
about: "If you would like to support our efforts in maintaining this project 🙌"
|
||||
|
||||
---
|
||||
|
||||
--------------^ Click "Preview" for a nicer view!
|
||||
|
||||
Help support Dibi!
|
||||
|
||||
We develop Dibi for more than 14 years. In order to make your life more comfortable. Dibi cares about the safety of your sites. Dibi saves you time. Dibi earns you money. And is absolutely free.
|
||||
|
||||
To ensure future development and improving the documentation, we need your donation.
|
||||
|
||||
**[Please make a donation now](https://nette.org/make-donation?to=dibi)**.
|
||||
|
||||
Thank you!
|
1
.github/funding.yml
vendored
1
.github/funding.yml
vendored
@@ -1 +0,0 @@
|
||||
github: dg
|
10
.github/pull_request_template.md
vendored
10
.github/pull_request_template.md
vendored
@@ -1,10 +0,0 @@
|
||||
- bug fix / new feature? <!-- #issue numbers, if any -->
|
||||
- BC break? yes/no
|
||||
|
||||
<!--
|
||||
Describe your changes here to communicate to the maintainers why we should accept this pull request.
|
||||
|
||||
Please add new tests to show the fix or feature works.
|
||||
|
||||
Thanks for contributing!
|
||||
-->
|
31
.github/workflows/coding-style.yml
vendored
31
.github/workflows/coding-style.yml
vendored
@@ -1,31 +0,0 @@
|
||||
name: Coding Style
|
||||
|
||||
on: [push, pull_request]
|
||||
|
||||
jobs:
|
||||
nette_cc:
|
||||
name: Nette Code Checker
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: shivammathur/setup-php@v2
|
||||
with:
|
||||
php-version: 8.0
|
||||
coverage: none
|
||||
|
||||
- run: composer create-project nette/code-checker temp/code-checker ^3 --no-progress
|
||||
- run: php temp/code-checker/code-checker --strict-types --no-progress --ignore "tests/*/fixtures"
|
||||
|
||||
|
||||
nette_cs:
|
||||
name: Nette Coding Standard
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: shivammathur/setup-php@v2
|
||||
with:
|
||||
php-version: 8.0
|
||||
coverage: none
|
||||
|
||||
- run: composer create-project nette/coding-standard temp/coding-standard ^3 --no-progress --ignore-platform-reqs
|
||||
- run: php temp/coding-standard/ecs check
|
21
.github/workflows/static-analysis.yml
vendored
21
.github/workflows/static-analysis.yml
vendored
@@ -1,21 +0,0 @@
|
||||
name: Static Analysis (only informative)
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- master
|
||||
|
||||
jobs:
|
||||
phpstan:
|
||||
name: PHPStan
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: shivammathur/setup-php@v2
|
||||
with:
|
||||
php-version: 8.0
|
||||
coverage: none
|
||||
|
||||
- run: composer install --no-progress --prefer-dist
|
||||
- run: composer phpstan -- --no-progress
|
||||
continue-on-error: true # is only informative
|
121
.github/workflows/tests.yml
vendored
121
.github/workflows/tests.yml
vendored
@@ -1,121 +0,0 @@
|
||||
name: Tests
|
||||
|
||||
on: [push, pull_request]
|
||||
|
||||
env:
|
||||
php-extensions: mbstring, intl, mysqli, pgsql, sqlsrv-5.10.0beta2, pdo_sqlsrv-5.10.0beta2
|
||||
php-tools: "composer:v2, pecl"
|
||||
|
||||
jobs:
|
||||
tests:
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
matrix:
|
||||
php: ['7.2', '7.3', '7.4', '8.0', '8.1']
|
||||
|
||||
fail-fast: false
|
||||
|
||||
name: PHP ${{ matrix.php }} tests
|
||||
|
||||
services:
|
||||
mysql57:
|
||||
image: mysql:5.7
|
||||
env:
|
||||
MYSQL_DATABASE: dibi_test
|
||||
MYSQL_ROOT_PASSWORD: root
|
||||
ports:
|
||||
- 3306:3306
|
||||
options: >-
|
||||
--health-cmd "mysqladmin ping -ppass"
|
||||
--health-interval 10s
|
||||
--health-start-period 10s
|
||||
--health-timeout 5s
|
||||
--health-retries 10
|
||||
|
||||
mysql80:
|
||||
image: mysql:8.0
|
||||
ports:
|
||||
- 3307:3306
|
||||
options: >-
|
||||
--health-cmd="mysqladmin ping -ppass"
|
||||
--health-interval=10s
|
||||
--health-timeout=5s
|
||||
--health-retries=5
|
||||
-e MYSQL_ROOT_PASSWORD=root
|
||||
-e MYSQL_DATABASE=dibi_test
|
||||
--entrypoint sh mysql:8 -c "exec docker-entrypoint.sh mysqld --default-authentication-plugin=mysql_native_password"
|
||||
|
||||
postgres96:
|
||||
image: postgres:9.6
|
||||
env:
|
||||
POSTGRES_USER: postgres
|
||||
POSTGRES_PASSWORD: postgres
|
||||
POSTGRES_DB: dibi_test
|
||||
ports:
|
||||
- 5432:5432
|
||||
options: >-
|
||||
--health-cmd pg_isready
|
||||
--health-interval 10s
|
||||
--health-timeout 5s
|
||||
--health-retries 5
|
||||
|
||||
postgres13:
|
||||
image: postgres:13
|
||||
env:
|
||||
POSTGRES_USER: postgres
|
||||
POSTGRES_PASSWORD: postgres
|
||||
POSTGRES_DB: dibi_test
|
||||
ports:
|
||||
- 5433:5432
|
||||
options: >-
|
||||
--health-cmd pg_isready
|
||||
--health-interval 10s
|
||||
--health-timeout 5s
|
||||
--health-retries 5
|
||||
|
||||
mssql:
|
||||
image: mcr.microsoft.com/mssql/server:latest
|
||||
env:
|
||||
ACCEPT_EULA: Y
|
||||
SA_PASSWORD: YourStrong!Passw0rd
|
||||
MSSQL_PID: Developer
|
||||
ports:
|
||||
- 1433:1433
|
||||
options: >-
|
||||
--name=mssql
|
||||
--health-cmd "/opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P 'YourStrong!Passw0rd' -Q 'SELECT 1'"
|
||||
--health-interval 10s
|
||||
--health-timeout 5s
|
||||
--health-retries 5
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: shivammathur/setup-php@v2
|
||||
with:
|
||||
php-version: ${{ matrix.php }}
|
||||
extensions: ${{ env.php-extensions }}
|
||||
tools: ${{ env.php-tools }}
|
||||
coverage: none
|
||||
|
||||
- name: Create databases.ini
|
||||
run: cp ./tests/databases.github.ini ./tests/databases.ini
|
||||
|
||||
- name: Create MS SQL Database
|
||||
run: docker exec -i mssql /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P 'YourStrong!Passw0rd' -Q 'CREATE DATABASE dibi_test'
|
||||
|
||||
- run: composer install --no-progress --prefer-dist
|
||||
- run: vendor/bin/tester -p phpdbg tests -s -C --coverage ./coverage.xml --coverage-src ./src
|
||||
- if: failure()
|
||||
uses: actions/upload-artifact@v2
|
||||
with:
|
||||
name: output
|
||||
path: tests/**/output
|
||||
|
||||
|
||||
- name: Save Code Coverage
|
||||
if: ${{ matrix.php == '8.0' }}
|
||||
env:
|
||||
COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
run: |
|
||||
wget https://github.com/php-coveralls/php-coveralls/releases/download/v2.4.3/php-coveralls.phar
|
||||
php php-coveralls.phar --verbose --config tests/.coveralls.yml
|
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,2 +0,0 @@
|
||||
/vendor
|
||||
/composer.lock
|
48
appveyor.yml
48
appveyor.yml
@@ -1,48 +0,0 @@
|
||||
build: off
|
||||
cache:
|
||||
- c:\php7 -> appveyor.yml
|
||||
- '%LOCALAPPDATA%\Composer\files -> appveyor.yml'
|
||||
|
||||
clone_folder: c:\projects\dibi
|
||||
|
||||
services:
|
||||
- mssql2012sp1
|
||||
# - mssql2014
|
||||
- mysql
|
||||
|
||||
init:
|
||||
- SET PATH=c:\php7;%PATH%
|
||||
- SET ANSICON=121x90 (121x90)
|
||||
|
||||
install:
|
||||
# Install PHP 7.2
|
||||
- IF EXIST c:\php7 (SET PHP=0) ELSE (SET PHP=1)
|
||||
- IF %PHP%==1 mkdir c:\php7
|
||||
- IF %PHP%==1 cd c:\php7
|
||||
- IF %PHP%==1 curl https://windows.php.net/downloads/releases/archives/php-7.2.18-Win32-VC15-x64.zip --output php.zip
|
||||
- IF %PHP%==1 7z x php.zip >nul
|
||||
- IF %PHP%==1 echo extension_dir=ext >> php.ini
|
||||
- IF %PHP%==1 echo extension=php_openssl.dll >> php.ini
|
||||
- IF %PHP%==1 curl https://github.com/microsoft/msphpsql/releases/download/v5.8.0/Windows-7.2.zip -L --output sqlsrv.zip
|
||||
- IF %PHP%==1 7z x sqlsrv.zip >nul
|
||||
- IF %PHP%==1 copy Windows-7.2\x64\php_sqlsrv_72_ts.dll ext\php_sqlsrv_ts.dll
|
||||
- IF %PHP%==1 del /Q *.zip
|
||||
|
||||
# Install Microsoft Access Database Engine x64
|
||||
- IF %PHP%==1 curl https://download.microsoft.com/download/2/4/3/24375141-E08D-4803-AB0E-10F2E3A07AAA/AccessDatabaseEngine_X64.exe --output AccessDatabaseEngine_X64.exe
|
||||
- cmd /c start /wait AccessDatabaseEngine_X64.exe /passive
|
||||
|
||||
# Install Nette Tester
|
||||
- cd c:\projects\dibi
|
||||
- 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 c:\php7\php -c tests\php-win.ini
|
||||
|
||||
on_failure:
|
||||
# Print *.actual content
|
||||
- for /r %%x in (*.actual) do ( type "%%x" )
|
@@ -1,37 +0,0 @@
|
||||
{
|
||||
"name": "dibi/dibi",
|
||||
"description": "Dibi is Database Abstraction Library for PHP",
|
||||
"keywords": ["database", "dbal", "mysql", "postgresql", "sqlite", "mssql", "sqlsrv", "oracle", "access", "pdo", "odbc"],
|
||||
"homepage": "https://dibiphp.com",
|
||||
"license": ["BSD-3-Clause", "GPL-2.0-only", "GPL-3.0-only"],
|
||||
"authors": [
|
||||
{
|
||||
"name": "David Grudl",
|
||||
"homepage": "https://davidgrudl.com"
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"php": ">=7.2"
|
||||
},
|
||||
"require-dev": {
|
||||
"tracy/tracy": "~2.2",
|
||||
"nette/tester": "~2.0",
|
||||
"nette/di": "^3.0",
|
||||
"phpstan/phpstan": "^0.12"
|
||||
},
|
||||
"replace": {
|
||||
"dg/dibi": "*"
|
||||
},
|
||||
"autoload": {
|
||||
"classmap": ["src/"]
|
||||
},
|
||||
"scripts": {
|
||||
"phpstan": "phpstan analyse",
|
||||
"tester": "tester tests -s"
|
||||
},
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-master": "4.2-dev"
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,31 +0,0 @@
|
||||
How to contribute & use the issue tracker
|
||||
=========================================
|
||||
|
||||
Dibi welcomes your contributions. There are several ways to help out:
|
||||
|
||||
* Create an issue on GitHub, if you have found a bug
|
||||
* Write test cases for open bug issues
|
||||
* Write fixes for open bug/feature issues, preferably with test cases included
|
||||
* Contribute to the documentation
|
||||
|
||||
Issues
|
||||
------
|
||||
|
||||
Please **do not use the issue tracker to ask questions**. We will be happy to help you
|
||||
on [Dibi forum](https://forum.dibiphp.com).
|
||||
|
||||
A good bug report shouldn't leave others needing to chase you up for more
|
||||
information. Please try to be as detailed as possible in your report.
|
||||
|
||||
**Feature requests** are welcome. But take a moment to find out whether your idea
|
||||
fits with the scope and aims of the project. It's up to *you* to make a strong
|
||||
case to convince the project's developers of the merits of this feature.
|
||||
|
||||
Contributing
|
||||
------------
|
||||
|
||||
The best way to propose a feature is to discuss your ideas on [Dibi forum](https://forum.dibiphp.com) before implementing them.
|
||||
|
||||
Please do not fix whitespace, format code, or make a purely cosmetic patch.
|
||||
|
||||
Thanks! :heart:
|
21
copyright.txt
Normal file
21
copyright.txt
Normal file
@@ -0,0 +1,21 @@
|
||||
Copyright notice
|
||||
----------------
|
||||
|
||||
dibi (C) David Grudl, 2005-2006 <dave@dgx.cz>
|
||||
|
||||
For more information, visit the homepage http://texy.info/dibi
|
||||
or author's weblog: http://www.dgx.cz/trine/
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
33
dibi.compact/dibi-compact.php
Normal file
33
dibi.compact/dibi-compact.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
/**
|
||||
* dibi - Database Abstraction Layer according to dgx
|
||||
* --------------------------------------------------
|
||||
*
|
||||
* This source file is subject to the GNU GPL license.
|
||||
*
|
||||
* @author David Grudl aka -dgx- <dave@dgx.cz>
|
||||
* @link http://texy.info/dibi/
|
||||
* @copyright Copyright (c) 2005-2006 David Grudl
|
||||
* @license GNU GENERAL PUBLIC LICENSE
|
||||
* @package dibi
|
||||
* @category Database
|
||||
* @version 0.5b (2006-05-31) for PHP5
|
||||
*/define('dibi','Database Abstraction Layer (c) David Grudl, http://texy.info/dibi/');if(version_compare(PHP_VERSION,'5.0.3','<'))die('dibi needs PHP 5.0.3 or newer');
|
||||
abstract class DibiDriver{protected$config;public$formats=array('NULL'=>"NULL",'TRUE'=>"1",'FALSE'=>"0",'date'=>"'Y-m-d'",'datetime'=>"'Y-m-d H:i:s'",);abstract static public function connect($config);protected function __construct($config){$this->config=$config;}public function getConfig(){return$this->config;}abstract public function query($sql);abstract public function affectedRows();abstract public function insertId();abstract public function begin();abstract public function commit();abstract public function rollback();abstract public function escape($value,$appendQuotes=FALSE);abstract public function quoteName($value);abstract public function getMetaData();}
|
||||
if(!interface_exists('Countable',false)){interface Countable{function count();}}abstract class DibiResult implements IteratorAggregate,Countable{const FIELD_TEXT='s',FIELD_BINARY='b',FIELD_BOOL='l',FIELD_INTEGER='i',FIELD_FLOAT='f',FIELD_DATE='d',FIELD_DATETIME='t',FIELD_UNKNOWN='?',FIELD_COUNTER='c';protected$convert;abstract public function seek($row);abstract public function rowCount();abstract public function getFields();abstract public function getMetaData($field);abstract protected function detectTypes();abstract protected function free();abstract protected function doFetch();final public function fetch(){$rec=$this->doFetch();if(!is_array($rec))return FALSE;if($t=$this->convert){foreach($rec as$key=>$value){if(isset($t[$key]))$rec[$key]=$this->convert($value,$t[$key]);}}return$rec;}final function fetchSingle(){$rec=$this->doFetch();if(!is_array($rec))return FALSE;if($t=$this->convert){$value=reset($rec);$key=key($rec);return isset($t[$key])?$this->convert($value,$t[$key]):$value;}return reset($rec);}final function fetchAll(){@$this->seek(0);$rec=$this->fetch();if(!$rec)return array();$assocBy=func_get_args();$arr=array();if(!$assocBy){$value=count($rec)==1?key($rec):NULL;do{$arr[]=$value===NULL?$rec:$rec[$value];}while($rec=$this->fetch());return$arr;}do{foreach($assocBy as$n=>$assoc){$val[$n]=$rec[$assoc];unset($rec[$assoc]);}foreach($assocBy as$n=>$assoc){if($n==0)$tmp=&$arr[$val[$n]];else$tmp=&$tmp[$assoc][$val[$n]];if($tmp===NULL)$tmp=$rec;}}while($rec=$this->fetch());return$arr;}final function fetchPairs($key,$value){@$this->seek(0);$rec=$this->fetch();if(!$rec)return array();$arr=array();do{$arr[$rec[$key]]=$rec[$value];}while($rec=$this->fetch());return$arr;}public function __destruct(){@$this->free();}public function setType($field,$type=NULL){if($field===TRUE)$this->detectTypes();elseif(is_array($field))$this->convert=$field;else$this->convert[$field]=$type;}public function getType($field){return isset($this->convert[$field])?$this->convert[$field]:NULL;}public function convert($value,$type){if($value===NULL||$value===FALSE)return$value;static$conv=array(self::FIELD_TEXT=>'string',self::FIELD_BINARY=>'string',self::FIELD_BOOL=>'bool',self::FIELD_INTEGER=>'int',self::FIELD_FLOAT=>'float',self::FIELD_COUNTER=>'int',);if(isset($conv[$type])){settype($value,$conv[$type]);return$value;}if($type==self::FIELD_DATE)return new TDate($value);if($type==self::FIELD_DATETIME)return new TDateTime($value);return$value;}public function getIterator($offset=NULL,$count=NULL){return new DibiResultIterator($this,$offset,$count);}public function count(){return$this->rowCount();}}class DibiResultIterator implements Iterator{private$result,$offset,$count,$record,$row;public function __construct(DibiResult$result,$offset=NULL,$count=NULL){$this->result=$result;$this->offset=(int)$offset;$this->count=$count===NULL?2147483647:(int)$count;}public function rewind(){$this->row=0;@$this->result->seek($this->offset);$this->record=$this->result->fetch();}public function key(){return$this->row;}public function current(){return$this->record;}public function next(){$this->record=$this->result->fetch();$this->row++;}public function valid(){return is_array($this->record)&&($this->row<$this->count);}}
|
||||
class DibiParser{private$modifier,$hasError,$driver;public function parse($driver,$args){$sql='';$this->driver=$driver;$this->modifier=0;$this->hasError=false;$command=null;$lastString=null;foreach($args as$index=>$arg){$sql.=' ';if(is_array($arg)){if($this->modifier){$type=$this->modifier;$this->modifier=false;}else{if(is_int(key($arg)))$type='L';else{if(!$command)$command=strtoupper(substr(ltrim($args[0]),0,6));$type=$command=='UPDATE'?'S':'V';}}$vx=$kx=array();switch($type){case'S':foreach($arg as$k=>$v)$vx[]=$this->driver->quoteName($k).'='.$this->formatValue($v);$sql.=implode(', ',$vx);break;case'V':foreach($arg as$k=>$v){$kx[]=$this->driver->quoteName($k);$vx[]=$this->formatValue($v);}$sql.='('.implode(', ',$kx).') VALUES ('.implode(', ',$vx).')';break;case'L':foreach($arg as$k=>$v)$vx[]=$this->formatValue($v);$sql.=implode(', ',$vx);break;case'N':foreach($arg as$v)$vx[]=$this->driver->quoteName($v);$sql.=implode(', ',$vx);break;default:$this->hasError=true;$sql.="**Unknown modifier %$type**";}continue;}if($this->modifier){if($arg instanceof IDibiVariable){$sql.=$arg->toSql($this->driver,$this->modifier);$this->modifier=false;continue;}if(!is_scalar($arg)&&!is_null($arg)){$this->hasError=true;$this->modifier=false;$sql.='**Unexpected '.gettype($arg).'**';continue;}switch($this->modifier){case"s":$sql.=$this->driver->escape($arg,TRUE);break;case'T':$sql.=date($this->driver->formats['date'],is_string($arg)?strtotime($arg):$arg);break;case't':$sql.=date($this->driver->formats['datetime'],is_string($arg)?strtotime($arg):$arg);break;case'b':$sql.=$arg?$this->driver->formats['TRUE']:$this->driver->formats['FALSE'];break;case'i':case'u':case'd':$sql.=(string)(int)$arg;break;case'f':$sql.=(string)(float)$arg;break;case'n':$sql.=$this->driver->quoteName($arg);break;default:$this->hasError=true;$sql.="**Unknown modifier %$this->modifier**";}$this->modifier=false;continue;}if(is_string($arg)){$lastString=$index;$toSkip=strcspn($arg,'`[\'"%');if($toSkip==strlen($arg)){$sql.=$arg;}else{$sql.=substr($arg,0,$toSkip).preg_replace_callback('/
|
||||
(?=`|\[|\'|"|%) ## speed-up
|
||||
(?:
|
||||
`(.+?)`| ## 1) `identifier`
|
||||
\[(.+?)\]| ## 2) [identifier]
|
||||
(\')((?:\'\'|[^\'])*)\'| ## 3,4) string
|
||||
(")((?:""|[^"])*)"| ## 5,6) "string"
|
||||
%([a-zA-Z])$| ## 7) right modifier
|
||||
(\'|") ## 8) lone-quote
|
||||
)/xs',array($this,'callback'),substr($arg,$toSkip));}continue;}$sql.=$this->formatValue($arg);}if($this->hasError)return new DibiException('Errors during generating SQL',array('sql'=>$sql));return trim($sql);}private function formatValue($value){if(is_string($value))return$this->driver->escape($value,TRUE);if(is_int($value)||is_float($value))return(string)$value;if(is_bool($value))return$value?$this->driver->formats['TRUE']:$this->driver->formats['FALSE'];if(is_null($value))return$this->driver->formats['NULL'];if($value instanceof IDibiVariable)return$value->toSql($this->driver);$this->hasError=true;return'**Unsupported type '.gettype($value).'**';}private function callback($matches){if($matches[1])return$this->driver->quoteName($matches[1]);if($matches[2])return$this->driver->quoteName($matches[2]);if($matches[3])return$this->driver->escape(strtr($matches[4],array("''"=>"'")),true);if($matches[5])return$this->driver->escape(strtr($matches[6],array('""'=>'"')),true);if($matches[7]){$this->modifier=$matches[7];return'';}if($matches[8]){return'**Alone quote**';$this->hasError=true;}die('this should be never executed');}}
|
||||
class DibiException extends Exception{private$info;public function __construct($message,$info=NULL){$this->info=$info;if(isset($info['message']))$message="$message: $info[message]";parent::__construct($message);}public function getSql(){return@$this->info['sql'];}}function is_error($var){return($var===FALSE)||($var instanceof Exception);}
|
||||
if(function_exists('date_default_timezone_set'))date_default_timezone_set('Europe/Prague');class TDate implements IDibiVariable{protected$time;public function __construct($time=NULL){if($time===NULL)$this->time=time();elseif(is_string($time))$this->time=strtotime($time);else$this->time=(int)$time;}public function toSQL($driver,$modifier=NULL){return date($driver->formats['date'],$this->time);}public function getTimeStamp(){return$this->time;}}class TDateTime extends TDate{public function toSQL($driver,$modifier=NULL){return date($driver->formats['datetime'],$this->time);}} interface IDibiVariable{public function toSQL($driver,$modifier=NULL);}class dibi{static private$registry=array();static private$conn;static private$parser;static public$sql;static public$error;static public$logfile;static public$debug=false;static private$query=array();static public function connect($config,$name='def'){if(!self::$parser)self::$parser=new DibiParser();if(isset(self::$registry[$name]))return new DibiException("Connection named '$name' already exists.");if(empty($config['driver']))return new DibiException('Driver is not specified.');$className="Dibi$config[driver]Driver"; if(!class_exists($className))return new DibiException("Unable to create instance of dibi driver class '$className'.");$conn=call_user_func(array($className,'connect'),$config);if(self::$logfile!=NULL){if(is_error($conn))$msg="Can't connect to DB '$config[driver]': ".$conn->getMessage();else$msg="Successfully connected to DB '$config[driver]'";$f=fopen(self::$logfile,'a');fwrite($f,"$msg\r\n\r\n");fclose($f);}if(is_error($conn)){if(self::$debug)echo'[dibi error] '.$conn->getMessage();return$conn;}self::$conn=self::$registry[$name]=$conn;return TRUE;}static public function isConnected(){return(bool)self::$conn;}static public function getConnection($name=NULL){return$name===NULL?self::$conn:@self::$registry[$name];}static public function activate($name){if(!isset(self::$registry[$name]))return FALSE;self::$conn=self::$registry[$name];return TRUE;}static public function query(){if(!self::$conn)return new DibiException('Dibi is not connected to DB');$args=func_num_args()?func_get_args():self::$query;self::$query=array();self::$sql=self::$parser->parse(self::$conn,$args);if(is_error(self::$sql))return self::$sql;$timer=-microtime(true);$res=self::$conn->query(self::$sql);$timer+=microtime(true);if(is_error($res)){if(self::$debug){echo'[dibi error] '.$res->getMessage();self::dump(self::$sql);}self::$error=$res;}else{self::$error=FALSE;}if(self::$logfile!=NULL){if(is_error($res))$msg=$res->getMessage();elseif($res instanceof DibiResult)$msg='object('.get_class($res).') rows: '.$res->rowCount();else$msg='OK';$f=fopen(self::$logfile,'a');fwrite($f,self::$sql.";\r\n-- Result: $msg"."\r\n-- Takes: ".sprintf('%0.3f',$timer*1000).' ms'."\r\n\r\n");fclose($f);}return$res;}static public function queryStart(){self::$query=func_get_args();}static public function queryAdd(){$args=func_get_args();self::$query=array_merge(self::$query,$args);}static public function test(){if(!self::$conn)return FALSE;$args=func_num_args()?func_get_args():self::$query;self::$query=array();$sql=self::$parser->parse(self::$conn,$args);if(is_error($sql)){self::dump($sql->getSql());return$sql->getSql();}else{self::dump($sql);return$sql;}}static public function insertId(){if(!self::$conn)return FALSE;return self::$conn->insertId();}static public function affectedRows(){if(!self::$conn)return FALSE;return self::$conn->affectedRows();}static public function dump($sql){static$highlight=array('ALL','DISTINCT','AS','ON','INTO','AND','OR','AS',);static$newline=array('SELECT','UPDATE','INSERT','DELETE','FROM','WHERE','HAVING','GROUP BY','ORDER BY','LIMIT','SET','VALUES','LEFT JOIN','INNER JOIN',);foreach($newline as$word)$sql=preg_replace('#\b'.$word.'\b#',"\n\$0",$sql);$sql=trim($sql);$sql=wordwrap($sql,100);$sql=htmlSpecialChars($sql);$sql=strtr($sql,array("\n"=>'<br />'));foreach($newline as$word)$sql=preg_replace('#\b'.$word.'\b#','<strong style="color:blue">$0</strong>',$sql);foreach($highlight as$word)$sql=preg_replace('#\b'.$word.'\b#','<strong style="color:green">$0</strong>',$sql);$sql=preg_replace('#\*\*.+?\*\*#','<strong style="color:red">$0</strong>',$sql);echo'<pre>',$sql,'</pre>';}static public function dumpResult(DibiResult$res){echo'<table class="dump"><tr>';echo'<th>Row</th>';$fieldCount=$res->fieldCount();for($i=0;$i<$fieldCount;$i++){$info=$res->fieldMeta($i);echo'<th>'.htmlSpecialChars($info['name']).'</th>';}echo'</tr>';foreach($res as$row=>$fields){echo'<tr><th>',$row,'</th>';foreach($fields as$field){if(is_object($field))$field=$field->__toString();echo'<td>',htmlSpecialChars($field),'</td>';}echo'</tr>';}echo'</table>';}}
|
||||
class DibiMySqlDriver extends DibiDriver{private$conn,$insertId=FALSE,$affectedRows=FALSE;public$formats=array('NULL'=>"NULL",'TRUE'=>"1",'FALSE'=>"0",'date'=>"'Y-m-d'",'datetime'=>"'Y-m-d H:i:s'",);public static function connect($config){if(!extension_loaded('mysql'))return new DibiException("PHP extension 'mysql' is not loaded");if(empty($config['host']))$config['host']='localhost';if(@$config['protocol']==='unix')$host=':'.$config['host'];else$host=$config['host'].(empty($config['port'])?'':$config['port']);if(function_exists('ini_set'))$save=ini_set('track_errors',TRUE);$php_errormsg='';if(empty($config['persistent']))$conn=@mysql_connect($host,@$config['username'],@$config['password']);else$conn=@mysql_pconnect($host,@$config['username'],@$config['password']);if(function_exists('ini_set'))ini_set('track_errors',$save);if(!is_resource($conn))return new DibiException("Connecting error",array('message'=>mysql_error()?mysql_error():$php_errormsg,'code'=>mysql_errno(),));if(!empty($config['charset'])){$succ=@mysql_query('SET CHARACTER SET '.$config['charset'],$conn);}if(!empty($config['database'])){if(!@mysql_select_db($config['database'],$conn))return new DibiException("Connecting error",array('message'=>mysql_error($conn),'code'=>mysql_errno($conn),));}$obj=new self($config);$obj->conn=$conn;return$obj;}public function query($sql){$this->insertId=$this->affectedRows=FALSE;$res=@mysql_query($sql,$this->conn);if(is_resource($res))return new DibiMySqlResult($res);if($res===FALSE)return new DibiException("Query error",array('message'=>mysql_error($this->conn),'code'=>mysql_errno($this->conn),'sql'=>$sql,));$this->affectedRows=mysql_affected_rows($this->conn);if($this->affectedRows<0)$this->affectedRows=FALSE;$this->insertId=mysql_insert_id($this->conn);if($this->insertId<1)$this->insertId=FALSE;return TRUE;}public function affectedRows(){return$this->affectedRows;}public function insertId(){return$this->insertId;}public function begin(){return mysql_query('BEGIN',$this->conn);}public function commit(){return mysql_query('COMMIT',$this->conn);}public function rollback(){return mysql_query('ROLLBACK',$this->conn);}public function escape($value,$appendQuotes=FALSE){return$appendQuotes?"'".mysql_real_escape_string($value,$this->conn)."'":mysql_real_escape_string($value,$this->conn);}public function quoteName($value){return'`'.strtr($value,array('.'=>'`.`')).'`';}public function getMetaData(){trigger_error('Meta is not implemented yet.',E_USER_WARNING);}}class DibiMySqlResult extends DibiResult{private$resource,$meta;public function __construct($resource){$this->resource=$resource;}public function rowCount(){return mysql_num_rows($this->resource);}protected function doFetch(){return mysql_fetch_assoc($this->resource);}public function seek($row){return mysql_data_seek($this->resource,$row);}protected function free(){mysql_free_result($this->resource);}public function getFields(){if($this->meta===NULL)$this->createMeta();return array_keys($this->meta);}protected function detectTypes(){if($this->meta===NULL)$this->createMeta();}public function getMetaData($field){if($this->meta===NULL)$this->createMeta();return isset($this->meta[$field])?$this->meta[$field]:FALSE;}private function createMeta(){static$types=array('ENUM'=>self::FIELD_TEXT,'SET'=>self::FIELD_TEXT,'CHAR'=>self::FIELD_TEXT,'VARCHAR'=>self::FIELD_TEXT,'STRING'=>self::FIELD_TEXT,'TINYTEXT'=>self::FIELD_TEXT,'TEXT'=>self::FIELD_TEXT,'MEDIUMTEXT'=>self::FIELD_TEXT,'LONGTEXT'=>self::FIELD_TEXT,'BINARY'=>self::FIELD_BINARY,'VARBINARY'=>self::FIELD_BINARY,'TINYBLOB'=>self::FIELD_BINARY,'BLOB'=>self::FIELD_BINARY,'MEDIUMBLOB'=>self::FIELD_BINARY,'LONGBLOB'=>self::FIELD_BINARY,'DATE'=>self::FIELD_DATE,'DATETIME'=>self::FIELD_DATETIME,'TIMESTAMP'=>self::FIELD_DATETIME,'TIME'=>self::FIELD_DATETIME,'BIT'=>self::FIELD_BOOL,'YEAR'=>self::FIELD_INTEGER,'TINYINT'=>self::FIELD_INTEGER,'SMALLINT'=>self::FIELD_INTEGER,'MEDIUMINT'=>self::FIELD_INTEGER,'INT'=>self::FIELD_INTEGER,'INTEGER'=>self::FIELD_INTEGER,'BIGINT'=>self::FIELD_INTEGER,'FLOAT'=>self::FIELD_FLOAT,'DOUBLE'=>self::FIELD_FLOAT,'REAL'=>self::FIELD_FLOAT,'DECIMAL'=>self::FIELD_FLOAT,'NUMERIC'=>self::FIELD_FLOAT,);$count=mysql_num_fields($this->resource);$this->meta=$this->convert=array();for($index=0;$index<$count;$index++){$info['native']=$native=strtoupper(mysql_field_type($this->resource,$index));$info['flags']=explode(' ',mysql_field_flags($this->resource,$index));$info['length']=mysql_field_len($this->resource,$index);$info['table']=mysql_field_table($this->resource,$index);if(in_array('auto_increment',$info['flags']))$info['type']=self::FIELD_COUNTER;else{$info['type']=isset($types[$native])?$types[$native]:self::FIELD_UNKNOWN;}$name=mysql_field_name($this->resource,$index);$this->meta[$name]=$info;$this->convert[$name]=$info['type'];}}}
|
||||
class DibiMySqliDriver extends DibiDriver{private$conn,$insertId=FALSE,$affectedRows=FALSE;public$formats=array('NULL'=>"NULL",'TRUE'=>"1",'FALSE'=>"0",'date'=>"'Y-m-d'",'datetime'=>"'Y-m-d H:i:s'",);public static function connect($config){if(!extension_loaded('mysqli'))return new DibiException("PHP extension 'mysqli' is not loaded");if(empty($config['host']))$config['host']='localhost';$conn=@mysqli_connect($config['host'],@$config['username'],@$config['password'],@$config['database'],@$config['port']);if(!$conn)return new DibiException("Connecting error",array('message'=>mysqli_connect_error(),'code'=>mysqli_connect_errno(),));if(!empty($config['charset']))mysqli_query($conn,'SET CHARACTER SET '.$config['charset']);$obj=new self($config);$obj->conn=$conn;return$obj;}public function query($sql){$this->insertId=$this->affectedRows=FALSE;$res=@mysqli_query($this->conn,$sql);if(is_object($res))return new DibiMySqliResult($res);if($res===FALSE)return new DibiException("Query error",$this->errorInfo($sql));$this->affectedRows=mysqli_affected_rows($this->conn);if($this->affectedRows<0)$this->affectedRows=FALSE;$this->insertId=mysqli_insert_id($this->conn);if($this->insertId<1)$this->insertId=FALSE;return TRUE;}public function affectedRows(){return$this->affectedRows;}public function insertId(){return$this->insertId;}public function begin(){return mysqli_autocommit($this->conn,FALSE);}public function commit(){$ok=mysqli_commit($this->conn);mysqli_autocommit($this->conn,TRUE);return$ok;}public function rollback(){$ok=mysqli_rollback($this->conn);mysqli_autocommit($this->conn,TRUE);return$ok;}private function errorInfo($sql=NULL){return array('message'=>mysqli_error($this->conn),'code'=>mysqli_errno($this->conn),'sql'=>$sql,);}public function escape($value,$appendQuotes=FALSE){return$appendQuotes?"'".mysqli_real_escape_string($this->conn,$value)."'":mysqli_real_escape_string($this->conn,$value);}public function quoteName($value){return'`'.strtr($value,array('.'=>'`.`')).'`';}public function getMetaData(){trigger_error('Meta is not implemented yet.',E_USER_WARNING);}}class DibiMySqliResult extends DibiResult{private$resource,$meta;public function __construct($resource){$this->resource=$resource;}public function rowCount(){return mysqli_num_rows($this->resource);}protected function doFetch(){return mysqli_fetch_assoc($this->resource);}public function seek($row){return mysqli_data_seek($this->resource,$row);}protected function free(){mysqli_free_result($this->resource);}public function getFields(){if($this->meta===NULL)$this->createMeta();return array_keys($this->meta);}protected function detectTypes(){if($this->meta===NULL)$this->createMeta();}public function getMetaData($field){if($this->meta===NULL)$this->createMeta();return isset($this->meta[$field])?$this->meta[$field]:FALSE;}private function createMeta(){static$types=array(MYSQLI_TYPE_FLOAT=>self::FIELD_FLOAT,MYSQLI_TYPE_DOUBLE=>self::FIELD_FLOAT,MYSQLI_TYPE_DECIMAL=>self::FIELD_FLOAT,MYSQLI_TYPE_TINY=>self::FIELD_INTEGER,MYSQLI_TYPE_SHORT=>self::FIELD_INTEGER,MYSQLI_TYPE_LONG=>self::FIELD_INTEGER,MYSQLI_TYPE_LONGLONG=>self::FIELD_INTEGER,MYSQLI_TYPE_INT24=>self::FIELD_INTEGER,MYSQLI_TYPE_YEAR=>self::FIELD_INTEGER,MYSQLI_TYPE_GEOMETRY=>self::FIELD_INTEGER,MYSQLI_TYPE_DATE=>self::FIELD_DATE,MYSQLI_TYPE_NEWDATE=>self::FIELD_DATE,MYSQLI_TYPE_TIMESTAMP=>self::FIELD_DATETIME,MYSQLI_TYPE_TIME=>self::FIELD_DATETIME,MYSQLI_TYPE_DATETIME=>self::FIELD_DATETIME,MYSQLI_TYPE_ENUM=>self::FIELD_TEXT,MYSQLI_TYPE_SET=>self::FIELD_TEXT,MYSQLI_TYPE_STRING=>self::FIELD_TEXT,MYSQLI_TYPE_VAR_STRING=>self::FIELD_TEXT,MYSQLI_TYPE_TINY_BLOB=>self::FIELD_BINARY,MYSQLI_TYPE_MEDIUM_BLOB=>self::FIELD_BINARY,MYSQLI_TYPE_LONG_BLOB=>self::FIELD_BINARY,MYSQLI_TYPE_BLOB=>self::FIELD_BINARY,);$count=mysqli_num_fields($this->resource);$this->meta=$this->convert=array();for($index=0;$index<$count;$index++){$info=(array)mysqli_fetch_field_direct($this->resource,$index);$native=$info['native']=$info['type'];if($info['flags']&MYSQLI_AUTO_INCREMENT_FLAG)$info['type']=self::FIELD_COUNTER;else{$info['type']=isset($types[$native])?$types[$native]:self::FIELD_UNKNOWN;}$this->meta[$info['name']]=$info;$this->convert[$info['name']]=$info['type'];}}}
|
||||
class DibiOdbcDriver extends DibiDriver{private$conn,$affectedRows=FALSE;public$formats=array('NULL'=>"NULL",'TRUE'=>"-1",'FALSE'=>"0",'date'=>"#m/d/Y#",'datetime'=>"#m/d/Y H:i:s#",);public static function connect($config){if(!extension_loaded('odbc'))return new DibiException("PHP extension 'odbc' is not loaded");if(@$config['persistent'])$conn=@odbc_pconnect($config['database'],$config['username'],$config['password']);else$conn=@odbc_connect($config['database'],$config['username'],$config['password']);if(!is_resource($conn))return new DibiException("Connecting error",array('message'=>odbc_errormsg(),'code'=>odbc_error(),));$obj=new self($config);$obj->conn=$conn;return$obj;}public function query($sql){$this->affectedRows=FALSE;$res=@odbc_exec($this->conn,$sql);if(is_resource($res))return new DibiOdbcResult($res);if($res===FALSE)return new DibiException("Query error",$this->errorInfo($sql));$this->affectedRows=odbc_num_rows($this->conn);if($this->affectedRows<0)$this->affectedRows=FALSE;return TRUE;}public function affectedRows(){return$this->affectedRows;}public function insertId(){return FALSE;}public function begin(){return odbc_autocommit($this->conn,FALSE);}public function commit(){$ok=odbc_commit($this->conn);odbc_autocommit($this->conn,TRUE);return$ok;}public function rollback(){$ok=odbc_rollback($this->conn);odbc_autocommit($this->conn,TRUE);return$ok;}private function errorInfo($sql=NULL){return array('message'=>odbc_errormsg($this->conn),'code'=>odbc_error($this->conn),'sql'=>$sql,);}public function escape($value,$appendQuotes=FALSE){$value=str_replace("'","''",$value);return$appendQuotes?"'".$value."'":$value;}public function quoteName($value){return'['.strtr($value,array('.'=>'].[')).']';}public function getMetaData(){trigger_error('Meta is not implemented yet.',E_USER_WARNING);}}class DibiOdbcResult extends DibiResult{private$resource,$meta,$row=0;public function __construct($resource){$this->resource=$resource;}public function rowCount(){return odbc_num_rows($this->resource);}protected function doFetch(){return odbc_fetch_array($this->resource,$this->row++);}public function seek($row){$this->row=$row;}protected function free(){odbc_free_result($this->resource);}public function getFields(){if($this->meta===NULL)$this->createMeta();return array_keys($this->meta);}protected function detectTypes(){if($this->meta===NULL)$this->createMeta();}public function getMetaData($field){if($this->meta===NULL)$this->createMeta();return isset($this->meta[$field])?$this->meta[$field]:FALSE;}private function createMeta(){if($this->meta!==NULL)return$this->meta;static$types=array('CHAR'=>self::FIELD_TEXT,'COUNTER'=>self::FIELD_COUNTER,'VARCHAR'=>self::FIELD_TEXT,'LONGCHAR'=>self::FIELD_TEXT,'INTEGER'=>self::FIELD_INTEGER,'DATETIME'=>self::FIELD_DATETIME,'CURRENCY'=>self::FIELD_FLOAT,'BIT'=>self::FIELD_BOOL,'LONGBINARY'=>self::FIELD_BINARY,'SMALLINT'=>self::FIELD_INTEGER,'BYTE'=>self::FIELD_INTEGER,'BIGINT'=>self::FIELD_INTEGER,'INT'=>self::FIELD_INTEGER,'TINYINT'=>self::FIELD_INTEGER,'REAL'=>self::FIELD_FLOAT,'DOUBLE'=>self::FIELD_FLOAT,'DECIMAL'=>self::FIELD_FLOAT,'NUMERIC'=>self::FIELD_FLOAT,'MONEY'=>self::FIELD_FLOAT,'SMALLMONEY'=>self::FIELD_FLOAT,'FLOAT'=>self::FIELD_FLOAT,'YESNO'=>self::FIELD_BOOL,);$count=odbc_num_fields($this->resource);$this->meta=$this->convert=array();for($index=1;$index<=$count;$index++){$native=strtoupper(odbc_field_type($this->resource,$index));$name=odbc_field_name($this->resource,$index);$this->meta[$name]=array('type'=>isset($types[$native])?$types[$native]:self::FIELD_UNKNOWN,'native'=>$native,'length'=>odbc_field_len($this->resource,$index),'scale'=>odbc_field_scale($this->resource,$index),'precision'=>odbc_field_precision($this->resource,$index),);$this->convert[$name]=$this->meta[$name]['type'];}}}
|
||||
class DibiSqliteDriver extends DibiDriver{private$conn,$insertId=FALSE,$affectedRows=FALSE;public$formats=array('NULL'=>"NULL",'TRUE'=>"1",'FALSE'=>"0",'date'=>"'Y-m-d'",'datetime'=>"'Y-m-d H:i:s'",);public static function connect($config){if(!extension_loaded('sqlite'))return new DibiException("PHP extension 'sqlite' is not loaded");if(empty($config['database']))return new DibiException("Database must be specified");$errorMsg='';if(empty($config['persistent']))$conn=@sqlite_open($config['database'],@$config['mode'],$errorMsg);else$conn=@sqlite_popen($config['database'],@$config['mode'],$errorMsg);if(!$conn)return new DibiException("Connecting error",array('message'=>$errorMsg,));$obj=new self($config);$obj->conn=$conn;return$obj;}public function query($sql){$this->insertId=$this->affectedRows=FALSE;$errorMsg='';$res=@sqlite_query($this->conn,$sql,SQLITE_ASSOC,$errorMsg);if($res===FALSE)return new DibiException("Query error",array('message'=>$errorMsg,'sql'=>$sql,));if(is_resource($res))return new DibiSqliteResult($res);$this->affectedRows=sqlite_changes($this->conn);if($this->affectedRows<0)$this->affectedRows=FALSE;$this->insertId=sqlite_last_insert_rowid($this->conn);if($this->insertId<1)$this->insertId=FALSE;return TRUE;}public function affectedRows(){return$this->affectedRows;}public function insertId(){return$this->insertId;}public function begin(){return sqlite_query($this->conn,'BEGIN');}public function commit(){return sqlite_query($this->conn,'COMMIT');}public function rollback(){return sqlite_query($this->conn,'ROLLBACK');}public function escape($value,$appendQuotes=FALSE){return$appendQuotes?"'".sqlite_escape_string($value)."'":sqlite_escape_string($value);}public function quoteName($value){return$value;}public function getMetaData(){trigger_error('Meta is not implemented yet.',E_USER_WARNING);}}class DibiSqliteResult extends DibiResult{private$resource,$meta;public function __construct($resource){$this->resource=$resource;}public function rowCount(){return sqlite_num_rows($this->resource);}protected function doFetch(){return sqlite_fetch_array($this->resource,SQLITE_ASSOC);}public function seek($row){return sqlite_seek($this->resource,$row);}protected function free(){}public function getFields(){if($this->meta===NULL)$this->createMeta();return array_keys($this->meta);}protected function detectTypes(){if($this->meta===NULL)$this->createMeta();}public function getMetaData($field){if($this->meta===NULL)$this->createMeta();return isset($this->meta[$field])?$this->meta[$field]:FALSE;}private function createMeta(){$count=sqlite_num_fields($this->resource);$this->meta=$this->convert=array();for($index=0;$index<$count;$index++){$name=sqlite_field_name($this->resource,$index);$this->meta[$name]=array('type'=>self::FIELD_UNKNOWN);$this->convert[$name]=self::FIELD_UNKNOWN;}}}?>
|
416
dibi/dibi.php
Normal file
416
dibi/dibi.php
Normal file
@@ -0,0 +1,416 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* dibi - Database Abstraction Layer according to dgx
|
||||
* --------------------------------------------------
|
||||
*
|
||||
* This source file is subject to the GNU GPL license.
|
||||
*
|
||||
* @author David Grudl aka -dgx- <dave@dgx.cz>
|
||||
* @link http://texy.info/dibi/
|
||||
* @copyright Copyright (c) 2005-2006 David Grudl
|
||||
* @license GNU GENERAL PUBLIC LICENSE
|
||||
* @package dibi
|
||||
* @category Database
|
||||
* @version 0.5b (2006-05-31) for PHP5
|
||||
*/
|
||||
|
||||
|
||||
define('dibi', 'Database Abstraction Layer (c) David Grudl, http://texy.info/dibi/');
|
||||
|
||||
|
||||
if (version_compare(PHP_VERSION , '5.0.3', '<'))
|
||||
die('dibi needs PHP 5.0.3 or newer');
|
||||
|
||||
|
||||
// libraries
|
||||
require_once dirname(__FILE__).'/libs/driver.php';
|
||||
require_once dirname(__FILE__).'/libs/resultset.php';
|
||||
require_once dirname(__FILE__).'/libs/parser.php';
|
||||
require_once dirname(__FILE__).'/libs/exception.php';
|
||||
|
||||
// support
|
||||
require_once dirname(__FILE__).'/libs/date.type.demo.php';
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Interface for user variable, used for generating SQL
|
||||
*/
|
||||
interface IDibiVariable
|
||||
{
|
||||
/**
|
||||
* Format for SQL
|
||||
*
|
||||
* @param object destination DibiDriver
|
||||
* @param string optional modifier
|
||||
* @return string SQL code
|
||||
*/
|
||||
public function toSQL($driver, $modifier = NULL);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Interface for database drivers
|
||||
*
|
||||
* This class is static container class for creating DB objects and
|
||||
* store debug & connections info.
|
||||
*
|
||||
*/
|
||||
class dibi
|
||||
{
|
||||
/**
|
||||
* Connection registry storage for DibiDriver objects
|
||||
* @var array
|
||||
*/
|
||||
static private $registry = array();
|
||||
|
||||
/**
|
||||
* Current connection
|
||||
* @var object DibiDriver
|
||||
*/
|
||||
static private $conn;
|
||||
|
||||
/**
|
||||
* Arguments -> SQL parser
|
||||
* @var object DibiParser
|
||||
*/
|
||||
static private $parser;
|
||||
|
||||
/**
|
||||
* Last SQL command @see dibi::query()
|
||||
* @var string
|
||||
*/
|
||||
static public $sql;
|
||||
static public $error;
|
||||
|
||||
/**
|
||||
* File for logging SQL queryies - strongly recommended to use with NSafeStream
|
||||
* @var string|NULL
|
||||
*/
|
||||
static public $logfile;
|
||||
|
||||
/**
|
||||
* Enable/disable debug mode
|
||||
* @var bool
|
||||
*/
|
||||
static public $debug = false;
|
||||
|
||||
/**
|
||||
* Progressive created query
|
||||
* @var array
|
||||
*/
|
||||
static private $query = array();
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new DibiDriver object and connects it to specified database
|
||||
*
|
||||
* @param array connection parameters
|
||||
* @param string connection name
|
||||
* @return bool|object TRUE on success, FALSE or Exception on failure
|
||||
*/
|
||||
static public function connect($config, $name = 'def')
|
||||
{
|
||||
// init parser
|
||||
if (!self::$parser) self::$parser = new DibiParser();
|
||||
|
||||
// $name must be unique
|
||||
if (isset(self::$registry[$name]))
|
||||
return new DibiException("Connection named '$name' already exists.");
|
||||
|
||||
// config['driver'] is required
|
||||
if (empty($config['driver']))
|
||||
return new DibiException('Driver is not specified.');
|
||||
|
||||
// include dibi driver
|
||||
$className = "Dibi$config[driver]Driver";
|
||||
require_once dirname(__FILE__) . "/drivers/$config[driver].php";
|
||||
|
||||
if (!class_exists($className))
|
||||
return new DibiException("Unable to create instance of dibi driver class '$className'.");
|
||||
|
||||
|
||||
// create connection object
|
||||
/** like $conn = $className::connect($config); */
|
||||
$conn = call_user_func(array($className, 'connect'), $config);
|
||||
|
||||
// optionally log to file
|
||||
// todo: log other exceptions!
|
||||
if (self::$logfile != NULL) {
|
||||
if (is_error($conn))
|
||||
$msg = "Can't connect to DB '$config[driver]': ".$conn->getMessage();
|
||||
else
|
||||
$msg = "Successfully connected to DB '$config[driver]'";
|
||||
|
||||
$f = fopen(self::$logfile, 'a');
|
||||
fwrite($f, "$msg\r\n\r\n");
|
||||
fclose($f);
|
||||
}
|
||||
|
||||
if (is_error($conn)) {
|
||||
// optionally debug on display
|
||||
if (self::$debug) echo '[dibi error] ' . $conn->getMessage();
|
||||
|
||||
return $conn; // reraise the exception
|
||||
}
|
||||
|
||||
// store connection in list
|
||||
self::$conn = self::$registry[$name] = $conn;
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Returns TRUE when connection was established
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
static public function isConnected()
|
||||
{
|
||||
return (bool) self::$conn;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Retrieve active connection
|
||||
*
|
||||
* @param string connection registy name or NULL for active connection
|
||||
* @return object DibiDriver object.
|
||||
*/
|
||||
static public function getConnection($name = NULL)
|
||||
{
|
||||
return $name === NULL
|
||||
? self::$conn
|
||||
: @self::$registry[$name];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Change active connection
|
||||
*
|
||||
* @param string connection registy name
|
||||
* @return void
|
||||
*/
|
||||
static public function activate($name)
|
||||
{
|
||||
if (!isset(self::$registry[$name]))
|
||||
return FALSE;
|
||||
|
||||
// change active connection
|
||||
self::$conn = self::$registry[$name];
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Generates and executes SQL query
|
||||
*
|
||||
* @param mixed one or more arguments
|
||||
* @return int|DibiResult|Exception
|
||||
*/
|
||||
static public function query()
|
||||
{
|
||||
if (!self::$conn) return new DibiException('Dibi is not connected to DB'); // is connected?
|
||||
|
||||
// receive arguments
|
||||
$args = func_num_args() ? func_get_args() : self::$query;
|
||||
self::$query = array();
|
||||
|
||||
// and generate SQL
|
||||
self::$sql = self::$parser->parse(self::$conn, $args);
|
||||
if (is_error(self::$sql)) return self::$sql; // reraise the exception
|
||||
|
||||
// execute SQL
|
||||
$timer = -microtime(true);
|
||||
$res = self::$conn->query(self::$sql);
|
||||
$timer += microtime(true);
|
||||
|
||||
if (is_error($res)) {
|
||||
// optionally debug on display
|
||||
if (self::$debug) {
|
||||
echo '[dibi error] ' . $res->getMessage();
|
||||
self::dump(self::$sql);
|
||||
}
|
||||
// todo: log all errors!
|
||||
self::$error = $res;
|
||||
} else {
|
||||
self::$error = FALSE;
|
||||
}
|
||||
|
||||
// optionally log to file
|
||||
if (self::$logfile != NULL)
|
||||
{
|
||||
if (is_error($res))
|
||||
$msg = $res->getMessage();
|
||||
elseif ($res instanceof DibiResult)
|
||||
$msg = 'object('.get_class($res).') rows: '.$res->rowCount();
|
||||
else
|
||||
$msg = 'OK';
|
||||
|
||||
$f = fopen(self::$logfile, 'a');
|
||||
fwrite($f,
|
||||
self::$sql
|
||||
. ";\r\n-- Result: $msg"
|
||||
. "\r\n-- Takes: " . sprintf('%0.3f', $timer * 1000) . ' ms'
|
||||
. "\r\n\r\n"
|
||||
);
|
||||
fclose($f);
|
||||
}
|
||||
|
||||
return $res;
|
||||
}
|
||||
|
||||
|
||||
static public function queryStart()
|
||||
{
|
||||
self::$query = func_get_args();
|
||||
}
|
||||
|
||||
|
||||
static public function queryAdd()
|
||||
{
|
||||
$args = func_get_args();
|
||||
self::$query = array_merge(self::$query, $args);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Generates and returns SQL query
|
||||
*
|
||||
* @param mixed one or more arguments
|
||||
* @return string
|
||||
*/
|
||||
static public function test()
|
||||
{
|
||||
if (!self::$conn) return FALSE; // is connected?
|
||||
|
||||
// receive arguments
|
||||
$args = func_num_args() ? func_get_args() : self::$query;
|
||||
self::$query = array();
|
||||
|
||||
// and generate SQL
|
||||
$sql = self::$parser->parse(self::$conn, $args);
|
||||
if (is_error($sql)) {
|
||||
self::dump($sql->getSql());
|
||||
return $sql->getSql();
|
||||
} else {
|
||||
self::dump($sql);
|
||||
return $sql;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Monostate for DibiDriver::insertId()
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
static public function insertId()
|
||||
{
|
||||
if (!self::$conn) return FALSE; // is connected?
|
||||
|
||||
return self::$conn->insertId();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Monostate for DibiDriver::affectedRows()
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
static public function affectedRows()
|
||||
{
|
||||
if (!self::$conn) return FALSE; // is connected?
|
||||
|
||||
return self::$conn->affectedRows();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Prints out a syntax highlighted version of the SQL command
|
||||
*
|
||||
* @param string SQL command
|
||||
* @return void
|
||||
*/
|
||||
static public function dump($sql) {
|
||||
static $highlight = array ('ALL', 'DISTINCT', 'AS', 'ON', 'INTO', 'AND', 'OR', 'AS', );
|
||||
static $newline = array ('SELECT', 'UPDATE', 'INSERT', 'DELETE', 'FROM', 'WHERE', 'HAVING', 'GROUP BY', 'ORDER BY', 'LIMIT', 'SET', 'VALUES', 'LEFT JOIN', 'INNER JOIN',);
|
||||
|
||||
// insert new lines
|
||||
foreach ($newline as $word)
|
||||
$sql = preg_replace('#\b'.$word.'\b#', "\n\$0", $sql);
|
||||
|
||||
$sql = trim($sql);
|
||||
// reduce spaces
|
||||
// $sql = preg_replace('# +#', ' ', $sql);
|
||||
|
||||
$sql = wordwrap($sql, 100);
|
||||
$sql = htmlSpecialChars($sql);
|
||||
$sql = strtr($sql, array("\n" => '<br />'));
|
||||
|
||||
foreach ($newline as $word)
|
||||
$sql = preg_replace('#\b'.$word.'\b#', '<strong style="color:blue">$0</strong>', $sql);
|
||||
|
||||
foreach ($highlight as $word)
|
||||
$sql = preg_replace('#\b'.$word.'\b#', '<strong style="color:green">$0</strong>', $sql);
|
||||
|
||||
$sql = preg_replace('#\*\*.+?\*\*#', '<strong style="color:red">$0</strong>', $sql);
|
||||
|
||||
echo '<pre>', $sql, '</pre>';
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Displays complete result-set as HTML table
|
||||
*
|
||||
* @param object DibiResult
|
||||
* @return void
|
||||
*/
|
||||
static public function dumpResult(DibiResult $res)
|
||||
{
|
||||
echo '<table class="dump"><tr>';
|
||||
echo '<th>Row</th>';
|
||||
$fieldCount = $res->fieldCount();
|
||||
for ($i = 0; $i < $fieldCount; $i++) {
|
||||
$info = $res->fieldMeta($i);
|
||||
echo '<th>'.htmlSpecialChars($info['name']).'</th>';
|
||||
}
|
||||
echo '</tr>';
|
||||
|
||||
foreach ($res as $row => $fields) {
|
||||
echo '<tr><th>', $row, '</th>';
|
||||
foreach ($fields as $field) {
|
||||
if (is_object($field)) $field = $field->__toString();
|
||||
echo '<td>', htmlSpecialChars($field), '</td>';
|
||||
}
|
||||
echo '</tr>';
|
||||
}
|
||||
echo '</table>';
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // class dibi
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
?>
|
341
dibi/drivers/mysql.php
Normal file
341
dibi/drivers/mysql.php
Normal file
@@ -0,0 +1,341 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* dibi - Database Abstraction Layer according to dgx
|
||||
* --------------------------------------------------
|
||||
*
|
||||
* This source file is subject to the GNU GPL license.
|
||||
*
|
||||
* @author David Grudl aka -dgx- <dave@dgx.cz>
|
||||
* @link http://texy.info/dibi/
|
||||
* @copyright Copyright (c) 2005-2006 David Grudl
|
||||
* @license GNU GENERAL PUBLIC LICENSE
|
||||
* @package dibi
|
||||
* @category Database
|
||||
* @version 0.5b (2006-05-31) for PHP5
|
||||
*/
|
||||
|
||||
|
||||
// security - include dibi.php, not this file
|
||||
if (!defined('dibi')) die();
|
||||
|
||||
|
||||
/**
|
||||
* The dibi driver for MySQL database
|
||||
*
|
||||
*/
|
||||
class DibiMySqlDriver extends DibiDriver {
|
||||
private
|
||||
$conn,
|
||||
$insertId = FALSE,
|
||||
$affectedRows = FALSE;
|
||||
|
||||
public
|
||||
$formats = array(
|
||||
'NULL' => "NULL",
|
||||
'TRUE' => "1",
|
||||
'FALSE' => "0",
|
||||
'date' => "'Y-m-d'",
|
||||
'datetime' => "'Y-m-d H:i:s'",
|
||||
);
|
||||
|
||||
|
||||
/**
|
||||
* Driver factory
|
||||
*/
|
||||
public static function connect($config)
|
||||
{
|
||||
if (!extension_loaded('mysql'))
|
||||
return new DibiException("PHP extension 'mysql' is not loaded");
|
||||
|
||||
|
||||
if (empty($config['host'])) $config['host'] = 'localhost';
|
||||
|
||||
if (@$config['protocol'] === 'unix') // host can be socket
|
||||
$host = ':' . $config['host'];
|
||||
else
|
||||
$host = $config['host'] . (empty($config['port']) ? '' : $config['port']);
|
||||
|
||||
|
||||
// some errors aren't handled. Must use $php_errormsg
|
||||
if (function_exists('ini_set'))
|
||||
$save = ini_set('track_errors', TRUE);
|
||||
$php_errormsg = '';
|
||||
|
||||
if (empty($config['persistent']))
|
||||
$conn = @mysql_connect($host, @$config['username'], @$config['password']);
|
||||
else
|
||||
$conn = @mysql_pconnect($host, @$config['username'], @$config['password']);
|
||||
|
||||
if (function_exists('ini_set'))
|
||||
ini_set('track_errors', $save);
|
||||
|
||||
|
||||
if (!is_resource($conn))
|
||||
return new DibiException("Connecting error", array(
|
||||
'message' => mysql_error() ? mysql_error() : $php_errormsg,
|
||||
'code' => mysql_errno(),
|
||||
));
|
||||
|
||||
|
||||
if (!empty($config['charset'])) {
|
||||
$succ = @mysql_query('SET CHARACTER SET '.$config['charset'], $conn);
|
||||
// don't handle this error...
|
||||
}
|
||||
|
||||
|
||||
if (!empty($config['database'])) {
|
||||
if (!@mysql_select_db($config['database'], $conn))
|
||||
return new DibiException("Connecting error", array(
|
||||
'message' => mysql_error($conn),
|
||||
'code' => mysql_errno($conn),
|
||||
));
|
||||
}
|
||||
|
||||
|
||||
$obj = new self($config);
|
||||
$obj->conn = $conn;
|
||||
return $obj;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function query($sql)
|
||||
{
|
||||
$this->insertId = $this->affectedRows = FALSE;
|
||||
$res = @mysql_query($sql, $this->conn);
|
||||
|
||||
if (is_resource($res))
|
||||
return new DibiMySqlResult($res);
|
||||
|
||||
if ($res === FALSE)
|
||||
return new DibiException("Query error", array(
|
||||
'message' => mysql_error($this->conn),
|
||||
'code' => mysql_errno($this->conn),
|
||||
'sql' => $sql,
|
||||
));
|
||||
|
||||
$this->affectedRows = mysql_affected_rows($this->conn);
|
||||
if ($this->affectedRows < 0) $this->affectedRows = FALSE;
|
||||
|
||||
$this->insertId = mysql_insert_id($this->conn);
|
||||
if ($this->insertId < 1) $this->insertId = FALSE;
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
||||
public function affectedRows()
|
||||
{
|
||||
return $this->affectedRows;
|
||||
}
|
||||
|
||||
|
||||
public function insertId()
|
||||
{
|
||||
return $this->insertId;
|
||||
}
|
||||
|
||||
|
||||
public function begin()
|
||||
{
|
||||
return mysql_query('BEGIN', $this->conn);
|
||||
}
|
||||
|
||||
|
||||
public function commit()
|
||||
{
|
||||
return mysql_query('COMMIT', $this->conn);
|
||||
}
|
||||
|
||||
|
||||
public function rollback()
|
||||
{
|
||||
return mysql_query('ROLLBACK', $this->conn);
|
||||
}
|
||||
|
||||
|
||||
public function escape($value, $appendQuotes = FALSE)
|
||||
{
|
||||
return $appendQuotes
|
||||
? "'" . mysql_real_escape_string($value, $this->conn) . "'"
|
||||
: mysql_real_escape_string($value, $this->conn);
|
||||
}
|
||||
|
||||
|
||||
public function quoteName($value)
|
||||
{
|
||||
return '`' . strtr($value, array('.' => '`.`')) . '`';
|
||||
}
|
||||
|
||||
|
||||
public function getMetaData()
|
||||
{
|
||||
trigger_error('Meta is not implemented yet.', E_USER_WARNING);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
// is this really needed?
|
||||
public function getResource()
|
||||
{
|
||||
return $this->conn;
|
||||
}
|
||||
|
||||
// experimental
|
||||
public function applyLimit(&$sql, $offset, $limit)
|
||||
{
|
||||
if ($limit > 0) {
|
||||
$sql .= " LIMIT " . (int) $limit . ($offset > 0 ? " OFFSET " . (int) $offset : "");
|
||||
} elseif ($offset > 0) {
|
||||
$sql .= " LIMIT " . $offset . ", 18446744073709551615";
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
} // DibiMySqlDriver
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
class DibiMySqlResult extends DibiResult
|
||||
{
|
||||
private
|
||||
$resource,
|
||||
$meta;
|
||||
|
||||
|
||||
public function __construct($resource)
|
||||
{
|
||||
$this->resource = $resource;
|
||||
}
|
||||
|
||||
|
||||
public function rowCount()
|
||||
{
|
||||
return mysql_num_rows($this->resource);
|
||||
}
|
||||
|
||||
|
||||
protected function doFetch()
|
||||
{
|
||||
return mysql_fetch_assoc($this->resource);
|
||||
}
|
||||
|
||||
|
||||
public function seek($row)
|
||||
{
|
||||
return mysql_data_seek($this->resource, $row);
|
||||
}
|
||||
|
||||
|
||||
protected function free()
|
||||
{
|
||||
mysql_free_result($this->resource);
|
||||
}
|
||||
|
||||
|
||||
public function getFields()
|
||||
{
|
||||
// cache
|
||||
if ($this->meta === NULL)
|
||||
$this->createMeta();
|
||||
|
||||
return array_keys($this->meta);
|
||||
}
|
||||
|
||||
|
||||
protected function detectTypes()
|
||||
{
|
||||
if ($this->meta === NULL)
|
||||
$this->createMeta();
|
||||
}
|
||||
|
||||
|
||||
/** this is experimental */
|
||||
public function getMetaData($field)
|
||||
{
|
||||
// cache
|
||||
if ($this->meta === NULL)
|
||||
$this->createMeta();
|
||||
|
||||
return isset($this->meta[$field]) ? $this->meta[$field] : FALSE;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/** this is experimental */
|
||||
private function createMeta()
|
||||
{
|
||||
static $types = array(
|
||||
'ENUM' => self::FIELD_TEXT, // eventually self::FIELD_INTEGER
|
||||
'SET' => self::FIELD_TEXT, // eventually self::FIELD_INTEGER
|
||||
'CHAR' => self::FIELD_TEXT,
|
||||
'VARCHAR' => self::FIELD_TEXT,
|
||||
'STRING' => self::FIELD_TEXT,
|
||||
'TINYTEXT' => self::FIELD_TEXT,
|
||||
'TEXT' => self::FIELD_TEXT,
|
||||
'MEDIUMTEXT'=> self::FIELD_TEXT,
|
||||
'LONGTEXT' => self::FIELD_TEXT,
|
||||
'BINARY' => self::FIELD_BINARY,
|
||||
'VARBINARY' => self::FIELD_BINARY,
|
||||
'TINYBLOB' => self::FIELD_BINARY,
|
||||
'BLOB' => self::FIELD_BINARY,
|
||||
'MEDIUMBLOB'=> self::FIELD_BINARY,
|
||||
'LONGBLOB' => self::FIELD_BINARY,
|
||||
'DATE' => self::FIELD_DATE,
|
||||
'DATETIME' => self::FIELD_DATETIME,
|
||||
'TIMESTAMP' => self::FIELD_DATETIME,
|
||||
'TIME' => self::FIELD_DATETIME,
|
||||
'BIT' => self::FIELD_BOOL,
|
||||
'YEAR' => self::FIELD_INTEGER,
|
||||
'TINYINT' => self::FIELD_INTEGER,
|
||||
'SMALLINT' => self::FIELD_INTEGER,
|
||||
'MEDIUMINT' => self::FIELD_INTEGER,
|
||||
'INT' => self::FIELD_INTEGER,
|
||||
'INTEGER' => self::FIELD_INTEGER,
|
||||
'BIGINT' => self::FIELD_INTEGER,
|
||||
'FLOAT' => self::FIELD_FLOAT,
|
||||
'DOUBLE' => self::FIELD_FLOAT,
|
||||
'REAL' => self::FIELD_FLOAT,
|
||||
'DECIMAL' => self::FIELD_FLOAT,
|
||||
'NUMERIC' => self::FIELD_FLOAT,
|
||||
);
|
||||
|
||||
$count = mysql_num_fields($this->resource);
|
||||
$this->meta = $this->convert = array();
|
||||
for ($index = 0; $index < $count; $index++) {
|
||||
|
||||
$info['native'] = $native = strtoupper(mysql_field_type($this->resource, $index));
|
||||
$info['flags'] = explode(' ', mysql_field_flags($this->resource, $index));
|
||||
$info['length'] = mysql_field_len($this->resource, $index);
|
||||
$info['table'] = mysql_field_table($this->resource, $index);
|
||||
|
||||
if (in_array('auto_increment', $info['flags'])) // or 'primary_key' ?
|
||||
$info['type'] = self::FIELD_COUNTER;
|
||||
else {
|
||||
$info['type'] = isset($types[$native]) ? $types[$native] : self::FIELD_UNKNOWN;
|
||||
|
||||
// if ($info['type'] == self::FIELD_TEXT && $info['length'] > 255)
|
||||
// $info['type'] = self::FIELD_LONG_TEXT;
|
||||
}
|
||||
|
||||
$name = mysql_field_name($this->resource, $index);
|
||||
$this->meta[$name] = $info;
|
||||
$this->convert[$name] = $info['type'];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
} // class DibiMySqlResult
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
?>
|
289
dibi/drivers/mysqli.php
Normal file
289
dibi/drivers/mysqli.php
Normal file
@@ -0,0 +1,289 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* dibi - Database Abstraction Layer according to dgx
|
||||
* --------------------------------------------------
|
||||
*
|
||||
* This source file is subject to the GNU GPL license.
|
||||
*
|
||||
* @author David Grudl aka -dgx- <dave@dgx.cz>
|
||||
* @link http://texy.info/dibi/
|
||||
* @copyright Copyright (c) 2005-2006 David Grudl
|
||||
* @license GNU GENERAL PUBLIC LICENSE
|
||||
* @package dibi
|
||||
* @category Database
|
||||
* @version 0.5b (2006-05-31) for PHP5
|
||||
*/
|
||||
|
||||
|
||||
// security - include dibi.php, not this file
|
||||
if (!defined('dibi')) die();
|
||||
|
||||
|
||||
/**
|
||||
* The dibi driver for MySQLi database
|
||||
*
|
||||
*/
|
||||
class DibiMySqliDriver extends DibiDriver {
|
||||
private
|
||||
$conn,
|
||||
$insertId = FALSE,
|
||||
$affectedRows = FALSE;
|
||||
|
||||
public
|
||||
$formats = array(
|
||||
'NULL' => "NULL",
|
||||
'TRUE' => "1",
|
||||
'FALSE' => "0",
|
||||
'date' => "'Y-m-d'",
|
||||
'datetime' => "'Y-m-d H:i:s'",
|
||||
);
|
||||
|
||||
|
||||
|
||||
public static function connect($config)
|
||||
{
|
||||
if (!extension_loaded('mysqli'))
|
||||
return new DibiException("PHP extension 'mysqli' is not loaded");
|
||||
|
||||
if (empty($config['host'])) $config['host'] = 'localhost';
|
||||
|
||||
$conn = @mysqli_connect($config['host'], @$config['username'], @$config['password'], @$config['database'], @$config['port']);
|
||||
|
||||
if (!$conn)
|
||||
return new DibiException("Connecting error", array(
|
||||
'message' => mysqli_connect_error(),
|
||||
'code' => mysqli_connect_errno(),
|
||||
));
|
||||
|
||||
if (!empty($config['charset']))
|
||||
mysqli_query($conn, 'SET CHARACTER SET '.$config['charset']);
|
||||
|
||||
$obj = new self($config);
|
||||
$obj->conn = $conn;
|
||||
return $obj;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function query($sql)
|
||||
{
|
||||
$this->insertId = $this->affectedRows = FALSE;
|
||||
$res = @mysqli_query($this->conn, $sql);
|
||||
|
||||
if (is_object($res))
|
||||
return new DibiMySqliResult($res);
|
||||
|
||||
if ($res === FALSE)
|
||||
return new DibiException("Query error", $this->errorInfo($sql));
|
||||
|
||||
$this->affectedRows = mysqli_affected_rows($this->conn);
|
||||
if ($this->affectedRows < 0) $this->affectedRows = FALSE;
|
||||
|
||||
$this->insertId = mysqli_insert_id($this->conn);
|
||||
if ($this->insertId < 1) $this->insertId = FALSE;
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
||||
public function affectedRows()
|
||||
{
|
||||
return $this->affectedRows;
|
||||
}
|
||||
|
||||
|
||||
public function insertId()
|
||||
{
|
||||
return $this->insertId;
|
||||
}
|
||||
|
||||
|
||||
public function begin()
|
||||
{
|
||||
return mysqli_autocommit($this->conn, FALSE);
|
||||
}
|
||||
|
||||
|
||||
public function commit()
|
||||
{
|
||||
$ok = mysqli_commit($this->conn);
|
||||
mysqli_autocommit($this->conn, TRUE);
|
||||
return $ok;
|
||||
}
|
||||
|
||||
|
||||
public function rollback()
|
||||
{
|
||||
$ok = mysqli_rollback($this->conn);
|
||||
mysqli_autocommit($this->conn, TRUE);
|
||||
return $ok;
|
||||
}
|
||||
|
||||
|
||||
private function errorInfo($sql = NULL)
|
||||
{
|
||||
return array(
|
||||
'message' => mysqli_error($this->conn),
|
||||
'code' => mysqli_errno($this->conn),
|
||||
'sql' => $sql,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public function escape($value, $appendQuotes = FALSE)
|
||||
{
|
||||
return $appendQuotes
|
||||
? "'" . mysqli_real_escape_string($this->conn, $value) . "'"
|
||||
: mysqli_real_escape_string($this->conn, $value);
|
||||
}
|
||||
|
||||
|
||||
public function quoteName($value)
|
||||
{
|
||||
return '`' . strtr($value, array('.' => '`.`')) . '`';
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function getMetaData()
|
||||
{
|
||||
trigger_error('Meta is not implemented yet.', E_USER_WARNING);
|
||||
}
|
||||
|
||||
|
||||
} // class DibiMySqliDriver
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
class DibiMySqliResult extends DibiResult
|
||||
{
|
||||
private
|
||||
$resource,
|
||||
$meta;
|
||||
|
||||
|
||||
public function __construct($resource)
|
||||
{
|
||||
$this->resource = $resource;
|
||||
}
|
||||
|
||||
|
||||
public function rowCount()
|
||||
{
|
||||
return mysqli_num_rows($this->resource);
|
||||
}
|
||||
|
||||
|
||||
protected function doFetch()
|
||||
{
|
||||
return mysqli_fetch_assoc($this->resource);
|
||||
}
|
||||
|
||||
|
||||
public function seek($row)
|
||||
{
|
||||
return mysqli_data_seek($this->resource, $row);
|
||||
}
|
||||
|
||||
|
||||
protected function free()
|
||||
{
|
||||
mysqli_free_result($this->resource);
|
||||
}
|
||||
|
||||
|
||||
public function getFields()
|
||||
{
|
||||
// cache
|
||||
if ($this->meta === NULL)
|
||||
$this->createMeta();
|
||||
|
||||
return array_keys($this->meta);
|
||||
}
|
||||
|
||||
|
||||
protected function detectTypes()
|
||||
{
|
||||
if ($this->meta === NULL)
|
||||
$this->createMeta();
|
||||
}
|
||||
|
||||
/** this is experimental */
|
||||
public function getMetaData($field)
|
||||
{
|
||||
// cache
|
||||
if ($this->meta === NULL)
|
||||
$this->createMeta();
|
||||
|
||||
return isset($this->meta[$field]) ? $this->meta[$field] : FALSE;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/** this is experimental */
|
||||
private function createMeta()
|
||||
{
|
||||
static $types = array(
|
||||
MYSQLI_TYPE_FLOAT => self::FIELD_FLOAT,
|
||||
MYSQLI_TYPE_DOUBLE => self::FIELD_FLOAT,
|
||||
MYSQLI_TYPE_DECIMAL => self::FIELD_FLOAT,
|
||||
// MYSQLI_TYPE_NEWDECIMAL=> self::FIELD_FLOAT,
|
||||
// MYSQLI_TYPE_BIT => self::FIELD_INTEGER,
|
||||
MYSQLI_TYPE_TINY => self::FIELD_INTEGER,
|
||||
MYSQLI_TYPE_SHORT => self::FIELD_INTEGER,
|
||||
MYSQLI_TYPE_LONG => self::FIELD_INTEGER,
|
||||
MYSQLI_TYPE_LONGLONG => self::FIELD_INTEGER,
|
||||
MYSQLI_TYPE_INT24 => self::FIELD_INTEGER,
|
||||
MYSQLI_TYPE_YEAR => self::FIELD_INTEGER,
|
||||
MYSQLI_TYPE_GEOMETRY => self::FIELD_INTEGER,
|
||||
MYSQLI_TYPE_DATE => self::FIELD_DATE,
|
||||
MYSQLI_TYPE_NEWDATE => self::FIELD_DATE,
|
||||
MYSQLI_TYPE_TIMESTAMP => self::FIELD_DATETIME,
|
||||
MYSQLI_TYPE_TIME => self::FIELD_DATETIME,
|
||||
MYSQLI_TYPE_DATETIME => self::FIELD_DATETIME,
|
||||
MYSQLI_TYPE_ENUM => self::FIELD_TEXT, // eventually self::FIELD_INTEGER
|
||||
MYSQLI_TYPE_SET => self::FIELD_TEXT, // eventually self::FIELD_INTEGER
|
||||
MYSQLI_TYPE_STRING => self::FIELD_TEXT,
|
||||
MYSQLI_TYPE_VAR_STRING=> self::FIELD_TEXT,
|
||||
MYSQLI_TYPE_TINY_BLOB => self::FIELD_BINARY,
|
||||
MYSQLI_TYPE_MEDIUM_BLOB=> self::FIELD_BINARY,
|
||||
MYSQLI_TYPE_LONG_BLOB => self::FIELD_BINARY,
|
||||
MYSQLI_TYPE_BLOB => self::FIELD_BINARY,
|
||||
);
|
||||
|
||||
$count = mysqli_num_fields($this->resource);
|
||||
$this->meta = $this->convert = array();
|
||||
for ($index = 0; $index < $count; $index++) {
|
||||
$info = (array) mysqli_fetch_field_direct($this->resource, $index);
|
||||
$native = $info['native'] = $info['type'];
|
||||
|
||||
if ($info['flags'] & MYSQLI_AUTO_INCREMENT_FLAG) // or 'primary_key' ?
|
||||
$info['type'] = self::FIELD_COUNTER;
|
||||
else {
|
||||
$info['type'] = isset($types[$native]) ? $types[$native] : self::FIELD_UNKNOWN;
|
||||
// if ($info['type'] == self::FIELD_TEXT && $info['length'] > 255)
|
||||
// $info['type'] = self::FIELD_LONG_TEXT;
|
||||
}
|
||||
|
||||
$this->meta[$info['name']] = $info;
|
||||
$this->convert[$info['name']] = $info['type'];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
} // class DibiMySqliResult
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
?>
|
278
dibi/drivers/odbc.php
Normal file
278
dibi/drivers/odbc.php
Normal file
@@ -0,0 +1,278 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* dibi - Database Abstraction Layer according to dgx
|
||||
* --------------------------------------------------
|
||||
*
|
||||
* This source file is subject to the GNU GPL license.
|
||||
*
|
||||
* @author David Grudl aka -dgx- <dave@dgx.cz>
|
||||
* @link http://texy.info/dibi/
|
||||
* @copyright Copyright (c) 2005-2006 David Grudl
|
||||
* @license GNU GENERAL PUBLIC LICENSE
|
||||
* @package dibi
|
||||
* @category Database
|
||||
* @version 0.5b (2006-05-31) for PHP5
|
||||
*/
|
||||
|
||||
|
||||
// security - include dibi.php, not this file
|
||||
if (!defined('dibi')) die();
|
||||
|
||||
|
||||
/**
|
||||
* The dibi driver interacting with databases via ODBC connections
|
||||
*
|
||||
*/
|
||||
class DibiOdbcDriver extends DibiDriver {
|
||||
private
|
||||
$conn,
|
||||
$affectedRows = FALSE;
|
||||
|
||||
public
|
||||
$formats = array(
|
||||
'NULL' => "NULL",
|
||||
'TRUE' => "-1",
|
||||
'FALSE' => "0",
|
||||
'date' => "#m/d/Y#",
|
||||
'datetime' => "#m/d/Y H:i:s#",
|
||||
);
|
||||
|
||||
|
||||
|
||||
public static function connect($config)
|
||||
{
|
||||
if (!extension_loaded('odbc'))
|
||||
return new DibiException("PHP extension 'odbc' is not loaded");
|
||||
|
||||
if (@$config['persistent'])
|
||||
$conn = @odbc_pconnect($config['database'], $config['username'], $config['password']);
|
||||
else
|
||||
$conn = @odbc_connect($config['database'], $config['username'], $config['password']);
|
||||
|
||||
if (!is_resource($conn))
|
||||
return new DibiException("Connecting error", array(
|
||||
'message' => odbc_errormsg(),
|
||||
'code' => odbc_error(),
|
||||
));
|
||||
|
||||
$obj = new self($config);
|
||||
$obj->conn = $conn;
|
||||
return $obj;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function query($sql)
|
||||
{
|
||||
$this->affectedRows = FALSE;
|
||||
|
||||
$res = @odbc_exec($this->conn, $sql);
|
||||
|
||||
if (is_resource($res))
|
||||
return new DibiOdbcResult($res);
|
||||
|
||||
if ($res === FALSE)
|
||||
return new DibiException("Query error", $this->errorInfo($sql));
|
||||
|
||||
$this->affectedRows = odbc_num_rows($this->conn);
|
||||
if ($this->affectedRows < 0) $this->affectedRows = FALSE;
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
||||
public function affectedRows()
|
||||
{
|
||||
return $this->affectedRows;
|
||||
}
|
||||
|
||||
|
||||
public function insertId()
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
|
||||
public function begin()
|
||||
{
|
||||
return odbc_autocommit($this->conn, FALSE);
|
||||
}
|
||||
|
||||
|
||||
public function commit()
|
||||
{
|
||||
$ok = odbc_commit($this->conn);
|
||||
odbc_autocommit($this->conn, TRUE);
|
||||
return $ok;
|
||||
}
|
||||
|
||||
|
||||
public function rollback()
|
||||
{
|
||||
$ok = odbc_rollback($this->conn);
|
||||
odbc_autocommit($this->conn, TRUE);
|
||||
return $ok;
|
||||
}
|
||||
|
||||
|
||||
private function errorInfo($sql = NULL)
|
||||
{
|
||||
return array(
|
||||
'message' => odbc_errormsg($this->conn),
|
||||
'code' => odbc_error($this->conn),
|
||||
'sql' => $sql,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function escape($value, $appendQuotes = FALSE)
|
||||
{
|
||||
$value = str_replace("'", "''", $value);
|
||||
return $appendQuotes
|
||||
? "'" . $value . "'"
|
||||
: $value;
|
||||
}
|
||||
|
||||
|
||||
public function quoteName($value)
|
||||
{
|
||||
return '[' . strtr($value, array('.' => '].[')) . ']';
|
||||
}
|
||||
|
||||
|
||||
public function getMetaData()
|
||||
{
|
||||
trigger_error('Meta is not implemented yet.', E_USER_WARNING);
|
||||
}
|
||||
|
||||
} // class DibiOdbcDriver
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
class DibiOdbcResult extends DibiResult
|
||||
{
|
||||
private
|
||||
$resource,
|
||||
$meta,
|
||||
$row = 0;
|
||||
|
||||
|
||||
public function __construct($resource)
|
||||
{
|
||||
$this->resource = $resource;
|
||||
}
|
||||
|
||||
|
||||
public function rowCount()
|
||||
{
|
||||
// will return -1 with many drivers :-(
|
||||
return odbc_num_rows($this->resource);
|
||||
}
|
||||
|
||||
|
||||
protected function doFetch()
|
||||
{
|
||||
return odbc_fetch_array($this->resource, $this->row++);
|
||||
}
|
||||
|
||||
|
||||
public function seek($row)
|
||||
{
|
||||
$this->row = $row;
|
||||
}
|
||||
|
||||
|
||||
protected function free()
|
||||
{
|
||||
odbc_free_result($this->resource);
|
||||
}
|
||||
|
||||
|
||||
public function getFields()
|
||||
{
|
||||
// cache
|
||||
if ($this->meta === NULL)
|
||||
$this->createMeta();
|
||||
|
||||
return array_keys($this->meta);
|
||||
}
|
||||
|
||||
|
||||
protected function detectTypes()
|
||||
{
|
||||
if ($this->meta === NULL)
|
||||
$this->createMeta();
|
||||
}
|
||||
|
||||
|
||||
/** this is experimental */
|
||||
public function getMetaData($field)
|
||||
{
|
||||
// cache
|
||||
if ($this->meta === NULL)
|
||||
$this->createMeta();
|
||||
|
||||
return isset($this->meta[$field]) ? $this->meta[$field] : FALSE;
|
||||
}
|
||||
|
||||
|
||||
/** this is experimental */
|
||||
private function createMeta()
|
||||
{
|
||||
// cache
|
||||
if ($this->meta !== NULL)
|
||||
return $this->meta;
|
||||
|
||||
static $types = array(
|
||||
'CHAR' => self::FIELD_TEXT,
|
||||
'COUNTER' => self::FIELD_COUNTER,
|
||||
'VARCHAR' => self::FIELD_TEXT,
|
||||
'LONGCHAR' => self::FIELD_TEXT,
|
||||
'INTEGER' => self::FIELD_INTEGER,
|
||||
'DATETIME' => self::FIELD_DATETIME,
|
||||
'CURRENCY' => self::FIELD_FLOAT,
|
||||
'BIT' => self::FIELD_BOOL,
|
||||
'LONGBINARY'=> self::FIELD_BINARY,
|
||||
'SMALLINT' => self::FIELD_INTEGER,
|
||||
'BYTE' => self::FIELD_INTEGER,
|
||||
'BIGINT' => self::FIELD_INTEGER,
|
||||
'INT' => self::FIELD_INTEGER,
|
||||
'TINYINT' => self::FIELD_INTEGER,
|
||||
'REAL' => self::FIELD_FLOAT,
|
||||
'DOUBLE' => self::FIELD_FLOAT,
|
||||
'DECIMAL' => self::FIELD_FLOAT,
|
||||
'NUMERIC' => self::FIELD_FLOAT,
|
||||
'MONEY' => self::FIELD_FLOAT,
|
||||
'SMALLMONEY'=> self::FIELD_FLOAT,
|
||||
'FLOAT' => self::FIELD_FLOAT,
|
||||
'YESNO' => self::FIELD_BOOL,
|
||||
// and many others?
|
||||
);
|
||||
|
||||
$count = odbc_num_fields($this->resource);
|
||||
$this->meta = $this->convert = array();
|
||||
for ($index = 1; $index <= $count; $index++) {
|
||||
$native = strtoupper(odbc_field_type($this->resource, $index));
|
||||
$name = odbc_field_name($this->resource, $index);
|
||||
$this->meta[$name] = array(
|
||||
'type' => isset($types[$native]) ? $types[$native] : self::FIELD_UNKNOWN,
|
||||
'native' => $native,
|
||||
'length' => odbc_field_len($this->resource, $index),
|
||||
'scale' => odbc_field_scale($this->resource, $index),
|
||||
'precision' => odbc_field_precision($this->resource, $index),
|
||||
);
|
||||
$this->convert[$name] = $this->meta[$name]['type'];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
} // class DibiOdbcResult
|
||||
|
||||
|
||||
?>
|
241
dibi/drivers/sqlite.php
Normal file
241
dibi/drivers/sqlite.php
Normal file
@@ -0,0 +1,241 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* dibi - Database Abstraction Layer according to dgx
|
||||
* --------------------------------------------------
|
||||
*
|
||||
* This source file is subject to the GNU GPL license.
|
||||
*
|
||||
* @author David Grudl aka -dgx- <dave@dgx.cz>
|
||||
* @link http://texy.info/dibi/
|
||||
* @copyright Copyright (c) 2005-2006 David Grudl
|
||||
* @license GNU GENERAL PUBLIC LICENSE
|
||||
* @package dibi
|
||||
* @category Database
|
||||
* @version 0.5b (2006-05-31) for PHP5
|
||||
*/
|
||||
|
||||
|
||||
// security - include dibi.php, not this file
|
||||
if (!defined('dibi')) die();
|
||||
|
||||
|
||||
/**
|
||||
* The dibi driver for SQlite database
|
||||
*
|
||||
*/
|
||||
class DibiSqliteDriver extends DibiDriver {
|
||||
private
|
||||
$conn,
|
||||
$insertId = FALSE,
|
||||
$affectedRows = FALSE;
|
||||
|
||||
public
|
||||
$formats = array(
|
||||
'NULL' => "NULL",
|
||||
'TRUE' => "1",
|
||||
'FALSE' => "0",
|
||||
'date' => "'Y-m-d'",
|
||||
'datetime' => "'Y-m-d H:i:s'",
|
||||
);
|
||||
|
||||
|
||||
|
||||
public static function connect($config)
|
||||
{
|
||||
if (!extension_loaded('sqlite'))
|
||||
return new DibiException("PHP extension 'sqlite' is not loaded");
|
||||
|
||||
if (empty($config['database']))
|
||||
return new DibiException("Database must be specified");
|
||||
|
||||
$errorMsg = '';
|
||||
if (empty($config['persistent']))
|
||||
$conn = @sqlite_open($config['database'], @$config['mode'], $errorMsg);
|
||||
else
|
||||
$conn = @sqlite_popen($config['database'], @$config['mode'], $errorMsg);
|
||||
|
||||
if (!$conn)
|
||||
return new DibiException("Connecting error", array(
|
||||
'message' => $errorMsg,
|
||||
));
|
||||
|
||||
$obj = new self($config);
|
||||
$obj->conn = $conn;
|
||||
return $obj;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function query($sql)
|
||||
{
|
||||
$this->insertId = $this->affectedRows = FALSE;
|
||||
|
||||
$errorMsg = '';
|
||||
$res = @sqlite_query($this->conn, $sql, SQLITE_ASSOC, $errorMsg);
|
||||
|
||||
if ($res === FALSE)
|
||||
return new DibiException("Query error", array(
|
||||
'message' => $errorMsg,
|
||||
'sql' => $sql,
|
||||
));
|
||||
|
||||
if (is_resource($res))
|
||||
return new DibiSqliteResult($res);
|
||||
|
||||
$this->affectedRows = sqlite_changes($this->conn);
|
||||
if ($this->affectedRows < 0) $this->affectedRows = FALSE;
|
||||
|
||||
$this->insertId = sqlite_last_insert_rowid($this->conn);
|
||||
if ($this->insertId < 1) $this->insertId = FALSE;
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
||||
public function affectedRows()
|
||||
{
|
||||
return $this->affectedRows;
|
||||
}
|
||||
|
||||
|
||||
public function insertId()
|
||||
{
|
||||
return $this->insertId;
|
||||
}
|
||||
|
||||
|
||||
public function begin()
|
||||
{
|
||||
return sqlite_query($this->conn, 'BEGIN');
|
||||
}
|
||||
|
||||
|
||||
public function commit()
|
||||
{
|
||||
return sqlite_query($this->conn, 'COMMIT');
|
||||
}
|
||||
|
||||
|
||||
public function rollback()
|
||||
{
|
||||
return sqlite_query($this->conn, 'ROLLBACK');
|
||||
}
|
||||
|
||||
|
||||
public function escape($value, $appendQuotes = FALSE)
|
||||
{
|
||||
return $appendQuotes
|
||||
? "'" . sqlite_escape_string($value) . "'"
|
||||
: sqlite_escape_string($value);
|
||||
}
|
||||
|
||||
|
||||
public function quoteName($value)
|
||||
{
|
||||
return $value;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function getMetaData()
|
||||
{
|
||||
trigger_error('Meta is not implemented yet.', E_USER_WARNING);
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // class DibiSqliteDriver
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
class DibiSqliteResult extends DibiResult
|
||||
{
|
||||
private
|
||||
$resource,
|
||||
$meta;
|
||||
|
||||
|
||||
public function __construct($resource)
|
||||
{
|
||||
$this->resource = $resource;
|
||||
}
|
||||
|
||||
|
||||
public function rowCount()
|
||||
{
|
||||
return sqlite_num_rows($this->resource);
|
||||
}
|
||||
|
||||
|
||||
protected function doFetch()
|
||||
{
|
||||
return sqlite_fetch_array($this->resource, SQLITE_ASSOC);
|
||||
}
|
||||
|
||||
|
||||
public function seek($row)
|
||||
{
|
||||
return sqlite_seek($this->resource, $row);
|
||||
}
|
||||
|
||||
|
||||
protected function free()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
public function getFields()
|
||||
{
|
||||
// cache
|
||||
if ($this->meta === NULL)
|
||||
$this->createMeta();
|
||||
|
||||
return array_keys($this->meta);
|
||||
}
|
||||
|
||||
|
||||
protected function detectTypes()
|
||||
{
|
||||
if ($this->meta === NULL)
|
||||
$this->createMeta();
|
||||
}
|
||||
|
||||
|
||||
/** this is experimental */
|
||||
public function getMetaData($field)
|
||||
{
|
||||
// cache
|
||||
if ($this->meta === NULL)
|
||||
$this->createMeta();
|
||||
|
||||
return isset($this->meta[$field]) ? $this->meta[$field] : FALSE;
|
||||
}
|
||||
|
||||
|
||||
/** this is experimental */
|
||||
private function createMeta()
|
||||
{
|
||||
$count = sqlite_num_fields($this->resource);
|
||||
$this->meta = $this->convert = array();
|
||||
for ($index = 0; $index < $count; $index++) {
|
||||
$name = sqlite_field_name($this->resource, $index);
|
||||
$this->meta[$name] = array('type' => self::FIELD_UNKNOWN);
|
||||
$this->convert[$name] = self::FIELD_UNKNOWN;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
} // class DibiSqliteResult
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
?>
|
101
dibi/libs/date.type.demo.php
Normal file
101
dibi/libs/date.type.demo.php
Normal file
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* dibi - Database Abstraction Layer according to dgx
|
||||
* --------------------------------------------------
|
||||
*
|
||||
* This source file is subject to the GNU GPL license.
|
||||
*
|
||||
* @author David Grudl aka -dgx- <dave@dgx.cz>
|
||||
* @link http://texy.info/dibi/
|
||||
* @copyright Copyright (c) 2005-2006 David Grudl
|
||||
* @license GNU GENERAL PUBLIC LICENSE
|
||||
* @package dibi
|
||||
* @category Database
|
||||
* @version 0.5b (2006-05-31) for PHP5
|
||||
*/
|
||||
|
||||
|
||||
// security - include dibi.php, not this file
|
||||
if (!defined('dibi')) die();
|
||||
|
||||
|
||||
// required since PHP 5.1.0
|
||||
// todo:
|
||||
if (function_exists('date_default_timezone_set'))
|
||||
date_default_timezone_set('Europe/Prague'); // or 'GMT'
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Pseudotype for UNIX timestamp representation
|
||||
*/
|
||||
class TDate implements IDibiVariable
|
||||
{
|
||||
/**
|
||||
* Unix timestamp
|
||||
* @var int
|
||||
*/
|
||||
protected $time;
|
||||
|
||||
|
||||
|
||||
public function __construct($time = NULL)
|
||||
{
|
||||
if ($time === NULL)
|
||||
$this->time = time(); // current time
|
||||
|
||||
elseif (is_string($time))
|
||||
$this->time = strtotime($time); // try convert to timestamp
|
||||
|
||||
else
|
||||
$this->time = (int) $time;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Format for SQL
|
||||
*
|
||||
* @param object destination DibiDriver
|
||||
* @param string optional modifier
|
||||
* @return string
|
||||
*/
|
||||
public function toSQL($driver, $modifier = NULL)
|
||||
{
|
||||
return date(
|
||||
$driver->formats['date'], // format according to driver's spec.
|
||||
$this->time
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function getTimeStamp()
|
||||
{
|
||||
return $this->time;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Pseudotype for datetime representation
|
||||
*/
|
||||
class TDateTime extends TDate
|
||||
{
|
||||
|
||||
public function toSQL($driver, $modifier = NULL)
|
||||
{
|
||||
return date(
|
||||
$driver->formats['datetime'], // format according to driver's spec.
|
||||
$this->time
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
164
dibi/libs/driver.php
Normal file
164
dibi/libs/driver.php
Normal file
@@ -0,0 +1,164 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* dibi - Database Abstraction Layer according to dgx
|
||||
* --------------------------------------------------
|
||||
*
|
||||
* This source file is subject to the GNU GPL license.
|
||||
*
|
||||
* @author David Grudl aka -dgx- <dave@dgx.cz>
|
||||
* @link http://texy.info/dibi/
|
||||
* @copyright Copyright (c) 2005-2006 David Grudl
|
||||
* @license GNU GENERAL PUBLIC LICENSE
|
||||
* @package dibi
|
||||
* @category Database
|
||||
* @version 0.5b (2006-05-31) for PHP5
|
||||
*/
|
||||
|
||||
|
||||
// security - include dibi.php, not this file
|
||||
if (!defined('dibi')) die();
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* dibi Common Driver
|
||||
*
|
||||
*/
|
||||
abstract class DibiDriver
|
||||
{
|
||||
/**
|
||||
* Current connection configuration
|
||||
* @var array
|
||||
*/
|
||||
protected
|
||||
$config;
|
||||
|
||||
/**
|
||||
* Describes how convert some datatypes to SQL command
|
||||
* @var array
|
||||
*/
|
||||
public $formats = array(
|
||||
'NULL' => "NULL", // NULL
|
||||
'TRUE' => "1", // boolean true
|
||||
'FALSE' => "0", // boolean false
|
||||
'date' => "'Y-m-d'", // format used by date()
|
||||
'datetime' => "'Y-m-d H:i:s'", // format used by date()
|
||||
);
|
||||
|
||||
|
||||
/**
|
||||
* DibiDriver factory: creates object and connects to a database
|
||||
*
|
||||
* @param array connect configuration
|
||||
* @return bool|object DibiDriver object on success, FALSE or Exception on failure
|
||||
*/
|
||||
abstract static public function connect($config);
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Driver initialization
|
||||
*
|
||||
* @param array connect configuration
|
||||
*/
|
||||
protected function __construct($config)
|
||||
{
|
||||
$this->config = $config;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the configuration descriptor used by connect() to connect to database.
|
||||
* @see connect()
|
||||
* @return array
|
||||
*/
|
||||
public function getConfig()
|
||||
{
|
||||
return $this->config;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Executes the SQL query
|
||||
*
|
||||
* @param string SQL statement.
|
||||
* @return object|bool Result set object or TRUE on success, Exception on failure
|
||||
*/
|
||||
abstract public function query($sql);
|
||||
|
||||
|
||||
/**
|
||||
* Gets the number of affected rows by the last INSERT, UPDATE or DELETE query
|
||||
*
|
||||
* @return int number of rows or FALSE on error
|
||||
*/
|
||||
abstract public function affectedRows();
|
||||
|
||||
|
||||
/**
|
||||
* Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query
|
||||
* @return int|bool int on success or FALSE on failure
|
||||
*/
|
||||
abstract public function insertId();
|
||||
|
||||
|
||||
/**
|
||||
* Begins a transaction (if supported).
|
||||
*/
|
||||
abstract public function begin();
|
||||
|
||||
|
||||
/**
|
||||
* Commits statements in a transaction.
|
||||
*/
|
||||
abstract public function commit();
|
||||
|
||||
|
||||
/**
|
||||
* Rollback changes in a transaction.
|
||||
*/
|
||||
abstract public function rollback();
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Escapes the string
|
||||
* @param string unescaped string
|
||||
* @param bool quote string?
|
||||
* @return string escaped and optionally quoted string
|
||||
*/
|
||||
abstract public function escape($value, $appendQuotes = FALSE);
|
||||
|
||||
|
||||
/**
|
||||
* Quotes SQL identifier (table's or column's name, etc.)
|
||||
* @param string identifier
|
||||
* @return string quoted identifier
|
||||
*/
|
||||
abstract public function quoteName($value);
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Gets a information of the current database.
|
||||
*
|
||||
* @return DibiMetaData
|
||||
*/
|
||||
abstract public function getMetaData();
|
||||
|
||||
|
||||
|
||||
|
||||
} // class DibiDriver
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
?>
|
67
dibi/libs/exception.php
Normal file
67
dibi/libs/exception.php
Normal file
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* dibi - Database Abstraction Layer according to dgx
|
||||
* --------------------------------------------------
|
||||
*
|
||||
* This source file is subject to the GNU GPL license.
|
||||
*
|
||||
* @author David Grudl aka -dgx- <dave@dgx.cz>
|
||||
* @link http://texy.info/dibi/
|
||||
* @copyright Copyright (c) 2005-2006 David Grudl
|
||||
* @license GNU GENERAL PUBLIC LICENSE
|
||||
* @package dibi
|
||||
* @category Database
|
||||
* @version 0.5b (2006-05-31) for PHP5
|
||||
*/
|
||||
|
||||
|
||||
// security - include dibi.php, not this file
|
||||
if (!defined('dibi')) die();
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* dibi exception class
|
||||
*
|
||||
*/
|
||||
class DibiException extends Exception
|
||||
{
|
||||
private
|
||||
$info;
|
||||
|
||||
|
||||
public function __construct($message, $info=NULL) {
|
||||
|
||||
$this->info = $info;
|
||||
|
||||
if (isset($info['message']))
|
||||
$message = "$message: $info[message]";
|
||||
/*
|
||||
if (isset($info['sql']))
|
||||
$message .= "\n[SQL] $info[sql]";
|
||||
*/
|
||||
parent::__construct($message);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function getSql()
|
||||
{
|
||||
return @$this->info['sql'];
|
||||
}
|
||||
|
||||
|
||||
} // class DibiException
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
function is_error($var)
|
||||
{
|
||||
return ($var === FALSE) || ($var instanceof Exception);
|
||||
}
|
||||
|
||||
|
||||
?>
|
295
dibi/libs/parser.php
Normal file
295
dibi/libs/parser.php
Normal file
@@ -0,0 +1,295 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* dibi - Database Abstraction Layer according to dgx
|
||||
* --------------------------------------------------
|
||||
*
|
||||
* This source file is subject to the GNU GPL license.
|
||||
*
|
||||
* @author David Grudl aka -dgx- <dave@dgx.cz>
|
||||
* @link http://texy.info/dibi/
|
||||
* @copyright Copyright (c) 2005-2006 David Grudl
|
||||
* @license GNU GENERAL PUBLIC LICENSE
|
||||
* @package dibi
|
||||
* @category Database
|
||||
* @version 0.5b (2006-05-31) for PHP5
|
||||
*/
|
||||
|
||||
|
||||
// security - include dibi.php, not this file
|
||||
if (!defined('dibi')) die();
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* dibi parser
|
||||
*
|
||||
*/
|
||||
class DibiParser
|
||||
{
|
||||
private
|
||||
$modifier,
|
||||
$hasError,
|
||||
$driver;
|
||||
|
||||
|
||||
/**
|
||||
* Generates SQL
|
||||
*
|
||||
* @param array
|
||||
* @return string
|
||||
*/
|
||||
public function parse($driver, $args)
|
||||
{
|
||||
$sql = '';
|
||||
$this->driver = $driver;
|
||||
$this->modifier = 0;
|
||||
$this->hasError = false;
|
||||
$command = null;
|
||||
$lastString = null;
|
||||
|
||||
foreach ($args as $index => $arg) {
|
||||
$sql .= ' '; // always add simple space
|
||||
|
||||
|
||||
// array processing (with or without modifier)
|
||||
if (is_array($arg)) {
|
||||
// determine type: set | values | list
|
||||
if ($this->modifier) {
|
||||
$type = $this->modifier;
|
||||
$this->modifier = false;
|
||||
} else {
|
||||
// autodetect
|
||||
if (is_int(key($arg)))
|
||||
$type = 'L'; // LIST
|
||||
else {
|
||||
if (!$command)
|
||||
$command = strtoupper(substr(ltrim($args[0]), 0, 6));
|
||||
|
||||
$type = $command == 'UPDATE' ? 'S' : 'V'; // SET | VALUES
|
||||
}
|
||||
}
|
||||
|
||||
// build array
|
||||
$vx = $kx = array();
|
||||
switch ($type) {
|
||||
case 'S': // SET
|
||||
foreach ($arg as $k => $v)
|
||||
$vx[] = $this->driver->quoteName($k) . '=' . $this->formatValue($v);
|
||||
|
||||
$sql .= implode(', ', $vx);
|
||||
break;
|
||||
|
||||
case 'V': // VALUES
|
||||
foreach ($arg as $k => $v) {
|
||||
$kx[] = $this->driver->quoteName($k);
|
||||
$vx[] = $this->formatValue($v);
|
||||
}
|
||||
|
||||
$sql .= '(' . implode(', ', $kx) . ') VALUES (' . implode(', ', $vx) . ')';
|
||||
break;
|
||||
|
||||
case 'L': // LIST
|
||||
foreach ($arg as $k => $v)
|
||||
$vx[] = $this->formatValue($v);
|
||||
|
||||
$sql .= implode(', ', $vx);
|
||||
break;
|
||||
|
||||
case 'N': // NAMES
|
||||
foreach ($arg as $v)
|
||||
$vx[] = $this->driver->quoteName($v);
|
||||
|
||||
$sql .= implode(', ', $vx);
|
||||
break;
|
||||
|
||||
default:
|
||||
$this->hasError = true;
|
||||
$sql .= "**Unknown modifier %$type**";
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// after-modifier procession
|
||||
if ($this->modifier) {
|
||||
if ($arg instanceof IDibiVariable) {
|
||||
$sql .= $arg->toSql($this->driver, $this->modifier);
|
||||
$this->modifier = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!is_scalar($arg) && !is_null($arg)) { // array is already processed
|
||||
$this->hasError = true;
|
||||
$this->modifier = false;
|
||||
$sql .= '**Unexpected '.gettype($arg).'**';
|
||||
continue;
|
||||
}
|
||||
|
||||
switch ($this->modifier) {
|
||||
case "s": // string
|
||||
$sql .= $this->driver->escape($arg, TRUE);
|
||||
break;
|
||||
case 'T': // date
|
||||
$sql .= date($this->driver->formats['date'], is_string($arg) ? strtotime($arg) : $arg);
|
||||
break;
|
||||
case 't': // datetime
|
||||
$sql .= date($this->driver->formats['datetime'], is_string($arg) ? strtotime($arg) : $arg);
|
||||
break;
|
||||
case 'b': // boolean
|
||||
$sql .= $arg ? $this->driver->formats['TRUE'] : $this->driver->formats['FALSE'];
|
||||
break;
|
||||
case 'i':
|
||||
case 'u': // unsigned int
|
||||
case 'd': // signed int
|
||||
$sql .= (string) (int) $arg;
|
||||
break;
|
||||
case 'f': // float
|
||||
$sql .= (string) (float) $arg; // something like -9E-005 is accepted by SQL
|
||||
break;
|
||||
case 'n': // identifier name
|
||||
$sql .= $this->driver->quoteName($arg);
|
||||
break;
|
||||
default:
|
||||
$this->hasError = true;
|
||||
$sql .= "**Unknown modifier %$this->modifier**";
|
||||
}
|
||||
|
||||
$this->modifier = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
// simple string means SQL
|
||||
if (is_string($arg)) {
|
||||
// double string warning
|
||||
// (problematic with dibi::queryStart & dibi::queryAdd
|
||||
// if ($lastString === $index-1)
|
||||
// trigger_error("Is seems there is error in SQL near '$arg'.", E_USER_WARNING);
|
||||
|
||||
$lastString = $index;
|
||||
|
||||
// speed-up - is regexp required?
|
||||
$toSkip = strcspn($arg, '`[\'"%');
|
||||
|
||||
if ($toSkip == strlen($arg)) {
|
||||
$sql .= $arg;
|
||||
} else {
|
||||
$sql .= substr($arg, 0, $toSkip)
|
||||
. preg_replace_callback('/
|
||||
(?=`|\[|\'|"|%) ## speed-up
|
||||
(?:
|
||||
`(.+?)`| ## 1) `identifier`
|
||||
\[(.+?)\]| ## 2) [identifier]
|
||||
(\')((?:\'\'|[^\'])*)\'| ## 3,4) string
|
||||
(")((?:""|[^"])*)"| ## 5,6) "string"
|
||||
%([a-zA-Z])$| ## 7) right modifier
|
||||
(\'|") ## 8) lone-quote
|
||||
)/xs',
|
||||
array($this, 'callback'),
|
||||
substr($arg, $toSkip)
|
||||
);
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
// default processing
|
||||
$sql .= $this->formatValue($arg);
|
||||
|
||||
} // for
|
||||
|
||||
|
||||
if ($this->hasError)
|
||||
return new DibiException('Errors during generating SQL', array('sql' => $sql));
|
||||
|
||||
return trim($sql);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
private function formatValue($value)
|
||||
{
|
||||
if (is_string($value))
|
||||
return $this->driver->escape($value, TRUE);
|
||||
|
||||
if (is_int($value) || is_float($value))
|
||||
return (string) $value; // something like -9E-005 is accepted by SQL
|
||||
|
||||
if (is_bool($value))
|
||||
return $value ? $this->driver->formats['TRUE'] : $this->driver->formats['FALSE'];
|
||||
|
||||
if (is_null($value))
|
||||
return $this->driver->formats['NULL'];
|
||||
|
||||
if ($value instanceof IDibiVariable)
|
||||
return $value->toSql($this->driver);
|
||||
|
||||
$this->hasError = true;
|
||||
return '**Unsupported type '.gettype($value).'**';
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* PREG callback for @see self::translate()
|
||||
* @param array
|
||||
* @return string
|
||||
*/
|
||||
private function callback($matches)
|
||||
{
|
||||
// [1] => `ident`
|
||||
// [2] => [ident]
|
||||
// [3] => '
|
||||
// [4] => string
|
||||
// [5] => "
|
||||
// [6] => string
|
||||
// [7] => right modifier
|
||||
// [8] => lone-quote
|
||||
|
||||
if ($matches[1]) // SQL identifiers: `ident`
|
||||
return $this->driver->quoteName($matches[1]);
|
||||
|
||||
if ($matches[2]) // SQL identifiers: [ident]
|
||||
return $this->driver->quoteName($matches[2]);
|
||||
|
||||
if ($matches[3]) // SQL strings: '....'
|
||||
return $this->driver->escape( strtr($matches[4], array("''" => "'")), true);
|
||||
|
||||
if ($matches[5]) // SQL strings: "..."
|
||||
return $this->driver->escape( strtr($matches[6], array('""' => '"')), true);
|
||||
|
||||
if ($matches[7]) { // modifier
|
||||
$this->modifier = $matches[7];
|
||||
return '';
|
||||
}
|
||||
|
||||
if ($matches[8]) { // string quote
|
||||
return '**Alone quote**';
|
||||
$this->hasError = true;
|
||||
}
|
||||
|
||||
die('this should be never executed');
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
} // class DibiParser
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
?>
|
396
dibi/libs/resultset.php
Normal file
396
dibi/libs/resultset.php
Normal file
@@ -0,0 +1,396 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* dibi - Database Abstraction Layer according to dgx
|
||||
* --------------------------------------------------
|
||||
*
|
||||
* This source file is subject to the GNU GPL license.
|
||||
*
|
||||
* @author David Grudl aka -dgx- <dave@dgx.cz>
|
||||
* @link http://texy.info/dibi/
|
||||
* @copyright Copyright (c) 2005-2006 David Grudl
|
||||
* @license GNU GENERAL PUBLIC LICENSE
|
||||
* @package dibi
|
||||
* @category Database
|
||||
* @version 0.5b (2006-05-31) for PHP5
|
||||
*/
|
||||
|
||||
|
||||
// security - include dibi.php, not this file
|
||||
if (!defined('dibi')) die();
|
||||
|
||||
|
||||
|
||||
// PHP < 5.1 compatibility
|
||||
if (!interface_exists('Countable', false)) {
|
||||
interface Countable
|
||||
{
|
||||
function count();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* dibi result-set abstract class
|
||||
*
|
||||
* <code>
|
||||
* $result = dibi::query('SELECT * FROM [table]');
|
||||
* $value = $result->fetchSingle();
|
||||
* $all = $result->fetchAll();
|
||||
* $assoc = $result->fetchAll('id');
|
||||
* $assoc = $result->fetchAll('active', 'id');
|
||||
* unset($result);
|
||||
* </code>
|
||||
*/
|
||||
abstract class DibiResult implements IteratorAggregate, Countable
|
||||
{
|
||||
/**
|
||||
* Column type in relation to PHP native type
|
||||
*/
|
||||
const
|
||||
FIELD_TEXT = 's', // as 'string'
|
||||
FIELD_BINARY = 'b',
|
||||
FIELD_BOOL = 'l', // as 'logical'
|
||||
FIELD_INTEGER = 'i',
|
||||
FIELD_FLOAT = 'f',
|
||||
FIELD_DATE = 'd',
|
||||
FIELD_DATETIME = 't',
|
||||
FIELD_UNKNOWN = '?',
|
||||
|
||||
// special
|
||||
FIELD_COUNTER = 'c'; // counter or autoincrement, is integer
|
||||
|
||||
|
||||
/**
|
||||
* Describes columns types
|
||||
* @var array
|
||||
*/
|
||||
protected $convert;
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Moves cursor position without fetching row
|
||||
* @param int the 0-based cursor pos to seek to
|
||||
* @return boolean TRUE on success, FALSE if unable to seek to specified record
|
||||
*/
|
||||
abstract public function seek($row);
|
||||
|
||||
/**
|
||||
* Returns the number of rows in a result set
|
||||
* @return int
|
||||
*/
|
||||
abstract public function rowCount();
|
||||
|
||||
/**
|
||||
* Gets an array of field names
|
||||
* @return array
|
||||
*/
|
||||
abstract public function getFields();
|
||||
|
||||
/**
|
||||
* Gets an array of meta informations about column
|
||||
* @param string column name
|
||||
* @return array
|
||||
*/
|
||||
abstract public function getMetaData($field);
|
||||
|
||||
/**
|
||||
* Acquires ....
|
||||
* @return void
|
||||
*/
|
||||
abstract protected function detectTypes();
|
||||
|
||||
/**
|
||||
* Frees the resources allocated for this result set
|
||||
* @return void
|
||||
*/
|
||||
abstract protected function free();
|
||||
|
||||
/**
|
||||
* Fetches the row at current position and moves the internal cursor to the next position
|
||||
* internal usage only
|
||||
* @return array|FALSE array() on success, FALSE if no next record
|
||||
*/
|
||||
abstract protected function doFetch();
|
||||
|
||||
|
||||
/**
|
||||
* Fetches the row at current position, process optional type conversion
|
||||
* and moves the internal cursor to the next position
|
||||
* @return array|FALSE array() on success, FALSE if no next record
|
||||
*/
|
||||
final public function fetch()
|
||||
{
|
||||
$rec = $this->doFetch();
|
||||
if (!is_array($rec))
|
||||
return FALSE;
|
||||
|
||||
// types-converting?
|
||||
if ($t = $this->convert) { // little speed-up
|
||||
foreach ($rec as $key => $value) {
|
||||
if (isset($t[$key]))
|
||||
$rec[$key] = $this->convert($value, $t[$key]);
|
||||
}
|
||||
}
|
||||
|
||||
return $rec;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Like fetch(), but returns only first field
|
||||
* @return mixed value on success, FALSE if no next record
|
||||
*/
|
||||
final function fetchSingle()
|
||||
{
|
||||
$rec = $this->doFetch();
|
||||
if (!is_array($rec))
|
||||
return FALSE;
|
||||
|
||||
// types-converting?
|
||||
if ($t = $this->convert) { // little speed-up
|
||||
$value = reset($rec);
|
||||
$key = key($rec);
|
||||
return isset($t[$key])
|
||||
? $this->convert($value, $t[$key])
|
||||
: $value;
|
||||
}
|
||||
|
||||
return reset($rec);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Fetches all records from table. Records , but returns only first field
|
||||
* @param string associative colum [, param, ... ]
|
||||
* @return array
|
||||
*/
|
||||
final function fetchAll()
|
||||
{
|
||||
@$this->seek(0);
|
||||
$rec = $this->fetch();
|
||||
if (!$rec)
|
||||
return array(); // empty resultset
|
||||
|
||||
$assocBy = func_get_args();
|
||||
$arr = array();
|
||||
|
||||
if (!$assocBy) { // no associative array
|
||||
$value = count($rec) == 1 ? key($rec) : NULL;
|
||||
do {
|
||||
$arr[] = $value === NULL ? $rec : $rec[$value];
|
||||
} while ($rec = $this->fetch());
|
||||
|
||||
return $arr;
|
||||
}
|
||||
|
||||
do { // make associative arrays
|
||||
foreach ($assocBy as $n => $assoc) {
|
||||
$val[$n] = $rec[$assoc];
|
||||
unset($rec[$assoc]);
|
||||
}
|
||||
|
||||
foreach ($assocBy as $n => $assoc) {
|
||||
if ($n == 0)
|
||||
$tmp = &$arr[ $val[$n] ];
|
||||
else
|
||||
$tmp = &$tmp[$assoc][ $val[$n] ];
|
||||
|
||||
if ($tmp === NULL)
|
||||
$tmp = $rec;
|
||||
}
|
||||
} while ($rec = $this->fetch());
|
||||
|
||||
return $arr;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Fetches all records from table like $key => $value pairs
|
||||
* @return array
|
||||
*/
|
||||
final function fetchPairs($key, $value)
|
||||
{
|
||||
@$this->seek(0);
|
||||
$rec = $this->fetch();
|
||||
if (!$rec)
|
||||
return array(); // empty resultset
|
||||
|
||||
$arr = array();
|
||||
do {
|
||||
$arr[ $rec[$key] ] = $rec[$value];
|
||||
} while ($rec = $this->fetch());
|
||||
|
||||
return $arr;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Automatically frees the resources allocated for this result set
|
||||
* @return void
|
||||
*/
|
||||
public function __destruct()
|
||||
{
|
||||
@$this->free();
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function setType($field, $type = NULL)
|
||||
{
|
||||
if ($field === TRUE)
|
||||
$this->detectTypes();
|
||||
|
||||
elseif (is_array($field))
|
||||
$this->convert = $field;
|
||||
|
||||
else
|
||||
$this->convert[$field] = $type;
|
||||
}
|
||||
|
||||
|
||||
/** is this needed? */
|
||||
public function getType($field)
|
||||
{
|
||||
return isset($this->convert[$field]) ? $this->convert[$field] : NULL;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function convert($value, $type)
|
||||
{
|
||||
if ($value === NULL || $value === FALSE)
|
||||
return $value;
|
||||
|
||||
static $conv = array(
|
||||
self::FIELD_TEXT => 'string',
|
||||
self::FIELD_BINARY => 'string',
|
||||
self::FIELD_BOOL => 'bool',
|
||||
self::FIELD_INTEGER => 'int',
|
||||
self::FIELD_FLOAT => 'float',
|
||||
self::FIELD_COUNTER => 'int',
|
||||
);
|
||||
|
||||
if (isset($conv[$type])) {
|
||||
settype($value, $conv[$type]);
|
||||
return $value;
|
||||
}
|
||||
|
||||
if ($type == self::FIELD_DATE)
|
||||
return new TDate($value); // !!! experimental
|
||||
|
||||
if ($type == self::FIELD_DATETIME)
|
||||
return new TDateTime($value); // !!! experimental
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
|
||||
/** these are the required IteratorAggregate functions */
|
||||
public function getIterator($offset = NULL, $count = NULL)
|
||||
{
|
||||
return new DibiResultIterator($this, $offset, $count);
|
||||
}
|
||||
/** end required IteratorAggregate functions */
|
||||
|
||||
|
||||
/** these are the required Countable functions */
|
||||
public function count()
|
||||
{
|
||||
return $this->rowCount();
|
||||
}
|
||||
/** end required Countable functions */
|
||||
|
||||
} // class DibiResult
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Basic Result set iterator.
|
||||
*
|
||||
* This can be returned by DibiResult::getIterator() method or directly using foreach:
|
||||
* <code>
|
||||
* $result = dibi::query('SELECT * FROM table');
|
||||
* foreach ($result as $fields) {
|
||||
* print_r($fields);
|
||||
* }
|
||||
* unset($result);
|
||||
* </code>
|
||||
*
|
||||
* Optionally you can specify offset and limit:
|
||||
* <code>
|
||||
* foreach ($result->getIterator(2, 3) as $fields) {
|
||||
* print_r($fields);
|
||||
* }
|
||||
* </code>
|
||||
*/
|
||||
class DibiResultIterator implements Iterator
|
||||
{
|
||||
private
|
||||
$result,
|
||||
$offset,
|
||||
$count,
|
||||
$record,
|
||||
$row;
|
||||
|
||||
|
||||
public function __construct(DibiResult $result, $offset = NULL, $count = NULL)
|
||||
{
|
||||
$this->result = $result;
|
||||
$this->offset = (int) $offset;
|
||||
$this->count = $count === NULL ? 2147483647 /*PHP_INT_MAX till 5.0.5 */ : (int) $count;
|
||||
}
|
||||
|
||||
|
||||
/** these are the required Iterator functions */
|
||||
public function rewind()
|
||||
{
|
||||
$this->row = 0;
|
||||
@$this->result->seek($this->offset);
|
||||
$this->record = $this->result->fetch();
|
||||
}
|
||||
|
||||
|
||||
public function key()
|
||||
{
|
||||
return $this->row;
|
||||
}
|
||||
|
||||
|
||||
public function current()
|
||||
{
|
||||
return $this->record;
|
||||
}
|
||||
|
||||
|
||||
public function next()
|
||||
{
|
||||
$this->record = $this->result->fetch();
|
||||
$this->row++;
|
||||
}
|
||||
|
||||
|
||||
public function valid()
|
||||
{
|
||||
return is_array($this->record) && ($this->row < $this->count);
|
||||
}
|
||||
/** end required Iterator functions */
|
||||
|
||||
|
||||
} // class DibiResultIterator
|
||||
|
||||
|
||||
|
||||
?>
|
4
examples/.gitignore
vendored
4
examples/.gitignore
vendored
@@ -1,4 +0,0 @@
|
||||
_test.bat
|
||||
ref
|
||||
output
|
||||
log
|
40
examples/connect.php
Normal file
40
examples/connect.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
require_once '../dibi/dibi.php';
|
||||
|
||||
// use two connections:
|
||||
|
||||
// first connection to mysql
|
||||
$state = dibi::connect(array(
|
||||
'driver' => 'mysql',
|
||||
'host' => 'localhost',
|
||||
'username' => 'root',
|
||||
'password' => '***',
|
||||
'database' => 'test',
|
||||
'charset' => 'utf8',
|
||||
), 1);
|
||||
|
||||
if ($state instanceof Exception) {
|
||||
echo $state;
|
||||
}
|
||||
|
||||
if (!dibi::isConnected()) {
|
||||
die();
|
||||
}
|
||||
|
||||
|
||||
|
||||
// second connection to odbc
|
||||
dibi::connect(array(
|
||||
'driver' => 'odbc',
|
||||
'username' => 'root',
|
||||
'password' => '***',
|
||||
'database' => 'Driver={Microsoft Access Driver (*.mdb)};Dbq=C:\\Database.mdb',
|
||||
), 3);
|
||||
|
||||
|
||||
echo dibi::isConnected();
|
||||
|
||||
|
||||
|
||||
?>
|
@@ -1,139 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
?>
|
||||
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
|
||||
|
||||
<h1>Connecting to Databases | Dibi</h1>
|
||||
|
||||
<?php
|
||||
|
||||
if (@!include __DIR__ . '/../vendor/autoload.php') {
|
||||
die('Install packages using `composer install`');
|
||||
}
|
||||
|
||||
|
||||
// connects to SQlite using Dibi class
|
||||
echo '<p>Connecting to Sqlite: ';
|
||||
try {
|
||||
dibi::connect([
|
||||
'driver' => 'sqlite',
|
||||
'database' => 'data/sample.s3db',
|
||||
]);
|
||||
echo 'OK';
|
||||
} catch (Dibi\Exception $e) {
|
||||
echo get_class($e), ': ', $e->getMessage(), "\n";
|
||||
}
|
||||
echo "</p>\n";
|
||||
|
||||
|
||||
// connects to SQlite using Dibi\Connection object
|
||||
echo '<p>Connecting to Sqlite: ';
|
||||
try {
|
||||
$connection = new Dibi\Connection([
|
||||
'driver' => 'sqlite',
|
||||
'database' => 'data/sample.s3db',
|
||||
]);
|
||||
echo 'OK';
|
||||
} catch (Dibi\Exception $e) {
|
||||
echo get_class($e), ': ', $e->getMessage(), "\n";
|
||||
}
|
||||
echo "</p>\n";
|
||||
|
||||
|
||||
// connects to MySQLi
|
||||
echo '<p>Connecting to MySQLi: ';
|
||||
try {
|
||||
dibi::connect([
|
||||
'driver' => 'mysqli',
|
||||
'host' => 'localhost',
|
||||
'username' => 'root',
|
||||
'password' => 'xxx',
|
||||
'database' => 'dibi',
|
||||
'options' => [
|
||||
MYSQLI_OPT_CONNECT_TIMEOUT => 30,
|
||||
],
|
||||
'flags' => MYSQLI_CLIENT_COMPRESS,
|
||||
]);
|
||||
echo 'OK';
|
||||
} catch (Dibi\Exception $e) {
|
||||
echo get_class($e), ': ', $e->getMessage(), "\n";
|
||||
}
|
||||
echo "</p>\n";
|
||||
|
||||
|
||||
// connects to ODBC
|
||||
echo '<p>Connecting to ODBC: ';
|
||||
try {
|
||||
dibi::connect([
|
||||
'driver' => 'odbc',
|
||||
'username' => 'root',
|
||||
'password' => '***',
|
||||
'dsn' => 'Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=' . __DIR__ . '/data/sample.mdb',
|
||||
]);
|
||||
echo 'OK';
|
||||
} catch (Dibi\Exception $e) {
|
||||
echo get_class($e), ': ', $e->getMessage(), "\n";
|
||||
}
|
||||
echo "</p>\n";
|
||||
|
||||
|
||||
// connects to PostgreSql
|
||||
echo '<p>Connecting to PostgreSql: ';
|
||||
try {
|
||||
dibi::connect([
|
||||
'driver' => 'postgre',
|
||||
'string' => 'host=localhost port=5432 dbname=mary',
|
||||
'persistent' => true,
|
||||
]);
|
||||
echo 'OK';
|
||||
} catch (Dibi\Exception $e) {
|
||||
echo get_class($e), ': ', $e->getMessage(), "\n";
|
||||
}
|
||||
echo "</p>\n";
|
||||
|
||||
|
||||
// connects to PDO
|
||||
echo '<p>Connecting to Sqlite via PDO: ';
|
||||
try {
|
||||
dibi::connect([
|
||||
'driver' => 'pdo',
|
||||
'dsn' => 'sqlite::memory:',
|
||||
]);
|
||||
echo 'OK';
|
||||
} catch (Dibi\Exception $e) {
|
||||
echo get_class($e), ': ', $e->getMessage(), "\n";
|
||||
}
|
||||
echo "</p>\n";
|
||||
|
||||
|
||||
// connects to SQLSRV
|
||||
echo '<p>Connecting to Microsoft SQL Server: ';
|
||||
try {
|
||||
dibi::connect([
|
||||
'driver' => 'sqlsrv',
|
||||
'host' => '(local)',
|
||||
'username' => 'Administrator',
|
||||
'password' => 'xxx',
|
||||
'database' => 'main',
|
||||
]);
|
||||
echo 'OK';
|
||||
} catch (Dibi\Exception $e) {
|
||||
echo get_class($e), ': ', $e->getMessage(), "\n";
|
||||
}
|
||||
echo "</p>\n";
|
||||
|
||||
|
||||
// connects to Oracle
|
||||
echo '<p>Connecting to Oracle: ';
|
||||
try {
|
||||
dibi::connect([
|
||||
'driver' => 'oracle',
|
||||
'username' => 'root',
|
||||
'password' => 'xxx',
|
||||
'database' => 'db',
|
||||
]);
|
||||
echo 'OK';
|
||||
} catch (Dibi\Exception $e) {
|
||||
echo get_class($e), ': ', $e->getMessage(), "\n";
|
||||
}
|
||||
echo "</p>\n";
|
Binary file not shown.
Before Width: | Height: | Size: 1.1 KiB |
Binary file not shown.
Before Width: | Height: | Size: 300 B |
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1,64 +0,0 @@
|
||||
body {
|
||||
font: 15px/1.5 Tahoma, Verdana, Myriad Web, Syntax, sans-serif;
|
||||
color: #333;
|
||||
background: #fff url('dibi-powered.gif') no-repeat 99% 1em;
|
||||
margin: 1.6em;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
h1, h2 {
|
||||
font-size: 210%;
|
||||
font-weight: normal;
|
||||
color: #036;
|
||||
}
|
||||
|
||||
h2 {
|
||||
font-size: 150%;
|
||||
}
|
||||
|
||||
a {
|
||||
color: #000080;
|
||||
}
|
||||
|
||||
table.dump {
|
||||
padding: 0;
|
||||
margin: 0;
|
||||
border-collapse:collapse;
|
||||
}
|
||||
|
||||
table.dump td, table.dump th {
|
||||
color: #505767;
|
||||
background: #fff;
|
||||
border: 1px solid #d1cdab;
|
||||
padding: 6px 6px 6px 12px;
|
||||
text-align: left;
|
||||
}
|
||||
|
||||
table.dump th {
|
||||
font-size: 80%;
|
||||
color: #525b37;
|
||||
background: #e3e9ba;
|
||||
}
|
||||
|
||||
/* dump() */
|
||||
pre.tracy-dump, pre.dump {
|
||||
color: #444; background: white;
|
||||
border: 1px solid silver;
|
||||
padding: 1em;
|
||||
margin: 1em 0;
|
||||
}
|
||||
pre.tracy-dump .php-array, pre.tracy-dump .php-object {
|
||||
color: #C22;
|
||||
}
|
||||
pre.tracy-dump .php-string {
|
||||
color: #080;
|
||||
}
|
||||
pre.tracy-dump .php-int, pre.tracy-dump .php-float {
|
||||
color: #37D;
|
||||
}
|
||||
pre.tracy-dump .php-null, pre.tracy-dump .php-bool {
|
||||
color: black;
|
||||
}
|
||||
pre.tracy-dump .php-visibility {
|
||||
font-size: 85%; color: #999;
|
||||
}
|
@@ -1,54 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
?>
|
||||
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
|
||||
|
||||
<h1>Database Reflection | Dibi</h1>
|
||||
|
||||
<?php
|
||||
|
||||
if (@!include __DIR__ . '/../vendor/autoload.php') {
|
||||
die('Install packages using `composer install`');
|
||||
}
|
||||
|
||||
|
||||
$dibi = new Dibi\Connection([
|
||||
'driver' => 'sqlite',
|
||||
'database' => 'data/sample.s3db',
|
||||
]);
|
||||
|
||||
|
||||
// retrieve database reflection
|
||||
$database = $dibi->getDatabaseInfo();
|
||||
|
||||
echo "<h2>Database '{$database->getName()}'</h2>\n";
|
||||
echo "<ul>\n";
|
||||
foreach ($database->getTables() as $table) {
|
||||
echo '<li>', ($table->isView() ? 'view' : 'table') . " {$table->getName()}</li>\n";
|
||||
}
|
||||
echo "</ul>\n";
|
||||
|
||||
|
||||
// table reflection
|
||||
$table = $database->getTable('products');
|
||||
|
||||
echo "<h2>Table '{$table->getName()}'</h2>\n";
|
||||
|
||||
echo "Columns\n";
|
||||
echo "<ul>\n";
|
||||
foreach ($table->getColumns() as $column) {
|
||||
echo "<li>{$column->getName()} <i>{$column->getNativeType()}</i> <code>{$column->getDefault()}</code></li>\n";
|
||||
}
|
||||
echo "</ul>\n";
|
||||
|
||||
|
||||
echo 'Indexes';
|
||||
echo "<ul>\n";
|
||||
foreach ($table->getIndexes() as $index) {
|
||||
echo "<li>{$index->getName()} " . ($index->isPrimary() ? 'primary ' : '') . ($index->isUnique() ? 'unique' : '') . ' (';
|
||||
foreach ($index->getColumns() as $column) {
|
||||
echo $column->getName(), ', ';
|
||||
}
|
||||
echo ")</li>\n";
|
||||
}
|
||||
echo "</ul>\n";
|
@@ -1,37 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
?>
|
||||
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
|
||||
|
||||
<h1>Dumping SQL and Result Set | Dibi</h1>
|
||||
|
||||
<?php
|
||||
|
||||
if (@!include __DIR__ . '/../vendor/autoload.php') {
|
||||
die('Install packages using `composer install`');
|
||||
}
|
||||
|
||||
|
||||
$dibi = new Dibi\Connection([
|
||||
'driver' => 'sqlite',
|
||||
'database' => 'data/sample.s3db',
|
||||
]);
|
||||
|
||||
|
||||
$res = $dibi->query('
|
||||
SELECT * FROM products
|
||||
INNER JOIN orders USING (product_id)
|
||||
INNER JOIN customers USING (customer_id)
|
||||
');
|
||||
|
||||
|
||||
echo '<h2>dibi::dump()</h2>';
|
||||
|
||||
// dump last query (dibi::$sql)
|
||||
dibi::dump();
|
||||
|
||||
|
||||
// dump result table
|
||||
echo '<h2>Dibi\Result::dump()</h2>';
|
||||
|
||||
$res->dump();
|
52
examples/fetch.php
Normal file
52
examples/fetch.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<pre>
|
||||
<?php
|
||||
|
||||
require_once '../dibi/dibi.php';
|
||||
|
||||
dibi::$debug = true;
|
||||
|
||||
// mysql
|
||||
dibi::connect(array(
|
||||
'driver' => 'mysqli',
|
||||
'host' => 'localhost',
|
||||
'username' => 'root',
|
||||
'password' => '***',
|
||||
'database' => 'test',
|
||||
'charset' => 'utf8',
|
||||
));
|
||||
|
||||
|
||||
if (!dibi::isConnected())
|
||||
die('Not connected');
|
||||
|
||||
|
||||
$res = dibi::query('SELECT * FROM table');
|
||||
|
||||
// fetch a single value
|
||||
$value = $res->fetchSingle();
|
||||
|
||||
// fetch complete result set
|
||||
$all = $res->fetchAll();
|
||||
|
||||
// fetch complete result set like association array
|
||||
$assoc = $res->fetchAll('id');
|
||||
|
||||
$assoc = $res->fetchAll('id', 'id2');
|
||||
|
||||
// fetch complete result set like pairs key => value
|
||||
$pairs = $res->fetchPairs('id', 'name');
|
||||
|
||||
|
||||
// fetch row by row
|
||||
foreach ($res as $row => $fields) {
|
||||
print_r($fields);
|
||||
}
|
||||
|
||||
// fetch row by row with defined offset and limit
|
||||
foreach ($res->getIterator(2, 3) as $row => $fields) {
|
||||
print_r($fields);
|
||||
}
|
||||
|
||||
|
||||
|
||||
?>
|
@@ -1,95 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
if (@!include __DIR__ . '/../vendor/autoload.php') {
|
||||
die('Install dependencies using `composer install --dev`');
|
||||
}
|
||||
|
||||
Tracy\Debugger::enable();
|
||||
|
||||
?>
|
||||
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
|
||||
|
||||
<h1>Fetching Examples | Dibi</h1>
|
||||
|
||||
<?php
|
||||
|
||||
$dibi = new Dibi\Connection([
|
||||
'driver' => 'sqlite',
|
||||
'database' => 'data/sample.s3db',
|
||||
]);
|
||||
|
||||
|
||||
/*
|
||||
TABLE products
|
||||
|
||||
product_id | title
|
||||
-----------+----------
|
||||
1 | Chair
|
||||
2 | Table
|
||||
3 | Computer
|
||||
|
||||
*/
|
||||
|
||||
|
||||
// fetch a single row
|
||||
echo "<h2>fetch()</h2>\n";
|
||||
$row = $dibi->fetch('SELECT title FROM products');
|
||||
Tracy\Dumper::dump($row); // Chair
|
||||
|
||||
|
||||
// fetch a single value
|
||||
echo "<h2>fetchSingle()</h2>\n";
|
||||
$value = $dibi->fetchSingle('SELECT title FROM products');
|
||||
Tracy\Dumper::dump($value); // Chair
|
||||
|
||||
|
||||
// fetch complete result set
|
||||
echo "<h2>fetchAll()</h2>\n";
|
||||
$all = $dibi->fetchAll('SELECT * FROM products');
|
||||
Tracy\Dumper::dump($all);
|
||||
|
||||
|
||||
// fetch complete result set like association array
|
||||
echo "<h2>fetchAssoc('title')</h2>\n";
|
||||
$res = $dibi->query('SELECT * FROM products');
|
||||
$assoc = $res->fetchAssoc('title'); // key
|
||||
Tracy\Dumper::dump($assoc);
|
||||
|
||||
|
||||
// fetch complete result set like pairs key => value
|
||||
echo "<h2>fetchPairs('product_id', 'title')</h2>\n";
|
||||
$res = $dibi->query('SELECT * FROM products');
|
||||
$pairs = $res->fetchPairs('product_id', 'title');
|
||||
Tracy\Dumper::dump($pairs);
|
||||
|
||||
|
||||
// fetch row by row
|
||||
echo "<h2>using foreach</h2>\n";
|
||||
$res = $dibi->query('SELECT * FROM products');
|
||||
foreach ($res as $n => $row) {
|
||||
Tracy\Dumper::dump($row);
|
||||
}
|
||||
|
||||
|
||||
// more complex association array
|
||||
$res = $dibi->query('
|
||||
SELECT *
|
||||
FROM products
|
||||
INNER JOIN orders USING (product_id)
|
||||
INNER JOIN customers USING (customer_id)
|
||||
');
|
||||
|
||||
echo "<h2>fetchAssoc('name|title')</h2>\n";
|
||||
$assoc = $res->fetchAssoc('name|title'); // key
|
||||
Tracy\Dumper::dump($assoc);
|
||||
|
||||
echo "<h2>fetchAssoc('name[]title')</h2>\n";
|
||||
$res = $dibi->query('SELECT * FROM products INNER JOIN orders USING (product_id) INNER JOIN customers USING (customer_id)');
|
||||
$assoc = $res->fetchAssoc('name[]title'); // key
|
||||
Tracy\Dumper::dump($assoc);
|
||||
|
||||
echo "<h2>fetchAssoc('name->title')</h2>\n";
|
||||
$res = $dibi->query('SELECT * FROM products INNER JOIN orders USING (product_id) INNER JOIN customers USING (customer_id)');
|
||||
$assoc = $res->fetchAssoc('name->title'); // key
|
||||
Tracy\Dumper::dump($assoc);
|
@@ -1,23 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
?>
|
||||
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
|
||||
|
||||
<h1>Importing SQL Dump from File | Dibi</h1>
|
||||
|
||||
<?php
|
||||
|
||||
if (@!include __DIR__ . '/../vendor/autoload.php') {
|
||||
die('Install packages using `composer install`');
|
||||
}
|
||||
|
||||
|
||||
$dibi = new Dibi\Connection([
|
||||
'driver' => 'sqlite',
|
||||
'database' => 'data/sample.s3db',
|
||||
]);
|
||||
|
||||
|
||||
$count = $dibi->loadFile('compress.zlib://data/sample.dump.sql.gz');
|
||||
|
||||
echo 'Number of SQL commands:', $count;
|
14
examples/log.sql
Normal file
14
examples/log.sql
Normal file
@@ -0,0 +1,14 @@
|
||||
Successfully connected to DB 'mysql'
|
||||
|
||||
SELECT * FROM `nucleus_item` WHERE `inumber` = 38;
|
||||
-- Result: object(DibiMySqlResult) rows: 1
|
||||
-- Takes: 4.994 ms
|
||||
|
||||
SELECT * FROM `nucleus_item` WHERE `inumber` < 38;
|
||||
-- Result: object(DibiMySqlResult) rows: 29
|
||||
-- Takes: 135.842 ms
|
||||
|
||||
SELECT * FROM `*nucleus_item` WHERE `inumber` < 38;
|
||||
-- Result: Query error: Can't find file: '.\dgx\*nucleus_item.frm' (errno: 22)
|
||||
-- Takes: 121.454 ms
|
||||
|
31
examples/logging.php
Normal file
31
examples/logging.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<pre>
|
||||
<?php
|
||||
|
||||
require_once '../dibi/dibi.php';
|
||||
|
||||
|
||||
dibi::$logfile = 'log.sql';
|
||||
|
||||
|
||||
// mysql
|
||||
dibi::connect(array(
|
||||
'driver' => 'mysql',
|
||||
'host' => 'localhost',
|
||||
'username' => 'root',
|
||||
'password' => '***',
|
||||
'database' => 'test',
|
||||
'charset' => 'utf8',
|
||||
));
|
||||
|
||||
|
||||
|
||||
$res = dibi::query('SELECT * FROM [nucleus_item] WHERE [inumber] = %i', 38);
|
||||
|
||||
|
||||
$res = dibi::query('SELECT * FROM [nucleus_item] WHERE [inumber] < %i', 38);
|
||||
|
||||
|
||||
$res = dibi::query('SELECT * FROM [*nucleus_item] WHERE [inumber] < %i', 38);
|
||||
|
||||
|
||||
?>
|
34
examples/metatypes.php
Normal file
34
examples/metatypes.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<pre>
|
||||
<?php
|
||||
|
||||
require_once '../dibi/dibi.php';
|
||||
|
||||
|
||||
// mysql
|
||||
dibi::connect(array(
|
||||
'driver' => 'mysql',
|
||||
'host' => 'localhost',
|
||||
'username' => 'root',
|
||||
'password' => '***',
|
||||
'database' => 'test',
|
||||
'charset' => 'utf8',
|
||||
));
|
||||
|
||||
$res = dibi::query('SELECT * FROM [nucleus_item] WHERE [inumber] <> %i', 38);
|
||||
|
||||
|
||||
$res = dibi::query('SELECT * FROM [nucleus_item] WHERE [inumber] <> %i', 38);
|
||||
|
||||
// auto-convert this field to integer
|
||||
$res->setType('inumber', DibiResult::FIELD_INTEGER);
|
||||
$record = $res->fetch();
|
||||
var_dump($record);
|
||||
|
||||
|
||||
// auto-detect all types
|
||||
$res->setType(TRUE);
|
||||
$record = $res->fetch();
|
||||
var_dump($record);
|
||||
|
||||
|
||||
?>
|
@@ -1,66 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
?>
|
||||
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
|
||||
|
||||
<h1>Query Language & Conditions | Dibi</h1>
|
||||
|
||||
<?php
|
||||
|
||||
if (@!include __DIR__ . '/../vendor/autoload.php') {
|
||||
die('Install packages using `composer install`');
|
||||
}
|
||||
|
||||
|
||||
$dibi = new Dibi\Connection([
|
||||
'driver' => 'sqlite',
|
||||
'database' => 'data/sample.s3db',
|
||||
]);
|
||||
|
||||
|
||||
// some variables
|
||||
$cond1 = true;
|
||||
$cond2 = false;
|
||||
$foo = -1;
|
||||
$bar = 2;
|
||||
|
||||
// conditional variable
|
||||
$name = $cond1 ? 'K%' : null;
|
||||
|
||||
// if & end
|
||||
$dibi->test('
|
||||
SELECT *
|
||||
FROM customers
|
||||
%if', isset($name), 'WHERE name LIKE ?', $name, '%end'
|
||||
);
|
||||
// -> SELECT * FROM customers WHERE name LIKE 'K%'
|
||||
|
||||
|
||||
// if & else & (optional) end
|
||||
$dibi->test('
|
||||
SELECT *
|
||||
FROM people
|
||||
WHERE id > 0
|
||||
%if', ($foo > 0), 'AND foo=?', $foo, '
|
||||
%else %if', ($bar > 0), 'AND bar=?', $bar, '
|
||||
');
|
||||
// -> SELECT * FROM people WHERE id > 0 AND bar=2
|
||||
|
||||
|
||||
// nested condition
|
||||
$dibi->test('
|
||||
SELECT *
|
||||
FROM customers
|
||||
WHERE
|
||||
%if', isset($name), 'name LIKE ?', $name, '
|
||||
%if', $cond2, 'AND admin=1 %end
|
||||
%else 1 LIMIT 10 %end'
|
||||
);
|
||||
// -> SELECT * FROM customers WHERE LIMIT 10
|
||||
|
||||
|
||||
// IF()
|
||||
$dibi->test('UPDATE products SET', [
|
||||
'price' => $dibi->expression('IF(price_fixed, price, ?)', 123),
|
||||
]);
|
||||
// -> SELECT * FROM customers WHERE LIMIT 10
|
@@ -1,92 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
?>
|
||||
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
|
||||
|
||||
<h1>Query Language Basic Examples | Dibi</h1>
|
||||
|
||||
<?php
|
||||
|
||||
if (@!include __DIR__ . '/../vendor/autoload.php') {
|
||||
die('Install packages using `composer install`');
|
||||
}
|
||||
|
||||
date_default_timezone_set('Europe/Prague');
|
||||
|
||||
|
||||
$dibi = new Dibi\Connection([
|
||||
'driver' => 'sqlite',
|
||||
'database' => 'data/sample.s3db',
|
||||
]);
|
||||
|
||||
|
||||
// SELECT
|
||||
$ipMask = '192.168.%';
|
||||
$timestamp = mktime(0, 0, 0, 10, 13, 1997);
|
||||
|
||||
$dibi->test('
|
||||
SELECT COUNT(*) as [count]
|
||||
FROM [comments]
|
||||
WHERE [ip] LIKE ?', $ipMask, '
|
||||
AND [date] > ', new Dibi\DateTime($timestamp)
|
||||
);
|
||||
// -> SELECT COUNT(*) as [count] FROM [comments] WHERE [ip] LIKE '192.168.%' AND [date] > 876693600
|
||||
|
||||
|
||||
// dibi detects INSERT or REPLACE command
|
||||
$dibi->test('
|
||||
REPLACE INTO products', [
|
||||
'title' => 'Super product',
|
||||
'price' => 318,
|
||||
'active' => true,
|
||||
]);
|
||||
// -> REPLACE INTO products ([title], [price], [active]) VALUES ('Super product', 318, 1)
|
||||
|
||||
|
||||
// multiple INSERT command
|
||||
$array = [
|
||||
'title' => 'Super Product',
|
||||
'price' => 12,
|
||||
'brand' => null,
|
||||
'created' => new DateTime,
|
||||
];
|
||||
$dibi->test('INSERT INTO products', $array, $array, $array);
|
||||
// -> INSERT INTO products ([title], [price], [brand], [created]) VALUES ('Super Product', ...) , (...) , (...)
|
||||
|
||||
|
||||
// dibi detects UPDATE command
|
||||
$dibi->test('
|
||||
UPDATE colors SET', [
|
||||
'color' => 'blue',
|
||||
'order' => 12,
|
||||
], '
|
||||
WHERE id=?', 123);
|
||||
// -> UPDATE colors SET [color]='blue', [order]=12 WHERE id=123
|
||||
|
||||
|
||||
// modifier applied to array
|
||||
$array = [1, 2, 3];
|
||||
$dibi->test('
|
||||
SELECT *
|
||||
FROM people
|
||||
WHERE id IN (?)', $array
|
||||
);
|
||||
// -> SELECT * FROM people WHERE id IN ( 1, 2, 3 )
|
||||
|
||||
|
||||
// modifier %by for ORDER BY
|
||||
$order = [
|
||||
'field1' => 'asc',
|
||||
'field2' => 'desc',
|
||||
];
|
||||
$dibi->test('
|
||||
SELECT *
|
||||
FROM people
|
||||
ORDER BY %by', $order, '
|
||||
');
|
||||
// -> SELECT * FROM people ORDER BY [field1] ASC, [field2] DESC
|
||||
|
||||
|
||||
// indentifiers and strings syntax mix
|
||||
$dibi->test('UPDATE [table] SET `item` = "5 1/4"" diskette"');
|
||||
// -> UPDATE [table] SET [item] = '5 1/4" diskette'
|
@@ -1,51 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
use Dibi\Type;
|
||||
|
||||
if (@!include __DIR__ . '/../vendor/autoload.php') {
|
||||
die('Install dependencies using `composer install --dev`');
|
||||
}
|
||||
|
||||
Tracy\Debugger::enable();
|
||||
|
||||
date_default_timezone_set('Europe/Prague');
|
||||
|
||||
?>
|
||||
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
|
||||
|
||||
<h1>Result Set Data Types | Dibi</h1>
|
||||
|
||||
<?php
|
||||
|
||||
$dibi = new Dibi\Connection([
|
||||
'driver' => 'sqlite',
|
||||
'database' => 'data/sample.s3db',
|
||||
]);
|
||||
|
||||
|
||||
// using manual hints
|
||||
$res = $dibi->query('SELECT * FROM [customers]');
|
||||
|
||||
$res->setType('customer_id', Type::INTEGER)
|
||||
->setType('added', Type::DATETIME)
|
||||
->setFormat(Type::DATETIME, 'Y-m-d H:i:s');
|
||||
|
||||
|
||||
Tracy\Dumper::dump($res->fetch());
|
||||
// outputs:
|
||||
// Dibi\Row(3) {
|
||||
// customer_id => 1
|
||||
// name => "Dave Lister" (11)
|
||||
// added => "2007-03-11 17:20:03" (19)
|
||||
|
||||
|
||||
// using auto-detection (works well with MySQL or other strictly typed databases)
|
||||
$res = $dibi->query('SELECT * FROM [customers]');
|
||||
|
||||
Tracy\Dumper::dump($res->fetch());
|
||||
// outputs:
|
||||
// Dibi\Row(3) {
|
||||
// customer_id => 1
|
||||
// name => "Dave Lister" (11)
|
||||
// added => "2007-03-11 17:20:03" (19)
|
48
examples/sql-builder.php
Normal file
48
examples/sql-builder.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<pre>
|
||||
<?php
|
||||
|
||||
require_once '../dibi/dibi.php';
|
||||
|
||||
|
||||
// mysql
|
||||
dibi::connect(array(
|
||||
'driver' => 'mysqli',
|
||||
'host' => 'localhost',
|
||||
'username' => 'root',
|
||||
'password' => '***',
|
||||
'database' => 'test',
|
||||
'charset' => 'utf8',
|
||||
));
|
||||
|
||||
|
||||
$arr1 = array(1, 2, 3);
|
||||
$arr2 = array('one', 'two', 'three');
|
||||
$arr3 = array(
|
||||
'a' => 'one',
|
||||
'b' => 'two',
|
||||
'c' => 'three',
|
||||
);
|
||||
$arr4 = array(
|
||||
'A' => 12,
|
||||
'B' => NULL,
|
||||
'C' => new TDateTime(31542),
|
||||
'D' => 'string',
|
||||
);
|
||||
|
||||
dibi::test(
|
||||
"
|
||||
SELECT *
|
||||
FROM [test]
|
||||
WHERE ([test.a] LIKE %T", '1995-03-01', "
|
||||
OR [b1] IN (", $arr1, ")
|
||||
OR [b2] IN (", $arr2, ")
|
||||
OR [b3] IN (%N", $arr3, ")
|
||||
OR [b4] IN %V", $arr4, "
|
||||
AND [c] = 'embedded '' string'
|
||||
OR [d]=%d", 10.3, "
|
||||
OR [true]=", true, "
|
||||
OR [false]=", false, "
|
||||
OR [null]=", NULL, "
|
||||
LIMIT 10");
|
||||
|
||||
?>
|
@@ -1,31 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
if (@!include __DIR__ . '/../vendor/autoload.php') {
|
||||
die('Install dependencies using `composer install --dev`');
|
||||
}
|
||||
|
||||
|
||||
// enable Tracy
|
||||
Tracy\Debugger::enable();
|
||||
|
||||
|
||||
$dibi = new Dibi\Connection([
|
||||
'driver' => 'sqlite',
|
||||
'database' => 'data/sample.s3db',
|
||||
]);
|
||||
|
||||
|
||||
// add panel to debug bar
|
||||
$panel = new Dibi\Bridges\Tracy\Panel;
|
||||
$panel->register($dibi);
|
||||
|
||||
|
||||
// throws error because SQL is bad
|
||||
$dibi->query('SELECT FROM customers WHERE customer_id < ?', 38);
|
||||
|
||||
?><!DOCTYPE html><link rel="stylesheet" href="data/style.css">
|
||||
|
||||
<h1>Tracy & SQL Exceptions | dibi</h1>
|
||||
|
||||
<p>Dibi can display and log exceptions via <a href="https://tracy.nette.org">Tracy</a>.</p>
|
@@ -1,38 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
if (@!include __DIR__ . '/../vendor/autoload.php') {
|
||||
die('Install dependencies using `composer install --dev`');
|
||||
}
|
||||
|
||||
|
||||
// enable Tracy
|
||||
Tracy\Debugger::enable();
|
||||
|
||||
|
||||
$dibi = new Dibi\Connection([
|
||||
'driver' => 'sqlite',
|
||||
'database' => 'data/sample.s3db',
|
||||
]);
|
||||
|
||||
|
||||
// add panel to debug bar
|
||||
$panel = new Dibi\Bridges\Tracy\Panel;
|
||||
$panel->register($dibi);
|
||||
|
||||
|
||||
// query will be logged
|
||||
$dibi->query('SELECT 123');
|
||||
|
||||
// result set will be dumped
|
||||
Tracy\Debugger::barDump($dibi->fetchAll('SELECT * FROM customers WHERE customer_id < ?', 38), '[customers]');
|
||||
|
||||
|
||||
?>
|
||||
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
|
||||
|
||||
<style> html { background: url(data/arrow.png) no-repeat bottom right; height: 100%; } </style>
|
||||
|
||||
<h1>Tracy | dibi</h1>
|
||||
|
||||
<p>Dibi can log queries and dump variables to the <a href="https://tracy.nette.org">Tracy</a>.</p>
|
@@ -1,34 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
?>
|
||||
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
|
||||
|
||||
<h1>Using DateTime | Dibi</h1>
|
||||
|
||||
<?php
|
||||
|
||||
if (@!include __DIR__ . '/../vendor/autoload.php') {
|
||||
die('Install packages using `composer install`');
|
||||
}
|
||||
|
||||
date_default_timezone_set('Europe/Prague');
|
||||
|
||||
|
||||
// CHANGE TO REAL PARAMETERS!
|
||||
$dibi = new Dibi\Connection([
|
||||
'driver' => 'sqlite',
|
||||
'database' => 'data/sample.s3db',
|
||||
'formatDate' => "'Y-m-d'",
|
||||
'formatDateTime' => "'Y-m-d H-i-s'",
|
||||
]);
|
||||
|
||||
|
||||
// generate and dump SQL
|
||||
$dibi->test('
|
||||
INSERT INTO [mytable]', [
|
||||
'id' => 123,
|
||||
'date' => new DateTime('12.3.2007'),
|
||||
'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')
|
@@ -1,82 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
?>
|
||||
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
|
||||
|
||||
<h1>Using Fluent Syntax | Dibi</h1>
|
||||
|
||||
<?php
|
||||
|
||||
if (@!include __DIR__ . '/../vendor/autoload.php') {
|
||||
die('Install packages using `composer install`');
|
||||
}
|
||||
|
||||
date_default_timezone_set('Europe/Prague');
|
||||
|
||||
|
||||
$dibi = new Dibi\Connection([
|
||||
'driver' => 'sqlite',
|
||||
'database' => 'data/sample.s3db',
|
||||
]);
|
||||
|
||||
|
||||
$id = 10;
|
||||
$record = [
|
||||
'title' => 'Super product',
|
||||
'price' => 318,
|
||||
'active' => true,
|
||||
];
|
||||
|
||||
// SELECT ...
|
||||
$dibi->select('product_id')->as('id')
|
||||
->select('title')
|
||||
->from('products')
|
||||
->innerJoin('orders')->using('(product_id)')
|
||||
->innerJoin('customers USING (customer_id)')
|
||||
->orderBy('title')
|
||||
->test();
|
||||
// -> SELECT [product_id] AS [id] , [title] FROM [products] INNER JOIN [orders]
|
||||
// USING (product_id) INNER JOIN customers USING (customer_id) ORDER BY [title]
|
||||
|
||||
|
||||
// SELECT ...
|
||||
echo $dibi->select('title')->as('id')
|
||||
->from('products')
|
||||
->fetchSingle();
|
||||
// -> Chair (as result of query: SELECT [title] AS [id] FROM [products])
|
||||
|
||||
|
||||
// INSERT ...
|
||||
$dibi->insert('products', $record)
|
||||
->setFlag('IGNORE')
|
||||
->test();
|
||||
// -> INSERT IGNORE INTO [products] ([title], [price], [active]) VALUES ('Super product', 318, 1)
|
||||
|
||||
|
||||
// UPDATE ...
|
||||
$dibi->update('products', $record)
|
||||
->where('product_id = ?', $id)
|
||||
->test();
|
||||
// -> UPDATE [products] SET [title]='Super product', [price]=318, [active]=1 WHERE product_id = 10
|
||||
|
||||
|
||||
// DELETE ...
|
||||
$dibi->delete('products')
|
||||
->where('product_id = ?', $id)
|
||||
->test();
|
||||
// -> DELETE FROM [products] WHERE product_id = 10
|
||||
|
||||
|
||||
// custom commands
|
||||
$dibi->command()
|
||||
->update('products')
|
||||
->where('product_id = ?', $id)
|
||||
->set($record)
|
||||
->test();
|
||||
// -> UPDATE [products] SET [title]='Super product', [price]=318, [active]=1 WHERE product_id = 10
|
||||
|
||||
|
||||
$dibi->command()
|
||||
->truncate('products')
|
||||
->test();
|
||||
// -> TRUNCATE [products]
|
@@ -1,33 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
?>
|
||||
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
|
||||
|
||||
<h1>Using Limit & Offset | Dibi</h1>
|
||||
|
||||
<?php
|
||||
|
||||
if (@!include __DIR__ . '/../vendor/autoload.php') {
|
||||
die('Install packages using `composer install`');
|
||||
}
|
||||
|
||||
|
||||
$dibi = new Dibi\Connection([
|
||||
'driver' => 'sqlite',
|
||||
'database' => 'data/sample.s3db',
|
||||
]);
|
||||
|
||||
|
||||
// no limit
|
||||
$dibi->test('SELECT * FROM [products]');
|
||||
// -> SELECT * FROM [products]
|
||||
|
||||
|
||||
// with limit = 2
|
||||
$dibi->test('SELECT * FROM [products] %lmt', 2);
|
||||
// -> SELECT * FROM [products] LIMIT 2
|
||||
|
||||
|
||||
// with limit = 2, offset = 1
|
||||
$dibi->test('SELECT * FROM [products] %lmt %ofs', 2, 1);
|
||||
// -> SELECT * FROM [products] LIMIT 2 OFFSET 1
|
@@ -1,42 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
?>
|
||||
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
|
||||
|
||||
<h1>Using Logger | Dibi</h1>
|
||||
|
||||
<?php
|
||||
|
||||
if (@!include __DIR__ . '/../vendor/autoload.php') {
|
||||
die('Install packages using `composer install`');
|
||||
}
|
||||
|
||||
date_default_timezone_set('Europe/Prague');
|
||||
|
||||
|
||||
$dibi = new Dibi\Connection([
|
||||
'driver' => 'sqlite',
|
||||
'database' => 'data/sample.s3db',
|
||||
// enable query logging to this file
|
||||
'profiler' => [
|
||||
'file' => 'log/log.sql',
|
||||
'errorsOnly' => false,
|
||||
],
|
||||
]);
|
||||
|
||||
|
||||
try {
|
||||
$res = $dibi->query('SELECT * FROM [customers] WHERE [customer_id] = ?', 1);
|
||||
|
||||
$res = $dibi->query('SELECT * FROM [customers] WHERE [customer_id] < ?', 5);
|
||||
|
||||
$res = $dibi->query('SELECT FROM [customers] WHERE [customer_id] < ?', 38);
|
||||
} catch (Dibi\Exception $e) {
|
||||
echo '<p>', get_class($e), ': ', $e->getMessage(), '</p>';
|
||||
}
|
||||
|
||||
|
||||
// outputs a log file
|
||||
echo '<h2>File log/log.sql:</h2>';
|
||||
|
||||
echo '<pre>', file_get_contents('log/log.sql'), '</pre>';
|
@@ -1,59 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
?>
|
||||
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
|
||||
|
||||
<h1>Using Substitutions | Dibi</h1>
|
||||
|
||||
<?php
|
||||
|
||||
if (@!include __DIR__ . '/../vendor/autoload.php') {
|
||||
die('Install packages using `composer install`');
|
||||
}
|
||||
|
||||
|
||||
$dibi = new Dibi\Connection([
|
||||
'driver' => 'sqlite',
|
||||
'database' => 'data/sample.s3db',
|
||||
]);
|
||||
|
||||
|
||||
// create new substitution :blog: ==> wp_
|
||||
$dibi->getSubstitutes()->blog = 'wp_';
|
||||
|
||||
$dibi->test('SELECT * FROM [:blog:items]');
|
||||
// -> SELECT * FROM [wp_items]
|
||||
|
||||
|
||||
// create new substitution :: (empty) ==> my_
|
||||
$dibi->getSubstitutes()->{''} = 'my_';
|
||||
|
||||
$dibi->test("UPDATE ::table SET [text]='Hello World'");
|
||||
// -> UPDATE my_table SET [text]='Hello World'
|
||||
|
||||
|
||||
// create substitutions using fallback callback
|
||||
function substFallBack($expr)
|
||||
{
|
||||
$const = 'SUBST_' . strtoupper($expr);
|
||||
if (defined($const)) {
|
||||
return constant($const);
|
||||
} else {
|
||||
throw new Exception("Undefined substitution :$expr:");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// define callback
|
||||
$dibi->getSubstitutes()->setCallback('substFallBack');
|
||||
|
||||
// define substitutes as constants
|
||||
define('SUBST_ACCOUNT', 'eshop_');
|
||||
define('SUBST_ACTIVE', 7);
|
||||
|
||||
$dibi->test("
|
||||
UPDATE :account:user
|
||||
SET name='John Doe', status=:active:
|
||||
WHERE id=", 7
|
||||
);
|
||||
// -> UPDATE eshop_user SET name='John Doe', status=7 WHERE id= 7
|
@@ -1,39 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
?>
|
||||
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
|
||||
|
||||
<h1>Using Transactions | Dibi</h1>
|
||||
|
||||
<?php
|
||||
|
||||
if (@!include __DIR__ . '/../vendor/autoload.php') {
|
||||
die('Install packages using `composer install`');
|
||||
}
|
||||
|
||||
|
||||
$dibi = new Dibi\Connection([
|
||||
'driver' => 'sqlite',
|
||||
'database' => 'data/sample.s3db',
|
||||
]);
|
||||
|
||||
|
||||
echo "<h2>Before</h2>\n";
|
||||
$dibi->query('SELECT * FROM [products]')->dump();
|
||||
// -> 3 rows
|
||||
|
||||
|
||||
$dibi->begin();
|
||||
$dibi->query('INSERT INTO [products]', [
|
||||
'title' => 'Test product',
|
||||
]);
|
||||
|
||||
echo "<h2>After INSERT</h2>\n";
|
||||
$dibi->query('SELECT * FROM [products]')->dump();
|
||||
|
||||
|
||||
$dibi->rollback(); // or $dibi->commit();
|
||||
|
||||
echo "<h2>After rollback</h2>\n";
|
||||
$dibi->query('SELECT * FROM [products]')->dump();
|
||||
// -> 3 rows again
|
55
license.md
55
license.md
@@ -1,55 +0,0 @@
|
||||
Licenses
|
||||
========
|
||||
|
||||
Good news! You may use Dibi under the terms of either the New BSD License
|
||||
or the GNU General Public License (GPL) version 2 or 3.
|
||||
|
||||
The BSD License is recommended for most projects. It is easy to understand and it
|
||||
places almost no restrictions on what you can do with the framework. If the GPL
|
||||
fits better to your project, you can use the framework under this license.
|
||||
|
||||
You don't have to notify anyone which license you are using. You can freely
|
||||
use Dibi in commercial projects as long as the copyright header
|
||||
remains intact.
|
||||
|
||||
|
||||
New BSD License
|
||||
---------------
|
||||
|
||||
Copyright (c) 2004, 2014 David Grudl (https://davidgrudl.com)
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification,
|
||||
are permitted provided that the following conditions are met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
* Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
* Neither the name of "Dibi" nor the names of its contributors
|
||||
may be used to endorse or promote products derived from this software
|
||||
without specific prior written permission.
|
||||
|
||||
This software is provided by the copyright holders and contributors "as is" and
|
||||
any express or implied warranties, including, but not limited to, the implied
|
||||
warranties of merchantability and fitness for a particular purpose are
|
||||
disclaimed. In no event shall the copyright owner or contributors be liable for
|
||||
any direct, indirect, incidental, special, exemplary, or consequential damages
|
||||
(including, but not limited to, procurement of substitute goods or services;
|
||||
loss of use, data, or profits; or business interruption) however caused and on
|
||||
any theory of liability, whether in contract, strict liability, or tort
|
||||
(including negligence or otherwise) arising in any way out of the use of this
|
||||
software, even if advised of the possibility of such damage.
|
||||
|
||||
|
||||
GNU General Public License
|
||||
--------------------------
|
||||
|
||||
GPL licenses are very very long, so instead of including them here we offer
|
||||
you URLs with full text:
|
||||
|
||||
- [GPL version 2](http://www.gnu.org/licenses/gpl-2.0.html)
|
||||
- [GPL version 3](http://www.gnu.org/licenses/gpl-3.0.html)
|
271
license/license.gpl.cz.txt
Normal file
271
license/license.gpl.cz.txt
Normal file
@@ -0,0 +1,271 @@
|
||||
----------------------------------------------------------------------------------
|
||||
Tento text je neofici<63>ln<6C>m p<>ekladem GNU General Public License (GNU GPL). Nebyl
|
||||
vyd<EFBFBD>n nadac<61> Free Software Foundation a nevyjad<61>uje pr<70>vn<76> podstatu podm<64>nek pro
|
||||
<EFBFBD><EFBFBD><EFBFBD>en<EFBFBD> softwaru pou<6F><75>vaj<61>c<EFBFBD>ho GNU GPL - tomuto <20><>elu slou<6F><75> v<>hradn<64> p<>vodn<64>
|
||||
anglick<EFBFBD> verze GNU GPL. P<>esto douf<75>me, <20>e tento p<>eklad pom<6F><6D>e <20>esk<73>m <20>ten<65><6E><EFBFBD>m
|
||||
l<EFBFBD>pe porozum<75>t licenci GNU GPL.
|
||||
----------------------------------------------------------------------------------
|
||||
|
||||
GNU GENERAL PUBLIC LICENSE
|
||||
<20>esk<73> p<>eklad verze 2, <20>erven 1991
|
||||
|
||||
Copyright (C) 1989, 1991 Free Software Foundation, Inc.
|
||||
675 Mass Ave, Cambridge, MA 02139, USA
|
||||
|
||||
Kop<6F>rov<6F>n<EFBFBD> a distribuce doslovn<76>ch kopi<70> tohoto licen<65>n<EFBFBD>ho dokumentu jsou
|
||||
dovoleny komukoliv, jeho zm<7A>ny jsou v<>ak zak<61>z<EFBFBD>ny.
|
||||
|
||||
Preambule
|
||||
|
||||
Softwarov<6F> licence jsou v<>t<EFBFBD>inou navr<76>eny tak, <20>e v<>m odeb<65>raj<61> pr<70>vo svobodn<64>ho
|
||||
sd<EFBFBD>len<EFBFBD> a <20>prav program<61>. Smyslem GNU General Public License je naproti tomu
|
||||
zaru<EFBFBD>it svobodu ke sd<73>len<65> a <20>prav<61>m svobodn<64>ho softwaru - pro zaji<6A>t<EFBFBD>n<EFBFBD>
|
||||
svobodn<EFBFBD>ho p<><70>stupu k tomuto softwaru pro v<>echny jeho u<>ivatele. Tato General
|
||||
Public License se vztahuje na v<>t<EFBFBD>inu softwaru nadace Free Software Foundation a
|
||||
na jak<61>koli jin<69> program, jeho<68> autor se p<>iklon<6F> k jej<65>mu pou<6F><75>v<EFBFBD>n<EFBFBD>. (N<>kter<65>
|
||||
dal<EFBFBD><EFBFBD> software od Free Software Foundation je nam<61>sto toho pokryt GNU Lesser
|
||||
General Public License.) M<><4D>ete ji rovn<76><6E> pou<6F><75>t pro sv<73> programy.
|
||||
|
||||
Pokud mluv<75>me o svobodn<64>m softwaru, m<>me na mysli svobodu, nikoliv cenu. Na<4E>e
|
||||
General Public License je navr<76>ena pro zaji<6A>t<EFBFBD>n<EFBFBD> toho, <20>e m<><6D>ete svobodn<64> <20><><EFBFBD>it
|
||||
kopie svobodn<64>ho softwaru (a <20><>tovat si poplatek za tuto slu<6C>bu, pokud chcete),
|
||||
<EFBFBD>e obdr<64><72>te zdrojov<6F> k<>d anebo jej m<><6D>ete z<>skat, pokud ho chcete, <20>e m<><6D>ete
|
||||
tento software modifikovat nebo jeho <20><>sti pou<6F><75>t v nov<6F>ch svobodn<64>ch programech;
|
||||
a <20>e v<>te, <20>e tyto v<>ci sm<73>te d<>lat.
|
||||
|
||||
Abychom mohli va<76>e pr<70>va chr<68>nit, mus<75>me vytvo<76>it omezen<65>, kter<65> zak<61><6B><EFBFBD> komukoli
|
||||
v<EFBFBD>m tato pr<70>va odep<65>rat nebo v<>s <20><>dat, abyste se t<>chto pr<70>v vzdal. Tato
|
||||
omezen<EFBFBD> se prom<6F>taj<61> do jist<73>ch povinnost<73>, kter<65>m mus<75>te dost<73>t, pokud <20><><EFBFBD><EFBFBD>te
|
||||
kopie doty<74>n<EFBFBD>ho softwaru anebo ho modifikujete.
|
||||
|
||||
Nap<61><70>klad, <20><><EFBFBD><EFBFBD>te-li kopie takov<6F>ho programu, a<> ji<6A> zdarma nebo za poplatek,
|
||||
mus<EFBFBD>te poskytnout p<><70>jemc<6D>m v<>echna pr<70>va, kter<65> m<>te s<>m. Mus<75>te zaru<72>it, <20>e
|
||||
p<EFBFBD><EFBFBD>jemci rovn<76><6E> dostanou anebo mohou z<>skat zdrojov<6F> k<>d. A mus<75>te jim uk<75>zat
|
||||
tyto podm<64>nky, aby znali sv<73> pr<70>va.
|
||||
|
||||
Va<56>e pr<70>va chr<68>n<EFBFBD>me ve dvou kroc<6F>ch: (1) autorizac<61> softwaru a (2) nab<61>dkou t<>to
|
||||
licence, kter<65> v<>m d<>v<EFBFBD> pr<70>voplatn<74> svolen<65> ke kop<6F>rov<6F>n<EFBFBD>, <20><><EFBFBD>en<65> a modifikaci
|
||||
softwaru.
|
||||
|
||||
Kv<4B>li ochran<61> ka<6B>d<EFBFBD>ho autora i n<>s samotn<74>ch chceme zajistit, aby ka<6B>d<EFBFBD> ch<63>pal
|
||||
skute<EFBFBD>nost, <20>e pro svobodn<64> software neplat<61> <20><>dn<64> z<>ruky. Je-li software n<>k<EFBFBD>m
|
||||
jin<EFBFBD>m modifikov<6F>n a posl<73>n d<>le, chceme, aby p<><70>jemci v<>d<EFBFBD>li, <20>e to, co maj<61>,
|
||||
nen<EFBFBD> origin<69>l, tak<61>e jak<61>koliv probl<62>my vnesen<65> jin<69>mi se neodraz<61> na reputaci
|
||||
p<EFBFBD>vodn<EFBFBD>ch autor<6F>.
|
||||
|
||||
Kone<6E>n<EFBFBD>, ka<6B>d<EFBFBD> svobodn<64> program je neust<73>le ohro<72>en softwarov<6F>mi patenty.
|
||||
P<EFBFBD>ejeme si zamezit nebezpe<70><65>, <20>e redistributo<74>i svobodn<64>ho programu obdr<64><72>
|
||||
samostatn<EFBFBD> patentov<6F> osv<73>d<EFBFBD>en<65> a t<>m u<>in<69> program v<>zan<61>m. Abychom tomu
|
||||
zamezili, deklarovali jsme, <20>e ka<6B>d<EFBFBD> patent mus<75> b<>t bu<62> vyd<79>n s t<>m, <20>e
|
||||
umo<EFBFBD><EFBFBD>uje ka<6B>d<EFBFBD>mu svobodn<64> u<>it<69>, anebo nesm<73> b<>t vyd<79>n v<>bec.
|
||||
|
||||
P<>esn<73> ustanoven<65> a podm<64>nky pro kop<6F>rov<6F>n<EFBFBD>, <20><><EFBFBD>en<65> a modifikaci jsou uvedeny
|
||||
d<EFBFBD>le.
|
||||
|
||||
USTANOVEN<45> A PODM<44>NKY PRO KOP<4F>ROV<4F>N<EFBFBD>, DISTRIBUCI A MODIFIKACI
|
||||
|
||||
0. Tato licence se vztahuje na kter<65>koliv program <20>i jin<69> d<>lo, kter<65> obsahuje
|
||||
zm<EFBFBD>nku, um<75>st<73>nou v n<>m dr<64>itelem autorsk<73>ch pr<70>v, o tom, <20>e d<>lo m<><6D>e b<>t
|
||||
<EFBFBD><EFBFBD><EFBFBD>eno podle ustanoven<65> GNU General Public License. V dal<61><6C>m textu znamen<65> "program"
|
||||
ka<EFBFBD>d<EFBFBD> takov<6F> program nebo d<>lo a "d<>lo zalo<6C>en<65> na programu" znamen<65> bu<62> program
|
||||
samotn<EFBFBD> anebo ka<6B>d<EFBFBD> jin<69> d<>lo z n<>j odvozen<65>, kter<65> podl<64>h<EFBFBD> autorsk<73>mu z<>konu:
|
||||
t<EFBFBD>m se m<>n<EFBFBD> d<>lo obsahuj<75>c<EFBFBD> program nebo jeho <20><>st, bu<62> doslovn<76> anebo s
|
||||
modifikacemi, pop<6F><70>pad<61> v p<>ekladu do jin<69>ho jazyka. (Nad<61>le je p<>eklad
|
||||
zahrnov<EFBFBD>n bez omezen<65> pod pojem "modifikace".) Ka<4B>d<EFBFBD> u<>ivatel licence je
|
||||
ozna<EFBFBD>ov<EFBFBD>n jako "vy".
|
||||
|
||||
Jin<EFBFBD> <20>innosti ne<6E> kop<6F>rov<6F>n<EFBFBD>, <20><><EFBFBD>en<65> a modifikace nejsou pokryty touto licenc<6E>;
|
||||
sahaj<EFBFBD> mimo jej<65> r<>mec. Akt spu<70>t<EFBFBD>n<EFBFBD> programu nen<65> omezen a v<>stup z programu je
|
||||
pokryt pouze tehdy, jestli<6C>e obsah v<>stupu tvo<76><6F> d<>lo zalo<6C>en<65> na programu (nez<65>visle
|
||||
na tom, zda bylo vytvo<76>eno <20>innost<73> programu). Posouzen<65> platnosti p<>edchoz<6F>
|
||||
v<EFBFBD>ty z<>vis<69> na tom, co program d<>l<EFBFBD>.
|
||||
|
||||
1. Sm<53>te kop<6F>rovat a <20><><EFBFBD>it doslovn<76> kopie zdrojov<6F>ho k<>du programu tak, jak jste
|
||||
jej obdr<64>el a na libovoln<6C>m m<>diu, za p<>edpokladu, <20>e na ka<6B>d<EFBFBD> kopii viditeln<6C> a
|
||||
n<EFBFBD>le<EFBFBD>it<EFBFBD> zve<76>ejn<6A>te zm<7A>nku o autorsk<73>ch pr<70>vech a absenci z<>ruky; ponech<63>te
|
||||
nedot<EFBFBD>en<EFBFBD> v<>echny zm<7A>nky vztahuj<75>c<EFBFBD> se k t<>to licenci a k absenci z<>ruky; a d<>te
|
||||
ka<EFBFBD>d<EFBFBD>mu p<><70>jemci spolu s programem kopii t<>to licence.
|
||||
|
||||
Za fyzick<63> akt p<>enesen<65> kopie m<><6D>ete <20><>dat poplatek a podle vlastn<74>ho uv<75><76>en<65>
|
||||
m<EFBFBD><EFBFBD>ete nab<61>dnout za poplatek z<>ru<72>n<EFBFBD> ochranu.
|
||||
|
||||
2. M<><4D>ete modifikovat va<76>i kopii <20>i kopie programu anebo kter<65>koliv jeho <20><>sti,
|
||||
a tak vytvo<76>it d<>lo zalo<6C>en<65> na programu a kop<6F>rovat a roz<6F>i<EFBFBD>ovat takov<6F>
|
||||
modifikace <20>i d<>lo podle podm<64>nek paragrafu 1 v<><76>e, za p<>edpokladu, <20>e spln<6C>te
|
||||
v<EFBFBD>echny tyto podm<64>nky:
|
||||
|
||||
a) Modifikovan<61> soubory mus<75>te opat<61>it z<>etelnou zm<7A>nkou uv<75>d<EFBFBD>j<EFBFBD>c<EFBFBD>, <20>e jste
|
||||
soubory zm<7A>nil a datum ka<6B>d<EFBFBD> zm<7A>ny.
|
||||
|
||||
b) Mus<75>te umo<6D>nit, aby jak<61>koliv v<>mi publikovan<61> <20>i roz<6F>i<EFBFBD>ovan<61> d<>lo, kter<65>
|
||||
obsahuje zcela nebo z<><7A>sti program nebo jakoukoli jeho <20><>st, pop<6F><70>pad<61> je z
|
||||
programu nebo jeho <20><>sti odvozeno, mohlo b<>t jako celek bezplatn<74> poskytnuto
|
||||
ka<6B>d<EFBFBD> t<>et<65> osob<6F> v souladu s ustanoven<65>mi t<>to licence.
|
||||
|
||||
c) Pokud modifikovan<61> program pracuje norm<72>ln<6C> tak, <20>e <20>te interaktivn<76> povely,
|
||||
mus<75>te zajistit, <20>e p<>i nejb<6A><62>n<EFBFBD>j<EFBFBD><6A>m zp<7A>sobu jeho spu<70>t<EFBFBD>n<EFBFBD> vytiskne nebo zobraz<61>
|
||||
hl<68><6C>en<65> zahrnuj<75>c<EFBFBD> p<><70>slu<6C>nou zm<7A>nku o autorsk<73>m pr<70>vu a uvede, <20>e neexistuje
|
||||
<20><>dn<64> z<>ruka (nebo p<><70>padn<64>, <20>e z<>ruku poskytujete vy), a <20>e u<>ivatel<65> mohou za
|
||||
t<>chto podm<64>nek program redistribuovat, a mus<75> u<>ivateli sd<73>lit, jak<61>m zp<7A>sobem
|
||||
m<><6D>e nahl<68>dnout do kopie t<>to licence. (V<>jimka: v p<><70>pad<61>, <20>e s<>m program je
|
||||
interaktivn<76>, av<61>ak <20><>dn<64> takov<6F> hl<68><6C>en<65> nevypisuje, nepo<70>aduje se, aby va<76>e
|
||||
d<>lo zalo<6C>en<65> na programu takov<6F> hl<68><6C>en<65> vypisovalo.)
|
||||
|
||||
Tyto po<70>adavky se vztahuj<75> k modifikovan<61>mu d<>lu jako celku. Pokud lze
|
||||
identifikovat <20><>sti takov<6F>ho d<>la, kter<65> z<>ejm<6A> nejsou odvozeny z programu a
|
||||
mohou b<>t samy o sob<6F> rozumn<6D> pova<76>ov<6F>ny za nez<65>visl<73> a samostatn<74> d<>la, pak se
|
||||
tato licence a jej<65> ustanoven<65> nevztahuj<75> na tyto <20><>sti, jsou-li <20><><EFBFBD>eny jako
|
||||
nez<EFBFBD>visl<EFBFBD> d<>la. Av<41>ak jakmile tyt<79><74> <20><>sti roz<6F>i<EFBFBD>ujete jako <20><>st celku, j<>m<EFBFBD> je
|
||||
d<EFBFBD>lo zalo<6C>en<65> na programu, mus<75> b<>t roz<6F>i<EFBFBD>ov<6F>n<EFBFBD> tohoto celku pod<6F><64>zeno
|
||||
ustanoven<EFBFBD>m t<>to licence tak, <20>e povolen<65> poskytnut<75> dal<61><6C>m u<>ivatel<65>m se
|
||||
roz<EFBFBD><EFBFBD><EFBFBD><EFBFBD> na cel<65> d<>lo, tedy na v<>echny jeho <20><>sti bez ohledu na to, kdo kterou
|
||||
<EFBFBD><EFBFBD>st napsal.
|
||||
|
||||
Smyslem tohoto paragrafu tedy nen<65> z<>sk<73>n<EFBFBD> pr<70>v na d<>lo zcela napsan<61> v<>mi ani
|
||||
pop<EFBFBD>r<EFBFBD>n<EFBFBD> va<76>ich pr<70>v v<><76>i n<>mu; skute<74>n<EFBFBD>m smyslem je v<>kon pr<70>va na <20><>zen<65>
|
||||
distribuce odvozen<65>ch nebo kolektivn<76>ch d<>l zalo<6C>en<65>ch na programu.
|
||||
|
||||
Pouh<EFBFBD> spojen<65> jin<69>ho d<>la, je<6A> nen<65> na programu zalo<6C>eno, s programem (anebo
|
||||
d<EFBFBD>lem zalo<6C>en<65>m na programu) na pam<61><6D>ov<6F>m nebo distribu<62>n<EFBFBD>m m<>diu neuvazuje toto
|
||||
jin<EFBFBD> d<>lo do p<>sobnosti t<>to licence.
|
||||
|
||||
3. M<><4D>ete kop<6F>rovat a roz<6F>i<EFBFBD>ovat program (nebo d<>lo na n<>m zalo<6C>en<65>, viz
|
||||
paragraf 2) v objektov<6F> anebo spustiteln<6C> podob<6F> podle ustanoven<65> paragraf<61> 1 a
|
||||
2 v<><76>e, pokud spln<6C>te n<>kterou z n<>sleduj<75>c<EFBFBD>ch n<>le<6C>itost<73>:
|
||||
|
||||
a) Doprovod<6F>te jej zdrojov<6F>m k<>dem ve strojov<6F> <20>iteln<6C> form<72>. Zdrojov<6F> k<>d mus<75>
|
||||
b<>t roz<6F>i<EFBFBD>ov<6F>n podle ustanoven<65> paragraf<61> 1 a 2 v<><76>e, a to na m<>diu b<><62>n<EFBFBD>
|
||||
pou<6F><75>van<61>m pro v<>m<EFBFBD>nu softwaru; nebo
|
||||
|
||||
b) Doprovod<6F>te jej p<>semnou nab<61>dkou s platnost<73> nejm<6A>n<EFBFBD> t<>i roky, podle n<><6E>
|
||||
poskytnete jak<61>koli t<>et<65> stran<61>, za poplatek nep<65>evy<76>uj<75>c<EFBFBD> va<76>e v<>daje
|
||||
vynalo<6C>en<65> na fyzickou v<>robou zdrojov<6F> distribuce, kompletn<74> strojov<6F> <20>itelnou
|
||||
kopii odpov<6F>daj<61>c<EFBFBD>ho zdrojov<6F>ho k<>du, jen<65> mus<75> b<>t <20><><EFBFBD>en podle ustanoven<65>
|
||||
paragraf<61> 1 a 2 v<><76>e na m<>diu b<><62>n<EFBFBD> pou<6F><75>van<61>m pro v<>m<EFBFBD>nu softwaru; nebo
|
||||
|
||||
c) Doprovod<6F>te jej informacemi, kter<65> jste dostal ohledn<64> nab<61>dky na poskytnut<75>
|
||||
zdrojov<6F>ho k<>du. (Tato alternativa je povolena jen pro nekomer<65>n<EFBFBD> <20><><EFBFBD>en<65> a jenom
|
||||
tehdy, pokud jste obdr<64>el program v objektov<6F>m nebo spustiteln<6C>m tvaru spolu s
|
||||
takovou nab<61>dkou, v souladu s polo<6C>kou b v<><76>e.)
|
||||
|
||||
Zdrojov<EFBFBD> k<>d k d<>lu je nejvhodn<64>j<EFBFBD><6A> formou d<>la z hlediska jeho p<><70>padn<64>ch
|
||||
modifikac<EFBFBD>. Pro d<>lo ve spustiteln<6C>m tvaru znamen<65> <20>pln<6C> zdrojov<6F> k<>d ve<76>ker<65>
|
||||
zdrojov<EFBFBD> k<>d pro v<>echny moduly, kter<65> obsahuje, plus jak<61>koli dal<61><6C> soubory pro
|
||||
definici rozhran<61>, plus d<>vkov<6F> soubory pot<6F>ebn<62> pro kompilaci a instalaci
|
||||
spustiteln<EFBFBD>ho programu. Zvl<76><6C>tn<74> v<>jimkou jsou v<>ak ty softwarov<6F> komponenty,
|
||||
kter<EFBFBD> jsou norm<72>ln<6C> <20><><EFBFBD>eny (bu<62> ve zdrojov<6F> nebo bin<69>rn<72> form<72>) s hlavn<76>mi
|
||||
sou<EFBFBD><EFBFBD>stmi opera<72>n<EFBFBD>ho syst<73>mu, na n<>m<EFBFBD> spustiteln<6C> program b<><62><EFBFBD> (tj. s
|
||||
p<EFBFBD>eklada<EFBFBD>em, j<>drem apod.). Tyto komponenty nemus<75> b<>t <20><><EFBFBD>eny se zdrojov<6F>m k<>dem,
|
||||
pokud ov<6F>em komponenta sama nedoprov<6F>z<EFBFBD> spustitelnou podobu d<>la.
|
||||
|
||||
Je-li <20><><EFBFBD>en<65> objektov<6F>ho nebo spustiteln<6C>ho k<>du <20>in<69>no nab<61>dkou p<><70>stupu ke
|
||||
kop<EFBFBD>rov<EFBFBD>n<EFBFBD> z ur<75>it<69>ho m<>sta, potom se za distribuci zdrojov<6F>ho k<>du po<70><6F>t<EFBFBD> i
|
||||
nab<EFBFBD>dnut<EFBFBD> ekvivalentn<74>ho p<><70>stupu ke kop<6F>rov<6F>n<EFBFBD> zdrojov<6F>ho k<>du ze stejn<6A>ho
|
||||
m<EFBFBD>sta, by<62> p<>itom nejsou t<>et<65> strany nuceny ke zkop<6F>rov<6F>n<EFBFBD> zdrojov<6F>ho k<>du
|
||||
spolu s objektov<6F>m.
|
||||
|
||||
4. Nesm<73>te kop<6F>rovat, modifikovat, poskytovat sublicence anebo <20><><EFBFBD>it program
|
||||
jin<EFBFBD>m zp<7A>sobem ne<6E> v<>slovn<76> uveden<65>m v t<>to licenci. Jak<61>koli jin<69> pokus o
|
||||
kop<EFBFBD>rov<EFBFBD>n<EFBFBD>, modifikov<6F>n<EFBFBD>, poskytnut<75> sublicence anebo <20><><EFBFBD>en<65> programu je
|
||||
neplatn<EFBFBD> a automaticky ukon<6F><6E> va<76>e pr<70>va dan<61> touto licenc<6E>. Strany, kter<65> od
|
||||
v<EFBFBD>s obdr<64>ely kopie anebo pr<70>va v souladu s touto licenc<6E>, v<>ak nemaj<61> sv<73>
|
||||
licence ukon<6F>eny, dokud se jim pln<6C> pod<6F>izuj<75>.
|
||||
|
||||
5. Nen<65> va<76><61> povinost<73> tuto licenci p<>ijmout, proto<74>e jste ji nepodepsal. Nic
|
||||
jin<EFBFBD>ho v<>m v<>ak ned<65>v<EFBFBD> mo<6D>nost kop<6F>rovat nebo <20><><EFBFBD>it program nebo odvozen<65> d<>la.
|
||||
V p<><70>pad<61>, <20>e tuto licenci nep<65>ijmete, jsou tyto <20>innosti z<>konem zak<61>z<EFBFBD>ny. T<>m
|
||||
p<EFBFBD>dem modifikac<61> anebo <20><><EFBFBD>en<65>m programu (anebo ka<6B>d<EFBFBD>ho d<>la zalo<6C>en<65>ho na
|
||||
programu) vyjad<61>ujete sv<73> pod<6F><64>zen<65> se licenci a v<>em jej<65>m ustanoven<65>m a
|
||||
podm<EFBFBD>nk<EFBFBD>m pro kop<6F>rovan<61>, modifikov<6F>n<EFBFBD> a <20><><EFBFBD>en<65> programu a d<>l na n<>m zalo<6C>en<65>ch.
|
||||
|
||||
6. Poka<6B>d<EFBFBD>, kdy<64> redistribuujete program (nebo d<>lo zalo<6C>en<65> na programu),
|
||||
z<EFBFBD>sk<EFBFBD>v<EFBFBD> p<><70>jemce od p<>vodn<64>ho dr<64>itele licence pr<70>vo kop<6F>rovat, modifikovat a
|
||||
<EFBFBD><EFBFBD><EFBFBD>it program v souladu s t<>mito ustanoven<65>mi a podm<64>nkami. Nesm<73>te kl<6B>st <20><>dn<64>
|
||||
dal<EFBFBD><EFBFBD> p<>ek<65><6B>ky v<>konu zde zaru<72>en<65>ch p<><70>jemcov<6F>ch pr<70>v. Nejste odpov<6F>dn<64> za
|
||||
vym<EFBFBD>h<EFBFBD>n<EFBFBD> dodr<64>ov<6F>n<EFBFBD> t<>to licence t<>et<65>mi stranami.
|
||||
|
||||
7. Jsou-li v<>m z rozhodnut<75> soudu, obvin<69>n<EFBFBD>m z poru<72>en<65> patentu nebo z
|
||||
jak<EFBFBD>hokoli jin<69>ho d<>vodu (nejen v souvislosti s patenty) ulo<6C>eny takov<6F> podm<64>nky
|
||||
(a<> ji<6A> p<><70>kazem soudu, smlouvou nebo jinak), kter<65> se vylu<6C>uj<75> s podm<64>nkami
|
||||
t<EFBFBD>to licence, nejste t<>m osvobozen od podm<64>nek t<>to licence. Pokud nem<65><6D>ete
|
||||
<EFBFBD><EFBFBD><EFBFBD>it program tak, abyste vyhov<6F>l z<>rove<76> sv<73>m z<>vazk<7A>m vypl<70>vaj<61>c<EFBFBD>m z t<>to
|
||||
licence a jin<69>m platn<74>m z<>vazk<7A>m, nesm<73>te jej v d<>sledku toho <20><><EFBFBD>it v<>bec. Pokud
|
||||
by nap<61><70>klad patentov<6F> osv<73>d<EFBFBD>en<65> nepovolovalo bezplatnou redistribuci programu
|
||||
v<EFBFBD>emi, kdo va<76><61>m p<>i<EFBFBD>in<69>n<EFBFBD>m z<>skaj<61> p<><70>mo nebo nep<65><70>mo jeho kopie, pak by jedin<69>
|
||||
mo<EFBFBD>n<EFBFBD> zp<7A>sob jak vyhov<6F>t z<>rove<76> patentov<6F>mu osv<73>d<EFBFBD>en<65> i t<>to licenci spo<70><6F>val v
|
||||
ukon<EFBFBD>en<EFBFBD> distribuce programu.
|
||||
|
||||
Pokud by se za n<>jak<61>ch specifick<63>ch okolnost<73> jevila n<>kter<65> <20><>st tohoto
|
||||
paragrafu jako neplatn<74> nebo nevynutiteln<6C>, pova<76>uje se za sm<73>rodatnou rovnov<6F>ha
|
||||
vyj<EFBFBD>d<EFBFBD>en<EFBFBD> t<>mto paragrafem a paragraf jako celek se pova<76>uje za sm<73>rodatn<74> za
|
||||
jin<EFBFBD>ch okolnost<73>.
|
||||
|
||||
Smyslem tohoto paragrafu nen<65> nav<61>d<EFBFBD>t v<>s k poru<72>ov<6F>n<EFBFBD> patent<6E> <20>i jin<69>ch
|
||||
ustanoven<EFBFBD> vlastnick<63>ho pr<70>va, anebo tato ustanoven<65> zpochyb<79>ovat; jedin<69>m jeho
|
||||
smyslem je ochrana integrity syst<73>mu <20><><EFBFBD>en<65> svobodn<64>ho softwaru, kter<65> je
|
||||
podlo<EFBFBD>en ve<76>ejn<6A>mi licen<65>n<EFBFBD>mi p<>edpisy. Mnoz<6F> lid<69> poskytli sv<73> p<><70>sp<73>vky do
|
||||
<EFBFBD>irok<EFBFBD>ho okruhu softwaru <20><><EFBFBD>en<65>ho t<>mto syst<73>mem, spolehnuv<75>e se na jeho
|
||||
d<EFBFBD>sledn<EFBFBD> uplat<61>ov<6F>n<EFBFBD>; z<>le<6C><65> na autorovi/d<>rci, aby rozhodl, zda si p<>eje <20><><EFBFBD>it
|
||||
software pomoc<6F> n<>jak<61>ho jin<69>ho syst<73>mu a <20><>dn<64> u<>ivatel licence nem<65><6D>e takov<6F>
|
||||
rozhodnut<EFBFBD> zpochyb<79>ovat.
|
||||
|
||||
Smyslem tohoto paragrafu je zevrubn<62> osv<73>tlit to, co je pova<76>ov<6F>no za d<>sledek
|
||||
plynouc<EFBFBD> ze zbytku t<>to licence.
|
||||
|
||||
8. Pokud je <20><><EFBFBD>en<65> <20>i pou<6F>it<69> programu v n<>kter<65>ch zem<65>ch omezeno bu<62> patenty
|
||||
anebo autorsky chr<68>n<EFBFBD>n<EFBFBD>mi rozhran<61>mi, m<><6D>e dr<64>itel p<>vodn<64>ch autorsk<73>ch pr<70>v,
|
||||
kter<EFBFBD> sv<73><76>uje program do p<>sobnosti t<>to licence, p<>idat v<>slovn<76> omezen<65> pro
|
||||
geografick<EFBFBD> <20><><EFBFBD>en<65>, vylu<6C>uj<75>c<EFBFBD> takov<6F> zem<65>, tak<61>e <20><><EFBFBD>en<65> je povoleno jen v t<>ch
|
||||
zem<EFBFBD>ch nebo mezi t<>mi zem<65>mi, kter<65> nejsou t<>mto zp<7A>sobem vylou<6F>eny. Tato
|
||||
licence zahrnuje v tomto p<><70>pad<61> takov<6F> omezen<65> p<>esn<73> tak, jako by bylo zaps<70>no
|
||||
v textu t<>to licence.
|
||||
|
||||
9. Free Software Foundation m<><6D>e <20>as od <20>asu vyd<79>vat upraven<65> nebo nov<6F> verze
|
||||
General Public License. Takov<6F> nov<6F> verze se budou sv<73>m duchem podobat sou<6F>asn<73>
|
||||
verzi, v jednotlivostech se v<>ak mohou li<6C>it s ohledem na nov<6F> probl<62>my <20>i z<>jmy.
|
||||
|
||||
Ka<EFBFBD>d<EFBFBD> verzi je p<>id<69>leno rozli<6C>uj<75>c<EFBFBD> <20><>slo verze. Pokud program specifikuje
|
||||
<EFBFBD><EFBFBD>slo verze, kter<65> se na n<>j vztahuje, a "v<>echny n<>sleduj<75>c<EFBFBD> verze", m<><6D>ete se
|
||||
podle uv<75><76>en<65> <20><>dit ustanoven<65>mi a podm<64>nkami bu<62>to on<6F> konkr<6B>tn<74> verze anebo
|
||||
kter<EFBFBD>koliv n<>sleduj<75>c<EFBFBD> verze, kterou vydala Free Software Foundation. Jestli<6C>e
|
||||
program nespecifikuje <20><>slo verze t<>to licence, m<><6D>ete si vybrat libovolnou
|
||||
verzi, kterou kdy Free Software Foundation vydala.
|
||||
|
||||
10. Pokud si p<>ejete zahrnout <20><>sti programu do jin<69>ch svobodn<64>ch program<61>,
|
||||
jejich<EFBFBD> distribu<62>n<EFBFBD> podm<64>nky jsou odli<6C>n<EFBFBD>, za<7A>lete autorovi <20><>dost o povolen<65>. V
|
||||
p<EFBFBD><EFBFBD>pad<EFBFBD> softwaru, k n<>mu<6D> vlastn<74> autorsk<73> pr<70>va Free Software Foundation,
|
||||
napi<EFBFBD>te Free Software Foundation; n<>kdy <20>in<69>me v<>jimky ze zde uveden<65>ch
|
||||
ustanoven<EFBFBD>. Na<4E>e rozhodnut<75> bude vedeno dv<64>ma c<>li: zachov<6F>n<EFBFBD>m svobodn<64> povahy
|
||||
v<EFBFBD>ech odvozenin na<6E>eho svobodn<64>ho softwaru a podporou sd<73>len<65> a op<6F>tovn<76>ho
|
||||
vyu<EFBFBD>it<EFBFBD> softwaru obecn<63>.
|
||||
|
||||
Z<>RUKA SE NEPOSKYTUJE
|
||||
|
||||
11. VZHLEDEM K BEZPLATN<54>MU POSKYTNUT<55> LICENCE K PROGRAMU SE NA PROGRAM
|
||||
NEVZTAHUJE <20><>DN<44> Z<>RUKA, A TO V M<><4D>E POVOLEN<45> PLATN<54>M Z<>KONEM. POKUD NEN<45>
|
||||
P<EFBFBD>SEMN<EFBFBD> STANOVENO JINAK, POSKYTUJ<55> DR<44>ITEL<45> AUTORSK<53>CH PR<50>V POP<4F><50>PAD<41> JIN<49>
|
||||
STRANY PROGRAM "TAK, JAK JE", BEZ Z<>RUKY JAK<41>HOKOLI DRUHU, A<> V<>SLOVN<56> NEBO
|
||||
VYPL<EFBFBD>VAJ<EFBFBD>C<EFBFBD>, V<>ETN<54>, ALE NIKOLI JEN, Z<>RUK PRODEJNOSTI A VHODNOSTI PRO UR<55>IT<49>
|
||||
<EFBFBD><EFBFBD>EL. POKUD JDE O KVALITU A V<>KONNOST PROGRAMU, LE<4C><45> VE<56>KER<45> RIZIKO NA V<>S.
|
||||
POKUD BY SE U PROGRAMU PROJEVILY Z<>VADY, PADAJ<41> N<>KLADY ZA V<>ECHNU POT<4F>EBNOU
|
||||
<EFBFBD>DR<EFBFBD>BU, OPRAVU <20>I N<>PRAVU NA V<><56> VRUB.
|
||||
|
||||
12. V <20><>DN<44>M P<><50>PAD<41>, S V<>JIMKOU TOHO, KDY<44> TO VY<56>ADUJE PLATN<54> Z<>KON, ANEBO KDY<44>
|
||||
TO BYLO P<>SEMN<4D> ODSOUHLASENO, V<>M NEBUDE <20><>DN<44> Z DR<44>ITEL<45> AUTORSK<53>CH PR<50>V ANI
|
||||
<EFBFBD><EFBFBD>DN<EFBFBD> JIN<49> STRANA, KTER<45> SM<53> MODIFIKOVAT <20>I <20><><EFBFBD>IT PROGRAM V SOULADU S
|
||||
P<EFBFBD>EDCHOZ<EFBFBD>MI USTANOVEN<45>MI, ODPOV<4F>DNI ZA <20>KODY, V<>ETN<54> V<>ECH OBECN<43>CH, SPECI<43>LN<4C>CH,
|
||||
NAHODIL<EFBFBD>CH NEBO N<>SLEDN<44>CH <20>KOD VYPL<50>VAJ<41>C<EFBFBD>CH Z U<><55>V<EFBFBD>N<EFBFBD> ANEBO NESCHOPNOSTI
|
||||
U<EFBFBD><EFBFBD>VAT PROGRAMU (V<>ETN<54>, ALE NIKOLI JEN, ZTR<54>TY NEBO ZKRESLEN<45> DAT, NEBO
|
||||
TRVAL<EFBFBD>CH <20>KOD ZP<5A>SOBEN<45>CH V<>M NEBO T<>ET<45>M STRAN<41>M, NEBO SELH<4C>N<EFBFBD> FUNKCE PROGRAMU
|
||||
V SOU<4F>INNOSTI S JIN<49>MI PROGRAMY), A TO I V P<><50>PAD<41>, <20>E TAKOV<4F> DR<44>ITEL AUTORSK<53>CH
|
||||
PR<EFBFBD>V NEBO JIN<49> STRANA BYLI UPOZORN<52>NI NA MO<4D>NOST TAKOV<4F>CH <20>KOD.
|
||||
|
||||
KONEC USTANOVEN<45> A PODM<44>NEK
|
||||
|
||||
|
||||
----------------------------------------------------------------------------------
|
||||
(c) 2004, Str<74>nky o svobodn<64>m software (info@gnu.cz)
|
||||
Tento p<>eklad je z velk<6C> <20><>sti zalo<6C>en na p<>ekladu od Ladislava Lhotky.
|
||||
V p<><70>pad<61> dopl<70>uj<75>c<EFBFBD>ch informac<61> nebo oprav kontaktujte maintainera: kysela@gnu.cz
|
||||
Posledn<EFBFBD> <20>prava: 22. 12. 2004
|
||||
----------------------------------------------------------------------------------
|
279
license/license.gpl.txt
Normal file
279
license/license.gpl.txt
Normal file
@@ -0,0 +1,279 @@
|
||||
GNU GENERAL PUBLIC LICENSE
|
||||
Version 2, June 1991
|
||||
|
||||
Copyright (C) 1989, 1991 Free Software Foundation, Inc.
|
||||
675 Mass Ave, Cambridge, MA 02139, USA
|
||||
Everyone is permitted to copy and distribute verbatim copies
|
||||
of this license document, but changing it is not allowed.
|
||||
|
||||
Preamble
|
||||
|
||||
The licenses for most software are designed to take away your
|
||||
freedom to share and change it. By contrast, the GNU General Public
|
||||
License is intended to guarantee your freedom to share and change free
|
||||
software--to make sure the software is free for all its users. This
|
||||
General Public License applies to most of the Free Software
|
||||
Foundation's software and to any other program whose authors commit to
|
||||
using it. (Some other Free Software Foundation software is covered by
|
||||
the GNU Library General Public License instead.) You can apply it to
|
||||
your programs, too.
|
||||
|
||||
When we speak of free software, we are referring to freedom, not
|
||||
price. Our General Public Licenses are designed to make sure that you
|
||||
have the freedom to distribute copies of free software (and charge for
|
||||
this service if you wish), that you receive source code or can get it
|
||||
if you want it, that you can change the software or use pieces of it
|
||||
in new free programs; and that you know you can do these things.
|
||||
|
||||
To protect your rights, we need to make restrictions that forbid
|
||||
anyone to deny you these rights or to ask you to surrender the rights.
|
||||
These restrictions translate to certain responsibilities for you if you
|
||||
distribute copies of the software, or if you modify it.
|
||||
|
||||
For example, if you distribute copies of such a program, whether
|
||||
gratis or for a fee, you must give the recipients all the rights that
|
||||
you have. You must make sure that they, too, receive or can get the
|
||||
source code. And you must show them these terms so they know their
|
||||
rights.
|
||||
|
||||
We protect your rights with two steps: (1) copyright the software, and
|
||||
(2) offer you this license which gives you legal permission to copy,
|
||||
distribute and/or modify the software.
|
||||
|
||||
Also, for each author's protection and ours, we want to make certain
|
||||
that everyone understands that there is no warranty for this free
|
||||
software. If the software is modified by someone else and passed on, we
|
||||
want its recipients to know that what they have is not the original, so
|
||||
that any problems introduced by others will not reflect on the original
|
||||
authors' reputations.
|
||||
|
||||
Finally, any free program is threatened constantly by software
|
||||
patents. We wish to avoid the danger that redistributors of a free
|
||||
program will individually obtain patent licenses, in effect making the
|
||||
program proprietary. To prevent this, we have made it clear that any
|
||||
patent must be licensed for everyone's free use or not licensed at all.
|
||||
|
||||
The precise terms and conditions for copying, distribution and
|
||||
modification follow.
|
||||
|
||||
GNU GENERAL PUBLIC LICENSE
|
||||
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
|
||||
|
||||
0. This License applies to any program or other work which contains
|
||||
a notice placed by the copyright holder saying it may be distributed
|
||||
under the terms of this General Public License. The "Program", below,
|
||||
refers to any such program or work, and a "work based on the Program"
|
||||
means either the Program or any derivative work under copyright law:
|
||||
that is to say, a work containing the Program or a portion of it,
|
||||
either verbatim or with modifications and/or translated into another
|
||||
language. (Hereinafter, translation is included without limitation in
|
||||
the term "modification".) Each licensee is addressed as "you".
|
||||
|
||||
Activities other than copying, distribution and modification are not
|
||||
covered by this License; they are outside its scope. The act of
|
||||
running the Program is not restricted, and the output from the Program
|
||||
is covered only if its contents constitute a work based on the
|
||||
Program (independent of having been made by running the Program).
|
||||
Whether that is true depends on what the Program does.
|
||||
|
||||
1. You may copy and distribute verbatim copies of the Program's
|
||||
source code as you receive it, in any medium, provided that you
|
||||
conspicuously and appropriately publish on each copy an appropriate
|
||||
copyright notice and disclaimer of warranty; keep intact all the
|
||||
notices that refer to this License and to the absence of any warranty;
|
||||
and give any other recipients of the Program a copy of this License
|
||||
along with the Program.
|
||||
|
||||
You may charge a fee for the physical act of transferring a copy, and
|
||||
you may at your option offer warranty protection in exchange for a fee.
|
||||
|
||||
2. You may modify your copy or copies of the Program or any portion
|
||||
of it, thus forming a work based on the Program, and copy and
|
||||
distribute such modifications or work under the terms of Section 1
|
||||
above, provided that you also meet all of these conditions:
|
||||
|
||||
a) You must cause the modified files to carry prominent notices
|
||||
stating that you changed the files and the date of any change.
|
||||
|
||||
b) You must cause any work that you distribute or publish, that in
|
||||
whole or in part contains or is derived from the Program or any
|
||||
part thereof, to be licensed as a whole at no charge to all third
|
||||
parties under the terms of this License.
|
||||
|
||||
c) If the modified program normally reads commands interactively
|
||||
when run, you must cause it, when started running for such
|
||||
interactive use in the most ordinary way, to print or display an
|
||||
announcement including an appropriate copyright notice and a
|
||||
notice that there is no warranty (or else, saying that you provide
|
||||
a warranty) and that users may redistribute the program under
|
||||
these conditions, and telling the user how to view a copy of this
|
||||
License. (Exception: if the Program itself is interactive but
|
||||
does not normally print such an announcement, your work based on
|
||||
the Program is not required to print an announcement.)
|
||||
|
||||
These requirements apply to the modified work as a whole. If
|
||||
identifiable sections of that work are not derived from the Program,
|
||||
and can be reasonably considered independent and separate works in
|
||||
themselves, then this License, and its terms, do not apply to those
|
||||
sections when you distribute them as separate works. But when you
|
||||
distribute the same sections as part of a whole which is a work based
|
||||
on the Program, the distribution of the whole must be on the terms of
|
||||
this License, whose permissions for other licensees extend to the
|
||||
entire whole, and thus to each and every part regardless of who wrote it.
|
||||
Thus, it is not the intent of this section to claim rights or contest
|
||||
your rights to work written entirely by you; rather, the intent is to
|
||||
exercise the right to control the distribution of derivative or
|
||||
collective works based on the Program.
|
||||
|
||||
In addition, mere aggregation of another work not based on the Program
|
||||
with the Program (or with a work based on the Program) on a volume of
|
||||
a storage or distribution medium does not bring the other work under
|
||||
the scope of this License.
|
||||
|
||||
3. You may copy and distribute the Program (or a work based on it,
|
||||
under Section 2) in object code or executable form under the terms of
|
||||
Sections 1 and 2 above provided that you also do one of the following:
|
||||
|
||||
a) Accompany it with the complete corresponding machine-readable
|
||||
source code, which must be distributed under the terms of Sections
|
||||
1 and 2 above on a medium customarily used for software interchange; or,
|
||||
|
||||
b) Accompany it with a written offer, valid for at least three
|
||||
years, to give any third party, for a charge no more than your
|
||||
cost of physically performing source distribution, a complete
|
||||
machine-readable copy of the corresponding source code, to be
|
||||
distributed under the terms of Sections 1 and 2 above on a medium
|
||||
customarily used for software interchange; or,
|
||||
|
||||
c) Accompany it with the information you received as to the offer
|
||||
to distribute corresponding source code. (This alternative is
|
||||
allowed only for noncommercial distribution and only if you
|
||||
received the program in object code or executable form with such
|
||||
an offer, in accord with Subsection b above.)
|
||||
|
||||
The source code for a work means the preferred form of the work for
|
||||
making modifications to it. For an executable work, complete source
|
||||
code means all the source code for all modules it contains, plus any
|
||||
associated interface definition files, plus the scripts used to
|
||||
control compilation and installation of the executable. However, as a
|
||||
special exception, the source code distributed need not include
|
||||
anything that is normally distributed (in either source or binary
|
||||
form) with the major components (compiler, kernel, and so on) of the
|
||||
operating system on which the executable runs, unless that component
|
||||
itself accompanies the executable.
|
||||
|
||||
If distribution of executable or object code is made by offering
|
||||
access to copy from a designated place, then offering equivalent
|
||||
access to copy the source code from the same place counts as
|
||||
distribution of the source code, even though third parties are not
|
||||
compelled to copy the source along with the object code.
|
||||
|
||||
4. You may not copy, modify, sublicense, or distribute the Program
|
||||
except as expressly provided under this License. Any attempt
|
||||
otherwise to copy, modify, sublicense or distribute the Program is
|
||||
void, and will automatically terminate your rights under this License.
|
||||
However, parties who have received copies, or rights, from you under
|
||||
this License will not have their licenses terminated so long as such
|
||||
parties remain in full compliance.
|
||||
|
||||
5. You are not required to accept this License, since you have not
|
||||
signed it. However, nothing else grants you permission to modify or
|
||||
distribute the Program or its derivative works. These actions are
|
||||
prohibited by law if you do not accept this License. Therefore, by
|
||||
modifying or distributing the Program (or any work based on the
|
||||
Program), you indicate your acceptance of this License to do so, and
|
||||
all its terms and conditions for copying, distributing or modifying
|
||||
the Program or works based on it.
|
||||
|
||||
6. Each time you redistribute the Program (or any work based on the
|
||||
Program), the recipient automatically receives a license from the
|
||||
original licensor to copy, distribute or modify the Program subject to
|
||||
these terms and conditions. You may not impose any further
|
||||
restrictions on the recipients' exercise of the rights granted herein.
|
||||
You are not responsible for enforcing compliance by third parties to
|
||||
this License.
|
||||
|
||||
7. If, as a consequence of a court judgment or allegation of patent
|
||||
infringement or for any other reason (not limited to patent issues),
|
||||
conditions are imposed on you (whether by court order, agreement or
|
||||
otherwise) that contradict the conditions of this License, they do not
|
||||
excuse you from the conditions of this License. If you cannot
|
||||
distribute so as to satisfy simultaneously your obligations under this
|
||||
License and any other pertinent obligations, then as a consequence you
|
||||
may not distribute the Program at all. For example, if a patent
|
||||
license would not permit royalty-free redistribution of the Program by
|
||||
all those who receive copies directly or indirectly through you, then
|
||||
the only way you could satisfy both it and this License would be to
|
||||
refrain entirely from distribution of the Program.
|
||||
|
||||
If any portion of this section is held invalid or unenforceable under
|
||||
any particular circumstance, the balance of the section is intended to
|
||||
apply and the section as a whole is intended to apply in other
|
||||
circumstances.
|
||||
|
||||
It is not the purpose of this section to induce you to infringe any
|
||||
patents or other property right claims or to contest validity of any
|
||||
such claims; this section has the sole purpose of protecting the
|
||||
integrity of the free software distribution system, which is
|
||||
implemented by public license practices. Many people have made
|
||||
generous contributions to the wide range of software distributed
|
||||
through that system in reliance on consistent application of that
|
||||
system; it is up to the author/donor to decide if he or she is willing
|
||||
to distribute software through any other system and a licensee cannot
|
||||
impose that choice.
|
||||
|
||||
This section is intended to make thoroughly clear what is believed to
|
||||
be a consequence of the rest of this License.
|
||||
|
||||
8. If the distribution and/or use of the Program is restricted in
|
||||
certain countries either by patents or by copyrighted interfaces, the
|
||||
original copyright holder who places the Program under this License
|
||||
may add an explicit geographical distribution limitation excluding
|
||||
those countries, so that distribution is permitted only in or among
|
||||
countries not thus excluded. In such case, this License incorporates
|
||||
the limitation as if written in the body of this License.
|
||||
|
||||
9. The Free Software Foundation may publish revised and/or new versions
|
||||
of the General Public License from time to time. Such new versions will
|
||||
be similar in spirit to the present version, but may differ in detail to
|
||||
address new problems or concerns.
|
||||
|
||||
Each version is given a distinguishing version number. If the Program
|
||||
specifies a version number of this License which applies to it and "any
|
||||
later version", you have the option of following the terms and conditions
|
||||
either of that version or of any later version published by the Free
|
||||
Software Foundation. If the Program does not specify a version number of
|
||||
this License, you may choose any version ever published by the Free Software
|
||||
Foundation.
|
||||
|
||||
10. If you wish to incorporate parts of the Program into other free
|
||||
programs whose distribution conditions are different, write to the author
|
||||
to ask for permission. For software which is copyrighted by the Free
|
||||
Software Foundation, write to the Free Software Foundation; we sometimes
|
||||
make exceptions for this. Our decision will be guided by the two goals
|
||||
of preserving the free status of all derivatives of our free software and
|
||||
of promoting the sharing and reuse of software generally.
|
||||
|
||||
NO WARRANTY
|
||||
|
||||
11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
|
||||
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
|
||||
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
|
||||
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
|
||||
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
|
||||
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
|
||||
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
|
||||
REPAIR OR CORRECTION.
|
||||
|
||||
12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
||||
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
|
||||
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
|
||||
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
|
||||
OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
|
||||
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
|
||||
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
|
||||
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
|
||||
POSSIBILITY OF SUCH DAMAGES.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
@@ -1,9 +0,0 @@
|
||||
parameters:
|
||||
level: 5
|
||||
|
||||
paths:
|
||||
- src
|
||||
|
||||
ignoreErrors:
|
||||
# The namespace is referenced, not the class.
|
||||
- '#Class dibi referenced with incorrect case: Dibi#'
|
664
readme.md
664
readme.md
@@ -1,664 +0,0 @@
|
||||
[Dibi](https://dibiphp.com) - smart database layer for PHP [](https://nette.org/make-donation?to=dibi)
|
||||
=========================================================
|
||||
|
||||
[](https://packagist.org/packages/dibi/dibi)
|
||||
[](https://github.com/dg/dibi/actions)
|
||||
[](https://ci.appveyor.com/project/dg/dibi/branch/master)
|
||||
[](https://github.com/dg/dibi/releases)
|
||||
[](https://github.com/dg/dibi/blob/master/license.md)
|
||||
|
||||
|
||||
Introduction
|
||||
------------
|
||||
|
||||
Database access functions in PHP are not standardised. This library
|
||||
hides the differences between them, and above all, it gives you a very handy interface.
|
||||
|
||||
|
||||
Support Me
|
||||
----------
|
||||
|
||||
Do you like Dibi? Are you looking forward to the new features?
|
||||
|
||||
[](https://github.com/sponsors/dg)
|
||||
|
||||
Thank you!
|
||||
|
||||
|
||||
Installation
|
||||
------------
|
||||
|
||||
Install Dibi via Composer:
|
||||
|
||||
```bash
|
||||
composer require dibi/dibi
|
||||
```
|
||||
|
||||
The Dibi 4.2 requires PHP version 7.2 and supports PHP up to 8.1.
|
||||
|
||||
|
||||
Usage
|
||||
-----
|
||||
|
||||
Refer to the `examples` directory for examples. Dibi documentation is
|
||||
available on the [homepage](https://dibiphp.com).
|
||||
|
||||
|
||||
### Connecting to database
|
||||
|
||||
The database connection is represented by the object `Dibi\Connection`:
|
||||
|
||||
```php
|
||||
$database = new Dibi\Connection([
|
||||
'driver' => 'mysqli',
|
||||
'host' => 'localhost',
|
||||
'username' => 'root',
|
||||
'password' => '***',
|
||||
'database' => 'table',
|
||||
]);
|
||||
|
||||
$result = $database->query('SELECT * FROM users');
|
||||
```
|
||||
|
||||
Alternatively, you can use the `dibi` static register, which maintains a connection object in a globally available storage and calls all the functions above it:
|
||||
|
||||
```php
|
||||
dibi::connect([
|
||||
'driver' => 'mysqli',
|
||||
'host' => 'localhost',
|
||||
'username' => 'root',
|
||||
'password' => '***',
|
||||
'database' => 'test',
|
||||
'charset' => 'utf8',
|
||||
]);
|
||||
|
||||
$result = dibi::query('SELECT * FROM users');
|
||||
```
|
||||
|
||||
In the event of a connection error, it throws `Dibi\Exception`.
|
||||
|
||||
|
||||
|
||||
### Queries
|
||||
|
||||
We query the database queries by the method `query()` which returns `Dibi\Result`. Rows are objects `Dibi\Row`.
|
||||
|
||||
You can try all the examples [online at the playground](https://repl.it/@DavidGrudl/dibi-playground).
|
||||
|
||||
```php
|
||||
$result = $database->query('SELECT * FROM users');
|
||||
|
||||
foreach ($result as $row) {
|
||||
echo $row->id;
|
||||
echo $row->name;
|
||||
}
|
||||
|
||||
// array of all rows
|
||||
$all = $result->fetchAll();
|
||||
|
||||
// array of all rows, key is 'id'
|
||||
$all = $result->fetchAssoc('id');
|
||||
|
||||
// associative pairs id => name
|
||||
$pairs = $result->fetchPairs('id', 'name');
|
||||
|
||||
// the number of rows of the result, if known, or number of affected rows
|
||||
$count = $result->getRowCount();
|
||||
```
|
||||
|
||||
Method fetchAssoc() can return a more complex associative array.
|
||||
|
||||
You can easily add parameters to the query, note the question mark:
|
||||
|
||||
```php
|
||||
$result = $database->query('SELECT * FROM users WHERE name = ? AND active = ?', $name, $active);
|
||||
|
||||
// or
|
||||
$result = $database->query('SELECT * FROM users WHERE name = ?', $name, 'AND active = ?', $active););
|
||||
|
||||
$ids = [10, 20, 30];
|
||||
$result = $database->query('SELECT * FROM users WHERE id IN (?)', $ids);
|
||||
```
|
||||
|
||||
**WARNING: Never concatenate parameters to SQL. It would create a [SQL injection](https://en.wikipedia.org/wiki/SQL_injection)** vulnerability.
|
||||
```
|
||||
$result = $database->query('SELECT * FROM users WHERE id = ' . $id); // BAD!!!
|
||||
```
|
||||
|
||||
Instead of a question mark, so-called modifiers can be used.
|
||||
|
||||
```php
|
||||
$result = $database->query('SELECT * FROM users WHERE name = %s', $name);
|
||||
```
|
||||
|
||||
In case of failure `query()` throws `Dibi\Exception`, or one of the descendants:
|
||||
|
||||
- `ConstraintViolationException` - violation of a table constraint
|
||||
- `ForeignKeyConstraintViolationException` - invalid foreign key
|
||||
- `NotNullConstraintViolationException` - violation of the NOT NULL condition
|
||||
- `UniqueConstraintViolationException` - collides unique index
|
||||
|
||||
You can use also shortcuts:
|
||||
|
||||
```php
|
||||
// returns associative pairs id => name, shortcut for query(...)->fetchPairs()
|
||||
$pairs = $database->fetchPairs('SELECT id, name FROM users');
|
||||
|
||||
// returns array of all rows, shortcut for query(...)->fetchAll()
|
||||
$rows = $database->fetchAll('SELECT * FROM users');
|
||||
|
||||
// returns row, shortcut for query(...)->fetch()
|
||||
$row = $database->fetch('SELECT * FROM users WHERE id = ?', $id);
|
||||
|
||||
// returns field, shortcut for query(...)->fetchSingle()
|
||||
$name = $database->fetchSingle('SELECT name FROM users WHERE id = ?', $id);
|
||||
```
|
||||
|
||||
|
||||
### Modifiers
|
||||
|
||||
In addition to the `?` wildcard char, we can also use modifiers:
|
||||
|
||||
| modifier | description
|
||||
|----------|-----
|
||||
| %s | string
|
||||
| %sN | string, but '' translates as NULL
|
||||
| %bin | binary data
|
||||
| %b | boolean
|
||||
| %i | integer
|
||||
| %iN | integer, but 0 is translates as NULL
|
||||
| %f | float
|
||||
| %d | date (accepts DateTime, string or UNIX timestamp)
|
||||
| %dt | datetime (accepts DateTime, string or UNIX timestamp)
|
||||
| %n | identifier, ie the name of the table or column
|
||||
| %N | identifier, treats period as a common character, ie alias or a database name (`%n AS %N` or `DROP DATABASE %N`)
|
||||
| %SQL | SQL - directly inserts into SQL (the alternative is Dibi\Literal)
|
||||
| %ex | SQL expression or array of expressions
|
||||
| %lmt | special - adds LIMIT to the query
|
||||
| %ofs | special - adds OFFSET to the query
|
||||
|
||||
Example:
|
||||
|
||||
```php
|
||||
$result = $database->query('SELECT * FROM users WHERE name = %s', $name);
|
||||
```
|
||||
|
||||
If $name is null, the NULL is inserted into the SQL statement.
|
||||
|
||||
If the variable is an array, the modifier is applied to all of its elements and they are inserted into SQL separated by commas:
|
||||
|
||||
```php
|
||||
$ids = [10, '20', 30];
|
||||
$result = $database->query('SELECT * FROM users WHERE id IN (%i)', $ids);
|
||||
// SELECT * FROM users WHERE id IN (10, 20, 30)
|
||||
```
|
||||
|
||||
The modifier `%n` is used if the table or column name is a variable. (Beware, do not allow the user to manipulate the content of such a variable):
|
||||
|
||||
```php
|
||||
$table = 'blog.users';
|
||||
$column = 'name';
|
||||
$result = $database->query('SELECT * FROM %n WHERE %n = ?', $table, $column, $value);
|
||||
// SELECT * FROM `blog`.`users` WHERE `name` = 'Jim'
|
||||
```
|
||||
|
||||
Three special modifiers are available for LIKE:
|
||||
|
||||
| modifier | description
|
||||
|----------|-----
|
||||
| `%like~` | the expression starts with a string
|
||||
| `%~like` | the expression ends with a string
|
||||
| `%~like~` | the expression contains a string
|
||||
| `%like` | the expression matches a string
|
||||
|
||||
Search for names beginning with a string:
|
||||
|
||||
```php
|
||||
$result = $database->query('SELECT * FROM table WHERE name LIKE %like~', $query);
|
||||
```
|
||||
|
||||
|
||||
### Modifiers for arrays
|
||||
|
||||
The parameter entered in the SQL query can also be an array. These modifiers determine how to compile the SQL statement:
|
||||
|
||||
| modifier | | result
|
||||
|----------|---|-----
|
||||
| %and | | `key1 = value1 AND key2 = value2 AND ...`
|
||||
| %or | | `key1 = value1 OR key2 = value2 OR ...`
|
||||
| %a | assoc | `key1 = value1, key2 = value2, ...`
|
||||
| %l %in | list | `(val1, val2, ...)`
|
||||
| %v | values | `(key1, key2, ...) VALUES (value1, value2, ...)`
|
||||
| %m | multi | `(key1, key2, ...) VALUES (value1, value2, ...), (value1, value2, ...), ...`
|
||||
| %by | ordering | `key1 ASC, key2 DESC ...`
|
||||
| %n | names | `key1, key2 AS alias, ...`
|
||||
|
||||
Example:
|
||||
|
||||
```php
|
||||
$arr = [
|
||||
'a' => 'hello',
|
||||
'b' => true,
|
||||
];
|
||||
|
||||
$database->query('INSERT INTO table %v', $arr);
|
||||
// INSERT INTO `table` (`a`, `b`) VALUES ('hello', 1)
|
||||
|
||||
$database->query('UPDATE `table` SET %a', $arr);
|
||||
// UPDATE `table` SET `a`='hello', `b`=1
|
||||
```
|
||||
|
||||
In the WHERE clause modifiers `%and` nebo `%or` can be used:
|
||||
|
||||
```php
|
||||
$result = $database->query('SELECT * FROM users WHERE %and', [
|
||||
'name' => $name,
|
||||
'year' => $year,
|
||||
]);
|
||||
// SELECT * FROM users WHERE `name` = 'Jim' AND `year` = 1978
|
||||
```
|
||||
|
||||
The modifier `%by` is used to sort, the keys show the columns, and the boolean value will determine whether to sort in ascending order:
|
||||
|
||||
```php
|
||||
$result = $database->query('SELECT id FROM author ORDER BY %by', [
|
||||
'id' => true, // ascending
|
||||
'name' => false, // descending
|
||||
]);
|
||||
// SELECT id FROM author ORDER BY `id`, `name` DESC
|
||||
```
|
||||
|
||||
|
||||
### Insert, Update & Delete
|
||||
|
||||
We insert the data into an SQL query as an associative array. Modifiers and wildcards `?` are not required in these cases.
|
||||
|
||||
```php
|
||||
$database->query('INSERT INTO users', [
|
||||
'name' => $name,
|
||||
'year' => $year,
|
||||
]);
|
||||
// INSERT INTO users (`name`, `year`) VALUES ('Jim', 1978)
|
||||
|
||||
$id = $database->getInsertId(); // returns the auto-increment of the inserted record
|
||||
|
||||
$id = $database->getInsertId($sequence); // or sequence value
|
||||
```
|
||||
|
||||
Multiple INSERT:
|
||||
|
||||
```php
|
||||
$database->query('INSERT INTO users', [
|
||||
'name' => 'Jim',
|
||||
'year' => 1978,
|
||||
], [
|
||||
'name' => 'Jack',
|
||||
'year' => 1987,
|
||||
]);
|
||||
// INSERT INTO users (`name`, `year`) VALUES ('Jim', 1978), ('Jack', 1987)
|
||||
```
|
||||
|
||||
Deleting:
|
||||
|
||||
```php
|
||||
$database->query('DELETE FROM users WHERE id = ?', $id);
|
||||
|
||||
// returns the number of deleted rows
|
||||
$affectedRows = $database->getAffectedRows();
|
||||
```
|
||||
|
||||
Update:
|
||||
|
||||
```php
|
||||
$database->query('UPDATE users SET', [
|
||||
'name' => $name,
|
||||
'year' => $year,
|
||||
], 'WHERE id = ?', $id);
|
||||
// UPDATE users SET `name` = 'Jim', `year` = 1978 WHERE id = 123
|
||||
|
||||
// returns the number of updated rows
|
||||
$affectedRows = $database->getAffectedRows();
|
||||
```
|
||||
|
||||
Insert an entry or update if it already exists:
|
||||
|
||||
```php
|
||||
$database->query('INSERT INTO users', [
|
||||
'id' => $id,
|
||||
'name' => $name,
|
||||
'year' => $year,
|
||||
], 'ON DUPLICATE KEY UPDATE %a', [ // here the modifier %a must be used
|
||||
'name' => $name,
|
||||
'year' => $year,
|
||||
]);
|
||||
// INSERT INTO users (`id`, `name`, `year`) VALUES (123, 'Jim', 1978)
|
||||
// ON DUPLICATE KEY UPDATE `name` = 'Jim', `year` = 1978
|
||||
```
|
||||
|
||||
|
||||
### Transaction
|
||||
|
||||
There are three methods for dealing with transactions:
|
||||
|
||||
```php
|
||||
$database->beginTransaction();
|
||||
|
||||
$database->commit();
|
||||
|
||||
$database->rollback();
|
||||
```
|
||||
|
||||
|
||||
### Testing
|
||||
|
||||
In order to play with Dibi a little, there is a `test()` method that you pass parameters like to `query()`, but instead of executing the SQL statement, it is echoed on the screen.
|
||||
|
||||
The query results can be echoed as a table using `$result->dump()`.
|
||||
|
||||
These variables are also available:
|
||||
|
||||
```php
|
||||
dibi::$sql; // the latest SQL query
|
||||
dibi::$elapsedTime; // its duration in sec
|
||||
dibi::$numOfQueries;
|
||||
dibi::$totalTime;
|
||||
```
|
||||
|
||||
|
||||
### Complex queries
|
||||
|
||||
The parameter may also be an object `DateTime`.
|
||||
|
||||
```php
|
||||
$result = $database->query('SELECT * FROM users WHERE created < ?', new DateTime);
|
||||
|
||||
$database->query('INSERT INTO users', [
|
||||
'created' => new DateTime,
|
||||
]);
|
||||
```
|
||||
|
||||
Or SQL literal:
|
||||
|
||||
```php
|
||||
$database->query('UPDATE table SET', [
|
||||
'date' => $database->literal('NOW()'),
|
||||
]);
|
||||
// UPDATE table SET `date` = NOW()
|
||||
```
|
||||
|
||||
Or an expression in which you can use `?` or modifiers:
|
||||
|
||||
```php
|
||||
$database->query('UPDATE `table` SET', [
|
||||
'title' => $database::expression('SHA1(?)', 'secret'),
|
||||
]);
|
||||
// UPDATE `table` SET `title` = SHA1('secret')
|
||||
```
|
||||
|
||||
When updating, modifiers can be placed directly in the keys:
|
||||
|
||||
```php
|
||||
$database->query('UPDATE table SET', [
|
||||
'date%SQL' => 'NOW()', // %SQL means SQL ;)
|
||||
]);
|
||||
// UPDATE table SET `date` = NOW()
|
||||
```
|
||||
|
||||
In conditions (ie, for `%and` and `%or` modifiers), it is not necessary to specify the keys:
|
||||
|
||||
```php
|
||||
$result = $database->query('SELECT * FROM `table` WHERE %and', [
|
||||
'number > 10',
|
||||
'number < 100',
|
||||
]);
|
||||
// SELECT * FROM `table` WHERE (number > 10) AND (number < 100)
|
||||
```
|
||||
|
||||
Modifiers or wildcards can also be used in expressions:
|
||||
|
||||
```php
|
||||
$result = $database->query('SELECT * FROM `table` WHERE %and', [
|
||||
['number > ?', 10], // or $database::expression('number > ?', 10)
|
||||
['number < ?', 100],
|
||||
['%or', [
|
||||
'left' => 1,
|
||||
'top' => 2,
|
||||
]],
|
||||
]);
|
||||
// SELECT * FROM `table` WHERE (number > 10) AND (number < 100) AND (`left` = 1 OR `top` = 2)
|
||||
```
|
||||
|
||||
The `%ex` modifier inserts all items of the array into SQL:
|
||||
|
||||
```php
|
||||
$result = $database->query('SELECT * FROM `table` WHERE %ex', [
|
||||
$database::expression('left = ?', 1),
|
||||
'AND',
|
||||
'top IS NULL',
|
||||
]);
|
||||
// SELECT * FROM `table` WHERE left = 1 AND top IS NULL
|
||||
```
|
||||
|
||||
|
||||
### Conditions in the SQL
|
||||
|
||||
Conditional SQL commands are controlled by three modifiers `%if`, `%else`, and `%end`. The `%if` must be at the end of the string representing SQL and is followed by the variable:
|
||||
|
||||
```php
|
||||
$user = ???
|
||||
|
||||
$result = $database->query('
|
||||
SELECT *
|
||||
FROM table
|
||||
%if', isset($user), 'WHERE user=%s', $user, '%end
|
||||
ORDER BY name
|
||||
');
|
||||
```
|
||||
|
||||
The condition can be supplemented by the section `%else`:
|
||||
|
||||
```php
|
||||
$result = $database->query('
|
||||
SELECT *
|
||||
FROM %if', $cond, 'one_table %else second_table
|
||||
');
|
||||
```
|
||||
|
||||
Conditions can nest together.
|
||||
|
||||
|
||||
### Identifiers and strings in SQL
|
||||
|
||||
SQL itself goes through processing to meet the conventions of the database. The identifiers (names of tables and columns) can be entered into square brackets or backticks, strings are quoted with single or double quotation marks, but the server always sends what the database asks for. Example:
|
||||
|
||||
```php
|
||||
$database->query("UPDATE `table` SET [status]='I''m fine'");
|
||||
// MySQL: UPDATE `table` SET `status`='I\'m fine'
|
||||
// ODBC: UPDATE [table] SET [status]='I''m fine'
|
||||
```
|
||||
|
||||
The quotation marks are duplicated inside the string in SQL.
|
||||
|
||||
|
||||
|
||||
### Result as associative array
|
||||
|
||||
Example: returns results as an associative field, where the key will be the value of the `id` field:
|
||||
|
||||
```php
|
||||
$assoc = $result->fetchAssoc('id');
|
||||
```
|
||||
|
||||
The greatest power of `fetchAssoc()` is reflected in a SQL query joining several tables with different types of joins. The database will make a flat table, fetchAssoc returns the shape.
|
||||
|
||||
Example: Let's take a customer and order table (N:M binding) and query:
|
||||
|
||||
```php
|
||||
$result = $database->query('
|
||||
SELECT customer_id, customers.name, order_id, orders.number, ...
|
||||
FROM customers
|
||||
INNER JOIN orders USING (customer_id)
|
||||
WHERE ...
|
||||
');
|
||||
```
|
||||
|
||||
And we'd like to get a nested associative array by Customer ID and then Order ID:
|
||||
|
||||
```php
|
||||
$all = $result->fetchAssoc('customer_id|order_id');
|
||||
|
||||
// we will iterate like this:
|
||||
foreach ($all as $customerId => $orders) {
|
||||
foreach ($orders as $orderId => $order) {
|
||||
...
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
An associative descriptor has a similar syntax as when you type the array by assigning it to PHP. Thus `'customer_id|order_id'` represents the assignment series `$all[$customerId][$orderId] = $row;` sequentially for all rows.
|
||||
|
||||
Sometimes it would be useful to associate by the customer's name instead of his ID:
|
||||
|
||||
```php
|
||||
$all = $result->fetchAssoc('name|order_id');
|
||||
|
||||
// the elements then proceeds like this:
|
||||
$order = $all['Arnold Rimmer'][$orderId];
|
||||
```
|
||||
|
||||
But what if there are more customers with the same name? The table should be in the form of:
|
||||
|
||||
```php
|
||||
$row = $all['Arnold Rimmer'][0][$orderId];
|
||||
$row = $all['Arnold Rimmer'][1][$orderId];
|
||||
...
|
||||
```
|
||||
|
||||
So we can distinguish between multiple possible Rimmers using an array. The associative descriptor has a format similar to the assignment, with the sequence array representing `[]`:
|
||||
|
||||
```php
|
||||
$all = $result->fetchAssoc('name[]order_id');
|
||||
|
||||
// we get all the Arnolds in the results
|
||||
foreach ($all['Arnold Rimmer'] as $arnoldOrders) {
|
||||
foreach ($arnoldOrders as $orderId => $order) {
|
||||
...
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Returning to the example with the `customer_id|order_id` descriptor, we will try to list the orders of each customer:
|
||||
|
||||
```php
|
||||
$all = $result->fetchAssoc('customer_id|order_id');
|
||||
|
||||
foreach ($all as $customerId => $orders) {
|
||||
echo "Customer $customerId":
|
||||
|
||||
foreach ($orders as $orderId => $order) {
|
||||
echo "ID number: $order->number";
|
||||
// customer name is in $order->name
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
It would be a nice to echo customer name too. But we would have to look for it in the `$orders` array. So let's adjust the results to such a shape:
|
||||
|
||||
```php
|
||||
$all[$customerId]->name = 'John Doe';
|
||||
$all[$customerId]->order_id[$orderId] = $row;
|
||||
$all[$customerId]->order_id[$orderId2] = $row2;
|
||||
```
|
||||
|
||||
So, between `$clientId` and `$orderId`, we will also insert an intermediate item. This time not the numbered indexes as we used to distinguish between individual Rimmers, but a database row. The solution is very similar, just remember that the row symbolizes the arrow:
|
||||
|
||||
```php
|
||||
$all = $result->fetchAssoc('customer_id->order_id');
|
||||
|
||||
foreach ($all as $customerId => $row) {
|
||||
echo "Customer $row->name":
|
||||
|
||||
foreach ($row->order_id as $orderId => $order) {
|
||||
echo "ID number: $order->number";
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
### Prefixes & substitutions
|
||||
|
||||
Table and column names can contain variable parts. You will first define:
|
||||
|
||||
```php
|
||||
// create new substitution :blog: ==> wp_
|
||||
$database->substitute('blog', 'wp_');
|
||||
```
|
||||
|
||||
and then use it in SQL. Note that in SQL they are quoted by the colon:
|
||||
|
||||
```php
|
||||
$database->query("UPDATE [:blog:items] SET [text]='Hello World'");
|
||||
// UPDATE `wp_items` SET `text`='Hello World'
|
||||
```
|
||||
|
||||
|
||||
### Field data types
|
||||
|
||||
Dibi automatically detects the types of query columns and converts fields them to native PHP types. We can also specify the type manually. You can find the possible types in the `Dibi\Type` class.
|
||||
|
||||
```php
|
||||
$result->setType('id', Dibi\Type::INTEGER); // id will be integer
|
||||
$row = $result->fetch();
|
||||
|
||||
is_int($row->id) // true
|
||||
```
|
||||
|
||||
|
||||
### Logger
|
||||
|
||||
Dibi has a built-in logger that lets you track all SQL statements executed and measure the length of their duration. Activating the logger:
|
||||
|
||||
```php
|
||||
$database->connect([
|
||||
'driver' => 'sqlite',
|
||||
'database' => 'sample.sdb',
|
||||
'profiler' => [
|
||||
'file' => 'file.log',
|
||||
],
|
||||
]);
|
||||
```
|
||||
|
||||
A more versatile profiler is a Tracy panel that is activated when connected to Nette.
|
||||
|
||||
|
||||
|
||||
### Connect to [Nette](https://nette.org)
|
||||
|
||||
In the configuration file, we will register the DI extensions and add the `dibi` section to create the required objects and also the database panel in the [Tracy](https://tracy.nette.org) debugger bar.
|
||||
|
||||
```neon
|
||||
extensions:
|
||||
dibi: Dibi\Bridges\Nette\DibiExtension22
|
||||
|
||||
dibi:
|
||||
host: localhost
|
||||
username: root
|
||||
password: ***
|
||||
database: foo
|
||||
lazy: true
|
||||
```
|
||||
|
||||
Then the object of connection can be [obtained as a service from the container DI](https://doc.nette.org/di-usage), eg:
|
||||
|
||||
```php
|
||||
class Model
|
||||
{
|
||||
private $database;
|
||||
|
||||
public function __construct(Dibi\Connection $database)
|
||||
{
|
||||
$this->database = $database;
|
||||
}
|
||||
}
|
||||
```
|
@@ -1,82 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Dibi\Bridges\Nette;
|
||||
|
||||
use Dibi;
|
||||
use Nette;
|
||||
use Tracy;
|
||||
|
||||
|
||||
/**
|
||||
* Dibi extension for Nette Framework 2.2. Creates 'connection' & 'panel' services.
|
||||
*/
|
||||
class DibiExtension22 extends Nette\DI\CompilerExtension
|
||||
{
|
||||
/** @var bool|null */
|
||||
private $debugMode;
|
||||
|
||||
/** @var bool|null */
|
||||
private $cliMode;
|
||||
|
||||
|
||||
public function __construct(?bool $debugMode = null, ?bool $cliMode = null)
|
||||
{
|
||||
$this->debugMode = $debugMode;
|
||||
$this->cliMode = $cliMode;
|
||||
}
|
||||
|
||||
|
||||
public function loadConfiguration()
|
||||
{
|
||||
$container = $this->getContainerBuilder();
|
||||
$config = $this->getConfig();
|
||||
|
||||
if ($this->debugMode === null) {
|
||||
$this->debugMode = $container->parameters['debugMode'];
|
||||
}
|
||||
|
||||
if ($this->cliMode === null) {
|
||||
$this->cliMode = $container->parameters['consoleMode'];
|
||||
}
|
||||
|
||||
$useProfiler = $config['profiler'] ?? (class_exists(Tracy\Debugger::class) && $this->debugMode && !$this->cliMode);
|
||||
|
||||
unset($config['profiler']);
|
||||
|
||||
if (isset($config['flags'])) {
|
||||
$flags = 0;
|
||||
foreach ((array) $config['flags'] as $flag) {
|
||||
$flags |= constant($flag);
|
||||
}
|
||||
|
||||
$config['flags'] = $flags;
|
||||
}
|
||||
|
||||
$connection = $container->addDefinition($this->prefix('connection'))
|
||||
->setFactory(Dibi\Connection::class, [$config])
|
||||
->setAutowired($config['autowired'] ?? true);
|
||||
|
||||
if (class_exists(Tracy\Debugger::class)) {
|
||||
$connection->addSetup(
|
||||
[new Nette\DI\Statement('Tracy\Debugger::getBlueScreen'), 'addPanel'],
|
||||
[[Dibi\Bridges\Tracy\Panel::class, 'renderException']]
|
||||
);
|
||||
}
|
||||
|
||||
if ($useProfiler) {
|
||||
$panel = $container->addDefinition($this->prefix('panel'))
|
||||
->setFactory(Dibi\Bridges\Tracy\Panel::class, [
|
||||
$config['explain'] ?? true,
|
||||
isset($config['filter']) && $config['filter'] === false ? Dibi\Event::ALL : Dibi\Event::QUERY,
|
||||
]);
|
||||
$connection->addSetup([$panel, 'register'], [$connection]);
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,12 +0,0 @@
|
||||
# This will create service named 'dibi.connection'.
|
||||
# Requires Nette Framework 2.2 or later
|
||||
|
||||
extensions:
|
||||
dibi: Dibi\Bridges\Nette\DibiExtension22
|
||||
|
||||
dibi:
|
||||
host: localhost
|
||||
username: root
|
||||
password: ***
|
||||
database: foo
|
||||
lazy: true
|
@@ -1,181 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Dibi\Bridges\Tracy;
|
||||
|
||||
use Dibi;
|
||||
use Dibi\Event;
|
||||
use Dibi\Helpers;
|
||||
use Tracy;
|
||||
|
||||
|
||||
/**
|
||||
* Dibi panel for Tracy.
|
||||
*/
|
||||
class Panel implements Tracy\IBarPanel
|
||||
{
|
||||
use Dibi\Strict;
|
||||
|
||||
/** @var int maximum SQL length */
|
||||
public static $maxLength = 1000;
|
||||
|
||||
/** @var bool|string explain queries? */
|
||||
public $explain;
|
||||
|
||||
/** @var int */
|
||||
public $filter;
|
||||
|
||||
/** @var array */
|
||||
private $events = [];
|
||||
|
||||
|
||||
public function __construct($explain = true, ?int $filter = null)
|
||||
{
|
||||
$this->filter = $filter ?: Event::QUERY;
|
||||
$this->explain = $explain;
|
||||
}
|
||||
|
||||
|
||||
public function register(Dibi\Connection $connection): void
|
||||
{
|
||||
Tracy\Debugger::getBar()->addPanel($this);
|
||||
Tracy\Debugger::getBlueScreen()->addPanel([self::class, 'renderException']);
|
||||
$connection->onEvent[] = [$this, 'logEvent'];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* After event notification.
|
||||
*/
|
||||
public function logEvent(Event $event): void
|
||||
{
|
||||
if (($event->type & $this->filter) === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->events[] = $event;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns blue-screen custom tab.
|
||||
*/
|
||||
public static function renderException(?\Throwable $e): ?array
|
||||
{
|
||||
if ($e instanceof Dibi\Exception && $e->getSql()) {
|
||||
return [
|
||||
'tab' => 'SQL',
|
||||
'panel' => Helpers::dump($e->getSql(), true),
|
||||
];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns HTML code for custom tab. (Tracy\IBarPanel)
|
||||
*/
|
||||
public function getTab(): string
|
||||
{
|
||||
$totalTime = 0;
|
||||
$count = count($this->events);
|
||||
foreach ($this->events as $event) {
|
||||
$totalTime += $event->time;
|
||||
}
|
||||
|
||||
return '<span title="dibi"><svg viewBox="0 0 2048 2048" style="vertical-align: bottom; width:1.23em; height:1.55em"><path fill="' . ($count ? '#b079d6' : '#aaa') . '" d="M1024 896q237 0 443-43t325-127v170q0 69-103 128t-280 93.5-385 34.5-385-34.5-280-93.5-103-128v-170q119 84 325 127t443 43zm0 768q237 0 443-43t325-127v170q0 69-103 128t-280 93.5-385 34.5-385-34.5-280-93.5-103-128v-170q119 84 325 127t443 43zm0-384q237 0 443-43t325-127v170q0 69-103 128t-280 93.5-385 34.5-385-34.5-280-93.5-103-128v-170q119 84 325 127t443 43zm0-1152q208 0 385 34.5t280 93.5 103 128v128q0 69-103 128t-280 93.5-385 34.5-385-34.5-280-93.5-103-128v-128q0-69 103-128t280-93.5 385-34.5z"/></svg><span class="tracy-label">'
|
||||
. $count . "\u{a0}queries"
|
||||
. ($totalTime ? ' / ' . number_format($totalTime * 1000, 1, '.', "\u{202f}") . "\u{202f}ms" : '')
|
||||
. '</span></span>';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns HTML code for custom panel. (Tracy\IBarPanel)
|
||||
*/
|
||||
public function getPanel(): ?string
|
||||
{
|
||||
if (!$this->events) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$totalTime = $s = null;
|
||||
|
||||
$singleConnection = reset($this->events)->connection;
|
||||
foreach ($this->events as $event) {
|
||||
if ($event->connection !== $singleConnection) {
|
||||
$singleConnection = null;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($this->events as $event) {
|
||||
$totalTime += $event->time;
|
||||
$connection = $event->connection;
|
||||
$explain = null; // EXPLAIN is called here to work SELECT FOUND_ROWS()
|
||||
if ($this->explain && $event->type === Event::SELECT) {
|
||||
$backup = [$connection->onEvent, \dibi::$numOfQueries, \dibi::$totalTime];
|
||||
$connection->onEvent = null;
|
||||
$cmd = is_string($this->explain)
|
||||
? $this->explain
|
||||
: ($connection->getConfig('driver') === 'oracle' ? 'EXPLAIN PLAN FOR' : 'EXPLAIN');
|
||||
try {
|
||||
$explain = @Helpers::dump($connection->nativeQuery("$cmd $event->sql"), true);
|
||||
} catch (Dibi\Exception $e) {
|
||||
}
|
||||
|
||||
[$connection->onEvent, \dibi::$numOfQueries, \dibi::$totalTime] = $backup;
|
||||
}
|
||||
|
||||
$s .= '<tr><td data-order="' . $event->time . '">' . number_format($event->time * 1000, 3, '.', "\u{202f}");
|
||||
if ($explain) {
|
||||
static $counter;
|
||||
$counter++;
|
||||
$s .= "<br /><a href='#tracy-debug-DibiProfiler-row-$counter' class='tracy-toggle tracy-collapsed' rel='#tracy-debug-DibiProfiler-row-$counter'>explain</a>";
|
||||
}
|
||||
|
||||
$s .= '</td><td class="tracy-DibiProfiler-sql">' . Helpers::dump(strlen($event->sql) > self::$maxLength ? substr($event->sql, 0, self::$maxLength) . '...' : $event->sql, true);
|
||||
if ($explain) {
|
||||
$s .= "<div id='tracy-debug-DibiProfiler-row-$counter' class='tracy-collapsed'>{$explain}</div>";
|
||||
}
|
||||
|
||||
if ($event->source) {
|
||||
$s .= Tracy\Helpers::editorLink($event->source[0], $event->source[1]); //->class('tracy-DibiProfiler-source');
|
||||
}
|
||||
|
||||
$s .= "</td><td>{$event->count}</td>";
|
||||
if (!$singleConnection) {
|
||||
$s .= '<td>' . htmlspecialchars($this->getConnectionName($connection)) . '</td></tr>';
|
||||
}
|
||||
}
|
||||
|
||||
return '<style> #tracy-debug td.tracy-DibiProfiler-sql { background: white !important }
|
||||
#tracy-debug .tracy-DibiProfiler-source { color: #999 !important }
|
||||
#tracy-debug tracy-DibiProfiler tr table { margin: 8px 0; max-height: 150px; overflow:auto } </style>
|
||||
<h1>Queries:\u{a0}' . count($this->events)
|
||||
. ($totalTime === null ? '' : ", time:\u{a0}" . number_format($totalTime * 1000, 1, '.', "\u{202f}") . "\u{202f}ms") . ', '
|
||||
. htmlspecialchars($this->getConnectionName($singleConnection)) . '</h1>
|
||||
<div class="tracy-inner tracy-DibiProfiler">
|
||||
<table class="tracy-sortable">
|
||||
<tr><th>Time ms</th><th>SQL Statement</th><th>Rows</th>' . (!$singleConnection ? '<th>Connection</th>' : '') . '</tr>
|
||||
' . $s . '
|
||||
</table>
|
||||
</div>';
|
||||
}
|
||||
|
||||
|
||||
private function getConnectionName(Dibi\Connection $connection): string
|
||||
{
|
||||
$driver = $connection->getConfig('driver');
|
||||
return (is_object($driver) ? get_class($driver) : $driver)
|
||||
. ($connection->getConfig('name') ? '/' . $connection->getConfig('name') : '')
|
||||
. ($connection->getConfig('host') ? "\u{202f}@\u{202f}" . $connection->getConfig('host') : '');
|
||||
}
|
||||
}
|
@@ -1,648 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Dibi;
|
||||
|
||||
use Traversable;
|
||||
|
||||
|
||||
/**
|
||||
* Dibi connection.
|
||||
*
|
||||
* @property-read int $affectedRows
|
||||
* @property-read int $insertId
|
||||
*/
|
||||
class Connection implements IConnection
|
||||
{
|
||||
use Strict;
|
||||
|
||||
/** @var array of function (Event $event); Occurs after query is executed */
|
||||
public $onEvent = [];
|
||||
|
||||
/** @var array Current connection configuration */
|
||||
private $config;
|
||||
|
||||
/** @var string[] resultset formats */
|
||||
private $formats;
|
||||
|
||||
/** @var Driver|null */
|
||||
private $driver;
|
||||
|
||||
/** @var Translator|null */
|
||||
private $translator;
|
||||
|
||||
/** @var HashMap Substitutes for identifiers */
|
||||
private $substitutes;
|
||||
|
||||
private $transactionDepth = 0;
|
||||
|
||||
|
||||
/**
|
||||
* Connection options: (see driver-specific options too)
|
||||
* - lazy (bool) => if true, connection will be established only when required
|
||||
* - result (array) => result set options
|
||||
* - normalize => normalizes result fields (default: true)
|
||||
* - formatDateTime => date-time format
|
||||
* empty for decoding as Dibi\DateTime (default)
|
||||
* "..." formatted according to given format, see https://www.php.net/manual/en/datetime.format.php
|
||||
* "native" for leaving value as is
|
||||
* - formatTimeInterval => time-interval format
|
||||
* empty for decoding as DateInterval (default)
|
||||
* "..." formatted according to given format, see https://www.php.net/manual/en/dateinterval.format.php
|
||||
* "native" for leaving value as is
|
||||
* - formatJson => json format
|
||||
* "array" for decoding json as an array (default)
|
||||
* "object" for decoding json as \stdClass
|
||||
* "native" for leaving value as is
|
||||
* - profiler (array)
|
||||
* - run (bool) => enable profiler?
|
||||
* - file => file to log
|
||||
* - errorsOnly (bool) => log only errors
|
||||
* - substitutes (array) => map of driver specific substitutes (under development)
|
||||
* - onConnect (array) => list of SQL queries to execute (by Connection::query()) after connection is established
|
||||
* @throws Exception
|
||||
*/
|
||||
public function __construct(array $config, ?string $name = null)
|
||||
{
|
||||
Helpers::alias($config, 'username', 'user');
|
||||
Helpers::alias($config, 'password', 'pass');
|
||||
Helpers::alias($config, 'host', 'hostname');
|
||||
Helpers::alias($config, 'result|formatDate', 'resultDate');
|
||||
Helpers::alias($config, 'result|formatDateTime', 'resultDateTime');
|
||||
$config['driver'] = $config['driver'] ?? 'mysqli';
|
||||
$config['name'] = $name;
|
||||
$this->config = $config;
|
||||
|
||||
$this->formats = [
|
||||
Type::DATE => $this->config['result']['formatDate'],
|
||||
Type::DATETIME => $this->config['result']['formatDateTime'],
|
||||
Type::JSON => $this->config['result']['formatJson'] ?? 'array',
|
||||
Type::TIME_INTERVAL => $this->config['result']['formatTimeInterval'] ?? null,
|
||||
];
|
||||
|
||||
// profiler
|
||||
if (isset($config['profiler']['file']) && (!isset($config['profiler']['run']) || $config['profiler']['run'])) {
|
||||
$filter = $config['profiler']['filter'] ?? Event::QUERY;
|
||||
$errorsOnly = $config['profiler']['errorsOnly'] ?? false;
|
||||
$this->onEvent[] = [new Loggers\FileLogger($config['profiler']['file'], $filter, $errorsOnly), 'logEvent'];
|
||||
}
|
||||
|
||||
$this->substitutes = new HashMap(function (string $expr) { return ":$expr:"; });
|
||||
if (!empty($config['substitutes'])) {
|
||||
foreach ($config['substitutes'] as $key => $value) {
|
||||
$this->substitutes->$key = $value;
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($config['onConnect']) && !is_array($config['onConnect'])) {
|
||||
throw new \InvalidArgumentException("Configuration option 'onConnect' must be array.");
|
||||
}
|
||||
|
||||
if (empty($config['lazy'])) {
|
||||
$this->connect();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Automatically frees the resources allocated for this result set.
|
||||
*/
|
||||
public function __destruct()
|
||||
{
|
||||
if ($this->driver && $this->driver->getResource()) {
|
||||
$this->disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Connects to a database.
|
||||
*/
|
||||
final public function connect(): void
|
||||
{
|
||||
if ($this->config['driver'] instanceof Driver) {
|
||||
$this->driver = $this->config['driver'];
|
||||
$this->translator = new Translator($this);
|
||||
return;
|
||||
|
||||
} elseif (is_subclass_of($this->config['driver'], Driver::class)) {
|
||||
$class = $this->config['driver'];
|
||||
|
||||
} else {
|
||||
$class = preg_replace(['#\W#', '#sql#'], ['_', 'Sql'], ucfirst(strtolower($this->config['driver'])));
|
||||
$class = "Dibi\\Drivers\\{$class}Driver";
|
||||
if (!class_exists($class)) {
|
||||
throw new Exception("Unable to create instance of Dibi driver '$class'.");
|
||||
}
|
||||
}
|
||||
|
||||
$event = $this->onEvent ? new Event($this, Event::CONNECT) : null;
|
||||
try {
|
||||
$this->driver = new $class($this->config);
|
||||
$this->translator = new Translator($this);
|
||||
|
||||
if ($event) {
|
||||
$this->onEvent($event->done());
|
||||
}
|
||||
|
||||
if (isset($this->config['onConnect'])) {
|
||||
foreach ($this->config['onConnect'] as $sql) {
|
||||
$this->query($sql);
|
||||
}
|
||||
}
|
||||
} catch (DriverException $e) {
|
||||
if ($event) {
|
||||
$this->onEvent($event->done($e));
|
||||
}
|
||||
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Disconnects from a database.
|
||||
*/
|
||||
final public function disconnect(): void
|
||||
{
|
||||
if ($this->driver) {
|
||||
$this->driver->disconnect();
|
||||
$this->driver = $this->translator = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns true when connection was established.
|
||||
*/
|
||||
final public function isConnected(): bool
|
||||
{
|
||||
return (bool) $this->driver;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns configuration variable. If no $key is passed, returns the entire array.
|
||||
* @see self::__construct
|
||||
* @return mixed
|
||||
*/
|
||||
final public function getConfig(?string $key = null, $default = null)
|
||||
{
|
||||
return $key === null
|
||||
? $this->config
|
||||
: ($this->config[$key] ?? $default);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the driver and connects to a database in lazy mode.
|
||||
*/
|
||||
final public function getDriver(): Driver
|
||||
{
|
||||
if (!$this->driver) {
|
||||
$this->connect();
|
||||
}
|
||||
|
||||
return $this->driver;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Generates (translates) and executes SQL query.
|
||||
* @param mixed ...$args
|
||||
* @throws Exception
|
||||
*/
|
||||
final public function query(...$args): Result
|
||||
{
|
||||
return $this->nativeQuery($this->translate(...$args));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Generates SQL query.
|
||||
* @param mixed ...$args
|
||||
* @throws Exception
|
||||
*/
|
||||
final public function translate(...$args): string
|
||||
{
|
||||
if (!$this->driver) {
|
||||
$this->connect();
|
||||
}
|
||||
|
||||
return (clone $this->translator)->translate($args);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Generates and prints SQL query.
|
||||
* @param mixed ...$args
|
||||
*/
|
||||
final public function test(...$args): bool
|
||||
{
|
||||
try {
|
||||
Helpers::dump($this->translate(...$args));
|
||||
return true;
|
||||
|
||||
} catch (Exception $e) {
|
||||
if ($e->getSql()) {
|
||||
Helpers::dump($e->getSql());
|
||||
} else {
|
||||
echo get_class($e) . ': ' . $e->getMessage() . (PHP_SAPI === 'cli' ? "\n" : '<br>');
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Generates (translates) and returns SQL query as DataSource.
|
||||
* @param mixed ...$args
|
||||
* @throws Exception
|
||||
*/
|
||||
final public function dataSource(...$args): DataSource
|
||||
{
|
||||
return new DataSource($this->translate(...$args), $this);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Executes the SQL query.
|
||||
* @throws Exception
|
||||
*/
|
||||
final public function nativeQuery(string $sql): Result
|
||||
{
|
||||
if (!$this->driver) {
|
||||
$this->connect();
|
||||
}
|
||||
|
||||
\dibi::$sql = $sql;
|
||||
$event = $this->onEvent ? new Event($this, Event::QUERY, $sql) : null;
|
||||
try {
|
||||
$res = $this->driver->query($sql);
|
||||
|
||||
} catch (DriverException $e) {
|
||||
if ($event) {
|
||||
$this->onEvent($event->done($e));
|
||||
}
|
||||
|
||||
throw $e;
|
||||
}
|
||||
|
||||
$res = $this->createResultSet($res ?: new Drivers\NoDataResult(max(0, $this->driver->getAffectedRows())));
|
||||
if ($event) {
|
||||
$this->onEvent($event->done($res));
|
||||
}
|
||||
|
||||
return $res;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets the number of affected rows by the last INSERT, UPDATE or DELETE query.
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getAffectedRows(): int
|
||||
{
|
||||
if (!$this->driver) {
|
||||
$this->connect();
|
||||
}
|
||||
|
||||
$rows = $this->driver->getAffectedRows();
|
||||
if ($rows === null || $rows < 0) {
|
||||
throw new Exception('Cannot retrieve number of affected rows.');
|
||||
}
|
||||
|
||||
return $rows;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query.
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getInsertId(?string $sequence = null): int
|
||||
{
|
||||
if (!$this->driver) {
|
||||
$this->connect();
|
||||
}
|
||||
|
||||
$id = $this->driver->getInsertId($sequence);
|
||||
if ($id === null) {
|
||||
throw new Exception('Cannot retrieve last generated ID.');
|
||||
}
|
||||
|
||||
return $id;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Begins a transaction (if supported).
|
||||
*/
|
||||
public function begin(?string $savepoint = null): void
|
||||
{
|
||||
if ($this->transactionDepth !== 0) {
|
||||
throw new \LogicException(__METHOD__ . '() call is forbidden inside a transaction() callback');
|
||||
}
|
||||
|
||||
if (!$this->driver) {
|
||||
$this->connect();
|
||||
}
|
||||
|
||||
$event = $this->onEvent ? new Event($this, Event::BEGIN, $savepoint) : null;
|
||||
try {
|
||||
$this->driver->begin($savepoint);
|
||||
if ($event) {
|
||||
$this->onEvent($event->done());
|
||||
}
|
||||
} catch (DriverException $e) {
|
||||
if ($event) {
|
||||
$this->onEvent($event->done($e));
|
||||
}
|
||||
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Commits statements in a transaction.
|
||||
*/
|
||||
public function commit(?string $savepoint = null): void
|
||||
{
|
||||
if ($this->transactionDepth !== 0) {
|
||||
throw new \LogicException(__METHOD__ . '() call is forbidden inside a transaction() callback');
|
||||
}
|
||||
|
||||
if (!$this->driver) {
|
||||
$this->connect();
|
||||
}
|
||||
|
||||
$event = $this->onEvent ? new Event($this, Event::COMMIT, $savepoint) : null;
|
||||
try {
|
||||
$this->driver->commit($savepoint);
|
||||
if ($event) {
|
||||
$this->onEvent($event->done());
|
||||
}
|
||||
} catch (DriverException $e) {
|
||||
if ($event) {
|
||||
$this->onEvent($event->done($e));
|
||||
}
|
||||
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Rollback changes in a transaction.
|
||||
*/
|
||||
public function rollback(?string $savepoint = null): void
|
||||
{
|
||||
if ($this->transactionDepth !== 0) {
|
||||
throw new \LogicException(__METHOD__ . '() call is forbidden inside a transaction() callback');
|
||||
}
|
||||
|
||||
if (!$this->driver) {
|
||||
$this->connect();
|
||||
}
|
||||
|
||||
$event = $this->onEvent ? new Event($this, Event::ROLLBACK, $savepoint) : null;
|
||||
try {
|
||||
$this->driver->rollback($savepoint);
|
||||
if ($event) {
|
||||
$this->onEvent($event->done());
|
||||
}
|
||||
} catch (DriverException $e) {
|
||||
if ($event) {
|
||||
$this->onEvent($event->done($e));
|
||||
}
|
||||
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function transaction(callable $callback)
|
||||
{
|
||||
if ($this->transactionDepth === 0) {
|
||||
$this->begin();
|
||||
}
|
||||
|
||||
$this->transactionDepth++;
|
||||
try {
|
||||
$res = $callback($this);
|
||||
} catch (\Throwable $e) {
|
||||
$this->transactionDepth--;
|
||||
if ($this->transactionDepth === 0) {
|
||||
$this->rollback();
|
||||
}
|
||||
|
||||
throw $e;
|
||||
}
|
||||
|
||||
$this->transactionDepth--;
|
||||
if ($this->transactionDepth === 0) {
|
||||
$this->commit();
|
||||
}
|
||||
|
||||
return $res;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Result set factory.
|
||||
*/
|
||||
public function createResultSet(ResultDriver $resultDriver): Result
|
||||
{
|
||||
return (new Result($resultDriver, $this->config['result']['normalize'] ?? true))
|
||||
->setFormats($this->formats);
|
||||
}
|
||||
|
||||
|
||||
/********************* fluent SQL builders ****************d*g**/
|
||||
|
||||
|
||||
public function command(): Fluent
|
||||
{
|
||||
return new Fluent($this);
|
||||
}
|
||||
|
||||
|
||||
public function select(...$args): Fluent
|
||||
{
|
||||
return $this->command()->select(...$args);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string|string[] $table
|
||||
*/
|
||||
public function update($table, iterable $args): Fluent
|
||||
{
|
||||
return $this->command()->update('%n', $table)->set($args);
|
||||
}
|
||||
|
||||
|
||||
public function insert(string $table, iterable $args): Fluent
|
||||
{
|
||||
if ($args instanceof Traversable) {
|
||||
$args = iterator_to_array($args);
|
||||
}
|
||||
|
||||
return $this->command()->insert()
|
||||
->into('%n', $table, '(%n)', array_keys($args))->values('%l', $args);
|
||||
}
|
||||
|
||||
|
||||
public function delete(string $table): Fluent
|
||||
{
|
||||
return $this->command()->delete()->from('%n', $table);
|
||||
}
|
||||
|
||||
|
||||
/********************* substitutions ****************d*g**/
|
||||
|
||||
|
||||
/**
|
||||
* Returns substitution hashmap.
|
||||
*/
|
||||
public function getSubstitutes(): HashMap
|
||||
{
|
||||
return $this->substitutes;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Provides substitution.
|
||||
*/
|
||||
public function substitute(string $value): string
|
||||
{
|
||||
return strpos($value, ':') === false
|
||||
? $value
|
||||
: preg_replace_callback('#:([^:\s]*):#', function (array $m) { return $this->substitutes->{$m[1]}; }, $value);
|
||||
}
|
||||
|
||||
|
||||
/********************* shortcuts ****************d*g**/
|
||||
|
||||
|
||||
/**
|
||||
* Executes SQL query and fetch result - shortcut for query() & fetch().
|
||||
* @param mixed ...$args
|
||||
* @throws Exception
|
||||
*/
|
||||
public function fetch(...$args): ?Row
|
||||
{
|
||||
return $this->query($args)->fetch();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Executes SQL query and fetch results - shortcut for query() & fetchAll().
|
||||
* @param mixed ...$args
|
||||
* @return Row[]|array[]
|
||||
* @throws Exception
|
||||
*/
|
||||
public function fetchAll(...$args): array
|
||||
{
|
||||
return $this->query($args)->fetchAll();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Executes SQL query and fetch first column - shortcut for query() & fetchSingle().
|
||||
* @param mixed ...$args
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function fetchSingle(...$args)
|
||||
{
|
||||
return $this->query($args)->fetchSingle();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Executes SQL query and fetch pairs - shortcut for query() & fetchPairs().
|
||||
* @param mixed ...$args
|
||||
* @throws Exception
|
||||
*/
|
||||
public function fetchPairs(...$args): array
|
||||
{
|
||||
return $this->query($args)->fetchPairs();
|
||||
}
|
||||
|
||||
|
||||
public static function literal(string $value): Literal
|
||||
{
|
||||
return new Literal($value);
|
||||
}
|
||||
|
||||
|
||||
public static function expression(...$args): Expression
|
||||
{
|
||||
return new Expression(...$args);
|
||||
}
|
||||
|
||||
|
||||
/********************* misc ****************d*g**/
|
||||
|
||||
|
||||
/**
|
||||
* Import SQL dump from file.
|
||||
* @param callable $onProgress function (int $count, ?float $percent): void
|
||||
* @return int count of sql commands
|
||||
*/
|
||||
public function loadFile(string $file, ?callable $onProgress = null): int
|
||||
{
|
||||
return Helpers::loadFromFile($this, $file, $onProgress);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets a information about the current database.
|
||||
*/
|
||||
public function getDatabaseInfo(): Reflection\Database
|
||||
{
|
||||
if (!$this->driver) {
|
||||
$this->connect();
|
||||
}
|
||||
|
||||
return new Reflection\Database($this->driver->getReflector(), $this->config['database'] ?? null);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Prevents unserialization.
|
||||
*/
|
||||
public function __wakeup()
|
||||
{
|
||||
throw new NotSupportedException('You cannot serialize or unserialize ' . static::class . ' instances.');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Prevents serialization.
|
||||
*/
|
||||
public function __sleep()
|
||||
{
|
||||
throw new NotSupportedException('You cannot serialize or unserialize ' . static::class . ' instances.');
|
||||
}
|
||||
|
||||
|
||||
protected function onEvent($arg): void
|
||||
{
|
||||
foreach ($this->onEvent as $handler) {
|
||||
$handler($arg);
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,286 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Dibi;
|
||||
|
||||
|
||||
/**
|
||||
* Default implementation of IDataSource.
|
||||
*/
|
||||
class DataSource implements IDataSource
|
||||
{
|
||||
use Strict;
|
||||
|
||||
/** @var Connection */
|
||||
private $connection;
|
||||
|
||||
/** @var string */
|
||||
private $sql;
|
||||
|
||||
/** @var Result|null */
|
||||
private $result;
|
||||
|
||||
/** @var int|null */
|
||||
private $count;
|
||||
|
||||
/** @var int|null */
|
||||
private $totalCount;
|
||||
|
||||
/** @var array */
|
||||
private $cols = [];
|
||||
|
||||
/** @var array */
|
||||
private $sorting = [];
|
||||
|
||||
/** @var array */
|
||||
private $conds = [];
|
||||
|
||||
/** @var int|null */
|
||||
private $offset;
|
||||
|
||||
/** @var int|null */
|
||||
private $limit;
|
||||
|
||||
|
||||
/**
|
||||
* @param string $sql command or table or view name, as data source
|
||||
*/
|
||||
public function __construct(string $sql, Connection $connection)
|
||||
{
|
||||
$this->sql = strpbrk($sql, " \t\r\n") === false
|
||||
? $connection->getDriver()->escapeIdentifier($sql) // table name
|
||||
: '(' . $sql . ') t'; // SQL command
|
||||
$this->connection = $connection;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Selects columns to query.
|
||||
* @param string|array $col column name or array of column names
|
||||
* @param string $as column alias
|
||||
*/
|
||||
public function select($col, ?string $as = null): self
|
||||
{
|
||||
if (is_array($col)) {
|
||||
$this->cols = $col;
|
||||
} else {
|
||||
$this->cols[$col] = $as;
|
||||
}
|
||||
|
||||
$this->result = null;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Adds conditions to query.
|
||||
*/
|
||||
public function where($cond): self
|
||||
{
|
||||
$this->conds[] = is_array($cond)
|
||||
? $cond // TODO: not consistent with select and orderBy
|
||||
: func_get_args();
|
||||
$this->result = $this->count = null;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Selects columns to order by.
|
||||
* @param string|array $row column name or array of column names
|
||||
*/
|
||||
public function orderBy($row, string $direction = 'ASC'): self
|
||||
{
|
||||
if (is_array($row)) {
|
||||
$this->sorting = $row;
|
||||
} else {
|
||||
$this->sorting[$row] = $direction;
|
||||
}
|
||||
|
||||
$this->result = null;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Limits number of rows.
|
||||
*/
|
||||
public function applyLimit(int $limit, ?int $offset = null): self
|
||||
{
|
||||
$this->limit = $limit;
|
||||
$this->offset = $offset;
|
||||
$this->result = $this->count = null;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
final public function getConnection(): Connection
|
||||
{
|
||||
return $this->connection;
|
||||
}
|
||||
|
||||
|
||||
/********************* executing ****************d*g**/
|
||||
|
||||
|
||||
/**
|
||||
* Returns (and queries) Result.
|
||||
*/
|
||||
public function getResult(): Result
|
||||
{
|
||||
if ($this->result === null) {
|
||||
$this->result = $this->connection->nativeQuery($this->__toString());
|
||||
}
|
||||
|
||||
return $this->result;
|
||||
}
|
||||
|
||||
|
||||
public function getIterator(): ResultIterator
|
||||
{
|
||||
return $this->getResult()->getIterator();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Generates, executes SQL query and fetches the single row.
|
||||
*/
|
||||
public function fetch(): ?Row
|
||||
{
|
||||
return $this->getResult()->fetch();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Like fetch(), but returns only first field.
|
||||
* @return mixed value on success, null if no next record
|
||||
*/
|
||||
public function fetchSingle()
|
||||
{
|
||||
return $this->getResult()->fetchSingle();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Fetches all records from table.
|
||||
*/
|
||||
public function fetchAll(): array
|
||||
{
|
||||
return $this->getResult()->fetchAll();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Fetches all records from table and returns associative tree.
|
||||
*/
|
||||
public function fetchAssoc(string $assoc): array
|
||||
{
|
||||
return $this->getResult()->fetchAssoc($assoc);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Fetches all records from table like $key => $value pairs.
|
||||
*/
|
||||
public function fetchPairs(?string $key = null, ?string $value = null): array
|
||||
{
|
||||
return $this->getResult()->fetchPairs($key, $value);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Discards the internal cache.
|
||||
*/
|
||||
public function release(): void
|
||||
{
|
||||
$this->result = $this->count = $this->totalCount = null;
|
||||
}
|
||||
|
||||
|
||||
/********************* exporting ****************d*g**/
|
||||
|
||||
|
||||
/**
|
||||
* Returns this data source wrapped in Fluent object.
|
||||
*/
|
||||
public function toFluent(): Fluent
|
||||
{
|
||||
return $this->connection->select('*')->from('(%SQL) t', $this->__toString());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns this data source wrapped in DataSource object.
|
||||
*/
|
||||
public function toDataSource(): self
|
||||
{
|
||||
return new self($this->__toString(), $this->connection);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns SQL query.
|
||||
*/
|
||||
public function __toString(): string
|
||||
{
|
||||
try {
|
||||
return $this->connection->translate(
|
||||
"\nSELECT %n",
|
||||
(empty($this->cols) ? '*' : $this->cols),
|
||||
"\nFROM %SQL",
|
||||
$this->sql,
|
||||
"\n%ex",
|
||||
$this->conds ? ['WHERE %and', $this->conds] : null,
|
||||
"\n%ex",
|
||||
$this->sorting ? ['ORDER BY %by', $this->sorting] : null,
|
||||
"\n%ofs %lmt",
|
||||
$this->offset,
|
||||
$this->limit
|
||||
);
|
||||
} catch (\Throwable $e) {
|
||||
trigger_error($e->getMessage(), E_USER_ERROR);
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/********************* counting ****************d*g**/
|
||||
|
||||
|
||||
/**
|
||||
* Returns the number of rows in a given data source.
|
||||
*/
|
||||
public function count(): int
|
||||
{
|
||||
if ($this->count === null) {
|
||||
$this->count = $this->conds || $this->offset || $this->limit
|
||||
? Helpers::intVal($this->connection->nativeQuery(
|
||||
'SELECT COUNT(*) FROM (' . $this->__toString() . ') t'
|
||||
)->fetchSingle())
|
||||
: $this->getTotalCount();
|
||||
}
|
||||
|
||||
return $this->count;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the number of rows in a given data source.
|
||||
*/
|
||||
public function getTotalCount(): int
|
||||
{
|
||||
if ($this->totalCount === null) {
|
||||
$this->totalCount = Helpers::intVal($this->connection->nativeQuery(
|
||||
'SELECT COUNT(*) FROM ' . $this->sql
|
||||
)->fetchSingle());
|
||||
}
|
||||
|
||||
return $this->totalCount;
|
||||
}
|
||||
}
|
@@ -1,39 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Dibi;
|
||||
|
||||
|
||||
/**
|
||||
* DateTime.
|
||||
*/
|
||||
class DateTime extends \DateTimeImmutable
|
||||
{
|
||||
use Strict;
|
||||
|
||||
/**
|
||||
* @param string|int $time
|
||||
*/
|
||||
public function __construct($time = 'now', ?\DateTimeZone $timezone = null)
|
||||
{
|
||||
$timezone = $timezone ?: new \DateTimeZone(date_default_timezone_get());
|
||||
if (is_numeric($time)) {
|
||||
$tmp = (new self('@' . $time))->setTimezone($timezone);
|
||||
parent::__construct($tmp->format('Y-m-d H:i:s.u'), $tmp->getTimezone());
|
||||
} else {
|
||||
parent::__construct($time, $timezone);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function __toString(): string
|
||||
{
|
||||
return $this->format('Y-m-d H:i:s.u');
|
||||
}
|
||||
}
|
@@ -1,219 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Dibi\Drivers;
|
||||
|
||||
use Dibi;
|
||||
|
||||
|
||||
/**
|
||||
* The dummy driver for testing purposes.
|
||||
*/
|
||||
class DummyDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
|
||||
{
|
||||
use Dibi\Strict;
|
||||
|
||||
public function disconnect(): void
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
public function query(string $sql): ?Dibi\ResultDriver
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public function getAffectedRows(): ?int
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public function getInsertId(?string $sequence): ?int
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public function begin(?string $savepoint = null): void
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
public function commit(?string $savepoint = null): void
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
public function rollback(?string $savepoint = null): void
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
public function getResource()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the connection reflector.
|
||||
*/
|
||||
public function getReflector(): Dibi\Reflector
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/********************* SQL ****************d*g**/
|
||||
|
||||
|
||||
/**
|
||||
* Encodes data for use in a SQL statement.
|
||||
*/
|
||||
public function escapeText(string $value): string
|
||||
{
|
||||
return "'" . str_replace("'", "''", $value) . "'";
|
||||
}
|
||||
|
||||
|
||||
public function escapeBinary(string $value): string
|
||||
{
|
||||
return "N'" . str_replace("'", "''", $value) . "'";
|
||||
}
|
||||
|
||||
|
||||
public function escapeIdentifier(string $value): string
|
||||
{
|
||||
return '[' . strtr($value, '[]', ' ') . ']';
|
||||
}
|
||||
|
||||
|
||||
public function escapeBool(bool $value): string
|
||||
{
|
||||
return $value ? '1' : '0';
|
||||
}
|
||||
|
||||
|
||||
public function escapeDate(\DateTimeInterface $value): string
|
||||
{
|
||||
return $value->format("'Y-m-d'");
|
||||
}
|
||||
|
||||
|
||||
public function escapeDateTime(\DateTimeInterface $value): string
|
||||
{
|
||||
return $value->format("'Y-m-d H:i:s.u'");
|
||||
}
|
||||
|
||||
|
||||
public function escapeDateInterval(\DateInterval $value): string
|
||||
{
|
||||
throw new Dibi\NotImplementedException;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Encodes string for use in a LIKE statement.
|
||||
*/
|
||||
public function escapeLike(string $value, int $pos): string
|
||||
{
|
||||
$value = strtr($value, ["'" => "''", '%' => '[%]', '_' => '[_]', '[' => '[[]']);
|
||||
return ($pos & 1 ? "'%" : "'") . $value . ($pos & 2 ? "%'" : "'");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Injects LIMIT/OFFSET to the SQL query.
|
||||
*/
|
||||
public function applyLimit(string &$sql, ?int $limit, ?int $offset): void
|
||||
{
|
||||
if ($limit < 0 || $offset < 0) {
|
||||
throw new Dibi\NotSupportedException('Negative offset or limit.');
|
||||
|
||||
} elseif ($limit !== null || $offset) {
|
||||
$sql .= ' LIMIT ' . ($limit ?? '-1')
|
||||
. ($offset ? ' OFFSET ' . $offset : '');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/********************* Result ****************d*g**/
|
||||
|
||||
|
||||
public function getRowCount(): int
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
public function fetch(bool $assoc): ?array
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public function seek(int $row): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public function free(): void
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
public function getResultResource()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
public function getResultColumns(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Decodes data from result set.
|
||||
*/
|
||||
public function unescapeBinary(string $value): string
|
||||
{
|
||||
return $value;
|
||||
}
|
||||
|
||||
|
||||
/********************* Reflector ****************d*g**/
|
||||
|
||||
|
||||
public function getTables(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
|
||||
public function getColumns(string $table): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
|
||||
public function getIndexes(string $table): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
|
||||
public function getForeignKeys(string $table): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
}
|
@@ -1,287 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Dibi\Drivers;
|
||||
|
||||
use Dibi;
|
||||
use Dibi\Helpers;
|
||||
|
||||
|
||||
/**
|
||||
* The driver for Firebird/InterBase database.
|
||||
*
|
||||
* Driver options:
|
||||
* - database => the path to database file (server:/path/database.fdb)
|
||||
* - username (or user)
|
||||
* - password (or pass)
|
||||
* - 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.
|
||||
* - resource (resource) => existing connection resource
|
||||
*/
|
||||
class FirebirdDriver implements Dibi\Driver
|
||||
{
|
||||
use Dibi\Strict;
|
||||
|
||||
public const ERROR_EXCEPTION_THROWN = -836;
|
||||
|
||||
/** @var resource */
|
||||
private $connection;
|
||||
|
||||
/** @var resource|null */
|
||||
private $transaction;
|
||||
|
||||
/** @var bool */
|
||||
private $inTransaction = false;
|
||||
|
||||
|
||||
/** @throws Dibi\NotSupportedException */
|
||||
public function __construct(array $config)
|
||||
{
|
||||
if (!extension_loaded('interbase')) {
|
||||
throw new Dibi\NotSupportedException("PHP extension 'interbase' is not loaded.");
|
||||
}
|
||||
|
||||
Helpers::alias($config, 'database', 'db');
|
||||
|
||||
if (isset($config['resource'])) {
|
||||
$this->connection = $config['resource'];
|
||||
|
||||
} else {
|
||||
// default values
|
||||
$config += [
|
||||
'username' => ini_get('ibase.default_password'),
|
||||
'password' => ini_get('ibase.default_user'),
|
||||
'database' => ini_get('ibase.default_db'),
|
||||
'charset' => ini_get('ibase.default_charset'),
|
||||
'buffers' => 0,
|
||||
];
|
||||
|
||||
$this->connection = empty($config['persistent'])
|
||||
? @ibase_connect($config['database'], $config['username'], $config['password'], $config['charset'], $config['buffers']) // intentionally @
|
||||
: @ibase_pconnect($config['database'], $config['username'], $config['password'], $config['charset'], $config['buffers']); // intentionally @
|
||||
|
||||
if (!is_resource($this->connection)) {
|
||||
throw new Dibi\DriverException(ibase_errmsg(), ibase_errcode());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Disconnects from a database.
|
||||
*/
|
||||
public function disconnect(): void
|
||||
{
|
||||
@ibase_close($this->connection); // @ - connection can be already disconnected
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Executes the SQL query.
|
||||
* @throws Dibi\DriverException|Dibi\Exception
|
||||
*/
|
||||
public function query(string $sql): ?Dibi\ResultDriver
|
||||
{
|
||||
$resource = $this->inTransaction
|
||||
? $this->transaction
|
||||
: $this->connection;
|
||||
$res = ibase_query($resource, $sql);
|
||||
|
||||
if ($res === false) {
|
||||
if (ibase_errcode() === self::ERROR_EXCEPTION_THROWN) {
|
||||
preg_match('/exception (\d+) (\w+) (.*)/i', ibase_errmsg(), $match);
|
||||
throw new Dibi\ProcedureException($match[3], $match[1], $match[2], $sql);
|
||||
|
||||
} else {
|
||||
throw new Dibi\DriverException(ibase_errmsg(), ibase_errcode(), $sql);
|
||||
}
|
||||
} elseif (is_resource($res)) {
|
||||
return $this->createResultDriver($res);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets the number of affected rows by the last INSERT, UPDATE or DELETE query.
|
||||
*/
|
||||
public function getAffectedRows(): ?int
|
||||
{
|
||||
return Helpers::false2Null(ibase_affected_rows($this->connection));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query.
|
||||
*/
|
||||
public function getInsertId(?string $sequence): ?int
|
||||
{
|
||||
return Helpers::false2Null(ibase_gen_id($sequence, 0, $this->connection));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Begins a transaction (if supported).
|
||||
* @throws Dibi\DriverException
|
||||
*/
|
||||
public function begin(?string $savepoint = null): void
|
||||
{
|
||||
if ($savepoint !== null) {
|
||||
throw new Dibi\NotSupportedException('Savepoints are not supported in Firebird/Interbase.');
|
||||
}
|
||||
|
||||
$this->transaction = ibase_trans($this->getResource());
|
||||
$this->inTransaction = true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Commits statements in a transaction.
|
||||
* @throws Dibi\DriverException
|
||||
*/
|
||||
public function commit(?string $savepoint = null): void
|
||||
{
|
||||
if ($savepoint !== null) {
|
||||
throw new Dibi\NotSupportedException('Savepoints are not supported in Firebird/Interbase.');
|
||||
}
|
||||
|
||||
if (!ibase_commit($this->transaction)) {
|
||||
throw new Dibi\DriverException('Unable to handle operation - failure when commiting transaction.');
|
||||
}
|
||||
|
||||
$this->inTransaction = false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Rollback changes in a transaction.
|
||||
* @throws Dibi\DriverException
|
||||
*/
|
||||
public function rollback(?string $savepoint = null): void
|
||||
{
|
||||
if ($savepoint !== null) {
|
||||
throw new Dibi\NotSupportedException('Savepoints are not supported in Firebird/Interbase.');
|
||||
}
|
||||
|
||||
if (!ibase_rollback($this->transaction)) {
|
||||
throw new Dibi\DriverException('Unable to handle operation - failure when rolbacking transaction.');
|
||||
}
|
||||
|
||||
$this->inTransaction = false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Is in transaction?
|
||||
*/
|
||||
public function inTransaction(): bool
|
||||
{
|
||||
return $this->inTransaction;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the connection resource.
|
||||
* @return resource|null
|
||||
*/
|
||||
public function getResource()
|
||||
{
|
||||
return is_resource($this->connection) ? $this->connection : null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the connection reflector.
|
||||
*/
|
||||
public function getReflector(): Dibi\Reflector
|
||||
{
|
||||
return new FirebirdReflector($this);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Result set driver factory.
|
||||
* @param resource $resource
|
||||
*/
|
||||
public function createResultDriver($resource): FirebirdResult
|
||||
{
|
||||
return new FirebirdResult($resource);
|
||||
}
|
||||
|
||||
|
||||
/********************* SQL ********************/
|
||||
|
||||
|
||||
/**
|
||||
* Encodes data for use in a SQL statement.
|
||||
*/
|
||||
public function escapeText(string $value): string
|
||||
{
|
||||
return "'" . str_replace("'", "''", $value) . "'";
|
||||
}
|
||||
|
||||
|
||||
public function escapeBinary(string $value): string
|
||||
{
|
||||
return "'" . str_replace("'", "''", $value) . "'";
|
||||
}
|
||||
|
||||
|
||||
public function escapeIdentifier(string $value): string
|
||||
{
|
||||
return '"' . str_replace('"', '""', $value) . '"';
|
||||
}
|
||||
|
||||
|
||||
public function escapeBool(bool $value): string
|
||||
{
|
||||
return $value ? '1' : '0';
|
||||
}
|
||||
|
||||
|
||||
public function escapeDate(\DateTimeInterface $value): string
|
||||
{
|
||||
return $value->format("'Y-m-d'");
|
||||
}
|
||||
|
||||
|
||||
public function escapeDateTime(\DateTimeInterface $value): string
|
||||
{
|
||||
return "'" . substr($value->format('Y-m-d H:i:s.u'), 0, -2) . "'";
|
||||
}
|
||||
|
||||
|
||||
public function escapeDateInterval(\DateInterval $value): string
|
||||
{
|
||||
throw new Dibi\NotImplementedException;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Encodes string for use in a LIKE statement.
|
||||
*/
|
||||
public function escapeLike(string $value, int $pos): string
|
||||
{
|
||||
$value = addcslashes($this->escapeText($value), '%_\\');
|
||||
return ($pos & 1 ? "'%" : "'") . $value . ($pos & 2 ? "%'" : "'") . " ESCAPE '\\'";
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Injects LIMIT/OFFSET to the SQL query.
|
||||
*/
|
||||
public function applyLimit(string &$sql, ?int $limit, ?int $offset): void
|
||||
{
|
||||
if ($limit > 0 || $offset > 0) {
|
||||
// http://www.firebirdsql.org/refdocs/langrefupd20-select.html
|
||||
$sql = 'SELECT ' . ($limit > 0 ? 'FIRST ' . $limit : '') . ($offset > 0 ? ' SKIP ' . $offset : '') . ' * FROM (' . $sql . ')';
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,391 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Dibi\Drivers;
|
||||
|
||||
use Dibi;
|
||||
|
||||
|
||||
/**
|
||||
* The reflector for Firebird/InterBase database.
|
||||
*/
|
||||
class FirebirdReflector implements Dibi\Reflector
|
||||
{
|
||||
use Dibi\Strict;
|
||||
|
||||
/** @var Dibi\Driver */
|
||||
private $driver;
|
||||
|
||||
|
||||
public function __construct(Dibi\Driver $driver)
|
||||
{
|
||||
$this->driver = $driver;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns list of tables.
|
||||
*/
|
||||
public function getTables(): array
|
||||
{
|
||||
$res = $this->driver->query("
|
||||
SELECT TRIM(RDB\$RELATION_NAME),
|
||||
CASE RDB\$VIEW_BLR WHEN NULL THEN 'TRUE' ELSE 'FALSE' END
|
||||
FROM RDB\$RELATIONS
|
||||
WHERE RDB\$SYSTEM_FLAG = 0;
|
||||
");
|
||||
$tables = [];
|
||||
while ($row = $res->fetch(false)) {
|
||||
$tables[] = [
|
||||
'name' => $row[0],
|
||||
'view' => $row[1] === 'TRUE',
|
||||
];
|
||||
}
|
||||
|
||||
return $tables;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns metadata for all columns in a table.
|
||||
*/
|
||||
public function getColumns(string $table): array
|
||||
{
|
||||
$table = strtoupper($table);
|
||||
$res = $this->driver->query("
|
||||
SELECT TRIM(r.RDB\$FIELD_NAME) AS FIELD_NAME,
|
||||
CASE f.RDB\$FIELD_TYPE
|
||||
WHEN 261 THEN 'BLOB'
|
||||
WHEN 14 THEN 'CHAR'
|
||||
WHEN 40 THEN 'CSTRING'
|
||||
WHEN 11 THEN 'D_FLOAT'
|
||||
WHEN 27 THEN 'DOUBLE'
|
||||
WHEN 10 THEN 'FLOAT'
|
||||
WHEN 16 THEN 'INT64'
|
||||
WHEN 8 THEN 'INTEGER'
|
||||
WHEN 9 THEN 'QUAD'
|
||||
WHEN 7 THEN 'SMALLINT'
|
||||
WHEN 12 THEN 'DATE'
|
||||
WHEN 13 THEN 'TIME'
|
||||
WHEN 35 THEN 'TIMESTAMP'
|
||||
WHEN 37 THEN 'VARCHAR'
|
||||
ELSE 'UNKNOWN'
|
||||
END AS FIELD_TYPE,
|
||||
f.RDB\$FIELD_LENGTH AS FIELD_LENGTH,
|
||||
r.RDB\$DEFAULT_VALUE AS DEFAULT_VALUE,
|
||||
CASE r.RDB\$NULL_FLAG
|
||||
WHEN 1 THEN 'FALSE' ELSE 'TRUE'
|
||||
END AS NULLABLE
|
||||
FROM RDB\$RELATION_FIELDS r
|
||||
LEFT JOIN RDB\$FIELDS f ON r.RDB\$FIELD_SOURCE = f.RDB\$FIELD_NAME
|
||||
WHERE r.RDB\$RELATION_NAME = '$table'
|
||||
ORDER BY r.RDB\$FIELD_POSITION;
|
||||
");
|
||||
$columns = [];
|
||||
while ($row = $res->fetch(true)) {
|
||||
$key = $row['FIELD_NAME'];
|
||||
$columns[$key] = [
|
||||
'name' => $key,
|
||||
'table' => $table,
|
||||
'nativetype' => trim($row['FIELD_TYPE']),
|
||||
'size' => $row['FIELD_LENGTH'],
|
||||
'nullable' => $row['NULLABLE'] === 'TRUE',
|
||||
'default' => $row['DEFAULT_VALUE'],
|
||||
'autoincrement' => false,
|
||||
];
|
||||
}
|
||||
|
||||
return $columns;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns metadata for all indexes in a table (the constraints are included).
|
||||
*/
|
||||
public function getIndexes(string $table): array
|
||||
{
|
||||
$table = strtoupper($table);
|
||||
$res = $this->driver->query("
|
||||
SELECT TRIM(s.RDB\$INDEX_NAME) AS INDEX_NAME,
|
||||
TRIM(s.RDB\$FIELD_NAME) AS FIELD_NAME,
|
||||
i.RDB\$UNIQUE_FLAG AS UNIQUE_FLAG,
|
||||
i.RDB\$FOREIGN_KEY AS FOREIGN_KEY,
|
||||
TRIM(r.RDB\$CONSTRAINT_TYPE) AS CONSTRAINT_TYPE,
|
||||
s.RDB\$FIELD_POSITION AS FIELD_POSITION
|
||||
FROM RDB\$INDEX_SEGMENTS s
|
||||
LEFT JOIN RDB\$INDICES i ON i.RDB\$INDEX_NAME = s.RDB\$INDEX_NAME
|
||||
LEFT JOIN RDB\$RELATION_CONSTRAINTS r ON r.RDB\$INDEX_NAME = s.RDB\$INDEX_NAME
|
||||
WHERE UPPER(i.RDB\$RELATION_NAME) = '$table'
|
||||
ORDER BY s.RDB\$FIELD_POSITION
|
||||
");
|
||||
$indexes = [];
|
||||
while ($row = $res->fetch(true)) {
|
||||
$key = $row['INDEX_NAME'];
|
||||
$indexes[$key]['name'] = $key;
|
||||
$indexes[$key]['unique'] = $row['UNIQUE_FLAG'] === 1;
|
||||
$indexes[$key]['primary'] = $row['CONSTRAINT_TYPE'] === 'PRIMARY KEY';
|
||||
$indexes[$key]['table'] = $table;
|
||||
$indexes[$key]['columns'][$row['FIELD_POSITION']] = $row['FIELD_NAME'];
|
||||
}
|
||||
|
||||
return $indexes;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns metadata for all foreign keys in a table.
|
||||
*/
|
||||
public function getForeignKeys(string $table): array
|
||||
{
|
||||
$table = strtoupper($table);
|
||||
$res = $this->driver->query("
|
||||
SELECT TRIM(s.RDB\$INDEX_NAME) AS INDEX_NAME,
|
||||
TRIM(s.RDB\$FIELD_NAME) AS FIELD_NAME,
|
||||
FROM RDB\$INDEX_SEGMENTS s
|
||||
LEFT JOIN RDB\$RELATION_CONSTRAINTS r ON r.RDB\$INDEX_NAME = s.RDB\$INDEX_NAME
|
||||
WHERE UPPER(i.RDB\$RELATION_NAME) = '$table'
|
||||
AND r.RDB\$CONSTRAINT_TYPE = 'FOREIGN KEY'
|
||||
ORDER BY s.RDB\$FIELD_POSITION
|
||||
");
|
||||
$keys = [];
|
||||
while ($row = $res->fetch(true)) {
|
||||
$key = $row['INDEX_NAME'];
|
||||
$keys[$key] = [
|
||||
'name' => $key,
|
||||
'column' => $row['FIELD_NAME'],
|
||||
'table' => $table,
|
||||
];
|
||||
}
|
||||
|
||||
return $keys;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns list of indices in given table (the constraints are not listed).
|
||||
*/
|
||||
public function getIndices(string $table): array
|
||||
{
|
||||
$res = $this->driver->query("
|
||||
SELECT TRIM(RDB\$INDEX_NAME)
|
||||
FROM RDB\$INDICES
|
||||
WHERE RDB\$RELATION_NAME = UPPER('$table')
|
||||
AND RDB\$UNIQUE_FLAG IS NULL
|
||||
AND RDB\$FOREIGN_KEY IS NULL;
|
||||
");
|
||||
$indices = [];
|
||||
while ($row = $res->fetch(false)) {
|
||||
$indices[] = $row[0];
|
||||
}
|
||||
|
||||
return $indices;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns list of constraints in given table.
|
||||
*/
|
||||
public function getConstraints(string $table): array
|
||||
{
|
||||
$res = $this->driver->query("
|
||||
SELECT TRIM(RDB\$INDEX_NAME)
|
||||
FROM RDB\$INDICES
|
||||
WHERE RDB\$RELATION_NAME = UPPER('$table')
|
||||
AND (
|
||||
RDB\$UNIQUE_FLAG IS NOT NULL
|
||||
OR RDB\$FOREIGN_KEY IS NOT NULL
|
||||
);
|
||||
");
|
||||
$constraints = [];
|
||||
while ($row = $res->fetch(false)) {
|
||||
$constraints[] = $row[0];
|
||||
}
|
||||
|
||||
return $constraints;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns metadata for all triggers in a table or database.
|
||||
* (Only if user has permissions on ALTER TABLE, INSERT/UPDATE/DELETE record in table)
|
||||
*/
|
||||
public function getTriggersMeta(?string $table = null): array
|
||||
{
|
||||
$res = $this->driver->query(
|
||||
"
|
||||
SELECT TRIM(RDB\$TRIGGER_NAME) AS TRIGGER_NAME,
|
||||
TRIM(RDB\$RELATION_NAME) AS TABLE_NAME,
|
||||
CASE RDB\$TRIGGER_TYPE
|
||||
WHEN 1 THEN 'BEFORE'
|
||||
WHEN 2 THEN 'AFTER'
|
||||
WHEN 3 THEN 'BEFORE'
|
||||
WHEN 4 THEN 'AFTER'
|
||||
WHEN 5 THEN 'BEFORE'
|
||||
WHEN 6 THEN 'AFTER'
|
||||
END AS TRIGGER_TYPE,
|
||||
CASE RDB\$TRIGGER_TYPE
|
||||
WHEN 1 THEN 'INSERT'
|
||||
WHEN 2 THEN 'INSERT'
|
||||
WHEN 3 THEN 'UPDATE'
|
||||
WHEN 4 THEN 'UPDATE'
|
||||
WHEN 5 THEN 'DELETE'
|
||||
WHEN 6 THEN 'DELETE'
|
||||
END AS TRIGGER_EVENT,
|
||||
CASE RDB\$TRIGGER_INACTIVE
|
||||
WHEN 1 THEN 'FALSE' ELSE 'TRUE'
|
||||
END AS TRIGGER_ENABLED
|
||||
FROM RDB\$TRIGGERS
|
||||
WHERE RDB\$SYSTEM_FLAG = 0"
|
||||
. ($table === null ? ';' : " AND RDB\$RELATION_NAME = UPPER('$table');")
|
||||
);
|
||||
$triggers = [];
|
||||
while ($row = $res->fetch(true)) {
|
||||
$triggers[$row['TRIGGER_NAME']] = [
|
||||
'name' => $row['TRIGGER_NAME'],
|
||||
'table' => $row['TABLE_NAME'],
|
||||
'type' => trim($row['TRIGGER_TYPE']),
|
||||
'event' => trim($row['TRIGGER_EVENT']),
|
||||
'enabled' => trim($row['TRIGGER_ENABLED']) === 'TRUE',
|
||||
];
|
||||
}
|
||||
|
||||
return $triggers;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns list of triggers for given table.
|
||||
* (Only if user has permissions on ALTER TABLE, INSERT/UPDATE/DELETE record in table)
|
||||
*/
|
||||
public function getTriggers(?string $table = null): array
|
||||
{
|
||||
$q = 'SELECT TRIM(RDB$TRIGGER_NAME)
|
||||
FROM RDB$TRIGGERS
|
||||
WHERE RDB$SYSTEM_FLAG = 0';
|
||||
$q .= $table === null
|
||||
? ';'
|
||||
: " AND RDB\$RELATION_NAME = UPPER('$table')";
|
||||
|
||||
$res = $this->driver->query($q);
|
||||
$triggers = [];
|
||||
while ($row = $res->fetch(false)) {
|
||||
$triggers[] = $row[0];
|
||||
}
|
||||
|
||||
return $triggers;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns metadata from stored procedures and their input and output parameters.
|
||||
*/
|
||||
public function getProceduresMeta(): array
|
||||
{
|
||||
$res = $this->driver->query("
|
||||
SELECT
|
||||
TRIM(p.RDB\$PARAMETER_NAME) AS PARAMETER_NAME,
|
||||
TRIM(p.RDB\$PROCEDURE_NAME) AS PROCEDURE_NAME,
|
||||
CASE p.RDB\$PARAMETER_TYPE
|
||||
WHEN 0 THEN 'INPUT'
|
||||
WHEN 1 THEN 'OUTPUT'
|
||||
ELSE 'UNKNOWN'
|
||||
END AS PARAMETER_TYPE,
|
||||
CASE f.RDB\$FIELD_TYPE
|
||||
WHEN 261 THEN 'BLOB'
|
||||
WHEN 14 THEN 'CHAR'
|
||||
WHEN 40 THEN 'CSTRING'
|
||||
WHEN 11 THEN 'D_FLOAT'
|
||||
WHEN 27 THEN 'DOUBLE'
|
||||
WHEN 10 THEN 'FLOAT'
|
||||
WHEN 16 THEN 'INT64'
|
||||
WHEN 8 THEN 'INTEGER'
|
||||
WHEN 9 THEN 'QUAD'
|
||||
WHEN 7 THEN 'SMALLINT'
|
||||
WHEN 12 THEN 'DATE'
|
||||
WHEN 13 THEN 'TIME'
|
||||
WHEN 35 THEN 'TIMESTAMP'
|
||||
WHEN 37 THEN 'VARCHAR'
|
||||
ELSE 'UNKNOWN'
|
||||
END AS FIELD_TYPE,
|
||||
f.RDB\$FIELD_LENGTH AS FIELD_LENGTH,
|
||||
p.RDB\$PARAMETER_NUMBER AS PARAMETER_NUMBER
|
||||
FROM RDB\$PROCEDURE_PARAMETERS p
|
||||
LEFT JOIN RDB\$FIELDS f ON f.RDB\$FIELD_NAME = p.RDB\$FIELD_SOURCE
|
||||
ORDER BY p.RDB\$PARAMETER_TYPE, p.RDB\$PARAMETER_NUMBER;
|
||||
");
|
||||
$procedures = [];
|
||||
while ($row = $res->fetch(true)) {
|
||||
$key = $row['PROCEDURE_NAME'];
|
||||
$io = trim($row['PARAMETER_TYPE']);
|
||||
$num = $row['PARAMETER_NUMBER'];
|
||||
$procedures[$key]['name'] = $row['PROCEDURE_NAME'];
|
||||
$procedures[$key]['params'][$io][$num]['name'] = $row['PARAMETER_NAME'];
|
||||
$procedures[$key]['params'][$io][$num]['type'] = trim($row['FIELD_TYPE']);
|
||||
$procedures[$key]['params'][$io][$num]['size'] = $row['FIELD_LENGTH'];
|
||||
}
|
||||
|
||||
return $procedures;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns list of stored procedures.
|
||||
*/
|
||||
public function getProcedures(): array
|
||||
{
|
||||
$res = $this->driver->query('
|
||||
SELECT TRIM(RDB$PROCEDURE_NAME)
|
||||
FROM RDB$PROCEDURES;
|
||||
');
|
||||
$procedures = [];
|
||||
while ($row = $res->fetch(false)) {
|
||||
$procedures[] = $row[0];
|
||||
}
|
||||
|
||||
return $procedures;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns list of generators.
|
||||
*/
|
||||
public function getGenerators(): array
|
||||
{
|
||||
$res = $this->driver->query('
|
||||
SELECT TRIM(RDB$GENERATOR_NAME)
|
||||
FROM RDB$GENERATORS
|
||||
WHERE RDB$SYSTEM_FLAG = 0;
|
||||
');
|
||||
$generators = [];
|
||||
while ($row = $res->fetch(false)) {
|
||||
$generators[] = $row[0];
|
||||
}
|
||||
|
||||
return $generators;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns list of user defined functions (UDF).
|
||||
*/
|
||||
public function getFunctions(): array
|
||||
{
|
||||
$res = $this->driver->query('
|
||||
SELECT TRIM(RDB$FUNCTION_NAME)
|
||||
FROM RDB$FUNCTIONS
|
||||
WHERE RDB$SYSTEM_FLAG = 0;
|
||||
');
|
||||
$functions = [];
|
||||
while ($row = $res->fetch(false)) {
|
||||
$functions[] = $row[0];
|
||||
}
|
||||
|
||||
return $functions;
|
||||
}
|
||||
}
|
@@ -1,141 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Dibi\Drivers;
|
||||
|
||||
use Dibi;
|
||||
use Dibi\Helpers;
|
||||
|
||||
|
||||
/**
|
||||
* The driver for Firebird/InterBase result set.
|
||||
*/
|
||||
class FirebirdResult implements Dibi\ResultDriver
|
||||
{
|
||||
use Dibi\Strict;
|
||||
|
||||
/** @var resource */
|
||||
private $resultSet;
|
||||
|
||||
/** @var bool */
|
||||
private $autoFree = true;
|
||||
|
||||
|
||||
/**
|
||||
* @param resource $resultSet
|
||||
*/
|
||||
public function __construct($resultSet)
|
||||
{
|
||||
$this->resultSet = $resultSet;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Automatically frees the resources allocated for this result set.
|
||||
*/
|
||||
public function __destruct()
|
||||
{
|
||||
if ($this->autoFree && $this->getResultResource()) {
|
||||
$this->free();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the number of rows in a result set.
|
||||
*/
|
||||
public function getRowCount(): int
|
||||
{
|
||||
throw new Dibi\NotSupportedException('Firebird/Interbase do not support returning number of rows in result set.');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Fetches the row at current position and moves the internal cursor to the next position.
|
||||
* @param bool $assoc true for associative array, false for numeric
|
||||
*/
|
||||
public function fetch(bool $assoc): ?array
|
||||
{
|
||||
$result = $assoc
|
||||
? @ibase_fetch_assoc($this->resultSet, IBASE_TEXT)
|
||||
: @ibase_fetch_row($this->resultSet, IBASE_TEXT); // intentionally @
|
||||
|
||||
if (ibase_errcode()) {
|
||||
if (ibase_errcode() === FirebirdDriver::ERROR_EXCEPTION_THROWN) {
|
||||
preg_match('/exception (\d+) (\w+) (.*)/is', ibase_errmsg(), $match);
|
||||
throw new Dibi\ProcedureException($match[3], $match[1], $match[2]);
|
||||
|
||||
} else {
|
||||
throw new Dibi\DriverException(ibase_errmsg(), ibase_errcode());
|
||||
}
|
||||
}
|
||||
|
||||
return Helpers::false2Null($result);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Moves cursor position without fetching row.
|
||||
* @throws Dibi\Exception
|
||||
*/
|
||||
public function seek(int $row): bool
|
||||
{
|
||||
throw new Dibi\NotSupportedException('Firebird/Interbase do not support seek in result set.');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Frees the resources allocated for this result set.
|
||||
*/
|
||||
public function free(): void
|
||||
{
|
||||
ibase_free_result($this->resultSet);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the result set resource.
|
||||
* @return resource|null
|
||||
*/
|
||||
public function getResultResource()
|
||||
{
|
||||
$this->autoFree = false;
|
||||
return is_resource($this->resultSet) ? $this->resultSet : null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns metadata for all columns in a result set.
|
||||
*/
|
||||
public function getResultColumns(): array
|
||||
{
|
||||
$count = ibase_num_fields($this->resultSet);
|
||||
$columns = [];
|
||||
for ($i = 0; $i < $count; $i++) {
|
||||
$row = (array) ibase_field_info($this->resultSet, $i);
|
||||
$columns[] = [
|
||||
'name' => $row['name'],
|
||||
'fullname' => $row['name'],
|
||||
'table' => $row['relation'],
|
||||
'nativetype' => $row['type'],
|
||||
];
|
||||
}
|
||||
|
||||
return $columns;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Decodes data from result set.
|
||||
*/
|
||||
public function unescapeBinary(string $value): string
|
||||
{
|
||||
return $value;
|
||||
}
|
||||
}
|
@@ -1,132 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Dibi\Drivers;
|
||||
|
||||
use Dibi;
|
||||
|
||||
|
||||
/**
|
||||
* The reflector for MySQL databases.
|
||||
* @internal
|
||||
*/
|
||||
class MySqlReflector implements Dibi\Reflector
|
||||
{
|
||||
use Dibi\Strict;
|
||||
|
||||
/** @var Dibi\Driver */
|
||||
private $driver;
|
||||
|
||||
|
||||
public function __construct(Dibi\Driver $driver)
|
||||
{
|
||||
$this->driver = $driver;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns list of tables.
|
||||
*/
|
||||
public function getTables(): array
|
||||
{
|
||||
$res = $this->driver->query('SHOW FULL TABLES');
|
||||
$tables = [];
|
||||
while ($row = $res->fetch(false)) {
|
||||
$tables[] = [
|
||||
'name' => $row[0],
|
||||
'view' => isset($row[1]) && $row[1] === 'VIEW',
|
||||
];
|
||||
}
|
||||
|
||||
return $tables;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns metadata for all columns in a table.
|
||||
*/
|
||||
public function getColumns(string $table): array
|
||||
{
|
||||
$res = $this->driver->query("SHOW FULL COLUMNS FROM {$this->driver->escapeIdentifier($table)}");
|
||||
$columns = [];
|
||||
while ($row = $res->fetch(true)) {
|
||||
$type = explode('(', $row['Type']);
|
||||
$columns[] = [
|
||||
'name' => $row['Field'],
|
||||
'table' => $table,
|
||||
'nativetype' => strtoupper($type[0]),
|
||||
'size' => isset($type[1]) ? (int) $type[1] : null,
|
||||
'nullable' => $row['Null'] === 'YES',
|
||||
'default' => $row['Default'],
|
||||
'autoincrement' => $row['Extra'] === 'auto_increment',
|
||||
'vendor' => $row,
|
||||
];
|
||||
}
|
||||
|
||||
return $columns;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns metadata for all indexes in a table.
|
||||
*/
|
||||
public function getIndexes(string $table): array
|
||||
{
|
||||
$res = $this->driver->query("SHOW INDEX FROM {$this->driver->escapeIdentifier($table)}");
|
||||
$indexes = [];
|
||||
while ($row = $res->fetch(true)) {
|
||||
$indexes[$row['Key_name']]['name'] = $row['Key_name'];
|
||||
$indexes[$row['Key_name']]['unique'] = !$row['Non_unique'];
|
||||
$indexes[$row['Key_name']]['primary'] = $row['Key_name'] === 'PRIMARY';
|
||||
$indexes[$row['Key_name']]['columns'][$row['Seq_in_index'] - 1] = $row['Column_name'];
|
||||
}
|
||||
|
||||
return array_values($indexes);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns metadata for all foreign keys in a table.
|
||||
* @throws Dibi\NotSupportedException
|
||||
*/
|
||||
public function getForeignKeys(string $table): array
|
||||
{
|
||||
$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') {
|
||||
throw new Dibi\NotSupportedException("Foreign keys are not supported in {$data['ENGINE']} tables.");
|
||||
}
|
||||
|
||||
$res = $this->driver->query("
|
||||
SELECT rc.CONSTRAINT_NAME, rc.UPDATE_RULE, rc.DELETE_RULE, kcu.REFERENCED_TABLE_NAME,
|
||||
GROUP_CONCAT(kcu.REFERENCED_COLUMN_NAME ORDER BY kcu.ORDINAL_POSITION) AS REFERENCED_COLUMNS,
|
||||
GROUP_CONCAT(kcu.COLUMN_NAME ORDER BY kcu.ORDINAL_POSITION) AS COLUMNS
|
||||
FROM information_schema.REFERENTIAL_CONSTRAINTS rc
|
||||
INNER JOIN information_schema.KEY_COLUMN_USAGE kcu ON
|
||||
kcu.CONSTRAINT_NAME = rc.CONSTRAINT_NAME
|
||||
AND kcu.CONSTRAINT_SCHEMA = rc.CONSTRAINT_SCHEMA
|
||||
WHERE rc.CONSTRAINT_SCHEMA = DATABASE()
|
||||
AND rc.TABLE_NAME = {$this->driver->escapeText($table)}
|
||||
GROUP BY rc.CONSTRAINT_NAME
|
||||
");
|
||||
|
||||
$foreignKeys = [];
|
||||
while ($row = $res->fetch(true)) {
|
||||
$keyName = $row['CONSTRAINT_NAME'];
|
||||
|
||||
$foreignKeys[$keyName]['name'] = $keyName;
|
||||
$foreignKeys[$keyName]['local'] = explode(',', $row['COLUMNS']);
|
||||
$foreignKeys[$keyName]['table'] = $row['REFERENCED_TABLE_NAME'];
|
||||
$foreignKeys[$keyName]['foreign'] = explode(',', $row['REFERENCED_COLUMNS']);
|
||||
$foreignKeys[$keyName]['onDelete'] = $row['DELETE_RULE'];
|
||||
$foreignKeys[$keyName]['onUpdate'] = $row['UPDATE_RULE'];
|
||||
}
|
||||
|
||||
return array_values($foreignKeys);
|
||||
}
|
||||
}
|
@@ -1,358 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Dibi\Drivers;
|
||||
|
||||
use Dibi;
|
||||
|
||||
|
||||
/**
|
||||
* The driver for MySQL database.
|
||||
*
|
||||
* Driver options:
|
||||
* - host => the MySQL server host name
|
||||
* - port (int) => the port number to attempt to connect to the MySQL server
|
||||
* - socket => the socket or named pipe
|
||||
* - username (or user)
|
||||
* - password (or pass)
|
||||
* - database => the database name to select
|
||||
* - options (array) => array of driver specific constants (MYSQLI_*) and values {@see mysqli_options}
|
||||
* - flags (int) => driver specific constants (MYSQLI_CLIENT_*) {@see mysqli_real_connect}
|
||||
* - charset => character encoding to set (default is utf8)
|
||||
* - persistent (bool) => try to find a persistent link?
|
||||
* - 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
|
||||
* - resource (mysqli) => existing connection resource
|
||||
*/
|
||||
class MySqliDriver implements Dibi\Driver
|
||||
{
|
||||
use Dibi\Strict;
|
||||
|
||||
public const ERROR_ACCESS_DENIED = 1045;
|
||||
|
||||
public const ERROR_DUPLICATE_ENTRY = 1062;
|
||||
|
||||
public const ERROR_DATA_TRUNCATED = 1265;
|
||||
|
||||
/** @var \mysqli */
|
||||
private $connection;
|
||||
|
||||
/** @var bool Is buffered (seekable and countable)? */
|
||||
private $buffered;
|
||||
|
||||
|
||||
/** @throws Dibi\NotSupportedException */
|
||||
public function __construct(array $config)
|
||||
{
|
||||
if (!extension_loaded('mysqli')) {
|
||||
throw new Dibi\NotSupportedException("PHP extension 'mysqli' is not loaded.");
|
||||
}
|
||||
|
||||
mysqli_report(MYSQLI_REPORT_OFF);
|
||||
if (isset($config['resource']) && $config['resource'] instanceof \mysqli) {
|
||||
$this->connection = $config['resource'];
|
||||
|
||||
} else {
|
||||
// default values
|
||||
$config += [
|
||||
'charset' => 'utf8',
|
||||
'timezone' => date('P'),
|
||||
'username' => ini_get('mysqli.default_user'),
|
||||
'password' => ini_get('mysqli.default_pw'),
|
||||
'socket' => (string) ini_get('mysqli.default_socket'),
|
||||
'port' => null,
|
||||
];
|
||||
if (!isset($config['host'])) {
|
||||
$host = ini_get('mysqli.default_host');
|
||||
if ($host) {
|
||||
$config['host'] = $host;
|
||||
$config['port'] = (int) ini_get('mysqli.default_port');
|
||||
} else {
|
||||
$config['host'] = null;
|
||||
$config['port'] = null;
|
||||
}
|
||||
}
|
||||
|
||||
$foo = &$config['flags'];
|
||||
$foo = &$config['database'];
|
||||
|
||||
$this->connection = mysqli_init();
|
||||
if (isset($config['options'])) {
|
||||
foreach ($config['options'] as $key => $value) {
|
||||
$this->connection->options($key, $value);
|
||||
}
|
||||
}
|
||||
|
||||
@$this->connection->real_connect( // intentionally @
|
||||
(empty($config['persistent']) ? '' : 'p:') . $config['host'],
|
||||
$config['username'],
|
||||
$config['password'] ?? '',
|
||||
$config['database'] ?? '',
|
||||
$config['port'] ?? 0,
|
||||
$config['socket'],
|
||||
$config['flags'] ?? 0
|
||||
);
|
||||
|
||||
if ($this->connection->connect_errno) {
|
||||
throw new Dibi\DriverException($this->connection->connect_error, $this->connection->connect_errno);
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($config['charset'])) {
|
||||
if (!@$this->connection->set_charset($config['charset'])) {
|
||||
$this->query("SET NAMES '$config[charset]'");
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($config['sqlmode'])) {
|
||||
$this->query("SET sql_mode='$config[sqlmode]'");
|
||||
}
|
||||
|
||||
if (isset($config['timezone'])) {
|
||||
$this->query("SET time_zone='$config[timezone]'");
|
||||
}
|
||||
|
||||
$this->buffered = empty($config['unbuffered']);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Disconnects from a database.
|
||||
*/
|
||||
public function disconnect(): void
|
||||
{
|
||||
@$this->connection->close(); // @ - connection can be already disconnected
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Pings a server connection, or tries to reconnect if the connection has gone down.
|
||||
*/
|
||||
public function ping(): bool
|
||||
{
|
||||
return $this->connection->ping();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Executes the SQL query.
|
||||
* @throws Dibi\DriverException
|
||||
*/
|
||||
public function query(string $sql): ?Dibi\ResultDriver
|
||||
{
|
||||
$res = @$this->connection->query($sql, $this->buffered ? MYSQLI_STORE_RESULT : MYSQLI_USE_RESULT); // intentionally @
|
||||
|
||||
if ($code = mysqli_errno($this->connection)) {
|
||||
throw static::createException(mysqli_error($this->connection), $code, $sql);
|
||||
|
||||
} elseif ($res instanceof \mysqli_result) {
|
||||
return $this->createResultDriver($res);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param int|string $code
|
||||
*/
|
||||
public static function createException(string $message, $code, string $sql): Dibi\DriverException
|
||||
{
|
||||
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.
|
||||
*/
|
||||
public function getInfo(): array
|
||||
{
|
||||
$res = [];
|
||||
preg_match_all('#(.+?): +(\d+) *#', $this->connection->info, $matches, PREG_SET_ORDER);
|
||||
if (preg_last_error()) {
|
||||
throw new Dibi\PcreException;
|
||||
}
|
||||
|
||||
foreach ($matches as $m) {
|
||||
$res[$m[1]] = (int) $m[2];
|
||||
}
|
||||
|
||||
return $res;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets the number of affected rows by the last INSERT, UPDATE or DELETE query.
|
||||
*/
|
||||
public function getAffectedRows(): ?int
|
||||
{
|
||||
return $this->connection->affected_rows === -1
|
||||
? null
|
||||
: $this->connection->affected_rows;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query.
|
||||
*/
|
||||
public function getInsertId(?string $sequence): ?int
|
||||
{
|
||||
return $this->connection->insert_id ?: null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Begins a transaction (if supported).
|
||||
* @throws Dibi\DriverException
|
||||
*/
|
||||
public function begin(?string $savepoint = null): void
|
||||
{
|
||||
$this->query($savepoint ? "SAVEPOINT $savepoint" : 'START TRANSACTION');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Commits statements in a transaction.
|
||||
* @throws Dibi\DriverException
|
||||
*/
|
||||
public function commit(?string $savepoint = null): void
|
||||
{
|
||||
$this->query($savepoint ? "RELEASE SAVEPOINT $savepoint" : 'COMMIT');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Rollback changes in a transaction.
|
||||
* @throws Dibi\DriverException
|
||||
*/
|
||||
public function rollback(?string $savepoint = null): void
|
||||
{
|
||||
$this->query($savepoint ? "ROLLBACK TO SAVEPOINT $savepoint" : 'ROLLBACK');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the connection resource.
|
||||
*/
|
||||
public function getResource(): ?\mysqli
|
||||
{
|
||||
try {
|
||||
return @$this->connection->thread_id ? $this->connection : null;
|
||||
} catch (\Throwable $e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the connection reflector.
|
||||
*/
|
||||
public function getReflector(): Dibi\Reflector
|
||||
{
|
||||
return new MySqlReflector($this);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Result set driver factory.
|
||||
*/
|
||||
public function createResultDriver(\mysqli_result $result): MySqliResult
|
||||
{
|
||||
return new MySqliResult($result, $this->buffered);
|
||||
}
|
||||
|
||||
|
||||
/********************* SQL ****************d*g**/
|
||||
|
||||
|
||||
/**
|
||||
* Encodes data for use in a SQL statement.
|
||||
*/
|
||||
public function escapeText(string $value): string
|
||||
{
|
||||
return "'" . $this->connection->escape_string($value) . "'";
|
||||
}
|
||||
|
||||
|
||||
public function escapeBinary(string $value): string
|
||||
{
|
||||
return "_binary'" . $this->connection->escape_string($value) . "'";
|
||||
}
|
||||
|
||||
|
||||
public function escapeIdentifier(string $value): string
|
||||
{
|
||||
return '`' . str_replace('`', '``', $value) . '`';
|
||||
}
|
||||
|
||||
|
||||
public function escapeBool(bool $value): string
|
||||
{
|
||||
return $value ? '1' : '0';
|
||||
}
|
||||
|
||||
|
||||
public function escapeDate(\DateTimeInterface $value): string
|
||||
{
|
||||
return $value->format("'Y-m-d'");
|
||||
}
|
||||
|
||||
|
||||
public function escapeDateTime(\DateTimeInterface $value): string
|
||||
{
|
||||
return $value->format("'Y-m-d H:i:s.u'");
|
||||
}
|
||||
|
||||
|
||||
public function escapeDateInterval(\DateInterval $value): string
|
||||
{
|
||||
if ($value->y || $value->m || $value->d) {
|
||||
throw new Dibi\NotSupportedException('Only time interval is supported.');
|
||||
}
|
||||
|
||||
return $value->format("'%r%H:%I:%S.%f'");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Encodes string for use in a LIKE statement.
|
||||
*/
|
||||
public function escapeLike(string $value, int $pos): string
|
||||
{
|
||||
$value = addcslashes(str_replace('\\', '\\\\', $value), "\x00\n\r\\'%_");
|
||||
return ($pos & 1 ? "'%" : "'") . $value . ($pos & 2 ? "%'" : "'");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Injects LIMIT/OFFSET to the SQL query.
|
||||
*/
|
||||
public function applyLimit(string &$sql, ?int $limit, ?int $offset): void
|
||||
{
|
||||
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
|
||||
$sql .= ' LIMIT ' . ($limit ?? '18446744073709551615')
|
||||
. ($offset ? ' OFFSET ' . $offset : '');
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,151 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Dibi\Drivers;
|
||||
|
||||
use Dibi;
|
||||
|
||||
|
||||
/**
|
||||
* The driver for MySQL result set.
|
||||
*/
|
||||
class MySqliResult implements Dibi\ResultDriver
|
||||
{
|
||||
use Dibi\Strict;
|
||||
|
||||
/** @var \mysqli_result */
|
||||
private $resultSet;
|
||||
|
||||
/** @var bool */
|
||||
private $autoFree = true;
|
||||
|
||||
/** @var bool Is buffered (seekable and countable)? */
|
||||
private $buffered;
|
||||
|
||||
|
||||
public function __construct(\mysqli_result $resultSet, bool $buffered)
|
||||
{
|
||||
$this->resultSet = $resultSet;
|
||||
$this->buffered = $buffered;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Automatically frees the resources allocated for this result set.
|
||||
*/
|
||||
public function __destruct()
|
||||
{
|
||||
if ($this->autoFree && $this->getResultResource()) {
|
||||
@$this->free();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the number of rows in a result set.
|
||||
*/
|
||||
public function getRowCount(): int
|
||||
{
|
||||
if (!$this->buffered) {
|
||||
throw new Dibi\NotSupportedException('Row count is not available for unbuffered queries.');
|
||||
}
|
||||
|
||||
return $this->resultSet->num_rows;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Fetches the row at current position and moves the internal cursor to the next position.
|
||||
* @param bool $assoc true for associative array, false for numeric
|
||||
*/
|
||||
public function fetch(bool $assoc): ?array
|
||||
{
|
||||
return $assoc
|
||||
? $this->resultSet->fetch_assoc()
|
||||
: $this->resultSet->fetch_row();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Moves cursor position without fetching row.
|
||||
* @throws Dibi\Exception
|
||||
*/
|
||||
public function seek(int $row): bool
|
||||
{
|
||||
if (!$this->buffered) {
|
||||
throw new Dibi\NotSupportedException('Cannot seek an unbuffered result set.');
|
||||
}
|
||||
|
||||
return $this->resultSet->data_seek($row);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Frees the resources allocated for this result set.
|
||||
*/
|
||||
public function free(): void
|
||||
{
|
||||
$this->resultSet->free();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns metadata for all columns in a result set.
|
||||
*/
|
||||
public function getResultColumns(): array
|
||||
{
|
||||
static $types;
|
||||
if ($types === null) {
|
||||
$consts = get_defined_constants(true);
|
||||
$types = [];
|
||||
foreach ($consts['mysqli'] ?? [] as $key => $value) {
|
||||
if (strncmp($key, 'MYSQLI_TYPE_', 12) === 0) {
|
||||
$types[$value] = substr($key, 12);
|
||||
}
|
||||
}
|
||||
|
||||
$types[MYSQLI_TYPE_TINY] = $types[MYSQLI_TYPE_SHORT] = $types[MYSQLI_TYPE_LONG] = 'INT';
|
||||
}
|
||||
|
||||
$count = $this->resultSet->field_count;
|
||||
$columns = [];
|
||||
for ($i = 0; $i < $count; $i++) {
|
||||
$row = (array) $this->resultSet->fetch_field_direct($i);
|
||||
$columns[] = [
|
||||
'name' => $row['name'],
|
||||
'table' => $row['orgtable'],
|
||||
'fullname' => $row['table'] ? $row['table'] . '.' . $row['name'] : $row['name'],
|
||||
'nativetype' => $types[$row['type']] ?? $row['type'],
|
||||
'type' => $row['type'] === MYSQLI_TYPE_TIME ? Dibi\Type::TIME_INTERVAL : null,
|
||||
'vendor' => $row,
|
||||
];
|
||||
}
|
||||
|
||||
return $columns;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the result set resource.
|
||||
*/
|
||||
public function getResultResource(): \mysqli_result
|
||||
{
|
||||
$this->autoFree = false;
|
||||
return $this->resultSet;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Decodes data from result set.
|
||||
*/
|
||||
public function unescapeBinary(string $value): string
|
||||
{
|
||||
return $value;
|
||||
}
|
||||
}
|
@@ -1,74 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Dibi\Drivers;
|
||||
|
||||
use Dibi;
|
||||
|
||||
|
||||
/**
|
||||
* The driver for no result set.
|
||||
*/
|
||||
class NoDataResult implements Dibi\ResultDriver
|
||||
{
|
||||
use Dibi\Strict;
|
||||
|
||||
/** @var int */
|
||||
private $rows;
|
||||
|
||||
|
||||
public function __construct(int $rows)
|
||||
{
|
||||
$this->rows = $rows;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the number of affected rows.
|
||||
*/
|
||||
public function getRowCount(): int
|
||||
{
|
||||
return $this->rows;
|
||||
}
|
||||
|
||||
|
||||
public function fetch(bool $assoc): ?array
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public function seek(int $row): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public function free(): void
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
public function getResultColumns(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
|
||||
public function getResultResource()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public function unescapeBinary(string $value): string
|
||||
{
|
||||
return $value;
|
||||
}
|
||||
}
|
@@ -1,273 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Dibi\Drivers;
|
||||
|
||||
use Dibi;
|
||||
|
||||
|
||||
/**
|
||||
* The driver interacting with databases via ODBC connections.
|
||||
*
|
||||
* Driver options:
|
||||
* - dsn => driver specific DSN
|
||||
* - username (or user)
|
||||
* - password (or pass)
|
||||
* - persistent (bool) => try to find a persistent link?
|
||||
* - resource (resource) => existing connection resource
|
||||
* - microseconds (bool) => use microseconds in datetime format?
|
||||
*/
|
||||
class OdbcDriver implements Dibi\Driver
|
||||
{
|
||||
use Dibi\Strict;
|
||||
|
||||
/** @var resource */
|
||||
private $connection;
|
||||
|
||||
/** @var int|null Affected rows */
|
||||
private $affectedRows;
|
||||
|
||||
/** @var bool */
|
||||
private $microseconds = true;
|
||||
|
||||
|
||||
/** @throws Dibi\NotSupportedException */
|
||||
public function __construct(array $config)
|
||||
{
|
||||
if (!extension_loaded('odbc')) {
|
||||
throw new Dibi\NotSupportedException("PHP extension 'odbc' is not loaded.");
|
||||
}
|
||||
|
||||
if (isset($config['resource'])) {
|
||||
$this->connection = $config['resource'];
|
||||
} else {
|
||||
// default values
|
||||
$config += [
|
||||
'username' => ini_get('odbc.default_user'),
|
||||
'password' => ini_get('odbc.default_pw'),
|
||||
'dsn' => ini_get('odbc.default_db'),
|
||||
];
|
||||
|
||||
$this->connection = empty($config['persistent'])
|
||||
? @odbc_connect($config['dsn'], $config['username'] ?? '', $config['password'] ?? '') // intentionally @
|
||||
: @odbc_pconnect($config['dsn'], $config['username'] ?? '', $config['password'] ?? ''); // intentionally @
|
||||
}
|
||||
|
||||
if (!is_resource($this->connection)) {
|
||||
throw new Dibi\DriverException(odbc_errormsg() . ' ' . odbc_error());
|
||||
}
|
||||
|
||||
if (isset($config['microseconds'])) {
|
||||
$this->microseconds = (bool) $config['microseconds'];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Disconnects from a database.
|
||||
*/
|
||||
public function disconnect(): void
|
||||
{
|
||||
@odbc_close($this->connection); // @ - connection can be already disconnected
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Executes the SQL query.
|
||||
* @throws Dibi\DriverException
|
||||
*/
|
||||
public function query(string $sql): ?Dibi\ResultDriver
|
||||
{
|
||||
$this->affectedRows = null;
|
||||
$res = @odbc_exec($this->connection, $sql); // intentionally @
|
||||
|
||||
if ($res === false) {
|
||||
throw new Dibi\DriverException(odbc_errormsg($this->connection) . ' ' . odbc_error($this->connection), 0, $sql);
|
||||
|
||||
} elseif (is_resource($res)) {
|
||||
$this->affectedRows = Dibi\Helpers::false2Null(odbc_num_rows($res));
|
||||
return odbc_num_fields($res)
|
||||
? $this->createResultDriver($res)
|
||||
: null;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets the number of affected rows by the last INSERT, UPDATE or DELETE query.
|
||||
*/
|
||||
public function getAffectedRows(): ?int
|
||||
{
|
||||
return $this->affectedRows;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query.
|
||||
*/
|
||||
public function getInsertId(?string $sequence): ?int
|
||||
{
|
||||
throw new Dibi\NotSupportedException('ODBC does not support autoincrementing.');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Begins a transaction (if supported).
|
||||
* @throws Dibi\DriverException
|
||||
*/
|
||||
public function begin(?string $savepoint = null): void
|
||||
{
|
||||
if (!odbc_autocommit($this->connection, PHP_VERSION_ID < 80000 ? 0 : false)) {
|
||||
throw new Dibi\DriverException(odbc_errormsg($this->connection) . ' ' . odbc_error($this->connection));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Commits statements in a transaction.
|
||||
* @throws Dibi\DriverException
|
||||
*/
|
||||
public function commit(?string $savepoint = null): void
|
||||
{
|
||||
if (!odbc_commit($this->connection)) {
|
||||
throw new Dibi\DriverException(odbc_errormsg($this->connection) . ' ' . odbc_error($this->connection));
|
||||
}
|
||||
|
||||
odbc_autocommit($this->connection, PHP_VERSION_ID < 80000 ? 1 : true);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Rollback changes in a transaction.
|
||||
* @throws Dibi\DriverException
|
||||
*/
|
||||
public function rollback(?string $savepoint = null): void
|
||||
{
|
||||
if (!odbc_rollback($this->connection)) {
|
||||
throw new Dibi\DriverException(odbc_errormsg($this->connection) . ' ' . odbc_error($this->connection));
|
||||
}
|
||||
|
||||
odbc_autocommit($this->connection, PHP_VERSION_ID < 80000 ? 1 : true);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Is in transaction?
|
||||
*/
|
||||
public function inTransaction(): bool
|
||||
{
|
||||
return !odbc_autocommit($this->connection);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the connection resource.
|
||||
* @return resource|null
|
||||
*/
|
||||
public function getResource()
|
||||
{
|
||||
return is_resource($this->connection) ? $this->connection : null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the connection reflector.
|
||||
*/
|
||||
public function getReflector(): Dibi\Reflector
|
||||
{
|
||||
return new OdbcReflector($this);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Result set driver factory.
|
||||
* @param resource $resource
|
||||
*/
|
||||
public function createResultDriver($resource): OdbcResult
|
||||
{
|
||||
return new OdbcResult($resource);
|
||||
}
|
||||
|
||||
|
||||
/********************* SQL ****************d*g**/
|
||||
|
||||
|
||||
/**
|
||||
* Encodes data for use in a SQL statement.
|
||||
*/
|
||||
public function escapeText(string $value): string
|
||||
{
|
||||
return "'" . str_replace("'", "''", $value) . "'";
|
||||
}
|
||||
|
||||
|
||||
public function escapeBinary(string $value): string
|
||||
{
|
||||
return "'" . str_replace("'", "''", $value) . "'";
|
||||
}
|
||||
|
||||
|
||||
public function escapeIdentifier(string $value): string
|
||||
{
|
||||
return '[' . str_replace(['[', ']'], ['[[', ']]'], $value) . ']';
|
||||
}
|
||||
|
||||
|
||||
public function escapeBool(bool $value): string
|
||||
{
|
||||
return $value ? '1' : '0';
|
||||
}
|
||||
|
||||
|
||||
public function escapeDate(\DateTimeInterface $value): string
|
||||
{
|
||||
return $value->format('#m/d/Y#');
|
||||
}
|
||||
|
||||
|
||||
public function escapeDateTime(\DateTimeInterface $value): string
|
||||
{
|
||||
return $value->format($this->microseconds ? '#m/d/Y H:i:s.u#' : '#m/d/Y H:i:s#');
|
||||
}
|
||||
|
||||
|
||||
public function escapeDateInterval(\DateInterval $value): string
|
||||
{
|
||||
throw new Dibi\NotImplementedException;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Encodes string for use in a LIKE statement.
|
||||
*/
|
||||
public function escapeLike(string $value, int $pos): string
|
||||
{
|
||||
$value = strtr($value, ["'" => "''", '%' => '[%]', '_' => '[_]', '[' => '[[]']);
|
||||
return ($pos & 1 ? "'%" : "'") . $value . ($pos & 2 ? "%'" : "'");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Injects LIMIT/OFFSET to the SQL query.
|
||||
*/
|
||||
public function applyLimit(string &$sql, ?int $limit, ?int $offset): void
|
||||
{
|
||||
if ($offset) {
|
||||
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 ' . $limit . ' * FROM (' . $sql . ') t';
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,94 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Dibi\Drivers;
|
||||
|
||||
use Dibi;
|
||||
|
||||
|
||||
/**
|
||||
* The reflector for ODBC connections.
|
||||
*/
|
||||
class OdbcReflector implements Dibi\Reflector
|
||||
{
|
||||
use Dibi\Strict;
|
||||
|
||||
/** @var Dibi\Driver */
|
||||
private $driver;
|
||||
|
||||
|
||||
public function __construct(Dibi\Driver $driver)
|
||||
{
|
||||
$this->driver = $driver;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns list of tables.
|
||||
*/
|
||||
public function getTables(): array
|
||||
{
|
||||
$res = odbc_tables($this->driver->getResource());
|
||||
$tables = [];
|
||||
while ($row = odbc_fetch_array($res)) {
|
||||
if ($row['TABLE_TYPE'] === 'TABLE' || $row['TABLE_TYPE'] === 'VIEW') {
|
||||
$tables[] = [
|
||||
'name' => $row['TABLE_NAME'],
|
||||
'view' => $row['TABLE_TYPE'] === 'VIEW',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
odbc_free_result($res);
|
||||
return $tables;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns metadata for all columns in a table.
|
||||
*/
|
||||
public function getColumns(string $table): array
|
||||
{
|
||||
$res = odbc_columns($this->driver->getResource());
|
||||
$columns = [];
|
||||
while ($row = odbc_fetch_array($res)) {
|
||||
if ($row['TABLE_NAME'] === $table) {
|
||||
$columns[] = [
|
||||
'name' => $row['COLUMN_NAME'],
|
||||
'table' => $table,
|
||||
'nativetype' => $row['TYPE_NAME'],
|
||||
'size' => $row['COLUMN_SIZE'],
|
||||
'nullable' => (bool) $row['NULLABLE'],
|
||||
'default' => $row['COLUMN_DEF'],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
odbc_free_result($res);
|
||||
return $columns;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns metadata for all indexes in a table.
|
||||
*/
|
||||
public function getIndexes(string $table): array
|
||||
{
|
||||
throw new Dibi\NotImplementedException;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns metadata for all foreign keys in a table.
|
||||
*/
|
||||
public function getForeignKeys(string $table): array
|
||||
{
|
||||
throw new Dibi\NotImplementedException;
|
||||
}
|
||||
}
|
@@ -1,144 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Dibi\Drivers;
|
||||
|
||||
use Dibi;
|
||||
|
||||
|
||||
/**
|
||||
* The driver interacting with result set via ODBC connections.
|
||||
*/
|
||||
class OdbcResult implements Dibi\ResultDriver
|
||||
{
|
||||
use Dibi\Strict;
|
||||
|
||||
/** @var resource */
|
||||
private $resultSet;
|
||||
|
||||
/** @var bool */
|
||||
private $autoFree = true;
|
||||
|
||||
/** @var int Cursor */
|
||||
private $row = 0;
|
||||
|
||||
|
||||
/**
|
||||
* @param resource $resultSet
|
||||
*/
|
||||
public function __construct($resultSet)
|
||||
{
|
||||
$this->resultSet = $resultSet;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Automatically frees the resources allocated for this result set.
|
||||
*/
|
||||
public function __destruct()
|
||||
{
|
||||
if ($this->autoFree && $this->getResultResource()) {
|
||||
$this->free();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the number of rows in a result set.
|
||||
*/
|
||||
public function getRowCount(): int
|
||||
{
|
||||
// will return -1 with many drivers :-(
|
||||
return odbc_num_rows($this->resultSet);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Fetches the row at current position and moves the internal cursor to the next position.
|
||||
* @param bool $assoc true for associative array, false for numeric
|
||||
*/
|
||||
public function fetch(bool $assoc): ?array
|
||||
{
|
||||
if ($assoc) {
|
||||
return Dibi\Helpers::false2Null(odbc_fetch_array($this->resultSet, ++$this->row));
|
||||
} else {
|
||||
$set = $this->resultSet;
|
||||
if (!odbc_fetch_row($set, ++$this->row)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$count = odbc_num_fields($set);
|
||||
$cols = [];
|
||||
for ($i = 1; $i <= $count; $i++) {
|
||||
$cols[] = odbc_result($set, $i);
|
||||
}
|
||||
|
||||
return $cols;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Moves cursor position without fetching row.
|
||||
*/
|
||||
public function seek(int $row): bool
|
||||
{
|
||||
$this->row = $row;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Frees the resources allocated for this result set.
|
||||
*/
|
||||
public function free(): void
|
||||
{
|
||||
odbc_free_result($this->resultSet);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns metadata for all columns in a result set.
|
||||
*/
|
||||
public function getResultColumns(): array
|
||||
{
|
||||
$count = odbc_num_fields($this->resultSet);
|
||||
$columns = [];
|
||||
for ($i = 1; $i <= $count; $i++) {
|
||||
$columns[] = [
|
||||
'name' => odbc_field_name($this->resultSet, $i),
|
||||
'table' => null,
|
||||
'fullname' => odbc_field_name($this->resultSet, $i),
|
||||
'nativetype' => odbc_field_type($this->resultSet, $i),
|
||||
];
|
||||
}
|
||||
|
||||
return $columns;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the result set resource.
|
||||
* @return resource|null
|
||||
*/
|
||||
public function getResultResource()
|
||||
{
|
||||
$this->autoFree = false;
|
||||
return is_resource($this->resultSet) ? $this->resultSet : null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Decodes data from result set.
|
||||
*/
|
||||
public function unescapeBinary(string $value): string
|
||||
{
|
||||
return $value;
|
||||
}
|
||||
}
|
@@ -1,298 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Dibi\Drivers;
|
||||
|
||||
use Dibi;
|
||||
|
||||
|
||||
/**
|
||||
* The driver for Oracle database.
|
||||
*
|
||||
* Driver options:
|
||||
* - database => the name of the local Oracle instance or the name of the entry in tnsnames.ora
|
||||
* - username (or user)
|
||||
* - password (or pass)
|
||||
* - charset => character encoding to set
|
||||
* - schema => alters session schema
|
||||
* - nativeDate => use native date format (defaults to true)
|
||||
* - resource (resource) => existing connection resource
|
||||
* - persistent => Creates persistent connections with oci_pconnect instead of oci_new_connect
|
||||
*/
|
||||
class OracleDriver implements Dibi\Driver
|
||||
{
|
||||
use Dibi\Strict;
|
||||
|
||||
/** @var resource */
|
||||
private $connection;
|
||||
|
||||
/** @var bool */
|
||||
private $autocommit = true;
|
||||
|
||||
/** @var bool use native datetime format */
|
||||
private $nativeDate;
|
||||
|
||||
/** @var int|null Number of affected rows */
|
||||
private $affectedRows;
|
||||
|
||||
|
||||
/** @throws Dibi\NotSupportedException */
|
||||
public function __construct(array $config)
|
||||
{
|
||||
if (!extension_loaded('oci8')) {
|
||||
throw new Dibi\NotSupportedException("PHP extension 'oci8' is not loaded.");
|
||||
}
|
||||
|
||||
$foo = &$config['charset'];
|
||||
$this->nativeDate = $config['nativeDate'] ?? true;
|
||||
|
||||
if (isset($config['resource'])) {
|
||||
$this->connection = $config['resource'];
|
||||
} elseif (empty($config['persistent'])) {
|
||||
$this->connection = @oci_new_connect($config['username'], $config['password'], $config['database'], $config['charset']); // intentionally @
|
||||
} else {
|
||||
$this->connection = @oci_pconnect($config['username'], $config['password'], $config['database'], $config['charset']); // intentionally @
|
||||
}
|
||||
|
||||
if (!$this->connection) {
|
||||
$err = oci_error();
|
||||
throw new Dibi\DriverException($err['message'], $err['code']);
|
||||
}
|
||||
|
||||
if (isset($config['schema'])) {
|
||||
$this->query('ALTER SESSION SET CURRENT_SCHEMA=' . $config['schema']);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Disconnects from a database.
|
||||
*/
|
||||
public function disconnect(): void
|
||||
{
|
||||
@oci_close($this->connection); // @ - connection can be already disconnected
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Executes the SQL query.
|
||||
* @throws Dibi\DriverException
|
||||
*/
|
||||
public function query(string $sql): ?Dibi\ResultDriver
|
||||
{
|
||||
$this->affectedRows = null;
|
||||
$res = oci_parse($this->connection, $sql);
|
||||
if ($res) {
|
||||
@oci_execute($res, $this->autocommit ? OCI_COMMIT_ON_SUCCESS : OCI_DEFAULT);
|
||||
$err = oci_error($res);
|
||||
if ($err) {
|
||||
throw static::createException($err['message'], $err['code'], $sql);
|
||||
|
||||
} elseif (is_resource($res)) {
|
||||
$this->affectedRows = Dibi\Helpers::false2Null(oci_num_rows($res));
|
||||
return oci_num_fields($res)
|
||||
? $this->createResultDriver($res)
|
||||
: null;
|
||||
}
|
||||
} else {
|
||||
$err = oci_error($this->connection);
|
||||
throw new Dibi\DriverException($err['message'], $err['code'], $sql);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public static function createException(string $message, $code, string $sql): Dibi\DriverException
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets the number of affected rows by the last INSERT, UPDATE or DELETE query.
|
||||
*/
|
||||
public function getAffectedRows(): ?int
|
||||
{
|
||||
return $this->affectedRows;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query.
|
||||
*/
|
||||
public function getInsertId(?string $sequence): ?int
|
||||
{
|
||||
$row = $this->query("SELECT $sequence.CURRVAL AS ID FROM DUAL")->fetch(true);
|
||||
return isset($row['ID']) ? Dibi\Helpers::intVal($row['ID']) : null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Begins a transaction (if supported).
|
||||
*/
|
||||
public function begin(?string $savepoint = null): void
|
||||
{
|
||||
$this->autocommit = false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Commits statements in a transaction.
|
||||
* @throws Dibi\DriverException
|
||||
*/
|
||||
public function commit(?string $savepoint = null): void
|
||||
{
|
||||
if (!oci_commit($this->connection)) {
|
||||
$err = oci_error($this->connection);
|
||||
throw new Dibi\DriverException($err['message'], $err['code']);
|
||||
}
|
||||
|
||||
$this->autocommit = true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Rollback changes in a transaction.
|
||||
* @throws Dibi\DriverException
|
||||
*/
|
||||
public function rollback(?string $savepoint = null): void
|
||||
{
|
||||
if (!oci_rollback($this->connection)) {
|
||||
$err = oci_error($this->connection);
|
||||
throw new Dibi\DriverException($err['message'], $err['code']);
|
||||
}
|
||||
|
||||
$this->autocommit = true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the connection resource.
|
||||
* @return resource|null
|
||||
*/
|
||||
public function getResource()
|
||||
{
|
||||
return is_resource($this->connection) ? $this->connection : null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the connection reflector.
|
||||
*/
|
||||
public function getReflector(): Dibi\Reflector
|
||||
{
|
||||
return new OracleReflector($this);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Result set driver factory.
|
||||
* @param resource $resource
|
||||
*/
|
||||
public function createResultDriver($resource): OracleResult
|
||||
{
|
||||
return new OracleResult($resource);
|
||||
}
|
||||
|
||||
|
||||
/********************* SQL ****************d*g**/
|
||||
|
||||
|
||||
/**
|
||||
* Encodes data for use in a SQL statement.
|
||||
*/
|
||||
public function escapeText(string $value): string
|
||||
{
|
||||
return "'" . str_replace("'", "''", $value) . "'"; // TODO: not tested
|
||||
}
|
||||
|
||||
|
||||
public function escapeBinary(string $value): string
|
||||
{
|
||||
return "'" . str_replace("'", "''", $value) . "'"; // TODO: not tested
|
||||
}
|
||||
|
||||
|
||||
public function escapeIdentifier(string $value): string
|
||||
{
|
||||
// @see http://download.oracle.com/docs/cd/B10500_01/server.920/a96540/sql_elements9a.htm
|
||||
return '"' . str_replace('"', '""', $value) . '"';
|
||||
}
|
||||
|
||||
|
||||
public function escapeBool(bool $value): string
|
||||
{
|
||||
return $value ? '1' : '0';
|
||||
}
|
||||
|
||||
|
||||
public function escapeDate(\DateTimeInterface $value): string
|
||||
{
|
||||
return $this->nativeDate
|
||||
? "to_date('" . $value->format('Y-m-d') . "', 'YYYY-mm-dd')"
|
||||
: $value->format('U');
|
||||
}
|
||||
|
||||
|
||||
public function escapeDateTime(\DateTimeInterface $value): string
|
||||
{
|
||||
return $this->nativeDate
|
||||
? "to_date('" . $value->format('Y-m-d G:i:s') . "', 'YYYY-mm-dd hh24:mi:ss')"
|
||||
: $value->format('U');
|
||||
}
|
||||
|
||||
|
||||
public function escapeDateInterval(\DateInterval $value): string
|
||||
{
|
||||
throw new Dibi\NotImplementedException;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Encodes string for use in a LIKE statement.
|
||||
*/
|
||||
public function escapeLike(string $value, int $pos): string
|
||||
{
|
||||
$value = addcslashes(str_replace('\\', '\\\\', $value), "\x00\\%_");
|
||||
$value = str_replace("'", "''", $value);
|
||||
return ($pos & 1 ? "'%" : "'") . $value . ($pos & 2 ? "%'" : "'");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Injects LIMIT/OFFSET to the SQL query.
|
||||
*/
|
||||
public function applyLimit(string &$sql, ?int $limit, ?int $offset): void
|
||||
{
|
||||
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
|
||||
$sql = 'SELECT * FROM (SELECT t.*, ROWNUM AS "__rnum" FROM (' . $sql . ') t '
|
||||
. ($limit !== null ? 'WHERE ROWNUM <= ' . ($offset + $limit) : '')
|
||||
. ') WHERE "__rnum" > ' . $offset;
|
||||
|
||||
} elseif ($limit !== null) {
|
||||
$sql = 'SELECT * FROM (' . $sql . ') WHERE ROWNUM <= ' . $limit;
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,91 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Dibi\Drivers;
|
||||
|
||||
use Dibi;
|
||||
|
||||
|
||||
/**
|
||||
* The reflector for Oracle database.
|
||||
*/
|
||||
class OracleReflector implements Dibi\Reflector
|
||||
{
|
||||
use Dibi\Strict;
|
||||
|
||||
/** @var Dibi\Driver */
|
||||
private $driver;
|
||||
|
||||
|
||||
public function __construct(Dibi\Driver $driver)
|
||||
{
|
||||
$this->driver = $driver;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns list of tables.
|
||||
*/
|
||||
public function getTables(): array
|
||||
{
|
||||
$res = $this->driver->query('SELECT * FROM cat');
|
||||
$tables = [];
|
||||
while ($row = $res->fetch(false)) {
|
||||
if ($row[1] === 'TABLE' || $row[1] === 'VIEW') {
|
||||
$tables[] = [
|
||||
'name' => $row[0],
|
||||
'view' => $row[1] === 'VIEW',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return $tables;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns metadata for all columns in a table.
|
||||
*/
|
||||
public function getColumns(string $table): array
|
||||
{
|
||||
$res = $this->driver->query('SELECT * FROM "ALL_TAB_COLUMNS" WHERE "TABLE_NAME" = ' . $this->driver->escapeText($table));
|
||||
$columns = [];
|
||||
while ($row = $res->fetch(true)) {
|
||||
$columns[] = [
|
||||
'table' => $row['TABLE_NAME'],
|
||||
'name' => $row['COLUMN_NAME'],
|
||||
'nativetype' => $row['DATA_TYPE'],
|
||||
'size' => $row['DATA_LENGTH'] ?? null,
|
||||
'nullable' => $row['NULLABLE'] === 'Y',
|
||||
'default' => $row['DATA_DEFAULT'],
|
||||
'vendor' => $row,
|
||||
];
|
||||
}
|
||||
|
||||
return $columns;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns metadata for all indexes in a table.
|
||||
*/
|
||||
public function getIndexes(string $table): array
|
||||
{
|
||||
throw new Dibi\NotImplementedException;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns metadata for all foreign keys in a table.
|
||||
*/
|
||||
public function getForeignKeys(string $table): array
|
||||
{
|
||||
throw new Dibi\NotImplementedException;
|
||||
}
|
||||
}
|
@@ -1,126 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Dibi\Drivers;
|
||||
|
||||
use Dibi;
|
||||
|
||||
|
||||
/**
|
||||
* The driver for Oracle result set.
|
||||
*/
|
||||
class OracleResult implements Dibi\ResultDriver
|
||||
{
|
||||
use Dibi\Strict;
|
||||
|
||||
/** @var resource */
|
||||
private $resultSet;
|
||||
|
||||
/** @var bool */
|
||||
private $autoFree = true;
|
||||
|
||||
|
||||
/**
|
||||
* @param resource $resultSet
|
||||
*/
|
||||
public function __construct($resultSet)
|
||||
{
|
||||
$this->resultSet = $resultSet;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Automatically frees the resources allocated for this result set.
|
||||
*/
|
||||
public function __destruct()
|
||||
{
|
||||
if ($this->autoFree && $this->getResultResource()) {
|
||||
$this->free();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the number of rows in a result set.
|
||||
*/
|
||||
public function getRowCount(): int
|
||||
{
|
||||
throw new Dibi\NotSupportedException('Row count is not available for unbuffered queries.');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Fetches the row at current position and moves the internal cursor to the next position.
|
||||
* @param bool $assoc true for associative array, false for numeric
|
||||
*/
|
||||
public function fetch(bool $assoc): ?array
|
||||
{
|
||||
return Dibi\Helpers::false2Null(oci_fetch_array($this->resultSet, ($assoc ? OCI_ASSOC : OCI_NUM) | OCI_RETURN_NULLS));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Moves cursor position without fetching row.
|
||||
*/
|
||||
public function seek(int $row): bool
|
||||
{
|
||||
throw new Dibi\NotImplementedException;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Frees the resources allocated for this result set.
|
||||
*/
|
||||
public function free(): void
|
||||
{
|
||||
oci_free_statement($this->resultSet);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns metadata for all columns in a result set.
|
||||
*/
|
||||
public function getResultColumns(): array
|
||||
{
|
||||
$count = oci_num_fields($this->resultSet);
|
||||
$columns = [];
|
||||
for ($i = 1; $i <= $count; $i++) {
|
||||
$type = oci_field_type($this->resultSet, $i);
|
||||
$columns[] = [
|
||||
'name' => oci_field_name($this->resultSet, $i),
|
||||
'table' => null,
|
||||
'fullname' => oci_field_name($this->resultSet, $i),
|
||||
'type' => $type === 'LONG' ? Dibi\Type::TEXT : null,
|
||||
'nativetype' => $type === 'NUMBER' && oci_field_scale($this->resultSet, $i) === 0 ? 'INTEGER' : $type,
|
||||
];
|
||||
}
|
||||
|
||||
return $columns;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the result set resource.
|
||||
* @return resource|null
|
||||
*/
|
||||
public function getResultResource()
|
||||
{
|
||||
$this->autoFree = false;
|
||||
return is_resource($this->resultSet) ? $this->resultSet : null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Decodes data from result set.
|
||||
*/
|
||||
public function unescapeBinary(string $value): string
|
||||
{
|
||||
return $value;
|
||||
}
|
||||
}
|
@@ -1,431 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Dibi\Drivers;
|
||||
|
||||
use Dibi;
|
||||
use Dibi\Helpers;
|
||||
use PDO;
|
||||
|
||||
|
||||
/**
|
||||
* The driver for PDO.
|
||||
*
|
||||
* Driver options:
|
||||
* - dsn => driver specific DSN
|
||||
* - username (or user)
|
||||
* - password (or pass)
|
||||
* - options (array) => driver specific options {@see PDO::__construct}
|
||||
* - resource (PDO) => existing connection
|
||||
* - version
|
||||
*/
|
||||
class PdoDriver implements Dibi\Driver
|
||||
{
|
||||
use Dibi\Strict;
|
||||
|
||||
/** @var PDO|null Connection resource */
|
||||
private $connection;
|
||||
|
||||
/** @var int|null Affected rows */
|
||||
private $affectedRows;
|
||||
|
||||
/** @var string */
|
||||
private $driverName;
|
||||
|
||||
/** @var string */
|
||||
private $serverVersion = '';
|
||||
|
||||
|
||||
/** @throws Dibi\NotSupportedException */
|
||||
public function __construct(array $config)
|
||||
{
|
||||
if (!extension_loaded('pdo')) {
|
||||
throw new Dibi\NotSupportedException("PHP extension 'pdo' is not loaded.");
|
||||
}
|
||||
|
||||
$foo = &$config['dsn'];
|
||||
$foo = &$config['options'];
|
||||
Helpers::alias($config, 'resource', 'pdo');
|
||||
|
||||
if ($config['resource'] instanceof PDO) {
|
||||
$this->connection = $config['resource'];
|
||||
unset($config['resource'], $config['pdo']);
|
||||
|
||||
if ($this->connection->getAttribute(PDO::ATTR_ERRMODE) !== PDO::ERRMODE_SILENT) {
|
||||
throw new Dibi\DriverException('PDO connection in exception or warning error mode is not supported.');
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
$this->connection = new PDO($config['dsn'], $config['username'], $config['password'], $config['options']);
|
||||
$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
|
||||
} catch (\PDOException $e) {
|
||||
if ($e->getMessage() === 'could not find driver') {
|
||||
throw new Dibi\NotSupportedException('PHP extension for PDO is not loaded.');
|
||||
}
|
||||
|
||||
throw new Dibi\DriverException($e->getMessage(), $e->getCode());
|
||||
}
|
||||
}
|
||||
|
||||
$this->driverName = $this->connection->getAttribute(PDO::ATTR_DRIVER_NAME);
|
||||
$this->serverVersion = (string) ($config['version'] ?? @$this->connection->getAttribute(PDO::ATTR_SERVER_VERSION)); // @ - may be not supported
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Disconnects from a database.
|
||||
*/
|
||||
public function disconnect(): void
|
||||
{
|
||||
$this->connection = null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Executes the SQL query.
|
||||
* @throws Dibi\DriverException
|
||||
*/
|
||||
public function query(string $sql): ?Dibi\ResultDriver
|
||||
{
|
||||
$res = $this->connection->query($sql);
|
||||
if ($res) {
|
||||
$this->affectedRows = $res->rowCount();
|
||||
return $res->columnCount() ? $this->createResultDriver($res) : null;
|
||||
}
|
||||
|
||||
$this->affectedRows = null;
|
||||
|
||||
[$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 SqliteDriver::createException($message, $code, $sql);
|
||||
|
||||
default:
|
||||
throw new Dibi\DriverException($message, $code, $sql);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets the number of affected rows by the last INSERT, UPDATE or DELETE query.
|
||||
*/
|
||||
public function getAffectedRows(): ?int
|
||||
{
|
||||
return $this->affectedRows;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query.
|
||||
*/
|
||||
public function getInsertId(?string $sequence): ?int
|
||||
{
|
||||
return Helpers::intVal($this->connection->lastInsertId($sequence));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Begins a transaction (if supported).
|
||||
* @throws Dibi\DriverException
|
||||
*/
|
||||
public function begin(?string $savepoint = null): void
|
||||
{
|
||||
if (!$this->connection->beginTransaction()) {
|
||||
$err = $this->connection->errorInfo();
|
||||
throw new Dibi\DriverException("SQLSTATE[$err[0]]: $err[2]", $err[1]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Commits statements in a transaction.
|
||||
* @throws Dibi\DriverException
|
||||
*/
|
||||
public function commit(?string $savepoint = null): void
|
||||
{
|
||||
if (!$this->connection->commit()) {
|
||||
$err = $this->connection->errorInfo();
|
||||
throw new Dibi\DriverException("SQLSTATE[$err[0]]: $err[2]", $err[1]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Rollback changes in a transaction.
|
||||
* @throws Dibi\DriverException
|
||||
*/
|
||||
public function rollback(?string $savepoint = null): void
|
||||
{
|
||||
if (!$this->connection->rollBack()) {
|
||||
$err = $this->connection->errorInfo();
|
||||
throw new Dibi\DriverException("SQLSTATE[$err[0]]: $err[2]", $err[1]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the connection resource.
|
||||
*/
|
||||
public function getResource(): ?PDO
|
||||
{
|
||||
return $this->connection;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the connection reflector.
|
||||
*/
|
||||
public function getReflector(): Dibi\Reflector
|
||||
{
|
||||
switch ($this->driverName) {
|
||||
case 'mysql':
|
||||
return new MySqlReflector($this);
|
||||
|
||||
case 'oci':
|
||||
return new OracleReflector($this);
|
||||
|
||||
case 'pgsql':
|
||||
return new PostgreReflector($this, $this->connection->getAttribute(PDO::ATTR_SERVER_VERSION));
|
||||
|
||||
case 'sqlite':
|
||||
return new SqliteReflector($this);
|
||||
|
||||
case 'mssql':
|
||||
case 'dblib':
|
||||
case 'sqlsrv':
|
||||
return new SqlsrvReflector($this);
|
||||
|
||||
default:
|
||||
throw new Dibi\NotSupportedException;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Result set driver factory.
|
||||
*/
|
||||
public function createResultDriver(\PDOStatement $result): PdoResult
|
||||
{
|
||||
return new PdoResult($result, $this->driverName);
|
||||
}
|
||||
|
||||
|
||||
/********************* SQL ****************d*g**/
|
||||
|
||||
|
||||
/**
|
||||
* Encodes data for use in a SQL statement.
|
||||
*/
|
||||
public function escapeText(string $value): string
|
||||
{
|
||||
return $this->driverName === 'odbc'
|
||||
? "'" . str_replace("'", "''", $value) . "'"
|
||||
: $this->connection->quote($value, PDO::PARAM_STR);
|
||||
}
|
||||
|
||||
|
||||
public function escapeBinary(string $value): string
|
||||
{
|
||||
return $this->driverName === 'odbc'
|
||||
? "'" . str_replace("'", "''", $value) . "'"
|
||||
: $this->connection->quote($value, PDO::PARAM_LOB);
|
||||
}
|
||||
|
||||
|
||||
public function escapeIdentifier(string $value): string
|
||||
{
|
||||
switch ($this->driverName) {
|
||||
case 'mysql':
|
||||
return '`' . str_replace('`', '``', $value) . '`';
|
||||
|
||||
case 'oci':
|
||||
case 'pgsql':
|
||||
return '"' . str_replace('"', '""', $value) . '"';
|
||||
|
||||
case 'sqlite':
|
||||
return '[' . strtr($value, '[]', ' ') . ']';
|
||||
|
||||
case 'odbc':
|
||||
case 'mssql':
|
||||
return '[' . str_replace(['[', ']'], ['[[', ']]'], $value) . ']';
|
||||
|
||||
case 'dblib':
|
||||
case 'sqlsrv':
|
||||
return '[' . str_replace(']', ']]', $value) . ']';
|
||||
|
||||
default:
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function escapeBool(bool $value): string
|
||||
{
|
||||
if ($this->driverName === 'pgsql') {
|
||||
return $value ? 'TRUE' : 'FALSE';
|
||||
} else {
|
||||
return $value ? '1' : '0';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function escapeDate(\DateTimeInterface $value): string
|
||||
{
|
||||
return $value->format($this->driverName === 'odbc' ? '#m/d/Y#' : "'Y-m-d'");
|
||||
}
|
||||
|
||||
|
||||
public function escapeDateTime(\DateTimeInterface $value): string
|
||||
{
|
||||
switch ($this->driverName) {
|
||||
case 'odbc':
|
||||
return $value->format('#m/d/Y H:i:s.u#');
|
||||
case 'mssql':
|
||||
case 'dblib':
|
||||
case 'sqlsrv':
|
||||
return 'CONVERT(DATETIME2(7), ' . $value->format("'Y-m-d H:i:s.u'") . ')';
|
||||
default:
|
||||
return $value->format("'Y-m-d H:i:s.u'");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function escapeDateInterval(\DateInterval $value): string
|
||||
{
|
||||
throw new Dibi\NotImplementedException;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Encodes string for use in a LIKE statement.
|
||||
*/
|
||||
public function escapeLike(string $value, int $pos): string
|
||||
{
|
||||
switch ($this->driverName) {
|
||||
case 'mysql':
|
||||
$value = addcslashes(str_replace('\\', '\\\\', $value), "\x00\n\r\\'%_");
|
||||
return ($pos & 1 ? "'%" : "'") . $value . ($pos & 2 ? "%'" : "'");
|
||||
|
||||
case 'oci':
|
||||
$value = addcslashes(str_replace('\\', '\\\\', $value), "\x00\\%_");
|
||||
$value = str_replace("'", "''", $value);
|
||||
return ($pos & 1 ? "'%" : "'") . $value . ($pos & 2 ? "%'" : "'");
|
||||
|
||||
case 'pgsql':
|
||||
$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 = strtr($value, ['%' => $bs . '%', '_' => $bs . '_', '\\' => '\\\\']);
|
||||
return ($pos & 1 ? "'%" : "'") . $value . ($pos & 2 ? "%'" : "'");
|
||||
|
||||
case 'sqlite':
|
||||
$value = addcslashes(substr($this->connection->quote($value, PDO::PARAM_STR), 1, -1), '%_\\');
|
||||
return ($pos & 1 ? "'%" : "'") . $value . ($pos & 2 ? "%'" : "'") . " ESCAPE '\\'";
|
||||
|
||||
case 'odbc':
|
||||
case 'mssql':
|
||||
case 'dblib':
|
||||
case 'sqlsrv':
|
||||
$value = strtr($value, ["'" => "''", '%' => '[%]', '_' => '[_]', '[' => '[[]']);
|
||||
return ($pos & 1 ? "'%" : "'") . $value . ($pos & 2 ? "%'" : "'");
|
||||
|
||||
default:
|
||||
throw new Dibi\NotImplementedException;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Injects LIMIT/OFFSET to the SQL query.
|
||||
*/
|
||||
public function applyLimit(string &$sql, ?int $limit, ?int $offset): void
|
||||
{
|
||||
if ($limit < 0 || $offset < 0) {
|
||||
throw new Dibi\NotSupportedException('Negative offset or limit.');
|
||||
}
|
||||
|
||||
switch ($this->driverName) {
|
||||
case 'mysql':
|
||||
if ($limit !== null || $offset) {
|
||||
// see http://dev.mysql.com/doc/refman/5.0/en/select.html
|
||||
$sql .= ' LIMIT ' . ($limit ?? '18446744073709551615')
|
||||
. ($offset ? ' OFFSET ' . $offset : '');
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case 'pgsql':
|
||||
if ($limit !== null) {
|
||||
$sql .= ' LIMIT ' . $limit;
|
||||
}
|
||||
|
||||
if ($offset) {
|
||||
$sql .= ' OFFSET ' . $offset;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case 'sqlite':
|
||||
if ($limit !== null || $offset) {
|
||||
$sql .= ' LIMIT ' . ($limit ?? '-1')
|
||||
. ($offset ? ' OFFSET ' . $offset : '');
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case 'oci':
|
||||
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 '
|
||||
. ($limit !== null ? 'WHERE ROWNUM <= ' . ($offset + $limit) : '')
|
||||
. ') WHERE "__rnum" > ' . $offset;
|
||||
|
||||
} elseif ($limit !== null) {
|
||||
$sql = 'SELECT * FROM (' . $sql . ') WHERE ROWNUM <= ' . $limit;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case 'mssql':
|
||||
case 'sqlsrv':
|
||||
case 'dblib':
|
||||
if (version_compare($this->serverVersion, '11.0') >= 0) { // 11 == SQL Server 2012
|
||||
// requires ORDER BY, see https://technet.microsoft.com/en-us/library/gg699618(v=sql.110).aspx
|
||||
if ($limit !== null) {
|
||||
$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 omitted
|
||||
case 'odbc':
|
||||
if ($offset) {
|
||||
throw new Dibi\NotSupportedException('Offset is not supported by this database.');
|
||||
|
||||
} elseif ($limit !== null) {
|
||||
$sql = 'SELECT TOP ' . $limit . ' * FROM (' . $sql . ') t';
|
||||
break;
|
||||
}
|
||||
// break omitted
|
||||
default:
|
||||
throw new Dibi\NotSupportedException('PDO or driver does not support applying limit or offset.');
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,124 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Dibi\Drivers;
|
||||
|
||||
use Dibi;
|
||||
use Dibi\Helpers;
|
||||
use PDO;
|
||||
|
||||
|
||||
/**
|
||||
* The driver for PDO result set.
|
||||
*/
|
||||
class PdoResult implements Dibi\ResultDriver
|
||||
{
|
||||
use Dibi\Strict;
|
||||
|
||||
/** @var \PDOStatement|null */
|
||||
private $resultSet;
|
||||
|
||||
/** @var string */
|
||||
private $driverName;
|
||||
|
||||
|
||||
public function __construct(\PDOStatement $resultSet, string $driverName)
|
||||
{
|
||||
$this->resultSet = $resultSet;
|
||||
$this->driverName = $driverName;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the number of rows in a result set.
|
||||
*/
|
||||
public function getRowCount(): int
|
||||
{
|
||||
return $this->resultSet->rowCount();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Fetches the row at current position and moves the internal cursor to the next position.
|
||||
* @param bool $assoc true for associative array, false for numeric
|
||||
*/
|
||||
public function fetch(bool $assoc): ?array
|
||||
{
|
||||
return Helpers::false2Null($this->resultSet->fetch($assoc ? PDO::FETCH_ASSOC : PDO::FETCH_NUM));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Moves cursor position without fetching row.
|
||||
*/
|
||||
public function seek(int $row): bool
|
||||
{
|
||||
throw new Dibi\NotSupportedException('Cannot seek an unbuffered result set.');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Frees the resources allocated for this result set.
|
||||
*/
|
||||
public function free(): void
|
||||
{
|
||||
$this->resultSet = null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns metadata for all columns in a result set.
|
||||
* @throws Dibi\Exception
|
||||
*/
|
||||
public function getResultColumns(): array
|
||||
{
|
||||
$count = $this->resultSet->columnCount();
|
||||
$columns = [];
|
||||
for ($i = 0; $i < $count; $i++) {
|
||||
$row = @$this->resultSet->getColumnMeta($i); // intentionally @
|
||||
if ($row === false) {
|
||||
throw new Dibi\NotSupportedException('Driver does not support meta data.');
|
||||
}
|
||||
|
||||
$row += [
|
||||
'table' => null,
|
||||
'native_type' => 'VAR_STRING',
|
||||
];
|
||||
|
||||
$columns[] = [
|
||||
'name' => $row['name'],
|
||||
'table' => $row['table'],
|
||||
'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'],
|
||||
'vendor' => $row,
|
||||
];
|
||||
}
|
||||
|
||||
return $columns;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the result set resource.
|
||||
*/
|
||||
public function getResultResource(): ?\PDOStatement
|
||||
{
|
||||
return $this->resultSet;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Decodes data from result set.
|
||||
*/
|
||||
public function unescapeBinary(string $value): string
|
||||
{
|
||||
return $value;
|
||||
}
|
||||
}
|
@@ -1,344 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Dibi\Drivers;
|
||||
|
||||
use Dibi;
|
||||
use Dibi\Helpers;
|
||||
use PgSql;
|
||||
|
||||
|
||||
/**
|
||||
* The driver for PostgreSQL database.
|
||||
*
|
||||
* Driver options:
|
||||
* - host, hostaddr, port, dbname, user, password, connect_timeout, options, sslmode, service => see PostgreSQL API
|
||||
* - string => or use connection string
|
||||
* - schema => the schema search path
|
||||
* - charset => character encoding to set (default is utf8)
|
||||
* - persistent (bool) => try to find a persistent link?
|
||||
* - resource (resource) => existing connection resource
|
||||
* - connect_type (int) => see pg_connect()
|
||||
*/
|
||||
class PostgreDriver implements Dibi\Driver
|
||||
{
|
||||
use Dibi\Strict;
|
||||
|
||||
/** @var resource|PgSql\Connection */
|
||||
private $connection;
|
||||
|
||||
/** @var int|null Affected rows */
|
||||
private $affectedRows;
|
||||
|
||||
|
||||
/** @throws Dibi\NotSupportedException */
|
||||
public function __construct(array $config)
|
||||
{
|
||||
if (!extension_loaded('pgsql')) {
|
||||
throw new Dibi\NotSupportedException("PHP extension 'pgsql' is not loaded.");
|
||||
}
|
||||
|
||||
$error = null;
|
||||
if (isset($config['resource'])) {
|
||||
$this->connection = $config['resource'];
|
||||
|
||||
} else {
|
||||
$config += [
|
||||
'charset' => 'utf8',
|
||||
];
|
||||
if (isset($config['string'])) {
|
||||
$string = $config['string'];
|
||||
} else {
|
||||
$string = '';
|
||||
Helpers::alias($config, 'user', 'username');
|
||||
Helpers::alias($config, 'dbname', 'database');
|
||||
foreach (['host', 'hostaddr', 'port', 'dbname', 'user', 'password', 'connect_timeout', 'options', 'sslmode', 'service'] as $key) {
|
||||
if (isset($config[$key])) {
|
||||
$string .= $key . '=' . $config[$key] . ' ';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$connectType = $config['connect_type'] ?? PGSQL_CONNECT_FORCE_NEW;
|
||||
|
||||
set_error_handler(function (int $severity, string $message) use (&$error) {
|
||||
$error = $message;
|
||||
});
|
||||
$this->connection = empty($config['persistent'])
|
||||
? pg_connect($string, $connectType)
|
||||
: pg_pconnect($string, $connectType);
|
||||
restore_error_handler();
|
||||
}
|
||||
|
||||
if (!is_resource($this->connection) && !$this->connection instanceof PgSql\Connection) {
|
||||
throw new Dibi\DriverException($error ?: 'Connecting error.');
|
||||
}
|
||||
|
||||
pg_set_error_verbosity($this->connection, PGSQL_ERRORS_VERBOSE);
|
||||
|
||||
if (isset($config['charset']) && pg_set_client_encoding($this->connection, $config['charset'])) {
|
||||
throw static::createException(pg_last_error($this->connection));
|
||||
}
|
||||
|
||||
if (isset($config['schema'])) {
|
||||
$this->query('SET search_path TO "' . implode('", "', (array) $config['schema']) . '"');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Disconnects from a database.
|
||||
*/
|
||||
public function disconnect(): void
|
||||
{
|
||||
@pg_close($this->connection); // @ - connection can be already disconnected
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Pings database.
|
||||
*/
|
||||
public function ping(): bool
|
||||
{
|
||||
return pg_ping($this->connection);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Executes the SQL query.
|
||||
* @throws Dibi\DriverException
|
||||
*/
|
||||
public function query(string $sql): ?Dibi\ResultDriver
|
||||
{
|
||||
$this->affectedRows = null;
|
||||
$res = @pg_query($this->connection, $sql); // intentionally @
|
||||
|
||||
if ($res === false) {
|
||||
throw static::createException(pg_last_error($this->connection), null, $sql);
|
||||
|
||||
} elseif (is_resource($res) || $res instanceof PgSql\Result) {
|
||||
$this->affectedRows = Helpers::false2Null(pg_affected_rows($res));
|
||||
if (pg_num_fields($res)) {
|
||||
return $this->createResultDriver($res);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public static function createException(string $message, $code = null, ?string $sql = null): Dibi\DriverException
|
||||
{
|
||||
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.
|
||||
*/
|
||||
public function getAffectedRows(): ?int
|
||||
{
|
||||
return $this->affectedRows;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query.
|
||||
*/
|
||||
public function getInsertId(?string $sequence): ?int
|
||||
{
|
||||
$res = $sequence === null
|
||||
? $this->query('SELECT LASTVAL()') // PostgreSQL 8.1 is needed
|
||||
: $this->query("SELECT CURRVAL('$sequence')");
|
||||
|
||||
if (!$res) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$row = $res->fetch(false);
|
||||
return is_array($row) ? (int) $row[0] : null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Begins a transaction (if supported).
|
||||
* @throws Dibi\DriverException
|
||||
*/
|
||||
public function begin(?string $savepoint = null): void
|
||||
{
|
||||
$this->query($savepoint ? "SAVEPOINT {$this->escapeIdentifier($savepoint)}" : 'START TRANSACTION');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Commits statements in a transaction.
|
||||
* @throws Dibi\DriverException
|
||||
*/
|
||||
public function commit(?string $savepoint = null): void
|
||||
{
|
||||
$this->query($savepoint ? "RELEASE SAVEPOINT {$this->escapeIdentifier($savepoint)}" : 'COMMIT');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Rollback changes in a transaction.
|
||||
* @throws Dibi\DriverException
|
||||
*/
|
||||
public function rollback(?string $savepoint = null): void
|
||||
{
|
||||
$this->query($savepoint ? "ROLLBACK TO SAVEPOINT {$this->escapeIdentifier($savepoint)}" : 'ROLLBACK');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Is in transaction?
|
||||
*/
|
||||
public function inTransaction(): bool
|
||||
{
|
||||
return !in_array(pg_transaction_status($this->connection), [PGSQL_TRANSACTION_UNKNOWN, PGSQL_TRANSACTION_IDLE], true);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the connection resource.
|
||||
* @return resource|null
|
||||
*/
|
||||
public function getResource()
|
||||
{
|
||||
return is_resource($this->connection) || $this->connection instanceof PgSql\Connection
|
||||
? $this->connection
|
||||
: null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the connection reflector.
|
||||
*/
|
||||
public function getReflector(): Dibi\Reflector
|
||||
{
|
||||
return new PostgreReflector($this, pg_parameter_status($this->connection, 'server_version'));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Result set driver factory.
|
||||
* @param resource $resource
|
||||
*/
|
||||
public function createResultDriver($resource): PostgreResult
|
||||
{
|
||||
return new PostgreResult($resource);
|
||||
}
|
||||
|
||||
|
||||
/********************* SQL ****************d*g**/
|
||||
|
||||
|
||||
/**
|
||||
* Encodes data for use in a SQL statement.
|
||||
*/
|
||||
public function escapeText(string $value): string
|
||||
{
|
||||
if (!$this->getResource()) {
|
||||
throw new Dibi\Exception('Lost connection to server.');
|
||||
}
|
||||
|
||||
return "'" . pg_escape_string($this->connection, $value) . "'";
|
||||
}
|
||||
|
||||
|
||||
public function escapeBinary(string $value): string
|
||||
{
|
||||
if (!$this->getResource()) {
|
||||
throw new Dibi\Exception('Lost connection to server.');
|
||||
}
|
||||
|
||||
return "'" . pg_escape_bytea($this->connection, $value) . "'";
|
||||
}
|
||||
|
||||
|
||||
public function escapeIdentifier(string $value): string
|
||||
{
|
||||
// @see http://www.postgresql.org/docs/8.2/static/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS
|
||||
return '"' . str_replace('"', '""', $value) . '"';
|
||||
}
|
||||
|
||||
|
||||
public function escapeBool(bool $value): string
|
||||
{
|
||||
return $value ? 'TRUE' : 'FALSE';
|
||||
}
|
||||
|
||||
|
||||
public function escapeDate(\DateTimeInterface $value): string
|
||||
{
|
||||
return $value->format("'Y-m-d'");
|
||||
}
|
||||
|
||||
|
||||
public function escapeDateTime(\DateTimeInterface $value): string
|
||||
{
|
||||
return $value->format("'Y-m-d H:i:s.u'");
|
||||
}
|
||||
|
||||
|
||||
public function escapeDateInterval(\DateInterval $value): string
|
||||
{
|
||||
throw new Dibi\NotImplementedException;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Encodes string for use in a LIKE statement.
|
||||
*/
|
||||
public function escapeLike(string $value, int $pos): string
|
||||
{
|
||||
$bs = pg_escape_string($this->connection, '\\'); // standard_conforming_strings = on/off
|
||||
$value = pg_escape_string($this->connection, $value);
|
||||
$value = strtr($value, ['%' => $bs . '%', '_' => $bs . '_', '\\' => '\\\\']);
|
||||
return ($pos & 1 ? "'%" : "'") . $value . ($pos & 2 ? "%'" : "'");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Injects LIMIT/OFFSET to the SQL query.
|
||||
*/
|
||||
public function applyLimit(string &$sql, ?int $limit, ?int $offset): void
|
||||
{
|
||||
if ($limit < 0 || $offset < 0) {
|
||||
throw new Dibi\NotSupportedException('Negative offset or limit.');
|
||||
}
|
||||
|
||||
if ($limit !== null) {
|
||||
$sql .= ' LIMIT ' . $limit;
|
||||
}
|
||||
|
||||
if ($offset) {
|
||||
$sql .= ' OFFSET ' . $offset;
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,265 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Dibi\Drivers;
|
||||
|
||||
use Dibi;
|
||||
|
||||
|
||||
/**
|
||||
* The reflector for PostgreSQL database.
|
||||
*/
|
||||
class PostgreReflector implements Dibi\Reflector
|
||||
{
|
||||
use Dibi\Strict;
|
||||
|
||||
/** @var Dibi\Driver */
|
||||
private $driver;
|
||||
|
||||
/** @var string */
|
||||
private $version;
|
||||
|
||||
|
||||
public function __construct(Dibi\Driver $driver, string $version)
|
||||
{
|
||||
$this->driver = $driver;
|
||||
$this->version = $version;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns list of tables.
|
||||
*/
|
||||
public function getTables(): array
|
||||
{
|
||||
$query = "
|
||||
SELECT
|
||||
table_name AS name,
|
||||
CASE table_type
|
||||
WHEN 'VIEW' THEN 1
|
||||
ELSE 0
|
||||
END AS view
|
||||
FROM
|
||||
information_schema.tables
|
||||
WHERE
|
||||
table_schema = ANY (current_schemas(false))";
|
||||
|
||||
if ($this->version >= 9.3) {
|
||||
$query .= '
|
||||
UNION ALL
|
||||
SELECT
|
||||
matviewname, 1
|
||||
FROM
|
||||
pg_matviews
|
||||
WHERE
|
||||
schemaname = ANY (current_schemas(false))';
|
||||
}
|
||||
|
||||
$res = $this->driver->query($query);
|
||||
$tables = [];
|
||||
while ($row = $res->fetch(true)) {
|
||||
$tables[] = $row;
|
||||
}
|
||||
|
||||
return $tables;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns metadata for all columns in a table.
|
||||
*/
|
||||
public function getColumns(string $table): array
|
||||
{
|
||||
$_table = $this->driver->escapeText($this->driver->escapeIdentifier($table));
|
||||
$res = $this->driver->query("
|
||||
SELECT indkey
|
||||
FROM pg_class
|
||||
LEFT JOIN pg_index on pg_class.oid = pg_index.indrelid AND pg_index.indisprimary
|
||||
WHERE pg_class.oid = $_table::regclass
|
||||
");
|
||||
$primary = (int) $res->fetch(true)['indkey'];
|
||||
|
||||
$res = $this->driver->query("
|
||||
SELECT *
|
||||
FROM information_schema.columns c
|
||||
JOIN pg_class ON pg_class.relname = c.table_name
|
||||
JOIN pg_namespace nsp ON nsp.oid = pg_class.relnamespace AND nsp.nspname = c.table_schema
|
||||
WHERE pg_class.oid = $_table::regclass
|
||||
ORDER BY c.ordinal_position
|
||||
");
|
||||
|
||||
if (!$res->getRowCount()) {
|
||||
$res = $this->driver->query("
|
||||
SELECT
|
||||
a.attname AS column_name,
|
||||
pg_type.typname AS udt_name,
|
||||
a.attlen AS numeric_precision,
|
||||
a.atttypmod-4 AS character_maximum_length,
|
||||
NOT a.attnotnull AS is_nullable,
|
||||
a.attnum AS ordinal_position,
|
||||
pg_get_expr(adef.adbin, adef.adrelid) AS column_default
|
||||
FROM
|
||||
pg_attribute a
|
||||
JOIN pg_type ON a.atttypid = pg_type.oid
|
||||
JOIN pg_class cls ON a.attrelid = cls.oid
|
||||
LEFT JOIN pg_attrdef adef ON adef.adnum = a.attnum AND adef.adrelid = a.attrelid
|
||||
WHERE
|
||||
cls.relkind IN ('r', 'v', 'mv')
|
||||
AND a.attrelid = $_table::regclass
|
||||
AND a.attnum > 0
|
||||
AND NOT a.attisdropped
|
||||
ORDER BY ordinal_position
|
||||
");
|
||||
}
|
||||
|
||||
$columns = [];
|
||||
while ($row = $res->fetch(true)) {
|
||||
$size = (int) max($row['character_maximum_length'], $row['numeric_precision']);
|
||||
$columns[] = [
|
||||
'name' => $row['column_name'],
|
||||
'table' => $table,
|
||||
'nativetype' => strtoupper($row['udt_name']),
|
||||
'size' => $size > 0 ? $size : null,
|
||||
'nullable' => $row['is_nullable'] === 'YES' || $row['is_nullable'] === 't' || $row['is_nullable'] === true,
|
||||
'default' => $row['column_default'],
|
||||
'autoincrement' => (int) $row['ordinal_position'] === $primary && substr($row['column_default'] ?? '', 0, 7) === 'nextval',
|
||||
'vendor' => $row,
|
||||
];
|
||||
}
|
||||
|
||||
return $columns;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns metadata for all indexes in a table.
|
||||
*/
|
||||
public function getIndexes(string $table): array
|
||||
{
|
||||
$_table = $this->driver->escapeText($this->driver->escapeIdentifier($table));
|
||||
$res = $this->driver->query("
|
||||
SELECT
|
||||
a.attnum AS ordinal_position,
|
||||
a.attname AS column_name
|
||||
FROM
|
||||
pg_attribute a
|
||||
JOIN pg_class cls ON a.attrelid = cls.oid
|
||||
WHERE
|
||||
a.attrelid = $_table::regclass
|
||||
AND a.attnum > 0
|
||||
AND NOT a.attisdropped
|
||||
ORDER BY ordinal_position
|
||||
");
|
||||
|
||||
$columns = [];
|
||||
while ($row = $res->fetch(true)) {
|
||||
$columns[$row['ordinal_position']] = $row['column_name'];
|
||||
}
|
||||
|
||||
$res = $this->driver->query("
|
||||
SELECT pg_class2.relname, indisunique, indisprimary, indkey
|
||||
FROM pg_class
|
||||
LEFT JOIN pg_index on pg_class.oid = pg_index.indrelid
|
||||
INNER JOIN pg_class as pg_class2 on pg_class2.oid = pg_index.indexrelid
|
||||
WHERE pg_class.oid = $_table::regclass
|
||||
");
|
||||
|
||||
$indexes = [];
|
||||
while ($row = $res->fetch(true)) {
|
||||
$indexes[$row['relname']]['name'] = $row['relname'];
|
||||
$indexes[$row['relname']]['unique'] = $row['indisunique'] === 't' || $row['indisunique'] === true;
|
||||
$indexes[$row['relname']]['primary'] = $row['indisprimary'] === 't' || $row['indisprimary'] === true;
|
||||
$indexes[$row['relname']]['columns'] = [];
|
||||
foreach (explode(' ', $row['indkey']) as $index) {
|
||||
if (isset($columns[$index])) {
|
||||
$indexes[$row['relname']]['columns'][] = $columns[$index];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return array_values($indexes);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns metadata for all foreign keys in a table.
|
||||
*/
|
||||
public function getForeignKeys(string $table): array
|
||||
{
|
||||
$_table = $this->driver->escapeText($this->driver->escapeIdentifier($table));
|
||||
|
||||
$res = $this->driver->query("
|
||||
SELECT
|
||||
c.conname AS name,
|
||||
lt.attname AS local,
|
||||
c.confrelid::regclass AS table,
|
||||
ft.attname AS foreign,
|
||||
|
||||
CASE c.confupdtype
|
||||
WHEN 'a' THEN 'NO ACTION'
|
||||
WHEN 'r' THEN 'RESTRICT'
|
||||
WHEN 'c' THEN 'CASCADE'
|
||||
WHEN 'n' THEN 'SET NULL'
|
||||
WHEN 'd' THEN 'SET DEFAULT'
|
||||
ELSE 'UNKNOWN'
|
||||
END AS \"onUpdate\",
|
||||
|
||||
CASE c.confdeltype
|
||||
WHEN 'a' THEN 'NO ACTION'
|
||||
WHEN 'r' THEN 'RESTRICT'
|
||||
WHEN 'c' THEN 'CASCADE'
|
||||
WHEN 'n' THEN 'SET NULL'
|
||||
WHEN 'd' THEN 'SET DEFAULT'
|
||||
ELSE 'UNKNOWN'
|
||||
END AS \"onDelete\",
|
||||
|
||||
c.conkey,
|
||||
lt.attnum AS lnum,
|
||||
c.confkey,
|
||||
ft.attnum AS fnum
|
||||
FROM
|
||||
pg_constraint c
|
||||
JOIN pg_attribute lt ON c.conrelid = lt.attrelid AND lt.attnum = ANY (c.conkey)
|
||||
JOIN pg_attribute ft ON c.confrelid = ft.attrelid AND ft.attnum = ANY (c.confkey)
|
||||
WHERE
|
||||
c.contype = 'f'
|
||||
AND
|
||||
c.conrelid = $_table::regclass
|
||||
");
|
||||
|
||||
$fKeys = $references = [];
|
||||
while ($row = $res->fetch(true)) {
|
||||
if (!isset($fKeys[$row['name']])) {
|
||||
$fKeys[$row['name']] = [
|
||||
'name' => $row['name'],
|
||||
'table' => $row['table'],
|
||||
'local' => [],
|
||||
'foreign' => [],
|
||||
'onUpdate' => $row['onUpdate'],
|
||||
'onDelete' => $row['onDelete'],
|
||||
];
|
||||
|
||||
$l = explode(',', trim($row['conkey'], '{}'));
|
||||
$f = explode(',', trim($row['confkey'], '{}'));
|
||||
|
||||
$references[$row['name']] = array_combine($l, $f);
|
||||
}
|
||||
|
||||
if (
|
||||
isset($references[$row['name']][$row['lnum']])
|
||||
&& $references[$row['name']][$row['lnum']] === $row['fnum']
|
||||
) {
|
||||
$fKeys[$row['name']]['local'][] = $row['local'];
|
||||
$fKeys[$row['name']]['foreign'][] = $row['foreign'];
|
||||
}
|
||||
}
|
||||
|
||||
return $fKeys;
|
||||
}
|
||||
}
|
@@ -1,131 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Dibi\Drivers;
|
||||
|
||||
use Dibi;
|
||||
use Dibi\Helpers;
|
||||
use PgSql;
|
||||
|
||||
|
||||
/**
|
||||
* The driver for PostgreSQL result set.
|
||||
*/
|
||||
class PostgreResult implements Dibi\ResultDriver
|
||||
{
|
||||
use Dibi\Strict;
|
||||
|
||||
/** @var resource|PgSql\Result */
|
||||
private $resultSet;
|
||||
|
||||
/** @var bool */
|
||||
private $autoFree = true;
|
||||
|
||||
|
||||
/**
|
||||
* @param resource|PgSql\Result $resultSet
|
||||
*/
|
||||
public function __construct($resultSet)
|
||||
{
|
||||
$this->resultSet = $resultSet;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Automatically frees the resources allocated for this result set.
|
||||
*/
|
||||
public function __destruct()
|
||||
{
|
||||
if ($this->autoFree && $this->getResultResource()) {
|
||||
$this->free();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the number of rows in a result set.
|
||||
*/
|
||||
public function getRowCount(): int
|
||||
{
|
||||
return pg_num_rows($this->resultSet);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Fetches the row at current position and moves the internal cursor to the next position.
|
||||
* @param bool $assoc true for associative array, false for numeric
|
||||
*/
|
||||
public function fetch(bool $assoc): ?array
|
||||
{
|
||||
return Helpers::false2Null(pg_fetch_array($this->resultSet, null, $assoc ? PGSQL_ASSOC : PGSQL_NUM));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Moves cursor position without fetching row.
|
||||
*/
|
||||
public function seek(int $row): bool
|
||||
{
|
||||
return pg_result_seek($this->resultSet, $row);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Frees the resources allocated for this result set.
|
||||
*/
|
||||
public function free(): void
|
||||
{
|
||||
pg_free_result($this->resultSet);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns metadata for all columns in a result set.
|
||||
*/
|
||||
public function getResultColumns(): array
|
||||
{
|
||||
$count = pg_num_fields($this->resultSet);
|
||||
$columns = [];
|
||||
for ($i = 0; $i < $count; $i++) {
|
||||
$row = [
|
||||
'name' => pg_field_name($this->resultSet, $i),
|
||||
'table' => pg_field_table($this->resultSet, $i),
|
||||
'nativetype' => pg_field_type($this->resultSet, $i),
|
||||
];
|
||||
$row['fullname'] = $row['table']
|
||||
? $row['table'] . '.' . $row['name']
|
||||
: $row['name'];
|
||||
$columns[] = $row;
|
||||
}
|
||||
|
||||
return $columns;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the result set resource.
|
||||
* @return resource|PgSql\Result|null
|
||||
*/
|
||||
public function getResultResource()
|
||||
{
|
||||
$this->autoFree = false;
|
||||
return is_resource($this->resultSet) || $this->resultSet instanceof PgSql\Result
|
||||
? $this->resultSet
|
||||
: null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Decodes data from result set.
|
||||
*/
|
||||
public function unescapeBinary(string $value): string
|
||||
{
|
||||
return pg_unescape_bytea($value);
|
||||
}
|
||||
}
|
@@ -1,18 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Dibi\Drivers;
|
||||
|
||||
|
||||
/**
|
||||
* Alias for SqliteDriver driver.
|
||||
*/
|
||||
class Sqlite3Driver extends SqliteDriver
|
||||
{
|
||||
}
|
@@ -1,18 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Dibi\Drivers;
|
||||
|
||||
|
||||
/**
|
||||
* Alias for SqliteResult driver.
|
||||
*/
|
||||
class Sqlite3Result extends SqliteResult
|
||||
{
|
||||
}
|
@@ -1,298 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Dibi\Drivers;
|
||||
|
||||
use Dibi;
|
||||
use Dibi\Helpers;
|
||||
use SQLite3;
|
||||
|
||||
|
||||
/**
|
||||
* The driver for SQLite v3 database.
|
||||
*
|
||||
* Driver options:
|
||||
* - database (or file) => the filename of the SQLite3 database
|
||||
* - formatDate => how to format date in SQL (@see date)
|
||||
* - formatDateTime => how to format datetime in SQL (@see date)
|
||||
* - resource (SQLite3) => existing connection resource
|
||||
*/
|
||||
class SqliteDriver implements Dibi\Driver
|
||||
{
|
||||
use Dibi\Strict;
|
||||
|
||||
/** @var SQLite3 */
|
||||
private $connection;
|
||||
|
||||
/** @var string Date format */
|
||||
private $fmtDate;
|
||||
|
||||
/** @var string Datetime format */
|
||||
private $fmtDateTime;
|
||||
|
||||
|
||||
/** @throws Dibi\NotSupportedException */
|
||||
public function __construct(array $config)
|
||||
{
|
||||
if (!extension_loaded('sqlite3')) {
|
||||
throw new Dibi\NotSupportedException("PHP extension 'sqlite3' is not loaded.");
|
||||
}
|
||||
|
||||
if (isset($config['dbcharset']) || isset($config['charset'])) {
|
||||
throw new Dibi\NotSupportedException('Options dbcharset and charset are not longer supported.');
|
||||
}
|
||||
|
||||
Helpers::alias($config, 'database', 'file');
|
||||
$this->fmtDate = $config['formatDate'] ?? 'U';
|
||||
$this->fmtDateTime = $config['formatDateTime'] ?? 'U';
|
||||
|
||||
if (isset($config['resource']) && $config['resource'] instanceof SQLite3) {
|
||||
$this->connection = $config['resource'];
|
||||
} else {
|
||||
try {
|
||||
$this->connection = new SQLite3($config['database']);
|
||||
} catch (\Throwable $e) {
|
||||
throw new Dibi\DriverException($e->getMessage(), $e->getCode());
|
||||
}
|
||||
}
|
||||
|
||||
// enable foreign keys support (defaultly disabled; if disabled then foreign key constraints are not enforced)
|
||||
$version = SQLite3::version();
|
||||
if ($version['versionNumber'] >= '3006019') {
|
||||
$this->query('PRAGMA foreign_keys = ON');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Disconnects from a database.
|
||||
*/
|
||||
public function disconnect(): void
|
||||
{
|
||||
$this->connection->close();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Executes the SQL query.
|
||||
* @throws Dibi\DriverException
|
||||
*/
|
||||
public function query(string $sql): ?Dibi\ResultDriver
|
||||
{
|
||||
$res = @$this->connection->query($sql); // intentionally @
|
||||
if ($code = $this->connection->lastErrorCode()) {
|
||||
throw static::createException($this->connection->lastErrorMsg(), $code, $sql);
|
||||
|
||||
} elseif ($res instanceof \SQLite3Result && $res->numColumns()) {
|
||||
return $this->createResultDriver($res);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public static function createException(string $message, $code, string $sql): Dibi\DriverException
|
||||
{
|
||||
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.
|
||||
*/
|
||||
public function getAffectedRows(): ?int
|
||||
{
|
||||
return $this->connection->changes();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query.
|
||||
*/
|
||||
public function getInsertId(?string $sequence): ?int
|
||||
{
|
||||
return $this->connection->lastInsertRowID() ?: null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Begins a transaction (if supported).
|
||||
* @throws Dibi\DriverException
|
||||
*/
|
||||
public function begin(?string $savepoint = null): void
|
||||
{
|
||||
$this->query($savepoint ? "SAVEPOINT $savepoint" : 'BEGIN');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Commits statements in a transaction.
|
||||
* @throws Dibi\DriverException
|
||||
*/
|
||||
public function commit(?string $savepoint = null): void
|
||||
{
|
||||
$this->query($savepoint ? "RELEASE SAVEPOINT $savepoint" : 'COMMIT');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Rollback changes in a transaction.
|
||||
* @throws Dibi\DriverException
|
||||
*/
|
||||
public function rollback(?string $savepoint = null): void
|
||||
{
|
||||
$this->query($savepoint ? "ROLLBACK TO SAVEPOINT $savepoint" : 'ROLLBACK');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the connection resource.
|
||||
*/
|
||||
public function getResource(): ?SQLite3
|
||||
{
|
||||
return $this->connection;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the connection reflector.
|
||||
*/
|
||||
public function getReflector(): Dibi\Reflector
|
||||
{
|
||||
return new SqliteReflector($this);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Result set driver factory.
|
||||
*/
|
||||
public function createResultDriver(\SQLite3Result $result): SqliteResult
|
||||
{
|
||||
return new SqliteResult($result);
|
||||
}
|
||||
|
||||
|
||||
/********************* SQL ****************d*g**/
|
||||
|
||||
|
||||
/**
|
||||
* Encodes data for use in a SQL statement.
|
||||
*/
|
||||
public function escapeText(string $value): string
|
||||
{
|
||||
return "'" . $this->connection->escapeString($value) . "'";
|
||||
}
|
||||
|
||||
|
||||
public function escapeBinary(string $value): string
|
||||
{
|
||||
return "X'" . bin2hex($value) . "'";
|
||||
}
|
||||
|
||||
|
||||
public function escapeIdentifier(string $value): string
|
||||
{
|
||||
return '[' . strtr($value, '[]', ' ') . ']';
|
||||
}
|
||||
|
||||
|
||||
public function escapeBool(bool $value): string
|
||||
{
|
||||
return $value ? '1' : '0';
|
||||
}
|
||||
|
||||
|
||||
public function escapeDate(\DateTimeInterface $value): string
|
||||
{
|
||||
return $value->format($this->fmtDate);
|
||||
}
|
||||
|
||||
|
||||
public function escapeDateTime(\DateTimeInterface $value): string
|
||||
{
|
||||
return $value->format($this->fmtDateTime);
|
||||
}
|
||||
|
||||
|
||||
public function escapeDateInterval(\DateInterval $value): string
|
||||
{
|
||||
throw new Dibi\NotImplementedException;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Encodes string for use in a LIKE statement.
|
||||
*/
|
||||
public function escapeLike(string $value, int $pos): string
|
||||
{
|
||||
$value = addcslashes($this->connection->escapeString($value), '%_\\');
|
||||
return ($pos & 1 ? "'%" : "'") . $value . ($pos & 2 ? "%'" : "'") . " ESCAPE '\\'";
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Injects LIMIT/OFFSET to the SQL query.
|
||||
*/
|
||||
public function applyLimit(string &$sql, ?int $limit, ?int $offset): void
|
||||
{
|
||||
if ($limit < 0 || $offset < 0) {
|
||||
throw new Dibi\NotSupportedException('Negative offset or limit.');
|
||||
|
||||
} elseif ($limit !== null || $offset) {
|
||||
$sql .= ' LIMIT ' . ($limit ?? '-1')
|
||||
. ($offset ? ' OFFSET ' . $offset : '');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/********************* user defined functions ****************d*g**/
|
||||
|
||||
|
||||
/**
|
||||
* Registers an user defined function for use in SQL statements.
|
||||
*/
|
||||
public function registerFunction(string $name, callable $callback, int $numArgs = -1): void
|
||||
{
|
||||
$this->connection->createFunction($name, $callback, $numArgs);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Registers an aggregating user defined function for use in SQL statements.
|
||||
*/
|
||||
public function registerAggregateFunction(
|
||||
string $name,
|
||||
callable $rowCallback,
|
||||
callable $agrCallback,
|
||||
int $numArgs = -1
|
||||
): void {
|
||||
$this->connection->createAggregate($name, $rowCallback, $agrCallback, $numArgs);
|
||||
}
|
||||
}
|
@@ -1,152 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Dibi\Drivers;
|
||||
|
||||
use Dibi;
|
||||
|
||||
|
||||
/**
|
||||
* The reflector for SQLite database.
|
||||
*/
|
||||
class SqliteReflector implements Dibi\Reflector
|
||||
{
|
||||
use Dibi\Strict;
|
||||
|
||||
/** @var Dibi\Driver */
|
||||
private $driver;
|
||||
|
||||
|
||||
public function __construct(Dibi\Driver $driver)
|
||||
{
|
||||
$this->driver = $driver;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns list of tables.
|
||||
*/
|
||||
public function getTables(): array
|
||||
{
|
||||
$res = $this->driver->query("
|
||||
SELECT name, type = 'view' as view FROM sqlite_master WHERE type IN ('table', 'view')
|
||||
UNION ALL
|
||||
SELECT name, type = 'view' as view FROM sqlite_temp_master WHERE type IN ('table', 'view')
|
||||
ORDER BY name
|
||||
");
|
||||
$tables = [];
|
||||
while ($row = $res->fetch(true)) {
|
||||
$tables[] = $row;
|
||||
}
|
||||
|
||||
return $tables;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns metadata for all columns in a table.
|
||||
*/
|
||||
public function getColumns(string $table): array
|
||||
{
|
||||
$res = $this->driver->query("PRAGMA table_info({$this->driver->escapeIdentifier($table)})");
|
||||
$columns = [];
|
||||
while ($row = $res->fetch(true)) {
|
||||
$column = $row['name'];
|
||||
$type = explode('(', $row['type']);
|
||||
$columns[] = [
|
||||
'name' => $column,
|
||||
'table' => $table,
|
||||
'fullname' => "$table.$column",
|
||||
'nativetype' => strtoupper($type[0]),
|
||||
'size' => isset($type[1]) ? (int) $type[1] : null,
|
||||
'nullable' => $row['notnull'] === 0,
|
||||
'default' => $row['dflt_value'],
|
||||
'autoincrement' => $row['pk'] && $type[0] === 'INTEGER',
|
||||
'vendor' => $row,
|
||||
];
|
||||
}
|
||||
|
||||
return $columns;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns metadata for all indexes in a table.
|
||||
*/
|
||||
public function getIndexes(string $table): array
|
||||
{
|
||||
$res = $this->driver->query("PRAGMA index_list({$this->driver->escapeIdentifier($table)})");
|
||||
$indexes = [];
|
||||
while ($row = $res->fetch(true)) {
|
||||
$indexes[$row['name']]['name'] = $row['name'];
|
||||
$indexes[$row['name']]['unique'] = (bool) $row['unique'];
|
||||
}
|
||||
|
||||
foreach ($indexes as $index => $values) {
|
||||
$res = $this->driver->query("PRAGMA index_info({$this->driver->escapeIdentifier($index)})");
|
||||
while ($row = $res->fetch(true)) {
|
||||
$indexes[$index]['columns'][$row['seqno']] = $row['name'];
|
||||
}
|
||||
}
|
||||
|
||||
$columns = $this->getColumns($table);
|
||||
foreach ($indexes as $index => $values) {
|
||||
$column = $indexes[$index]['columns'][0];
|
||||
$primary = false;
|
||||
foreach ($columns as $info) {
|
||||
if ($column === $info['name']) {
|
||||
$primary = $info['vendor']['pk'];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$indexes[$index]['primary'] = (bool) $primary;
|
||||
}
|
||||
|
||||
if (!$indexes) { // @see http://www.sqlite.org/lang_createtable.html#rowid
|
||||
foreach ($columns as $column) {
|
||||
if ($column['vendor']['pk']) {
|
||||
$indexes[] = [
|
||||
'name' => 'ROWID',
|
||||
'unique' => true,
|
||||
'primary' => true,
|
||||
'columns' => [$column['name']],
|
||||
];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return array_values($indexes);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns metadata for all foreign keys in a table.
|
||||
*/
|
||||
public function getForeignKeys(string $table): array
|
||||
{
|
||||
$res = $this->driver->query("PRAGMA foreign_key_list({$this->driver->escapeIdentifier($table)})");
|
||||
$keys = [];
|
||||
while ($row = $res->fetch(true)) {
|
||||
$keys[$row['id']]['name'] = $row['id']; // foreign key name
|
||||
$keys[$row['id']]['local'][$row['seq']] = $row['from']; // local columns
|
||||
$keys[$row['id']]['table'] = $row['table']; // referenced table
|
||||
$keys[$row['id']]['foreign'][$row['seq']] = $row['to']; // referenced columns
|
||||
$keys[$row['id']]['onDelete'] = $row['on_delete'];
|
||||
$keys[$row['id']]['onUpdate'] = $row['on_update'];
|
||||
|
||||
if ($keys[$row['id']]['foreign'][0] == null) {
|
||||
$keys[$row['id']]['foreign'] = null;
|
||||
}
|
||||
}
|
||||
|
||||
return array_values($keys);
|
||||
}
|
||||
}
|
@@ -1,124 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Dibi\Drivers;
|
||||
|
||||
use Dibi;
|
||||
use Dibi\Helpers;
|
||||
|
||||
|
||||
/**
|
||||
* The driver for SQLite result set.
|
||||
*/
|
||||
class SqliteResult implements Dibi\ResultDriver
|
||||
{
|
||||
use Dibi\Strict;
|
||||
|
||||
/** @var \SQLite3Result */
|
||||
private $resultSet;
|
||||
|
||||
/** @var bool */
|
||||
private $autoFree = true;
|
||||
|
||||
|
||||
public function __construct(\SQLite3Result $resultSet)
|
||||
{
|
||||
$this->resultSet = $resultSet;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Automatically frees the resources allocated for this result set.
|
||||
*/
|
||||
public function __destruct()
|
||||
{
|
||||
if ($this->autoFree && $this->getResultResource()) {
|
||||
@$this->free();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the number of rows in a result set.
|
||||
* @throws Dibi\NotSupportedException
|
||||
*/
|
||||
public function getRowCount(): int
|
||||
{
|
||||
throw new Dibi\NotSupportedException('Row count is not available for unbuffered queries.');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Fetches the row at current position and moves the internal cursor to the next position.
|
||||
* @param bool $assoc true for associative array, false for numeric
|
||||
*/
|
||||
public function fetch(bool $assoc): ?array
|
||||
{
|
||||
return Helpers::false2Null($this->resultSet->fetchArray($assoc ? SQLITE3_ASSOC : SQLITE3_NUM));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Moves cursor position without fetching row.
|
||||
* @throws Dibi\NotSupportedException
|
||||
*/
|
||||
public function seek(int $row): bool
|
||||
{
|
||||
throw new Dibi\NotSupportedException('Cannot seek an unbuffered result set.');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Frees the resources allocated for this result set.
|
||||
*/
|
||||
public function free(): void
|
||||
{
|
||||
$this->resultSet->finalize();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns metadata for all columns in a result set.
|
||||
*/
|
||||
public function getResultColumns(): array
|
||||
{
|
||||
$count = $this->resultSet->numColumns();
|
||||
$columns = [];
|
||||
static $types = [SQLITE3_INTEGER => 'int', SQLITE3_FLOAT => 'float', SQLITE3_TEXT => 'text', SQLITE3_BLOB => 'blob', SQLITE3_NULL => 'null'];
|
||||
for ($i = 0; $i < $count; $i++) {
|
||||
$columns[] = [
|
||||
'name' => $this->resultSet->columnName($i),
|
||||
'table' => null,
|
||||
'fullname' => $this->resultSet->columnName($i),
|
||||
'nativetype' => $types[$this->resultSet->columnType($i)] ?? null, // buggy in PHP 7.4.4 & 7.3.16, bug 79414
|
||||
];
|
||||
}
|
||||
|
||||
return $columns;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the result set resource.
|
||||
*/
|
||||
public function getResultResource(): \SQLite3Result
|
||||
{
|
||||
$this->autoFree = false;
|
||||
return $this->resultSet;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Decodes data from result set.
|
||||
*/
|
||||
public function unescapeBinary(string $value): string
|
||||
{
|
||||
return $value;
|
||||
}
|
||||
}
|
@@ -1,280 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Dibi\Drivers;
|
||||
|
||||
use Dibi;
|
||||
use Dibi\Helpers;
|
||||
|
||||
|
||||
/**
|
||||
* The driver for Microsoft SQL Server and SQL Azure databases.
|
||||
*
|
||||
* Driver options:
|
||||
* - host => the MS SQL server host name. It can also include a port number (hostname:port)
|
||||
* - username (or user)
|
||||
* - password (or pass)
|
||||
* - database => the database name to select
|
||||
* - options (array) => connection options {@link https://msdn.microsoft.com/en-us/library/cc296161(SQL.90).aspx}
|
||||
* - charset => character encoding to set (default is UTF-8)
|
||||
* - resource (resource) => existing connection resource
|
||||
*/
|
||||
class SqlsrvDriver implements Dibi\Driver
|
||||
{
|
||||
use Dibi\Strict;
|
||||
|
||||
/** @var resource */
|
||||
private $connection;
|
||||
|
||||
/** @var int|null Affected rows */
|
||||
private $affectedRows;
|
||||
|
||||
/** @var string */
|
||||
private $version = '';
|
||||
|
||||
|
||||
/** @throws Dibi\NotSupportedException */
|
||||
public function __construct(array $config)
|
||||
{
|
||||
if (!extension_loaded('sqlsrv')) {
|
||||
throw new Dibi\NotSupportedException("PHP extension 'sqlsrv' is not loaded.");
|
||||
}
|
||||
|
||||
Helpers::alias($config, 'options|UID', 'username');
|
||||
Helpers::alias($config, 'options|PWD', 'password');
|
||||
Helpers::alias($config, 'options|Database', 'database');
|
||||
Helpers::alias($config, 'options|CharacterSet', 'charset');
|
||||
|
||||
if (isset($config['resource'])) {
|
||||
$this->connection = $config['resource'];
|
||||
if (!is_resource($this->connection)) {
|
||||
throw new \InvalidArgumentException("Configuration option 'resource' is not resource.");
|
||||
}
|
||||
} else {
|
||||
$options = $config['options'];
|
||||
|
||||
// Default values
|
||||
$options['CharacterSet'] = $options['CharacterSet'] ?? 'UTF-8';
|
||||
$options['PWD'] = (string) $options['PWD'];
|
||||
$options['UID'] = (string) $options['UID'];
|
||||
$options['Database'] = (string) $options['Database'];
|
||||
|
||||
sqlsrv_configure('WarningsReturnAsErrors', 0);
|
||||
$this->connection = sqlsrv_connect($config['host'], $options);
|
||||
if (!is_resource($this->connection)) {
|
||||
$info = sqlsrv_errors(SQLSRV_ERR_ERRORS);
|
||||
throw new Dibi\DriverException($info[0]['message'], $info[0]['code']);
|
||||
}
|
||||
|
||||
sqlsrv_configure('WarningsReturnAsErrors', 1);
|
||||
}
|
||||
|
||||
$this->version = sqlsrv_server_info($this->connection)['SQLServerVersion'];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Disconnects from a database.
|
||||
*/
|
||||
public function disconnect(): void
|
||||
{
|
||||
@sqlsrv_close($this->connection); // @ - connection can be already disconnected
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Executes the SQL query.
|
||||
* @throws Dibi\DriverException
|
||||
*/
|
||||
public function query(string $sql): ?Dibi\ResultDriver
|
||||
{
|
||||
$this->affectedRows = null;
|
||||
$res = sqlsrv_query($this->connection, $sql);
|
||||
|
||||
if ($res === false) {
|
||||
$info = sqlsrv_errors();
|
||||
throw new Dibi\DriverException($info[0]['message'], $info[0]['code'], $sql);
|
||||
|
||||
} elseif (is_resource($res)) {
|
||||
$this->affectedRows = Helpers::false2Null(sqlsrv_rows_affected($res));
|
||||
return sqlsrv_num_fields($res)
|
||||
? $this->createResultDriver($res)
|
||||
: null;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets the number of affected rows by the last INSERT, UPDATE or DELETE query.
|
||||
*/
|
||||
public function getAffectedRows(): ?int
|
||||
{
|
||||
return $this->affectedRows;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query.
|
||||
*/
|
||||
public function getInsertId(?string $sequence): ?int
|
||||
{
|
||||
$res = sqlsrv_query($this->connection, 'SELECT SCOPE_IDENTITY()');
|
||||
if (is_resource($res)) {
|
||||
$row = sqlsrv_fetch_array($res, SQLSRV_FETCH_NUMERIC);
|
||||
return Dibi\Helpers::intVal($row[0]);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Begins a transaction (if supported).
|
||||
* @throws Dibi\DriverException
|
||||
*/
|
||||
public function begin(?string $savepoint = null): void
|
||||
{
|
||||
sqlsrv_begin_transaction($this->connection);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Commits statements in a transaction.
|
||||
* @throws Dibi\DriverException
|
||||
*/
|
||||
public function commit(?string $savepoint = null): void
|
||||
{
|
||||
sqlsrv_commit($this->connection);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Rollback changes in a transaction.
|
||||
* @throws Dibi\DriverException
|
||||
*/
|
||||
public function rollback(?string $savepoint = null): void
|
||||
{
|
||||
sqlsrv_rollback($this->connection);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the connection resource.
|
||||
* @return resource|null
|
||||
*/
|
||||
public function getResource()
|
||||
{
|
||||
return is_resource($this->connection) ? $this->connection : null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the connection reflector.
|
||||
*/
|
||||
public function getReflector(): Dibi\Reflector
|
||||
{
|
||||
return new SqlsrvReflector($this);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Result set driver factory.
|
||||
* @param resource $resource
|
||||
*/
|
||||
public function createResultDriver($resource): SqlsrvResult
|
||||
{
|
||||
return new SqlsrvResult($resource);
|
||||
}
|
||||
|
||||
|
||||
/********************* SQL ****************d*g**/
|
||||
|
||||
|
||||
/**
|
||||
* Encodes data for use in a SQL statement.
|
||||
*/
|
||||
public function escapeText(string $value): string
|
||||
{
|
||||
return "N'" . str_replace("'", "''", $value) . "'";
|
||||
}
|
||||
|
||||
|
||||
public function escapeBinary(string $value): string
|
||||
{
|
||||
return '0x' . bin2hex($value);
|
||||
}
|
||||
|
||||
|
||||
public function escapeIdentifier(string $value): string
|
||||
{
|
||||
// @see https://msdn.microsoft.com/en-us/library/ms176027.aspx
|
||||
return '[' . str_replace(']', ']]', $value) . ']';
|
||||
}
|
||||
|
||||
|
||||
public function escapeBool(bool $value): string
|
||||
{
|
||||
return $value ? '1' : '0';
|
||||
}
|
||||
|
||||
|
||||
public function escapeDate(\DateTimeInterface $value): string
|
||||
{
|
||||
return $value->format("'Y-m-d'");
|
||||
}
|
||||
|
||||
|
||||
public function escapeDateTime(\DateTimeInterface $value): string
|
||||
{
|
||||
return 'CONVERT(DATETIME2(7), ' . $value->format("'Y-m-d H:i:s.u'") . ')';
|
||||
}
|
||||
|
||||
|
||||
public function escapeDateInterval(\DateInterval $value): string
|
||||
{
|
||||
throw new Dibi\NotImplementedException;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Encodes string for use in a LIKE statement.
|
||||
*/
|
||||
public function escapeLike(string $value, int $pos): string
|
||||
{
|
||||
$value = strtr($value, ["'" => "''", '%' => '[%]', '_' => '[_]', '[' => '[[]']);
|
||||
return ($pos & 1 ? "'%" : "'") . $value . ($pos & 2 ? "%'" : "'");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Injects LIMIT/OFFSET to the SQL query.
|
||||
*/
|
||||
public function applyLimit(string &$sql, ?int $limit, ?int $offset): void
|
||||
{
|
||||
if ($limit < 0 || $offset < 0) {
|
||||
throw new Dibi\NotSupportedException('Negative offset or limit.');
|
||||
|
||||
} elseif (version_compare($this->version, '11', '<')) { // 11 == SQL Server 2012
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,131 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Dibi\Drivers;
|
||||
|
||||
use Dibi;
|
||||
|
||||
|
||||
/**
|
||||
* The reflector for Microsoft SQL Server and SQL Azure databases.
|
||||
*/
|
||||
class SqlsrvReflector implements Dibi\Reflector
|
||||
{
|
||||
use Dibi\Strict;
|
||||
|
||||
/** @var Dibi\Driver */
|
||||
private $driver;
|
||||
|
||||
|
||||
public function __construct(Dibi\Driver $driver)
|
||||
{
|
||||
$this->driver = $driver;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns list of tables.
|
||||
*/
|
||||
public function getTables(): array
|
||||
{
|
||||
$res = $this->driver->query("SELECT TABLE_NAME, TABLE_TYPE FROM INFORMATION_SCHEMA.TABLES WHERE [TABLE_SCHEMA] = 'dbo'");
|
||||
$tables = [];
|
||||
while ($row = $res->fetch(false)) {
|
||||
$tables[] = [
|
||||
'name' => $row[0],
|
||||
'view' => isset($row[1]) && $row[1] === 'VIEW',
|
||||
];
|
||||
}
|
||||
|
||||
return $tables;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns metadata for all columns in a table.
|
||||
*/
|
||||
public function getColumns(string $table): array
|
||||
{
|
||||
$res = $this->driver->query("
|
||||
SELECT c.name as COLUMN_NAME, c.is_identity AS AUTO_INCREMENT
|
||||
FROM sys.columns c
|
||||
INNER JOIN sys.tables t ON c.object_id = t.object_id
|
||||
WHERE t.name = {$this->driver->escapeText($table)}
|
||||
");
|
||||
|
||||
$autoIncrements = [];
|
||||
while ($row = $res->fetch(true)) {
|
||||
$autoIncrements[$row['COLUMN_NAME']] = (bool) $row['AUTO_INCREMENT'];
|
||||
}
|
||||
|
||||
$res = $this->driver->query("
|
||||
SELECT C.COLUMN_NAME, C.DATA_TYPE, C.CHARACTER_MAXIMUM_LENGTH , C.COLUMN_DEFAULT , C.NUMERIC_PRECISION, C.NUMERIC_SCALE , C.IS_NULLABLE, Case When Z.CONSTRAINT_NAME Is Null Then 0 Else 1 End As IsPartOfPrimaryKey
|
||||
FROM INFORMATION_SCHEMA.COLUMNS As C
|
||||
Outer Apply (
|
||||
SELECT CCU.CONSTRAINT_NAME
|
||||
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS As TC
|
||||
Join INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE As CCU
|
||||
On CCU.CONSTRAINT_NAME = TC.CONSTRAINT_NAME
|
||||
WHERE TC.TABLE_SCHEMA = C.TABLE_SCHEMA
|
||||
And TC.TABLE_NAME = C.TABLE_NAME
|
||||
And TC.CONSTRAINT_TYPE = 'PRIMARY KEY'
|
||||
And CCU.COLUMN_NAME = C.COLUMN_NAME
|
||||
) As Z
|
||||
WHERE C.TABLE_NAME = {$this->driver->escapeText($table)}
|
||||
");
|
||||
$columns = [];
|
||||
while ($row = $res->fetch(true)) {
|
||||
$columns[] = [
|
||||
'name' => $row['COLUMN_NAME'],
|
||||
'table' => $table,
|
||||
'nativetype' => strtoupper($row['DATA_TYPE']),
|
||||
'size' => $row['CHARACTER_MAXIMUM_LENGTH'],
|
||||
'nullable' => $row['IS_NULLABLE'] === 'YES',
|
||||
'default' => $row['COLUMN_DEFAULT'],
|
||||
'autoincrement' => $autoIncrements[$row['COLUMN_NAME']],
|
||||
'vendor' => $row,
|
||||
];
|
||||
}
|
||||
|
||||
return $columns;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns metadata for all indexes in a table.
|
||||
*/
|
||||
public function getIndexes(string $table): array
|
||||
{
|
||||
$keyUsagesRes = $this->driver->query(sprintf('EXEC [sys].[sp_helpindex] @objname = %s', $this->driver->escapeText($table)));
|
||||
$keyUsages = [];
|
||||
while ($row = $keyUsagesRes->fetch(true)) {
|
||||
$keyUsages[$row['index_name']] = explode(',', $row['index_keys']);
|
||||
}
|
||||
|
||||
$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 = [];
|
||||
while ($row = $res->fetch(true)) {
|
||||
$indexes[$row['name']]['name'] = $row['name'];
|
||||
$indexes[$row['name']]['unique'] = $row['is_unique'] === 1;
|
||||
$indexes[$row['name']]['primary'] = $row['is_primary_key'] === 1;
|
||||
$indexes[$row['name']]['columns'] = $keyUsages[$row['name']] ?? [];
|
||||
}
|
||||
|
||||
return array_values($indexes);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns metadata for all foreign keys in a table.
|
||||
*/
|
||||
public function getForeignKeys(string $table): array
|
||||
{
|
||||
throw new Dibi\NotImplementedException;
|
||||
}
|
||||
}
|
@@ -1,122 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Dibi\Drivers;
|
||||
|
||||
use Dibi;
|
||||
|
||||
|
||||
/**
|
||||
* The driver for Microsoft SQL Server and SQL Azure result set.
|
||||
*/
|
||||
class SqlsrvResult implements Dibi\ResultDriver
|
||||
{
|
||||
use Dibi\Strict;
|
||||
|
||||
/** @var resource */
|
||||
private $resultSet;
|
||||
|
||||
/** @var bool */
|
||||
private $autoFree = true;
|
||||
|
||||
|
||||
/**
|
||||
* @param resource $resultSet
|
||||
*/
|
||||
public function __construct($resultSet)
|
||||
{
|
||||
$this->resultSet = $resultSet;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Automatically frees the resources allocated for this result set.
|
||||
*/
|
||||
public function __destruct()
|
||||
{
|
||||
if ($this->autoFree && $this->getResultResource()) {
|
||||
$this->free();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the number of rows in a result set.
|
||||
*/
|
||||
public function getRowCount(): int
|
||||
{
|
||||
throw new Dibi\NotSupportedException('Row count is not available for unbuffered queries.');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Fetches the row at current position and moves the internal cursor to the next position.
|
||||
* @param bool $assoc true for associative array, false for numeric
|
||||
*/
|
||||
public function fetch(bool $assoc): ?array
|
||||
{
|
||||
return Dibi\Helpers::false2Null(sqlsrv_fetch_array($this->resultSet, $assoc ? SQLSRV_FETCH_ASSOC : SQLSRV_FETCH_NUMERIC));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Moves cursor position without fetching row.
|
||||
*/
|
||||
public function seek(int $row): bool
|
||||
{
|
||||
throw new Dibi\NotSupportedException('Cannot seek an unbuffered result set.');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Frees the resources allocated for this result set.
|
||||
*/
|
||||
public function free(): void
|
||||
{
|
||||
sqlsrv_free_stmt($this->resultSet);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns metadata for all columns in a result set.
|
||||
*/
|
||||
public function getResultColumns(): array
|
||||
{
|
||||
$columns = [];
|
||||
foreach ((array) sqlsrv_field_metadata($this->resultSet) as $fieldMetadata) {
|
||||
$columns[] = [
|
||||
'name' => $fieldMetadata['Name'],
|
||||
'fullname' => $fieldMetadata['Name'],
|
||||
'nativetype' => $fieldMetadata['Type'],
|
||||
];
|
||||
}
|
||||
|
||||
return $columns;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the result set resource.
|
||||
* @return resource|null
|
||||
*/
|
||||
public function getResultResource()
|
||||
{
|
||||
$this->autoFree = false;
|
||||
return is_resource($this->resultSet) ? $this->resultSet : null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Decodes data from result set.
|
||||
*/
|
||||
public function unescapeBinary(string $value): string
|
||||
{
|
||||
return $value;
|
||||
}
|
||||
}
|
@@ -1,102 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Dibi;
|
||||
|
||||
|
||||
/**
|
||||
* Profiler & logger event.
|
||||
*/
|
||||
class Event
|
||||
{
|
||||
use Strict;
|
||||
|
||||
/** event type */
|
||||
public const
|
||||
CONNECT = 1,
|
||||
SELECT = 4,
|
||||
INSERT = 8,
|
||||
DELETE = 16,
|
||||
UPDATE = 32,
|
||||
QUERY = 60, // SELECT | INSERT | DELETE | UPDATE
|
||||
BEGIN = 64,
|
||||
COMMIT = 128,
|
||||
ROLLBACK = 256,
|
||||
TRANSACTION = 448, // BEGIN | COMMIT | ROLLBACK
|
||||
ALL = 1023;
|
||||
|
||||
/** @var Connection */
|
||||
public $connection;
|
||||
|
||||
/** @var int */
|
||||
public $type;
|
||||
|
||||
/** @var string */
|
||||
public $sql;
|
||||
|
||||
/** @var Result|DriverException|null */
|
||||
public $result;
|
||||
|
||||
/** @var float */
|
||||
public $time;
|
||||
|
||||
/** @var int|null */
|
||||
public $count;
|
||||
|
||||
/** @var array|null */
|
||||
public $source;
|
||||
|
||||
|
||||
public function __construct(Connection $connection, int $type, ?string $sql = null)
|
||||
{
|
||||
$this->connection = $connection;
|
||||
$this->type = $type;
|
||||
$this->sql = trim((string) $sql);
|
||||
$this->time = -microtime(true);
|
||||
|
||||
if ($type === self::QUERY && preg_match('#\(?\s*(SELECT|UPDATE|INSERT|DELETE)#iA', $this->sql, $matches)) {
|
||||
static $types = [
|
||||
'SELECT' => self::SELECT, 'UPDATE' => self::UPDATE,
|
||||
'INSERT' => self::INSERT, 'DELETE' => self::DELETE,
|
||||
];
|
||||
$this->type = $types[strtoupper($matches[1])];
|
||||
}
|
||||
|
||||
$dibiDir = dirname((new \ReflectionClass('dibi'))->getFileName()) . DIRECTORY_SEPARATOR;
|
||||
foreach (debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS) as $row) {
|
||||
if (isset($row['file']) && is_file($row['file']) && strpos($row['file'], $dibiDir) !== 0) {
|
||||
$this->source = [$row['file'], (int) $row['line']];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
\dibi::$elapsedTime = null;
|
||||
\dibi::$numOfQueries++;
|
||||
\dibi::$sql = $sql;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Result|DriverException|null $result
|
||||
*/
|
||||
public function done($result = null): self
|
||||
{
|
||||
$this->result = $result;
|
||||
try {
|
||||
$this->count = $result instanceof Result ? count($result) : null;
|
||||
} catch (Exception $e) {
|
||||
$this->count = null;
|
||||
}
|
||||
|
||||
$this->time += microtime(true);
|
||||
\dibi::$elapsedTime = $this->time;
|
||||
\dibi::$totalTime += $this->time;
|
||||
return $this;
|
||||
}
|
||||
}
|
@@ -1,34 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Dibi;
|
||||
|
||||
|
||||
/**
|
||||
* SQL expression.
|
||||
*/
|
||||
class Expression
|
||||
{
|
||||
use Strict;
|
||||
|
||||
/** @var array */
|
||||
private $values;
|
||||
|
||||
|
||||
public function __construct(...$values)
|
||||
{
|
||||
$this->values = $values;
|
||||
}
|
||||
|
||||
|
||||
public function getValues(): array
|
||||
{
|
||||
return $this->values;
|
||||
}
|
||||
}
|
@@ -1,485 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Dibi;
|
||||
|
||||
|
||||
/**
|
||||
* SQL builder via fluent interfaces.
|
||||
*
|
||||
* @method Fluent select(...$field)
|
||||
* @method Fluent distinct()
|
||||
* @method Fluent from($table, ...$args = null)
|
||||
* @method Fluent where(...$cond)
|
||||
* @method Fluent groupBy(...$field)
|
||||
* @method Fluent having(...$cond)
|
||||
* @method Fluent orderBy(...$field)
|
||||
* @method Fluent limit(int $limit)
|
||||
* @method Fluent offset(int $offset)
|
||||
* @method Fluent join(...$table)
|
||||
* @method Fluent leftJoin(...$table)
|
||||
* @method Fluent innerJoin(...$table)
|
||||
* @method Fluent rightJoin(...$table)
|
||||
* @method Fluent outerJoin(...$table)
|
||||
* @method Fluent as(...$field)
|
||||
* @method Fluent on(...$cond)
|
||||
* @method Fluent and(...$cond)
|
||||
* @method Fluent or(...$cond)
|
||||
* @method Fluent using(...$cond)
|
||||
* @method Fluent update(...$cond)
|
||||
* @method Fluent insert(...$cond)
|
||||
* @method Fluent delete(...$cond)
|
||||
* @method Fluent into(...$cond)
|
||||
* @method Fluent values(...$cond)
|
||||
* @method Fluent set(...$args)
|
||||
* @method Fluent asc()
|
||||
* @method Fluent desc()
|
||||
*/
|
||||
class Fluent implements IDataSource
|
||||
{
|
||||
use Strict;
|
||||
|
||||
public const REMOVE = false;
|
||||
|
||||
/** @var array */
|
||||
public static $masks = [
|
||||
'SELECT' => ['SELECT', 'DISTINCT', 'FROM', 'WHERE', 'GROUP BY',
|
||||
'HAVING', 'ORDER BY', 'LIMIT', 'OFFSET', ],
|
||||
'UPDATE' => ['UPDATE', 'SET', 'WHERE', 'ORDER BY', 'LIMIT'],
|
||||
'INSERT' => ['INSERT', 'INTO', 'VALUES', 'SELECT'],
|
||||
'DELETE' => ['DELETE', 'FROM', 'USING', 'WHERE', 'ORDER BY', 'LIMIT'],
|
||||
];
|
||||
|
||||
/** @var array default modifiers for arrays */
|
||||
public static $modifiers = [
|
||||
'SELECT' => '%n',
|
||||
'FROM' => '%n',
|
||||
'IN' => '%in',
|
||||
'VALUES' => '%l',
|
||||
'SET' => '%a',
|
||||
'WHERE' => '%and',
|
||||
'HAVING' => '%and',
|
||||
'ORDER BY' => '%by',
|
||||
'GROUP BY' => '%by',
|
||||
];
|
||||
|
||||
/** @var array clauses separators */
|
||||
public static $separators = [
|
||||
'SELECT' => ',',
|
||||
'FROM' => ',',
|
||||
'WHERE' => 'AND',
|
||||
'GROUP BY' => ',',
|
||||
'HAVING' => 'AND',
|
||||
'ORDER BY' => ',',
|
||||
'LIMIT' => false,
|
||||
'OFFSET' => false,
|
||||
'SET' => ',',
|
||||
'VALUES' => ',',
|
||||
'INTO' => false,
|
||||
];
|
||||
|
||||
/** @var array clauses */
|
||||
public static $clauseSwitches = [
|
||||
'JOIN' => 'FROM',
|
||||
'INNER JOIN' => 'FROM',
|
||||
'LEFT JOIN' => 'FROM',
|
||||
'RIGHT JOIN' => 'FROM',
|
||||
];
|
||||
|
||||
/** @var Connection */
|
||||
private $connection;
|
||||
|
||||
/** @var array */
|
||||
private $setups = [];
|
||||
|
||||
/** @var string|null */
|
||||
private $command;
|
||||
|
||||
/** @var array */
|
||||
private $clauses = [];
|
||||
|
||||
/** @var array */
|
||||
private $flags = [];
|
||||
|
||||
/** @var array|null */
|
||||
private $cursor;
|
||||
|
||||
/** @var HashMap normalized clauses */
|
||||
private static $normalizer;
|
||||
|
||||
|
||||
public function __construct(Connection $connection)
|
||||
{
|
||||
$this->connection = $connection;
|
||||
|
||||
if (self::$normalizer === null) {
|
||||
self::$normalizer = new HashMap([self::class, '_formatClause']);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Appends new argument to the clause.
|
||||
*/
|
||||
public function __call(string $clause, array $args): self
|
||||
{
|
||||
$clause = self::$normalizer->$clause;
|
||||
|
||||
// lazy initialization
|
||||
if ($this->command === null) {
|
||||
if (isset(self::$masks[$clause])) {
|
||||
$this->clauses = array_fill_keys(self::$masks[$clause], null);
|
||||
}
|
||||
|
||||
$this->cursor = &$this->clauses[$clause];
|
||||
$this->cursor = [];
|
||||
$this->command = $clause;
|
||||
}
|
||||
|
||||
// auto-switch to a clause
|
||||
if (isset(self::$clauseSwitches[$clause])) {
|
||||
$this->cursor = &$this->clauses[self::$clauseSwitches[$clause]];
|
||||
}
|
||||
|
||||
if (array_key_exists($clause, $this->clauses)) {
|
||||
// append to clause
|
||||
$this->cursor = &$this->clauses[$clause];
|
||||
|
||||
// TODO: really delete?
|
||||
if ($args === [self::REMOVE]) {
|
||||
$this->cursor = null;
|
||||
return $this;
|
||||
}
|
||||
|
||||
if (isset(self::$separators[$clause])) {
|
||||
$sep = self::$separators[$clause];
|
||||
if ($sep === false) { // means: replace
|
||||
$this->cursor = [];
|
||||
|
||||
} elseif (!empty($this->cursor)) {
|
||||
$this->cursor[] = $sep;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// append to currect flow
|
||||
if ($args === [self::REMOVE]) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
$this->cursor[] = $clause;
|
||||
}
|
||||
|
||||
if ($this->cursor === null) {
|
||||
$this->cursor = [];
|
||||
}
|
||||
|
||||
// special types or argument
|
||||
if (count($args) === 1) {
|
||||
$arg = $args[0];
|
||||
// TODO: really ignore true?
|
||||
if ($arg === true) { // flag
|
||||
return $this;
|
||||
|
||||
} elseif (is_string($arg) && preg_match('#^[a-z:_][a-z0-9_.:]*\z#i', $arg)) { // identifier
|
||||
$args = [$clause === 'AS' ? '%N' : '%n', $arg];
|
||||
|
||||
} elseif (is_array($arg) || ($arg instanceof \Traversable && !$arg instanceof self)) { // any array
|
||||
if (isset(self::$modifiers[$clause])) {
|
||||
$args = [self::$modifiers[$clause], $arg];
|
||||
|
||||
} elseif (is_string(key($arg))) { // associative array
|
||||
$args = ['%a', $arg];
|
||||
}
|
||||
} // case $arg === false is handled above
|
||||
}
|
||||
|
||||
foreach ($args as $arg) {
|
||||
if ($arg instanceof self) {
|
||||
$arg = new Literal("($arg)");
|
||||
}
|
||||
|
||||
$this->cursor[] = $arg;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Switch to a clause.
|
||||
*/
|
||||
public function clause(string $clause): self
|
||||
{
|
||||
$this->cursor = &$this->clauses[self::$normalizer->$clause];
|
||||
if ($this->cursor === null) {
|
||||
$this->cursor = [];
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Removes a clause.
|
||||
*/
|
||||
public function removeClause(string $clause): self
|
||||
{
|
||||
$this->clauses[self::$normalizer->$clause] = null;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Change a SQL flag.
|
||||
*/
|
||||
public function setFlag(string $flag, bool $value = true): self
|
||||
{
|
||||
$flag = strtoupper($flag);
|
||||
if ($value) {
|
||||
$this->flags[$flag] = true;
|
||||
} else {
|
||||
unset($this->flags[$flag]);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Is a flag set?
|
||||
*/
|
||||
final public function getFlag(string $flag): bool
|
||||
{
|
||||
return isset($this->flags[strtoupper($flag)]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns SQL command.
|
||||
*/
|
||||
final public function getCommand(): ?string
|
||||
{
|
||||
return $this->command;
|
||||
}
|
||||
|
||||
|
||||
final public function getConnection(): Connection
|
||||
{
|
||||
return $this->connection;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Adds Result setup.
|
||||
*/
|
||||
public function setupResult(string $method): self
|
||||
{
|
||||
$this->setups[] = func_get_args();
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/********************* executing ****************d*g**/
|
||||
|
||||
|
||||
/**
|
||||
* Generates and executes SQL query.
|
||||
* @return Result|int|null result set or number of affected rows
|
||||
* @throws Exception
|
||||
*/
|
||||
public function execute(?string $return = null)
|
||||
{
|
||||
$res = $this->query($this->_export());
|
||||
switch ($return) {
|
||||
case \dibi::IDENTIFIER:
|
||||
return $this->connection->getInsertId();
|
||||
case \dibi::AFFECTED_ROWS:
|
||||
return $this->connection->getAffectedRows();
|
||||
default:
|
||||
return $res;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Generates, executes SQL query and fetches the single row.
|
||||
* @return Row|array|null
|
||||
*/
|
||||
public function fetch()
|
||||
{
|
||||
return $this->command === 'SELECT' && !$this->clauses['LIMIT']
|
||||
? $this->query($this->_export(null, ['%lmt', 1]))->fetch()
|
||||
: $this->query($this->_export())->fetch();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Like fetch(), but returns only first field.
|
||||
* @return mixed value on success, null if no next record
|
||||
*/
|
||||
public function fetchSingle()
|
||||
{
|
||||
return $this->command === 'SELECT' && !$this->clauses['LIMIT']
|
||||
? $this->query($this->_export(null, ['%lmt', 1]))->fetchSingle()
|
||||
: $this->query($this->_export())->fetchSingle();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Fetches all records from table.
|
||||
*/
|
||||
public function fetchAll(?int $offset = null, ?int $limit = null): array
|
||||
{
|
||||
return $this->query($this->_export(null, ['%ofs %lmt', $offset, $limit]))->fetchAll();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Fetches all records from table and returns associative tree.
|
||||
* @param string $assoc associative descriptor
|
||||
*/
|
||||
public function fetchAssoc(string $assoc): array
|
||||
{
|
||||
return $this->query($this->_export())->fetchAssoc($assoc);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Fetches all records from table like $key => $value pairs.
|
||||
*/
|
||||
public function fetchPairs(?string $key = null, ?string $value = null): array
|
||||
{
|
||||
return $this->query($this->_export())->fetchPairs($key, $value);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Required by the IteratorAggregate interface.
|
||||
*/
|
||||
public function getIterator(?int $offset = null, ?int $limit = null): ResultIterator
|
||||
{
|
||||
return $this->query($this->_export(null, ['%ofs %lmt', $offset, $limit]))->getIterator();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Generates and prints SQL query or it's part.
|
||||
*/
|
||||
public function test(?string $clause = null): bool
|
||||
{
|
||||
return $this->connection->test($this->_export($clause));
|
||||
}
|
||||
|
||||
|
||||
public function count(): int
|
||||
{
|
||||
return Helpers::intVal($this->query([
|
||||
'SELECT COUNT(*) FROM (%ex', $this->_export(), ') [data]',
|
||||
])->fetchSingle());
|
||||
}
|
||||
|
||||
|
||||
private function query($args): Result
|
||||
{
|
||||
$res = $this->connection->query($args);
|
||||
foreach ($this->setups as $setup) {
|
||||
$method = array_shift($setup);
|
||||
$res->$method(...$setup);
|
||||
}
|
||||
|
||||
return $res;
|
||||
}
|
||||
|
||||
|
||||
/********************* exporting ****************d*g**/
|
||||
|
||||
|
||||
public function toDataSource(): DataSource
|
||||
{
|
||||
return new DataSource($this->connection->translate($this->_export()), $this->connection);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns SQL query.
|
||||
*/
|
||||
final public function __toString(): string
|
||||
{
|
||||
try {
|
||||
return $this->connection->translate($this->_export());
|
||||
} catch (\Throwable $e) {
|
||||
trigger_error($e->getMessage(), E_USER_ERROR);
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Generates parameters for Translator.
|
||||
*/
|
||||
protected function _export(?string $clause = null, array $args = []): array
|
||||
{
|
||||
if ($clause === null) {
|
||||
$data = $this->clauses;
|
||||
if ($this->command === 'SELECT' && ($data['LIMIT'] || $data['OFFSET'])) {
|
||||
$args = array_merge(['%lmt %ofs', $data['LIMIT'][0] ?? null, $data['OFFSET'][0] ?? null], $args);
|
||||
unset($data['LIMIT'], $data['OFFSET']);
|
||||
}
|
||||
} else {
|
||||
$clause = self::$normalizer->$clause;
|
||||
if (array_key_exists($clause, $this->clauses)) {
|
||||
$data = [$clause => $this->clauses[$clause]];
|
||||
} else {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($data as $clause => $statement) {
|
||||
if ($statement !== null) {
|
||||
$args[] = $clause;
|
||||
if ($clause === $this->command && $this->flags) {
|
||||
$args[] = implode(' ', array_keys($this->flags));
|
||||
}
|
||||
|
||||
foreach ($statement as $arg) {
|
||||
$args[] = $arg;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $args;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Format camelCase clause name to UPPER CASE.
|
||||
* @internal
|
||||
*/
|
||||
public static function _formatClause(string $s): string
|
||||
{
|
||||
if ($s === 'order' || $s === 'group') {
|
||||
$s .= 'By';
|
||||
trigger_error("Did you mean '$s'?", E_USER_NOTICE);
|
||||
}
|
||||
|
||||
return strtoupper(preg_replace('#[a-z](?=[A-Z])#', '$0 ', $s));
|
||||
}
|
||||
|
||||
|
||||
public function __clone()
|
||||
{
|
||||
// remove references
|
||||
foreach ($this->clauses as $clause => $val) {
|
||||
$this->clauses[$clause] = &$val;
|
||||
unset($val);
|
||||
}
|
||||
|
||||
$this->cursor = &$foo;
|
||||
}
|
||||
}
|
@@ -1,67 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Dibi;
|
||||
|
||||
|
||||
/**
|
||||
* Lazy cached storage.
|
||||
* @internal
|
||||
*/
|
||||
abstract class HashMapBase
|
||||
{
|
||||
/** @var callable */
|
||||
private $callback;
|
||||
|
||||
|
||||
public function __construct(callable $callback)
|
||||
{
|
||||
$this->callback = $callback;
|
||||
}
|
||||
|
||||
|
||||
public function setCallback(callable $callback): void
|
||||
{
|
||||
$this->callback = $callback;
|
||||
}
|
||||
|
||||
|
||||
public function getCallback(): callable
|
||||
{
|
||||
return $this->callback;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Lazy cached storage.
|
||||
* @internal
|
||||
*/
|
||||
final class HashMap extends HashMapBase
|
||||
{
|
||||
public function __set(string $nm, $val)
|
||||
{
|
||||
if ($nm === '') {
|
||||
$nm = "\xFF";
|
||||
}
|
||||
|
||||
$this->$nm = $val;
|
||||
}
|
||||
|
||||
|
||||
public function __get(string $nm)
|
||||
{
|
||||
if ($nm === '') {
|
||||
$nm = "\xFF";
|
||||
return isset($this->$nm) && true ? $this->$nm : $this->$nm = $this->getCallback()('');
|
||||
} else {
|
||||
return $this->$nm = $this->getCallback()($nm);
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,310 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Dibi;
|
||||
|
||||
|
||||
class Helpers
|
||||
{
|
||||
use Strict;
|
||||
|
||||
/** @var HashMap */
|
||||
private static $types;
|
||||
|
||||
|
||||
/**
|
||||
* Prints out a syntax highlighted version of the SQL command or Result.
|
||||
* @param string|Result $sql
|
||||
*/
|
||||
public static function dump($sql = null, bool $return = false): ?string
|
||||
{
|
||||
ob_start();
|
||||
if ($sql instanceof Result && PHP_SAPI === 'cli') {
|
||||
$hasColors = (substr((string) 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((string) $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((string) $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((string) getenv('TERM'), 0, 5) === 'xterm') {
|
||||
$sql = preg_replace_callback($highlighter, function (array $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 (array $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();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Finds the best suggestion.
|
||||
* @internal
|
||||
*/
|
||||
public static function getSuggestion(array $items, string $value): ?string
|
||||
{
|
||||
$best = null;
|
||||
$min = (strlen($value) / 4 + 1) * 10 + .1;
|
||||
$items = array_map('strval', $items);
|
||||
foreach (array_unique($items) as $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, string $type): string
|
||||
{
|
||||
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.
|
||||
* @internal
|
||||
*/
|
||||
public static function detectType(string $type): ?string
|
||||
{
|
||||
static $patterns = [
|
||||
'^_' => Type::TEXT, // PostgreSQL arrays
|
||||
'RANGE$' => Type::TEXT, // PostgreSQL range types
|
||||
'BYTEA|BLOB|BIN' => Type::BINARY,
|
||||
'TEXT|CHAR|POINT|INTERVAL|STRING' => 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,
|
||||
'JSON' => Type::JSON,
|
||||
];
|
||||
|
||||
foreach ($patterns as $s => $val) {
|
||||
if (preg_match("#$s#i", $type)) {
|
||||
return $val;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/** @internal */
|
||||
public static function getTypeCache(): HashMap
|
||||
{
|
||||
if (self::$types === null) {
|
||||
self::$types = new HashMap([self::class, 'detectType']);
|
||||
}
|
||||
|
||||
return self::$types;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Apply configuration alias or default values.
|
||||
*/
|
||||
public static function alias(array &$config, string $key, string $alias): void
|
||||
{
|
||||
$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, string $file, ?callable $onProgress = null): int
|
||||
{
|
||||
@set_time_limit(0); // intentionally @
|
||||
|
||||
$handle = @fopen($file, 'r'); // intentionally @
|
||||
if (!$handle) {
|
||||
throw new \RuntimeException("Cannot open file '$file'.");
|
||||
}
|
||||
|
||||
$stat = fstat($handle);
|
||||
$count = $size = 0;
|
||||
$delimiter = ';';
|
||||
$sql = '';
|
||||
$driver = $connection->getDriver();
|
||||
while (($s = fgets($handle)) !== false) {
|
||||
$size += strlen($s);
|
||||
if (strtoupper(substr($s, 0, 10)) === 'DELIMITER ') {
|
||||
$delimiter = trim(substr($s, 10));
|
||||
|
||||
} elseif (substr($ts = rtrim($s), -strlen($delimiter)) === $delimiter) {
|
||||
$sql .= substr($ts, 0, -strlen($delimiter));
|
||||
$driver->query($sql);
|
||||
$sql = '';
|
||||
$count++;
|
||||
if ($onProgress) {
|
||||
$onProgress($count, isset($stat['size']) ? $size * 100 / $stat['size'] : null);
|
||||
}
|
||||
} else {
|
||||
$sql .= $s;
|
||||
}
|
||||
}
|
||||
|
||||
if (rtrim($sql) !== '') {
|
||||
$driver->query($sql);
|
||||
$count++;
|
||||
if ($onProgress) {
|
||||
$onProgress($count, isset($stat['size']) ? 100 : null);
|
||||
}
|
||||
}
|
||||
|
||||
fclose($handle);
|
||||
return $count;
|
||||
}
|
||||
|
||||
|
||||
/** @internal */
|
||||
public static function false2Null($val)
|
||||
{
|
||||
return $val === false ? null : $val;
|
||||
}
|
||||
|
||||
|
||||
/** @internal */
|
||||
public static function intVal($value): int
|
||||
{
|
||||
if (is_int($value)) {
|
||||
return $value;
|
||||
} elseif (is_string($value) && preg_match('#-?\d++\z#A', $value)) {
|
||||
if (is_float($value * 1)) {
|
||||
throw new Exception("Number $value is greater than integer.");
|
||||
}
|
||||
|
||||
return (int) $value;
|
||||
} else {
|
||||
throw new Exception("Expected number, '$value' given.");
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,34 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Dibi;
|
||||
|
||||
|
||||
/**
|
||||
* SQL literal value.
|
||||
*/
|
||||
class Literal
|
||||
{
|
||||
use Strict;
|
||||
|
||||
/** @var string */
|
||||
private $value;
|
||||
|
||||
|
||||
public function __construct($value)
|
||||
{
|
||||
$this->value = (string) $value;
|
||||
}
|
||||
|
||||
|
||||
public function __toString(): string
|
||||
{
|
||||
return $this->value;
|
||||
}
|
||||
}
|
@@ -1,84 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Dibi\Loggers;
|
||||
|
||||
use Dibi;
|
||||
|
||||
|
||||
/**
|
||||
* Dibi file logger.
|
||||
*/
|
||||
class FileLogger
|
||||
{
|
||||
use Dibi\Strict;
|
||||
|
||||
/** @var string Name of the file where SQL errors should be logged */
|
||||
public $file;
|
||||
|
||||
/** @var int */
|
||||
public $filter;
|
||||
|
||||
/** @var bool */
|
||||
private $errorsOnly;
|
||||
|
||||
|
||||
public function __construct(string $file, ?int $filter = null, bool $errorsOnly = false)
|
||||
{
|
||||
$this->file = $file;
|
||||
$this->filter = $filter ?: Dibi\Event::QUERY;
|
||||
$this->errorsOnly = $errorsOnly;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* After event notification.
|
||||
*/
|
||||
public function logEvent(Dibi\Event $event): void
|
||||
{
|
||||
if (
|
||||
(($event->type & $this->filter) === 0)
|
||||
|| ($this->errorsOnly === true && !$event->result instanceof \Exception)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($event->result instanceof \Exception) {
|
||||
$message = $event->result->getMessage();
|
||||
if ($code = $event->result->getCode()) {
|
||||
$message = "[$code] $message";
|
||||
}
|
||||
|
||||
$this->writeToFile(
|
||||
$event,
|
||||
"ERROR: $message"
|
||||
. "\n-- SQL: " . $event->sql
|
||||
);
|
||||
} else {
|
||||
$this->writeToFile(
|
||||
$event,
|
||||
'OK: ' . $event->sql
|
||||
. ($event->count ? ";\n-- rows: " . $event->count : '')
|
||||
. "\n-- takes: " . sprintf('%0.3f ms', $event->time * 1000)
|
||||
. "\n-- source: " . implode(':', $event->source)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private function writeToFile(Dibi\Event $event, string $message): void
|
||||
{
|
||||
$driver = $event->connection->getConfig('driver');
|
||||
$message .=
|
||||
"\n-- driver: " . (is_object($driver) ? get_class($driver) : $driver) . '/' . $event->connection->getConfig('name')
|
||||
. "\n-- " . date('Y-m-d H:i:s')
|
||||
. "\n\n";
|
||||
file_put_contents($this->file, $message, FILE_APPEND | LOCK_EX);
|
||||
}
|
||||
}
|
@@ -1,124 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Dibi\Reflection;
|
||||
|
||||
use Dibi;
|
||||
|
||||
|
||||
/**
|
||||
* 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|null $size
|
||||
* @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, array $info)
|
||||
{
|
||||
$this->reflector = $reflector;
|
||||
$this->info = $info;
|
||||
}
|
||||
|
||||
|
||||
public function getName(): string
|
||||
{
|
||||
return $this->info['name'];
|
||||
}
|
||||
|
||||
|
||||
public function getFullName(): string
|
||||
{
|
||||
return $this->info['fullname'] ?? null;
|
||||
}
|
||||
|
||||
|
||||
public function hasTable(): bool
|
||||
{
|
||||
return !empty($this->info['table']);
|
||||
}
|
||||
|
||||
|
||||
public function getTable(): Table
|
||||
{
|
||||
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']]);
|
||||
}
|
||||
|
||||
|
||||
public function getTableName(): ?string
|
||||
{
|
||||
return isset($this->info['table']) && $this->info['table'] != null // intentionally ==
|
||||
? $this->info['table']
|
||||
: null;
|
||||
}
|
||||
|
||||
|
||||
public function getType(): ?string
|
||||
{
|
||||
return Dibi\Helpers::getTypeCache()->{$this->info['nativetype']};
|
||||
}
|
||||
|
||||
|
||||
public function getNativeType(): string
|
||||
{
|
||||
return $this->info['nativetype'];
|
||||
}
|
||||
|
||||
|
||||
public function getSize(): ?int
|
||||
{
|
||||
return isset($this->info['size']) ? (int) $this->info['size'] : null;
|
||||
}
|
||||
|
||||
|
||||
public function isNullable(): bool
|
||||
{
|
||||
return !empty($this->info['nullable']);
|
||||
}
|
||||
|
||||
|
||||
public function isAutoIncrement(): bool
|
||||
{
|
||||
return !empty($this->info['autoincrement']);
|
||||
}
|
||||
|
||||
|
||||
/** @return mixed */
|
||||
public function getDefault()
|
||||
{
|
||||
return $this->info['default'] ?? null;
|
||||
}
|
||||
|
||||
|
||||
/** @return mixed */
|
||||
public function getVendorInfo(string $key)
|
||||
{
|
||||
return $this->info['vendor'][$key] ?? null;
|
||||
}
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user