Skip to content

Commit 8d06344

Browse files
committed
Fix column names when using complete-insert and hex-blob. Huge thanks to github.com/stainleebakhla. Closes #128
1 parent 0678c05 commit 8d06344

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed

src/Ifsnop/Mysqldump/Mysqldump.php

+29-2
Original file line numberDiff line numberDiff line change
@@ -900,7 +900,13 @@ private function listValues($tableName)
900900
$onlyOnce = true;
901901
$lineSize = 0;
902902

903+
// colStmt is used to form a query to obtain row values
903904
$colStmt = $this->getColumnStmt($tableName);
905+
// colNames is used to get the name of the columns when using complete-insert
906+
if ($this->dumpSettings['complete-insert']) {
907+
$colNames = $this->getColumnNames($tableName);
908+
}
909+
904910
$stmt = "SELECT ".implode(",", $colStmt)." FROM `$tableName`";
905911

906912
if ($this->dumpSettings['where']) {
@@ -918,7 +924,7 @@ private function listValues($tableName)
918924
if ($this->dumpSettings['complete-insert']) {
919925
$lineSize += $this->compressManager->write(
920926
"INSERT$ignore INTO `$tableName` (".
921-
implode(", ", $colStmt).
927+
implode(", ", $colNames).
922928
") VALUES (".implode(",", $vals).")"
923929
);
924930
} else {
@@ -1035,7 +1041,7 @@ function endListValues($tableName)
10351041
}
10361042

10371043
/**
1038-
* Build SQL List of all columns on current table
1044+
* Build SQL List of all columns on current table which will be used for selecting
10391045
*
10401046
* @param string $tableName Name of table to get columns
10411047
*
@@ -1059,6 +1065,27 @@ function getColumnStmt($tableName)
10591065

10601066
return $colStmt;
10611067
}
1068+
1069+
/**
1070+
* Build SQL List of all columns on current table which will be used for inserting
1071+
*
1072+
* @param string $tableName Name of table to get columns
1073+
*
1074+
* @return string SQL sentence with columns
1075+
*/
1076+
function getColumnNames($tableName)
1077+
{
1078+
$colNames = array();
1079+
foreach($this->tableColumnTypes[$tableName] as $colName => $colType) {
1080+
if ($colType['is_virtual']) {
1081+
$this->dumpSettings['complete-insert'] = true;
1082+
continue;
1083+
} else {
1084+
$colNames[] = "`${colName}`";
1085+
}
1086+
}
1087+
return $colNames;
1088+
}
10621089
}
10631090

10641091
/**

tests/test.php

+10
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@
4747
$dumpSettings);
4848
$dump->start("mysqldump-php_test001.sql");
4949

50+
// checks if complete-insert && hex-blob works ok together
51+
print "starting mysql-php_test001_complete.sql" . PHP_EOL;
52+
$dumpSettings['complete-insert'] = true;
53+
$dump = new IMysqldump\Mysqldump(
54+
"mysql:host=localhost;dbname=test001",
55+
"travis",
56+
"",
57+
$dumpSettings);
58+
$dump->start("mysqldump-php_test001_complete.sql");
59+
5060
print "starting mysql-php_test002.sql" . PHP_EOL;
5161
$dumpSettings['default-character-set'] = IMysqldump\Mysqldump::UTF8MB4;
5262
$dumpSettings['complete-insert'] = true;

tests/test.sh

+14-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@ mysqldump -utravis test001 \
4646
> mysqldump_test001.sql
4747
ret[((index++))]=$?
4848

49+
mysqldump -utravis test001 \
50+
--no-autocommit \
51+
--extended-insert=false \
52+
--hex-blob=true \
53+
--routines=true \
54+
--complete-insert=true \
55+
> mysqldump_test001_complete.sql
56+
ret[((index++))]=$?
57+
4958
mysqldump -utravis test002 \
5059
--no-autocommit \
5160
--extended-insert=false \
@@ -102,13 +111,14 @@ cat test005.src.sql | grep ^INSERT > test005.filtered.sql
102111
cat test008.src.sql | grep FOREIGN > test008.filtered.sql
103112
cat test010.src.sql | grep CREATE | grep EVENT > test010.filtered.sql
104113
cat test011.src.sql | egrep "INSERT|GENERATED" > test011.filtered.sql
105-
cat test013.src.sql | egrep "INSERT" > test013.filtered.sql
106114
cat mysqldump_test001.sql | grep ^INSERT > mysqldump_test001.filtered.sql
115+
cat mysqldump_test001_complete.sql | grep ^INSERT > mysqldump_test001_complete.filtered.sql
107116
cat mysqldump_test002.sql | grep ^INSERT > mysqldump_test002.filtered.sql
108117
cat mysqldump_test005.sql | grep ^INSERT > mysqldump_test005.filtered.sql
109118
cat mysqldump_test012.sql | grep -E -e '50001 (CREATE|VIEW)' -e '50013 DEFINER' -e 'TRIGGER' | grep -v -e 'TABLE' -e 'CREATE VIEW' > mysqldump_test012.filtered.sql
110119
cat mysqldump_test013.sql | grep "INSERT" > mysqldump_test013.filtered.sql
111120
cat mysqldump-php_test001.sql | grep ^INSERT > mysqldump-php_test001.filtered.sql
121+
cat mysqldump-php_test001_complete.sql | grep ^INSERT > mysqldump-php_test001_complete.filtered.sql
112122
cat mysqldump-php_test002.sql | grep ^INSERT > mysqldump-php_test002.filtered.sql
113123
cat mysqldump-php_test005.sql | grep ^INSERT > mysqldump-php_test005.filtered.sql
114124
cat mysqldump-php_test008.sql | grep FOREIGN > mysqldump-php_test008.filtered.sql
@@ -120,6 +130,9 @@ cat mysqldump-php_test013.sql | grep INSERT > mysqldump-php_test013.filtered.sql
120130

121131
diff test001.filtered.sql mysqldump_test001.filtered.sql
122132
ret[((index++))]=$?
133+
diff mysqldump_test001_complete.filtered.sql mysqldump-php_test001_complete.filtered.sql
134+
ret[((index++))]=$?
135+
123136
diff test002.filtered.sql mysqldump_test002.filtered.sql
124137
ret[((index++))]=$?
125138

0 commit comments

Comments
 (0)