2007-07-02 05:51:26 +00:00
< ? php
2009-08-29 13:57:50 +00:00
$TABLE = $_GET [ " create " ];
2013-07-19 19:37:12 +00:00
$partition_by = array ();
foreach ( array ( 'HASH' , 'LINEAR HASH' , 'KEY' , 'LINEAR KEY' , 'RANGE' , 'LIST' ) as $key ) {
$partition_by [ $key ] = $key ;
}
2008-12-12 14:03:02 +00:00
2009-08-29 13:57:50 +00:00
$referencable_primary = referencable_primary ( $TABLE );
2009-07-02 22:37:10 +00:00
$foreign_keys = array ();
foreach ( $referencable_primary as $table_name => $field ) {
2010-05-20 20:06:34 +00:00
$foreign_keys [ str_replace ( " ` " , " `` " , $table_name ) . " ` " . str_replace ( " ` " , " `` " , $field [ " field " ])] = $table_name ; // not idf_escape() - used in JS
2009-07-02 22:37:10 +00:00
}
2009-09-09 19:47:15 +00:00
$orig_fields = array ();
2013-06-24 16:08:26 +00:00
$table_status = array ();
2010-01-09 23:33:41 +00:00
if ( $TABLE != " " ) {
2009-08-29 13:57:50 +00:00
$orig_fields = fields ( $TABLE );
2013-06-24 16:08:26 +00:00
$table_status = table_status ( $TABLE );
if ( ! $table_status ) {
$error = lang ( 'No tables.' );
}
2008-07-24 12:03:28 +00:00
}
2013-05-08 15:43:15 +00:00
$row = $_POST ;
2023-12-04 21:03:59 +00:00
if ( isset ( $row [ " fields " ])) {
$row [ " fields " ] = ( array ) $row [ " fields " ];
}
if ( isset ( $row [ " auto_increment_col " ]) && $row [ " auto_increment_col " ]) {
2013-05-08 18:43:53 +00:00
$row [ " fields " ][ $row [ " auto_increment_col " ]][ " auto_increment " ] = true ;
}
2008-07-24 12:03:28 +00:00
2019-01-22 12:13:27 +00:00
if ( $_POST ) {
2023-12-04 21:03:59 +00:00
$array = [];
if ( isset ( $_POST [ " comments " ])) {
$array [ " comments " ] = $_POST [ " comments " ];
}
if ( isset ( $_POST [ " defaults " ])) {
$array [ " defaults " ] = $_POST [ " defaults " ];
}
set_adminer_settings ( $array );
2019-01-22 12:13:27 +00:00
}
2013-05-08 18:43:53 +00:00
if ( $_POST && ! process_fields ( $row [ " fields " ]) && ! $error ) {
2023-12-04 21:03:59 +00:00
if ( isset ( $_POST [ " drop " ]) && $_POST [ " drop " ]) {
2013-07-03 19:32:52 +00:00
queries_redirect ( substr ( ME , 0 , - 1 ), lang ( 'Table has been dropped.' ), drop_tables ( array ( $TABLE )));
2010-03-01 22:59:08 +00:00
} else {
2010-04-21 12:01:32 +00:00
$fields = array ();
2012-07-15 21:38:45 +00:00
$all_fields = array ();
$use_all_fields = false ;
2010-04-21 12:01:32 +00:00
$foreign = array ();
2010-03-01 22:59:08 +00:00
$orig_field = reset ( $orig_fields );
2012-07-15 21:38:45 +00:00
$after = " FIRST " ;
2013-07-24 23:26:41 +00:00
2013-05-08 15:43:15 +00:00
foreach ( $row [ " fields " ] as $key => $field ) {
2023-12-04 21:03:59 +00:00
$foreign_key = null ;
if ( isset ( $field [ " type " ]) && isset ( $foreign_keys [ $field [ " type " ]])) {
$foreign_key = $foreign_keys [ $field [ " type " ]];
}
2012-05-14 06:54:07 +00:00
$type_field = ( $foreign_key !== null ? $referencable_primary [ $foreign_key ] : $field ); //! can collide with user defined type
2010-03-01 22:59:08 +00:00
if ( $field [ " field " ] != " " ) {
2023-12-04 21:03:59 +00:00
if ( isset ( $field [ " has_default " ]) === false || ! $field [ " has_default " ]) {
2010-05-21 13:07:59 +00:00
$field [ " default " ] = null ;
}
2013-05-08 15:43:15 +00:00
if ( $key == $row [ " auto_increment_col " ]) {
2010-05-21 13:07:59 +00:00
$field [ " auto_increment " ] = true ;
}
$process_field = process_field ( $field , $type_field );
2012-07-15 21:38:45 +00:00
$all_fields [] = array ( $field [ " orig " ], $process_field , $after );
2021-02-08 09:33:45 +00:00
if ( ! $orig_field || $process_field != process_field ( $orig_field , $orig_field )) {
2010-05-21 13:07:59 +00:00
$fields [] = array ( $field [ " orig " ], $process_field , $after );
2012-07-15 21:38:45 +00:00
if ( $field [ " orig " ] != " " || $after ) {
$use_all_fields = true ;
}
2010-05-21 13:07:59 +00:00
}
2012-05-14 06:54:07 +00:00
if ( $foreign_key !== null ) {
2013-08-09 22:16:15 +00:00
$foreign [ idf_escape ( $field [ " field " ])] = ( $TABLE != " " && $jush != " sqlite " ? " ADD " : " " ) . format_foreign_key ( array (
'table' => $foreign_keys [ $field [ " type " ]],
'source' => array ( $field [ " field " ]),
'target' => array ( $type_field [ " field " ]),
'on_delete' => $field [ " on_delete " ],
));
2010-03-01 22:59:08 +00:00
}
2012-07-15 21:38:45 +00:00
$after = " AFTER " . idf_escape ( $field [ " field " ]);
2010-03-01 22:59:08 +00:00
} elseif ( $field [ " orig " ] != " " ) {
2012-07-15 21:38:45 +00:00
$use_all_fields = true ;
2010-04-21 12:01:32 +00:00
$fields [] = array ( $field [ " orig " ]);
2010-03-01 22:59:08 +00:00
}
if ( $field [ " orig " ] != " " ) {
$orig_field = next ( $orig_fields );
2012-07-15 21:38:45 +00:00
if ( ! $orig_field ) {
$after = " " ;
}
2010-03-01 22:59:08 +00:00
}
2008-12-12 14:03:02 +00:00
}
2013-07-24 23:26:41 +00:00
2010-04-21 12:01:32 +00:00
$partitioning = " " ;
2013-07-19 19:37:12 +00:00
if ( $partition_by [ $row [ " partition_by " ]]) {
2010-03-01 22:59:08 +00:00
$partitions = array ();
2013-05-08 15:43:15 +00:00
if ( $row [ " partition_by " ] == 'RANGE' || $row [ " partition_by " ] == 'LIST' ) {
foreach ( array_filter ( $row [ " partition_names " ]) as $key => $val ) {
$value = $row [ " partition_values " ][ $key ];
2013-05-08 19:13:04 +00:00
$partitions [] = " \n PARTITION " . idf_escape ( $val ) . " VALUES " . ( $row [ " partition_by " ] == 'RANGE' ? " LESS THAN " : " IN " ) . ( $value != " " ? " ( $value ) " : " MAXVALUE " ); //! SQL injection
2010-03-01 22:59:08 +00:00
}
2009-07-23 16:31:28 +00:00
}
2013-05-08 15:43:15 +00:00
$partitioning .= " \n PARTITION BY $row[partition_by] ( $row[partition] ) " . ( $partitions // $row["partition"] can be expression, not only column
2010-03-01 22:59:08 +00:00
? " ( " . implode ( " , " , $partitions ) . " \n ) "
2013-05-08 15:43:15 +00:00
: ( $row [ " partitions " ] ? " PARTITIONS " . ( + $row [ " partitions " ]) : " " )
2010-03-01 22:59:08 +00:00
);
2013-07-24 23:26:41 +00:00
} elseif ( support ( " partitioning " ) && preg_match ( " ~partitioned~ " , $table_status [ " Create_options " ])) {
2010-04-21 12:01:32 +00:00
$partitioning .= " \n REMOVE PARTITIONING " ;
2010-03-01 22:59:08 +00:00
}
2013-07-24 23:26:41 +00:00
2010-04-21 12:01:32 +00:00
$message = lang ( 'Table has been altered.' );
if ( $TABLE == " " ) {
2013-05-08 15:43:15 +00:00
cookie ( " adminer_engine " , $row [ " Engine " ]);
2010-04-21 12:01:32 +00:00
$message = lang ( 'Table has been created.' );
2007-08-04 19:38:01 +00:00
}
2013-05-08 15:43:15 +00:00
$name = trim ( $row [ " name " ]);
2013-07-24 23:26:41 +00:00
2013-07-06 17:31:21 +00:00
queries_redirect ( ME . ( support ( " table " ) ? " table= " : " select= " ) . urlencode ( $name ), $message , alter_table (
2010-04-21 12:01:32 +00:00
$TABLE ,
2011-09-10 11:06:59 +00:00
$name ,
2012-07-15 21:38:45 +00:00
( $jush == " sqlite " && ( $use_all_fields || $foreign ) ? $all_fields : $fields ),
2010-04-21 12:01:32 +00:00
$foreign ,
2014-11-18 17:50:02 +00:00
( $row [ " Comment " ] != $table_status [ " Comment " ] ? $row [ " Comment " ] : null ),
2013-06-24 16:08:26 +00:00
( $row [ " Engine " ] && $row [ " Engine " ] != $table_status [ " Engine " ] ? $row [ " Engine " ] : " " ),
( $row [ " Collation " ] && $row [ " Collation " ] != $table_status [ " Collation " ] ? $row [ " Collation " ] : " " ),
2014-11-25 02:17:43 +00:00
( $row [ " Auto_increment " ] != " " ? number ( $row [ " Auto_increment " ]) : " " ),
2010-04-21 12:01:32 +00:00
$partitioning
));
2007-07-02 05:51:26 +00:00
}
}
2009-07-20 12:12:55 +00:00
2013-07-05 08:31:53 +00:00
page_header (( $TABLE != " " ? lang ( 'Alter table' ) : lang ( 'Create table' )), $error , array ( " table " => $TABLE ), h ( $TABLE ));
2007-07-02 05:51:26 +00:00
2013-05-08 18:43:53 +00:00
if ( ! $_POST ) {
2013-05-08 15:43:15 +00:00
$row = array (
2023-12-04 21:03:59 +00:00
" Engine " => ( isset ( $_COOKIE [ " adminer_engine " ]) ? $_COOKIE [ " adminer_engine " ] : null ),
2018-02-01 12:55:11 +00:00
" fields " => array ( array ( " field " => " " , " type " => ( isset ( $types [ " int " ]) ? " int " : ( isset ( $types [ " integer " ]) ? " integer " : " " )), " on_update " => " " )),
2013-05-08 15:43:15 +00:00
" partition_names " => array ( " " ),
);
2013-07-24 23:26:41 +00:00
2013-05-08 18:43:53 +00:00
if ( $TABLE != " " ) {
2013-06-24 16:08:26 +00:00
$row = $table_status ;
2013-05-08 18:43:53 +00:00
$row [ " name " ] = $TABLE ;
$row [ " fields " ] = array ();
2023-12-04 21:03:59 +00:00
if ( isset ( $_GET [ " auto_increment " ]) === false || ! $_GET [ " auto_increment " ]) { // don't prefill by original Auto_increment for the sake of performance and not reusing deleted ids
2013-05-08 18:43:53 +00:00
$row [ " Auto_increment " ] = " " ;
}
foreach ( $orig_fields as $field ) {
$field [ " has_default " ] = isset ( $field [ " default " ]);
$row [ " fields " ][] = $field ;
}
2013-07-24 23:26:41 +00:00
2013-05-08 18:43:53 +00:00
if ( support ( " partitioning " )) {
$from = " FROM information_schema.PARTITIONS WHERE TABLE_SCHEMA = " . q ( DB ) . " AND TABLE_NAME = " . q ( $TABLE );
$result = $connection -> query ( " SELECT PARTITION_METHOD, PARTITION_ORDINAL_POSITION, PARTITION_EXPRESSION $from ORDER BY PARTITION_ORDINAL_POSITION DESC LIMIT 1 " );
list ( $row [ " partition_by " ], $row [ " partitions " ], $row [ " partition " ]) = $result -> fetch_row ();
2013-05-08 19:13:04 +00:00
$partitions = get_key_vals ( " SELECT PARTITION_NAME, PARTITION_DESCRIPTION $from AND PARTITION_NAME != '' ORDER BY PARTITION_ORDINAL_POSITION " );
$partitions [ " " ] = " " ;
$row [ " partition_names " ] = array_keys ( $partitions );
$row [ " partition_values " ] = array_values ( $partitions );
2013-05-08 18:43:53 +00:00
}
}
2007-07-09 06:12:22 +00:00
}
2009-03-02 13:38:00 +00:00
2013-05-02 01:28:04 +00:00
$collations = collations ();
2009-10-16 12:26:16 +00:00
$engines = engines ();
2009-09-13 22:17:56 +00:00
// case of engine may differ
foreach ( $engines as $engine ) {
if ( ! strcasecmp ( $engine , $row [ " Engine " ])) {
$row [ " Engine " ] = $engine ;
break ;
}
}
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 >
2013-08-08 20:24:20 +00:00
< ? php if ( support ( " columns " ) || $TABLE == " " ) { ?>
2018-07-15 19:05:26 +00:00
< ? php echo lang ( 'Table name' ); ?> : <input name="name" data-maxlength="64" value="<?php echo h($row["name"]); ?>" autocapitalize="off">
2018-01-12 14:27:44 +00:00
< ? php if ( $TABLE == " " && ! $_POST ) { echo script ( " focus(qs('#form')['name']); " ); } ?>
2018-01-12 17:11:00 +00:00
< ? php echo ( $engines ? " <select name='Engine'> " . optionlist ( array ( " " => " ( " . lang ( 'engine' ) . " ) " ) + $engines , $row [ " Engine " ]) . " </select> " . on_help ( " getTarget(event).value " , 1 ) . script ( " qsl('select').onchange = helpClose; " ) : " " ); ?>
2013-07-24 23:26:41 +00:00
< ? php echo ( $collations && ! preg_match ( " ~sqlite|mssql~ " , $jush ) ? html_select ( " Collation " , array ( " " => " ( " . lang ( 'collation' ) . " ) " ) + $collations , $row [ " Collation " ]) : " " ); ?>
2013-07-10 20:13:36 +00:00
< input type = " submit " value = " <?php echo lang('Save'); ?> " >
2013-07-06 17:31:21 +00:00
< ? php } ?>
2013-08-08 20:24:20 +00:00
< ? php if ( support ( " columns " )) { ?>
2018-10-27 19:20:56 +00:00
< div class = " scrollable " >
2010-01-28 15:17:20 +00:00
< table cellspacing = " 0 " id = " edit-fields " class = " nowrap " >
2011-06-07 13:00:18 +00:00
< ? php
2019-01-22 12:13:27 +00:00
edit_fields ( $row [ " fields " ], $collations , " TABLE " , $foreign_keys );
2011-06-07 13:00:18 +00:00
?>
2007-07-16 23:08:55 +00:00
</ table >
2019-12-20 12:50:20 +00:00
< ? php echo script ( " editFields(); " ); ?>
2018-10-25 10:31:28 +00:00
</ div >
2007-07-19 14:33:24 +00:00
< p >
2013-01-31 05:35:51 +00:00
< ? php echo lang ( 'Auto Increment' ); ?> : <input type="number" name="Auto_increment" size="6" value="<?php echo h($row["Auto_increment"]); ?>">
2019-01-22 12:13:27 +00:00
< ? php echo checkbox ( " defaults " , 1 , ( $_POST ? $_POST [ " defaults " ] : adminer_setting ( " defaults " )), lang ( 'Default values' ), " columnShow(this.checked, 5) " , " jsonly " ); ?>
2021-05-23 09:38:04 +00:00
< ? php
$comments = ( $_POST ? $_POST [ " comments " ] : adminer_setting ( " comments " ));
echo ( support ( " comment " )
? checkbox ( " comments " , 1 , $comments , lang ( 'Comment' ), " editingCommentsClick(this, true); " , " jsonly " )
. ' ' . ( preg_match ( '~\n~' , $row [ " Comment " ])
? " <textarea name='Comment' rows='2' cols='20' " . ( $comments ? " " : " class='hidden' " ) . " > " . h ( $row [ " Comment " ]) . " </textarea> "
: '<input name="Comment" value="' . h ( $row [ " Comment " ]) . '" data-maxlength="' . ( min_version ( 5.5 ) ? 2048 : 60 ) . '"' . ( $comments ? " " : " class='hidden' " ) . '>'
)
2013-05-20 17:06:23 +00:00
: '' )
2021-05-23 09:38:04 +00:00
;
?>
2007-07-02 05:51:26 +00:00
< p >
2013-07-10 20:13:36 +00:00
< input type = " submit " value = " <?php echo lang('Save'); ?> " >
2013-07-06 17:31:21 +00:00
< ? php } ?>
2018-01-24 17:36:19 +00:00
< ? php if ( $TABLE != " " ) { ?> <input type="submit" name="drop" value="<?php echo lang('Drop'); ?>"><?php echo confirm(lang('Drop %s?', $TABLE)); ?><?php } ?>
2009-04-29 11:07:27 +00:00
< ? php
2010-04-21 12:01:32 +00:00
if ( support ( " partitioning " )) {
2013-07-24 23:26:41 +00:00
$partition_table = preg_match ( '~RANGE|LIST~' , $row [ " partition_by " ]);
2010-02-24 10:39:44 +00:00
print_fieldset ( " partition " , lang ( 'Partition by' ), $row [ " partition_by " ]);
2009-04-29 11:07:27 +00:00
?>
2008-12-12 14:03:02 +00:00
< p >
2018-01-12 17:11:00 +00:00
< ? php echo " <select name='partition_by'> " . optionlist ( array ( " " => " " ) + $partition_by , $row [ " partition_by " ]) . " </select> " . on_help ( " getTarget(event).value.replace(/./, 'PARTITION BY \$ &') " , 1 ) . script ( " qsl('select').onchange = partitionByChange; " ); ?>
2009-07-28 16:20:50 +00:00
( < input name = " partition " value = " <?php echo h( $row["partition"] ); ?> " > )
2013-07-11 01:27:49 +00:00
< ? php echo lang ( 'Partitions' ); ?> : <input type="number" name="partitions" class="size<?php echo ($partition_table || !$row["partition_by"] ? " hidden" : ""); ?>" value="<?php echo h($row["partitions"]); ?>">
2009-05-01 10:41:33 +00:00
< table cellspacing = " 0 " id = " partition-table " < ? php echo ( $partition_table ? " " : " class='hidden' " ); ?> >
2009-07-11 20:30:40 +00:00
< thead >< tr >< th >< ? php echo lang ( 'Partition name' ); ?> <th><?php echo lang('Values'); ?></thead>
2008-12-12 14:03:02 +00:00
< ? php
2010-05-20 20:06:34 +00:00
foreach ( $row [ " partition_names " ] as $key => $val ) {
echo '<tr>' ;
2018-01-12 15:05:14 +00:00
echo '<td><input name="partition_names[]" value="' . h ( $val ) . '" autocapitalize="off">' ;
2018-01-12 21:18:54 +00:00
echo ( $key == count ( $row [ " partition_names " ]) - 1 ? script ( " qsl('input').oninput = partitionNameChange; " ) : '' );
2010-05-20 20:06:34 +00:00
echo '<td><input name="partition_values[]" value="' . h ( $row [ " partition_values " ][ $key ]) . '">' ;
}
?>
2008-12-12 14:03:02 +00:00
</ table >
2010-02-24 10:39:44 +00:00
</ div ></ fieldset >
2010-05-21 13:42:17 +00:00
< ? php
}
?>
2013-04-02 01:45:26 +00:00
< input type = " hidden " name = " token " value = " <?php echo $token ; ?> " >
2007-07-02 05:51:26 +00:00
</ form >