mirror of
https://github.com/xevidos/codiad.git
synced 2024-11-10 21:26:35 +01:00
Added creation of tables through sql library
This commit is contained in:
parent
cb362d58c2
commit
2d0f4e0286
@ -113,16 +113,16 @@ class sql_conversions {
|
||||
|
||||
"id" => array(
|
||||
|
||||
"mysql" => "PRIMARY KEY",
|
||||
"pgsql" => "PRIMARY KEY",
|
||||
"sqlite" => "PRIMARY KEY",
|
||||
"mysql" => "NOT NULL AUTO_INCREMENT PRIMARY KEY",
|
||||
"pgsql" => "SERIAL PRIMARY KEY",
|
||||
"sqlite" => "SERIAL PRIMARY KEY",
|
||||
),
|
||||
|
||||
"key" => array(
|
||||
|
||||
"mysql" => "CONSTRAINT %table_name% UNIQUE(%fields%)",
|
||||
"pgsql" => "CONSTRAINT %table_name% UNIQUE(%fields%)",
|
||||
"sqlite" => "CONSTRAINT %table_name% UNIQUE(%fields%)",
|
||||
"mysql" => "KEY",
|
||||
"pgsql" => "KEY",
|
||||
"sqlite" => "KEY",
|
||||
),
|
||||
|
||||
"auto increment" => array(
|
||||
@ -145,6 +145,13 @@ class sql_conversions {
|
||||
"pgsql" => "NULL",
|
||||
"sqlite" => "NULL",
|
||||
),
|
||||
|
||||
"unique" => array(
|
||||
|
||||
"mysql" => "CONSTRAINT %constraint_name% UNIQUE ( %field_names% )",
|
||||
"pgsql" => "UNIQUE",
|
||||
"sqlite" => "UNIQUE",
|
||||
),
|
||||
);
|
||||
|
||||
public $wraps = array(
|
||||
@ -165,6 +172,101 @@ class sql_conversions {
|
||||
"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 );
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
|
@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
require_once( "./class.sql.conversions.php" );
|
||||
require_once( __DIR__ . "/class.sql.conversions.php" );
|
||||
|
||||
class sql {
|
||||
|
||||
@ -35,52 +35,55 @@ class sql {
|
||||
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;
|
||||
$identifier_close = $this->conversions->wraps["close"][$dbtype];
|
||||
$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 );
|
||||
$query = $this->conversions->table( $table_name, $fields, $attributes );
|
||||
//$this->query( $query, array(), array(), null, "rowCount" );
|
||||
}
|
||||
|
||||
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 );
|
||||
$attribute_string = str_replace( "%fields%", $fields_string, $attribute_string );
|
||||
}
|
||||
$query .= " {$attribute_string}";
|
||||
}
|
||||
$query .= ",";
|
||||
}
|
||||
"id" => "int",
|
||||
"test_field" => "string"
|
||||
),
|
||||
"attributes" => array(
|
||||
|
||||
$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 ) {
|
||||
|
||||
$i = preg_replace('/[^A-Za-z0-9_]+/', '', $i );
|
||||
$i = $i;
|
||||
return $i;
|
||||
}
|
||||
|
||||
public static function is_not_error( $i ) {
|
||||
|
Loading…
Reference in New Issue
Block a user