Skip to content

Commit d40ce98

Browse files
Add connection.sql method for simple async queries
1 parent e1c507d commit d40ce98

File tree

3 files changed

+70
-3
lines changed

3 files changed

+70
-3
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@sqlitecloud/drivers",
3-
"version": "0.0.57",
3+
"version": "1.0.82",
44
"description": "SQLiteCloud drivers for Typescript/Javascript in edge, web and node clients",
55
"main": "./lib/index.js",
66
"types": "./lib/index.d.ts",

src/drivers/connection.ts

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
*/
44

55
import { SQLiteCloudConfig, SQLiteCloudError, ErrorCallback, ResultsCallback } from './types'
6-
import { validateConfiguration } from './utilities'
6+
import { validateConfiguration, prepareSql } from './utilities'
77
import { OperationsQueue } from './queue'
8-
import { anonimizeCommand } from './utilities'
8+
import { anonimizeCommand, getUpdateResults } from './utilities'
99

1010
/**
1111
* Base class for SQLiteCloudConnection handles basics and defines methods.
@@ -102,6 +102,49 @@ export abstract class SQLiteCloudConnection {
102102
return this
103103
}
104104

105+
/**
106+
* Sql is a promise based API for executing SQL statements. You can
107+
* pass a simple string with a SQL statement or a template string
108+
* using backticks and parameters in ${parameter} format. These parameters
109+
* will be properly escaped and quoted like when using a prepared statement.
110+
* @param sql A sql string or a template string in `backticks` format
111+
* @returns An array of rows in case of selections or an object with
112+
* metadata in case of insert, update, delete.
113+
*/
114+
public async sql(sql: TemplateStringsArray | string, ...values: any[]): Promise<any> {
115+
let preparedSql = ''
116+
117+
// sql is a TemplateStringsArray, the 'raw' property is specific to TemplateStringsArray
118+
if (Array.isArray(sql) && 'raw' in sql) {
119+
sql.forEach((string, i) => {
120+
preparedSql += string + (i < values.length ? '?' : '')
121+
})
122+
preparedSql = prepareSql(preparedSql, ...values)
123+
} else {
124+
if (typeof sql === 'string') {
125+
if (values?.length > 0) {
126+
preparedSql = prepareSql(sql, ...values)
127+
} else {
128+
preparedSql = sql
129+
}
130+
} else {
131+
throw new Error('Invalid sql')
132+
}
133+
}
134+
135+
return new Promise((resolve, reject) => {
136+
this.sendCommands(preparedSql, (error, results) => {
137+
if (error) {
138+
reject(error)
139+
} else {
140+
// metadata for operations like insert, update, delete?
141+
const context = getUpdateResults(results)
142+
resolve(context ? context : results)
143+
}
144+
})
145+
})
146+
}
147+
105148
/** Disconnect from server, release transport. */
106149
public abstract close(): this
107150
}

src/drivers/utilities.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
//
44

55
import { SQLiteCloudConfig, SQLiteCloudError, SQLiteCloudDataTypes, DEFAULT_PORT, DEFAULT_TIMEOUT } from './types'
6+
import { SQLiteCloudArrayType } from './types'
67

78
//
89
// determining running environment, thanks to browser-or-node
@@ -149,6 +150,29 @@ export function prepareSql(sql: string, ...params: (SQLiteCloudDataTypes | SQLit
149150
return preparedSql
150151
}
151152

153+
/** Converts results of an update or insert call into a more meaning full result set */
154+
export function getUpdateResults(results?: any): Record<string, any> | undefined {
155+
if (results) {
156+
if (Array.isArray(results) && results.length > 0) {
157+
switch (results[0]) {
158+
case SQLiteCloudArrayType.ARRAY_TYPE_SQLITE_EXEC:
159+
return {
160+
type: results[0],
161+
index: results[1],
162+
lastID: results[2], // ROWID (sqlite3_last_insert_rowid)
163+
changes: results[3], // CHANGES(sqlite3_changes)
164+
totalChanges: results[4], // TOTAL_CHANGES (sqlite3_total_changes)
165+
finalized: results[5], // FINALIZED
166+
//
167+
rowId: results[2] // same as lastId
168+
}
169+
}
170+
}
171+
}
172+
173+
return undefined
174+
}
175+
152176
/**
153177
* Many of the methods in our API may contain a callback as their last argument.
154178
* This method will take the arguments array passed to the method and return an object

0 commit comments

Comments
 (0)