From ce659efc22655aca4ab601c9d3f73a9c559dca36 Mon Sep 17 00:00:00 2001 From: "Visal .In" Date: Mon, 2 Dec 2024 08:55:37 +0700 Subject: [PATCH] fixing fallback view and some minor bug related to rowid fallback --- src/drivers/base-driver.ts | 2 ++ src/drivers/common-sql-imp.ts | 6 ++++-- src/drivers/mysql/mysql-driver.ts | 1 + src/drivers/postgres/postgres-driver.ts | 1 + src/drivers/query-builder.ts | 10 ++++++---- src/drivers/sqlite-base-driver.ts | 7 +++++-- 6 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/drivers/base-driver.ts b/src/drivers/base-driver.ts index 32a28694..a755ce1b 100644 --- a/src/drivers/base-driver.ts +++ b/src/drivers/base-driver.ts @@ -153,6 +153,7 @@ export interface DatabaseTableSchema { constraints?: DatabaseTableColumnConstraint[]; createScript?: string; fts5?: DatabaseTableFts5; + type?: "table" | "view"; withoutRowId?: boolean; strict?: boolean; } @@ -213,6 +214,7 @@ export interface DriverFlags { // data when update supportInsertReturning: boolean; supportUpdateReturning: boolean; + supportRowId: boolean; } export interface DatabaseTableColumnChange { diff --git a/src/drivers/common-sql-imp.ts b/src/drivers/common-sql-imp.ts index aa8c1621..4345bda2 100644 --- a/src/drivers/common-sql-imp.ts +++ b/src/drivers/common-sql-imp.ts @@ -41,7 +41,8 @@ export default abstract class CommonSQLImplement extends BaseDriver { schemaName, tableName, op.values, - this.getFlags().supportInsertReturning + this.getFlags().supportInsertReturning, + this.getFlags().supportRowId ); if (op.operation === "DELETE") @@ -53,7 +54,8 @@ export default abstract class CommonSQLImplement extends BaseDriver { tableName, op.values, op.where, - this.getFlags().supportInsertReturning + this.getFlags().supportInsertReturning, + this.getFlags().supportRowId ); }); diff --git a/src/drivers/mysql/mysql-driver.ts b/src/drivers/mysql/mysql-driver.ts index bf01efb8..6f6ecffb 100644 --- a/src/drivers/mysql/mysql-driver.ts +++ b/src/drivers/mysql/mysql-driver.ts @@ -55,6 +55,7 @@ export default abstract class MySQLLikeDriver extends CommonSQLImplement { supportCreateUpdateTable: false, dialect: "mysql", + supportRowId: false, supportInsertReturning: false, supportUpdateReturning: false, }; diff --git a/src/drivers/postgres/postgres-driver.ts b/src/drivers/postgres/postgres-driver.ts index 80868657..48e106ca 100644 --- a/src/drivers/postgres/postgres-driver.ts +++ b/src/drivers/postgres/postgres-driver.ts @@ -67,6 +67,7 @@ export default abstract class PostgresLikeDriver extends CommonSQLImplement { defaultSchema: "public", dialect: "postgres", optionalSchema: false, + supportRowId: false, supportBigInt: false, supportModifyColumn: false, mismatchDetection: false, diff --git a/src/drivers/query-builder.ts b/src/drivers/query-builder.ts index e6f82eab..239d00d2 100644 --- a/src/drivers/query-builder.ts +++ b/src/drivers/query-builder.ts @@ -65,13 +65,14 @@ export function insertInto( schema: string, table: string, value: Record, - supportReturning: boolean + supportReturning: boolean, + supportRowId: boolean ) { return [ "INSERT INTO", `${dialect.escapeId(schema)}.${dialect.escapeId(table)}`, generateInsertValue(dialect, value), - supportReturning ? "RETURNING *" : "", + supportReturning ? `RETURNING ${supportRowId ? "rowid, " : ""}*` : "", ].join(" "); } @@ -81,7 +82,8 @@ export function updateTable( table: string, value: Record, where: Record, - supportReturning: boolean + supportReturning: boolean, + supportRowId: boolean ): string { return [ "UPDATE", @@ -89,7 +91,7 @@ export function updateTable( "SET", generateSet(dialect, value), generateWhere(dialect, where), - supportReturning ? "RETURNING *" : "", + supportReturning ? `RETURNING ${supportRowId ? "rowid, " : ""}*` : "", ] .filter(Boolean) .join(" "); diff --git a/src/drivers/sqlite-base-driver.ts b/src/drivers/sqlite-base-driver.ts index 065ea806..4ce5d765 100644 --- a/src/drivers/sqlite-base-driver.ts +++ b/src/drivers/sqlite-base-driver.ts @@ -31,6 +31,7 @@ export abstract class SqliteLikeBaseDriver extends CommonSQLImplement { getFlags(): DriverFlags { return { + supportRowId: true, supportBigInt: false, supportModifyColumn: false, supportInsertReturning: true, @@ -193,6 +194,7 @@ export abstract class SqliteLikeBaseDriver extends CommonSQLImplement { try { const rows = result.rows as Array<{ type: string; sql: string }>; const def = rows.find((row) => row.type === "table"); + if (def) { const createScript = def.sql; @@ -200,6 +202,7 @@ export abstract class SqliteLikeBaseDriver extends CommonSQLImplement { ...parseCreateTableScript(schemaName, createScript), createScript, schemaName, + type: "table", }; } @@ -228,7 +231,6 @@ export abstract class SqliteLikeBaseDriver extends CommonSQLImplement { .join(", "); // If there is rowid, it is likely, we need to query that row back - console.log("sss", key); const hasRowId = !!key["rowid"]; const sql = `SELECT ${hasRowId ? "rowid, " : ""}* FROM ${this.escapeId(schemaName)}.${this.escapeId(tableName)} ${wherePart ? "WHERE " + wherePart : ""} LIMIT 1 OFFSET 0`; @@ -248,7 +250,8 @@ export abstract class SqliteLikeBaseDriver extends CommonSQLImplement { if ( schema.pk.length === 0 && !schema.withoutRowId && - !schema.columns.find((c) => c.name === "rowid") + !schema.columns.find((c) => c.name === "rowid") && + schema.type === "table" ) { // Inject the rowid column injectRowIdColumn = true;