Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Execute overloading #236

Merged
merged 2 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions packages/libsql-client-wasm/src/wasm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import sqlite3InitModule from "@libsql/libsql-wasm-experimental";

import type {
Database,
InitOptions,
SqlValue,
Sqlite3Static,
} from "@libsql/libsql-wasm-experimental";
Expand All @@ -18,6 +17,7 @@ import type {
Value,
InValue,
InStatement,
InArgs
} from "@libsql/core/api";
import { LibsqlError } from "@libsql/core/api";
import type { ExpandedConfig } from "@libsql/core/config";
Expand Down Expand Up @@ -122,7 +122,21 @@ export class Sqlite3Client implements Client {
this.protocol = "file";
}

async execute(stmt: InStatement): Promise<ResultSet> {
async execute(stmt: InStatement): Promise<ResultSet>;
async execute(sql: string, args?: InArgs): Promise<ResultSet>;

async execute(stmtOrSql: InStatement | string, args?: InArgs): Promise<ResultSet> {
let stmt: InStatement;

if (typeof stmtOrSql === 'string') {
stmt = {
sql: stmtOrSql,
args: args || []
};
} else {
stmt = stmtOrSql;
}

this.#checkNotClosed();
return executeStmt(this.#getDb(), stmt, this.#intMode);
}
Expand Down
17 changes: 16 additions & 1 deletion packages/libsql-client/src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
ResultSet,
Transaction,
IntMode,
InArgs
} from "@libsql/core/api";
import { TransactionMode, LibsqlError } from "@libsql/core/api";
import type { ExpandedConfig } from "@libsql/core/config";
Expand Down Expand Up @@ -112,7 +113,21 @@ export class HttpClient implements Client {
return this.#promiseLimitFunction(fn);
}

async execute(stmt: InStatement): Promise<ResultSet> {
async execute(stmt: InStatement): Promise<ResultSet>;
async execute(sql: string, args?: InArgs): Promise<ResultSet>;

async execute(stmtOrSql: InStatement | string, args?: InArgs): Promise<ResultSet> {
let stmt: InStatement;

if (typeof stmtOrSql === 'string') {
stmt = {
sql: stmtOrSql,
args: args || []
};
} else {
stmt = stmtOrSql;
}

return this.limit<ResultSet>(async () => {
try {
const isSchemaDatabasePromise = this.getIsSchemaDatabase();
Expand Down
33 changes: 31 additions & 2 deletions packages/libsql-client/src/sqlite3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type {
Value,
InValue,
InStatement,
InArgs
} from "@libsql/core/api";
import { LibsqlError } from "@libsql/core/api";
import type { ExpandedConfig } from "@libsql/core/config";
Expand Down Expand Up @@ -105,7 +106,21 @@ export class Sqlite3Client implements Client {
this.protocol = "file";
}

async execute(stmt: InStatement): Promise<ResultSet> {
async execute(stmt: InStatement): Promise<ResultSet>;
async execute(sql: string, args?: InArgs): Promise<ResultSet>;

async execute(stmtOrSql: InStatement | string, args?: InArgs): Promise<ResultSet> {
let stmt: InStatement;

if (typeof stmtOrSql === 'string') {
stmt = {
sql: stmtOrSql,
args: args || []
};
} else {
stmt = stmtOrSql;
}

this.#checkNotClosed();
return executeStmt(this.#getDb(), stmt, this.#intMode);
}
Expand Down Expand Up @@ -192,7 +207,21 @@ export class Sqlite3Transaction implements Transaction {
this.#intMode = intMode;
}

async execute(stmt: InStatement): Promise<ResultSet> {
async execute(stmt: InStatement): Promise<ResultSet>;
async execute(sql: string, args?: InArgs): Promise<ResultSet>;

async execute(stmtOrSql: InStatement | string, args?: InArgs): Promise<ResultSet> {
let stmt: InStatement;

if (typeof stmtOrSql === 'string') {
stmt = {
sql: stmtOrSql,
args: args || []
};
} else {
stmt = stmtOrSql;
}

this.#checkNotClosed();
return executeStmt(this.#database, stmt, this.#intMode);
}
Expand Down
17 changes: 16 additions & 1 deletion packages/libsql-client/src/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type {
Transaction,
ResultSet,
InStatement,
InArgs
} from "@libsql/core/api";
import { TransactionMode, LibsqlError } from "@libsql/core/api";
import type { ExpandedConfig } from "@libsql/core/config";
Expand Down Expand Up @@ -167,7 +168,21 @@ export class WsClient implements Client {
return this.#promiseLimitFunction(fn);
}

async execute(stmt: InStatement): Promise<ResultSet> {
async execute(stmt: InStatement): Promise<ResultSet>;
async execute(sql: string, args?: InArgs): Promise<ResultSet>;

async execute(stmtOrSql: InStatement | string, args?: InArgs): Promise<ResultSet> {
let stmt: InStatement;

if (typeof stmtOrSql === 'string') {
stmt = {
sql: stmtOrSql,
args: args || []
};
} else {
stmt = stmtOrSql;
}

return this.limit<ResultSet>(async () => {
const streamState = await this.#openStream();
try {
Expand Down
Loading