Misc fixes for compatibility with existing XML queries and schemas

This commit is contained in:
Kijin Sung 2020-06-30 14:43:42 +09:00
parent fa220fb1e1
commit 85df901924
7 changed files with 68 additions and 31 deletions

View file

@ -37,8 +37,9 @@ class DB
protected $_total_time = 0; protected $_total_time = 0;
/** /**
* Error codes. * Current query ID and error information.
*/ */
protected $_query_id = '';
protected $_errno = 0; protected $_errno = 0;
protected $_errstr = ''; protected $_errstr = '';
@ -270,10 +271,12 @@ class DB
$last_index = 0; $last_index = 0;
if ($query->requiresPagination()) if ($query->requiresPagination())
{ {
$this->_query_id = $query_id . ' (count)';
$output = $this->_executeCountQuery($query_id, $query, $args, $last_index); $output = $this->_executeCountQuery($query_id, $query, $args, $last_index);
if (!$output->toBool()) if (!$output->toBool())
{ {
$output->page_navigation = new \PageHandler(0, 0, 0); $output->page_navigation = new \PageHandler(0, 0, 0);
$this->_query_id = '';
$this->_total_time += (microtime(true) - $start_time); $this->_total_time += (microtime(true) - $start_time);
return $output; return $output;
} }
@ -284,6 +287,7 @@ class DB
$output->add('_query', $query_string); $output->add('_query', $query_string);
$output->add('_elapsed_time', '0.00000'); $output->add('_elapsed_time', '0.00000');
$output->page_navigation = new \PageHandler(0, 0, 0); $output->page_navigation = new \PageHandler(0, 0, 0);
$this->_query_id = '';
$this->_total_time += (microtime(true) - $start_time); $this->_total_time += (microtime(true) - $start_time);
return $output; return $output;
} }
@ -296,6 +300,7 @@ class DB
// Prepare and execute the main query. // Prepare and execute the main query.
try try
{ {
$this->_query_id = $query_id;
if (count($query_params)) if (count($query_params))
{ {
$this->_last_stmt = $this->_handle->prepare($query_string); $this->_last_stmt = $this->_handle->prepare($query_string);
@ -312,6 +317,7 @@ class DB
$output->add('_query', $query_string); $output->add('_query', $query_string);
$output->add('_elapsed_time', '0.00000'); $output->add('_elapsed_time', '0.00000');
$output->page_navigation = new \PageHandler(0, 0, 0); $output->page_navigation = new \PageHandler(0, 0, 0);
$this->_query_id = '';
$this->_total_time += (microtime(true) - $start_time); $this->_total_time += (microtime(true) - $start_time);
return $output; return $output;
} }
@ -322,14 +328,17 @@ class DB
} }
catch (Exceptions\DBError $e) catch (Exceptions\DBError $e)
{ {
$output = $this->setError(-1, $e->getMessage());
$output->add('_query', $query_string); $output->add('_query', $query_string);
$output->add('_elapsed_time', '0.00000'); $output->add('_elapsed_time', '0.00000');
$output->page_navigation = new \PageHandler(0, 0, 0); $output->page_navigation = new \PageHandler(0, 0, 0);
$this->_query_id = '';
$this->_total_time += (microtime(true) - $start_time); $this->_total_time += (microtime(true) - $start_time);
return $output; return $output;
} }
// Fill query information and result data in the output object. // Fill query information and result data in the output object.
$this->_query_id = '';
$this->_total_time += ($elapsed_time = microtime(true) - $start_time); $this->_total_time += ($elapsed_time = microtime(true) - $start_time);
$output->add('_query', $query_string); $output->add('_query', $query_string);
$output->add('_elapsed_time', sprintf('%0.5f', $elapsed_time)); $output->add('_elapsed_time', sprintf('%0.5f', $elapsed_time));
@ -476,7 +485,7 @@ class DB
{ {
$this->setError(-1, $e->getMessage()); $this->setError(-1, $e->getMessage());
} }
Debug::addQuery($this->getQueryLog('START TRANSACTION', '', 0)); Debug::addQuery($this->getQueryLog('START TRANSACTION', 0));
} }
$this->_transaction_level++; $this->_transaction_level++;
return $this->_transaction_level; return $this->_transaction_level;
@ -500,7 +509,7 @@ class DB
{ {
$this->setError(-1, $e->getMessage()); $this->setError(-1, $e->getMessage());
} }
Debug::addQuery($this->getQueryLog('ROLLBACK', '', 0)); Debug::addQuery($this->getQueryLog('ROLLBACK', 0));
} }
$this->_transaction_level--; $this->_transaction_level--;
return $this->_transaction_level; return $this->_transaction_level;
@ -524,7 +533,7 @@ class DB
{ {
$this->setError(-1, $e->getMessage()); $this->setError(-1, $e->getMessage());
} }
Debug::addQuery($this->getQueryLog('COMMIT', '', 0)); Debug::addQuery($this->getQueryLog('COMMIT', 0));
} }
$this->_transaction_level--; $this->_transaction_level--;
return $this->_transaction_level; return $this->_transaction_level;
@ -986,11 +995,10 @@ class DB
* Generate a query log entry. * Generate a query log entry.
* *
* @param string $query * @param string $query
* @param string $query_id
* @param float $elapsed_time * @param float $elapsed_time
* @return array * @return array
*/ */
public function getQueryLog(string $query, string $query_id, float $elapsed_time): array public function getQueryLog(string $query, float $elapsed_time): array
{ {
// Cache the debug status to improve performance. // Cache the debug status to improve performance.
static $debug_enabled = null; static $debug_enabled = null;
@ -1007,7 +1015,7 @@ class DB
// Compose the basic structure of the log entry. // Compose the basic structure of the log entry.
$result = array( $result = array(
'query' => $query, 'query' => $query,
'query_id' => $query_id, 'query_id' => $this->_query_id,
'connection' => $this->_type, 'connection' => $this->_type,
'elapsed_time' => sprintf('%0.5f', $elapsed_time), 'elapsed_time' => sprintf('%0.5f', $elapsed_time),
'result' => 'success', 'result' => 'success',
@ -1029,6 +1037,7 @@ class DB
{ {
$result['called_file'] = $backtrace[$no]['file']; $result['called_file'] = $backtrace[$no]['file'];
$result['called_line'] = $backtrace[$no]['line']; $result['called_line'] = $backtrace[$no]['line'];
$no++;
$result['called_method'] = $backtrace[$no]['class'] . $backtrace[$no]['type'] . $backtrace[$no]['function']; $result['called_method'] = $backtrace[$no]['class'] . $backtrace[$no]['type'] . $backtrace[$no]['function'];
$result['backtrace'] = array_slice($backtrace, $no, 1); $result['backtrace'] = array_slice($backtrace, $no, 1);
break; break;

View file

@ -355,7 +355,7 @@ class Debug
'query_id' => $query['query_id'], 'query_id' => $query['query_id'],
'query_connection' => $query['connection'], 'query_connection' => $query['connection'],
'query_string' => $query['query'], 'query_string' => $query['query'],
'query_time' => $query['elapsed_time'], 'query_time' => floatval($query['elapsed_time']),
'file' => $query['called_file'], 'file' => $query['called_file'],
'line' => $query['called_line'], 'line' => $query['called_line'],
'method' => $query['called_method'], 'method' => $query['called_method'],

View file

@ -33,6 +33,9 @@ class DBHelper extends \PDO
*/ */
public function prepare($statement, $driver_options = null) public function prepare($statement, $driver_options = null)
{ {
$start_time = microtime(true);
$db_class = DB::getInstance($this->_type);
try try
{ {
if ($driver_options) if ($driver_options)
@ -48,6 +51,10 @@ class DBHelper extends \PDO
} }
catch (\PDOException $e) catch (\PDOException $e)
{ {
$elapsed_time = microtime(true) - $start_time;
$db_class->addElapsedTime($elapsed_time);
$db_class->setError(-1, $e->getMessage());
Debug::addQuery($db_class->getQueryLog($statement, $elapsed_time));
throw new DBError($e->getMessage(), 0, $e); throw new DBError($e->getMessage(), 0, $e);
} }
@ -81,7 +88,7 @@ class DBHelper extends \PDO
$elapsed_time = microtime(true) - $start_time; $elapsed_time = microtime(true) - $start_time;
$db_class->addElapsedTime($elapsed_time); $db_class->addElapsedTime($elapsed_time);
Debug::addQuery($db_class->getQueryLog($statement, '', $elapsed_time)); Debug::addQuery($db_class->getQueryLog($statement, $elapsed_time));
return $stmt; return $stmt;
} }
@ -109,7 +116,7 @@ class DBHelper extends \PDO
$elapsed_time = microtime(true) - $start_time; $elapsed_time = microtime(true) - $start_time;
$db_class->addElapsedTime($elapsed_time); $db_class->addElapsedTime($elapsed_time);
Debug::addQuery($db_class->getQueryLog($query, '', $elapsed_time)); Debug::addQuery($db_class->getQueryLog($query, $elapsed_time));
return $result; return $result;
} }

View file

@ -42,7 +42,7 @@ class DBStmtHelper extends \PDOStatement
$elapsed_time = microtime(true) - $start_time; $elapsed_time = microtime(true) - $start_time;
$db_class->addElapsedTime($elapsed_time); $db_class->addElapsedTime($elapsed_time);
Debug::addQuery($db_class->getQueryLog($this->queryString, '', $elapsed_time)); Debug::addQuery($db_class->getQueryLog($this->queryString, $elapsed_time));
} }
catch (\PDOException $e) catch (\PDOException $e)
{ {
@ -50,7 +50,7 @@ class DBStmtHelper extends \PDOStatement
$elapsed_time = microtime(true) - $start_time; $elapsed_time = microtime(true) - $start_time;
$db_class->addElapsedTime($elapsed_time); $db_class->addElapsedTime($elapsed_time);
Debug::addQuery($db_class->getQueryLog($this->queryString, '', $elapsed_time)); Debug::addQuery($db_class->getQueryLog($this->queryString, $elapsed_time));
throw new DBError($e->getMessage(), 0, $e); throw new DBError($e->getMessage(), 0, $e);
} }

View file

@ -130,7 +130,7 @@ class Query extends VariableBase
elseif ($this->_column_list) elseif ($this->_column_list)
{ {
$result .= implode(', ', array_map(function($str) { $result .= implode(', ', array_map(function($str) {
return '`' . $str . '`'; return self::quoteName($str);
}, $this->_column_list)); }, $this->_column_list));
} }
else else
@ -233,7 +233,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); $tables = $this->_arrangeTables($this->tables, false);
if ($tables !== '') if ($tables !== '')
{ {
$result .= ' INTO ' . $tables; $result .= ' INTO ' . $tables;
@ -280,7 +280,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); $tables = $this->_arrangeTables($this->tables, false);
if ($tables !== '') if ($tables !== '')
{ {
$result .= $tables; $result .= $tables;
@ -326,7 +326,7 @@ class Query extends VariableBase
// Compose the FROM clause. // Compose the FROM clause.
if (count($this->tables)) if (count($this->tables))
{ {
$tables = $this->_arrangeTables($this->tables); $tables = $this->_arrangeTables($this->tables, false);
if ($tables !== '') if ($tables !== '')
{ {
$result .= ' FROM ' . $tables; $result .= ' FROM ' . $tables;
@ -363,9 +363,10 @@ class Query extends VariableBase
* Generate a FROM clause from a list of tables. * Generate a FROM clause from a list of tables.
* *
* @param array $tables * @param array $tables
* @param bool $use_aliases
* @return string * @return string
*/ */
protected function _arrangeTables(array $tables): string protected function _arrangeTables(array $tables, bool $use_aliases = true): string
{ {
// Initialize the result. // Initialize the result.
$result = array(); $result = array();
@ -376,7 +377,11 @@ class Query extends VariableBase
// Subquery // Subquery
if ($table instanceof self) if ($table instanceof self)
{ {
$tabledef = sprintf('(%s) AS `%s`', $table->getQueryString($this->_prefix, $this->_args), $table->alias); $tabledef = '(' . $table->getQueryString($this->_prefix, $this->_args) . ')';
if ($table->alias)
{
$tabledef .= ' AS `' . $table->alias . '`';
}
foreach ($table->getQueryParams() as $param) foreach ($table->getQueryParams() as $param)
{ {
$this->_params[] = $param; $this->_params[] = $param;
@ -386,7 +391,11 @@ class Query extends VariableBase
// Regular table // Regular table
else else
{ {
$tabledef = self::quoteName($this->_prefix . $table->name) . ($table->alias ? (' AS `' . $table->alias . '`') : ''); $tabledef = self::quoteName($this->_prefix . $table->name);
if ($use_aliases && $table->alias && $table->alias !== ($this->_prefix . $table->name))
{
$tabledef .= ' AS `' . $table->alias . '`';
}
} }
// Add join conditions // Add join conditions
@ -485,7 +494,11 @@ class Query extends VariableBase
} }
// Get the ordering (ASC or DESC). // Get the ordering (ASC or DESC).
if (isset($this->_args[$orderby->order_var])) if (preg_match('/^(ASC|DESC)$/i', $orderby->order_var, $matches))
{
$column_order = strtoupper($matches[1]);
}
elseif (isset($this->_args[$orderby->order_var]))
{ {
$column_order = preg_replace('/[^A-Z]/', '', strtoupper($this->_args[$orderby->order_var])); $column_order = preg_replace('/[^A-Z]/', '', strtoupper($this->_args[$orderby->order_var]));
} }

View file

@ -282,7 +282,7 @@ class VariableBase
{ {
return [true, Query::quoteName($this->default)]; return [true, Query::quoteName($this->default)];
} }
elseif (isset($this->column) && preg_match('/_srl$/', $this->column) && !ctype_digit($this->default)) elseif (isset($this->column) && preg_match('/_srl$/', $this->column) && !is_numeric($this->default))
{ {
return [true, Query::quoteName($this->default)]; return [true, Query::quoteName($this->default)];
} }

View file

@ -105,15 +105,16 @@ class DBQueryParser extends BaseParser
} }
else else
{ {
$attribs = self::_getAttributes($tag);
$column = new DBQuery\ColumnWrite; $column = new DBQuery\ColumnWrite;
$column->name = trim($tag['name']); $column->name = $attribs['name'];
$column->operation = trim($tag['operation']) ?: 'equal'; $column->operation = $attribs['operation'] ?: 'equal';
$column->var = trim($tag['var']) ?: null; $column->var = $attribs['var'] ?? null;
$column->default = trim($tag['default']) ?: null; $column->default = $attribs['default'] ?? null;
$column->not_null = self::_getAttributes($tag)['notnull'] ? true : false; $column->not_null = $attribs['notnull'] ? true : false;
$column->filter = trim($tag['filter']) ?: null; $column->filter = $attribs['filter'] ?? null;
$column->minlength = intval(trim($tag['minlength']), 10); $column->minlength = intval($attribs['minlength'], 10);
$column->maxlength = intval(trim($tag['maxlength']), 10); $column->maxlength = intval($attribs['maxlength'], 10);
$query->columns[] = $column; $query->columns[] = $column;
} }
} }
@ -215,8 +216,15 @@ class DBQueryParser extends BaseParser
$cond = new DBQuery\Condition; $cond = new DBQuery\Condition;
$cond->operation = $attribs['operation']; $cond->operation = $attribs['operation'];
$cond->column = $attribs['column']; $cond->column = $attribs['column'];
$cond->var = $attribs['var'] ?? null; if (isset($attribs['var']) && !isset($attribs['default']) && preg_match('/^\w+\.\w+$/', $attribs['var']))
$cond->default = $attribs['default'] ?? null; {
$cond->default = $attribs['var'];
}
else
{
$cond->var = $attribs['var'] ?? null;
$cond->default = $attribs['default'] ?? null;
}
$cond->not_null = $attribs['notnull'] ? true : false; $cond->not_null = $attribs['notnull'] ? true : false;
$cond->filter = $attribs['filter'] ?? null; $cond->filter = $attribs['filter'] ?? null;
$cond->minlength = intval($attribs['minlength'], 10); $cond->minlength = intval($attribs['minlength'], 10);