1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-13 17:44:11 +02:00

added PHP 7.1 scalar and return type hints

This commit is contained in:
David Grudl
2017-06-09 21:46:11 +02:00
parent 7d42317279
commit 859eada4e7
37 changed files with 499 additions and 1333 deletions

View File

@@ -33,7 +33,7 @@ class Panel implements Tracy\IBarPanel
private $events = []; private $events = [];
public function __construct($explain = TRUE, $filter = NULL) public function __construct(bool $explain = TRUE, int $filter = NULL)
{ {
$this->filter = $filter ? (int) $filter : Event::QUERY; $this->filter = $filter ? (int) $filter : Event::QUERY;
$this->explain = $explain; $this->explain = $explain;
@@ -50,9 +50,8 @@ class Panel implements Tracy\IBarPanel
/** /**
* After event notification. * After event notification.
* @return void
*/ */
public function logEvent(Event $event) public function logEvent(Event $event): void
{ {
if (($event->type & $this->filter) === 0) { if (($event->type & $this->filter) === 0) {
return; return;
@@ -63,9 +62,8 @@ class Panel implements Tracy\IBarPanel
/** /**
* Returns blue-screen custom tab. * Returns blue-screen custom tab.
* @return array|NULL
*/ */
public static function renderException($e) public static function renderException($e): ?array
{ {
if ($e instanceof Dibi\Exception && $e->getSql()) { if ($e instanceof Dibi\Exception && $e->getSql()) {
return [ return [
@@ -78,9 +76,8 @@ class Panel implements Tracy\IBarPanel
/** /**
* Returns HTML code for custom tab. (Tracy\IBarPanel) * Returns HTML code for custom tab. (Tracy\IBarPanel)
* @return string
*/ */
public function getTab() public function getTab(): string
{ {
$totalTime = 0; $totalTime = 0;
$count = count($this->events); $count = count($this->events);
@@ -96,9 +93,8 @@ class Panel implements Tracy\IBarPanel
/** /**
* Returns HTML code for custom panel. (Tracy\IBarPanel) * Returns HTML code for custom panel. (Tracy\IBarPanel)
* @return string|NULL
*/ */
public function getPanel() public function getPanel(): ?string
{ {
if (!$this->events) { if (!$this->events) {
return NULL; return NULL;

View File

@@ -53,7 +53,7 @@ class Connection
* @param string connection name * @param string connection name
* @throws Exception * @throws Exception
*/ */
public function __construct($config, $name = NULL) public function __construct($config, string $name = NULL)
{ {
if (is_string($config)) { if (is_string($config)) {
parse_str($config, $config); parse_str($config, $config);
@@ -128,7 +128,6 @@ class Connection
/** /**
* Automatically frees the resources allocated for this result set. * Automatically frees the resources allocated for this result set.
* @return void
*/ */
public function __destruct() public function __destruct()
{ {
@@ -139,9 +138,8 @@ class Connection
/** /**
* Connects to a database. * Connects to a database.
* @return void
*/ */
final public function connect() final public function connect(): void
{ {
$event = $this->onEvent ? new Event($this, Event::CONNECT) : NULL; $event = $this->onEvent ? new Event($this, Event::CONNECT) : NULL;
try { try {
@@ -158,9 +156,8 @@ class Connection
/** /**
* Disconnects from a database. * Disconnects from a database.
* @return void
*/ */
final public function disconnect() final public function disconnect(): void
{ {
$this->driver->disconnect(); $this->driver->disconnect();
$this->connected = FALSE; $this->connected = FALSE;
@@ -169,9 +166,8 @@ class Connection
/** /**
* Returns TRUE when connection was established. * Returns TRUE when connection was established.
* @return bool
*/ */
final public function isConnected() final public function isConnected(): bool
{ {
return $this->connected; return $this->connected;
} }
@@ -180,11 +176,10 @@ class Connection
/** /**
* Returns configuration variable. If no $key is passed, returns the entire array. * Returns configuration variable. If no $key is passed, returns the entire array.
* @see self::__construct * @see self::__construct
* @param string * @param mixed $default default value to use if key not found
* @param mixed default value to use if key not found
* @return mixed * @return mixed
*/ */
final public function getConfig($key = NULL, $default = NULL) final public function getConfig(string $key = NULL, $default = NULL)
{ {
return $key === NULL return $key === NULL
? $this->config ? $this->config
@@ -194,9 +189,8 @@ class Connection
/** /**
* Returns the driver and connects to a database in lazy mode. * Returns the driver and connects to a database in lazy mode.
* @return Driver
*/ */
final public function getDriver() final public function getDriver(): Driver
{ {
$this->connected || $this->connect(); $this->connected || $this->connect();
return $this->driver; return $this->driver;
@@ -218,10 +212,9 @@ class Connection
/** /**
* Generates SQL query. * Generates SQL query.
* @param mixed one or more arguments * @param mixed one or more arguments
* @return string
* @throws Exception * @throws Exception
*/ */
final public function translate(...$args) final public function translate(...$args): string
{ {
return $this->translateArgs($args); return $this->translateArgs($args);
} }
@@ -230,9 +223,8 @@ class Connection
/** /**
* Generates and prints SQL query. * Generates and prints SQL query.
* @param mixed one or more arguments * @param mixed one or more arguments
* @return bool
*/ */
final public function test(...$args) final public function test(...$args): bool
{ {
try { try {
Helpers::dump($this->translateArgs($args)); Helpers::dump($this->translateArgs($args));
@@ -252,10 +244,9 @@ class Connection
/** /**
* Generates (translates) and returns SQL query as DataSource. * Generates (translates) and returns SQL query as DataSource.
* @param mixed one or more arguments * @param mixed one or more arguments
* @return DataSource
* @throws Exception * @throws Exception
*/ */
final public function dataSource(...$args) final public function dataSource(...$args): DataSource
{ {
return new DataSource($this->translateArgs($args), $this); return new DataSource($this->translateArgs($args), $this);
} }
@@ -263,10 +254,8 @@ class Connection
/** /**
* Generates SQL query. * Generates SQL query.
* @param array
* @return string
*/ */
protected function translateArgs($args) protected function translateArgs(array $args): string
{ {
$this->connected || $this->connect(); $this->connected || $this->connect();
if (!$this->translator) { if (!$this->translator) {
@@ -283,7 +272,7 @@ class Connection
* @return Result|int result set or number of affected rows * @return Result|int result set or number of affected rows
* @throws Exception * @throws Exception
*/ */
final public function nativeQuery($sql) final public function nativeQuery(string $sql)
{ {
$this->connected || $this->connect(); $this->connected || $this->connect();
@@ -313,7 +302,7 @@ class Connection
* @return int number of rows * @return int number of rows
* @throws Exception * @throws Exception
*/ */
public function getAffectedRows() public function getAffectedRows(): int
{ {
$this->connected || $this->connect(); $this->connected || $this->connect();
$rows = $this->driver->getAffectedRows(); $rows = $this->driver->getAffectedRows();
@@ -329,7 +318,7 @@ class Connection
* @return int number of rows * @return int number of rows
* @throws Exception * @throws Exception
*/ */
public function affectedRows() public function affectedRows(): int
{ {
return $this->getAffectedRows(); return $this->getAffectedRows();
} }
@@ -338,10 +327,9 @@ class Connection
/** /**
* Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query. * Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query.
* @param string optional sequence name * @param string optional sequence name
* @return int
* @throws Exception * @throws Exception
*/ */
public function getInsertId($sequence = NULL) public function getInsertId(string $sequence = NULL): int
{ {
$this->connected || $this->connect(); $this->connected || $this->connect();
$id = $this->driver->getInsertId($sequence); $id = $this->driver->getInsertId($sequence);
@@ -355,10 +343,9 @@ class Connection
/** /**
* Retrieves the ID generated for an AUTO_INCREMENT column. Alias for getInsertId(). * Retrieves the ID generated for an AUTO_INCREMENT column. Alias for getInsertId().
* @param string optional sequence name * @param string optional sequence name
* @return int
* @throws Exception * @throws Exception
*/ */
public function insertId($sequence = NULL) public function insertId(string $sequence = NULL): int
{ {
return $this->getInsertId($sequence); return $this->getInsertId($sequence);
} }
@@ -367,9 +354,8 @@ class Connection
/** /**
* Begins a transaction (if supported). * Begins a transaction (if supported).
* @param string optional savepoint name * @param string optional savepoint name
* @return void
*/ */
public function begin($savepoint = NULL) public function begin(string $savepoint = NULL): void
{ {
$this->connected || $this->connect(); $this->connected || $this->connect();
$event = $this->onEvent ? new Event($this, Event::BEGIN, $savepoint) : NULL; $event = $this->onEvent ? new Event($this, Event::BEGIN, $savepoint) : NULL;
@@ -387,9 +373,8 @@ class Connection
/** /**
* Commits statements in a transaction. * Commits statements in a transaction.
* @param string optional savepoint name * @param string optional savepoint name
* @return void
*/ */
public function commit($savepoint = NULL) public function commit(string $savepoint = NULL): void
{ {
$this->connected || $this->connect(); $this->connected || $this->connect();
$event = $this->onEvent ? new Event($this, Event::COMMIT, $savepoint) : NULL; $event = $this->onEvent ? new Event($this, Event::COMMIT, $savepoint) : NULL;
@@ -407,9 +392,8 @@ class Connection
/** /**
* Rollback changes in a transaction. * Rollback changes in a transaction.
* @param string optional savepoint name * @param string optional savepoint name
* @return void
*/ */
public function rollback($savepoint = NULL) public function rollback(string $savepoint = NULL): void
{ {
$this->connected || $this->connect(); $this->connected || $this->connect();
$event = $this->onEvent ? new Event($this, Event::ROLLBACK, $savepoint) : NULL; $event = $this->onEvent ? new Event($this, Event::ROLLBACK, $savepoint) : NULL;
@@ -426,10 +410,8 @@ class Connection
/** /**
* Result set factory. * Result set factory.
* @param ResultDriver
* @return Result
*/ */
public function createResultSet(ResultDriver $resultDriver) public function createResultSet(ResultDriver $resultDriver): Result
{ {
$res = new Result($resultDriver); $res = new Result($resultDriver);
return $res->setFormat(Type::DATE, $this->config['result']['formatDate']) return $res->setFormat(Type::DATE, $this->config['result']['formatDate'])
@@ -440,10 +422,7 @@ class Connection
/********************* fluent SQL builders ****************d*g**/ /********************* fluent SQL builders ****************d*g**/
/** public function command(): Fluent
* @return Fluent
*/
public function command()
{ {
return new Fluent($this); return new Fluent($this);
} }
@@ -451,20 +430,14 @@ class Connection
/** /**
* @param mixed column name * @param mixed column name
* @return Fluent
*/ */
public function select(...$args) public function select(...$args): Fluent
{ {
return $this->command()->select(...$args); return $this->command()->select(...$args);
} }
/** public function update(string $table, array $args): Fluent
* @param string table
* @param array
* @return Fluent
*/
public function update($table, $args)
{ {
if (!(is_array($args) || $args instanceof Traversable)) { if (!(is_array($args) || $args instanceof Traversable)) {
throw new \InvalidArgumentException('Arguments must be array or Traversable.'); throw new \InvalidArgumentException('Arguments must be array or Traversable.');
@@ -473,12 +446,7 @@ class Connection
} }
/** public function insert(string $table, array $args): Fluent
* @param string table
* @param array
* @return Fluent
*/
public function insert($table, $args)
{ {
if ($args instanceof Traversable) { if ($args instanceof Traversable) {
$args = iterator_to_array($args); $args = iterator_to_array($args);
@@ -490,11 +458,7 @@ class Connection
} }
/** public function delete(string $table): Fluent
* @param string table
* @return Fluent
*/
public function delete($table)
{ {
return $this->command()->delete()->from('%n', $table); return $this->command()->delete()->from('%n', $table);
} }
@@ -505,9 +469,8 @@ class Connection
/** /**
* Returns substitution hashmap. * Returns substitution hashmap.
* @return HashMap
*/ */
public function getSubstitutes() public function getSubstitutes(): HashMap
{ {
return $this->substitutes; return $this->substitutes;
} }
@@ -515,9 +478,8 @@ class Connection
/** /**
* Provides substitution. * Provides substitution.
* @return string
*/ */
public function substitute($value) public function substitute(string $value): string
{ {
return strpos($value, ':') === FALSE return strpos($value, ':') === FALSE
? $value ? $value
@@ -546,7 +508,7 @@ class Connection
* @return Row[] * @return Row[]
* @throws Exception * @throws Exception
*/ */
public function fetchAll(...$args) public function fetchAll(...$args): array
{ {
return $this->query($args)->fetchAll(); return $this->query($args)->fetchAll();
} }
@@ -567,19 +529,15 @@ class Connection
/** /**
* Executes SQL query and fetch pairs - shortcut for query() & fetchPairs(). * Executes SQL query and fetch pairs - shortcut for query() & fetchPairs().
* @param mixed one or more arguments * @param mixed one or more arguments
* @return array
* @throws Exception * @throws Exception
*/ */
public function fetchPairs(...$args) public function fetchPairs(...$args): array
{ {
return $this->query($args)->fetchPairs(); return $this->query($args)->fetchPairs();
} }
/** public static function literal($value): Literal
* @return Literal
*/
public static function literal($value)
{ {
return new Literal($value); return new Literal($value);
} }
@@ -594,7 +552,7 @@ class Connection
* @param callable function (int $count, ?float $percent): void * @param callable function (int $count, ?float $percent): void
* @return int count of sql commands * @return int count of sql commands
*/ */
public function loadFile($file, callable $onProgress = NULL) public function loadFile(string $file, callable $onProgress = NULL): int
{ {
return Helpers::loadFromFile($this, $file, $onProgress); return Helpers::loadFromFile($this, $file, $onProgress);
} }
@@ -602,9 +560,8 @@ class Connection
/** /**
* Gets a information about the current database. * Gets a information about the current database.
* @return Reflection\Database
*/ */
public function getDatabaseInfo() public function getDatabaseInfo(): Reflection\Database
{ {
$this->connected || $this->connect(); $this->connected || $this->connect();
return new Reflection\Database($this->driver->getReflector(), $this->config['database'] ?? NULL); return new Reflection\Database($this->driver->getReflector(), $this->config['database'] ?? NULL);

View File

@@ -49,9 +49,8 @@ class DataSource implements IDataSource
/** /**
* @param string SQL command or table or view name, as data source * @param string SQL command or table or view name, as data source
* @param Connection connection
*/ */
public function __construct($sql, Connection $connection) public function __construct(string $sql, Connection $connection)
{ {
if (strpbrk($sql, " \t\r\n") === FALSE) { if (strpbrk($sql, " \t\r\n") === FALSE) {
$this->sql = $connection->getDriver()->escapeIdentifier($sql); // table name $this->sql = $connection->getDriver()->escapeIdentifier($sql); // table name
@@ -66,9 +65,8 @@ class DataSource implements IDataSource
* Selects columns to query. * Selects columns to query.
* @param string|array column name or array of column names * @param string|array column name or array of column names
* @param string column alias * @param string column alias
* @return self
*/ */
public function select($col, $as = NULL) public function select($col, string $as = NULL): self
{ {
if (is_array($col)) { if (is_array($col)) {
$this->cols = $col; $this->cols = $col;
@@ -83,9 +81,8 @@ class DataSource implements IDataSource
/** /**
* Adds conditions to query. * Adds conditions to query.
* @param mixed conditions * @param mixed conditions
* @return self
*/ */
public function where($cond) public function where($cond): self
{ {
if (is_array($cond)) { if (is_array($cond)) {
// TODO: not consistent with select and orderBy // TODO: not consistent with select and orderBy
@@ -102,9 +99,8 @@ class DataSource implements IDataSource
* Selects columns to order by. * Selects columns to order by.
* @param string|array column name or array of column names * @param string|array column name or array of column names
* @param string sorting direction * @param string sorting direction
* @return self
*/ */
public function orderBy($row, $sorting = 'ASC') public function orderBy($row, string $sorting = 'ASC'): self
{ {
if (is_array($row)) { if (is_array($row)) {
$this->sorting = $row; $this->sorting = $row;
@@ -118,11 +114,8 @@ class DataSource implements IDataSource
/** /**
* Limits number of rows. * Limits number of rows.
* @param int|NULL limit
* @param int offset
* @return self
*/ */
public function applyLimit($limit, $offset = NULL) public function applyLimit(int $limit, int $offset = NULL): self
{ {
$this->limit = $limit; $this->limit = $limit;
$this->offset = $offset; $this->offset = $offset;
@@ -133,9 +126,8 @@ class DataSource implements IDataSource
/** /**
* Returns the dibi connection. * Returns the dibi connection.
* @return Connection
*/ */
final public function getConnection() final public function getConnection(): Connection
{ {
return $this->connection; return $this->connection;
} }
@@ -146,9 +138,8 @@ class DataSource implements IDataSource
/** /**
* Returns (and queries) Result. * Returns (and queries) Result.
* @return Result
*/ */
public function getResult() public function getResult(): Result
{ {
if ($this->result === NULL) { if ($this->result === NULL) {
$this->result = $this->connection->nativeQuery($this->__toString()); $this->result = $this->connection->nativeQuery($this->__toString());
@@ -157,10 +148,7 @@ class DataSource implements IDataSource
} }
/** public function getIterator(): ResultIterator
* @return ResultIterator
*/
public function getIterator()
{ {
return $this->getResult()->getIterator(); return $this->getResult()->getIterator();
} }
@@ -188,9 +176,8 @@ class DataSource implements IDataSource
/** /**
* Fetches all records from table. * Fetches all records from table.
* @return array
*/ */
public function fetchAll() public function fetchAll(): array
{ {
return $this->getResult()->fetchAll(); return $this->getResult()->fetchAll();
} }
@@ -199,9 +186,8 @@ class DataSource implements IDataSource
/** /**
* Fetches all records from table and returns associative tree. * Fetches all records from table and returns associative tree.
* @param string associative descriptor * @param string associative descriptor
* @return array
*/ */
public function fetchAssoc($assoc) public function fetchAssoc(string $assoc): array
{ {
return $this->getResult()->fetchAssoc($assoc); return $this->getResult()->fetchAssoc($assoc);
} }
@@ -210,10 +196,8 @@ class DataSource implements IDataSource
/** /**
* Fetches all records from table like $key => $value pairs. * Fetches all records from table like $key => $value pairs.
* @param string associative key * @param string associative key
* @param string value
* @return array
*/ */
public function fetchPairs($key = NULL, $value = NULL) public function fetchPairs(string $key = NULL, string $value = NULL): array
{ {
return $this->getResult()->fetchPairs($key, $value); return $this->getResult()->fetchPairs($key, $value);
} }
@@ -221,9 +205,8 @@ class DataSource implements IDataSource
/** /**
* Discards the internal cache. * Discards the internal cache.
* @return void
*/ */
public function release() public function release(): void
{ {
$this->result = $this->count = $this->totalCount = NULL; $this->result = $this->count = $this->totalCount = NULL;
} }
@@ -234,9 +217,8 @@ class DataSource implements IDataSource
/** /**
* Returns this data source wrapped in Fluent object. * Returns this data source wrapped in Fluent object.
* @return Fluent
*/ */
public function toFluent() public function toFluent(): Fluent
{ {
return $this->connection->select('*')->from('(%SQL) t', $this->__toString()); return $this->connection->select('*')->from('(%SQL) t', $this->__toString());
} }
@@ -244,9 +226,8 @@ class DataSource implements IDataSource
/** /**
* Returns this data source wrapped in DataSource object. * Returns this data source wrapped in DataSource object.
* @return DataSource
*/ */
public function toDataSource() public function toDataSource(): DataSource
{ {
return new self($this->__toString(), $this->connection); return new self($this->__toString(), $this->connection);
} }
@@ -254,9 +235,8 @@ class DataSource implements IDataSource
/** /**
* Returns SQL query. * Returns SQL query.
* @return string
*/ */
public function __toString() public function __toString(): string
{ {
try { try {
return $this->connection->translate(' return $this->connection->translate('
@@ -277,9 +257,8 @@ FROM %SQL', $this->sql, '
/** /**
* Returns the number of rows in a given data source. * Returns the number of rows in a given data source.
* @return int
*/ */
public function count() public function count(): int
{ {
if ($this->count === NULL) { if ($this->count === NULL) {
$this->count = $this->conds || $this->offset || $this->limit $this->count = $this->conds || $this->offset || $this->limit
@@ -294,9 +273,8 @@ FROM %SQL', $this->sql, '
/** /**
* Returns the number of rows in a given data source. * Returns the number of rows in a given data source.
* @return int
*/ */
public function getTotalCount() public function getTotalCount(): int
{ {
if ($this->totalCount === NULL) { if ($this->totalCount === NULL) {
$this->totalCount = (int) $this->connection->nativeQuery( $this->totalCount = (int) $this->connection->nativeQuery(

View File

@@ -31,7 +31,7 @@ class DateTime extends \DateTime
} }
public function modifyClone($modify = '') public function modifyClone(string $modify = '')
{ {
$dolly = clone($this); $dolly = clone($this);
return $modify ? $dolly->modify($modify) : $dolly; return $modify ? $dolly->modify($modify) : $dolly;

View File

@@ -58,10 +58,9 @@ class FirebirdDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Connects to a database. * Connects to a database.
* @return void
* @throws Dibi\Exception * @throws Dibi\Exception
*/ */
public function connect(array &$config) public function connect(array &$config): void
{ {
Helpers::alias($config, 'database', 'db'); Helpers::alias($config, 'database', 'db');
@@ -93,9 +92,8 @@ class FirebirdDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Disconnects from a database. * Disconnects from a database.
* @return void
*/ */
public function disconnect() public function disconnect(): void
{ {
@ibase_close($this->connection); // @ - connection can be already disconnected @ibase_close($this->connection); // @ - connection can be already disconnected
} }
@@ -104,10 +102,9 @@ class FirebirdDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Executes the SQL query. * Executes the SQL query.
* @param string SQL statement. * @param string SQL statement.
* @return Dibi\ResultDriver|NULL
* @throws Dibi\DriverException|Dibi\Exception * @throws Dibi\DriverException|Dibi\Exception
*/ */
public function query($sql) public function query(string $sql): ?Dibi\ResultDriver
{ {
$resource = $this->inTransaction ? $this->transaction : $this->connection; $resource = $this->inTransaction ? $this->transaction : $this->connection;
$res = ibase_query($resource, $sql); $res = ibase_query($resource, $sql);
@@ -130,9 +127,8 @@ class FirebirdDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Gets the number of affected rows by the last INSERT, UPDATE or DELETE query. * Gets the number of affected rows by the last INSERT, UPDATE or DELETE query.
* @return int|NULL number of rows or NULL on error
*/ */
public function getAffectedRows() public function getAffectedRows(): ?int
{ {
return Helpers::false2Null(ibase_affected_rows($this->connection)); return Helpers::false2Null(ibase_affected_rows($this->connection));
} }
@@ -140,10 +136,8 @@ class FirebirdDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query. * Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query.
* @param string generator name
* @return int|NULL int on success or NULL on failure
*/ */
public function getInsertId($sequence) public function getInsertId(?string $sequence): ?int
{ {
return Helpers::false2Null(ibase_gen_id($sequence, 0, $this->connection)); return Helpers::false2Null(ibase_gen_id($sequence, 0, $this->connection));
} }
@@ -152,10 +146,9 @@ class FirebirdDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Begins a transaction (if supported). * Begins a transaction (if supported).
* @param string optional savepoint name * @param string optional savepoint name
* @return void
* @throws Dibi\DriverException * @throws Dibi\DriverException
*/ */
public function begin($savepoint = NULL) public function begin(string $savepoint = NULL): void
{ {
if ($savepoint !== NULL) { if ($savepoint !== NULL) {
throw new Dibi\NotSupportedException('Savepoints are not supported in Firebird/Interbase.'); throw new Dibi\NotSupportedException('Savepoints are not supported in Firebird/Interbase.');
@@ -168,10 +161,9 @@ class FirebirdDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Commits statements in a transaction. * Commits statements in a transaction.
* @param string optional savepoint name * @param string optional savepoint name
* @return void
* @throws Dibi\DriverException * @throws Dibi\DriverException
*/ */
public function commit($savepoint = NULL) public function commit(string $savepoint = NULL): void
{ {
if ($savepoint !== NULL) { if ($savepoint !== NULL) {
throw new Dibi\NotSupportedException('Savepoints are not supported in Firebird/Interbase.'); throw new Dibi\NotSupportedException('Savepoints are not supported in Firebird/Interbase.');
@@ -188,10 +180,9 @@ class FirebirdDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Rollback changes in a transaction. * Rollback changes in a transaction.
* @param string optional savepoint name * @param string optional savepoint name
* @return void
* @throws Dibi\DriverException * @throws Dibi\DriverException
*/ */
public function rollback($savepoint = NULL) public function rollback(string $savepoint = NULL): void
{ {
if ($savepoint !== NULL) { if ($savepoint !== NULL) {
throw new Dibi\NotSupportedException('Savepoints are not supported in Firebird/Interbase.'); throw new Dibi\NotSupportedException('Savepoints are not supported in Firebird/Interbase.');
@@ -207,9 +198,8 @@ class FirebirdDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Is in transaction? * Is in transaction?
* @return bool
*/ */
public function inTransaction() public function inTransaction(): bool
{ {
return $this->inTransaction; return $this->inTransaction;
} }
@@ -227,9 +217,8 @@ class FirebirdDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Returns the connection reflector. * Returns the connection reflector.
* @return Dibi\Reflector
*/ */
public function getReflector() public function getReflector(): Dibi\Reflector
{ {
return $this; return $this;
} }
@@ -238,9 +227,8 @@ class FirebirdDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Result set driver factory. * Result set driver factory.
* @param resource * @param resource
* @return Dibi\ResultDriver
*/ */
public function createResultDriver($resource) public function createResultDriver($resource): Dibi\ResultDriver
{ {
$res = clone $this; $res = clone $this;
$res->resultSet = $resource; $res->resultSet = $resource;
@@ -253,40 +241,26 @@ class FirebirdDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Encodes data for use in a SQL statement. * Encodes data for use in a SQL statement.
* @param string
* @return string
*/ */
public function escapeText($value) public function escapeText(string $value): string
{ {
return "'" . str_replace("'", "''", $value) . "'"; return "'" . str_replace("'", "''", $value) . "'";
} }
/** public function escapeBinary(string $value): string
* @param string
* @return string
*/
public function escapeBinary($value)
{ {
return "'" . str_replace("'", "''", $value) . "'"; return "'" . str_replace("'", "''", $value) . "'";
} }
/** public function escapeIdentifier(string $value): string
* @param string
* @return string
*/
public function escapeIdentifier($value)
{ {
return '"' . str_replace('"', '""', $value). '"'; return '"' . str_replace('"', '""', $value). '"';
} }
/** public function escapeBool(bool $value): string
* @param bool
* @return string
*/
public function escapeBool($value)
{ {
return $value ? '1' : '0'; return $value ? '1' : '0';
} }
@@ -294,9 +268,8 @@ class FirebirdDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* @param \DateTimeInterface|string|int * @param \DateTimeInterface|string|int
* @return string
*/ */
public function escapeDate($value) public function escapeDate($value): string
{ {
if (!$value instanceof \DateTimeInterface) { if (!$value instanceof \DateTimeInterface) {
$value = new Dibi\DateTime($value); $value = new Dibi\DateTime($value);
@@ -307,9 +280,8 @@ class FirebirdDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* @param \DateTimeInterface|string|int * @param \DateTimeInterface|string|int
* @return string
*/ */
public function escapeDateTime($value) public function escapeDateTime($value): string
{ {
if (!$value instanceof \DateTimeInterface) { if (!$value instanceof \DateTimeInterface) {
$value = new Dibi\DateTime($value); $value = new Dibi\DateTime($value);
@@ -320,11 +292,8 @@ class FirebirdDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Encodes string for use in a LIKE statement. * Encodes string for use in a LIKE statement.
* @param string
* @param int
* @return string
*/ */
public function escapeLike($value, $pos) public function escapeLike(string $value, int $pos): string
{ {
throw new Dibi\NotImplementedException; throw new Dibi\NotImplementedException;
} }
@@ -332,10 +301,8 @@ class FirebirdDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Decodes data from result set. * Decodes data from result set.
* @param string
* @return string
*/ */
public function unescapeBinary($value) public function unescapeBinary(string $value): string
{ {
return $value; return $value;
} }
@@ -343,12 +310,8 @@ class FirebirdDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Injects LIMIT/OFFSET to the SQL query. * Injects LIMIT/OFFSET to the SQL query.
* @param string
* @param int|NULL
* @param int|NULL
* @return void
*/ */
public function applyLimit(&$sql, $limit, $offset) public function applyLimit(string &$sql, ?int $limit, ?int $offset): void
{ {
if ($limit > 0 || $offset > 0) { if ($limit > 0 || $offset > 0) {
// http://www.firebirdsql.org/refdocs/langrefupd20-select.html // http://www.firebirdsql.org/refdocs/langrefupd20-select.html
@@ -362,7 +325,6 @@ class FirebirdDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Automatically frees the resources allocated for this result set. * Automatically frees the resources allocated for this result set.
* @return void
*/ */
public function __destruct() public function __destruct()
{ {
@@ -372,9 +334,8 @@ class FirebirdDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Returns the number of rows in a result set. * Returns the number of rows in a result set.
* @return int
*/ */
public function getRowCount() public function getRowCount(): int
{ {
throw new Dibi\NotSupportedException('Firebird/Interbase do not support returning number of rows in result set.'); throw new Dibi\NotSupportedException('Firebird/Interbase do not support returning number of rows in result set.');
} }
@@ -383,9 +344,8 @@ class FirebirdDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Fetches the row at current position and moves the internal cursor to the next position. * Fetches the row at current position and moves the internal cursor to the next position.
* @param bool TRUE for associative array, FALSE for numeric * @param bool TRUE for associative array, FALSE for numeric
* @return array|NULL array on success, NULL if no next record
*/ */
public function fetch($assoc) public function fetch(bool $assoc): ?array
{ {
$result = $assoc ? @ibase_fetch_assoc($this->resultSet, IBASE_TEXT) : @ibase_fetch_row($this->resultSet, IBASE_TEXT); // intentionally @ $result = $assoc ? @ibase_fetch_assoc($this->resultSet, IBASE_TEXT) : @ibase_fetch_row($this->resultSet, IBASE_TEXT); // intentionally @
@@ -405,11 +365,9 @@ class FirebirdDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Moves cursor position without fetching row. * Moves cursor position without fetching row.
* @param int the 0-based cursor pos to seek to
* @return bool TRUE on success, FALSE if unable to seek to specified record
* @throws Dibi\Exception * @throws Dibi\Exception
*/ */
public function seek($row) public function seek(int $row): bool
{ {
throw new Dibi\NotSupportedException('Firebird/Interbase do not support seek in result set.'); throw new Dibi\NotSupportedException('Firebird/Interbase do not support seek in result set.');
} }
@@ -417,9 +375,8 @@ class FirebirdDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Frees the resources allocated for this result set. * Frees the resources allocated for this result set.
* @return void
*/ */
public function free() public function free(): void
{ {
ibase_free_result($this->resultSet); ibase_free_result($this->resultSet);
$this->resultSet = NULL; $this->resultSet = NULL;
@@ -439,9 +396,8 @@ class FirebirdDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Returns metadata for all columns in a result set. * Returns metadata for all columns in a result set.
* @return array
*/ */
public function getResultColumns() public function getResultColumns(): array
{ {
$count = ibase_num_fields($this->resultSet); $count = ibase_num_fields($this->resultSet);
$columns = []; $columns = [];
@@ -463,9 +419,8 @@ class FirebirdDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Returns list of tables. * Returns list of tables.
* @return array
*/ */
public function getTables() public function getTables(): array
{ {
$res = $this->query(" $res = $this->query("
SELECT TRIM(RDB\$RELATION_NAME), SELECT TRIM(RDB\$RELATION_NAME),
@@ -486,10 +441,8 @@ class FirebirdDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Returns metadata for all columns in a table. * Returns metadata for all columns in a table.
* @param string
* @return array
*/ */
public function getColumns($table) public function getColumns(string $table): array
{ {
$table = strtoupper($table); $table = strtoupper($table);
$res = $this->query(" $res = $this->query("
@@ -541,10 +494,8 @@ class FirebirdDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Returns metadata for all indexes in a table (the constraints are included). * Returns metadata for all indexes in a table (the constraints are included).
* @param string
* @return array
*/ */
public function getIndexes($table) public function getIndexes(string $table): array
{ {
$table = strtoupper($table); $table = strtoupper($table);
$res = $this->query(" $res = $this->query("
@@ -575,10 +526,8 @@ class FirebirdDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Returns metadata for all foreign keys in a table. * Returns metadata for all foreign keys in a table.
* @param string
* @return array
*/ */
public function getForeignKeys($table) public function getForeignKeys(string $table): array
{ {
$table = strtoupper($table); $table = strtoupper($table);
$res = $this->query(" $res = $this->query("
@@ -605,10 +554,8 @@ class FirebirdDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Returns list of indices in given table (the constraints are not listed). * Returns list of indices in given table (the constraints are not listed).
* @param string
* @return array
*/ */
public function getIndices($table) public function getIndices(string $table): array
{ {
$res = $this->query(" $res = $this->query("
SELECT TRIM(RDB\$INDEX_NAME) SELECT TRIM(RDB\$INDEX_NAME)
@@ -627,10 +574,8 @@ class FirebirdDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Returns list of constraints in given table. * Returns list of constraints in given table.
* @param string
* @return array
*/ */
public function getConstraints($table) public function getConstraints(string $table): array
{ {
$res = $this->query(" $res = $this->query("
SELECT TRIM(RDB\$INDEX_NAME) SELECT TRIM(RDB\$INDEX_NAME)
@@ -652,11 +597,8 @@ class FirebirdDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Returns metadata for all triggers in a table or database. * Returns metadata for all triggers in a table or database.
* (Only if user has permissions on ALTER TABLE, INSERT/UPDATE/DELETE record in table) * (Only if user has permissions on ALTER TABLE, INSERT/UPDATE/DELETE record in table)
* @param string
* @param string
* @return array
*/ */
public function getTriggersMeta($table = NULL) public function getTriggersMeta(string $table = NULL): array
{ {
$res = $this->query(" $res = $this->query("
SELECT TRIM(RDB\$TRIGGER_NAME) AS TRIGGER_NAME, SELECT TRIM(RDB\$TRIGGER_NAME) AS TRIGGER_NAME,
@@ -701,10 +643,8 @@ class FirebirdDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Returns list of triggers for given table. * Returns list of triggers for given table.
* (Only if user has permissions on ALTER TABLE, INSERT/UPDATE/DELETE record in table) * (Only if user has permissions on ALTER TABLE, INSERT/UPDATE/DELETE record in table)
* @param string
* @return array
*/ */
public function getTriggers($table = NULL) public function getTriggers(string $table = NULL): array
{ {
$q = "SELECT TRIM(RDB\$TRIGGER_NAME) $q = "SELECT TRIM(RDB\$TRIGGER_NAME)
FROM RDB\$TRIGGERS FROM RDB\$TRIGGERS
@@ -723,9 +663,8 @@ class FirebirdDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Returns metadata from stored procedures and their input and output parameters. * Returns metadata from stored procedures and their input and output parameters.
* @param string * @param string
* @return array
*/ */
public function getProceduresMeta() public function getProceduresMeta(): array
{ {
$res = $this->query(" $res = $this->query("
SELECT SELECT
@@ -775,9 +714,8 @@ class FirebirdDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Returns list of stored procedures. * Returns list of stored procedures.
* @return array
*/ */
public function getProcedures() public function getProcedures(): array
{ {
$res = $this->query(" $res = $this->query("
SELECT TRIM(RDB\$PROCEDURE_NAME) SELECT TRIM(RDB\$PROCEDURE_NAME)
@@ -793,9 +731,8 @@ class FirebirdDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Returns list of generators. * Returns list of generators.
* @return array
*/ */
public function getGenerators() public function getGenerators(): array
{ {
$res = $this->query(" $res = $this->query("
SELECT TRIM(RDB\$GENERATOR_NAME) SELECT TRIM(RDB\$GENERATOR_NAME)
@@ -812,9 +749,8 @@ class FirebirdDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Returns list of user defined functions (UDF). * Returns list of user defined functions (UDF).
* @return array
*/ */
public function getFunctions() public function getFunctions(): array
{ {
$res = $this->query(" $res = $this->query("
SELECT TRIM(RDB\$FUNCTION_NAME) SELECT TRIM(RDB\$FUNCTION_NAME)

View File

@@ -49,10 +49,9 @@ class MsSqlDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Connects to a database. * Connects to a database.
* @return void
* @throws Dibi\Exception * @throws Dibi\Exception
*/ */
public function connect(array &$config) public function connect(array &$config): void
{ {
if (isset($config['resource'])) { if (isset($config['resource'])) {
$this->connection = $config['resource']; $this->connection = $config['resource'];
@@ -74,9 +73,8 @@ class MsSqlDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Disconnects from a database. * Disconnects from a database.
* @return void
*/ */
public function disconnect() public function disconnect(): void
{ {
@mssql_close($this->connection); // @ - connection can be already disconnected @mssql_close($this->connection); // @ - connection can be already disconnected
} }
@@ -85,10 +83,9 @@ class MsSqlDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Executes the SQL query. * Executes the SQL query.
* @param string SQL statement. * @param string SQL statement.
* @return Dibi\ResultDriver|NULL
* @throws Dibi\DriverException * @throws Dibi\DriverException
*/ */
public function query($sql) public function query(string $sql): ?Dibi\ResultDriver
{ {
$res = @mssql_query($sql, $this->connection); // intentionally @ $res = @mssql_query($sql, $this->connection); // intentionally @
@@ -104,9 +101,8 @@ class MsSqlDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Gets the number of affected rows by the last INSERT, UPDATE or DELETE query. * Gets the number of affected rows by the last INSERT, UPDATE or DELETE query.
* @return int|NULL number of rows or NULL on error
*/ */
public function getAffectedRows() public function getAffectedRows(): ?int
{ {
return Dibi\Helpers::false2Null(mssql_rows_affected($this->connection)); return Dibi\Helpers::false2Null(mssql_rows_affected($this->connection));
} }
@@ -114,9 +110,8 @@ class MsSqlDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query. * Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query.
* @return int|NULL int on success or NULL on failure
*/ */
public function getInsertId($sequence) public function getInsertId(?string $sequence): ?int
{ {
$res = mssql_query('SELECT @@IDENTITY', $this->connection); $res = mssql_query('SELECT @@IDENTITY', $this->connection);
if (is_resource($res)) { if (is_resource($res)) {
@@ -130,10 +125,9 @@ class MsSqlDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Begins a transaction (if supported). * Begins a transaction (if supported).
* @param string optional savepoint name * @param string optional savepoint name
* @return void
* @throws Dibi\DriverException * @throws Dibi\DriverException
*/ */
public function begin($savepoint = NULL) public function begin(string $savepoint = NULL): void
{ {
$this->query('BEGIN TRANSACTION'); $this->query('BEGIN TRANSACTION');
} }
@@ -142,10 +136,9 @@ class MsSqlDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Commits statements in a transaction. * Commits statements in a transaction.
* @param string optional savepoint name * @param string optional savepoint name
* @return void
* @throws Dibi\DriverException * @throws Dibi\DriverException
*/ */
public function commit($savepoint = NULL) public function commit(string $savepoint = NULL): void
{ {
$this->query('COMMIT'); $this->query('COMMIT');
} }
@@ -154,10 +147,9 @@ class MsSqlDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Rollback changes in a transaction. * Rollback changes in a transaction.
* @param string optional savepoint name * @param string optional savepoint name
* @return void
* @throws Dibi\DriverException * @throws Dibi\DriverException
*/ */
public function rollback($savepoint = NULL) public function rollback(string $savepoint = NULL): void
{ {
$this->query('ROLLBACK'); $this->query('ROLLBACK');
} }
@@ -175,9 +167,8 @@ class MsSqlDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Returns the connection reflector. * Returns the connection reflector.
* @return Dibi\Reflector
*/ */
public function getReflector() public function getReflector(): Dibi\Reflector
{ {
return new MsSqlReflector($this); return new MsSqlReflector($this);
} }
@@ -186,9 +177,8 @@ class MsSqlDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Result set driver factory. * Result set driver factory.
* @param resource * @param resource
* @return Dibi\ResultDriver
*/ */
public function createResultDriver($resource) public function createResultDriver($resource): Dibi\ResultDriver
{ {
$res = clone $this; $res = clone $this;
$res->resultSet = $resource; $res->resultSet = $resource;
@@ -201,41 +191,27 @@ class MsSqlDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Encodes data for use in a SQL statement. * Encodes data for use in a SQL statement.
* @param string value
* @return string encoded value
*/ */
public function escapeText($value) public function escapeText(string $value): string
{ {
return "'" . str_replace("'", "''", $value) . "'"; return "'" . str_replace("'", "''", $value) . "'";
} }
/** public function escapeBinary(string $value): string
* @param string
* @return string
*/
public function escapeBinary($value)
{ {
return "'" . str_replace("'", "''", $value) . "'"; return "'" . str_replace("'", "''", $value) . "'";
} }
/** public function escapeIdentifier(string $value): string
* @param string
* @return string
*/
public function escapeIdentifier($value)
{ {
// @see https://msdn.microsoft.com/en-us/library/ms176027.aspx // @see https://msdn.microsoft.com/en-us/library/ms176027.aspx
return '[' . str_replace(['[', ']'], ['[[', ']]'], $value) . ']'; return '[' . str_replace(['[', ']'], ['[[', ']]'], $value) . ']';
} }
/** public function escapeBool(bool $value): string
* @param bool
* @return string
*/
public function escapeBool($value)
{ {
return $value ? '1' : '0'; return $value ? '1' : '0';
} }
@@ -243,9 +219,8 @@ class MsSqlDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* @param \DateTimeInterface|string|int * @param \DateTimeInterface|string|int
* @return string
*/ */
public function escapeDate($value) public function escapeDate($value): string
{ {
if (!$value instanceof \DateTimeInterface) { if (!$value instanceof \DateTimeInterface) {
$value = new Dibi\DateTime($value); $value = new Dibi\DateTime($value);
@@ -256,9 +231,8 @@ class MsSqlDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* @param \DateTimeInterface|string|int * @param \DateTimeInterface|string|int
* @return string
*/ */
public function escapeDateTime($value) public function escapeDateTime($value): string
{ {
if (!$value instanceof \DateTimeInterface) { if (!$value instanceof \DateTimeInterface) {
$value = new Dibi\DateTime($value); $value = new Dibi\DateTime($value);
@@ -269,11 +243,8 @@ class MsSqlDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Encodes string for use in a LIKE statement. * Encodes string for use in a LIKE statement.
* @param string
* @param int
* @return string
*/ */
public function escapeLike($value, $pos) public function escapeLike(string $value, int $pos): string
{ {
$value = strtr($value, ["'" => "''", '%' => '[%]', '_' => '[_]', '[' => '[[]']); $value = strtr($value, ["'" => "''", '%' => '[%]', '_' => '[_]', '[' => '[[]']);
return ($pos <= 0 ? "'%" : "'") . $value . ($pos >= 0 ? "%'" : "'"); return ($pos <= 0 ? "'%" : "'") . $value . ($pos >= 0 ? "%'" : "'");
@@ -282,10 +253,8 @@ class MsSqlDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Decodes data from result set. * Decodes data from result set.
* @param string
* @return string
*/ */
public function unescapeBinary($value) public function unescapeBinary(string $value): string
{ {
return $value; return $value;
} }
@@ -293,12 +262,8 @@ class MsSqlDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Injects LIMIT/OFFSET to the SQL query. * Injects LIMIT/OFFSET to the SQL query.
* @param string
* @param int|NULL
* @param int|NULL
* @return void
*/ */
public function applyLimit(&$sql, $limit, $offset) public function applyLimit(string &$sql, ?int $limit, ?int $offset): void
{ {
if ($offset) { if ($offset) {
throw new Dibi\NotSupportedException('Offset is not supported by this database.'); throw new Dibi\NotSupportedException('Offset is not supported by this database.');
@@ -317,7 +282,6 @@ class MsSqlDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Automatically frees the resources allocated for this result set. * Automatically frees the resources allocated for this result set.
* @return void
*/ */
public function __destruct() public function __destruct()
{ {
@@ -327,9 +291,8 @@ class MsSqlDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Returns the number of rows in a result set. * Returns the number of rows in a result set.
* @return int
*/ */
public function getRowCount() public function getRowCount(): int
{ {
return mssql_num_rows($this->resultSet); return mssql_num_rows($this->resultSet);
} }
@@ -338,9 +301,8 @@ class MsSqlDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Fetches the row at current position and moves the internal cursor to the next position. * Fetches the row at current position and moves the internal cursor to the next position.
* @param bool TRUE for associative array, FALSE for numeric * @param bool TRUE for associative array, FALSE for numeric
* @return array|NULL array on success, NULL if no next record
*/ */
public function fetch($assoc) public function fetch(bool $assoc): ?array
{ {
return Dibi\Helpers::false2Null(mssql_fetch_array($this->resultSet, $assoc ? MSSQL_ASSOC : MSSQL_NUM)); return Dibi\Helpers::false2Null(mssql_fetch_array($this->resultSet, $assoc ? MSSQL_ASSOC : MSSQL_NUM));
} }
@@ -351,7 +313,7 @@ class MsSqlDriver implements Dibi\Driver, Dibi\ResultDriver
* @param int the 0-based cursor pos to seek to * @param int the 0-based cursor pos to seek to
* @return bool TRUE on success, FALSE if unable to seek to specified record * @return bool TRUE on success, FALSE if unable to seek to specified record
*/ */
public function seek($row) public function seek(int $row): bool
{ {
return mssql_data_seek($this->resultSet, $row); return mssql_data_seek($this->resultSet, $row);
} }
@@ -359,9 +321,8 @@ class MsSqlDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Frees the resources allocated for this result set. * Frees the resources allocated for this result set.
* @return void
*/ */
public function free() public function free(): void
{ {
mssql_free_result($this->resultSet); mssql_free_result($this->resultSet);
$this->resultSet = NULL; $this->resultSet = NULL;
@@ -370,9 +331,8 @@ class MsSqlDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Returns metadata for all columns in a result set. * Returns metadata for all columns in a result set.
* @return array
*/ */
public function getResultColumns() public function getResultColumns(): array
{ {
$count = mssql_num_fields($this->resultSet); $count = mssql_num_fields($this->resultSet);
$columns = []; $columns = [];

View File

@@ -30,9 +30,8 @@ class MsSqlReflector implements Dibi\Reflector
/** /**
* Returns list of tables. * Returns list of tables.
* @return array
*/ */
public function getTables() public function getTables(): array
{ {
$res = $this->driver->query(' $res = $this->driver->query('
SELECT TABLE_NAME, TABLE_TYPE SELECT TABLE_NAME, TABLE_TYPE
@@ -51,10 +50,8 @@ class MsSqlReflector implements Dibi\Reflector
/** /**
* Returns count of rows in a table * Returns count of rows in a table
* @param string
* @return int
*/ */
public function getTableCount($table, $fallback = TRUE) public function getTableCount(string $table, bool $fallback = TRUE): int
{ {
if (empty($table)) { if (empty($table)) {
return NULL; return NULL;
@@ -83,10 +80,8 @@ class MsSqlReflector implements Dibi\Reflector
/** /**
* Returns metadata for all columns in a table. * Returns metadata for all columns in a table.
* @param string
* @return array
*/ */
public function getColumns($table) public function getColumns(string $table): array
{ {
$res = $this->driver->query(" $res = $this->driver->query("
SELECT * FROM SELECT * FROM
@@ -133,10 +128,8 @@ class MsSqlReflector implements Dibi\Reflector
/** /**
* Returns metadata for all indexes in a table. * Returns metadata for all indexes in a table.
* @param string
* @return array
*/ */
public function getIndexes($table) public function getIndexes(string $table): array
{ {
$res = $this->driver->query( $res = $this->driver->query(
"SELECT ind.name index_name, ind.index_id, ic.index_column_id, "SELECT ind.name index_name, ind.index_id, ic.index_column_id,
@@ -174,10 +167,8 @@ class MsSqlReflector implements Dibi\Reflector
/** /**
* Returns metadata for all foreign keys in a table. * Returns metadata for all foreign keys in a table.
* @param string
* @return array
*/ */
public function getForeignKeys($table) public function getForeignKeys(string $table): array
{ {
$res = $this->driver->query(" $res = $this->driver->query("
SELECT f.name AS foreign_key, SELECT f.name AS foreign_key,

View File

@@ -30,9 +30,8 @@ class MySqlReflector implements Dibi\Reflector
/** /**
* Returns list of tables. * Returns list of tables.
* @return array
*/ */
public function getTables() public function getTables(): array
{ {
$res = $this->driver->query('SHOW FULL TABLES'); $res = $this->driver->query('SHOW FULL TABLES');
$tables = []; $tables = [];
@@ -48,10 +47,8 @@ class MySqlReflector implements Dibi\Reflector
/** /**
* Returns metadata for all columns in a table. * Returns metadata for all columns in a table.
* @param string
* @return array
*/ */
public function getColumns($table) public function getColumns(string $table): array
{ {
$res = $this->driver->query("SHOW FULL COLUMNS FROM {$this->driver->escapeIdentifier($table)}"); $res = $this->driver->query("SHOW FULL COLUMNS FROM {$this->driver->escapeIdentifier($table)}");
$columns = []; $columns = [];
@@ -75,10 +72,8 @@ class MySqlReflector implements Dibi\Reflector
/** /**
* Returns metadata for all indexes in a table. * Returns metadata for all indexes in a table.
* @param string
* @return array
*/ */
public function getIndexes($table) public function getIndexes(string $table): array
{ {
$res = $this->driver->query("SHOW INDEX FROM {$this->driver->escapeIdentifier($table)}"); $res = $this->driver->query("SHOW INDEX FROM {$this->driver->escapeIdentifier($table)}");
$indexes = []; $indexes = [];
@@ -94,11 +89,9 @@ class MySqlReflector implements Dibi\Reflector
/** /**
* Returns metadata for all foreign keys in a table. * Returns metadata for all foreign keys in a table.
* @param string
* @return array
* @throws Dibi\NotSupportedException * @throws Dibi\NotSupportedException
*/ */
public function getForeignKeys($table) 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); $data = $this->driver->query("SELECT `ENGINE` FROM information_schema.TABLES WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = {$this->driver->escapeText($table)}")->fetch(TRUE);
if ($data['ENGINE'] !== 'InnoDB') { if ($data['ENGINE'] !== 'InnoDB') {

View File

@@ -63,10 +63,9 @@ class MySqliDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Connects to a database. * Connects to a database.
* @return void
* @throws Dibi\Exception * @throws Dibi\Exception
*/ */
public function connect(array &$config) public function connect(array &$config): void
{ {
mysqli_report(MYSQLI_REPORT_OFF); mysqli_report(MYSQLI_REPORT_OFF);
if (isset($config['resource'])) { if (isset($config['resource'])) {
@@ -134,9 +133,8 @@ class MySqliDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Disconnects from a database. * Disconnects from a database.
* @return void
*/ */
public function disconnect() public function disconnect(): void
{ {
@mysqli_close($this->connection); // @ - connection can be already disconnected @mysqli_close($this->connection); // @ - connection can be already disconnected
} }
@@ -145,10 +143,9 @@ class MySqliDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Executes the SQL query. * Executes the SQL query.
* @param string SQL statement. * @param string SQL statement.
* @return Dibi\ResultDriver|NULL
* @throws Dibi\DriverException * @throws Dibi\DriverException
*/ */
public function query($sql) public function query(string $sql): ?Dibi\ResultDriver
{ {
$res = @mysqli_query($this->connection, $sql, $this->buffered ? MYSQLI_STORE_RESULT : MYSQLI_USE_RESULT); // intentionally @ $res = @mysqli_query($this->connection, $sql, $this->buffered ? MYSQLI_STORE_RESULT : MYSQLI_USE_RESULT); // intentionally @
@@ -162,10 +159,7 @@ class MySqliDriver implements Dibi\Driver, Dibi\ResultDriver
} }
/** public static function createException(string $message, $code, string $sql): Dibi\DriverException
* @return Dibi\DriverException
*/
public static function createException($message, $code, $sql)
{ {
if (in_array($code, [1216, 1217, 1451, 1452, 1701], TRUE)) { if (in_array($code, [1216, 1217, 1451, 1452, 1701], TRUE)) {
return new Dibi\ForeignKeyConstraintViolationException($message, $code, $sql); return new Dibi\ForeignKeyConstraintViolationException($message, $code, $sql);
@@ -184,9 +178,8 @@ class MySqliDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Retrieves information about the most recently executed query. * Retrieves information about the most recently executed query.
* @return array
*/ */
public function getInfo() public function getInfo(): array
{ {
$res = []; $res = [];
preg_match_all('#(.+?): +(\d+) *#', mysqli_info($this->connection), $matches, PREG_SET_ORDER); preg_match_all('#(.+?): +(\d+) *#', mysqli_info($this->connection), $matches, PREG_SET_ORDER);
@@ -203,9 +196,8 @@ class MySqliDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Gets the number of affected rows by the last INSERT, UPDATE or DELETE query. * Gets the number of affected rows by the last INSERT, UPDATE or DELETE query.
* @return int|NULL number of rows or NULL on error
*/ */
public function getAffectedRows() public function getAffectedRows(): ?int
{ {
return mysqli_affected_rows($this->connection) === -1 ? NULL : mysqli_affected_rows($this->connection); return mysqli_affected_rows($this->connection) === -1 ? NULL : mysqli_affected_rows($this->connection);
} }
@@ -213,9 +205,8 @@ class MySqliDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query. * Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query.
* @return int|NULL int on success or NULL on failure
*/ */
public function getInsertId($sequence) public function getInsertId(?string $sequence): ?int
{ {
return mysqli_insert_id($this->connection); return mysqli_insert_id($this->connection);
} }
@@ -224,10 +215,9 @@ class MySqliDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Begins a transaction (if supported). * Begins a transaction (if supported).
* @param string optional savepoint name * @param string optional savepoint name
* @return void
* @throws Dibi\DriverException * @throws Dibi\DriverException
*/ */
public function begin($savepoint = NULL) public function begin(string $savepoint = NULL): void
{ {
$this->query($savepoint ? "SAVEPOINT $savepoint" : 'START TRANSACTION'); $this->query($savepoint ? "SAVEPOINT $savepoint" : 'START TRANSACTION');
} }
@@ -236,10 +226,9 @@ class MySqliDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Commits statements in a transaction. * Commits statements in a transaction.
* @param string optional savepoint name * @param string optional savepoint name
* @return void
* @throws Dibi\DriverException * @throws Dibi\DriverException
*/ */
public function commit($savepoint = NULL) public function commit(string $savepoint = NULL): void
{ {
$this->query($savepoint ? "RELEASE SAVEPOINT $savepoint" : 'COMMIT'); $this->query($savepoint ? "RELEASE SAVEPOINT $savepoint" : 'COMMIT');
} }
@@ -248,10 +237,9 @@ class MySqliDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Rollback changes in a transaction. * Rollback changes in a transaction.
* @param string optional savepoint name * @param string optional savepoint name
* @return void
* @throws Dibi\DriverException * @throws Dibi\DriverException
*/ */
public function rollback($savepoint = NULL) public function rollback(string $savepoint = NULL): void
{ {
$this->query($savepoint ? "ROLLBACK TO SAVEPOINT $savepoint" : 'ROLLBACK'); $this->query($savepoint ? "ROLLBACK TO SAVEPOINT $savepoint" : 'ROLLBACK');
} }
@@ -259,9 +247,8 @@ class MySqliDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Returns the connection resource. * Returns the connection resource.
* @return \mysqli
*/ */
public function getResource() public function getResource(): \mysqli
{ {
return @$this->connection->thread_id ? $this->connection : NULL; return @$this->connection->thread_id ? $this->connection : NULL;
} }
@@ -269,9 +256,8 @@ class MySqliDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Returns the connection reflector. * Returns the connection reflector.
* @return Dibi\Reflector
*/ */
public function getReflector() public function getReflector(): Dibi\Reflector
{ {
return new MySqlReflector($this); return new MySqlReflector($this);
} }
@@ -279,9 +265,8 @@ class MySqliDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Result set driver factory. * Result set driver factory.
* @return Dibi\ResultDriver
*/ */
public function createResultDriver(\mysqli_result $resource) public function createResultDriver(\mysqli_result $resource): Dibi\ResultDriver
{ {
$res = clone $this; $res = clone $this;
$res->resultSet = $resource; $res->resultSet = $resource;
@@ -294,40 +279,26 @@ class MySqliDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Encodes data for use in a SQL statement. * Encodes data for use in a SQL statement.
* @param string value
* @return string encoded value
*/ */
public function escapeText($value) public function escapeText(string $value): string
{ {
return "'" . mysqli_real_escape_string($this->connection, $value) . "'"; return "'" . mysqli_real_escape_string($this->connection, $value) . "'";
} }
/** public function escapeBinary(string $value): string
* @param string
* @return string
*/
public function escapeBinary($value)
{ {
return "_binary'" . mysqli_real_escape_string($this->connection, $value) . "'"; return "_binary'" . mysqli_real_escape_string($this->connection, $value) . "'";
} }
/** public function escapeIdentifier(string $value): string
* @param string
* @return string
*/
public function escapeIdentifier($value)
{ {
return '`' . str_replace('`', '``', $value) . '`'; return '`' . str_replace('`', '``', $value) . '`';
} }
/** public function escapeBool(bool $value): string
* @param bool
* @return string
*/
public function escapeBool($value)
{ {
return $value ? '1' : '0'; return $value ? '1' : '0';
} }
@@ -335,9 +306,8 @@ class MySqliDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* @param \DateTimeInterface|string|int * @param \DateTimeInterface|string|int
* @return string
*/ */
public function escapeDate($value) public function escapeDate($value): string
{ {
if (!$value instanceof \DateTimeInterface) { if (!$value instanceof \DateTimeInterface) {
$value = new Dibi\DateTime($value); $value = new Dibi\DateTime($value);
@@ -348,9 +318,8 @@ class MySqliDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* @param \DateTimeInterface|string|int * @param \DateTimeInterface|string|int
* @return string
*/ */
public function escapeDateTime($value) public function escapeDateTime($value): string
{ {
if (!$value instanceof \DateTimeInterface) { if (!$value instanceof \DateTimeInterface) {
$value = new Dibi\DateTime($value); $value = new Dibi\DateTime($value);
@@ -361,11 +330,8 @@ class MySqliDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Encodes string for use in a LIKE statement. * Encodes string for use in a LIKE statement.
* @param string
* @param int
* @return string
*/ */
public function escapeLike($value, $pos) public function escapeLike(string $value, int $pos): string
{ {
$value = addcslashes(str_replace('\\', '\\\\', $value), "\x00\n\r\\'%_"); $value = addcslashes(str_replace('\\', '\\\\', $value), "\x00\n\r\\'%_");
return ($pos <= 0 ? "'%" : "'") . $value . ($pos >= 0 ? "%'" : "'"); return ($pos <= 0 ? "'%" : "'") . $value . ($pos >= 0 ? "%'" : "'");
@@ -374,10 +340,8 @@ class MySqliDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Decodes data from result set. * Decodes data from result set.
* @param string
* @return string
*/ */
public function unescapeBinary($value) public function unescapeBinary(string $value): string
{ {
return $value; return $value;
} }
@@ -385,12 +349,8 @@ class MySqliDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Injects LIMIT/OFFSET to the SQL query. * Injects LIMIT/OFFSET to the SQL query.
* @param string
* @param int|NULL
* @param int|NULL
* @return void
*/ */
public function applyLimit(&$sql, $limit, $offset) public function applyLimit(string &$sql, ?int $limit, ?int $offset): void
{ {
if ($limit < 0 || $offset < 0) { if ($limit < 0 || $offset < 0) {
throw new Dibi\NotSupportedException('Negative offset or limit.'); throw new Dibi\NotSupportedException('Negative offset or limit.');
@@ -408,7 +368,6 @@ class MySqliDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Automatically frees the resources allocated for this result set. * Automatically frees the resources allocated for this result set.
* @return void
*/ */
public function __destruct() public function __destruct()
{ {
@@ -418,9 +377,8 @@ class MySqliDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Returns the number of rows in a result set. * Returns the number of rows in a result set.
* @return int
*/ */
public function getRowCount() public function getRowCount(): int
{ {
if (!$this->buffered) { if (!$this->buffered) {
throw new Dibi\NotSupportedException('Row count is not available for unbuffered queries.'); throw new Dibi\NotSupportedException('Row count is not available for unbuffered queries.');
@@ -432,9 +390,8 @@ class MySqliDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Fetches the row at current position and moves the internal cursor to the next position. * Fetches the row at current position and moves the internal cursor to the next position.
* @param bool TRUE for associative array, FALSE for numeric * @param bool TRUE for associative array, FALSE for numeric
* @return array|NULL array on success, NULL if no next record
*/ */
public function fetch($assoc) public function fetch(bool $assoc): ?array
{ {
return mysqli_fetch_array($this->resultSet, $assoc ? MYSQLI_ASSOC : MYSQLI_NUM); return mysqli_fetch_array($this->resultSet, $assoc ? MYSQLI_ASSOC : MYSQLI_NUM);
} }
@@ -442,11 +399,9 @@ class MySqliDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Moves cursor position without fetching row. * Moves cursor position without fetching row.
* @param int the 0-based cursor pos to seek to
* @return bool TRUE on success, FALSE if unable to seek to specified record
* @throws Dibi\Exception * @throws Dibi\Exception
*/ */
public function seek($row) public function seek(int $row): bool
{ {
if (!$this->buffered) { if (!$this->buffered) {
throw new Dibi\NotSupportedException('Cannot seek an unbuffered result set.'); throw new Dibi\NotSupportedException('Cannot seek an unbuffered result set.');
@@ -457,9 +412,8 @@ class MySqliDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Frees the resources allocated for this result set. * Frees the resources allocated for this result set.
* @return void
*/ */
public function free() public function free(): void
{ {
mysqli_free_result($this->resultSet); mysqli_free_result($this->resultSet);
$this->resultSet = NULL; $this->resultSet = NULL;
@@ -468,9 +422,8 @@ class MySqliDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Returns metadata for all columns in a result set. * Returns metadata for all columns in a result set.
* @return array
*/ */
public function getResultColumns() public function getResultColumns(): array
{ {
static $types; static $types;
if ($types === NULL) { if ($types === NULL) {
@@ -503,9 +456,8 @@ class MySqliDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Returns the result set resource. * Returns the result set resource.
* @return \mysqli_result|NULL
*/ */
public function getResultResource() public function getResultResource(): ?\mysqli_result
{ {
$this->autoFree = FALSE; $this->autoFree = FALSE;
return $this->resultSet; return $this->resultSet;

View File

@@ -54,10 +54,9 @@ class OdbcDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Connects to a database. * Connects to a database.
* @return void
* @throws Dibi\Exception * @throws Dibi\Exception
*/ */
public function connect(array &$config) public function connect(array &$config): void
{ {
if (isset($config['resource'])) { if (isset($config['resource'])) {
$this->connection = $config['resource']; $this->connection = $config['resource'];
@@ -84,9 +83,8 @@ class OdbcDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Disconnects from a database. * Disconnects from a database.
* @return void
*/ */
public function disconnect() public function disconnect(): void
{ {
@odbc_close($this->connection); // @ - connection can be already disconnected @odbc_close($this->connection); // @ - connection can be already disconnected
} }
@@ -95,10 +93,9 @@ class OdbcDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Executes the SQL query. * Executes the SQL query.
* @param string SQL statement. * @param string SQL statement.
* @return Dibi\ResultDriver|NULL
* @throws Dibi\DriverException * @throws Dibi\DriverException
*/ */
public function query($sql) public function query(string $sql): ?Dibi\ResultDriver
{ {
$this->affectedRows = NULL; $this->affectedRows = NULL;
$res = @odbc_exec($this->connection, $sql); // intentionally @ $res = @odbc_exec($this->connection, $sql); // intentionally @
@@ -116,9 +113,8 @@ class OdbcDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Gets the number of affected rows by the last INSERT, UPDATE or DELETE query. * Gets the number of affected rows by the last INSERT, UPDATE or DELETE query.
* @return int|NULL number of rows or NULL on error
*/ */
public function getAffectedRows() public function getAffectedRows(): ?int
{ {
return $this->affectedRows; return $this->affectedRows;
} }
@@ -126,9 +122,8 @@ class OdbcDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query. * Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query.
* @return int|NULL int on success or NULL on failure
*/ */
public function getInsertId($sequence) public function getInsertId(?string $sequence): ?int
{ {
throw new Dibi\NotSupportedException('ODBC does not support autoincrementing.'); throw new Dibi\NotSupportedException('ODBC does not support autoincrementing.');
} }
@@ -137,10 +132,9 @@ class OdbcDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Begins a transaction (if supported). * Begins a transaction (if supported).
* @param string optional savepoint name * @param string optional savepoint name
* @return void
* @throws Dibi\DriverException * @throws Dibi\DriverException
*/ */
public function begin($savepoint = NULL) public function begin(string $savepoint = NULL): void
{ {
if (!odbc_autocommit($this->connection, FALSE)) { if (!odbc_autocommit($this->connection, FALSE)) {
throw new Dibi\DriverException(odbc_errormsg($this->connection) . ' ' . odbc_error($this->connection)); throw new Dibi\DriverException(odbc_errormsg($this->connection) . ' ' . odbc_error($this->connection));
@@ -151,10 +145,9 @@ class OdbcDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Commits statements in a transaction. * Commits statements in a transaction.
* @param string optional savepoint name * @param string optional savepoint name
* @return void
* @throws Dibi\DriverException * @throws Dibi\DriverException
*/ */
public function commit($savepoint = NULL) public function commit(string $savepoint = NULL): void
{ {
if (!odbc_commit($this->connection)) { if (!odbc_commit($this->connection)) {
throw new Dibi\DriverException(odbc_errormsg($this->connection) . ' ' . odbc_error($this->connection)); throw new Dibi\DriverException(odbc_errormsg($this->connection) . ' ' . odbc_error($this->connection));
@@ -166,10 +159,9 @@ class OdbcDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Rollback changes in a transaction. * Rollback changes in a transaction.
* @param string optional savepoint name * @param string optional savepoint name
* @return void
* @throws Dibi\DriverException * @throws Dibi\DriverException
*/ */
public function rollback($savepoint = NULL) public function rollback(string $savepoint = NULL): void
{ {
if (!odbc_rollback($this->connection)) { if (!odbc_rollback($this->connection)) {
throw new Dibi\DriverException(odbc_errormsg($this->connection) . ' ' . odbc_error($this->connection)); throw new Dibi\DriverException(odbc_errormsg($this->connection) . ' ' . odbc_error($this->connection));
@@ -180,9 +172,8 @@ class OdbcDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Is in transaction? * Is in transaction?
* @return bool
*/ */
public function inTransaction() public function inTransaction(): bool
{ {
return !odbc_autocommit($this->connection); return !odbc_autocommit($this->connection);
} }
@@ -200,9 +191,8 @@ class OdbcDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Returns the connection reflector. * Returns the connection reflector.
* @return Dibi\Reflector
*/ */
public function getReflector() public function getReflector(): Dibi\Reflector
{ {
return $this; return $this;
} }
@@ -211,9 +201,8 @@ class OdbcDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Result set driver factory. * Result set driver factory.
* @param resource * @param resource
* @return Dibi\ResultDriver
*/ */
public function createResultDriver($resource) public function createResultDriver($resource): Dibi\ResultDriver
{ {
$res = clone $this; $res = clone $this;
$res->resultSet = $resource; $res->resultSet = $resource;
@@ -226,40 +215,26 @@ class OdbcDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Encodes data for use in a SQL statement. * Encodes data for use in a SQL statement.
* @param string value
* @return string encoded value
*/ */
public function escapeText($value) public function escapeText(string $value): string
{ {
return "'" . str_replace("'", "''", $value) . "'"; return "'" . str_replace("'", "''", $value) . "'";
} }
/** public function escapeBinary(string $value): string
* @param string
* @return string
*/
public function escapeBinary($value)
{ {
return "'" . str_replace("'", "''", $value) . "'"; return "'" . str_replace("'", "''", $value) . "'";
} }
/** public function escapeIdentifier(string $value): string
* @param string
* @return string
*/
public function escapeIdentifier($value)
{ {
return '[' . str_replace(['[', ']'], ['[[', ']]'], $value) . ']'; return '[' . str_replace(['[', ']'], ['[[', ']]'], $value) . ']';
} }
/** public function escapeBool(bool $value): string
* @param bool
* @return string
*/
public function escapeBool($value)
{ {
return $value ? '1' : '0'; return $value ? '1' : '0';
} }
@@ -267,9 +242,8 @@ class OdbcDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* @param \DateTimeInterface|string|int * @param \DateTimeInterface|string|int
* @return string
*/ */
public function escapeDate($value) public function escapeDate($value): string
{ {
if (!$value instanceof \DateTimeInterface) { if (!$value instanceof \DateTimeInterface) {
$value = new Dibi\DateTime($value); $value = new Dibi\DateTime($value);
@@ -280,9 +254,8 @@ class OdbcDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* @param \DateTimeInterface|string|int * @param \DateTimeInterface|string|int
* @return string
*/ */
public function escapeDateTime($value) public function escapeDateTime($value): string
{ {
if (!$value instanceof \DateTimeInterface) { if (!$value instanceof \DateTimeInterface) {
$value = new Dibi\DateTime($value); $value = new Dibi\DateTime($value);
@@ -293,11 +266,8 @@ class OdbcDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Encodes string for use in a LIKE statement. * Encodes string for use in a LIKE statement.
* @param string
* @param int
* @return string
*/ */
public function escapeLike($value, $pos) public function escapeLike(string $value, int $pos): string
{ {
$value = strtr($value, ["'" => "''", '%' => '[%]', '_' => '[_]', '[' => '[[]']); $value = strtr($value, ["'" => "''", '%' => '[%]', '_' => '[_]', '[' => '[[]']);
return ($pos <= 0 ? "'%" : "'") . $value . ($pos >= 0 ? "%'" : "'"); return ($pos <= 0 ? "'%" : "'") . $value . ($pos >= 0 ? "%'" : "'");
@@ -306,10 +276,8 @@ class OdbcDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Decodes data from result set. * Decodes data from result set.
* @param string
* @return string
*/ */
public function unescapeBinary($value) public function unescapeBinary(string $value): string
{ {
return $value; return $value;
} }
@@ -317,12 +285,8 @@ class OdbcDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Injects LIMIT/OFFSET to the SQL query. * Injects LIMIT/OFFSET to the SQL query.
* @param string
* @param int|NULL
* @param int|NULL
* @return void
*/ */
public function applyLimit(&$sql, $limit, $offset) public function applyLimit(string &$sql, ?int $limit, ?int $offset): void
{ {
if ($offset) { if ($offset) {
throw new Dibi\NotSupportedException('Offset is not supported by this database.'); throw new Dibi\NotSupportedException('Offset is not supported by this database.');
@@ -341,7 +305,6 @@ class OdbcDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Automatically frees the resources allocated for this result set. * Automatically frees the resources allocated for this result set.
* @return void
*/ */
public function __destruct() public function __destruct()
{ {
@@ -351,9 +314,8 @@ class OdbcDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Returns the number of rows in a result set. * Returns the number of rows in a result set.
* @return int
*/ */
public function getRowCount() public function getRowCount(): int
{ {
// will return -1 with many drivers :-( // will return -1 with many drivers :-(
return odbc_num_rows($this->resultSet); return odbc_num_rows($this->resultSet);
@@ -363,9 +325,8 @@ class OdbcDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Fetches the row at current position and moves the internal cursor to the next position. * Fetches the row at current position and moves the internal cursor to the next position.
* @param bool TRUE for associative array, FALSE for numeric * @param bool TRUE for associative array, FALSE for numeric
* @return array|NULL array on success, NULL if no next record
*/ */
public function fetch($assoc) public function fetch(bool $assoc): ?array
{ {
if ($assoc) { if ($assoc) {
return Dibi\Helpers::false2Null(odbc_fetch_array($this->resultSet, ++$this->row)); return Dibi\Helpers::false2Null(odbc_fetch_array($this->resultSet, ++$this->row));
@@ -386,10 +347,8 @@ class OdbcDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Moves cursor position without fetching row. * Moves cursor position without fetching row.
* @param int the 0-based cursor pos to seek to
* @return bool TRUE on success, FALSE if unable to seek to specified record
*/ */
public function seek($row) public function seek(int $row): bool
{ {
$this->row = $row; $this->row = $row;
return TRUE; return TRUE;
@@ -398,9 +357,8 @@ class OdbcDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Frees the resources allocated for this result set. * Frees the resources allocated for this result set.
* @return void
*/ */
public function free() public function free(): void
{ {
odbc_free_result($this->resultSet); odbc_free_result($this->resultSet);
$this->resultSet = NULL; $this->resultSet = NULL;
@@ -409,9 +367,8 @@ class OdbcDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Returns metadata for all columns in a result set. * Returns metadata for all columns in a result set.
* @return array
*/ */
public function getResultColumns() public function getResultColumns(): array
{ {
$count = odbc_num_fields($this->resultSet); $count = odbc_num_fields($this->resultSet);
$columns = []; $columns = [];
@@ -443,9 +400,8 @@ class OdbcDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Returns list of tables. * Returns list of tables.
* @return array
*/ */
public function getTables() public function getTables(): array
{ {
$res = odbc_tables($this->connection); $res = odbc_tables($this->connection);
$tables = []; $tables = [];
@@ -464,10 +420,8 @@ class OdbcDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Returns metadata for all columns in a table. * Returns metadata for all columns in a table.
* @param string
* @return array
*/ */
public function getColumns($table) public function getColumns(string $table): array
{ {
$res = odbc_columns($this->connection); $res = odbc_columns($this->connection);
$columns = []; $columns = [];
@@ -490,10 +444,8 @@ class OdbcDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Returns metadata for all indexes in a table. * Returns metadata for all indexes in a table.
* @param string
* @return array
*/ */
public function getIndexes($table) public function getIndexes(string $table): array
{ {
throw new Dibi\NotImplementedException; throw new Dibi\NotImplementedException;
} }
@@ -501,10 +453,8 @@ class OdbcDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Returns metadata for all foreign keys in a table. * Returns metadata for all foreign keys in a table.
* @param string
* @return array
*/ */
public function getForeignKeys($table) public function getForeignKeys(string $table): array
{ {
throw new Dibi\NotImplementedException; throw new Dibi\NotImplementedException;
} }

View File

@@ -60,10 +60,9 @@ class OracleDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Connects to a database. * Connects to a database.
* @return void
* @throws Dibi\Exception * @throws Dibi\Exception
*/ */
public function connect(array &$config) public function connect(array &$config): void
{ {
$foo = &$config['charset']; $foo = &$config['charset'];
@@ -93,9 +92,8 @@ class OracleDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Disconnects from a database. * Disconnects from a database.
* @return void
*/ */
public function disconnect() public function disconnect(): void
{ {
@oci_close($this->connection); // @ - connection can be already disconnected @oci_close($this->connection); // @ - connection can be already disconnected
} }
@@ -104,10 +102,9 @@ class OracleDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Executes the SQL query. * Executes the SQL query.
* @param string SQL statement. * @param string SQL statement.
* @return Dibi\ResultDriver|NULL
* @throws Dibi\DriverException * @throws Dibi\DriverException
*/ */
public function query($sql) public function query(string $sql): ?Dibi\ResultDriver
{ {
$this->affectedRows = NULL; $this->affectedRows = NULL;
$res = oci_parse($this->connection, $sql); $res = oci_parse($this->connection, $sql);
@@ -129,10 +126,7 @@ class OracleDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
} }
/** public static function createException(string $message, $code, string $sql): Dibi\DriverException
* @return Dibi\DriverException
*/
public static function createException($message, $code, $sql)
{ {
if (in_array($code, [1, 2299, 38911], TRUE)) { if (in_array($code, [1, 2299, 38911], TRUE)) {
return new Dibi\UniqueConstraintViolationException($message, $code, $sql); return new Dibi\UniqueConstraintViolationException($message, $code, $sql);
@@ -151,9 +145,8 @@ class OracleDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Gets the number of affected rows by the last INSERT, UPDATE or DELETE query. * Gets the number of affected rows by the last INSERT, UPDATE or DELETE query.
* @return int|NULL number of rows or NULL on error
*/ */
public function getAffectedRows() public function getAffectedRows(): ?int
{ {
return $this->affectedRows; return $this->affectedRows;
} }
@@ -161,9 +154,8 @@ class OracleDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query. * Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query.
* @return int|NULL int on success or NULL on failure
*/ */
public function getInsertId($sequence) public function getInsertId(?string $sequence): ?int
{ {
$row = $this->query("SELECT $sequence.CURRVAL AS ID FROM DUAL")->fetch(TRUE); $row = $this->query("SELECT $sequence.CURRVAL AS ID FROM DUAL")->fetch(TRUE);
return isset($row['ID']) ? (int) $row['ID'] : NULL; return isset($row['ID']) ? (int) $row['ID'] : NULL;
@@ -173,9 +165,8 @@ class OracleDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Begins a transaction (if supported). * Begins a transaction (if supported).
* @param string optional savepoint name * @param string optional savepoint name
* @return void
*/ */
public function begin($savepoint = NULL) public function begin(string $savepoint = NULL): void
{ {
$this->autocommit = FALSE; $this->autocommit = FALSE;
} }
@@ -184,10 +175,9 @@ class OracleDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Commits statements in a transaction. * Commits statements in a transaction.
* @param string optional savepoint name * @param string optional savepoint name
* @return void
* @throws Dibi\DriverException * @throws Dibi\DriverException
*/ */
public function commit($savepoint = NULL) public function commit(string $savepoint = NULL): void
{ {
if (!oci_commit($this->connection)) { if (!oci_commit($this->connection)) {
$err = oci_error($this->connection); $err = oci_error($this->connection);
@@ -200,10 +190,9 @@ class OracleDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Rollback changes in a transaction. * Rollback changes in a transaction.
* @param string optional savepoint name * @param string optional savepoint name
* @return void
* @throws Dibi\DriverException * @throws Dibi\DriverException
*/ */
public function rollback($savepoint = NULL) public function rollback(string $savepoint = NULL): void
{ {
if (!oci_rollback($this->connection)) { if (!oci_rollback($this->connection)) {
$err = oci_error($this->connection); $err = oci_error($this->connection);
@@ -225,9 +214,8 @@ class OracleDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Returns the connection reflector. * Returns the connection reflector.
* @return Dibi\Reflector
*/ */
public function getReflector() public function getReflector(): Dibi\Reflector
{ {
return $this; return $this;
} }
@@ -236,9 +224,8 @@ class OracleDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Result set driver factory. * Result set driver factory.
* @param resource * @param resource
* @return Dibi\ResultDriver
*/ */
public function createResultDriver($resource) public function createResultDriver($resource): Dibi\ResultDriver
{ {
$res = clone $this; $res = clone $this;
$res->resultSet = $resource; $res->resultSet = $resource;
@@ -251,41 +238,27 @@ class OracleDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Encodes data for use in a SQL statement. * Encodes data for use in a SQL statement.
* @param string value
* @return string encoded value
*/ */
public function escapeText($value) public function escapeText(string $value): string
{ {
return "'" . str_replace("'", "''", $value) . "'"; // TODO: not tested return "'" . str_replace("'", "''", $value) . "'"; // TODO: not tested
} }
/** public function escapeBinary(string $value): string
* @param string
* @return string
*/
public function escapeBinary($value)
{ {
return "'" . str_replace("'", "''", $value) . "'"; // TODO: not tested return "'" . str_replace("'", "''", $value) . "'"; // TODO: not tested
} }
/** public function escapeIdentifier(string $value): string
* @param string
* @return string
*/
public function escapeIdentifier($value)
{ {
// @see http://download.oracle.com/docs/cd/B10500_01/server.920/a96540/sql_elements9a.htm // @see http://download.oracle.com/docs/cd/B10500_01/server.920/a96540/sql_elements9a.htm
return '"' . str_replace('"', '""', $value) . '"'; return '"' . str_replace('"', '""', $value) . '"';
} }
/** public function escapeBool(bool $value): string
* @param bool
* @return string
*/
public function escapeBool($value)
{ {
return $value ? '1' : '0'; return $value ? '1' : '0';
} }
@@ -293,9 +266,8 @@ class OracleDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* @param \DateTimeInterface|string|int * @param \DateTimeInterface|string|int
* @return string
*/ */
public function escapeDate($value) public function escapeDate($value): string
{ {
if (!$value instanceof \DateTimeInterface) { if (!$value instanceof \DateTimeInterface) {
$value = new Dibi\DateTime($value); $value = new Dibi\DateTime($value);
@@ -308,9 +280,8 @@ class OracleDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* @param \DateTimeInterface|string|int * @param \DateTimeInterface|string|int
* @return string
*/ */
public function escapeDateTime($value) public function escapeDateTime($value): string
{ {
if (!$value instanceof \DateTimeInterface) { if (!$value instanceof \DateTimeInterface) {
$value = new Dibi\DateTime($value); $value = new Dibi\DateTime($value);
@@ -323,11 +294,8 @@ class OracleDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Encodes string for use in a LIKE statement. * Encodes string for use in a LIKE statement.
* @param string
* @param int
* @return string
*/ */
public function escapeLike($value, $pos) public function escapeLike(string $value, int $pos): string
{ {
$value = addcslashes(str_replace('\\', '\\\\', $value), "\x00\\%_"); $value = addcslashes(str_replace('\\', '\\\\', $value), "\x00\\%_");
$value = str_replace("'", "''", $value); $value = str_replace("'", "''", $value);
@@ -337,10 +305,8 @@ class OracleDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Decodes data from result set. * Decodes data from result set.
* @param string
* @return string
*/ */
public function unescapeBinary($value) public function unescapeBinary(string $value): string
{ {
return $value; return $value;
} }
@@ -348,12 +314,8 @@ class OracleDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Injects LIMIT/OFFSET to the SQL query. * Injects LIMIT/OFFSET to the SQL query.
* @param string
* @param int|NULL
* @param int|NULL
* @return void
*/ */
public function applyLimit(&$sql, $limit, $offset) public function applyLimit(string &$sql, ?int $limit, ?int $offset): void
{ {
if ($limit < 0 || $offset < 0) { if ($limit < 0 || $offset < 0) {
throw new Dibi\NotSupportedException('Negative offset or limit.'); throw new Dibi\NotSupportedException('Negative offset or limit.');
@@ -375,7 +337,6 @@ class OracleDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Automatically frees the resources allocated for this result set. * Automatically frees the resources allocated for this result set.
* @return void
*/ */
public function __destruct() public function __destruct()
{ {
@@ -385,9 +346,8 @@ class OracleDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Returns the number of rows in a result set. * Returns the number of rows in a result set.
* @return int
*/ */
public function getRowCount() public function getRowCount(): int
{ {
throw new Dibi\NotSupportedException('Row count is not available for unbuffered queries.'); throw new Dibi\NotSupportedException('Row count is not available for unbuffered queries.');
} }
@@ -396,9 +356,8 @@ class OracleDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Fetches the row at current position and moves the internal cursor to the next position. * Fetches the row at current position and moves the internal cursor to the next position.
* @param bool TRUE for associative array, FALSE for numeric * @param bool TRUE for associative array, FALSE for numeric
* @return array|NULL array on success, NULL if no next record
*/ */
public function fetch($assoc) public function fetch(bool $assoc): ?array
{ {
return Dibi\Helpers::false2Null(oci_fetch_array($this->resultSet, ($assoc ? OCI_ASSOC : OCI_NUM) | OCI_RETURN_NULLS)); return Dibi\Helpers::false2Null(oci_fetch_array($this->resultSet, ($assoc ? OCI_ASSOC : OCI_NUM) | OCI_RETURN_NULLS));
} }
@@ -406,10 +365,8 @@ class OracleDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Moves cursor position without fetching row. * Moves cursor position without fetching row.
* @param int the 0-based cursor pos to seek to
* @return bool TRUE on success, FALSE if unable to seek to specified record
*/ */
public function seek($row) public function seek(int $row): bool
{ {
throw new Dibi\NotImplementedException; throw new Dibi\NotImplementedException;
} }
@@ -417,9 +374,8 @@ class OracleDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Frees the resources allocated for this result set. * Frees the resources allocated for this result set.
* @return void
*/ */
public function free() public function free(): void
{ {
oci_free_statement($this->resultSet); oci_free_statement($this->resultSet);
$this->resultSet = NULL; $this->resultSet = NULL;
@@ -428,9 +384,8 @@ class OracleDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Returns metadata for all columns in a result set. * Returns metadata for all columns in a result set.
* @return array
*/ */
public function getResultColumns() public function getResultColumns(): array
{ {
$count = oci_num_fields($this->resultSet); $count = oci_num_fields($this->resultSet);
$columns = []; $columns = [];
@@ -463,9 +418,8 @@ class OracleDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Returns list of tables. * Returns list of tables.
* @return array
*/ */
public function getTables() public function getTables(): array
{ {
$res = $this->query('SELECT * FROM cat'); $res = $this->query('SELECT * FROM cat');
$tables = []; $tables = [];
@@ -483,10 +437,8 @@ class OracleDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Returns metadata for all columns in a table. * Returns metadata for all columns in a table.
* @param string
* @return array
*/ */
public function getColumns($table) public function getColumns(string $table): array
{ {
$res = $this->query('SELECT * FROM "ALL_TAB_COLUMNS" WHERE "TABLE_NAME" = ' . $this->escapeText($table)); $res = $this->query('SELECT * FROM "ALL_TAB_COLUMNS" WHERE "TABLE_NAME" = ' . $this->escapeText($table));
$columns = []; $columns = [];
@@ -507,10 +459,8 @@ class OracleDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Returns metadata for all indexes in a table. * Returns metadata for all indexes in a table.
* @param string
* @return array
*/ */
public function getIndexes($table) public function getIndexes(string $table): array
{ {
throw new Dibi\NotImplementedException; throw new Dibi\NotImplementedException;
} }
@@ -518,10 +468,8 @@ class OracleDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Returns metadata for all foreign keys in a table. * Returns metadata for all foreign keys in a table.
* @param string
* @return array
*/ */
public function getForeignKeys($table) public function getForeignKeys(string $table): array
{ {
throw new Dibi\NotImplementedException; throw new Dibi\NotImplementedException;
} }

View File

@@ -57,10 +57,9 @@ class PdoDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Connects to a database. * Connects to a database.
* @return void
* @throws Dibi\Exception * @throws Dibi\Exception
*/ */
public function connect(array &$config) public function connect(array &$config): void
{ {
$foo = &$config['dsn']; $foo = &$config['dsn'];
$foo = &$config['options']; $foo = &$config['options'];
@@ -88,9 +87,8 @@ class PdoDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Disconnects from a database. * Disconnects from a database.
* @return void
*/ */
public function disconnect() public function disconnect(): void
{ {
$this->connection = NULL; $this->connection = NULL;
} }
@@ -99,10 +97,9 @@ class PdoDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Executes the SQL query. * Executes the SQL query.
* @param string SQL statement. * @param string SQL statement.
* @return Dibi\ResultDriver|NULL
* @throws Dibi\DriverException * @throws Dibi\DriverException
*/ */
public function query($sql) public function query(string $sql): ?Dibi\ResultDriver
{ {
// must detect if SQL returns result set or num of affected rows // must detect if SQL returns result set or num of affected rows
$cmd = strtoupper(substr(ltrim($sql), 0, 6)); $cmd = strtoupper(substr(ltrim($sql), 0, 6));
@@ -144,9 +141,8 @@ class PdoDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Gets the number of affected rows by the last INSERT, UPDATE or DELETE query. * Gets the number of affected rows by the last INSERT, UPDATE or DELETE query.
* @return int|NULL number of rows or NULL on error
*/ */
public function getAffectedRows() public function getAffectedRows(): ?int
{ {
return $this->affectedRows; return $this->affectedRows;
} }
@@ -154,9 +150,8 @@ class PdoDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query. * Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query.
* @return int|NULL int on success or NULL on failure
*/ */
public function getInsertId($sequence) public function getInsertId(?string $sequence): ?int
{ {
return Helpers::false2Null($this->connection->lastInsertId()); return Helpers::false2Null($this->connection->lastInsertId());
} }
@@ -165,10 +160,9 @@ class PdoDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Begins a transaction (if supported). * Begins a transaction (if supported).
* @param string optional savepoint name * @param string optional savepoint name
* @return void
* @throws Dibi\DriverException * @throws Dibi\DriverException
*/ */
public function begin($savepoint = NULL) public function begin(string $savepoint = NULL): void
{ {
if (!$this->connection->beginTransaction()) { if (!$this->connection->beginTransaction()) {
$err = $this->connection->errorInfo(); $err = $this->connection->errorInfo();
@@ -180,10 +174,9 @@ class PdoDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Commits statements in a transaction. * Commits statements in a transaction.
* @param string optional savepoint name * @param string optional savepoint name
* @return void
* @throws Dibi\DriverException * @throws Dibi\DriverException
*/ */
public function commit($savepoint = NULL) public function commit(string $savepoint = NULL): void
{ {
if (!$this->connection->commit()) { if (!$this->connection->commit()) {
$err = $this->connection->errorInfo(); $err = $this->connection->errorInfo();
@@ -195,10 +188,9 @@ class PdoDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Rollback changes in a transaction. * Rollback changes in a transaction.
* @param string optional savepoint name * @param string optional savepoint name
* @return void
* @throws Dibi\DriverException * @throws Dibi\DriverException
*/ */
public function rollback($savepoint = NULL) public function rollback(string $savepoint = NULL): void
{ {
if (!$this->connection->rollBack()) { if (!$this->connection->rollBack()) {
$err = $this->connection->errorInfo(); $err = $this->connection->errorInfo();
@@ -209,9 +201,8 @@ class PdoDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Returns the connection resource. * Returns the connection resource.
* @return PDO
*/ */
public function getResource() public function getResource(): PDO
{ {
return $this->connection; return $this->connection;
} }
@@ -219,9 +210,8 @@ class PdoDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Returns the connection reflector. * Returns the connection reflector.
* @return Dibi\Reflector
*/ */
public function getReflector() public function getReflector(): Dibi\Reflector
{ {
switch ($this->driverName) { switch ($this->driverName) {
case 'mysql': case 'mysql':
@@ -238,10 +228,8 @@ class PdoDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Result set driver factory. * Result set driver factory.
* @param \PDOStatement
* @return Dibi\ResultDriver
*/ */
public function createResultDriver(\PDOStatement $resource) public function createResultDriver(\PDOStatement $resource): Dibi\ResultDriver
{ {
$res = clone $this; $res = clone $this;
$res->resultSet = $resource; $res->resultSet = $resource;
@@ -254,10 +242,8 @@ class PdoDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Encodes data for use in a SQL statement. * Encodes data for use in a SQL statement.
* @param string value
* @return string encoded value
*/ */
public function escapeText($value) public function escapeText(string $value): string
{ {
if ($this->driverName === 'odbc') { if ($this->driverName === 'odbc') {
return "'" . str_replace("'", "''", $value) . "'"; return "'" . str_replace("'", "''", $value) . "'";
@@ -267,11 +253,7 @@ class PdoDriver implements Dibi\Driver, Dibi\ResultDriver
} }
/** public function escapeBinary(string $value): string
* @param string
* @return string
*/
public function escapeBinary($value)
{ {
if ($this->driverName === 'odbc') { if ($this->driverName === 'odbc') {
return "'" . str_replace("'", "''", $value) . "'"; return "'" . str_replace("'", "''", $value) . "'";
@@ -281,11 +263,7 @@ class PdoDriver implements Dibi\Driver, Dibi\ResultDriver
} }
/** public function escapeIdentifier(string $value): string
* @param string
* @return string
*/
public function escapeIdentifier($value)
{ {
switch ($this->driverName) { switch ($this->driverName) {
case 'mysql': case 'mysql':
@@ -312,11 +290,7 @@ class PdoDriver implements Dibi\Driver, Dibi\ResultDriver
} }
/** public function escapeBool(bool $value): string
* @param bool
* @return string
*/
public function escapeBool($value)
{ {
if ($this->driverName === 'pgsql') { if ($this->driverName === 'pgsql') {
return $value ? 'TRUE' : 'FALSE'; return $value ? 'TRUE' : 'FALSE';
@@ -328,9 +302,8 @@ class PdoDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* @param \DateTimeInterface|string|int * @param \DateTimeInterface|string|int
* @return string
*/ */
public function escapeDate($value) public function escapeDate($value): string
{ {
if (!$value instanceof \DateTimeInterface) { if (!$value instanceof \DateTimeInterface) {
$value = new Dibi\DateTime($value); $value = new Dibi\DateTime($value);
@@ -341,9 +314,8 @@ class PdoDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* @param \DateTimeInterface|string|int * @param \DateTimeInterface|string|int
* @return string
*/ */
public function escapeDateTime($value) public function escapeDateTime($value): string
{ {
if (!$value instanceof \DateTimeInterface) { if (!$value instanceof \DateTimeInterface) {
$value = new Dibi\DateTime($value); $value = new Dibi\DateTime($value);
@@ -354,11 +326,8 @@ class PdoDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Encodes string for use in a LIKE statement. * Encodes string for use in a LIKE statement.
* @param string
* @param int
* @return string
*/ */
public function escapeLike($value, $pos) public function escapeLike(string $value, int $pos): string
{ {
switch ($this->driverName) { switch ($this->driverName) {
case 'mysql': case 'mysql':
@@ -395,10 +364,8 @@ class PdoDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Decodes data from result set. * Decodes data from result set.
* @param string
* @return string
*/ */
public function unescapeBinary($value) public function unescapeBinary(string $value): string
{ {
return $value; return $value;
} }
@@ -406,12 +373,8 @@ class PdoDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Injects LIMIT/OFFSET to the SQL query. * Injects LIMIT/OFFSET to the SQL query.
* @param string
* @param int|NULL
* @param int|NULL
* @return void
*/ */
public function applyLimit(&$sql, $limit, $offset) public function applyLimit(string &$sql, ?int $limit, ?int $offset): void
{ {
if ($limit < 0 || $offset < 0) { if ($limit < 0 || $offset < 0) {
throw new Dibi\NotSupportedException('Negative offset or limit.'); throw new Dibi\NotSupportedException('Negative offset or limit.');
@@ -489,9 +452,8 @@ class PdoDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Returns the number of rows in a result set. * Returns the number of rows in a result set.
* @return int
*/ */
public function getRowCount() public function getRowCount(): int
{ {
return $this->resultSet->rowCount(); return $this->resultSet->rowCount();
} }
@@ -500,9 +462,8 @@ class PdoDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Fetches the row at current position and moves the internal cursor to the next position. * Fetches the row at current position and moves the internal cursor to the next position.
* @param bool TRUE for associative array, FALSE for numeric * @param bool TRUE for associative array, FALSE for numeric
* @return array|NULL array on success, NULL if no next record
*/ */
public function fetch($assoc) public function fetch(bool $assoc): ?array
{ {
return Helpers::false2Null($this->resultSet->fetch($assoc ? PDO::FETCH_ASSOC : PDO::FETCH_NUM)); return Helpers::false2Null($this->resultSet->fetch($assoc ? PDO::FETCH_ASSOC : PDO::FETCH_NUM));
} }
@@ -510,10 +471,8 @@ class PdoDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Moves cursor position without fetching row. * Moves cursor position without fetching row.
* @param int the 0-based cursor pos to seek to
* @return bool TRUE on success, FALSE if unable to seek to specified record
*/ */
public function seek($row) public function seek(int $row): bool
{ {
throw new Dibi\NotSupportedException('Cannot seek an unbuffered result set.'); throw new Dibi\NotSupportedException('Cannot seek an unbuffered result set.');
} }
@@ -521,9 +480,8 @@ class PdoDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Frees the resources allocated for this result set. * Frees the resources allocated for this result set.
* @return void
*/ */
public function free() public function free(): void
{ {
$this->resultSet = NULL; $this->resultSet = NULL;
} }
@@ -531,10 +489,9 @@ class PdoDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Returns metadata for all columns in a result set. * Returns metadata for all columns in a result set.
* @return array
* @throws Dibi\Exception * @throws Dibi\Exception
*/ */
public function getResultColumns() public function getResultColumns(): array
{ {
$count = $this->resultSet->columnCount(); $count = $this->resultSet->columnCount();
$columns = []; $columns = [];
@@ -563,9 +520,8 @@ class PdoDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Returns the result set resource. * Returns the result set resource.
* @return \PDOStatement|NULL
*/ */
public function getResultResource() public function getResultResource(): ?\PDOStatement
{ {
return $this->resultSet; return $this->resultSet;
} }

View File

@@ -53,10 +53,9 @@ class PostgreDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Connects to a database. * Connects to a database.
* @return void
* @throws Dibi\Exception * @throws Dibi\Exception
*/ */
public function connect(array &$config) public function connect(array &$config): void
{ {
$error = NULL; $error = NULL;
if (isset($config['resource'])) { if (isset($config['resource'])) {
@@ -108,9 +107,8 @@ class PostgreDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Disconnects from a database. * Disconnects from a database.
* @return void
*/ */
public function disconnect() public function disconnect(): void
{ {
@pg_close($this->connection); // @ - connection can be already disconnected @pg_close($this->connection); // @ - connection can be already disconnected
} }
@@ -118,9 +116,8 @@ class PostgreDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Pings database. * Pings database.
* @return bool
*/ */
public function ping() public function ping(): bool
{ {
return pg_ping($this->connection); return pg_ping($this->connection);
} }
@@ -129,10 +126,9 @@ class PostgreDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Executes the SQL query. * Executes the SQL query.
* @param string SQL statement. * @param string SQL statement.
* @return Dibi\ResultDriver|NULL
* @throws Dibi\DriverException * @throws Dibi\DriverException
*/ */
public function query($sql) public function query(string $sql): ?Dibi\ResultDriver
{ {
$this->affectedRows = NULL; $this->affectedRows = NULL;
$res = @pg_query($this->connection, $sql); // intentionally @ $res = @pg_query($this->connection, $sql); // intentionally @
@@ -150,10 +146,7 @@ class PostgreDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
} }
/** public static function createException(string $message, $code = NULL, string $sql = NULL): Dibi\DriverException
* @return Dibi\DriverException
*/
public static function createException($message, $code = NULL, $sql = NULL)
{ {
if ($code === NULL && preg_match('#^ERROR:\s+(\S+):\s*#', $message, $m)) { if ($code === NULL && preg_match('#^ERROR:\s+(\S+):\s*#', $message, $m)) {
$code = $m[1]; $code = $m[1];
@@ -180,9 +173,8 @@ class PostgreDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Gets the number of affected rows by the last INSERT, UPDATE or DELETE query. * Gets the number of affected rows by the last INSERT, UPDATE or DELETE query.
* @return int|NULL number of rows or NULL on error
*/ */
public function getAffectedRows() public function getAffectedRows(): ?int
{ {
return $this->affectedRows; return $this->affectedRows;
} }
@@ -190,9 +182,8 @@ class PostgreDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query. * Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query.
* @return int|NULL int on success or NULL on failure
*/ */
public function getInsertId($sequence) public function getInsertId(?string $sequence): ?int
{ {
if ($sequence === NULL) { if ($sequence === NULL) {
// PostgreSQL 8.1 is needed // PostgreSQL 8.1 is needed
@@ -213,10 +204,9 @@ class PostgreDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Begins a transaction (if supported). * Begins a transaction (if supported).
* @param string optional savepoint name * @param string optional savepoint name
* @return void
* @throws Dibi\DriverException * @throws Dibi\DriverException
*/ */
public function begin($savepoint = NULL) public function begin(string $savepoint = NULL): void
{ {
$this->query($savepoint ? "SAVEPOINT $savepoint" : 'START TRANSACTION'); $this->query($savepoint ? "SAVEPOINT $savepoint" : 'START TRANSACTION');
} }
@@ -225,10 +215,9 @@ class PostgreDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Commits statements in a transaction. * Commits statements in a transaction.
* @param string optional savepoint name * @param string optional savepoint name
* @return void
* @throws Dibi\DriverException * @throws Dibi\DriverException
*/ */
public function commit($savepoint = NULL) public function commit(string $savepoint = NULL): void
{ {
$this->query($savepoint ? "RELEASE SAVEPOINT $savepoint" : 'COMMIT'); $this->query($savepoint ? "RELEASE SAVEPOINT $savepoint" : 'COMMIT');
} }
@@ -237,10 +226,9 @@ class PostgreDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Rollback changes in a transaction. * Rollback changes in a transaction.
* @param string optional savepoint name * @param string optional savepoint name
* @return void
* @throws Dibi\DriverException * @throws Dibi\DriverException
*/ */
public function rollback($savepoint = NULL) public function rollback(string $savepoint = NULL): void
{ {
$this->query($savepoint ? "ROLLBACK TO SAVEPOINT $savepoint" : 'ROLLBACK'); $this->query($savepoint ? "ROLLBACK TO SAVEPOINT $savepoint" : 'ROLLBACK');
} }
@@ -248,9 +236,8 @@ class PostgreDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Is in transaction? * Is in transaction?
* @return bool
*/ */
public function inTransaction() public function inTransaction(): bool
{ {
return !in_array(pg_transaction_status($this->connection), [PGSQL_TRANSACTION_UNKNOWN, PGSQL_TRANSACTION_IDLE], TRUE); return !in_array(pg_transaction_status($this->connection), [PGSQL_TRANSACTION_UNKNOWN, PGSQL_TRANSACTION_IDLE], TRUE);
} }
@@ -268,9 +255,8 @@ class PostgreDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Returns the connection reflector. * Returns the connection reflector.
* @return Dibi\Reflector
*/ */
public function getReflector() public function getReflector(): Dibi\Reflector
{ {
return $this; return $this;
} }
@@ -279,9 +265,8 @@ class PostgreDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Result set driver factory. * Result set driver factory.
* @param resource * @param resource
* @return Dibi\ResultDriver
*/ */
public function createResultDriver($resource) public function createResultDriver($resource): Dibi\ResultDriver
{ {
$res = clone $this; $res = clone $this;
$res->resultSet = $resource; $res->resultSet = $resource;
@@ -294,10 +279,8 @@ class PostgreDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Encodes data for use in a SQL statement. * Encodes data for use in a SQL statement.
* @param string value
* @return string encoded value
*/ */
public function escapeText($value) public function escapeText(string $value): string
{ {
if (!is_resource($this->connection)) { if (!is_resource($this->connection)) {
throw new Dibi\Exception('Lost connection to server.'); throw new Dibi\Exception('Lost connection to server.');
@@ -306,11 +289,7 @@ class PostgreDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
} }
/** public function escapeBinary(string $value): string
* @param string
* @return string
*/
public function escapeBinary($value)
{ {
if (!is_resource($this->connection)) { if (!is_resource($this->connection)) {
throw new Dibi\Exception('Lost connection to server.'); throw new Dibi\Exception('Lost connection to server.');
@@ -319,22 +298,14 @@ class PostgreDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
} }
/** public function escapeIdentifier(string $value): string
* @param string
* @return string
*/
public function escapeIdentifier($value)
{ {
// @see http://www.postgresql.org/docs/8.2/static/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS // @see http://www.postgresql.org/docs/8.2/static/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS
return '"' . str_replace('"', '""', $value) . '"'; return '"' . str_replace('"', '""', $value) . '"';
} }
/** public function escapeBool(bool $value): string
* @param bool
* @return string
*/
public function escapeBool($value)
{ {
return $value ? 'TRUE' : 'FALSE'; return $value ? 'TRUE' : 'FALSE';
} }
@@ -342,9 +313,8 @@ class PostgreDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* @param \DateTimeInterface|string|int * @param \DateTimeInterface|string|int
* @return string
*/ */
public function escapeDate($value) public function escapeDate($value): string
{ {
if (!$value instanceof \DateTimeInterface) { if (!$value instanceof \DateTimeInterface) {
$value = new Dibi\DateTime($value); $value = new Dibi\DateTime($value);
@@ -355,9 +325,8 @@ class PostgreDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* @param \DateTimeInterface|string|int * @param \DateTimeInterface|string|int
* @return string
*/ */
public function escapeDateTime($value) public function escapeDateTime($value): string
{ {
if (!$value instanceof \DateTimeInterface) { if (!$value instanceof \DateTimeInterface) {
$value = new Dibi\DateTime($value); $value = new Dibi\DateTime($value);
@@ -368,11 +337,8 @@ class PostgreDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Encodes string for use in a LIKE statement. * Encodes string for use in a LIKE statement.
* @param string
* @param int
* @return string
*/ */
public function escapeLike($value, $pos) public function escapeLike(string $value, int $pos): string
{ {
$bs = pg_escape_string($this->connection, '\\'); // standard_conforming_strings = on/off $bs = pg_escape_string($this->connection, '\\'); // standard_conforming_strings = on/off
$value = pg_escape_string($this->connection, $value); $value = pg_escape_string($this->connection, $value);
@@ -383,10 +349,8 @@ class PostgreDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Decodes data from result set. * Decodes data from result set.
* @param string
* @return string
*/ */
public function unescapeBinary($value) public function unescapeBinary(string $value): string
{ {
return pg_unescape_bytea($value); return pg_unescape_bytea($value);
} }
@@ -394,12 +358,8 @@ class PostgreDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Injects LIMIT/OFFSET to the SQL query. * Injects LIMIT/OFFSET to the SQL query.
* @param string
* @param int|NULL
* @param int|NULL
* @return void
*/ */
public function applyLimit(&$sql, $limit, $offset) public function applyLimit(string &$sql, ?int $limit, ?int $offset): void
{ {
if ($limit < 0 || $offset < 0) { if ($limit < 0 || $offset < 0) {
throw new Dibi\NotSupportedException('Negative offset or limit.'); throw new Dibi\NotSupportedException('Negative offset or limit.');
@@ -418,7 +378,6 @@ class PostgreDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Automatically frees the resources allocated for this result set. * Automatically frees the resources allocated for this result set.
* @return void
*/ */
public function __destruct() public function __destruct()
{ {
@@ -428,9 +387,8 @@ class PostgreDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Returns the number of rows in a result set. * Returns the number of rows in a result set.
* @return int
*/ */
public function getRowCount() public function getRowCount(): int
{ {
return pg_num_rows($this->resultSet); return pg_num_rows($this->resultSet);
} }
@@ -439,9 +397,8 @@ class PostgreDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Fetches the row at current position and moves the internal cursor to the next position. * Fetches the row at current position and moves the internal cursor to the next position.
* @param bool TRUE for associative array, FALSE for numeric * @param bool TRUE for associative array, FALSE for numeric
* @return array|NULL array on success, NULL if no next record
*/ */
public function fetch($assoc) public function fetch(bool $assoc): ?array
{ {
return Helpers::false2Null(pg_fetch_array($this->resultSet, NULL, $assoc ? PGSQL_ASSOC : PGSQL_NUM)); return Helpers::false2Null(pg_fetch_array($this->resultSet, NULL, $assoc ? PGSQL_ASSOC : PGSQL_NUM));
} }
@@ -449,10 +406,8 @@ class PostgreDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Moves cursor position without fetching row. * Moves cursor position without fetching row.
* @param int the 0-based cursor pos to seek to
* @return bool TRUE on success, FALSE if unable to seek to specified record
*/ */
public function seek($row) public function seek(int $row): bool
{ {
return pg_result_seek($this->resultSet, $row); return pg_result_seek($this->resultSet, $row);
} }
@@ -460,9 +415,8 @@ class PostgreDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Frees the resources allocated for this result set. * Frees the resources allocated for this result set.
* @return void
*/ */
public function free() public function free(): void
{ {
pg_free_result($this->resultSet); pg_free_result($this->resultSet);
$this->resultSet = NULL; $this->resultSet = NULL;
@@ -471,9 +425,8 @@ class PostgreDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Returns metadata for all columns in a result set. * Returns metadata for all columns in a result set.
* @return array
*/ */
public function getResultColumns() public function getResultColumns(): array
{ {
$count = pg_num_fields($this->resultSet); $count = pg_num_fields($this->resultSet);
$columns = []; $columns = [];
@@ -506,9 +459,8 @@ class PostgreDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Returns list of tables. * Returns list of tables.
* @return array
*/ */
public function getTables() public function getTables(): array
{ {
$version = pg_parameter_status($this->getResource(), 'server_version'); $version = pg_parameter_status($this->getResource(), 'server_version');
if ($version < 7.4) { if ($version < 7.4) {
@@ -546,10 +498,8 @@ class PostgreDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Returns metadata for all columns in a table. * Returns metadata for all columns in a table.
* @param string
* @return array
*/ */
public function getColumns($table) public function getColumns(string $table): array
{ {
$_table = $this->escapeText($this->escapeIdentifier($table)); $_table = $this->escapeText($this->escapeIdentifier($table));
$res = $this->query(" $res = $this->query("
@@ -613,10 +563,8 @@ class PostgreDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Returns metadata for all indexes in a table. * Returns metadata for all indexes in a table.
* @param string
* @return array
*/ */
public function getIndexes($table) public function getIndexes(string $table): array
{ {
$_table = $this->escapeText($this->escapeIdentifier($table)); $_table = $this->escapeText($this->escapeIdentifier($table));
$res = $this->query(" $res = $this->query("
@@ -661,10 +609,8 @@ class PostgreDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
/** /**
* Returns metadata for all foreign keys in a table. * Returns metadata for all foreign keys in a table.
* @param string
* @return array
*/ */
public function getForeignKeys($table) public function getForeignKeys(string $table): array
{ {
$_table = $this->escapeText($this->escapeIdentifier($table)); $_table = $this->escapeText($this->escapeIdentifier($table));

View File

@@ -56,10 +56,9 @@ class Sqlite3Driver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Connects to a database. * Connects to a database.
* @return void
* @throws Dibi\Exception * @throws Dibi\Exception
*/ */
public function connect(array &$config) public function connect(array &$config): void
{ {
Dibi\Helpers::alias($config, 'database', 'file'); Dibi\Helpers::alias($config, 'database', 'file');
$this->fmtDate = $config['formatDate'] ?? 'U'; $this->fmtDate = $config['formatDate'] ?? 'U';
@@ -91,9 +90,8 @@ class Sqlite3Driver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Disconnects from a database. * Disconnects from a database.
* @return void
*/ */
public function disconnect() public function disconnect(): void
{ {
$this->connection->close(); $this->connection->close();
} }
@@ -102,10 +100,9 @@ class Sqlite3Driver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Executes the SQL query. * Executes the SQL query.
* @param string SQL statement. * @param string SQL statement.
* @return Dibi\ResultDriver|NULL
* @throws Dibi\DriverException * @throws Dibi\DriverException
*/ */
public function query($sql) public function query(string $sql): ?Dibi\ResultDriver
{ {
if ($this->dbcharset !== NULL) { if ($this->dbcharset !== NULL) {
$sql = iconv($this->charset, $this->dbcharset . '//IGNORE', $sql); $sql = iconv($this->charset, $this->dbcharset . '//IGNORE', $sql);
@@ -122,10 +119,7 @@ class Sqlite3Driver implements Dibi\Driver, Dibi\ResultDriver
} }
/** public static function createException(string $message, $code, string $sql): Dibi\DriverException
* @return Dibi\DriverException
*/
public static function createException($message, $code, $sql)
{ {
if ($code !== 19) { if ($code !== 19) {
return new Dibi\DriverException($message, $code, $sql); return new Dibi\DriverException($message, $code, $sql);
@@ -154,9 +148,8 @@ class Sqlite3Driver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Gets the number of affected rows by the last INSERT, UPDATE or DELETE query. * Gets the number of affected rows by the last INSERT, UPDATE or DELETE query.
* @return int|NULL number of rows or NULL on error
*/ */
public function getAffectedRows() public function getAffectedRows(): ?int
{ {
return $this->connection->changes(); return $this->connection->changes();
} }
@@ -164,9 +157,8 @@ class Sqlite3Driver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query. * Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query.
* @return int|NULL int on success or NULL on failure
*/ */
public function getInsertId($sequence) public function getInsertId(?string $sequence): ?int
{ {
return $this->connection->lastInsertRowID(); return $this->connection->lastInsertRowID();
} }
@@ -175,10 +167,9 @@ class Sqlite3Driver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Begins a transaction (if supported). * Begins a transaction (if supported).
* @param string optional savepoint name * @param string optional savepoint name
* @return void
* @throws Dibi\DriverException * @throws Dibi\DriverException
*/ */
public function begin($savepoint = NULL) public function begin(string $savepoint = NULL): void
{ {
$this->query($savepoint ? "SAVEPOINT $savepoint" : 'BEGIN'); $this->query($savepoint ? "SAVEPOINT $savepoint" : 'BEGIN');
} }
@@ -187,10 +178,9 @@ class Sqlite3Driver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Commits statements in a transaction. * Commits statements in a transaction.
* @param string optional savepoint name * @param string optional savepoint name
* @return void
* @throws Dibi\DriverException * @throws Dibi\DriverException
*/ */
public function commit($savepoint = NULL) public function commit(string $savepoint = NULL): void
{ {
$this->query($savepoint ? "RELEASE SAVEPOINT $savepoint" : 'COMMIT'); $this->query($savepoint ? "RELEASE SAVEPOINT $savepoint" : 'COMMIT');
} }
@@ -199,10 +189,9 @@ class Sqlite3Driver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Rollback changes in a transaction. * Rollback changes in a transaction.
* @param string optional savepoint name * @param string optional savepoint name
* @return void
* @throws Dibi\DriverException * @throws Dibi\DriverException
*/ */
public function rollback($savepoint = NULL) public function rollback(string $savepoint = NULL): void
{ {
$this->query($savepoint ? "ROLLBACK TO SAVEPOINT $savepoint" : 'ROLLBACK'); $this->query($savepoint ? "ROLLBACK TO SAVEPOINT $savepoint" : 'ROLLBACK');
} }
@@ -210,9 +199,8 @@ class Sqlite3Driver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Returns the connection resource. * Returns the connection resource.
* @return SQLite3
*/ */
public function getResource() public function getResource(): SQLite3
{ {
return $this->connection; return $this->connection;
} }
@@ -220,9 +208,8 @@ class Sqlite3Driver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Returns the connection reflector. * Returns the connection reflector.
* @return Dibi\Reflector
*/ */
public function getReflector() public function getReflector(): Dibi\Reflector
{ {
return new SqliteReflector($this); return new SqliteReflector($this);
} }
@@ -230,10 +217,8 @@ class Sqlite3Driver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Result set driver factory. * Result set driver factory.
* @param \SQLite3Result
* @return Dibi\ResultDriver
*/ */
public function createResultDriver(\SQLite3Result $resource) public function createResultDriver(\SQLite3Result $resource): Dibi\ResultDriver
{ {
$res = clone $this; $res = clone $this;
$res->resultSet = $resource; $res->resultSet = $resource;
@@ -246,40 +231,26 @@ class Sqlite3Driver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Encodes data for use in a SQL statement. * Encodes data for use in a SQL statement.
* @param string value
* @return string encoded value
*/ */
public function escapeText($value) public function escapeText(string $value): string
{ {
return "'" . $this->connection->escapeString($value) . "'"; return "'" . $this->connection->escapeString($value) . "'";
} }
/** public function escapeBinary(string $value): string
* @param string
* @return string
*/
public function escapeBinary($value)
{ {
return "X'" . bin2hex((string) $value) . "'"; return "X'" . bin2hex((string) $value) . "'";
} }
/** public function escapeIdentifier(string $value): string
* @param string
* @return string
*/
public function escapeIdentifier($value)
{ {
return '[' . strtr($value, '[]', ' ') . ']'; return '[' . strtr($value, '[]', ' ') . ']';
} }
/** public function escapeBool(bool $value): string
* @param bool
* @return string
*/
public function escapeBool($value)
{ {
return $value ? '1' : '0'; return $value ? '1' : '0';
} }
@@ -287,9 +258,8 @@ class Sqlite3Driver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* @param \DateTimeInterface|string|int * @param \DateTimeInterface|string|int
* @return string
*/ */
public function escapeDate($value) public function escapeDate($value): string
{ {
if (!$value instanceof \DateTimeInterface) { if (!$value instanceof \DateTimeInterface) {
$value = new Dibi\DateTime($value); $value = new Dibi\DateTime($value);
@@ -300,9 +270,8 @@ class Sqlite3Driver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* @param \DateTimeInterface|string|int * @param \DateTimeInterface|string|int
* @return string
*/ */
public function escapeDateTime($value) public function escapeDateTime($value): string
{ {
if (!$value instanceof \DateTimeInterface) { if (!$value instanceof \DateTimeInterface) {
$value = new Dibi\DateTime($value); $value = new Dibi\DateTime($value);
@@ -313,11 +282,8 @@ class Sqlite3Driver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Encodes string for use in a LIKE statement. * Encodes string for use in a LIKE statement.
* @param string
* @param int
* @return string
*/ */
public function escapeLike($value, $pos) public function escapeLike(string $value, int $pos): string
{ {
$value = addcslashes($this->connection->escapeString($value), '%_\\'); $value = addcslashes($this->connection->escapeString($value), '%_\\');
return ($pos <= 0 ? "'%" : "'") . $value . ($pos >= 0 ? "%'" : "'") . " ESCAPE '\\'"; return ($pos <= 0 ? "'%" : "'") . $value . ($pos >= 0 ? "%'" : "'") . " ESCAPE '\\'";
@@ -326,10 +292,8 @@ class Sqlite3Driver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Decodes data from result set. * Decodes data from result set.
* @param string
* @return string
*/ */
public function unescapeBinary($value) public function unescapeBinary(string $value): string
{ {
return $value; return $value;
} }
@@ -337,12 +301,8 @@ class Sqlite3Driver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Injects LIMIT/OFFSET to the SQL query. * Injects LIMIT/OFFSET to the SQL query.
* @param string
* @param int|NULL
* @param int|NULL
* @return void
*/ */
public function applyLimit(&$sql, $limit, $offset) public function applyLimit(string &$sql, ?int $limit, ?int $offset): void
{ {
if ($limit < 0 || $offset < 0) { if ($limit < 0 || $offset < 0) {
throw new Dibi\NotSupportedException('Negative offset or limit.'); throw new Dibi\NotSupportedException('Negative offset or limit.');
@@ -359,7 +319,6 @@ class Sqlite3Driver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Automatically frees the resources allocated for this result set. * Automatically frees the resources allocated for this result set.
* @return void
*/ */
public function __destruct() public function __destruct()
{ {
@@ -369,10 +328,9 @@ class Sqlite3Driver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Returns the number of rows in a result set. * Returns the number of rows in a result set.
* @return int
* @throws Dibi\NotSupportedException * @throws Dibi\NotSupportedException
*/ */
public function getRowCount() public function getRowCount(): int
{ {
throw new Dibi\NotSupportedException('Row count is not available for unbuffered queries.'); throw new Dibi\NotSupportedException('Row count is not available for unbuffered queries.');
} }
@@ -381,9 +339,8 @@ class Sqlite3Driver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Fetches the row at current position and moves the internal cursor to the next position. * Fetches the row at current position and moves the internal cursor to the next position.
* @param bool TRUE for associative array, FALSE for numeric * @param bool TRUE for associative array, FALSE for numeric
* @return array|NULL array on success, NULL if no next record
*/ */
public function fetch($assoc) public function fetch(bool $assoc): ?array
{ {
$row = $this->resultSet->fetchArray($assoc ? SQLITE3_ASSOC : SQLITE3_NUM); $row = $this->resultSet->fetchArray($assoc ? SQLITE3_ASSOC : SQLITE3_NUM);
if (!$row) { if (!$row) {
@@ -406,11 +363,9 @@ class Sqlite3Driver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Moves cursor position without fetching row. * Moves cursor position without fetching row.
* @param int the 0-based cursor pos to seek to
* @return bool TRUE on success, FALSE if unable to seek to specified record
* @throws Dibi\NotSupportedException * @throws Dibi\NotSupportedException
*/ */
public function seek($row) public function seek(int $row): bool
{ {
throw new Dibi\NotSupportedException('Cannot seek an unbuffered result set.'); throw new Dibi\NotSupportedException('Cannot seek an unbuffered result set.');
} }
@@ -418,9 +373,8 @@ class Sqlite3Driver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Frees the resources allocated for this result set. * Frees the resources allocated for this result set.
* @return void
*/ */
public function free() public function free(): void
{ {
$this->resultSet->finalize(); $this->resultSet->finalize();
$this->resultSet = NULL; $this->resultSet = NULL;
@@ -429,9 +383,8 @@ class Sqlite3Driver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Returns metadata for all columns in a result set. * Returns metadata for all columns in a result set.
* @return array
*/ */
public function getResultColumns() public function getResultColumns(): array
{ {
$count = $this->resultSet->numColumns(); $count = $this->resultSet->numColumns();
$columns = []; $columns = [];
@@ -450,9 +403,8 @@ class Sqlite3Driver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Returns the result set resource. * Returns the result set resource.
* @return \SQLite3Result|NULL
*/ */
public function getResultResource() public function getResultResource(): ?\SQLite3Result
{ {
$this->autoFree = FALSE; $this->autoFree = FALSE;
return $this->resultSet; return $this->resultSet;
@@ -465,11 +417,10 @@ class Sqlite3Driver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Registers an user defined function for use in SQL statements. * Registers an user defined function for use in SQL statements.
* @param string function name * @param string function name
* @param mixed callback * @param mixed
* @param int num of arguments * @param int num of arguments
* @return void
*/ */
public function registerFunction($name, callable $callback, $numArgs = -1) public function registerFunction(string $name, callable $callback, int $numArgs = -1): void
{ {
$this->connection->createFunction($name, $callback, $numArgs); $this->connection->createFunction($name, $callback, $numArgs);
} }
@@ -481,9 +432,8 @@ class Sqlite3Driver implements Dibi\Driver, Dibi\ResultDriver
* @param mixed callback called for each row of the result set * @param mixed callback called for each row of the result set
* @param mixed callback called to aggregate the "stepped" data from each row * @param mixed callback called to aggregate the "stepped" data from each row
* @param int num of arguments * @param int num of arguments
* @return void
*/ */
public function registerAggregateFunction($name, callable $rowCallback, callable $agrCallback, $numArgs = -1) public function registerAggregateFunction(string $name, callable $rowCallback, callable $agrCallback, int $numArgs = -1): void
{ {
$this->connection->createAggregate($name, $rowCallback, $agrCallback, $numArgs); $this->connection->createAggregate($name, $rowCallback, $agrCallback, $numArgs);
} }

View File

@@ -30,9 +30,8 @@ class SqliteReflector implements Dibi\Reflector
/** /**
* Returns list of tables. * Returns list of tables.
* @return array
*/ */
public function getTables() public function getTables(): array
{ {
$res = $this->driver->query(" $res = $this->driver->query("
SELECT name, type = 'view' as view FROM sqlite_master WHERE type IN ('table', 'view') SELECT name, type = 'view' as view FROM sqlite_master WHERE type IN ('table', 'view')
@@ -50,10 +49,8 @@ class SqliteReflector implements Dibi\Reflector
/** /**
* Returns metadata for all columns in a table. * Returns metadata for all columns in a table.
* @param string
* @return array
*/ */
public function getColumns($table) public function getColumns(string $table): array
{ {
$res = $this->driver->query("PRAGMA table_info({$this->driver->escapeIdentifier($table)})"); $res = $this->driver->query("PRAGMA table_info({$this->driver->escapeIdentifier($table)})");
$columns = []; $columns = [];
@@ -78,10 +75,8 @@ class SqliteReflector implements Dibi\Reflector
/** /**
* Returns metadata for all indexes in a table. * Returns metadata for all indexes in a table.
* @param string
* @return array
*/ */
public function getIndexes($table) public function getIndexes(string $table): array
{ {
$res = $this->driver->query("PRAGMA index_list({$this->driver->escapeIdentifier($table)})"); $res = $this->driver->query("PRAGMA index_list({$this->driver->escapeIdentifier($table)})");
$indexes = []; $indexes = [];
@@ -129,10 +124,8 @@ class SqliteReflector implements Dibi\Reflector
/** /**
* Returns metadata for all foreign keys in a table. * Returns metadata for all foreign keys in a table.
* @param string
* @return array
*/ */
public function getForeignKeys($table) public function getForeignKeys(string $table): array
{ {
$res = $this->driver->query("PRAGMA foreign_key_list({$this->driver->escapeIdentifier($table)})"); $res = $this->driver->query("PRAGMA foreign_key_list({$this->driver->escapeIdentifier($table)})");
$keys = []; $keys = [];

View File

@@ -58,10 +58,9 @@ class SqlsrvDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Connects to a database. * Connects to a database.
* @return void
* @throws Dibi\Exception * @throws Dibi\Exception
*/ */
public function connect(array &$config) public function connect(array &$config): void
{ {
Helpers::alias($config, 'options|UID', 'username'); Helpers::alias($config, 'options|UID', 'username');
Helpers::alias($config, 'options|PWD', 'password'); Helpers::alias($config, 'options|PWD', 'password');
@@ -93,9 +92,8 @@ class SqlsrvDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Disconnects from a database. * Disconnects from a database.
* @return void
*/ */
public function disconnect() public function disconnect(): void
{ {
@sqlsrv_close($this->connection); // @ - connection can be already disconnected @sqlsrv_close($this->connection); // @ - connection can be already disconnected
} }
@@ -104,10 +102,9 @@ class SqlsrvDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Executes the SQL query. * Executes the SQL query.
* @param string SQL statement. * @param string SQL statement.
* @return Dibi\ResultDriver|NULL
* @throws Dibi\DriverException * @throws Dibi\DriverException
*/ */
public function query($sql) public function query(string $sql): ?Dibi\ResultDriver
{ {
$this->affectedRows = NULL; $this->affectedRows = NULL;
$res = sqlsrv_query($this->connection, $sql); $res = sqlsrv_query($this->connection, $sql);
@@ -126,9 +123,8 @@ class SqlsrvDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Gets the number of affected rows by the last INSERT, UPDATE or DELETE query. * Gets the number of affected rows by the last INSERT, UPDATE or DELETE query.
* @return int|NULL number of rows or NULL on error
*/ */
public function getAffectedRows() public function getAffectedRows(): ?int
{ {
return $this->affectedRows; return $this->affectedRows;
} }
@@ -136,9 +132,8 @@ class SqlsrvDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query. * Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query.
* @return int|NULL int on success or NULL on failure
*/ */
public function getInsertId($sequence) public function getInsertId(?string $sequence): ?int
{ {
$res = sqlsrv_query($this->connection, 'SELECT SCOPE_IDENTITY()'); $res = sqlsrv_query($this->connection, 'SELECT SCOPE_IDENTITY()');
if (is_resource($res)) { if (is_resource($res)) {
@@ -152,10 +147,9 @@ class SqlsrvDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Begins a transaction (if supported). * Begins a transaction (if supported).
* @param string optional savepoint name * @param string optional savepoint name
* @return void
* @throws Dibi\DriverException * @throws Dibi\DriverException
*/ */
public function begin($savepoint = NULL) public function begin(string $savepoint = NULL): void
{ {
sqlsrv_begin_transaction($this->connection); sqlsrv_begin_transaction($this->connection);
} }
@@ -164,10 +158,9 @@ class SqlsrvDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Commits statements in a transaction. * Commits statements in a transaction.
* @param string optional savepoint name * @param string optional savepoint name
* @return void
* @throws Dibi\DriverException * @throws Dibi\DriverException
*/ */
public function commit($savepoint = NULL) public function commit(string $savepoint = NULL): void
{ {
sqlsrv_commit($this->connection); sqlsrv_commit($this->connection);
} }
@@ -176,10 +169,9 @@ class SqlsrvDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Rollback changes in a transaction. * Rollback changes in a transaction.
* @param string optional savepoint name * @param string optional savepoint name
* @return void
* @throws Dibi\DriverException * @throws Dibi\DriverException
*/ */
public function rollback($savepoint = NULL) public function rollback(string $savepoint = NULL): void
{ {
sqlsrv_rollback($this->connection); sqlsrv_rollback($this->connection);
} }
@@ -197,9 +189,8 @@ class SqlsrvDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Returns the connection reflector. * Returns the connection reflector.
* @return Dibi\Reflector
*/ */
public function getReflector() public function getReflector(): Dibi\Reflector
{ {
return new SqlsrvReflector($this); return new SqlsrvReflector($this);
} }
@@ -208,9 +199,8 @@ class SqlsrvDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Result set driver factory. * Result set driver factory.
* @param resource * @param resource
* @return Dibi\ResultDriver
*/ */
public function createResultDriver($resource) public function createResultDriver($resource): Dibi\ResultDriver
{ {
$res = clone $this; $res = clone $this;
$res->resultSet = $resource; $res->resultSet = $resource;
@@ -223,41 +213,27 @@ class SqlsrvDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Encodes data for use in a SQL statement. * Encodes data for use in a SQL statement.
* @param string value
* @return string encoded value
*/ */
public function escapeText($value) public function escapeText(string $value): string
{ {
return "'" . str_replace("'", "''", $value) . "'"; return "'" . str_replace("'", "''", $value) . "'";
} }
/** public function escapeBinary(string $value): string
* @param string
* @return string
*/
public function escapeBinary($value)
{ {
return "'" . str_replace("'", "''", $value) . "'"; return "'" . str_replace("'", "''", $value) . "'";
} }
/** public function escapeIdentifier(string $value): string
* @param string
* @return string
*/
public function escapeIdentifier($value)
{ {
// @see https://msdn.microsoft.com/en-us/library/ms176027.aspx // @see https://msdn.microsoft.com/en-us/library/ms176027.aspx
return '[' . str_replace(']', ']]', $value) . ']'; return '[' . str_replace(']', ']]', $value) . ']';
} }
/** public function escapeBool(bool $value): string
* @param bool
* @return string
*/
public function escapeBool($value)
{ {
return $value ? '1' : '0'; return $value ? '1' : '0';
} }
@@ -265,9 +241,8 @@ class SqlsrvDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* @param \DateTimeInterface|string|int * @param \DateTimeInterface|string|int
* @return string
*/ */
public function escapeDate($value) public function escapeDate($value): string
{ {
if (!$value instanceof \DateTimeInterface) { if (!$value instanceof \DateTimeInterface) {
$value = new Dibi\DateTime($value); $value = new Dibi\DateTime($value);
@@ -278,9 +253,8 @@ class SqlsrvDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* @param \DateTimeInterface|string|int * @param \DateTimeInterface|string|int
* @return string
*/ */
public function escapeDateTime($value) public function escapeDateTime($value): string
{ {
if (!$value instanceof \DateTimeInterface) { if (!$value instanceof \DateTimeInterface) {
$value = new Dibi\DateTime($value); $value = new Dibi\DateTime($value);
@@ -291,11 +265,8 @@ class SqlsrvDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Encodes string for use in a LIKE statement. * Encodes string for use in a LIKE statement.
* @param string
* @param int
* @return string
*/ */
public function escapeLike($value, $pos) public function escapeLike(string $value, int $pos): string
{ {
$value = strtr($value, ["'" => "''", '%' => '[%]', '_' => '[_]', '[' => '[[]']); $value = strtr($value, ["'" => "''", '%' => '[%]', '_' => '[_]', '[' => '[[]']);
return ($pos <= 0 ? "'%" : "'") . $value . ($pos >= 0 ? "%'" : "'"); return ($pos <= 0 ? "'%" : "'") . $value . ($pos >= 0 ? "%'" : "'");
@@ -304,10 +275,8 @@ class SqlsrvDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Decodes data from result set. * Decodes data from result set.
* @param string
* @return string
*/ */
public function unescapeBinary($value) public function unescapeBinary(string $value): string
{ {
return $value; return $value;
} }
@@ -315,12 +284,8 @@ class SqlsrvDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Injects LIMIT/OFFSET to the SQL query. * Injects LIMIT/OFFSET to the SQL query.
* @param string
* @param int|NULL
* @param int|NULL
* @return void
*/ */
public function applyLimit(&$sql, $limit, $offset) public function applyLimit(string &$sql, ?int $limit, ?int $offset): void
{ {
if ($limit < 0 || $offset < 0) { if ($limit < 0 || $offset < 0) {
throw new Dibi\NotSupportedException('Negative offset or limit.'); throw new Dibi\NotSupportedException('Negative offset or limit.');
@@ -348,7 +313,6 @@ class SqlsrvDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Automatically frees the resources allocated for this result set. * Automatically frees the resources allocated for this result set.
* @return void
*/ */
public function __destruct() public function __destruct()
{ {
@@ -358,9 +322,8 @@ class SqlsrvDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Returns the number of rows in a result set. * Returns the number of rows in a result set.
* @return int
*/ */
public function getRowCount() public function getRowCount(): int
{ {
throw new Dibi\NotSupportedException('Row count is not available for unbuffered queries.'); throw new Dibi\NotSupportedException('Row count is not available for unbuffered queries.');
} }
@@ -369,9 +332,8 @@ class SqlsrvDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Fetches the row at current position and moves the internal cursor to the next position. * Fetches the row at current position and moves the internal cursor to the next position.
* @param bool TRUE for associative array, FALSE for numeric * @param bool TRUE for associative array, FALSE for numeric
* @return array|NULL array on success, NULL if no next record
*/ */
public function fetch($assoc) public function fetch(bool $assoc): ?array
{ {
return sqlsrv_fetch_array($this->resultSet, $assoc ? SQLSRV_FETCH_ASSOC : SQLSRV_FETCH_NUMERIC); return sqlsrv_fetch_array($this->resultSet, $assoc ? SQLSRV_FETCH_ASSOC : SQLSRV_FETCH_NUMERIC);
} }
@@ -379,10 +341,8 @@ class SqlsrvDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Moves cursor position without fetching row. * Moves cursor position without fetching row.
* @param int the 0-based cursor pos to seek to
* @return bool TRUE on success, FALSE if unable to seek to specified record
*/ */
public function seek($row) public function seek(int $row): bool
{ {
throw new Dibi\NotSupportedException('Cannot seek an unbuffered result set.'); throw new Dibi\NotSupportedException('Cannot seek an unbuffered result set.');
} }
@@ -390,9 +350,8 @@ class SqlsrvDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Frees the resources allocated for this result set. * Frees the resources allocated for this result set.
* @return void
*/ */
public function free() public function free(): void
{ {
sqlsrv_free_stmt($this->resultSet); sqlsrv_free_stmt($this->resultSet);
$this->resultSet = NULL; $this->resultSet = NULL;
@@ -401,9 +360,8 @@ class SqlsrvDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Returns metadata for all columns in a result set. * Returns metadata for all columns in a result set.
* @return array
*/ */
public function getResultColumns() public function getResultColumns(): array
{ {
$columns = []; $columns = [];
foreach ((array) sqlsrv_field_metadata($this->resultSet) as $fieldMetadata) { foreach ((array) sqlsrv_field_metadata($this->resultSet) as $fieldMetadata) {

View File

@@ -30,9 +30,8 @@ class SqlsrvReflector implements Dibi\Reflector
/** /**
* Returns list of tables. * Returns list of tables.
* @return array
*/ */
public function getTables() public function getTables(): array
{ {
$res = $this->driver->query("SELECT TABLE_NAME, TABLE_TYPE FROM INFORMATION_SCHEMA.TABLES WHERE [TABLE_SCHEMA] = 'dbo'"); $res = $this->driver->query("SELECT TABLE_NAME, TABLE_TYPE FROM INFORMATION_SCHEMA.TABLES WHERE [TABLE_SCHEMA] = 'dbo'");
$tables = []; $tables = [];
@@ -48,10 +47,8 @@ class SqlsrvReflector implements Dibi\Reflector
/** /**
* Returns metadata for all columns in a table. * Returns metadata for all columns in a table.
* @param string
* @return array
*/ */
public function getColumns($table) public function getColumns(string $table): array
{ {
$res = $this->driver->query(" $res = $this->driver->query("
SELECT c.name as COLUMN_NAME, c.is_identity AS AUTO_INCREMENT SELECT c.name as COLUMN_NAME, c.is_identity AS AUTO_INCREMENT
@@ -100,10 +97,8 @@ class SqlsrvReflector implements Dibi\Reflector
/** /**
* Returns metadata for all indexes in a table. * Returns metadata for all indexes in a table.
* @param string
* @return array
*/ */
public function getIndexes($table) public function getIndexes(string $table): array
{ {
$keyUsagesRes = $this->driver->query(sprintf("EXEC [sys].[sp_helpindex] @objname = N%s", $this->driver->escapeText($table))); $keyUsagesRes = $this->driver->query(sprintf("EXEC [sys].[sp_helpindex] @objname = N%s", $this->driver->escapeText($table)));
$keyUsages = []; $keyUsages = [];
@@ -125,10 +120,8 @@ class SqlsrvReflector implements Dibi\Reflector
/** /**
* Returns metadata for all foreign keys in a table. * Returns metadata for all foreign keys in a table.
* @param string
* @return array
*/ */
public function getForeignKeys($table) public function getForeignKeys(string $table): array
{ {
throw new Dibi\NotImplementedException; throw new Dibi\NotImplementedException;
} }

View File

@@ -102,9 +102,6 @@ class Fluent implements IDataSource
private static $normalizer; private static $normalizer;
/**
* @param Connection
*/
public function __construct(Connection $connection) public function __construct(Connection $connection)
{ {
$this->connection = $connection; $this->connection = $connection;
@@ -119,9 +116,8 @@ class Fluent implements IDataSource
* Appends new argument to the clause. * Appends new argument to the clause.
* @param string clause name * @param string clause name
* @param array arguments * @param array arguments
* @return self
*/ */
public function __call($clause, $args) public function __call(string $clause, array $args): self
{ {
$clause = self::$normalizer->$clause; $clause = self::$normalizer->$clause;
@@ -207,9 +203,8 @@ class Fluent implements IDataSource
/** /**
* Switch to a clause. * Switch to a clause.
* @param string clause name * @param string clause name
* @return self
*/ */
public function clause($clause) public function clause(string $clause): self
{ {
$this->cursor = &$this->clauses[self::$normalizer->$clause]; $this->cursor = &$this->clauses[self::$normalizer->$clause];
if ($this->cursor === NULL) { if ($this->cursor === NULL) {
@@ -223,9 +218,8 @@ class Fluent implements IDataSource
/** /**
* Removes a clause. * Removes a clause.
* @param string clause name * @param string clause name
* @return self
*/ */
public function removeClause($clause) public function removeClause(string $clause): self
{ {
$this->clauses[self::$normalizer->$clause] = NULL; $this->clauses[self::$normalizer->$clause] = NULL;
return $this; return $this;
@@ -235,10 +229,8 @@ class Fluent implements IDataSource
/** /**
* Change a SQL flag. * Change a SQL flag.
* @param string flag name * @param string flag name
* @param bool value
* @return self
*/ */
public function setFlag($flag, $value = TRUE) public function setFlag(string $flag, bool $value = TRUE): self
{ {
$flag = strtoupper($flag); $flag = strtoupper($flag);
if ($value) { if ($value) {
@@ -253,9 +245,8 @@ class Fluent implements IDataSource
/** /**
* Is a flag set? * Is a flag set?
* @param string flag name * @param string flag name
* @return bool
*/ */
final public function getFlag($flag) final public function getFlag(string $flag): bool
{ {
return isset($this->flags[strtoupper($flag)]); return isset($this->flags[strtoupper($flag)]);
} }
@@ -263,9 +254,8 @@ class Fluent implements IDataSource
/** /**
* Returns SQL command. * Returns SQL command.
* @return string
*/ */
final public function getCommand() final public function getCommand(): string
{ {
return $this->command; return $this->command;
} }
@@ -273,9 +263,8 @@ class Fluent implements IDataSource
/** /**
* Returns the dibi connection. * Returns the dibi connection.
* @return Connection
*/ */
final public function getConnection() final public function getConnection(): Connection
{ {
return $this->connection; return $this->connection;
} }
@@ -283,11 +272,9 @@ class Fluent implements IDataSource
/** /**
* Adds Result setup. * Adds Result setup.
* @param string method
* @param mixed args * @param mixed args
* @return self
*/ */
public function setupResult($method) public function setupResult(string $method): self
{ {
$this->setups[] = func_get_args(); $this->setups[] = func_get_args();
return $this; return $this;
@@ -347,11 +334,8 @@ class Fluent implements IDataSource
/** /**
* Fetches all records from table. * Fetches all records from table.
* @param int offset
* @param int limit
* @return array
*/ */
public function fetchAll($offset = NULL, $limit = NULL) public function fetchAll(int $offset = NULL, int $limit = NULL): array
{ {
return $this->query($this->_export(NULL, ['%ofs %lmt', $offset, $limit]))->fetchAll(); return $this->query($this->_export(NULL, ['%ofs %lmt', $offset, $limit]))->fetchAll();
} }
@@ -360,9 +344,8 @@ class Fluent implements IDataSource
/** /**
* Fetches all records from table and returns associative tree. * Fetches all records from table and returns associative tree.
* @param string associative descriptor * @param string associative descriptor
* @return array
*/ */
public function fetchAssoc($assoc) public function fetchAssoc(string $assoc): array
{ {
return $this->query($this->_export())->fetchAssoc($assoc); return $this->query($this->_export())->fetchAssoc($assoc);
} }
@@ -371,10 +354,8 @@ class Fluent implements IDataSource
/** /**
* Fetches all records from table like $key => $value pairs. * Fetches all records from table like $key => $value pairs.
* @param string associative key * @param string associative key
* @param string value
* @return array
*/ */
public function fetchPairs($key = NULL, $value = NULL) public function fetchPairs(string $key = NULL, string $value = NULL): array
{ {
return $this->query($this->_export())->fetchPairs($key, $value); return $this->query($this->_export())->fetchPairs($key, $value);
} }
@@ -382,11 +363,8 @@ class Fluent implements IDataSource
/** /**
* Required by the IteratorAggregate interface. * Required by the IteratorAggregate interface.
* @param int offset
* @param int limit
* @return ResultIterator
*/ */
public function getIterator($offset = NULL, $limit = NULL) public function getIterator(int $offset = NULL, int $limit = NULL): ResultIterator
{ {
return $this->query($this->_export(NULL, ['%ofs %lmt', $offset, $limit]))->getIterator(); return $this->query($this->_export(NULL, ['%ofs %lmt', $offset, $limit]))->getIterator();
} }
@@ -395,18 +373,14 @@ class Fluent implements IDataSource
/** /**
* Generates and prints SQL query or it's part. * Generates and prints SQL query or it's part.
* @param string clause name * @param string clause name
* @return bool
*/ */
public function test($clause = NULL) public function test(string $clause = NULL): bool
{ {
return $this->connection->test($this->_export($clause)); return $this->connection->test($this->_export($clause));
} }
/** public function count(): int
* @return int
*/
public function count()
{ {
return (int) $this->query([ return (int) $this->query([
'SELECT COUNT(*) FROM (%ex', $this->_export(), ') [data]', 'SELECT COUNT(*) FROM (%ex', $this->_export(), ') [data]',
@@ -414,10 +388,7 @@ class Fluent implements IDataSource
} }
/** private function query($args): Result
* @return Result
*/
private function query($args)
{ {
$res = $this->connection->query($args); $res = $this->connection->query($args);
foreach ($this->setups as $setup) { foreach ($this->setups as $setup) {
@@ -431,10 +402,7 @@ class Fluent implements IDataSource
/********************* exporting ****************d*g**/ /********************* exporting ****************d*g**/
/** public function toDataSource(): DataSource
* @return DataSource
*/
public function toDataSource()
{ {
return new DataSource($this->connection->translate($this->_export()), $this->connection); return new DataSource($this->connection->translate($this->_export()), $this->connection);
} }
@@ -442,9 +410,8 @@ class Fluent implements IDataSource
/** /**
* Returns SQL query. * Returns SQL query.
* @return string
*/ */
final public function __toString() final public function __toString(): string
{ {
try { try {
return $this->connection->translate($this->_export()); return $this->connection->translate($this->_export());
@@ -457,9 +424,8 @@ class Fluent implements IDataSource
/** /**
* Generates parameters for Translator. * Generates parameters for Translator.
* @param string clause name * @param string clause name
* @return array
*/ */
protected function _export($clause = NULL, $args = []) protected function _export(string $clause = NULL, array $args = []): array
{ {
if ($clause === NULL) { if ($clause === NULL) {
$data = $this->clauses; $data = $this->clauses;
@@ -495,11 +461,9 @@ class Fluent implements IDataSource
/** /**
* Format camelCase clause name to UPPER CASE. * Format camelCase clause name to UPPER CASE.
* @param string
* @return string
* @internal * @internal
*/ */
public static function _formatClause($s) public static function _formatClause(string $s): string
{ {
if ($s === 'order' || $s === 'group') { if ($s === 'order' || $s === 'group') {
$s .= 'By'; $s .= 'By';

View File

@@ -19,9 +19,8 @@ class Helpers
* Prints out a syntax highlighted version of the SQL command or Result. * Prints out a syntax highlighted version of the SQL command or Result.
* @param string|Result * @param string|Result
* @param bool return output instead of printing it? * @param bool return output instead of printing it?
* @return string
*/ */
public static function dump($sql = NULL, $return = FALSE) public static function dump($sql = NULL, bool $return = FALSE): string
{ {
ob_start(); ob_start();
if ($sql instanceof Result && PHP_SAPI === 'cli') { if ($sql instanceof Result && PHP_SAPI === 'cli') {
@@ -135,10 +134,9 @@ class Helpers
/** /**
* Finds the best suggestion. * Finds the best suggestion.
* @return string|NULL
* @internal * @internal
*/ */
public static function getSuggestion(array $items, $value) public static function getSuggestion(array $items, $value): ?string
{ {
$best = NULL; $best = NULL;
$min = (strlen($value) / 4 + 1) * 10 + .1; $min = (strlen($value) / 4 + 1) * 10 + .1;
@@ -174,11 +172,9 @@ class Helpers
/** /**
* Heuristic type detection. * Heuristic type detection.
* @param string
* @return string|NULL
* @internal * @internal
*/ */
public static function detectType($type) public static function detectType(string $type): ?string
{ {
static $patterns = [ static $patterns = [
'^_' => Type::TEXT, // PostgreSQL arrays '^_' => Type::TEXT, // PostgreSQL arrays
@@ -216,11 +212,9 @@ class Helpers
/** /**
* Apply configuration alias or default values. * Apply configuration alias or default values.
* @param array connect configuration * @param array connect configuration
* @param string key * @param string $alias alias key
* @param string alias key
* @return void
*/ */
public static function alias(&$config, $key, $alias) public static function alias(array &$config, string $key, string $alias): void
{ {
$foo = &$config; $foo = &$config;
foreach (explode('|', $key) as $key) { foreach (explode('|', $key) as $key) {
@@ -238,7 +232,7 @@ class Helpers
* Import SQL dump from file. * Import SQL dump from file.
* @return int count of sql commands * @return int count of sql commands
*/ */
public static function loadFromFile(Connection $connection, $file, callable $onProgress = NULL) public static function loadFromFile(Connection $connection, $file, callable $onProgress = NULL): int
{ {
@set_time_limit(0); // intentionally @ @set_time_limit(0); // intentionally @

View File

@@ -25,10 +25,7 @@ class Literal
} }
/** public function __toString(): string
* @return string
*/
public function __toString()
{ {
return $this->value; return $this->value;
} }

View File

@@ -24,7 +24,7 @@ class FileLogger
public $filter; public $filter;
public function __construct($file, $filter = NULL) public function __construct(string $file, int $filter = NULL)
{ {
$this->file = $file; $this->file = $file;
$this->filter = $filter ? (int) $filter : Dibi\Event::QUERY; $this->filter = $filter ? (int) $filter : Dibi\Event::QUERY;
@@ -33,9 +33,8 @@ class FileLogger
/** /**
* After event notification. * After event notification.
* @return void
*/ */
public function logEvent(Dibi\Event $event) public function logEvent(Dibi\Event $event): void
{ {
if (($event->type & $this->filter) === 0) { if (($event->type & $this->filter) === 0) {
return; return;

View File

@@ -39,16 +39,13 @@ class FirePhpLogger
private static $fireTable = [['Time', 'SQL Statement', 'Rows', 'Connection']]; private static $fireTable = [['Time', 'SQL Statement', 'Rows', 'Connection']];
/** public static function isAvailable(): bool
* @return bool
*/
public static function isAvailable()
{ {
return isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], 'FirePHP/'); return isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], 'FirePHP/');
} }
public function __construct($filter = NULL) public function __construct(int $filter = NULL)
{ {
$this->filter = $filter ? (int) $filter : Dibi\Event::QUERY; $this->filter = $filter ? (int) $filter : Dibi\Event::QUERY;
} }
@@ -56,9 +53,8 @@ class FirePhpLogger
/** /**
* After event notification. * After event notification.
* @return void
*/ */
public function logEvent(Dibi\Event $event) public function logEvent(Dibi\Event $event): void
{ {
if (headers_sent() || ($event->type & $this->filter) === 0 || count(self::$fireTable) > self::$maxQueries) { if (headers_sent() || ($event->type & $this->filter) === 0 || count(self::$fireTable) > self::$maxQueries) {
return; return;

View File

@@ -43,37 +43,25 @@ class Column
} }
/** public function getName(): string
* @return string
*/
public function getName()
{ {
return $this->info['name']; return $this->info['name'];
} }
/** public function getFullName(): string
* @return string
*/
public function getFullName()
{ {
return $this->info['fullname'] ?? NULL; return $this->info['fullname'] ?? NULL;
} }
/** public function hasTable(): bool
* @return bool
*/
public function hasTable()
{ {
return !empty($this->info['table']); return !empty($this->info['table']);
} }
/** public function getTable(): Table
* @return Table
*/
public function getTable()
{ {
if (empty($this->info['table']) || !$this->reflector) { if (empty($this->info['table']) || !$this->reflector) {
throw new Dibi\Exception("Table is unknown or not available."); throw new Dibi\Exception("Table is unknown or not available.");
@@ -82,64 +70,43 @@ class Column
} }
/** public function getTableName(): ?string
* @return string|NULL
*/
public function getTableName()
{ {
return isset($this->info['table']) && $this->info['table'] != NULL ? $this->info['table'] : NULL; // intentionally == return isset($this->info['table']) && $this->info['table'] != NULL ? $this->info['table'] : NULL; // intentionally ==
} }
/** public function getType(): string
* @return string
*/
public function getType()
{ {
return Dibi\Helpers::getTypeCache()->{$this->info['nativetype']}; return Dibi\Helpers::getTypeCache()->{$this->info['nativetype']};
} }
/** public function getNativeType(): string
* @return string
*/
public function getNativeType()
{ {
return $this->info['nativetype']; return $this->info['nativetype'];
} }
/** public function getSize(): ?int
* @return int|NULL
*/
public function getSize()
{ {
return isset($this->info['size']) ? (int) $this->info['size'] : NULL; return isset($this->info['size']) ? (int) $this->info['size'] : NULL;
} }
/** public function isUnsigned(): ?bool
* @return bool|NULL
*/
public function isUnsigned()
{ {
return isset($this->info['unsigned']) ? (bool) $this->info['unsigned'] : NULL; return isset($this->info['unsigned']) ? (bool) $this->info['unsigned'] : NULL;
} }
/** public function isNullable(): ?bool
* @return bool|NULL
*/
public function isNullable()
{ {
return isset($this->info['nullable']) ? (bool) $this->info['nullable'] : NULL; return isset($this->info['nullable']) ? (bool) $this->info['nullable'] : NULL;
} }
/** public function isAutoIncrement(): ?bool
* @return bool|NULL
*/
public function isAutoIncrement()
{ {
return isset($this->info['autoincrement']) ? (bool) $this->info['autoincrement'] : NULL; return isset($this->info['autoincrement']) ? (bool) $this->info['autoincrement'] : NULL;
} }
@@ -155,10 +122,9 @@ class Column
/** /**
* @param string
* @return mixed * @return mixed
*/ */
public function getVendorInfo($key) public function getVendorInfo(string $key)
{ {
return $this->info['vendor'][$key] ?? NULL; return $this->info['vendor'][$key] ?? NULL;
} }

View File

@@ -38,10 +38,7 @@ class Database
} }
/** public function getName(): string
* @return string
*/
public function getName()
{ {
return $this->name; return $this->name;
} }
@@ -50,7 +47,7 @@ class Database
/** /**
* @return Table[] * @return Table[]
*/ */
public function getTables() public function getTables(): array
{ {
$this->init(); $this->init();
return array_values($this->tables); return array_values($this->tables);
@@ -60,7 +57,7 @@ class Database
/** /**
* @return string[] * @return string[]
*/ */
public function getTableNames() public function getTableNames(): array
{ {
$this->init(); $this->init();
$res = []; $res = [];
@@ -71,11 +68,7 @@ class Database
} }
/** public function getTable(string $name): Table
* @param string
* @return Table
*/
public function getTable($name)
{ {
$this->init(); $this->init();
$l = strtolower($name); $l = strtolower($name);
@@ -88,21 +81,14 @@ class Database
} }
/** public function hasTable(string $name): bool
* @param string
* @return bool
*/
public function hasTable($name)
{ {
$this->init(); $this->init();
return isset($this->tables[strtolower($name)]); return isset($this->tables[strtolower($name)]);
} }
/** protected function init(): void
* @return void
*/
protected function init()
{ {
if ($this->tables === NULL) { if ($this->tables === NULL) {
$this->tables = []; $this->tables = [];

View File

@@ -34,19 +34,13 @@ class ForeignKey
} }
/** public function getName(): string
* @return string
*/
public function getName()
{ {
return $this->name; return $this->name;
} }
/** public function getReferences(): array
* @return array
*/
public function getReferences()
{ {
return $this->references; return $this->references;
} }

View File

@@ -32,37 +32,25 @@ class Index
} }
/** public function getName(): string
* @return string
*/
public function getName()
{ {
return $this->info['name']; return $this->info['name'];
} }
/** public function getColumns(): array
* @return array
*/
public function getColumns()
{ {
return $this->info['columns']; return $this->info['columns'];
} }
/** public function isUnique(): bool
* @return bool
*/
public function isUnique()
{ {
return !empty($this->info['unique']); return !empty($this->info['unique']);
} }
/** public function isPrimary(): bool
* @return bool
*/
public function isPrimary()
{ {
return !empty($this->info['primary']); return !empty($this->info['primary']);
} }

View File

@@ -39,7 +39,7 @@ class Result
/** /**
* @return Column[] * @return Column[]
*/ */
public function getColumns() public function getColumns(): array
{ {
$this->initColumns(); $this->initColumns();
return array_values($this->columns); return array_values($this->columns);
@@ -47,10 +47,9 @@ class Result
/** /**
* @param bool
* @return string[] * @return string[]
*/ */
public function getColumnNames($fullNames = FALSE) public function getColumnNames(bool $fullNames = FALSE): array
{ {
$this->initColumns(); $this->initColumns();
$res = []; $res = [];
@@ -61,11 +60,7 @@ class Result
} }
/** public function getColumn(string $name): Column
* @param string
* @return Column
*/
public function getColumn($name)
{ {
$this->initColumns(); $this->initColumns();
$l = strtolower($name); $l = strtolower($name);
@@ -78,21 +73,14 @@ class Result
} }
/** public function hasColumn(string $name): bool
* @param string
* @return bool
*/
public function hasColumn($name)
{ {
$this->initColumns(); $this->initColumns();
return isset($this->names[strtolower($name)]); return isset($this->names[strtolower($name)]);
} }
/** protected function initColumns(): void
* @return void
*/
protected function initColumns()
{ {
if ($this->columns === NULL) { if ($this->columns === NULL) {
$this->columns = []; $this->columns = [];

View File

@@ -55,19 +55,13 @@ class Table
} }
/** public function getName(): string
* @return string
*/
public function getName()
{ {
return $this->name; return $this->name;
} }
/** public function isView(): bool
* @return bool
*/
public function isView()
{ {
return $this->view; return $this->view;
} }
@@ -76,7 +70,7 @@ class Table
/** /**
* @return Column[] * @return Column[]
*/ */
public function getColumns() public function getColumns(): array
{ {
$this->initColumns(); $this->initColumns();
return array_values($this->columns); return array_values($this->columns);
@@ -86,7 +80,7 @@ class Table
/** /**
* @return string[] * @return string[]
*/ */
public function getColumnNames() public function getColumnNames(): array
{ {
$this->initColumns(); $this->initColumns();
$res = []; $res = [];
@@ -97,11 +91,7 @@ class Table
} }
/** public function getColumn(string $name): Column
* @param string
* @return Column
*/
public function getColumn($name)
{ {
$this->initColumns(); $this->initColumns();
$l = strtolower($name); $l = strtolower($name);
@@ -114,11 +104,7 @@ class Table
} }
/** public function hasColumn(string $name): bool
* @param string
* @return bool
*/
public function hasColumn($name)
{ {
$this->initColumns(); $this->initColumns();
return isset($this->columns[strtolower($name)]); return isset($this->columns[strtolower($name)]);
@@ -128,7 +114,7 @@ class Table
/** /**
* @return ForeignKey[] * @return ForeignKey[]
*/ */
public function getForeignKeys() public function getForeignKeys(): array
{ {
$this->initForeignKeys(); $this->initForeignKeys();
return $this->foreignKeys; return $this->foreignKeys;
@@ -138,27 +124,21 @@ class Table
/** /**
* @return Index[] * @return Index[]
*/ */
public function getIndexes() public function getIndexes(): array
{ {
$this->initIndexes(); $this->initIndexes();
return $this->indexes; return $this->indexes;
} }
/** public function getPrimaryKey(): Index
* @return Index
*/
public function getPrimaryKey()
{ {
$this->initIndexes(); $this->initIndexes();
return $this->primaryKey; return $this->primaryKey;
} }
/** protected function initColumns(): void
* @return void
*/
protected function initColumns()
{ {
if ($this->columns === NULL) { if ($this->columns === NULL) {
$this->columns = []; $this->columns = [];
@@ -169,10 +149,7 @@ class Table
} }
/** protected function initIndexes(): void
* @return void
*/
protected function initIndexes()
{ {
if ($this->indexes === NULL) { if ($this->indexes === NULL) {
$this->initColumns(); $this->initColumns();
@@ -190,10 +167,7 @@ class Table
} }
/** protected function initForeignKeys(): void
* @return void
*/
protected function initForeignKeys()
{ {
throw new Dibi\NotImplementedException; throw new Dibi\NotImplementedException;
} }

View File

@@ -52,10 +52,7 @@ class Result implements IDataSource
private $formats = []; private $formats = [];
/** public function __construct(ResultDriver $driver)
* @param ResultDriver
*/
public function __construct($driver)
{ {
$this->driver = $driver; $this->driver = $driver;
$this->detectTypes(); $this->detectTypes();
@@ -64,9 +61,8 @@ class Result implements IDataSource
/** /**
* Frees the resources allocated for this result set. * Frees the resources allocated for this result set.
* @return void
*/ */
final public function free() final public function free(): void
{ {
if ($this->driver !== NULL) { if ($this->driver !== NULL) {
$this->driver->free(); $this->driver->free();
@@ -77,10 +73,9 @@ class Result implements IDataSource
/** /**
* Safe access to property $driver. * Safe access to property $driver.
* @return ResultDriver
* @throws \RuntimeException * @throws \RuntimeException
*/ */
final public function getResultDriver() final public function getResultDriver(): ResultDriver
{ {
if ($this->driver === NULL) { if ($this->driver === NULL) {
throw new \RuntimeException('Result-set was released from memory.'); throw new \RuntimeException('Result-set was released from memory.');
@@ -95,11 +90,9 @@ class Result implements IDataSource
/** /**
* Moves cursor position without fetching row. * Moves cursor position without fetching row.
* @param int the 0-based cursor pos to seek to
* @return bool TRUE on success, FALSE if unable to seek to specified record
* @throws Exception * @throws Exception
*/ */
final public function seek($row) final public function seek(int $row): bool
{ {
return ($row !== 0 || $this->fetched) ? (bool) $this->getResultDriver()->seek($row) : TRUE; return ($row !== 0 || $this->fetched) ? (bool) $this->getResultDriver()->seek($row) : TRUE;
} }
@@ -107,9 +100,8 @@ class Result implements IDataSource
/** /**
* Required by the Countable interface. * Required by the Countable interface.
* @return int
*/ */
final public function count() final public function count(): int
{ {
return $this->getResultDriver()->getRowCount(); return $this->getResultDriver()->getRowCount();
} }
@@ -117,9 +109,8 @@ class Result implements IDataSource
/** /**
* Returns the number of rows in a result set. * Returns the number of rows in a result set.
* @return int
*/ */
final public function getRowCount() final public function getRowCount(): int
{ {
return $this->getResultDriver()->getRowCount(); return $this->getResultDriver()->getRowCount();
} }
@@ -127,9 +118,8 @@ class Result implements IDataSource
/** /**
* Required by the IteratorAggregate interface. * Required by the IteratorAggregate interface.
* @return ResultIterator
*/ */
final public function getIterator() final public function getIterator(): ResultIterator
{ {
return new ResultIterator($this); return new ResultIterator($this);
} }
@@ -140,10 +130,8 @@ class Result implements IDataSource
/** /**
* Set fetched object class. This class should extend the Row class. * Set fetched object class. This class should extend the Row class.
* @param string
* @return self
*/ */
public function setRowClass($class) public function setRowClass(string $class): self
{ {
$this->rowClass = $class; $this->rowClass = $class;
return $this; return $this;
@@ -152,9 +140,8 @@ class Result implements IDataSource
/** /**
* Returns fetched object class name. * Returns fetched object class name.
* @return string
*/ */
public function getRowClass() public function getRowClass(): string
{ {
return $this->rowClass; return $this->rowClass;
} }
@@ -162,9 +149,8 @@ class Result implements IDataSource
/** /**
* Set a factory to create fetched object instances. These should extend the Row class. * Set a factory to create fetched object instances. These should extend the Row class.
* @return self
*/ */
public function setRowFactory(callable $callback) public function setRowFactory(callable $callback): self
{ {
$this->rowFactory = $callback; $this->rowFactory = $callback;
return $this; return $this;
@@ -174,9 +160,8 @@ class Result implements IDataSource
/** /**
* Fetches the row at current position, process optional type conversion. * Fetches the row at current position, process optional type conversion.
* and moves the internal cursor to the next position * and moves the internal cursor to the next position
* @return Row|NULL
*/ */
final public function fetch() final public function fetch(): ?Row
{ {
$row = $this->getResultDriver()->fetch(TRUE); $row = $this->getResultDriver()->fetch(TRUE);
if ($row === NULL) { if ($row === NULL) {
@@ -211,11 +196,9 @@ class Result implements IDataSource
/** /**
* Fetches all records from table. * Fetches all records from table.
* @param int offset
* @param int limit
* @return Row[] * @return Row[]
*/ */
final public function fetchAll($offset = NULL, $limit = NULL) final public function fetchAll(int $offset = NULL, int $limit = NULL): array
{ {
$limit = $limit === NULL ? -1 : (int) $limit; $limit = $limit === NULL ? -1 : (int) $limit;
$this->seek((int) $offset); $this->seek((int) $offset);
@@ -245,10 +228,9 @@ class Result implements IDataSource
* - associative descriptor: col1|col2->col3=col4 * - associative descriptor: col1|col2->col3=col4
* builds a tree: $tree[$val1][$val2]->col3[$val3] = val4 * builds a tree: $tree[$val1][$val2]->col3[$val3] = val4
* @param string associative descriptor * @param string associative descriptor
* @return array
* @throws \InvalidArgumentException * @throws \InvalidArgumentException
*/ */
final public function fetchAssoc($assoc) final public function fetchAssoc(string $assoc): array
{ {
if (strpos($assoc, ',') !== FALSE) { if (strpos($assoc, ',') !== FALSE) {
return $this->oldFetchAssoc($assoc); return $this->oldFetchAssoc($assoc);
@@ -393,11 +375,9 @@ class Result implements IDataSource
/** /**
* Fetches all records from table like $key => $value pairs. * Fetches all records from table like $key => $value pairs.
* @param string associative key * @param string associative key
* @param string value
* @return array
* @throws \InvalidArgumentException * @throws \InvalidArgumentException
*/ */
final public function fetchPairs($key = NULL, $value = NULL) final public function fetchPairs(string $key = NULL, string $value = NULL): array
{ {
$this->seek(0); $this->seek(0);
$row = $this->fetch(); $row = $this->fetch();
@@ -454,9 +434,8 @@ class Result implements IDataSource
/** /**
* Autodetect column types. * Autodetect column types.
* @return void
*/ */
private function detectTypes() private function detectTypes(): void
{ {
$cache = Helpers::getTypeCache(); $cache = Helpers::getTypeCache();
try { try {
@@ -470,10 +449,8 @@ class Result implements IDataSource
/** /**
* Converts values to specified type and format. * Converts values to specified type and format.
* @param array
* @return void
*/ */
private function normalize(array &$row) private function normalize(array &$row): void
{ {
foreach ($this->types as $key => $type) { foreach ($this->types as $key => $type) {
if (!isset($row[$key])) { // NULL if (!isset($row[$key])) { // NULL
@@ -528,9 +505,8 @@ class Result implements IDataSource
* Define column type. * Define column type.
* @param string column * @param string column
* @param string type (use constant Type::*) * @param string type (use constant Type::*)
* @return self
*/ */
final public function setType($col, $type) final public function setType(string $col, string $type): self
{ {
$this->types[$col] = $type; $this->types[$col] = $type;
return $this; return $this;
@@ -539,9 +515,8 @@ class Result implements IDataSource
/** /**
* Returns column type. * Returns column type.
* @return string
*/ */
final public function getType($col) final public function getType($col): string
{ {
return $this->types[$col] ?? NULL; return $this->types[$col] ?? NULL;
} }
@@ -550,10 +525,8 @@ class Result implements IDataSource
/** /**
* Sets date format. * Sets date format.
* @param string * @param string
* @param string|NULL format
* @return self
*/ */
final public function setFormat($type, $format) final public function setFormat(string $type, ?string $format): self
{ {
$this->formats[$type] = $format; $this->formats[$type] = $format;
return $this; return $this;
@@ -562,9 +535,8 @@ class Result implements IDataSource
/** /**
* Returns data format. * Returns data format.
* @return string|NULL
*/ */
final public function getFormat($type) final public function getFormat($type): ?string
{ {
return $this->formats[$type] ?? NULL; return $this->formats[$type] ?? NULL;
} }
@@ -575,9 +547,8 @@ class Result implements IDataSource
/** /**
* Returns a meta information about the current result set. * Returns a meta information about the current result set.
* @return Reflection\Result
*/ */
public function getInfo() public function getInfo(): Reflection\Result
{ {
if ($this->meta === NULL) { if ($this->meta === NULL) {
$this->meta = new Reflection\Result($this->getResultDriver()); $this->meta = new Reflection\Result($this->getResultDriver());
@@ -589,7 +560,7 @@ class Result implements IDataSource
/** /**
* @return Reflection\Column[] * @return Reflection\Column[]
*/ */
final public function getColumns() final public function getColumns(): array
{ {
return $this->getInfo()->getColumns(); return $this->getInfo()->getColumns();
} }
@@ -600,9 +571,8 @@ class Result implements IDataSource
/** /**
* Displays complete result set as HTML or text table for debug purposes. * Displays complete result set as HTML or text table for debug purposes.
* @return void
*/ */
final public function dump() final public function dump(): void
{ {
echo Helpers::dump($this); echo Helpers::dump($this);
} }

View File

@@ -34,9 +34,6 @@ class ResultIterator implements \Iterator, \Countable
private $pointer; private $pointer;
/**
* @param Result
*/
public function __construct(Result $result) public function __construct(Result $result)
{ {
$this->result = $result; $this->result = $result;
@@ -45,9 +42,8 @@ class ResultIterator implements \Iterator, \Countable
/** /**
* Rewinds the iterator to the first element. * Rewinds the iterator to the first element.
* @return void
*/ */
public function rewind() public function rewind(): void
{ {
$this->pointer = 0; $this->pointer = 0;
$this->result->seek(0); $this->result->seek(0);
@@ -77,9 +73,8 @@ class ResultIterator implements \Iterator, \Countable
/** /**
* Moves forward to next element. * Moves forward to next element.
* @return void
*/ */
public function next() public function next(): void
{ {
$this->row = $this->result->fetch(); $this->row = $this->result->fetch();
$this->pointer++; $this->pointer++;
@@ -88,9 +83,8 @@ class ResultIterator implements \Iterator, \Countable
/** /**
* Checks if there is a current element after calls to rewind() or next(). * Checks if there is a current element after calls to rewind() or next().
* @return bool
*/ */
public function valid() public function valid(): bool
{ {
return !empty($this->row); return !empty($this->row);
} }
@@ -98,9 +92,8 @@ class ResultIterator implements \Iterator, \Countable
/** /**
* Required by the Countable interface. * Required by the Countable interface.
* @return int
*/ */
public function count() public function count(): int
{ {
return $this->result->getRowCount(); return $this->result->getRowCount();
} }

View File

@@ -30,11 +30,8 @@ class Row implements \ArrayAccess, \IteratorAggregate, \Countable
/** /**
* Converts value to DateTime object. * Converts value to DateTime object.
* @param string key
* @param string format
* @return \DateTime
*/ */
public function asDateTime($key, $format = NULL) public function asDateTime(string $key, string $format = NULL): \DateTime
{ {
$time = $this[$key]; $time = $this[$key];
if (!$time instanceof DateTime) { if (!$time instanceof DateTime) {

View File

@@ -83,10 +83,7 @@ trait Strict
} }
/** public function __isset($name): bool
* @return bool
*/
public function __isset($name)
{ {
return FALSE; return FALSE;
} }
@@ -105,10 +102,9 @@ trait Strict
/** /**
* @param string method name * @param string method name
* @param callable
* @return mixed * @return mixed
*/ */
public static function extensionMethod($name, $callback = NULL) public static function extensionMethod(string $name, callable $callback = NULL)
{ {
if (strpos($name, '::') === FALSE) { if (strpos($name, '::') === FALSE) {
$class = get_called_class(); $class = get_called_class();

View File

@@ -59,11 +59,9 @@ final class Translator
/** /**
* Generates SQL. Can be called only once. * Generates SQL. Can be called only once.
* @param array
* @return string
* @throws Exception * @throws Exception
*/ */
public function translate(array $args) public function translate(array $args): string
{ {
$args = array_values($args); $args = array_values($args);
while (count($args) === 1 && is_array($args[0])) { // implicit array expansion while (count($args) === 1 && is_array($args[0])) { // implicit array expansion
@@ -168,10 +166,8 @@ final class Translator
/** /**
* Apply modifier to single value. * Apply modifier to single value.
* @param mixed * @param mixed
* @param string|NULL
* @return string
*/ */
public function formatValue($value, $modifier) public function formatValue($value, ?string $modifier): string
{ {
if ($this->comment) { if ($this->comment) {
return '...'; return '...';
@@ -454,10 +450,8 @@ final class Translator
/** /**
* PREG callback from translate() or formatValue(). * PREG callback from translate() or formatValue().
* @param array
* @return string
*/ */
private function cb($matches) private function cb(array $matches): string
{ {
// [1] => `ident` // [1] => `ident`
// [2] => [ident] // [2] => [ident]
@@ -586,10 +580,9 @@ final class Translator
/** /**
* Apply substitutions to indentifier and delimites it. * Apply substitutions to indentifier and delimites it.
* @param string indentifier * @param string indentifier
* @return string
* @internal * @internal
*/ */
public function delimite($value) public function delimite(string $value): string
{ {
$value = $this->connection->substitute($value); $value = $this->connection->substitute($value);
$parts = explode('.', $value); $parts = explode('.', $value);

View File

@@ -67,10 +67,9 @@ class dibi
* Creates a new Connection object and connects it to specified database. * Creates a new Connection object and connects it to specified database.
* @param mixed connection parameters * @param mixed connection parameters
* @param string connection name * @param string connection name
* @return Dibi\Connection
* @throws Dibi\Exception * @throws Dibi\Exception
*/ */
public static function connect($config = [], $name = '0') public static function connect(array $config = [], string $name = '0'): Dibi\Connection
{ {
return self::$connection = self::$registry[$name] = new Dibi\Connection($config, $name); return self::$connection = self::$registry[$name] = new Dibi\Connection($config, $name);
} }
@@ -78,9 +77,8 @@ class dibi
/** /**
* Disconnects from database (doesn't destroy Connection object). * Disconnects from database (doesn't destroy Connection object).
* @return void
*/ */
public static function disconnect() public static function disconnect(): void
{ {
self::getConnection()->disconnect(); self::getConnection()->disconnect();
} }
@@ -88,9 +86,8 @@ class dibi
/** /**
* Returns TRUE when connection was established. * Returns TRUE when connection was established.
* @return bool
*/ */
public static function isConnected() public static function isConnected(): bool
{ {
return (self::$connection !== NULL) && self::$connection->isConnected(); return (self::$connection !== NULL) && self::$connection->isConnected();
} }
@@ -99,10 +96,9 @@ class dibi
/** /**
* Retrieve active connection. * Retrieve active connection.
* @param string connection registy name * @param string connection registy name
* @return Dibi\Connection
* @throws Dibi\Exception * @throws Dibi\Exception
*/ */
public static function getConnection($name = NULL) public static function getConnection(string $name = NULL): Dibi\Connection
{ {
if ($name === NULL) { if ($name === NULL) {
if (self::$connection === NULL) { if (self::$connection === NULL) {
@@ -122,10 +118,8 @@ class dibi
/** /**
* Sets connection. * Sets connection.
* @param Dibi\Connection
* @return Dibi\Connection
*/ */
public static function setConnection(Dibi\Connection $connection) public static function setConnection(Dibi\Connection $connection): Dibi\Connection
{ {
return self::$connection = $connection; return self::$connection = $connection;
} }
@@ -151,7 +145,7 @@ class dibi
* @param string SQL statement. * @param string SQL statement.
* @return Dibi\Result|int result set or number of affected rows * @return Dibi\Result|int result set or number of affected rows
*/ */
public static function nativeQuery($sql) public static function nativeQuery(string $sql)
{ {
return self::getConnection()->nativeQuery($sql); return self::getConnection()->nativeQuery($sql);
} }
@@ -160,9 +154,8 @@ class dibi
/** /**
* Generates and prints SQL query - Monostate for Dibi\Connection::test(). * Generates and prints SQL query - Monostate for Dibi\Connection::test().
* @param mixed one or more arguments * @param mixed one or more arguments
* @return bool
*/ */
public static function test(...$args) public static function test(...$args): bool
{ {
return self::getConnection()->test($args); return self::getConnection()->test($args);
} }
@@ -171,9 +164,8 @@ class dibi
/** /**
* Generates and returns SQL query as DataSource - Monostate for Dibi\Connection::test(). * Generates and returns SQL query as DataSource - Monostate for Dibi\Connection::test().
* @param mixed one or more arguments * @param mixed one or more arguments
* @return Dibi\DataSource
*/ */
public static function dataSource(...$args) public static function dataSource(...$args): Dibi\DataSource
{ {
return self::getConnection()->dataSource($args); return self::getConnection()->dataSource($args);
} }
@@ -197,7 +189,7 @@ class dibi
* @return Dibi\Row[] * @return Dibi\Row[]
* @throws Dibi\Exception * @throws Dibi\Exception
*/ */
public static function fetchAll(...$args) public static function fetchAll(...$args): array
{ {
return self::getConnection()->query($args)->fetchAll(); return self::getConnection()->query($args)->fetchAll();
} }
@@ -218,10 +210,9 @@ class dibi
/** /**
* Executes SQL query and fetch pairs - Monostate for Dibi\Connection::query() & fetchPairs(). * Executes SQL query and fetch pairs - Monostate for Dibi\Connection::query() & fetchPairs().
* @param mixed one or more arguments * @param mixed one or more arguments
* @return array
* @throws Dibi\Exception * @throws Dibi\Exception
*/ */
public static function fetchPairs(...$args) public static function fetchPairs(...$args): array
{ {
return self::getConnection()->query($args)->fetchPairs(); return self::getConnection()->query($args)->fetchPairs();
} }
@@ -230,10 +221,9 @@ class dibi
/** /**
* Gets the number of affected rows. * Gets the number of affected rows.
* Monostate for Dibi\Connection::getAffectedRows() * Monostate for Dibi\Connection::getAffectedRows()
* @return int number of rows
* @throws Dibi\Exception * @throws Dibi\Exception
*/ */
public static function getAffectedRows() public static function getAffectedRows(): int
{ {
return self::getConnection()->getAffectedRows(); return self::getConnection()->getAffectedRows();
} }
@@ -241,10 +231,9 @@ class dibi
/** /**
* Gets the number of affected rows. Alias for getAffectedRows(). * Gets the number of affected rows. Alias for getAffectedRows().
* @return int number of rows
* @throws Dibi\Exception * @throws Dibi\Exception
*/ */
public static function affectedRows() public static function affectedRows(): int
{ {
return self::getConnection()->getAffectedRows(); return self::getConnection()->getAffectedRows();
} }
@@ -254,10 +243,9 @@ class dibi
* Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query. * Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query.
* Monostate for Dibi\Connection::getInsertId() * Monostate for Dibi\Connection::getInsertId()
* @param string optional sequence name * @param string optional sequence name
* @return int
* @throws Dibi\Exception * @throws Dibi\Exception
*/ */
public static function getInsertId($sequence = NULL) public static function getInsertId(string $sequence = NULL): int
{ {
return self::getConnection()->getInsertId($sequence); return self::getConnection()->getInsertId($sequence);
} }
@@ -266,10 +254,9 @@ class dibi
/** /**
* Retrieves the ID generated for an AUTO_INCREMENT column. Alias for getInsertId(). * Retrieves the ID generated for an AUTO_INCREMENT column. Alias for getInsertId().
* @param string optional sequence name * @param string optional sequence name
* @return int
* @throws Dibi\Exception * @throws Dibi\Exception
*/ */
public static function insertId($sequence = NULL) public static function insertId(string $sequence = NULL): int
{ {
return self::getConnection()->getInsertId($sequence); return self::getConnection()->getInsertId($sequence);
} }
@@ -278,10 +265,9 @@ class dibi
/** /**
* Begins a transaction - Monostate for Dibi\Connection::begin(). * Begins a transaction - Monostate for Dibi\Connection::begin().
* @param string optional savepoint name * @param string optional savepoint name
* @return void
* @throws Dibi\Exception * @throws Dibi\Exception
*/ */
public static function begin($savepoint = NULL) public static function begin(string $savepoint = NULL): void
{ {
self::getConnection()->begin($savepoint); self::getConnection()->begin($savepoint);
} }
@@ -290,10 +276,9 @@ class dibi
/** /**
* Commits statements in a transaction - Monostate for Dibi\Connection::commit($savepoint = NULL). * Commits statements in a transaction - Monostate for Dibi\Connection::commit($savepoint = NULL).
* @param string optional savepoint name * @param string optional savepoint name
* @return void
* @throws Dibi\Exception * @throws Dibi\Exception
*/ */
public static function commit($savepoint = NULL) public static function commit(string $savepoint = NULL): void
{ {
self::getConnection()->commit($savepoint); self::getConnection()->commit($savepoint);
} }
@@ -302,10 +287,9 @@ class dibi
/** /**
* Rollback changes in a transaction - Monostate for Dibi\Connection::rollback(). * Rollback changes in a transaction - Monostate for Dibi\Connection::rollback().
* @param string optional savepoint name * @param string optional savepoint name
* @return void
* @throws Dibi\Exception * @throws Dibi\Exception
*/ */
public static function rollback($savepoint = NULL) public static function rollback(string $savepoint = NULL): void
{ {
self::getConnection()->rollback($savepoint); self::getConnection()->rollback($savepoint);
} }
@@ -313,9 +297,8 @@ class dibi
/** /**
* Gets a information about the current database - Monostate for Dibi\Connection::getDatabaseInfo(). * Gets a information about the current database - Monostate for Dibi\Connection::getDatabaseInfo().
* @return Dibi\Reflection\Database
*/ */
public static function getDatabaseInfo() public static function getDatabaseInfo(): Dibi\Reflection\Database
{ {
return self::getConnection()->getDatabaseInfo(); return self::getConnection()->getDatabaseInfo();
} }
@@ -326,7 +309,7 @@ class dibi
* @param string filename * @param string filename
* @return int count of sql commands * @return int count of sql commands
*/ */
public static function loadFile($file) public static function loadFile(string $file): int
{ {
return Dibi\Helpers::loadFromFile(self::getConnection(), $file); return Dibi\Helpers::loadFromFile(self::getConnection(), $file);
} }
@@ -335,10 +318,7 @@ class dibi
/********************* fluent SQL builders ****************d*g**/ /********************* fluent SQL builders ****************d*g**/
/** public static function command(): Dibi\Fluent
* @return Dibi\Fluent
*/
public static function command()
{ {
return self::getConnection()->command(); return self::getConnection()->command();
} }
@@ -346,41 +326,26 @@ class dibi
/** /**
* @param mixed column name * @param mixed column name
* @return Dibi\Fluent
*/ */
public static function select(...$args) public static function select(...$args): Dibi\Fluent
{ {
return self::getConnection()->select(...$args); return self::getConnection()->select(...$args);
} }
/** public static function update(string $table, array $args): Dibi\Fluent
* @param string table
* @param array
* @return Dibi\Fluent
*/
public static function update($table, $args)
{ {
return self::getConnection()->update($table, $args); return self::getConnection()->update($table, $args);
} }
/** public static function insert(string $table, array $args): Dibi\Fluent
* @param string table
* @param array
* @return Dibi\Fluent
*/
public static function insert($table, $args)
{ {
return self::getConnection()->insert($table, $args); return self::getConnection()->insert($table, $args);
} }
/** public static function delete(string $table): Dibi\Fluent
* @param string table
* @return Dibi\Fluent
*/
public static function delete($table)
{ {
return self::getConnection()->delete($table); return self::getConnection()->delete($table);
} }
@@ -391,9 +356,8 @@ class dibi
/** /**
* Returns substitution hashmap - Monostate for Dibi\Connection::getSubstitutes(). * Returns substitution hashmap - Monostate for Dibi\Connection::getSubstitutes().
* @return Dibi\HashMap
*/ */
public static function getSubstitutes() public static function getSubstitutes(): Dibi\HashMap
{ {
return self::getConnection()->getSubstitutes(); return self::getConnection()->getSubstitutes();
} }
@@ -406,9 +370,8 @@ class dibi
* Prints out a syntax highlighted version of the SQL command or Result. * Prints out a syntax highlighted version of the SQL command or Result.
* @param string|Result * @param string|Result
* @param bool return output instead of printing it? * @param bool return output instead of printing it?
* @return string
*/ */
public static function dump($sql = NULL, $return = FALSE) public static function dump($sql = NULL, bool $return = FALSE): string
{ {
return Dibi\Helpers::dump($sql, $return); return Dibi\Helpers::dump($sql, $return);
} }

View File

@@ -23,7 +23,7 @@ class Exception extends \Exception
* @param mixed * @param mixed
* @param string SQL command * @param string SQL command
*/ */
public function __construct($message = '', $code = 0, $sql = NULL) public function __construct(string $message = '', $code = 0, string $sql = NULL)
{ {
parent::__construct($message); parent::__construct($message);
$this->code = $code; $this->code = $code;
@@ -31,19 +31,13 @@ class Exception extends \Exception
} }
/** final public function getSql(): ?string
* @return string The SQL passed to the constructor
*/
final public function getSql()
{ {
return $this->sql; return $this->sql;
} }
/** public function __toString(): string
* @return string string represenation of exception with SQL command
*/
public function __toString()
{ {
return parent::__toString() . ($this->sql ? "\nSQL: " . $this->sql : ''); return parent::__toString() . ($this->sql ? "\nSQL: " . $this->sql : '');
} }
@@ -65,7 +59,7 @@ class DriverException extends Exception
class PcreException extends Exception class PcreException extends Exception
{ {
public function __construct($message = '%msg.') public function __construct(string $message = '%msg.')
{ {
static $messages = [ static $messages = [
PREG_INTERNAL_ERROR => 'Internal error', PREG_INTERNAL_ERROR => 'Internal error',
@@ -105,7 +99,7 @@ class ProcedureException extends Exception
* @param int Some code * @param int Some code
* @param string SQL command * @param string SQL command
*/ */
public function __construct($message = NULL, $code = 0, $severity = NULL, $sql = NULL) public function __construct(string $message = NULL, int $code = 0, string $severity = NULL, $sql = NULL)
{ {
parent::__construct($message, (int) $code, $sql); parent::__construct($message, (int) $code, $sql);
$this->severity = $severity; $this->severity = $severity;
@@ -114,9 +108,8 @@ class ProcedureException extends Exception
/** /**
* Gets the exception severity. * Gets the exception severity.
* @return string
*/ */
public function getSeverity() public function getSeverity(): string
{ {
$this->severity; $this->severity;
} }

View File

@@ -26,62 +26,53 @@ interface Driver
/** /**
* Connects to a database. * Connects to a database.
* @param array
* @return void
* @throws Exception * @throws Exception
*/ */
function connect(array &$config); function connect(array &$config): void;
/** /**
* Disconnects from a database. * Disconnects from a database.
* @return void
* @throws Exception * @throws Exception
*/ */
function disconnect(); function disconnect(): void;
/** /**
* Internal: Executes the SQL query. * Internal: Executes the SQL query.
* @param string SQL statement. * @param string SQL statement.
* @return ResultDriver|NULL
* @throws DriverException * @throws DriverException
*/ */
function query($sql); function query(string $sql): ?ResultDriver;
/** /**
* Gets the number of affected rows by the last INSERT, UPDATE or DELETE query. * Gets the number of affected rows by the last INSERT, UPDATE or DELETE query.
* @return int|NULL number of rows or NULL on error
*/ */
function getAffectedRows(); function getAffectedRows(): ?int;
/** /**
* Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query. * Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query.
* @return int|NULL int on success or NULL on failure
*/ */
function getInsertId($sequence); function getInsertId(?string $sequence): ?int;
/** /**
* Begins a transaction (if supported). * Begins a transaction (if supported).
* @param string optional savepoint name * @param string optional savepoint name
* @return void
* @throws DriverException * @throws DriverException
*/ */
function begin($savepoint = NULL); function begin(string $savepoint = NULL): void;
/** /**
* Commits statements in a transaction. * Commits statements in a transaction.
* @param string optional savepoint name * @param string optional savepoint name
* @return void
* @throws DriverException * @throws DriverException
*/ */
function commit($savepoint = NULL); function commit(string $savepoint = NULL): void;
/** /**
* Rollback changes in a transaction. * Rollback changes in a transaction.
* @param string optional savepoint name * @param string optional savepoint name
* @return void
* @throws DriverException * @throws DriverException
*/ */
function rollback($savepoint = NULL); function rollback(string $savepoint = NULL): void;
/** /**
* Returns the connection resource. * Returns the connection resource.
@@ -91,63 +82,39 @@ interface Driver
/** /**
* Returns the connection reflector. * Returns the connection reflector.
* @return Reflector
*/ */
function getReflector(); function getReflector(): Reflector;
/** /**
* Encodes data for use in a SQL statement. * Encodes data for use in a SQL statement.
* @param string value
* @return string encoded value
*/ */
function escapeText($value); function escapeText(string $value): string;
/** function escapeBinary(string $value): string;
* @param string
* @return string
*/
function escapeBinary($value);
/** function escapeIdentifier(string $value): string;
* @param string
* @return string
*/
function escapeIdentifier($value);
/** function escapeBool(bool $value): string;
* @param bool
* @return string
*/
function escapeBool($value);
/** /**
* @param \DateTimeInterface|string|int * @param \DateTimeInterface|string|int
* @return string
*/ */
function escapeDate($value); function escapeDate($value): string;
/** /**
* @param \DateTimeInterface|string|int * @param \DateTimeInterface|string|int
* @return string
*/ */
function escapeDateTime($value); function escapeDateTime($value): string;
/** /**
* Encodes string for use in a LIKE statement. * Encodes string for use in a LIKE statement.
* @param string
* @param int
* @return string
*/ */
function escapeLike($value, $pos); function escapeLike(string $value, int $pos): string;
/** /**
* Injects LIMIT/OFFSET to the SQL query. * Injects LIMIT/OFFSET to the SQL query.
* @param string
* @param int|NULL
* @param int|NULL
* @return void
*/ */
function applyLimit(&$sql, $limit, $offset); function applyLimit(string &$sql, ?int $limit, ?int $offset): void;
} }
@@ -160,9 +127,8 @@ interface ResultDriver
/** /**
* Returns the number of rows in a result set. * Returns the number of rows in a result set.
* @return int
*/ */
function getRowCount(); function getRowCount(): int;
/** /**
* Moves cursor position without fetching row. * Moves cursor position without fetching row.
@@ -170,28 +136,26 @@ interface ResultDriver
* @return bool TRUE on success, FALSE if unable to seek to specified record * @return bool TRUE on success, FALSE if unable to seek to specified record
* @throws Exception * @throws Exception
*/ */
function seek($row); function seek(int $row): bool;
/** /**
* Fetches the row at current position and moves the internal cursor to the next position. * Fetches the row at current position and moves the internal cursor to the next position.
* @param bool TRUE for associative array, FALSE for numeric * @param bool TRUE for associative array, FALSE for numeric
* @return array|NULL array on success, NULL if no next record
* @internal * @internal
*/ */
function fetch($type); function fetch(bool $type): ?array;
/** /**
* Frees the resources allocated for this result set. * Frees the resources allocated for this result set.
* @param resource result set resource * @param resource result set resource
* @return void
*/ */
function free(); function free(): void;
/** /**
* Returns metadata for all columns in a result set. * Returns metadata for all columns in a result set.
* @return array of {name, nativetype [, table, fullname, (int) size, (bool) nullable, (mixed) default, (bool) autoincrement, (array) vendor ]} * @return array of {name, nativetype [, table, fullname, (int) size, (bool) nullable, (mixed) default, (bool) autoincrement, (array) vendor ]}
*/ */
function getResultColumns(); function getResultColumns(): array;
/** /**
* Returns the result set resource. * Returns the result set resource.
@@ -201,10 +165,8 @@ interface ResultDriver
/** /**
* Decodes data from result set. * Decodes data from result set.
* @param string
* @return string
*/ */
function unescapeBinary($value); function unescapeBinary(string $value): string;
} }
@@ -219,27 +181,23 @@ interface Reflector
* Returns list of tables. * Returns list of tables.
* @return array of {name [, (bool) view ]} * @return array of {name [, (bool) view ]}
*/ */
function getTables(); function getTables(): array;
/** /**
* Returns metadata for all columns in a table. * Returns metadata for all columns in a table.
* @param string
* @return array of {name, nativetype [, table, fullname, (int) size, (bool) nullable, (mixed) default, (bool) autoincrement, (array) vendor ]} * @return array of {name, nativetype [, table, fullname, (int) size, (bool) nullable, (mixed) default, (bool) autoincrement, (array) vendor ]}
*/ */
function getColumns($table); function getColumns(string $table): array;
/** /**
* Returns metadata for all indexes in a table. * Returns metadata for all indexes in a table.
* @param string
* @return array of {name, (array of names) columns [, (bool) unique, (bool) primary ]} * @return array of {name, (array of names) columns [, (bool) unique, (bool) primary ]}
*/ */
function getIndexes($table); function getIndexes(string $table): array;
/** /**
* Returns metadata for all foreign keys in a table. * Returns metadata for all foreign keys in a table.
* @param string
* @return array
*/ */
function getForeignKeys($table); function getForeignKeys(string $table): array;
} }

View File

@@ -10,20 +10,20 @@ class MockDriver extends Dibi\Drivers\SqlsrvDriver
function __construct() function __construct()
{} {}
function connect(array &$config) function connect(array & $config): void
{} {}
function query($sql) function query(string $sql): ?Dibi\ResultDriver
{ {
return $this; return $this;
} }
function getResultColumns() function getResultColumns(): array
{ {
return []; return [];
} }
function fetch($assoc) function fetch(bool $type): ?array
{ {
return NULL; return NULL;
} }