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

Function getTables

backend/plugin/db/redshift/sync.go:286–337  ·  view source on GitHub ↗

getTables gets all tables of a database.

(txn *sql.Tx, columnMap map[db.TableKey][]*storepb.ColumnMetadata)

Source from the content-addressed store, hash-verified

284
285// getTables gets all tables of a database.
286func getTables(txn *sql.Tx, columnMap map[db.TableKey][]*storepb.ColumnMetadata) (map[string][]*storepb.TableMetadata, error) {
287 indexMap, err := getIndexes(txn)
288 if err != nil {
289 return nil, errors.Wrapf(err, "failed to get indices")
290 }
291 foreignKeysMap, err := getForeignKeys(txn)
292 if err != nil {
293 return nil, errors.Wrapf(err, "failed to get foreign keys")
294 }
295
296 tableMap := make(map[string][]*storepb.TableMetadata)
297 query := `
298 SELECT
299 ptbl.schemaname,
300 ptbl.tablename,
301 0, -- data size
302 0, -- index size
303 GREATEST(pc.reltuples::bigint, 0::bigint) AS estimate,
304 obj_description(pc.oid) AS comment
305 FROM pg_catalog.pg_tables AS ptbl
306 JOIN pg_namespace AS pns ON pns.nspname = ptbl.schemaname
307 LEFT JOIN pg_class AS pc ON pc.relname = ptbl.tablename AND pns.oid = pc.relnamespace
308 WHERE ptbl.schemaname NOT IN ('pg_catalog', 'information_schema');`
309 rows, err := txn.Query(query)
310 if err != nil {
311 return nil, err
312 }
313 defer rows.Close()
314
315 for rows.Next() {
316 table := &storepb.TableMetadata{}
317 var schemaName string
318 var comment sql.NullString
319 if err := rows.Scan(&schemaName, &table.Name, &table.DataSize, &table.IndexSize, &table.RowCount, &comment); err != nil {
320 return nil, err
321 }
322 if comment.Valid {
323 table.Comment = comment.String
324 }
325 key := db.TableKey{Schema: schemaName, Table: table.Name}
326 table.Columns = columnMap[key]
327 table.Indexes = indexMap[key]
328 table.ForeignKeys = foreignKeysMap[key]
329
330 tableMap[schemaName] = append(tableMap[schemaName], table)
331 }
332 if err := rows.Err(); err != nil {
333 return nil, err
334 }
335
336 return tableMap, nil
337}
338
339// getTableColumns gets the columns of a table.
340func getTableColumns(txn *sql.Tx) (map[db.TableKey][]*storepb.ColumnMetadata, error) {

Callers 1

SyncDBSchemaMethod · 0.70

Calls 6

ScanMethod · 0.80
getIndexesFunction · 0.70
getForeignKeysFunction · 0.70
QueryMethod · 0.65
CloseMethod · 0.65
NextMethod · 0.45

Tested by

no test coverage detected