Import DB parser classes from previous private project

This commit is contained in:
Kijin Sung 2020-06-22 11:00:18 +09:00
parent 653fc532f5
commit 59f14d8a3f
6 changed files with 231 additions and 0 deletions

View file

@ -0,0 +1,27 @@
<?php
namespace Rhymix\Framework\Parsers;
use Rhymix\Framework\Storage;
/**
* DB query parser class for XE compatibility.
*/
class DBQueryParser
{
/**
* Load a query XML file.
*
* @param string $filename
* @return object|false
*/
public static function loadXML($filename)
{
// Load the XML file.
$xml = simplexml_load_file($filename);
if ($xml === false)
{
return false;
}
}
}

View file

@ -0,0 +1,18 @@
<?php
namespace Rhymix\Framework\Parsers\DBTable;
/**
* Column class.
*/
class Column
{
public $name;
public $type;
public $size;
public $utf8mb4 = true;
public $default_value;
public $not_null = false;
public $is_primary_key = false;
public $auto_increment = false;
}

View file

@ -0,0 +1,15 @@
<?php
namespace Rhymix\Framework\Parsers\DBTable;
/**
* Constraint class.
*/
class Constraint
{
public $type;
public $column;
public $references;
public $on_update;
public $on_delete;
}

View file

@ -0,0 +1,13 @@
<?php
namespace Rhymix\Framework\Parsers\DBTable;
/**
* Index class.
*/
class Index
{
public $name;
public $columns = array();
public $is_unique = false;
}

View file

@ -0,0 +1,15 @@
<?php
namespace Rhymix\Framework\Parsers\DBTable;
/**
* Table class.
*/
class Table
{
public $name;
public $columns = array();
public $indexes = array();
public $primary_key = array();
public $constraints = array();
}

View file

@ -0,0 +1,143 @@
<?php
namespace Rhymix\Framework\Parsers;
use Rhymix\Framework\Storage;
/**
* DB table parser class for XE compatibility.
*/
class DBTableParser
{
/**
* Load a table definition XML file.
*
* @param string $filename
* @return object|false
*/
public static function loadXML($filename)
{
// Initialize table definition.
$table = new DBTable\Table;
$table->name = preg_replace('/\.xml$/', '', basename($filename));
// Load the XML file.
$xml = simplexml_load_file($filename);
if ($xml === false)
{
return false;
}
// Load columns.
foreach ($xml->column as $column_info)
{
// Get the column name and type.
$column = new DBTable\Column;
$column->name = strval($column_info['name']);
$column->type = strval($column_info['type']);
// Get the size.
if (preg_match('/^([a-z0-9_]+)\(([0-9,\s]+)\)$/i', $column->type, $matches))
{
$column->type = $matches[1];
$column->size = $matches[2];
}
if (isset($column_info['size']))
{
$column->size = strval($column_info['size']);
}
$column->size = implode(',', array_map('trim', explode(',', $column->size))) ?: null;
// Get the utf8mb4 attribute.
if (isset($column_info['utf8mb4']))
{
$column->utf8mb4 = toBool(strval($column_info['utf8mb4']));
}
// Get the default value.
if (isset($column_info['default']))
{
$column->default_value = strval($column_info['default']);
}
// Get the NOT NULL attribute.
if (isset($column_info['notnull']) || isset($column_info['not-null']))
{
$attr = strval($column_info['notnull'] ?: $column_info['not-null']);
$column->not_null = ($attr === 'notnull' || $attr === 'not-null' || toBool($attr));
}
// Get index information.
if (isset($column_info['index']))
{
$index_name = strval($column_info['index']);
if (!isset($table->indexes[$index_name]))
{
$table->indexes[$index_name] = new DBTable\Index;
$table->indexes[$index_name]->name = $index_name;
}
$table->indexes[$index_name]->columns[] = $column->name;
}
if (isset($column_info['unique']))
{
$index_name = strval($column_info['unique']);
if (!isset($table->indexes[$index_name]))
{
$table->indexes[$index_name] = new DBTable\Index;
$table->indexes[$index_name]->name = $index_name;
$table->indexes[$index_name]->is_unique = true;
}
$table->indexes[$index_name]->columns[] = $column->name;
}
// Get primary key information.
if (isset($column_info['primary_key']) || isset($column_info['primary-key']))
{
$attr = strval($column_info['primary_key'] ?: $column_info['primary-key']);
if ($attr === 'primary_key' || $attr === 'primary-key' || toBool($attr))
{
$table->primary_key[] = $column->name;
$column->is_primary_key = true;
}
}
// Get auto-increment information.
if (isset($column_info['auto_increment']) || isset($column_info['auto-increment']))
{
$attr = strval($column_info['auto_increment'] ?: $column_info['auto-increment']);
if ($attr === 'auto_increment' || $attr === 'auto-increment' || toBool($attr))
{
$column->auto_increment = true;
}
}
// Add the column to the table definition.
$table->columns[$column->name] = $column;
}
// Load indexes.
foreach ($xml->index as $index_info)
{
$index = new DBTable\Index;
$index->name = strval($index_info['name']);
$index->columns = array_map('trim', explode(',', strval($index_info['columns'])));
$index->is_unique = ($index_info['unique'] === 'unique' || toBool(strval($index_info['unique'])));
$table->indexes[$index->name] = $index;
}
// Load other constraints (foreign keys).
foreach ($xml->constraint as $const_info)
{
$const = new DBTable\Constraint;
$const->type = strtolower($const_info['type']);
$const->column = strval($const_info['column']);
$const->references = strval($const_info['references']);
$const->on_update = strtolower($const_info['on_update'] ?: $const_info['on-update']);
$const->on_delete = strtolower($const_info['on_delete'] ?: $const_info['on-delete']);
$table->constraints[] = $const;
}
// Return the complete table definition.
return $table;
}
}