velocious 1.0.101 → 1.0.103
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/package.json +1 -1
- package/spec/database/drivers/create-sql/create-index-sql-spec.js +24 -0
- package/src/configuration-types.js +7 -1
- package/src/configuration.js +2 -2
- package/src/database/drivers/base-column.js +2 -2
- package/src/database/drivers/base-columns-index.js +6 -0
- package/src/database/drivers/base.js +60 -22
- package/src/database/drivers/mssql/index.js +70 -22
- package/src/database/drivers/mssql/sql/create-database.js +9 -2
- package/src/database/drivers/mssql/table.js +14 -4
- package/src/database/drivers/mysql/column.js +6 -0
- package/src/database/drivers/mysql/columns-index.js +3 -6
- package/src/database/drivers/mysql/foreign-key.js +2 -0
- package/src/database/drivers/mysql/index.js +43 -16
- package/src/database/drivers/mysql/query-parser.js +2 -0
- package/src/database/drivers/mysql/query.js +7 -2
- package/src/database/drivers/mysql/table.js +6 -0
- package/src/database/drivers/pgsql/index.js +78 -11
- package/src/database/drivers/sqlite/base.js +77 -25
- package/src/database/drivers/sqlite/column.js +8 -0
- package/src/database/drivers/sqlite/sql/alter-table.js +25 -20
- package/src/database/drivers/sqlite/sql/create-index.js +2 -0
- package/src/database/drivers/sqlite/sql/create-table.js +2 -0
- package/src/database/drivers/sqlite/sql/delete.js +4 -2
- package/src/database/drivers/sqlite/sql/drop-table.js +2 -0
- package/src/database/drivers/sqlite/sql/insert.js +2 -0
- package/src/database/drivers/sqlite/sql/update.js +2 -0
- package/src/database/drivers/sqlite/table.js +14 -0
- package/src/database/migration/index.js +6 -4
- package/src/database/pool/base-methods-forward.js +2 -2
- package/src/database/query/alter-table-base.js +1 -1
- package/src/database/query/base.js +2 -2
- package/src/database/query/create-database-base.js +8 -4
- package/src/database/query/create-index-base.js +12 -7
- package/src/database/query/create-table-base.js +4 -4
- package/src/database/query/drop-table-base.js +8 -8
- package/src/database/query/index.js +31 -18
- package/src/database/query/insert-base.js +18 -3
- package/src/database/query-parser/base-query-parser.js +2 -2
- package/src/database/record/index.js +444 -172
- package/src/database/record/instance-relationships/base.js +41 -44
- package/src/database/record/instance-relationships/belongs-to.js +15 -3
- package/src/database/record/instance-relationships/has-many.js +49 -28
- package/src/database/record/instance-relationships/has-one.js +22 -7
- package/src/database/record/relationships/base.js +33 -43
- package/src/database/record/relationships/belongs-to.js +13 -3
- package/src/database/record/relationships/has-many.js +8 -2
- package/src/database/record/relationships/has-one.js +8 -2
- package/src/database/record/validators/base.js +14 -2
- package/src/database/record/validators/presence.js +7 -0
- package/src/database/table-data/table-column.js +3 -3
- package/src/environment-handlers/node.js +1 -2
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
1
3
|
import AlterTable from "./sql/alter-table.js"
|
|
2
4
|
import Base from "../base.js"
|
|
3
5
|
import CreateDatabase from "./sql/create-database.js"
|
|
@@ -23,6 +25,7 @@ export default class VelociousDatabaseDriversMysql extends Base{
|
|
|
23
25
|
this.pool.on("error", this.onPoolError)
|
|
24
26
|
}
|
|
25
27
|
|
|
28
|
+
/** @param {Error} error */
|
|
26
29
|
onPoolError = (error) => {
|
|
27
30
|
console.error("Velocious / MySQL driver / Pool error", error)
|
|
28
31
|
}
|
|
@@ -31,7 +34,7 @@ export default class VelociousDatabaseDriversMysql extends Base{
|
|
|
31
34
|
* @returns {Promise<void>}
|
|
32
35
|
*/
|
|
33
36
|
async close() {
|
|
34
|
-
await this.pool
|
|
37
|
+
await this.pool?.end()
|
|
35
38
|
this.pool = undefined
|
|
36
39
|
}
|
|
37
40
|
|
|
@@ -40,9 +43,11 @@ export default class VelociousDatabaseDriversMysql extends Base{
|
|
|
40
43
|
*/
|
|
41
44
|
connectArgs() {
|
|
42
45
|
const args = this.getArgs()
|
|
43
|
-
const connectArgs = []
|
|
44
46
|
const forward = ["database", "host", "password"]
|
|
45
47
|
|
|
48
|
+
/** @type {Record<string, any>} */
|
|
49
|
+
const connectArgs = {}
|
|
50
|
+
|
|
46
51
|
for (const forwardValue of forward) {
|
|
47
52
|
if (forwardValue in args) connectArgs[forwardValue] = digg(args, forwardValue)
|
|
48
53
|
}
|
|
@@ -53,17 +58,21 @@ export default class VelociousDatabaseDriversMysql extends Base{
|
|
|
53
58
|
}
|
|
54
59
|
|
|
55
60
|
/**
|
|
61
|
+
* @param {import("../../table-data/index.js").default} tableData
|
|
56
62
|
* @returns {Promise<string[]>}
|
|
57
63
|
*/
|
|
58
|
-
async
|
|
64
|
+
async alterTableSQLs(tableData) {
|
|
59
65
|
const alterArgs = {tableData, driver: this}
|
|
60
66
|
const alterTable = new AlterTable(alterArgs)
|
|
61
67
|
|
|
62
|
-
return await alterTable.
|
|
68
|
+
return await alterTable.toSQLs()
|
|
63
69
|
}
|
|
64
70
|
|
|
65
71
|
/**
|
|
66
|
-
* @
|
|
72
|
+
* @param {string} databaseName
|
|
73
|
+
* @param {object} [args]
|
|
74
|
+
* @param {boolean} [args.ifNotExists]
|
|
75
|
+
* @returns {string[]}
|
|
67
76
|
*/
|
|
68
77
|
createDatabaseSql(databaseName, args) {
|
|
69
78
|
const createArgs = Object.assign({databaseName, driver: this}, args)
|
|
@@ -73,16 +82,18 @@ export default class VelociousDatabaseDriversMysql extends Base{
|
|
|
73
82
|
}
|
|
74
83
|
|
|
75
84
|
/**
|
|
76
|
-
* @
|
|
85
|
+
* @param {import("../base.js").CreateIndexSqlArgs} indexData
|
|
86
|
+
* @returns {string[]}
|
|
77
87
|
*/
|
|
78
|
-
|
|
88
|
+
createIndexSQLs(indexData) {
|
|
79
89
|
const createArgs = Object.assign({driver: this}, indexData)
|
|
80
90
|
const createIndex = new CreateIndex(createArgs)
|
|
81
91
|
|
|
82
|
-
return createIndex.
|
|
92
|
+
return createIndex.toSQLs()
|
|
83
93
|
}
|
|
84
94
|
|
|
85
95
|
/**
|
|
96
|
+
* @param {import("../../table-data/index.js").default} tableData
|
|
86
97
|
* @returns {string[]}
|
|
87
98
|
*/
|
|
88
99
|
createTableSql(tableData) {
|
|
@@ -116,13 +127,15 @@ export default class VelociousDatabaseDriversMysql extends Base{
|
|
|
116
127
|
}
|
|
117
128
|
|
|
118
129
|
/**
|
|
130
|
+
* @param {string} tableName
|
|
131
|
+
* @param {import("../base.js").DropTableSqlArgsType} [args]
|
|
119
132
|
* @returns {string[]}
|
|
120
133
|
*/
|
|
121
|
-
|
|
134
|
+
dropTableSQLs(tableName, args = {}) {
|
|
122
135
|
const dropArgs = Object.assign({tableName, driver: this}, args)
|
|
123
136
|
const dropTable = new DropTable(dropArgs)
|
|
124
137
|
|
|
125
|
-
return dropTable.
|
|
138
|
+
return dropTable.toSQLs()
|
|
126
139
|
}
|
|
127
140
|
|
|
128
141
|
/**
|
|
@@ -136,18 +149,26 @@ export default class VelociousDatabaseDriversMysql extends Base{
|
|
|
136
149
|
primaryKeyType() { return "bigint" }
|
|
137
150
|
|
|
138
151
|
/**
|
|
139
|
-
* @
|
|
152
|
+
* @param {string} sql
|
|
153
|
+
* @returns {Promise<import("../base.js").QueryResultType>}
|
|
140
154
|
*/
|
|
141
155
|
async _queryActual(sql) {
|
|
156
|
+
if (!this.pool) throw new Error("Not connected to a pool yet")
|
|
157
|
+
|
|
142
158
|
try {
|
|
143
159
|
return await query(this.pool, sql)
|
|
144
160
|
} catch (error) {
|
|
145
161
|
// Re-throw to un-corrupt stacktrace
|
|
146
|
-
|
|
162
|
+
if (error instanceof Error) {
|
|
163
|
+
throw new Error(`Query failed: ${error.message}`)
|
|
164
|
+
} else {
|
|
165
|
+
throw new Error(`Query failed: ${error}`)
|
|
166
|
+
}
|
|
147
167
|
}
|
|
148
168
|
}
|
|
149
169
|
|
|
150
170
|
/**
|
|
171
|
+
* @param {import("../../query/index.js").default} query
|
|
151
172
|
* @returns {string}
|
|
152
173
|
*/
|
|
153
174
|
queryToSql(query) { return new QueryParser({query}).toSql() }
|
|
@@ -158,7 +179,8 @@ export default class VelociousDatabaseDriversMysql extends Base{
|
|
|
158
179
|
shouldSetAutoIncrementWhenPrimaryKey() { return true }
|
|
159
180
|
|
|
160
181
|
/**
|
|
161
|
-
* @
|
|
182
|
+
* @param {any} value
|
|
183
|
+
* @returns {any}
|
|
162
184
|
*/
|
|
163
185
|
escape(value) {
|
|
164
186
|
if (!this.pool) throw new Error("Can't escape before connected")
|
|
@@ -169,6 +191,7 @@ export default class VelociousDatabaseDriversMysql extends Base{
|
|
|
169
191
|
}
|
|
170
192
|
|
|
171
193
|
/**
|
|
194
|
+
* @param {string} value
|
|
172
195
|
* @returns {string}
|
|
173
196
|
*/
|
|
174
197
|
quote(value) {
|
|
@@ -178,6 +201,7 @@ export default class VelociousDatabaseDriversMysql extends Base{
|
|
|
178
201
|
}
|
|
179
202
|
|
|
180
203
|
/**
|
|
204
|
+
* @param {import("../base.js").DeleteSqlArgsType} args
|
|
181
205
|
* @returns {string}
|
|
182
206
|
*/
|
|
183
207
|
deleteSql({tableName, conditions}) {
|
|
@@ -187,6 +211,8 @@ export default class VelociousDatabaseDriversMysql extends Base{
|
|
|
187
211
|
}
|
|
188
212
|
|
|
189
213
|
/**
|
|
214
|
+
* @abstract
|
|
215
|
+
* @param {import("../base.js").InsertSqlArgsType} args
|
|
190
216
|
* @returns {string}
|
|
191
217
|
*/
|
|
192
218
|
insertSql(args) {
|
|
@@ -197,7 +223,7 @@ export default class VelociousDatabaseDriversMysql extends Base{
|
|
|
197
223
|
}
|
|
198
224
|
|
|
199
225
|
/**
|
|
200
|
-
* @returns {Array<
|
|
226
|
+
* @returns {Promise<Array<import("../base-table.js").default>>}
|
|
201
227
|
*/
|
|
202
228
|
async getTables() {
|
|
203
229
|
const result = await this.query("SHOW FULL TABLES")
|
|
@@ -213,7 +239,7 @@ export default class VelociousDatabaseDriversMysql extends Base{
|
|
|
213
239
|
}
|
|
214
240
|
|
|
215
241
|
/**
|
|
216
|
-
* @returns {number}
|
|
242
|
+
* @returns {Promise<number>}
|
|
217
243
|
*/
|
|
218
244
|
async lastInsertID() {
|
|
219
245
|
const result = await this.query("SELECT LAST_INSERT_ID() AS last_insert_id")
|
|
@@ -231,13 +257,14 @@ export default class VelociousDatabaseDriversMysql extends Base{
|
|
|
231
257
|
}
|
|
232
258
|
|
|
233
259
|
/**
|
|
234
|
-
* @returns {void}
|
|
260
|
+
* @returns {Promise<void>}
|
|
235
261
|
*/
|
|
236
262
|
async _startTransactionAction() {
|
|
237
263
|
await this.query("START TRANSACTION")
|
|
238
264
|
}
|
|
239
265
|
|
|
240
266
|
/**
|
|
267
|
+
* @param {import("../base.js").UpdateSqlArgsType} args
|
|
241
268
|
* @returns {string}
|
|
242
269
|
*/
|
|
243
270
|
updateSql({conditions, data, tableName}) {
|
|
@@ -1,12 +1,17 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @param {import("mysql").Pool} pool
|
|
3
|
+
* @param {string} sql
|
|
4
|
+
*/
|
|
5
|
+
export default async function query(pool, sql) {
|
|
2
6
|
return new Promise((resolve, reject) => {
|
|
3
|
-
|
|
7
|
+
pool.query(sql, (error, results, fields) => {
|
|
4
8
|
if (error) {
|
|
5
9
|
reject(new Error(`Query failed because of ${error}: ${sql}`))
|
|
6
10
|
} else {
|
|
7
11
|
const rows = []
|
|
8
12
|
|
|
9
13
|
for (const resultIndex in results) {
|
|
14
|
+
/** @type {Record<string, any>} */
|
|
10
15
|
const result = {}
|
|
11
16
|
|
|
12
17
|
for (const fieldKey in fields) {
|
|
@@ -1,9 +1,15 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
1
3
|
import BaseTable from "../base-table.js"
|
|
2
4
|
import Column from "./column.js"
|
|
3
5
|
import ColumnsIndex from "./columns-index.js"
|
|
4
6
|
import ForeignKey from "./foreign-key.js"
|
|
5
7
|
|
|
6
8
|
export default class VelociousDatabaseDriversMysqlTable extends BaseTable {
|
|
9
|
+
/**
|
|
10
|
+
* @param {import("../base.js").default} driver
|
|
11
|
+
* @param {Record<string, any>} data
|
|
12
|
+
*/
|
|
7
13
|
constructor(driver, data) {
|
|
8
14
|
super()
|
|
9
15
|
this.data = data
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
1
3
|
import AlterTable from "./sql/alter-table.js"
|
|
2
4
|
import Base from "../base.js"
|
|
3
5
|
import {Client} from "pg"
|
|
@@ -21,21 +23,27 @@ export default class VelociousDatabaseDriversPgsql extends Base{
|
|
|
21
23
|
await client.connect()
|
|
22
24
|
} catch (error) {
|
|
23
25
|
// Re-throw to recover real stack trace
|
|
24
|
-
|
|
26
|
+
if (error instanceof Error) {
|
|
27
|
+
throw new Error(`Connect to Postgres server failed: ${error.message}`)
|
|
28
|
+
} else {
|
|
29
|
+
throw new Error(`Connect to Postgres server failed: ${error}`)
|
|
30
|
+
}
|
|
25
31
|
}
|
|
26
32
|
|
|
27
33
|
this.connection = client
|
|
28
34
|
}
|
|
29
35
|
|
|
30
36
|
async disconnect() {
|
|
31
|
-
await this.connection
|
|
37
|
+
await this.connection?.end()
|
|
32
38
|
}
|
|
33
39
|
|
|
34
40
|
connectArgs() {
|
|
35
41
|
const args = this.getArgs()
|
|
36
|
-
const connectArgs = []
|
|
37
42
|
const forward = ["database", "host", "password", "port"]
|
|
38
43
|
|
|
44
|
+
/** @type {Record<string, any>} */
|
|
45
|
+
const connectArgs = {}
|
|
46
|
+
|
|
39
47
|
for (const forwardValue of forward) {
|
|
40
48
|
if (forwardValue in args) connectArgs[forwardValue] = digg(args, forwardValue)
|
|
41
49
|
}
|
|
@@ -46,17 +54,27 @@ export default class VelociousDatabaseDriversPgsql extends Base{
|
|
|
46
54
|
}
|
|
47
55
|
|
|
48
56
|
async close() {
|
|
49
|
-
await this.connection
|
|
57
|
+
await this.connection?.end()
|
|
50
58
|
this.connection = undefined
|
|
51
59
|
}
|
|
52
60
|
|
|
53
|
-
|
|
61
|
+
/**
|
|
62
|
+
* @param {import("../../table-data/index.js").default} tableData
|
|
63
|
+
* @returns {Promise<string[]>}
|
|
64
|
+
*/
|
|
65
|
+
async alterTableSQLs(tableData) {
|
|
54
66
|
const alterArgs = {tableData, driver: this}
|
|
55
67
|
const alterTable = new AlterTable(alterArgs)
|
|
56
68
|
|
|
57
|
-
return await alterTable.
|
|
69
|
+
return await alterTable.toSQLs()
|
|
58
70
|
}
|
|
59
71
|
|
|
72
|
+
/**
|
|
73
|
+
* @param {string} databaseName
|
|
74
|
+
* @param {object} [args]
|
|
75
|
+
* @param {boolean} [args.ifNotExists]
|
|
76
|
+
* @returns {string[]}
|
|
77
|
+
*/
|
|
60
78
|
createDatabaseSql(databaseName, args) {
|
|
61
79
|
const createArgs = Object.assign({databaseName, driver: this}, args)
|
|
62
80
|
const createDatabase = new CreateDatabase(createArgs)
|
|
@@ -64,13 +82,21 @@ export default class VelociousDatabaseDriversPgsql extends Base{
|
|
|
64
82
|
return createDatabase.toSql()
|
|
65
83
|
}
|
|
66
84
|
|
|
67
|
-
|
|
85
|
+
/**
|
|
86
|
+
* @param {import("../base.js").CreateIndexSqlArgs} indexData
|
|
87
|
+
* @returns {string[]}
|
|
88
|
+
*/
|
|
89
|
+
createIndexSQLs(indexData) {
|
|
68
90
|
const createArgs = Object.assign({driver: this}, indexData)
|
|
69
91
|
const createIndex = new CreateIndex(createArgs)
|
|
70
92
|
|
|
71
|
-
return createIndex.
|
|
93
|
+
return createIndex.toSQLs()
|
|
72
94
|
}
|
|
73
95
|
|
|
96
|
+
/**
|
|
97
|
+
* @param {import("../../table-data/index.js").default} tableData
|
|
98
|
+
* @returns {string[]}
|
|
99
|
+
*/
|
|
74
100
|
createTableSql(tableData) {
|
|
75
101
|
const createArgs = {tableData, driver: this, indexInCreateTable: false}
|
|
76
102
|
const createTable = new CreateTable(createArgs)
|
|
@@ -92,31 +118,54 @@ export default class VelociousDatabaseDriversPgsql extends Base{
|
|
|
92
118
|
await this.query("SET session_replication_role = 'origin'")
|
|
93
119
|
}
|
|
94
120
|
|
|
95
|
-
|
|
121
|
+
/**
|
|
122
|
+
* @param {string} tableName
|
|
123
|
+
* @param {import("../base.js").DropTableSqlArgsType} [args]
|
|
124
|
+
* @returns {string[]}
|
|
125
|
+
*/
|
|
126
|
+
dropTableSQLs(tableName, args = {}) {
|
|
96
127
|
const dropArgs = Object.assign({tableName, driver: this}, args)
|
|
97
128
|
const dropTable = new DropTable(dropArgs)
|
|
98
129
|
|
|
99
|
-
return dropTable.
|
|
130
|
+
return dropTable.toSQLs()
|
|
100
131
|
}
|
|
101
132
|
|
|
102
133
|
getType() { return "pgsql" }
|
|
103
134
|
primaryKeyType() { return "bigint" }
|
|
104
135
|
|
|
136
|
+
/**
|
|
137
|
+
* @param {string} sql
|
|
138
|
+
* @returns {Promise<import("../base.js").QueryResultType>}
|
|
139
|
+
*/
|
|
105
140
|
async _queryActual(sql) {
|
|
106
141
|
let response
|
|
107
142
|
|
|
143
|
+
if (!this.connection) throw new Error("Not yet connected")
|
|
144
|
+
|
|
108
145
|
try {
|
|
109
146
|
response = await this.connection.query(sql)
|
|
110
147
|
} catch (error) {
|
|
111
|
-
|
|
148
|
+
if (error instanceof Error) {
|
|
149
|
+
throw new Error(`Query failed: ${error.message} with SQL: ${sql}`)
|
|
150
|
+
} else {
|
|
151
|
+
throw new Error(`Query failed: ${error} with SQL: ${sql}`)
|
|
152
|
+
}
|
|
112
153
|
}
|
|
113
154
|
|
|
114
155
|
return response.rows
|
|
115
156
|
}
|
|
116
157
|
|
|
158
|
+
/**
|
|
159
|
+
* @param {import("../../query/index.js").default} query
|
|
160
|
+
* @returns {string}
|
|
161
|
+
*/
|
|
117
162
|
queryToSql(query) { return new QueryParser({query}).toSql() }
|
|
118
163
|
shouldSetAutoIncrementWhenPrimaryKey() { return true }
|
|
119
164
|
|
|
165
|
+
/**
|
|
166
|
+
* @param {any} value
|
|
167
|
+
* @returns {any}
|
|
168
|
+
*/
|
|
120
169
|
escape(value) {
|
|
121
170
|
if (!this.connection) throw new Error("Can't escape before connected")
|
|
122
171
|
if (typeof value === "number") return value
|
|
@@ -126,6 +175,10 @@ export default class VelociousDatabaseDriversPgsql extends Base{
|
|
|
126
175
|
return escapedValueWithQuotes.slice(1, escapedValueWithQuotes.length - 1)
|
|
127
176
|
}
|
|
128
177
|
|
|
178
|
+
/**
|
|
179
|
+
* @param {string} value
|
|
180
|
+
* @returns {string}
|
|
181
|
+
*/
|
|
129
182
|
quote(value) {
|
|
130
183
|
if (!this.connection) throw new Error("Can't escape before connected")
|
|
131
184
|
if (typeof value === "number") return value
|
|
@@ -133,12 +186,21 @@ export default class VelociousDatabaseDriversPgsql extends Base{
|
|
|
133
186
|
return this.connection.escapeLiteral(this._convertValue(value))
|
|
134
187
|
}
|
|
135
188
|
|
|
189
|
+
/**
|
|
190
|
+
* @param {import("../base.js").DeleteSqlArgsType} args
|
|
191
|
+
* @returns {string}
|
|
192
|
+
*/
|
|
136
193
|
deleteSql({tableName, conditions}) {
|
|
137
194
|
const deleteInstruction = new Delete({conditions, driver: this, tableName})
|
|
138
195
|
|
|
139
196
|
return deleteInstruction.toSql()
|
|
140
197
|
}
|
|
141
198
|
|
|
199
|
+
/**
|
|
200
|
+
* @abstract
|
|
201
|
+
* @param {import("../base.js").InsertSqlArgsType} args
|
|
202
|
+
* @returns {string}
|
|
203
|
+
*/
|
|
142
204
|
insertSql(args) {
|
|
143
205
|
const insertArgs = Object.assign({driver: this}, args)
|
|
144
206
|
const insert = new Insert(insertArgs)
|
|
@@ -175,6 +237,11 @@ export default class VelociousDatabaseDriversPgsql extends Base{
|
|
|
175
237
|
await this.query("START TRANSACTION")
|
|
176
238
|
}
|
|
177
239
|
|
|
240
|
+
/**
|
|
241
|
+
* @abstract
|
|
242
|
+
* @param {import("../base.js").UpdateSqlArgsType} args
|
|
243
|
+
* @returns {string}
|
|
244
|
+
*/
|
|
178
245
|
updateSql({conditions, data, tableName}) {
|
|
179
246
|
const update = new Update({conditions, data, driver: this, tableName})
|
|
180
247
|
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
1
3
|
import {digg} from "diggerize"
|
|
2
4
|
|
|
3
5
|
import AlterTable from "./sql/alter-table.js"
|
|
@@ -14,20 +16,33 @@ import Table from "./table.js"
|
|
|
14
16
|
import Update from "./sql/update.js"
|
|
15
17
|
|
|
16
18
|
export default class VelociousDatabaseDriversSqliteBase extends Base {
|
|
17
|
-
|
|
19
|
+
/**
|
|
20
|
+
* @param {import("../../table-data/index.js").default} tableData
|
|
21
|
+
* @returns {Promise<string[]>}
|
|
22
|
+
*/
|
|
23
|
+
async alterTableSQLs(tableData) {
|
|
18
24
|
const alterArgs = {driver: this, tableData}
|
|
19
25
|
const alterTable = new AlterTable(alterArgs)
|
|
20
26
|
|
|
21
|
-
return await alterTable.
|
|
27
|
+
return await alterTable.toSQLs()
|
|
22
28
|
}
|
|
23
29
|
|
|
24
|
-
|
|
30
|
+
/**
|
|
31
|
+
* @param {import("../base.js").CreateIndexSqlArgs} indexData
|
|
32
|
+
* @returns {string[]}
|
|
33
|
+
*/
|
|
34
|
+
createIndexSQLs(indexData) {
|
|
25
35
|
const createArgs = Object.assign({driver: this}, indexData)
|
|
26
36
|
const createIndex = new CreateIndex(createArgs)
|
|
27
37
|
|
|
28
|
-
return createIndex.
|
|
38
|
+
return createIndex.toSQLs()
|
|
29
39
|
}
|
|
30
40
|
|
|
41
|
+
/**
|
|
42
|
+
* @abstract
|
|
43
|
+
* @param {import("../../table-data/index.js").default} tableData
|
|
44
|
+
* @returns {string[]}
|
|
45
|
+
*/
|
|
31
46
|
createTableSql(tableData) {
|
|
32
47
|
const createArgs = {tableData, driver: this, indexInCreateTable: false}
|
|
33
48
|
const createTable = new CreateTable(createArgs)
|
|
@@ -47,14 +62,21 @@ export default class VelociousDatabaseDriversSqliteBase extends Base {
|
|
|
47
62
|
await this.query("PRAGMA foreign_keys = 1")
|
|
48
63
|
}
|
|
49
64
|
|
|
50
|
-
|
|
51
|
-
|
|
65
|
+
/**
|
|
66
|
+
* @param {string} tableName
|
|
67
|
+
* @param {import("../base.js").DropTableSqlArgsType} [args]
|
|
68
|
+
* @returns {string[]}
|
|
69
|
+
*/
|
|
70
|
+
dropTableSQLs(tableName, args = {}) {
|
|
71
|
+
const driver = /** @type {import("../base.js").default} */ (this)
|
|
72
|
+
const dropArgs = Object.assign({tableName, driver}, args)
|
|
52
73
|
const dropTable = new DropTable(dropArgs)
|
|
53
74
|
|
|
54
|
-
return dropTable.
|
|
75
|
+
return dropTable.toSQLs()
|
|
55
76
|
}
|
|
56
77
|
|
|
57
78
|
/**
|
|
79
|
+
* @param {import("../base.js").DeleteSqlArgsType} args
|
|
58
80
|
* @returns {string}
|
|
59
81
|
*/
|
|
60
82
|
deleteSql(args) { return new Delete(Object.assign({driver: this}, args)).toSql() }
|
|
@@ -65,18 +87,19 @@ export default class VelociousDatabaseDriversSqliteBase extends Base {
|
|
|
65
87
|
getType() { return "sqlite" }
|
|
66
88
|
|
|
67
89
|
/**
|
|
90
|
+
* @param {import("../base.js").InsertSqlArgsType} args
|
|
68
91
|
* @returns {string}
|
|
69
92
|
*/
|
|
70
93
|
insertSql(args) { return new Insert(Object.assign({driver: this}, args)).toSql() }
|
|
71
94
|
|
|
72
95
|
/**
|
|
73
|
-
* @param {string}
|
|
74
|
-
* @param {object} args
|
|
96
|
+
* @param {string} name
|
|
97
|
+
* @param {object} [args]
|
|
75
98
|
* @param {boolean} args.throwError
|
|
76
|
-
* @returns {Promise<
|
|
99
|
+
* @returns {Promise<import("../base-table.js").default | undefined>}
|
|
77
100
|
*/
|
|
78
|
-
async getTableByName(
|
|
79
|
-
const result = await this.query(`SELECT name FROM sqlite_master WHERE type = 'table' AND name = ${this.quote(
|
|
101
|
+
async getTableByName(name, args) {
|
|
102
|
+
const result = await this.query(`SELECT name FROM sqlite_master WHERE type = 'table' AND name = ${this.quote(name)} LIMIT 1`)
|
|
80
103
|
const row = result[0]
|
|
81
104
|
|
|
82
105
|
if (row) {
|
|
@@ -87,13 +110,11 @@ export default class VelociousDatabaseDriversSqliteBase extends Base {
|
|
|
87
110
|
const tables = await this.getTables()
|
|
88
111
|
const tableNames = tables.map((table) => table.getName())
|
|
89
112
|
|
|
90
|
-
throw new Error(`No table by that name: ${
|
|
113
|
+
throw new Error(`No table by that name: ${name} in ${tableNames.join(", ")}`)
|
|
91
114
|
}
|
|
92
115
|
}
|
|
93
116
|
|
|
94
|
-
/**
|
|
95
|
-
* @returns {Promise<Table[]>}
|
|
96
|
-
*/
|
|
117
|
+
/** @returns {Promise<Array<import("../base-table.js").default>>} */
|
|
97
118
|
async getTables() {
|
|
98
119
|
const result = await this.query("SELECT name FROM sqlite_master WHERE type = 'table' ORDER BY name")
|
|
99
120
|
const tables = []
|
|
@@ -107,13 +128,19 @@ export default class VelociousDatabaseDriversSqliteBase extends Base {
|
|
|
107
128
|
return tables
|
|
108
129
|
}
|
|
109
130
|
|
|
110
|
-
|
|
131
|
+
/**
|
|
132
|
+
* @param {string} tableName
|
|
133
|
+
* @param {Array<string>} columns
|
|
134
|
+
* @param {Array<Array<string>>} rows
|
|
135
|
+
* @returns {Promise<void>}
|
|
136
|
+
*/
|
|
137
|
+
async insertMultiple(tableName, columns, rows) {
|
|
111
138
|
await this.registerVersion()
|
|
112
139
|
|
|
113
140
|
if (this.supportsMultipleInsertValues()) {
|
|
114
|
-
|
|
141
|
+
await this.insertMultipleWithSingleInsert(tableName, columns, rows)
|
|
115
142
|
} else {
|
|
116
|
-
|
|
143
|
+
await this.insertMultipleWithTransaction(tableName, columns, rows)
|
|
117
144
|
}
|
|
118
145
|
}
|
|
119
146
|
|
|
@@ -140,20 +167,28 @@ export default class VelociousDatabaseDriversSqliteBase extends Base {
|
|
|
140
167
|
|
|
141
168
|
/**
|
|
142
169
|
* @param {string} tableName
|
|
143
|
-
* @param {string
|
|
144
|
-
* @param {
|
|
145
|
-
* @returns {Promise<
|
|
170
|
+
* @param {Array<string>} columns
|
|
171
|
+
* @param {Array<Array<string>>} rows
|
|
172
|
+
* @returns {Promise<void>}
|
|
146
173
|
*/
|
|
147
174
|
async insertMultipleWithSingleInsert(tableName, columns, rows) {
|
|
148
175
|
const sql = new Insert({columns, driver: this, rows, tableName}).toSql()
|
|
149
176
|
|
|
150
|
-
|
|
177
|
+
await this.query(sql)
|
|
151
178
|
}
|
|
152
179
|
|
|
180
|
+
/**
|
|
181
|
+
* @param {string} tableName
|
|
182
|
+
* @param {Array<string>} columns
|
|
183
|
+
* @param {Array<Array<string>>} rows
|
|
184
|
+
* @returns {Promise<void>}
|
|
185
|
+
*/
|
|
153
186
|
async insertMultipleWithTransaction(tableName, columns, rows) {
|
|
187
|
+
/** @type {string[]} */
|
|
154
188
|
const sqls = []
|
|
155
189
|
|
|
156
190
|
for (const row of rows) {
|
|
191
|
+
/** @type {Record<string, any>} */
|
|
157
192
|
const data = []
|
|
158
193
|
|
|
159
194
|
for (const columnIndex in columns) {
|
|
@@ -193,6 +228,7 @@ export default class VelociousDatabaseDriversSqliteBase extends Base {
|
|
|
193
228
|
primaryKeyType() { return "integer" } // Because bigint on SQLite doesn't support auto increment
|
|
194
229
|
|
|
195
230
|
/**
|
|
231
|
+
* @param {import("../../query/index.js").default} query
|
|
196
232
|
* @returns {string}
|
|
197
233
|
*/
|
|
198
234
|
queryToSql(query) { return new QueryParser({query}).toSql() }
|
|
@@ -215,6 +251,10 @@ export default class VelociousDatabaseDriversSqliteBase extends Base {
|
|
|
215
251
|
|
|
216
252
|
shouldSetAutoIncrementWhenPrimaryKey() { return false }
|
|
217
253
|
|
|
254
|
+
/**
|
|
255
|
+
* @param {any} value
|
|
256
|
+
* @returns {any}
|
|
257
|
+
*/
|
|
218
258
|
escape(value) {
|
|
219
259
|
value = this._convertValue(value)
|
|
220
260
|
|
|
@@ -222,12 +262,16 @@ export default class VelociousDatabaseDriversSqliteBase extends Base {
|
|
|
222
262
|
|
|
223
263
|
if (type != "string") value = `${value}`
|
|
224
264
|
|
|
225
|
-
const resultWithQuotes = escapeString(value)
|
|
265
|
+
const resultWithQuotes = escapeString(value, null)
|
|
226
266
|
const result = resultWithQuotes.substring(1, resultWithQuotes.length - 1)
|
|
227
267
|
|
|
228
268
|
return result
|
|
229
269
|
}
|
|
230
270
|
|
|
271
|
+
/**
|
|
272
|
+
* @param {Error} error
|
|
273
|
+
* @returns {boolean}
|
|
274
|
+
*/
|
|
231
275
|
retryableDatabaseError(error) {
|
|
232
276
|
if (error.message?.startsWith("attempt to write a readonly database")) return true
|
|
233
277
|
if (error.message?.startsWith("database is locked")) return true
|
|
@@ -236,6 +280,10 @@ export default class VelociousDatabaseDriversSqliteBase extends Base {
|
|
|
236
280
|
return false
|
|
237
281
|
}
|
|
238
282
|
|
|
283
|
+
/**
|
|
284
|
+
* @param {string} value
|
|
285
|
+
* @returns {string}
|
|
286
|
+
*/
|
|
239
287
|
quote(value) {
|
|
240
288
|
value = this._convertValue(value)
|
|
241
289
|
|
|
@@ -244,8 +292,12 @@ export default class VelociousDatabaseDriversSqliteBase extends Base {
|
|
|
244
292
|
if (type == "number") return value
|
|
245
293
|
if (type != "string") value = `${value}`
|
|
246
294
|
|
|
247
|
-
return escapeString(value)
|
|
295
|
+
return escapeString(value, null)
|
|
248
296
|
}
|
|
249
297
|
|
|
298
|
+
/**
|
|
299
|
+
* @param {import("../base.js").UpdateSqlArgsType} args
|
|
300
|
+
* @returns {string}
|
|
301
|
+
*/
|
|
250
302
|
updateSql({conditions, data, tableName}) { return new Update({conditions, data, driver: this, tableName}).toSql() }
|
|
251
303
|
}
|
|
@@ -1,7 +1,15 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
1
3
|
import {digg} from "diggerize"
|
|
2
4
|
import BaseColumn from "../base-column.js"
|
|
3
5
|
|
|
4
6
|
export default class VelociousDatabaseDriversSqliteColumn extends BaseColumn {
|
|
7
|
+
/**
|
|
8
|
+
* @param {object} args
|
|
9
|
+
* @param {Record<string, any>} args.column
|
|
10
|
+
* @param {import("../base.js").default} args.driver
|
|
11
|
+
* @param {import("../base-table.js").default} args.table
|
|
12
|
+
*/
|
|
5
13
|
constructor({column, driver, table}) {
|
|
6
14
|
super()
|
|
7
15
|
this.column = column
|