@peerbit/indexer-sqlite3 1.0.7 → 1.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.
- package/dist/peerbit/sqlite3.min.js +542 -626
- package/dist/peerbit/sqlite3.worker.min.js +25 -19
- package/dist/src/engine.d.ts +10 -20
- package/dist/src/engine.d.ts.map +1 -1
- package/dist/src/engine.js +104 -87
- package/dist/src/engine.js.map +1 -1
- package/dist/src/schema.d.ts +21 -8
- package/dist/src/schema.d.ts.map +1 -1
- package/dist/src/schema.js +141 -62
- package/dist/src/schema.js.map +1 -1
- package/dist/src/sqlite3.browser.d.ts.map +1 -1
- package/dist/src/sqlite3.browser.js +11 -1
- package/dist/src/sqlite3.browser.js.map +1 -1
- package/dist/src/sqlite3.d.ts.map +1 -1
- package/dist/src/sqlite3.js +19 -2
- package/dist/src/sqlite3.js.map +1 -1
- package/dist/src/sqlite3.wasm.d.ts +5 -5
- package/dist/src/sqlite3.wasm.d.ts.map +1 -1
- package/dist/src/sqlite3.wasm.js +9 -3
- package/dist/src/sqlite3.wasm.js.map +1 -1
- package/dist/src/types.d.ts +5 -1
- package/dist/src/types.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/engine.ts +165 -151
- package/src/schema.ts +206 -92
- package/src/sqlite3.browser.ts +15 -2
- package/src/sqlite3.ts +22 -2
- package/src/sqlite3.wasm.ts +19 -12
- package/src/types.ts +6 -1
package/dist/src/schema.d.ts
CHANGED
|
@@ -39,7 +39,7 @@ export interface Table {
|
|
|
39
39
|
isSimpleValue: boolean;
|
|
40
40
|
}
|
|
41
41
|
export declare const getSQLTable: (ctor: AbstractType<any>, path: string[], primary: string | false, inline: boolean, addJoinField: ((fields: SQLField[], constraints: SQLConstraint[]) => void) | undefined, fromOptionalField?: boolean) => Table[];
|
|
42
|
-
export declare const getTableName: (path: string[], clazz: string | Constructor<any>) => string;
|
|
42
|
+
export declare const getTableName: (path: string[] | undefined, clazz: string | Constructor<any>) => string;
|
|
43
43
|
export declare const CHILD_TABLE_ID = "__id";
|
|
44
44
|
export declare const ARRAY_INDEX_COLUMN = "__index";
|
|
45
45
|
export declare const PARENT_TABLE_ID = "__parent_id";
|
|
@@ -48,11 +48,11 @@ export declare const getSQLFields: (tableName: string, path: string[], ctor: Con
|
|
|
48
48
|
constraints: SQLConstraint[];
|
|
49
49
|
dependencies: Table[];
|
|
50
50
|
};
|
|
51
|
-
export declare const resolveTable: <B extends boolean, R = B extends true ? Table : Table>(key: string[], tables: Map<string, Table>, clazz: string | Constructor<any>, throwOnMissing: B) => R;
|
|
51
|
+
export declare const resolveTable: <B extends boolean, R = B extends true ? Table : Table | undefined>(key: string[], tables: Map<string, Table>, clazz: string | Constructor<any>, throwOnMissing: B) => R;
|
|
52
52
|
export declare const insert: (insertFn: (values: any[], table: Table) => Promise<any> | any, obj: Record<string, any>, tables: Map<string, Table>, table: Table, fields: Field[], handleNestedCallback?: (cb: (parentId: any) => Promise<void>) => void, parentId?: any, index?: number) => Promise<void>;
|
|
53
53
|
export declare const getTablePrefixedField: (table: Table, key: string, skipPrefix?: boolean) => string;
|
|
54
54
|
export declare const getTableNameFromPrefixedField: (prefixedField: string) => string;
|
|
55
|
-
export declare const getInlineTableFieldName: (path: string[] | undefined, key?: string) => string;
|
|
55
|
+
export declare const getInlineTableFieldName: (path: string[] | string | undefined, key?: string) => string;
|
|
56
56
|
export declare const selectChildren: (childrenTable: Table) => string;
|
|
57
57
|
export declare const generateSelectQuery: (table: Table, selects: {
|
|
58
58
|
from: string;
|
|
@@ -72,15 +72,28 @@ export declare const selectAllFieldsFromTable: (table: Table, shape: types.Shape
|
|
|
72
72
|
}[];
|
|
73
73
|
join: Map<string, JoinTable>;
|
|
74
74
|
};
|
|
75
|
-
export declare const resolveInstanceFromValue: <T>(fromTablePrefixedValues: Record<string, any>, tables: Map<string, Table>, table: Table, resolveChildren: (parentId: any, table: Table) => Promise<any[]>, tablePrefixed: boolean, shape
|
|
75
|
+
export declare const resolveInstanceFromValue: <T, S extends types.Shape | undefined>(fromTablePrefixedValues: Record<string, any>, tables: Map<string, Table>, table: Table, resolveChildren: (parentId: any, table: Table) => Promise<any[]>, tablePrefixed: boolean, shape?: S) => Promise<types.ReturnTypeFromShape<T, S>>;
|
|
76
76
|
export declare const fromRowToObj: (row: any, ctor: Constructor<any>) => any;
|
|
77
|
-
export declare const convertDeleteRequestToQuery: (request: types.
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
77
|
+
export declare const convertDeleteRequestToQuery: (request: types.DeleteOptions, tables: Map<string, Table>, table: Table) => {
|
|
78
|
+
sql: string;
|
|
79
|
+
bindable: any[];
|
|
80
|
+
};
|
|
81
|
+
export declare const convertSumRequestToQuery: (request: types.SumOptions, tables: Map<string, Table>, table: Table) => {
|
|
82
|
+
sql: string;
|
|
83
|
+
bindable: any[];
|
|
84
|
+
};
|
|
85
|
+
export declare const convertCountRequestToQuery: (request: types.CountOptions | undefined, tables: Map<string, Table>, table: Table) => {
|
|
86
|
+
sql: string;
|
|
87
|
+
bindable: any[];
|
|
88
|
+
};
|
|
89
|
+
export declare const convertSearchRequestToQuery: (request: types.IterateOptions | undefined, tables: Map<string, Table>, rootTables: Table[], shape: types.Shape | undefined) => {
|
|
90
|
+
sql: string;
|
|
91
|
+
bindable: any[];
|
|
92
|
+
};
|
|
81
93
|
export declare const buildJoin: (joinBuilder: Map<string, JoinTable>, resolveAllColumns: boolean) => string;
|
|
82
94
|
export declare const convertQueryToSQLQuery: (query: types.Query, tables: Map<string, Table>, table: Table, joinBuilder: Map<string, JoinTable>, path?: string[], tableAlias?: string | undefined) => {
|
|
83
95
|
where: string;
|
|
96
|
+
bindable: any[];
|
|
84
97
|
};
|
|
85
98
|
type JoinTable = {
|
|
86
99
|
table: Table;
|
package/dist/src/schema.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../../src/schema.ts"],"names":[],"mappings":"AA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7B,MAAM,EAAE,eACF,MAAM,GAAG,SAAS,KAC5B;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,GAAG,EAAE,CAAA;CA+ElC,CAAC;AAMF,KAAK,SAAS,GAAG;IAChB,KAAK,EAAE,KAAK,CAAC;IACb,EAAE,EAAE,MAAM,CAAC;CACX,CAAC"}
|
package/dist/src/schema.js
CHANGED
|
@@ -570,11 +570,20 @@ export const getTablePrefixedField = (table, key, skipPrefix = false) => `${skip
|
|
|
570
570
|
export const getTableNameFromPrefixedField = (prefixedField) => prefixedField.split("#")[0];
|
|
571
571
|
export const getInlineTableFieldName = (path, key) => {
|
|
572
572
|
if (key) {
|
|
573
|
-
|
|
573
|
+
if (Array.isArray(path)) {
|
|
574
|
+
return path && path.length > 0 ? `${path.join("_")}__${key}` : key;
|
|
575
|
+
}
|
|
576
|
+
return path + "__" + key;
|
|
574
577
|
}
|
|
575
578
|
else {
|
|
576
579
|
// last element in the path is the key, the rest is the path
|
|
577
580
|
// join key with __ , rest with _
|
|
581
|
+
if (!Array.isArray(path)) {
|
|
582
|
+
if (!path) {
|
|
583
|
+
throw new Error("Unexpected missing path");
|
|
584
|
+
}
|
|
585
|
+
return path;
|
|
586
|
+
}
|
|
578
587
|
return path.length > 2
|
|
579
588
|
? `${path.slice(0, -1).join("_")}__${path[path.length - 1]}`
|
|
580
589
|
: path.join("__");
|
|
@@ -597,7 +606,12 @@ const matchFieldInShape = (shape, path, field) => {
|
|
|
597
606
|
if (nextShape === true) {
|
|
598
607
|
return true;
|
|
599
608
|
}
|
|
600
|
-
|
|
609
|
+
if (Array.isArray(nextShape)) {
|
|
610
|
+
currentShape = nextShape[0];
|
|
611
|
+
}
|
|
612
|
+
else {
|
|
613
|
+
currentShape = nextShape;
|
|
614
|
+
}
|
|
601
615
|
}
|
|
602
616
|
}
|
|
603
617
|
throw new Error("Unexpected");
|
|
@@ -669,7 +683,12 @@ export const selectAllFieldsFromTable = (table, shape) => {
|
|
|
669
683
|
if (!maybeShape) {
|
|
670
684
|
continue;
|
|
671
685
|
}
|
|
672
|
-
childShape =
|
|
686
|
+
childShape =
|
|
687
|
+
maybeShape === true
|
|
688
|
+
? undefined
|
|
689
|
+
: Array.isArray(maybeShape)
|
|
690
|
+
? maybeShape[0]
|
|
691
|
+
: maybeShape;
|
|
673
692
|
}
|
|
674
693
|
stack.push({ table: child, shape: childShape });
|
|
675
694
|
if (!child.inline) {
|
|
@@ -702,7 +721,18 @@ export const resolveInstanceFromValue = async (fromTablePrefixedValues, tables,
|
|
|
702
721
|
const handleNested = async (field, isOptional, isArray) => {
|
|
703
722
|
const subTables = getTableFromField(table, tables, field); // TODO fix
|
|
704
723
|
let maybeShape = shape?.[field.key];
|
|
705
|
-
|
|
724
|
+
const subshapeIsArray = Array.isArray(maybeShape);
|
|
725
|
+
if (isArray && maybeShape && !subshapeIsArray && maybeShape !== true) {
|
|
726
|
+
throw new Error("Shape is not matching the array field type: " +
|
|
727
|
+
field.key +
|
|
728
|
+
". Shape: " +
|
|
729
|
+
JSON.stringify(shape));
|
|
730
|
+
}
|
|
731
|
+
let subshape = maybeShape === true
|
|
732
|
+
? undefined
|
|
733
|
+
: subshapeIsArray
|
|
734
|
+
? maybeShape[0]
|
|
735
|
+
: maybeShape;
|
|
706
736
|
if (isArray) {
|
|
707
737
|
let once = false;
|
|
708
738
|
let resolvedArr = [];
|
|
@@ -815,13 +845,25 @@ export const fromRowToObj = (row, ctor) => {
|
|
|
815
845
|
return Object.assign(Object.create(ctor.prototype), obj);
|
|
816
846
|
};
|
|
817
847
|
export const convertDeleteRequestToQuery = (request, tables, table) => {
|
|
818
|
-
|
|
848
|
+
const { query, bindable } = convertRequestToQuery("delete", request, tables, table);
|
|
849
|
+
return {
|
|
850
|
+
sql: `DELETE FROM ${table.name} WHERE ${table.primary} IN (SELECT ${table.primary} from ${table.name} ${query}) returning ${table.primary}`,
|
|
851
|
+
bindable,
|
|
852
|
+
};
|
|
819
853
|
};
|
|
820
854
|
export const convertSumRequestToQuery = (request, tables, table) => {
|
|
821
|
-
|
|
855
|
+
const { query, bindable } = convertRequestToQuery("sum", request, tables, table);
|
|
856
|
+
return {
|
|
857
|
+
sql: `SELECT SUM(${table.name}.${getInlineTableFieldName(request.key)}) as sum FROM ${table.name} ${query}`,
|
|
858
|
+
bindable,
|
|
859
|
+
};
|
|
822
860
|
};
|
|
823
861
|
export const convertCountRequestToQuery = (request, tables, table) => {
|
|
824
|
-
|
|
862
|
+
const { query, bindable } = convertRequestToQuery("count", request, tables, table);
|
|
863
|
+
return {
|
|
864
|
+
sql: `SELECT count(*) as count FROM ${table.name} ${query}`,
|
|
865
|
+
bindable,
|
|
866
|
+
};
|
|
825
867
|
};
|
|
826
868
|
export const convertSearchRequestToQuery = (request, tables, rootTables, shape) => {
|
|
827
869
|
let unionBuilder = "";
|
|
@@ -829,11 +871,12 @@ export const convertSearchRequestToQuery = (request, tables, rootTables, shape)
|
|
|
829
871
|
let matchedOnce = false;
|
|
830
872
|
let lastError = undefined;
|
|
831
873
|
const selectsPerTable = selectAllFieldsFromTables(rootTables, shape);
|
|
874
|
+
let bindableBuilder = [];
|
|
832
875
|
for (const [i, table] of rootTables.entries()) {
|
|
833
876
|
const { selects, joins: joinFromSelect } = selectsPerTable[i];
|
|
834
877
|
const selectQuery = generateSelectQuery(table, selects);
|
|
835
878
|
try {
|
|
836
|
-
const { orderBy, query } = convertRequestToQuery(request, tables, table, joinFromSelect);
|
|
879
|
+
const { orderBy, query, bindable } = convertRequestToQuery("iterate", request, tables, table, joinFromSelect);
|
|
837
880
|
unionBuilder += `${unionBuilder.length > 0 ? " UNION ALL " : ""} ${selectQuery} ${query}`;
|
|
838
881
|
orderByClause =
|
|
839
882
|
orderBy?.length > 0
|
|
@@ -842,6 +885,7 @@ export const convertSearchRequestToQuery = (request, tables, rootTables, shape)
|
|
|
842
885
|
: orderBy
|
|
843
886
|
: orderByClause;
|
|
844
887
|
matchedOnce = true;
|
|
888
|
+
bindableBuilder.push(...bindable);
|
|
845
889
|
}
|
|
846
890
|
catch (error) {
|
|
847
891
|
if (error instanceof MissingFieldError) {
|
|
@@ -854,52 +898,66 @@ export const convertSearchRequestToQuery = (request, tables, rootTables, shape)
|
|
|
854
898
|
if (!matchedOnce) {
|
|
855
899
|
throw lastError;
|
|
856
900
|
}
|
|
857
|
-
return
|
|
901
|
+
return {
|
|
902
|
+
sql: `${unionBuilder} ${orderByClause ? "ORDER BY " + orderByClause : ""} limit ? offset ?`,
|
|
903
|
+
bindable: bindableBuilder,
|
|
904
|
+
};
|
|
858
905
|
};
|
|
859
|
-
|
|
906
|
+
function isIterateRequest(request, type) {
|
|
907
|
+
return type === "iterate";
|
|
908
|
+
}
|
|
909
|
+
const convertRequestToQuery = (type, request, tables, table, extraJoin, path = []) => {
|
|
860
910
|
let whereBuilder = "";
|
|
911
|
+
let bindableBuilder = [];
|
|
861
912
|
let orderByBuilder = undefined;
|
|
862
913
|
/* let tablesToSelect: string[] = [table.name]; */
|
|
863
914
|
let joinBuilder = extraJoin || new Map();
|
|
864
|
-
|
|
865
|
-
|
|
915
|
+
const coercedQuery = types.toQuery(request?.query);
|
|
916
|
+
if (coercedQuery.length === 1) {
|
|
917
|
+
const { where, bindable } = convertQueryToSQLQuery(coercedQuery[0], tables, table, joinBuilder, path);
|
|
866
918
|
whereBuilder += where;
|
|
919
|
+
bindableBuilder.push(...bindable);
|
|
867
920
|
}
|
|
868
|
-
else if (
|
|
869
|
-
const { where } = convertQueryToSQLQuery(new types.And(
|
|
921
|
+
else if (coercedQuery.length > 1) {
|
|
922
|
+
const { where, bindable } = convertQueryToSQLQuery(new types.And(coercedQuery), tables, table, joinBuilder, path);
|
|
870
923
|
whereBuilder += where;
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
let
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
924
|
+
bindableBuilder.push(...bindable);
|
|
925
|
+
}
|
|
926
|
+
if (isIterateRequest(request, type)) {
|
|
927
|
+
if (request?.sort) {
|
|
928
|
+
let sortArr = Array.isArray(request.sort) ? request.sort : [request.sort];
|
|
929
|
+
if (sortArr.length > 0) {
|
|
930
|
+
orderByBuilder = "";
|
|
931
|
+
let once = false;
|
|
932
|
+
for (const sort of sortArr) {
|
|
933
|
+
const { foreignTables, queryKey } = resolveTableToQuery(table, tables, joinBuilder, [...path, ...sort.key], undefined, true);
|
|
934
|
+
for (const table of foreignTables) {
|
|
935
|
+
if (once) {
|
|
936
|
+
orderByBuilder += ", ";
|
|
937
|
+
}
|
|
938
|
+
once = true;
|
|
939
|
+
orderByBuilder += `${table.as}.${queryKey} ${sort.direction === types.SortDirection.ASC ? "ASC" : "DESC"}`;
|
|
881
940
|
}
|
|
882
|
-
once = true;
|
|
883
|
-
orderByBuilder += `${table.as}.${queryKey} ${sort.direction === types.SortDirection.ASC ? "ASC" : "DESC"}`;
|
|
884
941
|
}
|
|
942
|
+
/* orderByBuilder += request.sort
|
|
943
|
+
.map(
|
|
944
|
+
(sort) =>
|
|
945
|
+
`${table.name}.${sort.key} ${sort.direction === types.SortDirection.ASC ? "ASC" : "DESC"}`
|
|
946
|
+
)
|
|
947
|
+
.join(", "); */
|
|
885
948
|
}
|
|
886
|
-
/* orderByBuilder += request.sort
|
|
887
|
-
.map(
|
|
888
|
-
(sort) =>
|
|
889
|
-
`${table.name}.${sort.key} ${sort.direction === types.SortDirection.ASC ? "ASC" : "DESC"}`
|
|
890
|
-
)
|
|
891
|
-
.join(", "); */
|
|
892
949
|
}
|
|
893
950
|
}
|
|
894
951
|
const where = whereBuilder.length > 0 ? "where " + whereBuilder : undefined;
|
|
895
952
|
if (extraJoin && extraJoin.size > 0) {
|
|
896
953
|
insertMapIntoMap(joinBuilder, extraJoin);
|
|
897
954
|
}
|
|
898
|
-
let join = buildJoin(joinBuilder,
|
|
955
|
+
let join = buildJoin(joinBuilder, type === "iterate" ? true : false);
|
|
899
956
|
const query = `${join ? join : ""} ${where ? where : ""}`;
|
|
900
957
|
return {
|
|
901
958
|
query,
|
|
902
959
|
orderBy: orderByBuilder,
|
|
960
|
+
bindable: bindableBuilder,
|
|
903
961
|
};
|
|
904
962
|
};
|
|
905
963
|
export const buildJoin = (joinBuilder, resolveAllColumns) => {
|
|
@@ -926,17 +984,20 @@ const insertMapIntoMap = (map, insert) => {
|
|
|
926
984
|
};
|
|
927
985
|
export const convertQueryToSQLQuery = (query, tables, table, joinBuilder, path = [], tableAlias = undefined) => {
|
|
928
986
|
let whereBuilder = "";
|
|
987
|
+
let bindableBuilder = [];
|
|
929
988
|
/* let tablesToSelect: string[] = []; */
|
|
930
989
|
const handleAnd = (queries, path, tableAlias) => {
|
|
931
990
|
for (const query of queries) {
|
|
932
|
-
const { where } = convertQueryToSQLQuery(query, tables, table, joinBuilder, path, tableAlias);
|
|
991
|
+
const { where, bindable } = convertQueryToSQLQuery(query, tables, table, joinBuilder, path, tableAlias);
|
|
933
992
|
whereBuilder =
|
|
934
993
|
whereBuilder.length > 0 ? `(${whereBuilder}) AND (${where})` : where;
|
|
994
|
+
bindableBuilder.push(...bindable);
|
|
935
995
|
}
|
|
936
996
|
};
|
|
937
997
|
if (query instanceof types.StateFieldQuery) {
|
|
938
|
-
const { where } = convertStateFieldQuery(query, tables, table, joinBuilder, path, tableAlias);
|
|
998
|
+
const { where, bindable } = convertStateFieldQuery(query, tables, table, joinBuilder, path, tableAlias);
|
|
939
999
|
whereBuilder += where;
|
|
1000
|
+
bindableBuilder.push(...bindable);
|
|
940
1001
|
}
|
|
941
1002
|
else if (query instanceof types.Nested) {
|
|
942
1003
|
let joinPrefix = "__" + String(tables.size);
|
|
@@ -949,14 +1010,16 @@ export const convertQueryToSQLQuery = (query, tables, table, joinBuilder, path =
|
|
|
949
1010
|
}
|
|
950
1011
|
else if (query instanceof types.Or) {
|
|
951
1012
|
for (const subquery of query.or) {
|
|
952
|
-
const { where } = convertQueryToSQLQuery(subquery, tables, table, joinBuilder, path, tableAlias);
|
|
1013
|
+
const { where, bindable } = convertQueryToSQLQuery(subquery, tables, table, joinBuilder, path, tableAlias);
|
|
953
1014
|
whereBuilder =
|
|
954
1015
|
whereBuilder.length > 0 ? `(${whereBuilder}) OR (${where})` : where;
|
|
1016
|
+
bindableBuilder.push(...bindable);
|
|
955
1017
|
}
|
|
956
1018
|
}
|
|
957
1019
|
else if (query instanceof types.Not) {
|
|
958
|
-
const { where } = convertQueryToSQLQuery(query.not, tables, table, joinBuilder, path, tableAlias);
|
|
1020
|
+
const { where, bindable } = convertQueryToSQLQuery(query.not, tables, table, joinBuilder, path, tableAlias);
|
|
959
1021
|
whereBuilder = `NOT (${where})`;
|
|
1022
|
+
bindableBuilder.push(...bindable);
|
|
960
1023
|
}
|
|
961
1024
|
else {
|
|
962
1025
|
throw new Error("Unsupported query type: " + query.constructor.name);
|
|
@@ -967,6 +1030,7 @@ export const convertQueryToSQLQuery = (query, tables, table, joinBuilder, path =
|
|
|
967
1030
|
}
|
|
968
1031
|
return {
|
|
969
1032
|
where: whereBuilder,
|
|
1033
|
+
bindable: bindableBuilder,
|
|
970
1034
|
};
|
|
971
1035
|
};
|
|
972
1036
|
const cloneQuery = (query) => {
|
|
@@ -1075,27 +1139,36 @@ const convertStateFieldQuery = (query, tables, table, join, path, tableAlias = u
|
|
|
1075
1139
|
query = cloneQuery(query);
|
|
1076
1140
|
query.key = [queryKey];
|
|
1077
1141
|
let whereBuilder = [];
|
|
1142
|
+
let bindableBuilder = [];
|
|
1078
1143
|
for (const ftable of foreignTables) {
|
|
1079
1144
|
if (ftable.table === table) {
|
|
1080
1145
|
throw new Error("Unexpected");
|
|
1081
1146
|
}
|
|
1082
|
-
const { where } = convertQueryToSQLQuery(query, tables, ftable.table, join, path, ftable.as);
|
|
1147
|
+
const { where, bindable } = convertQueryToSQLQuery(query, tables, ftable.table, join, path, ftable.as);
|
|
1083
1148
|
whereBuilder.push(where);
|
|
1149
|
+
bindableBuilder.push(bindable);
|
|
1084
1150
|
}
|
|
1085
|
-
return {
|
|
1151
|
+
return {
|
|
1152
|
+
where: whereBuilder.join(" OR "),
|
|
1153
|
+
bindable: bindableBuilder.flat(),
|
|
1154
|
+
};
|
|
1086
1155
|
}
|
|
1156
|
+
let bindable = [];
|
|
1087
1157
|
const keyWithTable = (tableAlias || table.name) + "." + escapeColumnName(inlinedName);
|
|
1088
1158
|
let where;
|
|
1089
1159
|
if (query instanceof types.StringMatch) {
|
|
1090
1160
|
let statement = "";
|
|
1091
1161
|
if (query.method === types.StringMatchMethod.contains) {
|
|
1092
|
-
statement = `${keyWithTable} LIKE
|
|
1162
|
+
statement = `${keyWithTable} LIKE ?`;
|
|
1163
|
+
bindable.push(`%${query.value}%`);
|
|
1093
1164
|
}
|
|
1094
1165
|
else if (query.method === types.StringMatchMethod.prefix) {
|
|
1095
|
-
statement = `${keyWithTable} LIKE
|
|
1166
|
+
statement = `${keyWithTable} LIKE ?`;
|
|
1167
|
+
bindable.push(`${query.value}%`);
|
|
1096
1168
|
}
|
|
1097
1169
|
else if (query.method === types.StringMatchMethod.exact) {
|
|
1098
|
-
statement = `${keyWithTable} =
|
|
1170
|
+
statement = `${keyWithTable} = ?`;
|
|
1171
|
+
bindable.push(`${query.value}`);
|
|
1099
1172
|
}
|
|
1100
1173
|
if (query.caseInsensitive) {
|
|
1101
1174
|
statement += " COLLATE NOCASE";
|
|
@@ -1104,42 +1177,48 @@ const convertStateFieldQuery = (query, tables, table, join, path, tableAlias = u
|
|
|
1104
1177
|
}
|
|
1105
1178
|
else if (query instanceof types.ByteMatchQuery) {
|
|
1106
1179
|
// compare Blob compule with f.value
|
|
1107
|
-
const statement = `${keyWithTable} =
|
|
1180
|
+
const statement = `${keyWithTable} = ?`;
|
|
1181
|
+
bindable.push(query.value);
|
|
1108
1182
|
where = statement;
|
|
1109
1183
|
}
|
|
1110
1184
|
else if (query instanceof types.IntegerCompare) {
|
|
1111
1185
|
if (tableField.type === "BLOB") {
|
|
1112
1186
|
// TODO perf
|
|
1113
|
-
where = `hex(${keyWithTable}) LIKE
|
|
1114
|
-
|
|
1115
|
-
else if (query.compare === types.Compare.Equal) {
|
|
1116
|
-
where = `${keyWithTable} = ${query.value.value}`;
|
|
1117
|
-
}
|
|
1118
|
-
else if (query.compare === types.Compare.Greater) {
|
|
1119
|
-
where = `${keyWithTable} > ${query.value.value}`;
|
|
1120
|
-
}
|
|
1121
|
-
else if (query.compare === types.Compare.Less) {
|
|
1122
|
-
where = `${keyWithTable} < ${query.value.value}`;
|
|
1123
|
-
}
|
|
1124
|
-
else if (query.compare === types.Compare.GreaterOrEqual) {
|
|
1125
|
-
where = `${keyWithTable} >= ${query.value.value}`;
|
|
1126
|
-
}
|
|
1127
|
-
else if (query.compare === types.Compare.LessOrEqual) {
|
|
1128
|
-
where = `${keyWithTable} <= ${query.value.value}`;
|
|
1187
|
+
where = `hex(${keyWithTable}) LIKE ?`;
|
|
1188
|
+
bindable.push(`%${toHexString(new Uint8Array([Number(query.value.value)]))}%`);
|
|
1129
1189
|
}
|
|
1130
1190
|
else {
|
|
1131
|
-
|
|
1191
|
+
if (query.compare === types.Compare.Equal) {
|
|
1192
|
+
where = `${keyWithTable} = ?`;
|
|
1193
|
+
}
|
|
1194
|
+
else if (query.compare === types.Compare.Greater) {
|
|
1195
|
+
where = `${keyWithTable} > ?`;
|
|
1196
|
+
}
|
|
1197
|
+
else if (query.compare === types.Compare.Less) {
|
|
1198
|
+
where = `${keyWithTable} < ?`;
|
|
1199
|
+
}
|
|
1200
|
+
else if (query.compare === types.Compare.GreaterOrEqual) {
|
|
1201
|
+
where = `${keyWithTable} >= ?`;
|
|
1202
|
+
}
|
|
1203
|
+
else if (query.compare === types.Compare.LessOrEqual) {
|
|
1204
|
+
where = `${keyWithTable} <= ?`;
|
|
1205
|
+
}
|
|
1206
|
+
else {
|
|
1207
|
+
throw new Error(`Unsupported compare type: ${query.compare}`);
|
|
1208
|
+
}
|
|
1209
|
+
bindable.push(query.value.value);
|
|
1132
1210
|
}
|
|
1133
1211
|
}
|
|
1134
1212
|
else if (query instanceof types.IsNull) {
|
|
1135
1213
|
where = `${keyWithTable} IS NULL`;
|
|
1136
1214
|
}
|
|
1137
1215
|
else if (query instanceof types.BoolQuery) {
|
|
1138
|
-
where = `${keyWithTable} =
|
|
1216
|
+
where = `${keyWithTable} = ?`;
|
|
1217
|
+
bindable.push(query.value ? 1 : 0);
|
|
1139
1218
|
}
|
|
1140
1219
|
else {
|
|
1141
1220
|
throw new Error("Unsupported query type: " + query.constructor.name);
|
|
1142
1221
|
}
|
|
1143
|
-
return { where };
|
|
1222
|
+
return { where, bindable };
|
|
1144
1223
|
};
|
|
1145
1224
|
//# sourceMappingURL=schema.js.map
|