getTables gets all tables of a database.
(txn *sql.Tx, schemaName string, columnMap map[db.TableKey][]*storepb.ColumnMetadata, triggerMap map[db.TableKey][]*storepb.TriggerMetadata)
| 285 | |
| 286 | // getTables gets all tables of a database. |
| 287 | func getTables(txn *sql.Tx, schemaName string, columnMap map[db.TableKey][]*storepb.ColumnMetadata, triggerMap map[db.TableKey][]*storepb.TriggerMetadata) (map[string][]*storepb.TableMetadata, error) { |
| 288 | indexMap, checkConstraintMap, foreignKeyMap, err := getIndexesAndConstraints(txn, schemaName) |
| 289 | if err != nil { |
| 290 | return nil, errors.Wrapf(err, "failed to get indices") |
| 291 | } |
| 292 | columnCommentMap, err := getTableColumnComments(txn, schemaName) |
| 293 | if err != nil { |
| 294 | return nil, errors.Wrapf(err, "failed to get table column comments") |
| 295 | } |
| 296 | for key, columns := range columnMap { |
| 297 | for _, column := range columns { |
| 298 | comment, ok := columnCommentMap[db.ColumnKey{Schema: key.Schema, Table: key.Table, Column: column.Name}] |
| 299 | if ok { |
| 300 | column.Comment = comment |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 | tableCommentMap, err := getTableComments(txn, schemaName) |
| 305 | if err != nil { |
| 306 | return nil, errors.Wrapf(err, "failed to get table comments") |
| 307 | } |
| 308 | tableMap := make(map[string][]*storepb.TableMetadata) |
| 309 | |
| 310 | query := fmt.Sprintf(` |
| 311 | SELECT OWNER, TABLE_NAME, NUM_ROWS |
| 312 | FROM all_tables |
| 313 | WHERE OWNER = '%s' |
| 314 | AND TABLE_NAME NOT IN ( |
| 315 | SELECT MVIEW_NAME FROM all_mviews WHERE OWNER = '%s' |
| 316 | ) |
| 317 | ORDER BY TABLE_NAME`, schemaName, schemaName) |
| 318 | |
| 319 | slog.Debug("running get tables query") |
| 320 | rows, err := txn.Query(query) |
| 321 | if err != nil { |
| 322 | return nil, util.FormatErrorWithQuery(err, query) |
| 323 | } |
| 324 | defer rows.Close() |
| 325 | |
| 326 | for rows.Next() { |
| 327 | table := &storepb.TableMetadata{} |
| 328 | var schemaName string |
| 329 | // https://github.com/rana/ora/issues/57#issuecomment-179909837 |
| 330 | // NUMBER in Oracle can hold 38 decimal digits, so int64 is not enough with its 19 decimal digits. |
| 331 | // float64 is a little bit better - not precise enough, but won't overflow. |
| 332 | var count sql.NullFloat64 |
| 333 | if err := rows.Scan(&schemaName, &table.Name, &count); err != nil { |
| 334 | return nil, err |
| 335 | } |
| 336 | table.RowCount = int64(count.Float64) |
| 337 | key := db.TableKey{Schema: schemaName, Table: table.Name} |
| 338 | table.Columns = columnMap[key] |
| 339 | table.Indexes = indexMap[key] |
| 340 | table.CheckConstraints = checkConstraintMap[key] |
| 341 | table.ForeignKeys = foreignKeyMap[key] |
| 342 | table.Triggers = triggerMap[key] |
| 343 | if comment, ok := tableCommentMap[db.TableKey{Schema: schemaName, Table: table.Name}]; ok { |
| 344 | table.Comment = comment |
no test coverage detected