Skip to content

Commit

Permalink
Fixed PR
Browse files Browse the repository at this point in the history
  • Loading branch information
mmoreram committed Jun 16, 2020
1 parent 6182cbb commit ffcd5d8
Show file tree
Hide file tree
Showing 10 changed files with 144 additions and 56 deletions.
2 changes: 1 addition & 1 deletion .php_cs.cache
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"php":"7.4.5","version":"2.16.1","indent":" ","lineEnding":"\n","rules":{"blank_line_after_namespace":true,"braces":{"allow_single_line_closure":true},"class_definition":{"single_line":true},"constant_case":true,"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_keywords":true,"method_argument_space":true,"no_break_comment":true,"no_closing_tag":true,"no_spaces_after_function_name":true,"no_spaces_inside_parenthesis":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":true,"single_import_per_statement":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"visibility_required":true,"encoding":true,"full_opening_tag":true,"array_syntax":{"syntax":"short"},"binary_operator_spaces":true,"blank_line_after_opening_tag":true,"blank_line_before_statement":{"statements":["return"]},"cast_spaces":true,"class_attributes_separation":{"elements":["method"]},"concat_space":true,"declare_equal_normalize":true,"function_typehint_space":true,"include":true,"increment_style":true,"lowercase_cast":true,"lowercase_static_reference":true,"magic_constant_casing":true,"magic_method_casing":true,"native_function_casing":true,"native_function_type_declaration_casing":true,"new_with_braces":true,"no_blank_lines_after_class_opening":true,"no_blank_lines_after_phpdoc":true,"no_empty_comment":true,"no_empty_phpdoc":true,"no_empty_statement":true,"no_extra_blank_lines":{"tokens":["curly_brace_block","extra","parenthesis_brace_block","square_brace_block","throw","use"]},"no_leading_import_slash":true,"no_leading_namespace_whitespace":true,"no_mixed_echo_print":true,"no_multiline_whitespace_around_double_arrow":true,"no_short_bool_cast":true,"no_singleline_whitespace_before_semicolons":true,"no_spaces_around_offset":true,"no_trailing_comma_in_list_call":true,"no_trailing_comma_in_singleline_array":true,"no_unneeded_control_parentheses":true,"no_unneeded_curly_braces":true,"no_unneeded_final_method":true,"no_unused_imports":true,"no_whitespace_before_comma_in_array":true,"no_whitespace_in_blank_line":true,"normalize_index_brace":true,"object_operator_without_whitespace":true,"ordered_imports":true,"php_unit_fqcn_annotation":true,"phpdoc_align":{"tags":["method","param","property","return","throws","type","var"]},"phpdoc_annotation_without_dot":true,"phpdoc_indent":true,"phpdoc_inline_tag":true,"phpdoc_no_access":true,"phpdoc_no_alias_tag":true,"phpdoc_no_package":true,"phpdoc_no_useless_inheritdoc":true,"phpdoc_return_self_reference":true,"phpdoc_scalar":true,"phpdoc_separation":true,"phpdoc_single_line_var_spacing":true,"phpdoc_summary":true,"phpdoc_to_comment":true,"phpdoc_trim":true,"phpdoc_trim_consecutive_blank_line_separation":true,"phpdoc_types":true,"phpdoc_types_order":{"null_adjustment":"always_last","sort_algorithm":"none"},"phpdoc_var_without_name":true,"return_type_declaration":true,"semicolon_after_instruction":true,"short_scalar_cast":true,"single_blank_line_before_namespace":true,"single_line_comment_style":{"comment_types":["hash"]},"single_line_throw":true,"single_quote":true,"single_trait_insert_per_statement":true,"space_after_semicolon":{"remove_in_empty_for_expressions":true},"standardize_increment":true,"standardize_not_equals":true,"ternary_operator_spaces":true,"trailing_comma_in_multiline_array":true,"trim_array_spaces":true,"unary_operator_spaces":true,"whitespace_after_comma_in_array":true,"yoda_style":true},"hashes":{"src\/Driver\/Mysql\/MysqlDriver.php":1904002967,"src\/Driver\/Mysql\/EmptyDoctrineMysqlDriver.php":3752159343,"src\/Driver\/PlainDriverException.php":1220843308,"src\/Driver\/SQLite\/SQLiteDriver.php":1438486404,"src\/Driver\/SQLite\/EmptyDoctrineSQLiteDriver.php":117021226,"src\/Driver\/Driver.php":4188879766,"src\/Driver\/PostgreSQL\/EmptyDoctrinePostgreSQLDriver.php":2852569520,"src\/Driver\/PostgreSQL\/PostgreSQLDriver.php":72186178,"src\/Credentials.php":1470751445,"src\/Result.php":85073553,"src\/Connection.php":1534170315,"src\/Mock\/MockedDBALConnection.php":2847858253,"src\/Mock\/MockedDriver.php":1506357739,"tests\/ConnectionTest.php":720518795,"tests\/SQLiteConnectionTest.php":715561380,"tests\/PostgreSQLConnectionTest.php":1654116774,"tests\/MysqlConnectionTest.php":1643302408}}
{"php":"7.4.5","version":"2.16.1","indent":" ","lineEnding":"\n","rules":{"blank_line_after_namespace":true,"braces":{"allow_single_line_closure":true},"class_definition":{"single_line":true},"constant_case":true,"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_keywords":true,"method_argument_space":true,"no_break_comment":true,"no_closing_tag":true,"no_spaces_after_function_name":true,"no_spaces_inside_parenthesis":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":true,"single_import_per_statement":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"visibility_required":true,"encoding":true,"full_opening_tag":true,"array_syntax":{"syntax":"short"},"binary_operator_spaces":true,"blank_line_after_opening_tag":true,"blank_line_before_statement":{"statements":["return"]},"cast_spaces":true,"class_attributes_separation":{"elements":["method"]},"concat_space":true,"declare_equal_normalize":true,"function_typehint_space":true,"include":true,"increment_style":true,"lowercase_cast":true,"lowercase_static_reference":true,"magic_constant_casing":true,"magic_method_casing":true,"native_function_casing":true,"native_function_type_declaration_casing":true,"new_with_braces":true,"no_blank_lines_after_class_opening":true,"no_blank_lines_after_phpdoc":true,"no_empty_comment":true,"no_empty_phpdoc":true,"no_empty_statement":true,"no_extra_blank_lines":{"tokens":["curly_brace_block","extra","parenthesis_brace_block","square_brace_block","throw","use"]},"no_leading_import_slash":true,"no_leading_namespace_whitespace":true,"no_mixed_echo_print":true,"no_multiline_whitespace_around_double_arrow":true,"no_short_bool_cast":true,"no_singleline_whitespace_before_semicolons":true,"no_spaces_around_offset":true,"no_trailing_comma_in_list_call":true,"no_trailing_comma_in_singleline_array":true,"no_unneeded_control_parentheses":true,"no_unneeded_curly_braces":true,"no_unneeded_final_method":true,"no_unused_imports":true,"no_whitespace_before_comma_in_array":true,"no_whitespace_in_blank_line":true,"normalize_index_brace":true,"object_operator_without_whitespace":true,"ordered_imports":true,"php_unit_fqcn_annotation":true,"phpdoc_align":{"tags":["method","param","property","return","throws","type","var"]},"phpdoc_annotation_without_dot":true,"phpdoc_indent":true,"phpdoc_inline_tag":true,"phpdoc_no_access":true,"phpdoc_no_alias_tag":true,"phpdoc_no_package":true,"phpdoc_no_useless_inheritdoc":true,"phpdoc_return_self_reference":true,"phpdoc_scalar":true,"phpdoc_separation":true,"phpdoc_single_line_var_spacing":true,"phpdoc_summary":true,"phpdoc_to_comment":true,"phpdoc_trim":true,"phpdoc_trim_consecutive_blank_line_separation":true,"phpdoc_types":true,"phpdoc_types_order":{"null_adjustment":"always_last","sort_algorithm":"none"},"phpdoc_var_without_name":true,"return_type_declaration":true,"semicolon_after_instruction":true,"short_scalar_cast":true,"single_blank_line_before_namespace":true,"single_line_comment_style":{"comment_types":["hash"]},"single_line_throw":true,"single_quote":true,"single_trait_insert_per_statement":true,"space_after_semicolon":{"remove_in_empty_for_expressions":true},"standardize_increment":true,"standardize_not_equals":true,"ternary_operator_spaces":true,"trailing_comma_in_multiline_array":true,"trim_array_spaces":true,"unary_operator_spaces":true,"whitespace_after_comma_in_array":true,"yoda_style":true},"hashes":{"src\/Driver\/Mysql\/MysqlDriver.php":347719532,"src\/Driver\/Mysql\/EmptyDoctrineMysqlDriver.php":3752159343,"src\/Driver\/PlainDriverException.php":1220843308,"src\/Driver\/SQLite\/SQLiteDriver.php":1642906119,"src\/Driver\/SQLite\/EmptyDoctrineSQLiteDriver.php":117021226,"src\/Driver\/Driver.php":2665996535,"src\/Driver\/PostgreSQL\/EmptyDoctrinePostgreSQLDriver.php":2852569520,"src\/Driver\/PostgreSQL\/PostgreSQLDriver.php":3513572624,"src\/Credentials.php":1470751445,"src\/Result.php":85073553,"src\/Connection.php":3362170122,"src\/Mock\/MockedDBALConnection.php":2847858253,"src\/Mock\/MockedDriver.php":1506357739,"tests\/ConnectionTest.php":2012967726,"tests\/SQLiteConnectionTest.php":715561380,"tests\/PostgreSQLConnectionTest.php":1654116774,"tests\/MysqlConnectionTest.php":1643302408,"src\/Driver\/AbstractDriver.php":219146991}}
6 changes: 3 additions & 3 deletions docker-compose.yml → docker-compose/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ services:

postgres:
image: "postgres:alpine"
container_name: postgres-drift
container_name: drift-postgres
environment:
POSTGRES_PASSWORD: root
POSTGRES_USER: root
POSTGRES_DB: test
ports:
- "5432:5432"

mysql:
mysql5:
image: "mysql:5"
container_name: mysql-drift
container_name: drift-mysql5
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: test
Expand Down
7 changes: 1 addition & 6 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,6 @@ public function insert(
string $table,
array $values
): PromiseInterface {

$queryBuilder = $this->createQueryBuilder();

return $this->driver->insert($queryBuilder, $table, $values);
Expand Down Expand Up @@ -309,11 +308,7 @@ public function update(
$queryBuilder->setParameters($parameters);
$this->applyWhereClausesFromArray($queryBuilder, $id);

return $this
->query($queryBuilder)
->then(function (Result $result) {
return $result->fetchAllRows();
});
return $this->query($queryBuilder);
}

/**
Expand Down
35 changes: 31 additions & 4 deletions src/Driver/AbstractDriver.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,49 @@
<?php

/*
* This file is part of the DriftPHP Project
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Feel free to edit as you please, and have fun.
*
* @author Marc Morera <yuhu@mmoreram.com>
*/

namespace Drift\DBAL\Driver;
declare(strict_types=1);

namespace Drift\DBAL\Driver;

use Doctrine\DBAL\Query\QueryBuilder;
use React\Promise\PromiseInterface;

abstract class AbstractDriver
/**
* Class AbstractDriver.
*/
abstract class AbstractDriver implements Driver
{
abstract public function query(string $sql, array $parameters): PromiseInterface;

/**
* @param QueryBuilder $queryBuilder
* @param string $table
* @param array $values
*
* @return PromiseInterface
*/
public function insert(QueryBuilder $queryBuilder, string $table, array $values): PromiseInterface
{
$queryBuilder = $this->createInsertQuery($queryBuilder, $table, $values);

return $this->query($queryBuilder->getSQL(), $queryBuilder->getParameters());
}

/**
* @param QueryBuilder $queryBuilder
* @param string $table
* @param array $values
*
* @return QueryBuilder
*/
protected function createInsertQuery(QueryBuilder $queryBuilder, string $table, array $values): QueryBuilder
{
return $queryBuilder->insert($table)
Expand Down
2 changes: 1 addition & 1 deletion src/Driver/Mysql/EmptyDoctrineMysqlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
/**
* Class EmptyDoctrineMysqlDriver.
*/
final class EmptyDoctrineMysqlDriver
final class EmptyDoctrineMysqlDriver extends AbstractMySQLDriver
{
/**
* {@inheritdoc}
Expand Down
5 changes: 1 addition & 4 deletions src/Driver/Mysql/MysqlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@

namespace Drift\DBAL\Driver\Mysql;

use Doctrine\DBAL\Query\QueryBuilder;
use Drift\DBAL\Credentials;
use Drift\DBAL\Driver\AbstractDriver;
use Drift\DBAL\Driver\Driver;
use Drift\DBAL\Driver\PlainDriverException;
use Drift\DBAL\Result;
use React\EventLoop\LoopInterface;
Expand All @@ -32,7 +30,7 @@
/**
* Class MysqlDriver.
*/
class MysqlDriver extends AbstractDriver implements Driver
class MysqlDriver extends AbstractDriver
{
/**
* @var Factory
Expand Down Expand Up @@ -91,7 +89,6 @@ public function query(
);
})
->otherwise(function (Exception $exception) {

$message = $exception->getMessage();

throw $this->doctrineDriver->convertException($message, PlainDriverException::createFromMessageAndErrorCode($message, (string) $exception->getCode()));
Expand Down
47 changes: 34 additions & 13 deletions src/Driver/PostgreSQL/PostgreSQLDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
use Doctrine\DBAL\Query\QueryBuilder;
use Drift\DBAL\Credentials;
use Drift\DBAL\Driver\AbstractDriver;
use Drift\DBAL\Driver\Driver;
use Drift\DBAL\Driver\PlainDriverException;
use Drift\DBAL\Result;
use PgAsync\Client;
Expand All @@ -30,7 +29,7 @@
/**
* Class PostgreSQLDriver.
*/
class PostgreSQLDriver extends AbstractDriver implements Driver
class PostgreSQLDriver extends AbstractDriver
{
/**
* @var Client
Expand Down Expand Up @@ -124,19 +123,41 @@ public function query(
});
}

/**
* @param QueryBuilder $queryBuilder
* @param string $table
* @param array $values
*
* @return PromiseInterface
*/
public function insert(QueryBuilder $queryBuilder, string $table, array $values): PromiseInterface
{

$queryBuilder = $this->createInsertQuery($queryBuilder, $table, $values);
$query = "SELECT COLUMN_NAME FROM information_schema.COLUMNS WHERE TABLE_NAME = ?";

return $this->query($query, [$table])->then(function (Result $response) use($queryBuilder){
$allRows = $response->fetchAllRows();
$fields = array_map(function ($item){
return $item['column_name'];
}, $allRows);

return $this->query($queryBuilder->getSQL() . ' RETURNING ' . implode(',', $fields), $queryBuilder->getParameters());
});
$query = 'SELECT COLUMN_NAME FROM information_schema.COLUMNS WHERE TABLE_NAME = ?';

return $this
->query($query, [$table])
->then(function (Result $response) use ($queryBuilder) {
$allRows = $response->fetchAllRows();
$fields = array_map(function ($item) {
return $item['column_name'];
}, $allRows);

// When there are no fields, means that the table does not exist
// To make the normal behavior, we make a simple query and let
// the DBAL do the job (no last_inserted_it is expected here

$returningPart = empty($fields)
? ''
: ' RETURNING '.implode(',', $fields);

return $this
->query($queryBuilder->getSQL().$returningPart, $queryBuilder->getParameters())
->then(function (Result $result) use ($fields) {
return 0 === count($fields)
? new Result()
: new Result([], \intval($result->fetchFirstRow()[$fields[0]]), 1);
});
});
}
}
5 changes: 1 addition & 4 deletions src/Driver/SQLite/SQLiteDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@
use Clue\React\SQLite\DatabaseInterface;
use Clue\React\SQLite\Factory;
use Clue\React\SQLite\Result as SQLiteResult;
use Doctrine\DBAL\Query\QueryBuilder;
use Drift\DBAL\Credentials;
use Drift\DBAL\Driver\AbstractDriver;
use Drift\DBAL\Driver\Driver;
use Drift\DBAL\Driver\PlainDriverException;
use Drift\DBAL\Result;
use React\EventLoop\LoopInterface;
Expand All @@ -31,7 +29,7 @@
/**
* Class SQLiteDriver.
*/
class SQLiteDriver extends AbstractDriver implements Driver
class SQLiteDriver extends AbstractDriver
{
/**
* @var Factory
Expand Down Expand Up @@ -92,5 +90,4 @@ public function query(
throw $this->doctrineDriver->convertException($message, PlainDriverException::createFromMessageAndErrorCode($message, (string) $exception->getCode()));
});
}

}
87 changes: 69 additions & 18 deletions tests/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,12 @@ public function testFindShortcut()
]);

$this->assertNull($results[1]);
$this->assertEquals($results[2], [
$listResults = $results[2];
usort($listResults, function ($a1, $a2) {
return $a1['id'] > $a2['id'];
});

$this->assertSame($listResults, [
[
'id' => '1',
'field1' => 'val1',
Expand Down Expand Up @@ -503,8 +508,6 @@ public function testDelete()

/**
* Test get last inserted id.
*
* @group lele
*/
public function testGetLastInsertedId()
{
Expand All @@ -524,33 +527,81 @@ public function testGetLastInsertedId()
->then(function () use ($connection) {
return all([
$connection->insert('test', [
'field1' => 'val3',
'field2' => 'val4',
]),
'field1' => 'val3',
'field2' => 'val4',
]),
$connection->insert('test', [
'field1' => 'val5',
'field2' => 'val6',
]),
]);
})
->then(function (array $results) {
$this->assertEquals(1, $results[0]->getAffectedRows());
$this->assertEquals(2, $results[0]->getLastInsertedId());
$this->assertEquals(3, $results[1]->getLastInsertedId());
})
->then(function () use ($connection) {
return all([
$connection->delete('test', [
'id' => 2,
]),
$connection->insert('test', [
'field1' => 'val7',
'field2' => 'val8',
]),
return $connection->insert('test', [
'field1' => 'val7',
'field2' => 'val8',
]);
})
->then(function (array $results) {
$this->assertEquals(1, $results[0]->getAffectedRows());
$this->assertEquals(4, $results[1]->getLastInsertedId());
->then(function (Result $result) {
$this->assertEquals(4, $result->getLastInsertedId());
});

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

/**
* Test affected rows.
*/
public function testAffectedRows()
{
if ($this instanceof PostgreSQLConnectionTest) {
$this->markTestSkipped('This feature is not implemented in the Postgres client');
}

$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) use ($connection) {
$this->assertEquals(1, $result->getAffectedRows());

return $connection->insert('test', [
'field1' => 'val1',
'field2' => 'val4',
]);
})
->then(function () use ($connection) {
return $connection->update('test', [
'field1' => 'val1',
], [
'field2' => 'new5',
]);
})
->then(function (Result $result) use ($connection) {
$this->assertEquals(2, $result->getAffectedRows());

return $connection->insert('test', [
'field1' => 'val1',
'field2' => 'val8',
]);
})
->then(function () use ($connection) {
return $connection->delete('test', [
'field1' => 'val1',
]);
})
->then(function (Result $result) {
$this->assertEquals(3, $result->getAffectedRows());
});

await($promise, $loop, self::MAX_TIMEOUT);
Expand Down
Loading

0 comments on commit ffcd5d8

Please sign in to comment.