Guest User

Untitled

a guest
Feb 25th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. function buildMultiInsert(table, columns, dataRows) {
  2. const header = "insert into " + table + ' (' +columns.join(',') +') values ';
  3. return {
  4. statement: header + buildParameters(columns, dataRows),
  5. parameters: buildValues(columns, dataRows)
  6. }
  7. }
  8.  
  9. function buildParameters(columns, dataRows) {
  10. return dataRows.map((row,rowIndex)=>{
  11. return'(' + columns.map((c, i)=>{
  12. return "$" + ((rowIndex * columns.length) + i+1)
  13. }).join(', ') + ')'
  14. }).join(', ')
  15. }
  16.  
  17. function buildValues(columns, dataRows) {
  18. const valuesArr = dataRows.map((row,rowIndex)=>{
  19. return columns.map((c)=>{
  20. return row[c]
  21. })
  22. })
  23. return [].concat.apply([], valuesArr);
  24. }
  25.  
  26.  
  27. buildMultiInsert('foo',['name', 'agr'], [{
  28. name: 'Brian',
  29. age: 31
  30. }, {
  31. name: 'Aaron',
  32. age: 29
  33. }] )
Add Comment
Please, Sign In to add comment