From fb2dc7d824b78458e22495e756f371bd4c7084bd Mon Sep 17 00:00:00 2001 From: TheoryOfNekomata Date: Wed, 17 Apr 2024 16:54:16 +0800 Subject: [PATCH] Add preliminary implementation for duckdb Include SQL statements for duckdb statements. --- packages/data-sources/duckdb/src/index.ts | 35 ++++++++++++----------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/packages/data-sources/duckdb/src/index.ts b/packages/data-sources/duckdb/src/index.ts index 6404df66..37a3f65 100644 --- a/packages/data-sources/duckdb/src/index.ts +++ b/packages/data-sources/duckdb/src/index.ts @@ -51,7 +51,7 @@ export class DuckDbDataSource< return [columnName, columnDef.type].join(' '); }) .join(','); - const sql = `${clause} (${idAttr} ${idSchema.type}, ${tableSchema});`; + const sql = `${clause} (${idAttr} ${idSchema.type},${tableSchema});`; const con = await this.db.connect(); const stmt = await con.prepare(sql); await stmt.run(); @@ -146,8 +146,8 @@ export class DuckDbDataSource< const con = await this.db.connect(); const stmt = await con.prepare(` - SELECT * FROM ? WHERE ? = ?; - `, this.resource.state.routeName, idAttr, id); + SELECT * FROM ${this.resource.state.routeName} WHERE ${idAttr} = ${JSON.stringify(id).replace(/"/g, "'")}; + `); const data = await stmt.all(); return data as Data; } @@ -158,8 +158,8 @@ export class DuckDbDataSource< const con = await this.db.connect(); const stmt = await con.prepare(` - SELECT * FROM ? LIMIT 1; - `, this.resource.state.routeName); + SELECT * FROM ${this.resource.state.routeName} LIMIT 1; + `); const [data] = await stmt.all(); return data as Data; } @@ -173,8 +173,8 @@ export class DuckDbDataSource< const con = await this.db.connect(); const stmt = await con.prepare(` - DELETE FROM ? WHERE ? = ?; - `, this.resource.state.routeName, idAttr, id); + DELETE FROM ${this.resource.state.routeName} WHERE ${idAttr} = ${JSON.stringify(id).replace(/"/g, "'")}; + `); await stmt.run(); } @@ -185,10 +185,12 @@ export class DuckDbDataSource< const idAttr = this.resource.state.shared.get('idAttr'); assert(typeof idAttr === 'string'); + const clause = `INSERT OR REPLACE INTO ${this.resource.state.routeName}`; + const keys = Object.keys(data).join(','); + const values = Object.values(data).map((d) => JSON.stringify(d).replace(/"/g, "'")).join(','); + const sql = `${clause} (${idAttr},${keys}) VALUES (${id},${values});`; const con = await this.db.connect(); - const stmt = await con.prepare(` - INSERT OR REPLACE INTO ? (?, ...?) VALUES (...?); - `, this.resource.state.routeName, id, Object.keys(data), Object.values(data)); + const stmt = await con.prepare(sql); const [newData] = await stmt.all(); // TODO check if created flag return [newData, false] as [Data, boolean]; @@ -201,14 +203,13 @@ export class DuckDbDataSource< const idAttr = this.resource.state.shared.get('idAttr'); assert(typeof idAttr === 'string'); + const clause = `UPDATE ${this.resource.state.routeName}`; + const setParams = Object.entries(data).map(([key, value]) => ( + `${key} = ${JSON.stringify(value).replace(/"/g, "'")}` + )).join(','); + const sql = `${clause} SET ${setParams} WHERE ${idAttr} = ${JSON.stringify(id).replace(/"/g, "'")}` const con = await this.db.connect(); - const stmt = await con.prepare(` - UPDATE ? SET ? = ? WHERE ? = ?; - `, - this.resource.state.routeName, - Object.keys(data), Object.values(data), - idAttr, id - ); + const stmt = await con.prepare(sql); const [newData] = await stmt.all(); return newData as Data; }