powersync 0.1.0 → 0.1.1

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 (191) hide show
  1. package/README.md +14 -14
  2. package/dist/commands/init.d.ts +0 -1
  3. package/dist/commands/init.js +9 -8
  4. package/dist/commands/instance/sync-rules/generate-schema.js +6 -6
  5. package/dist/constants.d.ts +2 -5
  6. package/dist/constants.js +4 -2
  7. package/dist/service/accounts.client.js +11129 -25
  8. package/dist/service/powersync.client.js +37453 -113
  9. package/dist/service/types.js +1 -1
  10. package/dist/service/utils.js +6 -6
  11. package/dist/utils.js +3 -3
  12. package/oclif.manifest.json +1 -1
  13. package/package.json +28 -20
  14. package/dist/powersync-sdk/accounts/api.d.ts +0 -717
  15. package/dist/powersync-sdk/accounts/api.js +0 -182
  16. package/dist/powersync-sdk/accounts/client.d.ts +0 -1096
  17. package/dist/powersync-sdk/accounts/client.js +0 -124
  18. package/dist/powersync-sdk/accounts/codecs.d.ts +0 -32
  19. package/dist/powersync-sdk/accounts/codecs.js +0 -93
  20. package/dist/powersync-sdk/accounts/index.d.ts +0 -4
  21. package/dist/powersync-sdk/accounts/index.js +0 -4
  22. package/dist/powersync-sdk/accounts/models.d.ts +0 -260
  23. package/dist/powersync-sdk/accounts/models.js +0 -96
  24. package/dist/powersync-sdk/accounts/routes.d.ts +0 -44
  25. package/dist/powersync-sdk/accounts/routes.js +0 -45
  26. package/dist/powersync-sdk/accounts/source.d.ts +0 -76
  27. package/dist/powersync-sdk/accounts/source.js +0 -32
  28. package/dist/powersync-sdk/errors/errors.d.ts +0 -62
  29. package/dist/powersync-sdk/errors/errors.js +0 -115
  30. package/dist/powersync-sdk/errors/index.d.ts +0 -2
  31. package/dist/powersync-sdk/errors/index.js +0 -2
  32. package/dist/powersync-sdk/errors/utils.d.ts +0 -4
  33. package/dist/powersync-sdk/errors/utils.js +0 -18
  34. package/dist/powersync-sdk/fetch-clients/codecs.d.ts +0 -4
  35. package/dist/powersync-sdk/fetch-clients/codecs.js +0 -34
  36. package/dist/powersync-sdk/fetch-clients/decoders.d.ts +0 -3
  37. package/dist/powersync-sdk/fetch-clients/decoders.js +0 -35
  38. package/dist/powersync-sdk/fetch-clients/definitions.d.ts +0 -97
  39. package/dist/powersync-sdk/fetch-clients/definitions.js +0 -22
  40. package/dist/powersync-sdk/fetch-clients/errors.d.ts +0 -7
  41. package/dist/powersync-sdk/fetch-clients/errors.js +0 -19
  42. package/dist/powersync-sdk/fetch-clients/index.d.ts +0 -5
  43. package/dist/powersync-sdk/fetch-clients/index.js +0 -5
  44. package/dist/powersync-sdk/fetch-clients/node-client.d.ts +0 -9
  45. package/dist/powersync-sdk/fetch-clients/node-client.js +0 -107
  46. package/dist/powersync-sdk/fetch-clients/request.d.ts +0 -11
  47. package/dist/powersync-sdk/fetch-clients/request.js +0 -138
  48. package/dist/powersync-sdk/fetch-clients/streaming.d.ts +0 -65
  49. package/dist/powersync-sdk/fetch-clients/streaming.js +0 -99
  50. package/dist/powersync-sdk/fetch-clients/utils.d.ts +0 -13
  51. package/dist/powersync-sdk/fetch-clients/utils.js +0 -43
  52. package/dist/powersync-sdk/fetch-clients/web-client.d.ts +0 -14
  53. package/dist/powersync-sdk/fetch-clients/web-client.js +0 -56
  54. package/dist/powersync-sdk/json/index.d.ts +0 -4
  55. package/dist/powersync-sdk/json/index.js +0 -3
  56. package/dist/powersync-sdk/json/json.d.ts +0 -5
  57. package/dist/powersync-sdk/json/json.js +0 -35
  58. package/dist/powersync-sdk/json/json_container.d.ts +0 -18
  59. package/dist/powersync-sdk/json/json_container.js +0 -28
  60. package/dist/powersync-sdk/json/json_stringify.d.ts +0 -36
  61. package/dist/powersync-sdk/json/json_stringify.js +0 -162
  62. package/dist/powersync-sdk/powersync/client.d.ts +0 -449
  63. package/dist/powersync-sdk/powersync/client.js +0 -53
  64. package/dist/powersync-sdk/powersync/config/HostedConfig.d.ts +0 -327
  65. package/dist/powersync-sdk/powersync/config/HostedConfig.js +0 -92
  66. package/dist/powersync-sdk/powersync/config/PowerSyncConfig.d.ts +0 -211
  67. package/dist/powersync-sdk/powersync/config/PowerSyncConfig.js +0 -97
  68. package/dist/powersync-sdk/powersync/config/normalize.d.ts +0 -32
  69. package/dist/powersync-sdk/powersync/config/normalize.js +0 -85
  70. package/dist/powersync-sdk/powersync/definitions.d.ts +0 -149
  71. package/dist/powersync-sdk/powersync/definitions.js +0 -97
  72. package/dist/powersync-sdk/powersync/index.d.ts +0 -8
  73. package/dist/powersync-sdk/powersync/index.js +0 -8
  74. package/dist/powersync-sdk/powersync/internal_routes.d.ts +0 -247
  75. package/dist/powersync-sdk/powersync/internal_routes.js +0 -69
  76. package/dist/powersync-sdk/powersync/routes.d.ts +0 -779
  77. package/dist/powersync-sdk/powersync/routes.js +0 -191
  78. package/dist/powersync-sdk/sdk-clients/endpoints.d.ts +0 -31
  79. package/dist/powersync-sdk/sdk-clients/endpoints.js +0 -48
  80. package/dist/powersync-sdk/sdk-clients/index.d.ts +0 -3
  81. package/dist/powersync-sdk/sdk-clients/index.js +0 -3
  82. package/dist/powersync-sdk/sdk-clients/pagination.d.ts +0 -14
  83. package/dist/powersync-sdk/sdk-clients/pagination.js +0 -21
  84. package/dist/powersync-sdk/sdk-clients/sdk-client.d.ts +0 -17
  85. package/dist/powersync-sdk/sdk-clients/sdk-client.js +0 -30
  86. package/dist/powersync-sdk/streaming/bson/buffer-array.d.ts +0 -31
  87. package/dist/powersync-sdk/streaming/bson/buffer-array.js +0 -79
  88. package/dist/powersync-sdk/streaming/bson/constants.d.ts +0 -2
  89. package/dist/powersync-sdk/streaming/bson/constants.js +0 -1
  90. package/dist/powersync-sdk/streaming/bson/decoder.d.ts +0 -10
  91. package/dist/powersync-sdk/streaming/bson/decoder.js +0 -62
  92. package/dist/powersync-sdk/streaming/bson/encoder.d.ts +0 -10
  93. package/dist/powersync-sdk/streaming/bson/encoder.js +0 -23
  94. package/dist/powersync-sdk/streaming/bson/header.d.ts +0 -11
  95. package/dist/powersync-sdk/streaming/bson/header.js +0 -52
  96. package/dist/powersync-sdk/streaming/bson/index.d.ts +0 -5
  97. package/dist/powersync-sdk/streaming/bson/index.js +0 -5
  98. package/dist/powersync-sdk/streaming/core/backpressure.d.ts +0 -15
  99. package/dist/powersync-sdk/streaming/core/backpressure.js +0 -34
  100. package/dist/powersync-sdk/streaming/core/cross-stream.d.ts +0 -6
  101. package/dist/powersync-sdk/streaming/core/cross-stream.js +0 -17
  102. package/dist/powersync-sdk/streaming/core/index.d.ts +0 -4
  103. package/dist/powersync-sdk/streaming/core/index.js +0 -4
  104. package/dist/powersync-sdk/streaming/core/node-utils.d.ts +0 -7
  105. package/dist/powersync-sdk/streaming/core/node-utils.js +0 -11
  106. package/dist/powersync-sdk/streaming/core/transformers.d.ts +0 -41
  107. package/dist/powersync-sdk/streaming/core/transformers.js +0 -70
  108. package/dist/powersync-sdk/streaming/core/utils.d.ts +0 -36
  109. package/dist/powersync-sdk/streaming/core/utils.js +0 -66
  110. package/dist/powersync-sdk/streaming/index.d.ts +0 -3
  111. package/dist/powersync-sdk/streaming/index.js +0 -3
  112. package/dist/powersync-sdk/streaming/web.d.ts +0 -4
  113. package/dist/powersync-sdk/streaming/web.js +0 -4
  114. package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/binaryreader.d.ts +0 -2
  115. package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/binaryreader.js +0 -36
  116. package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/binarywriter.d.ts +0 -2
  117. package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/binarywriter.js +0 -58
  118. package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/geometry.d.ts +0 -18
  119. package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/geometry.js +0 -348
  120. package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/geometrycollection.d.ts +0 -11
  121. package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/geometrycollection.js +0 -118
  122. package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/linestring.d.ts +0 -11
  123. package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/linestring.js +0 -131
  124. package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/multilinestring.d.ts +0 -11
  125. package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/multilinestring.js +0 -134
  126. package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/multipoint.d.ts +0 -11
  127. package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/multipoint.js +0 -122
  128. package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/multipolygon.d.ts +0 -11
  129. package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/multipolygon.js +0 -158
  130. package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/point.d.ts +0 -14
  131. package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/point.js +0 -165
  132. package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/polygon.d.ts +0 -11
  133. package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/polygon.js +0 -207
  134. package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/types.d.ts +0 -30
  135. package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/types.js +0 -29
  136. package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/wktparser.d.ts +0 -2
  137. package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/wktparser.js +0 -111
  138. package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/wkx.d.ts +0 -9
  139. package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/wkx.js +0 -9
  140. package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/zigzag.d.ts +0 -5
  141. package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/zigzag.js +0 -9
  142. package/dist/powersync-sdk/sync-rules/DartSchemaGenerator.d.ts +0 -12
  143. package/dist/powersync-sdk/sync-rules/DartSchemaGenerator.js +0 -35
  144. package/dist/powersync-sdk/sync-rules/ExpressionType.d.ts +0 -33
  145. package/dist/powersync-sdk/sync-rules/ExpressionType.js +0 -62
  146. package/dist/powersync-sdk/sync-rules/IdSequence.d.ts +0 -4
  147. package/dist/powersync-sdk/sync-rules/IdSequence.js +0 -7
  148. package/dist/powersync-sdk/sync-rules/JsSchemaGenerator.d.ts +0 -12
  149. package/dist/powersync-sdk/sync-rules/JsSchemaGenerator.js +0 -38
  150. package/dist/powersync-sdk/sync-rules/SchemaGenerator.d.ts +0 -14
  151. package/dist/powersync-sdk/sync-rules/SchemaGenerator.js +0 -24
  152. package/dist/powersync-sdk/sync-rules/SourceTableInterface.d.ts +0 -5
  153. package/dist/powersync-sdk/sync-rules/SourceTableInterface.js +0 -1
  154. package/dist/powersync-sdk/sync-rules/SqlBucketDescriptor.d.ts +0 -37
  155. package/dist/powersync-sdk/sync-rules/SqlBucketDescriptor.js +0 -116
  156. package/dist/powersync-sdk/sync-rules/SqlDataQuery.d.ts +0 -39
  157. package/dist/powersync-sdk/sync-rules/SqlDataQuery.js +0 -245
  158. package/dist/powersync-sdk/sync-rules/SqlParameterQuery.d.ts +0 -43
  159. package/dist/powersync-sdk/sync-rules/SqlParameterQuery.js +0 -254
  160. package/dist/powersync-sdk/sync-rules/SqlSyncRules.d.ts +0 -52
  161. package/dist/powersync-sdk/sync-rules/SqlSyncRules.js +0 -252
  162. package/dist/powersync-sdk/sync-rules/StaticSchema.d.ts +0 -26
  163. package/dist/powersync-sdk/sync-rules/StaticSchema.js +0 -71
  164. package/dist/powersync-sdk/sync-rules/StaticSqlParameterQuery.d.ts +0 -24
  165. package/dist/powersync-sdk/sync-rules/StaticSqlParameterQuery.js +0 -71
  166. package/dist/powersync-sdk/sync-rules/TablePattern.d.ts +0 -17
  167. package/dist/powersync-sdk/sync-rules/TablePattern.js +0 -56
  168. package/dist/powersync-sdk/sync-rules/TableQuerySchema.d.ts +0 -9
  169. package/dist/powersync-sdk/sync-rules/TableQuerySchema.js +0 -41
  170. package/dist/powersync-sdk/sync-rules/errors.d.ts +0 -22
  171. package/dist/powersync-sdk/sync-rules/errors.js +0 -60
  172. package/dist/powersync-sdk/sync-rules/generators.d.ts +0 -6
  173. package/dist/powersync-sdk/sync-rules/generators.js +0 -6
  174. package/dist/powersync-sdk/sync-rules/index.d.ts +0 -17
  175. package/dist/powersync-sdk/sync-rules/index.js +0 -17
  176. package/dist/powersync-sdk/sync-rules/json_schema.d.ts +0 -3
  177. package/dist/powersync-sdk/sync-rules/json_schema.js +0 -50
  178. package/dist/powersync-sdk/sync-rules/sql_filters.d.ts +0 -91
  179. package/dist/powersync-sdk/sync-rules/sql_filters.js +0 -491
  180. package/dist/powersync-sdk/sync-rules/sql_functions.d.ts +0 -53
  181. package/dist/powersync-sdk/sync-rules/sql_functions.js +0 -755
  182. package/dist/powersync-sdk/sync-rules/sql_support.d.ts +0 -22
  183. package/dist/powersync-sdk/sync-rules/sql_support.js +0 -208
  184. package/dist/powersync-sdk/sync-rules/types.d.ts +0 -174
  185. package/dist/powersync-sdk/sync-rules/types.js +0 -9
  186. package/dist/powersync-sdk/sync-rules/utils.d.ts +0 -45
  187. package/dist/powersync-sdk/sync-rules/utils.js +0 -163
  188. package/dist/service/accounts.client.d.ts +0 -26
  189. package/dist/service/powersync.client.d.ts +0 -269
  190. package/dist/service/types.d.ts +0 -69
  191. package/dist/service/utils.d.ts +0 -5
@@ -1,491 +0,0 @@
1
- import { ExpressionType, TYPE_NONE } from './ExpressionType.js';
2
- import { SqlRuleError } from './errors.js';
3
- import { BASIC_OPERATORS, CAST_TYPES, SQL_FUNCTIONS, cast, jsonExtract, sqliteTypeOf } from './sql_functions.js';
4
- import { SQLITE_FALSE, SQLITE_TRUE, andFilters, compileStaticOperator, isClauseError, isParameterMatchClause, isParameterValueClause, isStaticRowValueClause, orFilters, sqliteNot, toBooleanParameterSetClause } from './sql_support.js';
5
- import { isJsonValue } from './utils.js';
6
- export const MATCH_CONST_FALSE = [];
7
- export const MATCH_CONST_TRUE = [{}];
8
- Object.freeze(MATCH_CONST_TRUE);
9
- Object.freeze(MATCH_CONST_FALSE);
10
- export class SqlTools {
11
- default_table;
12
- errors = [];
13
- parameter_tables;
14
- schema;
15
- sql;
16
- supports_expanding_parameters;
17
- value_tables;
18
- constructor(options) {
19
- this.default_table = options.table;
20
- this.schema = options.schema;
21
- if (options.value_tables) {
22
- this.value_tables = options.value_tables;
23
- }
24
- else if (this.default_table) {
25
- this.value_tables = [this.default_table];
26
- }
27
- else {
28
- this.value_tables = [];
29
- }
30
- this.parameter_tables = options.parameter_tables ?? [];
31
- this.sql = options.sql;
32
- this.supports_expanding_parameters = options.supports_expanding_parameters ?? false;
33
- }
34
- /**
35
- * Given an expression, return a compiled clause.
36
- */
37
- compileClause(expr) {
38
- if (expr == null) {
39
- return {
40
- evaluate: () => SQLITE_TRUE,
41
- getType() {
42
- return ExpressionType.INTEGER;
43
- }
44
- };
45
- }
46
- if (isStatic(expr)) {
47
- const value = staticValue(expr);
48
- return {
49
- evaluate: () => value,
50
- getType() {
51
- return ExpressionType.fromTypeText(sqliteTypeOf(value));
52
- }
53
- };
54
- }
55
- if (expr.type == 'ref') {
56
- const column = expr.name;
57
- if (column == '*') {
58
- return this.error('* not supported here', expr);
59
- }
60
- if (this.refHasSchema(expr)) {
61
- return this.error(`Schema is not supported in column references`, expr);
62
- }
63
- if (this.isParameterRef(expr)) {
64
- const param = this.getParameterRef(expr);
65
- return { bucketParameter: param };
66
- }
67
- if (this.isTableRef(expr)) {
68
- const table = this.getTableName(expr);
69
- this.checkRef(table, expr);
70
- return {
71
- evaluate(tables) {
72
- return tables[table]?.[column];
73
- },
74
- getType(schema) {
75
- return schema.getType(table, column);
76
- }
77
- };
78
- }
79
- const ref = [expr.table?.schema, expr.table?.name, expr.name]
80
- .filter((e) => e != null)
81
- .join('.');
82
- return this.error(`Undefined reference: ${ref}`, expr);
83
- }
84
- if (expr.type == 'binary') {
85
- const { left, op, right } = expr;
86
- const leftFilter = this.compileClause(left);
87
- const rightFilter = this.compileClause(right);
88
- if (isClauseError(leftFilter) || isClauseError(rightFilter)) {
89
- return { error: true };
90
- }
91
- if (op == 'AND') {
92
- try {
93
- return andFilters(leftFilter, rightFilter);
94
- }
95
- catch (error) {
96
- return this.error(error.message, expr);
97
- }
98
- }
99
- else if (op == 'OR') {
100
- try {
101
- return orFilters(leftFilter, rightFilter);
102
- }
103
- catch (error) {
104
- return this.error(error.message, expr);
105
- }
106
- }
107
- else if (op == '=') {
108
- // Options:
109
- // 1. static, static
110
- // 2. static, parameterValue
111
- // 3. static true, parameterMatch - not supported yet
112
- let staticFilter1;
113
- let otherFilter1;
114
- if (!isStaticRowValueClause(leftFilter) && !isStaticRowValueClause(rightFilter)) {
115
- return this.error(`Cannot have bucket parameters on both sides of = operator`, expr);
116
- }
117
- if (isStaticRowValueClause(leftFilter)) {
118
- staticFilter1 = leftFilter;
119
- otherFilter1 = rightFilter;
120
- }
121
- else {
122
- staticFilter1 = rightFilter;
123
- otherFilter1 = leftFilter;
124
- }
125
- const staticFilter = staticFilter1;
126
- const otherFilter = otherFilter1;
127
- if (isStaticRowValueClause(otherFilter)) {
128
- // 1. static, static
129
- return compileStaticOperator(op, leftFilter, rightFilter);
130
- }
131
- if (isParameterValueClause(otherFilter)) {
132
- // 2. static, parameterValue
133
- return {
134
- bucketParameters: [otherFilter.bucketParameter],
135
- error: false,
136
- filter(tables) {
137
- const value = staticFilter.evaluate(tables);
138
- if (value == null) {
139
- // null never matches on =
140
- // Should technically return null, but "false" is sufficient here
141
- return MATCH_CONST_FALSE;
142
- }
143
- if (!isJsonValue(value)) {
144
- // Cannot persist this, e.g. BLOB
145
- return MATCH_CONST_FALSE;
146
- }
147
- return [{ [otherFilter.bucketParameter]: value }];
148
- },
149
- unbounded: false
150
- };
151
- }
152
- if (isParameterMatchClause(otherFilter)) {
153
- // 3. static, parameterMatch
154
- // (bucket.param = 'something') = staticValue
155
- // To implement this, we need to ensure the static value here can only be true.
156
- return this.error(`Bucket parameter clauses cannot currently be combined with other operators`, expr);
157
- }
158
- throw new Error('Unexpected');
159
- }
160
- else if (op == 'IN') {
161
- // Options:
162
- // static IN static
163
- // parameterValue IN static
164
- if (isStaticRowValueClause(leftFilter) && isStaticRowValueClause(rightFilter)) {
165
- return compileStaticOperator(op, leftFilter, rightFilter);
166
- }
167
- if (isParameterValueClause(leftFilter) && isStaticRowValueClause(rightFilter)) {
168
- const param = leftFilter.bucketParameter;
169
- return {
170
- bucketParameters: [param],
171
- error: false,
172
- filter(tables) {
173
- const aValue = rightFilter.evaluate(tables);
174
- if (aValue == null) {
175
- return MATCH_CONST_FALSE;
176
- }
177
- const values = JSON.parse(aValue);
178
- if (!Array.isArray(values)) {
179
- throw new TypeError('Not an array');
180
- }
181
- return values.map((value) => {
182
- return { [param]: value };
183
- });
184
- },
185
- unbounded: true
186
- };
187
- }
188
- if (this.supports_expanding_parameters &&
189
- isStaticRowValueClause(leftFilter) &&
190
- isParameterValueClause(rightFilter)) {
191
- const param = `${rightFilter.bucketParameter}[*]`;
192
- return {
193
- bucketParameters: [param],
194
- error: false,
195
- filter(tables) {
196
- const value = leftFilter.evaluate(tables);
197
- if (!isJsonValue(value)) {
198
- // Cannot persist, e.g. BLOB
199
- return MATCH_CONST_FALSE;
200
- }
201
- return [{ [param]: value }];
202
- },
203
- unbounded: false
204
- };
205
- }
206
- return this.error(`Unsupported usage of IN operator`, expr);
207
- }
208
- else if (BASIC_OPERATORS.has(op)) {
209
- if (!isStaticRowValueClause(leftFilter) || !isStaticRowValueClause(rightFilter)) {
210
- return this.error(`Operator ${op} is not supported on bucket parameters`, expr);
211
- }
212
- return compileStaticOperator(op, leftFilter, rightFilter);
213
- }
214
- else {
215
- return this.error(`Operator not supported: ${op}`, expr);
216
- }
217
- }
218
- else if (expr.type == 'unary') {
219
- if (expr.op == 'NOT') {
220
- const filter = this.compileClause(expr.operand);
221
- if (isClauseError(filter)) {
222
- return filter;
223
- }
224
- if (!isStaticRowValueClause(filter)) {
225
- return this.error('Cannot use NOT on bucket parameter filters', expr);
226
- }
227
- return {
228
- evaluate(tables) {
229
- const value = filter.evaluate(tables);
230
- return sqliteNot(value);
231
- },
232
- getType() {
233
- return ExpressionType.INTEGER;
234
- }
235
- };
236
- }
237
- if (expr.op == 'IS NULL') {
238
- const leftFilter = this.compileClause(expr.operand);
239
- if (isClauseError(leftFilter)) {
240
- return leftFilter;
241
- }
242
- if (isStaticRowValueClause(leftFilter)) {
243
- // 1. static IS NULL
244
- const nullValue = {
245
- evaluate: () => null,
246
- getType() {
247
- return ExpressionType.INTEGER;
248
- }
249
- };
250
- return compileStaticOperator('IS', leftFilter, nullValue);
251
- }
252
- if (isParameterValueClause(leftFilter)) {
253
- // 2. param IS NULL
254
- return {
255
- bucketParameters: [leftFilter.bucketParameter],
256
- error: false,
257
- filter(tables) {
258
- return [{ [leftFilter.bucketParameter]: null }];
259
- },
260
- unbounded: false
261
- };
262
- }
263
- return this.error(`Cannot use IS NULL here`, expr);
264
- }
265
- if (expr.op == 'IS NOT NULL') {
266
- const leftFilter = this.compileClause(expr.operand);
267
- if (isClauseError(leftFilter)) {
268
- return leftFilter;
269
- }
270
- if (isStaticRowValueClause(leftFilter)) {
271
- // 1. static IS NULL
272
- const nullValue = {
273
- evaluate: () => null,
274
- getType() {
275
- return ExpressionType.INTEGER;
276
- }
277
- };
278
- return compileStaticOperator('IS NOT', leftFilter, nullValue);
279
- }
280
- return this.error(`Cannot use IS NOT NULL here`, expr);
281
- }
282
- return this.error(`Operator ${expr.op} is not supported`, expr);
283
- }
284
- else if (expr.type == 'call' && expr.function?.name != null) {
285
- const fn = expr.function.name;
286
- const fnImpl = SQL_FUNCTIONS[fn];
287
- if (fnImpl == null) {
288
- return this.error(`Function '${fn}' is not defined`, expr);
289
- }
290
- let error = false;
291
- const argExtractors = expr.args.map((arg) => {
292
- const clause = this.compileClause(arg);
293
- if (isClauseError(clause)) {
294
- error = true;
295
- }
296
- else if (!isStaticRowValueClause(clause)) {
297
- error = true;
298
- return this.error(`Bucket parameters are not supported in function call arguments`, arg);
299
- }
300
- return clause;
301
- });
302
- if (error) {
303
- return { error: true };
304
- }
305
- return {
306
- evaluate(tables) {
307
- const args = argExtractors.map((e) => e.evaluate(tables));
308
- return fnImpl.call(...args);
309
- },
310
- getType(schema) {
311
- const argTypes = argExtractors.map((e) => e.getType(schema));
312
- return fnImpl.getReturnType(argTypes);
313
- }
314
- };
315
- }
316
- else if (expr.type == 'member') {
317
- const operand = this.compileClause(expr.operand);
318
- if (isClauseError(operand)) {
319
- return operand;
320
- }
321
- if (!isStaticRowValueClause(operand)) {
322
- return this.error(`Bucket parameters are not supported in member lookups`, expr.operand);
323
- }
324
- if (typeof expr.member == 'string' && (expr.op == '->>' || expr.op == '->')) {
325
- return {
326
- evaluate(tables) {
327
- const containerString = operand.evaluate(tables);
328
- return jsonExtract(containerString, expr.member, expr.op);
329
- },
330
- getType() {
331
- return ExpressionType.ANY_JSON;
332
- }
333
- };
334
- }
335
- return this.error(`Unsupported member operation ${expr.op}`, expr);
336
- }
337
- else if (expr.type == 'cast') {
338
- const operand = this.compileClause(expr.operand);
339
- if (isClauseError(operand)) {
340
- return operand;
341
- }
342
- if (!isStaticRowValueClause(operand)) {
343
- return this.error(`Bucket parameters are not supported in cast expressions`, expr.operand);
344
- }
345
- const to = expr.to?.name?.toLowerCase();
346
- if (CAST_TYPES.has(to)) {
347
- return {
348
- evaluate(tables) {
349
- const value = operand.evaluate(tables);
350
- if (value == null) {
351
- return null;
352
- }
353
- return cast(value, to);
354
- },
355
- getType() {
356
- return ExpressionType.fromTypeText(to);
357
- }
358
- };
359
- }
360
- return this.error(`CAST not supported for '${to}'`, expr);
361
- }
362
- else {
363
- return this.error(`${expr.type} not supported here`, expr);
364
- }
365
- }
366
- compileStaticExtractor(expr) {
367
- const clause = this.compileClause(expr);
368
- if (!isStaticRowValueClause(clause) && !isClauseError(clause)) {
369
- throw new SqlRuleError('Bucket parameters are not allowed here', this.sql, expr ?? undefined);
370
- }
371
- return clause;
372
- }
373
- /**
374
- * Compile the where clause into a ParameterMatchClause.
375
- *
376
- * A ParameterMatchClause takes a data row, and returns filter values that
377
- * would make the expression true for the row.
378
- */
379
- compileWhereClause(where) {
380
- const base = this.compileClause(where);
381
- return toBooleanParameterSetClause(base);
382
- }
383
- error(message, expr) {
384
- this.errors.push(new SqlRuleError(message, this.sql, expr));
385
- return { error: true };
386
- }
387
- /**
388
- * "some_column" => "some_column"
389
- * "table.some_column" => "some_column".
390
- * "some_function() AS some_column" => "some_column"
391
- * "some_function() some_column" => "some_column"
392
- * "some_function()" => error
393
- */
394
- getOutputName(column) {
395
- const alias = column.alias?.name;
396
- if (alias) {
397
- return alias;
398
- }
399
- const { expr } = column;
400
- if (expr.type == 'ref') {
401
- return expr.name;
402
- }
403
- throw new SqlRuleError(`alias is required`, this.sql, column.expr);
404
- }
405
- getParameterRef(expr) {
406
- if (this.isParameterRef(expr)) {
407
- return `${expr.table.name}.${expr.name}`;
408
- }
409
- }
410
- getSpecificOutputName(column) {
411
- const name = this.getOutputName(column);
412
- if (name == '*') {
413
- throw new SqlRuleError('* is not supported here - use explicit columns', this.sql, column.expr);
414
- }
415
- return name;
416
- }
417
- /**
418
- * Get the table name from an expression.
419
- *
420
- * Only "value" tables are supported here, not parameter values.
421
- */
422
- getTableName(ref) {
423
- if (this.refHasSchema(ref)) {
424
- throw new SqlRuleError(`Specifying schema in column references is not supported`, this.sql, ref);
425
- }
426
- if (ref.table?.name == null && this.default_table != null) {
427
- return this.default_table;
428
- }
429
- if (this.value_tables.includes(ref.table?.name ?? '')) {
430
- return ref.table.name;
431
- }
432
- const error = ref.table?.name == null
433
- ? new SqlRuleError(`Table name required`, this.sql, ref)
434
- : new SqlRuleError(`Undefined table ${ref.table?.name}`, this.sql, ref);
435
- throw error;
436
- }
437
- /**
438
- * Check if an expression is a parameter_table reference.
439
- */
440
- isParameterRef(expr) {
441
- if (expr.type != 'ref') {
442
- return false;
443
- }
444
- return this.parameter_tables.includes(expr.table?.name ?? '');
445
- }
446
- /**
447
- * Check if an expression is a value_tables reference.
448
- *
449
- * This means the expression can be evaluated directly on a value row.
450
- */
451
- isTableRef(expr) {
452
- if (expr.type != 'ref') {
453
- return false;
454
- }
455
- try {
456
- this.getTableName(expr);
457
- return true;
458
- }
459
- catch {
460
- return false;
461
- }
462
- }
463
- refHasSchema(ref) {
464
- return ref.table?.schema != null;
465
- }
466
- warn(message, expr) {
467
- const error = new SqlRuleError(message, this.sql, expr);
468
- error.type = 'warning';
469
- this.errors.push(error);
470
- }
471
- checkRef(table, ref) {
472
- if (this.schema) {
473
- const type = this.schema.getType(table, ref.name);
474
- if (type.typeFlags == TYPE_NONE) {
475
- this.warn(`Column not found: ${ref.name}`, ref);
476
- }
477
- }
478
- }
479
- }
480
- function isStatic(expr) {
481
- return ['boolean', 'integer', 'null', 'numeric', 'string'].includes(expr.type);
482
- }
483
- function staticValue(expr) {
484
- if (expr.type == 'boolean') {
485
- return expr.value ? SQLITE_TRUE : SQLITE_FALSE;
486
- }
487
- if (expr.type == 'integer') {
488
- return BigInt(expr.value);
489
- }
490
- return expr.value;
491
- }
@@ -1,53 +0,0 @@
1
- import { ExpressionType } from './ExpressionType.js';
2
- import { SqliteValue } from './types.js';
3
- export declare const BASIC_OPERATORS: Set<string>;
4
- export interface FunctionParameter {
5
- name: string;
6
- optional: boolean;
7
- type: ExpressionType;
8
- }
9
- export interface SqlFunction {
10
- call: (...args: SqliteValue[]) => SqliteValue;
11
- getReturnType(args: ExpressionType[]): ExpressionType;
12
- parameters: FunctionParameter[];
13
- }
14
- export declare const SQL_FUNCTIONS_NAMED: {
15
- base64: SqlFunction;
16
- datetime: SqlFunction;
17
- hex: SqlFunction;
18
- ifnull: SqlFunction;
19
- json_array_length: SqlFunction;
20
- json_extract: SqlFunction;
21
- json_valid: SqlFunction;
22
- length: SqlFunction;
23
- lower: SqlFunction;
24
- st_asgeojson: SqlFunction;
25
- st_astext: SqlFunction;
26
- st_x: SqlFunction;
27
- st_y: SqlFunction;
28
- typeof: SqlFunction;
29
- unixepoch: SqlFunction;
30
- upper: SqlFunction;
31
- };
32
- export declare const SQL_FUNCTIONS_CALL: Record<"length" | "base64" | "hex" | "json_extract" | "datetime" | "ifnull" | "json_array_length" | "json_valid" | "lower" | "st_asgeojson" | "st_astext" | "st_x" | "st_y" | "typeof" | "unixepoch" | "upper", (...args: SqliteValue[]) => SqliteValue>;
33
- export declare const SQL_FUNCTIONS: Record<string, SqlFunction>;
34
- export declare const CAST_TYPES: Set<string>;
35
- export declare function castAsText(value: SqliteValue): null | string;
36
- export declare function castAsBlob(value: SqliteValue): Uint8Array | null;
37
- export declare function cast(value: SqliteValue, to: string): string | number | bigint | Uint8Array | null;
38
- export declare function sqliteTypeOf(arg: SqliteValue): "blob" | "null" | "integer" | "real" | "text";
39
- export declare function parseGeometry(value?: SqliteValue): any;
40
- export declare function evaluateOperator(op: string, a: SqliteValue, b: SqliteValue): SqliteValue;
41
- export declare function getOperatorReturnType(op: string, left: ExpressionType, right: ExpressionType): ExpressionType;
42
- export declare function jsonExtract(sourceValue: SqliteValue, path: SqliteValue, operator: string): SqliteValue;
43
- export interface ParseDateFlags {
44
- /**
45
- * True if output should include milliseconds
46
- */
47
- subsecond?: boolean;
48
- /**
49
- * True if input is unixepoch instead of julien days
50
- */
51
- unixepoch?: boolean;
52
- }
53
- export declare function convertToDate(dateTime: SqliteValue, flags: ParseDateFlags): Date | null;