Source
<?php
define('DEFAULT_SRC_FILE', dirname(__FILE__).'/../src/schema.tmpl');
define('DEFAULT_DEST_FILE', dirname(__FILE__).'/../src/schema.inc.php');
function parse_schema($path) {
$schema = [];
$lines = file($path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach ($lines as $line) {
$str = explode('|', $line, 2);
$part = trim($str[0]);
$rest_line = isset($str[1]) ? $str[1] : '';
switch (trim($part)) {
case 'TABLE':
$str = explode('|', $rest_line);
$table = trim($str[0]);
$key = trim($str[1]);
$schema[$table] = ['key' => $key, 'fields' => []];
break;
case 'FIELD':
$str = explode('|', $rest_line);
$field = trim($str[0]);
$type = trim($str[1]);
if ($table === 'triggers' && $field === 'description' && $type === 't_shorttext') {
$type = 't_varchar(255)';
}
$default = trim($str[2]);
$null = trim($str[3]);
$ref_table = isset($str[6]) ? trim($str[6]) : null;
$ref_field = isset($str[7]) ? trim($str[7]) : null;
preg_match('/(?<type>[a-z_]+)(?:\((?<length>[0-9]+)\))?/', $type, $type_data);
switch ($type_data['type']) {
case 't_integer':
case 't_nanosec':
case 't_time':
$type = 'DB::FIELD_TYPE_INT';
$length = 10;
break;
case 't_id':
$type = 'DB::FIELD_TYPE_ID';
$length = 20;
break;
case 't_bigint':
case 't_serial':
$type = 'DB::FIELD_TYPE_UINT';
$length = 20;
break;
case 't_double':
$type = 'DB::FIELD_TYPE_FLOAT';
$length = false;
break;
case 't_varchar':
$type = 'DB::FIELD_TYPE_CHAR';
$length = $type_data['length'];
break;
case 't_text':
case 't_longtext':
$type = 'DB::FIELD_TYPE_NCLOB';
$length = false;