diff --git a/composer.json b/composer.json index 9a6d73c..d443357 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ } }, "require": { - "php": ">=7", + "php": "^7.1", "phramework/exceptions": "^1.0" }, "require-dev": { diff --git a/src/Operations/Create.php b/src/Operations/Create.php index ac52423..7754e0d 100644 --- a/src/Operations/Create.php +++ b/src/Operations/Create.php @@ -16,8 +16,8 @@ */ namespace Phramework\Database\Operations; -use Phramework\Database\Database; -use Phramework\Exceptions\ServerException; +use Phramework\Database\IAdapter; +use Phramework\Exceptions\DatabaseException; /** * Create operation for databases @@ -27,14 +27,21 @@ */ class Create { + /** @var IAdapter */ + protected $adapter; - const RETURN_ID = 1; - const RETURN_RECORDS = 2; - const RETURN_NUMBER_OF_RECORDS = 4; + public function __construct(IAdapter $adapter) + { + $this->adapter = $adapter; + } + + public const RETURN_ID = 1; + public const RETURN_RECORDS = 2; + public const RETURN_NUMBER_OF_RECORDS = 4; /** * Create a new record in database - * @param array|object $attributes Key-value array or object with records's attributes + * @param object $attributes Key-value array or object with records's attributes * @param string $table Table's name * @param string|null $schema *[Optional]* Table's schema, default is null for no schema * @param integer $return Return method type @@ -42,28 +49,27 @@ class Create * - if **`RETURN_RECORDS`** will return the inserted record * - if **`RETURN_NUMBER_OF_RECORDS`** will return the number of records affected * @return integer|array - * @throws ServerException - * @todo Check RETURNING id for another primary key attribute - * @deprecated + * @throws DatabaseException + * @throws \Exception */ - public static function create( - $attributes, - $table, - $schema = null, + public function create( + \stdClass $attributes, + string $table, + ?string $schema = null, $return = self::RETURN_ID ) { if (is_object($attributes)) { $attributes = (array) $attributes; } - $driver = Database::getAdapterName(); + $driver = $this->adapter->getAdapterName(); //prepare query $query_keys = implode('" , "', array_keys($attributes)); $query_parameter_string = trim(str_repeat('?,', count($attributes)), ','); $query_values = array_values($attributes); - if ($driver == 'postgresql') { + if ($driver === 'postgresql') { //Make sure boolean are strings foreach ($query_values as &$queryValue) { if (is_bool($queryValue)) { @@ -90,29 +96,31 @@ public static function create( $query_parameter_string ); - if ($return == self::RETURN_ID) { + if ($return === self::RETURN_ID) { //Return inserted id - if ($driver == 'postgresql') { + if ($driver === 'postgresql') { $query .= ' RETURNING id'; - $id = Database::executeAndFetch($query, $query_values); + $id = $this->adapter->executeAndFetch($query, $query_values); return $id['id']; } - return Database::executeLastInsertId($query, $query_values); - } elseif ($return == self::RETURN_RECORDS) { + return $this->adapter->executeLastInsertId($query, $query_values); + } + + if ($return === self::RETURN_RECORDS) { //Return records - if ($driver != 'postgresql') { - throw new ServerExcetion( + if ($driver !== 'postgresql') { + throw new \Exception( 'RETURN_RECORDS works only with postgresql adapter' ); } $query .= 'RETURNING *'; - return Database::executeAndFetch($query, $query_values); - } else { - //Return number of records affected - return Database::execute($query, $query_values); + return $this->adapter->executeAndFetch($query, $query_values); } + + //Return number of records affected + return $this->adapter->execute($query, $query_values); } } diff --git a/src/Operations/Delete.php b/src/Operations/Delete.php index 22cfc06..36cab90 100644 --- a/src/Operations/Delete.php +++ b/src/Operations/Delete.php @@ -16,7 +16,8 @@ */ namespace Phramework\Database\Operations; -use \Phramework\Database\Database; +use Phramework\Database\IAdapter; +use Phramework\Exceptions\DatabaseException; /** * Delete operation for databases @@ -26,22 +27,35 @@ */ class Delete { + /** @var IAdapter */ + protected $adapter; + + public function __construct(IAdapter $adapter) + { + $this->adapter = $adapter; + } + /** * Delete database records - * @param string|integer $id + * @param string $id * Id value - * @param array|object $additionalAttributes + * @param \stdClass $additionalAttributes * Additional attributes to use in WHERE $idAttribute - * @param string|array $table Table's name * @param string $idAttribute **[Optional]** * Id attribute * @param null|integer $limit **[Optional]** * Limit clause, when null there is not limit. * @return integer Return number of affected records - * @deprecated + * @throws DatabaseException */ - public static function delete($id, $additionalAttributes, $table, $idAttribute = 'id', $limit = 1) - { + public function delete( + string $id, + \stdClass $additionalAttributes, + string $table, + ?string $schema = null, + string $idAttribute = 'id', + int $limit = 1 + ) { $queryValues = [$id]; $additional = []; @@ -56,14 +70,17 @@ public static function delete($id, $additionalAttributes, $table, $idAttribute = $queryValues[] = $value; } - $tableName = ''; - if (is_array($table) && - isset($table['schema']) && - isset($table['table'])) { - $tableName = '"' . $table['schema'] . '"' - .'."' . $table['table'] . '"'; + if ($schema !== null) { + $tableName = sprintf( + '"%s"."%s"', + $schema, + $table + ); } else { - $tableName = '"' . $table . '"'; + $tableName = sprintf( + '"%s"', + $table + ); } $query = sprintf( @@ -81,6 +98,6 @@ public static function delete($id, $additionalAttributes, $table, $idAttribute = ) ); - return Database::execute($query, $queryValues); + return $this->adapter->execute($query, $queryValues); } } diff --git a/src/Operations/Update.php b/src/Operations/Update.php index 24b5a22..72f8cc3 100644 --- a/src/Operations/Update.php +++ b/src/Operations/Update.php @@ -16,8 +16,8 @@ */ namespace Phramework\Database\Operations; -use \Phramework\Database\Database; -use \Phramework\Exceptions\NotFoundException; +use Phramework\Database\IAdapter; +use Phramework\Exceptions\DatabaseException; /** * Update operation for databases @@ -27,19 +27,25 @@ */ class Update { + /** @var IAdapter */ + protected $adapter; + + public function __construct(IAdapter $adapter) + { + $this->adapter = $adapter; + } + /** * Update database records * @param string|integer $id - * @param array|object $keysValues - * @param string|array $table Table's name - * @param string $idAttribute **[Optional]** Id attribute + * @param object $keysValues * @return integer Return number of affected records * @param null|integer $limit **[Optional]** * Limit clause, when null there is not limit. * @todo Add $additionalAttributes - * @deprecated + * @throws DatabaseException */ - public static function update($id, $keysValues, $table, $idAttribute = 'id', $limit = 1) + public function update($id, \stdClass $keysValues, string $table, ?string $schema = null, $idAttribute = 'id') { //Work with arrays if (is_object($keysValues)) { @@ -52,22 +58,12 @@ public static function update($id, $keysValues, $table, $idAttribute = 'id', $li //Push id to the end $queryValues[] = $id; - $tableName = ''; - - //Work with array - if (is_object($table)) { - $table = (array)$table; - } - - if (is_array($table) - && isset($table['schema']) - && isset($table['table']) - ) { + if ($schema !== null) { $tableName = sprintf( '"%s"."%s"', - $table['schema'], - $table['table'] + $schema, + $table ); } else { $tableName = sprintf( @@ -78,20 +74,14 @@ public static function update($id, $keysValues, $table, $idAttribute = 'id', $li $query = sprintf( 'UPDATE %s SET "%s" = ? - WHERE "%s" = ? - %s', + WHERE "%s" = ?', $tableName, $queryKeys, - $idAttribute, - ( - $limit === null - ? '' - : '' //'LIMIT ' . $limit - ) + $idAttribute ); //Return number of rows affected - $result = Database::execute($query, $queryValues); + $result = $this->adapter->execute($query, $queryValues); return $result; }