Skip to content

Commit

Permalink
fix: sql parser hangs during import
Browse files Browse the repository at this point in the history
  • Loading branch information
toriphes committed Feb 27, 2022
1 parent 251795e commit 7a6bd8b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 13 deletions.
16 changes: 6 additions & 10 deletions src/common/libs/sqlParser.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Duplex } from 'stream';
import { Transform } from 'stream';

const chars = {
NEWLINE: 0x0A,
Expand All @@ -8,7 +8,7 @@ const chars = {
BACKSLASH: 0x5C
};

export default class SqlParser extends Duplex {
export default class SqlParser extends Transform {
constructor (opts) {
opts = {
delimiter: ';',
Expand All @@ -27,7 +27,7 @@ export default class SqlParser extends Duplex {
this.isDelimiter = false;
}

_write (chunk, encoding, next) {
_transform (chunk, encoding, next) {
for (const char of chunk) {
this.checkEscape();
this._buffer = Buffer.concat([this._buffer, Buffer.from([char])]);
Expand Down Expand Up @@ -66,7 +66,7 @@ export default class SqlParser extends Duplex {
}

checkQuote (char) {
const isQuote = !this.isEscape && [chars.QUOTE, chars.DOUBLE_QUOTE].includes(char);
const isQuote = !this.isEscape && (chars.QUOTE === char || chars.DOUBLE_QUOTE === char);
if (isQuote && this.currentQuote === char)
this.currentQuote = null;

Expand All @@ -84,8 +84,8 @@ export default class SqlParser extends Duplex {
demiliterFound = this._buffer.slice(-this.delimiter.length).toString(this.encoding) === this.delimiter;

if (demiliterFound) {
this._buffer = this._buffer.slice(0, this._buffer.length - 1);
query = this.parsedStr;
const str = this.parsedStr;
query = str.slice(0, str.length - this.delimiter.length);
this._buffer = Buffer.from([]);
}

Expand All @@ -95,8 +95,4 @@ export default class SqlParser extends Duplex {
get parsedStr () {
return this._buffer.toString(this.encoding).trim();
}

_read (size) {

}
}
5 changes: 2 additions & 3 deletions src/main/libs/importers/sql/MysqlImporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default class MysqlImporter extends BaseImporter {
this._fileHandler.pipe(parser);

parser.on('error', (err) => {
console.log(err);
console.log('err', err);
reject(err);
});

Expand All @@ -43,9 +43,8 @@ export default class MysqlImporter extends BaseImporter {
});

parser.on('data', async (query) => {
console.log('query: ', query);
parser.pause();
await this._client.raw(query);
await this._client.raw(query).catch(_ => false);
parser.resume();
queryCount++;
});
Expand Down

0 comments on commit 7a6bd8b

Please sign in to comment.