Skip to content

Instantly share code, notes, and snippets.

@you21979
Created October 11, 2014 08:01
Show Gist options
  • Save you21979/d6028d6c57450c861456 to your computer and use it in GitHub Desktop.
Save you21979/d6028d6c57450c861456 to your computer and use it in GitHub Desktop.
var createBulkInsertBuilder = function(tbl, keys, count){
var f = function(keys, count){
var w = [];
for(var i = 0; i<count; ++i){
w.push('(' + keys.map(function(v){ return '?'}).join(',') + ')')
}
return w;
}
var q = [
'INSERT INTO',
tbl,
'(' + keys.join(',') + ')',
'VALUES',
f(keys, count).join(',')
];
return q.join(' ');
}
var createBulkInsert = function(tbl, keys, datalist){
return {
query:createBulkInsertBuilder(tbl, keys, datalist.length),
datalist:datalist,
}
}
var l = [[100,'aaa'],[101,'bbb'],[102,'ccc']]
console.log(createBulkInsert('hoge', ['id','aaa'], l));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment