mirror of
https://github.com/silverstripe/silverstripe-postgresql
synced 2024-10-22 17:05:45 +02:00
Update API for 3.2 release
Fix bug with literal question mark in conditions See https://github.com/silverstripe/silverstripe-framework/pull/4288
This commit is contained in:
parent
31abb29c5d
commit
a596d9d343
@ -161,6 +161,7 @@ class PostgreSQLConnector extends DBConnector {
|
||||
$segments = preg_split('/\?/', $sql);
|
||||
$joined = '';
|
||||
$inString = false;
|
||||
$num = 0;
|
||||
for($i = 0; $i < count($segments); $i++) {
|
||||
// Append next segment
|
||||
$joined .= $segments[$i];
|
||||
@ -174,12 +175,19 @@ class PostgreSQLConnector extends DBConnector {
|
||||
}
|
||||
|
||||
// Append placeholder replacement
|
||||
$joined .= $inString ? "?" : ('$'.($i+1));
|
||||
if($inString) {
|
||||
$joined .= "?";
|
||||
} else {
|
||||
$joined .= '$' . ++$num;
|
||||
}
|
||||
}
|
||||
return $joined;
|
||||
}
|
||||
|
||||
public function preparedQuery($sql, $parameters, $errorLevel = E_USER_ERROR) {
|
||||
// Reset state
|
||||
$this->lastQuery = null;
|
||||
$this->lastRows = 0;
|
||||
|
||||
// Replace question mark placeholders with numeric placeholders
|
||||
if(!empty($parameters)) {
|
||||
@ -187,26 +195,22 @@ class PostgreSQLConnector extends DBConnector {
|
||||
$parameters = $this->parameterValues($parameters);
|
||||
}
|
||||
|
||||
// Check if we should only preview this query
|
||||
if ($this->previewWrite($sql)) return;
|
||||
|
||||
// Benchmark query
|
||||
$conn = $this->dbConn;
|
||||
$this->lastQuery = $result = $this->benchmarkQuery($sql, function($sql) use($conn, $parameters) {
|
||||
// Execute query
|
||||
if(!empty($parameters)) {
|
||||
return pg_query_params($conn, $sql, $parameters);
|
||||
$result = pg_query_params($this->dbConn, $sql, $parameters);
|
||||
} else {
|
||||
return pg_query($conn, $sql);
|
||||
$result = pg_query($this->dbConn, $sql);
|
||||
}
|
||||
});
|
||||
$this->lastRows = 0;
|
||||
|
||||
// Handle error
|
||||
if ($result === false) {
|
||||
$this->databaseError($this->getLastError(), $errorLevel, $sql, $parameters);
|
||||
return null;
|
||||
} else {
|
||||
$this->lastRows = pg_affected_rows($result);
|
||||
}
|
||||
|
||||
// Save and return results
|
||||
$this->lastQuery = $result;
|
||||
$this->lastRows = pg_affected_rows($result);
|
||||
return new PostgreSQLQuery($result);
|
||||
}
|
||||
|
||||
|
@ -32,17 +32,17 @@ class PostgreSQLConnectorTest extends SapphireTest {
|
||||
|
||||
// Ignoring question mark placeholders within string literals with escaped slashes
|
||||
$this->assertEquals(
|
||||
"SELECT * FROM Table WHERE ID = $1 AND Title = '\\'' AND Name = $2 AND Content = '<p>What is love?</p>'",
|
||||
"SELECT * FROM Table WHERE ID = $1 AND Title = '\\'' AND Content = '<p>What is love?</p>' AND Name = $2",
|
||||
$connector->replacePlaceholders(
|
||||
"SELECT * FROM Table WHERE ID = ? AND Title = '\\'' AND Name = ? AND Content = '<p>What is love?</p>'"
|
||||
"SELECT * FROM Table WHERE ID = ? AND Title = '\\'' AND Content = '<p>What is love?</p>' AND Name = ?"
|
||||
)
|
||||
);
|
||||
|
||||
// same as above, but use double single quote escape syntax
|
||||
$this->assertEquals(
|
||||
"SELECT * FROM Table WHERE ID = $1 AND Title = '''' AND Name = $2 AND Content = '<p>What is love?</p>'",
|
||||
"SELECT * FROM Table WHERE ID = $1 AND Title = '''' AND Content = '<p>What is love?</p>' AND Name = $2",
|
||||
$connector->replacePlaceholders(
|
||||
"SELECT * FROM Table WHERE ID = ? AND Title = '''' AND Name = ? AND Content = '<p>What is love?</p>'"
|
||||
"SELECT * FROM Table WHERE ID = ? AND Title = '''' AND Content = '<p>What is love?</p>' AND Name = ?"
|
||||
)
|
||||
);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user