rado 0.1.52 → 0.1.53
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/CPU.20230126.092143.12244.0.001.cpuprofile +1 -0
- package/dist/Column.d.ts +54 -0
- package/dist/Column.js +63 -0
- package/dist/Cursor.d.ts +77 -0
- package/dist/Cursor.js +221 -0
- package/dist/Driver.d.ts +89 -0
- package/dist/Driver.js +262 -0
- package/dist/Expr.d.ts +149 -0
- package/dist/Expr.js +284 -0
- package/dist/Fields.d.ts +11 -0
- package/dist/Fields.js +0 -0
- package/dist/Formatter.d.ts +55 -0
- package/dist/Formatter.js +496 -0
- package/dist/Functions.d.ts +7 -0
- package/dist/Functions.js +11 -0
- package/dist/Id.d.ts +3 -0
- package/dist/Id.js +0 -0
- package/dist/Ops.d.ts +8 -0
- package/dist/Ops.js +45 -0
- package/dist/OrderBy.d.ts +9 -0
- package/dist/OrderBy.js +9 -0
- package/dist/Param.d.ts +15 -0
- package/dist/Param.js +18 -0
- package/dist/Query.d.ts +107 -0
- package/dist/Query.js +74 -0
- package/dist/Sanitizer.d.ts +4 -0
- package/dist/Sanitizer.js +0 -0
- package/dist/Schema.d.ts +23 -0
- package/dist/Schema.js +60 -0
- package/dist/Selection.d.ts +20 -0
- package/dist/Selection.js +7 -0
- package/dist/Statement.d.ts +57 -0
- package/dist/Statement.js +184 -0
- package/dist/Table.d.ts +56 -0
- package/dist/Table.js +104 -0
- package/dist/Target.d.ts +30 -0
- package/dist/Target.js +37 -0
- package/dist/Update.d.ts +4 -0
- package/dist/Update.js +0 -0
- package/dist/define/Column.d.ts +58 -58
- package/dist/define/Cursor.d.ts +85 -85
- package/dist/define/Expr.d.ts +154 -154
- package/dist/define/Fields.d.ts +13 -13
- package/dist/define/Functions.d.ts +5 -5
- package/dist/define/Id.d.ts +3 -3
- package/dist/define/Index.d.ts +17 -17
- package/dist/define/Ops.d.ts +7 -7
- package/dist/define/OrderBy.d.ts +9 -9
- package/dist/define/Param.d.ts +15 -15
- package/dist/define/Query.d.ts +109 -109
- package/dist/define/Schema.d.ts +18 -18
- package/dist/define/Selection.d.ts +20 -20
- package/dist/define/Table.d.ts +62 -62
- package/dist/define/Target.d.ts +38 -38
- package/dist/define/Update.d.ts +4 -4
- package/dist/driver/better-sqlite3.d.ts +15 -15
- package/dist/driver/bun-sqlite.d.ts +18 -18
- package/dist/driver/sql.js.d.ts +15 -15
- package/dist/driver/sqlite3.d.ts +18 -18
- package/dist/index.d.ts +21 -21
- package/dist/lib/Column.d.ts +57 -0
- package/dist/lib/Column.js +63 -0
- package/dist/lib/Cursor.d.ts +79 -0
- package/dist/lib/Cursor.js +231 -0
- package/dist/lib/Driver.d.ts +110 -110
- package/dist/lib/Expr.d.ts +154 -0
- package/dist/lib/Expr.js +284 -0
- package/dist/lib/Fields.d.ts +13 -0
- package/dist/lib/Fields.js +0 -0
- package/dist/lib/Formatter.d.ts +78 -67
- package/dist/lib/Formatter.js +13 -9
- package/dist/lib/Functions.d.ts +5 -0
- package/dist/lib/Functions.js +11 -0
- package/dist/lib/Id.d.ts +3 -0
- package/dist/lib/Id.js +0 -0
- package/dist/lib/Index.d.ts +17 -0
- package/dist/lib/Index.js +20 -0
- package/dist/lib/Ops.d.ts +8 -0
- package/dist/lib/Ops.js +45 -0
- package/dist/lib/OrderBy.d.ts +9 -0
- package/dist/lib/OrderBy.js +9 -0
- package/dist/lib/Param.d.ts +20 -0
- package/dist/lib/Param.js +27 -0
- package/dist/lib/Query.d.ts +109 -0
- package/dist/lib/Query.js +74 -0
- package/dist/lib/Sanitizer.d.ts +5 -5
- package/dist/lib/Schema.d.ts +18 -0
- package/dist/lib/Schema.js +67 -0
- package/dist/lib/Selection.d.ts +20 -0
- package/dist/lib/Selection.js +7 -0
- package/dist/lib/SqlError.d.ts +3 -3
- package/dist/lib/Statement.d.ts +33 -29
- package/dist/lib/Statement.js +10 -7
- package/dist/lib/Table.d.ts +62 -0
- package/dist/lib/Table.js +130 -0
- package/dist/lib/Target.d.ts +30 -0
- package/dist/lib/Target.js +37 -0
- package/dist/lib/Update.d.ts +4 -0
- package/dist/lib/Update.js +0 -0
- package/dist/sqlite/SqliteFormatter.d.ts +11 -11
- package/dist/sqlite/SqliteFunctions.d.ts +165 -165
- package/dist/sqlite/SqliteSchema.d.ts +25 -25
- package/dist/sqlite/index.d.ts +2 -0
- package/dist/sqlite/index.js +3 -0
- package/dist/sqlite.d.ts +2 -2
- package/dist/src/driver/better-sqlite3.js +49 -0
- package/dist/src/driver/sql.js.js +66 -0
- package/dist/src/driver/sqlite3.js +87 -0
- package/dist/src/index.js +24 -0
- package/dist/src/lib/Column.js +63 -0
- package/dist/src/lib/Cursor.js +231 -0
- package/dist/src/lib/Driver.js +316 -0
- package/dist/src/lib/Expr.js +284 -0
- package/dist/src/lib/Fields.js +0 -0
- package/dist/src/lib/Formatter.js +625 -0
- package/dist/src/lib/Functions.js +11 -0
- package/dist/src/lib/Id.js +0 -0
- package/dist/src/lib/Index.js +20 -0
- package/dist/src/lib/Ops.js +45 -0
- package/dist/src/lib/OrderBy.js +9 -0
- package/dist/src/lib/Param.js +27 -0
- package/dist/src/lib/Query.js +74 -0
- package/dist/src/lib/Sanitizer.js +0 -0
- package/dist/src/lib/Schema.js +67 -0
- package/dist/src/lib/Selection.js +7 -0
- package/dist/src/lib/Statement.js +103 -0
- package/dist/src/lib/Table.js +130 -0
- package/dist/src/lib/Target.js +37 -0
- package/dist/src/lib/Update.js +0 -0
- package/dist/src/sqlite/SqliteFormatter.js +67 -0
- package/dist/src/sqlite/SqliteFunctions.js +146 -0
- package/dist/src/sqlite/SqliteSchema.js +47 -0
- package/dist/src/sqlite.js +3 -0
- package/dist/test/DbSuite.js +28 -0
- package/dist/test/TSTestTypes.js +6 -0
- package/dist/test/TestBasic.js +172 -0
- package/dist/test/TestExtend.js +68 -0
- package/dist/test/TestFts5.js +34 -0
- package/dist/test/TestFunctions.js +28 -0
- package/dist/test/TestJoins.js +34 -0
- package/dist/test/TestJson.js +33 -0
- package/dist/test/TestPrepare.js +27 -0
- package/dist/test/TestRawSql.js +31 -0
- package/dist/test/TestSchema.js +69 -0
- package/dist/test/TestSubQueries.js +79 -0
- package/dist/test/TestTransaction.js +54 -0
- package/dist/test/TestUpdate.js +95 -0
- package/dist/test/index.js +14 -0
- package/package.json +60 -60
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// src/lib/Param.ts
|
|
2
|
+
import { Expr, ExprData } from "./Expr.js";
|
|
3
|
+
var ParamType = /* @__PURE__ */ ((ParamType2) => {
|
|
4
|
+
ParamType2["Value"] = "Value";
|
|
5
|
+
ParamType2["Named"] = "Named";
|
|
6
|
+
return ParamType2;
|
|
7
|
+
})(ParamType || {});
|
|
8
|
+
var ParamData = {
|
|
9
|
+
Value(value) {
|
|
10
|
+
return { type: "Value" /* Value */, value };
|
|
11
|
+
},
|
|
12
|
+
Named(name) {
|
|
13
|
+
return { type: "Named" /* Named */, name };
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
function createParams() {
|
|
17
|
+
return new Proxy({}, {
|
|
18
|
+
get(target, prop) {
|
|
19
|
+
return new Expr(ExprData.Param(ParamData.Named(prop)));
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
export {
|
|
24
|
+
ParamData,
|
|
25
|
+
ParamType,
|
|
26
|
+
createParams
|
|
27
|
+
};
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
// src/lib/Query.ts
|
|
2
|
+
var QueryType = /* @__PURE__ */ ((QueryType2) => {
|
|
3
|
+
QueryType2["Insert"] = "Insert";
|
|
4
|
+
QueryType2["Select"] = "Select";
|
|
5
|
+
QueryType2["Update"] = "Update";
|
|
6
|
+
QueryType2["Delete"] = "Delete";
|
|
7
|
+
QueryType2["CreateTable"] = "CreateTable";
|
|
8
|
+
QueryType2["CreateIndex"] = "CreateIndex";
|
|
9
|
+
QueryType2["DropIndex"] = "DropIndex";
|
|
10
|
+
QueryType2["AlterTable"] = "AlterTable";
|
|
11
|
+
QueryType2["Batch"] = "Batch";
|
|
12
|
+
QueryType2["Transaction"] = "Transaction";
|
|
13
|
+
QueryType2["Raw"] = "Raw";
|
|
14
|
+
return QueryType2;
|
|
15
|
+
})(QueryType || {});
|
|
16
|
+
var Query;
|
|
17
|
+
((Query2) => {
|
|
18
|
+
function Insert(insert) {
|
|
19
|
+
return { type: "Insert" /* Insert */, ...insert };
|
|
20
|
+
}
|
|
21
|
+
Query2.Insert = Insert;
|
|
22
|
+
function Select(select) {
|
|
23
|
+
return { type: "Select" /* Select */, ...select };
|
|
24
|
+
}
|
|
25
|
+
Query2.Select = Select;
|
|
26
|
+
function Update(update) {
|
|
27
|
+
return { type: "Update" /* Update */, ...update };
|
|
28
|
+
}
|
|
29
|
+
Query2.Update = Update;
|
|
30
|
+
function Delete(del) {
|
|
31
|
+
return { type: "Delete" /* Delete */, ...del };
|
|
32
|
+
}
|
|
33
|
+
Query2.Delete = Delete;
|
|
34
|
+
function CreateTable(create) {
|
|
35
|
+
return { type: "CreateTable" /* CreateTable */, ...create };
|
|
36
|
+
}
|
|
37
|
+
Query2.CreateTable = CreateTable;
|
|
38
|
+
function CreateIndex(create) {
|
|
39
|
+
return { type: "CreateIndex" /* CreateIndex */, ...create };
|
|
40
|
+
}
|
|
41
|
+
Query2.CreateIndex = CreateIndex;
|
|
42
|
+
function DropIndex(drop) {
|
|
43
|
+
return { type: "DropIndex" /* DropIndex */, ...drop };
|
|
44
|
+
}
|
|
45
|
+
Query2.DropIndex = DropIndex;
|
|
46
|
+
function Batch(batch) {
|
|
47
|
+
return { type: "Batch" /* Batch */, ...batch };
|
|
48
|
+
}
|
|
49
|
+
Query2.Batch = Batch;
|
|
50
|
+
let TransactionOperation;
|
|
51
|
+
((TransactionOperation2) => {
|
|
52
|
+
TransactionOperation2["Begin"] = "Begin";
|
|
53
|
+
TransactionOperation2["Commit"] = "Commit";
|
|
54
|
+
TransactionOperation2["Rollback"] = "Rollback";
|
|
55
|
+
})(TransactionOperation = Query2.TransactionOperation || (Query2.TransactionOperation = {}));
|
|
56
|
+
function Transaction(transaction) {
|
|
57
|
+
return { type: "Transaction" /* Transaction */, ...transaction };
|
|
58
|
+
}
|
|
59
|
+
Query2.Transaction = Transaction;
|
|
60
|
+
function Raw(raw) {
|
|
61
|
+
if (raw.strings.some((chunk) => chunk.includes("?")))
|
|
62
|
+
throw new TypeError("SQL injection hazard");
|
|
63
|
+
return { type: "Raw" /* Raw */, ...raw };
|
|
64
|
+
}
|
|
65
|
+
Query2.Raw = Raw;
|
|
66
|
+
function AlterTable(alter) {
|
|
67
|
+
return { type: "AlterTable" /* AlterTable */, ...alter };
|
|
68
|
+
}
|
|
69
|
+
Query2.AlterTable = AlterTable;
|
|
70
|
+
})(Query || (Query = {}));
|
|
71
|
+
export {
|
|
72
|
+
Query,
|
|
73
|
+
QueryType
|
|
74
|
+
};
|
|
File without changes
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
// src/lib/Schema.ts
|
|
2
|
+
import { Query } from "./Query.js";
|
|
3
|
+
import { Statement } from "./Statement.js";
|
|
4
|
+
var Schema;
|
|
5
|
+
((Schema2) => {
|
|
6
|
+
function create(schema) {
|
|
7
|
+
const queries = [];
|
|
8
|
+
queries.push(Query.CreateTable({ table: schema, ifNotExists: true }));
|
|
9
|
+
for (const index of Object.values(schema.indexes))
|
|
10
|
+
queries.push(Query.CreateIndex({ table: schema, index, ifNotExists: true }));
|
|
11
|
+
return Query.Batch({ queries });
|
|
12
|
+
}
|
|
13
|
+
Schema2.create = create;
|
|
14
|
+
function removeLeadingWhitespace(str) {
|
|
15
|
+
return str.replace(/\n\s+/g, "\n");
|
|
16
|
+
}
|
|
17
|
+
function upgrade(formatter, local, schema) {
|
|
18
|
+
const columnNames = /* @__PURE__ */ new Set([
|
|
19
|
+
...Object.keys(local.columns),
|
|
20
|
+
...Object.keys(schema.columns)
|
|
21
|
+
]);
|
|
22
|
+
const res = [];
|
|
23
|
+
for (const columnName of columnNames) {
|
|
24
|
+
const localInstruction = local.columns[columnName];
|
|
25
|
+
const schemaCol = schema.columns[columnName];
|
|
26
|
+
if (!localInstruction) {
|
|
27
|
+
res.push(Query.AlterTable({ table: schema, addColumn: schemaCol }));
|
|
28
|
+
} else if (!schemaCol) {
|
|
29
|
+
res.push(Query.AlterTable({ table: schema, dropColumn: columnName }));
|
|
30
|
+
} else {
|
|
31
|
+
const { sql: instruction } = formatter.formatColumn(
|
|
32
|
+
{ stmt: new Statement(formatter) },
|
|
33
|
+
{ ...schemaCol, references: void 0 }
|
|
34
|
+
);
|
|
35
|
+
if (removeLeadingWhitespace(localInstruction) !== removeLeadingWhitespace(instruction)) {
|
|
36
|
+
res.push(Query.AlterTable({ table: schema, alterColumn: schemaCol }));
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
const indexNames = /* @__PURE__ */ new Set([
|
|
41
|
+
...Object.keys(local.indexes),
|
|
42
|
+
...Object.keys(schema.indexes)
|
|
43
|
+
]);
|
|
44
|
+
for (const indexName of indexNames) {
|
|
45
|
+
const localInstruction = local.indexes[indexName];
|
|
46
|
+
const schemaIndex = schema.indexes[indexName];
|
|
47
|
+
if (!localInstruction) {
|
|
48
|
+
res.push(Query.CreateIndex({ table: schema, index: schemaIndex }));
|
|
49
|
+
} else if (!schemaIndex) {
|
|
50
|
+
res.unshift(Query.DropIndex({ table: schema, name: indexName }));
|
|
51
|
+
} else {
|
|
52
|
+
const { sql: instruction } = formatter.compile(
|
|
53
|
+
Query.CreateIndex({ table: schema, index: schemaIndex })
|
|
54
|
+
);
|
|
55
|
+
if (removeLeadingWhitespace(localInstruction) !== removeLeadingWhitespace(instruction)) {
|
|
56
|
+
res.unshift(Query.DropIndex({ table: schema, name: indexName }));
|
|
57
|
+
res.push(Query.CreateIndex({ table: schema, index: schemaIndex }));
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return res;
|
|
62
|
+
}
|
|
63
|
+
Schema2.upgrade = upgrade;
|
|
64
|
+
})(Schema || (Schema = {}));
|
|
65
|
+
export {
|
|
66
|
+
Schema
|
|
67
|
+
};
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
// src/lib/Statement.ts
|
|
2
|
+
import { ParamData, ParamType } from "./Param.js";
|
|
3
|
+
var SEPARATE = ", ";
|
|
4
|
+
var WHITESPACE = " ";
|
|
5
|
+
var NEWLINE = "\n";
|
|
6
|
+
var INSERT_PARAM = "?";
|
|
7
|
+
var Statement = class {
|
|
8
|
+
constructor(sanitizer, sql = "", paramData = []) {
|
|
9
|
+
this.sanitizer = sanitizer;
|
|
10
|
+
this.sql = sql;
|
|
11
|
+
this.paramData = paramData;
|
|
12
|
+
}
|
|
13
|
+
currentIndent = "";
|
|
14
|
+
space() {
|
|
15
|
+
if (this.sql === "")
|
|
16
|
+
return this;
|
|
17
|
+
return this.raw(WHITESPACE);
|
|
18
|
+
}
|
|
19
|
+
add(addition) {
|
|
20
|
+
if (!addition)
|
|
21
|
+
return this;
|
|
22
|
+
return this.space().raw(addition);
|
|
23
|
+
}
|
|
24
|
+
addLine(addition) {
|
|
25
|
+
if (!addition)
|
|
26
|
+
return this;
|
|
27
|
+
return this.newline().add(addition);
|
|
28
|
+
}
|
|
29
|
+
indent() {
|
|
30
|
+
this.currentIndent = this.currentIndent + " ";
|
|
31
|
+
return this;
|
|
32
|
+
}
|
|
33
|
+
dedent() {
|
|
34
|
+
this.currentIndent = this.currentIndent.slice(0, -2);
|
|
35
|
+
return this;
|
|
36
|
+
}
|
|
37
|
+
newline() {
|
|
38
|
+
return this.raw(NEWLINE + this.currentIndent);
|
|
39
|
+
}
|
|
40
|
+
identifier(name) {
|
|
41
|
+
return this.raw(this.sanitizer.escapeIdentifier(name));
|
|
42
|
+
}
|
|
43
|
+
addIdentifier(name) {
|
|
44
|
+
return this.space().identifier(name);
|
|
45
|
+
}
|
|
46
|
+
value(value) {
|
|
47
|
+
this.paramData.push(ParamData.Value(value));
|
|
48
|
+
return this.raw(INSERT_PARAM);
|
|
49
|
+
}
|
|
50
|
+
addValue(value) {
|
|
51
|
+
return this.space().value(value);
|
|
52
|
+
}
|
|
53
|
+
param(name) {
|
|
54
|
+
this.paramData.push(ParamData.Named(name));
|
|
55
|
+
return this.raw(INSERT_PARAM);
|
|
56
|
+
}
|
|
57
|
+
addParam(name) {
|
|
58
|
+
return this.space().param(name);
|
|
59
|
+
}
|
|
60
|
+
raw(query) {
|
|
61
|
+
if (!query)
|
|
62
|
+
return this;
|
|
63
|
+
this.sql += query;
|
|
64
|
+
return this;
|
|
65
|
+
}
|
|
66
|
+
openParenthesis() {
|
|
67
|
+
return this.raw("(").indent().newline();
|
|
68
|
+
}
|
|
69
|
+
closeParenthesis() {
|
|
70
|
+
return this.dedent().newline().raw(")");
|
|
71
|
+
}
|
|
72
|
+
*call(parts, separator = SEPARATE) {
|
|
73
|
+
this.openParenthesis();
|
|
74
|
+
yield* this.separate(parts, separator);
|
|
75
|
+
this.closeParenthesis();
|
|
76
|
+
}
|
|
77
|
+
*separate(parts, separator = SEPARATE) {
|
|
78
|
+
for (let i = 0; i < parts.length; i++) {
|
|
79
|
+
if (i > 0)
|
|
80
|
+
this.raw(separator).newline();
|
|
81
|
+
yield parts[i];
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
isEmpty() {
|
|
85
|
+
return this.sql === "";
|
|
86
|
+
}
|
|
87
|
+
params(input) {
|
|
88
|
+
return this.paramData.map((param) => {
|
|
89
|
+
if (param.type === ParamType.Named) {
|
|
90
|
+
if (input && param.name in input)
|
|
91
|
+
return this.sanitizer.formatParamValue(input[param.name]);
|
|
92
|
+
throw new TypeError(`Missing parameter ${param.name}`);
|
|
93
|
+
}
|
|
94
|
+
return this.sanitizer.formatParamValue(param.value);
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
toString() {
|
|
98
|
+
return this.sql;
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
export {
|
|
102
|
+
Statement
|
|
103
|
+
};
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
// src/lib/Table.ts
|
|
2
|
+
import { Cursor } from "./Cursor.js";
|
|
3
|
+
import { Expr, ExprData } from "./Expr.js";
|
|
4
|
+
import { Query } from "./Query.js";
|
|
5
|
+
import { Selection } from "./Selection.js";
|
|
6
|
+
import { Target } from "./Target.js";
|
|
7
|
+
var Table = class extends Cursor.SelectMultiple {
|
|
8
|
+
[Selection.__tableType]() {
|
|
9
|
+
throw "assert";
|
|
10
|
+
}
|
|
11
|
+
constructor(schema) {
|
|
12
|
+
super(
|
|
13
|
+
Query.Select({
|
|
14
|
+
from: Target.Table(schema),
|
|
15
|
+
selection: ExprData.Row(Target.Table(schema))
|
|
16
|
+
})
|
|
17
|
+
);
|
|
18
|
+
Object.defineProperty(this, "schema", {
|
|
19
|
+
enumerable: false,
|
|
20
|
+
value: () => schema
|
|
21
|
+
});
|
|
22
|
+
if (schema.columns)
|
|
23
|
+
for (const column of Object.keys(schema.columns)) {
|
|
24
|
+
Object.defineProperty(this, column, {
|
|
25
|
+
enumerable: true,
|
|
26
|
+
get: () => this.get(column)
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
return new Proxy(this, {
|
|
30
|
+
get(target, key) {
|
|
31
|
+
return key in target ? target[key] : target.get(key);
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
fetch(id) {
|
|
36
|
+
return this.where(this.get("id").is(id)).first();
|
|
37
|
+
}
|
|
38
|
+
insertOne(record) {
|
|
39
|
+
return new Cursor(
|
|
40
|
+
Query.Insert({
|
|
41
|
+
into: this.schema(),
|
|
42
|
+
data: [record],
|
|
43
|
+
selection: ExprData.Row(Target.Table(this.schema())),
|
|
44
|
+
singleResult: true
|
|
45
|
+
})
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
insertAll(data) {
|
|
49
|
+
return new Cursor.Insert(this.schema()).values(...data);
|
|
50
|
+
}
|
|
51
|
+
set(data) {
|
|
52
|
+
return new Cursor.Update(
|
|
53
|
+
Query.Update({
|
|
54
|
+
table: this.schema()
|
|
55
|
+
})
|
|
56
|
+
).set(data);
|
|
57
|
+
}
|
|
58
|
+
delete() {
|
|
59
|
+
return new Cursor.Delete(Query.Delete({ table: this.schema() }));
|
|
60
|
+
}
|
|
61
|
+
createTable() {
|
|
62
|
+
return new Cursor.Create(this.schema());
|
|
63
|
+
}
|
|
64
|
+
as(alias) {
|
|
65
|
+
return new Table({ ...this.schema(), alias });
|
|
66
|
+
}
|
|
67
|
+
alias() {
|
|
68
|
+
return new Proxy(
|
|
69
|
+
{},
|
|
70
|
+
{
|
|
71
|
+
get: (_, key) => {
|
|
72
|
+
return this.as(key);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
get(name) {
|
|
78
|
+
return new Expr(ExprData.Field(this.toExpr().expr, name));
|
|
79
|
+
}
|
|
80
|
+
toExpr() {
|
|
81
|
+
return new Expr(
|
|
82
|
+
ExprData.Row(Target.Table(this.schema()))
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
schema() {
|
|
86
|
+
throw new Error("Not implemented");
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
function table(options) {
|
|
90
|
+
const schema = {
|
|
91
|
+
...options,
|
|
92
|
+
columns: Object.fromEntries(
|
|
93
|
+
Object.entries(options.columns).map(([key, column]) => {
|
|
94
|
+
const { data } = column;
|
|
95
|
+
return [key, { ...data, name: data.name || key }];
|
|
96
|
+
})
|
|
97
|
+
),
|
|
98
|
+
indexes: {}
|
|
99
|
+
};
|
|
100
|
+
const indexes = Object.fromEntries(
|
|
101
|
+
Object.entries(
|
|
102
|
+
options.indexes ? options.indexes.call(
|
|
103
|
+
new Expr(ExprData.Row(Target.Table(schema)))
|
|
104
|
+
) : {}
|
|
105
|
+
).map(([key, index]) => {
|
|
106
|
+
const indexName = `${schema.name}.${key}`;
|
|
107
|
+
return [indexName, { name: indexName, ...index.data }];
|
|
108
|
+
})
|
|
109
|
+
);
|
|
110
|
+
return new Table({
|
|
111
|
+
...schema,
|
|
112
|
+
indexes
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
((table2) => {
|
|
116
|
+
function extend(target, extensions) {
|
|
117
|
+
return new Proxy(target, {
|
|
118
|
+
get(target2, key) {
|
|
119
|
+
if (key === "as")
|
|
120
|
+
return (...args) => extend(target2.as(...args), extensions);
|
|
121
|
+
return key in extensions ? extensions[key].bind(target2) : target2[key];
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
table2.extend = extend;
|
|
126
|
+
})(table || (table = {}));
|
|
127
|
+
export {
|
|
128
|
+
Table,
|
|
129
|
+
table
|
|
130
|
+
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// src/lib/Target.ts
|
|
2
|
+
var TargetType = /* @__PURE__ */ ((TargetType2) => {
|
|
3
|
+
TargetType2["Each"] = "Each";
|
|
4
|
+
TargetType2["Table"] = "Table";
|
|
5
|
+
TargetType2["Join"] = "Join";
|
|
6
|
+
return TargetType2;
|
|
7
|
+
})(TargetType || {});
|
|
8
|
+
var Target;
|
|
9
|
+
((Target2) => {
|
|
10
|
+
function Each(expr, alias) {
|
|
11
|
+
return { type: "Each" /* Each */, expr, alias };
|
|
12
|
+
}
|
|
13
|
+
Target2.Each = Each;
|
|
14
|
+
function Table(table) {
|
|
15
|
+
return { type: "Table" /* Table */, table };
|
|
16
|
+
}
|
|
17
|
+
Target2.Table = Table;
|
|
18
|
+
function Join(left, right, joinType, on) {
|
|
19
|
+
return { type: "Join" /* Join */, left, right, joinType, on };
|
|
20
|
+
}
|
|
21
|
+
Target2.Join = Join;
|
|
22
|
+
function source(from) {
|
|
23
|
+
switch (from.type) {
|
|
24
|
+
case "Table" /* Table */:
|
|
25
|
+
return from.table;
|
|
26
|
+
case "Join" /* Join */:
|
|
27
|
+
return source(from.left);
|
|
28
|
+
default:
|
|
29
|
+
return void 0;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
Target2.source = source;
|
|
33
|
+
})(Target || (Target = {}));
|
|
34
|
+
export {
|
|
35
|
+
Target,
|
|
36
|
+
TargetType
|
|
37
|
+
};
|
|
File without changes
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
// src/sqlite/SqliteFormatter.ts
|
|
2
|
+
import { ExprType } from "../lib/Expr.js";
|
|
3
|
+
import { Formatter } from "../lib/Formatter.js";
|
|
4
|
+
import { TargetType } from "../lib/Target.js";
|
|
5
|
+
var BACKTICK = "`";
|
|
6
|
+
var ESCAPE_BACKTICK = "``";
|
|
7
|
+
var SINGLE_QUOTE = "'";
|
|
8
|
+
var ESCAPE_SINGLE_QUOTE = "''";
|
|
9
|
+
var MATCH_BACKTICK = /`/g;
|
|
10
|
+
var MATCH_SINGLE_QUOTE = /'/g;
|
|
11
|
+
var SqliteFormatter = class extends Formatter {
|
|
12
|
+
formatParamValue(paramValue) {
|
|
13
|
+
if (paramValue === null || paramValue === void 0)
|
|
14
|
+
return null;
|
|
15
|
+
if (typeof paramValue === "boolean")
|
|
16
|
+
return paramValue ? 1 : 0;
|
|
17
|
+
if (typeof paramValue === "number")
|
|
18
|
+
return paramValue;
|
|
19
|
+
if (typeof paramValue === "string")
|
|
20
|
+
return paramValue;
|
|
21
|
+
return JSON.stringify(paramValue);
|
|
22
|
+
}
|
|
23
|
+
escapeValue(value) {
|
|
24
|
+
if (value === null || value === void 0)
|
|
25
|
+
return "NULL";
|
|
26
|
+
if (typeof value === "boolean")
|
|
27
|
+
return value ? "1" : "0";
|
|
28
|
+
if (typeof value === "number")
|
|
29
|
+
return String(value);
|
|
30
|
+
if (typeof value === "string")
|
|
31
|
+
return this.escapeString(value);
|
|
32
|
+
return "json(" + this.escapeString(JSON.stringify(value)) + ")";
|
|
33
|
+
}
|
|
34
|
+
escapeIdentifier(input) {
|
|
35
|
+
return BACKTICK + input.replace(MATCH_BACKTICK, ESCAPE_BACKTICK) + BACKTICK;
|
|
36
|
+
}
|
|
37
|
+
escapeString(input) {
|
|
38
|
+
return SINGLE_QUOTE + input.replace(MATCH_SINGLE_QUOTE, ESCAPE_SINGLE_QUOTE) + SINGLE_QUOTE;
|
|
39
|
+
}
|
|
40
|
+
formatAccess(ctx, mkSubject, field) {
|
|
41
|
+
const { stmt, formatAsJson } = ctx;
|
|
42
|
+
mkSubject();
|
|
43
|
+
stmt.raw(formatAsJson ? "->" : "->>");
|
|
44
|
+
return this.formatString(ctx, `$.${field}`);
|
|
45
|
+
}
|
|
46
|
+
formatExpr(ctx, expr) {
|
|
47
|
+
const { stmt } = ctx;
|
|
48
|
+
switch (expr.type) {
|
|
49
|
+
case ExprType.Call:
|
|
50
|
+
if (expr.method === "match") {
|
|
51
|
+
const [from, query] = expr.params;
|
|
52
|
+
if (from.type !== ExprType.Row)
|
|
53
|
+
throw new Error("not implemented");
|
|
54
|
+
if (from.target.type !== TargetType.Table)
|
|
55
|
+
throw new Error("not implemented");
|
|
56
|
+
stmt.identifier(from.target.table.alias || from.target.table.name).raw(" MATCH ");
|
|
57
|
+
this.formatExprValue(ctx, query);
|
|
58
|
+
return stmt;
|
|
59
|
+
}
|
|
60
|
+
default:
|
|
61
|
+
return super.formatExpr(ctx, expr);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
export {
|
|
66
|
+
SqliteFormatter
|
|
67
|
+
};
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
// src/sqlite/SqliteFunctions.ts
|
|
2
|
+
import { Functions } from "../lib/Functions.js";
|
|
3
|
+
var SqliteFunctions = Functions;
|
|
4
|
+
var {
|
|
5
|
+
count,
|
|
6
|
+
iif,
|
|
7
|
+
exists,
|
|
8
|
+
match,
|
|
9
|
+
cast,
|
|
10
|
+
abs,
|
|
11
|
+
changes,
|
|
12
|
+
char,
|
|
13
|
+
coalesce,
|
|
14
|
+
ifnull,
|
|
15
|
+
instr,
|
|
16
|
+
last_insert_rowid,
|
|
17
|
+
length,
|
|
18
|
+
likelihood,
|
|
19
|
+
likely,
|
|
20
|
+
lower,
|
|
21
|
+
ltrim,
|
|
22
|
+
max,
|
|
23
|
+
min,
|
|
24
|
+
nullif,
|
|
25
|
+
prnumberf,
|
|
26
|
+
quote,
|
|
27
|
+
random,
|
|
28
|
+
replace,
|
|
29
|
+
round,
|
|
30
|
+
rtrim,
|
|
31
|
+
sign,
|
|
32
|
+
soundex,
|
|
33
|
+
sqlite_version,
|
|
34
|
+
substr,
|
|
35
|
+
total_changes,
|
|
36
|
+
trim,
|
|
37
|
+
unicode,
|
|
38
|
+
unlikely,
|
|
39
|
+
upper,
|
|
40
|
+
avg,
|
|
41
|
+
group_concat,
|
|
42
|
+
sum,
|
|
43
|
+
acos,
|
|
44
|
+
acosh,
|
|
45
|
+
asin,
|
|
46
|
+
asinh,
|
|
47
|
+
atan,
|
|
48
|
+
atan2,
|
|
49
|
+
atanh,
|
|
50
|
+
ceil,
|
|
51
|
+
cos,
|
|
52
|
+
cosh,
|
|
53
|
+
degrees,
|
|
54
|
+
exp,
|
|
55
|
+
floor,
|
|
56
|
+
ln,
|
|
57
|
+
log,
|
|
58
|
+
log2,
|
|
59
|
+
mod,
|
|
60
|
+
pi,
|
|
61
|
+
pow,
|
|
62
|
+
radians,
|
|
63
|
+
sin,
|
|
64
|
+
sinh,
|
|
65
|
+
sqrt,
|
|
66
|
+
tan,
|
|
67
|
+
tanh,
|
|
68
|
+
trunc,
|
|
69
|
+
date,
|
|
70
|
+
time,
|
|
71
|
+
datetime,
|
|
72
|
+
julianday,
|
|
73
|
+
strftime
|
|
74
|
+
} = SqliteFunctions;
|
|
75
|
+
export {
|
|
76
|
+
SqliteFunctions,
|
|
77
|
+
abs,
|
|
78
|
+
acos,
|
|
79
|
+
acosh,
|
|
80
|
+
asin,
|
|
81
|
+
asinh,
|
|
82
|
+
atan,
|
|
83
|
+
atan2,
|
|
84
|
+
atanh,
|
|
85
|
+
avg,
|
|
86
|
+
cast,
|
|
87
|
+
ceil,
|
|
88
|
+
changes,
|
|
89
|
+
char,
|
|
90
|
+
coalesce,
|
|
91
|
+
cos,
|
|
92
|
+
cosh,
|
|
93
|
+
count,
|
|
94
|
+
date,
|
|
95
|
+
datetime,
|
|
96
|
+
degrees,
|
|
97
|
+
exists,
|
|
98
|
+
exp,
|
|
99
|
+
floor,
|
|
100
|
+
group_concat,
|
|
101
|
+
ifnull,
|
|
102
|
+
iif,
|
|
103
|
+
instr,
|
|
104
|
+
julianday,
|
|
105
|
+
last_insert_rowid,
|
|
106
|
+
length,
|
|
107
|
+
likelihood,
|
|
108
|
+
likely,
|
|
109
|
+
ln,
|
|
110
|
+
log,
|
|
111
|
+
log2,
|
|
112
|
+
lower,
|
|
113
|
+
ltrim,
|
|
114
|
+
match,
|
|
115
|
+
max,
|
|
116
|
+
min,
|
|
117
|
+
mod,
|
|
118
|
+
nullif,
|
|
119
|
+
pi,
|
|
120
|
+
pow,
|
|
121
|
+
prnumberf,
|
|
122
|
+
quote,
|
|
123
|
+
radians,
|
|
124
|
+
random,
|
|
125
|
+
replace,
|
|
126
|
+
round,
|
|
127
|
+
rtrim,
|
|
128
|
+
sign,
|
|
129
|
+
sin,
|
|
130
|
+
sinh,
|
|
131
|
+
soundex,
|
|
132
|
+
sqlite_version,
|
|
133
|
+
sqrt,
|
|
134
|
+
strftime,
|
|
135
|
+
substr,
|
|
136
|
+
sum,
|
|
137
|
+
tan,
|
|
138
|
+
tanh,
|
|
139
|
+
time,
|
|
140
|
+
total_changes,
|
|
141
|
+
trim,
|
|
142
|
+
trunc,
|
|
143
|
+
unicode,
|
|
144
|
+
unlikely,
|
|
145
|
+
upper
|
|
146
|
+
};
|