| 50 | } |
| 51 | |
| 52 | export class BigQueryDriver implements DbDriver { |
| 53 | readonly displayName: string; |
| 54 | readonly sourceId: DataSourceId; |
| 55 | readonly dialect: SQLDialect = BigQueryDialect; |
| 56 | projectId: string; |
| 57 | datasetName: string; |
| 58 | bigquery: BigQuery; |
| 59 | bigquery_meta: BigQuery; |
| 60 | dataset: Dataset; |
| 61 | tableMap: LeafSchemaMap; |
| 62 | |
| 63 | constructor(connectionInfo: BigQueryConnectionInfo) { |
| 64 | const { projectId, datasetName } = connectionInfo; |
| 65 | const resourceId = JSON.stringify(connectionInfo); |
| 66 | this.displayName = `bigquery: ${projectId}`; |
| 67 | this.sourceId = { |
| 68 | providerName: "bigquery", |
| 69 | resourceId, |
| 70 | }; |
| 71 | this.projectId = projectId; |
| 72 | this.datasetName = datasetName; |
| 73 | |
| 74 | this.bigquery = new BigQuery(); |
| 75 | this.bigquery_meta = new BigQuery({ projectId, location: LOCATION }); |
| 76 | this.dataset = this.bigquery_meta.dataset(datasetName); |
| 77 | this.tableMap = {}; |
| 78 | } |
| 79 | |
| 80 | async getDisplayName(): Promise<string> { |
| 81 | return this.displayName; |
| 82 | } |
| 83 | |
| 84 | async runSqlQuery(sqlQuery: string): Promise<Row[]> { |
| 85 | const [dbRows] = await this.bigquery.query({ |
| 86 | query: sqlQuery, |
| 87 | location: LOCATION, |
| 88 | }); |
| 89 | const rows = dbRows as Row[]; |
| 90 | return rows; |
| 91 | } |
| 92 | |
| 93 | async importCsv(pathname: string, metadata: any): Promise<void> { |
| 94 | const tableName = genTableName(pathname); |
| 95 | const [job] = await this.dataset.table(tableName).load(pathname, metadata); |
| 96 | console.log( |
| 97 | "importCsv: load completed: ", |
| 98 | JSON.stringify(job, undefined, 2) |
| 99 | ); |
| 100 | } |
| 101 | |
| 102 | async getTableSchema(tableName: string): Promise<Schema> { |
| 103 | const [projectId, datasetName, baseTableName] = tableName.split("."); |
| 104 | const sqlQuery = `SELECT column_name, data_type FROM \`${projectId}.${datasetName}\`.INFORMATION_SCHEMA.COLUMNS WHERE table_name="${baseTableName}"`; |
| 105 | |
| 106 | const [dbRows] = await this.bigquery.query({ |
| 107 | query: sqlQuery, |
| 108 | location: LOCATION, |
| 109 | }); |
nothing calls this directly
no outgoing calls
no test coverage detected