compile-statement.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Build up a SQLBuilder instance when the file is required rather than when the
  2. // function is run.
  3. var SQLBuilder = require('waterline-sql-builder')({
  4. dialect: 'mysql'
  5. });
  6. module.exports = {
  7. friendlyName: 'Compile statement',
  8. description: 'Compile a Waterline statement to a native query for MySQL.',
  9. sideEffects: 'cacheable',
  10. sync: true,
  11. inputs: {
  12. statement: {
  13. description: 'A Waterline statement.',
  14. extendedDescription: 'See documentation for more information. Note that `opts` may be used for expressing driver-specific customizations as a sibling to `from`, `where`, `select`, etc. In other words, recursively deep within a Waterline query statement. This is distinct from `meta`, which contains driver-specific customizations about the statement as a whole.',
  15. moreInfoUrl: 'https://github.com/particlebanana/waterline-query-builder/blob/master/docs/syntax.md',
  16. example: '===',
  17. // example: {},
  18. required: true
  19. },
  20. meta: {
  21. friendlyName: 'Meta (custom)',
  22. description: 'Additional stuff to pass to the driver.',
  23. extendedDescription: 'This is reserved for custom driver-specific extensions. Please refer to the documentation for the driver you are using for more specific information.',
  24. example: '==='
  25. }
  26. },
  27. exits: {
  28. success: {
  29. description: 'The provided Waterline statement was compiled successfully.',
  30. outputVariableName: 'report',
  31. outputDescription: 'The `nativeQuery` property is the compiled native query for the database. The `meta` property is reserved for custom driver-specific extensions.',
  32. outputExample: '==='
  33. // example: {
  34. // nativeQuery: 'SELECT * FROM foo',
  35. // valuesToEscape: ['foo']
  36. // meta: '==='
  37. // }
  38. },
  39. malformed: {
  40. description: 'The provided Waterline statement could not be compiled due to malformed syntax.',
  41. outputVariableName: 'report',
  42. outputDescription: 'The `error` property is a JavaScript error instance explaining that (or preferably even _why_) the Waterline syntax is not valid. The `meta` property is reserved for custom driver-specific extensions.',
  43. outputExample: '==='
  44. // example: {
  45. // error: '===',
  46. // meta: '==='
  47. // }
  48. },
  49. notSupported: {
  50. description: 'The provided Waterline statement could not be compiled because it is not supported by this MySQL driver.',
  51. extendedDescription: 'If even one clause of the Waterline statement is not supported by the MySQL driver, the compilation of the entire statement _always fails_.',
  52. outputVariableName: 'report',
  53. outputDescription: 'The `error` property is a JavaScript error instance explaining that (or preferably even _why_) the Waterline statement is not supported. The `meta` property is reserved for custom driver-specific extensions.',
  54. outputExample: '==='
  55. // example: {
  56. // error: '===',
  57. // meta: '==='
  58. // }
  59. }
  60. },
  61. fn: function compileStatement(inputs, exits) {
  62. var compiledNativeQuery;
  63. try {
  64. compiledNativeQuery = SQLBuilder.generate(inputs.statement);
  65. } catch (err) {
  66. if (!err.code || err.code === 'error') {
  67. return exits.error(err);
  68. }
  69. if (err.code === 'malformed') {
  70. return exits.malformed({
  71. error: err,
  72. meta: inputs.meta
  73. });
  74. }
  75. if (err.code === 'notSupported') {
  76. return exits.notSupported({
  77. error: err,
  78. meta: inputs.meta
  79. });
  80. }
  81. return exits.error(err);
  82. }
  83. // Attach a flag to the meta object to denote that the query was generated
  84. // with Knex and that its valuesToEscape don't need to be processed any further.
  85. var meta = inputs.meta || {};
  86. meta.isUsingQuestionMarks = true;
  87. return exits.success({
  88. nativeQuery: compiledNativeQuery.sql,
  89. valuesToEscape: compiledNativeQuery.bindings || [],
  90. meta: meta
  91. });
  92. }
  93. };