Skip to content

Commit

Permalink
Added write operations result
Browse files Browse the repository at this point in the history
- Last inserted ID
- Number of affected rows

!PgSQL is not implementing this yet. Values 0 for both variables!
  • Loading branch information
mmoreram committed Apr 4, 2020
1 parent 413566d commit d5be64f
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 21 deletions.
25 changes: 19 additions & 6 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,8 @@ public function upsert(
*
* @param string $name
* @param array $fields
* @param array $extra
* @param bool $autoincrementId
*
* @return PromiseInterface<Connection>
*
Expand All @@ -366,7 +368,9 @@ public function upsert(
*/
public function createTable(
string $name,
array $fields
array $fields,
array $extra = [],
bool $autoincrementId = false
): PromiseInterface {
if (empty($fields)) {
throw InvalidArgumentException::fromEmptyFieldsArray();
Expand All @@ -375,15 +379,24 @@ public function createTable(
$schema = new Schema();
$table = $schema->createTable($name);
foreach ($fields as $field => $type) {
$extra = [];
if ($type = 'string') {
$extra = ['length' => 255];
$extraField = (
array_key_exists($field, $extra) &&
is_array($extra[$field])
) ? $extra[$field] : [];

if (
$type == 'string' &&
!array_key_exists('length', $extraField)
) {
$extraField = ['length' => 255];
}

$table->addColumn($field, $type, $extra);
$table->addColumn($field, $type, $extraField);
}

$table->setPrimaryKey([array_key_first($fields)]);
$id = array_key_first($fields);
$table->setPrimaryKey([$id]);
$table->getColumn($id)->setAutoincrement($autoincrementId);

return $this->executeSchema($schema);
}
Expand Down
7 changes: 6 additions & 1 deletion src/Driver/Mysql/MysqlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,12 @@ public function query(
->connection
->query($sql, $parameters)
->then(function (QueryResult $queryResult) {
return new Result($queryResult->resultRows);

return new Result(
$queryResult->resultRows,
$queryResult->insertId,
$queryResult->affectedRows
);
})
->otherwise(function (Exception $exception) {
$message = $exception->getMessage();
Expand Down
2 changes: 1 addition & 1 deletion src/Driver/PostgreSQL/PostgreSQLDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public function query(
return $deferred
->promise()
->then(function ($results) {
return new Result($results);
return new Result($results, 0, 0);
});
}
}
7 changes: 6 additions & 1 deletion src/Driver/SQLite/SQLiteDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,12 @@ public function query(
->database
->query($sql, $parameters)
->then(function (SQLiteResult $sqliteResult) {
return new Result($sqliteResult->rows);

return new Result(
$sqliteResult->rows,
$sqliteResult->insertId,
$sqliteResult->changed
);
})
->otherwise(function (RuntimeException $exception) {
$message = $exception->getMessage();
Expand Down
52 changes: 46 additions & 6 deletions src/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,52 @@ class Result
/**
* @var mixed
*/
private $data;
private $rows;

/**
* @var int
*/
private $lastInsertedId;

/**
* @var int
*/
private $affectedRows;

/**
* Result constructor.
*
* @param mixed $data
* @param mixed $rows
* @param int $lastInsertedId
* @param int $affectedRows
*/
public function __construct($data)
public function __construct(
$rows,
int $lastInsertedId,
int $affectedRows
)
{
$this->data = $data;
$this->rows = $rows;
$this->lastInsertedId = $lastInsertedId;
$this->affectedRows = $affectedRows;
}

/**
* Fetch count.
*
* @return int
*/
public function fetchCount()
public function fetchCount() : int
{
return count($this->data);
return is_array($this->data)
? count($this->data)
: 0;
}

/**
* Fetch all rows.
*
* @return mixed
*/
public function fetchAllRows()
{
Expand All @@ -63,4 +87,20 @@ public function fetchFirstRow()
? reset($this->data)
: null;
}

/**
* @return int
*/
public function getLastInsertedId(): int
{
return $this->lastInsertedId;
}

/**
* @return int
*/
public function getAffectedRows(): int
{
return $this->affectedRows;
}
}
80 changes: 74 additions & 6 deletions tests/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,21 @@ abstract protected function getConnection(LoopInterface $loop): Connection;

/**
* @param Connection $connection
* @param bool $autoincrementedId
*
* @return PromiseInterface
*/
protected function createInfrastructure(Connection $connection): PromiseInterface
protected function createInfrastructure(
Connection $connection,
bool $autoincrementedId = false
): PromiseInterface
{
return $connection
->createTable('test', [
'id' => 'string',
'id' => $autoincrementedId ? 'integer' : 'string',
'field1' => 'string',
'field2' => 'string',
])
], [], $autoincrementedId)
->otherwise(function (TableExistsException $_) use ($connection) {
// Silent pass

Expand Down Expand Up @@ -87,15 +91,19 @@ protected function dropInfrastructure(Connection $connection): PromiseInterface

/**
* @param Connection $connection
* @param bool $autoincrementedId
*
* @return PromiseInterface
*/
protected function resetInfrastructure(Connection $connection): PromiseInterface
protected function resetInfrastructure(
Connection $connection,
bool $autoincrementedId = false
): PromiseInterface
{
return $this
->dropInfrastructure($connection)
->then(function () use ($connection) {
return $this->createInfrastructure($connection);
->then(function () use ($connection, $autoincrementedId) {
return $this->createInfrastructure($connection, $autoincrementedId);
});
}

Expand Down Expand Up @@ -446,4 +454,64 @@ public function testDelete()

await($promise, $loop, self::MAX_TIMEOUT);
}

/**
* Test get last inserted id
*
* @group lele
*/
public function testGetLastInsertedId()
{
if (get_class($this) === PostgreSQLConnectionTest::class) {
$this->markTestSkipped('Not implemented yet on postgresql');
return;
}

$loop = $this->createLoop();
$connection = $this->getConnection($loop);
$promise = $this
->resetInfrastructure($connection, true)
->then(function (Connection $connection) {
return $connection->insert('test', [
'field1' => 'val1',
'field2' => 'val2',
]);
})
->then(function(Result $result) {
$this->assertEquals(1, $result->getLastInsertedId());
})
->then(function () use ($connection) {
return all([
$connection->insert('test', [
'field1' => 'val3',
'field2' => 'val4',
]),
$connection->insert('test', [
'field1' => 'val5',
'field2' => 'val6',
])
]);
})
->then(function(array $results) {
$this->assertEquals(1, $results[0]->getAffectedRows());
$this->assertEquals(3, $results[1]->getLastInsertedId());
})
->then(function () use ($connection) {
return all([
$connection->delete('test', [
'id' => 2,
]),
$connection->insert('test', [
'field1' => 'val7',
'field2' => 'val8',
])
]);
})
->then(function(array$results) {
$this->assertEquals(1, $results[0]->getAffectedRows());
$this->assertEquals(4, $results[1]->getLastInsertedId());
});

await($promise, $loop, self::MAX_TIMEOUT);
}
}

0 comments on commit d5be64f

Please sign in to comment.