| 42 | ); |
| 43 | |
| 44 | export class SqliteDriver implements DbDriver { |
| 45 | readonly displayName: string; |
| 46 | readonly sourceId: DataSourceId; |
| 47 | readonly dialect: SQLDialect = SQLiteDialect; |
| 48 | dbfile: string; |
| 49 | db: sqlite3.Database; |
| 50 | private tableMap: LeafSchemaMap; |
| 51 | |
| 52 | constructor(dbfile: string, db: any) { |
| 53 | this.dbfile = dbfile; |
| 54 | this.displayName = dbfile; |
| 55 | this.sourceId = { providerName: "sqlite", resourceId: dbfile }; |
| 56 | this.db = db; |
| 57 | this.tableMap = {}; |
| 58 | } |
| 59 | |
| 60 | async getDisplayName(): Promise<string> { |
| 61 | return this.displayName; |
| 62 | } |
| 63 | |
| 64 | async runSqlQuery(sqlQuery: string): Promise<Row[]> { |
| 65 | const dbRows = await dbAll(this.db, sqlQuery); |
| 66 | const rows = dbRows as Row[]; |
| 67 | return rows; |
| 68 | } |
| 69 | |
| 70 | // Get table info directly from sqlite db |
| 71 | async getTableSchema(tableName: string): Promise<Schema> { |
| 72 | const tiQuery = `PRAGMA table_info(${tableName})`; |
| 73 | const dbRows = await this.runSqlQuery(tiQuery); |
| 74 | // log.debug("getTableSchema: ", rows); |
| 75 | |
| 76 | const extendCMap = ( |
| 77 | cmm: ColumnMetaMap, |
| 78 | row: any, |
| 79 | idx: number |
| 80 | ): ColumnMetaMap => { |
| 81 | const cnm = row.name; |
| 82 | const cType = row.type.toLocaleUpperCase(); |
| 83 | |
| 84 | if (cType == null) { |
| 85 | log.error( |
| 86 | 'mkTableInfo: No column type for "' + cnm + '", index: ' + idx |
| 87 | ); |
| 88 | } |
| 89 | const cmd = { |
| 90 | displayName: cnm, |
| 91 | columnType: cType, |
| 92 | }; |
| 93 | cmm[cnm] = cmd; |
| 94 | return cmm; |
| 95 | }; |
| 96 | |
| 97 | const cmMap = dbRows.reduce(extendCMap, {}); |
| 98 | const columnIds = dbRows.map((r) => r.name); |
| 99 | const schema = new Schema(SQLiteDialect, columnIds as string[], cmMap); |
| 100 | return schema; |
| 101 | } |
nothing calls this directly
no outgoing calls
no test coverage detected