@@ -102,16 +102,17 @@ function addTablestatus(){
102102 */
103103
104104 function prepare_index_array ($ keys ){
105+
105106 $ array = array ();
106107
107108 foreach ($ keys as $ key ){
108109 if ($ key ['Key_name ' ] == 'PRIMARY ' ){
109- $ array [$ key ['Table ' ]]['PRIMARY ' ][] = $ key ['Column_name ' ];
110+ $ array [$ key ['Table ' ]]['PRIMARY ' ][] = array ( ' NAME ' => $ key ['Column_name ' ], ' Sub_part ' => $ key [ ' Sub_part ' ]) ;
110111 }else {
111112 if ($ key ['Non_unique ' ]){
112- $ array [$ key ['Table ' ]]['index ' ][] = array ('NAME ' => $ key ['Key_name ' ],'COLUMN ' => $ key ['Column_name ' ]);
113+ $ array [$ key ['Table ' ]]['index ' ][] = array ('NAME ' => $ key ['Key_name ' ],'COLUMN ' => $ key ['Column_name ' ], ' Sub_part ' => $ key [ ' Sub_part ' ] );
113114 }else {
114- $ array [$ key ['Table ' ]]['unique ' ][] = array ('NAME ' => $ key ['Key_name ' ],'COLUMN ' => $ key ['Column_name ' ]);
115+ $ array [$ key ['Table ' ]]['unique ' ][] = array ('NAME ' => $ key ['Key_name ' ],'COLUMN ' => $ key ['Column_name ' ], ' Sub_part ' => $ key [ ' Sub_part ' ] );
115116 }
116117 }
117118 }
@@ -248,7 +249,7 @@ function generateCollation($collation){
248249 return '' ;
249250 }
250251
251- function generateDefaultCommand ($ definitions ) {
252+ function generateDefaultCommand ($ definitions ){
252253/*
253254 if ($definitions['Extra'] == 'auto_increment') {
254255 return "AUTO_INCREMENT";
@@ -275,11 +276,11 @@ function generateDefaultCommand($definitions) {
275276 }
276277
277278 if ($ definitions ['Null ' ] == 'NO ' and $ definitions ['Default ' ] == '' and strpos (strtolower ($ definitions ['Type ' ]), 'char ' ) !== false ) {
278- //return "DEFAULT ''";
279+ return '' ; //return "DEFAULT ''";
279280 }
280281
281282 if ($ definitions ['Null ' ] == 'NO ' and $ definitions ['Default ' ] == '' and strpos (strtolower ($ definitions ['Type ' ]), 'int ' ) !== false ) {
282- //return "DEFAULT 0";
283+ return '' ; //return "DEFAULT 0";
283284 }
284285
285286 return '' ;
@@ -345,34 +346,8 @@ function column_defination($column){
345346 }else {
346347 $ return .= " NULL " ;
347348 }
348-
349- if (in_array ($ column ['Default ' ], array ('CURRENT_TIMESTAMP ' , 'CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ' ))) {
350- $ return .= "DEFAULT {$ column ['Default ' ]}" ;
351- }
352-
353- if ($ column ['Default ' ] != '' and strpos ($ column ['Type ' ], 'int ' ) !== false ) {
354- $ return .= "DEFAULT {$ column ['Default ' ]}" ;
355- }
356-
357- if ($ column ['Default ' ] != '' and strpos ($ column ['Type ' ], 'decimal ' ) !== false ) {
358- $ return .= "DEFAULT {$ column ['Default ' ]}" ;
359- }
360-
361- if ($ column ['Default ' ] != '' ) {
362- $ return .= "DEFAULT ' {$ column ['Default ' ]}' " ;
363- }
364-
365- if ($ column ['Null ' ] == 'YES ' and $ column ['Default ' ] == '' ) {
366- $ return .= "DEFAULT NULL " ;
367- }
368-
369- if ($ column ['Null ' ] == 'NO ' and $ column ['Default ' ] == '' and strpos (strtolower ($ column ['Type ' ]), 'char ' ) !== false ) {
370- $ return .= "DEFAULT '' " ;
371- }
372-
373- if ($ column ['Null ' ] == 'NO ' and $ column ['Default ' ] == '' and strpos (strtolower ($ column ['Type ' ]), 'int ' ) !== false ) {
374- $ return .= "" ;
375- }
349+
350+ $ return .= $ this ->generateDefaultCommand ($ column );
376351
377352 $ return .= "\n" ;
378353
@@ -428,41 +403,7 @@ function dropKeys(){
428403 $ return [] = "ALTER TABLE ` {$ constraint ['TABLE_NAME ' ]}` DROP FOREIGN KEY ` {$ constraint ['CONSTRAINT_NAME ' ]}`; " ;
429404
430405 }
431-
432- $ otables = $ this ->getTables ();
433-
434- $ tables = db::get ('table ' );
435-
436- $ tables = array_intersect ($ otables ,$ tables );
437-
438- //To include keys in defination
439- $ this ->key = true ;
440-
441- foreach ($ tables as $ table ){
442-
443- $ stmt = $ this ->pdo ->query ("SHOW FULL COLUMNS FROM $ table; " );
444-
445- $ row = $ stmt ->fetchall ();
446-
447- if ($ this ->check_autoincreate ($ this ->TableDefinition ($ table ,$ row ))){
448- // key to reduce dublicate query
449- $ return [$ table .'_0 ' ] = "ALTER TABLE ` $ table` MODIFY `id` INT; " ;
450- }
451-
452- if ($ this ->check_primary_key ($ this ->TableDefinition ($ table ,$ row ))){
453- // key to reduce dublicate query
454- $ return [$ table .'_1 ' ] = "ALTER TABLE ` $ table` DROP PRIMARY KEY; " ;
455- }
456-
457- if ($ column = $ this ->check_unique_key ($ this ->TableDefinition ($ table ,$ row ))){
458- // key to reduce dublicate query
459- $ return [$ table .'_2 ' ] = "DROP INDEX ` {$ column ['NAME ' ]}` ON ` $ table`; " ;
460- }
461- }
462-
463- //To exclude keys in defination
464- $ this ->key = false ;
465-
406+
466407 return array_values ($ return );
467408 }
468409
@@ -487,28 +428,46 @@ function addKeys(){
487428 foreach ($ indexs as $ table => $ index ){
488429 $ temp = "ALTER TABLE ` $ table` " ;
489430 $ _temp = array ();
431+ //ADDING PRIMARY KEY
490432 if (isset ($ index ['PRIMARY ' ])){
491- $ columns = implode (", " ,$ this ->tilde ($ index ['PRIMARY ' ]));
433+ $ _primary = array ();
434+ foreach ($ index ['PRIMARY ' ] as $ primary ){
435+ $ sub_part = empty ($ primary ['Sub_part ' ])?'' :"( {$ primary ['Sub_part ' ]}) " ;
436+ $ _primary [] = "` {$ primary ['NAME ' ]}` $ sub_part " ;
437+ }
438+ $ columns = implode (", " ,$ _primary );
492439 $ _temp [] = "ADD PRIMARY KEY ( {$ columns }) " ;
493440 }
494-
441+ //ADDING INDEX KEY
495442 if (isset ($ index ['index ' ])){
496443 $ index_temp = array ();
444+ $ sub_part = array ();
497445 foreach ($ index ['index ' ] as $ key ){
498446 $ index_temp [$ key ['NAME ' ]][] = $ key ['COLUMN ' ];
447+ $ sub_part [] = empty ($ key ['Sub_part ' ])?'' :"( {$ key ['Sub_part ' ]}) " ;
499448 }
500449 foreach ($ index_temp as $ name => $ _keys ){
501- $ _temp [] = "ADD KEY ` {$ name }` ( " .implode (", " ,$ this ->tilde ($ _keys )).") " ;
450+ $ _indexs = array ();
451+ foreach ($ this ->tilde ($ _keys ) as $ no => $ val ){
452+ $ _indexs [] = "$ val {$ sub_part [$ no ]}" ;
453+ }
454+ $ _temp [] = "ADD KEY ` {$ name }` ( " .implode (", " ,$ _indexs ).") " ;
502455 }
503456 }
504-
457+ //ADDING INDEX UNIQUE KEY
505458 if (isset ($ index ['unique ' ])){
506459 $ index_temp = array ();
460+ $ sub_part = array ();
507461 foreach ($ index ['unique ' ] as $ key ){
508462 $ index_temp [$ key ['NAME ' ]][] = $ key ['COLUMN ' ];
463+ $ sub_part [] = empty ($ key ['Sub_part ' ])?'' :"( {$ key ['Sub_part ' ]}) " ;
509464 }
510465 foreach ($ index_temp as $ name => $ _keys ){
511- $ _temp [] = "ADD UNIQUE KEY ` {$ name }` ( " .implode (", " ,$ this ->tilde ($ _keys )).") " ;
466+ $ _indexs = array ();
467+ foreach ($ this ->tilde ($ _keys ) as $ no => $ val ){
468+ $ _indexs [] = "$ val {$ sub_part [$ no ]}" ;
469+ }
470+ $ _temp [] = "ADD UNIQUE KEY ` {$ name }` ( " .implode (", " ,$ _indexs ).") " ;
512471 }
513472 }
514473
@@ -629,11 +588,37 @@ function dropIndexs(){
629588 $ indexs = $ this ->getIndexs ();
630589 $ sql = array ();
631590
591+ //droping key index
632592 foreach ($ indexs as $ table => $ index ){
633593 if (isset ($ index ['index ' ])){
634594 foreach ($ index ['index ' ] as $ keys ){
635595 // key to reduce dublicate query
636- $ sql [$ table .'_ ' .$ keys ['NAME ' ]] = "ALTER TABLE ` {$ table }` DROP INDEX ` {$ keys ['NAME ' ]}`; " ;
596+ $ sql [$ table .'_0_ ' .$ keys ['NAME ' ]] = "ALTER TABLE ` {$ table }` DROP INDEX ` {$ keys ['NAME ' ]}`; " ;
597+ }
598+ }
599+ }
600+ //droping unique key index
601+ foreach ($ indexs as $ table => $ index ){
602+ if (isset ($ index ['unique ' ])){
603+ foreach ($ index ['unique ' ] as $ keys ){
604+ // key to reduce dublicate query
605+ $ sql [$ table .'_1_ ' .$ keys ['NAME ' ]] = "ALTER TABLE ` {$ table }` DROP INDEX ` {$ keys ['NAME ' ]}`; " ;
606+ }
607+ }
608+ }
609+ //droping primary key
610+ foreach ($ indexs as $ table => $ index ){
611+ if (isset ($ index ['PRIMARY ' ])){
612+ //modifing column to override not increament column structure to drop auto increament
613+ $ stmt = $ this ->pdo ->query ("SHOW FULL COLUMNS FROM $ table; " );
614+ $ row = $ stmt ->fetchall ();
615+ if ($ column = $ this ->check_autoincreate ($ this ->TableDefinition ($ table ,$ row ))){
616+ $ sql [$ table .'_0_ ' .$ row ['Field ' ]] = "ALTER TABLE ` {$ table }` MODIFY ` {$ column ['NAME ' ]}` INT; " ;
617+ }
618+ //droping primary key
619+ foreach ($ index ['PRIMARY ' ] as $ keys ){
620+ // key to reduce dublicate query
621+ $ sql [$ table .'_2 ' ] = "ALTER TABLE ` {$ table }` DROP PRIMARY KEY; " ;
637622 }
638623 }
639624 }
0 commit comments