From 32c4c8e4350070bbf068b557215eebc924e7d19f Mon Sep 17 00:00:00 2001 From: Matthew Peveler Date: Tue, 23 Feb 2021 19:25:00 -0500 Subject: [PATCH 01/14] Add optional argument to query/execute Signed-off-by: Matthew Peveler --- src/Phinx/Db/Adapter/AdapterInterface.php | 6 ++-- src/Phinx/Db/Adapter/AdapterWrapper.php | 8 ++--- src/Phinx/Db/Adapter/PdoAdapter.php | 14 +++++---- tests/Phinx/Db/Adapter/SQLiteAdapterTest.php | 31 ++++++++++++++++++++ 4 files changed, 48 insertions(+), 11 deletions(-) diff --git a/src/Phinx/Db/Adapter/AdapterInterface.php b/src/Phinx/Db/Adapter/AdapterInterface.php index fa971eea1..c84c58f4b 100644 --- a/src/Phinx/Db/Adapter/AdapterInterface.php +++ b/src/Phinx/Db/Adapter/AdapterInterface.php @@ -265,10 +265,11 @@ public function rollbackTransaction(); * Executes a SQL statement and returns the number of affected rows. * * @param string $sql SQL + * @param array $params parameters to use for prepared query * * @return int */ - public function execute($sql); + public function execute($sql, array $params = []); /** * Executes a list of migration actions for the given table @@ -293,10 +294,11 @@ public function getQueryBuilder(); * The return type depends on the underlying adapter being used. * * @param string $sql SQL + * @param array $params parameters to use for prepared query * * @return mixed */ - public function query($sql); + public function query($sql, array $params = []); /** * Executes a query and returns only one row as an array. diff --git a/src/Phinx/Db/Adapter/AdapterWrapper.php b/src/Phinx/Db/Adapter/AdapterWrapper.php index 7fae31a9c..8f479fc46 100644 --- a/src/Phinx/Db/Adapter/AdapterWrapper.php +++ b/src/Phinx/Db/Adapter/AdapterWrapper.php @@ -151,17 +151,17 @@ public function disconnect() /** * @inheritDoc */ - public function execute($sql) + public function execute($sql, array $params = []) { - return $this->getAdapter()->execute($sql); + return $this->getAdapter()->execute($sql, $params); } /** * @inheritDoc */ - public function query($sql) + public function query($sql, array $params = []) { - return $this->getAdapter()->query($sql); + return $this->getAdapter()->query($sql, $params); } /** diff --git a/src/Phinx/Db/Adapter/PdoAdapter.php b/src/Phinx/Db/Adapter/PdoAdapter.php index 0a85d19dd..89ecb0655 100644 --- a/src/Phinx/Db/Adapter/PdoAdapter.php +++ b/src/Phinx/Db/Adapter/PdoAdapter.php @@ -182,7 +182,7 @@ public function disconnect() /** * @inheritDoc */ - public function execute($sql) + public function execute($sql, array $params = []) { $sql = rtrim($sql, "; \t\n\r\0\x0B") . ';'; $this->verboseLog($sql); @@ -191,7 +191,9 @@ public function execute($sql) return 0; } - return $this->getConnection()->exec($sql); + $stmt = $this->getConnection()->prepare($sql); + $result = $stmt->execute($params); + return $result ? $stmt->rowCount() : $result; } /** @@ -215,11 +217,13 @@ public function getQueryBuilder() * * @param string $sql SQL * - * @return \PDOStatement + * @return \PDOStatement|false */ - public function query($sql) + public function query($sql, array $params = []) { - return $this->getConnection()->query($sql); + $stmt = $this->getConnection()->prepare($sql); + $result = $stmt->execute($params); + return $result ? $stmt : false; } /** diff --git a/tests/Phinx/Db/Adapter/SQLiteAdapterTest.php b/tests/Phinx/Db/Adapter/SQLiteAdapterTest.php index d5a9c293f..c81279d32 100644 --- a/tests/Phinx/Db/Adapter/SQLiteAdapterTest.php +++ b/tests/Phinx/Db/Adapter/SQLiteAdapterTest.php @@ -1161,6 +1161,37 @@ public function testQueryBuilder() $this->assertEquals(1, $stm->rowCount()); } + public function testQueryWithParams() + { + $table = new \Phinx\Db\Table('table1', [], $this->adapter); + $table->addColumn('string_col', 'string') + ->addColumn('int_col', 'integer') + ->save(); + + $this->adapter->insert($table->getTable(), [ + 'string_col' => 'test data', + 'int_col' => 10, + ]); + + $this->adapter->insert($table->getTable(), [ + 'string_col' => null, + ]); + + $this->adapter->insert($table->getTable(), [ + 'int_col' => 23, + ]); + + $countQuery = $this->adapter->query('SELECT COUNT(*) FROM table1 WHERE int_col > ?', [5]); + $res = $countQuery->fetchAll(); + $this->assertEquals(2, $res[0]['COUNT(*)']); + + $this->adapter->execute('UPDATE table1 SET int_col = ? WHERE int_col IS NULL', [12]); + + $countQuery->execute([1]); + $res = $countQuery->fetchAll(); + $this->assertEquals(3, $res[0]['COUNT(*)']); + } + /** * Tests adding more than one column to a table * that already exists due to adapters having different add column instructions From bc9f1249b9a95760bf5147139580af12db51b305 Mon Sep 17 00:00:00 2001 From: Matthew Peveler Date: Tue, 23 Feb 2021 19:27:32 -0500 Subject: [PATCH 02/14] expose to migration / seed interfaces Signed-off-by: Matthew Peveler --- src/Phinx/Migration/AbstractMigration.php | 8 ++++---- src/Phinx/Migration/MigrationInterface.php | 6 ++++-- src/Phinx/Seed/AbstractSeed.php | 8 ++++---- src/Phinx/Seed/SeedInterface.php | 6 ++++-- 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/Phinx/Migration/AbstractMigration.php b/src/Phinx/Migration/AbstractMigration.php index 68d969a45..beea2d8ab 100644 --- a/src/Phinx/Migration/AbstractMigration.php +++ b/src/Phinx/Migration/AbstractMigration.php @@ -193,17 +193,17 @@ public function isMigratingUp() /** * @inheritDoc */ - public function execute($sql) + public function execute($sql, array $params = []) { - return $this->getAdapter()->execute($sql); + return $this->getAdapter()->execute($sql, $params); } /** * @inheritDoc */ - public function query($sql) + public function query($sql, array $params = []) { - return $this->getAdapter()->query($sql); + return $this->getAdapter()->query($sql, $params); } /** diff --git a/src/Phinx/Migration/MigrationInterface.php b/src/Phinx/Migration/MigrationInterface.php index c611f123d..d3685ddca 100644 --- a/src/Phinx/Migration/MigrationInterface.php +++ b/src/Phinx/Migration/MigrationInterface.php @@ -137,10 +137,11 @@ public function isMigratingUp(); * Executes a SQL statement and returns the number of affected rows. * * @param string $sql SQL + * @param array $params parameters to use for prepared query * * @return int */ - public function execute($sql); + public function execute($sql, array $params = []); /** * Executes a SQL statement. @@ -151,10 +152,11 @@ public function execute($sql); * you can set the return type by the adapter in your current use. * * @param string $sql SQL + * @param array $params parameters to use for prepared query * * @return mixed */ - public function query($sql); + public function query($sql, array $params = []); /** * Returns a new Query object that can be used to build complex SELECT, UPDATE, INSERT or DELETE diff --git a/src/Phinx/Seed/AbstractSeed.php b/src/Phinx/Seed/AbstractSeed.php index ee259f4c0..0f2e1f5f9 100644 --- a/src/Phinx/Seed/AbstractSeed.php +++ b/src/Phinx/Seed/AbstractSeed.php @@ -128,17 +128,17 @@ public function getName() /** * @inheritDoc */ - public function execute($sql) + public function execute($sql, array $params = []) { - return $this->getAdapter()->execute($sql); + return $this->getAdapter()->execute($sql, $params); } /** * @inheritDoc */ - public function query($sql) + public function query($sql, array $params = []) { - return $this->getAdapter()->query($sql); + return $this->getAdapter()->query($sql, $params); } /** diff --git a/src/Phinx/Seed/SeedInterface.php b/src/Phinx/Seed/SeedInterface.php index 2165fdb22..c6acc8f49 100644 --- a/src/Phinx/Seed/SeedInterface.php +++ b/src/Phinx/Seed/SeedInterface.php @@ -94,10 +94,11 @@ public function getName(); * Executes a SQL statement and returns the number of affected rows. * * @param string $sql SQL + * @param array $params parameters to use for prepared query * * @return int */ - public function execute($sql); + public function execute($sql, array $params = []); /** * Executes a SQL statement. @@ -108,10 +109,11 @@ public function execute($sql); * you can set the return type by the adapter in your current use. * * @param string $sql SQL + * @param array $params parameters to use for prepared query * * @return mixed */ - public function query($sql); + public function query($sql, array $params = []); /** * Executes a query and returns only one row as an array. From 4f9df731fd195d566ccb9db8330835c281cc76f5 Mon Sep 17 00:00:00 2001 From: stickler-ci Date: Wed, 24 Feb 2021 00:28:16 +0000 Subject: [PATCH 03/14] Fixing style errors. --- src/Phinx/Db/Adapter/PdoAdapter.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Phinx/Db/Adapter/PdoAdapter.php b/src/Phinx/Db/Adapter/PdoAdapter.php index 89ecb0655..56396e1a5 100644 --- a/src/Phinx/Db/Adapter/PdoAdapter.php +++ b/src/Phinx/Db/Adapter/PdoAdapter.php @@ -193,6 +193,7 @@ public function execute($sql, array $params = []) $stmt = $this->getConnection()->prepare($sql); $result = $stmt->execute($params); + return $result ? $stmt->rowCount() : $result; } From 65d022c54fd0053f04af1c8c09a2801ddc9572d1 Mon Sep 17 00:00:00 2001 From: Matthew Peveler Date: Tue, 23 Feb 2021 19:35:09 -0500 Subject: [PATCH 04/14] fix stickler error Signed-off-by: Matthew Peveler --- src/Phinx/Db/Adapter/PdoAdapter.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Phinx/Db/Adapter/PdoAdapter.php b/src/Phinx/Db/Adapter/PdoAdapter.php index 56396e1a5..e5a78a8c3 100644 --- a/src/Phinx/Db/Adapter/PdoAdapter.php +++ b/src/Phinx/Db/Adapter/PdoAdapter.php @@ -224,6 +224,7 @@ public function query($sql, array $params = []) { $stmt = $this->getConnection()->prepare($sql); $result = $stmt->execute($params); + return $result ? $stmt : false; } From 96e5fb9e7d35ab34b61d850ebe9e74444a041b1c Mon Sep 17 00:00:00 2001 From: Matthew Peveler Date: Wed, 24 Feb 2021 00:11:35 -0500 Subject: [PATCH 05/14] Update SQLiteAdapterTest.php --- tests/Phinx/Db/Adapter/SQLiteAdapterTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Phinx/Db/Adapter/SQLiteAdapterTest.php b/tests/Phinx/Db/Adapter/SQLiteAdapterTest.php index c81279d32..387ad2aac 100644 --- a/tests/Phinx/Db/Adapter/SQLiteAdapterTest.php +++ b/tests/Phinx/Db/Adapter/SQLiteAdapterTest.php @@ -1181,15 +1181,15 @@ public function testQueryWithParams() 'int_col' => 23, ]); - $countQuery = $this->adapter->query('SELECT COUNT(*) FROM table1 WHERE int_col > ?', [5]); + $countQuery = $this->adapter->query('SELECT COUNT(*) as c FROM table1 WHERE int_col > ?', [5]); $res = $countQuery->fetchAll(); - $this->assertEquals(2, $res[0]['COUNT(*)']); + $this->assertEquals(2, $res[0]['c']); $this->adapter->execute('UPDATE table1 SET int_col = ? WHERE int_col IS NULL', [12]); $countQuery->execute([1]); $res = $countQuery->fetchAll(); - $this->assertEquals(3, $res[0]['COUNT(*)']); + $this->assertEquals(3, $res[0]['c']); } /** From 12f7d257757bd7f84611b5b60ca624dd81735d2d Mon Sep 17 00:00:00 2001 From: Matthew Peveler Date: Wed, 24 Feb 2021 00:19:53 -0500 Subject: [PATCH 06/14] Update SQLiteAdapterTest.php --- tests/Phinx/Db/Adapter/SQLiteAdapterTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Phinx/Db/Adapter/SQLiteAdapterTest.php b/tests/Phinx/Db/Adapter/SQLiteAdapterTest.php index 387ad2aac..d8d2ad18e 100644 --- a/tests/Phinx/Db/Adapter/SQLiteAdapterTest.php +++ b/tests/Phinx/Db/Adapter/SQLiteAdapterTest.php @@ -1181,7 +1181,7 @@ public function testQueryWithParams() 'int_col' => 23, ]); - $countQuery = $this->adapter->query('SELECT COUNT(*) as c FROM table1 WHERE int_col > ?', [5]); + $countQuery = $this->adapter->query('SELECT COUNT(*) AS c FROM table1 WHERE int_col > ?', [5]); $res = $countQuery->fetchAll(); $this->assertEquals(2, $res[0]['c']); From 9725c30f2c15d1beb9f27e8c2d219b9e33317d8d Mon Sep 17 00:00:00 2001 From: Matthew Peveler Date: Sun, 28 Mar 2021 13:12:21 -0400 Subject: [PATCH 07/14] fix failing PDO test Signed-off-by: Matthew Peveler --- tests/Phinx/Db/Adapter/PdoAdapterTest.php | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/tests/Phinx/Db/Adapter/PdoAdapterTest.php b/tests/Phinx/Db/Adapter/PdoAdapterTest.php index 5de3aa705..4728b8bdb 100644 --- a/tests/Phinx/Db/Adapter/PdoAdapterTest.php +++ b/tests/Phinx/Db/Adapter/PdoAdapterTest.php @@ -14,8 +14,21 @@ public function __construct() } } +class MockPdoStatement extends \PDOStatement +{ + public function execute() + { + return true; + } + + public function rowCount() + { + return 1; + } +} + /** - * A mock PDO that stores its last exec()'d SQL that can be retrieved for queries. + * A mock PDO that stores its last prepare()'d SQL that can be retrieved for queries. * * This exists as $this->getMockForAbstractClass('\PDO') fails under PHP5.4 and * an older PHPUnit; a PDO instance cannot be serialised. @@ -24,9 +37,10 @@ class PdoAdapterTestPDOMockWithExecChecks extends PdoAdapterTestPDOMock { private $sql; - public function exec($sql) + public function prepare($sql, $options = []) { $this->sql = $sql; + return new MockPdoStatement(); } public function getExecutedSqlForTest() From da8849da5c4b7780bc84981834149c29022ae5ce Mon Sep 17 00:00:00 2001 From: stickler-ci Date: Sun, 28 Mar 2021 17:12:33 +0000 Subject: [PATCH 08/14] Fixing style errors. --- tests/Phinx/Db/Adapter/PdoAdapterTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/Phinx/Db/Adapter/PdoAdapterTest.php b/tests/Phinx/Db/Adapter/PdoAdapterTest.php index 4728b8bdb..176dab77b 100644 --- a/tests/Phinx/Db/Adapter/PdoAdapterTest.php +++ b/tests/Phinx/Db/Adapter/PdoAdapterTest.php @@ -40,6 +40,7 @@ class PdoAdapterTestPDOMockWithExecChecks extends PdoAdapterTestPDOMock public function prepare($sql, $options = []) { $this->sql = $sql; + return new MockPdoStatement(); } From e53d4e0b9816d516124ea84789184972d9f21022 Mon Sep 17 00:00:00 2001 From: Matthew Peveler Date: Sun, 28 Mar 2021 13:13:35 -0400 Subject: [PATCH 09/14] add tests for other adapters Signed-off-by: Matthew Peveler --- tests/Phinx/Db/Adapter/MysqlAdapterTest.php | 31 +++++++++++++++++++ .../Phinx/Db/Adapter/PostgresAdapterTest.php | 31 +++++++++++++++++++ .../Phinx/Db/Adapter/SqlServerAdapterTest.php | 31 +++++++++++++++++++ 3 files changed, 93 insertions(+) diff --git a/tests/Phinx/Db/Adapter/MysqlAdapterTest.php b/tests/Phinx/Db/Adapter/MysqlAdapterTest.php index 3475ea910..f9c728337 100644 --- a/tests/Phinx/Db/Adapter/MysqlAdapterTest.php +++ b/tests/Phinx/Db/Adapter/MysqlAdapterTest.php @@ -1972,6 +1972,37 @@ public function testQueryBuilder() $this->assertEquals(1, $stm->rowCount()); } + public function testQueryWithParams() + { + $table = new \Phinx\Db\Table('table1', [], $this->adapter); + $table->addColumn('string_col', 'string') + ->addColumn('int_col', 'integer') + ->save(); + + $this->adapter->insert($table->getTable(), [ + 'string_col' => 'test data', + 'int_col' => 10, + ]); + + $this->adapter->insert($table->getTable(), [ + 'string_col' => null, + ]); + + $this->adapter->insert($table->getTable(), [ + 'int_col' => 23, + ]); + + $countQuery = $this->adapter->query('SELECT COUNT(*) AS c FROM table1 WHERE int_col > ?', [5]); + $res = $countQuery->fetchAll(); + $this->assertEquals(2, $res[0]['c']); + + $this->adapter->execute('UPDATE table1 SET int_col = ? WHERE int_col IS NULL', [12]); + + $countQuery->execute([1]); + $res = $countQuery->fetchAll(); + $this->assertEquals(3, $res[0]['c']); + } + public function testLiteralSupport() { $createQuery = <<<'INPUT' diff --git a/tests/Phinx/Db/Adapter/PostgresAdapterTest.php b/tests/Phinx/Db/Adapter/PostgresAdapterTest.php index 3ac75fb77..57acaf613 100644 --- a/tests/Phinx/Db/Adapter/PostgresAdapterTest.php +++ b/tests/Phinx/Db/Adapter/PostgresAdapterTest.php @@ -2216,6 +2216,37 @@ public function testQueryBuilder() $this->assertEquals(1, $stm->rowCount()); } + public function testQueryWithParams() + { + $table = new \Phinx\Db\Table('table1', [], $this->adapter); + $table->addColumn('string_col', 'string') + ->addColumn('int_col', 'integer') + ->save(); + + $this->adapter->insert($table->getTable(), [ + 'string_col' => 'test data', + 'int_col' => 10, + ]); + + $this->adapter->insert($table->getTable(), [ + 'string_col' => null, + ]); + + $this->adapter->insert($table->getTable(), [ + 'int_col' => 23, + ]); + + $countQuery = $this->adapter->query('SELECT COUNT(*) AS c FROM table1 WHERE int_col > ?', [5]); + $res = $countQuery->fetchAll(); + $this->assertEquals(2, $res[0]['c']); + + $this->adapter->execute('UPDATE table1 SET int_col = ? WHERE int_col IS NULL', [12]); + + $countQuery->execute([1]); + $res = $countQuery->fetchAll(); + $this->assertEquals(3, $res[0]['c']); + } + public function testRenameMixedCaseTableAndColumns() { $table = new \Phinx\Db\Table('OrganizationSettings', [], $this->adapter); diff --git a/tests/Phinx/Db/Adapter/SqlServerAdapterTest.php b/tests/Phinx/Db/Adapter/SqlServerAdapterTest.php index 7e58b0320..7ebfe1897 100644 --- a/tests/Phinx/Db/Adapter/SqlServerAdapterTest.php +++ b/tests/Phinx/Db/Adapter/SqlServerAdapterTest.php @@ -1109,6 +1109,37 @@ public function testQueryBuilder() $stm->closeCursor(); } + public function testQueryWithParams() + { + $table = new \Phinx\Db\Table('table1', [], $this->adapter); + $table->addColumn('string_col', 'string') + ->addColumn('int_col', 'integer') + ->save(); + + $this->adapter->insert($table->getTable(), [ + 'string_col' => 'test data', + 'int_col' => 10, + ]); + + $this->adapter->insert($table->getTable(), [ + 'string_col' => null, + ]); + + $this->adapter->insert($table->getTable(), [ + 'int_col' => 23, + ]); + + $countQuery = $this->adapter->query('SELECT COUNT(*) AS c FROM table1 WHERE int_col > ?', [5]); + $res = $countQuery->fetchAll(); + $this->assertEquals(2, $res[0]['c']); + + $this->adapter->execute('UPDATE table1 SET int_col = ? WHERE int_col IS NULL', [12]); + + $countQuery->execute([1]); + $res = $countQuery->fetchAll(); + $this->assertEquals(3, $res[0]['c']); + } + public function testLiteralSupport() { $createQuery = <<<'INPUT' From e3659fb34c2a66002933f5b767ec46ba88ae97ca Mon Sep 17 00:00:00 2001 From: Matthew Peveler Date: Sun, 28 Mar 2021 13:20:12 -0400 Subject: [PATCH 10/14] remove extends \PDOStatement --- tests/Phinx/Db/Adapter/PdoAdapterTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Phinx/Db/Adapter/PdoAdapterTest.php b/tests/Phinx/Db/Adapter/PdoAdapterTest.php index 176dab77b..4d2d59980 100644 --- a/tests/Phinx/Db/Adapter/PdoAdapterTest.php +++ b/tests/Phinx/Db/Adapter/PdoAdapterTest.php @@ -14,7 +14,7 @@ public function __construct() } } -class MockPdoStatement extends \PDOStatement +class MockPdoStatement { public function execute() { From bf0f8d37656385ab3933aa96823b6e447f1ce0c9 Mon Sep 17 00:00:00 2001 From: Matthew Peveler Date: Thu, 17 Jun 2021 11:45:10 +0000 Subject: [PATCH 11/14] allow multiple statements --- src/Phinx/Db/Adapter/PdoAdapter.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/Phinx/Db/Adapter/PdoAdapter.php b/src/Phinx/Db/Adapter/PdoAdapter.php index e5a78a8c3..d8d295088 100644 --- a/src/Phinx/Db/Adapter/PdoAdapter.php +++ b/src/Phinx/Db/Adapter/PdoAdapter.php @@ -191,6 +191,10 @@ public function execute($sql, array $params = []) return 0; } + if (empty($params)) { + return $this->getConnection()->exec($sql); + } + $stmt = $this->getConnection()->prepare($sql); $result = $stmt->execute($params); @@ -222,6 +226,9 @@ public function getQueryBuilder() */ public function query($sql, array $params = []) { + if (empty($params)) { + return $this->getConnection()->query($sql); + } $stmt = $this->getConnection()->prepare($sql); $result = $stmt->execute($params); From 386b25adf283d096621ba49c33c7be097eee23bf Mon Sep 17 00:00:00 2001 From: Matthew Peveler Date: Thu, 17 Jun 2021 11:48:00 +0000 Subject: [PATCH 12/14] re-add missing test method --- tests/Phinx/Db/Adapter/PdoAdapterTest.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/Phinx/Db/Adapter/PdoAdapterTest.php b/tests/Phinx/Db/Adapter/PdoAdapterTest.php index efb5cdc70..c061bac35 100644 --- a/tests/Phinx/Db/Adapter/PdoAdapterTest.php +++ b/tests/Phinx/Db/Adapter/PdoAdapterTest.php @@ -37,6 +37,11 @@ class PdoAdapterTestPDOMockWithExecChecks extends PdoAdapterTestPDOMock { private $sql; + public function exec($sql) + { + $this->sql = $sql; + } + public function prepare($sql, $options = []) { $this->sql = $sql; From cd469cdeff7d1f9485df9eee8ffc7230be6d6499 Mon Sep 17 00:00:00 2001 From: Matthew Peveler Date: Thu, 17 Jun 2021 11:54:26 +0000 Subject: [PATCH 13/14] add note about prepared queries --- docs/en/migrations.rst | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/docs/en/migrations.rst b/docs/en/migrations.rst index 612de7542..00f96fce2 100644 --- a/docs/en/migrations.rst +++ b/docs/en/migrations.rst @@ -172,7 +172,9 @@ Executing Queries Queries can be executed with the ``execute()`` and ``query()`` methods. The ``execute()`` method returns the number of affected rows whereas the ``query()`` method returns the result as a -`PDOStatement `_ +`PDOStatement `_. Both methods +accept an optional second parameter ``$params`` which is an array of elements, +and if used will cause the underlying connection to use a prepared statement. .. code-block:: php @@ -193,6 +195,11 @@ Queries can be executed with the ``execute()`` and ``query()`` methods. The // query() $stmt = $this->query('SELECT * FROM users'); // returns PDOStatement $rows = $stmt->fetchAll(); // returns the result as an array + + // using prepared queries + $count = $this->execute('DELETE FROM users WHERE id = ?', [5]); + $stmt = $this->query('SELECT * FROM users WHERE id > 5'); // returns PDOStatement + $rows = $stmt->fetchAll(); } /** @@ -213,6 +220,12 @@ Queries can be executed with the ``execute()`` and ``query()`` methods. The DELIMITERs during insertion of stored procedures or triggers which don't support DELIMITERs. +.. note:: + + If you wish to execute multiple queries at once, you may not also use the prepared + variant of these functions. When using prepared queries, PDO can only execute + them one at a time. + .. warning:: When using ``execute()`` or ``query()`` with a batch of queries, PDO doesn't From 5132b12f1924794283ecc1873dae6b8055759958 Mon Sep 17 00:00:00 2001 From: Matthew Peveler Date: Thu, 17 Jun 2021 13:10:54 +0000 Subject: [PATCH 14/14] fix phpcs errors --- src/Phinx/Db/Adapter/PdoAdapter.php | 1 - tests/Phinx/Db/Mock/PdoAdapterTestPDOMockWithExecChecks.php | 2 +- tests/Phinx/Db/Mock/PdoStatementMock.php | 2 +- 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Phinx/Db/Adapter/PdoAdapter.php b/src/Phinx/Db/Adapter/PdoAdapter.php index 688e0c138..d9b2535ba 100644 --- a/src/Phinx/Db/Adapter/PdoAdapter.php +++ b/src/Phinx/Db/Adapter/PdoAdapter.php @@ -219,7 +219,6 @@ public function getQueryBuilder() * Executes a query and returns PDOStatement. * * @param string $sql SQL - * * @return \PDOStatement|false */ public function query($sql, array $params = []) diff --git a/tests/Phinx/Db/Mock/PdoAdapterTestPDOMockWithExecChecks.php b/tests/Phinx/Db/Mock/PdoAdapterTestPDOMockWithExecChecks.php index 56c4c9aab..08c37806c 100644 --- a/tests/Phinx/Db/Mock/PdoAdapterTestPDOMockWithExecChecks.php +++ b/tests/Phinx/Db/Mock/PdoAdapterTestPDOMockWithExecChecks.php @@ -21,7 +21,7 @@ public function prepare($sql, $options = []) { $this->sql = $sql; - return new MockPdoStatement(); + return new PdoStatementMock(); } public function getExecutedSqlForTest() diff --git a/tests/Phinx/Db/Mock/PdoStatementMock.php b/tests/Phinx/Db/Mock/PdoStatementMock.php index 0df2e136e..e6051a7b3 100644 --- a/tests/Phinx/Db/Mock/PdoStatementMock.php +++ b/tests/Phinx/Db/Mock/PdoStatementMock.php @@ -2,7 +2,7 @@ namespace Test\Phinx\Db\Mock; -class MockPdoStatement +class PdoStatementMock { public function execute() {