Merge branch '4.3' into 4

This commit is contained in:
Aaron Carlino 2019-04-18 11:57:36 +12:00
commit c63eecc3e1
3 changed files with 49 additions and 3 deletions

View File

@ -206,8 +206,8 @@ class DBText extends DBString
if ($position > 0) {
// We don't want to start mid-word
$position = max(
(int) mb_strrpos(substr($text, 0, $position), ' '),
(int) mb_strrpos(substr($text, 0, $position), "\n")
(int) mb_strrpos(mb_substr($text, 0, $position), ' '),
(int) mb_strrpos(mb_substr($text, 0, $position), "\n")
);
}

View File

@ -283,6 +283,16 @@ abstract class SQLConditionalExpression extends SQLExpression
// array('type' => 'inner', 'table' => 'SiteTree', 'filter' => array("SiteTree.ID = 1",
// "Status = 'approved'", 'order' => 20))
if (!is_array($join)) {
if (empty($alias) || is_numeric($alias)) {
continue;
}
$trimmedAlias = trim($alias, '"');
if ($trimmedAlias !== trim($join, '"')) {
$joins[$alias] = "{$join} AS \"{$trimmedAlias}\"";
}
continue;
}
@ -321,8 +331,15 @@ abstract class SQLConditionalExpression extends SQLExpression
*/
protected function getOrderedJoins($from)
{
if (count($from) <= 1) {
return $from;
}
// shift the first FROM table out from so we only deal with the JOINs
reset($from);
$baseFromAlias = key($from);
$baseFrom = array_shift($from);
$this->mergesort($from, function ($firstJoin, $secondJoin) {
if (!is_array($firstJoin)
|| !is_array($secondJoin)
@ -335,7 +352,12 @@ abstract class SQLConditionalExpression extends SQLExpression
});
// Put the first FROM table back into the results
array_unshift($from, $baseFrom);
if (!empty($baseFromAlias) && !is_numeric($baseFromAlias)) {
$from = array_merge([$baseFromAlias => $baseFrom], $from);
} else {
array_unshift($from, $baseFrom);
}
return $from;
}

View File

@ -823,4 +823,28 @@ class SQLSelectTest extends SapphireTest
$this->assertEquals(array('%MyName%', '2012-08-08 12:00'), $parameters);
$query->execute();
}
public function testBaseTableAliases()
{
$query = SQLSelect::create('*', ['"MyTableAlias"' => '"MyTable"']);
$sql = $query->sql();
$this->assertSQLEquals('SELECT * FROM "MyTable" AS "MyTableAlias"', $sql);
$query = SQLSelect::create('*', ['MyTableAlias' => '"MyTable"']);
$sql = $query->sql();
$this->assertSQLEquals('SELECT * FROM "MyTable" AS "MyTableAlias"', $sql);
$query = SQLSelect::create('*', ['"MyTableAlias"' => '"MyTable"']);
$query->addLeftJoin('OtherTable', '"Thing" = "OtherThing"', 'OtherTableAlias');
$sql = $query->sql();
$this->assertSQLEquals(
'SELECT *
FROM "MyTable" AS "MyTableAlias"
LEFT JOIN "OtherTable" AS "OtherTableAlias" ON "Thing" = "OtherThing"',
$sql
);
}
}