Skip to content

Commit f7a4202

Browse files
committed
MDI-2027: add pdo support from 7.3
1 parent 9f4c1f9 commit f7a4202

File tree

7 files changed

+333
-110
lines changed

7 files changed

+333
-110
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
vendor/
22
composer.lock
33
.phpunit.result.cache
4-
4+
instances/example/config/test.sqlite3

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"weotch/phpthumb": "^1.0.5"
4141
},
4242
"require-dev": {
43+
"ext-pdo_sqlite": "*",
4344
"adlawson/vfs": "^0.12.1",
4445
"mikey179/vfsstream": "^1.6.10",
4546
"phpcompatibility/php-compatibility": "^9.3",

instances/example/config/domains.config.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,8 @@
2020
'static_host' => 'http://static.sifo.local',
2121
'media_host' => 'http://static.sifo.local', // Alternative static content (media). Comment to disable.
2222
'database' => array(
23-
'db_driver' => 'mysql', // To use transactions you must use mysqli driver.
24-
'db_host' => '127.0.0.1',
25-
'db_user' => 'root',
26-
'db_password' => 'root',
27-
'db_name' => 'yourdatabase',
28-
'db_init_commands' => array( 'SET NAMES utf8' ) // Commands launched before the queries.
23+
'db_driver' => 'sqlite', // To use transactions you must use mysqli driver.
24+
'db_dsn' => sprintf('sqlite:/%s/test.sqlite3', __DIR__),
2925
),
3026
'php_ini_sets' => array( // Empty array if you don't want any php.ini overriden.
3127
'log_errors' => 'On',

src/Sifo/Debug/Mysql.php

Lines changed: 73 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,10 @@
2121
namespace Sifo;
2222
use PDO,PDOStatement;
2323

24-
include_once ROOT_PATH . '/vendor/sifophp/sifo/src/Sifo/Mysql.php';
25-
2624
/**
2725
* DbDebugStatement class that is extended for debugging purposes.
2826
*/
29-
class DebugMysqlStatement extends MysqlStatement
27+
abstract class PDOBadAbstractionDebugMysqlStatement extends MysqlStatement
3028
{
3129
/**
3230
* Binded parameters ( not binded values ).
@@ -83,8 +81,11 @@ public function execute($parameters = null)
8381
$query_string = $this->_replacePreparedParameters( $query_string, $parameters );
8482

8583
preg_match('/\/\* (.*?) \*\/\n(.*)/s', $query_string, $matches);
86-
$context = $matches[1];
87-
$query_string = $matches[2];
84+
$context = 'unknow';
85+
if (false === empty($matches)) {
86+
$context = $matches[1];
87+
$query_string = $matches[2];
88+
}
8889

8990
DebugMysql::setDebug( $query_string, $query_time, $context, $this, $this->db_params );
9091

@@ -152,32 +153,64 @@ public function fetch( $fetch_style = PDO::FETCH_ASSOC, $cursor_orientation = PD
152153

153154
return parent::fetch( $fetch_style, $cursor_orientation, $cursor_offset );
154155
}
156+
}
155157

156-
/**
157-
* Returns an array containing all of the result set rows.
158-
*
159-
* @param integer $fetch_style Controls the contents of the returned array as documented in PDOStatement::fetch().
160-
* @param mixed $fetch_argument This argument have a different meaning depending on the value of the fetch_style parameter.
161-
* @param array $ctor_args Arguments of custom class constructor when the fetch_style parameter is PDO::FETCH_CLASS.
162-
* @return array
163-
*/
164-
public function fetchAll( int $fetch_style = PDO::FETCH_ASSOC, $fetch_argument = null, mixed ...$ctor_args ): array
165-
{
166-
if ($this->result !== null)
158+
if (version_compare(PHP_VERSION, '8.0.0') >= 0) {
159+
class DebugMysqlStatement extends PDOBadAbstractionDebugMysqlStatement
160+
{
161+
/**
162+
* Returns an array containing all of the result set rows.
163+
*
164+
* @param integer $fetch_style Controls the contents of the returned array as documented in PDOStatement::fetch().
165+
* @param mixed $fetch_argument This argument have a different meaning depending on the value of the fetch_style parameter.
166+
* @param array $ctor_args Arguments of custom class constructor when the fetch_style parameter is PDO::FETCH_CLASS.
167+
* @return array
168+
*/
169+
public function fetchAll(int $fetch_style = PDO::FETCH_ASSOC, $fetch_argument = null, mixed ...$ctor_args): array
167170
{
168-
$results = $this->result;
169-
$this->result = null;
171+
if ($this->result !== null) {
172+
$results = $this->result;
173+
$this->result = null;
174+
175+
return $results;
176+
}
177+
178+
if ($fetch_argument === null) {
179+
return $this->result = parent::fetchAll($fetch_style);
180+
}
170181

171-
return $results;
182+
return $this->result = parent::fetchAll($fetch_style, $fetch_argument, $ctor_args);
172183
}
184+
}
185+
}
173186

174-
if ( $fetch_argument === null )
175-
{
176-
return $this->result = parent::fetchAll( $fetch_style );
177-
}
187+
if (version_compare(PHP_VERSION, '8.0.0') < 0) {
188+
class DebugMysqlStatement extends PDOBadAbstractionDebugMysqlStatement
189+
{
190+
/**
191+
* Returns an array containing all of the result set rows.
192+
*
193+
* @param integer $fetch_style Controls the contents of the returned array as documented in PDOStatement::fetch().
194+
* @param mixed $fetch_argument This argument have a different meaning depending on the value of the fetch_style parameter.
195+
* @param array $ctor_args Arguments of custom class constructor when the fetch_style parameter is PDO::FETCH_CLASS.
196+
* @return array
197+
*/
198+
public function fetchAll($fetch_style = PDO::FETCH_ASSOC, $fetch_argument = null, $ctor_args = []): array
199+
{
200+
if ($this->result !== null) {
201+
$results = $this->result;
202+
$this->result = null;
178203

179-
return $this->result = parent::fetchAll( $fetch_style, $fetch_argument, $ctor_args );
180-
}
204+
return $results;
205+
}
206+
207+
if ($fetch_argument === null) {
208+
return $this->result = parent::fetchAll($fetch_style);
209+
}
210+
211+
return $this->result = parent::fetchAll($fetch_style, $fetch_argument, $ctor_args);
212+
}
213+
}
181214
}
182215

183216
/**
@@ -302,20 +335,30 @@ public static function setDebug( $statement, $query_time, $context, $resultset,
302335
}
303336

304337
$sql = '/* ' . $context . ' */' . PHP_EOL . $statement;
305-
$debug_query = array(
338+
if (isset($db_params['db_dsn'])) {
339+
$database = [
340+
'dsn' => $db_params['db_dsn']
341+
];
342+
343+
} else {
344+
$database = [
345+
'host' => $db_params['db_host'],
346+
'database' => $db_params['db_name'],
347+
'user' => $db_params['db_user'],
348+
];
349+
}
350+
351+
$debug_query = array_merge([
306352
"tag" => $context,
307353
"sql" => $sql,
308354
"type" => ( ( 0 === stripos( $statement, 'SELECT' ) ) ? 'read' : 'write' ),
309-
"host" => $db_params['db_host'],
310-
"database" => $db_params['db_name'],
311-
"user" => $db_params['db_user'],
312355
// phpcs:ignore
313356
"trace" => DebugMysql::generateTrace( debug_backtrace( false ) ),
314357
// Show a table with the method name and number (functions: Affected_Rows, Last_InsertID
315358
"resultset" => $resultset_array,
316359
"time" => $query_time,
317360
"error" => ( isset( $error[2] ) !== false ) ? $error[2] : false
318-
);
361+
], $database);
319362

320363
$debug_query['rows_num'] = $rows_num;
321364

src/Sifo/Mysql.php

Lines changed: 115 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -21,88 +21,123 @@
2121
namespace Sifo;
2222

2323
use PDO,PDOStatement;
24-
/**
25-
* DbStatement class that is extended to customize some PDO functionality.
26-
*/
27-
class MysqlStatement extends PDOStatement
24+
25+
abstract class PDOBadAbstractionStatement extends PDOStatement
2826
{
29-
/**
30-
* The pdo object instance.
31-
*
32-
* @var PDO Object.
33-
*/
34-
public $dbh;
27+
/**
28+
* The pdo object instance.
29+
*
30+
* @var PDO Object.
31+
*/
32+
public $dbh;
3533

36-
/**
37-
* The domains.config params related to the database.
38-
*
39-
* @var array
40-
*/
41-
protected $db_params;
34+
/**
35+
* The domains.config params related to the database.
36+
*
37+
* @var array
38+
*/
39+
protected $db_params;
4240

43-
/**
44-
* Construction method. Sets the pdo object and the db parameters.
45-
*
46-
* @param PDO $dbh The pdo instance executing the statement.
47-
* @param string $profile The profile being used for this statement.
48-
*/
41+
/**
42+
* Construction method. Sets the pdo object and the db parameters.
43+
*
44+
* @param PDO $dbh The pdo instance executing the statement.
45+
* @param string $profile The profile being used for this statement.
46+
*/
4947
protected function __construct( $dbh, $profile )
50-
{
48+
{
5149
$this->dbh = $dbh;
52-
$params = Domains::getInstance()->getDatabaseParams();
50+
$params = Domains::getInstance()->getDatabaseParams();
5351

54-
if (!array_key_exists($profile, $params))
55-
{
56-
$params[$profile] = $params;
57-
}
52+
if (!array_key_exists($profile, $params))
53+
{
54+
$params[$profile] = $params;
55+
}
5856

59-
$this->db_params = $params[$profile];
57+
$this->db_params = $params[$profile];
6058
}
6159

62-
/**
63-
* Executes the current statement.
64-
*
65-
* @param array $parameters The array of parameters to be replaced in the statement.
66-
*
60+
/**
61+
* Executes the current statement.
62+
*
63+
* @param array $parameters The array of parameters to be replaced in the statement.
64+
*
6765
* @return bool True if everything went OK, false otherwise.
68-
*/
69-
public function execute($parameters = null)
70-
{
66+
*/
67+
public function execute($parameters = null)
68+
{
7169
return parent::execute($parameters );
72-
}
70+
}
7371

74-
/**
75-
* Fetches the resultset. Extended to make PDO::FETCH_ASSOC as default $fetch_style.
76-
*
77-
* @param integer $fetch_style Controls how the next row will be returned to the caller. This value must be one of the PDO::FETCH_* constants, defaulting to PDO::FETCH_ASSOC.
78-
* @param integer $cursor_orientation For a PDOStatement object representing a scrollable cursor, this value determines which row will be returned to the caller. This value must be one of the PDO::FETCH_ORI_* constants, defaulting to PDO::FETCH_ORI_NEXT. To request a scrollable cursor for your PDOStatement object, you must set the PDO::ATTR_CURSOR attribute to PDO::CURSOR_SCROLL when you prepare the SQL statement with PDO::prepare().
79-
* @param integer $cursor_offset For a PDOStatement object representing a scrollable cursor for which the cursor_orientation parameter is set to PDO::FETCH_ORI_ABS, this value specifies the absolute number of the row in the result set that shall be fetched.
80-
* @return mixed
81-
*/
82-
public function fetch( $fetch_style = PDO::FETCH_ASSOC, $cursor_orientation = PDO::FETCH_ORI_NEXT, $cursor_offset = 0 )
83-
{
84-
return parent::fetch( $fetch_style, $cursor_orientation, $cursor_offset );
85-
}
72+
/**
73+
* Fetches the resultset. Extended to make PDO::FETCH_ASSOC as default $fetch_style.
74+
*
75+
* @param integer $fetch_style Controls how the next row will be returned to the caller. This value must be one of the PDO::FETCH_* constants, defaulting to PDO::FETCH_ASSOC.
76+
* @param integer $cursor_orientation For a PDOStatement object representing a scrollable cursor, this value determines which row will be returned to the caller. This value must be one of the PDO::FETCH_ORI_* constants, defaulting to PDO::FETCH_ORI_NEXT. To request a scrollable cursor for your PDOStatement object, you must set the PDO::ATTR_CURSOR attribute to PDO::CURSOR_SCROLL when you prepare the SQL statement with PDO::prepare().
77+
* @param integer $cursor_offset For a PDOStatement object representing a scrollable cursor for which the cursor_orientation parameter is set to PDO::FETCH_ORI_ABS, this value specifies the absolute number of the row in the result set that shall be fetched.
78+
* @return mixed
79+
*/
80+
public function fetch( $fetch_style = PDO::FETCH_ASSOC, $cursor_orientation = PDO::FETCH_ORI_NEXT, $cursor_offset = 0 )
81+
{
82+
return parent::fetch( $fetch_style, $cursor_orientation, $cursor_offset );
83+
}
8684

87-
/**
88-
* Returns an array containing all of the result set rows.
89-
*
90-
* @param integer $fetch_style Controls the contents of the returned array as documented in PDOStatement::fetch().
91-
* @param mixed $fetch_argument This argument have a different meaning depending on the value of the fetch_style parameter.
92-
* @param array $ctor_args Arguments of custom class constructor when the fetch_style parameter is PDO::FETCH_CLASS.
93-
* @return array
94-
*/
95-
public function fetchAll( int $fetch_style = PDO::FETCH_ASSOC, $fetch_argument = null, mixed ...$ctor_args ): array
96-
{
97-
if ( $fetch_argument === null )
98-
{
99-
return parent::fetchAll( $fetch_style );
100-
}
85+
}
10186

102-
return parent::fetchAll( $fetch_style, $fetch_argument, $ctor_args );
103-
}
87+
if (version_compare(PHP_VERSION, '8.0.0') >= 0) {
88+
/**
89+
* DbStatement class that is extended to customize some PDO functionality.
90+
*/
91+
class MysqlStatement extends PDOBadAbstractionStatement
92+
{
93+
/**
94+
* Returns an array containing all of the result set rows.
95+
*
96+
* @param integer $fetch_style Controls the contents of the returned array as documented in PDOStatement::fetch().
97+
* @param mixed $fetch_argument This argument have a different meaning depending on the value of the fetch_style parameter.
98+
* @param array $ctor_args Arguments of custom class constructor when the fetch_style parameter is PDO::FETCH_CLASS.
99+
* @return array
100+
*/
101+
public function fetchAll( int $fetch_style = PDO::FETCH_ASSOC, $fetch_argument = null, mixed ...$ctor_args ): array
102+
{
103+
if ( $fetch_argument === null )
104+
{
105+
return parent::fetchAll( $fetch_style );
106+
}
107+
108+
return parent::fetchAll( $fetch_style, $fetch_argument, $ctor_args );
109+
}
110+
}
111+
112+
}
113+
114+
if (version_compare(PHP_VERSION, '8.0.0') < 0) {
115+
/**
116+
* DbStatement class that is extended to customize some PDO functionality.
117+
*/
118+
class MysqlStatement extends PDOBadAbstractionStatement
119+
{
120+
/**
121+
* Returns an array containing all of the result set rows.
122+
*
123+
* @param integer $fetch_style Controls the contents of the returned array as documented in PDOStatement::fetch().
124+
* @param mixed $fetch_argument This argument have a different meaning depending on the value of the fetch_style parameter.
125+
* @param array $ctor_args Arguments of custom class constructor when the fetch_style parameter is PDO::FETCH_CLASS.
126+
* @return array
127+
*/
128+
public function fetchAll( $fetch_style = PDO::FETCH_ASSOC, $fetch_argument = null, $ctor_args = [] ): array
129+
{
130+
if ( $fetch_argument === null )
131+
{
132+
return parent::fetchAll( $fetch_style );
133+
}
134+
135+
return parent::fetchAll( $fetch_style, $fetch_argument, $ctor_args );
136+
}
137+
}
104138
}
105139

140+
106141
/**
107142
* Database class. Uses PDO.
108143
*/
@@ -144,17 +179,24 @@ class Mysql
144179
public function __construct( $profile )
145180
{
146181
$this->db_params = Domains::getInstance()->getDatabaseParams();
147-
$init_commands = array();
182+
$init_commands = [];
148183

149-
if ( !empty( $this->db_params['db_init_commands'] ) )
150-
{
151-
$init_commands = array( PDO::MYSQL_ATTR_INIT_COMMAND => implode( ';', $this->db_params['db_init_commands'] ) );
184+
if (!empty($this->db_params['db_init_commands'])) {
185+
$init_commands = [
186+
PDO::MYSQL_ATTR_INIT_COMMAND => implode( ';', $this->db_params['db_init_commands'])
187+
];
152188
}
153189

190+
if ($this->db_params['db_driver'] === 'sqlite') {
191+
$dsn = $this->db_params['db_dsn'];
192+
} else {
193+
$dsn = "mysql:host={$this->db_params['db_host']};dbname={$this->db_params['db_name']}";
194+
}
195+
154196
$this->pdo = new PDO(
155-
"mysql:host={$this->db_params['db_host']};dbname={$this->db_params['db_name']}",
156-
$this->db_params['db_user'],
157-
$this->db_params['db_password'],
197+
$dsn,
198+
$this->db_params['db_user'] ?? null,
199+
$this->db_params['db_password'] ?? null,
158200
$init_commands
159201

160202
);

0 commit comments

Comments
 (0)