<?php
define ("TABLE_PARAMS", "Table_Params");
define ("TYPE_INTEGER", "integer");
define ("TYPE_STRING", "string");
define ("TYPE_TEXT", "text");
define ("TYPE_DATE", "date");
define ("TYPE_INTEGER_SIZE", 11);
define ("TYPE_STRING_SIZE", 100);
class tables{
var $scheme= array();
function scheme( $args= array() ){
$this->scheme= $args;
}
function scheme2sql(){
$sql= '';
foreach($this->scheme as $table_name=>$table_structure){
$sql.= "CREATE TABLE IF NOT EXISTS `{$table_name}` (\n";
//1 Структуру таблицы представим в виде Имени поля и параметров поля
foreach($table_structure as $field_name=>$pram){
//1.2 Если это не массив параметров таблицы
if($field_name!=TABLE_PARAMS){
if( is_array($pram) && (sizeof($pram)>0) ){
$sql.= "\t";
$sql.= "`{$field_name}` ";
// PARAMETER TYPE
if(isset($pram['type'])){
switch ($pram['type']) {
case TYPE_INTEGER:
$size= TYPE_INTEGER_SIZE;
if(isset($pram['size'])) $size= $pram['size'];
$sql.= "INT ( {$size} ) ";
break;
case TYPE_STRING:
$size= TYPE_STRING_SIZE;
if(isset($pram['size'])) $size= $pram['size'];
$sql.= "VARCHAR( {$size} ) ";
break;
case TYPE_TEXT:
$sql.= "TEXT ";
break;
}
}else exit("field `{$field_name}` have no type parameter");
// PARAMETER TYPE
// PARAMETER UNSIGNED
if(isset($pram['unsigned'])){
if($pram['unsigned']==true) $sql.= "UNSIGNED ";
}
// PARAMETER UNSIGNED
// PARAMETER NOT_NULL
if(isset($pram['not_null'])){
if($pram['not_null']==true) $sql.= "NOT NULL ";
}
// PARAMETER NOT_NULL
// PARAMETER DEFAULT
if(isset($pram['default'])){
$sql.= "DEFAULT '{$pram['default']}' ";
}
// PARAMETER DEFAULT
$sql.= ",\n";
}else{ exit('field have no params'); }
}//1.2
}//1
$table_param= $table_structure[TABLE_PARAMS];
// PRIMARY KEY
if(isset($table_param['primary']))
$sql.= "\n\tPRIMARY KEY ( `{$table_param['primary']}` )\n";
else $sql.= "\n\tPRIMARY KEY ( `Id` )\n";
// PRIMARY KEY
$sql.= ") ";
// DB TYPE
if(isset($table_param['type']))
$sql.= "TYPE = {$table_param['type']} ";
else $sql.= "TYPE = MYISAM ";
// DB TYPE
//CHARACTER_SET
if(isset($table_param['charset'])) $sql.= "CHARACTER SET {$table_param['charset']} ";
else $sql.= "CHARACTER SET cp1251 ";
//CHARACTER_SET
//COLLATE
if(isset($table_param['collate'])) $sql.= "COLLATE {$table_param['collate']} ";
else $sql.= "COLLATE cp1251_general_ci ";
//COLLATE
$sql.= ";\n\n";
}
return $sql;
}
function fields_rename( $args= array() ){
}
function fields_delete( $args= array() ){
}
function migrate( $args= array() ){
}
};
$tables= new tables();
$tables->scheme(
array(
'publications'=>array(
'Id'=>array('type'=>'integer', 'size'=>7, 'not_null'=>true, 'unsigned'=>true, 'default'=>1),
'Parent_Id'=>array('type'=>'integer', 'size'=>7, 'not_null'=>true, 'default'=>1),
'Meta'=>array('type'=>'string', 'size'=>200, 'not_null'=>true),
'Content'=>array('type'=>'text', 'not_null'=>true),
'Table_Params'=>array('primary'=>'Id', 'type'=>'MYISAM', 'charset'=>'cp1251', 'collate'=>'cp1251_general_ci')
),
'messages'=>array(
'Id'=>array('type'=>'integer', 'size'=>11, 'not_null'=>true, 'unsigned'=>true, 'default'=>1),
'From_User_Id'=>array('type'=>'integer', 'size'=>11, 'not_null'=>true, 'unsigned'=>true, 'default'=>1),
'For_User_Id'=>array('type'=>'integer', 'size'=>11, 'not_null'=>true, 'unsigned'=>true, 'default'=>1),
'Looked'=>array('type'=>'integer', 'size'=>1, 'not_null'=>true, 'unsigned'=>true, 'default'=>1),
'Text'=>array('type'=>'string', 'size'=>200, 'not_null'=>true),
'Table_Params'=>array('primary'=>'Id', 'type'=>'MYISAM', 'charset'=>'cp1251', 'collate'=>'cp1251_general_ci')
)
)
);
echo $tables->scheme2sql();
echo '<br />';
/*
in progress….
$tables->migrate( array('all_force'=>true, 'all_fields_data_migrate'=>true) );
$tables->fields_rename(
array(
'publications'=>array(
'parent_id'=>'parent',
'updated'=>'was_updated'
)
)
);
$tables->fields_delete(
array(
'publications'=>array(
'parent_id', 'updated'
)
)
);
*/
?>
Как думаете, из этого получится что то хорошее? ;)