Skip to content

Commit

Permalink
fixing fallback view and some minor bug related to rowid fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
invisal committed Dec 2, 2024
1 parent e67a326 commit ce659ef
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/drivers/base-driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ export interface DatabaseTableSchema {
constraints?: DatabaseTableColumnConstraint[];
createScript?: string;
fts5?: DatabaseTableFts5;
type?: "table" | "view";
withoutRowId?: boolean;
strict?: boolean;
}
Expand Down Expand Up @@ -213,6 +214,7 @@ export interface DriverFlags {
// data when update
supportInsertReturning: boolean;
supportUpdateReturning: boolean;
supportRowId: boolean;
}

export interface DatabaseTableColumnChange {
Expand Down
6 changes: 4 additions & 2 deletions src/drivers/common-sql-imp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
);
});

Expand Down
1 change: 1 addition & 0 deletions src/drivers/mysql/mysql-driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export default abstract class MySQLLikeDriver extends CommonSQLImplement {
supportCreateUpdateTable: false,
dialect: "mysql",

supportRowId: false,
supportInsertReturning: false,
supportUpdateReturning: false,
};
Expand Down
1 change: 1 addition & 0 deletions src/drivers/postgres/postgres-driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 6 additions & 4 deletions src/drivers/query-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,14 @@ export function insertInto(
schema: string,
table: string,
value: Record<string, unknown>,
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(" ");
}

Expand All @@ -81,15 +82,16 @@ export function updateTable(
table: string,
value: Record<string, unknown>,
where: Record<string, unknown>,
supportReturning: boolean
supportReturning: boolean,
supportRowId: boolean
): string {
return [
"UPDATE",
`${dialect.escapeId(schema)}.${dialect.escapeId(table)}`,
"SET",
generateSet(dialect, value),
generateWhere(dialect, where),
supportReturning ? "RETURNING *" : "",
supportReturning ? `RETURNING ${supportRowId ? "rowid, " : ""}*` : "",
]
.filter(Boolean)
.join(" ");
Expand Down
7 changes: 5 additions & 2 deletions src/drivers/sqlite-base-driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export abstract class SqliteLikeBaseDriver extends CommonSQLImplement {

getFlags(): DriverFlags {
return {
supportRowId: true,
supportBigInt: false,
supportModifyColumn: false,
supportInsertReturning: true,
Expand Down Expand Up @@ -193,13 +194,15 @@ 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;

return {
...parseCreateTableScript(schemaName, createScript),
createScript,
schemaName,
type: "table",
};
}

Expand Down Expand Up @@ -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`;
Expand All @@ -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;
Expand Down

0 comments on commit ce659ef

Please sign in to comment.