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 <?php
/* /*
FusionPBX FusionPBX
Version: MPL 1.1 Version: MPL 1.1
@ -22,10 +23,11 @@
Contributor(s): Contributor(s):
Mark J Crane <markjcrane@fusionpbx.com> Mark J Crane <markjcrane@fusionpbx.com>
*/ */
//define the schema class //define the schema class
if (!class_exists('schema')) { if (!class_exists('schema')) {
class schema { class schema {
//define variables //define variables
@ -46,12 +48,11 @@ if (!class_exists('schema')) {
//get the list of installed apps from the core and mod directories //get the list of installed apps from the core and mod directories
$config_list = glob($_SERVER["DOCUMENT_ROOT"] . PROJECT_PATH . "/*/*/app_config.php"); $config_list = glob($_SERVER["DOCUMENT_ROOT"] . PROJECT_PATH . "/*/*/app_config.php");
$x=0; $x = 0;
foreach ($config_list as $config_path) { foreach ($config_list as $config_path) {
try { try {
include($config_path); include($config_path);
} } catch (Exception $e) {
catch (Exception $e) {
//echo 'Caught exception: ', $e->getMessage(), "\n"; //echo 'Caught exception: ', $e->getMessage(), "\n";
} }
$x++; $x++;
@ -73,19 +74,18 @@ if (!class_exists('schema')) {
foreach ($row['fields'] as $field) { foreach ($row['fields'] as $field) {
if (!empty($field['deprecated']) and ($field['deprecated'] == "true")) { if (!empty($field['deprecated']) and ($field['deprecated'] == "true")) {
//skip this field //skip this field
} else {
if ($field_count > 0) {
$sql .= ",\n";
} }
else {
if ($field_count > 0 ) { $sql .= ",\n"; }
if (is_array($field['name'])) { if (is_array($field['name'])) {
$sql .= $field['name']['text']." "; $sql .= $field['name']['text'] . " ";
} } else {
else { $sql .= $field['name'] . " ";
$sql .= $field['name']." ";
} }
if (is_array($field['type'])) { if (is_array($field['type'])) {
$sql .= $field['type'][$this->db_type]; $sql .= $field['type'][$this->db_type];
} } else {
else {
$sql .= $field['type']; $sql .= $field['type'];
} }
if (isset($field['key']) && isset($field['key']['type']) && ($field['key']['type'] == "primary")) { 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") { if ($this->db_type == "mysql") {
$sql .= ") ENGINE=INNODB;"; $sql .= ") ENGINE=INNODB;";
} } else {
else {
$sql .= ");"; $sql .= ");";
} }
$this->result['sql'][] = $sql; $this->result['sql'][] = $sql;
@ -126,8 +125,7 @@ if (!class_exists('schema')) {
//execute the sql query //execute the sql query
try { try {
$this->database->query($sql); $this->database->query($sql);
} } catch (PDOException $error) {
catch (PDOException $error) {
echo "error: " . $error->getMessage() . " sql: $sql<br/>"; echo "error: " . $error->getMessage() . " sql: $sql<br/>";
} }
//complete the transaction //complete the transaction
@ -146,14 +144,13 @@ if (!class_exists('schema')) {
} }
//check if a column exists //check if a column exists
public function column_exists ($db_name, $table_name, $column_name) { public function column_exists($db_name, $table_name, $column_name) {
if ($this->db_type == "sqlite") { if ($this->db_type == "sqlite") {
$table_info = $this->table_info($db_name, $table_name); $table_info = $this->table_info($db_name, $table_name);
if ($this->sqlite_column_exists($table_info, $column_name)) { if ($this->sqlite_column_exists($table_info, $column_name)) {
return true; return true;
} } else {
else {
return false; return false;
} }
} }
@ -174,19 +171,20 @@ if (!class_exists('schema')) {
} }
if (count($result) > 0) { if (count($result) > 0) {
return true; return true;
} } else {
else {
return false; return false;
} }
unset ($prep_statement); unset($prep_statement);
} }
} }
//get the table information //get the table information
public function table_info($db_name, $table_name) { 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") { if ($this->db_type == "sqlite") {
$sql = "PRAGMA table_info(".$table_name.");"; $sql = "PRAGMA table_info(" . $table_name . ");";
} }
if ($this->db_type == "pgsql") { if ($this->db_type == "pgsql") {
$sql = "SELECT ordinal_position, "; $sql = "SELECT ordinal_position, ";
@ -197,12 +195,12 @@ if (!class_exists('schema')) {
$sql .= "character_maximum_length, "; $sql .= "character_maximum_length, ";
$sql .= "numeric_precision "; $sql .= "numeric_precision ";
$sql .= "FROM information_schema.columns "; $sql .= "FROM information_schema.columns ";
$sql .= "WHERE table_name = '".$table_name."' "; $sql .= "WHERE table_name = '" . $table_name . "' ";
$sql .= "and table_catalog = '".$db_name."' "; $sql .= "and table_catalog = '" . $db_name . "' ";
$sql .= "ORDER BY ordinal_position; "; $sql .= "ORDER BY ordinal_position; ";
} }
if ($this->db_type == "mysql") { if ($this->db_type == "mysql") {
$sql = "describe ".$table_name.";"; $sql = "describe " . $table_name . ";";
} }
$prep_statement = $this->database->db->prepare($sql); $prep_statement = $this->database->db->prepare($sql);
$prep_statement->execute(); $prep_statement->execute();
@ -210,19 +208,18 @@ if (!class_exists('schema')) {
} }
//database table exists alternate //database table exists alternate
private function db_table_exists_alternate ($db_type, $table_name) { private function db_table_exists_alternate($db_type, $table_name) {
$sql = "select count(*) from $table_name "; $sql = "select count(*) from $table_name ";
$result = $this->database->query($sql); $result = $this->database->query($sql);
if ($result > 0) { if ($result > 0) {
return true; //table exists return true; //table exists
} } else {
else {
return false; //table doesn't exist return false; //table doesn't exist
} }
} }
//database table exists //database table exists
private function db_table_exists ($db_type, $db_name, $table_name) { private function db_table_exists($db_type, $db_name, $table_name) {
$sql = ""; $sql = "";
if ($db_type == "sqlite") { if ($db_type == "sqlite") {
$sql .= "SELECT * FROM sqlite_master WHERE type='table' and name='$table_name' "; $sql .= "SELECT * FROM sqlite_master WHERE type='table' and name='$table_name' ";
@ -238,17 +235,18 @@ if (!class_exists('schema')) {
$result = $prep_statement->fetchAll(PDO::FETCH_NAMED); $result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
if (count($result) > 0) { if (count($result) > 0) {
return true; //table exists return true; //table exists
} } else {
else {
return false; //table doesn't exist return false; //table doesn't exist
} }
} }
//database table information //database table information
private function db_table_info($db_name, $db_type, $table_name) { 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") { if ($db_type == "sqlite") {
$sql = "PRAGMA table_info(".$table_name.");"; $sql = "PRAGMA table_info(" . $table_name . ");";
} }
if ($db_type == "pgsql") { if ($db_type == "pgsql") {
$sql = "SELECT ordinal_position, "; $sql = "SELECT ordinal_position, ";
@ -259,12 +257,12 @@ if (!class_exists('schema')) {
$sql .= "character_maximum_length, "; $sql .= "character_maximum_length, ";
$sql .= "numeric_precision "; $sql .= "numeric_precision ";
$sql .= "FROM information_schema.columns "; $sql .= "FROM information_schema.columns ";
$sql .= "WHERE table_name = '".$table_name."' "; $sql .= "WHERE table_name = '" . $table_name . "' ";
$sql .= "and table_catalog = '".$db_name."' "; $sql .= "and table_catalog = '" . $db_name . "' ";
$sql .= "ORDER BY ordinal_position; "; $sql .= "ORDER BY ordinal_position; ";
} }
if ($db_type == "mysql") { if ($db_type == "mysql") {
$sql = "describe ".$table_name.";"; $sql = "describe " . $table_name . ";";
} }
$prep_statement = $this->database->db->prepare($sql); $prep_statement = $this->database->db->prepare($sql);
$prep_statement->execute(); $prep_statement->execute();
@ -307,14 +305,13 @@ if (!class_exists('schema')) {
} }
//database column exists //database column exists
private function db_column_exists ($db_type, $db_name, $table_name, $column_name) { private function db_column_exists($db_type, $db_name, $table_name, $column_name) {
if ($db_type == "sqlite") { if ($db_type == "sqlite") {
$table_info = $this->db_table_info($db_name, $db_type, $table_name); $table_info = $this->db_table_info($db_name, $db_type, $table_name);
if ($this->db_sqlite_column_exists($table_info, $column_name)) { if ($this->db_sqlite_column_exists($table_info, $column_name)) {
return true; return true;
} } else {
else {
return false; return false;
} }
} }
@ -331,50 +328,52 @@ if (!class_exists('schema')) {
$result = $prep_statement->fetchAll(PDO::FETCH_NAMED); $result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
if (!empty($result)) { if (!empty($result)) {
return true; return true;
} } else {
else {
return false; return false;
} }
unset ($prep_statement); unset($prep_statement);
} }
} }
//database column data type //database column data type
private function db_column_data_type ($db_type, $db_name, $table_name, $column_name) { private function db_column_data_type($db_type, $db_name, $table_name, $column_name) {
$table_info = $this->db_table_info($db_name, $db_type, $table_name); $table_info = $this->db_table_info($db_name, $db_type, $table_name);
return $this->db_data_type($db_type, $table_info, $column_name); return $this->db_data_type($db_type, $table_info, $column_name);
} }
//database create table //database create table
public function db_create_table ($apps, $db_type, $table) { public function db_create_table($apps, $db_type, $table) {
if (empty($apps)) { return false; } if (empty($apps)) {
if (is_array($apps)) foreach ($apps as $x => $app) { return false;
if (!empty($app['db']) && is_array($app['db'])) foreach ($app['db'] as $y => $row) { }
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'])) { if (!empty($row['table']['name']) && is_array($row['table']['name'])) {
$table_name = $row['table']['name']['text']; $table_name = $row['table']['name']['text'];
} } else {
else {
$table_name = $row['table']['name']; $table_name = $row['table']['name'];
} }
if ($table_name == $table) { if ($table_name == $table) {
$sql = "CREATE TABLE " . $table_name . " (\n"; $sql = "CREATE TABLE " . $table_name . " (\n";
(int)$field_count = 0; (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") { if (!empty($field['deprecated']) && $field['deprecated'] == "true") {
//skip this row //skip this row
} else {
if ($field_count > 0) {
$sql .= ",\n";
} }
else { if (!empty($field['name']) && is_array($field['name'])) {
if ($field_count > 0 ) { $sql .= ",\n"; }
if (!empty($field['name']) &&is_array($field['name'])) {
$sql .= $field['name']['text'] . " "; $sql .= $field['name']['text'] . " ";
} } else {
else {
$sql .= $field['name'] . " "; $sql .= $field['name'] . " ";
} }
if (!empty($field['type']) &&is_array($field['type'])) { if (!empty($field['type']) && is_array($field['type'])) {
$sql .= $field['type'][$db_type]; $sql .= $field['type'][$db_type];
} } else {
else {
$sql .= $field['type']; $sql .= $field['type'];
} }
if (!empty($field['key']['type']) && $field['key']['type'] == "primary") { if (!empty($field['key']['type']) && $field['key']['type'] == "primary") {
@ -383,15 +382,18 @@ if (!class_exists('schema')) {
$field_count++; $field_count++;
} }
} }
}
$sql .= ");\n"; $sql .= ");\n";
return $sql; return $sql;
} }
} }
} }
} }
}
}
//database insert //database insert
private function db_insert_into ($apps, $db_type, $table) { private function db_insert_into($apps, $db_type, $table) {
global $db_name; global $db_name;
foreach ($apps as $x => $app) { foreach ($apps as $x => $app) {
foreach ($app['db'] as $y => $row) { foreach ($app['db'] as $y => $row) {
@ -401,13 +403,13 @@ if (!class_exists('schema')) {
foreach ($row['fields'] as $field) { foreach ($row['fields'] as $field) {
if (!empty($field['deprecated']) && $field['deprecated'] == "true") { if (!empty($field['deprecated']) && $field['deprecated'] == "true") {
//skip this field //skip this field
} else {
if ($field_count > 0) {
$sql .= ",";
} }
else {
if ($field_count > 0 ) { $sql .= ","; }
if (is_array($field['name'])) { if (is_array($field['name'])) {
$sql .= $field['name']['text']; $sql .= $field['name']['text'];
} } else {
else {
$sql .= $field['name']; $sql .= $field['name'];
} }
$field_count++; $field_count++;
@ -419,42 +421,41 @@ if (!class_exists('schema')) {
foreach ($row['fields'] as $field) { foreach ($row['fields'] as $field) {
if (!empty($field['deprecated']) && $field['deprecated'] == "true") { if (!empty($field['deprecated']) && $field['deprecated'] == "true") {
//skip this field //skip this field
} else {
if ($field_count > 0) {
$sql .= ",";
} }
else {
if ($field_count > 0 ) { $sql .= ","; }
if (is_array($field['name'])) { if (is_array($field['name'])) {
if ($field['exists'] == "false") { if ($field['exists'] == "false") {
if (is_array($field['name']['deprecated'])) { if (is_array($field['name']['deprecated'])) {
$found = false; $found = false;
foreach ($field['name']['deprecated'] as $row) { foreach ($field['name']['deprecated'] as $row) {
if ($this->db_column_exists ($db_type, $db_name, 'tmp_'.$table, $row)) { if ($this->db_column_exists($db_type, $db_name, 'tmp_' . $table, $row)) {
$sql .= $row; $sql .= $row;
$found = true; $found = true;
break; break;
} }
} }
if (!$found) { $sql .= "''"; } if (!$found) {
$sql .= "''";
} }
else { } else {
if ($this->db_column_exists ($db_type, $db_name, 'tmp_'.$table, $field['name']['deprecated'])) { if ($this->db_column_exists($db_type, $db_name, 'tmp_' . $table, $field['name']['deprecated'])) {
$sql .= $field['name']['deprecated']; $sql .= $field['name']['deprecated'];
} } else {
else {
$sql .= "''"; $sql .= "''";
} }
} }
} } else {
else {
$sql .= $field['name']['text']; $sql .= $field['name']['text'];
} }
} } else {
else {
$sql .= $field['name']; $sql .= $field['name'];
} }
$field_count++; $field_count++;
} }
} }
$sql .= " FROM tmp_".$table.";\n"; $sql .= " FROM tmp_" . $table . ";\n";
return $sql; return $sql;
} }
} }
@ -462,12 +463,14 @@ if (!class_exists('schema')) {
} }
//datatase schema //datatase schema
public function schema ($format = '') { public function schema($format = '') {
//set the global variable //set the global variable
global $text, $output_format; global $text, $output_format;
if ($format == '') $format = $output_format; if ($format == '') {
$format = $output_format;
}
//get the db variables //get the db variables
//require_once "resources/classes/config.php"; //require_once "resources/classes/config.php";
@ -482,14 +485,13 @@ if (!class_exists('schema')) {
//$db_host = $config->db_host; //$db_host = $config->db_host;
//$db_path = $config->db_path; //$db_path = $config->db_path;
//$db_port = $config->db_port; //$db_port = $config->db_port;
//includes files //includes files
require dirname(__DIR__, 2) . "/resources/require.php"; require dirname(__DIR__, 2) . "/resources/require.php";
//add multi-lingual support //add multi-lingual support
if (!isset($text)) { if (!isset($text)) {
$language = new text; $language = new text;
$text = $language->get(null,'core/upgrade'); $text = $language->get(null, 'core/upgrade');
} }
//PHP PDO check if table or column exists //PHP PDO check if table or column exists
@ -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,%' // SELECT * FROM sqlite_master WHERE type='table' AND name='v_cdr' AND sql LIKE '%caller_id_name TEXT,%'
//aditional information //aditional information
// http://www.sqlite.org/faq.html#q9 // http://www.sqlite.org/faq.html#q9
//postgresql //postgresql
//list all tables in the database //list all tables in the database
// SELECT table_name FROM pg_tables WHERE schemaname='public'; // SELECT table_name FROM pg_tables WHERE schemaname='public';
@ -517,25 +518,22 @@ if (!class_exists('schema')) {
//oracle //oracle
//check if table exists //check if table exists
// SELECT TABLE_NAME FROM ALL_TABLES // SELECT TABLE_NAME FROM ALL_TABLES
//update the app db array add exists true or false //update the app db array add exists true or false
$sql = ''; $sql = '';
foreach ($this->apps as $x => $app) { 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 (isset($row['table']['name'])) {
if (is_array($row['table']['name'])) { if (is_array($row['table']['name'])) {
$table_name = $row['table']['name']['text']; $table_name = $row['table']['name']['text'];
} } else {
else {
$table_name = $row['table']['name']; $table_name = $row['table']['name'];
} }
} } else {
else {
//old array syntax //old array syntax
if (is_array($row['table'])) { if (is_array($row['table'])) {
$table_name = $row['table']['text']; $table_name = $row['table']['text'];
} } else {
else {
$table_name = $row['table']; $table_name = $row['table'];
} }
} }
@ -544,28 +542,24 @@ if (!class_exists('schema')) {
//check if the table exists //check if the table exists
if ($this->db_table_exists($db_type, $db_name, $table_name)) { if ($this->db_table_exists($db_type, $db_name, $table_name)) {
$this->apps[$x]['db'][$y]['exists'] = 'true'; $this->apps[$x]['db'][$y]['exists'] = 'true';
} } else {
else {
$this->apps[$x]['db'][$y]['exists'] = 'false'; $this->apps[$x]['db'][$y]['exists'] = 'false';
} }
//check if the column exists //check if the column exists
foreach ($row['fields'] as $z => $field) { foreach ($row['fields'] as $z => $field) {
if (!empty($field['deprecated']) && $field['deprecated'] == "true") { if (!empty($field['deprecated']) && $field['deprecated'] == "true") {
//skip this field //skip this field
} } else {
else {
if (is_array($field['name'])) { if (is_array($field['name'])) {
$field_name = $field['name']['text']; $field_name = $field['name']['text'];
} } else {
else {
$field_name = $field['name']; $field_name = $field['name'];
} }
if (!empty($field_name)) { if (!empty($field_name)) {
if ($this->db_column_exists ($db_type, $db_name, $table_name, $field_name)) { if ($this->db_column_exists($db_type, $db_name, $table_name, $field_name)) {
//found //found
$this->apps[$x]['db'][$y]['fields'][$z]['exists'] = 'true'; $this->apps[$x]['db'][$y]['fields'][$z]['exists'] = 'true';
} } else {
else {
//not found //not found
$this->apps[$x]['db'][$y]['fields'][$z]['exists'] = 'false'; $this->apps[$x]['db'][$y]['fields'][$z]['exists'] = 'false';
} }
@ -577,42 +571,40 @@ if (!class_exists('schema')) {
} }
} }
} }
}
//prepare the variables //prepare the variables
$sql_update = ''; $sql_update = '';
//add missing tables and fields //add missing tables and fields
foreach ($this->apps as $x => $app) { 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'])) { if (is_array($row['table']['name'])) {
$table_name = $row['table']['name']['text']; $table_name = $row['table']['name']['text'];
if ($this->db_table_exists($db_type, $db_name, $row['table']['name']['deprecated'])) { if ($this->db_table_exists($db_type, $db_name, $row['table']['name']['deprecated'])) {
$row['exists'] = "false"; //testing $row['exists'] = "false"; //testing
if ($db_type == "pgsql") { if ($db_type == "pgsql") {
$sql_update .= "ALTER TABLE ".$row['table']['name']['deprecated']." RENAME TO ".$row['table']['name']['text'].";\n"; $sql_update .= "ALTER TABLE " . $row['table']['name']['deprecated'] . " RENAME TO " . $row['table']['name']['text'] . ";\n";
} }
if ($db_type == "mysql") { if ($db_type == "mysql") {
$sql_update .= "RENAME TABLE ".$row['table']['name']['deprecated']." TO ".$row['table']['name']['text'].";\n"; $sql_update .= "RENAME TABLE " . $row['table']['name']['deprecated'] . " TO " . $row['table']['name']['text'] . ";\n";
} }
if ($db_type == "sqlite") { if ($db_type == "sqlite") {
$sql_update .= "ALTER TABLE ".$row['table']['name']['deprecated']." RENAME TO ".$row['table']['name']['text'].";\n"; $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'])) { if ($this->db_table_exists($db_type, $db_name, $row['table']['name']['text'])) {
$row['exists'] = "true"; $row['exists'] = "true";
} } else {
else {
$row['exists'] = "false"; $row['exists'] = "false";
$sql_update .= $this->db_create_table($this->apps, $db_type, $row['table']['name']['text']); $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'])) { if ($this->db_table_exists($db_type, $db_name, $row['table']['name'])) {
$row['exists'] = "true"; $row['exists'] = "true";
} } else {
else {
$row['exists'] = "false"; $row['exists'] = "false";
} }
$table_name = $row['table']['name']; $table_name = $row['table']['name'];
@ -624,20 +616,17 @@ if (!class_exists('schema')) {
foreach ($row['fields'] as $z => $field) { foreach ($row['fields'] as $z => $field) {
if (!empty($field['deprecated']) && $field['deprecated'] == "true") { if (!empty($field['deprecated']) && $field['deprecated'] == "true") {
//skip this field //skip this field
} } else {
else {
//get the data type //get the data type
if (is_array($field['type'])) { if (is_array($field['type'])) {
$field_type = $field['type'][$db_type]; $field_type = $field['type'][$db_type];
} } else {
else {
$field_type = $field['type']; $field_type = $field['type'];
} }
//get the field name //get the field name
if (is_array($field['name'])) { if (is_array($field['name'])) {
$field_name = $field['name']['text']; $field_name = $field['name']['text'];
} } else {
else {
$field_name = $field['name']; $field_name = $field['name'];
} }
@ -648,60 +637,68 @@ if (!class_exists('schema')) {
// else { // else {
// $field['exists'] = "false"; // $field['exists'] = "false";
// } // }
//add or rename fields //add or rename fields
if (isset($field['name']['deprecated']) && $this->db_column_exists ($db_type, $db_name, $table_name, $field['name']['deprecated'])) { if (isset($field['name']['deprecated']) && $this->db_column_exists($db_type, $db_name, $table_name, $field['name']['deprecated'])) {
if ($db_type == "pgsql") { if ($db_type == "pgsql") {
$sql_update .= "ALTER TABLE ".$table_name." RENAME COLUMN ".$field['name']['deprecated']." to ".$field['name']['text'].";\n"; $sql_update .= "ALTER TABLE " . $table_name . " RENAME COLUMN " . $field['name']['deprecated'] . " to " . $field['name']['text'] . ";\n";
} }
if ($db_type == "mysql") { if ($db_type == "mysql") {
$field_type = str_replace("AUTO_INCREMENT PRIMARY KEY", "", $field_type); $field_type = str_replace("AUTO_INCREMENT PRIMARY KEY", "", $field_type);
$sql_update .= "ALTER TABLE ".$table_name." CHANGE ".$field['name']['deprecated']." ".$field['name']['text']." ".$field_type.";\n"; $sql_update .= "ALTER TABLE " . $table_name . " CHANGE " . $field['name']['deprecated'] . " " . $field['name']['text'] . " " . $field_type . ";\n";
} }
if ($db_type == "sqlite") { if ($db_type == "sqlite") {
//a change has been made to the field name //a change has been made to the field name
$this->apps[$x]['db'][$y]['rebuild'] = 'true'; $this->apps[$x]['db'][$y]['rebuild'] = 'true';
} }
} } else {
else {
//find missing fields and add them //find missing fields and add them
if ($field['exists'] == "false") { if ($field['exists'] == "false") {
$sql_update .= "ALTER TABLE ".$table_name." ADD ".$field_name." ".$field_type.";\n"; $sql_update .= "ALTER TABLE " . $table_name . " ADD " . $field_name . " " . $field_type . ";\n";
} }
} }
//change the data type if it has been changed //change the data type if it has been changed
//if the data type in the app db array is different than the type in the database then change the data type //if the data type in the app db array is different than the type in the database then change the data type
if ($this->data_types) { if ($this->data_types) {
$db_field_type = $this->db_column_data_type ($db_type, $db_name, $table_name, $field_name); $db_field_type = $this->db_column_data_type($db_type, $db_name, $table_name, $field_name);
$field_type_array = explode("(", $field_type); $field_type_array = explode("(", $field_type);
$field_type = $field_type_array[0]; $field_type = $field_type_array[0];
if (trim($db_field_type) != trim($field_type) && !empty($db_field_type)) { if (trim($db_field_type) != trim($field_type) && !empty($db_field_type)) {
if ($db_type == "pgsql") { if ($db_type == "pgsql") {
if (strtolower($field_type) == "uuid") { if (strtolower($field_type) == "uuid") {
$sql_update .= "ALTER TABLE ".$table_name." ALTER COLUMN ".$field_name." TYPE uuid USING\n"; $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 .= "CAST(regexp_replace(" . $field_name . ", '([A-Z0-9]{4})([A-Z0-9]{12})', E'\\1-\\2')\n";
$sql_update .= "AS uuid);\n"; $sql_update .= "AS uuid);\n";
} } else {
else {
//field type has not changed //field type has not changed
if ($db_field_type == "integer" && strtolower($field_type) == "serial") { } 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 without time zone" && strtolower($field_type) == "timestamp") {
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 == "timestamp without time zone" && strtolower($field_type) == "datetime") {
else if ($db_field_type == "character" && strtolower($field_type) == "char") { }
} 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 //field type has changed
else { else {
switch ($field_type) { switch ($field_type) {
case 'numeric': $using = $field_name."::numeric"; break; case 'numeric': $using = $field_name . "::numeric";
break;
case 'timestamp': case 'timestamp':
case 'datetime': $using = $field_name."::timestamp without time zone"; break; case 'datetime': $using = $field_name . "::timestamp without time zone";
case 'timestamptz': $using = $field_name."::timestamp with time zone"; break; break;
case 'boolean': $using = $field_name."::boolean"; break; case 'timestamptz': $using = $field_name . "::timestamp with time zone";
break;
case 'boolean': $using = $field_name . "::boolean";
break;
default: unset($using); default: unset($using);
} }
$sql_update .= "ALTER TABLE ".$table_name." ALTER COLUMN ".$field_name." TYPE ".$field_type." ".($using ? "USING ".$using : null).";\n"; $sql_update .= "ALTER TABLE " . $table_name . " ALTER COLUMN " . $field_name . " TYPE " . $field_type . " " . ($using ? "USING " . $using : null) . ";\n";
} }
} }
} }
@ -709,12 +706,10 @@ if (!class_exists('schema')) {
$type = explode("(", $db_field_type); $type = explode("(", $db_field_type);
if ($type[0] == $field_type) { if ($type[0] == $field_type) {
//do nothing //do nothing
} } else if ($field_type == "numeric" && $type[0] == "decimal") {
else if ($field_type == "numeric" && $type[0] == "decimal") {
//do nothing //do nothing
} } else {
else { $sql_update .= "ALTER TABLE " . $table_name . " modify " . $field_name . " " . $field_type . ";\n";
$sql_update .= "ALTER TABLE ".$table_name." modify ".$field_name." ".$field_type.";\n";
} }
unset($type); unset($type);
} }
@ -727,20 +722,20 @@ if (!class_exists('schema')) {
} }
} }
} }
} } elseif (!is_array($row['table']['name'])) {
elseif (!is_array($row['table']['name'])) {
//create table //create table
$sql_update .= $this->db_create_table($this->apps, $db_type, $row['table']['name']); $sql_update .= $this->db_create_table($this->apps, $db_type, $row['table']['name']);
} }
} }
} }
}
//rebuild and populate the table //rebuild and populate the table
foreach ($this->apps as $x => $app) { 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'])) { if (is_array($row['table']['name'])) {
$table_name = $row['table']['name']['text']; $table_name = $row['table']['name']['text'];
} } else {
else {
$table_name = $row['table']['name']; $table_name = $row['table']['name'];
} }
if (!empty($field['rebuild']) && $row['rebuild'] == "true") { if (!empty($field['rebuild']) && $row['rebuild'] == "true") {
@ -748,19 +743,20 @@ if (!class_exists('schema')) {
//start the transaction //start the transaction
//$sql_update .= "BEGIN TRANSACTION;\n"; //$sql_update .= "BEGIN TRANSACTION;\n";
//rename the table //rename the table
$sql_update .= "ALTER TABLE ".$table_name." RENAME TO tmp_".$table_name.";\n"; $sql_update .= "ALTER TABLE " . $table_name . " RENAME TO tmp_" . $table_name . ";\n";
//create the table //create the table
$sql_update .= $this->db_create_table($this->apps, $db_type, $table_name); $sql_update .= $this->db_create_table($this->apps, $db_type, $table_name);
//insert the data into the new table //insert the data into the new table
$sql_update .= $this->db_insert_into($this->apps, $db_type, $table_name); $sql_update .= $this->db_insert_into($this->apps, $db_type, $table_name);
//drop the old table //drop the old table
$sql_update .= "DROP TABLE tmp_".$table_name.";\n"; $sql_update .= "DROP TABLE tmp_" . $table_name . ";\n";
//commit the transaction //commit the transaction
//$sql_update .= "COMMIT;\n"; //$sql_update .= "COMMIT;\n";
} }
} }
} }
} }
}
// initialize response variable // initialize response variable
$response = ''; $response = '';
@ -768,7 +764,7 @@ if (!class_exists('schema')) {
//display results as html //display results as html
if ($format == "html") { if ($format == "html") {
//show the database type //show the database type
$response .= "<strong>".$text['header-database_type'].": ".$db_type. "</strong><br /><br />"; $response .= "<strong>" . $text['header-database_type'] . ": " . $db_type . "</strong><br /><br />";
//start the table //start the table
$response .= "<table width='100%' border='0' cellpadding='20' cellspacing='0'>\n"; $response .= "<table width='100%' border='0' cellpadding='20' cellspacing='0'>\n";
//show the changes //show the changes
@ -776,7 +772,7 @@ if (!class_exists('schema')) {
$response .= "<tr>\n"; $response .= "<tr>\n";
$response .= "<td class='row_style1' colspan='3'>\n"; $response .= "<td class='row_style1' colspan='3'>\n";
$response .= "<br />\n"; $response .= "<br />\n";
$response .= "<strong>".$text['label-sql_changes'].":</strong><br />\n"; $response .= "<strong>" . $text['label-sql_changes'] . ":</strong><br />\n";
$response .= "<pre>\n"; $response .= "<pre>\n";
$response .= $sql_update; $response .= $sql_update;
$response .= "</pre>\n"; $response .= "</pre>\n";
@ -786,62 +782,58 @@ if (!class_exists('schema')) {
} }
//list all tables //list all tables
$response .= "<tr>\n"; $response .= "<tr>\n";
$response .= "<th>".$text['label-table']."</th>\n"; $response .= "<th>" . $text['label-table'] . "</th>\n";
$response .= "<th>".$text['label-exists']."</th>\n"; $response .= "<th>" . $text['label-exists'] . "</th>\n";
$response .= "<th>".$text['label-details']."</th>\n"; $response .= "<th>" . $text['label-details'] . "</th>\n";
$response .= "<tr>\n"; $response .= "<tr>\n";
//build the html while looping through the app db array //build the html while looping through the app db array
$sql = ''; $sql = '';
foreach ($this->apps as $app) { 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'])) { if (is_array($row['table']['name'])) {
$table_name = $row['table']['name']['text']; $table_name = $row['table']['name']['text'];
} } else {
else {
$table_name = $row['table']['name']; $table_name = $row['table']['name'];
} }
$response .= "<tr>\n"; $response .= "<tr>\n";
//check if the table exists //check if the table exists
if ($row['exists'] == "true") { if ($row['exists'] == "true") {
$response .= "<td valign='top' class='row_style1'>".$table_name."</td>\n"; $response .= "<td valign='top' class='row_style1'>" . $table_name . "</td>\n";
$response .= "<td valign='top' class='vncell' style='padding-top: 3px;'>".$text['option-true']."</td>\n"; $response .= "<td valign='top' class='vncell' style='padding-top: 3px;'>" . $text['option-true'] . "</td>\n";
if (count($row['fields']) > 0) { if (count($row['fields']) > 0) {
$response .= "<td class='row_style1'>\n"; $response .= "<td class='row_style1'>\n";
//show the list of columns //show the list of columns
$response .= "<table border='0' cellpadding='10' cellspacing='0'>\n"; $response .= "<table border='0' cellpadding='10' cellspacing='0'>\n";
$response .= "<tr>\n"; $response .= "<tr>\n";
$response .= "<th>".$text['label-name']."</th>\n"; $response .= "<th>" . $text['label-name'] . "</th>\n";
$response .= "<th>".$text['label-type']."</th>\n"; $response .= "<th>" . $text['label-type'] . "</th>\n";
$response .= "<th>".$text['label-exists']."</th>\n"; $response .= "<th>" . $text['label-exists'] . "</th>\n";
$response .= "</tr>\n"; $response .= "</tr>\n";
foreach ($row['fields'] as $field) { foreach ($row['fields'] as $field) {
if (!empty($field['deprecated']) && $field['deprecated'] == "true") { if (!empty($field['deprecated']) && $field['deprecated'] == "true") {
//skip this field //skip this field
} } else {
else {
if (is_array($field['name'])) { if (is_array($field['name'])) {
$field_name = $field['name']['text']; $field_name = $field['name']['text'];
} } else {
else {
$field_name = $field['name']; $field_name = $field['name'];
} }
if (is_array($field['type'])) { if (is_array($field['type'])) {
$field_type = $field['type'][$db_type]; $field_type = $field['type'][$db_type];
} } else {
else {
$field_type = $field['type']; $field_type = $field['type'];
} }
$response .= "<tr>\n"; $response .= "<tr>\n";
$response .= "<td class='row_style1' width='200'>".$field_name."</td>\n"; $response .= "<td class='row_style1' width='200'>" . $field_name . "</td>\n";
$response .= "<td class='row_style1'>".$field_type."</td>\n"; $response .= "<td class='row_style1'>" . $field_type . "</td>\n";
if ($field['exists'] == "true") { if ($field['exists'] == "true") {
$response .= "<td class='row_style0' style=''>".$text['option-true']."</td>\n"; $response .= "<td class='row_style0' style=''>" . $text['option-true'] . "</td>\n";
$response .= "<td>&nbsp;</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 class='row_style1' style='background-color:#444444;color:#CCCCCC;'>".$text['option-false']."</td>\n";
$response .= "<td>&nbsp;</td>\n"; $response .= "<td>&nbsp;</td>\n";
} }
$response .= "</tr>\n"; $response .= "</tr>\n";
@ -850,48 +842,45 @@ if (!class_exists('schema')) {
$response .= " </table>\n"; $response .= " </table>\n";
$response .= "</td>\n"; $response .= "</td>\n";
} }
} } else {
else {
$response .= "<td valign='top' class='row_style1'>$table_name</td>\n"; $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' 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"; $response .= "<td valign='top' class='row_style1'>&nbsp;</td>\n";
} }
$response .= "</tr>\n"; $response .= "</tr>\n";
} }
} }
}
//end the list of tables //end the list of tables
$response .= "</table>\n"; $response .= "</table>\n";
$response .= "<br />\n"; $response .= "<br />\n";
} }
//loop line by line through all the lines of sql code //loop line by line through all the lines of sql code
$x = 0; $x = 0;
if (empty($sql_update) && $format == "text") { if (empty($sql_update) && $format == "text") {
$response .= " ".$text['label-schema'].": ".$text['label-no_change']."\n"; $response .= " " . $text['label-schema'] . ": " . $text['label-no_change'] . "\n";
} } else {
else {
if ($format == "text") { if ($format == "text") {
$response .= " ".$text['label-schema']."\n"; $response .= " " . $text['label-schema'] . "\n";
} }
//$this->db->beginTransaction(); //$this->db->beginTransaction();
$update_array = explode(";", $sql_update); $update_array = explode(";", $sql_update);
foreach($update_array as $sql) { foreach ($update_array as $sql) {
if (strlen(trim($sql))) { if (strlen(trim($sql))) {
try { try {
$this->database->db->query(trim($sql)); $this->database->db->query(trim($sql));
if ($format == "text") { if ($format == "text") {
$response .= " $sql;\n"; $response .= " $sql;\n";
} }
} } catch (PDOException $error) {
catch (PDOException $error) {
$response .= " error: " . $error->getMessage() . " sql: $sql\n"; $response .= " error: " . $error->getMessage() . " sql: $sql\n";
} }
} }
} }
//$this->db->commit(); //$this->db->commit();
$response .= "\n"; $response .= "\n";
unset ($sql_update, $sql); unset($sql_update, $sql);
} }
//refresh each postgresql subscription with its publication //refresh each postgresql subscription with its publication
@ -903,7 +892,7 @@ if (!class_exists('schema')) {
//refresh each subscription publication //refresh each subscription publication
foreach ($pg_subscriptions as $row) { foreach ($pg_subscriptions as $row) {
$sql = "ALTER SUBSCRIPTION ".$row['subname']." REFRESH PUBLICATION;"; $sql = "ALTER SUBSCRIPTION " . $row['subname'] . " REFRESH PUBLICATION;";
$response .= $sql; $response .= $sql;
$this->database->execute($sql); $this->database->execute($sql);
} }
@ -918,14 +907,14 @@ if (!class_exists('schema')) {
//} //}
} //end function } //end function
} }
} }
//example use //example use
//require_once "resources/classes/schema.php"; //require_once "resources/classes/schema.php";
//$obj = new schema; //$obj = new schema;
//$obj->db_type = $db_type; //$obj->db_type = $db_type;
//$obj->schema(); //$obj->schema();
//$result_array = $schema->obj['sql']; //$result_array = $schema->obj['sql'];
//print_r($result_array); //print_r($result_array);
?> ?>