Skip to content

Commit

Permalink
Fixed regression in SqlBuilder with ` escaping. Added unit test for t…
Browse files Browse the repository at this point in the history
…his regression.
  • Loading branch information
KrisJordan committed Feb 18, 2009
1 parent d116cc5 commit ce766e4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
22 changes: 11 additions & 11 deletions recess/lib/recess/database/sql/SqlBuilder.class.php
Expand Up @@ -461,7 +461,7 @@ protected function tableAsPrefix() {
if($this->usingAliases) {
$spacePos = strrpos($this->table, ' ');
if($spacePos !== false) {
return substr($this->table, $spacePos);
return substr($this->table, $spacePos + 1);
}
}
return $this->table;
Expand Down Expand Up @@ -512,7 +512,7 @@ protected function join($leftOrRight, $innerOrOuter, $table, $tablePrimaryKey, $
$number = 2;
}
$tableAlias = $this->table . '__' . $number;
$this->table = $this->table . ' AS ' . $tableAlias;
$this->table = self::escapeWithTicks($this->table) . ' AS ' . self::escapeWithTicks($tableAlias);
$this->usingAliases = true;

$tablePrimaryKey = str_replace($oldTable,$tableAlias,$tablePrimaryKey);
Expand Down Expand Up @@ -575,15 +575,15 @@ protected function joinHelper() {
if(!empty($this->joins)) {
$joins = array_reverse($this->joins, true);
foreach($joins as $join) {
$joinStatement = '';
$joinStatement = ' ';

if(isset($join->natural)) {
if(isset($join->natural) && $join->natural != '') {
$joinStatement .= $join->natural . ' ';
}
if(isset($join->leftRightOrFull)) {
if(isset($join->leftRightOrFull) && $join->leftRightOrFull != '') {
$joinStatement .= $join->leftRightOrFull . ' ';
}
if(isset($join->innerOuterOrCross)) {
if(isset($join->innerOuterOrCross) && $join->innerOuterOrCross != '') {
$joinStatement .= $join->innerOuterOrCross . ' ';
}

Expand All @@ -597,11 +597,11 @@ protected function joinHelper() {
}

protected static function escapeWithTicks($string) {
if($string == '*') {
if($string == '*' || strpos($string, '`') !== false) {
return $string;
}
if(strpos($string,Library::dotSeparator) !== false) {
$parts = explode('.', $string);
if(strpos($string,Library::dotSeparator) !== false) { // Todo: Replace with Regexp
$parts = explode(Library::dotSeparator, $string);
if(isset($parts[1]) && $parts[1] == '*') {
return '`' . $parts[0] . '`.*';
} else {
Expand Down Expand Up @@ -703,8 +703,8 @@ class Join {
const OUTER = 'OUTER';
const CROSS = 'CROSS';

public $natural = '';
public $leftRightOrFull = '';
public $natural;
public $leftRightOrFull;
public $innerOuterOrCross = 'OUTER';

public $table;
Expand Down
5 changes: 5 additions & 0 deletions recess/test/recess/database/sql/SelectSqlBuilderTest.php
Expand Up @@ -111,6 +111,11 @@ function testClone() {
$this->assertNotEquals($this->builder->select(), $anotherBuilder->select());
}

function testSameTableJoin() {
$this->builder->from('recess_tools_packages')->equal('id', 8)->innerJoin('recess_tools_packages', 'recess_tools_packages.parentId', 'recess_tools_packages.id');
$this->assertEquals('SELECT `recess_tools_packages__2`.* FROM `recess_tools_packages` AS `recess_tools_packages__2` INNER JOIN `recess_tools_packages` ON `recess_tools_packages__2`.`parentId` = `recess_tools_packages`.`id` WHERE `recess_tools_packages`.`id` = 8', $this->builder->select());
}

function tearDown() {
unset($this->builder);
}
Expand Down

0 comments on commit ce766e4

Please sign in to comment.