Skip to content

Commit

Permalink
InternalBackwardsCompatibilityBreak - Changed the structure of the ar…
Browse files Browse the repository at this point in the history
…ray returned from fSQLTranslation::translate() to include a number plus : before the original SQL in the array keys.

Fixed a bug with running multiple identical SQL statements containing literal strings through fDatabase::translatedQuery().
  • Loading branch information
wbond committed Apr 12, 2012
1 parent 88b4ad4 commit 6d3c4b6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
25 changes: 15 additions & 10 deletions classes/fDatabase.php
Expand Up @@ -52,7 +52,8 @@
* @package Flourish
* @link http://flourishlib.com/fDatabase
*
* @version 1.0.0b26
* @version 1.0.0b27
* @changes 1.0.0b27 Fixed a bug with running multiple copies of a SQL statement with string values through a single ::translatedQuery() call [wb, 2010-07-14]
* @changes 1.0.0b26 Updated the class to use new fCore functionality [wb, 2010-07-05]
* @changes 1.0.0b25 Added IBM DB2 support [wb, 2010-04-13]
* @changes 1.0.0b24 Fixed an auto-incrementing transaction bug with Oracle and debugging issues with all databases [wb, 2010-03-17]
Expand Down Expand Up @@ -2554,7 +2555,7 @@ private function prepareStatement($sql, $translate=FALSE)
* @param string $sql The SQL to prepare
* @param array $values Literal values to escape into the SQL
* @param boolean $translate If the SQL should be translated
* @return array The split out SQL queries, queries that have been translated will have a string key of the original SQL, non-translated SQL will have a numeric key
* @return array The split out SQL queries, queries that have been translated will have a string key of a number, `:` and the original SQL, non-translated SQL will have a numeric key
*/
private function prepareSQL($sql, $values, $translate)
{
Expand Down Expand Up @@ -2662,8 +2663,10 @@ private function prepareSQL($sql, $values, $translate)
}

$output = array();
foreach (array_keys($queries) as $number => $key) {
foreach (array_keys($queries) as $key) {
$query = $queries[$key];
$parts = explode(':', $key, 2);
$number = $parts[0];

// Escape the values into the SQL
if (isset($chunked_values[$number])) {
Expand Down Expand Up @@ -2706,7 +2709,7 @@ public function query($statement)
}

$queries = $this->prepareSQL($statement, $params, FALSE);

$output = array();
foreach ($queries as $query) {
$output[] = $this->run($query, 'fResult');
Expand Down Expand Up @@ -2991,10 +2994,11 @@ public function translatedQuery($sql)
);

$output = array();
foreach ($queries as $original_query => $query) {
foreach ($queries as $key => $query) {
$result = $this->run($query, 'fResult');
if (!is_numeric($original_query)) {
$result->setUntranslatedSQL($original_query);
if (!is_numeric($key)) {
list($number, $original_query) = explode(':', $key, 2);
$result->setUntranslatedSQL($original_query);
}
$output[] = $result;
}
Expand Down Expand Up @@ -3070,10 +3074,11 @@ public function unbufferedTranslatedQuery($sql)
);
}

$query_keys = array_keys($queries);
$original_query = $query_keys[0];
$query_keys = array_keys($queries);
$key = $query_keys[0];
list($number, $original_query) = explode(':', $key, 2);

$result = $this->run($queries[$original_query], 'fUnbufferedResult');
$result = $this->run($queries[$key], 'fUnbufferedResult');
$result->setUntranslatedSQL($original_query);

$this->unbuffered_result = $result;
Expand Down
7 changes: 4 additions & 3 deletions classes/fSQLTranslation.php
Expand Up @@ -9,7 +9,8 @@
* @package Flourish
* @link http://flourishlib.com/fSQLTranslation
*
* @version 1.0.0b16
* @version 1.0.0b17
* @changes 1.0.0b17 Internal Backwards Compatiblity Break - changed the array keys for translated queries returned from ::translate() to include a number plus `:` before the original SQL, preventing duplicate keys [wb, 2010-07-14]
* @changes 1.0.0b16 Added IBM DB2 support [wb, 2010-04-13]
* @changes 1.0.0b15 Fixed a bug with MSSQL national character conversion when running a SQL statement with a sub-select containing joins [wb, 2009-12-18]
* @changes 1.0.0b14 Changed PostgreSQL to cast columns in LOWER() calls to VARCHAR to allow UUID columns (which are treated as a VARCHAR by fSchema) to work with default primary key ordering in fRecordSet [wb, 2009-12-16]
Expand Down Expand Up @@ -762,7 +763,7 @@ private function makeCachePrefix()
* @internal
*
* @param array $statements The SQL statements to translate
* @return array The translated SQL statements all ready for execution. Statements that have been translated will have string key of the original SQL, all other will have a numeric key.
* @return array The translated SQL statements all ready for execution. Statements that have been translated will have string key of the number, `:` and the original SQL, all other will have a numeric key.
*/
public function translate($statements)
{
Expand Down Expand Up @@ -811,7 +812,7 @@ public function translate($statements)
);
}

$output = array_merge($output, array($sql => $new_sql), $extra_statements);
$output = array_merge($output, array($number . ':' . $sql => $new_sql), $extra_statements);
}

return $output;
Expand Down

0 comments on commit 6d3c4b6

Please sign in to comment.