Added creation of tables through sql library

This commit is contained in:
xevidos 2019-04-04 13:23:03 -04:00
parent cb362d58c2
commit 2d0f4e0286
2 changed files with 145 additions and 40 deletions

View File

@ -113,16 +113,16 @@ class sql_conversions {
"id" => array( "id" => array(
"mysql" => "PRIMARY KEY", "mysql" => "NOT NULL AUTO_INCREMENT PRIMARY KEY",
"pgsql" => "PRIMARY KEY", "pgsql" => "SERIAL PRIMARY KEY",
"sqlite" => "PRIMARY KEY", "sqlite" => "SERIAL PRIMARY KEY",
), ),
"key" => array( "key" => array(
"mysql" => "CONSTRAINT %table_name% UNIQUE(%fields%)", "mysql" => "KEY",
"pgsql" => "CONSTRAINT %table_name% UNIQUE(%fields%)", "pgsql" => "KEY",
"sqlite" => "CONSTRAINT %table_name% UNIQUE(%fields%)", "sqlite" => "KEY",
), ),
"auto increment" => array( "auto increment" => array(
@ -145,6 +145,13 @@ class sql_conversions {
"pgsql" => "NULL", "pgsql" => "NULL",
"sqlite" => "NULL", "sqlite" => "NULL",
), ),
"unique" => array(
"mysql" => "CONSTRAINT %constraint_name% UNIQUE ( %field_names% )",
"pgsql" => "UNIQUE",
"sqlite" => "UNIQUE",
),
); );
public $wraps = array( public $wraps = array(
@ -165,6 +172,101 @@ class sql_conversions {
"sqlite" => "\"", "sqlite" => "\"",
), ),
); );
public function find( $string, $substring ) {
$dbtype = DBTYPE;
$id_close = $this->wraps["close"][$dbtype];
$id_open = $this->wraps["open"][$dbtype];
$find_string = $this->actions["find"][$dbtype];
$find_string = str_replace( "%string%", $string, $find_string );
$find_string = str_replace( "%substring%", $substring, $find_string );
return $find_string;
}
public function table( $table_name, $fields, $attributes ) {
$dbtype = DBTYPE;
$id_close = $this->wraps["close"][$dbtype];
$id_open = $this->wraps["open"][$dbtype];
$query = "{$this->actions["create"][$dbtype]} {$table_name} (";
foreach( $fields as $id => $type ) {
$query .= "{$id} {$this->data_types[$type][$dbtype]}";
foreach( $attributes[$id] as $attribute ) {
$attribute_string = $this->specials["$attribute"][$dbtype];
if( $attribute == "unique" && $dbtype == "mysql" ) {
continue;
}
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 .= "{$id_open}field{$id_close},";
}
$fields_string = substr( $fields_string, 0, -1 );
$attribute_string = str_replace( "%fields%", $fields_string, $attribute_string );
}
$query .= " {$attribute_string}";
}
$query .= ",";
}
if( $dbtype == "mysql" ) {
$constraint_name = "";
$id_close = $this->wraps["close"][$dbtype];
$id_open = $this->wraps["open"][$dbtype];
$fields_string = "";
$unique_string = "";
foreach( $attributes as $id => $attributes ) {
if( in_array( "unique", $attributes ) ) {
if( $unique_string == "" ) {
$unique_string = $this->specials["unique"] . ",";
}
$fields_string .= "{$id_open}{$id}{$id_close},";
}
}
$unique_string = str_replace( "%constraint_name%", $constraint_name, $unique_string );
$unique_string = str_replace( "%field_names%", $fields_string, $unique_string );
$query .= $unique_string;
}
$query = substr( $query, 0, -1 );
$query .= ");";
return( $query );
}
public function tables( $tables ) {
$query = "";
foreach( $tables as $table_name => $table_data ) {
$query .= $this->table( $table_name, $table_data["fields"], $table_data["attributes"] );
}
return( $query );
}
} }
?> ?>

View File

@ -1,6 +1,6 @@
<?php <?php
require_once( "./class.sql.conversions.php" ); require_once( __DIR__ . "/class.sql.conversions.php" );
class sql { class sql {
@ -35,52 +35,55 @@ class sql {
return( $this->connection ); return( $this->connection );
} }
public static function create_table( $table_name, $fields=array(), $attributes=array() ) { public function create_table( $table_name, $fields=array(), $attributes=array() ) {
$dbtype = DBTYPE; $query = $this->conversions->table( $table_name, $fields, $attributes );
$identifier_close = $this->conversions->wraps["close"][$dbtype]; //$this->query( $query, array(), array(), null, "rowCount" );
$identifier_open = $this->conversions->wraps["open"][$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 ) { public function create_tables( $table ) {
$fields_string = ""; /**
Tables layout
array(
foreach( $fields as $field ) { "table_name" => array(
$fields_string .= "{$identifier_open}field{$identifier_close},"; "fields" => array(
}
$fields_string = substr( $fields_string, 0, -1 ); "id" => "int",
$attribute_string = str_replace( "%fields%", $fields_string, $attribute_string ); "test_field" => "string"
} ),
$query .= " {$attribute_string}"; "attributes" => array(
}
$query .= ",";
}
$query .= ");"; "id" => array( "id" ),
"test_field" => array( "not null" ),
)
),
"table2_name" => array(
"fields" => array(
"id" => "int",
"test_field" => "string"
),
"attributes" => array(
"id" => array( "id" ),
"test_field" => array( "not null" ),
)
)
);
*/
$query = $this->conversions->tables( $table );
echo var_dump( $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 );
$i = $i; return $i;
} }
public static function is_not_error( $i ) { public static function is_not_error( $i ) {