Skip to content

Commit b671c24

Browse files
committed
fix(dialect): fix and support returning and raw
1 parent f54061e commit b671c24

File tree

18 files changed

+90
-131
lines changed

18 files changed

+90
-131
lines changed

packages/dialect-bun-worker/src/worker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ function run({ isSelect, sql, parameters }: RunMsg): QueryResult<any> {
88
const stmt = db[cache ? 'query' : 'prepare'](sql)
99
const rows = stmt.all(parameters as any)
1010

11-
if (isSelect) {
11+
if (isSelect || rows.length) {
1212
return { rows }
1313
}
1414
return {

packages/dialect-tauri/README.md

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,4 @@ export interface TauriSqlDialectConfig<T extends 'sqlite' | 'mysql' | 'postgres'
3131
type: T
3232
onCreateConnection?: (connection: DatabaseConnection) => Promisable<void>
3333
}
34-
```
35-
36-
### known issue
37-
38-
- no RETURNING support
39-
- only return rows when executing raw sql
34+
```

packages/dialect-tauri/src/driver.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -83,18 +83,15 @@ class TauriSqlConnection implements DatabaseConnection {
8383
}
8484

8585
async executeQuery<R>({ parameters, query, sql }: CompiledQuery<unknown>): Promise<QueryResult<R>> {
86-
switch (query.kind) {
87-
case 'SelectQueryNode':
88-
case 'RawNode':
89-
return { rows: await this.db.select<any>(sql, parameters) }
90-
default: {
91-
const { lastInsertId, rowsAffected } = await this.db.execute(sql, parameters)
92-
return {
93-
rows: [],
94-
insertId: BigInt(lastInsertId),
95-
numAffectedRows: BigInt(rowsAffected),
96-
}
97-
}
86+
const rows = await this.db.select<any[]>(sql, parameters)
87+
if (query.kind === 'SelectQueryNode' || rows.length) {
88+
return { rows }
89+
}
90+
const { lastInsertId, rowsAffected } = await this.db.execute('select 1')
91+
return {
92+
rows,
93+
insertId: BigInt(lastInsertId),
94+
numAffectedRows: BigInt(rowsAffected),
9895
}
9996
}
10097
}

packages/dialect-tauri/src/index.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,6 @@ export class TauriSqlDialect<T extends 'sqlite' | 'mysql' | 'postgres'> {
4141
* dialect for Tauri,
4242
* using [official sql plugin](https://github.com/tauri-apps/plugins-workspace/tree/dev/plugins/sql),
4343
* support MySQL, PostgreSQL and SQLite
44-
*
45-
* - no `RETURNING` support
46-
* - only return rows when executing raw sql
4744
*/
4845
constructor(config: TauriSqlDialectConfig<T>) {
4946
this.#config = {

packages/dialect-wasm/README.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ you can **get total buffer** on every sql execution except `select` and **no bac
3131

3232
#### `OfficialWasmDialect`: performance
3333

34-
you can choose to use [OPFS](https://developer.mozilla.org/en-US/docs/Web/API/File_System_Access_API#origin_private_file_system) as backend storage(your server must response [COOP and COEP](https://sqlite.org/wasm/doc/trunk/persistence.md#coop-coep) in header), which is recommended officially (see [this](https://sqlite.org/forum/forumpost/59097f57cbe647a2d1950fab93e7ab82dd24c1e384d38b90ec1e2f03a2a4e580) and [this](https://sqlite.org/forum/forumpost/8f50dc99149a6cedade784595238f45aa912144fae81821d5f9db31965f754dd)) and **only work in WebWorker**.
34+
you can choose to use [OPFS](https://developer.mozilla.org/en-US/docs/Web/API/File_System_Access_API#origin_private_file_system) as backend storage(for some vfs, your server must response COOP and COEP in header, see [doc](https://sqlite.org/wasm/doc/trunk/persistence.md#coop-coep)), which is recommended officially (see [this](https://sqlite.org/forum/forumpost/59097f57cbe647a2d1950fab93e7ab82dd24c1e384d38b90ec1e2f03a2a4e580) and [this](https://sqlite.org/forum/forumpost/8f50dc99149a6cedade784595238f45aa912144fae81821d5f9db31965f754dd)) and **only work in WebWorker**.
3535

3636
#### `WaSqliteDialect`: polyfill
3737

@@ -43,9 +43,6 @@ you can choose not only `OPFS` but also `IndexedDB` as backend storage for bette
4343

4444
you can choose to use `native file system` as backend storage, which is no need to recompile for different platform
4545

46-
- no `RETURNING` support
47-
- only return rows when executing raw sql
48-
4946
#### `CrsqliteDialect`: CRDT
5047

5148
you can choose to use `IndexedDB` as backend storage

packages/dialect-wasm/src/baseDriver.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,23 +60,23 @@ class ConnectionMutex {
6060
}
6161
}
6262

63-
export type ExecuteReturn = Promise<
64-
Pick<QueryResult<any>, 'insertId' | 'rows' | 'numAffectedRows'>
65-
>
66-
export type QueryReturn = Promise<
67-
Pick<QueryResult<any>, 'rows'>
68-
>
63+
export type InfoReturn = Promise<Pick<QueryResult<any>, 'insertId' | 'numAffectedRows'>>
64+
export type QueryReturn = Promise<Pick<QueryResult<any>, 'rows'>['rows']>
6965

7066
export abstract class BaseSqliteConnection implements DatabaseConnection {
71-
abstract query(sql: string, param?: any[]): QueryReturn
72-
abstract execute(sql: string, param?: any[]): ExecuteReturn
67+
abstract query(sql: string, params?: any[]): QueryReturn
68+
abstract info(): InfoReturn
7369
streamQuery<R>(): AsyncIterableIterator<QueryResult<R>> {
7470
throw new Error('SQLite driver doesn\'t support streaming')
7571
}
7672

7773
async executeQuery<R>({ parameters, query, sql }: CompiledQuery<unknown>): Promise<QueryResult<R>> {
78-
return query.kind === 'SelectQueryNode'
79-
? await this.query(sql, parameters as any[])
80-
: await this.execute(sql, parameters as any[])
74+
const rows = await this.query(sql, parameters as any[])
75+
return query.kind === 'SelectQueryNode' || rows.length
76+
? { rows }
77+
: {
78+
rows,
79+
...await this.info(),
80+
}
8181
}
8282
}

packages/dialect-wasm/src/crsqlite-dialect/driver.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { ExecuteReturn, QueryReturn } from '../baseDriver'
1+
import type { InfoReturn, QueryReturn } from '../baseDriver'
22
import { BaseDriver, BaseSqliteConnection } from '../baseDriver'
33
import type { CrSqliteDB } from './type'
44
import type { CrSqliteDialectConfig } from '.'
@@ -31,16 +31,14 @@ class CrSqliteConnection extends BaseSqliteConnection {
3131
this.db = db
3232
}
3333

34-
async query(sql: string, param?: any[]): QueryReturn {
35-
return { rows: await this.db.execO(sql, param) }
34+
async query(sql: string, params?: any[]): QueryReturn {
35+
return await this.db.execO(sql, params as any[])
3636
}
3737

38-
async execute(sql: string, param?: any[]): ExecuteReturn {
39-
const rows = await this.db.execO(sql, param)
38+
async info(): InfoReturn {
4039
return {
41-
rows,
4240
numAffectedRows: BigInt(this.db.api.changes(this.db.db)),
43-
insertId: BigInt((await this.db.execA('SELECT last_insert_rowid() as id'))[0]),
41+
insertId: BigInt((await this.db.execA('SELECT last_insert_rowid()'))[0]),
4442
}
4543
}
4644
}
Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import type { CompiledQuery, DatabaseConnection, QueryResult } from 'kysely'
2-
import { BaseDriver } from '../baseDriver'
1+
import type { InfoReturn, QueryReturn } from '../baseDriver'
2+
import { BaseDriver, BaseSqliteConnection } from '../baseDriver'
33
import type { NodeWasmDataBase } from './type'
44
import type { NodeWasmDialectConfig } from '.'
55

@@ -25,29 +25,22 @@ export class NodeWasmDriver extends BaseDriver {
2525
}
2626
}
2727

28-
class NodeWasmConnection implements DatabaseConnection {
28+
class NodeWasmConnection extends BaseSqliteConnection {
2929
private db: NodeWasmDataBase
3030
constructor(db: any) {
31+
super()
3132
this.db = db
3233
}
3334

34-
async executeQuery<R>({ parameters, query, sql }: CompiledQuery<unknown>): Promise<QueryResult<R>> {
35-
switch (query.kind) {
36-
case 'SelectQueryNode':
37-
case 'RawNode':
38-
return { rows: this.db.all(sql, parameters) }
39-
default: {
40-
const { changes, lastInsertRowid } = this.db.run(sql, parameters)
41-
return {
42-
rows: [],
43-
insertId: BigInt(lastInsertRowid),
44-
numAffectedRows: BigInt(changes),
45-
}
46-
}
47-
}
35+
async query(sql: string, params?: any[] | undefined): QueryReturn {
36+
return this.db.all(sql, params)
4837
}
4938

50-
streamQuery<R>(): AsyncIterableIterator<QueryResult<R>> {
51-
throw new Error('SQLite driver doesn\'t support streaming')
39+
async info(): InfoReturn {
40+
const { changes, lastInsertRowid } = this.db.run('select 1')
41+
return {
42+
numAffectedRows: BigInt(changes),
43+
insertId: BigInt(lastInsertRowid),
44+
}
5245
}
5346
}

packages/dialect-wasm/src/node-wasm-dialect/index.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ export class NodeWasmDialect extends BaseDialect {
1313
config: NodeWasmDialectConfig
1414
/**
1515
* dialect for [node sqlite3 wasm](https://github.com/tndrle/node-sqlite3-wasm)
16-
*
17-
* - no `RETURNING` support
18-
* - only return rows when executing raw sql
1916
*/
2017
constructor(config: NodeWasmDialectConfig) {
2118
super()

packages/dialect-wasm/src/node-wasm-dialect/type.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export interface NodeWasmDBOptions {
88
}
99

1010
export interface NodeWasmDataBase extends BaseDB {
11+
get: (sql: string, param?: readonly unknown[]) => any
1112
all: (sql: string, param?: readonly unknown[]) => any[]
1213
run: (sql: string, param?: readonly unknown[]) => NodeWasmRunReturn
1314
}

0 commit comments

Comments
 (0)