@sap/cds 5.4.6 → 5.5.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 (228) hide show
  1. package/CHANGELOG.md +208 -2
  2. package/apis/ql.d.ts +17 -15
  3. package/app/index.js +1 -1
  4. package/bin/build/buildTaskEngine.js +26 -42
  5. package/bin/build/buildTaskFactory.js +6 -10
  6. package/bin/build/buildTaskHandler.js +2 -4
  7. package/bin/build/buildTaskProvider.js +3 -1
  8. package/bin/build/buildTaskProviderFactory.js +9 -15
  9. package/bin/build/constants.js +15 -3
  10. package/bin/build/index.js +5 -4
  11. package/bin/build/mtaUtil.js +8 -11
  12. package/bin/build/provider/buildTaskHandlerEdmx.js +63 -6
  13. package/bin/build/provider/buildTaskHandlerInternal.js +2 -34
  14. package/bin/build/provider/buildTaskProviderInternal.js +16 -42
  15. package/bin/build/provider/fiori/index.js +13 -24
  16. package/bin/build/provider/hana/2migration.js +17 -15
  17. package/bin/build/provider/hana/2tabledata.js +52 -48
  18. package/bin/build/provider/hana/index.js +27 -25
  19. package/bin/build/provider/hana/migrationtable.js +91 -67
  20. package/bin/build/provider/java-cf/index.js +14 -24
  21. package/bin/build/provider/mtx/index.js +12 -14
  22. package/bin/build/provider/node-cf/index.js +18 -32
  23. package/bin/cds.js +5 -5
  24. package/bin/serve.js +29 -23
  25. package/bin/version.js +0 -1
  26. package/lib/compile/etc/_localized.js +4 -9
  27. package/lib/compile/for/sql.js +5 -2
  28. package/lib/compile/parse.js +25 -17
  29. package/lib/compile/to/srvinfo.js +2 -1
  30. package/lib/connect/bindings.js +2 -1
  31. package/lib/connect/index.js +48 -49
  32. package/lib/core/classes.js +1 -1
  33. package/lib/core/reflect.js +10 -2
  34. package/lib/deploy.js +26 -23
  35. package/lib/env/defaults.js +13 -6
  36. package/lib/env/index.js +73 -78
  37. package/lib/env/requires.js +38 -19
  38. package/lib/index.js +9 -10
  39. package/lib/lazy.js +2 -2
  40. package/lib/log/index.js +33 -45
  41. package/lib/log/service/index.js +2 -2
  42. package/lib/ql/CREATE.js +14 -9
  43. package/lib/ql/DELETE.js +6 -5
  44. package/lib/ql/DROP.js +12 -9
  45. package/lib/ql/INSERT.js +40 -16
  46. package/lib/ql/Query.js +67 -40
  47. package/lib/ql/SELECT.js +162 -127
  48. package/lib/ql/UPDATE.js +74 -42
  49. package/lib/ql/Whereable.js +77 -87
  50. package/lib/ql/index.js +36 -24
  51. package/lib/ql/parse.js +35 -0
  52. package/lib/req/context.js +44 -8
  53. package/lib/req/locale.js +7 -7
  54. package/lib/serve/Service-api.js +21 -14
  55. package/lib/serve/Service-dispatch.js +28 -12
  56. package/lib/serve/Transaction.js +22 -10
  57. package/lib/serve/index.js +16 -11
  58. package/lib/utils/axios.js +23 -16
  59. package/lib/utils/data.js +35 -0
  60. package/lib/utils/tests.js +27 -18
  61. package/libx/_runtime/audit/generic/personal/access.js +81 -0
  62. package/libx/_runtime/audit/generic/personal/constants.js +4 -0
  63. package/libx/_runtime/audit/generic/personal/index.js +50 -0
  64. package/libx/_runtime/audit/generic/personal/modification.js +138 -0
  65. package/libx/_runtime/audit/generic/personal/utils.js +186 -0
  66. package/libx/_runtime/audit/utils/v2.js +10 -4
  67. package/libx/_runtime/cds-services/adapter/odata-v4/Dispatcher.js +5 -5
  68. package/libx/_runtime/cds-services/adapter/odata-v4/OData.js +6 -7
  69. package/libx/_runtime/cds-services/adapter/odata-v4/handlers/action.js +5 -7
  70. package/libx/_runtime/cds-services/adapter/odata-v4/handlers/create.js +5 -7
  71. package/libx/_runtime/cds-services/adapter/odata-v4/handlers/delete.js +2 -3
  72. package/libx/_runtime/cds-services/adapter/odata-v4/handlers/error.js +4 -0
  73. package/libx/_runtime/cds-services/adapter/odata-v4/handlers/metadata.js +7 -4
  74. package/libx/_runtime/cds-services/adapter/odata-v4/handlers/read.js +59 -8
  75. package/libx/_runtime/cds-services/adapter/odata-v4/handlers/request.js +11 -1
  76. package/libx/_runtime/cds-services/adapter/odata-v4/handlers/update.js +6 -10
  77. package/libx/_runtime/cds-services/adapter/odata-v4/odata-to-cqn/ExpressionToCQN.js +3 -46
  78. package/libx/_runtime/cds-services/adapter/odata-v4/odata-to-cqn/applyToCQN.js +2 -5
  79. package/libx/_runtime/cds-services/adapter/odata-v4/odata-to-cqn/createToCQN.js +2 -2
  80. package/libx/_runtime/cds-services/adapter/odata-v4/odata-to-cqn/deleteToCQN.js +4 -3
  81. package/libx/_runtime/cds-services/adapter/odata-v4/odata-to-cqn/expandToCQN.js +1 -2
  82. package/libx/_runtime/cds-services/adapter/odata-v4/odata-to-cqn/index.js +0 -1
  83. package/libx/_runtime/cds-services/adapter/odata-v4/odata-to-cqn/selectHelper.js +1 -1
  84. package/libx/_runtime/cds-services/adapter/odata-v4/odata-to-cqn/updateToCQN.js +2 -2
  85. package/libx/_runtime/cds-services/adapter/odata-v4/odata-to-cqn/utils.js +16 -18
  86. package/libx/_runtime/cds-services/adapter/odata-v4/okra/odata-commons/edm/EdmEntityType.js +6 -3
  87. package/libx/_runtime/cds-services/adapter/odata-v4/okra/odata-commons/format/RepresentationKind.js +4 -1
  88. package/libx/_runtime/cds-services/adapter/odata-v4/okra/odata-server/core/OdataRequest.js +1 -0
  89. package/libx/_runtime/cds-services/adapter/odata-v4/okra/odata-server/serializer/SerializerFactory.js +15 -2
  90. package/libx/_runtime/cds-services/adapter/odata-v4/okra/odata-server/validator/OperationValidator.js +1 -0
  91. package/libx/_runtime/cds-services/adapter/odata-v4/to.js +1 -1
  92. package/libx/_runtime/cds-services/adapter/odata-v4/utils/data.js +8 -1
  93. package/libx/_runtime/cds-services/adapter/odata-v4/utils/handlerUtils.js +6 -1
  94. package/libx/_runtime/cds-services/adapter/odata-v4/utils/omitValues.js +12 -5
  95. package/libx/_runtime/cds-services/adapter/odata-v4/utils/readAfterWrite.js +1 -7
  96. package/libx/_runtime/cds-services/adapter/odata-v4/utils/request.js +7 -7
  97. package/libx/_runtime/cds-services/adapter/odata-v4/utils/result.js +14 -18
  98. package/libx/_runtime/cds-services/adapter/odata-v4/utils/stream.js +13 -13
  99. package/libx/_runtime/cds-services/adapter/rest/handlers/create.js +0 -1
  100. package/libx/_runtime/cds-services/adapter/rest/handlers/operation.js +2 -1
  101. package/libx/_runtime/cds-services/adapter/rest/handlers/read.js +2 -2
  102. package/libx/_runtime/cds-services/adapter/rest/rest-to-cqn/index.js +2 -4
  103. package/libx/_runtime/cds-services/adapter/rest/utils/result.js +4 -2
  104. package/libx/_runtime/cds-services/services/Service.js +40 -5
  105. package/libx/_runtime/cds-services/services/utils/columns.js +13 -7
  106. package/libx/_runtime/cds-services/services/utils/compareJson.js +88 -4
  107. package/libx/_runtime/cds-services/services/utils/differ.js +24 -6
  108. package/libx/_runtime/cds-services/services/utils/handlerUtils.js +2 -2
  109. package/libx/_runtime/common/composition/data.js +44 -55
  110. package/libx/_runtime/common/composition/delete.js +97 -71
  111. package/libx/_runtime/common/composition/index.js +2 -1
  112. package/libx/_runtime/common/composition/insert.js +34 -11
  113. package/libx/_runtime/common/composition/tree.js +119 -92
  114. package/libx/_runtime/common/composition/update.js +4 -1
  115. package/libx/_runtime/common/composition/utils.js +1 -3
  116. package/libx/_runtime/common/constants/draft.js +12 -1
  117. package/libx/_runtime/common/generic/auth.js +6 -22
  118. package/libx/_runtime/common/generic/crud.js +14 -13
  119. package/libx/_runtime/common/generic/input.js +23 -26
  120. package/libx/_runtime/common/generic/put.js +1 -1
  121. package/libx/_runtime/common/generic/sorting.js +16 -16
  122. package/libx/_runtime/common/i18n/index.js +1 -1
  123. package/libx/_runtime/common/i18n/messages.properties +4 -0
  124. package/libx/_runtime/common/utils/backlinks.js +12 -5
  125. package/libx/_runtime/common/utils/cqn.js +6 -1
  126. package/libx/_runtime/common/utils/cqn2cqn4sql.js +102 -101
  127. package/libx/_runtime/common/utils/csn.js +47 -4
  128. package/libx/_runtime/common/utils/data.js +0 -37
  129. package/libx/_runtime/common/utils/enrichWithKeysFromWhere.js +1 -1
  130. package/libx/_runtime/common/utils/entityFromCqn.js +7 -24
  131. package/libx/_runtime/common/utils/foreignKeyPropagations.js +39 -7
  132. package/libx/_runtime/common/utils/generateOnCond.js +11 -12
  133. package/libx/_runtime/common/utils/onlyKeysRemain.js +10 -0
  134. package/libx/_runtime/common/utils/path.js +35 -0
  135. package/libx/_runtime/common/utils/postProcessing.js +86 -0
  136. package/libx/_runtime/common/utils/quotingStyles.js +37 -26
  137. package/libx/_runtime/common/utils/resolveView.js +223 -171
  138. package/libx/_runtime/common/utils/rewriteAsterisk.js +46 -26
  139. package/libx/_runtime/common/utils/structured.js +6 -12
  140. package/libx/_runtime/common/utils/template.js +10 -5
  141. package/libx/_runtime/common/utils/templateDelimiter.js +1 -0
  142. package/libx/_runtime/common/utils/templateProcessor.js +22 -30
  143. package/libx/_runtime/common/utils/union.js +31 -0
  144. package/libx/_runtime/common/utils/unionCqnTemplate.js +184 -0
  145. package/libx/_runtime/db/Service.js +1 -1
  146. package/libx/_runtime/db/data-conversion/timestamp.js +2 -9
  147. package/libx/_runtime/db/expand/expandCQNToJoin.js +204 -297
  148. package/libx/_runtime/db/expand/index.js +3 -3
  149. package/libx/_runtime/db/expand/rawToExpanded.js +36 -7
  150. package/libx/_runtime/db/generic/index.js +1 -1
  151. package/libx/_runtime/db/generic/input.js +5 -7
  152. package/libx/_runtime/db/generic/integrity.js +1 -1
  153. package/libx/_runtime/db/generic/rewrite.js +2 -10
  154. package/libx/_runtime/db/generic/update.js +13 -5
  155. package/libx/_runtime/db/generic/virtual.js +22 -58
  156. package/libx/_runtime/db/query/delete.js +7 -4
  157. package/libx/_runtime/db/query/insert.js +6 -4
  158. package/libx/_runtime/db/query/read.js +13 -20
  159. package/libx/_runtime/db/query/run.js +4 -1
  160. package/libx/_runtime/db/query/update.js +5 -4
  161. package/libx/_runtime/db/sql-builder/ExpressionBuilder.js +35 -2
  162. package/libx/_runtime/db/sql-builder/FunctionBuilder.js +17 -2
  163. package/libx/_runtime/db/sql-builder/InsertBuilder.js +6 -5
  164. package/libx/_runtime/db/sql-builder/ReferenceBuilder.js +10 -0
  165. package/libx/_runtime/db/sql-builder/SelectBuilder.js +35 -24
  166. package/libx/_runtime/db/sql-builder/UpdateBuilder.js +14 -4
  167. package/libx/_runtime/db/sql-builder/arrayed.js +4 -0
  168. package/libx/_runtime/db/utils/deep.js +8 -0
  169. package/libx/_runtime/db/utils/generateAliases.js +2 -1
  170. package/libx/_runtime/fiori/generic/activate.js +19 -15
  171. package/libx/_runtime/fiori/generic/before.js +3 -11
  172. package/libx/_runtime/fiori/generic/cancel.js +1 -1
  173. package/libx/_runtime/fiori/generic/delete.js +3 -1
  174. package/libx/_runtime/fiori/generic/edit.js +12 -2
  175. package/libx/_runtime/fiori/generic/new.js +5 -5
  176. package/libx/_runtime/fiori/generic/patch.js +0 -18
  177. package/libx/_runtime/fiori/generic/read.js +241 -189
  178. package/libx/_runtime/fiori/utils/delete.js +36 -7
  179. package/libx/_runtime/fiori/utils/handler.js +43 -44
  180. package/libx/_runtime/fiori/utils/where.js +30 -15
  181. package/libx/_runtime/hana/customBuilder/CustomSelectBuilder.js +4 -6
  182. package/libx/_runtime/hana/execute.js +2 -2
  183. package/libx/_runtime/hana/localized.js +4 -4
  184. package/libx/_runtime/hana/pool.js +29 -14
  185. package/libx/_runtime/hana/search2cqn4sql.js +2 -1
  186. package/libx/_runtime/hana/searchToContains.js +18 -14
  187. package/libx/_runtime/index.js +0 -5
  188. package/libx/_runtime/messaging/AMQPWebhookMessaging.js +13 -5
  189. package/libx/_runtime/messaging/common-utils/naming-conventions.js +4 -1
  190. package/libx/_runtime/messaging/enterprise-messaging-utils/EMManagement.js +31 -19
  191. package/libx/_runtime/messaging/enterprise-messaging-utils/registerEndpoints.js +1 -2
  192. package/libx/_runtime/messaging/enterprise-messaging.js +6 -4
  193. package/libx/_runtime/messaging/service.js +7 -6
  194. package/libx/_runtime/odata/cqn2odata.js +110 -43
  195. package/libx/_runtime/odata/index.js +26 -48
  196. package/libx/_runtime/odata/odata2cqn.js +1 -6154
  197. package/libx/_runtime/odata/odata2cqn.pegjs +559 -0
  198. package/libx/_runtime/odata/readToCqn.js +94 -64
  199. package/libx/_runtime/remote/Service.js +74 -21
  200. package/libx/_runtime/remote/cqn2odata/index.js +1 -5
  201. package/libx/_runtime/remote/utils/client.js +24 -101
  202. package/libx/_runtime/remote/utils/dataConversion.js +27 -12
  203. package/libx/_runtime/sqlite/Service.js +3 -5
  204. package/libx/_runtime/sqlite/execute.js +23 -24
  205. package/libx/_runtime/sqlite/localized.js +12 -7
  206. package/libx/_runtime/types/api.js +10 -0
  207. package/package.json +1 -1
  208. package/server.js +16 -2
  209. package/lib/ql/grammar.pegjs +0 -208
  210. package/lib/ql/parser.js +0 -1
  211. package/lib/ql/rt/DELETE.js +0 -29
  212. package/lib/ql/rt/INSERT.js +0 -23
  213. package/lib/ql/rt/Query.js +0 -84
  214. package/lib/ql/rt/SELECT.js +0 -174
  215. package/lib/ql/rt/UPDATE.js +0 -119
  216. package/lib/ql/rt/_helpers.js +0 -91
  217. package/lib/ql/rt/index.js +0 -32
  218. package/libx/_runtime/audit/generic/personal.js +0 -260
  219. package/libx/_runtime/cds-services/statements/BaseStatement.js +0 -72
  220. package/libx/_runtime/cds-services/statements/Create.js +0 -57
  221. package/libx/_runtime/cds-services/statements/Delete.js +0 -33
  222. package/libx/_runtime/cds-services/statements/Drop.js +0 -42
  223. package/libx/_runtime/cds-services/statements/Insert.js +0 -201
  224. package/libx/_runtime/cds-services/statements/Select.js +0 -826
  225. package/libx/_runtime/cds-services/statements/Update.js +0 -181
  226. package/libx/_runtime/cds-services/statements/Where.js +0 -726
  227. package/libx/_runtime/cds-services/statements/index.js +0 -25
  228. package/libx/_runtime/common/generic/resolve-mock.js +0 -9
@@ -1,72 +0,0 @@
1
- const cds = require('../../cds')
2
-
3
- const { invalidFunctionArgumentError } = require('../util/errors')
4
-
5
- const quotingStyles = require('../../common/utils/quotingStyles')
6
-
7
- const fnChain = Symbol.for('sap.cds.fnChain')
8
-
9
- const _resolveEntityAndKeys = pathExpression => {
10
- return {
11
- transformedEntity: `![${pathExpression.substring(0, pathExpression.indexOf('[')) || pathExpression}]`,
12
- keys: pathExpression.substring(pathExpression.indexOf('['))
13
- }
14
- }
15
-
16
- class BaseStatement {
17
- constructor(dml) {
18
- this[dml] = {}
19
-
20
- Object.defineProperty(this, fnChain, { value: dml, writable: true })
21
- }
22
-
23
- static _isEntity(value, fnName) {
24
- if (value) {
25
- const entity = value.name || value
26
-
27
- if (typeof entity === 'string') {
28
- return entity
29
- }
30
- }
31
-
32
- throw invalidFunctionArgumentError(fnName, value)
33
- }
34
-
35
- static convertPathExpression(entity) {
36
- if (entity.includes(':') && entity.includes('[')) {
37
- // if ']' included -> keys provided, ':' can be part of date key or navigationh
38
- const baseEntity = entity.substring(0, entity.indexOf(']') + 1)
39
- if (baseEntity.length === entity.length) {
40
- // no navigation
41
- const { transformedEntity, keys } = _resolveEntityAndKeys(baseEntity)
42
- return cds.parse.expr(`${transformedEntity}${keys}`)
43
- } else {
44
- // ']' is followed by ':', we use + 1 to get the path
45
- const path = entity.substring(baseEntity.length + 1, entity.length)
46
- const { transformedEntity, keys } = _resolveEntityAndKeys(baseEntity)
47
- return cds.parse.expr(`${transformedEntity}${keys}.${path}`)
48
- }
49
- }
50
-
51
- if (entity.includes(':')) {
52
- // if no ']' included -> no keys provided -> first ':' must be separator
53
- const [baseEntity, path] = entity.split(/:(.+)/)
54
- const { transformedEntity } = _resolveEntityAndKeys(baseEntity)
55
- return cds.parse.expr(`${transformedEntity}.${path}`)
56
- }
57
-
58
- // entity.includes.only('['))
59
- const { transformedEntity, keys } = _resolveEntityAndKeys(entity)
60
- return cds.parse.expr(`${transformedEntity}${keys}`)
61
- }
62
-
63
- static _quoteElement(element) {
64
- return quotingStyles[cds.env.sql.names || 'plain'](element)
65
- }
66
-
67
- get _isQuery() {
68
- return true
69
- }
70
- }
71
-
72
- module.exports = BaseStatement
@@ -1,57 +0,0 @@
1
- const BaseStatement = require('./BaseStatement')
2
- const { hasBeenCalledError, invalidFunctionArgumentError } = require('../util/errors')
3
-
4
- const fnChain = Symbol.for('sap.cds.fnChain')
5
-
6
- /**
7
- * CREATE statement creates entity
8
- */
9
- class Create extends BaseStatement {
10
- constructor() {
11
- super('CREATE')
12
- }
13
-
14
- /**
15
- * @param {string | object} entity - entity name or an entity from reflection API
16
- * @param {object} elements - dictionary as specified in CSN element definition
17
- */
18
- static entity(entity, elements) {
19
- BaseStatement._isEntity(entity, 'CREATE.entity()')
20
-
21
- const cqn = new Create()
22
- cqn[fnChain] = cqn[fnChain].concat('.entity()')
23
-
24
- if (elements) {
25
- cqn.CREATE.entity = { elements: elements, kind: 'entity', name: entity }
26
- } else {
27
- cqn.CREATE.entity = entity
28
- }
29
-
30
- return cqn
31
- }
32
-
33
- /**
34
- * Constructs a create statement as SELECT.from statement
35
- *
36
- * @param {object} query - SELECT query
37
- * @example
38
- * CREATE.entity('Bar').as(SELECT.from('Foo'))
39
- * @throws Error - if called twice or if query is not of expected format
40
- *
41
- */
42
- as(query) {
43
- this[fnChain] = this[fnChain].concat('.as()')
44
- if (!query || !query.SELECT) {
45
- throw invalidFunctionArgumentError(this[fnChain], query)
46
- }
47
-
48
- if (this.CREATE.as) {
49
- throw hasBeenCalledError('as()', this[fnChain])
50
- }
51
-
52
- this.CREATE.as = query
53
- return this
54
- }
55
- }
56
-
57
- module.exports = Create
@@ -1,33 +0,0 @@
1
- const Where = require('./Where')
2
-
3
- /**
4
- * DELETE statement removes values or rows from a specific table.
5
- *
6
- * @extends {Where}
7
- */
8
- class Delete extends Where {
9
- constructor() {
10
- super('DELETE')
11
- }
12
-
13
- /**
14
- * @param {string|object} entity string or an entity from reflection API
15
- */
16
- static from(entity) {
17
- const cqn = new Delete()
18
-
19
- if (typeof entity === 'string' && (entity.includes(':') || entity.includes('['))) {
20
- cqn.DELETE.from = Where.convertPathExpression(entity)
21
- } else {
22
- cqn.DELETE.from = Where._isEntity(entity, 'DELETE.from()')
23
- }
24
-
25
- return cqn
26
- }
27
-
28
- valueOf() {
29
- return `DELETE FROM ${Delete._quoteElement(this.DELETE.from)} `
30
- }
31
- }
32
-
33
- module.exports = Delete
@@ -1,42 +0,0 @@
1
- /* istanbul ignore file */
2
- // REVISIT: why never used?!
3
-
4
- const BaseStatement = require('./BaseStatement')
5
-
6
- /**
7
- * DROP statement removes a specific table and or view.
8
- */
9
- class Drop extends BaseStatement {
10
- constructor() {
11
- super('DROP')
12
- }
13
-
14
- /**
15
- * Drop table and or view for an entity.
16
- *
17
- * @param {string | object} entity - entity name or an entity from reflection API
18
- */
19
- static entity(entity) {
20
- BaseStatement._isEntity(entity, 'DROP.entity()')
21
- const cqn = new Drop()
22
- cqn.DROP.entity = entity
23
-
24
- return cqn
25
- }
26
-
27
- static table(entity) {
28
- const cqn = Drop.entity(entity)
29
- cqn.DROP.table = cqn.DROP.entity
30
-
31
- return cqn
32
- }
33
-
34
- static view(entity) {
35
- const cqn = Drop.entity(entity)
36
- cqn.DROP.view = cqn.DROP.entity
37
-
38
- return cqn
39
- }
40
- }
41
-
42
- module.exports = Drop
@@ -1,201 +0,0 @@
1
- const BaseStatement = require('./BaseStatement')
2
- const { invalidFunctionArgumentError } = require('../util/errors')
3
-
4
- const fnChain = Symbol.for('sap.cds.fnChain')
5
-
6
- /**
7
- * Insert values into table
8
- */
9
- class Insert extends BaseStatement {
10
- constructor() {
11
- super('INSERT')
12
- }
13
-
14
- /**
15
- * Name of the entity where the entries should be inserted
16
- *
17
- * @example <caption>In case the order of columns is known, their names can be omitted</caption>
18
- * INSERT.into('Books')
19
- * .rows([1, 'Harry Potter'])
20
- *
21
- * @example <caption>In case the order of columns is unknown, specify the name of the columns</caption>
22
- * INSERT.into('Books')
23
- * .columns('ID', 'NAME')
24
- * .rows([1, 'The Hitchhikers Guide to the Galaxy'])
25
- *
26
- * @example <caption>Use an object instead of array</caption>
27
- * INSERT.into('Books')
28
- * .rows({ID:1, NAME: 'Animal Farm'})
29
- *
30
- * @param {string|object} entity string or an entity from reflection API
31
- *
32
- * @returns {Insert} CQN object for Insert
33
- */
34
- static into(entity) {
35
- const cqn = new Insert()
36
- cqn[fnChain] = cqn[fnChain].concat('.into()')
37
-
38
- if (typeof entity === 'string' && (entity.includes(':') || entity.includes('['))) {
39
- cqn.INSERT.into = BaseStatement.convertPathExpression(entity)
40
- } else {
41
- cqn.INSERT.into = BaseStatement._isEntity(entity, 'INSERT.into()')
42
- }
43
-
44
- return cqn
45
- }
46
-
47
- /**
48
- * Optional. Can be used to order the value entries if column names are known
49
- *
50
- * @param {Array} columnNames - names of the columns
51
- * @param {Array} args - List of columns in case one arguemnt per column is used
52
- * @returns {Insert} current instance of the insert
53
- * @throws Error if columnNames is neither String nor Array
54
- */
55
- columns(columnNames, ...args) {
56
- this[fnChain] = this[fnChain].concat('.columns()')
57
- if (typeof columnNames === 'string') {
58
- args.unshift(columnNames)
59
- columnNames = args
60
- }
61
-
62
- if (Array.isArray(columnNames)) {
63
- this.INSERT.columns = columnNames
64
- return this
65
- }
66
-
67
- throw invalidFunctionArgumentError(this[fnChain], columnNames)
68
- }
69
-
70
- /**
71
- * Adds one or multiple rows to the table.
72
- *
73
- * @example <caption>In case the order of columns is known, their names can be omitted</caption>
74
- * @param {...any} args
75
- * INSERT.into('Books')
76
- * .rows([1, 'Harry Potter'])
77
- *
78
- * @example <caption>If order is unknown specify the name of the columns</caption>
79
- * INSERT.into('Books')
80
- * .columns('ID', 'NAME')
81
- * .rows([1, 'The Hitchhikers Guide to the Galaxy'])
82
- *
83
- * @example <caption>Use an object instead of array</caption>
84
- * INSERT.into('Books')
85
- * .rows({ID:1, NAME: 'Animal Farm'})
86
- *
87
- * @example <caption>Batch insert</caption>
88
- * INSERT.into('Books').rows([
89
- * [1, 'Harry Potter'],
90
- * [2, 'The Hitchhikers Guide to the Galaxy'],
91
- * [3, 'Animal Farm']
92
- * ])
93
- * @example <caption>Batch insert with objects</caption>
94
- * INSERT.into('Books').rows([
95
- * {ID:1, NAME: 'Harry Potter'},
96
- * {ID:2, NAME: 'The Hitchhikers Guide to the Galaxy'},
97
- * {ID:3, NAME: 'Animal Farm'}
98
- * ])
99
- * @param {Array | object} rows
100
- * @returns {object} query object according to CQN format
101
- * @throws Error if no valid rows are provided
102
- */
103
- rows(rows, ...args) {
104
- this[fnChain] = this[fnChain].concat('.rows()')
105
- if (!rows) {
106
- throw invalidFunctionArgumentError(this[fnChain])
107
- }
108
-
109
- if (Array.isArray(rows)) {
110
- // check if we have at least one element
111
- if (rows.length === 0) {
112
- throw invalidFunctionArgumentError(this[fnChain], rows)
113
- }
114
-
115
- // check if all the entries in the array are arrays
116
- if (rows.every(e => Array.isArray(e))) {
117
- this._rows(rows)
118
- // check if array contains one or multiple objects
119
- } else if (rows.every(e => typeof e === 'object')) {
120
- this._entries(rows)
121
- // the rows have been added as arguments
122
- } else if (args.length !== 0) {
123
- args.unshift(rows)
124
- this._rows(args)
125
- } else {
126
- this._values(rows)
127
- }
128
- } else if (typeof rows === 'object') {
129
- this._entries(rows)
130
- } else {
131
- throw invalidFunctionArgumentError(this[fnChain], rows)
132
- }
133
-
134
- return this
135
- }
136
-
137
- /**
138
- * Adds one or more entries as rows.
139
- *
140
- * @param {...any} args
141
- * @throws Error if no valid entries are provided
142
- */
143
- entries(...args) {
144
- this[fnChain] = this[fnChain].concat('.entries()')
145
- const entries = Array.isArray(args[0]) ? args[0] : args
146
- this._invalidArgs('entries', entries)
147
- return this.rows(entries)
148
- }
149
-
150
- values(...args) {
151
- this[fnChain] = this[fnChain].concat('.values()')
152
- const values = Array.isArray(args[0]) ? args[0] : args
153
- this._invalidArgs('values', values)
154
- this._values(values)
155
- return this
156
- }
157
-
158
- as(selectCqn) {
159
- this[fnChain] = this[fnChain].concat('.as()')
160
- if (!selectCqn || !selectCqn.SELECT) {
161
- throw invalidFunctionArgumentError(this[fnChain], selectCqn)
162
- }
163
-
164
- this.INSERT.as = selectCqn
165
- return this
166
- }
167
-
168
- _invalidArgs(argName, args) {
169
- if (!Array.isArray(args) || args.length === 0) {
170
- throw invalidFunctionArgumentError(this[fnChain], args)
171
- }
172
- }
173
-
174
- _rows(rows) {
175
- this.INSERT.rows = rows
176
- }
177
-
178
- _values(values) {
179
- this.INSERT.values = values
180
- }
181
-
182
- _entries(entries) {
183
- // object can be passed directly, so if only 1 object is passed wrap it into an array
184
- this.INSERT.entries = Array.isArray(entries) ? entries : [entries]
185
- }
186
-
187
- valueOf() {
188
- return `INSERT INTO ${Insert._quoteElement(this.INSERT.into)} `
189
- }
190
- }
191
-
192
- const insert = (...entries) => {
193
- return {
194
- into: entity => {
195
- return Insert.into(entity).entries(...entries)
196
- }
197
- }
198
- }
199
- insert.into = Insert.into
200
-
201
- module.exports = insert