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

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
}
},
"require": {
"php": ">=7",
"php": "^7.1",
"phramework/exceptions": "^1.0"
},
"require-dev": {
Expand Down
60 changes: 34 additions & 26 deletions src/Operations/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -27,43 +27,49 @@
*/
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
* - if **`RETURN_ID`** will return the id of last inserted record
* - 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)) {
Expand All @@ -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);
}
}
47 changes: 32 additions & 15 deletions src/Operations/Delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
*/
namespace Phramework\Database\Operations;

use \Phramework\Database\Database;
use Phramework\Database\IAdapter;
use Phramework\Exceptions\DatabaseException;

/**
* Delete operation for databases
Expand All @@ -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 = [];
Expand All @@ -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(
Expand All @@ -81,6 +98,6 @@ public static function delete($id, $additionalAttributes, $table, $idAttribute =
)
);

return Database::execute($query, $queryValues);
return $this->adapter->execute($query, $queryValues);
}
}
48 changes: 19 additions & 29 deletions src/Operations/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)) {
Expand All @@ -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(
Expand All @@ -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;
}
Expand Down