Source
<?php
/*
** Copyright (C) 2001-2025 Zabbix SIA
**
** This program is free software: you can redistribute it and/or modify it under the terms of
** the GNU Affero General Public License as published by the Free Software Foundation, version 3.
**
** This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
** without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
** See the GNU Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License along with this program.
** If not, see <https://www.gnu.org/licenses/>.
**/
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]);
$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':