diff --git a/components/sql/class.sql.conversions.php b/components/sql/class.sql.conversions.php index 422f2cd..163f661 100644 --- a/components/sql/class.sql.conversions.php +++ b/components/sql/class.sql.conversions.php @@ -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 ); + } } ?> diff --git a/components/sql/class.sql.php b/components/sql/class.sql.php index 932a3bd..2ff6654 100755 --- a/components/sql/class.sql.php +++ b/components/sql/class.sql.php @@ -1,6 +1,6 @@ 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->table( $table_name, $fields, $attributes ); + //$this->query( $query, array(), array(), null, "rowCount" ); + } + + public function create_tables( $table ) { - $query = "{$this->conversions->actions["create"][$dbtype]} {$table_name} ("; - - foreach( $fields as $id => $type ) { + /** + Tables layout + array( - $query .= "{$id} {$this->conversions->data_types[$type][$dbtype]}"; - - foreach( $attributes[$id] as $attribute ) { + "table_name" => array( - $attribute_string = $this->conversions->specials["$attribute"]; + "fields" => array( + + "id" => "int", + "test_field" => "string" + ), + "attributes" => array( + + "id" => array( "id" ), + "test_field" => array( "not null" ), + ) + ), + "table2_name" => array( - if( ! strpos( $attribute_string, "%table_name%" ) === FALSE ) { + "fields" => array( - $attribute_string = str_replace( "%table_name%", $table_name, $attribute_string ); - } - - if( ! strpos( $attribute_string, "%fields%" ) === FALSE ) { + "id" => "int", + "test_field" => "string" + ), + "attributes" => array( - $fields_string = ""; - - foreach( $fields as $field ) { - - $fields_string .= "{$identifier_open}field{$identifier_close},"; - } - - $fields_string = substr( $fields_string, 0, -1 ); - $attribute_string = str_replace( "%fields%", $fields_string, $attribute_string ); - } - $query .= " {$attribute_string}"; - } - $query .= ","; - } - - $query .= ");"; + "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 ) {