Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
71.43% |
5 / 7 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
Driver | |
71.43% |
5 / 7 |
|
0.00% |
0 / 1 |
9.49 | |
0.00% |
0 / 1 |
__construct | n/a |
0 / 0 |
n/a |
0 / 0 |
3 | |||||
returning | |
71.43% |
5 / 7 |
|
0.00% |
0 / 1 |
5.58 |
1 | <?php declare(strict_types=1); |
2 | /** |
3 | * Query |
4 | * |
5 | * SQL Query Builder / Database Abstraction Layer |
6 | * |
7 | * PHP version 8.1 |
8 | * |
9 | * @package Query |
10 | * @author Timothy J. Warren <tim@timshome.page> |
11 | * @copyright 2012 - 2023 Timothy J. Warren |
12 | * @license http://www.opensource.org/licenses/mit-license.html MIT License |
13 | * @link https://git.timshomepage.net/aviat/Query |
14 | * @version 4.0.0 |
15 | */ |
16 | |
17 | namespace Query\Drivers\Mysql; |
18 | |
19 | use PDO; |
20 | use Query\Drivers\AbstractDriver; |
21 | use function defined; |
22 | |
23 | /** |
24 | * MySQL specific class |
25 | */ |
26 | class Driver extends AbstractDriver |
27 | { |
28 | /** |
29 | * Set the backtick as the MySQL escape character |
30 | */ |
31 | protected string $escapeCharOpen = '`'; |
32 | |
33 | /** |
34 | * Set the backtick as the MySQL escape character |
35 | */ |
36 | protected string $escapeCharClose = '`'; |
37 | |
38 | /** |
39 | * Connect to MySQL Database |
40 | * |
41 | * @codeCoverageIgnore |
42 | */ |
43 | public function __construct(string $dsn, ?string $username=NULL, ?string $password=NULL, array $options=[]) |
44 | { |
45 | // Set the charset to UTF-8 |
46 | if (defined('\\PDO::MYSQL_ATTR_INIT_COMMAND')) |
47 | { |
48 | $options = array_merge($options, [ |
49 | PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES UTF-8 COLLATE 'UTF-8'", |
50 | ]); |
51 | } |
52 | |
53 | if ( ! str_contains($dsn, 'mysql')) |
54 | { |
55 | $dsn = 'mysql:' . $dsn; |
56 | } |
57 | |
58 | parent::__construct($dsn, $username, $password, $options); |
59 | } |
60 | |
61 | /** |
62 | * Generate the returning clause for the current database |
63 | */ |
64 | public function returning(string $query, string $select): string |
65 | { |
66 | // @TODO add checks for MariaDB for future-proofing |
67 | // MariaDB 10.5.0+ supports the returning clause for insert |
68 | if ( |
69 | stripos($query, 'insert') !== FALSE |
70 | && version_compare($this->getVersion(), '10.5.0', '>=') |
71 | ) { |
72 | return parent::returning($query, $select); |
73 | } |
74 | |
75 | // MariaDB 10.0.5+ supports the returning clause for delete |
76 | if ( |
77 | stripos($query, 'delete') !== FALSE |
78 | && version_compare($this->getVersion(), '10.0.5', '>=') |
79 | ) { |
80 | return parent::returning($query, $select); |
81 | } |
82 | |
83 | // Just return the same SQL if the returning clause is not supported |
84 | return $query; |
85 | } |
86 | } |