Skip to content
Closed
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
46 changes: 3 additions & 43 deletions src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,40 +93,6 @@ class BunSQLiteQueryable implements SqlQueryable {
}
}

private getTableFromQuery(sql: string): string | null {
// Simple regex to extract table name from SELECT queries
// This handles common cases like SELECT ... FROM table, SELECT ... FROM "table", etc.
const match = sql.match(/\bFROM\s+(?:`([^`]+)`|"([^"]+)"|(\w+))/i);
return match ? (match[1] || match[2] || match[3]) : null;
}

private async getColumnTypes(tableName: string, columnNames: string[]): Promise<Array<string | null>> {
try {
const tableInfoStmt = this.db.query(`PRAGMA table_info(${tableName})`);
const tableInfo = tableInfoStmt.all() as Array<{
cid: number;
name: string;
type: string;
notnull: number;
dflt_value: string | null;
pk: number;
}>;

// Create a map of column names to types
const typeMap = new Map<string, string>();
tableInfo.forEach(col => {
typeMap.set(col.name, col.type);
});

// Return types in the same order as columnNames
return columnNames.map(name => typeMap.get(name) || null);
} catch (e) {
debug("Failed to get column types for table %s: %O", tableName, e);
// Fall back to null types if we can't get schema info
return columnNames.map(() => null);
}
}

private async performIO(query: SqlQuery): Promise<BunSQLiteResultSet> {
try {
const stmt = this.db.query(query.sql);
Expand All @@ -143,14 +109,8 @@ class BunSQLiteQueryable implements SqlQueryable {
});
}

// Try to get proper column types from table schema
let declaredTypes: Array<string | null>;
const tableName = this.getTableFromQuery(query.sql);
if (tableName) {
declaredTypes = await this.getColumnTypes(tableName, columns);
} else {
declaredTypes = columns.map((col: any) => null);
}
// Schema-declared types may be available. Will be `null` for e.g. computed columns or expressions
const declaredTypes: Array<string | null> = stmt.declaredTypes;

const resultSet = {
declaredTypes,
Expand Down Expand Up @@ -445,4 +405,4 @@ function configureWALMode(db: Database, walConfig: boolean | WALConfig): void {
db.exec(`PRAGMA busy_timeout = ${config.busyTimeout};`);
debug(`Busy timeout set to: ${config.busyTimeout}ms`);
}
}
}