Browse Source

Add preliminary implementation for duckdb

Include SQL statements for duckdb statements.
master
TheoryOfNekomata 8 months ago
parent
commit
fb2dc7d824
1 changed files with 18 additions and 17 deletions
  1. +18
    -17
      packages/data-sources/duckdb/src/index.ts

+ 18
- 17
packages/data-sources/duckdb/src/index.ts View File

@@ -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;
}


Loading…
Cancel
Save