insert.test.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. var Test = require('../../support/test-runner');
  2. describe('Query Generation ::', function() {
  3. describe('INSERT statements', function() {
  4. it('should generate an insert query', function(done) {
  5. Test({
  6. query: {
  7. insert: {
  8. title: 'Slaughterhouse Five'
  9. },
  10. into: 'books'
  11. },
  12. outcomes: [
  13. {
  14. dialect: 'postgresql',
  15. sql: 'insert into "books" ("title") values ($1)',
  16. bindings: ['Slaughterhouse Five']
  17. },
  18. {
  19. dialect: 'mysql',
  20. sql: 'insert into `books` (`title`) values (?)',
  21. bindings: ['Slaughterhouse Five']
  22. },
  23. {
  24. dialect: 'sqlite3',
  25. sql: 'insert into "books" ("title") values (?)',
  26. bindings: ['Slaughterhouse Five']
  27. },
  28. {
  29. dialect: 'oracle',
  30. sql: 'insert into "books" ("title") values (:1)',
  31. bindings: ['Slaughterhouse Five']
  32. },
  33. {
  34. dialect: 'mariadb',
  35. sql: 'insert into `books` (`title`) values (?)',
  36. bindings: ['Slaughterhouse Five']
  37. }
  38. ]
  39. }, done);
  40. });
  41. it('should generate an insert query when using multiple values', function(done) {
  42. Test({
  43. query: {
  44. insert: {
  45. title: 'Slaughterhouse Five',
  46. author: 'Kurt Vonnegut'
  47. },
  48. into: 'books'
  49. },
  50. outcomes: [
  51. {
  52. dialect: 'postgresql',
  53. sql: 'insert into "books" ("author", "title") values ($1, $2)',
  54. bindings: ['Kurt Vonnegut', 'Slaughterhouse Five']
  55. },
  56. {
  57. dialect: 'mysql',
  58. sql: 'insert into `books` (`author`, `title`) values (?, ?)',
  59. bindings: ['Kurt Vonnegut', 'Slaughterhouse Five']
  60. },
  61. {
  62. dialect: 'sqlite3',
  63. sql: 'insert into "books" ("author", "title") values (?, ?)',
  64. bindings: ['Kurt Vonnegut', 'Slaughterhouse Five']
  65. },
  66. {
  67. dialect: 'oracle',
  68. sql: 'insert into "books" ("author", "title") values (:1, :2)',
  69. bindings: ['Kurt Vonnegut', 'Slaughterhouse Five']
  70. },
  71. {
  72. dialect: 'mariadb',
  73. sql: 'insert into `books` (`author`, `title`) values (?, ?)',
  74. bindings: ['Kurt Vonnegut', 'Slaughterhouse Five']
  75. }
  76. ]
  77. }, done);
  78. });
  79. it('should generate an insert query when using an array of values', function(done) {
  80. Test({
  81. query: {
  82. insert: [
  83. {
  84. title: 'Slaughterhouse Five',
  85. author: 'Kurt Vonnegut'
  86. },
  87. {
  88. title: 'The Great Gatsby',
  89. author: 'F. Scott Fitzgerald'
  90. }
  91. ],
  92. into: 'books'
  93. },
  94. outcomes: [
  95. {
  96. dialect: 'postgresql',
  97. sql: 'insert into "books" ("author", "title") values ($1, $2), ($3, $4)',
  98. bindings: ['Kurt Vonnegut', 'Slaughterhouse Five', 'F. Scott Fitzgerald', 'The Great Gatsby']
  99. },
  100. {
  101. dialect: 'mysql',
  102. sql: 'insert into `books` (`author`, `title`) values (?, ?), (?, ?)',
  103. bindings: ['Kurt Vonnegut', 'Slaughterhouse Five', 'F. Scott Fitzgerald', 'The Great Gatsby']
  104. },
  105. {
  106. dialect: 'sqlite3',
  107. sql: 'insert into "books" ("author", "title") select ? as "author", ? as "title" union all select ? as "author", ? as "title"',
  108. bindings: ['Kurt Vonnegut', 'Slaughterhouse Five', 'F. Scott Fitzgerald', 'The Great Gatsby']
  109. },
  110. {
  111. dialect: 'oracle',
  112. sql: 'begin execute immediate \'insert into "books" ("author", "title") values (:1, :2)\' using :1, :2; execute immediate \'insert into "books" ("author", "title") values (:1, :2)\' using :3, :4;end;',
  113. bindings: ['Kurt Vonnegut', 'Slaughterhouse Five', 'F. Scott Fitzgerald', 'The Great Gatsby']
  114. },
  115. {
  116. dialect: 'mariadb',
  117. sql: 'insert into `books` (`author`, `title`) values (?, ?), (?, ?)',
  118. bindings: ['Kurt Vonnegut', 'Slaughterhouse Five', 'F. Scott Fitzgerald', 'The Great Gatsby']
  119. }
  120. ]
  121. }, done);
  122. });
  123. });
  124. });