2012-06-04 14:58:40 +00:00
<? php
/*
FusionPBX
Version: MPL 1.1
The contents of this file are subject to the Mozilla Public License Version
1.1 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
for the specific language governing rights and limitations under the
License.
The Original Code is FusionPBX
The Initial Developer of the Original Code is
Mark J Crane <markjcrane@fusionpbx.com>
2023-11-03 23:55:35 -06:00
Copyright (C) 2013 - 2023
2012-06-04 14:58:40 +00:00
All Rights Reserved.
Contributor(s):
Mark J Crane <markjcrane@fusionpbx.com>
*/
2013-01-23 13:49:51 +00:00
//define the schema class
2016-04-14 23:23:14 -06:00
if ( ! class_exists ( 'schema' )) {
2012-06-04 14:58:40 +00:00
class schema {
2016-04-14 22:34:10 -06:00
2014-07-15 07:58:01 +00:00
//define variables
2023-12-16 10:54:20 -04:00
private $database ;
2014-07-15 07:58:01 +00:00
public $apps ;
public $db_type ;
public $result ;
2020-10-21 14:28:15 -06:00
public $data_types ;
2012-06-04 14:58:40 +00:00
2016-04-14 22:10:47 -06:00
//class constructor
2012-06-04 14:58:40 +00:00
public function __construct () {
2023-06-17 09:11:32 -06:00
//includes files
require dirname ( __DIR__ , 2 ) . "/resources/require.php" ;
2023-03-24 12:18:23 -03:00
//connect to the database
2023-12-16 10:54:20 -04:00
$this -> database = database :: new ();
2016-04-14 22:10:47 -06:00
//get the list of installed apps from the core and mod directories
2012-06-04 14:58:40 +00:00
$config_list = glob ( $_SERVER [ "DOCUMENT_ROOT" ] . PROJECT_PATH . "/*/*/app_config.php" );
$x = 0 ;
foreach ( $config_list as & $config_path ) {
2020-11-04 10:00:31 -07:00
try {
2023-11-03 23:55:35 -06:00
include ( $config_path );
2020-11-04 10:00:31 -07:00
}
catch ( Exception $e ) {
2023-11-03 23:55:35 -06:00
//echo 'Caught exception: ', $e->getMessage(), "\n";
2020-11-04 10:00:31 -07:00
}
2012-06-04 14:58:40 +00:00
$x ++ ;
}
$this -> apps = $apps ;
}
//create the database schema
public function sql () {
$sql = '' ;
$sql_schema = '' ;
foreach ( $this -> apps as $app ) {
2016-04-25 21:09:14 -05:00
if ( isset ( $app [ 'db' ]) && count ( $app [ 'db' ])) {
2012-06-04 14:58:40 +00:00
foreach ( $app [ 'db' ] as $row ) {
//create the sql string
2017-02-16 00:03:33 -07:00
$table_name = $row [ 'table' ][ 'name' ];
$sql = "CREATE TABLE " . $row [ 'table' ][ 'name' ] . " ( \n " ;
2012-06-04 14:58:40 +00:00
$field_count = 0 ;
foreach ( $row [ 'fields' ] as $field ) {
2023-05-13 17:31:27 -06:00
if ( ! empty ( $field [ 'deprecated' ]) and ( $field [ 'deprecated' ] == "true" )) {
2012-06-04 14:58:40 +00:00
//skip this field
}
else {
if ( $field_count > 0 ) { $sql .= ", \n " ; }
if ( is_array ( $field [ 'name' ])) {
$sql .= $field [ 'name' ][ 'text' ] . " " ;
}
else {
$sql .= $field [ 'name' ] . " " ;
}
if ( is_array ( $field [ 'type' ])) {
$sql .= $field [ 'type' ][ $this -> db_type ];
}
else {
$sql .= $field [ 'type' ];
}
2016-04-25 21:09:14 -05:00
if ( isset ( $field [ 'key' ]) && isset ( $field [ 'key' ][ 'type' ]) && ( $field [ 'key' ][ 'type' ] == "primary" )) {
2012-06-04 14:58:40 +00:00
$sql .= " PRIMARY KEY" ;
}
2016-04-25 21:09:14 -05:00
if ( isset ( $field [ 'key' ]) && isset ( $field [ 'key' ][ 'type' ]) && ( $field [ 'key' ][ 'type' ] == "foreign" )) {
2012-06-04 14:58:40 +00:00
if ( $this -> db_type == "pgsql" ) {
//$sql .= " references ".$field['key']['reference']['table']."(".$field['key']['reference']['field'].")";
}
if ( $this -> db_type == "sqlite" ) {
//$sql .= " references ".$field['key']['reference']['table']."(".$field['key']['reference']['field'].")";
}
if ( $this -> db_type == "mysql" ) {
//$sql .= " references ".$field['key']['reference']['table']."(".$field['key']['reference']['field'].")";
}
}
$field_count ++ ;
}
}
if ( $this -> db_type == "mysql" ) {
2013-01-23 13:49:51 +00:00
$sql .= ") ENGINE=INNODB;" ;
2012-06-04 14:58:40 +00:00
}
else {
$sql .= ");" ;
}
$this -> result [ 'sql' ][] = $sql ;
unset ( $sql );
}
}
}
}
//create the database schema
public function exec () {
foreach ( $this -> result [ 'sql' ] as $sql ) {
//start the sql transaction
2023-12-16 10:54:20 -04:00
$this -> database -> beginTransaction ();
2012-06-04 14:58:40 +00:00
//execute the sql query
try {
2023-12-16 10:54:20 -04:00
$this -> database -> query ( $sql );
2012-06-04 14:58:40 +00:00
}
catch ( PDOException $error ) {
echo "error: " . $error -> getMessage () . " sql: $sql <br/>" ;
}
//complete the transaction
2023-12-16 10:54:20 -04:00
$this -> database -> commit ();
2012-06-04 14:58:40 +00:00
}
}
2014-04-01 18:52:58 +00:00
//check if a column exists in sqlite
private function sqlite_column_exists ( $table_info , $column_name ) {
foreach ( $table_info as $key => & $row ) {
if ( $row [ 'name' ] == $column_name ) {
return true ;
}
}
return $false ;
}
//check if a column exists
2014-04-01 20:20:49 +00:00
public function column_exists ( $db_name , $table_name , $column_name ) {
2014-04-01 18:52:58 +00:00
2014-04-01 20:20:49 +00:00
if ( $this -> db_type == "sqlite" ) {
$table_info = $this -> table_info ( $db_name , $table_name );
2014-04-01 18:52:58 +00:00
if ( $this -> sqlite_column_exists ( $table_info , $column_name )) {
return true ;
}
else {
return false ;
}
}
2014-04-01 20:20:49 +00:00
if ( $this -> db_type == "pgsql" ) {
2023-05-12 16:22:50 -06:00
$sql = "SELECT attname FROM pg_attribute WHERE attrelid = (SELECT oid FROM pg_class WHERE relname = ' $table_name ' limit 1) AND attname = ' $column_name '; " ;
2014-04-01 18:52:58 +00:00
}
2014-04-01 20:20:49 +00:00
if ( $this -> db_type == "mysql" ) {
2014-04-01 18:52:58 +00:00
//$sql .= "SELECT * FROM information_schema.COLUMNS where TABLE_SCHEMA = '$db_name' and TABLE_NAME = '$table_name' and COLUMN_NAME = '$column_name' ";
$sql = "show columns from $table_name where field = ' $column_name ' " ;
}
2014-04-01 20:20:49 +00:00
2014-04-01 18:52:58 +00:00
if ( $sql ) {
2023-12-16 10:54:20 -04:00
$prep_statement = $this -> database -> db -> prepare ( $sql );
2014-04-01 18:52:58 +00:00
$prep_statement -> execute ();
$result = $prep_statement -> fetchAll ( PDO :: FETCH_NAMED );
if ( ! $result ) {
return false ;
}
if ( count ( $result ) > 0 ) {
return true ;
}
else {
return false ;
}
unset ( $prep_statement );
}
}
//get the table information
2014-04-01 20:20:49 +00:00
public function table_info ( $db_name , $table_name ) {
2023-05-05 13:46:37 -03:00
if ( empty ( $table_name )) { return false ; }
2014-04-01 20:20:49 +00:00
if ( $this -> db_type == "sqlite" ) {
2014-04-01 18:52:58 +00:00
$sql = "PRAGMA table_info(" . $table_name . ");" ;
}
2014-04-01 20:20:49 +00:00
if ( $this -> db_type == "pgsql" ) {
2014-04-01 18:52:58 +00:00
$sql = "SELECT ordinal_position, " ;
$sql .= "column_name, " ;
$sql .= "data_type, " ;
$sql .= "column_default, " ;
$sql .= "is_nullable, " ;
$sql .= "character_maximum_length, " ;
$sql .= "numeric_precision " ;
$sql .= "FROM information_schema.columns " ;
$sql .= "WHERE table_name = '" . $table_name . "' " ;
$sql .= "and table_catalog = '" . $db_name . "' " ;
$sql .= "ORDER BY ordinal_position; " ;
}
2014-04-01 20:20:49 +00:00
if ( $this -> db_type == "mysql" ) {
2014-04-01 18:52:58 +00:00
$sql = "describe " . $table_name . ";" ;
}
2023-12-16 10:54:20 -04:00
$prep_statement = $this -> database -> db -> prepare ( $sql );
2014-04-01 18:52:58 +00:00
$prep_statement -> execute ();
return $prep_statement -> fetchAll ( PDO :: FETCH_ASSOC );
}
2014-07-15 07:58:01 +00:00
//database table exists alternate
2016-04-14 21:51:11 -06:00
private function db_table_exists_alternate ( $db_type , $table_name ) {
2014-07-15 07:58:01 +00:00
$sql = "select count(*) from $table_name " ;
2023-12-16 10:54:20 -04:00
$result = $this -> database -> query ( $sql );
2014-07-15 07:58:01 +00:00
if ( $result > 0 ) {
return true ; //table exists
}
else {
return false ; //table doesn't exist
}
}
//database table exists
2016-04-14 21:51:11 -06:00
private function db_table_exists ( $db_type , $db_name , $table_name ) {
2014-07-15 07:58:01 +00:00
$sql = "" ;
if ( $db_type == "sqlite" ) {
$sql .= "SELECT * FROM sqlite_master WHERE type='table' and name=' $table_name ' " ;
}
if ( $db_type == "pgsql" ) {
$sql .= "select * from pg_tables where schemaname='public' and tablename = ' $table_name ' " ;
}
if ( $db_type == "mysql" ) {
$sql .= "SELECT TABLE_NAME FROM information_schema.tables WHERE table_schema = ' $db_name ' and TABLE_NAME = ' $table_name ' " ;
}
2023-12-16 10:54:20 -04:00
$prep_statement = $this -> database -> db -> prepare ( check_sql ( $sql ));
2014-07-15 07:58:01 +00:00
$prep_statement -> execute ();
$result = $prep_statement -> fetchAll ( PDO :: FETCH_NAMED );
if ( count ( $result ) > 0 ) {
return true ; //table exists
}
else {
return false ; //table doesn't exist
}
}
//database table information
2016-04-14 21:51:11 -06:00
private function db_table_info ( $db_name , $db_type , $table_name ) {
2023-05-05 13:46:37 -03:00
if ( empty ( $table_name )) { return false ; }
2014-07-15 07:58:01 +00:00
if ( $db_type == "sqlite" ) {
$sql = "PRAGMA table_info(" . $table_name . ");" ;
}
if ( $db_type == "pgsql" ) {
$sql = "SELECT ordinal_position, " ;
$sql .= "column_name, " ;
$sql .= "data_type, " ;
$sql .= "column_default, " ;
$sql .= "is_nullable, " ;
$sql .= "character_maximum_length, " ;
$sql .= "numeric_precision " ;
$sql .= "FROM information_schema.columns " ;
$sql .= "WHERE table_name = '" . $table_name . "' " ;
$sql .= "and table_catalog = '" . $db_name . "' " ;
$sql .= "ORDER BY ordinal_position; " ;
}
if ( $db_type == "mysql" ) {
$sql = "describe " . $table_name . ";" ;
}
2023-12-16 10:54:20 -04:00
$prep_statement = $this -> database -> db -> prepare ( $sql );
2014-07-15 07:58:01 +00:00
$prep_statement -> execute ();
return $prep_statement -> fetchAll ( PDO :: FETCH_ASSOC );
}
//database type
private function db_data_type ( $db_type , $table_info , $column_name ) {
if ( $db_type == "sqlite" ) {
foreach ( $table_info as $key => & $row ) {
if ( $row [ 'name' ] == $column_name ) {
return $row [ 'type' ];
}
}
}
if ( $db_type == "pgsql" ) {
foreach ( $table_info as $key => & $row ) {
if ( $row [ 'column_name' ] == $column_name ) {
return $row [ 'data_type' ];
}
}
}
if ( $db_type == "mysql" ) {
foreach ( $table_info as $key => & $row ) {
if ( $row [ 'Field' ] == $column_name ) {
return $row [ 'Type' ];
}
}
}
}
//sqlite column exists
private function db_sqlite_column_exists ( $table_info , $column_name ) {
foreach ( $table_info as $key => & $row ) {
if ( $row [ 'name' ] == $column_name ) {
return true ;
}
}
return $false ;
}
//database column exists
2016-04-14 21:51:11 -06:00
private function db_column_exists ( $db_type , $db_name , $table_name , $column_name ) {
2014-07-15 07:58:01 +00:00
if ( $db_type == "sqlite" ) {
2016-04-14 21:51:11 -06:00
$table_info = $this -> db_table_info ( $db_name , $db_type , $table_name );
2014-07-15 07:58:01 +00:00
if ( $this -> db_sqlite_column_exists ( $table_info , $column_name )) {
return true ;
}
else {
return false ;
}
}
if ( $db_type == "pgsql" ) {
2023-05-12 16:22:50 -06:00
$sql = "SELECT attname FROM pg_attribute WHERE attrelid = (SELECT oid FROM pg_class WHERE relname = ' $table_name ' limit 1) AND attname = ' $column_name '; " ;
2014-07-15 07:58:01 +00:00
}
if ( $db_type == "mysql" ) {
//$sql .= "SELECT * FROM information_schema.COLUMNS where TABLE_SCHEMA = '$db_name' and TABLE_NAME = '$table_name' and COLUMN_NAME = '$column_name' ";
$sql = "show columns from $table_name where field = ' $column_name ' " ;
}
if ( $sql ) {
2023-12-16 10:54:20 -04:00
$prep_statement = $this -> database -> db -> prepare ( check_sql ( $sql ));
2014-07-15 07:58:01 +00:00
$prep_statement -> execute ();
$result = $prep_statement -> fetchAll ( PDO :: FETCH_NAMED );
2023-11-08 14:18:24 -07:00
if ( ! empty ( $result )) {
2014-07-15 07:58:01 +00:00
return true ;
}
else {
return false ;
}
unset ( $prep_statement );
}
}
//database column data type
2016-04-14 21:51:11 -06:00
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 );
2014-07-15 07:58:01 +00:00
return $this -> db_data_type ( $db_type , $table_info , $column_name );
}
//database create table
2014-07-26 22:48:32 +00:00
public function db_create_table ( $apps , $db_type , $table ) {
2023-07-10 18:33:18 -06:00
if ( empty ( $apps )) { return false ; }
2017-01-05 03:53:02 -07:00
if ( is_array ( $apps )) foreach ( $apps as $x => & $app ) {
2023-07-10 18:33:18 -06:00
if ( ! empty ( $app [ 'db' ]) && is_array ( $app [ 'db' ])) foreach ( $app [ 'db' ] as $y => $row ) {
if ( ! empty ( $row [ 'table' ][ 'name' ]) && is_array ( $row [ 'table' ][ 'name' ])) {
2019-02-12 08:47:44 -07:00
$table_name = $row [ 'table' ][ 'name' ][ 'text' ];
}
else {
$table_name = $row [ 'table' ][ 'name' ];
}
if ( $table_name == $table ) {
$sql = "CREATE TABLE " . $table_name . " ( \n " ;
2023-07-10 18:33:18 -06:00
( int ) $field_count = 0 ;
if ( ! empty ( $row [ 'fields' ]) && is_array ( $row [ 'fields' ])) foreach ( $row [ 'fields' ] as $field ) {
2023-05-13 17:31:27 -06:00
if ( ! empty ( $field [ 'deprecated' ]) && $field [ 'deprecated' ] == "true" ) {
2014-07-15 07:58:01 +00:00
//skip this row
}
else {
if ( $field_count > 0 ) { $sql .= ", \n " ; }
2023-07-10 18:33:18 -06:00
if ( ! empty ( $field [ 'name' ]) && is_array ( $field [ 'name' ])) {
2014-07-15 07:58:01 +00:00
$sql .= $field [ 'name' ][ 'text' ] . " " ;
}
else {
$sql .= $field [ 'name' ] . " " ;
}
2023-07-10 18:33:18 -06:00
if ( ! empty ( $field [ 'type' ]) && is_array ( $field [ 'type' ])) {
2014-07-15 07:58:01 +00:00
$sql .= $field [ 'type' ][ $db_type ];
}
else {
$sql .= $field [ 'type' ];
}
2023-07-10 18:33:18 -06:00
if ( ! empty ( $field [ 'key' ][ 'type' ]) && $field [ 'key' ][ 'type' ] == "primary" ) {
2014-07-15 07:58:01 +00:00
$sql .= " PRIMARY KEY" ;
}
$field_count ++ ;
}
}
2020-10-21 11:32:46 -06:00
$sql .= "); \n " ;
2014-07-15 07:58:01 +00:00
return $sql ;
}
}
}
}
//database insert
private function db_insert_into ( $apps , $db_type , $table ) {
2016-04-14 21:51:11 -06:00
global $db_name ;
2014-07-15 07:58:01 +00:00
foreach ( $apps as $x => & $app ) {
foreach ( $app [ 'db' ] as $y => $row ) {
2017-02-16 00:03:33 -07:00
if ( $row [ 'table' ][ 'name' ] == $table ) {
$sql = "INSERT INTO " . $row [ 'table' ][ 'name' ] . " (" ;
2014-07-15 07:58:01 +00:00
$field_count = 0 ;
foreach ( $row [ 'fields' ] as $field ) {
2023-05-13 17:31:27 -06:00
if ( ! empty ( $field [ 'deprecated' ]) && $field [ 'deprecated' ] == "true" ) {
2014-07-15 07:58:01 +00:00
//skip this field
}
else {
if ( $field_count > 0 ) { $sql .= "," ; }
if ( is_array ( $field [ 'name' ])) {
$sql .= $field [ 'name' ][ 'text' ];
}
else {
$sql .= $field [ 'name' ];
}
$field_count ++ ;
}
}
$sql .= ") \n " ;
$sql .= "SELECT " ;
$field_count = 0 ;
foreach ( $row [ 'fields' ] as $field ) {
2023-05-13 17:31:27 -06:00
if ( ! empty ( $field [ 'deprecated' ]) && $field [ 'deprecated' ] == "true" ) {
2014-07-15 07:58:01 +00:00
//skip this field
}
else {
if ( $field_count > 0 ) { $sql .= "," ; }
if ( is_array ( $field [ 'name' ])) {
if ( $field [ 'exists' ] == "false" ) {
if ( is_array ( $field [ 'name' ][ 'deprecated' ])) {
$found = false ;
foreach ( $field [ 'name' ][ 'deprecated' ] as $row ) {
2016-04-14 21:51:11 -06:00
if ( $this -> db_column_exists ( $db_type , $db_name , 'tmp_' . $table , $row )) {
2014-07-15 07:58:01 +00:00
$sql .= $row ;
$found = true ;
break ;
}
}
if ( ! $found ) { $sql .= "''" ; }
}
else {
2016-04-14 21:51:11 -06:00
if ( $this -> db_column_exists ( $db_type , $db_name , 'tmp_' . $table , $field [ 'name' ][ 'deprecated' ])) {
2014-07-15 07:58:01 +00:00
$sql .= $field [ 'name' ][ 'deprecated' ];
}
else {
$sql .= "''" ;
}
}
}
else {
$sql .= $field [ 'name' ][ 'text' ];
}
}
else {
$sql .= $field [ 'name' ];
}
$field_count ++ ;
}
}
2020-10-21 11:32:46 -06:00
$sql .= " FROM tmp_" . $table . "; \n " ;
2014-07-15 07:58:01 +00:00
return $sql ;
}
}
}
}
//datatase schema
2016-04-25 21:09:14 -05:00
public function schema ( $format = '' ) {
2023-11-08 14:18:24 -07:00
2016-04-25 21:09:14 -05:00
//set the global variable
2023-12-16 10:54:20 -04:00
global $text , $output_format ;
2020-10-21 14:28:15 -06:00
if ( $format == '' ) $format = $output_format ;
2015-11-27 19:59:50 -07:00
//get the db variables
2023-06-17 09:11:32 -06:00
//require_once "resources/classes/config.php";
//$config = new config;
//$config_exists = $config->exists();
//$config_path = $config->find();
//$config->get();
//$db_type = $config->db_type;
//$db_name = $config->db_name;
//$db_username = $config->db_username;
//$db_password = $config->db_password;
//$db_host = $config->db_host;
//$db_path = $config->db_path;
//$db_port = $config->db_port;
2015-01-18 10:33:34 +00:00
2022-10-10 16:35:14 -06:00
//includes files
2023-06-17 09:11:32 -06:00
require dirname ( __DIR__ , 2 ) . "/resources/require.php" ;
2014-07-15 07:58:01 +00:00
2014-07-16 04:30:49 +00:00
//add multi-lingual support
if ( ! isset ( $text )) {
2015-01-18 10:33:34 +00:00
$language = new text ;
$text = $language -> get ( null , 'core/upgrade' );
2014-07-16 04:30:49 +00:00
}
2014-07-15 07:58:01 +00:00
//PHP PDO check if table or column exists
//check if table exists
// SELECT * FROM sqlite_master WHERE type='table' AND name='v_cdr'
//check if column exists
// 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';
//check if table exists
// SELECT * FROM pg_tables WHERE schemaname='public' AND table_name = 'v_groups'
//check if column exists
// SELECT attname FROM pg_attribute WHERE attrelid = (SELECT oid FROM pg_class WHERE relname = 'v_cdr') AND attname = 'caller_id_name';
//mysql
//list all tables in the database
// SELECT TABLE_NAME FROM information_schema.tables WHERE table_schema = 'fusionpbx'
//check if table exists
// SELECT TABLE_NAME FROM information_schema.tables WHERE table_schema = 'fusionpbx' AND TABLE_NAME = 'v_groups'
//check if column exists
// SELECT * FROM information_schema.COLUMNS where TABLE_SCHEMA = 'fusionpbx' AND TABLE_NAME = 'v_cdr' AND COLUMN_NAME = 'context'
//oracle
//check if table exists
// SELECT TABLE_NAME FROM ALL_TABLES
//update the app db array add exists true or false
$sql = '' ;
2023-11-03 17:04:07 -03:00
foreach ( $this -> apps as $x => & $app ) {
2016-04-25 21:09:14 -05:00
if ( isset ( $app [ 'db' ])) foreach ( $app [ 'db' ] as $y => & $row ) {
2017-02-16 00:03:33 -07:00
if ( isset ( $row [ 'table' ][ 'name' ])) {
if ( is_array ( $row [ 'table' ][ 'name' ])) {
$table_name = $row [ 'table' ][ 'name' ][ 'text' ];
}
else {
$table_name = $row [ 'table' ][ 'name' ];
}
2014-07-15 07:58:01 +00:00
}
else {
2017-02-16 00:03:33 -07:00
//old array syntax
if ( is_array ( $row [ 'table' ])) {
$table_name = $row [ 'table' ][ 'text' ];
}
else {
$table_name = $row [ 'table' ];
}
2014-07-15 07:58:01 +00:00
}
2023-05-05 13:46:37 -03:00
if ( ! empty ( $table_name )) {
2022-10-11 17:10:39 -06:00
2014-07-15 07:58:01 +00:00
//check if the table exists
2016-04-14 21:51:11 -06:00
if ( $this -> db_table_exists ( $db_type , $db_name , $table_name )) {
2023-11-08 14:18:24 -07:00
$this -> apps [ $x ][ 'db' ][ $y ][ 'exists' ] = 'true' ;
2014-07-15 07:58:01 +00:00
}
else {
2023-11-08 14:18:24 -07:00
$this -> apps [ $x ][ 'db' ][ $y ][ 'exists' ] = 'false' ;
2014-07-15 07:58:01 +00:00
}
//check if the column exists
foreach ( $row [ 'fields' ] as $z => $field ) {
2023-05-13 17:31:27 -06:00
if ( ! empty ( $field [ 'deprecated' ]) && $field [ 'deprecated' ] == "true" ) {
2014-07-15 07:58:01 +00:00
//skip this field
}
else {
if ( is_array ( $field [ 'name' ])) {
$field_name = $field [ 'name' ][ 'text' ];
}
else {
$field_name = $field [ 'name' ];
}
2023-05-05 13:46:37 -03:00
if ( ! empty ( $field_name )) {
2016-04-14 21:51:11 -06:00
if ( $this -> db_column_exists ( $db_type , $db_name , $table_name , $field_name )) {
2014-07-15 07:58:01 +00:00
//found
2023-11-08 14:18:24 -07:00
$this -> apps [ $x ][ 'db' ][ $y ][ 'fields' ][ $z ][ 'exists' ] = 'true' ;
2014-07-15 07:58:01 +00:00
}
else {
//not found
2023-11-08 14:18:24 -07:00
$this -> apps [ $x ][ 'db' ][ $y ][ 'fields' ][ $z ][ 'exists' ] = 'false' ;
2014-07-15 07:58:01 +00:00
}
}
unset ( $field_name );
}
}
unset ( $table_name );
}
}
}
//prepare the variables
$sql_update = '' ;
//add missing tables and fields
2023-11-03 17:04:07 -03:00
foreach ( $this -> apps as $x => & $app ) {
2016-04-25 21:09:14 -05:00
if ( isset ( $app [ 'db' ])) foreach ( $app [ 'db' ] as $y => & $row ) {
2017-02-16 00:03:33 -07:00
if ( is_array ( $row [ 'table' ][ 'name' ])) {
$table_name = $row [ 'table' ][ 'name' ][ 'text' ];
2019-02-12 08:47:44 -07:00
if ( $this -> db_table_exists ( $db_type , $db_name , $row [ 'table' ][ 'name' ][ 'deprecated' ])) {
$row [ 'exists' ] = "false" ; //testing
if ( $db_type == "pgsql" ) {
$sql_update .= "ALTER TABLE " . $row [ 'table' ][ 'name' ][ 'deprecated' ] . " RENAME TO " . $row [ 'table' ][ 'name' ][ 'text' ] . "; \n " ;
}
if ( $db_type == "mysql" ) {
$sql_update .= "RENAME TABLE " . $row [ 'table' ][ 'name' ][ 'deprecated' ] . " TO " . $row [ 'table' ][ 'name' ][ 'text' ] . "; \n " ;
}
if ( $db_type == "sqlite" ) {
$sql_update .= "ALTER TABLE " . $row [ 'table' ][ 'name' ][ 'deprecated' ] . " RENAME TO " . $row [ 'table' ][ 'name' ][ 'text' ] . "; \n " ;
}
}
else {
if ( $this -> db_table_exists ( $db_type , $db_name , $row [ 'table' ][ 'name' ][ 'text' ])) {
$row [ 'exists' ] = "true" ;
}
else {
$row [ 'exists' ] = "false" ;
2023-11-03 17:04:07 -03:00
$sql_update .= $this -> db_create_table ( $this -> apps , $db_type , $row [ 'table' ][ 'name' ][ 'text' ]);
2019-02-12 08:47:44 -07:00
}
2014-07-15 07:58:01 +00:00
}
}
else {
2023-11-03 23:55:35 -06:00
if ( $this -> db_table_exists ( $db_type , $db_name , $row [ 'table' ][ 'name' ])) {
$row [ 'exists' ] = "true" ;
}
else {
$row [ 'exists' ] = "false" ;
}
2017-02-16 00:03:33 -07:00
$table_name = $row [ 'table' ][ 'name' ];
2014-07-15 07:58:01 +00:00
}
2023-11-03 23:55:35 -06:00
2014-07-15 07:58:01 +00:00
//check if the table exists
if ( $row [ 'exists' ] == "true" ) {
if ( count ( $row [ 'fields' ]) > 0 ) {
foreach ( $row [ 'fields' ] as $z => $field ) {
2023-05-13 17:31:27 -06:00
if ( ! empty ( $field [ 'deprecated' ]) && $field [ 'deprecated' ] == "true" ) {
2014-07-15 07:58:01 +00:00
//skip this field
}
else {
//get the data type
if ( is_array ( $field [ 'type' ])) {
$field_type = $field [ 'type' ][ $db_type ];
}
else {
$field_type = $field [ 'type' ];
}
//get the field name
if ( is_array ( $field [ 'name' ])) {
$field_name = $field [ 'name' ][ 'text' ];
}
else {
$field_name = $field [ 'name' ];
}
2019-07-27 16:53:42 -06:00
2023-11-08 14:18:24 -07:00
//check if the field exists
// if ($this->db_column_exists($db_type, $db_name, $table_name, $field_name)) {
// $field['exists'] = "true";
// }
// else {
// $field['exists'] = "false";
// }
2019-07-27 16:53:42 -06:00
//add or rename fields
2019-09-10 17:50:41 -06:00
if ( isset ( $field [ 'name' ][ 'deprecated' ]) && $this -> db_column_exists ( $db_type , $db_name , $table_name , $field [ 'name' ][ 'deprecated' ])) {
2019-07-27 16:53:42 -06:00
if ( $db_type == "pgsql" ) {
$sql_update .= "ALTER TABLE " . $table_name . " RENAME COLUMN " . $field [ 'name' ][ 'deprecated' ] . " to " . $field [ 'name' ][ 'text' ] . "; \n " ;
}
if ( $db_type == "mysql" ) {
$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 " ;
}
if ( $db_type == "sqlite" ) {
//a change has been made to the field name
2023-11-08 14:18:24 -07:00
$this -> apps [ $x ][ 'db' ][ $y ][ 'rebuild' ] = 'true' ;
2019-07-27 16:53:42 -06:00
}
2014-07-15 07:58:01 +00:00
}
2019-07-27 16:53:42 -06:00
else {
//find missing fields and add them
if ( $field [ 'exists' ] == "false" ) {
$sql_update .= "ALTER TABLE " . $table_name . " ADD " . $field_name . " " . $field_type . "; \n " ;
2014-07-15 07:58:01 +00:00
}
}
2019-07-27 16:53:42 -06:00
2014-07-15 07:58:01 +00:00
//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
2020-10-21 14:28:15 -06:00
if ( $this -> data_types ) {
2016-04-14 21:51:11 -06:00
$db_field_type = $this -> db_column_data_type ( $db_type , $db_name , $table_name , $field_name );
2014-07-15 07:58:01 +00:00
$field_type_array = explode ( "(" , $field_type );
$field_type = $field_type_array [ 0 ];
2023-05-05 13:46:37 -03:00
if ( trim ( $db_field_type ) != trim ( $field_type ) && ! empty ( $db_field_type )) {
2014-07-15 07:58:01 +00:00
if ( $db_type == "pgsql" ) {
if ( strtolower ( $field_type ) == "uuid" ) {
$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 {
2020-11-25 15:11:45 -07:00
//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" ) { }
//field type has changed
2014-07-15 07:58:01 +00:00
else {
2019-12-02 14:51:02 -07:00
switch ( $field_type ) {
2020-11-25 15:11:45 -07:00
case 'numeric' : $using = $field_name . "::numeric" ; break ;
2019-12-02 14:51:02 -07:00
case 'timestamp' :
2020-11-25 15:11:45 -07:00
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 );
2019-12-02 14:51:02 -07:00
}
2020-11-25 15:11:45 -07:00
$sql_update .= "ALTER TABLE " . $table_name . " ALTER COLUMN " . $field_name . " TYPE " . $field_type . " " . ( $using ? "USING " . $using : null ) . "; \n " ;
2014-07-15 07:58:01 +00:00
}
}
}
if ( $db_type == "mysql" ) {
$type = explode ( "(" , $db_field_type );
if ( $type [ 0 ] == $field_type ) {
//do nothing
}
2019-12-02 14:51:02 -07:00
else if ( $field_type == "numeric" && $type [ 0 ] == "decimal" ) {
2014-07-15 07:58:01 +00:00
//do nothing
}
else {
$sql_update .= "ALTER TABLE " . $table_name . " modify " . $field_name . " " . $field_type . "; \n " ;
}
unset ( $type );
}
if ( $db_type == "sqlite" ) {
//a change has been made to the field type
2023-11-08 14:18:24 -07:00
$this -> apps [ $x ][ 'db' ][ $y ][ 'rebuild' ] = 'true' ;
2014-07-15 07:58:01 +00:00
}
}
}
}
}
}
}
2023-11-03 23:55:35 -06:00
elseif ( ! is_array ( $row [ 'table' ][ 'name' ])) {
2014-07-15 07:58:01 +00:00
//create table
2023-11-03 23:55:35 -06:00
$sql_update .= $this -> db_create_table ( $this -> apps , $db_type , $row [ 'table' ][ 'name' ]);
2014-07-15 07:58:01 +00:00
}
}
}
//rebuild and populate the table
2023-11-03 17:04:07 -03:00
foreach ( $this -> apps as $x => & $app ) {
2016-04-25 21:09:14 -05:00
if ( isset ( $app [ 'db' ])) foreach ( $app [ 'db' ] as $y => & $row ) {
2017-02-16 00:03:33 -07:00
if ( is_array ( $row [ 'table' ][ 'name' ])) {
$table_name = $row [ 'table' ][ 'name' ][ 'text' ];
2014-07-15 07:58:01 +00:00
}
else {
2017-02-16 00:03:33 -07:00
$table_name = $row [ 'table' ][ 'name' ];
2014-07-15 07:58:01 +00:00
}
2023-05-13 17:31:27 -06:00
if ( ! empty ( $field [ 'rebuild' ]) && $row [ 'rebuild' ] == "true" ) {
2014-07-15 07:58:01 +00:00
if ( $db_type == "sqlite" ) {
//start the transaction
//$sql_update .= "BEGIN TRANSACTION;\n";
//rename the table
$sql_update .= "ALTER TABLE " . $table_name . " RENAME TO tmp_" . $table_name . "; \n " ;
//create the table
2023-11-03 17:04:07 -03:00
$sql_update .= $this -> db_create_table ( $this -> apps , $db_type , $table_name );
2014-07-15 07:58:01 +00:00
//insert the data into the new table
2023-11-03 17:04:07 -03:00
$sql_update .= $this -> db_insert_into ( $this -> apps , $db_type , $table_name );
2014-07-15 07:58:01 +00:00
//drop the old table
$sql_update .= "DROP TABLE tmp_" . $table_name . "; \n " ;
//commit the transaction
//$sql_update .= "COMMIT;\n";
}
}
}
}
// initialize response variable
$response = '' ;
//display results as html
2014-07-16 04:30:49 +00:00
if ( $format == "html" ) {
2014-07-15 07:58:01 +00:00
//show the database type
$response .= "<strong>" . $text [ 'header-database_type' ] . ": " . $db_type . "</strong><br /><br />" ;
//start the table
$response .= "<table width='100%' border='0' cellpadding='20' cellspacing='0'> \n " ;
//show the changes
2023-05-05 13:46:37 -03:00
if ( ! empty ( $sql_update )) {
2014-07-15 07:58:01 +00:00
$response .= "<tr> \n " ;
$response .= "<td class='row_style1' colspan='3'> \n " ;
$response .= "<br /> \n " ;
$response .= "<strong>" . $text [ 'label-sql_changes' ] . ":</strong><br /> \n " ;
$response .= "<pre> \n " ;
$response .= $sql_update ;
$response .= "</pre> \n " ;
$response .= "<br /> \n " ;
$response .= "</td> \n " ;
$response .= "</tr> \n " ;
}
//list all tables
$response .= "<tr> \n " ;
$response .= "<th>" . $text [ 'label-table' ] . "</th> \n " ;
$response .= "<th>" . $text [ 'label-exists' ] . "</th> \n " ;
$response .= "<th>" . $text [ 'label-details' ] . "</th> \n " ;
$response .= "<tr> \n " ;
//build the html while looping through the app db array
$sql = '' ;
2023-11-03 17:04:07 -03:00
foreach ( $this -> apps as & $app ) {
2016-04-25 21:09:14 -05:00
if ( isset ( $app [ 'db' ])) foreach ( $app [ 'db' ] as $row ) {
2017-02-16 00:03:33 -07:00
if ( is_array ( $row [ 'table' ][ 'name' ])) {
$table_name = $row [ 'table' ][ 'name' ][ 'text' ];
2014-07-15 07:58:01 +00:00
}
else {
2017-02-16 00:03:33 -07:00
$table_name = $row [ 'table' ][ 'name' ];
2014-07-15 07:58:01 +00:00
}
$response .= "<tr> \n " ;
//check if the table exists
if ( $row [ 'exists' ] == "true" ) {
$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 " ;
if ( count ( $row [ 'fields' ]) > 0 ) {
$response .= "<td class='row_style1'> \n " ;
//show the list of columns
$response .= "<table border='0' cellpadding='10' cellspacing='0'> \n " ;
$response .= "<tr> \n " ;
$response .= "<th>" . $text [ 'label-name' ] . "</th> \n " ;
$response .= "<th>" . $text [ 'label-type' ] . "</th> \n " ;
$response .= "<th>" . $text [ 'label-exists' ] . "</th> \n " ;
$response .= "</tr> \n " ;
foreach ( $row [ 'fields' ] as $field ) {
2023-05-13 17:31:27 -06:00
if ( ! empty ( $field [ 'deprecated' ]) && $field [ 'deprecated' ] == "true" ) {
2014-07-15 07:58:01 +00:00
//skip this field
}
else {
if ( is_array ( $field [ 'name' ])) {
$field_name = $field [ 'name' ][ 'text' ];
}
else {
$field_name = $field [ 'name' ];
}
if ( is_array ( $field [ 'type' ])) {
$field_type = $field [ 'type' ][ $db_type ];
}
else {
$field_type = $field [ 'type' ];
}
$response .= "<tr> \n " ;
$response .= "<td class='row_style1' width='200'>" . $field_name . "</td> \n " ;
$response .= "<td class='row_style1'>" . $field_type . "</td> \n " ;
if ( $field [ 'exists' ] == "true" ) {
$response .= "<td class='row_style0' style=''>" . $text [ 'option-true' ] . "</td> \n " ;
$response .= "<td> </td> \n " ;
}
else {
$response .= "<td class='row_style1' style='background-color:#444444;color:#CCCCCC;'>" . $text [ 'option-false' ] . "</td> \n " ;
$response .= "<td> </td> \n " ;
}
$response .= "</tr> \n " ;
}
}
$response .= " </table> \n " ;
$response .= "</td> \n " ;
}
}
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'> </td> \n " ;
}
$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 ;
2023-05-05 13:46:37 -03:00
if ( empty ( $sql_update ) && $format == "text" ) {
2014-07-15 07:58:01 +00:00
$response .= " " . $text [ 'label-schema' ] . ": " . $text [ 'label-no_change' ] . " \n " ;
}
else {
2014-07-16 04:30:49 +00:00
if ( $format == "text" ) {
2015-02-15 06:50:00 +00:00
$response .= " " . $text [ 'label-schema' ] . " \n " ;
2014-07-15 07:58:01 +00:00
}
2016-04-14 21:51:11 -06:00
//$this->db->beginTransaction();
2014-07-15 07:58:01 +00:00
$update_array = explode ( ";" , $sql_update );
foreach ( $update_array as $sql ) {
if ( strlen ( trim ( $sql ))) {
try {
2023-12-16 10:54:20 -04:00
$this -> database -> db -> query ( trim ( $sql ));
2014-07-16 04:30:49 +00:00
if ( $format == "text" ) {
2020-10-21 11:32:46 -06:00
$response .= " $sql ; \n " ;
2014-07-15 07:58:01 +00:00
}
}
catch ( PDOException $error ) {
2020-10-21 11:32:46 -06:00
$response .= " error: " . $error -> getMessage () . " sql: $sql \n " ;
2014-07-15 07:58:01 +00:00
}
}
}
2016-04-14 21:51:11 -06:00
//$this->db->commit();
2014-07-15 07:58:01 +00:00
$response .= " \n " ;
2023-11-03 17:04:07 -03:00
unset ( $sql_update , $sql );
2014-07-15 07:58:01 +00:00
}
//handle response
2014-07-16 04:30:49 +00:00
//if ($output == "echo") {
// echo $response;
//}
//else if ($output == "return") {
2014-07-15 07:58:01 +00:00
return $response ;
2014-07-16 04:30:49 +00:00
//}
2014-07-15 07:58:01 +00:00
} //end function
2016-04-14 23:23:14 -06:00
}
2014-07-15 07:58:01 +00:00
}
2013-01-23 13:49:51 +00:00
//example use
2013-07-06 07:13:36 +00:00
//require_once "resources/classes/schema.php";
2014-07-15 10:30:31 +00:00
//$obj = new schema;
//$obj->db_type = $db_type;
//$obj->schema();
//$result_array = $schema->obj['sql'];
2013-01-23 13:49:51 +00:00
//print_r($result_array);
2017-01-05 03:53:02 -07:00
?>
2023-11-08 14:18:24 -07:00