mirror of
https://github.com/Lastorder-DC/rhymix.git
synced 2026-01-04 17:21:39 +09:00
Create basic structure of XML query
This commit is contained in:
parent
c97b161e42
commit
20025077f7
11 changed files with 303 additions and 8 deletions
14
common/framework/parsers/dbquery/column.php
Normal file
14
common/framework/parsers/dbquery/column.php
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
<?php
|
||||
|
||||
namespace Rhymix\Framework\Parsers\DBQuery;
|
||||
|
||||
/**
|
||||
* Column class.
|
||||
*/
|
||||
class Column
|
||||
{
|
||||
public $name;
|
||||
public $alias;
|
||||
public $is_expression = false;
|
||||
public $is_wildcard = false;
|
||||
}
|
||||
16
common/framework/parsers/dbquery/condition.php
Normal file
16
common/framework/parsers/dbquery/condition.php
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
<?php
|
||||
|
||||
namespace Rhymix\Framework\Parsers\DBQuery;
|
||||
|
||||
/**
|
||||
* Condition class.
|
||||
*/
|
||||
class Condition extends GenericVar
|
||||
{
|
||||
public $operation;
|
||||
public $column;
|
||||
public $var;
|
||||
public $default;
|
||||
public $not_null;
|
||||
public $operator = 'AND';
|
||||
}
|
||||
12
common/framework/parsers/dbquery/conditiongroup.php
Normal file
12
common/framework/parsers/dbquery/conditiongroup.php
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
<?php
|
||||
|
||||
namespace Rhymix\Framework\Parsers\DBQuery;
|
||||
|
||||
/**
|
||||
* Condition Group class.
|
||||
*/
|
||||
class ConditionGroup
|
||||
{
|
||||
public $conditions = array();
|
||||
public $operator = 'AND';
|
||||
}
|
||||
12
common/framework/parsers/dbquery/genericvar.php
Normal file
12
common/framework/parsers/dbquery/genericvar.php
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
<?php
|
||||
|
||||
namespace Rhymix\Framework\Parsers\DBQuery;
|
||||
|
||||
/**
|
||||
* GenericVar class.
|
||||
*/
|
||||
class GenericVar
|
||||
{
|
||||
public $var;
|
||||
public $default;
|
||||
}
|
||||
12
common/framework/parsers/dbquery/groupby.php
Normal file
12
common/framework/parsers/dbquery/groupby.php
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
<?php
|
||||
|
||||
namespace Rhymix\Framework\Parsers\DBQuery;
|
||||
|
||||
/**
|
||||
* GroupBy class.
|
||||
*/
|
||||
class GroupBy
|
||||
{
|
||||
public $columns = array();
|
||||
public $having = array();
|
||||
}
|
||||
15
common/framework/parsers/dbquery/navigation.php
Normal file
15
common/framework/parsers/dbquery/navigation.php
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
<?php
|
||||
|
||||
namespace Rhymix\Framework\Parsers\DBQuery;
|
||||
|
||||
/**
|
||||
* Navigation class.
|
||||
*/
|
||||
class Navigation
|
||||
{
|
||||
public $orderby = array();
|
||||
public $list_count;
|
||||
public $page_count;
|
||||
public $page;
|
||||
public $offset;
|
||||
}
|
||||
14
common/framework/parsers/dbquery/orderby.php
Normal file
14
common/framework/parsers/dbquery/orderby.php
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
<?php
|
||||
|
||||
namespace Rhymix\Framework\Parsers\DBQuery;
|
||||
|
||||
/**
|
||||
* OrderBy class.
|
||||
*/
|
||||
class OrderBy extends GenericVar
|
||||
{
|
||||
public $var;
|
||||
public $default;
|
||||
public $order_var;
|
||||
public $order_default = 'ASC';
|
||||
}
|
||||
18
common/framework/parsers/dbquery/query.php
Normal file
18
common/framework/parsers/dbquery/query.php
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
<?php
|
||||
|
||||
namespace Rhymix\Framework\Parsers\DBQuery;
|
||||
|
||||
/**
|
||||
* Query class.
|
||||
*/
|
||||
class Query
|
||||
{
|
||||
public $name;
|
||||
public $alias;
|
||||
public $type;
|
||||
public $tables = array();
|
||||
public $columns = array();
|
||||
public $conditions = array();
|
||||
public $groupby = null;
|
||||
public $navigation = null;
|
||||
}
|
||||
14
common/framework/parsers/dbquery/table.php
Normal file
14
common/framework/parsers/dbquery/table.php
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
<?php
|
||||
|
||||
namespace Rhymix\Framework\Parsers\DBQuery;
|
||||
|
||||
/**
|
||||
* Table class.
|
||||
*/
|
||||
class Table
|
||||
{
|
||||
public $name;
|
||||
public $alias;
|
||||
public $join_type;
|
||||
public $join_conditions = array();
|
||||
}
|
||||
|
|
@ -2,8 +2,6 @@
|
|||
|
||||
namespace Rhymix\Framework\Parsers;
|
||||
|
||||
use Rhymix\Framework\Storage;
|
||||
|
||||
/**
|
||||
* DB query parser class for XE compatibility.
|
||||
*/
|
||||
|
|
@ -23,5 +21,177 @@ class DBQueryParser
|
|||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Parse the query.
|
||||
$query_name = preg_replace('/\.xml$/', '', basename($filename));
|
||||
$query = self::_parseQuery($xml, $query_name);
|
||||
|
||||
return $query;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a query.
|
||||
*
|
||||
* @param SimpleXMLElement $xml
|
||||
* @param string $name
|
||||
* @return object
|
||||
*/
|
||||
protected static function _parseQuery(\SimpleXMLElement $xml, string $name = ''): DBQuery\Query
|
||||
{
|
||||
// Load basic information about this query.
|
||||
$query = new DBQuery\Query;
|
||||
$query->name = $name ?: null;
|
||||
$query->alias = trim($xml['alias']) ?: null;
|
||||
if ($query->alias && !$query->name)
|
||||
{
|
||||
$query->name = $query->alias;
|
||||
}
|
||||
$query->type = trim($xml['action']);
|
||||
|
||||
// Load tables.
|
||||
foreach ($xml->tables->table as $tag)
|
||||
{
|
||||
if (trim($tag['query']) === 'true')
|
||||
{
|
||||
$table = self::_parseQuery($tag);
|
||||
$query->tables[$table->alias] = $table;
|
||||
}
|
||||
else
|
||||
{
|
||||
$table = new DBQuery\Table;
|
||||
$table->name = trim($tag['name']);
|
||||
$table->alias = trim($tag['alias']) ?: $table->name;
|
||||
$table_type = trim($tag['type']);
|
||||
if (stripos($table_type, 'join') !== false)
|
||||
{
|
||||
$table->join_type = strtoupper($table_type);
|
||||
if ($tag->conditions)
|
||||
{
|
||||
$table->join_conditions = self::_parseConditions($tag->conditions);
|
||||
}
|
||||
}
|
||||
$query->tables[$table->alias] = $table;
|
||||
}
|
||||
}
|
||||
|
||||
// Load columns.
|
||||
foreach ($xml->columns->column as $tag)
|
||||
{
|
||||
$column = new DBQuery\Column;
|
||||
$column->name = trim($tag['name']);
|
||||
$column->alias = trim($tag['alias']) ?: null;
|
||||
if ($column->name === '*' || preg_match('/\.\*$/', $column->name))
|
||||
{
|
||||
$column->is_wildcard = true;
|
||||
}
|
||||
if (!self::_isValidColumnName($column->name))
|
||||
{
|
||||
$column->is_expression = true;
|
||||
}
|
||||
$query->columns[] = $column;
|
||||
}
|
||||
|
||||
// Load conditions.
|
||||
if ($xml->conditions)
|
||||
{
|
||||
$query->conditions = self::_parseConditions($xml->conditions);
|
||||
}
|
||||
|
||||
// Load groups.
|
||||
if ($xml->groups)
|
||||
{
|
||||
$query->groupby = new DBQuery\GroupBy;
|
||||
foreach ($xml->groups->children() as $tag)
|
||||
{
|
||||
$name = $tag->getName();
|
||||
if ($name === 'group')
|
||||
{
|
||||
$query->groupby->columns[] = trim($tag['column']);
|
||||
}
|
||||
elseif ($name === 'having')
|
||||
{
|
||||
$query->groupby->having = self::_parseConditions($tag);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Load navigation settings.
|
||||
if ($xml->navigation)
|
||||
{
|
||||
$query->navigation = new DBQuery\Navigation;
|
||||
foreach ($xml->navigation->index as $tag)
|
||||
{
|
||||
$orderby = new DBQuery\OrderBy;
|
||||
$orderby->var = trim($tag['var']) ?: null;
|
||||
$orderby->default = trim($tag['default']) ?: null;
|
||||
$orderby->order_var = trim($tag['order']) ?: null;
|
||||
$query->navigation->orderby[] = $orderby;
|
||||
}
|
||||
foreach (['list_count', 'page_count', 'page', 'offset'] as $key)
|
||||
{
|
||||
if ($tag = $xml->navigation->{$key})
|
||||
{
|
||||
$query->navigation->{$key} = new DBQuery\GenericVar;
|
||||
$query->navigation->{$key}->var = trim($tag['var']) ?: null;
|
||||
$query->navigation->{$key}->default = trim($tag['default']) ?: null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Return the complete query definition.
|
||||
return $query;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse conditions.
|
||||
*
|
||||
* @param SimpleXMLElement $parent
|
||||
* @return array
|
||||
*/
|
||||
protected static function _parseConditions(\SimpleXMLElement $parent): array
|
||||
{
|
||||
$result = array();
|
||||
foreach ($parent->children() as $tag)
|
||||
{
|
||||
$name = $tag->getName();
|
||||
if ($name === 'condition')
|
||||
{
|
||||
$cond = new DBQuery\Condition;
|
||||
$cond->operation = trim($tag['operation']);
|
||||
$cond->column = trim($tag['column']);
|
||||
$cond->var = trim($tag['var']);
|
||||
$cond->default = trim($tag['default']);
|
||||
$cond->not_null = trim($tag['notnull'] ?: $tag['not-null']) !== '' ? true : false;
|
||||
$cond->operator = strtoupper($tag['pipe']) ?: 'AND';
|
||||
$result[] = $cond;
|
||||
}
|
||||
elseif ($name === 'group')
|
||||
{
|
||||
$group = new DBQuery\ConditionGroup;
|
||||
$group->conditions = self::_parseConditions($tag);
|
||||
$group->operator = strtoupper($tag['pipe']) ?: 'AND';
|
||||
$result[] = $group;
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if an expression might be a valid column name.
|
||||
*
|
||||
* @param string $column_name
|
||||
* @return bool
|
||||
*/
|
||||
protected static function _isValidColumnName(string $column_name): bool
|
||||
{
|
||||
if (preg_match('/^[a-z0-9_]+(?:\.[a-z0-9_]+)*$/', $column_name))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,8 +2,6 @@
|
|||
|
||||
namespace Rhymix\Framework\Parsers;
|
||||
|
||||
use Rhymix\Framework\Storage;
|
||||
|
||||
/**
|
||||
* DB table parser class for XE compatibility.
|
||||
*/
|
||||
|
|
@ -35,10 +33,6 @@ class DBTableParser
|
|||
*/
|
||||
public static function loadXML(string $filename)
|
||||
{
|
||||
// Initialize table definition.
|
||||
$table = new DBTable\Table;
|
||||
$table->name = preg_replace('/\.xml$/', '', basename($filename));
|
||||
|
||||
// Load the XML file.
|
||||
$xml = simplexml_load_string(file_get_contents($filename));
|
||||
if ($xml === false)
|
||||
|
|
@ -46,6 +40,10 @@ class DBTableParser
|
|||
return false;
|
||||
}
|
||||
|
||||
// Initialize table definition.
|
||||
$table = new DBTable\Table;
|
||||
$table->name = preg_replace('/\.xml$/', '', basename($filename));
|
||||
|
||||
// Load columns.
|
||||
foreach ($xml->column as $column_info)
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue