Skip to content

Commit f35c9f4

Browse files
committed
Fixed PR + Added tests for mysql8
1 parent 6182cbb commit f35c9f4

11 files changed

+200
-56
lines changed

.php_cs.cache

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +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}}
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":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}}

docker-compose.yml

+13-3
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,29 @@ services:
44

55
postgres:
66
image: "postgres:alpine"
7-
container_name: postgres-drift
7+
container_name: drift-postgres
88
environment:
99
POSTGRES_PASSWORD: root
1010
POSTGRES_USER: root
1111
POSTGRES_DB: test
1212
ports:
1313
- "5432:5432"
1414

15-
mysql:
15+
mysql5:
1616
image: "mysql:5"
17-
container_name: mysql-drift
17+
container_name: drift-mysql5
1818
environment:
1919
MYSQL_ROOT_PASSWORD: root
2020
MYSQL_DATABASE: test
2121
ports:
2222
- "3306:3306"
23+
24+
mysql8:
25+
image: "mysql:8"
26+
container_name: drift-mysql8
27+
command: --default-authentication-plugin=mysql_native_password
28+
environment:
29+
MYSQL_ROOT_PASSWORD: root
30+
MYSQL_DATABASE: test
31+
ports:
32+
- "3307:3306"

src/Connection.php

+1-6
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,6 @@ public function insert(
248248
string $table,
249249
array $values
250250
): PromiseInterface {
251-
252251
$queryBuilder = $this->createQueryBuilder();
253252

254253
return $this->driver->insert($queryBuilder, $table, $values);
@@ -309,11 +308,7 @@ public function update(
309308
$queryBuilder->setParameters($parameters);
310309
$this->applyWhereClausesFromArray($queryBuilder, $id);
311310

312-
return $this
313-
->query($queryBuilder)
314-
->then(function (Result $result) {
315-
return $result->fetchAllRows();
316-
});
311+
return $this->query($queryBuilder);
317312
}
318313

319314
/**

src/Driver/AbstractDriver.php

+31-4
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,49 @@
11
<?php
22

3+
/*
4+
* This file is part of the DriftPHP Project
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*
9+
* Feel free to edit as you please, and have fun.
10+
*
11+
* @author Marc Morera <[email protected]>
12+
*/
313

4-
namespace Drift\DBAL\Driver;
14+
declare(strict_types=1);
515

16+
namespace Drift\DBAL\Driver;
617

718
use Doctrine\DBAL\Query\QueryBuilder;
819
use React\Promise\PromiseInterface;
920

10-
abstract class AbstractDriver
21+
/**
22+
* Class AbstractDriver.
23+
*/
24+
abstract class AbstractDriver implements Driver
1125
{
12-
abstract public function query(string $sql, array $parameters): PromiseInterface;
13-
26+
/**
27+
* @param QueryBuilder $queryBuilder
28+
* @param string $table
29+
* @param array $values
30+
*
31+
* @return PromiseInterface
32+
*/
1433
public function insert(QueryBuilder $queryBuilder, string $table, array $values): PromiseInterface
1534
{
1635
$queryBuilder = $this->createInsertQuery($queryBuilder, $table, $values);
36+
1737
return $this->query($queryBuilder->getSQL(), $queryBuilder->getParameters());
1838
}
1939

40+
/**
41+
* @param QueryBuilder $queryBuilder
42+
* @param string $table
43+
* @param array $values
44+
*
45+
* @return QueryBuilder
46+
*/
2047
protected function createInsertQuery(QueryBuilder $queryBuilder, string $table, array $values): QueryBuilder
2148
{
2249
return $queryBuilder->insert($table)

src/Driver/Mysql/EmptyDoctrineMysqlDriver.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
/**
2222
* Class EmptyDoctrineMysqlDriver.
2323
*/
24-
final class EmptyDoctrineMysqlDriver
24+
final class EmptyDoctrineMysqlDriver extends AbstractMySQLDriver
2525
{
2626
/**
2727
* {@inheritdoc}

src/Driver/Mysql/MysqlDriver.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,8 @@
1515

1616
namespace Drift\DBAL\Driver\Mysql;
1717

18-
use Doctrine\DBAL\Query\QueryBuilder;
1918
use Drift\DBAL\Credentials;
2019
use Drift\DBAL\Driver\AbstractDriver;
21-
use Drift\DBAL\Driver\Driver;
2220
use Drift\DBAL\Driver\PlainDriverException;
2321
use Drift\DBAL\Result;
2422
use React\EventLoop\LoopInterface;
@@ -32,7 +30,7 @@
3230
/**
3331
* Class MysqlDriver.
3432
*/
35-
class MysqlDriver extends AbstractDriver implements Driver
33+
class MysqlDriver extends AbstractDriver
3634
{
3735
/**
3836
* @var Factory
@@ -91,7 +89,6 @@ public function query(
9189
);
9290
})
9391
->otherwise(function (Exception $exception) {
94-
9592
$message = $exception->getMessage();
9693

9794
throw $this->doctrineDriver->convertException($message, PlainDriverException::createFromMessageAndErrorCode($message, (string) $exception->getCode()));

src/Driver/PostgreSQL/PostgreSQLDriver.php

+34-13
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
use Doctrine\DBAL\Query\QueryBuilder;
1919
use Drift\DBAL\Credentials;
2020
use Drift\DBAL\Driver\AbstractDriver;
21-
use Drift\DBAL\Driver\Driver;
2221
use Drift\DBAL\Driver\PlainDriverException;
2322
use Drift\DBAL\Result;
2423
use PgAsync\Client;
@@ -30,7 +29,7 @@
3029
/**
3130
* Class PostgreSQLDriver.
3231
*/
33-
class PostgreSQLDriver extends AbstractDriver implements Driver
32+
class PostgreSQLDriver extends AbstractDriver
3433
{
3534
/**
3635
* @var Client
@@ -124,19 +123,41 @@ public function query(
124123
});
125124
}
126125

126+
/**
127+
* @param QueryBuilder $queryBuilder
128+
* @param string $table
129+
* @param array $values
130+
*
131+
* @return PromiseInterface
132+
*/
127133
public function insert(QueryBuilder $queryBuilder, string $table, array $values): PromiseInterface
128134
{
129-
130135
$queryBuilder = $this->createInsertQuery($queryBuilder, $table, $values);
131-
$query = "SELECT COLUMN_NAME FROM information_schema.COLUMNS WHERE TABLE_NAME = ?";
132-
133-
return $this->query($query, [$table])->then(function (Result $response) use($queryBuilder){
134-
$allRows = $response->fetchAllRows();
135-
$fields = array_map(function ($item){
136-
return $item['column_name'];
137-
}, $allRows);
138-
139-
return $this->query($queryBuilder->getSQL() . ' RETURNING ' . implode(',', $fields), $queryBuilder->getParameters());
140-
});
136+
$query = 'SELECT COLUMN_NAME FROM information_schema.COLUMNS WHERE TABLE_NAME = ?';
137+
138+
return $this
139+
->query($query, [$table])
140+
->then(function (Result $response) use ($queryBuilder) {
141+
$allRows = $response->fetchAllRows();
142+
$fields = array_map(function ($item) {
143+
return $item['column_name'];
144+
}, $allRows);
145+
146+
// When there are no fields, means that the table does not exist
147+
// To make the normal behavior, we make a simple query and let
148+
// the DBAL do the job (no last_inserted_it is expected here
149+
150+
$returningPart = empty($fields)
151+
? ''
152+
: ' RETURNING '.implode(',', $fields);
153+
154+
return $this
155+
->query($queryBuilder->getSQL().$returningPart, $queryBuilder->getParameters())
156+
->then(function (Result $result) use ($fields) {
157+
return 0 === count($fields)
158+
? new Result()
159+
: new Result([], \intval($result->fetchFirstRow()[$fields[0]]), 1);
160+
});
161+
});
141162
}
142163
}

src/Driver/SQLite/SQLiteDriver.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@
1818
use Clue\React\SQLite\DatabaseInterface;
1919
use Clue\React\SQLite\Factory;
2020
use Clue\React\SQLite\Result as SQLiteResult;
21-
use Doctrine\DBAL\Query\QueryBuilder;
2221
use Drift\DBAL\Credentials;
2322
use Drift\DBAL\Driver\AbstractDriver;
24-
use Drift\DBAL\Driver\Driver;
2523
use Drift\DBAL\Driver\PlainDriverException;
2624
use Drift\DBAL\Result;
2725
use React\EventLoop\LoopInterface;
@@ -31,7 +29,7 @@
3129
/**
3230
* Class SQLiteDriver.
3331
*/
34-
class SQLiteDriver extends AbstractDriver implements Driver
32+
class SQLiteDriver extends AbstractDriver
3533
{
3634
/**
3735
* @var Factory
@@ -92,5 +90,4 @@ public function query(
9290
throw $this->doctrineDriver->convertException($message, PlainDriverException::createFromMessageAndErrorCode($message, (string) $exception->getCode()));
9391
});
9492
}
95-
9693
}

0 commit comments

Comments
 (0)