From acaa35ae580362599b0ff4e1b3811edd2159a78e Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Wed, 20 Feb 2019 15:42:57 +0100 Subject: [PATCH] chore(php) Move to PHP 7.1. --- Dal.php => Source/Dal.php | 147 ++++-------------- DalStatement.php => Source/DalStatement.php | 106 +++---------- Exception.php => Source/Exception.php | 5 +- {IDal => Source/IDal}/Wrapper.php | 99 +++--------- {IDal => Source/IDal}/WrapperIterator.php | 10 +- {IDal => Source/IDal}/WrapperStatement.php | 85 +++------- {Layer => Source/Layer}/Dba/Dba.php | 0 {Layer => Source/Layer}/Dba/DbaStatement.php | 0 {Layer => Source/Layer}/Dbx/Dbx.php | 0 {Layer => Source/Layer}/Dbx/DbxStatement.php | 0 {Layer => Source/Layer}/Odbc/Odbc.php | 0 .../Layer}/Odbc/OdbcStatement.php | 0 {Layer => Source/Layer}/Pdo/Iterator.php | 28 +--- {Layer => Source/Layer}/Pdo/Pdo.php | 111 +++---------- {Layer => Source/Layer}/Pdo/Statement.php | 108 +++---------- {Query => Source/Query}/Delete.php | 16 +- {Query => Source/Query}/Dml.php | 20 +-- {Query => Source/Query}/EncloseIdentifier.php | 24 +-- {Query => Source/Query}/Insert.php | 80 +++------- {Query => Source/Query}/Join.php | 27 +--- {Query => Source/Query}/Query.php | 56 ++----- {Query => Source/Query}/Select.php | 55 ++----- {Query => Source/Query}/SelectCore.php | 123 ++++----------- {Query => Source/Query}/Update.php | 49 ++---- {Query => Source/Query}/Where.php | 35 ++--- Test/Unit/Query/EncloseIdentifier.php | 11 +- Test/Unit/Query/Example.php | 7 +- composer.json | 19 +-- 28 files changed, 293 insertions(+), 928 deletions(-) rename Dal.php => Source/Dal.php (72%) rename DalStatement.php => Source/DalStatement.php (71%) rename Exception.php => Source/Exception.php (95%) rename {IDal => Source/IDal}/Wrapper.php (50%) rename {IDal => Source/IDal}/WrapperIterator.php (86%) rename {IDal => Source/IDal}/WrapperStatement.php (52%) rename {Layer => Source/Layer}/Dba/Dba.php (100%) rename {Layer => Source/Layer}/Dba/DbaStatement.php (100%) rename {Layer => Source/Layer}/Dbx/Dbx.php (100%) rename {Layer => Source/Layer}/Dbx/DbxStatement.php (100%) rename {Layer => Source/Layer}/Odbc/Odbc.php (100%) rename {Layer => Source/Layer}/Odbc/OdbcStatement.php (100%) rename {Layer => Source/Layer}/Pdo/Iterator.php (88%) rename {Layer => Source/Layer}/Pdo/Pdo.php (68%) rename {Layer => Source/Layer}/Pdo/Statement.php (63%) rename {Query => Source/Query}/Delete.php (87%) rename {Query => Source/Query}/Dml.php (78%) rename {Query => Source/Query}/EncloseIdentifier.php (82%) rename {Query => Source/Query}/Insert.php (73%) rename {Query => Source/Query}/Join.php (77%) rename {Query => Source/Query}/Query.php (72%) rename {Query => Source/Query}/Select.php (77%) rename {Query => Source/Query}/SelectCore.php (70%) rename {Query => Source/Query}/Update.php (76%) rename {Query => Source/Query}/Where.php (79%) diff --git a/Dal.php b/Source/Dal.php similarity index 72% rename from Dal.php rename to Source/Dal.php index 4cc3e08..8a7eca7 100644 --- a/Dal.php +++ b/Source/Dal.php @@ -1,5 +1,7 @@ getId(); $event = 'hoa://Event/Database/' . $id; @@ -346,11 +322,8 @@ public function close() /** * Set database abstract layer instance. - * - * @param \Hoa\Database\IDal\Wrapper $dal The DAL instance. - * @return \Hoa\Database\IDal\Wrapper */ - protected function setDal(IDal\Wrapper $dal) + protected function setDal(IDal\Wrapper $dal): ?IDal\Wrapper { $old = $this->_layer; $this->_layer = $dal; @@ -360,10 +333,8 @@ protected function setDal(IDal\Wrapper $dal) /** * Get the database abstract layer instance. - * - * @return \Hoa\Database\IDal\Wrapper */ - protected function getDal() + protected function getDal(): ?IDal\Wrapper { if (null === $this->_layer) { $this->open(); @@ -374,46 +345,32 @@ protected function getDal() /** * Initiate a transaction. - * - * @return bool - * @throws \Hoa\Database\Exception */ - public function beginTransaction() + public function beginTransaction(): bool { return $this->getDal()->beginTransaction(); } /** * Commit a transaction. - * - * @return bool - * @throws \Hoa\Database\Exception */ - public function commit() + public function commit(): bool { return $this->getDal()->commit(); } /** * Roll back a transaction. - * - * @return bool - * @throws \Hoa\Database\Exception */ - public function rollBack() + public function rollBack(): bool { return $this->getDal()->rollBack(); } /** * Return the ID of the last inserted row or sequence value. - * - * @param string $name Name of sequence object (needed for some - * driver). - * @return string - * @throws \Hoa\Database\Exception */ - public function lastInsertId($name = null) + public function lastInsertId(string $name = null): string { if (null === $name) { return $this->getDal()->lastInsertId(); @@ -424,15 +381,8 @@ public function lastInsertId($name = null) /** * Prepare a statement for execution and returns a statement object. - * - * @param string $statement This must be a valid SQL statement for the - * target database server. - * @param array $options Options to set attributes values for the - * layer statement. - * @return \Hoa\Database\DalStatement - * @throws \Hoa\Database\Exception - */ - public function prepare($statement, array $options = []) + */ + public function prepare(string $statement, array $options = []): DalStatement { return new DalStatement( $this->getDal()->prepare( @@ -443,14 +393,8 @@ public function prepare($statement, array $options = []) /** * Quote a string for use in a query. - * - * @param string $string The string to be quoted. - * @param int $type Provide a data type hint for drivers that - * have alternate quoting styles. - * @return string - * @throws \Hoa\Database\Exception */ - public function quote($string = null, $type = -1) + public function quote(?string $string, int $type = -1): string { if ($type < 0) { return $this->getDal()->quote($string); @@ -462,12 +406,8 @@ public function quote($string = null, $type = -1) /** * Execute an SQL statement, returning a result set as a * \Hoa\Database\DalStatement object. - * - * @param string $statement The SQL statement to prepare and execute. - * @return \Hoa\Database\DalStatement - * @throws \Hoa\Database\Exception */ - public function query($statement) + public function query(string $statement): DalStatement { return new DalStatement( $this->getDal()->query($statement) @@ -477,11 +417,8 @@ public function query($statement) /** * Fetch the SQLSTATE associated with the last operation on the database * handle. - * - * @return string - * @throws \Hoa\Database\Exception */ - public function errorCode() + public function errorCode(): string { return $this->getDal()->errorCode(); } @@ -489,45 +426,30 @@ public function errorCode() /** * Fetch extends error information associated with the last operation on the * database handle. - * - * @return array - * @throws \Hoa\Database\Exception */ - public function errorInfo() + public function errorInfo(): array { return $this->getDal()->errorInfo(); } /** * Return an array of available drivers. - * - * @return array - * @throws \Hoa\Database\Exception */ - public function getAvailableDrivers() + public function getAvailableDrivers(): array { return $this->getDal()->getAvailableDrivers(); } /** * Set attributes. - * - * @param array $attributes Attributes values. - * @return array - * @throws \Hoa\Database\Exception */ - public function setAttributes(array $attributes) + public function setAttributes(array $attributes): array { return $this->getDal()->setAttributes($attributes); } /** * Set a specific attribute. - * - * @param mixed $attribute Attribute name. - * @param mixed $value Attribute value. - * @return mixed - * @throws \Hoa\Database\Exception */ public function setAttribute($attribute, $value) { @@ -536,33 +458,24 @@ public function setAttribute($attribute, $value) /** * Retrieve all database connection attributes. - * - * @return array - * @throws \Hoa\Database\Exception */ - public function getAttributes() + public function getAttributes(): array { return $this->getDal()->getAttributes(); } /** * Retrieve a database connection attribute. - * - * @param string $attribute Attribute name. - * @return mixed - * @throws \Hoa\Database\Exception */ - public function getAttribute($attribute) + public function getAttribute(string $attribute) { return $this->getDal()->getAttribute($attribute); } /** * Get current ID. - * - * @return string */ - public function getId() + public function getId(): string { return $this->__id; } diff --git a/DalStatement.php b/Source/DalStatement.php similarity index 71% rename from DalStatement.php rename to Source/DalStatement.php index 4da82f3..284b674 100644 --- a/DalStatement.php +++ b/Source/DalStatement.php @@ -1,5 +1,7 @@ statement; $this->statement = $statement; @@ -223,23 +213,16 @@ protected function setStatement(IDal\WrapperStatement $statement) /** * Get the statement instance. - * - * @return \Hoa\Database\IDal\WrapperStatement */ - protected function getStatement() + protected function getStatement(): IDal\WrapperStatement { return $this->statement; } /** * Execute a prepared statement. - * - * @param array $bindParameters Bind parameters values if bindParam is - * not called. - * @return \Hoa\Database\DalStatement - * @throws \Hoa\Database\Exception */ - public function execute(array $bindParameters = []) + public function execute(array $bindParameters = []): self { if (empty($bindParameters)) { return $this->getStatement()->execute(); @@ -252,20 +235,13 @@ public function execute(array $bindParameters = []) /** * Bind a parameter to te specified variable name. - * - * @param mixed $parameter Parameter name. - * @param mixed $value Parameter value. - * @param int $type Type of value. - * @param int $length Length of data type. - * @return bool - * @throws \Hoa\Database\Exception */ public function bindParameter( $parameter, &$value, - $type = null, - $length = null - ) { + ?int $type = null, + int $length = null + ) : bool { if (null === $type) { return $this->getStatement()->bindParameter($parameter, $value); } @@ -288,37 +264,22 @@ public function bindParameter( /** * Return an array containing all of the result set rows. - * - * @return array - * @throws \Hoa\Database\Exception */ - public function fetchAll() + public function fetchAll(): array { return $this->getStatement()->fetchAll(); } /** * Set the iterator fetching style. - * - * @param int $offset This value must be one of the - * DalStatement::FROM_* constants or an - * arbitrary offset. - * @param int $orientation This value must be DalStatement::FORWARD - * or DalStatement::BACKWARD constant. - * @param int $style This value must be one of the - * DalStatement::AS_* constants. - * @param mixed $arg1 For AS_CLASS: The class name. - * For AS_REUSABLE_OBJECT: An object. - * @param array $arg2 For AS_CLASS: Constructor arguments. - * @return \Hoa\Database\DalStatement */ public function setFetchingStyle( - $offset = self::FROM_START, - $orientation = self::FORWARD, - $style = self::AS_MAP, - $arg1 = null, - $arg2 = null - ) { + int $offset = self::FROM_START, + int $orientation = self::FORWARD, + int $style = self::AS_MAP, + $arg1 = null, + array $arg2 = null + ): self { $this->getStatement()->setFetchingStyle( $offset, $orientation, @@ -332,32 +293,24 @@ public function setFetchingStyle( /** * Get an Iterator. - * - * @return \Hoa\Database\IDal\WrapperIterator */ - public function getIterator() + public function getIterator(): IDal\WrapperIterator { return $this->getStatement()->getIterator(); } /** * Fetch the first row in the result set. - * - * @param int $style Must be one of the DalStatement::AS_* constants. - * @return mixed */ - public function fetchFirst($style = null) + public function fetchFirst(int $style = null) { return $this->getStatement()->fetchFirst($style); } /** * Fetch the last row in the result set. - * - * @param int $style Must be one of the DalStatement::AS_* constants. - * @return mixed */ - public function fetchLast($style = null) + public function fetchLast(int $style = null) { return $this->getStatement()->fetchLast($style); } @@ -365,23 +318,16 @@ public function fetchLast($style = null) /** * Return a single column from the next row of the result set or false if * there is no more row. - * - * @param int $column Column index. - * @return mixed - * @throws \Hoa\Database\Exception */ - public function fetchColumn($column = 0) + public function fetchColumn(int $column = 0) { return $this->getStatement()->fetchColumn($column); } /** * Close the cursor, enabling the statement to be executed again. - * - * @return bool - * @throws \Hoa\Database\Exception */ - public function closeCursor() + public function closeCursor(): bool { return $this->getStatement()->closeCursor(); } @@ -389,11 +335,8 @@ public function closeCursor() /** * Fetch the SQLSTATE associated with the last operation on the statement * handle. - * - * @return string - * @throws \Hoa\Database\Exception */ - public function errorCode() + public function errorCode(): string { return $this->getStatement()->errorCode(); } @@ -401,11 +344,8 @@ public function errorCode() /** * Fetch extends error information associated with the last operation on the * statement handle. - * - * @return array - * @throws \Hoa\Database\Exception */ - public function errorInfo() + public function errorInfo(): array { return $this->getStatement()->errorInfo(); } diff --git a/Exception.php b/Source/Exception.php similarity index 95% rename from Exception.php rename to Source/Exception.php index b20918b..4df96fd 100644 --- a/Exception.php +++ b/Source/Exception.php @@ -1,5 +1,7 @@ _statement = $statement; $this->_style = $style; @@ -111,28 +107,22 @@ public function __construct($statement, $style) /** * Get the statement instance. - * - * @return \PDOStatement */ - protected function getStatement() + protected function getStatement(): \PDOStatement { return $this->_statement; } /** * Return the current element. - * - * @return array */ - public function current() + public function current(): array { return $this->_row; } /** * Move forward to next element. - * - * @return void */ public function next() { @@ -146,8 +136,6 @@ public function next() /** * Return always null. - * - * @return null */ public function key() { @@ -156,18 +144,14 @@ public function key() /** * Checks if current position is valid. - * - * @return bool */ - public function valid() + public function valid() : bool { return false !== $this->_row; } /** * Rewind the iterator to the first element. - * - * @return void */ public function rewind() { diff --git a/Layer/Pdo/Pdo.php b/Source/Layer/Pdo/Pdo.php similarity index 68% rename from Layer/Pdo/Pdo.php rename to Source/Layer/Pdo/Pdo.php index afdae5d..1812686 100644 --- a/Layer/Pdo/Pdo.php +++ b/Source/Layer/Pdo/Pdo.php @@ -1,5 +1,7 @@ _connection; $this->_connection = $connection; @@ -114,11 +104,8 @@ protected function setConnection(\PDO $connection) /** * Get the connection instance. - * - * @return \PDO - * @throws \Hoa\Database\Exception */ - protected function getConnection() + protected function getConnection(): \PDO { if (null === $this->_connection) { throw new Database\Exception( @@ -132,46 +119,32 @@ protected function getConnection() /** * Initiate a transaction. - * - * @return bool - * @throws \Hoa\Database\Exception */ - public function beginTransaction() + public function beginTransaction(): bool { return $this->getConnection()->beginTransaction(); } /** * Commit a transaction. - * - * @return bool - * @throws \Hoa\Database\Exception */ - public function commit() + public function commit(): bool { return $this->getConnection()->commit(); } /** * Roll back a transaction. - * - * @return bool - * @throws \Hoa\Database\Exception */ - public function rollBack() + public function rollBack(): bool { return $this->getConnection()->rollBack(); } /** * Return the ID of the last inserted row or sequence value. - * - * @param string $name Name of sequence object (needed for some - * driver). - * @return string - * @throws \Hoa\Database\Exception */ - public function lastInsertId($name = null) + public function lastInsertId(string $name = null): string { if (null === $name) { return $this->getConnection()->lastInsertId(); @@ -182,15 +155,8 @@ public function lastInsertId($name = null) /** * Prepare a statement for execution and returns a statement object. - * - * @param string $statement This must be a valid SQL statement for the - * target database server. - * @param array $options Options to set attributes values for the - * Layer Statement. - * @return \Hoa\Database\Layer\Pdo\Statement - * @throws \Hoa\Database\Exception */ - public function prepare($statement, array $options = []) + public function prepare(string $statement, array $options = []): Database\IDal\WrapperStatement { if (!isset($options[\PDO::ATTR_CURSOR])) { try { @@ -217,14 +183,8 @@ public function prepare($statement, array $options = []) /** * Quote a sting for use in a query. - * - * @param string $string The string to be quoted. - * @param int $type Provide a data type hint for drivers that - * have alternate quoting styles. - * @return string - * @throws \Hoa\Database\Exception */ - public function quote($string = null, $type = -1) + public function quote(?string $string, int $type = -1): string { if ($type < 0) { return $this->getConnection()->quote($string); @@ -236,12 +196,8 @@ public function quote($string = null, $type = -1) /** * Execute an SQL statement, returning a result set as a * \Hoa\Database\Layer\Pdo\Statement object. - * - * @param string $statement The SQL statement to prepare and execute. - * @return \Hoa\Database\Layer\Pdo\Statement - * @throws \Hoa\Database\Exception */ - public function query($statement) + public function query(string $statement): Database\IDal\WrapperStatement { $handle = $this->getConnection()->query($statement); @@ -259,11 +215,8 @@ public function query($statement) /** * Fetch the SQLSTATE associated with the last operation on the database * handle. - * - * @return string - * @throws \Hoa\Database\Exception */ - public function errorCode() + public function errorCode(): string { return $this->getConnection()->errorCode(); } @@ -271,34 +224,24 @@ public function errorCode() /** * Fetch extends error information associated with the last operation on the * database handle. - * - * @return array - * @throws \Hoa\Database\Exception */ - public function errorInfo() + public function errorInfo(): array { return $this->getConnection()->errorInfo(); } /** * Return an array of available drivers. - * - * @return array - * @throws \Hoa\Database\Exception */ - public function getAvailableDrivers() + public function getAvailableDrivers(): array { return $this->getConnection()->getAvailableDrivers(); } /** * Set attributes. - * - * @param array $attributes Attributes values. - * @return array - * @throws \Hoa\Database\Exception */ - public function setAttributes(array $attributes) + public function setAttributes(array $attributes): array { $out = true; @@ -311,11 +254,6 @@ public function setAttributes(array $attributes) /** * Set a specific attribute. - * - * @param mixed $attribute Attribute name. - * @param mixed $value Attribute value. - * @return mixed - * @throws \Hoa\Database\Exception */ public function setAttribute($attribute, $value) { @@ -324,11 +262,8 @@ public function setAttribute($attribute, $value) /** * Retrieve all database connection attributes. - * - * @return array - * @throws \Hoa\Database\Exception */ - public function getAttributes() + public function getAttributes(): array { $out = []; $attributes = [ @@ -355,12 +290,8 @@ public function getAttributes() /** * Retrieve a database connection attribute. - * - * @param string $attribute Attribute name. - * @return mixed - * @throws \Hoa\Database\Exception */ - public function getAttribute($attribute) + public function getAttribute(string $attribute) { return $this @@ -372,4 +303,4 @@ public function getAttribute($attribute) /** * Flex entity. */ -Consistency::flexEntity('Hoa\Database\Layer\Pdo\Pdo'); +Consistency::flexEntity(Pdo::class); diff --git a/Layer/Pdo/Statement.php b/Source/Layer/Pdo/Statement.php similarity index 63% rename from Layer/Pdo/Statement.php rename to Source/Layer/Pdo/Statement.php index 72b5997..c54a4b7 100644 --- a/Layer/Pdo/Statement.php +++ b/Source/Layer/Pdo/Statement.php @@ -1,5 +1,7 @@ _statement; $this->_statement = $statement; @@ -96,23 +90,16 @@ protected function setStatement(\PDOStatement $statement) /** * Get the statement instance. - * - * @return \PDOStatement */ - protected function getStatement() + protected function getStatement(): \PDOStatement { return $this->_statement; } /** * Execute a prepared statement. - * - * @param array $bindParameters Bind parameters values if bindParam - * is not called. - * @return \Hoa\Database\Layer\Pdo\Statement - * @throws \Hoa\Database\Exception */ - public function execute(array $bindParameters = null) + public function execute(array $bindParameters = null): Database\IDal\WrapperStatement { if (false === $this->getStatement()->execute($bindParameters)) { throw new Database\Exception( @@ -127,20 +114,13 @@ public function execute(array $bindParameters = null) /** * Bind a parameter to te specified variable name. - * - * @param mixed $parameter Parameter name. - * @param mixed $value Parameter value. - * @param int $type Type of value. - * @param int $length Length of data type. - * @return bool - * @throws \Hoa\Database\Exception */ public function bindParameter( $parameter, &$value, - $type = null, - $length = null - ) { + ?int $type = null, + int $length = null + ): bool { if (null === $type) { return $this->getStatement()->bindParam($parameter, $value); } @@ -154,26 +134,14 @@ public function bindParameter( /** * Set the iterator fetching style. - * - * @param int $offset This value must be one of the - * DalStatement::FROM_* constants or an - * arbitrary offset. - * @param int $orientation This value must be DalStatement::FORWARD - * or DalStatement::BACKWARD constant. - * @param int $style This value must be one of the - * DalStatement::AS_* constants. - * @param mixed $arg1 For AS_CLASS: The class name. - * For AS_REUSABLE_OBJECT: An object. - * @param array $arg2 For AS_CLASS: Constructor arguments. - * @return \Hoa\Database\Layer\Pdo\Statement */ public function setFetchingStyle( - $offset = Database\DalStatement::FROM_START, - $orientation = Database\DalStatement::FORWARD, - $style = Database\DalStatement::AS_MAP, - $arg1 = null, - $arg2 = null - ) { + int $offset = Database\DalStatement::FROM_START, + int $orientation = Database\DalStatement::FORWARD, + int $style = Database\DalStatement::AS_MAP, + $arg1 = null, + array $arg2 = null + ): Database\IDal\WrapperStatement { $this->_style[Database\DalStatement::STYLE_OFFSET] = $offset; $this->_style[Database\DalStatement::STYLE_ORIENTATION] = $orientation; $this->_style[Database\DalStatement::STYLE_MODE] = $style; @@ -190,10 +158,8 @@ public function setFetchingStyle( /** * Get an Iterator. - * - * @return \Hoa\Database\Layer\Pdo\Iterator */ - public function getIterator() + public function getIterator(): Iterator { return new Iterator( $this->getStatement(), @@ -203,47 +169,32 @@ public function getIterator() /** * Return an array containing all of the result set rows. - * - * @return array - * @throws \Hoa\Database\Exception */ - public function fetchAll() + public function fetchAll(): array { return $this->getStatement()->fetchAll(\PDO::FETCH_ASSOC); } /** * Fetch the first row in the result set. - * - * @param int $style Must be one of the DalStatement::AS_* constants. - * @return mixed */ - public function fetchFirst($style = null) + public function fetchFirst(int $style = null) { return $this->fetch($style, \PDO::FETCH_ORI_FIRST); } /** * Fetch the last row in the result set. - * - * @param int $style Must be one of the DalStatement::AS_* constants. - * @return mixed */ - public function fetchLast($style = null) + public function fetchLast(int $style = null) { return $this->fetch($style, \PDO::FETCH_ORI_LAST); } /** * Fetch a row in the result set. - * - * @param int $style Must be one of the DalStatement::AS_* - * constants. - * @param int $orientation Must be one of the \PDO::FETCH_ORI_* - * constants. - * @return mixed */ - protected function fetch($style, $orientation) + protected function fetch(int $style, int $orientation) { return $this->getStatement()->fetch( $style ?: $this->_style, @@ -254,23 +205,16 @@ protected function fetch($style, $orientation) /** * Return a single column from the next row of the result set or false if * there is no more row. - * - * @param int $column Column index. - * @return mixed - * @throws \Hoa\Database\Exception */ - public function fetchColumn($column = 0) + public function fetchColumn(int $column = 0) { return $this->getStatement()->fetchColumn($column); } /** * Close the cursor, enabling the statement to be executed again. - * - * @return bool - * @throws \Hoa\Database\Exception */ - public function closeCursor() + public function closeCursor(): bool { return $this->getStatement()->closeCursor(); } @@ -278,11 +222,8 @@ public function closeCursor() /** * Fetch the SQLSTATE associated with the last operation on the statement * handle. - * - * @return string - * @throws \Hoa\Database\Exception */ - public function errorCode() + public function errorCode(): string { return $this->getStatement()->errorCode(); } @@ -290,11 +231,8 @@ public function errorCode() /** * Fetch extends error information associated with the last operation on the * statement handle. - * - * @return array - * @throws \Hoa\Database\Exception */ - public function errorInfo() + public function errorInfo(): array { return $this->getStatement()->errorInfo(); } diff --git a/Query/Delete.php b/Source/Query/Delete.php similarity index 87% rename from Query/Delete.php rename to Source/Query/Delete.php index eb17384..5a7aa85 100644 --- a/Query/Delete.php +++ b/Source/Query/Delete.php @@ -1,5 +1,7 @@ _from = $source; @@ -72,10 +68,8 @@ public function from($source) /** * Generate the query. - * - * @return string */ - public function __toString() + public function __toString(): string { return 'DELETE FROM ' . diff --git a/Query/Dml.php b/Source/Query/Dml.php similarity index 78% rename from Query/Dml.php rename to Source/Query/Dml.php index 9a471b3..021578b 100644 --- a/Query/Dml.php +++ b/Source/Query/Dml.php @@ -1,5 +1,7 @@ _openingSymbol = $openingSymbol; $this->_closingSymbol = $closingSymbol ?: $openingSymbol; @@ -87,11 +82,8 @@ public function setEncloseSymbol($openingSymbol, $closingSymbol = null) /** * Enable or disable enclosing identifiers. - * - * @param bool $enable Enable or disable. - * @return bool */ - public function enableEncloseIdentifier($enable = true) + public function enableEncloseIdentifier(bool $enable = true): bool { $old = $this->_enableEnclose; $this->_enableEnclose = $enable; @@ -101,9 +93,6 @@ public function enableEncloseIdentifier($enable = true) /** * Enclose identifiers with defined symbol. - * - * @param array|string $identifiers Table/column/alias identifiers. - * @return array|string */ protected function enclose($identifiers) { @@ -124,11 +113,8 @@ protected function enclose($identifiers) /** * Enclose identifier with defined symbol. - * - * @param string $identifier Identifier. - * @return string */ - protected function _enclose($identifier) + protected function _enclose(string $identifier): string { if (0 === preg_match('#\s|\(#', $identifier)) { return $this->_openingSymbol . $identifier . $this->_closingSymbol; diff --git a/Query/Insert.php b/Source/Query/Insert.php similarity index 73% rename from Query/Insert.php rename to Source/Query/Insert.php index 1a2c122..b34b3b7 100644 --- a/Query/Insert.php +++ b/Source/Query/Insert.php @@ -1,5 +1,7 @@ _into = $name; @@ -100,61 +96,48 @@ public function into($name) /** * Insert or rollback. - * - * @return \Hoa\Database\Query\Insert */ - public function rollback() + public function rollback(): self { return $this->_or('ROLLBACK'); } /** * Insert or abort. - * - * @return \Hoa\Database\Query\Insert */ - public function abort() + public function abort(): self { return $this->_or('ABORT'); } /** * Insert or replace. - * - * @return \Hoa\Database\Query\Insert */ - public function replace() + public function replace(): self { return $this->_or('REPLACE'); } /** * Insert or fail. - * - * @return \Hoa\Database\Query\Insert */ - public function fail() + public function fail(): self { return $this->_or('FAIL'); } /** * Insert or ignore. - * - * @return \Hoa\Database\Query\Insert */ - public function ignore() + public function ignore(): self { return $this->_or('IGNORE'); } /** * Declare an alternative to “INSERT”. - * - * @param string $or Alternative. - * @return \Hoa\Database\Query\Insert */ - protected function _or($or) + protected function _or(string $or): self { $this->_or = $or; @@ -163,14 +146,10 @@ protected function _or($or) /** * Set columns. - * - * @param string $column Column name. - * @param ... ... - * @return \Hoa\Database\Query\Insert */ - public function on($column) + public function on(string ...$columns): self { - foreach (func_get_args() as $column) { + foreach ($columns as $column) { $this->_columns[] = $column; } @@ -178,17 +157,13 @@ public function on($column) } /** - * Set values (on call per tuple). + * Set values (one call per tuple). * Expression can be: a regular value or a SELECT query. - * - * @param mixed $expression Expression. - * @param ... ... - * @return \Hoa\Database\Query\Insert */ - public function values($expression) + public function values(...$expressions): self { - if ($expression instanceof Select) { - $this->_values = (string) $expression; + if (1 === count($expressions) && $expressions[0] instanceof Select) { + $this->_values = (string) $expressions[0]; } else { if (is_string($this->_values)) { $this->_values = []; @@ -197,7 +172,7 @@ public function values($expression) $values = &$this->_values[]; $values = []; - foreach (func_get_args() as $expression) { + foreach ($expressions as $expression) { $values[] = $expression; } } @@ -207,10 +182,8 @@ public function values($expression) /** * Use default values. - * - * @return \Hoa\Database\Query\Insert */ - public function defaultValues() + public function defaultValues(): self { $this->_defaultValues = true; @@ -219,11 +192,8 @@ public function defaultValues() /** * Allow to use the “or” attribute to chain method calls. - * - * @param string $name Name. - * @return mixed */ - public function __get($name) + public function __get(string $name) { switch (strtolower($name)) { case 'or': @@ -236,10 +206,8 @@ public function __get($name) /** * Generate the query. - * - * @return string */ - public function __toString() + public function __toString(): string { $out = 'INSERT'; diff --git a/Query/Join.php b/Source/Query/Join.php similarity index 77% rename from Query/Join.php rename to Source/Query/Join.php index 1cdd07b..3388f5e 100644 --- a/Query/Join.php +++ b/Source/Query/Join.php @@ -1,5 +1,7 @@ _parent = $parent; $this->_from = &$from; @@ -79,11 +75,8 @@ public function __construct(Select $parent, array &$from) /** * Declare the JOIN constraint ON. - * - * @param string $expression Expression. - * @return \Hoa\Database\Query\Select */ - public function on($expression) + public function on(string $expression): Select { $this->_from[key($this->_from)] = current($this->_from) . @@ -94,17 +87,13 @@ public function on($expression) /** * Declare the JOIN constraint USING. - * - * @param string $expression Expression. - * @param ... ... - * @return \Hoa\Database\Query\Select */ - public function using($expression) + public function using(string ...$expressions) : Select { $this->_from[key($this->_from)] = current($this->_from) . ' USING (' . - implode(', ', func_get_args()) . ')'; + implode(', ', $expressions) . ')'; return $this->_parent; } diff --git a/Query/Query.php b/Source/Query/Query.php similarity index 72% rename from Query/Query.php rename to Source/Query/Query.php index e66e065..012f3ea 100644 --- a/Query/Query.php +++ b/Source/Query/Query.php @@ -1,5 +1,7 @@ _id = $id; @@ -79,63 +75,48 @@ public function setId($id) /** * Get current instance ID. - * - * @return string */ - public function getId() + public function getId(): ?string { return $this->_id; } /** * Start a START query. - * - * @param string $column Column. - * @param ... ... - * @return \Hoa\Database\Query\Select */ - public function select($column = null) + public function select(string ...$columns): Select { - return $this->store(new Select(func_get_args())); + return $this->store(new Select($columns)); } /** * Start an INSERT query. - * - * @return \Hoa\Database\Query\Insert */ - public function insert() + public function insert(): Insert { return $this->store(new Insert()); } /** * Start an UPDATE query. - * - * @return \Hoa\Database\Query\Update */ - public function update() + public function update(): Update { return $this->store(new Update()); } /** * Start a DELETE query. - * - * @return \Hoa\Database\Query\Delete */ - public function delete() + public function delete(): Delete { return $this->store(new Delete()); } /** * Start a WHERE clause. - * - * @param string $expression Expression. - * @return \Hoa\Database\Query\Where */ - public function where($expression) + public function where(string $expression): Where { $where = new Where(); @@ -144,9 +125,6 @@ public function where($expression) /** * Store the current instance if necessary. - * - * @param \Hoa\Database\Query\Dml $object Object. - * @return \Hoa\Database\Query\Dml */ protected function store($object) { @@ -163,11 +141,8 @@ protected function store($object) /** * Get a query (a clone of it). - * - * @param string $id ID. - * @return \Hoa\Database\Query\Dml */ - public static function get($id) + public static function get(string $id): ?Dml { if (null === $out = static::getReference($id)) { return null; @@ -178,11 +153,8 @@ public static function get($id) /** * Get a query (not a clone of it). - * - * @param string $id ID. - * @return \Hoa\Database\Query\Dml */ - public static function getReference($id) + public static function getReference(string $id): ?Dml { if (false === array_key_exists($id, static::$_queries)) { return null; @@ -195,4 +167,4 @@ public static function getReference($id) /** * Flex entity. */ -Consistency::flexEntity('Hoa\Database\Query\Query'); +Consistency::flexEntity(Query::class); diff --git a/Query/Select.php b/Source/Query/Select.php similarity index 77% rename from Query/Select.php rename to Source/Query/Select.php index d928d77..13a974a 100644 --- a/Query/Select.php +++ b/Source/Query/Select.php @@ -1,5 +1,7 @@ compose('UNION'); } /** * Start a new SELECT query which is an unionAll of the previous one. - * - * @return \Hoa\Database\Query\Select */ - public function unionAll() + public function unionAll(): self { return $this->compose('UNION ALL'); } /** * Start a new SELECT query which is an intersection of the previous one. - * - * @return \Hoa\Database\Query\Select */ - public function intersect() + public function intersect(): self { return $this->compose('INTERSECT'); } /** * Start a new SELECT query which is an exception of the previous one. - * - * @return \Hoa\Database\Query\Select */ - public function except() + public function except(): self { return $this->compose('EXCEPT'); } /** * Compose SELECT queries. - * - * @param string $operator Composition operator. - * @return \Hoa\Database\Query\Select */ - protected function compose($operator) + protected function compose(string $operator): self { $this->_select[] = parent::__toString() . ' ' . $operator; $this->reset(); @@ -132,14 +120,10 @@ protected function compose($operator) /** * Add ordering terms. - * - * @param string $term Term. - * @param ... ... - * @return \Hoa\Database\Query\Select */ - public function orderBy($term) + public function orderBy(string ...$terms): self { - foreach (func_get_args() as $term) { + foreach ($terms as $term) { $this->_orderBy[] = $term; } @@ -148,14 +132,10 @@ public function orderBy($term) /** * Add limit expressions. - * - * @param string $expression Expression. - * @param ... ... - * @return \Hoa\Database\Query\Select */ - public function limit($expression) + public function limit(int ...$expressions): self { - foreach (func_get_args() as $expression) { + foreach ($expressions as $expression) { $this->_limit[] = $expression; } @@ -164,11 +144,8 @@ public function limit($expression) /** * Add offset expression. - * - * @param string $expression Expression. - * @return \Hoa\Database\Query\Select */ - public function offset($expression) + public function offset(string $expression): self { $this->_offset = $expression; @@ -177,12 +154,10 @@ public function offset($expression) /** * Generate the query. - * - * @return string */ - public function __toString() + public function __toString(): string { - $out = null; + $out = ''; $select = implode(' ', $this->_select); if (!empty($select)) { diff --git a/Query/SelectCore.php b/Source/Query/SelectCore.php similarity index 70% rename from Query/SelectCore.php rename to Source/Query/SelectCore.php index e4d39ab..85887f1 100644 --- a/Query/SelectCore.php +++ b/Source/Query/SelectCore.php @@ -1,5 +1,7 @@ _distinctOrAll = 'DISTINCT'; @@ -111,10 +108,8 @@ public function distinct() /** * Make a SELECT ALL. - * - * @return \Hoa\Database\Query\SelectCore */ - public function all() + public function all(): self { $this->_distinctOrAll = 'ALL'; @@ -123,14 +118,10 @@ public function all() /** * Select a column. - * - * @param string $column Column. - * @param ... ... - * @return \Hoa\Database\Query\SelectCore */ - public function select($column) + public function select(string ...$columns): self { - foreach (func_get_args() as $column) { + foreach ($columns as $column) { $this->_columns[] = $column; } @@ -139,14 +130,10 @@ public function select($column) /** * Group by expression. - * - * @param string $expression Expression. - * @param ... ... - * @return \Hoa\Database\Query\SelectCore */ - public function groupBy($expression) + public function groupBy(string ...$expressions): self { - foreach (func_get_args() as $expression) { + foreach ($expressions as $expression) { $this->_groupBy[] = $expression; } @@ -155,11 +142,8 @@ public function groupBy($expression) /** * Having expression. - * - * @param string $expression Expression. - * @return \Hoa\Database\Query\SelectCore */ - public function having($expression) + public function having(string $expression): self { $this->_having = $expression; @@ -168,14 +152,10 @@ public function having($expression) /** * Set source (regular or a SELECT query). - * - * @param mixed $source Source. - * @param ... ... - * @return \Hoa\Database\Query\SelectCore */ - public function from($source) + public function from(...$sources): self { - foreach (func_get_args() as $source) { + foreach ($sources as $source) { if ($source instanceof self) { $source = '(' . $source . ')'; } @@ -188,11 +168,8 @@ public function from($source) /** * Alias the last declared source. - * - * @param string $alias Alias. - * @return \Hoa\Database\Query\SelectCore */ - public function _as($alias) + public function _as(string $alias): self { if (empty($this->_from)) { return $this; @@ -205,125 +182,91 @@ public function _as($alias) /** * Join a source (regular of a SELECT query). - * - * @param mixed $source Source. - * @return \Hoa\Database\Query\Join */ - public function join($source) + public function join($source): Join { return $this->_join('JOIN', $source); } /** * Natural join a source (regular of a SELECT query). - * - * @param mixed $source Source. - * @return \Hoa\Database\Query\Join */ - public function naturalJoin($source) + public function naturalJoin($source): Join { return $this->_join('NATURAL JOIN', $source); } /** * Left join a source (regular of a SELECT query). - * - * @param mixed $source Source. - * @return \Hoa\Database\Query\Join */ - public function leftJoin($source) + public function leftJoin($source): Join { return $this->_join('LEFT JOIN', $source); } /** * Natural left join a source (regular of a SELECT query). - * - * @param mixed $source Source. - * @return \Hoa\Database\Query\Join */ - public function naturalLeftJoin($source) + public function naturalLeftJoin($source): Join { return $this->_join('NATURAL LEFT JOIN', $source); } /** * Left outer join a source (regular of a SELECT query). - * - * @param mixed $source Source. - * @return \Hoa\Database\Query\Join */ - public function leftOuterJoin($source) + public function leftOuterJoin($source): Join { return $this->_join('LEFT OUTER JOIN', $source); } /** * Natural left outer join a source (regular of a SELECT query). - * - * @param mixed $source Source. - * @return \Hoa\Database\Query\Join */ - public function naturalLeftOuterJoin($source) + public function naturalLeftOuterJoin($source): Join { return $this->_join('NATURAL LEFT OUTER JOIN', $source); } /** * Inner join a source (regular of a SELECT query). - * - * @param mixed $source Source. - * @return \Hoa\Database\Query\Join */ - public function innerJoin($source) + public function innerJoin($source): Join { return $this->_join('INNER JOIN', $source); } /** * Natural inner join a source (regular of a SELECT query). - * - * @param mixed $source Source. - * @return \Hoa\Database\Query\Join */ - public function naturalInnerJoin($source) + public function naturalInnerJoin($source): Join { return $this->_join('NATURAL INNER JOIN', $source); } /** * Cross join a source (regular of a SELECT query). - * - * @param mixed $source Source. - * @return \Hoa\Database\Query\Join */ - public function crossJoin($source) + public function crossJoin($source): Join { return $this->_join('CROSS JOIN', $source); } /** * Natural cross join a source (regular of a SELECT query). - * - * @param mixed $source Source. - * @return \Hoa\Database\Query\Join */ - public function naturalCrossJoin($source) + public function naturalCrossJoin($source): Join { return $this->_join('NATURAL CROSS JOIN', $source); } /** * Make a join. - * - * @param string $type Type. - * @param mixed $source Source. - * @return \Hoa\Database\Query\Join */ - protected function _join($type, $source) + protected function _join(string $type, $source): Join { if (empty($this->_from)) { - return $this; + throw new Exception('Cannot join if there is no `FROM` set.', 0); } if ($source instanceof self) { @@ -343,10 +286,8 @@ protected function _join($type, $source) /** * Reset some properties. - * - * @return \Hoa\Database\Query\SelectCore */ - public function reset() + public function reset(): parent { parent::reset(); $this->_columns = []; @@ -360,10 +301,8 @@ public function reset() /** * Generate the query. - * - * @return string */ - public function __toString() + public function __toString(): string { $out = 'SELECT'; diff --git a/Query/Update.php b/Source/Query/Update.php similarity index 76% rename from Query/Update.php rename to Source/Query/Update.php index 35bc05f..b00fe96 100644 --- a/Query/Update.php +++ b/Source/Query/Update.php @@ -1,5 +1,7 @@ _or('ROLLBACK'); } /** * Update or abort. - * - * @return \Hoa\Database\Query\Update */ - public function abort() + public function abort(): self { return $this->_or('ABORT'); } /** * Update or replace. - * - * @return \Hoa\Database\Query\Update */ - public function replace() + public function replace(): self { return $this->_or('REPLACE'); } /** * Update or fail. - * - * @return \Hoa\Database\Query\Update */ - public function fail() + public function fail(): self { return $this->_or('FAIL'); } /** * Update or ignore. - * - * @return \Hoa\Database\Query\Update */ - public function ignore() + public function ignore(): self { return $this->_or('IGNORE'); } /** * Declare an alternative to “INSERT”. - * - * @param string $or Alternative. - * @return \Hoa\Database\Query\Update */ - protected function _or($or) + protected function _or(string $or): self { $this->_or = $or; @@ -136,11 +122,8 @@ protected function _or($or) /** * Set the table. - * - * @param string $table Table. - * @return \Hoa\Database\Query\Update */ - public function table($table) + public function table(string $table): self { $this->_table = $table; @@ -149,12 +132,8 @@ public function table($table) /** * Set a pair. - * - * @param string $name Name. - * @param mixed $value Value. - * @return \Hoa\Database\Query\Update */ - public function set($name, $value) + public function set(string $name, $value): self { $this->_set[$name] = $value; @@ -163,10 +142,8 @@ public function set($name, $value) /** * Generate the query. - * - * @return string */ - public function __toString() + public function __toString(): string { $out = 'UPDATE'; diff --git a/Query/Where.php b/Source/Query/Where.php similarity index 79% rename from Query/Where.php rename to Source/Query/Where.php index 61e8dd6..d4f2daf 100644 --- a/Query/Where.php +++ b/Source/Query/Where.php @@ -1,5 +1,7 @@ _where[] = $where . $expression; @@ -88,23 +84,16 @@ public function where($expression) /** * Redirect undefined calls to _calls. - * - * @param string $name Name. - * @param array $values Values. - * @return \Hoa\Database\Query\Where */ - public function __call($name, array $values) + public function __call(string $name, array $values): self { return call_user_func_array([$this, '_' . $name], $values); } /** * Set the current logic operator. - * - * @param string $name Name. - * @return \Hoa\Database\Query\Where */ - public function __get($name) + public function __get(string $name): self { switch (strtolower($name)) { case 'and': @@ -122,10 +111,8 @@ public function __get($name) /** * Reset. - * - * @return \Hoa\Database\Query\Where */ - public function reset() + public function reset(): self { $this->_where = []; @@ -134,13 +121,11 @@ public function reset() /** * Generate the query. - * - * @return string */ - public function __toString() + public function __toString(): string { if (empty($this->_where)) { - return null; + return ''; } return ' WHERE ' . implode(' ', $this->_where); diff --git a/Test/Unit/Query/EncloseIdentifier.php b/Test/Unit/Query/EncloseIdentifier.php index 30dd379..9730d1e 100644 --- a/Test/Unit/Query/EncloseIdentifier.php +++ b/Test/Unit/Query/EncloseIdentifier.php @@ -1,5 +1,7 @@ groupBy('a') ->having('"a" > 42') // manual enclose ->orderBy('a') - ->limit('2') + ->limit(2) ->offset('1') ) ->then @@ -154,7 +153,7 @@ public function case_select_expr() ->groupBy('a') ->having('"a" > 42') // manual enclose ->orderBy('a') - ->limit('2') + ->limit(2) ->offset('1') ) ->then @@ -182,7 +181,7 @@ public function case_select_alias() ->groupBy('a') ->having('"a" > 42') // manual enclose ->orderBy('a') - ->limit('2') + ->limit(2) ->offset('1') ) ->then diff --git a/Test/Unit/Query/Example.php b/Test/Unit/Query/Example.php index 175bc79..36365fd 100644 --- a/Test/Unit/Query/Example.php +++ b/Test/Unit/Query/Example.php @@ -1,5 +1,7 @@ select('c') ->from('bar') ->orderBy('c') - ->limit('2') + ->limit(2) ->offset('1') ) ->then diff --git a/composer.json b/composer.json index a8b9ed7..787ad54 100644 --- a/composer.json +++ b/composer.json @@ -24,21 +24,22 @@ "source": "https://central.hoa-project.net/Resource/Library/Database" }, "require": { - "php" : ">=5.5.0", - "hoa/consistency": "~1.0", - "hoa/event" : "~1.0", - "hoa/exception" : "~1.0", - "hoa/iterator" : "~2.0", - "hoa/protocol" : "~1.0", - "hoa/zformat" : "~1.0", + "php" : ">=7.1", + "hoa/consistency": "dev-master", + "hoa/event" : "dev-master", + "hoa/exception" : "dev-master", + "hoa/iterator" : "dev-master", + "hoa/protocol" : "dev-master", + "hoa/zformat" : "dev-master", "ext-pdo" : "*" }, "require-dev": { - "hoa/test": "~2.0" + "hoa/test": "dev-master" }, "autoload": { "psr-4": { - "Hoa\\Database\\": "." + "Hoa\\Database\\" : "Source", + "Hoa\\Database\\Test\\": "Test" } }, "extra": {