|
21 | 21 | namespace Sifo;
|
22 | 22 |
|
23 | 23 | 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 |
28 | 26 | {
|
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; |
35 | 33 |
|
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; |
42 | 40 |
|
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 | + */ |
49 | 47 | protected function __construct( $dbh, $profile )
|
50 |
| - { |
| 48 | + { |
51 | 49 | $this->dbh = $dbh;
|
52 |
| - $params = Domains::getInstance()->getDatabaseParams(); |
| 50 | + $params = Domains::getInstance()->getDatabaseParams(); |
53 | 51 |
|
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 | + } |
58 | 56 |
|
59 |
| - $this->db_params = $params[$profile]; |
| 57 | + $this->db_params = $params[$profile]; |
60 | 58 | }
|
61 | 59 |
|
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 | + * |
67 | 65 | * @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 | + { |
71 | 69 | return parent::execute($parameters );
|
72 |
| - } |
| 70 | + } |
73 | 71 |
|
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 | + } |
86 | 84 |
|
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 | +} |
101 | 86 |
|
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 | + } |
104 | 138 | }
|
105 | 139 |
|
| 140 | + |
106 | 141 | /**
|
107 | 142 | * Database class. Uses PDO.
|
108 | 143 | */
|
@@ -144,17 +179,24 @@ class Mysql
|
144 | 179 | public function __construct( $profile )
|
145 | 180 | {
|
146 | 181 | $this->db_params = Domains::getInstance()->getDatabaseParams();
|
147 |
| - $init_commands = array(); |
| 182 | + $init_commands = []; |
148 | 183 |
|
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 | + ]; |
152 | 188 | }
|
153 | 189 |
|
| 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 | + |
154 | 196 | $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, |
158 | 200 | $init_commands
|
159 | 201 |
|
160 | 202 | );
|
|
0 commit comments