Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional $params argument to query/execute adapter methods #1962

Merged
merged 17 commits into from
Jun 17, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/Phinx/Db/Adapter/AdapterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
8 changes: 4 additions & 4 deletions src/Phinx/Db/Adapter/AdapterWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
16 changes: 11 additions & 5 deletions src/Phinx/Db/Adapter/PdoAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -191,7 +191,10 @@ 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;
}

/**
Expand All @@ -215,11 +218,14 @@ public function getQueryBuilder()
*
* @param string $sql SQL
*
* @return \PDOStatement
* @return \PDOStatement|false
dereuromark marked this conversation as resolved.
Show resolved Hide resolved
*/
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;
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/Phinx/Migration/AbstractMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
6 changes: 4 additions & 2 deletions src/Phinx/Migration/MigrationInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions src/Phinx/Seed/AbstractSeed.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
6 changes: 4 additions & 2 deletions src/Phinx/Seed/SeedInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down
31 changes: 31 additions & 0 deletions tests/Phinx/Db/Adapter/SQLiteAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(*) 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']);
}

/**
* Tests adding more than one column to a table
* that already exists due to adapters having different add column instructions
Expand Down