MCPcopy Create free account
hub / github.com/bytebase/bytebase / getTableInfo

Method getTableInfo

backend/plugin/db/hive/sync.go:238–323  ·  view source on GitHub ↗
(ctx context.Context, tableName string, databaseName string)

Source from the content-addressed store, hash-verified

236)
237
238func (d *Driver) getTableInfo(ctx context.Context, tableName string, databaseName string) (
239 *TableInfo,
240 error,
241) {
242 tableInfo := &TableInfo{}
243
244 query := fmt.Sprintf("DESCRIBE FORMATTED `%s`.`%s`", databaseName, tableName)
245 rows, err := d.db.QueryContext(ctx, query)
246 if err != nil {
247 return nil, errors.Wrapf(err, "failed to describe table %s", tableName)
248 }
249 defer rows.Close()
250
251 section := columnSection
252 for rows.Next() {
253 var colName, dataType, comment string
254 if err := rows.Scan(&colName, &dataType, &comment); err != nil {
255 return nil, errors.Wrap(err, "failed to scan row")
256 }
257
258 // The first rows are column metadata, followed by "# Detailed Table Information" and "# Storage Information"
259 switch {
260 case strings.HasPrefix(colName, "# col_name"):
261 continue
262 case strings.HasPrefix(colName, "# Detailed Table Information"):
263 section = tableInfoSection
264 case strings.HasPrefix(colName, "# Storage Information"):
265 section = storageSection
266 case strings.HasPrefix(colName, "# View Information"):
267 section = viewSection
268 default:
269 // No action needed for other cases
270 }
271 switch section {
272 case columnSection:
273 if colName != "" && dataType != "" {
274 // Column metadata.
275 position := len(tableInfo.colMetadatas) + 1
276 tableInfo.colMetadatas = append(tableInfo.colMetadatas, &storepb.ColumnMetadata{
277 Name: colName,
278 Type: dataType,
279 Comment: comment,
280 Position: int32(position),
281 })
282 }
283 case tableInfoSection:
284 switch {
285 case trimField(colName) == "Table Type:":
286 tableInfo.tableType = trimField(dataType)
287 case trimField(dataType) == "numRows":
288 n, err := strconv.Atoi(trimField(comment))
289 if err != nil {
290 return nil, errors.Wrapf(err, "failed to parse row count")
291 }
292 tableInfo.numRows = n
293 case trimField(dataType) == "totalSize":
294 n, err := strconv.Atoi(trimField(comment))
295 if err != nil {

Callers 1

getTablesMethod · 0.95

Implementers 15

MockDriverbackend/plugin/advisor/utils_for_tests
Driverbackend/plugin/db/bigquery/bigquery.go
Driverbackend/plugin/db/mongodb/mongodb.go
Driverbackend/plugin/db/trino/trino.go
Driverbackend/plugin/db/redshift/redshift.go
Driverbackend/plugin/db/oracle/oracle.go
Driverbackend/plugin/db/dynamodb/dynamodb.go
Driverbackend/plugin/db/cosmosdb/cosmosdb.go
Driverbackend/plugin/db/spanner/spanner.go
Driverbackend/plugin/db/mssql/mssql.go
Driverbackend/plugin/db/mysql/mysql.go
Driverbackend/plugin/db/pg/pg.go

Calls 5

trimFieldFunction · 0.85
QueryContextMethod · 0.80
ScanMethod · 0.80
CloseMethod · 0.65
NextMethod · 0.45

Tested by

no test coverage detected