2008-08-16 12:28:31 +00:00
< ? php
2008-08-19 10:32:34 +00:00
function dump_table ( $table , $style , $is_view = false ) {
2009-05-08 05:23:03 +00:00
global $dbh ;
2008-08-18 22:40:45 +00:00
if ( $_POST [ " format " ] == " csv " ) {
2009-06-21 23:20:32 +00:00
echo " \xef \xbb \xbf " ; // UTF-8 byte order mark
2008-08-18 22:40:45 +00:00
if ( $style ) {
dump_csv ( array_keys ( fields ( $table )));
}
} elseif ( $style ) {
2009-05-08 05:23:03 +00:00
$result = $dbh -> query ( " SHOW CREATE TABLE " . idf_escape ( $table ));
2008-08-18 22:40:45 +00:00
if ( $result ) {
2009-06-01 12:53:19 +00:00
if ( $style == " DROP+CREATE " ) {
2009-01-28 11:18:00 +00:00
echo " DROP " . ( $is_view ? " VIEW " : " TABLE " ) . " IF EXISTS " . idf_escape ( $table ) . " ; \n " ;
2008-08-18 22:40:45 +00:00
}
2009-05-08 05:23:03 +00:00
$create = $dbh -> result ( $result , 1 );
2008-08-19 10:32:34 +00:00
$result -> free ();
2009-06-01 12:53:19 +00:00
echo ( $style != " CREATE+ALTER " ? $create : ( $is_view ? substr_replace ( $create , " OR REPLACE " , 6 , 0 ) : substr_replace ( $create , " IF NOT EXISTS " , 12 , 0 ))) . " ; \n \n " ;
2008-08-18 22:40:45 +00:00
}
2009-06-01 12:53:19 +00:00
if ( $style == " CREATE+ALTER " && ! $is_view ) {
2009-06-21 23:20:32 +00:00
// create procedure which iterates over original columns and adds new and removes old
2009-06-22 00:53:51 +00:00
$query = " SELECT COLUMN_NAME, COLUMN_DEFAULT, IS_NULLABLE, COLLATION_NAME, COLUMN_TYPE, EXTRA, COLUMN_COMMENT FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = " . $dbh -> quote ( $table ) . " ORDER BY ORDINAL_POSITION " ;
2008-08-19 10:50:04 +00:00
?>
DELIMITER ;;
2009-06-08 19:02:30 +00:00
CREATE PROCEDURE adminer_alter () BEGIN
2008-08-19 10:50:04 +00:00
DECLARE _column_name , _collation_name , _column_type , after varchar ( 64 ) DEFAULT '' ;
DECLARE _column_default longtext ;
DECLARE _is_nullable char ( 3 );
DECLARE _extra varchar ( 20 );
DECLARE _column_comment varchar ( 255 );
DECLARE done , set_after bool DEFAULT 0 ;
DECLARE add_columns text DEFAULT ' < ? php
2009-06-22 00:53:51 +00:00
$fields = array ();
$result = $dbh -> query ( $query );
$after = " " ;
while ( $row = $result -> fetch_assoc ()) {
$row [ " default " ] = ( isset ( $row [ " COLUMN_DEFAULT " ]) ? $dbh -> quote ( $row [ " COLUMN_DEFAULT " ]) : " NULL " );
$row [ " after " ] = $dbh -> quote ( $after ); //! rgt AFTER lft, lft AFTER id doesn't work
$row [ " alter " ] = escape_string ( idf_escape ( $row [ " COLUMN_NAME " ])
. " $row[COLUMN_TYPE] "
. ( $row [ " COLLATION_NAME " ] ? " COLLATE $row[COLLATION_NAME] " : " " )
. ( isset ( $row [ " COLUMN_DEFAULT " ]) ? " DEFAULT $row[default] " : " " )
. ( $row [ " IS_NULLABLE " ] == " YES " ? " " : " NOT NULL " )
. ( $row [ " EXTRA " ] ? " $row[EXTRA] " : " " )
. ( $row [ " COLUMN_COMMENT " ] ? " COMMENT " . $dbh -> quote ( $row [ " COLUMN_COMMENT " ]) : " " )
. ( $after ? " AFTER " . idf_escape ( $after ) : " FIRST " )
);
echo " , ADD $row[alter] " ;
$fields [] = $row ;
$after = $row [ " COLUMN_NAME " ];
}
$result -> free ();
?> ';
2008-08-19 10:50:04 +00:00
DECLARE columns CURSOR FOR < ? php echo $query ; ?> ;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1 ;
SET @ alter_table = '' ;
OPEN columns ;
REPEAT
FETCH columns INTO _column_name , _column_default , _is_nullable , _collation_name , _column_type , _extra , _column_comment ;
IF NOT done THEN
SET set_after = 1 ;
CASE _column_name < ? php
2009-06-22 00:53:51 +00:00
foreach ( $fields as $row ) {
echo "
WHEN " . $dbh->quote ( $row["COLUMN_NAME"] ) . " THEN
2008-08-19 10:50:04 +00:00
SET add_columns = REPLACE ( add_columns , ', ADD $row[alter]' , '' );
2009-06-22 00:53:51 +00:00
IF NOT ( _column_default <=> $row [ default ]) OR _is_nullable != '$row[IS_NULLABLE]' OR _collation_name != '$row[COLLATION_NAME]' OR _column_type != '$row[COLUMN_TYPE]' OR _extra != '$row[EXTRA]' OR _column_comment != " . $dbh->quote ( $row["COLUMN_COMMENT"] ) . " OR after != $row [ after ] THEN
2008-08-19 10:50:04 +00:00
SET @ alter_table = CONCAT ( @ alter_table , ', MODIFY $row[alter]' );
END IF ; " ; //! don't replace in comment
2009-06-22 00:53:51 +00:00
}
?>
2008-08-19 10:50:04 +00:00
ELSE
SET @ alter_table = CONCAT ( @ alter_table , ', DROP ' , _column_name );
SET set_after = 0 ;
END CASE ;
IF set_after THEN
SET after = _column_name ;
END IF ;
END IF ;
UNTIL done END REPEAT ;
CLOSE columns ;
IF @ alter_table != '' OR add_columns != '' THEN
SET @ alter_table = CONCAT ( 'ALTER TABLE <?php echo idf_escape($table); ?>' , SUBSTR ( CONCAT ( add_columns , @ alter_table ), 2 ));
PREPARE alter_command FROM @ alter_table ;
EXECUTE alter_command ;
DROP PREPARE alter_command ;
END IF ;
END ;;
DELIMITER ;
2009-06-08 19:02:30 +00:00
CALL adminer_alter ;
DROP PROCEDURE adminer_alter ;
2008-08-19 10:50:04 +00:00
< ? php
2009-06-01 12:53:19 +00:00
//! indexes
}
2008-08-18 22:40:45 +00:00
}
}
2009-06-13 17:20:29 +00:00
function dump_data ( $table , $style , $select = " " ) {
2009-05-08 05:23:03 +00:00
global $dbh , $max_packet ;
2008-08-16 12:28:31 +00:00
if ( $style ) {
2009-06-01 12:53:19 +00:00
if ( $_POST [ " format " ] != " csv " && $style == " TRUNCATE+INSERT " ) {
2008-08-16 12:28:31 +00:00
echo " TRUNCATE " . idf_escape ( $table ) . " ; \n " ;
}
2009-06-13 17:20:29 +00:00
$result = $dbh -> query (( $select ? $select : " SELECT * FROM " . idf_escape ( $table ))); //! enum and set as numbers, binary as _binary, microtime
2008-08-16 12:28:31 +00:00
if ( $result ) {
2009-08-04 11:13:35 +00:00
$fields = fields ( $table );
2008-08-16 12:28:31 +00:00
$length = 0 ;
while ( $row = $result -> fetch_assoc ()) {
if ( $_POST [ " format " ] == " csv " ) {
dump_csv ( $row );
} else {
2009-06-15 11:12:09 +00:00
$insert = " INSERT INTO " . idf_escape ( $table ) . " ( " . implode ( " , " , array_map ( 'idf_escape' , array_keys ( $row ))) . " ) VALUES " ;
2009-06-13 17:20:29 +00:00
$row2 = array ();
2008-08-16 12:28:31 +00:00
foreach ( $row as $key => $val ) {
2009-08-04 11:13:35 +00:00
$row2 [ $key ] = ( isset ( $val ) ? ( ereg ( 'int|float|double|decimal' , $fields [ $key ][ " type " ]) ? $val : $dbh -> quote ( $val )) : " NULL " ); //! columns looking like functions
2008-08-16 12:28:31 +00:00
}
2009-08-04 11:13:35 +00:00
$s = implode ( " , \t " , $row2 );
2009-06-13 17:20:29 +00:00
if ( $style == " INSERT+UPDATE " ) {
$set = array ();
2009-08-04 11:13:35 +00:00
foreach ( $row2 as $key => $val ) {
$set [] = idf_escape ( $key ) . " = $val " ;
2009-06-13 17:20:29 +00:00
}
2009-08-04 11:13:35 +00:00
echo " $insert ( $s ) ON DUPLICATE KEY UPDATE " . implode ( " , " , $set ) . " ; \n " ;
2008-08-16 12:28:31 +00:00
} else {
2009-08-04 11:13:35 +00:00
$s = " \n ( $s ) " ;
2009-06-13 17:20:29 +00:00
if ( ! $length ) {
2009-07-03 09:21:28 +00:00
echo $insert . $s ;
2008-08-16 12:28:31 +00:00
$length = strlen ( $insert ) + strlen ( $s );
2009-06-13 17:20:29 +00:00
} else {
2009-08-19 15:50:58 +00:00
$length += 1 + strlen ( $s ); // 1 - separator length
2009-06-13 17:20:29 +00:00
if ( $length < $max_packet ) {
2009-08-19 15:50:58 +00:00
echo " , $s " ;
2009-06-13 17:20:29 +00:00
} else {
2009-07-03 09:21:28 +00:00
echo " ; \n $insert $s " ;
2009-06-13 17:20:29 +00:00
$length = strlen ( $insert ) + strlen ( $s );
}
2008-08-16 12:28:31 +00:00
}
}
}
}
2009-06-01 12:53:19 +00:00
if ( $_POST [ " format " ] != " csv " && $style != " INSERT+UPDATE " && $result -> num_rows ) {
2008-08-16 12:28:31 +00:00
echo " ; \n " ;
}
$result -> free ();
}
}
}
2008-08-16 21:15:46 +00:00
2008-08-18 15:18:16 +00:00
function dump_headers ( $identifier , $multi_table = false ) {
2009-06-11 05:18:40 +00:00
$filename = ( strlen ( $identifier ) ? friendly_url ( $identifier ) : " dump " );
2009-06-21 23:20:32 +00:00
$ext = ( $_POST [ " format " ] == " sql " ? " sql " : ( $multi_table ? " tar " : " csv " )); // multiple CSV packed to TAR
2008-08-18 15:18:16 +00:00
header ( " Content-Type: " . ( $ext == " tar " ? " application/x-tar " : ( $ext == " sql " || $_POST [ " output " ] != " file " ? " text/plain " : " text/csv " )) . " ; charset=utf-8 " );
2009-06-15 10:57:43 +00:00
if ( $_POST [ " output " ] == " file " ) {
header ( " Content-Disposition: attachment; filename= $filename . $ext " );
}
2008-08-18 15:18:16 +00:00
return $ext ;
}
2009-07-11 20:30:40 +00:00
$dump_output = " <select name='output'><option value='text'> " . lang ( 'open' ) . " <option value='file'> " . lang ( 'save' ) . " </select> " ;
$dump_format = " <select name='format'><option value='sql'> " . lang ( 'SQL' ) . " <option value='csv'> " . lang ( 'CSV' ) . " </select> " ;
2009-06-15 10:01:55 +00:00
$max_packet = 1048576 ; // default, minimum is 1024