MCPcopy Index your code
hub / github.com/bytebase/bytebase / getTableColumns

Function getTableColumns

backend/plugin/db/oracle/sync.go:443–546  ·  view source on GitHub ↗

getTableColumns gets the columns of a table.

(txn *sql.Tx, schemaName string, version *plsql.Version)

Source from the content-addressed store, hash-verified

441
442// getTableColumns gets the columns of a table.
443func getTableColumns(txn *sql.Tx, schemaName string, version *plsql.Version) (map[db.TableKey][]*storepb.ColumnMetadata, error) {
444 columnsMap := make(map[db.TableKey][]*storepb.ColumnMetadata)
445
446 // https://github.com/bytebase/bytebase/issues/6663
447 // Invisible columns don't have column ID so that we need to filter out them.
448 query := ""
449 // https://docs.oracle.com/en/database/oracle/oracle-database/12.2/refrn/ALL_TAB_COLS.html#GUID-85036F42-140A-406B-BE11-0AC49A00DBA3
450 equalOrHigherThan12c2release := version.First > 12 || (version.First == 12 && version.Second >= 2)
451 if equalOrHigherThan12c2release {
452 query = fmt.Sprintf(`
453 SELECT
454 OWNER,
455 TABLE_NAME,
456 COLUMN_NAME,
457 DATA_TYPE,
458 DATA_LENGTH,
459 DATA_PRECISION,
460 DATA_SCALE,
461 COLUMN_ID,
462 DATA_DEFAULT,
463 NULLABLE,
464 COLLATION,
465 DEFAULT_ON_NULL
466 FROM sys.all_tab_columns
467 WHERE OWNER = '%s' AND COLUMN_ID IS NOT NULL
468 ORDER BY TABLE_NAME, COLUMN_ID`, schemaName)
469 } else {
470 query = fmt.Sprintf(`
471 SELECT
472 OWNER,
473 TABLE_NAME,
474 COLUMN_NAME,
475 DATA_TYPE,
476 DATA_LENGTH,
477 DATA_PRECISION,
478 DATA_SCALE,
479 COLUMN_ID,
480 DATA_DEFAULT,
481 NULLABLE,
482 NULL,
483 NULL
484 FROM sys.all_tab_columns
485 WHERE OWNER = '%s' AND COLUMN_ID IS NOT NULL
486 ORDER BY TABLE_NAME, COLUMN_ID`, schemaName)
487 }
488
489 slog.Debug("running get columns query")
490 rows, err := txn.Query(query)
491 if err != nil {
492 return nil, util.FormatErrorWithQuery(err, query)
493 }
494 defer rows.Close()
495 for rows.Next() {
496 column := &storepb.ColumnMetadata{}
497 var schemaName, tableName, nullable string
498 var defaultStr, collation, defaultOnNull sql.NullString
499 var dataLength, dataPrecision, dataScale sql.NullInt64
500 if err := rows.Scan(

Callers 1

SyncDBSchemaMethod · 0.70

Calls 9

FormatErrorWithQueryFunction · 0.92
ConvertYesNoFunction · 0.92
getTypeStringFunction · 0.85
cleanDefaultExpressionFunction · 0.85
DebugMethod · 0.80
ScanMethod · 0.80
QueryMethod · 0.65
CloseMethod · 0.65
NextMethod · 0.45

Tested by

no test coverage detected