Last active
October 31, 2016 04:34
-
-
Save trungdq88/5530aa0421320aa31db7f40d94457fe3 to your computer and use it in GitHub Desktop.
Fix mysqldump single quote espace (http://bugs.mysql.com/bug.php?id=65941)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Fix this bug: http://bugs.mysql.com/bug.php?id=65941 | |
// Usage: | |
// node fix_mysqldump_single_quote_espace.js data.sql > data_fixed.sql | |
const fs = require('fs') | |
const file = process.argv.pop(); | |
const inputStream = fs.createReadStream(file, 'utf-8'); | |
const outputStream = fs.createWriteStream('output', 'utf-8'); | |
let backslashStack = []; | |
const stats = fs.statSync(file) | |
const fileSizeInBytes = stats["size"] | |
const write = data => outputStream.write(data); | |
inputStream.on('open', () => { | |
console.log('Start writing data...'); | |
outputStream.open(); | |
}); | |
inputStream.on('close', () => { | |
console.log('Done.'); | |
outputStream.close(); | |
}); | |
let char; | |
inputStream.on('data', data => { | |
console.log(inputStream.bytesRead/fileSizeInBytes); | |
for (let i = 0; i < data.length; i++) { | |
switch (char = data[i]) { | |
case '\\': | |
backslashStack.push(char); | |
if (backslashStack.length % 2 === 0) { | |
write(backslashStack.join('')); | |
backslashStack.length = 0; | |
} | |
break; | |
case "'": | |
if (backslashStack.length > 0) { | |
write("''"); | |
backslashStack = []; | |
} else { | |
write(char); | |
} | |
break; | |
default: | |
write(char); | |
} | |
}; | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment