Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"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",
"homepage": "https://dibi.nette.org",
"license": ["BSD-3-Clause", "GPL-2.0-only", "GPL-3.0-only"],
"authors": [
{
Expand Down Expand Up @@ -36,7 +36,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "5.1-dev"
"dev-master": "6.0-dev"
}
}
}
4 changes: 2 additions & 2 deletions contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ 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).
on [Dibi forum](https://forum.dibi.nette.org).

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.
Expand All @@ -24,7 +24,7 @@ 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.
The best way to propose a feature is to discuss your ideas on [Dibi forum](https://forum.dibi.nette.org) before implementing them.

Please do not fix whitespace, format code, or make a purely cosmetic patch.

Expand Down
6 changes: 3 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[Dibi](https://dibiphp.com) - smart database layer for PHP [![Buy me a coffee](https://files.nette.org/images/coffee1s.png)](https://nette.org/make-donation?to=dibi)
[Dibi](https://dibi.nette.org) - smart database layer for PHP [![Buy me a coffee](https://files.nette.org/images/coffee1s.png)](https://nette.org/make-donation?to=dibi)
=========================================================

[![Downloads this Month](https://img.shields.io/packagist/dm/dibi/dibi.svg)](https://packagist.org/packages/dibi/dibi)
Expand Down Expand Up @@ -34,14 +34,14 @@ Install Dibi via Composer:
composer require dibi/dibi
```

The Dibi 5.1 requires PHP version 8.2 and supports PHP up to 8.5.
The Dibi 6.0 requires PHP version 8.2 and supports PHP up to 8.5.


Usage
-----

Refer to the `examples` directory for examples. Dibi documentation is
available on the [homepage](https://dibiphp.com).
available on the [homepage](https://dibi.nette.org).


### Connecting to database
Expand Down
2 changes: 1 addition & 1 deletion src/Dibi/Bridges/Nette/DibiExtension22.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/**
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
* This file is part of the Dibi, smart database abstraction layer (https://dibi.nette.org)
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/

Expand Down
2 changes: 1 addition & 1 deletion src/Dibi/Bridges/Nette/DibiExtension3.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/**
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
* This file is part of the Dibi, smart database abstraction layer (https://dibi.nette.org)
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/

Expand Down
2 changes: 1 addition & 1 deletion src/Dibi/Bridges/Tracy/Panel.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/**
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
* This file is part of the Dibi, smart database abstraction layer (https://dibi.nette.org)
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/

Expand Down
43 changes: 32 additions & 11 deletions src/Dibi/Connection.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/**
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
* This file is part of the Dibi, smart database abstraction layer (https://dibi.nette.org)
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/

Expand All @@ -21,15 +21,28 @@
* @property-read int $affectedRows
* @property-read int $insertId
*/
class Connection implements IConnection
class Connection
{
private const Drivers = [
'firebird' => Drivers\Ibase\Connection::class,
'mysqli' => Drivers\MySQLi\Connection::class,
'odbc' => Drivers\ODBC\Connection::class,
'oracle' => Drivers\OCI8\Connection::class,
'pdo' => Drivers\PDO\Connection::class,
'postgre' => Drivers\PgSQL\Connection::class,
'sqlite3' => Drivers\SQLite3\Connection::class,
'sqlite' => Drivers\SQLite3\Connection::class,
'sqlsrv' => Drivers\SQLSrv\Connection::class,
];

/** function (Event $event); Occurs after query is executed */
public ?array $onEvent = [];
private array $config;

/** @var string[] resultset formats */
private array $formats;
private ?Driver $driver = null;
private ?Drivers\Connection $driver = null;
private Drivers\Engine $engine;
private ?Translator $translator = null;

/** @var array<string, callable(object): Expression | null> */
Expand Down Expand Up @@ -122,17 +135,16 @@ public function __destruct()
*/
final public function connect(): void
{
if ($this->config['driver'] instanceof Driver) {
if ($this->config['driver'] instanceof Drivers\Connection) {
$this->driver = $this->config['driver'];
$this->translator = new Translator($this);
return;

} elseif (is_subclass_of($this->config['driver'], Driver::class)) {
} elseif (is_subclass_of($this->config['driver'], Drivers\Connection::class)) {
$class = $this->config['driver'];

} else {
$class = preg_replace(['#\W#', '#sql#'], ['_', 'Sql'], ucfirst(strtolower($this->config['driver'])));
$class = "Dibi\\Drivers\\{$class}Driver";
$class = self::Drivers[strtolower($this->config['driver'])] ?? throw new Exception("Unknown driver '{$this->config['driver']}'.");
if (!class_exists($class)) {
throw new Exception("Unable to create instance of Dibi driver '$class'.");
}
Expand Down Expand Up @@ -198,7 +210,7 @@ final public function getConfig(?string $key = null, $default = null): mixed
/**
* Returns the driver and connects to a database in lazy mode.
*/
final public function getDriver(): Driver
final public function getDriver(): Drivers\Connection
{
if (!$this->driver) {
$this->connect();
Expand Down Expand Up @@ -286,7 +298,7 @@ final public function nativeQuery(#[Language('SQL')] string $sql): Result
throw $e;
}

$res = $this->createResultSet($res ?: new Drivers\NoDataResult(max(0, $this->driver->getAffectedRows())));
$res = $this->createResultSet($res ?: new Drivers\Dummy\Result(max(0, $this->driver->getAffectedRows())));
if ($event) {
$this->onEvent($event->done($res));
}
Expand Down Expand Up @@ -450,7 +462,7 @@ public function transaction(callable $callback): mixed
/**
* Result set factory.
*/
public function createResultSet(ResultDriver $resultDriver): Result
public function createResultSet(Drivers\Result $resultDriver): Result
{
return (new Result($resultDriver, $this->config['result']['normalize'] ?? true))
->setFormats($this->formats);
Expand Down Expand Up @@ -659,6 +671,15 @@ public function loadFile(string $file, ?callable $onProgress = null): int
}


public function getDatabaseEngine(): Drivers\Engine
{
if (!$this->driver) { // TODO
$this->connect();
}
return $this->engine ??= $this->driver->getReflector();
}


/**
* Gets a information about the current database.
*/
Expand All @@ -668,7 +689,7 @@ public function getDatabaseInfo(): Reflection\Database
$this->connect();
}

return new Reflection\Database($this->driver->getReflector(), $this->config['database'] ?? null);
return new Reflection\Database($this->getDatabaseEngine(), $this->config['database'] ?? null);
}


Expand Down
4 changes: 2 additions & 2 deletions src/Dibi/DataSource.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/**
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
* This file is part of the Dibi, smart database abstraction layer (https://dibi.nette.org)
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/

Expand Down Expand Up @@ -35,7 +35,7 @@ class DataSource implements IDataSource
public function __construct(string $sql, Connection $connection)
{
$this->sql = strpbrk($sql, " \t\r\n") === false
? $connection->getDriver()->escapeIdentifier($sql) // table name
? $connection->getDatabaseEngine()->escapeIdentifier($sql) // table name
: '(' . $sql . ') t'; // SQL command
$this->connection = $connection;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Dibi/DateTime.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/**
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
* This file is part of the Dibi, smart database abstraction layer (https://dibi.nette.org)
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/

Expand Down
77 changes: 77 additions & 0 deletions src/Dibi/Drivers/Connection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

/**
* This file is part of the Dibi, smart database abstraction layer (https://dibi.nette.org)
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/

declare(strict_types=1);

namespace Dibi\Drivers;

use Dibi\DriverException;
use Dibi\Exception;


/**
* Database connection driver.
*/
interface Connection
{
/**
* Disconnects from a database.
* @throws Exception
*/
function disconnect(): void;

/**
* Internal: Executes the SQL query.
* @throws DriverException
*/
function query(string $sql): ?Result;

/**
* Gets the number of affected rows by the last INSERT, UPDATE or DELETE query.
*/
function getAffectedRows(): ?int;

/**
* Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query.
*/
function getInsertId(?string $sequence): ?int;

/**
* Begins a transaction (if supported).
* @throws DriverException
*/
function begin(?string $savepoint = null): void;

/**
* Commits statements in a transaction.
* @throws DriverException
*/
function commit(?string $savepoint = null): void;

/**
* Rollback changes in a transaction.
* @throws DriverException
*/
function rollback(?string $savepoint = null): void;

/**
* Returns the connection resource.
*/
function getResource(): mixed;

/**
* Returns the connection reflector.
*/
function getReflector(): Engine;

/**
* Encodes data for use in a SQL statement.
*/
function escapeText(string $value): string;

function escapeBinary(string $value): string;
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
<?php

/**
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
* This file is part of the Dibi, smart database abstraction layer (https://dibi.nette.org)
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/

declare(strict_types=1);

namespace Dibi\Drivers;
namespace Dibi\Drivers\Dummy;

use Dibi;
use Dibi\Drivers;


/**
* The dummy driver for testing purposes.
*/
class DummyDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
class Connection implements Drivers\Connection, Drivers\Result, Drivers\Engine
{
public function disconnect(): void
{
}


public function query(string $sql): ?Dibi\ResultDriver
public function query(string $sql): ?Result
{
return null;
}
Expand Down Expand Up @@ -64,7 +65,7 @@ public function getResource(): mixed
/**
* Returns the connection reflector.
*/
public function getReflector(): Dibi\Reflector
public function getReflector(): Drivers\Engine
{
return $this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
<?php

/**
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
* This file is part of the Dibi, smart database abstraction layer (https://dibi.nette.org)
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/

declare(strict_types=1);

namespace Dibi\Drivers;
namespace Dibi\Drivers\Dummy;

use Dibi;
use Dibi\Drivers;


/**
* The driver for no result set.
*/
class NoDataResult implements Dibi\ResultDriver
class Result implements Drivers\Result
{
public function __construct(
private readonly int $rows,
Expand Down
Loading
Loading