Skip to content

Commit 8939fd6

Browse files
committed
key Sub_part added
key Sub_part added
1 parent d366ca9 commit 8939fd6

File tree

2 files changed

+70
-84
lines changed

2 files changed

+70
-84
lines changed

README.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ $ composer update
2929

3030
```php
3131

32-
// If you installed via composer, just use this code to requrie autoloader on the top of your projects.
33-
require 'vendor/autoload.php';
34-
3532
// Using kdbv namespace
3633
namespace kanduganesh;
3734

35+
// If you installed via composer, just use this code to require auto loader on the top of your projects.
36+
require 'vendor/autoload.php';
37+
3838
// Initialize
3939

4040
/*
@@ -64,15 +64,15 @@ $obj->make();
6464

6565
```
6666

67-
## get mysql quries in array
67+
## get mysql queries in array
6868

6969
```php
7070

71-
require 'vendor/autoload.php';
72-
7371
// Using kdbv namespace
7472
namespace kanduganesh;
7573

74+
require 'vendor/autoload.php';
75+
7676
// Initialize
7777

7878
/*
@@ -101,15 +101,15 @@ $sql = $obj->query();
101101

102102
```
103103

104-
## Upgrad mysql database
104+
## Upgrade mysql database
105105

106106
```php
107107

108-
require 'vendor/autoload.php';
109-
110108
// Using kdbv namespace
111109
namespace kanduganesh;
112110

111+
require 'vendor/autoload.php';
112+
113113
// Initialize
114114

115115
/*
@@ -143,3 +143,4 @@ $obj->upgrade();
143143
- [Ganesh Kandu](https://github.com/GaneshKandu)
144144
- [Google+](https://plus.google.com/u/0/+ganeshkandu)
145145
- [Linkedin](https://www.linkedin.com/in/ganesh-kandu-42b14373/)
146+

src/table.php

Lines changed: 60 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)