Merge branch 'dewekk/fix-dbquery' into develop

This commit is contained in:
Kijin Sung 2022-07-03 17:26:15 +09:00
commit fe7833698a
3 changed files with 11 additions and 5 deletions

View file

@ -326,7 +326,7 @@ class Query extends VariableBase
// Compose the INTO clause. // Compose the INTO clause.
if (count($this->tables)) if (count($this->tables))
{ {
$tables = $this->_arrangeTables($this->tables, false); $tables = $this->_arrangeTables($this->tables, true, false);
if ($tables !== '') if ($tables !== '')
{ {
$result .= $tables; $result .= $tables;
@ -419,9 +419,10 @@ class Query extends VariableBase
* *
* @param array $tables * @param array $tables
* @param bool $use_aliases * @param bool $use_aliases
* @param bool $use_default_aliases
* @return string * @return string
*/ */
protected function _arrangeTables(array $tables, bool $use_aliases = true): string protected function _arrangeTables(array $tables, bool $use_aliases = true, bool $use_default_aliases = true): string
{ {
// Initialize the result. // Initialize the result.
$result = array(); $result = array();
@ -439,6 +440,7 @@ class Query extends VariableBase
if ($table instanceof self) if ($table instanceof self)
{ {
$tabledef = '(' . $table->getQueryString($this->_prefix, $this->_args) . ')'; $tabledef = '(' . $table->getQueryString($this->_prefix, $this->_args) . ')';
$table->alias = $table->alias ?: $table->name;
if ($table->alias) if ($table->alias)
{ {
$tabledef .= ' AS `' . $table->alias . '`'; $tabledef .= ' AS `' . $table->alias . '`';
@ -453,6 +455,10 @@ class Query extends VariableBase
else else
{ {
$tabledef = self::quoteName($this->_prefix . $table->name); $tabledef = self::quoteName($this->_prefix . $table->name);
if ($use_default_aliases)
{
$table->alias = $table->alias ?: $table->name;
}
if ($use_aliases && $table->alias && $table->alias !== ($this->_prefix . $table->name)) if ($use_aliases && $table->alias && $table->alias !== ($this->_prefix . $table->name))
{ {
$tabledef .= ' AS `' . $table->alias . '`'; $tabledef .= ' AS `' . $table->alias . '`';

View file

@ -65,7 +65,7 @@ class DBQueryParser extends BaseParser
{ {
$table = new DBQuery\Table; $table = new DBQuery\Table;
$table->name = trim($tag['name']); $table->name = trim($tag['name']);
$table->alias = trim($tag['alias']) ?: $table->name; $table->alias = trim($tag['alias']) ?: null;
$table->ifvar = trim($tag['if']) ?: null; $table->ifvar = trim($tag['if']) ?: null;
} }
@ -78,7 +78,7 @@ class DBQueryParser extends BaseParser
$table->join_conditions = self::_parseConditions($tag->conditions); $table->join_conditions = self::_parseConditions($tag->conditions);
} }
} }
$query->tables[$table->alias] = $table; $query->tables[$table->alias ?: $table->name] = $table;
} }
// Load index hints. // Load index hints.

View file

@ -11,7 +11,7 @@ class DBQueryParserTest extends \Codeception\TestCase\Test
$this->assertTrue($query->select_distinct); $this->assertTrue($query->select_distinct);
$this->assertTrue($query->tables['documents'] instanceof Rhymix\Framework\Parsers\DBQuery\Table); $this->assertTrue($query->tables['documents'] instanceof Rhymix\Framework\Parsers\DBQuery\Table);
$this->assertEquals('documents', $query->tables['documents']->alias); $this->assertEquals('documents', $query->tables['documents']->name);
$this->assertEquals(1, count($query->columns)); $this->assertEquals(1, count($query->columns));
$this->assertEquals('*', $query->columns[0]->name); $this->assertEquals('*', $query->columns[0]->name);