Navigation Menu

Skip to content

Commit

Permalink
Fix column names when using complete-insert and hex-blob. Huge thanks…
Browse files Browse the repository at this point in the history
… to github.com/stainleebakhla. Closes #128
  • Loading branch information
ifsnop committed Jun 30, 2018
1 parent 0678c05 commit 8d06344
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
31 changes: 29 additions & 2 deletions src/Ifsnop/Mysqldump/Mysqldump.php
Expand Up @@ -900,7 +900,13 @@ private function listValues($tableName)
$onlyOnce = true;
$lineSize = 0;

// colStmt is used to form a query to obtain row values
$colStmt = $this->getColumnStmt($tableName);
// colNames is used to get the name of the columns when using complete-insert
if ($this->dumpSettings['complete-insert']) {
$colNames = $this->getColumnNames($tableName);
}

$stmt = "SELECT ".implode(",", $colStmt)." FROM `$tableName`";

if ($this->dumpSettings['where']) {
Expand All @@ -918,7 +924,7 @@ private function listValues($tableName)
if ($this->dumpSettings['complete-insert']) {
$lineSize += $this->compressManager->write(
"INSERT$ignore INTO `$tableName` (".
implode(", ", $colStmt).
implode(", ", $colNames).
") VALUES (".implode(",", $vals).")"
);
} else {
Expand Down Expand Up @@ -1035,7 +1041,7 @@ function endListValues($tableName)
}

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

return $colStmt;
}

/**
* Build SQL List of all columns on current table which will be used for inserting
*
* @param string $tableName Name of table to get columns
*
* @return string SQL sentence with columns
*/
function getColumnNames($tableName)
{
$colNames = array();
foreach($this->tableColumnTypes[$tableName] as $colName => $colType) {
if ($colType['is_virtual']) {
$this->dumpSettings['complete-insert'] = true;
continue;
} else {
$colNames[] = "`${colName}`";
}
}
return $colNames;
}
}

/**
Expand Down
10 changes: 10 additions & 0 deletions tests/test.php
Expand Up @@ -47,6 +47,16 @@
$dumpSettings);
$dump->start("mysqldump-php_test001.sql");

// checks if complete-insert && hex-blob works ok together
print "starting mysql-php_test001_complete.sql" . PHP_EOL;
$dumpSettings['complete-insert'] = true;
$dump = new IMysqldump\Mysqldump(
"mysql:host=localhost;dbname=test001",
"travis",
"",
$dumpSettings);
$dump->start("mysqldump-php_test001_complete.sql");

print "starting mysql-php_test002.sql" . PHP_EOL;
$dumpSettings['default-character-set'] = IMysqldump\Mysqldump::UTF8MB4;
$dumpSettings['complete-insert'] = true;
Expand Down
15 changes: 14 additions & 1 deletion tests/test.sh
Expand Up @@ -46,6 +46,15 @@ mysqldump -utravis test001 \
> mysqldump_test001.sql
ret[((index++))]=$?

mysqldump -utravis test001 \
--no-autocommit \
--extended-insert=false \
--hex-blob=true \
--routines=true \
--complete-insert=true \
> mysqldump_test001_complete.sql
ret[((index++))]=$?

mysqldump -utravis test002 \
--no-autocommit \
--extended-insert=false \
Expand Down Expand Up @@ -102,13 +111,14 @@ cat test005.src.sql | grep ^INSERT > test005.filtered.sql
cat test008.src.sql | grep FOREIGN > test008.filtered.sql
cat test010.src.sql | grep CREATE | grep EVENT > test010.filtered.sql
cat test011.src.sql | egrep "INSERT|GENERATED" > test011.filtered.sql
cat test013.src.sql | egrep "INSERT" > test013.filtered.sql
cat mysqldump_test001.sql | grep ^INSERT > mysqldump_test001.filtered.sql
cat mysqldump_test001_complete.sql | grep ^INSERT > mysqldump_test001_complete.filtered.sql
cat mysqldump_test002.sql | grep ^INSERT > mysqldump_test002.filtered.sql
cat mysqldump_test005.sql | grep ^INSERT > mysqldump_test005.filtered.sql
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
cat mysqldump_test013.sql | grep "INSERT" > mysqldump_test013.filtered.sql
cat mysqldump-php_test001.sql | grep ^INSERT > mysqldump-php_test001.filtered.sql
cat mysqldump-php_test001_complete.sql | grep ^INSERT > mysqldump-php_test001_complete.filtered.sql
cat mysqldump-php_test002.sql | grep ^INSERT > mysqldump-php_test002.filtered.sql
cat mysqldump-php_test005.sql | grep ^INSERT > mysqldump-php_test005.filtered.sql
cat mysqldump-php_test008.sql | grep FOREIGN > mysqldump-php_test008.filtered.sql
Expand All @@ -120,6 +130,9 @@ cat mysqldump-php_test013.sql | grep INSERT > mysqldump-php_test013.filtered.sql

diff test001.filtered.sql mysqldump_test001.filtered.sql
ret[((index++))]=$?
diff mysqldump_test001_complete.filtered.sql mysqldump-php_test001_complete.filtered.sql
ret[((index++))]=$?

diff test002.filtered.sql mysqldump_test002.filtered.sql
ret[((index++))]=$?

Expand Down

0 comments on commit 8d06344

Please sign in to comment.