Skip to content

Instantly share code, notes, and snippets.

@ondrejhlavacek
Last active September 25, 2017 08:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ondrejhlavacek/6ebe05261c4b24ca14b1ab5c65d81529 to your computer and use it in GitHub Desktop.
Save ondrejhlavacek/6ebe05261c4b24ca14b1ab5c65d81529 to your computer and use it in GitHub Desktop.
/**
* Taken and modified from
* http://stackoverflow.com/questions/4747808/split-mysql-queries-in-array-each-queries-separated-by/5610067#5610067
*/
const regex = /\s*((?:'[^'\\]*(?:\\.[^'\\]*)*'|"[^"\\]*(?:\\.[^"\\]*)*"|\/\*[^*]*\*+(?:[^*\/][^*]*\*+)*\/|#.*|--.*|[^"';#])+(?:;|$))/g;
const re = new RegExp(regex, 'g');
self.addEventListener('message', function(e) {
var data = e.data;
if (data.queries === '') {
postMessage([]);
return;
}
const matches = data.queries.match(re);
const response = matches
.filter((line) => line.trim() !== '')
.map((line) => line.trim());
postMessage(response);
}, false);
const maxSqlExecutionTime = 2000;
import Promise from 'bluebird';
export default function(queries) {
var promise = new Promise(function(resolve, reject) {
// worker-loader webpack plugin
const worker = require('worker-loader?inline!./worker.js')();
var success = false;
worker.onmessage = function(e) {
success = true;
resolve(e.data);
};
worker.postMessage({
queries: queries
});
setTimeout(function() {
if (!success) {
reject();
}
worker.terminate();
}, maxSqlExecutionTime);
});
return promise;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment