(
schemaName: string,
tableName: string,
options: SelectFromTableOptions
)
| 318 | } |
| 319 | |
| 320 | async selectTable( |
| 321 | schemaName: string, |
| 322 | tableName: string, |
| 323 | options: SelectFromTableOptions |
| 324 | ): Promise<{ data: DatabaseResultSet; schema: DatabaseTableSchema }> { |
| 325 | const schema = await this.tableSchema(schemaName, tableName); |
| 326 | let injectRowIdColumn = false; |
| 327 | |
| 328 | // If there is no primary key, we will fallback to rowid. |
| 329 | // But we need to make sure there is no rowid column |
| 330 | if ( |
| 331 | schema.pk.length === 0 && |
| 332 | !schema.withoutRowId && |
| 333 | !schema.columns.find((c) => c.name === "rowid") && |
| 334 | schema.type === "table" |
| 335 | ) { |
| 336 | // Inject the rowid column |
| 337 | injectRowIdColumn = true; |
| 338 | schema.columns = [ |
| 339 | { |
| 340 | name: "rowid", |
| 341 | type: "INTEGER", |
| 342 | constraint: { |
| 343 | primaryKey: true, |
| 344 | autoIncrement: true, |
| 345 | }, |
| 346 | }, |
| 347 | ...schema.columns, |
| 348 | ]; |
| 349 | schema.pk = ["rowid"]; |
| 350 | schema.autoIncrement = true; |
| 351 | } |
| 352 | |
| 353 | const whereRaw = options.whereRaw?.trim(); |
| 354 | |
| 355 | const orderPart = |
| 356 | options.orderBy && options.orderBy.length > 0 |
| 357 | ? options.orderBy |
| 358 | .map((r) => `${this.escapeId(r.columnName)} ${r.by}`) |
| 359 | .join(", ") |
| 360 | : ""; |
| 361 | |
| 362 | const sql = `SELECT ${injectRowIdColumn ? "rowid, " : ""}* FROM ${this.escapeId(schemaName)}.${this.escapeId(tableName)}${ |
| 363 | whereRaw ? ` WHERE ${whereRaw} ` : "" |
| 364 | } ${orderPart ? ` ORDER BY ${orderPart}` : ""} LIMIT ${escapeSqlValue(options.limit)} OFFSET ${escapeSqlValue(options.offset)};`; |
| 365 | |
| 366 | let data = await this.query(sql); |
| 367 | |
| 368 | // If data does not have any header, we will inject the header from schema |
| 369 | if (data.headers.length === 0 && data.rows.length === 0) { |
| 370 | data = { |
| 371 | ...data, |
| 372 | headers: schema.columns.map((col) => { |
| 373 | return { |
| 374 | name: col.name, |
| 375 | originalType: col.type, |
| 376 | displayName: col.name, |
| 377 | }; |
no test coverage detected