table.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. 'use strict';
  2. const { cppdb } = require('../util');
  3. module.exports = function defineTable(name, factory) {
  4. // Validate arguments
  5. if (typeof name !== 'string') throw new TypeError('Expected first argument to be a string');
  6. if (!name) throw new TypeError('Virtual table module name cannot be an empty string');
  7. // Determine whether the module is eponymous-only or not
  8. let eponymous = false;
  9. if (typeof factory === 'object' && factory !== null) {
  10. eponymous = true;
  11. factory = defer(parseTableDefinition(factory, 'used', name));
  12. } else {
  13. if (typeof factory !== 'function') throw new TypeError('Expected second argument to be a function or a table definition object');
  14. factory = wrapFactory(factory);
  15. }
  16. this[cppdb].table(factory, name, eponymous);
  17. return this;
  18. };
  19. function wrapFactory(factory) {
  20. return function virtualTableFactory(moduleName, databaseName, tableName, ...args) {
  21. const thisObject = {
  22. module: moduleName,
  23. database: databaseName,
  24. table: tableName,
  25. };
  26. // Generate a new table definition by invoking the factory
  27. const def = apply.call(factory, thisObject, args);
  28. if (typeof def !== 'object' || def === null) {
  29. throw new TypeError(`Virtual table module "${moduleName}" did not return a table definition object`);
  30. }
  31. return parseTableDefinition(def, 'returned', moduleName);
  32. };
  33. }
  34. function parseTableDefinition(def, verb, moduleName) {
  35. // Validate required properties
  36. if (!hasOwnProperty.call(def, 'rows')) {
  37. throw new TypeError(`Virtual table module "${moduleName}" ${verb} a table definition without a "rows" property`);
  38. }
  39. if (!hasOwnProperty.call(def, 'columns')) {
  40. throw new TypeError(`Virtual table module "${moduleName}" ${verb} a table definition without a "columns" property`);
  41. }
  42. // Validate "rows" property
  43. const rows = def.rows;
  44. if (typeof rows !== 'function' || Object.getPrototypeOf(rows) !== GeneratorFunctionPrototype) {
  45. throw new TypeError(`Virtual table module "${moduleName}" ${verb} a table definition with an invalid "rows" property (should be a generator function)`);
  46. }
  47. // Validate "columns" property
  48. let columns = def.columns;
  49. if (!Array.isArray(columns) || !(columns = [...columns]).every(x => typeof x === 'string')) {
  50. throw new TypeError(`Virtual table module "${moduleName}" ${verb} a table definition with an invalid "columns" property (should be an array of strings)`);
  51. }
  52. if (columns.length !== new Set(columns).size) {
  53. throw new TypeError(`Virtual table module "${moduleName}" ${verb} a table definition with duplicate column names`);
  54. }
  55. if (!columns.length) {
  56. throw new RangeError(`Virtual table module "${moduleName}" ${verb} a table definition with zero columns`);
  57. }
  58. // Validate "parameters" property
  59. let parameters;
  60. if (hasOwnProperty.call(def, 'parameters')) {
  61. parameters = def.parameters;
  62. if (!Array.isArray(parameters) || !(parameters = [...parameters]).every(x => typeof x === 'string')) {
  63. throw new TypeError(`Virtual table module "${moduleName}" ${verb} a table definition with an invalid "parameters" property (should be an array of strings)`);
  64. }
  65. } else {
  66. parameters = inferParameters(rows);
  67. }
  68. if (parameters.length !== new Set(parameters).size) {
  69. throw new TypeError(`Virtual table module "${moduleName}" ${verb} a table definition with duplicate parameter names`);
  70. }
  71. if (parameters.length > 32) {
  72. throw new RangeError(`Virtual table module "${moduleName}" ${verb} a table definition with more than the maximum number of 32 parameters`);
  73. }
  74. for (const parameter of parameters) {
  75. if (columns.includes(parameter)) {
  76. throw new TypeError(`Virtual table module "${moduleName}" ${verb} a table definition with column "${parameter}" which was ambiguously defined as both a column and parameter`);
  77. }
  78. }
  79. // Validate "safeIntegers" option
  80. let safeIntegers = 2;
  81. if (hasOwnProperty.call(def, 'safeIntegers')) {
  82. const bool = def.safeIntegers;
  83. if (typeof bool !== 'boolean') {
  84. throw new TypeError(`Virtual table module "${moduleName}" ${verb} a table definition with an invalid "safeIntegers" property (should be a boolean)`);
  85. }
  86. safeIntegers = +bool;
  87. }
  88. // Validate "directOnly" option
  89. let directOnly = false;
  90. if (hasOwnProperty.call(def, 'directOnly')) {
  91. directOnly = def.directOnly;
  92. if (typeof directOnly !== 'boolean') {
  93. throw new TypeError(`Virtual table module "${moduleName}" ${verb} a table definition with an invalid "directOnly" property (should be a boolean)`);
  94. }
  95. }
  96. // Generate SQL for the virtual table definition
  97. const columnDefinitions = [
  98. ...parameters.map(identifier).map(str => `${str} HIDDEN`),
  99. ...columns.map(identifier),
  100. ];
  101. return [
  102. `CREATE TABLE x(${columnDefinitions.join(', ')});`,
  103. wrapGenerator(rows, new Map(columns.map((x, i) => [x, parameters.length + i])), moduleName),
  104. parameters,
  105. safeIntegers,
  106. directOnly,
  107. ];
  108. }
  109. function wrapGenerator(generator, columnMap, moduleName) {
  110. return function* virtualTable(...args) {
  111. /*
  112. We must defensively clone any buffers in the arguments, because
  113. otherwise the generator could mutate one of them, which would cause
  114. us to return incorrect values for hidden columns, potentially
  115. corrupting the database.
  116. */
  117. const output = args.map(x => Buffer.isBuffer(x) ? Buffer.from(x) : x);
  118. for (let i = 0; i < columnMap.size; ++i) {
  119. output.push(null); // Fill with nulls to prevent gaps in array (v8 optimization)
  120. }
  121. for (const row of generator(...args)) {
  122. if (Array.isArray(row)) {
  123. extractRowArray(row, output, columnMap.size, moduleName);
  124. yield output;
  125. } else if (typeof row === 'object' && row !== null) {
  126. extractRowObject(row, output, columnMap, moduleName);
  127. yield output;
  128. } else {
  129. throw new TypeError(`Virtual table module "${moduleName}" yielded something that isn't a valid row object`);
  130. }
  131. }
  132. };
  133. }
  134. function extractRowArray(row, output, columnCount, moduleName) {
  135. if (row.length !== columnCount) {
  136. throw new TypeError(`Virtual table module "${moduleName}" yielded a row with an incorrect number of columns`);
  137. }
  138. const offset = output.length - columnCount;
  139. for (let i = 0; i < columnCount; ++i) {
  140. output[i + offset] = row[i];
  141. }
  142. }
  143. function extractRowObject(row, output, columnMap, moduleName) {
  144. let count = 0;
  145. for (const key of Object.keys(row)) {
  146. const index = columnMap.get(key);
  147. if (index === undefined) {
  148. throw new TypeError(`Virtual table module "${moduleName}" yielded a row with an undeclared column "${key}"`);
  149. }
  150. output[index] = row[key];
  151. count += 1;
  152. }
  153. if (count !== columnMap.size) {
  154. throw new TypeError(`Virtual table module "${moduleName}" yielded a row with missing columns`);
  155. }
  156. }
  157. function inferParameters({ length }) {
  158. if (!Number.isInteger(length) || length < 0) {
  159. throw new TypeError('Expected function.length to be a positive integer');
  160. }
  161. const params = [];
  162. for (let i = 0; i < length; ++i) {
  163. params.push(`$${i + 1}`);
  164. }
  165. return params;
  166. }
  167. const { hasOwnProperty } = Object.prototype;
  168. const { apply } = Function.prototype;
  169. const GeneratorFunctionPrototype = Object.getPrototypeOf(function*(){});
  170. const identifier = str => `"${str.replace(/"/g, '""')}"`;
  171. const defer = x => () => x;