Started new sql library system, Started refactor of install process

This commit is contained in:
xevidos 2019-04-04 08:18:08 -04:00
parent b200d119e7
commit 9f993ce056
3 changed files with 242 additions and 1 deletions

View file

@ -0,0 +1,70 @@
<?php
class Install {
public $active = "";
public $config = "";
public $db_types = array();
public $projects = "";
public $sessions = "";
public $sql = null;
public $users = "";
public $workspace = "";
function __construct() {
$path = $_POST['path'];
$rel = str_replace( '/components/install/process.php', '', $_SERVER['REQUEST_URI'] );
$this->active = $path . "/data/active.php";
$this->config = $path . "/config.php";
$this->projects = $path . "/data/projects.php";
$this->sessions = $path . "/data/sessions";
$this->users = $path . "/data/users.php";
$this->workspace = $path . "/workspace";
$this->db_types = sql::db_types;
$this->check();
require_once( "../sql/class.sql.php" );
$this->sql = new sql();
}
function check() {
if ( ! ( defined( 'DBHOST' ) && defined( 'DBNAME' ) && defined( 'DBUSER' ) && defined( 'DBPASS' ) && defined( 'DBTYPE' ) ) ) {
define( 'DBHOST', $_POST["dbhost"] );
define( 'DBNAME', $_POST["dbname"] );
define( 'DBUSER', $_POST["dbuser"] );
define( 'DBPASS', $_POST["dbpass"] );
define( 'DBTYPE', $_POST["dbtype"] );
} else {
$this->JSEND( "The config file already exists.", "One or more of the following have already been set: {DBHOST},{DBNAME},{DBUSER},{DBPASS},{DBTYPE}," );
}
if( ! in_array( DBTYPE, $this->db_types ) ) {
$this->JSEND( "Invalid database. Please select one of the following: " . implode( ", ", $db_types ), addslashes( json_encode( array( $dbtype, $db_types ) ) ) );
}
}
function JSEND( $message, $error=null ) {
$message = array(
"message" => $message
);
if( ! $error === null ) {
$message["error"] = $error;
}
exit( json_encode( $message ) );
}
}
$Install = new Install();
?>

View file

@ -0,0 +1,130 @@
<?php
class sql_conversions {
public $actions = array(
"create" => array(
"mysql" => "CREATE TABLE IF NOT EXISTS",
"pgsql" => "CREATE TABLE IF NOT EXISTS",
"sqlite" => "CREATE TABLE IF NOT EXISTS",
),
"delete" => array(
"mysql" => "DELETE",
"pgsql" => "DELETE",
"sqlite" => "DELETE",
),
"select" => array(
"mysql" => "SELECT",
"pgsql" => "SELECT",
"sqlite" => "SELECT",
),
"update" => array(
"mysql" => "UPDATE",
"pgsql" => "UPDATE",
"sqlite" => "UPDATE",
),
);
public $comparisons = array(
"equal" => array(
"mysql" => "=",
"pgsql" => "=",
"sqlite" => "=",
),
"less than" => array(
"mysql" => "<",
"pgsql" => "<",
"sqlite" => "<",
),
"more than" => array(
"mysql" => ">",
"pgsql" => ">",
"sqlite" => ">",
),
"not" => array(
"mysql" => "!",
"pgsql" => "!",
"sqlite" => "!",
),
"not equal" => array(
"mysql" => "!=",
"pgsql" => "!=",
"sqlite" => "!=",
),
);
public $data_types = array(
"bool" => array(
"mysql" => "BOOL",
"pgsql" => "BOOL",
"sqlite" => "BOOL",
),
"int" => array(
"mysql" => "INT",
"pgsql" => "INT",
"sqlite" => "INT",
),
"string" => array(
"mysql" => "VARCHAR",
"pgsql" => "VARCHAR",
"sqlite" => "VARCHAR",
),
"text" => array(
"mysql" => "TEXT",
"pgsql" => "TEXT",
"sqlite" => "TEXT",
),
);
public $specials = array(
"id" => array(
"mysql" => "PRIMARY KEY",
"pgsql" => "PRIMARY KEY",
"sqlite" => "PRIMARY KEY",
),
"key" => array(
"mysql" => "CONSTRAINT %table_name% UNIQUE(%fields%)",
"pgsql" => "CONSTRAINT %table_name% UNIQUE(%fields%)",
"sqlite" => "CONSTRAINT %table_name% UNIQUE(%fields%)",
),
"auto increment" => array(
"mysql" => "AUTO_INCREMENT",
"pgsql" => "AUTO_INCREMENT",
"sqlite" => "AUTO_INCREMENT",
),
"not null" => array(
"mysql" => "NOT NULL",
"pgsql" => "NOT NULL",
"sqlite" => "NOT NULL",
),
"null" => array(
"mysql" => "NULL",
"pgsql" => "NULL",
"sqlite" => "NULL",
),
);
}
?>

View file

@ -1,15 +1,17 @@
<?php <?php
require_once( "./class.sql.conversions.php" );
class sql { class sql {
public $connection = null; public $connection = null;
public $conversions = null;
public $identifier_character = null; public $identifier_character = null;
protected static $instance = null; protected static $instance = null;
public function __construct() { public function __construct() {
$this->conversions = new sql_conversions();
} }
public function close() { public function close() {
@ -33,6 +35,45 @@ class sql {
return( $this->connection ); return( $this->connection );
} }
public static function create_table( $table_name, $fields=array(), $attributes=array() ) {
$dbtype = DBTYPE;
$query = "{$this->conversions->actions["create"][$dbtype]} {$table_name} (";
foreach( $fields as $id => $type ) {
$query .= "{$id} {$this->conversions->data_types[$type][$dbtype]}";
foreach( $attributes[$id] as $attribute ) {
$attribute_string = $this->conversions->specials["$attribute"];
if( ! strpos( $attribute_string, "%table_name%" ) === FALSE ) {
$attribute_string = str_replace( "%table_name%", $table_name, $attribute_string );
}
if( ! strpos( $attribute_string, "%fields%" ) === FALSE ) {
$fields_string = "";
foreach( $fields as $field ) {
$fields_string .= "field,";
}
$fields_string = substr( $fields_string, 0, -1 );
$attribute_string = str_replace( "%fields%", $fields_string, $attribute_string );
}
$query .= " {$attribute_string}";
}
$query .= ",";
}
$query .= ");";
}
public static function escape_identifier( $i ) { public static function escape_identifier( $i ) {
$i = preg_replace('/[^A-Za-z0-9_]+/', '', $i ); $i = preg_replace('/[^A-Za-z0-9_]+/', '', $i );