Skip to content

Commit

Permalink
fix hex-blob when converting bit type
Browse files Browse the repository at this point in the history
  • Loading branch information
ifsnop committed Sep 12, 2014
1 parent 420ae16 commit ea1c826
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ To dump a database, you need the following privileges :
- If any table has one or more triggers.
- **LOCK TABLES**
- If "lock tables" option was enabled.
- **SUPER**
- If "single-transaction" option was enabled.

Use **SHOW GRANTS FOR user@host;** to know what privileges user has. See the following link for more information:

Expand Down
22 changes: 13 additions & 9 deletions src/Ifsnop/Mysqldump/Mysqldump.php
Original file line number Diff line number Diff line change
Expand Up @@ -471,15 +471,17 @@ private function getTableStructure($tableName)
}

$columnTypes = array();
$columns = $this->dbHandler->query($this->typeAdapter->show_columns($tableName),
$columns = $this->dbHandler->query(
$this->typeAdapter->show_columns($tableName),
PDO::FETCH_ASSOC
);

foreach($columns as $key => $col) {
$types = $this->parseColumnType($col);
$columnTypes[$col['Field']] = array(
'is_numeric'=> $types['is_numeric'],
'is_blob' => $types['is_blob']
'is_blob' => $types['is_blob'],
'type' => $types['type']
);
}
$this->tableColumnTypes[$tableName] = $columnTypes;
Expand Down Expand Up @@ -571,8 +573,6 @@ private function getTriggerStructure($triggerName)

/**
* Escape values with quotes when needed
* @todo use is_number instead of ctype_digit and intval
* @todo get column data type, use it to quote results
*
* @param string $tableName Name of table which contains rows
* @param array $row Associative array of column names and values to be quoted
Expand All @@ -586,14 +586,18 @@ private function escape($tableName, $row)
foreach ($row as $colName => $colValue) {
if (is_null($colValue)) {
$ret[] = "NULL";
} elseif ($columnTypes[$colName]['is_numeric']) {
$ret[] = $colValue;
} elseif ($this->dumpSettings['hex-blob'] && $columnTypes[$colName]['is_blob']) {
if (empty($colValue)) {
$ret[] = "''";
if ($columnTypes[$colName]['type'] == 'bit') {
$ret[] = '0x' . bin2hex(chr($colValue));
} else {
$ret[] = '0x' . bin2hex($colValue);
if (empty($colValue)) {
$ret[] = "''";
} else {
$ret[] = '0x' . bin2hex($colValue);
}
}
} elseif ($columnTypes[$colName]['is_numeric']) {
$ret[] = $colValue;
} else {
$ret[] = $this->dbHandler->quote($colValue);
}
Expand Down
11 changes: 8 additions & 3 deletions tests/test.sh
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
mysql -uroot < original.sql
mysqldump -uroot test001 --extended-insert=false --hex-blob=true > mysqldump.sql
php test.php

cat original.sql | grep ^INSERT > original.filtered.sql
cat mysqldump.sql | grep ^INSERT > mysqldump.filtered.sql
cat mysqldump-php.sql | grep ^INSERT > mysqldump-php.filtered.sql

diff original.filtered.sql mysqldump.filtered.sql
ret1=$?

ret=$?
diff original.filtered.sql mysqldump-php.filtered.sql
ret2=$?

rm original.filtered.sql
rm mysqldump.filtered.sql
rm mysqldump-php.filtered.sql
rm mysqldump-php.sql
rm mysqldump.sql

echo $ret

exit $ret1



0 comments on commit ea1c826

Please sign in to comment.