Skip to content

Commit a8defc7

Browse files
committed
🐛 fixs
🐛 fixs
1 parent 8939fd6 commit a8defc7

File tree

5 files changed

+114
-32
lines changed

5 files changed

+114
-32
lines changed

.github/ISSUE_TEMPLATE.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
To help us resolve your issue more quickly, please use the following template:
3+
4+
## Version Info
5+
- KDBV version: ``` ? ```
6+
- PHP version: ``` ? ```
7+
- MySQL version: ``` ? ```
8+
- Web server (Ex. Apache, nginx or IIS?) : ``` ? ```
9+
- OS name and version: ``` ? ```
10+
11+
## Expected Behavior
12+
13+
> Write here
14+
15+
## Actual Behavior
16+
17+
> Write here
18+
19+
## Steps to Reproduce
20+
21+
> Write here
22+
23+
## Schema Dump of Latest version and old Version
24+
25+
> Upload sql files

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,5 @@ Temporary Items
5050
table.db
5151
index.php
5252
vendor/*
53-
composer.lock
53+
composer.lock
54+
log.txt

README.md

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,48 @@ PHP 5.3+ and PDO extension installed
1313

1414
## Get Started
1515

16-
### Install via composer
16+
## Install
1717

18-
Add KDBV to composer.json configuration file.
18+
This library is designed to be installed via [Composer](https://getcomposer.org/doc/).
19+
20+
Add the dependency into your projects composer.json.
21+
```
22+
{
23+
"require": {
24+
"ganeshkandu/kdbv": "*"
25+
}
26+
}
1927
```
20-
$ composer require ganeshkandu/kdbv
28+
29+
Download the composer.phar
30+
``` bash
31+
curl -sS https://getcomposer.org/installer | php
2132
```
2233

23-
And update the composer
34+
Install the library.
35+
``` bash
36+
php composer.phar install
2437
```
25-
$ composer update
38+
39+
## or
40+
41+
> To add in in your dependencies
42+
43+
``` bash
44+
php composer.phar require ganeshkandu/kdbv
2645
```
2746

28-
## create kdbv databade
47+
## Autoloading
48+
49+
This library requires an autoloader, if you aren't already using one you can include [Composers autoloader](https://getcomposer.org/doc/01-basic-usage.md#autoloading).
50+
51+
``` php
52+
require('vendor/autoload.php');
53+
```
54+
55+
## Usage
56+
57+
### create kdbv databade
2958

3059
```php
3160

@@ -64,7 +93,7 @@ $obj->make();
6493

6594
```
6695

67-
## get mysql queries in array
96+
### get mysql queries in array
6897

6998
```php
7099

@@ -101,7 +130,7 @@ $sql = $obj->query();
101130

102131
```
103132

104-
## Upgrade mysql database
133+
### Upgrade mysql database
105134

106135
```php
107136

src/kdbv.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ function init($pdodsn){
1818

1919
$dsn = "mysql:host={$pdodsn['HOST']};port={$pdodsn['PORT']};dbname={$pdodsn['DATABASE']};charset=$charset";
2020

21-
$opt = [
21+
$opt = array(
2222
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
2323
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
2424
PDO::ATTR_EMULATE_PREPARES => false,
25-
];
25+
);
2626

2727
$this->pdo = new PDO($dsn, $pdodsn['USER'], $pdodsn['PASS'], $opt);
2828
}

src/table.php

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class tabledef{
1111
public $pdodsn = null;
1212
public $sql = array();
1313
public $key = false;
14+
public $extra = false;
1415

1516
function setTables(){
1617

@@ -85,7 +86,7 @@ function addTablestatus(){
8586

8687
if(!empty($stat['Comment'])){
8788
$stat['Comment'] = $this->quoteIdent($stat['Comment']);
88-
$temp[] = "COMMENT = {$stat['Comment']}";
89+
$temp[] = "COMMENT = '{$stat['Comment']}'";
8990
}
9091

9192
$sql[] = "ALTER TABLE {$stat['Name']} ". implode(' ',$temp) .";";
@@ -107,12 +108,26 @@ function prepare_index_array($keys){
107108

108109
foreach($keys as $key){
109110
if($key['Key_name'] == 'PRIMARY'){
110-
$array[$key['Table']]['PRIMARY'][] = array('NAME' => $key['Column_name'],'Sub_part' => $key['Sub_part']);
111+
$array[$key['Table']]['PRIMARY'][] = array(
112+
'NAME' => $key['Column_name'],
113+
'Sub_part' => $key['Sub_part'],
114+
'Index_type' => $key['Index_type']
115+
);
111116
}else{
112117
if($key['Non_unique']){
113-
$array[$key['Table']]['index'][] = array('NAME' => $key['Key_name'],'COLUMN' => $key['Column_name'],'Sub_part' => $key['Sub_part']);
118+
$array[$key['Table']]['index'][] = array(
119+
'NAME' => $key['Key_name'],
120+
'COLUMN' => $key['Column_name'],
121+
'Sub_part' => $key['Sub_part'],
122+
'Index_type' => $key['Index_type']
123+
);
114124
}else{
115-
$array[$key['Table']]['unique'][] = array('NAME' => $key['Key_name'],'COLUMN' => $key['Column_name'],'Sub_part' => $key['Sub_part']);
125+
$array[$key['Table']]['unique'][] = array(
126+
'NAME' => $key['Key_name'],
127+
'COLUMN' => $key['Column_name'],
128+
'Sub_part' => $key['Sub_part'],
129+
'Index_type' => $key['Index_type']
130+
);
116131
}
117132
}
118133
}
@@ -170,11 +185,13 @@ function getIndexs(){
170185
function setTblDesc(){
171186
$td = array();
172187
$tables = db::get('table');
188+
$this->key = true;
173189
foreach($tables as $table){
174190
$stmt = $this->pdo->query("SHOW FULL COLUMNS FROM $table;");
175191
$row = $stmt->fetchall();
176192
db::set($table,$this->TableDefinition($table,$row));
177193
}
194+
$this->key = false;
178195
}
179196

180197
function TableDefinition($table,$columns){
@@ -250,11 +267,13 @@ function generateCollation($collation){
250267
}
251268

252269
function generateDefaultCommand($definitions){
253-
/*
254-
if ($definitions['Extra'] == 'auto_increment') {
255-
return "AUTO_INCREMENT";
270+
271+
if($this->extra){
272+
if($definitions['Extra'] == 'auto_increment'){
273+
return "AUTO_INCREMENT";
274+
}
256275
}
257-
*/
276+
258277
if (in_array($definitions['Default'], array('CURRENT_TIMESTAMP', 'CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'))) {
259278
return "DEFAULT {$definitions['Default']}";
260279
}
@@ -290,6 +309,7 @@ function create_table($ctable){
290309
$entries = array();
291310
$primaryKey = array();
292311
$keys = array();
312+
//$this->extra = true;
293313
foreach (db::get($ctable) as $columnName => $definitions) {
294314
$entries[] = '`' . $columnName . '` ' . $definitions['Type'] . ' '. $this->generateCollation($definitions['Collation']) .' ' . $this->generateNullCommand($definitions['Null']) . ' ' . $this->generateDefaultCommand($definitions);
295315

@@ -301,13 +321,14 @@ function create_table($ctable){
301321
$keys[] = $columnName;
302322
}
303323
}
304-
324+
//$this->extra = false;
325+
305326
if (count($primaryKey) > 0) {
306-
$entries[] = 'PRIMARY KEY (`' . implode('`,`', $primaryKey) . '`)';
327+
//$entries[] = 'PRIMARY KEY (`' . implode('`,`', $primaryKey) . '`)';
307328
}
308329

309330
foreach ($keys as $key) {
310-
$entries[] = 'KEY (`' . $key . '`)';
331+
//$entries[] = 'KEY (`' . $key . '`)';
311332
}
312333

313334
return "CREATE TABLE IF NOT EXISTS `{$ctable}` (" . implode(',', $entries) . ");";
@@ -436,7 +457,7 @@ function addKeys(){
436457
$_primary[] = "`{$primary['NAME']}`$sub_part";
437458
}
438459
$columns = implode(",",$_primary);
439-
$_temp[] = "ADD PRIMARY KEY ({$columns})";
460+
$_temp[] = "\nADD PRIMARY KEY ({$columns})";
440461
}
441462
//ADDING INDEX KEY
442463
if(isset($index['index'])){
@@ -451,7 +472,11 @@ function addKeys(){
451472
foreach($this->tilde($_keys) as $no => $val){
452473
$_indexs[] = "$val{$sub_part[$no]}";
453474
}
454-
$_temp[] = "ADD KEY `{$name}` (".implode(",",$_indexs).")";
475+
$_key = '';
476+
if($key['Index_type'] != 'BTREE'){
477+
$_key = $key['Index_type'];
478+
}
479+
$_temp[] = "\nADD $_key KEY `{$name}` (".implode(",",$_indexs).")";
455480
}
456481
}
457482
//ADDING INDEX UNIQUE KEY
@@ -467,7 +492,7 @@ function addKeys(){
467492
foreach($this->tilde($_keys) as $no => $val){
468493
$_indexs[] = "$val{$sub_part[$no]}";
469494
}
470-
$_temp[] = "ADD UNIQUE KEY `{$name}` (".implode(",",$_indexs).")";
495+
$_temp[] = "\nADD UNIQUE KEY `{$name}` (".implode(",",$_indexs).")";
471496
}
472497
}
473498

@@ -574,11 +599,13 @@ function execute($queries){
574599
$stmt = $this->pdo->prepare($query);
575600
$stmt->execute(array());
576601
}catch (PDOException $e) {
577-
//echo "$query\n";
578-
//echo $e->getCode();
579-
//echo "\n";
580-
//echo $e->getMessage();
581-
//echo "\n";
602+
if(defined('DEBUG')){
603+
echo "$query\n";
604+
echo $e->getCode();
605+
echo "\n";
606+
echo $e->getMessage();
607+
echo "\n";
608+
}
582609
}
583610
}
584611
}
@@ -613,7 +640,7 @@ function dropIndexs(){
613640
$stmt = $this->pdo->query("SHOW FULL COLUMNS FROM $table;");
614641
$row = $stmt->fetchall();
615642
if($column = $this->check_autoincreate($this->TableDefinition($table,$row))){
616-
$sql[$table.'_0_'.$row['Field']] = "ALTER TABLE `{$table}` MODIFY `{$column['NAME']}` INT;";
643+
$sql[$table.'_0_'.$column['NAME']] = "ALTER TABLE `{$table}` MODIFY `{$column['NAME']}` INT;";
617644
}
618645
//droping primary key
619646
foreach($index['PRIMARY'] as $keys){
@@ -627,7 +654,7 @@ function dropIndexs(){
627654
}
628655

629656
function quoteIdent($field) {
630-
return "`".str_replace("`","``",$field)."`";
657+
return str_replace("`","``",$field);
631658
}
632659

633660
}

0 commit comments

Comments
 (0)