orange-orm 4.0.1 → 4.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (89) hide show
  1. package/docs/changelog.md +5 -2
  2. package/package.json +1 -1
  3. package/src/getManyDto/query/newSingleQuery.js +4 -3
  4. package/src/map.d.ts +19 -12
  5. package/src/mssql/newTransaction.js +1 -0
  6. package/src/mySql/deleteFromSql.js +3 -1
  7. package/src/mySql/insertSql.js +14 -7
  8. package/src/mySql/lastInsertedSql.js +4 -1
  9. package/src/mySql/newTransaction.js +1 -0
  10. package/src/oracle/deleteFromSql.js +3 -1
  11. package/src/oracle/formatDateOut.js +3 -1
  12. package/src/oracle/insertSql.js +8 -5
  13. package/src/oracle/mergeSql.js +15 -12
  14. package/src/oracle/newTransaction.js +1 -0
  15. package/src/oracle/selectForUpdateSql.js +3 -1
  16. package/src/pg/deleteFromSql.js +3 -1
  17. package/src/pg/formatDateOut.js +3 -1
  18. package/src/pg/insertSql.js +12 -7
  19. package/src/pg/lastInsertedSql.js +4 -1
  20. package/src/pg/newTransaction.js +1 -0
  21. package/src/pg/selectForUpdateSql.js +3 -1
  22. package/src/sap/deleteFromSql.js +3 -1
  23. package/src/sap/formatDateOut.js +3 -1
  24. package/src/sap/insertSql.js +6 -3
  25. package/src/sap/mergeSql.js +14 -11
  26. package/src/sap/newTransaction.js +1 -0
  27. package/src/sap/selectForUpdateSql.js +3 -1
  28. package/src/sqlite/deleteFromSql.js +3 -1
  29. package/src/sqlite/insertSql.js +11 -8
  30. package/src/sqlite/newTransaction.js +1 -0
  31. package/src/sqlite/selectForUpdateSql.js +3 -1
  32. package/src/table/column/date/formatOut.js +4 -1
  33. package/src/table/column/equal.js +2 -1
  34. package/src/table/column/extractAlias.js +2 -0
  35. package/src/table/column/greaterThan.js +2 -1
  36. package/src/table/column/greaterThanOrEqual.js +2 -1
  37. package/src/table/column/in.js +2 -1
  38. package/src/table/column/json/formatOut.js +2 -1
  39. package/src/table/column/lessThan.js +2 -1
  40. package/src/table/column/lessThanOrEqual.js +3 -1
  41. package/src/table/column/newColumn.js +6 -3
  42. package/src/table/column/notEqual.js +2 -1
  43. package/src/table/column/string/containsCore.js +3 -1
  44. package/src/table/column/string/endsWithCore.js +3 -1
  45. package/src/table/column/string/iEqual.js +2 -1
  46. package/src/table/column/string/startsWithCore.js +2 -1
  47. package/src/table/commands/delete/singleCommand/selectSql.js +4 -2
  48. package/src/table/commands/insert/getSqlTemplate.js +4 -5
  49. package/src/table/commands/newGetLastInsertedCommandCore.js +7 -5
  50. package/src/table/commands/newUpdateCommandCore.js +5 -3
  51. package/src/table/count.js +3 -1
  52. package/src/table/newRelatedTable.js +0 -1
  53. package/src/table/query/extractOrderBy.js +5 -1
  54. package/src/table/query/newSingleQuery.js +3 -2
  55. package/src/table/query/singleQuery/columnSql/newShallowColumnSql.js +7 -3
  56. package/src/table/query/singleQuery/joinSql/newShallowJoinSql.js +3 -1
  57. package/src/table/query/singleQuery/joinSql/newShallowJoinSqlCore.js +4 -1
  58. package/src/table/query/singleQuery/newDiscriminatorSql.js +6 -2
  59. package/src/table/quote.js +11 -0
  60. package/src/table/readStream/extractOrderBy.js +5 -1
  61. package/src/table/readStream/mySql/query/newSubQueries/joinLegToQuery.js +2 -1
  62. package/src/table/readStream/mySql/query/singleQuery/newShallowColumnSql.js +2 -1
  63. package/src/table/readStream/pg/query/singleQuery/newShallowColumnSql.js +2 -1
  64. package/src/table/relatedTable/childColumn.js +10 -5
  65. package/src/table/relatedTable/columnAggregate.js +6 -2
  66. package/src/table/relatedTable/columnAggregateGroup.js +19 -13
  67. package/src/table/relatedTable/selectSql.js +7 -4
  68. package/src/table/relatedTable/whereSql.js +3 -0
  69. package/src/tedious/deleteFromSql.js +3 -1
  70. package/src/tedious/formatDateOut.js +3 -1
  71. package/src/tedious/formatJSONOut.js +1 -1
  72. package/src/tedious/getManyDto/query/newSingleQuery.js +1 -3
  73. package/src/tedious/getManyDto/query/singleQuery/newShallowColumnSql.js +6 -2
  74. package/src/tedious/insertSql.js +3 -5
  75. package/src/tedious/mergeSql.js +11 -12
  76. package/src/tedious/newTransaction.js +1 -0
  77. package/src/tedious/selectForUpdateSql.js +3 -1
  78. package/src/getManyDto/query/newColumnSql.js +0 -8
  79. package/src/getManyDto/query/singleQuery/newJoinedColumnSql.js +0 -25
  80. package/src/getManyDto/query/singleQuery/newShallowColumnSql.js +0 -25
  81. package/src/table/getManyDtoFast/getSubRows.js +0 -172
  82. package/src/table/getManyDtoFast/newQuery.js +0 -8
  83. package/src/table/getManyDtoFast/newQueryCore.js +0 -18
  84. package/src/table/getManyDtoFast/query/newSingleQuery.js +0 -20
  85. package/src/table/getManyDtoFast/query/newSubQueries/joinLegToQuery.js +0 -18
  86. package/src/table/getManyDtoFast/query/newSubQueries/manyLegToQuery.js +0 -20
  87. package/src/table/getManyDtoFast/query/newSubQueries/oneLegToQuery.js +0 -21
  88. package/src/table/getManyDtoFast/query/newSubQueries.js +0 -47
  89. package/src/table/getManyDtoFast/query/singleQuery/newShallowColumnSql.js +0 -21
package/docs/changelog.md CHANGED
@@ -1,5 +1,8 @@
1
1
  ## Changelog
2
- __4.0.1_
2
+ __4.1.0__
3
+ Escape table and column names. [#91](https://github.com/alfateam/orange-orm/issues/91) [#92](https://github.com/alfateam/orange-orm/issues/92)
4
+ NotNullable relations [#104](https://github.com/alfateam/orange-orm/issues/104)
5
+ __4.0.1__
3
6
  Inhouse definitions of ajv to avoid trouble with nestjs.
4
7
  __4.0.0__
5
8
  Changed the behaviour of `update` to accept a `where` filter and only update passed in columns and relations. The previous behaviour of `update` has moved to `replace` method.
@@ -464,4 +467,4 @@ Improved sql compression on insert/update.
464
467
  __0.2.4__
465
468
  Bugfix: getMany with many-strategy and shallowFilter yields incorrect query #6.
466
469
  __0.2.3__
467
- Reformatted documentation. No code changes.
470
+ Reformatted documentation. No code changes.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "orange-orm",
3
- "version": "4.0.1",
3
+ "version": "4.1.0",
4
4
  "main": "./src/index.js",
5
5
  "browser": "./src/client/index.mjs",
6
6
  "bin": {
@@ -2,17 +2,18 @@ var newColumnSql = require('../../table/query/singleQuery/newColumnSql');
2
2
  var newWhereSql = require('../../table/query/singleQuery/newWhereSql');
3
3
  var newJoinSql = require('../../table/query/singleQuery/newJoinSql');
4
4
  var newParameterized = require('../../table/query/newParameterized');
5
+ var getSessionSingleton = require('../../table/getSessionSingleton');
5
6
 
6
7
  function _new(table,filter,span, alias,orderBy,limit,offset) {
7
- //todo
8
- var name = table._dbName;
8
+ var quote = getSessionSingleton('quote');
9
+ var name = quote(table._dbName);
9
10
  var columnSql = newColumnSql(table,span,alias,true);
10
11
  var joinSql = newJoinSql(span, alias);
11
12
  var whereSql = newWhereSql(table,filter,alias);
12
13
  if (limit)
13
14
  limit = limit + ' ';
14
15
 
15
- return newParameterized('select ' + limit + columnSql + ' from ' + name + ' ' + alias).append(joinSql).append(whereSql).append(orderBy + offset);
16
+ return newParameterized('select ' + limit + columnSql + ' from ' + name + ' ' + quote(alias)).append(joinSql).append(whereSql).append(orderBy + offset);
16
17
 
17
18
  }
18
19
 
package/src/map.d.ts CHANGED
@@ -837,7 +837,7 @@ type ReferenceMapperHelper<TFrom, TTo, TPrimaryCount> =
837
837
  column4: C4,
838
838
  column5: C5,
839
839
  column6: C6
840
- ): MappedTableDef<TTo> & RelatedTable;
840
+ ): MappedTableDef<TTo> & RelatedTable & NegotiateNotNullColumn<C1,TFrom>;
841
841
  }
842
842
  : 5 extends TPrimaryCount
843
843
  ? {
@@ -847,7 +847,7 @@ type ReferenceMapperHelper<TFrom, TTo, TPrimaryCount> =
847
847
  column3: C3,
848
848
  column4: C4,
849
849
  column5: C5
850
- ): MappedTableDef<TTo> & RelatedTable;
850
+ ): MappedTableDef<TTo> & RelatedTable & NegotiateNotNullColumn<C1,TFrom>;
851
851
  }
852
852
  : 4 extends TPrimaryCount
853
853
  ? {
@@ -856,7 +856,7 @@ type ReferenceMapperHelper<TFrom, TTo, TPrimaryCount> =
856
856
  column2: C2,
857
857
  column3: C3,
858
858
  column4: C4
859
- ): MappedTableDef<TTo> & RelatedTable;
859
+ ): MappedTableDef<TTo> & RelatedTable & NegotiateNotNullColumn<C1,TFrom>;
860
860
  }
861
861
  : 3 extends TPrimaryCount
862
862
  ? {
@@ -864,23 +864,25 @@ type ReferenceMapperHelper<TFrom, TTo, TPrimaryCount> =
864
864
  column: C1,
865
865
  column2: C2,
866
866
  column3: C3
867
- ): MappedTableDef<TTo> & RelatedTable;
867
+ ): MappedTableDef<TTo> & RelatedTable & NegotiateNotNullColumn<C1,TFrom>;
868
868
  }
869
869
  : 2 extends TPrimaryCount
870
870
  ? {
871
871
  by<C1 extends keyof KeyCandidates1<TFrom, TTo>, C2 extends keyof KeyCandidates2<TFrom, TTo>>(
872
872
  column: C1,
873
873
  column2: C2
874
- ): MappedTableDef<TTo> & RelatedTable;
874
+ ): MappedTableDef<TTo> & RelatedTable & NegotiateNotNullColumn<C1,TFrom>;
875
875
  }
876
876
  : 1 extends TPrimaryCount
877
877
  ? {
878
878
  by<C1 extends keyof KeyCandidates1<TFrom, TTo>>(
879
879
  column: C1
880
- ): MappedTableDef<TTo> & RelatedTable;
880
+ ): MappedTableDef<TTo> & RelatedTable & NegotiateNotNullColumn<C1,TFrom>;
881
881
  }
882
882
  : {};
883
883
 
884
+ type NegotiateNotNullColumn<C, TFrom> = C extends keyof TFrom ? TFrom[C] extends NotNullExceptInsert ? NotNullExceptInsert : TFrom[C] extends NotNull ? NotNull : {}: {};
885
+
884
886
  type HasMapperHelper<
885
887
  TFrom,
886
888
  TTo,
@@ -895,7 +897,7 @@ type HasMapperHelper<
895
897
  column4: keyof KeyCandidates4<TTo, TFrom>,
896
898
  column5: keyof KeyCandidates5<TTo, TFrom>,
897
899
  column6: keyof KeyCandidates6<TTo, TFrom>
898
- ): MappedTableDef<TTo> & RelatedTable & TExtra;
900
+ ): MappedTableDef<TTo> & RelatedTable & TExtra & HasMapperHelper2<MappedTableDef<TTo> & RelatedTable & TExtra>;
899
901
  }
900
902
  : 5 extends TPrimaryCount
901
903
  ? {
@@ -905,7 +907,7 @@ type HasMapperHelper<
905
907
  column3: keyof KeyCandidates3<TTo, TFrom>,
906
908
  column4: keyof KeyCandidates4<TTo, TFrom>,
907
909
  column5: keyof KeyCandidates5<TTo, TFrom>
908
- ): MappedTableDef<TTo> & RelatedTable & TExtra;
910
+ ): MappedTableDef<TTo> & RelatedTable & TExtra & HasMapperHelper2<MappedTableDef<TTo> & RelatedTable & TExtra>;
909
911
  }
910
912
  : 4 extends TPrimaryCount
911
913
  ? {
@@ -914,7 +916,7 @@ type HasMapperHelper<
914
916
  column2: keyof KeyCandidates2<TTo, TFrom>,
915
917
  column3: keyof KeyCandidates3<TTo, TFrom>,
916
918
  column4: keyof KeyCandidates4<TTo, TFrom>
917
- ): MappedTableDef<TTo> & RelatedTable & TExtra;
919
+ ): MappedTableDef<TTo> & RelatedTable & TExtra & HasMapperHelper2<MappedTableDef<TTo> & RelatedTable & TExtra>;
918
920
  }
919
921
  : 3 extends TPrimaryCount
920
922
  ? {
@@ -922,23 +924,28 @@ type HasMapperHelper<
922
924
  column: keyof KeyCandidates1<TTo, TFrom>,
923
925
  column2: keyof KeyCandidates2<TTo, TFrom>,
924
926
  column3: keyof KeyCandidates3<TTo, TFrom>
925
- ): MappedTableDef<TTo> & RelatedTable & TExtra;
927
+ ): MappedTableDef<TTo> & RelatedTable & TExtra & HasMapperHelper2<MappedTableDef<TTo> & RelatedTable & TExtra>;
926
928
  }
927
929
  : 2 extends TPrimaryCount
928
930
  ? {
929
931
  by(
930
932
  column: keyof KeyCandidates1<TTo, TFrom>,
931
933
  column2: keyof KeyCandidates2<TTo, TFrom>
932
- ): MappedTableDef<TTo> & RelatedTable & TExtra;
934
+ ): MappedTableDef<TTo> & RelatedTable & TExtra & HasMapperHelper2<MappedTableDef<TTo> & RelatedTable & TExtra>;
933
935
  }
934
936
  : 1 extends TPrimaryCount
935
937
  ? {
936
938
  by(
937
939
  column: keyof KeyCandidates1<TTo, TFrom>
938
- ): MappedTableDef<TTo> & RelatedTable & TExtra;
940
+ ): MappedTableDef<TTo> & RelatedTable & TExtra & HasMapperHelper2<MappedTableDef<TTo> & RelatedTable & TExtra>;
939
941
  }
940
942
  : {};
941
943
 
944
+ type HasMapperHelper2<T> = {
945
+ notNull(): T & NotNull;
946
+ notNullExceptInsert(): T & NotNull & NotNullExceptInsert;
947
+ }
948
+
942
949
  type ColumnMapperInit<T> = {
943
950
  column(columnName: string): ColumnType<{}>;
944
951
  primaryColumn(columnName: string): ColumnType<IsPrimary>;
@@ -54,6 +54,7 @@ function newResolveTransaction(domain, pool) {
54
54
  caller.visitSqlite();
55
55
  };
56
56
  rdb.aggregateCount = 0;
57
+ rdb.quote = (name) => `[${name}]`;
57
58
  domain.rdb = rdb;
58
59
  onSuccess();
59
60
  } catch (e) {
@@ -1,8 +1,10 @@
1
1
  var format = 'delete %s from %s as %s%s';
2
2
  var util = require('util');
3
+ const quote = require('../table/quote');
3
4
 
4
5
  function deleteFromSql(table, alias, whereSql) {
5
- var name = table._dbName;
6
+ var name = quote(table._dbName);
7
+ alias = quote(alias);
6
8
  return util.format(format, alias, name, alias, whereSql);
7
9
  }
8
10
  module.exports = deleteFromSql;
@@ -1,9 +1,12 @@
1
+ const getSessionSingleton = require('../table/getSessionSingleton');
2
+
1
3
  function insertSql(table, row, options) {
4
+ const quote = getSessionSingleton('quote');
2
5
  let columnNames = [];
3
6
  let regularColumnNames = [];
4
7
  let conflictColumnUpdateSql = '';
5
8
  let values = [];
6
- let sql = 'INSERT INTO ' + table._dbName + ' ';
9
+ let sql = 'INSERT INTO ' + quote(table._dbName) + ' ';
7
10
  addDiscriminators();
8
11
  addColumns();
9
12
  if (columnNames.length === 0) {
@@ -25,7 +28,7 @@ function insertSql(table, row, options) {
25
28
  let discriminators = table._columnDiscriminators;
26
29
  for (let i = 0; i < discriminators.length; i++) {
27
30
  let parts = discriminators[i].split('=');
28
- columnNames.push(parts[0]);
31
+ columnNames.push(quote(parts[0]));
29
32
  values.push(parts[1]);
30
33
  }
31
34
  }
@@ -35,25 +38,29 @@ function insertSql(table, row, options) {
35
38
  let columns = table._columns;
36
39
  for (let i = 0; i < columns.length; i++) {
37
40
  let column = columns[i];
38
- regularColumnNames.push(column._dbName);
41
+ const columnName = quote(column._dbName);
42
+ regularColumnNames.push(columnName);
39
43
  if (row['__' + column.alias] !== undefined) {
40
- columnNames.push(column._dbName);
44
+ columnNames.push(columnName);
41
45
  values.push('%s');
42
46
  addConflictUpdate(column);
43
47
  }
44
48
  }
45
49
  if (conflictColumnUpdates.length === 0) {
46
50
  const column = table._primaryColumns[0];
47
- conflictColumnUpdates.push(`${column._dbName}=VALUES(${column._dbName})`);
51
+ const columnName = quote(column._dbName);
52
+ conflictColumnUpdates.push(`${columnName}=VALUES(${columnName})`);
48
53
  }
49
54
  conflictColumnUpdateSql = conflictColumnUpdates.join(',');
50
55
 
51
56
  function addConflictUpdate(column) {
52
57
  let concurrency = options[column.alias]?.concurrency || options.concurrency;
58
+ const columnName = quote(column._dbName);
59
+ const tableName = quote(table._dbName);
53
60
  if (concurrency === 'overwrite') {
54
- conflictColumnUpdates.push(`${column._dbName}=VALUES(${column._dbName})`);
61
+ conflictColumnUpdates.push(`${columnName}=VALUES(${columnName})`);
55
62
  } else if (concurrency === 'optimistic') {
56
- conflictColumnUpdates.push(`${column._dbName} = CASE WHEN ${table._dbName}.${column._dbName} <> VALUES(${column._dbName}) THEN CAST('12345678-1234-1234-1234-123456789012Conflict when updating ${column._dbName}12345678-1234-1234-1234-123456789012' AS SIGNED) ELSE ${table._dbName}.${column._dbName} END`);
63
+ conflictColumnUpdates.push(`${columnName} = CASE WHEN ${tableName}.${columnName} <> VALUES(${columnName}) THEN CAST('12345678-1234-1234-1234-123456789012Conflict when updating ${columnName}12345678-1234-1234-1234-123456789012' AS SIGNED) ELSE ${tableName}.${columnName} END`);
57
64
  }
58
65
  }
59
66
  }
@@ -1,8 +1,11 @@
1
+ const getSessionSingleton = require('../table/getSessionSingleton');
2
+
1
3
  function lastInsertedSql(table, keyValues) {
4
+ const quote = getSessionSingleton('quote');
2
5
  return keyValues.map((value,i) => {
3
6
  let column = table._primaryColumns[i];
4
7
  if (value === undefined && column.tsType === 'NumberColumn')
5
- return `${column._dbName}=LAST_INSERT_ID()`;
8
+ return `${quote(column._dbName)}=LAST_INSERT_ID()`;
6
9
  else
7
10
  return column.eq(value);
8
11
  });
@@ -42,6 +42,7 @@ function newResolveTransaction(domain, pool) {
42
42
  caller.visitMySql();
43
43
  };
44
44
  rdb.aggregateCount = 0;
45
+ rdb.quote = (name) => `\`${name}\``;
45
46
  domain.rdb = rdb;
46
47
  onSuccess();
47
48
  } catch (e) {
@@ -1,8 +1,10 @@
1
1
  var format = 'delete from %s where %s.rowId in (SELECT %s.rowId FROM %s %s%s)';
2
2
  var util = require('util');
3
+ const quote = require('../table/quote');
3
4
 
4
5
  function deleteFromSql(table, alias, whereSql) {
5
- var name = table._dbName;
6
+ var name = quote(table._dbName);
7
+ alias = quote(alias);
6
8
  return util.format(format, name, name, alias, name, alias, whereSql);
7
9
  }
8
10
  module.exports = deleteFromSql;
@@ -1,5 +1,7 @@
1
+ const quote = require('../table/quote');
2
+
1
3
  function formatDateColumn(column, alias) {
2
- return `TO_CHAR(${alias}.${column._dbName}, 'YYYY-MM-DD"T"HH24:MI:SS.FF3')`;
4
+ return `TO_CHAR(${alias}.${quote(column._dbName)}, 'YYYY-MM-DD"T"HH24:MI:SS.FF3')`;
3
5
  }
4
6
 
5
7
  module.exports = formatDateColumn;
@@ -1,5 +1,6 @@
1
1
  let outputInsertedSql = require('./outputInsertedSql');
2
2
  let mergeSql = require('./mergeSql');
3
+ const getSessionSingleton = require('../table/getSessionSingleton');
3
4
 
4
5
  function getSqlTemplate(_table, _row, options) {
5
6
  if (hasConcurrency(_table, options) && hasColumns())
@@ -26,14 +27,15 @@ function hasConcurrency(table,options) {
26
27
  }
27
28
 
28
29
  function insertSql(table, row) {
30
+ const quote = getSessionSingleton('quote');
29
31
  let columnNames = [];
30
32
  let regularColumnNames = [];
31
33
  let values = [];
32
- let sql = 'INSERT INTO ' + table._dbName + ' ';
34
+ let sql = 'INSERT INTO "' + table._dbName + '" ';
33
35
  addDiscriminators();
34
36
  addColumns();
35
37
  if (columnNames.length === 0)
36
- sql += `${outputInserted()} (${table._primaryColumns[0]._dbName}) VALUES(DEFAULT)`;
38
+ sql += `${outputInserted()} (${quote(table._primaryColumns[0]._dbName)}) VALUES(DEFAULT)`;
37
39
  else
38
40
  sql = sql + '('+ columnNames.join(',') + ')' + outputInserted() + 'VALUES (' + values.join(',') + ')';
39
41
  return sql;
@@ -42,7 +44,7 @@ function insertSql(table, row) {
42
44
  let discriminators = table._columnDiscriminators;
43
45
  for (let i = 0; i < discriminators.length; i++) {
44
46
  let parts = discriminators[i].split('=');
45
- columnNames.push(parts[0]);
47
+ columnNames.push(quote(parts[0]));
46
48
  values.push(parts[1]);
47
49
  }
48
50
  }
@@ -51,9 +53,10 @@ function insertSql(table, row) {
51
53
  let columns = table._columns;
52
54
  for (let i = 0; i < columns.length; i++) {
53
55
  let column = columns[i];
54
- regularColumnNames.push(column._dbName);
56
+ const columnName = quote(column._dbName);
57
+ regularColumnNames.push(columnName);
55
58
  if (row['__' + column.alias] !== undefined) {
56
- columnNames.push(column._dbName);
59
+ columnNames.push(columnName);
57
60
  if (column.tsType === 'DateColumn')
58
61
  values.push('TO_TIMESTAMP(%s, \'YYYY-MM-DD"T"HH24:MI:SS.FF6\')');
59
62
  else
@@ -1,6 +1,8 @@
1
1
  const outputInsertedSql = require('./outputInsertedSql');
2
+ const getSessionSingleton = require('../table/getSessionSingleton');
2
3
 
3
4
  function insertSql(table, row, options) {
5
+ const quote = getSessionSingleton('quote');
4
6
  let columnNames = [];
5
7
  let regularColumnNames = [];
6
8
  let conflictColumnUpdateSql = '';
@@ -11,18 +13,17 @@ function insertSql(table, row, options) {
11
13
  const matched = whenMatched();
12
14
  let sql;
13
15
  if (matched)
14
- sql = `MERGE INTO ${table._dbName} target USING (SELECT ${values.join(',')} FROM DUAL) source ON (${join()}) WHEN MATCHED THEN ${matched} WHEN NOT MATCHED THEN ${whenNotMatched()} ${outputInsertedSql(table)}`;
16
+ sql = `MERGE INTO ${quote(table._dbName)} target USING (SELECT ${values.join(',')} FROM DUAL) source ON (${join()}) WHEN MATCHED THEN ${matched} WHEN NOT MATCHED THEN ${whenNotMatched()} ${outputInsertedSql(table)}`;
15
17
  else
16
- sql = `MERGE INTO ${table._dbName} target USING (SELECT ${values.join(',')} FROM DUAL) source ON (${join()}) WHEN NOT MATCHED THEN ${whenNotMatched()} ${outputInsertedSql(table)}`;
18
+ sql = `MERGE INTO ${quote(table._dbName)} target USING (SELECT ${values.join(',')} FROM DUAL) source ON (${join()}) WHEN NOT MATCHED THEN ${whenNotMatched()} ${outputInsertedSql(table)}`;
17
19
  return sql;
18
20
 
19
21
  function join() {
20
22
  const discriminators = table._columnDiscriminators.map(x => {
21
23
  const name = x.split('=')[0];
22
-
23
- return `target.${name}=source.${name}`;
24
+ return `target."${name}"=source."${name}"`;
24
25
  });
25
- const primaries = table._primaryColumns.map(x => `target.${x._dbName}=source.${x._dbName}`);
26
+ const primaries = table._primaryColumns.map(x => `target.${quote(x._dbName)}=source.${quote(x._dbName)}`);
26
27
  return [...discriminators, ...primaries].join(' AND ');
27
28
  }
28
29
 
@@ -41,8 +42,8 @@ function insertSql(table, row, options) {
41
42
  let discriminators = table._columnDiscriminators;
42
43
  for (let i = 0; i < discriminators.length; i++) {
43
44
  let parts = discriminators[i].split('=');
44
- columnNames.push(parts[0]);
45
- values.push(`${parts[1]} ${parts[0]}`);
45
+ columnNames.push(quote(parts[0]));
46
+ values.push(`${parts[1]} ${quote(parts[0])}`);
46
47
  }
47
48
  }
48
49
 
@@ -51,10 +52,11 @@ function insertSql(table, row, options) {
51
52
  let columns = table._columns;
52
53
  for (let i = 0; i < columns.length; i++) {
53
54
  let column = columns[i];
54
- regularColumnNames.push(column._dbName);
55
+ const columnName = quote(column._dbName);
56
+ regularColumnNames.push(columnName);
55
57
  if (row['__' + column.alias] !== undefined) {
56
- columnNames.push(column._dbName);
57
- values.push(`%s ${column.alias}`);
58
+ columnNames.push(columnName);
59
+ values.push(`%s ${quote(column.alias)}`);
58
60
  if (!column.isPrimary)
59
61
  addConflictUpdate(column);
60
62
  }
@@ -66,11 +68,12 @@ function insertSql(table, row, options) {
66
68
 
67
69
  function addConflictUpdate(column) {
68
70
  let concurrency = options[column.alias]?.concurrency || options.concurrency;
71
+ const columnName = quote(column._dbName);
69
72
  if (concurrency === 'overwrite')
70
- conflictColumnUpdates.push(`target.${column._dbName}=source.${column._dbName}`);
73
+ conflictColumnUpdates.push(`target.${columnName}=source.${columnName}`);
71
74
  else if (concurrency === 'optimistic')
72
75
  // conflictColumnUpdates.push(`target.${column._dbName} = CASE WHEN target.${column._dbName} <> source.${column._dbName} THEN RAISE_APPLICATION_ERROR(-20001, 'Conflict when updating ${column._dbName}') ELSE target.${column._dbName} END`);
73
- conflictColumnUpdates.push(`target.${column._dbName} = CASE WHEN target.${column._dbName} <> source.${column._dbName} THEN 1/0 ELSE target.${column._dbName} END`);
76
+ conflictColumnUpdates.push(`target.${columnName} = CASE WHEN target.${columnName} <> source.${columnName} THEN 1/0 ELSE target.${columnName} END`);
74
77
 
75
78
  }
76
79
  }
@@ -47,6 +47,7 @@ function newResolveTransaction(domain, pool) {
47
47
  caller.visitSqlite();
48
48
  };
49
49
  rdb.aggregateCount = 0;
50
+ rdb.quote = (name) => `"${name}"`;
50
51
  domain.rdb = rdb;
51
52
  onSuccess();
52
53
  } catch (e) {
@@ -1,3 +1,5 @@
1
+ const quote = require('../table/quote');
2
+
1
3
  module.exports = function(alias) {
2
- return ' FOR UPDATE OF ' + alias;
4
+ return ' FOR UPDATE OF ' + quote(alias);
3
5
  };
@@ -1,8 +1,10 @@
1
1
  var format = 'delete from %s %s%s';
2
2
  var util = require('util');
3
+ const quote = require('../table/quote');
3
4
 
4
5
  function deleteFromSql(table, alias, whereSql) {
5
- var name = table._dbName;
6
+ var name = quote(table._dbName);
7
+ alias = quote(alias);
6
8
  return util.format(format, name, alias, whereSql);
7
9
  }
8
10
  module.exports = deleteFromSql;
@@ -1,5 +1,7 @@
1
+ const quote = require('../table/quote');
2
+
1
3
  function formatDateOut(column, alias) {
2
- return `${alias}.${column._dbName}::text`;
4
+ return `${alias}.${quote(column._dbName)}::text`;
3
5
  }
4
6
 
5
7
  module.exports = formatDateOut;
@@ -1,12 +1,14 @@
1
1
  let lastInsertedSql = require('./lastInsertedSql');
2
2
  let getSessionContext = require('../table/getSessionContext');
3
+ const getSessionSingleton = require('../table/getSessionSingleton');
3
4
 
4
5
  function insertSql(table, row, options) {
6
+ const quote = getSessionSingleton('quote');
5
7
  let columnNames = [];
6
8
  let regularColumnNames = [];
7
9
  let conflictColumnUpdateSql = '';
8
10
  let values = [];
9
- let sql = 'INSERT INTO ' + table._dbName + ' ';
11
+ let sql = 'INSERT INTO ' + quote(table._dbName) + ' ';
10
12
  addDiscriminators();
11
13
  addColumns();
12
14
  if (columnNames.length === 0)
@@ -17,7 +19,7 @@ function insertSql(table, row, options) {
17
19
 
18
20
  function onConflict() {
19
21
  if (options.concurrency === 'skipOnConflict' || options.concurrency === 'overwrite') {
20
- const primaryKeys = table._primaryColumns.map(x => x._dbName).join(',');
22
+ const primaryKeys = table._primaryColumns.map(x => quote(x._dbName)).join(',');
21
23
  return ` ON CONFLICT(${primaryKeys}) ${conflictColumnUpdateSql} `;
22
24
  }
23
25
  else return '';
@@ -27,7 +29,7 @@ function insertSql(table, row, options) {
27
29
  let discriminators = table._columnDiscriminators;
28
30
  for (let i = 0; i < discriminators.length; i++) {
29
31
  let parts = discriminators[i].split('=');
30
- columnNames.push(parts[0]);
32
+ columnNames.push(quote(parts[0]));
31
33
  values.push(parts[1]);
32
34
  }
33
35
  }
@@ -37,9 +39,10 @@ function insertSql(table, row, options) {
37
39
  let columns = table._columns;
38
40
  for (let i = 0; i < columns.length; i++) {
39
41
  let column = columns[i];
40
- regularColumnNames.push(column._dbName);
42
+ const columnName = quote(column._dbName);
43
+ regularColumnNames.push(columnName);
41
44
  if (row['__' + column.alias] !== undefined) {
42
- columnNames.push(column._dbName);
45
+ columnNames.push(columnName);
43
46
  values.push('%s');
44
47
  addConflictUpdate(column);
45
48
  }
@@ -51,10 +54,12 @@ function insertSql(table, row, options) {
51
54
 
52
55
  function addConflictUpdate(column) {
53
56
  let concurrency = options[column.alias]?.concurrency || options.concurrency;
57
+ const columnName = quote(column._dbName);
58
+ const tableName = quote(table._dbName);
54
59
  if (concurrency === 'overwrite')
55
- conflictColumnUpdates.push(`${column._dbName}=EXCLUDED.${column._dbName}`);
60
+ conflictColumnUpdates.push(`${columnName}=EXCLUDED.${columnName}`);
56
61
  else if (concurrency === 'optimistic')
57
- conflictColumnUpdates.push(`${column._dbName} = CASE WHEN ${table._dbName}.${column._dbName} <> EXCLUDED.${column._dbName} THEN CAST(random()::int || '12345678-1234-1234-1234-123456789012Conflict when updating ${column._dbName}12345678-1234-1234-1234-123456789012' AS INTEGER) ELSE ${table._dbName}.${column._dbName} END`);
62
+ conflictColumnUpdates.push(`${columnName} = CASE WHEN ${tableName}.${columnName} <> EXCLUDED.${columnName} THEN CAST(random()::int || '12345678-1234-1234-1234-123456789012Conflict when updating ${columnName}12345678-1234-1234-1234-123456789012' AS INTEGER) ELSE ${tableName}.${columnName} END`);
58
63
  }
59
64
  }
60
65
 
@@ -1,8 +1,11 @@
1
+ const getSessionSingleton = require('../table/getSessionSingleton');
2
+
1
3
  function lastInsertedSql(table) {
4
+ const quote = getSessionSingleton('quote');
2
5
  let separator = '';
3
6
  let result = 'RETURNING ';
4
7
  for (let i = 0; i < table._columns.length; i++) {
5
- result += separator + table._columns[i]._dbName;
8
+ result += separator + quote(table._columns[i]._dbName);
6
9
  separator = ',';
7
10
  }
8
11
  return result;
@@ -46,6 +46,7 @@ function newResolveTransaction(domain, pool) {
46
46
  caller.visitPg();
47
47
  };
48
48
  rdb.aggregateCount = 0;
49
+ rdb.quote = (name) => `"${name}"`;
49
50
  domain.rdb = rdb;
50
51
  onSuccess();
51
52
  } catch (e) {
@@ -1,3 +1,5 @@
1
+ const quote = require('../table/quote');
2
+
1
3
  module.exports = function(alias) {
2
- return ' FOR UPDATE OF ' + alias;
4
+ return ' FOR UPDATE OF ' + quote(alias);
3
5
  };
@@ -1,8 +1,10 @@
1
1
  var format = 'delete from %s from %s as %s %s';
2
2
  var util = require('util');
3
+ const quote = require('../table/quote');
3
4
 
4
5
  function deleteFromSql(table, alias, whereSql) {
5
- var name = table._dbName;
6
+ var name = quote(table._dbName);
7
+ alias = quote(alias) ;
6
8
  return util.format(format, name, name, alias, whereSql);
7
9
  }
8
10
  module.exports = deleteFromSql;
@@ -1,5 +1,7 @@
1
+ const quote = require('../table/quote');
2
+
1
3
  function formatDateOut(column, alias) {
2
- return `CONVERT(VARCHAR, ${alias}.${column._dbName}, 23)`;
4
+ return `CONVERT(VARCHAR, ${alias}.${quote(column._dbName)}, 23)`;
3
5
  }
4
6
 
5
7
  module.exports = formatDateOut;
@@ -1,3 +1,4 @@
1
+ const getSessionSingleton = require('../table/getSessionSingleton');
1
2
  const mergeSql = require('./mergeSql');
2
3
 
3
4
  function getSqlTemplate(_table, _row, options) {
@@ -26,10 +27,11 @@ function hasConcurrency(table,options) {
26
27
  }
27
28
 
28
29
  function insertSql(table, row) {
30
+ const quote = getSessionSingleton('quote');
29
31
  let columnNames = [];
30
32
  let regularColumnNames = [];
31
33
  let values = [];
32
- let sql = 'INSERT INTO ' + table._dbName + ' ';
34
+ let sql = 'INSERT INTO ' + quote(table._dbName) + ' ';
33
35
  addDiscriminators();
34
36
  addColumns();
35
37
  if (columnNames.length === 0)
@@ -42,7 +44,7 @@ function insertSql(table, row) {
42
44
  let discriminators = table._columnDiscriminators;
43
45
  for (let i = 0; i < discriminators.length; i++) {
44
46
  let parts = discriminators[i].split('=');
45
- columnNames.push(parts[0]);
47
+ columnNames.push(quote(parts[0]));
46
48
  values.push(parts[1]);
47
49
  }
48
50
  }
@@ -51,9 +53,10 @@ function insertSql(table, row) {
51
53
  let columns = table._columns;
52
54
  for (let i = 0; i < columns.length; i++) {
53
55
  let column = columns[i];
56
+ const columnName = quote(column._dbName);
54
57
  regularColumnNames.push(column._dbName);
55
58
  if (row['__' + column.alias] !== undefined) {
56
- columnNames.push(column._dbName);
59
+ columnNames.push(columnName);
57
60
  values.push('%s');
58
61
  }
59
62
  }