format schema class (#7145)

- format schema class using autoformat
This commit is contained in:
frytimo 2025-01-28 20:05:00 -04:00 committed by GitHub
parent 77f9161408
commit d6f9b25283
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 799 additions and 810 deletions

View File

@ -1,4 +1,5 @@
<?php
/*
FusionPBX
Version: MPL 1.1
@ -26,6 +27,7 @@
//define the schema class
if (!class_exists('schema')) {
class schema {
//define variables
@ -50,8 +52,7 @@ if (!class_exists('schema')) {
foreach ($config_list as $config_path) {
try {
include($config_path);
}
catch (Exception $e) {
} catch (Exception $e) {
//echo 'Caught exception: ', $e->getMessage(), "\n";
}
$x++;
@ -73,19 +74,18 @@ if (!class_exists('schema')) {
foreach ($row['fields'] as $field) {
if (!empty($field['deprecated']) and ($field['deprecated'] == "true")) {
//skip this field
} else {
if ($field_count > 0) {
$sql .= ",\n";
}
else {
if ($field_count > 0 ) { $sql .= ",\n"; }
if (is_array($field['name'])) {
$sql .= $field['name']['text'] . " ";
}
else {
} else {
$sql .= $field['name'] . " ";
}
if (is_array($field['type'])) {
$sql .= $field['type'][$this->db_type];
}
else {
} else {
$sql .= $field['type'];
}
if (isset($field['key']) && isset($field['key']['type']) && ($field['key']['type'] == "primary")) {
@ -107,8 +107,7 @@ if (!class_exists('schema')) {
}
if ($this->db_type == "mysql") {
$sql .= ") ENGINE=INNODB;";
}
else {
} else {
$sql .= ");";
}
$this->result['sql'][] = $sql;
@ -126,8 +125,7 @@ if (!class_exists('schema')) {
//execute the sql query
try {
$this->database->query($sql);
}
catch (PDOException $error) {
} catch (PDOException $error) {
echo "error: " . $error->getMessage() . " sql: $sql<br/>";
}
//complete the transaction
@ -152,8 +150,7 @@ if (!class_exists('schema')) {
$table_info = $this->table_info($db_name, $table_name);
if ($this->sqlite_column_exists($table_info, $column_name)) {
return true;
}
else {
} else {
return false;
}
}
@ -174,8 +171,7 @@ if (!class_exists('schema')) {
}
if (count($result) > 0) {
return true;
}
else {
} else {
return false;
}
unset($prep_statement);
@ -184,7 +180,9 @@ if (!class_exists('schema')) {
//get the table information
public function table_info($db_name, $table_name) {
if (empty($table_name)) { return false; }
if (empty($table_name)) {
return false;
}
if ($this->db_type == "sqlite") {
$sql = "PRAGMA table_info(" . $table_name . ");";
}
@ -215,8 +213,7 @@ if (!class_exists('schema')) {
$result = $this->database->query($sql);
if ($result > 0) {
return true; //table exists
}
else {
} else {
return false; //table doesn't exist
}
}
@ -238,15 +235,16 @@ if (!class_exists('schema')) {
$result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
if (count($result) > 0) {
return true; //table exists
}
else {
} else {
return false; //table doesn't exist
}
}
//database table information
private function db_table_info($db_name, $db_type, $table_name) {
if (empty($table_name)) { return false; }
if (empty($table_name)) {
return false;
}
if ($db_type == "sqlite") {
$sql = "PRAGMA table_info(" . $table_name . ");";
}
@ -313,8 +311,7 @@ if (!class_exists('schema')) {
$table_info = $this->db_table_info($db_name, $db_type, $table_name);
if ($this->db_sqlite_column_exists($table_info, $column_name)) {
return true;
}
else {
} else {
return false;
}
}
@ -331,8 +328,7 @@ if (!class_exists('schema')) {
$result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
if (!empty($result)) {
return true;
}
else {
} else {
return false;
}
unset($prep_statement);
@ -347,34 +343,37 @@ if (!class_exists('schema')) {
//database create table
public function db_create_table($apps, $db_type, $table) {
if (empty($apps)) { return false; }
if (is_array($apps)) foreach ($apps as $x => $app) {
if (!empty($app['db']) && is_array($app['db'])) foreach ($app['db'] as $y => $row) {
if (empty($apps)) {
return false;
}
if (is_array($apps)) {
foreach ($apps as $x => $app) {
if (!empty($app['db']) && is_array($app['db'])) {
foreach ($app['db'] as $y => $row) {
if (!empty($row['table']['name']) && is_array($row['table']['name'])) {
$table_name = $row['table']['name']['text'];
}
else {
} else {
$table_name = $row['table']['name'];
}
if ($table_name == $table) {
$sql = "CREATE TABLE " . $table_name . " (\n";
(int) $field_count = 0;
if (!empty($row['fields']) && is_array($row['fields'])) foreach ($row['fields'] as $field) {
if (!empty($row['fields']) && is_array($row['fields'])) {
foreach ($row['fields'] as $field) {
if (!empty($field['deprecated']) && $field['deprecated'] == "true") {
//skip this row
} else {
if ($field_count > 0) {
$sql .= ",\n";
}
else {
if ($field_count > 0 ) { $sql .= ",\n"; }
if (!empty($field['name']) && is_array($field['name'])) {
$sql .= $field['name']['text'] . " ";
}
else {
} else {
$sql .= $field['name'] . " ";
}
if (!empty($field['type']) && is_array($field['type'])) {
$sql .= $field['type'][$db_type];
}
else {
} else {
$sql .= $field['type'];
}
if (!empty($field['key']['type']) && $field['key']['type'] == "primary") {
@ -383,12 +382,15 @@ if (!class_exists('schema')) {
$field_count++;
}
}
}
$sql .= ");\n";
return $sql;
}
}
}
}
}
}
//database insert
private function db_insert_into($apps, $db_type, $table) {
@ -401,13 +403,13 @@ if (!class_exists('schema')) {
foreach ($row['fields'] as $field) {
if (!empty($field['deprecated']) && $field['deprecated'] == "true") {
//skip this field
} else {
if ($field_count > 0) {
$sql .= ",";
}
else {
if ($field_count > 0 ) { $sql .= ","; }
if (is_array($field['name'])) {
$sql .= $field['name']['text'];
}
else {
} else {
$sql .= $field['name'];
}
$field_count++;
@ -419,9 +421,10 @@ if (!class_exists('schema')) {
foreach ($row['fields'] as $field) {
if (!empty($field['deprecated']) && $field['deprecated'] == "true") {
//skip this field
} else {
if ($field_count > 0) {
$sql .= ",";
}
else {
if ($field_count > 0 ) { $sql .= ","; }
if (is_array($field['name'])) {
if ($field['exists'] == "false") {
if (is_array($field['name']['deprecated'])) {
@ -433,22 +436,20 @@ if (!class_exists('schema')) {
break;
}
}
if (!$found) { $sql .= "''"; }
if (!$found) {
$sql .= "''";
}
else {
} else {
if ($this->db_column_exists($db_type, $db_name, 'tmp_' . $table, $field['name']['deprecated'])) {
$sql .= $field['name']['deprecated'];
}
else {
} else {
$sql .= "''";
}
}
}
else {
} else {
$sql .= $field['name']['text'];
}
}
else {
} else {
$sql .= $field['name'];
}
$field_count++;
@ -467,7 +468,9 @@ if (!class_exists('schema')) {
//set the global variable
global $text, $output_format;
if ($format == '') $format = $output_format;
if ($format == '') {
$format = $output_format;
}
//get the db variables
//require_once "resources/classes/config.php";
@ -482,7 +485,6 @@ if (!class_exists('schema')) {
//$db_host = $config->db_host;
//$db_path = $config->db_path;
//$db_port = $config->db_port;
//includes files
require dirname(__DIR__, 2) . "/resources/require.php";
@ -499,7 +501,6 @@ if (!class_exists('schema')) {
// SELECT * FROM sqlite_master WHERE type='table' AND name='v_cdr' AND sql LIKE '%caller_id_name TEXT,%'
//aditional information
// http://www.sqlite.org/faq.html#q9
//postgresql
//list all tables in the database
// SELECT table_name FROM pg_tables WHERE schemaname='public';
@ -517,25 +518,22 @@ if (!class_exists('schema')) {
//oracle
//check if table exists
// SELECT TABLE_NAME FROM ALL_TABLES
//update the app db array add exists true or false
$sql = '';
foreach ($this->apps as $x => $app) {
if (isset($app['db'])) foreach ($app['db'] as $y => $row) {
if (isset($app['db'])) {
foreach ($app['db'] as $y => $row) {
if (isset($row['table']['name'])) {
if (is_array($row['table']['name'])) {
$table_name = $row['table']['name']['text'];
}
else {
} else {
$table_name = $row['table']['name'];
}
}
else {
} else {
//old array syntax
if (is_array($row['table'])) {
$table_name = $row['table']['text'];
}
else {
} else {
$table_name = $row['table'];
}
}
@ -544,28 +542,24 @@ if (!class_exists('schema')) {
//check if the table exists
if ($this->db_table_exists($db_type, $db_name, $table_name)) {
$this->apps[$x]['db'][$y]['exists'] = 'true';
}
else {
} else {
$this->apps[$x]['db'][$y]['exists'] = 'false';
}
//check if the column exists
foreach ($row['fields'] as $z => $field) {
if (!empty($field['deprecated']) && $field['deprecated'] == "true") {
//skip this field
}
else {
} else {
if (is_array($field['name'])) {
$field_name = $field['name']['text'];
}
else {
} else {
$field_name = $field['name'];
}
if (!empty($field_name)) {
if ($this->db_column_exists($db_type, $db_name, $table_name, $field_name)) {
//found
$this->apps[$x]['db'][$y]['fields'][$z]['exists'] = 'true';
}
else {
} else {
//not found
$this->apps[$x]['db'][$y]['fields'][$z]['exists'] = 'false';
}
@ -577,13 +571,15 @@ if (!class_exists('schema')) {
}
}
}
}
//prepare the variables
$sql_update = '';
//add missing tables and fields
foreach ($this->apps as $x => $app) {
if (isset($app['db'])) foreach ($app['db'] as $y => $row) {
if (isset($app['db'])) {
foreach ($app['db'] as $y => $row) {
if (is_array($row['table']['name'])) {
$table_name = $row['table']['name']['text'];
if ($this->db_table_exists($db_type, $db_name, $row['table']['name']['deprecated'])) {
@ -597,22 +593,18 @@ if (!class_exists('schema')) {
if ($db_type == "sqlite") {
$sql_update .= "ALTER TABLE " . $row['table']['name']['deprecated'] . " RENAME TO " . $row['table']['name']['text'] . ";\n";
}
}
else {
} else {
if ($this->db_table_exists($db_type, $db_name, $row['table']['name']['text'])) {
$row['exists'] = "true";
}
else {
} else {
$row['exists'] = "false";
$sql_update .= $this->db_create_table($this->apps, $db_type, $row['table']['name']['text']);
}
}
}
else {
} else {
if ($this->db_table_exists($db_type, $db_name, $row['table']['name'])) {
$row['exists'] = "true";
}
else {
} else {
$row['exists'] = "false";
}
$table_name = $row['table']['name'];
@ -624,20 +616,17 @@ if (!class_exists('schema')) {
foreach ($row['fields'] as $z => $field) {
if (!empty($field['deprecated']) && $field['deprecated'] == "true") {
//skip this field
}
else {
} else {
//get the data type
if (is_array($field['type'])) {
$field_type = $field['type'][$db_type];
}
else {
} else {
$field_type = $field['type'];
}
//get the field name
if (is_array($field['name'])) {
$field_name = $field['name']['text'];
}
else {
} else {
$field_name = $field['name'];
}
@ -648,7 +637,6 @@ if (!class_exists('schema')) {
// else {
// $field['exists'] = "false";
// }
//add or rename fields
if (isset($field['name']['deprecated']) && $this->db_column_exists($db_type, $db_name, $table_name, $field['name']['deprecated'])) {
if ($db_type == "pgsql") {
@ -662,8 +650,7 @@ if (!class_exists('schema')) {
//a change has been made to the field name
$this->apps[$x]['db'][$y]['rebuild'] = 'true';
}
}
else {
} else {
//find missing fields and add them
if ($field['exists'] == "false") {
$sql_update .= "ALTER TABLE " . $table_name . " ADD " . $field_name . " " . $field_type . ";\n";
@ -682,23 +669,33 @@ if (!class_exists('schema')) {
$sql_update .= "ALTER TABLE " . $table_name . " ALTER COLUMN " . $field_name . " TYPE uuid USING\n";
$sql_update .= "CAST(regexp_replace(" . $field_name . ", '([A-Z0-9]{4})([A-Z0-9]{12})', E'\\1-\\2')\n";
$sql_update .= "AS uuid);\n";
}
else {
} else {
//field type has not changed
if ($db_field_type == "integer" && strtolower($field_type) == "serial") { }
else if ($db_field_type == "timestamp without time zone" && strtolower($field_type) == "timestamp") { }
else if ($db_field_type == "timestamp without time zone" && strtolower($field_type) == "datetime") { }
else if ($db_field_type == "timestamp with time zone" && strtolower($field_type) == "timestamptz") { }
else if ($db_field_type == "integer" && strtolower($field_type) == "numeric") { }
else if ($db_field_type == "character" && strtolower($field_type) == "char") { }
if ($db_field_type == "integer" && strtolower($field_type) == "serial") {
} else if ($db_field_type == "timestamp without time zone" && strtolower($field_type) == "timestamp") {
} else if ($db_field_type == "timestamp without time zone" && strtolower($field_type) == "datetime") {
} else if ($db_field_type == "timestamp with time zone" && strtolower($field_type) == "timestamptz") {
} else if ($db_field_type == "integer" && strtolower($field_type) == "numeric") {
} else if ($db_field_type == "character" && strtolower($field_type) == "char") {
}
//field type has changed
else {
switch ($field_type) {
case 'numeric': $using = $field_name."::numeric"; break;
case 'numeric': $using = $field_name . "::numeric";
break;
case 'timestamp':
case 'datetime': $using = $field_name."::timestamp without time zone"; break;
case 'timestamptz': $using = $field_name."::timestamp with time zone"; break;
case 'boolean': $using = $field_name."::boolean"; break;
case 'datetime': $using = $field_name . "::timestamp without time zone";
break;
case 'timestamptz': $using = $field_name . "::timestamp with time zone";
break;
case 'boolean': $using = $field_name . "::boolean";
break;
default: unset($using);
}
$sql_update .= "ALTER TABLE " . $table_name . " ALTER COLUMN " . $field_name . " TYPE " . $field_type . " " . ($using ? "USING " . $using : null) . ";\n";
@ -709,11 +706,9 @@ if (!class_exists('schema')) {
$type = explode("(", $db_field_type);
if ($type[0] == $field_type) {
//do nothing
}
else if ($field_type == "numeric" && $type[0] == "decimal") {
} else if ($field_type == "numeric" && $type[0] == "decimal") {
//do nothing
}
else {
} else {
$sql_update .= "ALTER TABLE " . $table_name . " modify " . $field_name . " " . $field_type . ";\n";
}
unset($type);
@ -727,20 +722,20 @@ if (!class_exists('schema')) {
}
}
}
}
elseif (!is_array($row['table']['name'])) {
} elseif (!is_array($row['table']['name'])) {
//create table
$sql_update .= $this->db_create_table($this->apps, $db_type, $row['table']['name']);
}
}
}
}
//rebuild and populate the table
foreach ($this->apps as $x => $app) {
if (isset($app['db'])) foreach ($app['db'] as $y => $row) {
if (isset($app['db'])) {
foreach ($app['db'] as $y => $row) {
if (is_array($row['table']['name'])) {
$table_name = $row['table']['name']['text'];
}
else {
} else {
$table_name = $row['table']['name'];
}
if (!empty($field['rebuild']) && $row['rebuild'] == "true") {
@ -761,6 +756,7 @@ if (!class_exists('schema')) {
}
}
}
}
// initialize response variable
$response = '';
@ -793,11 +789,11 @@ if (!class_exists('schema')) {
//build the html while looping through the app db array
$sql = '';
foreach ($this->apps as $app) {
if (isset($app['db'])) foreach ($app['db'] as $row) {
if (isset($app['db'])) {
foreach ($app['db'] as $row) {
if (is_array($row['table']['name'])) {
$table_name = $row['table']['name']['text'];
}
else {
} else {
$table_name = $row['table']['name'];
}
$response .= "<tr>\n";
@ -819,18 +815,15 @@ if (!class_exists('schema')) {
foreach ($row['fields'] as $field) {
if (!empty($field['deprecated']) && $field['deprecated'] == "true") {
//skip this field
}
else {
} else {
if (is_array($field['name'])) {
$field_name = $field['name']['text'];
}
else {
} else {
$field_name = $field['name'];
}
if (is_array($field['type'])) {
$field_type = $field['type'][$db_type];
}
else {
} else {
$field_type = $field['type'];
}
$response .= "<tr>\n";
@ -839,8 +832,7 @@ if (!class_exists('schema')) {
if ($field['exists'] == "true") {
$response .= "<td class='row_style0' style=''>" . $text['option-true'] . "</td>\n";
$response .= "<td>&nbsp;</td>\n";
}
else {
} else {
$response .= "<td class='row_style1' style='background-color:#444444;color:#CCCCCC;'>" . $text['option-false'] . "</td>\n";
$response .= "<td>&nbsp;</td>\n";
}
@ -850,8 +842,7 @@ if (!class_exists('schema')) {
$response .= " </table>\n";
$response .= "</td>\n";
}
}
else {
} else {
$response .= "<td valign='top' class='row_style1'>$table_name</td>\n";
$response .= "<td valign='top' class='row_style1' style='background-color:#444444;color:#CCCCCC;'><strong>" . $text['label-exists'] . "</strong><br />" . $text['option-false'] . "</td>\n";
$response .= "<td valign='top' class='row_style1'>&nbsp;</td>\n";
@ -859,18 +850,17 @@ if (!class_exists('schema')) {
$response .= "</tr>\n";
}
}
}
//end the list of tables
$response .= "</table>\n";
$response .= "<br />\n";
}
//loop line by line through all the lines of sql code
$x = 0;
if (empty($sql_update) && $format == "text") {
$response .= " " . $text['label-schema'] . ": " . $text['label-no_change'] . "\n";
}
else {
} else {
if ($format == "text") {
$response .= " " . $text['label-schema'] . "\n";
}
@ -883,8 +873,7 @@ if (!class_exists('schema')) {
if ($format == "text") {
$response .= " $sql;\n";
}
}
catch (PDOException $error) {
} catch (PDOException $error) {
$response .= " error: " . $error->getMessage() . " sql: $sql\n";
}
}
@ -918,6 +907,7 @@ if (!class_exists('schema')) {
//}
} //end function
}
}
//example use
@ -927,5 +917,4 @@ if (!class_exists('schema')) {
//$obj->schema();
//$result_array = $schema->obj['sql'];
//print_r($result_array);
?>