From e6f9904ee5d1149f6bf6fba43dd859a582d9635c Mon Sep 17 00:00:00 2001 From: Hari K T Date: Sat, 21 Dec 2024 19:23:43 +0530 Subject: [PATCH 1/4] PDO according to 8.4 changes and feedbacks from @pmjones https://github.com/auraphp/Aura.Sql/pull/231#issuecomment-2554773240 --- .gitignore | 1 + docs/getting-started.md | 4 +- docs/upgrade.md | 11 ++- src/AbstractExtendedPdo.php | 127 ++++++++++++++++++----------------- src/DecoratedPdo.php | 11 ++- src/ExtendedPdo.php | 32 ++++----- src/ExtendedPdoInterface.php | 33 ++++++--- src/PdoInterface.php | 57 ++++++++++++---- 8 files changed, 166 insertions(+), 110 deletions(-) diff --git a/.gitignore b/.gitignore index 5708e161..da57c2a8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ composer.lock vendor/* +.phpunit.result.cache diff --git a/docs/getting-started.md b/docs/getting-started.md index 6af71c26..0e8abe97 100644 --- a/docs/getting-started.md +++ b/docs/getting-started.md @@ -42,7 +42,7 @@ Whereas the native _PDO_ connects on instantiation, _ExtendedPdo_ does not connect immediately. Instead, it connects only when you call a method that actually needs the connection to the database; e.g., on `query()`. -If you want to force a connection, call the `establishConnection()` method. +If you want to force a connection, call the `lazyConnect()` method. ```php // does not connect to the database @@ -56,7 +56,7 @@ $pdo = new ExtendedPdo( $pdo->exec('SELECT * FROM test'); // explicitly forces a connection -$pdo->establishConnection(); +$pdo->lazyConnect(); ``` If you want to explicitly force a disconnect, call the `disconnect()` method. diff --git a/docs/upgrade.md b/docs/upgrade.md index f0d423dc..18ce2bfa 100644 --- a/docs/upgrade.md +++ b/docs/upgrade.md @@ -4,8 +4,8 @@ Most changes are to provide better compatability with PHP 8.4 and above. With PHP 8.4 introducing `Pdo::connect()` as a way of creating driver specific connections. -We have introducing our `ExtendedPdo::connect()` which uses the underlining PDO features then with all our -normal added features. +`ExtendedPdo::connect()` will be still using only the PDO and not subclass feature. +If you want to use the subclass feature use the `DecoratedPdo` passing the pdo instance directly. ```php // does not connect to the database @@ -19,7 +19,7 @@ $pdo = ExtendedPdo::connect( $pdo->exec('SELECT * FROM test'); // explicitly forces a connection -$pdo->establishConnection(); +$pdo->lazyConnect(); ``` # 5.x Upgrade Notes @@ -46,7 +46,7 @@ $pdo->exec('SELECT * FROM test'); // explicitly forces a connection $pdo->connect(); ``` -... and now needs to be changed to `ExtendedPdo::establishConnection()` +... and now needs to be changed to `ExtendedPdo::lazyConnect()` ```php // does not connect to the database @@ -60,7 +60,7 @@ $pdo = new ExtendedPdo( $pdo->exec('SELECT * FROM test'); // explicitly forces a connection -$pdo->establishConnection(); +$pdo->lazyConnect(); ``` # 3.x Upgrade Notes @@ -298,4 +298,3 @@ underlying PDO instance to make those methods available, if they exist. - When dumping an ExtendedPdo object, the username and password are omitted. This should help keep unexpected output of stack traces from revealing credentials. - diff --git a/src/AbstractExtendedPdo.php b/src/AbstractExtendedPdo.php index 3e237fe3..3069dad2 100644 --- a/src/AbstractExtendedPdo.php +++ b/src/AbstractExtendedPdo.php @@ -105,9 +105,9 @@ abstract class AbstractExtendedPdo extends PDO implements ExtendedPdoInterface */ public function __call(string $name, array $arguments) { - $this->establishConnection(); + $this->lazyConnect(); - if (! method_exists($this->pdo, $name)) { + if (!method_exists($this->pdo, $name)) { $class = get_class($this); $message = "Class '$class' does not have a method '$name'"; throw new BadMethodCallException($message); @@ -127,7 +127,7 @@ public function __call(string $name, array $arguments) */ public function beginTransaction(): bool { - $this->establishConnection(); + $this->lazyConnect(); $this->profiler->start(__FUNCTION__); $result = $this->pdo->beginTransaction(); $this->profiler->finish(); @@ -145,7 +145,7 @@ public function beginTransaction(): bool */ public function commit(): bool { - $this->establishConnection(); + $this->lazyConnect(); $this->profiler->start(__FUNCTION__); $result = $this->pdo->commit(); $this->profiler->finish(); @@ -158,7 +158,7 @@ public function commit(): bool * * @return void */ - abstract public function establishConnection(): void; + abstract public function lazyConnect(): void; /** * @@ -177,7 +177,7 @@ abstract public function disconnect(): void; */ public function errorCode(): ?string { - $this->establishConnection(); + $this->lazyConnect(); return $this->pdo->errorCode(); } @@ -190,7 +190,7 @@ public function errorCode(): ?string */ public function errorInfo(): array { - $this->establishConnection(); + $this->lazyConnect(); return $this->pdo->errorInfo(); } @@ -207,7 +207,7 @@ public function errorInfo(): array */ public function exec(string $statement): int|false { - $this->establishConnection(); + $this->lazyConnect(); $this->profiler->start(__FUNCTION__); $affectedRows = $this->pdo->exec($statement); $this->profiler->finish($statement); @@ -267,7 +267,7 @@ public function fetchAll(string $statement, array $values = []): array */ public function fetchAssoc(string $statement, array $values = []): array { - $sth = $this->perform($statement, $values); + $sth = $this->perform($statement, $values); $data = []; while ($row = $sth->fetch(self::FETCH_ASSOC)) { $data[current($row)] = $row; @@ -342,12 +342,12 @@ public function fetchGroup( public function fetchObject( string $statement, array $values = [], - string $class = 'stdClass', + string $class = "stdClass", array $args = [] ): object|false { $sth = $this->perform($statement, $values); - if (! empty($args)) { + if (!empty($args)) { return $sth->fetchObject($class, $args); } @@ -382,12 +382,12 @@ public function fetchObject( public function fetchObjects( string $statement, array $values = [], - string $class = 'stdClass', + string $class = "stdClass", array $args = [] ): array { $sth = $this->perform($statement, $values); - if (! empty($args)) { + if (!empty($args)) { return $sth->fetchAll(self::FETCH_CLASS, $class, $args); } @@ -493,7 +493,7 @@ public function getProfiler(): ProfilerInterface */ public function inTransaction(): bool { - $this->establishConnection(); + $this->lazyConnect(); $this->profiler->start(__FUNCTION__); $result = $this->pdo->inTransaction(); $this->profiler->finish(); @@ -525,7 +525,7 @@ public function isConnected(): bool */ public function lastInsertId(?string $name = null): string|false { - $this->establishConnection(); + $this->lazyConnect(); $this->profiler->start(__FUNCTION__); $result = $this->pdo->lastInsertId($name); $this->profiler->finish(); @@ -550,7 +550,7 @@ public function lastInsertId(?string $name = null): string|false */ public function perform(string $statement, array $values = []): PDOStatement { - $this->establishConnection(); + $this->lazyConnect(); $sth = $this->prepareWithValues($statement, $values); $this->profiler->start(__FUNCTION__); $sth->execute(); @@ -572,9 +572,11 @@ public function perform(string $statement, array $values = []): PDOStatement * @see http://php.net/manual/en/pdo.prepare.php * */ - public function prepare(string $query, array $options = []): PDOStatement|false - { - $this->establishConnection(); + public function prepare( + string $query, + array $options = [] + ): PDOStatement|false { + $this->lazyConnect(); $sth = $this->pdo->prepare($query, $options); return $sth; } @@ -602,15 +604,17 @@ public function prepare(string $query, array $options = []): PDOStatement|false * @see http://php.net/manual/en/pdo.prepare.php * */ - public function prepareWithValues(string $statement, array $values = []): PDOStatement - { + public function prepareWithValues( + string $statement, + array $values = [] + ): PDOStatement { // if there are no values to bind ... if (empty($values)) { // ... use the normal preparation return $this->prepare($statement); } - $this->establishConnection(); + $this->lazyConnect(); // rebuild the statement and values $parser = clone $this->parser; @@ -643,9 +647,12 @@ public function prepareWithValues(string $statement, array $values = []): PDOSta * @see http://php.net/manual/en/pdo.query.php * */ - public function query(string $query, ?int $fetchMode = null, mixed ...$fetch_mode_args): PDOStatement|false - { - $this->establishConnection(); + public function query( + string $query, + ?int $fetchMode = null, + mixed ...$fetch_mode_args + ): PDOStatement|false { + $this->lazyConnect(); $this->profiler->start(__FUNCTION__); $sth = $this->pdo->query($query, $fetchMode, ...$fetch_mode_args); $this->profiler->finish($sth->queryString); @@ -668,14 +675,16 @@ public function query(string $query, ?int $fetchMode = null, mixed ...$fetch_mod * @see http://php.net/manual/en/pdo.quote.php * */ - public function quote(string|int|array|float|null $value, int $type = self::PARAM_STR): string|false - { - $this->establishConnection(); + public function quote( + string|int|array|float|null $value, + int $type = self::PARAM_STR + ): string|false { + $this->lazyConnect(); - $value = $value ?? ''; + $value = $value ?? ""; // non-array quoting - if (! is_array($value)) { + if (!is_array($value)) { return $this->pdo->quote($value, $type); } @@ -683,7 +692,7 @@ public function quote(string|int|array|float|null $value, int $type = self::PARA foreach ($value as $k => $v) { $value[$k] = $this->pdo->quote($v, $type); } - return implode(', ', $value); + return implode(", ", $value); } /** @@ -697,16 +706,13 @@ public function quote(string|int|array|float|null $value, int $type = self::PARA */ public function quoteName(string $name): string { - if (!str_contains($name, '.')) { + if (!str_contains($name, ".")) { return $this->quoteSingleName($name); } return implode( - '.', - array_map( - [$this, 'quoteSingleName'], - explode('.', $name) - ) + ".", + array_map([$this, "quoteSingleName"], explode(".", $name)) ); } @@ -726,9 +732,7 @@ public function quoteSingleName(string $name): string $this->quoteNameEscapeRepl, $name ); - return $this->quoteNamePrefix - . $name - . $this->quoteNameSuffix; + return $this->quoteNamePrefix . $name . $this->quoteNameSuffix; } /** @@ -742,7 +746,7 @@ public function quoteSingleName(string $name): string */ public function rollBack(): bool { - $this->establishConnection(); + $this->lazyConnect(); $this->profiler->start(__FUNCTION__); $result = $this->pdo->rollBack(); $this->profiler->finish(); @@ -856,7 +860,7 @@ public function yieldCol(string $statement, array $values = []): Generator public function yieldObjects( string $statement, array $values = [], - string $class = 'stdClass', + string $class = "stdClass", array $args = [] ): Generator { $sth = $this->perform($statement, $values); @@ -908,8 +912,11 @@ public function yieldPairs(string $statement, array $values = []): Generator * bindable (e.g., array, object, or resource). * */ - protected function bindValue(PDOStatement $sth, mixed $key, mixed $val): bool - { + protected function bindValue( + PDOStatement $sth, + mixed $key, + mixed $val + ): bool { if (is_int($val)) { return $sth->bindValue($key, $val, self::PARAM_INT); } @@ -922,7 +929,7 @@ protected function bindValue(PDOStatement $sth, mixed $key, mixed $val): bool return $sth->bindValue($key, $val, self::PARAM_NULL); } - if (! is_scalar($val)) { + if (!is_scalar($val)) { $type = gettype($val); throw new Exception\CannotBindValue( "Cannot bind value of type '{$type}' to placeholder '{$key}'" @@ -943,9 +950,9 @@ protected function bindValue(PDOStatement $sth, mixed $key, mixed $val): bool */ protected function newParser(string $driver): ParserInterface { - $class = 'Aura\Sql\Parser\\' . ucfirst($driver) . 'Parser'; - if (! class_exists($class)) { - $class = 'Aura\Sql\Parser\SqliteParser'; + $class = "Aura\Sql\Parser\\" . ucfirst($driver) . "Parser"; + if (!class_exists($class)) { + $class = "Aura\Sql\Parser\SqliteParser"; } return new $class(); } @@ -962,17 +969,17 @@ protected function newParser(string $driver): ParserInterface protected function setQuoteName(string $driver): void { switch ($driver) { - case 'mysql': - $this->quoteNamePrefix = '`'; - $this->quoteNameSuffix = '`'; - $this->quoteNameEscapeFind = '`'; - $this->quoteNameEscapeRepl = '``'; + case "mysql": + $this->quoteNamePrefix = "`"; + $this->quoteNameSuffix = "`"; + $this->quoteNameEscapeFind = "`"; + $this->quoteNameEscapeRepl = "``"; return; - case 'sqlsrv': - $this->quoteNamePrefix = '['; - $this->quoteNameSuffix = ']'; - $this->quoteNameEscapeFind = ']'; - $this->quoteNameEscapeRepl = ']['; + case "sqlsrv": + $this->quoteNamePrefix = "["; + $this->quoteNameSuffix = "]"; + $this->quoteNameEscapeFind = "]"; + $this->quoteNameEscapeRepl = "]["; return; default: $this->quoteNamePrefix = '"'; @@ -992,7 +999,7 @@ protected function setQuoteName(string $driver): void */ public function getAttribute(int $attribute): bool|int|string|array|null { - $this->establishConnection(); + $this->lazyConnect(); return $this->pdo->getAttribute($attribute); } @@ -1006,7 +1013,7 @@ public function getAttribute(int $attribute): bool|int|string|array|null */ public function setAttribute(int $attribute, mixed $value): bool { - $this->establishConnection(); + $this->lazyConnect(); return $this->pdo->setAttribute($attribute, $value); } } diff --git a/src/DecoratedPdo.php b/src/DecoratedPdo.php index 0eb683fa..901669e6 100644 --- a/src/DecoratedPdo.php +++ b/src/DecoratedPdo.php @@ -44,6 +44,15 @@ public function __construct(PDO $pdo, ?ProfilerInterface $profiler = null) $this->setQuoteName($driver); } + public static function connect( + string $dsn, + ?string $username = null, + ?string $password = null, + ?array $options = [] + ): static { + return new static(\PDO::connect($dsn, $username, $password, $options)); + } + /** * * Connects to the database. @@ -51,7 +60,7 @@ public function __construct(PDO $pdo, ?ProfilerInterface $profiler = null) * @return void * */ - public function establishConnection(): void + public function lazyConnect(): void { // already connected } diff --git a/src/ExtendedPdo.php b/src/ExtendedPdo.php index bb6ff658..60d69ff3 100644 --- a/src/ExtendedPdo.php +++ b/src/ExtendedPdo.php @@ -60,24 +60,18 @@ public function __construct( ?ProfilerInterface $profiler = null ) { // if no error mode is specified, use exceptions - if (! isset($options[PDO::ATTR_ERRMODE])) { + if (!isset($options[PDO::ATTR_ERRMODE])) { $options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION; } // retain the arguments for later - $this->args = [ - $dsn, - $username, - $password, - $options, - $queries - ]; + $this->args = [$dsn, $username, $password, $options, $queries]; // retain a profiler, instantiating a default one if needed $this->setProfiler($profiler ?? new Profiler()); // retain a query parser - $parts = explode(':', $dsn); + $parts = explode(":", $dsn); $parser = $this->newParser($parts[0]); $this->setParser($parser); @@ -89,11 +83,9 @@ public static function connect( string $dsn, ?string $username = null, ?string $password = null, - ?array $options = [], - array $queries = [], - ?ProfilerInterface $profiler = null + ?array $options = [] ): static { - return new static($dsn, $username, $password, $options ?? [], $queries, $profiler); + return new static($dsn, $username, $password, $options); } /** @@ -102,7 +94,7 @@ public static function connect( * * @return void */ - public function establishConnection(): void + public function lazyConnect(): void { if ($this->pdo) { return; @@ -111,7 +103,7 @@ public function establishConnection(): void // connect $this->profiler->start(__FUNCTION__); list($dsn, $username, $password, $options, $queries) = $this->args; - $this->pdo = PDO::connect($dsn, $username, $password, $options); + $this->pdo = new PDO($dsn, $username, $password, $options); $this->profiler->finish(); // connection-time queries @@ -144,13 +136,13 @@ public function disconnect(): void public function __debugInfo(): array { return [ - 'args' => [ + "args" => [ $this->args[0], - '****', - '****', + "****", + "****", $this->args[3], $this->args[4], - ] + ], ]; } @@ -163,7 +155,7 @@ public function __debugInfo(): array */ public function getPdo(): PDO { - $this->establishConnection(); + $this->lazyConnect(); return $this->pdo; } } diff --git a/src/ExtendedPdoInterface.php b/src/ExtendedPdoInterface.php index 4313185b..12e68c68 100644 --- a/src/ExtendedPdoInterface.php +++ b/src/ExtendedPdoInterface.php @@ -28,7 +28,7 @@ interface ExtendedPdoInterface extends PdoInterface * Connects to the database. * */ - public function establishConnection(): void; + public function lazyConnect(): void; /** * @@ -141,7 +141,7 @@ public function fetchGroup( public function fetchObject( string $statement, array $values = [], - string $class = 'stdClass', + string $class = "stdClass", array $args = [] ): object|false; @@ -172,7 +172,7 @@ public function fetchObject( public function fetchObjects( string $statement, array $values = [], - string $class = 'stdClass', + string $class = "stdClass", array $args = [] ): array; @@ -187,7 +187,10 @@ public function fetchObjects( * @return array|false * */ - public function fetchOne(string $statement, array $values = []): array|false; + public function fetchOne( + string $statement, + array $values = [] + ): array|false; /** * @@ -316,7 +319,10 @@ public function yieldAll(string $statement, array $values = []): Generator; * @return \Generator * */ - public function yieldAssoc(string $statement, array $values = []): Generator; + public function yieldAssoc( + string $statement, + array $values = [] + ): Generator; /** * @@ -356,7 +362,7 @@ public function yieldCol(string $statement, array $values = []): Generator; public function yieldObjects( string $statement, array $values = [], - string $class = 'stdClass', + string $class = "stdClass", array $args = [] ): Generator; @@ -372,7 +378,10 @@ public function yieldObjects( * @return \Generator * */ - public function yieldPairs(string $statement, array $values = []): Generator; + public function yieldPairs( + string $statement, + array $values = [] + ): Generator; /** * @@ -386,7 +395,10 @@ public function yieldPairs(string $statement, array $values = []): Generator; * @return \PDOStatement * */ - public function perform(string $statement, array $values = []): PDOStatement; + public function perform( + string $statement, + array $values = [] + ): PDOStatement; /** * @@ -410,5 +422,8 @@ public function perform(string $statement, array $values = []): PDOStatement; * @see http://php.net/manual/en/pdo.prepare.php * */ - public function prepareWithValues(string $statement, array $values = []): PDOStatement; + public function prepareWithValues( + string $statement, + array $values = [] + ): PDOStatement; } diff --git a/src/PdoInterface.php b/src/PdoInterface.php index f873b87c..d0687a7c 100644 --- a/src/PdoInterface.php +++ b/src/PdoInterface.php @@ -42,6 +42,29 @@ public function beginTransaction(): bool; */ public function commit(): bool; + /** + * + * Introduced in 6.x due to PHP 8.4 change. This is a BC break for Aura.Sql. + * + * @param string $dsn The Data Source Name, or DSN, contains the information required to connect to the database. + * + * @param string | null $username The user name for the DSN string. This parameter is optional for some PDO drivers. + * + * @param string | null $password The password for the DSN string. This parameter is optional for some PDO drivers. + * + * @param array | null $options A key=>value array of driver-specific connection options. + * + * @return \PDO Returns an instance of a generic PDO instance. + * + * @see https://www.php.net/manual/en/pdo.connect.php + */ + public static function connect( + string $dsn, + ?string $username = null, + #[\SensitiveParameter] ?string $password = null, + ?array $options = null + ): static; + /** * * Gets the most recent error code. @@ -83,6 +106,15 @@ public function exec(string $statement): int|false; */ public function getAttribute(int $attribute): bool|int|string|array|null; + /** + * + * Returns all currently available PDO drivers. + * + * @return array + * + */ + public static function getAvailableDrivers(): array; + /** * * Is a transaction currently active? @@ -121,7 +153,10 @@ public function lastInsertId(?string $name = null): string|false; * * @see http://php.net/manual/en/pdo.prepare.php */ - public function prepare(string $query, array $options = []): PDOStatement|false; + public function prepare( + string $query, + array $options = [] + ): PDOStatement|false; /** * @@ -138,7 +173,11 @@ public function prepare(string $query, array $options = []): PDOStatement|false; * @see http://php.net/manual/en/pdo.query.php * */ - public function query(string $query, ?int $fetchMode = null, ...$fetch_mode_args): PDOStatement|false; + public function query( + string $query, + ?int $fetchMode = null, + ...$fetch_mode_args + ): PDOStatement|false; /** * @@ -153,7 +192,10 @@ public function query(string $query, ?int $fetchMode = null, ...$fetch_mode_args * @see http://php.net/manual/en/pdo.quote.php * */ - public function quote(string|int|array|float|null $value, int $type = PDO::PARAM_STR): string|false; + public function quote( + string|int|array|float|null $value, + int $type = PDO::PARAM_STR + ): string|false; /** * @@ -178,13 +220,4 @@ public function rollBack(): bool; * */ public function setAttribute(int $attribute, mixed $value): bool; - - /** - * - * Returns all currently available PDO drivers. - * - * @return array - * - */ - public static function getAvailableDrivers(): array; } From 894022f5d806ac4635f85c2afeed6e78dffb19cc Mon Sep 17 00:00:00 2001 From: Hari K T Date: Sat, 21 Dec 2024 19:32:31 +0530 Subject: [PATCH 2/4] Fix the upgrade guide --- docs/upgrade.md | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/docs/upgrade.md b/docs/upgrade.md index 18ce2bfa..d0c41326 100644 --- a/docs/upgrade.md +++ b/docs/upgrade.md @@ -2,10 +2,7 @@ Most changes are to provide better compatability with PHP 8.4 and above. -With PHP 8.4 introducing `Pdo::connect()` as a way of creating driver specific connections. - -`ExtendedPdo::connect()` will be still using only the PDO and not subclass feature. -If you want to use the subclass feature use the `DecoratedPdo` passing the pdo instance directly. +BC Break : With PHP 8.4 introducing `Pdo::connect()` as a way of creating driver specific connections. ```php // does not connect to the database From c10530be051490df4bd51252aa6e4dca885351ea Mon Sep 17 00:00:00 2001 From: Hari K T Date: Sat, 21 Dec 2024 19:55:08 +0530 Subject: [PATCH 3/4] Fix the formatting caused with zed --- src/AbstractExtendedPdo.php | 91 +++++++++++++++++------------------- src/ExtendedPdo.php | 16 +++++-- src/ExtendedPdoInterface.php | 31 ++++-------- src/PdoInterface.php | 18 ++----- 4 files changed, 65 insertions(+), 91 deletions(-) diff --git a/src/AbstractExtendedPdo.php b/src/AbstractExtendedPdo.php index 3069dad2..3b73af32 100644 --- a/src/AbstractExtendedPdo.php +++ b/src/AbstractExtendedPdo.php @@ -107,7 +107,7 @@ public function __call(string $name, array $arguments) { $this->lazyConnect(); - if (!method_exists($this->pdo, $name)) { + if (! method_exists($this->pdo, $name)) { $class = get_class($this); $message = "Class '$class' does not have a method '$name'"; throw new BadMethodCallException($message); @@ -267,7 +267,7 @@ public function fetchAll(string $statement, array $values = []): array */ public function fetchAssoc(string $statement, array $values = []): array { - $sth = $this->perform($statement, $values); + $sth = $this->perform($statement, $values); $data = []; while ($row = $sth->fetch(self::FETCH_ASSOC)) { $data[current($row)] = $row; @@ -342,12 +342,12 @@ public function fetchGroup( public function fetchObject( string $statement, array $values = [], - string $class = "stdClass", + string $class = 'stdClass', array $args = [] ): object|false { $sth = $this->perform($statement, $values); - if (!empty($args)) { + if (! empty($args)) { return $sth->fetchObject($class, $args); } @@ -382,12 +382,12 @@ public function fetchObject( public function fetchObjects( string $statement, array $values = [], - string $class = "stdClass", + string $class = 'stdClass', array $args = [] ): array { $sth = $this->perform($statement, $values); - if (!empty($args)) { + if (! empty($args)) { return $sth->fetchAll(self::FETCH_CLASS, $class, $args); } @@ -572,10 +572,8 @@ public function perform(string $statement, array $values = []): PDOStatement * @see http://php.net/manual/en/pdo.prepare.php * */ - public function prepare( - string $query, - array $options = [] - ): PDOStatement|false { + public function prepare(string $query, array $options = []): PDOStatement|false + { $this->lazyConnect(); $sth = $this->pdo->prepare($query, $options); return $sth; @@ -604,10 +602,8 @@ public function prepare( * @see http://php.net/manual/en/pdo.prepare.php * */ - public function prepareWithValues( - string $statement, - array $values = [] - ): PDOStatement { + public function prepareWithValues(string $statement, array $values = []): PDOStatement + { // if there are no values to bind ... if (empty($values)) { // ... use the normal preparation @@ -647,11 +643,8 @@ public function prepareWithValues( * @see http://php.net/manual/en/pdo.query.php * */ - public function query( - string $query, - ?int $fetchMode = null, - mixed ...$fetch_mode_args - ): PDOStatement|false { + public function query(string $query, ?int $fetchMode = null, mixed ...$fetch_mode_args): PDOStatement|false + { $this->lazyConnect(); $this->profiler->start(__FUNCTION__); $sth = $this->pdo->query($query, $fetchMode, ...$fetch_mode_args); @@ -675,16 +668,14 @@ public function query( * @see http://php.net/manual/en/pdo.quote.php * */ - public function quote( - string|int|array|float|null $value, - int $type = self::PARAM_STR - ): string|false { + public function quote(string|int|array|float|null $value, int $type = self::PARAM_STR): string|false + { $this->lazyConnect(); $value = $value ?? ""; // non-array quoting - if (!is_array($value)) { + if (! is_array($value)) { return $this->pdo->quote($value, $type); } @@ -692,7 +683,7 @@ public function quote( foreach ($value as $k => $v) { $value[$k] = $this->pdo->quote($v, $type); } - return implode(", ", $value); + return implode(', ', $value); } /** @@ -706,13 +697,16 @@ public function quote( */ public function quoteName(string $name): string { - if (!str_contains($name, ".")) { + if (! str_contains($name, '.')) { return $this->quoteSingleName($name); } return implode( - ".", - array_map([$this, "quoteSingleName"], explode(".", $name)) + '.', + array_map( + [$this, 'quoteSingleName'], + explode('.', $name) + ) ); } @@ -732,7 +726,9 @@ public function quoteSingleName(string $name): string $this->quoteNameEscapeRepl, $name ); - return $this->quoteNamePrefix . $name . $this->quoteNameSuffix; + return $this->quoteNamePrefix + . $name + . $this->quoteNameSuffix; } /** @@ -860,7 +856,7 @@ public function yieldCol(string $statement, array $values = []): Generator public function yieldObjects( string $statement, array $values = [], - string $class = "stdClass", + string $class = 'stdClass', array $args = [] ): Generator { $sth = $this->perform($statement, $values); @@ -912,11 +908,8 @@ public function yieldPairs(string $statement, array $values = []): Generator * bindable (e.g., array, object, or resource). * */ - protected function bindValue( - PDOStatement $sth, - mixed $key, - mixed $val - ): bool { + protected function bindValue(PDOStatement $sth, mixed $key, mixed $val): bool + { if (is_int($val)) { return $sth->bindValue($key, $val, self::PARAM_INT); } @@ -929,7 +922,7 @@ protected function bindValue( return $sth->bindValue($key, $val, self::PARAM_NULL); } - if (!is_scalar($val)) { + if (! is_scalar($val)) { $type = gettype($val); throw new Exception\CannotBindValue( "Cannot bind value of type '{$type}' to placeholder '{$key}'" @@ -950,9 +943,9 @@ protected function bindValue( */ protected function newParser(string $driver): ParserInterface { - $class = "Aura\Sql\Parser\\" . ucfirst($driver) . "Parser"; - if (!class_exists($class)) { - $class = "Aura\Sql\Parser\SqliteParser"; + $class = 'Aura\Sql\Parser\\' . ucfirst($driver) . 'Parser'; + if (! class_exists($class)) { + $class = 'Aura\Sql\Parser\SqliteParser'; } return new $class(); } @@ -969,17 +962,17 @@ protected function newParser(string $driver): ParserInterface protected function setQuoteName(string $driver): void { switch ($driver) { - case "mysql": - $this->quoteNamePrefix = "`"; - $this->quoteNameSuffix = "`"; - $this->quoteNameEscapeFind = "`"; - $this->quoteNameEscapeRepl = "``"; + case 'mysql': + $this->quoteNamePrefix = '`'; + $this->quoteNameSuffix = '`'; + $this->quoteNameEscapeFind = '`'; + $this->quoteNameEscapeRepl = '``'; return; - case "sqlsrv": - $this->quoteNamePrefix = "["; - $this->quoteNameSuffix = "]"; - $this->quoteNameEscapeFind = "]"; - $this->quoteNameEscapeRepl = "]["; + case 'sqlsrv': + $this->quoteNamePrefix = '['; + $this->quoteNameSuffix = ']'; + $this->quoteNameEscapeFind = ']'; + $this->quoteNameEscapeRepl = ']['; return; default: $this->quoteNamePrefix = '"'; diff --git a/src/ExtendedPdo.php b/src/ExtendedPdo.php index 60d69ff3..019c2e29 100644 --- a/src/ExtendedPdo.php +++ b/src/ExtendedPdo.php @@ -60,12 +60,18 @@ public function __construct( ?ProfilerInterface $profiler = null ) { // if no error mode is specified, use exceptions - if (!isset($options[PDO::ATTR_ERRMODE])) { + if (! isset($options[PDO::ATTR_ERRMODE])) { $options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION; } // retain the arguments for later - $this->args = [$dsn, $username, $password, $options, $queries]; + $this->args = [ + $dsn, + $username, + $password, + $options, + $queries + ]; // retain a profiler, instantiating a default one if needed $this->setProfiler($profiler ?? new Profiler()); @@ -136,10 +142,10 @@ public function disconnect(): void public function __debugInfo(): array { return [ - "args" => [ + 'args' => [ $this->args[0], - "****", - "****", + '****', + '****', $this->args[3], $this->args[4], ], diff --git a/src/ExtendedPdoInterface.php b/src/ExtendedPdoInterface.php index 12e68c68..0aaa3f43 100644 --- a/src/ExtendedPdoInterface.php +++ b/src/ExtendedPdoInterface.php @@ -141,7 +141,7 @@ public function fetchGroup( public function fetchObject( string $statement, array $values = [], - string $class = "stdClass", + string $class = 'stdClass', array $args = [] ): object|false; @@ -172,7 +172,7 @@ public function fetchObject( public function fetchObjects( string $statement, array $values = [], - string $class = "stdClass", + string $class = 'stdClass', array $args = [] ): array; @@ -187,10 +187,7 @@ public function fetchObjects( * @return array|false * */ - public function fetchOne( - string $statement, - array $values = [] - ): array|false; + public function fetchOne(string $statement, array $values = []): array|false; /** * @@ -319,10 +316,7 @@ public function yieldAll(string $statement, array $values = []): Generator; * @return \Generator * */ - public function yieldAssoc( - string $statement, - array $values = [] - ): Generator; + public function yieldAssoc(string $statement, array $values = []): Generator; /** * @@ -362,7 +356,7 @@ public function yieldCol(string $statement, array $values = []): Generator; public function yieldObjects( string $statement, array $values = [], - string $class = "stdClass", + string $class = 'stdClass', array $args = [] ): Generator; @@ -378,10 +372,7 @@ public function yieldObjects( * @return \Generator * */ - public function yieldPairs( - string $statement, - array $values = [] - ): Generator; + public function yieldPairs(string $statement, array $values = []): Generator; /** * @@ -395,10 +386,7 @@ public function yieldPairs( * @return \PDOStatement * */ - public function perform( - string $statement, - array $values = [] - ): PDOStatement; + public function perform(string $statement, array $values = []): PDOStatement; /** * @@ -422,8 +410,5 @@ public function perform( * @see http://php.net/manual/en/pdo.prepare.php * */ - public function prepareWithValues( - string $statement, - array $values = [] - ): PDOStatement; + public function prepareWithValues(string $statement, array $values = []): PDOStatement; } diff --git a/src/PdoInterface.php b/src/PdoInterface.php index d0687a7c..e5370ecc 100644 --- a/src/PdoInterface.php +++ b/src/PdoInterface.php @@ -153,11 +153,8 @@ public function lastInsertId(?string $name = null): string|false; * * @see http://php.net/manual/en/pdo.prepare.php */ - public function prepare( - string $query, - array $options = [] - ): PDOStatement|false; - + public function prepare(string $query, array $options = []): PDOStatement|false; + /** * * Queries the database and returns a PDOStatement. @@ -173,11 +170,7 @@ public function prepare( * @see http://php.net/manual/en/pdo.query.php * */ - public function query( - string $query, - ?int $fetchMode = null, - ...$fetch_mode_args - ): PDOStatement|false; + public function query(string $query, ?int $fetchMode = null, ...$fetch_mode_args): PDOStatement|false; /** * @@ -192,10 +185,7 @@ public function query( * @see http://php.net/manual/en/pdo.quote.php * */ - public function quote( - string|int|array|float|null $value, - int $type = PDO::PARAM_STR - ): string|false; + public function quote(string|int|array|float|null $value, int $type = PDO::PARAM_STR): string|false; /** * From 1d76d7ce171fabfa186e5564c1a8964a1cb055ba Mon Sep 17 00:00:00 2001 From: Hari K T Date: Sat, 21 Dec 2024 19:56:52 +0530 Subject: [PATCH 4/4] Fix format, probably need to introduce formatting tool to dev --- src/PdoInterface.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PdoInterface.php b/src/PdoInterface.php index e5370ecc..7f295457 100644 --- a/src/PdoInterface.php +++ b/src/PdoInterface.php @@ -154,7 +154,7 @@ public function lastInsertId(?string $name = null): string|false; * @see http://php.net/manual/en/pdo.prepare.php */ public function prepare(string $query, array $options = []): PDOStatement|false; - + /** * * Queries the database and returns a PDOStatement.