Implement INSERT and UPDATE queries using new parser

This commit is contained in:
Kijin Sung 2020-06-27 00:49:04 +09:00
parent 03279788da
commit 7f8312ee1a
3 changed files with 67 additions and 5 deletions

View file

@ -204,7 +204,33 @@ class Query extends VariableBase
*/
protected function _getInsertQueryString(): string
{
return '';
// Initialize the query string.
$result = 'INSERT';
// Compose the INTO clause.
if (count($this->tables))
{
$tables = $this->_arrangeTables($this->tables);
if ($tables !== '')
{
$result .= ' INTO ' . $tables;
}
}
// Process the SET clause with new values.
$columns = array();
foreach ($this->columns as $column)
{
$setval_string = $this->_parseCondition($column);
if ($setval_string !== '')
{
$columns[] = $setval_string;
}
}
$result .= ' SET ' . implode(', ', $columns);
// Return the final query string.
return $result;
}
/**
@ -214,7 +240,43 @@ class Query extends VariableBase
*/
protected function _getUpdateQueryString(): string
{
return '';
// Initialize the query string.
$result = 'UPDATE ';
// Compose the INTO clause.
if (count($this->tables))
{
$tables = $this->_arrangeTables($this->tables);
if ($tables !== '')
{
$result .= $tables;
}
}
// Compose the SET clause with updated values.
$columns = array();
foreach ($this->columns as $column)
{
$setval_string = $this->_parseCondition($column);
if ($setval_string !== '')
{
$columns[] = $setval_string;
}
}
$result .= ' SET ' . implode(', ', $columns);
// Compose the WHERE clause.
if (count($this->conditions))
{
$where = $this->_arrangeConditions($this->conditions);
if ($where !== '')
{
$result .= ' WHERE ' . $where;
}
}
// Return the final query string.
return $result;
}
/**

View file

@ -23,7 +23,7 @@ class VariableBase
public function getQueryStringAndParams(array $args, string $prefix = ''): array
{
// Return if this method is called on an invalid child class.
if (!isset($this->column) || !isset($this->operation))
if ((!isset($this->column) && !isset($this->name)) || !isset($this->operation))
{
throw new \Rhymix\Framework\Exceptions\QueryError('Invalid invocation of getQueryStringAndParams()');
}
@ -59,7 +59,7 @@ class VariableBase
}
// Quote the column name.
$column = Query::quoteName($this->column);
$column = Query::quoteName(isset($this->column) ? $this->column : $this->name);
// Prepare the target value.
$list_ops = array('in' => true, 'notin' => true, 'not_in' => true, 'between' => true);

View file

@ -113,7 +113,7 @@ class DBQueryParser
{
$column = new DBQuery\ColumnWrite;
$column->name = trim($tag['name']);
$column->name = trim($tag['operation']) ?: 'equal';
$column->operation = trim($tag['operation']) ?: 'equal';
$column->var = trim($tag['var']) ?: null;
$column->default = trim($tag['default']) ?: null;
$column->not_null = trim($tag['notnull'] ?: $tag['not-null']) !== '' ? true : false;