2007-07-02 05:51:26 +00:00
< ? php
2008-12-12 14:03:02 +00:00
$partition_by = array ( 'HASH' , 'LINEAR HASH' , 'KEY' , 'LINEAR KEY' , 'RANGE' , 'LIST' );
2009-07-02 22:37:10 +00:00
$referencable_primary = referencable_primary ( $_GET [ " create " ]);
$foreign_keys = array ();
foreach ( $referencable_primary as $table_name => $field ) {
$foreign_keys [ idf_escape ( $table_name ) . " . " . idf_escape ( $field [ " field " ])] = $table_name ;
}
2008-07-24 12:03:28 +00:00
if ( strlen ( $_GET [ " create " ])) {
$orig_fields = fields ( $_GET [ " create " ]);
}
2008-05-07 11:22:08 +00:00
if ( $_POST && ! $error && ! $_POST [ " add " ] && ! $_POST [ " drop_col " ] && ! $_POST [ " up " ] && ! $_POST [ " down " ]) {
2007-07-02 16:21:04 +00:00
if ( $_POST [ " drop " ]) {
2008-09-02 08:42:14 +00:00
query_redirect ( " DROP TABLE " . idf_escape ( $_GET [ " create " ]), substr ( $SELF , 0 , - 1 ), lang ( 'Table has been dropped.' ));
2007-07-02 16:21:04 +00:00
} else {
2007-07-05 14:56:27 +00:00
$auto_increment_index = " PRIMARY KEY " ;
2009-06-21 23:20:32 +00:00
// don't overwrite primary key by auto_increment
2007-07-19 14:33:24 +00:00
if ( strlen ( $_GET [ " create " ]) && strlen ( $_POST [ " fields " ][ $_POST [ " auto_increment_col " ]][ " orig " ])) {
2007-07-05 14:56:27 +00:00
foreach ( indexes ( $_GET [ " create " ]) as $index ) {
foreach ( $index [ " columns " ] as $column ) {
2007-07-19 14:33:24 +00:00
if ( $column === $_POST [ " fields " ][ $_POST [ " auto_increment_col " ]][ " orig " ]) {
2007-07-05 14:56:27 +00:00
$auto_increment_index = " " ;
break 2 ;
}
}
if ( $index [ " type " ] == " PRIMARY " ) {
$auto_increment_index = " UNIQUE " ;
}
}
}
2007-07-02 16:21:04 +00:00
$fields = array ();
ksort ( $_POST [ " fields " ]);
2007-07-09 22:15:11 +00:00
$after = " FIRST " ;
2007-07-02 16:21:04 +00:00
foreach ( $_POST [ " fields " ] as $key => $field ) {
2009-07-02 22:37:10 +00:00
$type_field = ( isset ( $types [ $field [ " type " ]]) ? $field : $referencable_primary [ $foreign_keys [ $field [ " type " ]]]);
if ( strlen ( $field [ " field " ]) && $type_field ) {
$fields [] = " \n " . ( strlen ( $_GET [ " create " ]) ? ( strlen ( $field [ " orig " ]) ? " CHANGE " . idf_escape ( $field [ " orig " ]) . " " : " ADD " ) : " " )
. idf_escape ( $field [ " field " ]) . process_type ( $type_field )
2007-07-25 15:18:59 +00:00
. ( $field [ " null " ] ? " NULL " : " NOT NULL " ) // NULL for timestamp
2009-06-22 00:53:51 +00:00
. ( strlen ( $_GET [ " create " ]) && strlen ( $field [ " orig " ]) && isset ( $orig_fields [ $field [ " orig " ]][ " default " ]) && $field [ " type " ] != " timestamp " ? " DEFAULT " . $dbh -> quote ( $orig_fields [ $field [ " orig " ]][ " default " ]) : " " ) //! timestamp
2007-07-19 14:33:24 +00:00
. ( $key == $_POST [ " auto_increment_col " ] ? " AUTO_INCREMENT $auto_increment_index " : " " )
2009-06-22 00:53:51 +00:00
. " COMMENT " . $dbh -> quote ( $field [ " comment " ])
2008-05-07 11:22:08 +00:00
. ( strlen ( $_GET [ " create " ]) ? " $after " : " " )
2007-07-05 14:36:35 +00:00
;
2007-07-09 22:15:11 +00:00
$after = " AFTER " . idf_escape ( $field [ " field " ]);
2009-07-02 22:37:10 +00:00
if ( ! isset ( $types [ $field [ " type " ]])) {
$fields [] = ( strlen ( $_GET [ " create " ]) ? " ADD " : " " ) . " FOREIGN KEY ( " . idf_escape ( $field [ " field " ]) . " ) REFERENCES " . idf_escape ( $foreign_keys [ $field [ " type " ]]) . " ( " . idf_escape ( $type_field [ " field " ]) . " ) " ;
}
2007-07-04 14:39:45 +00:00
} elseif ( strlen ( $field [ " orig " ])) {
2009-06-05 08:30:21 +00:00
$fields [] = " \n DROP " . idf_escape ( $field [ " orig " ]);
2007-07-02 16:21:04 +00:00
}
2007-07-02 05:51:26 +00:00
}
2009-06-22 00:53:51 +00:00
$status = ( $_POST [ " Engine " ] ? " ENGINE= " . $dbh -> quote ( $_POST [ " Engine " ]) : " " )
. ( $_POST [ " Collation " ] ? " COLLATE " . $dbh -> quote ( $_POST [ " Collation " ]) : " " )
2007-07-19 14:33:24 +00:00
. ( strlen ( $_POST [ " Auto_increment " ]) ? " AUTO_INCREMENT= " . intval ( $_POST [ " Auto_increment " ]) : " " )
2009-06-22 00:53:51 +00:00
. " COMMENT= " . $dbh -> quote ( $_POST [ " Comment " ])
2007-07-09 06:12:22 +00:00
;
2008-12-12 14:03:02 +00:00
if ( in_array ( $_POST [ " partition_by " ], $partition_by )) {
$partitions = array ();
if ( $_POST [ " partition_by " ] == 'RANGE' || $_POST [ " partition_by " ] == 'LIST' ) {
foreach ( array_filter ( $_POST [ " partition_names " ]) as $key => $val ) {
$value = $_POST [ " partition_values " ][ $key ];
2009-06-22 05:19:26 +00:00
$partitions [] = " \n PARTITION " . idf_escape ( $val ) . " VALUES " . ( $_POST [ " partition_by " ] == 'RANGE' ? " LESS THAN " : " IN " ) . ( strlen ( $value ) ? " ( $value ) " : " MAXVALUE " ); //! SQL injection
2008-12-12 14:03:02 +00:00
}
}
2009-06-22 05:19:26 +00:00
$status .= " \n PARTITION BY $_POST[partition_by] ( $_POST[partition] ) " . ( $partitions ? " ( " . implode ( " , " , $partitions ) . " \n ) " : ( $_POST [ " partitions " ] ? " PARTITIONS " . intval ( $_POST [ " partitions " ]) : " " )); // $_POST["partition"] can be expression, not only column
2009-05-08 05:23:03 +00:00
} elseif ( $dbh -> server_info >= 5.1 && strlen ( $_GET [ " create " ])) {
2009-06-05 08:30:21 +00:00
$status .= " \n REMOVE PARTITIONING " ;
2008-12-12 14:03:02 +00:00
}
2008-09-02 08:42:14 +00:00
$location = $SELF . " table= " . urlencode ( $_POST [ " name " ]);
2007-07-02 16:21:04 +00:00
if ( strlen ( $_GET [ " create " ])) {
2009-06-05 08:30:21 +00:00
query_redirect ( " ALTER TABLE " . idf_escape ( $_GET [ " create " ]) . implode ( " , " , $fields ) . " , \n RENAME TO " . idf_escape ( $_POST [ " name " ]) . " , \n $status " , $location , lang ( 'Table has been altered.' ));
2007-07-02 16:21:04 +00:00
} else {
2009-06-14 20:45:28 +00:00
$path = preg_replace ( '~\\?.*~' , '' , $_SERVER [ " REQUEST_URI " ]);
2009-06-29 10:44:36 +00:00
setcookie ( " adminer_engine " , $_POST [ " Engine " ], gmmktime ( 0 , 0 , 0 , gmdate ( " n " ) + 1 ), $path );
2009-06-05 08:30:21 +00:00
query_redirect ( " CREATE TABLE " . idf_escape ( $_POST [ " name " ]) . " ( " . implode ( " , " , $fields ) . " \n ) $status " , $location , lang ( 'Table has been created.' ));
2007-08-04 19:38:01 +00:00
}
2007-07-02 05:51:26 +00:00
}
}
2008-04-10 14:37:10 +00:00
page_header (( strlen ( $_GET [ " create " ]) ? lang ( 'Alter table' ) : lang ( 'Create table' )), $error , array ( " table " => $_GET [ " create " ]), $_GET [ " create " ]);
2007-07-02 05:51:26 +00:00
2007-07-16 22:40:55 +00:00
$engines = array ();
2009-05-08 05:23:03 +00:00
$result = $dbh -> query ( " SHOW ENGINES " );
2007-07-16 22:40:55 +00:00
while ( $row = $result -> fetch_assoc ()) {
if ( $row [ " Support " ] == " YES " || $row [ " Support " ] == " DEFAULT " ) {
$engines [] = $row [ " Engine " ];
}
}
$result -> free ();
2007-07-02 05:51:26 +00:00
if ( $_POST ) {
2007-07-09 22:15:11 +00:00
$row = $_POST ;
2007-07-19 14:33:24 +00:00
if ( $row [ " auto_increment_col " ]) {
2008-05-30 07:08:11 +00:00
$row [ " fields " ][ $row [ " auto_increment_col " ]][ " auto_increment " ] = true ;
2007-07-09 06:12:22 +00:00
}
2008-05-30 07:08:11 +00:00
process_fields ( $row [ " fields " ]);
2007-07-02 05:51:26 +00:00
} elseif ( strlen ( $_GET [ " create " ])) {
2007-07-13 07:15:45 +00:00
$row = table_status ( $_GET [ " create " ]);
2008-10-03 12:40:20 +00:00
table_comment ( $row );
2007-07-02 16:21:04 +00:00
$row [ " name " ] = $_GET [ " create " ];
2008-07-24 12:03:28 +00:00
$row [ " fields " ] = array_values ( $orig_fields );
2009-05-08 05:23:03 +00:00
if ( $dbh -> server_info >= 5.1 ) {
2009-06-22 00:53:51 +00:00
$from = " FROM information_schema.PARTITIONS WHERE TABLE_SCHEMA = " . $dbh -> quote ( $_GET [ " db " ]) . " AND TABLE_NAME = " . $dbh -> quote ( $_GET [ " create " ]);
2009-05-08 05:23:03 +00:00
$result = $dbh -> query ( " SELECT PARTITION_METHOD, PARTITION_ORDINAL_POSITION, PARTITION_EXPRESSION $from ORDER BY PARTITION_ORDINAL_POSITION DESC LIMIT 1 " );
2008-12-12 14:03:02 +00:00
list ( $row [ " partition_by " ], $row [ " partitions " ], $row [ " partition " ]) = $result -> fetch_row ();
$result -> free ();
$row [ " partition_names " ] = array ();
$row [ " partition_values " ] = array ();
2009-05-08 05:23:03 +00:00
$result = $dbh -> query ( " SELECT PARTITION_NAME, PARTITION_DESCRIPTION $from AND PARTITION_NAME != '' ORDER BY PARTITION_ORDINAL_POSITION " );
2008-12-12 14:03:02 +00:00
while ( $row1 = $result -> fetch_assoc ()) {
$row [ " partition_names " ][] = $row1 [ " PARTITION_NAME " ];
$row [ " partition_values " ][] = $row1 [ " PARTITION_DESCRIPTION " ];
}
$result -> free ();
2009-05-31 14:26:54 +00:00
$row [ " partition_names " ][] = " " ;
2008-12-12 14:03:02 +00:00
}
2007-07-03 07:21:43 +00:00
} else {
2009-06-14 20:45:28 +00:00
$row = array (
2009-06-21 23:31:12 +00:00
" Engine " => $_COOKIE [ " adminer_engine " ],
2009-06-14 20:45:28 +00:00
" fields " => array ( array ( " field " => " " )),
" partition_names " => array ( " " ),
);
2007-07-09 06:12:22 +00:00
}
2007-07-05 14:36:35 +00:00
$collations = collations ();
2009-03-02 13:38:00 +00:00
$suhosin = floor ( extension_loaded ( " suhosin " ) ? ( min ( ini_get ( " suhosin.request.max_vars " ), ini_get ( " suhosin.post.max_vars " )) - 13 ) / 8 : 0 );
if ( $suhosin && count ( $row [ " fields " ]) > $suhosin ) {
echo " <p class='error'> " . htmlspecialchars ( lang ( 'Maximum number of allowed fields exceeded. Please increase %s and %s.' , 'suhosin.post.max_vars' , 'suhosin.request.max_vars' )) . " </p> \n " ;
}
2007-07-02 05:51:26 +00:00
?>
2007-07-09 14:47:46 +00:00
2007-07-05 14:36:35 +00:00
< form action = " " method = " post " id = " form " >
2007-07-02 05:51:26 +00:00
< p >
2007-07-02 16:21:04 +00:00
< ? php echo lang ( 'Table name' ); ?> : <input name="name" maxlength="64" value="<?php echo htmlspecialchars($row["name"]); ?>" />
2007-07-16 22:40:55 +00:00
< select name = " Engine " >< option value = " " > ( < ? php echo lang ( 'engine' ); ?> )</option><?php echo optionlist($engines, $row["Engine"]); ?></select>
2007-07-11 22:25:54 +00:00
< select name = " Collation " >< option value = " " > ( < ? php echo lang ( 'collation' ); ?> )</option><?php echo optionlist($collations, $row["Collation"]); ?></select>
2007-07-11 13:56:28 +00:00
< input type = " submit " value = " <?php echo lang('Save'); ?> " />
2007-07-02 05:51:26 +00:00
</ p >
2009-05-29 16:14:54 +00:00
< table cellspacing = " 0 " id = " edit-fields " >
2009-07-02 22:37:10 +00:00
< ? php $column_comments = edit_fields ( $row [ " fields " ], $collations , " TABLE " , $suhosin , $foreign_keys ); ?>
2007-07-16 23:08:55 +00:00
</ table >
2007-07-19 14:33:24 +00:00
< p >
< ? php echo lang ( 'Auto Increment' ); ?> : <input name="Auto_increment" size="4" value="<?php echo intval($row["Auto_increment"]); ?>" />
< ? php echo lang ( 'Comment' ); ?> : <input name="Comment" value="<?php echo htmlspecialchars($row["Comment"]); ?>" maxlength="60" />
2008-09-24 20:46:21 +00:00
< script type = " text/javascript " >// <! [ CDATA [
2008-10-01 11:39:13 +00:00
document . write ( '<label><input type="checkbox"<?php if ($column_comments) { ?> checked="checked"<?php } ?> onclick="column_comments_click(this.checked);" /><?php echo lang(' Show column comments '); ?></label>' );
2008-09-24 20:46:21 +00:00
// ]]></script>
2007-07-09 06:12:22 +00:00
</ p >
2007-07-02 05:51:26 +00:00
< p >
2007-07-09 06:12:22 +00:00
< input type = " hidden " name = " token " value = " <?php echo $token ; ?> " />
2007-07-02 10:49:36 +00:00
< input type = " submit " value = " <?php echo lang('Save'); ?> " />
2008-10-09 14:13:17 +00:00
< ? php if ( strlen ( $_GET [ " create " ])) { ?> <input type="submit" name="drop" value="<?php echo lang('Drop'); ?>"<?php echo $confirm; ?> /><?php } ?>
2007-07-02 05:51:26 +00:00
</ p >
2009-04-29 11:07:27 +00:00
< ? php
2009-05-08 05:23:03 +00:00
if ( $dbh -> server_info >= 5.1 ) {
2009-04-29 11:07:27 +00:00
$partition_table = ereg ( 'RANGE|LIST' , $row [ " partition_by " ]);
?>
2009-03-18 10:35:57 +00:00
< fieldset >< legend >< ? php echo lang ( 'Partition by' ); ?> </legend>
2008-12-12 14:03:02 +00:00
< p >
2009-05-29 16:14:54 +00:00
< select name = " partition_by " onchange = " partition_by_change(this); " >< option ></ option >< ? php echo optionlist ( $partition_by , $row [ " partition_by " ]); ?> </select>
2008-12-12 14:03:02 +00:00
( < input name = " partition " value = " <?php echo htmlspecialchars( $row["partition"] ); ?> " /> )
2009-04-29 11:07:27 +00:00
< ? php echo lang ( 'Partitions' ); ?> : <input name="partitions" size="2" value="<?php echo htmlspecialchars($row["partitions"]); ?>"<?php echo ($partition_table || !$row["partition_by"] ? " class='hidden'" : ""); ?> />
2008-12-12 14:03:02 +00:00
</ p >
2009-05-01 10:41:33 +00:00
< table cellspacing = " 0 " id = " partition-table " < ? php echo ( $partition_table ? " " : " class='hidden' " ); ?> >
2008-12-12 14:03:02 +00:00
< thead >< tr >< th >< ? php echo lang ( 'Partition name' ); ?> </th><th><?php echo lang('Values'); ?></th></tr></thead>
< ? php
foreach ( $row [ " partition_names " ] as $key => $val ) {
2009-06-03 09:17:20 +00:00
echo '<tr>' ;
echo '<td><input name="partition_names[]" value="' . htmlspecialchars ( $val ) . '"' . ( $key == count ( $row [ " partition_names " ]) - 1 ? ' onchange="partition_name_change(this);"' : '' ) . ' /></td>' ;
echo '<td><input name="partition_values[]" value="' . htmlspecialchars ( $row [ " partition_values " ][ $key ]) . '" /></td>' ;
echo " </tr> \n " ;
2008-12-12 14:03:02 +00:00
}
?>
</ table >
2009-03-18 10:35:57 +00:00
</ fieldset >
2008-12-12 14:03:02 +00:00
< ? php } ?>
2007-07-02 05:51:26 +00:00
</ form >