Skip to content
Merged
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
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, null, null);
});
}
}
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
60 changes: 50 additions & 10 deletions src/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,56 @@ class Result
/**
* @var mixed
*/
private $data;
private $rows;

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

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

/**
* Result constructor.
*
* @param mixed $data
* @param mixed $rows
* @param int|null $lastInsertedId
* @param int|null $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->rows)
? count($this->rows)
: 0;
}

/**
* Fetch all rows.
*
* @return mixed
*/
public function fetchAllRows()
{
return $this->data;
return $this->rows;
}

/**
Expand All @@ -58,9 +82,25 @@ public function fetchAllRows()
*/
public function fetchFirstRow()
{
return is_array($this->data)
&& count($this->data) >= 1
? reset($this->data)
return is_array($this->rows)
&& count($this->rows) >= 1
? reset($this->rows)
: null;
}

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

/**
* @return int|null
*/
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);
}
}