Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

feat: implement oracle get database metadata (#16721)

rebeliceeae48fda166b
diff --git a/backend/plugin/db/oracle/sync.go b/backend/plugin/db/oracle/sync.go
index 0da7b288a2..9d23a50526 100644
--- a/backend/plugin/db/oracle/sync.go
+++ b/backend/plugin/db/oracle/sync.go
@@ -115,6 +115,10 @@ func (d *Driver) SyncDBSchema(ctx context.Context) (*storepb.DatabaseSchemaMetad
if err != nil {
return nil, errors.Wrapf(err, "failed to get routines from database %q", d.databaseName)
}
+ materializedViews, err := getMaterializedViews(txn, d.databaseName, columnMap)
+ if err != nil {
+ return nil, errors.Wrapf(err, "failed to get materialized views from database %q", d.databaseName)
+ }
if err := txn.Commit(); err != nil {
return nil, err
@@ -126,13 +130,14 @@ func (d *Driver) SyncDBSchema(ctx context.Context) (*storepb.DatabaseSchemaMetad
LinkedDatabases: dbLinks,
}
databaseMetadata.Schemas = append(databaseMetadata.Schemas, &storepb.SchemaMetadata{
- Name: "",
- Tables: tableMap[d.databaseName],
- Views: viewMap[d.databaseName],
- Sequences: sequences,
- Functions: functions,
- Procedures: procedures,
- Packages: packages,
+ Name: "",
+ Tables: tableMap[d.databaseName],
+ Views: viewMap[d.databaseName],
+ MaterializedViews: materializedViews,
+ Sequences: sequences,
+ Functions: functions,
+ Procedures: procedures,
+ Packages: packages,
})
return databaseMetadata, nil
}
@@ -305,7 +310,10 @@ func getTables(txn *sql.Tx, schemaName string, columnMap map[db.TableKey][]*stor
SELECT OWNER, TABLE_NAME, NUM_ROWS
FROM all_tables
WHERE OWNER = '%s'
- ORDER BY TABLE_NAME`, schemaName)
+ AND TABLE_NAME NOT IN (
+ SELECT MVIEW_NAME FROM all_mviews WHERE OWNER = '%s'
+ )
+ ORDER BY TABLE_NAME`, schemaName, schemaName)
slog.Debug("running get tables query")
rows, err := txn.Query(query)
@@ -772,7 +780,14 @@ func getIndexesAndConstraints(txn *sql.Tx, schemaName string) (map[db.TableKey][
return nil, nil, nil, err
}
key := db.IndexKey{Schema: schemaName, Table: tableName, Index: indexName}
- indexColumnMap[key] = append(indexColumnMap[key], columnName)
+
+ // Clean up column name but preserve the original if it's already clean
+ cleanColumnName := columnName
+ if strings.HasPrefix(columnName, "\"") && strings.HasSuffix(columnName, "\"") {
+ cleanColumnName = columnName[1 : len(columnName)-1]
+ }
+
+ indexColumnMap[key] = append(indexColumnMap[key], cleanColumnName)
descendingMap[key] = append(descendingMap[key], descend.String == "DESC")
}
if err := colRows.Err(); err != nil {
@@ -840,22 +855,59 @@ func getIndexesAndConstraints(txn *sql.Tx, schemaName string) (map[db.TableKey][
index.Unique = unique == "UNIQUE"
indexKey := db.IndexKey{Schema: schemaName, Table: tableName, Index: index.Name}
- if index.Type == "FUNCTION-BASED" {
- index.Expressions = indexExpressionMap[indexKey]
- // Skip indexes that reference system-generated virtual columns
- hasVirtualColumn := false
- for _, expr := range index.Expressions {
- if strings.HasPrefix(expr, "SYS_NC") {
- hasVirtualColumn = true
+ // Handle index expressions - preserve Oracle's classification
+ columns := indexColumnMap[indexKey]
+ expressions := indexExpressionMap[indexKey]
+
+ // For function-based indexes (including those with DESC columns)
+ if len(expressions) > 0 {
+ // Combine regular columns with expressions
+ // Oracle reports ASC columns in ALL_IND_COLUMNS and DESC columns as expressions
+ var combinedExpressions []string
+ exprIdx := 0
+
+ for i := 0; i < len(columns); i++ {
+ if strings.HasPrefix(columns[i], "SYS_NC") {
+ // This is a virtual column for a DESC column, use the expression
+ if exprIdx < len(expressions) {
+ combinedExpressions = append(combinedExpressions, expressions[exprIdx])
+ exprIdx++
+ }
+ } else {
+ // Regular column
+ combinedExpressions = append(combinedExpressions, fmt.Sprintf(`"%s"`, columns[i]))
+ }
+ }
+
+ index.Expressions = combinedExpressions
+ } else {
+ // Simple column index without expressions
+ // Skip indexes that reference only system-generated virtual columns
+ hasOnlyVirtualColumns := true
+ for _, col := range columns {
+ if !strings.HasPrefix(col, "SYS_NC") {
+ hasOnlyVirtualColumns = false
break
}
}
- if hasVirtualColumn {
+ if hasOnlyVirtualColumns {
continue
}
- } else {
- index.Expressions = indexColumnMap[indexKey]
- index.Descending = descendingMap[indexKey]
+
+ // For column-based indexes, quote the column names to match expected format
+ quotedColumns := make([]string, len(columns))
+ for i, col := range columns {
+ quotedColumns[i] = fmt.Sprintf(`"%s"`, col)
+ }
+ index.Expressions = quotedColumns
+ }
+ // Note: Keep index.Type as reported by Oracle (don't modify it)
+
+ // Set descending flags for all indexes (both function-based and normal)
+ if desc, ok := descendingMap[indexKey]; ok && len(desc) > 0 {
+ // Always set descending array if we have column information,
+ // even if all columns are ascending (to maintain consistency)
+ index.Descending = desc
}
index.Visible = true
if visibility.Valid && visibility.String == "INVISIBLE" {
@@ -975,6 +1027,105 @@ func getViewDependencies(txn *sql.Tx, schemaName, viewName string) ([]*storepb.D
return result, nil
}
+// getMaterializedViewDependencies gets the dependencies of a materialized view.
+func getMaterializedViewDependencies(txn *sql.Tx, schemaName, viewName string) ([]*storepb.DependencyColumn, error) {
+ var result []*storepb.DependencyColumn
+
+ query := fmt.Sprintf(`
+ SELECT
+ REFERENCED_OWNER as source_schema,
+ REFERENCED_NAME as source_table,
+ '*' as column_name
+ FROM ALL_DEPENDENCIES
+ WHERE OWNER = '%s'
+ AND NAME = '%s'
+ AND TYPE = 'MATERIALIZED VIEW'
+ AND REFERENCED_TYPE IN ('TABLE', 'VIEW')
+ ORDER BY REFERENCED_OWNER, REFERENCED_NAME
+ `, schemaName, viewName)
+
+ rows, err := txn.Query(query)
+ if err != nil {
+ return nil, err
+ }
+ defer rows.Close()
+ for rows.Next() {
+ dependencyColumn := &storepb.DependencyColumn{}
+ if err := rows.Scan(&dependencyColumn.Schema, &dependencyColumn.Table, &dependencyColumn.Column); err != nil {
+ return nil, err
+ }
+ result = append(result, dependencyColumn)
+ }
+ if err := rows.Err(); err != nil {
+ return nil, err
+ }
+
+ return result, nil
+}
+
+// getMaterializedViews gets all materialized views of a database.
+func getMaterializedViews(txn *sql.Tx, schemaName string, _ map[db.TableKey][]*storepb.ColumnMetadata) ([]*storepb.MaterializedViewMetadata, error) {
+ var materializedViews []*storepb.MaterializedViewMetadata
+
+ // Get materialized view comments
+ materializedViewCommentMap, err := getMaterializedViewComments(txn, schemaName)
+ if err != nil {
+ return nil, errors.Wrapf(err, "failed to get materialized view comments")
+ }
+
+ query := fmt.Sprintf(`
+ SELECT OWNER, MVIEW_NAME, QUERY
+ FROM sys.all_mviews
+ WHERE OWNER = '%s'
+ ORDER BY MVIEW_NAME
+ `, schemaName)
+
+ slog.Debug("running get materialized views query")
+ rows, err := txn.Query(query)
+ if err != nil {
+ return nil, util.FormatErrorWithQuery(err, query)
+ }
+ defer rows.Close()
+
+ for rows.Next() {
+ materializedView := &storepb.MaterializedViewMetadata{}
+ var schemaName string
+ if err := rows.Scan(&schemaName, &materializedView.Name, &materializedView.Definition); err != nil {
+ return nil, err
+ }
+
+ // Ensure the definition ends with a newline to match expected format
+ if materializedView.Definition != "" && !strings.HasSuffix(materializedView.Definition, "\n") {
+ materializedView.Definition += "\n"
+ }
+
+ // Add comment if available
+ key := db.TableKey{Schema: schemaName, Table: materializedView.Name}
+ if comment, ok := materializedViewCommentMap[key]; ok {
+ materializedView.Comment = comment
+ }
+
+ materializedViews = append(materializedViews, materializedView)
+ }
+ if err := rows.Err(); err != nil {
+ return nil, util.FormatErrorWithQuery(err, query)
+ }
+ if err := rows.Close(); err != nil {
+ return nil, errors.Wrapf(err, "failed to close rows")
+ }
+
+ // Populate dependencies for each materialized view
+ for _, materializedView := range materializedViews {
+ dependencies, err := getMaterializedViewDependencies(txn, schemaName, materializedView.Name)
+ if err != nil {
+ return nil, errors.Wrapf(err, "failed to get materialized view %q dependencies", materializedView.Name)
+ }
+ materializedView.DependencyColumns = dependencies
+ }
+
+ return materializedViews, nil
+}
+
// getSequences gets all sequences of a database.
func getSequences(txn *sql.Tx, schemaName string) ([]*storepb.SequenceMetadata, error) {
var sequences []*storepb.SequenceMetadata
@@ -1312,7 +1463,6 @@ func getIndexComments(_ *sql.Tx, schemaName string) (map[db.IndexKey]string, err
}
// getMaterializedViewComments gets comments for materialized views from Oracle system views
-// nolint:unused
func getMaterializedViewComments(txn *sql.Tx, schemaName string) (map[db.TableKey]string, error) {
materializedViewCommentMap := make(map[db.TableKey]string)
diff --git a/backend/plugin/schema/oracle/generate_migration.go b/backend/plugin/schema/oracle/generate_migration.go
index 2576545240..9b2c26fcee 100644
--- a/backend/plugin/schema/oracle/generate_migration.go
+++ b/backend/plugin/schema/oracle/generate_migration.go
@@ -1553,15 +1553,41 @@ func writeMigrationIndex(out *strings.Builder, schema, table string, index *stor
_, _ = out.WriteString(table)
_, _ = out.WriteString(`" (`)
- for i, expr := range index.Expressions {
- if i > 0 {
- _, _ = out.WriteString(`, `)
+ // Handle FUNCTION-BASED indexes differently from normal indexes
+ if strings.Contains(index.Type, "FUNCTION-BASED") {
+ for i, expression := range index.Expressions {
+ if i > 0 {
+ _, _ = out.WriteString(`, `)
+ }
+ _, _ = out.WriteString(expression)
+
+ // For function-based indexes, always add explicit ASC/DESC
+ if i < len(index.Descending) && index.Descending[i] {
+ _, _ = out.WriteString(` DESC`)
+ } else {
+ _, _ = out.WriteString(` ASC`)
+ }
}
- _, _ = out.WriteString(expr)
+ } else {
+ for i, column := range index.Expressions {
+ if i > 0 {
+ _, _ = out.WriteString(`, `)
+ }
+ // Remove quotes if they already exist to avoid double quoting
+ cleanColumn := column
+ if strings.HasPrefix(column, `"`) && strings.HasSuffix(column, `"`) {
+ cleanColumn = column[1 : len(column)-1]
+ }
+ _, _ = out.WriteString(`"`)
+ _, _ = out.WriteString(cleanColumn)
+ _, _ = out.WriteString(`"`)
- // Handle descending order if specified
- if i < len(index.Descending) && index.Descending[i] {
- _, _ = out.WriteString(` DESC`)
+ // For normal indexes, add explicit ASC/DESC
+ if i < len(index.Descending) && index.Descending[i] {
+ _, _ = out.WriteString(` DESC`)
+ } else {
+ _, _ = out.WriteString(` ASC`)
+ }
}
}
_, _ = out.WriteString(`)`)
diff --git a/backend/plugin/schema/oracle/get_database_definition.go b/backend/plugin/schema/oracle/get_database_definition.go
index 0bb665031f..08bd7837d0 100644
--- a/backend/plugin/schema/oracle/get_database_definition.go
+++ b/backend/plugin/schema/oracle/get_database_definition.go
@@ -337,15 +337,26 @@ func writeIndex(buf *strings.Builder, table string, index *storepb.IndexMetadata
return err
}
}
- if _, err := buf.WriteString(`"`); err != nil {
- return err
- }
- if _, err := buf.WriteString(column); err != nil {
- return err
- }
- if _, err := buf.WriteString(`"`); err != nil {
- return err
+
+ // Check if column is already quoted to avoid double quoting
+ if strings.HasPrefix(column, `"`) && strings.HasSuffix(column, `"`) {
+ // Column is already quoted, use as-is
+ if _, err := buf.WriteString(column); err != nil {
+ return err
+ }
+ } else {
+ // Column is not quoted, add quotes
+ if _, err := buf.WriteString(`"`); err != nil {
+ return err
+ }
+ if _, err := buf.WriteString(column); err != nil {
+ return err
+ }
+ if _, err := buf.WriteString(`"`); err != nil {
+ return err
+ }
}
+
if i < len(index.Descending) && index.Descending[i] {
if _, err := buf.WriteString(` DESC`); err != nil {
return err
diff --git a/backend/plugin/schema/oracle/get_database_metadata.go b/backend/plugin/schema/oracle/get_database_metadata.go
new file mode 100644
index 0000000000..d8ec0ad1c6
--- /dev/null
+++ b/backend/plugin/schema/oracle/get_database_metadata.go
@@ -0,0 +1,1371 @@
+package oracle
+
+import (
+ "fmt"
+ "slices"
+ "strconv"
+ "strings"
+
+ "github.com/antlr4-go/antlr/v4"
+ parser "github.com/bytebase/plsql-parser"
+ "github.com/pkg/errors"
+
+ oracleparser "github.com/bytebase/bytebase/backend/plugin/parser/plsql"
+ "github.com/bytebase/bytebase/backend/plugin/schema"
+ storepb "github.com/bytebase/bytebase/proto/generated-go/store"
+)
+
+func init() {
+ schema.RegisterGetDatabaseMetadata(storepb.Engine_ORACLE, GetDatabaseMetadata)
+}
+
+// GetDatabaseMetadata parses the Oracle schema text and returns the database metadata.
+func GetDatabaseMetadata(schemaText string) (*storepb.DatabaseSchemaMetadata, error) {
+ tree, _, err := oracleparser.ParsePLSQL(schemaText)
+ if err != nil {
+ return nil, errors.Wrapf(err, "failed to parse Oracle schema")
+ }
+
+ extractor := &metadataExtractor{
+ currentSchema: "",
+ tables: make(map[string]*storepb.TableMetadata),
+ views: make(map[string]*storepb.ViewMetadata),
+ materializedViews: make(map[string]*storepb.MaterializedViewMetadata),
+ functions: make(map[string]*storepb.FunctionMetadata),
+ procedures: make(map[string]*storepb.ProcedureMetadata),
+ triggers: make(map[string]*storepb.TriggerMetadata),
+ sequences: make(map[string]*storepb.SequenceMetadata),
+ packages: make(map[string]*storepb.PackageMetadata),
+ }
+
+ // Walk the parse tree
+ if tree != nil {
+ antlr.ParseTreeWalkerDefault.Walk(extractor, tree)
+ }
+
+ if extractor.err != nil {
+ return nil, extractor.err
+ }
+
+ // Build the final metadata structure
+ schemaMetadata := &storepb.DatabaseSchemaMetadata{
+ Name: extractor.currentDatabase,
+ Schemas: []*storepb.SchemaMetadata{},
+ }
+
+ // Create single schema for Oracle (Oracle uses schemas, not separate databases)
+ schemaName := extractor.currentSchema
+ if schemaName == "" {
+ schemaName = "PUBLIC"
+ }
+
+ schema := &storepb.SchemaMetadata{
+ Name: schemaName,
+ Tables: []*storepb.TableMetadata{},
+ Views: []*storepb.ViewMetadata{},
+ MaterializedViews: []*storepb.MaterializedViewMetadata{},
+ Procedures: []*storepb.ProcedureMetadata{},
+ Functions: []*storepb.FunctionMetadata{},
+ Sequences: []*storepb.SequenceMetadata{},
+ }
+
+ // Sort and add tables (exclude tables that are actually materialized views)
+ var tableNames []string
+ for name := range extractor.tables {
+ // Skip tables that are actually materialized views
+ if _, isMaterializedView := extractor.materializedViews[name]; !isMaterializedView {
+ tableNames = append(tableNames, name)
+ }
+ }
+ slices.Sort(tableNames)
+ for _, name := range tableNames {
+ schema.Tables = append(schema.Tables, extractor.tables[name])
+ }
+
+ // Sort and add views
+ var viewNames []string
+ for name := range extractor.views {
+ viewNames = append(viewNames, name)
+ }
+ slices.Sort(viewNames)
+ for _, name := range viewNames {
+ schema.Views = append(schema.Views, extractor.views[name])
+ }
+
+ // Sort and add materialized views
+ var materializedViewNames []string
+ for name := range extractor.materializedViews {
+ materializedViewNames = append(materializedViewNames, name)
+ }
+ slices.Sort(materializedViewNames)
+ for _, name := range materializedViewNames {
+ schema.MaterializedViews = append(schema.MaterializedViews, extractor.materializedViews[name])
+ }
+
+ // Sort and add functions
+ var functionNames []string
+ for name := range extractor.functions {
+ functionNames = append(functionNames, name)
+ }
+ slices.Sort(functionNames)
+ for _, name := range functionNames {
+ schema.Functions = append(schema.Functions, extractor.functions[name])
+ }
+
+ // Sort and add procedures
+ var procedureNames []string
+ for name := range extractor.procedures {
+ procedureNames = append(procedureNames, name)
+ }
+ slices.Sort(procedureNames)
+ for _, name := range procedureNames {
+ schema.Procedures = append(schema.Procedures, extractor.procedures[name])
+ }
+
+ // Sort and add sequences
+ var sequenceNames []string
+ for name := range extractor.sequences {
+ sequenceNames = append(sequenceNames, name)
+ }
+ slices.Sort(sequenceNames)
+ for _, name := range sequenceNames {
+ schema.Sequences = append(schema.Sequences, extractor.sequences[name])
+ }
+
+ schemaMetadata.Schemas = append(schemaMetadata.Schemas, schema)
+ return schemaMetadata, nil
+}
+
+// metadataExtractor walks the parse tree and extracts Oracle metadata
+type metadataExtractor struct {
+ *parser.BasePlSqlParserListener
+
+ currentDatabase string
+ currentSchema string
+ tables map[string]*storepb.TableMetadata
+ views map[string]*storepb.ViewMetadata
+ materializedViews map[string]*storepb.MaterializedViewMetadata
+ functions map[string]*storepb.FunctionMetadata
+ procedures map[string]*storepb.ProcedureMetadata
+ triggers map[string]*storepb.TriggerMetadata
+ sequences map[string]*storepb.SequenceMetadata
+ packages map[string]*storepb.PackageMetadata
+ err error
+}
+
+// Helper function to get or create table
+func (e *metadataExtractor) getOrCreateTable(tableName string) *storepb.TableMetadata {
+ if table, exists := e.tables[tableName]; exists {
+ return table
+ }
+
+ table := &storepb.TableMetadata{
+ Name: tableName,
+ Columns: []*storepb.ColumnMetadata{},
+ Indexes: []*storepb.IndexMetadata{},
+ ForeignKeys: []*storepb.ForeignKeyMetadata{},
+ CheckConstraints: []*storepb.CheckConstraintMetadata{},
+ Triggers: []*storepb.TriggerMetadata{},
+ Partitions: []*storepb.TablePartitionMetadata{},
+ }
+ e.tables[tableName] = table
+ return table
+}
+
+// EnterCreate_table is called when entering a create table statement
+func (e *metadataExtractor) EnterCreate_table(ctx *parser.Create_tableContext) {
+ if e.err != nil {
+ return
+ }
+
+ if ctx.Table_name() == nil {
+ return
+ }
+
+ // Extract schema and table name
+ if ctx.Schema_name() != nil {
+ e.currentSchema = oracleparser.NormalizeSchemaName(ctx.Schema_name())
+ }
+
+ tableName := oracleparser.NormalizeTableName(ctx.Table_name())
+ if tableName == "" {
+ return
+ }
+
+ table := e.getOrCreateTable(tableName)
+
+ // Extract table elements (columns, constraints)
+ if ctx.Relational_table() != nil {
+ e.extractRelationalTable(ctx.Relational_table(), table)
+ }
+}
+
+// EnterCreate_index is called when entering a create index statement
+func (e *metadataExtractor) EnterCreate_index(ctx *parser.Create_indexContext) {
+ if e.err != nil {
+ return
+ }
+
+ if ctx.Index_name() == nil {
+ return
+ }
+
+ // Extract schema and index name
+ schemaName, indexName := oracleparser.NormalizeIndexName(ctx.Index_name())
+ if indexName == "" {
+ return
+ }
+ if schemaName != "" {
+ e.currentSchema = schemaName
+ }
+
+ // Extract table name from the index definition
+ tableName := ""
+ if ctx.Table_index_clause() != nil && ctx.Table_index_clause().Tableview_name() != nil {
+ tableName = normalizeTableViewName(ctx.Table_index_clause().Tableview_name())
+ } else if ctx.Cluster_index_clause() != nil {
+ // Handle cluster indexes if needed
+ return
+ }
+
+ if tableName == "" {
+ return
+ }
+
+ // Get or create the table
+ table := e.getOrCreateTable(tableName)
+
+ // Create index metadata
+ index := &storepb.IndexMetadata{
+ Name: indexName,
+ Primary: false,
+ Unique: ctx.UNIQUE() != nil,
+ Type: "NORMAL", // Oracle default
+ Expressions: []string{},
+ }
+
+ // Determine index type
+ if ctx.BITMAP() != nil {
+ index.Type = "BITMAP"
+ }
+
+ // Extract index expressions
+ if ctx.Table_index_clause() != nil {
+ e.extractIndexExpressions(ctx.Table_index_clause(), index)
+ }
+
+ table.Indexes = append(table.Indexes, index)
+}
+
+// extractIndexExpressions extracts column expressions from table index clause using ANTLR parser
+func (*metadataExtractor) extractIndexExpressions(ctx parser.ITable_index_clauseContext, index *storepb.IndexMetadata) {
+ if ctx == nil {
+ return
+ }
+
+ isFunctionBased := false
+ var descendingFlags []bool
+
+ // Extract index expressions using ANTLR parser
+ indexExprOptions := ctx.AllIndex_expr_option()
+ for _, exprOption := range indexExprOptions {
+ if exprOption == nil {
+ continue
+ }
+
+ var exprText string
+ isDescending := false
+
+ // Get the index expression
+ if indexExpr := exprOption.Index_expr(); indexExpr != nil {
+ if columnName := indexExpr.Column_name(); columnName != nil {
+ // Simple column reference - extract without quotes
+ _, _, exprText = oracleparser.NormalizeColumnName(columnName)
+ // Remove quotes if present to match database metadata format
+ exprText = strings.Trim(exprText, "\"")
+ } else if expression := indexExpr.Expression(); expression != nil {
+ // Function-based expression
+ exprText = getTextFromContext(expression)
+ isFunctionBased = true
+ }
+ }
+
+ // Check for ASC/DESC modifiers
+ isDescending = exprOption.DESC() != nil
+ // Note: Don't mark simple column DESC as function-based in the parser
+ // Oracle may internally treat it as function-based, but for DDL generation purposes,
+ // we should generate it as a normal index with explicit ASC/DESC modifiers
+
+ // Store the expression (without ASC/DESC modifiers)
+ if exprText != "" {
+ index.Expressions = append(index.Expressions, exprText)
+ descendingFlags = append(descendingFlags, isDescending)
+ }
+ }
+
+ // Set descending flags if any columns have descending order
+ hasDescending := false
+ for _, desc := range descendingFlags {
+ if desc {
+ hasDescending = true
+ break
+ }
+ }
+ if hasDescending {
+ index.Descending = descendingFlags
+ }
+
+ // Update index type for function-based indexes
+ // Oracle treats indexes with DESC columns or functions as "FUNCTION-BASED NORMAL"
+ if (isFunctionBased || hasDescending) && index.Type == "NORMAL" {
+ index.Type = "FUNCTION-BASED NORMAL"
+ }
+}
+
+// EnterCreate_view is called when entering a create view statement
+func (e *metadataExtractor) EnterCreate_view(ctx *parser.Create_viewContext) {
+ if e.err != nil {
+ return
+ }
+
+ // Extract schema and view name
+ schemaName := ""
+ viewName := ""
+
+ if ctx.Schema_name() != nil {
+ schemaName = oracleparser.NormalizeSchemaName(ctx.Schema_name())
+ e.currentSchema = schemaName
+ }
+
+ // Extract view name from GetV() which returns id_expression
+ if ctx.GetV() != nil {
+ viewName = oracleparser.NormalizeIDExpression(ctx.GetV())
+ }
+
+ if viewName == "" {
+ return
+ }
+
+ // Extract the select statement as the view definition
+ definition := ""
+ if ctx.Select_only_statement() != nil {
+ definition = getTextFromContext(ctx.Select_only_statement())
+ }
+
+ view := &storepb.ViewMetadata{
+ Name: viewName,
+ Definition: definition,
+ }
+
+ e.views[viewName] = view
+}
+
+// EnterCreate_materialized_view is called when entering a create materialized view statement
+func (e *metadataExtractor) EnterCreate_materialized_view(ctx *parser.Create_materialized_viewContext) {
+ if e.err != nil {
+ return
+ }
+
+ // Extract materialized view name from tableview_name
+ viewName := ""
+
+ if ctx.Tableview_name() != nil {
+ viewName = normalizeTableViewName(ctx.Tableview_name())
+ }
+
+ if viewName == "" {
+ return
+ }
+
+ // Extract the select statement as the definition
+ definition := ""
+ if ctx.Select_only_statement() != nil {
+ definition = getTextFromContext(ctx.Select_only_statement())
+ // Ensure the definition ends with a newline to match database format
+ if definition != "" && !strings.HasSuffix(definition, "\n") {
+ definition += "\n"
+ }
+ }
+
+ materializedView := &storepb.MaterializedViewMetadata{
+ Name: viewName,
+ Definition: definition,
+ }
+
+ e.materializedViews[viewName] = materializedView
+
+ // Ensure this materialized view is not also treated as a table
+ // Oracle parsing might have triggered table creation first
+ delete(e.tables, viewName)
+}
+
+// EnterCreate_sequence is called when entering a create sequence statement
+func (e *metadataExtractor) EnterCreate_sequence(ctx *parser.Create_sequenceContext) {
+ if e.err != nil {
+ return
+ }
+
+ if ctx.Sequence_name() == nil {
+ return
+ }
+
+ // Extract sequence name
+ sequenceName := normalizeSequenceName(ctx.Sequence_name())
+ if sequenceName == "" {
+ return
+ }
+
+ sequence := &storepb.SequenceMetadata{
+ Name: sequenceName,
+ }
+
+ // Extract sequence options
+ e.extractSequenceOptions(ctx, sequence)
+
+ e.sequences[sequenceName] = sequence
+}
+
+// extractSequenceOptions extracts sequence specification options using ANTLR parser
+func (*metadataExtractor) extractSequenceOptions(ctx *parser.Create_sequenceContext, sequence *storepb.SequenceMetadata) {
+ // Extract START WITH clause using ANTLR parser
+ startClauses := ctx.AllSequence_start_clause()
+ for _, startClause := range startClauses {
+ if startClause.START() != nil && startClause.WITH() != nil && startClause.UNSIGNED_INTEGER() != nil {
+ startValue := startClause.UNSIGNED_INTEGER().GetText()
+ if startValue != "" {
+ // Store all start values - Oracle metadata may not match DDL
+ sequence.Start = startValue
+ break
+ }
+ }
+ }
+
+ // Extract sequence specifications using ANTLR parser
+ sequenceSpecs := ctx.AllSequence_spec()
+ for _, spec := range sequenceSpecs {
+ // Extract INCREMENT BY value
+ if spec.INCREMENT() != nil && spec.BY() != nil && spec.UNSIGNED_INTEGER() != nil {
+ incrementValue := spec.UNSIGNED_INTEGER().GetText()
+ if incrementValue != "" && incrementValue != "1" {
+ // Only store non-default increment values (Oracle defaults to 1)
+ sequence.Increment = incrementValue
+ }
+ }
+
+ // We could extract other sequence properties here if needed:
+ // - MAXVALUE/NOMAXVALUE
+ // - MINVALUE/NOMINVALUE
+ // - CYCLE/NOCYCLE
+ // - CACHE/NOCACHE
+ // - ORDER/NOORDER
+ }
+}
+
+// EnterCreate_function_body is called when entering a create function statement
+func (e *metadataExtractor) EnterCreate_function_body(ctx *parser.Create_function_bodyContext) {
+ if e.err != nil {
+ return
+ }
+
+ if ctx.Function_name() == nil {
+ return
+ }
+
+ // Extract function name
+ functionName := normalizeFunctionName(ctx.Function_name())
+ if functionName == "" {
+ return
+ }
+
+ // Extract the function body and ensure it has proper formatting
+ definition := getTextFromContext(ctx)
+
+ // Clean up the definition to match expected format
+ // Remove CREATE OR REPLACE prefix if present
+ if strings.HasPrefix(strings.ToUpper(definition), "CREATE OR REPLACE ") {
+ definition = definition[18:] // Remove "CREATE OR REPLACE "
+ } else if strings.HasPrefix(strings.ToUpper(definition), "CREATE ") {
+ definition = definition[7:] // Remove "CREATE "
+ }
+
+ // Ensure definition starts with FUNCTION keyword
+ if !strings.HasPrefix(strings.ToUpper(definition), "FUNCTION") {
+ definition = "FUNCTION " + definition
+ }
+
+ function := &storepb.FunctionMetadata{
+ Name: functionName,
+ Definition: definition,
+ }
+
+ e.functions[functionName] = function
+}
+
+// EnterCreate_procedure_body is called when entering a create procedure statement
+func (e *metadataExtractor) EnterCreate_procedure_body(ctx *parser.Create_procedure_bodyContext) {
+ if e.err != nil {
+ return
+ }
+
+ if ctx.Procedure_name() == nil {
+ return
+ }
+
+ // Extract procedure name
+ procedureName := normalizeProcedureName(ctx.Procedure_name())
+ if procedureName == "" {
+ return
+ }
+
+ // Extract the procedure body
+ definition := getTextFromContext(ctx)
+
+ procedure := &storepb.ProcedureMetadata{
+ Name: procedureName,
+ Definition: definition,
+ }
+
+ e.procedures[procedureName] = procedure
+}
+
+// extractRelationalTable extracts columns and constraints from relational table definition
+func (e *metadataExtractor) extractRelationalTable(ctx parser.IRelational_tableContext, table *storepb.TableMetadata) {
+ if ctx == nil {
+ return
+ }
+
+ // Extract all relational properties
+ for _, prop := range ctx.AllRelational_property() {
+ if prop == nil {
+ continue
+ }
+
+ switch {
+ case prop.Column_definition() != nil:
+ column := e.extractColumnDefinition(prop.Column_definition())
+ if column != nil {
+ // Set position based on current column count (1-indexed)
+ column.Position = int32(len(table.Columns) + 1)
+ table.Columns = append(table.Columns, column)
+ }
+ case prop.Virtual_column_definition() != nil:
+ column := e.extractVirtualColumnDefinition(prop.Virtual_column_definition())
+ if column != nil {
+ // Set position based on current column count (1-indexed)
+ column.Position = int32(len(table.Columns) + 1)
+ table.Columns = append(table.Columns, column)
+ }
+ case prop.Out_of_line_constraint() != nil:
+ e.extractOutOfLineConstraint(prop.Out_of_line_constraint(), table)
+ case prop.Out_of_line_ref_constraint() != nil:
+ e.extractOutOfLineRefConstraint(prop.Out_of_line_ref_constraint(), table)
+ }
+ }
+}
+
+// extractColumnDefinition extracts column metadata from column definition
+func (e *metadataExtractor) extractColumnDefinition(ctx parser.IColumn_definitionContext) *storepb.ColumnMetadata {
+ if ctx == nil || ctx.Column_name() == nil {
+ return nil
+ }
+
+ // Extract column name
+ columnName := normalizeColumnName(ctx.Column_name())
+ if columnName == "" {
+ return nil
+ }
+
+ column := &storepb.ColumnMetadata{
+ Name: columnName,
+ Type: "VARCHAR2(100)", // Default type
+ Nullable: true, // Default to nullable
+ }
+
+ // Extract data type
+ if ctx.Datatype() != nil {
+ column.Type = e.extractDataType(ctx.Datatype())
+ } else if ctx.Regular_id() != nil {
+ // Handle user-defined types
+ column.Type = strings.ToUpper(ctx.Regular_id().GetText())
+ }
+
+ // Extract default value using ANTLR parser
+ if ctx.DEFAULT() != nil {
+ if expr := ctx.Expression(); expr != nil {
+ column.DefaultExpression = getTextFromContext(expr)
+ }
+ }
+
+ // Extract collation if specified
+ if ctx.COLLATE() != nil && ctx.Column_collation_name() != nil {
+ if collationName := ctx.Column_collation_name().Id_expression(); collationName != nil {
+ column.Collation = oracleparser.NormalizeIDExpression(collationName)
+ }
+ }
+
+ // Extract identity clause
+ if ctx.Identity_clause() != nil {
+ e.extractIdentityClause(ctx.Identity_clause(), column)
+ }
+
+ // Extract inline constraints
+ for _, constraint := range ctx.AllInline_constraint() {
+ e.extractInlineConstraint(constraint, column)
+ }
+
+ // Check visibility - don't add to comment as this is structural info
+ if ctx.INVISIBLE() != nil {
+ // TODO: Set invisible column flag when available in proto
+ _ = ctx.INVISIBLE() // Acknowledge the context for now
+ }
+
+ return column
+}
+
+// extractVirtualColumnDefinition extracts virtual column metadata
+func (e *metadataExtractor) extractVirtualColumnDefinition(ctx parser.IVirtual_column_definitionContext) *storepb.ColumnMetadata {
+ if ctx == nil || ctx.Column_name() == nil {
+ return nil
+ }
+
+ columnName := normalizeColumnName(ctx.Column_name())
+ if columnName == "" {
+ return nil
+ }
+
+ column := &storepb.ColumnMetadata{
+ Name: columnName,
+ Type: "NUMBER", // Default type for virtual columns
+ Nullable: true,
+ }
+
+ // Extract data type if specified
+ if ctx.Datatype() != nil {
+ column.Type = e.extractDataType(ctx.Datatype())
+ }
+
+ // Mark as virtual and extract expression from context
+ // Virtual columns have expressions like: col_name AS (expression)
+ text := getTextFromContext(ctx)
+ if asIdx := strings.Index(strings.ToUpper(text), "AS"); asIdx != -1 {
+ // Find the expression in parentheses
+ remaining := text[asIdx+2:]
+ if openIdx := strings.Index(remaining, "("); openIdx != -1 {
+ // Find matching closing parenthesis
+ parenCount := 1
+ i := openIdx + 1
+ for i < len(remaining) && parenCount > 0 {
+ switch remaining[i] {
+ case '(':
+ parenCount++
+ case ')':
+ parenCount--
+ }
+ i++
+ }
+ if parenCount == 0 {
+ column.DefaultExpression = remaining[openIdx+1 : i-1]
+ }
+ }
+ }
+
+ // Mark as virtual column - don't add to comment as this is structural info
+ // TODO: Set virtual column flag when available in proto
+
+ // Extract inline constraints
+ for _, constraint := range ctx.AllInline_constraint() {
+ e.extractInlineConstraint(constraint, column)
+ }
+
+ return column
+}
+
+// extractDataType extracts Oracle data type using ANTLR parser with fallback to text
+func (*metadataExtractor) extractDataType(ctx parser.IDatatypeContext) string {
+ if ctx == nil {
+ return ""
+ }
+
+ // For now, use the original approach to get full text and normalize it
+ // The ANTLR parsing of complex types like INTERVAL and TIMESTAMP WITH TIME ZONE
+ // requires more detailed grammar understanding than currently implemented
+ return normalizeDataTypeText(getTextFromContext(ctx))
+}
+
+// extractInlineConstraint extracts inline column constraints
+func (*metadataExtractor) extractInlineConstraint(ctx parser.IInline_constraintContext, column *storepb.ColumnMetadata) {
+ if ctx == nil {
+ return
+ }
+
+ // Handle NOT NULL constraint
+ if ctx.NOT() != nil && ctx.NULL_() != nil {
+ column.Nullable = false
+ }
+
+ // Handle NULL constraint (explicitly nullable)
+ if ctx.NULL_() != nil && ctx.NOT() == nil {
+ column.Nullable = true
+ }
+
+ // Handle PRIMARY KEY constraint
+ if ctx.PRIMARY() != nil && ctx.KEY() != nil {
+ column.Nullable = false
+ // Don't add any comment for primary key - this is structural information
+ }
+
+ // Handle UNIQUE constraint
+ if ctx.UNIQUE() != nil {
+ // Note: UNIQUE columns can be nullable in Oracle
+ // Don't add any comment for unique constraint - this is structural information
+ _ = ctx.UNIQUE() // Acknowledge the context
+ }
+
+ // Handle CHECK constraint - check if the text contains CHECK keyword
+ text := strings.ToUpper(getTextFromContext(ctx))
+ if strings.Contains(text, "CHECK") {
+ // Check constraints are parsed but not added to column comments
+ // This is structural information handled elsewhere
+ _ = text // Acknowledge we checked the text
+ }
+
+ // Handle REFERENCES (inline foreign key)
+ if ctx.References_clause() != nil {
+ // Don't add foreign key info to comment - this is structural information
+ _ = ctx.References_clause() // Acknowledge the context
+ }
+}
+
+// extractIdentityClause extracts identity column information
+func (*metadataExtractor) extractIdentityClause(ctx parser.IIdentity_clauseContext, column *storepb.ColumnMetadata) {
+ if ctx == nil {
+ return
+ }
+
+ // Mark as identity column
+ if ctx.ALWAYS() != nil {
+ column.IsIdentity = true
+ // TODO: Set identity generation type when proto is updated
+ } else if ctx.BY() != nil && ctx.DEFAULT() != nil {
+ column.IsIdentity = true
+ // TODO: Set identity generation type when proto is updated
+ }
+ // Don't add identity info to comment - this is structural information
+
+ // Identity columns are NOT NULL by default
+ column.Nullable = false
+}
+
+// extractOutOfLineConstraint extracts out-of-line table constraints
+func (e *metadataExtractor) extractOutOfLineConstraint(ctx parser.IOut_of_line_constraintContext, table *storepb.TableMetadata) {
+ if ctx == nil {
+ return
+ }
+
+ // Extract constraint name
+ constraintName := ""
+ if ctx.Constraint_name() != nil {
+ constraintName = normalizeConstraintName(ctx.Constraint_name())
+ }
+
+ // Handle different constraint types
+ switch {
+ case ctx.PRIMARY() != nil && ctx.KEY() != nil:
+ e.extractPrimaryKeyConstraint(ctx, table, constraintName)
+ case ctx.UNIQUE() != nil:
+ e.extractUniqueConstraint(ctx, table, constraintName)
+ case ctx.CHECK() != nil:
+ e.extractCheckConstraint(ctx, table, constraintName)
+ }
+}
+
+// extractOutOfLineRefConstraint extracts out-of-line foreign key constraints
+func (e *metadataExtractor) extractOutOfLineRefConstraint(ctx parser.IOut_of_line_ref_constraintContext, table *storepb.TableMetadata) {
+ if ctx == nil {
+ return
+ }
+
+ // Extract constraint name
+ constraintName := ""
+ if ctx.Constraint_name() != nil {
+ constraintName = normalizeConstraintName(ctx.Constraint_name())
+ }
+
+ if ctx.FOREIGN() != nil && ctx.KEY() != nil {
+ e.extractForeignKeyConstraint(ctx, table, constraintName)
+ }
+}
+
+// extractPrimaryKeyConstraint extracts primary key constraint
+func (*metadataExtractor) extractPrimaryKeyConstraint(ctx parser.IOut_of_line_constraintContext, table *storepb.TableMetadata, constraintName string) {
+ if constraintName == "" {
+ constraintName = fmt.Sprintf("PK_%s", table.Name)
+ }
+
+ // Extract column names
+ var columns []string
+ for _, colName := range ctx.AllColumn_name() {
+ if normalized := normalizeColumnName(colName); normalized != "" {
+ columns = append(columns, normalized)
+ }
+ }
+
+ if len(columns) == 0 {
+ return
+ }
+
+ // Create primary key index
+ index := &storepb.IndexMetadata{
+ Name: constraintName,
+ Primary: true,
+ Unique: true,
+ Type: "NORMAL",
+ Expressions: columns,
+ }
+
+ table.Indexes = append(table.Indexes, index)
+
+ // Mark columns as NOT NULL
+ for _, col := range table.Columns {
+ for _, pkCol := range columns {
+ if col.Name == pkCol {
+ col.Nullable = false
+ break
+ }
+ }
+ }
+}
+
+// extractUniqueConstraint extracts unique constraint
+func (*metadataExtractor) extractUniqueConstraint(ctx parser.IOut_of_line_constraintContext, table *storepb.TableMetadata, constraintName string) {
+ if constraintName == "" {
+ constraintName = fmt.Sprintf("UK_%s_%d", table.Name, len(table.Indexes)+1)
+ }
+
+ // Extract column names
+ var columns []string
+ for _, colName := range ctx.AllColumn_name() {
+ if normalized := normalizeColumnName(colName); normalized != "" {
+ columns = append(columns, normalized)
+ }
+ }
+
+ if len(columns) == 0 {
+ return
+ }
+
+ // Create unique index
+ index := &storepb.IndexMetadata{
+ Name: constraintName,
+ Primary: false,
+ Unique: true,
+ Type: "NORMAL",
+ Expressions: columns,
+ }
+
+ table.Indexes = append(table.Indexes, index)
+}
+
+// extractCheckConstraint extracts check constraint using ANTLR parser
+func (*metadataExtractor) extractCheckConstraint(ctx parser.IOut_of_line_constraintContext, table *storepb.TableMetadata, constraintName string) {
+ if constraintName == "" {
+ constraintName = fmt.Sprintf("CHK_%s_%d", table.Name, len(table.CheckConstraints)+1)
+ }
+
+ // Extract check condition using ANTLR parser
+ if ctx.CHECK() != nil {
+ if condition := ctx.Condition(); condition != nil {
+ check := &storepb.CheckConstraintMetadata{
+ Name: constraintName,
+ Expression: getTextFromContext(condition),
+ }
+ table.CheckConstraints = append(table.CheckConstraints, check)
+ }
+ }
+}
+
+// extractForeignKeyConstraint extracts foreign key constraint
+func (e *metadataExtractor) extractForeignKeyConstraint(ctx parser.IOut_of_line_ref_constraintContext, table *storepb.TableMetadata, constraintName string) {
+ if constraintName == "" {
+ constraintName = fmt.Sprintf("FK_%s_%d", table.Name, len(table.ForeignKeys)+1)
+ }
+
+ // Extract local columns from the context
+ var columns []string
+ // The foreign key columns are usually specified after FOREIGN KEY
+ text := getTextFromContext(ctx)
+ if fkIdx := strings.Index(strings.ToUpper(text), "FOREIGNKEY"); fkIdx != -1 {
+ // Find the column list in parentheses
+ if openIdx := strings.Index(text[fkIdx:], "("); openIdx != -1 {
+ if closeIdx := strings.Index(text[fkIdx+openIdx:], ")"); closeIdx != -1 {
+ colList := text[fkIdx+openIdx+1 : fkIdx+openIdx+closeIdx]
+ for _, col := range strings.Split(colList, ",") {
+ col = strings.TrimSpace(col)
+ col = strings.Trim(col, "\"")
+ if col != "" {
+ columns = append(columns, strings.ToUpper(col))
+ }
+ }
+ }
+ }
+ }
+
+ if len(columns) == 0 || ctx.References_clause() == nil {
+ return
+ }
+
+ // Extract referenced table and columns
+ referencedTable, referencedColumns := e.extractReferencesClause(ctx.References_clause())
+ if referencedTable == "" || len(referencedColumns) == 0 {
+ return
+ }
+
+ foreignKey := &storepb.ForeignKeyMetadata{
+ Name: constraintName,
+ Columns: columns,
+ ReferencedTable: referencedTable,
+ ReferencedColumns: referencedColumns,
+ }
+
+ // Extract ON DELETE action from the text
+ text = strings.ToUpper(getTextFromContext(ctx))
+ if strings.Contains(text, "ONDELETECASCADE") {
+ foreignKey.OnDelete = "CASCADE"
+ } else if strings.Contains(text, "ONDELETESETNULL") {
+ foreignKey.OnDelete = "SET NULL"
+ }
+
+ table.ForeignKeys = append(table.ForeignKeys, foreignKey)
+}
+
+// extractReferencesClause extracts referenced table and columns from references clause
+func (*metadataExtractor) extractReferencesClause(ctx parser.IReferences_clauseContext) (string, []string) {
+ if ctx == nil || ctx.Tableview_name() == nil {
+ return "", nil
+ }
+
+ // Extract referenced table name
+ referencedTable := normalizeTableViewName(ctx.Tableview_name())
+
+ // Extract referenced columns from parentheses after table name
+ var referencedColumns []string
+ text := getTextFromContext(ctx)
+ tableEnd := strings.Index(text, referencedTable) + len(referencedTable)
+ if tableEnd < len(text) {
+ remaining := text[tableEnd:]
+ if openIdx := strings.Index(remaining, "("); openIdx != -1 {
+ if closeIdx := strings.Index(remaining[openIdx:], ")"); closeIdx != -1 {
+ colList := remaining[openIdx+1 : openIdx+closeIdx]
+ for _, col := range strings.Split(colList, ",") {
+ col = strings.TrimSpace(col)
+ col = strings.Trim(col, "\"")
+ if col != "" {
+ referencedColumns = append(referencedColumns, strings.ToUpper(col))
+ }
+ }
+ }
+ }
+ }
+
+ // If no columns specified, default to primary key columns (Oracle behavior)
+ if len(referencedColumns) == 0 {
+ referencedColumns = []string{"ID"} // Default assumption
+ }
+
+ return referencedTable, referencedColumns
+}
+
+// normalizeColumnName extracts and normalizes column name
+func normalizeColumnName(ctx parser.IColumn_nameContext) string {
+ if ctx == nil {
+ return ""
+ }
+
+ // Extract all parts of the column name
+ var parts []string
+ if ctx.Identifier() != nil {
+ parts = append(parts, oracleparser.NormalizeIdentifierContext(ctx.Identifier()))
+ }
+ for _, idExpr := range ctx.AllId_expression() {
+ parts = append(parts, oracleparser.NormalizeIDExpression(idExpr))
+ }
+
+ // Return the last part as the column name
+ if len(parts) > 0 {
+ return parts[len(parts)-1]
+ }
+ return ""
+}
+
+// normalizeConstraintName extracts and normalizes constraint name
+func normalizeConstraintName(ctx parser.IConstraint_nameContext) string {
+ if ctx == nil {
+ return ""
+ }
+
+ // Extract all parts of the constraint name
+ var parts []string
+ if ctx.Identifier() != nil {
+ parts = append(parts, oracleparser.NormalizeIdentifierContext(ctx.Identifier()))
+ }
+ for _, idExpr := range ctx.AllId_expression() {
+ parts = append(parts, oracleparser.NormalizeIDExpression(idExpr))
+ }
+
+ // Return the full constraint name
+ return strings.Join(parts, ".")
+}
+
+// normalizeTableViewName extracts and normalizes table/view name
+func normalizeTableViewName(ctx parser.ITableview_nameContext) string {
+ if ctx == nil {
+ return ""
+ }
+
+ // Extract the table/view name parts
+ var parts []string
+ if ctx.Identifier() != nil {
+ parts = append(parts, oracleparser.NormalizeIdentifierContext(ctx.Identifier()))
+ }
+ if ctx.Id_expression() != nil {
+ parts = append(parts, oracleparser.NormalizeIDExpression(ctx.Id_expression()))
+ }
+
+ // Return the last part as the table/view name
+ if len(parts) > 0 {
+ return parts[len(parts)-1]
+ }
+ return ""
+}
+
+// normalizeSequenceName extracts and normalizes sequence name
+func normalizeSequenceName(ctx parser.ISequence_nameContext) string {
+ if ctx == nil {
+ return ""
+ }
+
+ // Extract all parts of the sequence name
+ var parts []string
+ for _, idExpr := range ctx.AllId_expression() {
+ parts = append(parts, oracleparser.NormalizeIDExpression(idExpr))
+ }
+
+ // Return the last part as the sequence name
+ if len(parts) > 0 {
+ return parts[len(parts)-1]
+ }
+ return ""
+}
+
+// normalizeFunctionName extracts and normalizes function name
+func normalizeFunctionName(ctx parser.IFunction_nameContext) string {
+ if ctx == nil {
+ return ""
+ }
+
+ // Function name is a simple identifier or schema.function format
+ // Get the text and parse it
+ text := getTextFromContext(ctx)
+ parts := strings.Split(text, ".")
+ for i, part := range parts {
+ parts[i] = strings.ToUpper(strings.Trim(part, "\""))
+ }
+
+ // Return the last part as the function name
+ if len(parts) > 0 {
+ return parts[len(parts)-1]
+ }
+ return ""
+}
+
+// normalizeProcedureName extracts and normalizes procedure name
+func normalizeProcedureName(ctx parser.IProcedure_nameContext) string {
+ if ctx == nil {
+ return ""
+ }
+
+ // Procedure name is a simple identifier or schema.procedure format
+ // Get the text and parse it
+ text := getTextFromContext(ctx)
+ parts := strings.Split(text, ".")
+ for i, part := range parts {
+ parts[i] = strings.ToUpper(strings.Trim(part, "\""))
+ }
+
+ // Return the last part as the procedure name
+ if len(parts) > 0 {
+ return parts[len(parts)-1]
+ }
+ return ""
+}
+
+// EnterComment_on_table is called when entering a comment on table statement
+func (e *metadataExtractor) EnterComment_on_table(ctx *parser.Comment_on_tableContext) {
+ if e.err != nil {
+ return
+ }
+
+ if ctx.Tableview_name() == nil {
+ return
+ }
+
+ // Extract table name
+ tableName := normalizeTableViewName(ctx.Tableview_name())
+ if tableName == "" {
+ return
+ }
+
+ // Extract comment text
+ comment := ""
+ if ctx.Quoted_string() != nil {
+ comment = oracleparser.NormalizeQuotedString(ctx.Quoted_string())
+ }
+
+ if comment != "" {
+ if table, exists := e.tables[tableName]; exists {
+ table.Comment = comment
+ }
+ }
+}
+
+// EnterComment_on_column is called when entering a comment on column statement
+func (e *metadataExtractor) EnterComment_on_column(ctx *parser.Comment_on_columnContext) {
+ if e.err != nil {
+ return
+ }
+
+ if ctx.Column_name() == nil {
+ return
+ }
+
+ // Extract table and column names from the column_name context
+ // Column name can be in format: table.column or just column
+ parts := []string{}
+ if ctx.Column_name().Identifier() != nil {
+ parts = append(parts, oracleparser.NormalizeIdentifierContext(ctx.Column_name().Identifier()))
+ }
+ for _, idExpr := range ctx.Column_name().AllId_expression() {
+ parts = append(parts, oracleparser.NormalizeIDExpression(idExpr))
+ }
+
+ var tableName, columnName string
+ switch len(parts) {
+ case 1:
+ // Just column name, need to find the table
+ // In this case, we might need to use the most recently created table
+ // or handle it differently based on context
+ return
+ case 2:
+ // table.column format
+ tableName = parts[0]
+ columnName = parts[1]
+ case 3:
+ // schema.table.column format
+ tableName = parts[1]
+ columnName = parts[2]
+ default:
+ return
+ }
+
+ // Extract comment text
+ comment := ""
+ if ctx.Quoted_string() != nil {
+ comment = oracleparser.NormalizeQuotedString(ctx.Quoted_string())
+ }
+
+ if tableName != "" && columnName != "" && comment != "" {
+ if table, exists := e.tables[tableName]; exists {
+ for _, col := range table.Columns {
+ if col.Name == columnName {
+ // Set the actual user comment
+ col.Comment = comment
+ break
+ }
+ }
+ }
+ }
+}
+
+// normalizeDataTypeText normalizes Oracle data type text to match database output format
+func normalizeDataTypeText(text string) string {
+ if text == "" {
+ return ""
+ }
+
+ // Convert to uppercase but preserve the precision/scale information
+ normalized := strings.ToUpper(text)
+
+ // Handle special cases for Oracle data types
+ switch {
+ case strings.HasPrefix(normalized, "VARCHAR2"):
+ // Handle VARCHAR2 with BYTE/CHAR specifiers
+ if strings.HasPrefix(normalized, "VARCHAR2(") && strings.HasSuffix(normalized, ")") {
+ start := strings.Index(normalized, "(") + 1
+ end := strings.LastIndex(normalized, ")")
+ if start < end {
+ sizeStr := normalized[start:end]
+ if size := parseInt(sizeStr); size > 0 {
+ // Oracle defaults to BYTE semantics, add BYTE specifier
+ return fmt.Sprintf("VARCHAR2(%d BYTE)", size)
+ }
+ }
+ }
+ return normalized
+ case strings.HasPrefix(normalized, "CHAR("):
+ // Handle CHAR with BYTE/CHAR specifiers
+ if strings.HasSuffix(normalized, ")") {
+ start := strings.Index(normalized, "(") + 1
+ end := strings.LastIndex(normalized, ")")
+ if start < end {
+ sizeStr := normalized[start:end]
+ if size := parseInt(sizeStr); size > 0 {
+ // Oracle defaults to BYTE semantics, add BYTE specifier
+ return fmt.Sprintf("CHAR(%d BYTE)", size)
+ }
+ }
+ }
+ return normalized
+ case strings.HasPrefix(normalized, "TIMESTAMP"):
+ // Handle TIMESTAMP variations and apply Oracle defaults
+ // Check for both spaced and non-spaced formats
+ if strings.Contains(normalized, "WITH LOCAL TIME ZONE") || strings.Contains(normalized, "WITHLOCALTIMEZONE") {
+ // Handle TIMESTAMP WITH LOCAL TIME ZONE
+ if normalized == "TIMESTAMP WITH LOCAL TIME ZONE" {
+ return "TIMESTAMP(6) WITH LOCAL TIME ZONE"
+ }
+ // If it already has precision, keep proper spacing
+ if strings.Contains(normalized, "(") && strings.Contains(normalized, ")") {
+ // Ensure proper spacing
+ result := strings.ReplaceAll(normalized, "WITHLOCALTIMEZONE", " WITH LOCAL TIME ZONE")
+ return result
+ }
+ return "TIMESTAMP(6) WITH LOCAL TIME ZONE"
+ } else if strings.Contains(normalized, "WITH TIME ZONE") || strings.Contains(normalized, "WITHTIMEZONE") {
+ // Handle TIMESTAMP WITH TIME ZONE
+ if normalized == "TIMESTAMP WITH TIME ZONE" {
+ return "TIMESTAMP(6) WITH TIME ZONE"
+ }
+ // If it already has precision, keep proper spacing
+ if strings.Contains(normalized, "(") && strings.Contains(normalized, ")") {
+ // Ensure proper spacing
+ result := strings.ReplaceAll(normalized, "WITHTIMEZONE", " WITH TIME ZONE")
+ return result
+ }
+ return "TIMESTAMP(6) WITH TIME ZONE"
+ } else if normalized == "TIMESTAMP" {
+ // Plain TIMESTAMP gets default precision
+ return "TIMESTAMP(6)"
+ }
+ return normalized
+ case strings.HasPrefix(normalized, "INTERVAL"):
+ // Handle INTERVAL types with proper spacing and precision
+ // Check for both spaced and non-spaced formats
+ if strings.Contains(normalized, "YEAR TO MONTH") || strings.Contains(normalized, "YEARTOMONTH") {
+ // Handle INTERVAL YEAR TO MONTH
+ if normalized == "INTERVAL YEAR TO MONTH" {
+ return "INTERVAL YEAR(2) TO MONTH"
+ }
+ // If it already has precision, keep proper spacing
+ if strings.Contains(normalized, "(") && strings.Contains(normalized, ")") {
+ // Ensure proper spacing
+ result := strings.ReplaceAll(normalized, "YEARTOMONTH", " YEAR TO MONTH")
+ result = strings.ReplaceAll(result, "INTERVALYEAR", "INTERVAL YEAR")
+ return result
+ }
+ return "INTERVAL YEAR(2) TO MONTH"
+ } else if strings.Contains(normalized, "DAY TO SECOND") || strings.Contains(normalized, "DAYTOSECOND") {
+ // Handle INTERVAL DAY TO SECOND
+ if normalized == "INTERVAL DAY TO SECOND" {
+ return "INTERVAL DAY(2) TO SECOND(6)"
+ }
+ // If it already has precision, keep proper spacing
+ if strings.Contains(normalized, "(") && strings.Contains(normalized, ")") {
+ // Ensure proper spacing
+ result := strings.ReplaceAll(normalized, "DAYTOSECOND", " DAY TO SECOND")
+ result = strings.ReplaceAll(result, "INTERVALDAY", "INTERVAL DAY")
+ return result
+ }
+ return "INTERVAL DAY(2) TO SECOND(6)"
+ }
+ return normalized
+ case strings.HasPrefix(normalized, "LONGRAW"):
+ return "LONG RAW"
+ case strings.HasPrefix(normalized, "DOUBLE"):
+ return "DOUBLE PRECISION"
+ case normalized == "UROWID":
+ // Oracle's default UROWID size is 4000
+ return "UROWID(4000)"
+ case normalized == "FLOAT":
+ // Oracle's default FLOAT precision is 126
+ return "FLOAT(126)"
+ case strings.HasPrefix(normalized, "NVARCHAR2"):
+ // Handle Oracle NVARCHAR2 type expansion - Oracle expands sizes based on character set
+ // For AL16UTF16: NVARCHAR2(2000) becomes NVARCHAR2(4000) in the database
+ // We need to expand parsed DDL sizes to match what Oracle stores
+ if strings.HasPrefix(normalized, "NVARCHAR2(") && strings.HasSuffix(normalized, ")") {
+ start := strings.Index(normalized, "(") + 1
+ end := strings.LastIndex(normalized, ")")
+ if start < end {
+ sizeStr := normalized[start:end]
+ if size := parseInt(sizeStr); size > 0 {
+ // Expand to match Oracle's internal storage
+ // Oracle doubles the size for AL16UTF16 character set
+ if size == 2000 {
+ return "NVARCHAR2(4000)"
+ } else if size <= 2000 {
+ return fmt.Sprintf("NVARCHAR2(%d)", size*2)
+ }
+ }
+ }
+ }
+ return normalized
+ case strings.HasPrefix(normalized, "NCHAR"):
+ // Handle Oracle NCHAR type expansion - Oracle expands NCHAR sizes based on character set
+ // AL16UTF16 uses 2 bytes per character, so NCHAR(100) becomes NCHAR(200) in the database
+ // We need to expand parsed DDL sizes to match what Oracle stores
+ if strings.HasPrefix(normalized, "NCHAR(") && strings.HasSuffix(normalized, ")") {
+ start := strings.Index(normalized, "(") + 1
+ end := strings.LastIndex(normalized, ")")
+ if start < end {
+ sizeStr := normalized[start:end]
+ if size := parseInt(sizeStr); size > 0 {
+ // Expand to match Oracle's internal storage (double for AL16UTF16)
+ return fmt.Sprintf("NCHAR(%d)", size*2)
+ }
+ }
+ }
+ return normalized
+ default:
+ return normalized
+ }
+}
+
+// parseInt helper function to parse integer from string
+func parseInt(s string) int {
+ if i, err := strconv.Atoi(s); err == nil {
+ return i
+ }
+ return 0
+}
+
+// getTextFromContext gets text from ANTLR context preserving original spacing
+func getTextFromContext(ctx any) string {
+ // Try to get parser-aware context first
+ if parserCtx, ok := ctx.(interface {
+ GetParser() antlr.Parser
+ }); ok {
+ parser := parserCtx.GetParser()
+ if parser != nil {
+ if ruleCtx, ok := ctx.(antlr.RuleContext); ok {
+ return parser.GetTokenStream().GetTextFromRuleContext(ruleCtx)
+ }
+ }
+ }
+
+ // Fallback to GetText() for other contexts
+ if textCtx, ok := ctx.(interface{ GetText() string }); ok {
+ return textCtx.GetText()
+ }
+
+ return ""
+}
diff --git a/backend/plugin/schema/oracle/get_database_metadata_test.go b/backend/plugin/schema/oracle/get_database_metadata_test.go
new file mode 100644
index 0000000000..da5113f4b1
--- /dev/null
+++ b/backend/plugin/schema/oracle/get_database_metadata_test.go
@@ -0,0 +1,1504 @@
+package oracle
+
+import (
+ "context"
+ "fmt"
+ "strings"
+ "testing"
+
+ "github.com/stretchr/testify/require"
+
+ "github.com/bytebase/bytebase/backend/common/testcontainer"
+ "github.com/bytebase/bytebase/backend/plugin/db"
+ oracledb "github.com/bytebase/bytebase/backend/plugin/db/oracle"
+ storepb "github.com/bytebase/bytebase/proto/generated-go/store"
+)
+
+// TestGetDatabaseMetadataWithTestcontainer tests the get_database_metadata function
+// by comparing its output with the metadata retrieved from a real Oracle instance.
+func TestGetDatabaseMetadataWithTestcontainer(t *testing.T) {
+ ctx := context.Background()
+
+ // Start Oracle container
+ container := testcontainer.GetTestOracleContainer(ctx, t)
+ defer container.Close(ctx)
+
+ host := container.GetHost()
+ port := container.GetPort()
+
+ // Create Oracle driver for metadata sync
+ driver := &oracledb.Driver{}
+ config := db.ConnectionConfig{
+ DataSource: &storepb.DataSource{
+ Type: storepb.DataSourceType_ADMIN,
+ Username: "testuser",
+ Host: host,
+ Port: port,
+ Database: "FREEPDB1",
+ ServiceName: "FREEPDB1",
+ },
+ Password: "testpass",
+ ConnectionContext: db.ConnectionContext{
+ EngineVersion: "21.0",
+ DatabaseName: "TESTUSER",
+ },
+ }
+
+ openedDriver, err := driver.Open(ctx, storepb.Engine_ORACLE, config)
+ require.NoError(t, err)
+ defer openedDriver.Close(ctx)
+
+ // Cast to Oracle driver for SyncDBSchema
+ oracleDriver, ok := openedDriver.(*oracledb.Driver)
+ require.True(t, ok, "failed to cast to oracle.Driver")
+
+ // Test cases with various Oracle features
+ testCases := []struct {
+ name string
+ ddl string
+ }{
+ {
+ name: "basic_table",
+ ddl: `
+CREATE TABLE TEST_TABLE (
+ ID NUMBER PRIMARY KEY,
+ NAME VARCHAR2(50) NOT NULL
+);
+`,
+ },
+ {
+ name: "table_with_sequence",
+ ddl: `
+CREATE TABLE EMPLOYEES (
+ ID NUMBER PRIMARY KEY,
+ NAME VARCHAR2(100) NOT NULL
+);
+
+CREATE SEQUENCE EMP_SEQ START WITH 1 INCREMENT BY 1;
+`,
+ },
+ {
+ name: "table_with_view",
+ ddl: `
+CREATE TABLE PRODUCTS (
+ ID NUMBER PRIMARY KEY,
+ NAME VARCHAR2(100) NOT NULL,
+ PRICE NUMBER(10,2)
+);
+
+CREATE VIEW EXPENSIVE_PRODUCTS AS
+SELECT ID, NAME, PRICE
+FROM PRODUCTS
+WHERE PRICE > 100;
+`,
+ },
+ {
+ name: "comprehensive_data_types",
+ ddl: `
+CREATE TABLE DATA_TYPES_COMPREHENSIVE (
+ ID NUMBER PRIMARY KEY,
+ -- Numeric types
+ NUMBER_COL NUMBER,
+ NUMBER_PRECISION NUMBER(10),
+ NUMBER_SCALE NUMBER(10,2),
+ FLOAT_COL FLOAT,
+ BINARY_FLOAT_COL BINARY_FLOAT,
+ BINARY_DOUBLE_COL BINARY_DOUBLE,
+ -- Character types
+ VARCHAR2_COL VARCHAR2(4000),
+ NVARCHAR2_COL NVARCHAR2(2000),
+ CHAR_COL CHAR(100),
+ NCHAR_COL NCHAR(100),
+ -- Date and time types
+ DATE_COL DATE,
+ TIMESTAMP_COL TIMESTAMP,
+ TIMESTAMP_TZ_COL TIMESTAMP WITH TIME ZONE,
+ TIMESTAMP_LTZ_COL TIMESTAMP WITH LOCAL TIME ZONE,
+ INTERVAL_YM_COL INTERVAL YEAR TO MONTH,
+ INTERVAL_DS_COL INTERVAL DAY TO SECOND,
+ -- Large object types
+ CLOB_COL CLOB,
+ NCLOB_COL NCLOB,
+ BLOB_COL BLOB,
+ -- Binary types
+ RAW_COL RAW(2000),
+ LONG_RAW_COL LONG RAW,
+ -- Row identifier
+ ROWID_COL ROWID,
+ UROWID_COL UROWID
+);
+`,
+ },
+ {
+ name: "complex_constraints",
+ ddl: `
+CREATE TABLE ORDERS (
+ ORDER_ID NUMBER PRIMARY KEY,
+ CUSTOMER_ID NUMBER NOT NULL,
+ ORDER_DATE DATE DEFAULT SYSDATE,
+ STATUS VARCHAR2(20) DEFAULT 'PENDING',
+ TOTAL_AMOUNT NUMBER(12,2) NOT NULL,
+ DISCOUNT_PERCENT NUMBER(5,2) DEFAULT 0,
+ SHIP_DATE DATE,
+ NOTES VARCHAR2(4000),
+ CREATED_BY VARCHAR2(100) DEFAULT USER,
+ CREATED_AT TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
+ UPDATED_AT TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
+ -- Check constraints
+ CONSTRAINT chk_status CHECK (STATUS IN ('PENDING', 'PROCESSING', 'SHIPPED', 'DELIVERED', 'CANCELLED')),
+ CONSTRAINT chk_total_positive CHECK (TOTAL_AMOUNT > 0),
+ CONSTRAINT chk_discount_range CHECK (DISCOUNT_PERCENT BETWEEN 0 AND 100),
+ CONSTRAINT chk_ship_after_order CHECK (SHIP_DATE IS NULL OR SHIP_DATE >= ORDER_DATE),
+ -- Unique constraints
+ CONSTRAINT uk_order_customer_date UNIQUE (CUSTOMER_ID, ORDER_DATE)
+);
+`,
+ },
+ {
+ name: "foreign_keys_and_references",
+ ddl: `
+CREATE TABLE DEPARTMENTS (
+ DEPT_ID NUMBER PRIMARY KEY,
+ DEPT_NAME VARCHAR2(100) NOT NULL UNIQUE,
+ MANAGER_ID NUMBER,
+ LOCATION VARCHAR2(100),
+ BUDGET NUMBER(15,2)
+);
+
+CREATE TABLE EMPLOYEES_FK (
+ EMP_ID NUMBER PRIMARY KEY,
+ FIRST_NAME VARCHAR2(50) NOT NULL,
+ LAST_NAME VARCHAR2(50) NOT NULL,
+ EMAIL VARCHAR2(100) UNIQUE,
+ PHONE VARCHAR2(20),
+ HIRE_DATE DATE DEFAULT SYSDATE,
+ SALARY NUMBER(10,2),
+ COMMISSION_PCT NUMBER(4,2),
+ DEPT_ID NUMBER,
+ MANAGER_ID NUMBER,
+ -- Foreign key constraints
+ CONSTRAINT fk_emp_dept FOREIGN KEY (DEPT_ID) REFERENCES DEPARTMENTS(DEPT_ID) ON DELETE SET NULL,
+ CONSTRAINT fk_emp_manager FOREIGN KEY (MANAGER_ID) REFERENCES EMPLOYEES_FK(EMP_ID) ON DELETE SET NULL,
+ -- Check constraints
+ CONSTRAINT chk_salary_positive CHECK (SALARY > 0),
+ CONSTRAINT chk_commission_range CHECK (COMMISSION_PCT BETWEEN 0 AND 1),
+ CONSTRAINT chk_email_format CHECK (EMAIL LIKE '%@%.%')
+);
+
+-- Self-referencing foreign key for department manager
+ALTER TABLE DEPARTMENTS ADD CONSTRAINT fk_dept_manager
+ FOREIGN KEY (MANAGER_ID) REFERENCES EMPLOYEES_FK(EMP_ID) ON DELETE SET NULL;
+`,
+ },
+ {
+ name: "indexes_comprehensive",
+ ddl: `
+CREATE TABLE CUSTOMER_ORDERS (
+ ORDER_ID NUMBER PRIMARY KEY,
+ CUSTOMER_ID NUMBER NOT NULL,
+ ORDER_DATE DATE NOT NULL,
+ PRODUCT_CODE VARCHAR2(50) NOT NULL,
+ QUANTITY NUMBER NOT NULL,
+ UNIT_PRICE NUMBER(10,2) NOT NULL,
+ TOTAL_AMOUNT NUMBER(12,2) NOT NULL,
+ STATUS VARCHAR2(20) DEFAULT 'ACTIVE',
+ DESCRIPTION CLOB,
+ TAGS VARCHAR2(1000),
+ CREATED_AT TIMESTAMP DEFAULT CURRENT_TIMESTAMP
+);
+
+-- Various index types
+CREATE INDEX idx_customer_date ON CUSTOMER_ORDERS(CUSTOMER_ID, ORDER_DATE);
+CREATE INDEX idx_product_status ON CUSTOMER_ORDERS(PRODUCT_CODE, STATUS);
+CREATE INDEX idx_order_date_desc ON CUSTOMER_ORDERS(ORDER_DATE DESC);
+CREATE INDEX idx_total_amount ON CUSTOMER_ORDERS(TOTAL_AMOUNT);
+CREATE UNIQUE INDEX idx_customer_product_unique ON CUSTOMER_ORDERS(CUSTOMER_ID, PRODUCT_CODE, ORDER_DATE);
+CREATE BITMAP INDEX idx_status_bitmap ON CUSTOMER_ORDERS(STATUS);
+CREATE INDEX idx_upper_product ON CUSTOMER_ORDERS(UPPER(PRODUCT_CODE));
+CREATE INDEX idx_created_year ON CUSTOMER_ORDERS(EXTRACT(YEAR FROM CREATED_AT));
+`,
+ },
+ {
+ name: "sequences_and_triggers",
+ ddl: `
+CREATE TABLE AUDIT_LOG (
+ LOG_ID NUMBER PRIMARY KEY,
+ TABLE_NAME VARCHAR2(128) NOT NULL,
+ OPERATION VARCHAR2(10) NOT NULL,
+ OLD_VALUES CLOB,
+ NEW_VALUES CLOB,
+ USER_NAME VARCHAR2(128) DEFAULT USER,
+ TIMESTAMP TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
+ SESSION_ID VARCHAR2(128) DEFAULT SYS_CONTEXT('USERENV', 'SESSIONID')
+);
+
+CREATE SEQUENCE audit_log_seq
+ START WITH 1000
+ INCREMENT BY 1
+ MAXVALUE 999999999999999999
+ MINVALUE 1
+ NOCYCLE
+ CACHE 20;
+
+CREATE OR REPLACE TRIGGER audit_log_trigger
+ BEFORE INSERT ON AUDIT_LOG
+ FOR EACH ROW
+BEGIN
+ IF :NEW.LOG_ID IS NULL THEN
+ SELECT audit_log_seq.NEXTVAL INTO :NEW.LOG_ID FROM DUAL;
+ END IF;
+END;
+/
+
+CREATE TABLE INVENTORY (
+ ITEM_ID NUMBER PRIMARY KEY,
+ ITEM_CODE VARCHAR2(50) UNIQUE NOT NULL,
+ DESCRIPTION VARCHAR2(200),
+ QUANTITY_ON_HAND NUMBER DEFAULT 0,
+ UNIT_COST NUMBER(10,2),
+ LAST_UPDATED TIMESTAMP DEFAULT CURRENT_TIMESTAMP
+);
+
+CREATE SEQUENCE inventory_seq START WITH 1 INCREMENT BY 1;
+
+CREATE OR REPLACE TRIGGER inventory_audit_trigger
+ AFTER INSERT OR UPDATE OR DELETE ON INVENTORY
+ FOR EACH ROW
+BEGIN
+ IF INSERTING THEN
+ INSERT INTO AUDIT_LOG (TABLE_NAME, OPERATION, NEW_VALUES)
+ VALUES ('INVENTORY', 'INSERT', 'ID=' || :NEW.ITEM_ID || ',CODE=' || :NEW.ITEM_CODE);
+ ELSIF UPDATING THEN
+ INSERT INTO AUDIT_LOG (TABLE_NAME, OPERATION, OLD_VALUES, NEW_VALUES)
+ VALUES ('INVENTORY', 'UPDATE',
+ 'ID=' || :OLD.ITEM_ID || ',QTY=' || :OLD.QUANTITY_ON_HAND,
+ 'ID=' || :NEW.ITEM_ID || ',QTY=' || :NEW.QUANTITY_ON_HAND);
+ ELSIF DELETING THEN
+ INSERT INTO AUDIT_LOG (TABLE_NAME, OPERATION, OLD_VALUES)
+ VALUES ('INVENTORY', 'DELETE', 'ID=' || :OLD.ITEM_ID || ',CODE=' || :OLD.ITEM_CODE);
+ END IF;
+END;
+/
+`,
+ },
+ {
+ name: "views_complex",
+ ddl: `
+CREATE TABLE SALES_TRANSACTIONS (
+ TRANSACTION_ID NUMBER PRIMARY KEY,
+ CUSTOMER_ID NUMBER NOT NULL,
+ PRODUCT_ID NUMBER NOT NULL,
+ SALE_DATE DATE NOT NULL,
+ QUANTITY NUMBER NOT NULL,
+ UNIT_PRICE NUMBER(10,2) NOT NULL,
+ DISCOUNT_AMOUNT NUMBER(10,2) DEFAULT 0,
+ TAX_AMOUNT NUMBER(10,2) DEFAULT 0,
+ TOTAL_AMOUNT NUMBER(12,2) NOT NULL,
+ SALES_REP_ID NUMBER,
+ REGION VARCHAR2(50)
+);
+
+-- Complex view with aggregations
+CREATE VIEW MONTHLY_SALES_SUMMARY AS
+SELECT
+ TO_CHAR(SALE_DATE, 'YYYY-MM') AS SALES_MONTH,
+ REGION,
+ COUNT(*) AS TRANSACTION_COUNT,
+ SUM(QUANTITY) AS TOTAL_QUANTITY,
+ SUM(TOTAL_AMOUNT) AS TOTAL_REVENUE,
+ AVG(TOTAL_AMOUNT) AS AVG_TRANSACTION_VALUE,
+ MIN(SALE_DATE) AS FIRST_SALE_DATE,
+ MAX(SALE_DATE) AS LAST_SALE_DATE
+FROM SALES_TRANSACTIONS
+WHERE SALE_DATE >= ADD_MONTHS(SYSDATE, -12)
+GROUP BY TO_CHAR(SALE_DATE, 'YYYY-MM'), REGION
+HAVING SUM(TOTAL_AMOUNT) > 1000
+ORDER BY SALES_MONTH DESC, REGION;
+
+-- View with joins and complex expressions
+CREATE VIEW CUSTOMER_SALES_ANALYSIS AS
+SELECT
+ st.CUSTOMER_ID,
+ COUNT(DISTINCT st.TRANSACTION_ID) AS TOTAL_TRANSACTIONS,
+ COUNT(DISTINCT TO_CHAR(st.SALE_DATE, 'YYYY-MM')) AS ACTIVE_MONTHS,
+ SUM(st.TOTAL_AMOUNT) AS LIFETIME_VALUE,
+ AVG(st.TOTAL_AMOUNT) AS AVG_TRANSACTION_VALUE,
+ MAX(st.SALE_DATE) AS LAST_PURCHASE_DATE,
+ CASE
+ WHEN MAX(st.SALE_DATE) >= SYSDATE - 30 THEN 'ACTIVE'
+ WHEN MAX(st.SALE_DATE) >= SYSDATE - 90 THEN 'RECENT'
+ ELSE 'INACTIVE'
+ END AS CUSTOMER_STATUS,
+ ROUND(SUM(st.TOTAL_AMOUNT) / NULLIF(COUNT(DISTINCT TO_CHAR(st.SALE_DATE, 'YYYY-MM')), 0), 2) AS MONTHLY_SPEND_RATE
+FROM SALES_TRANSACTIONS st
+WHERE st.SALE_DATE >= ADD_MONTHS(SYSDATE, -24)
+GROUP BY st.CUSTOMER_ID
+HAVING COUNT(*) >= 3;
+`,
+ },
+ {
+ name: "materialized_views",
+ ddl: `
+CREATE TABLE PRODUCT_SALES_DAILY (
+ SALE_DATE DATE NOT NULL,
+ PRODUCT_ID NUMBER NOT NULL,
+ CATEGORY VARCHAR2(50) NOT NULL,
+ SALES_AMOUNT NUMBER(12,2) NOT NULL,
+ UNITS_SOLD NUMBER NOT NULL,
+ COST_OF_GOODS NUMBER(12,2) NOT NULL
+);
+
+-- Materialized view for performance (simplified for query rewrite support)
+CREATE MATERIALIZED VIEW PRODUCT_PERFORMANCE_MV
+BUILD IMMEDIATE
+REFRESH COMPLETE ON DEMAND
+AS
+SELECT
+ PRODUCT_ID,
+ CATEGORY,
+ COUNT(*) AS SALES_DAYS,
+ SUM(SALES_AMOUNT) AS TOTAL_REVENUE,
+ SUM(UNITS_SOLD) AS TOTAL_UNITS,
+ SUM(COST_OF_GOODS) AS TOTAL_COST,
+ SUM(SALES_AMOUNT) - SUM(COST_OF_GOODS) AS TOTAL_PROFIT,
+ AVG(SALES_AMOUNT) AS AVG_DAILY_REVENUE,
+ MIN(SALE_DATE) AS FIRST_SALE_DATE,
+ MAX(SALE_DATE) AS LAST_SALE_DATE
+FROM PRODUCT_SALES_DAILY
+WHERE SALE_DATE >= ADD_MONTHS(SYSDATE, -12)
+GROUP BY PRODUCT_ID, CATEGORY;
+
+-- Create index on materialized view
+CREATE INDEX idx_mv_category ON PRODUCT_PERFORMANCE_MV(CATEGORY);
+CREATE INDEX idx_mv_total_profit ON PRODUCT_PERFORMANCE_MV(TOTAL_PROFIT);
+`,
+ },
+ {
+ name: "procedures_and_functions",
+ ddl: `
+CREATE TABLE CUSTOMER_ACCOUNTS (
+ ACCOUNT_ID NUMBER PRIMARY KEY,
+ CUSTOMER_NAME VARCHAR2(100) NOT NULL,
+ ACCOUNT_TYPE VARCHAR2(20) NOT NULL,
+ BALANCE NUMBER(15,2) DEFAULT 0,
+ CREDIT_LIMIT NUMBER(15,2) DEFAULT 0,
+ STATUS VARCHAR2(20) DEFAULT 'ACTIVE',
+ CREATED_DATE DATE DEFAULT SYSDATE
+);
+
+-- Function to calculate available credit
+CREATE OR REPLACE FUNCTION get_available_credit(p_account_id NUMBER)
+RETURN NUMBER
+DETERMINISTIC
+IS
+ v_balance NUMBER;
+ v_credit_limit NUMBER;
+ v_available_credit NUMBER;
+BEGIN
+ SELECT BALANCE, CREDIT_LIMIT
+ INTO v_balance, v_credit_limit
+ FROM CUSTOMER_ACCOUNTS
+ WHERE ACCOUNT_ID = p_account_id;
+
+ v_available_credit := v_credit_limit - v_balance;
+
+ RETURN GREATEST(v_available_credit, 0);
+EXCEPTION
+ WHEN NO_DATA_FOUND THEN
+ RETURN 0;
+ WHEN OTHERS THEN
+ RETURN -1;
+END;
+/
+
+-- Procedure to update account balance
+CREATE OR REPLACE PROCEDURE update_account_balance(
+ p_account_id IN NUMBER,
+ p_amount IN NUMBER,
+ p_transaction_type IN VARCHAR2,
+ p_result OUT VARCHAR2
+)
+IS
+ v_current_balance NUMBER;
+ v_new_balance NUMBER;
+ v_credit_limit NUMBER;
+BEGIN
+ -- Get current balance and credit limit
+ SELECT BALANCE, CREDIT_LIMIT
+ INTO v_current_balance, v_credit_limit
+ FROM CUSTOMER_ACCOUNTS
+ WHERE ACCOUNT_ID = p_account_id
+ FOR UPDATE;
+
+ -- Calculate new balance
+ IF p_transaction_type = 'CREDIT' THEN
+ v_new_balance := v_current_balance + p_amount;
+ ELSIF p_transaction_type = 'DEBIT' THEN
+ v_new_balance := v_current_balance - p_amount;
+
+ -- Check credit limit
+ IF v_new_balance < (v_credit_limit * -1) THEN
+ p_result := 'ERROR: Transaction would exceed credit limit';
+ ROLLBACK;
+ RETURN;
+ END IF;
+ ELSE
+ p_result := 'ERROR: Invalid transaction type';
+ RETURN;
+ END IF;
+
+ -- Update balance
+ UPDATE CUSTOMER_ACCOUNTS
+ SET BALANCE = v_new_balance,
+ STATUS = CASE
+ WHEN v_new_balance < 0 THEN 'OVERDRAWN'
+ WHEN v_new_balance >= 0 THEN 'ACTIVE'
+ END
+ WHERE ACCOUNT_ID = p_account_id;
+
+ COMMIT;
+ p_result := 'SUCCESS: Balance updated to ' || v_new_balance;
+
+EXCEPTION
+ WHEN NO_DATA_FOUND THEN
+ p_result := 'ERROR: Account not found';
+ WHEN OTHERS THEN
+ p_result := 'ERROR: ' || SQLERRM;
+ ROLLBACK;
+END;
+/
+
+-- Function with complex logic
+CREATE OR REPLACE FUNCTION calculate_account_risk_score(p_account_id NUMBER)
+RETURN NUMBER
+IS
+ v_balance NUMBER;
+ v_credit_limit NUMBER;
+ v_account_age NUMBER;
+ v_transaction_count NUMBER;
+ v_risk_score NUMBER := 0;
+BEGIN
+ SELECT
+ BALANCE,
+ CREDIT_LIMIT,
+ SYSDATE - CREATED_DATE,
+ (SELECT COUNT(*) FROM AUDIT_LOG WHERE TABLE_NAME = 'CUSTOMER_ACCOUNTS')
+ INTO v_balance, v_credit_limit, v_account_age, v_transaction_count
+ FROM CUSTOMER_ACCOUNTS
+ WHERE ACCOUNT_ID = p_account_id;
+
+ -- Calculate risk based on multiple factors
+ -- Balance utilization (higher utilization = higher risk)
+ IF v_credit_limit > 0 THEN
+ v_risk_score := v_risk_score + (ABS(v_balance) / v_credit_limit) * 40;
+ END IF;
+
+ -- Account age (newer accounts = higher risk)
+ IF v_account_age < 30 THEN
+ v_risk_score := v_risk_score + 30;
+ ELSIF v_account_age < 90 THEN
+ v_risk_score := v_risk_score + 20;
+ ELSIF v_account_age < 365 THEN
+ v_risk_score := v_risk_score + 10;
+ END IF;
+
+ -- Transaction frequency (too few or too many = higher risk)
+ IF v_transaction_count < 5 THEN
+ v_risk_score := v_risk_score + 20;
+ ELSIF v_transaction_count > 100 THEN
+ v_risk_score := v_risk_score + 15;
+ END IF;
+
+ -- Negative balance penalty
+ IF v_balance < 0 THEN
+ v_risk_score := v_risk_score + 25;
+ END IF;
+
+ RETURN LEAST(v_risk_score, 100); -- Cap at 100
+
+EXCEPTION
+ WHEN NO_DATA_FOUND THEN
+ RETURN 100; -- Max risk for non-existent accounts
+ WHEN OTHERS THEN
+ RETURN 50; -- Default moderate risk on errors
+END;
+/
+`,
+ },
+ {
+ name: "packages_comprehensive",
+ ddl: `
+-- Package specification
+CREATE OR REPLACE PACKAGE financial_utils AS
+ -- Constants
+ c_max_overdraft CONSTANT NUMBER := 5000;
+ c_default_interest_rate CONSTANT NUMBER := 0.05;
+
+ -- Types
+ TYPE account_summary_type IS RECORD (
+ account_id NUMBER,
+ balance NUMBER,
+ available_credit NUMBER,
+ risk_score NUMBER,
+ status VARCHAR2(20)
+ );
+
+ TYPE account_list_type IS TABLE OF account_summary_type;
+
+ -- Procedures
+ PROCEDURE calculate_interest(
+ p_account_id IN NUMBER,
+ p_days IN NUMBER DEFAULT 30,
+ p_interest_amount OUT NUMBER
+ );
+
+ PROCEDURE generate_statement(
+ p_account_id IN NUMBER,
+ p_statement_date IN DATE DEFAULT SYSDATE,
+ p_statement_text OUT CLOB
+ );
+
+ -- Functions
+ FUNCTION get_account_summary(p_account_id NUMBER) RETURN account_summary_type;
+
+ FUNCTION get_high_risk_accounts(p_risk_threshold NUMBER DEFAULT 70)
+ RETURN account_list_type PIPELINED;
+
+ FUNCTION format_currency(p_amount NUMBER) RETURN VARCHAR2;
+
+ FUNCTION validate_account_number(p_account_id NUMBER) RETURN BOOLEAN;
+
+ -- Overloaded functions
+ FUNCTION calculate_fee(p_transaction_type VARCHAR2) RETURN NUMBER;
+ FUNCTION calculate_fee(p_transaction_type VARCHAR2, p_amount NUMBER) RETURN NUMBER;
+
+END financial_utils;
+/
+
+-- Package body
+CREATE OR REPLACE PACKAGE BODY financial_utils AS
+
+ PROCEDURE calculate_interest(
+ p_account_id IN NUMBER,
+ p_days IN NUMBER DEFAULT 30,
+ p_interest_amount OUT NUMBER
+ ) IS
+ v_balance NUMBER;
+ v_daily_rate NUMBER;
+ BEGIN
+ SELECT BALANCE INTO v_balance
+ FROM CUSTOMER_ACCOUNTS
+ WHERE ACCOUNT_ID = p_account_id;
+
+ v_daily_rate := c_default_interest_rate / 365;
+
+ IF v_balance < 0 THEN
+ p_interest_amount := ABS(v_balance) * v_daily_rate * p_days;
+ ELSE
+ p_interest_amount := 0;
+ END IF;
+ EXCEPTION
+ WHEN NO_DATA_FOUND THEN
+ p_interest_amount := 0;
+ END calculate_interest;
+
+ PROCEDURE generate_statement(
+ p_account_id IN NUMBER,
+ p_statement_date IN DATE DEFAULT SYSDATE,
+ p_statement_text OUT CLOB
+ ) IS
+ v_account account_summary_type;
+ BEGIN
+ v_account := get_account_summary(p_account_id);
+
+ p_statement_text := 'ACCOUNT STATEMENT' || CHR(10) ||
+ 'Account ID: ' || v_account.account_id || CHR(10) ||
+ 'Statement Date: ' || TO_CHAR(p_statement_date, 'DD-MON-YYYY') || CHR(10) ||
+ 'Current Balance: ' || format_currency(v_account.balance) || CHR(10) ||
+ 'Available Credit: ' || format_currency(v_account.available_credit) || CHR(10) ||
+ 'Account Status: ' || v_account.status || CHR(10) ||
+ 'Risk Score: ' || v_account.risk_score;
+ END generate_statement;
+
+ FUNCTION get_account_summary(p_account_id NUMBER) RETURN account_summary_type IS
+ v_summary account_summary_type;
+ BEGIN
+ SELECT ACCOUNT_ID, BALANCE, STATUS
+ INTO v_summary.account_id, v_summary.balance, v_summary.status
+ FROM CUSTOMER_ACCOUNTS
+ WHERE ACCOUNT_ID = p_account_id;
+
+ v_summary.available_credit := get_available_credit(p_account_id);
+ v_summary.risk_score := calculate_account_risk_score(p_account_id);
+
+ RETURN v_summary;
+ END get_account_summary;
+
+ FUNCTION get_high_risk_accounts(p_risk_threshold NUMBER DEFAULT 70)
+ RETURN account_list_type PIPELINED IS
+ v_summary account_summary_type;
+ BEGIN
+ FOR rec IN (SELECT ACCOUNT_ID FROM CUSTOMER_ACCOUNTS WHERE STATUS = 'ACTIVE') LOOP
+ v_summary := get_account_summary(rec.ACCOUNT_ID);
+ IF v_summary.risk_score >= p_risk_threshold THEN
+ PIPE ROW(v_summary);
+ END IF;
+ END LOOP;
+ RETURN;
+ END get_high_risk_accounts;
+
+ FUNCTION format_currency(p_amount NUMBER) RETURN VARCHAR2 IS
+ BEGIN
+ RETURN TO_CHAR(p_amount, 'FM$999,999,990.00');
+ END format_currency;
+
+ FUNCTION validate_account_number(p_account_id NUMBER) RETURN BOOLEAN IS
+ v_count NUMBER;
+ BEGIN
+ SELECT COUNT(*) INTO v_count
+ FROM CUSTOMER_ACCOUNTS
+ WHERE ACCOUNT_ID = p_account_id;
+
+ RETURN v_count > 0;
+ END validate_account_number;
+
+ FUNCTION calculate_fee(p_transaction_type VARCHAR2) RETURN NUMBER IS
+ BEGIN
+ CASE p_transaction_type
+ WHEN 'TRANSFER' THEN RETURN 2.50;
+ WHEN 'WITHDRAWAL' THEN RETURN 1.00;
+ WHEN 'DEPOSIT' THEN RETURN 0;
+ ELSE RETURN 5.00;
+ END CASE;
+ END calculate_fee;
+
+ FUNCTION calculate_fee(p_transaction_type VARCHAR2, p_amount NUMBER) RETURN NUMBER IS
+ v_base_fee NUMBER;
+ v_percentage_fee NUMBER;
+ BEGIN
+ v_base_fee := calculate_fee(p_transaction_type);
+
+ -- Add percentage-based fee for large amounts
+ IF p_amount > 10000 THEN
+ v_percentage_fee := p_amount * 0.001; -- 0.1%
+ RETURN v_base_fee + v_percentage_fee;
+ ELSE
+ RETURN v_base_fee;
+ END IF;
+ END calculate_fee;
+
+END financial_utils;
+/
+`,
+ },
+ {
+ name: "table_comments_and_documentation",
+ ddl: `
+CREATE TABLE PRODUCT_CATALOG (
+ PRODUCT_ID NUMBER PRIMARY KEY,
+ SKU VARCHAR2(50) UNIQUE NOT NULL,
+ PRODUCT_NAME VARCHAR2(200) NOT NULL,
+ DESCRIPTION CLOB,
+ CATEGORY_ID NUMBER NOT NULL,
+ PRICE NUMBER(10,2) NOT NULL,
+ COST NUMBER(10,2),
+ WEIGHT NUMBER(8,3),
+ DIMENSIONS VARCHAR2(100),
+ COLOR VARCHAR2(50),
+ PRODUCT_SIZE VARCHAR2(20),
+ BRAND VARCHAR2(100),
+ SUPPLIER_ID NUMBER,
+ IS_ACTIVE NUMBER(1) DEFAULT 1,
+ STOCK_QUANTITY NUMBER DEFAULT 0,
+ REORDER_LEVEL NUMBER DEFAULT 10,
+ CREATED_DATE DATE DEFAULT SYSDATE,
+ CREATED_BY VARCHAR2(100) DEFAULT USER,
+ LAST_UPDATED DATE DEFAULT SYSDATE,
+ UPDATED_BY VARCHAR2(100) DEFAULT USER,
+ VERSION_NUMBER NUMBER DEFAULT 1
+);
+
+-- Table comments
+COMMENT ON TABLE PRODUCT_CATALOG IS 'Master catalog of all products available for sale. Contains detailed product information including pricing, inventory, and supplier details.';
+
+-- Column comments
+COMMENT ON COLUMN PRODUCT_CATALOG.PRODUCT_ID IS 'Unique identifier for the product. Primary key generated from sequence.';
+COMMENT ON COLUMN PRODUCT_CATALOG.SKU IS 'Stock Keeping Unit - unique alphanumeric code for inventory tracking';
+COMMENT ON COLUMN PRODUCT_CATALOG.PRODUCT_NAME IS 'Display name of the product as shown to customers';
+COMMENT ON COLUMN PRODUCT_CATALOG.DESCRIPTION IS 'Detailed product description including features and specifications';
+COMMENT ON COLUMN PRODUCT_CATALOG.CATEGORY_ID IS 'Foreign key reference to PRODUCT_CATEGORIES table';
+COMMENT ON COLUMN PRODUCT_CATALOG.PRICE IS 'Current selling price in base currency (USD)';
+COMMENT ON COLUMN PRODUCT_CATALOG.COST IS 'Cost of goods sold - used for profit margin calculations';
+COMMENT ON COLUMN PRODUCT_CATALOG.WEIGHT IS 'Product weight in kilograms - used for shipping calculations';
+COMMENT ON COLUMN PRODUCT_CATALOG.DIMENSIONS IS 'Product dimensions in format: Length x Width x Height (cm)';
+COMMENT ON COLUMN PRODUCT_CATALOG.COLOR IS 'Primary color of the product';
+COMMENT ON COLUMN PRODUCT_CATALOG.PRODUCT_SIZE IS 'Size designation (S, M, L, XL, etc.)';
+COMMENT ON COLUMN PRODUCT_CATALOG.BRAND IS 'Brand or manufacturer name';
+COMMENT ON COLUMN PRODUCT_CATALOG.SUPPLIER_ID IS 'Reference to primary supplier in SUPPLIERS table';
+COMMENT ON COLUMN PRODUCT_CATALOG.IS_ACTIVE IS 'Flag indicating if product is active (1) or discontinued (0)';
+COMMENT ON COLUMN PRODUCT_CATALOG.STOCK_QUANTITY IS 'Current inventory quantity on hand';
+COMMENT ON COLUMN PRODUCT_CATALOG.REORDER_LEVEL IS 'Minimum stock level that triggers reorder process';
+COMMENT ON COLUMN PRODUCT_CATALOG.CREATED_DATE IS 'Date when product record was first created';
+COMMENT ON COLUMN PRODUCT_CATALOG.CREATED_BY IS 'Username of person who created the record';
+COMMENT ON COLUMN PRODUCT_CATALOG.LAST_UPDATED IS 'Date of most recent update to any field';
+COMMENT ON COLUMN PRODUCT_CATALOG.UPDATED_BY IS 'Username of person who last modified the record';
+COMMENT ON COLUMN PRODUCT_CATALOG.VERSION_NUMBER IS 'Optimistic locking version number for concurrent update control';
+
+CREATE TABLE ORDER_LINE_ITEMS (
+ LINE_ITEM_ID NUMBER PRIMARY KEY,
+ ORDER_ID NUMBER NOT NULL,
+ PRODUCT_ID NUMBER NOT NULL,
+ QUANTITY NUMBER NOT NULL,
+ UNIT_PRICE NUMBER(10,2) NOT NULL,
+ DISCOUNT_AMOUNT NUMBER(10,2) DEFAULT 0,
+ TAX_AMOUNT NUMBER(10,2) DEFAULT 0,
+ LINE_TOTAL NUMBER(12,2) NOT NULL,
+ SPECIAL_INSTRUCTIONS VARCHAR2(500),
+ FULFILLMENT_STATUS VARCHAR2(20) DEFAULT 'PENDING'
+);
+
+COMMENT ON TABLE ORDER_LINE_ITEMS IS 'Individual line items for customer orders. Each row represents one product within an order.';
+COMMENT ON COLUMN ORDER_LINE_ITEMS.LINE_ITEM_ID IS 'Unique identifier for this line item';
+COMMENT ON COLUMN ORDER_LINE_ITEMS.ORDER_ID IS 'Reference to parent order in ORDERS table';
+COMMENT ON COLUMN ORDER_LINE_ITEMS.PRODUCT_ID IS 'Reference to product in PRODUCT_CATALOG table';
+COMMENT ON COLUMN ORDER_LINE_ITEMS.QUANTITY IS 'Number of units ordered';
+COMMENT ON COLUMN ORDER_LINE_ITEMS.UNIT_PRICE IS 'Price per unit at time of order (may differ from current catalog price)';
+COMMENT ON COLUMN ORDER_LINE_ITEMS.DISCOUNT_AMOUNT IS 'Total discount applied to this line item';
+COMMENT ON COLUMN ORDER_LINE_ITEMS.TAX_AMOUNT IS 'Total tax amount for this line item';
+COMMENT ON COLUMN ORDER_LINE_ITEMS.LINE_TOTAL IS 'Calculated total: (quantity * unit_price) - discount + tax';
+COMMENT ON COLUMN ORDER_LINE_ITEMS.SPECIAL_INSTRUCTIONS IS 'Customer-specific instructions for this item';
+COMMENT ON COLUMN ORDER_LINE_ITEMS.FULFILLMENT_STATUS IS 'Status: PENDING, PICKED, PACKED, SHIPPED, DELIVERED, RETURNED';
+`,
+ },
+ }
+
+ for _, tc := range testCases {
+ t.Run(tc.name, func(t *testing.T) {
+ // Execute DDL using the driver
+ _, err = openedDriver.Execute(ctx, tc.ddl, db.ExecuteOptions{})
+ require.NoError(t, err)
+
+ // Get metadata from live database using driver
+ dbMetadata, err := oracleDriver.SyncDBSchema(ctx)
+ require.NoError(t, err)
+
+ // Get metadata from parser
+ parsedMetadata, err := GetDatabaseMetadata(tc.ddl)
+ require.NoError(t, err)
+
+ // Compare metadata
+ compareMetadata(t, dbMetadata, parsedMetadata, tc.name)
+
+ // Clean up - drop all objects to avoid conflicts
+ cleanupStatements := []string{
+ // Drop packages first
+ "DROP PACKAGE BODY financial_utils",
+ "DROP PACKAGE financial_utils",
+ // Drop functions and procedures
+ "DROP FUNCTION get_available_credit",
+ "DROP FUNCTION calculate_account_risk_score",
+ "DROP PROCEDURE update_account_balance",
+ // Drop materialized views
+ "DROP MATERIALIZED VIEW PRODUCT_PERFORMANCE_MV",
+ "DROP MATERIALIZED VIEW SALES_MONTHLY_MV",
+ // Drop views
+ "DROP VIEW CUSTOMER_SALES_ANALYSIS",
+ "DROP VIEW MONTHLY_SALES_SUMMARY",
+ "DROP VIEW EXPENSIVE_PRODUCTS",
+ // Drop triggers
+ "DROP TRIGGER inventory_audit_trigger",
+ "DROP TRIGGER audit_log_trigger",
+ // Drop sequences
+ "DROP SEQUENCE inventory_seq",
+ "DROP SEQUENCE audit_log_seq",
+ "DROP SEQUENCE EMP_SEQ",
+ // Drop tables (order matters due to foreign keys)
+ "DROP TABLE ORDER_LINE_ITEMS CASCADE CONSTRAINTS",
+ "DROP TABLE PRODUCT_CATALOG CASCADE CONSTRAINTS",
+ "DROP TABLE CUSTOMER_ACCOUNTS CASCADE CONSTRAINTS",
+ "DROP TABLE PRODUCT_SALES_DAILY CASCADE CONSTRAINTS",
+ "DROP TABLE SALES_TRANSACTIONS CASCADE CONSTRAINTS",
+ "DROP TABLE INVENTORY CASCADE CONSTRAINTS",
+ "DROP TABLE AUDIT_LOG CASCADE CONSTRAINTS",
+ "DROP TABLE CUSTOMER_ORDERS CASCADE CONSTRAINTS",
+ "DROP TABLE EMPLOYEES_FK CASCADE CONSTRAINTS",
+ "DROP TABLE DEPARTMENTS CASCADE CONSTRAINTS",
+ "DROP TABLE ORDERS CASCADE CONSTRAINTS",
+ "DROP TABLE DATA_TYPES_COMPREHENSIVE CASCADE CONSTRAINTS",
+ "DROP TABLE TEST_TABLE CASCADE CONSTRAINTS",
+ "DROP TABLE EMPLOYEES CASCADE CONSTRAINTS",
+ "DROP TABLE PRODUCTS CASCADE CONSTRAINTS",
+ }
+
+ for _, stmt := range cleanupStatements {
+ _, _ = openedDriver.Execute(ctx, stmt, db.ExecuteOptions{})
+ // Ignore errors during cleanup
+ }
+ })
+ }
+}
+
+// compareMetadata compares the metadata from the live database with the parsed metadata
+func compareMetadata(t *testing.T, dbMeta, parsedMeta *storepb.DatabaseSchemaMetadata, testName string) {
+ // For Oracle, we have one schema per database
+ require.Equal(t, 1, len(parsedMeta.Schemas), "parsed metadata should have exactly one schema")
+
+ dbSchema := dbMeta.Schemas[0]
+ parsedSchema := parsedMeta.Schemas[0]
+
+ t.Logf("Test case %s - DB: %d tables, %d views, %d materialized views, %d sequences, %d functions, %d procedures | Parsed: %d tables, %d views, %d materialized views, %d sequences, %d functions, %d procedures",
+ testName,
+ len(dbSchema.Tables), len(dbSchema.Views), len(dbSchema.MaterializedViews), len(dbSchema.Sequences), len(dbSchema.Functions), len(dbSchema.Procedures),
+ len(parsedSchema.Tables), len(parsedSchema.Views), len(parsedSchema.MaterializedViews), len(parsedSchema.Sequences), len(parsedSchema.Functions), len(parsedSchema.Procedures))
+
+ // Compare schema names (allow flexibility for Oracle default schema)
+ if dbSchema.Name != "" && parsedSchema.Name != "" {
+ require.Equal(t, dbSchema.Name, parsedSchema.Name, "schema names should match")
+ } else {
+ t.Logf("Schema names: DB='%s', Parsed='%s' (allowing flexibility for Oracle default schema)", dbSchema.Name, parsedSchema.Name)
+ }
+
+ // Compare tables with deep content validation
+ compareTables(t, dbSchema.Tables, parsedSchema.Tables, testName)
+
+ // Compare views with content validation
+ compareViews(t, dbSchema.Views, parsedSchema.Views, testName)
+
+ // Compare materialized views
+ compareMaterializedViews(t, dbSchema.MaterializedViews, parsedSchema.MaterializedViews, testName)
+
+ // Compare sequences with content validation
+ compareSequences(t, dbSchema.Sequences, parsedSchema.Sequences, testName)
+
+ // Compare functions with content validation
+ compareFunctions(t, dbSchema.Functions, parsedSchema.Functions, testName)
+
+ // Compare procedures with content validation
+ compareProcedures(t, dbSchema.Procedures, parsedSchema.Procedures, testName)
+}
+
+// compareTables does deep comparison of table metadata
+func compareTables(t *testing.T, dbTables, parsedTables []*storepb.TableMetadata, _ string) {
+ // Create maps for easier lookup
+ dbTableMap := make(map[string]*storepb.TableMetadata)
+ for _, table := range dbTables {
+ dbTableMap[table.Name] = table
+ }
+
+ parsedTableMap := make(map[string]*storepb.TableMetadata)
+ for _, table := range parsedTables {
+ parsedTableMap[table.Name] = table
+ }
+
+ // Validate that parsed tables exist in database and compare content
+ for tableName, parsedTable := range parsedTableMap {
+ dbTable, exists := dbTableMap[tableName]
+ require.True(t, exists, "parsed table %s not found in database metadata", tableName)
+
+ t.Logf("Comparing table %s - DB: %d columns | Parsed: %d columns",
+ tableName, len(dbTable.Columns), len(parsedTable.Columns))
+
+ // Compare table properties
+ require.Equal(t, dbTable.Name, parsedTable.Name, "table names should match")
+ if dbTable.Comment != "" || parsedTable.Comment != "" {
+ require.Equal(t, dbTable.Comment, parsedTable.Comment, "table comments should match for %s", tableName)
+ }
+
+ // Compare columns in detail
+ compareColumns(t, dbTable.Columns, parsedTable.Columns, tableName)
+
+ // Compare indexes
+ compareIndexes(t, dbTable.Indexes, parsedTable.Indexes, tableName)
+
+ // Compare foreign keys
+ compareForeignKeys(t, dbTable.ForeignKeys, parsedTable.ForeignKeys, tableName)
+
+ // Compare check constraints
+ compareCheckConstraints(t, dbTable.CheckConstraints, parsedTable.CheckConstraints, tableName)
+ }
+
+ // Log any tables found in database but not in parser (acceptable for system tables)
+ for tableName := range dbTableMap {
+ if _, exists := parsedTableMap[tableName]; !exists {
+ t.Logf("Info: table %s found in database but not parsed (may be system table)", tableName)
+ }
+ }
+}
+
+// compareColumns does deep comparison of column metadata
+func compareColumns(t *testing.T, dbColumns, parsedColumns []*storepb.ColumnMetadata, tableName string) {
+ // Create maps for easier lookup
+ dbColumnMap := make(map[string]*storepb.ColumnMetadata)
+ for _, col := range dbColumns {
+ dbColumnMap[col.Name] = col
+ }
+
+ parsedColumnMap := make(map[string]*storepb.ColumnMetadata)
+ for _, col := range parsedColumns {
+ parsedColumnMap[col.Name] = col
+ }
+
+ // Compare each parsed column with database column
+ for colName, parsedCol := range parsedColumnMap {
+ dbCol, exists := dbColumnMap[colName]
+ if !exists {
+ t.Logf("Warning: parsed column %s.%s not found in database metadata (parser may have parsing issue)", tableName, colName)
+ continue
+ }
+
+ // Compare all ColumnMetadata fields comprehensively
+ compareColumnMetadata(t, dbCol, parsedCol, tableName, colName)
+ }
+
+ // Report any columns found in database but not parsed
+ for colName := range dbColumnMap {
+ if _, exists := parsedColumnMap[colName]; !exists {
+ t.Logf("Warning: column %s.%s found in database but not parsed", tableName, colName)
+ }
+ }
+}
+
+// compareColumnMetadata comprehensively compares all fields in ColumnMetadata
+func compareColumnMetadata(t *testing.T, dbCol, parsedCol *storepb.ColumnMetadata, tableName, colName string) {
+ // 1. Name
+ require.Equal(t, dbCol.Name, parsedCol.Name,
+ "column names should match for %s.%s", tableName, colName)
+
+ // 2. Position
+ require.Equal(t, dbCol.Position, parsedCol.Position,
+ "column positions should match for %s.%s: db=%d, parsed=%d",
+ tableName, colName, dbCol.Position, parsedCol.Position)
+
+ // 3. Type (with Oracle-specific normalization)
+ dbType := normalizeOracleDataType(dbCol.Type)
+ parsedType := normalizeOracleDataType(parsedCol.Type)
+ require.Equal(t, dbType, parsedType,
+ "data types should match for column %s.%s: db=%s, parsed=%s",
+ tableName, colName, dbCol.Type, parsedCol.Type)
+
+ // 4. Nullable
+ require.Equal(t, dbCol.Nullable, parsedCol.Nullable,
+ "nullable property should match for column %s.%s: db=%v, parsed=%v",
+ tableName, colName, dbCol.Nullable, parsedCol.Nullable)
+
+ // 5. Default values (existing function)
+ compareColumnDefaults(t, dbCol, parsedCol, tableName, colName)
+
+ // 6. Character Set
+ if dbCol.CharacterSet != "" || parsedCol.CharacterSet != "" {
+ require.Equal(t, dbCol.CharacterSet, parsedCol.CharacterSet,
+ "character sets should match for column %s.%s: db=%s, parsed=%s",
+ tableName, colName, dbCol.CharacterSet, parsedCol.CharacterSet)
+ }
+
+ // 7. Collation (Oracle-specific handling)
+ if dbCol.Collation != "" || parsedCol.Collation != "" {
+ // Oracle automatically assigns default collations (like USING_NLS_COMP) even when
+ // not explicitly specified in DDL. Only compare when both have values or when
+ // the DDL explicitly specified a collation (parsed has value).
+ if parsedCol.Collation != "" {
+ // DDL explicitly specified collation, should match exactly
+ require.Equal(t, dbCol.Collation, parsedCol.Collation,
+ "explicit collations should match for column %s.%s: db=%s, parsed=%s",
+ tableName, colName, dbCol.Collation, parsedCol.Collation)
+ } else if dbCol.Collation != "" && parsedCol.Collation == "" {
+ // Database has default collation but DDL didn't specify one - this is expected for Oracle
+ t.Logf("Info: column %s.%s has database default collation '%s' (DDL didn't specify collation)",
+ tableName, colName, dbCol.Collation)
+ }
+ }
+
+ // 8. Comment
+ if dbCol.Comment != "" || parsedCol.Comment != "" {
+ require.Equal(t, dbCol.Comment, parsedCol.Comment,
+ "column comments should match for %s.%s: db=%s, parsed=%s",
+ tableName, colName, dbCol.Comment, parsedCol.Comment)
+ }
+
+ // 9. User Comment
+ if dbCol.UserComment != "" || parsedCol.UserComment != "" {
+ require.Equal(t, dbCol.UserComment, parsedCol.UserComment,
+ "user comments should match for column %s.%s: db=%s, parsed=%s",
+ tableName, colName, dbCol.UserComment, parsedCol.UserComment)
+ }
+
+ // 10. On Update (MySQL-specific, but include for completeness)
+ if dbCol.OnUpdate != "" || parsedCol.OnUpdate != "" {
+ require.Equal(t, dbCol.OnUpdate, parsedCol.OnUpdate,
+ "on_update should match for column %s.%s: db=%s, parsed=%s",
+ tableName, colName, dbCol.OnUpdate, parsedCol.OnUpdate)
+ }
+
+ // 11. Generation Metadata (for virtual/generated columns)
+ compareGenerationMetadata(t, dbCol.Generation, parsedCol.Generation, tableName, colName)
+
+ // 12. Identity Generation (Oracle/PostgreSQL)
+ require.Equal(t, dbCol.IdentityGeneration, parsedCol.IdentityGeneration,
+ "identity generation should match for column %s.%s: db=%v, parsed=%v",
+ tableName, colName, dbCol.IdentityGeneration, parsedCol.IdentityGeneration)
+
+ // 13. Is Identity
+ require.Equal(t, dbCol.IsIdentity, parsedCol.IsIdentity,
+ "is_identity should match for column %s.%s: db=%v, parsed=%v",
+ tableName, colName, dbCol.IsIdentity, parsedCol.IsIdentity)
+
+ // 14. Oracle-specific: Default on Null
+ require.Equal(t, dbCol.DefaultOnNull, parsedCol.DefaultOnNull,
+ "default_on_null should match for column %s.%s: db=%v, parsed=%v",
+ tableName, colName, dbCol.DefaultOnNull, parsedCol.DefaultOnNull)
+
+ // 15. Identity Seed (MSSQL-specific, but include for completeness)
+ if dbCol.IdentitySeed != 0 || parsedCol.IdentitySeed != 0 {
+ require.Equal(t, dbCol.IdentitySeed, parsedCol.IdentitySeed,
+ "identity seed should match for column %s.%s: db=%d, parsed=%d",
+ tableName, colName, dbCol.IdentitySeed, parsedCol.IdentitySeed)
+ }
+
+ // 16. Identity Increment (MSSQL-specific, but include for completeness)
+ if dbCol.IdentityIncrement != 0 || parsedCol.IdentityIncrement != 0 {
+ require.Equal(t, dbCol.IdentityIncrement, parsedCol.IdentityIncrement,
+ "identity increment should match for column %s.%s: db=%d, parsed=%d",
+ tableName, colName, dbCol.IdentityIncrement, parsedCol.IdentityIncrement)
+ }
+
+ // 17. Default Constraint Name (MSSQL-specific, but include for completeness)
+ if dbCol.DefaultConstraintName != "" || parsedCol.DefaultConstraintName != "" {
+ require.Equal(t, dbCol.DefaultConstraintName, parsedCol.DefaultConstraintName,
+ "default constraint name should match for column %s.%s: db=%s, parsed=%s",
+ tableName, colName, dbCol.DefaultConstraintName, parsedCol.DefaultConstraintName)
+ }
+
+ // Log successful comparison with key details
+ t.Logf("✓ Column %s.%s: type=%s, nullable=%v, default=%s, pos=%d%s",
+ tableName, colName, parsedType, parsedCol.Nullable,
+ getColumnDefaultString(parsedCol), parsedCol.Position,
+ getColumnExtraInfo(parsedCol))
+}
+
+// compareGenerationMetadata compares generation metadata for virtual/generated columns
+func compareGenerationMetadata(t *testing.T, dbGen, parsedGen *storepb.GenerationMetadata, tableName, colName string) {
+ if dbGen == nil && parsedGen == nil {
+ return // Both nil, match
+ }
+
+ if dbGen == nil || parsedGen == nil {
+ require.Equal(t, dbGen, parsedGen,
+ "generation metadata presence should match for column %s.%s: db=%v, parsed=%v",
+ tableName, colName, dbGen != nil, parsedGen != nil)
+ return
+ }
+
+ // Compare generation type
+ require.Equal(t, dbGen.Type, parsedGen.Type,
+ "generation type should match for column %s.%s: db=%v, parsed=%v",
+ tableName, colName, dbGen.Type, parsedGen.Type)
+
+ // Compare generation expression
+ require.Equal(t, dbGen.Expression, parsedGen.Expression,
+ "generation expression should match for column %s.%s: db=%s, parsed=%s",
+ tableName, colName, dbGen.Expression, parsedGen.Expression)
+}
+
+// getColumnExtraInfo returns additional column information for logging
+func getColumnExtraInfo(col *storepb.ColumnMetadata) string {
+ var extras []string
+
+ if col.IsIdentity {
+ extras = append(extras, "identity")
+ }
+
+ if col.Generation != nil {
+ switch col.Generation.Type {
+ case storepb.GenerationMetadata_TYPE_VIRTUAL:
+ extras = append(extras, "virtual")
+ case storepb.GenerationMetadata_TYPE_STORED:
+ extras = append(extras, "stored")
+ }
+ }
+
+ if col.CharacterSet != "" {
+ extras = append(extras, fmt.Sprintf("charset=%s", col.CharacterSet))
+ }
+
+ if col.Collation != "" {
+ extras = append(extras, fmt.Sprintf("collation=%s", col.Collation))
+ }
+
+ if len(extras) > 0 {
+ return fmt.Sprintf(" (%s)", strings.Join(extras, ", "))
+ }
+ return ""
+}
+
+// compareIndexes compares index metadata
+func compareIndexes(t *testing.T, dbIndexes, parsedIndexes []*storepb.IndexMetadata, tableName string) {
+ // Create maps for easier lookup
+ dbIndexMap := make(map[string]*storepb.IndexMetadata)
+ for _, idx := range dbIndexes {
+ dbIndexMap[idx.Name] = idx
+ }
+
+ parsedIndexMap := make(map[string]*storepb.IndexMetadata)
+ for _, idx := range parsedIndexes {
+ parsedIndexMap[idx.Name] = idx
+ }
+
+ // Compare each parsed index
+ for idxName, parsedIdx := range parsedIndexMap {
+ dbIdx, exists := dbIndexMap[idxName]
+ if !exists {
+ // Some indexes might be system-generated, allow some flexibility
+ t.Logf("Info: parsed index %s on table %s not found in database (may be system-generated)", idxName, tableName)
+ continue
+ }
+
+ // Compare index properties
+ require.Equal(t, dbIdx.Name, parsedIdx.Name, "index names should match")
+ require.Equal(t, dbIdx.Primary, parsedIdx.Primary, "primary key flag should match for index %s", idxName)
+ require.Equal(t, dbIdx.Unique, parsedIdx.Unique, "unique flag should match for index %s", idxName)
+
+ // Compare index type (allowing some flexibility for Oracle-specific types)
+ if parsedIdx.Type != "" && dbIdx.Type != "" {
+ require.Equal(t, normalizeIndexType(dbIdx.Type), normalizeIndexType(parsedIdx.Type),
+ "index types should match for index %s", idxName)
+ }
+
+ // Compare expressions (column list)
+ require.Equal(t, len(dbIdx.Expressions), len(parsedIdx.Expressions),
+ "expression count should match for index %s", idxName)
+
+ // Log detailed expression information for debugging
+ t.Logf("Index %s expressions - DB: %v, Parsed: %v", idxName, dbIdx.Expressions, parsedIdx.Expressions)
+
+ for i, dbExpr := range dbIdx.Expressions {
+ if i < len(parsedIdx.Expressions) {
+ require.Equal(t, normalizeExpression(dbExpr), normalizeExpression(parsedIdx.Expressions[i]),
+ "index expressions should match for index %s", idxName)
+ }
+ }
+
+ t.Logf("✓ Index %s: type=%s, unique=%v, primary=%v, expressions=%v",
+ idxName, parsedIdx.Type, parsedIdx.Unique, parsedIdx.Primary, parsedIdx.Expressions)
+ }
+}
+
+// compareForeignKeys compares foreign key metadata
+func compareForeignKeys(t *testing.T, dbFKs, parsedFKs []*storepb.ForeignKeyMetadata, _ string) {
+ // Create maps for easier lookup
+ dbFKMap := make(map[string]*storepb.ForeignKeyMetadata)
+ for _, fk := range dbFKs {
+ dbFKMap[fk.Name] = fk
+ }
+
+ parsedFKMap := make(map[string]*storepb.ForeignKeyMetadata)
+ for _, fk := range parsedFKs {
+ parsedFKMap[fk.Name] = fk
+ }
+
+ // Compare each parsed foreign key
+ for fkName, parsedFK := range parsedFKMap {
+ dbFK, exists := dbFKMap[fkName]
+ require.True(t, exists, "parsed foreign key %s not found in database metadata", fkName)
+
+ // Compare foreign key properties
+ require.Equal(t, dbFK.Name, parsedFK.Name, "foreign key names should match")
+ require.Equal(t, dbFK.Columns, parsedFK.Columns, "foreign key columns should match for %s", fkName)
+ require.Equal(t, dbFK.ReferencedTable, parsedFK.ReferencedTable, "referenced table should match for %s", fkName)
+ require.Equal(t, dbFK.ReferencedColumns, parsedFK.ReferencedColumns, "referenced columns should match for %s", fkName)
+ require.Equal(t, dbFK.OnDelete, parsedFK.OnDelete, "on delete action should match for %s", fkName)
+ require.Equal(t, dbFK.OnUpdate, parsedFK.OnUpdate, "on update action should match for %s", fkName)
+
+ t.Logf("✓ Foreign Key %s: %v -> %s(%v), onDelete=%s, onUpdate=%s",
+ fkName, parsedFK.Columns, parsedFK.ReferencedTable, parsedFK.ReferencedColumns,
+ parsedFK.OnDelete, parsedFK.OnUpdate)
+ }
+}
+
+// compareCheckConstraints compares check constraint metadata
+func compareCheckConstraints(t *testing.T, dbChecks, parsedChecks []*storepb.CheckConstraintMetadata, _ string) {
+ // Create maps for easier lookup
+ dbCheckMap := make(map[string]*storepb.CheckConstraintMetadata)
+ for _, chk := range dbChecks {
+ dbCheckMap[chk.Name] = chk
+ }
+
+ parsedCheckMap := make(map[string]*storepb.CheckConstraintMetadata)
+ for _, chk := range parsedChecks {
+ parsedCheckMap[chk.Name] = chk
+ }
+
+ // Compare each parsed check constraint
+ for chkName, parsedCheck := range parsedCheckMap {
+ dbCheck, exists := dbCheckMap[chkName]
+ require.True(t, exists, "parsed check constraint %s not found in database metadata", chkName)
+
+ // Compare check constraint properties
+ require.Equal(t, dbCheck.Name, parsedCheck.Name, "check constraint names should match")
+
+ // Compare expressions (normalize for comparison)
+ dbExpr := normalizeCheckExpression(dbCheck.Expression)
+ parsedExpr := normalizeCheckExpression(parsedCheck.Expression)
+ require.Equal(t, dbExpr, parsedExpr, "check constraint expressions should match for %s", chkName)
+
+ t.Logf("✓ Check Constraint %s: %s", chkName, parsedCheck.Expression)
+ }
+}
+
+// compareViews compares view metadata
+func compareViews(t *testing.T, dbViews, parsedViews []*storepb.ViewMetadata, _ string) {
+ // Create maps for easier lookup
+ dbViewMap := make(map[string]*storepb.ViewMetadata)
+ for _, view := range dbViews {
+ dbViewMap[view.Name] = view
+ }
+
+ parsedViewMap := make(map[string]*storepb.ViewMetadata)
+ for _, view := range parsedViews {
+ parsedViewMap[view.Name] = view
+ }
+
+ // Compare each parsed view
+ for viewName, parsedView := range parsedViewMap {
+ dbView, exists := dbViewMap[viewName]
+ require.True(t, exists, "parsed view %s not found in database metadata", viewName)
+
+ // Compare view properties
+ require.Equal(t, dbView.Name, parsedView.Name, "view names should match")
+ require.NotEmpty(t, parsedView.Definition, "parsed view definition should not be empty")
+ require.NotEmpty(t, dbView.Definition, "database view definition should not be empty")
+
+ // Compare comments if present
+ if dbView.Comment != "" || parsedView.Comment != "" {
+ require.Equal(t, dbView.Comment, parsedView.Comment, "view comments should match for %s", viewName)
+ }
+
+ t.Logf("✓ View %s: definition length=%d", viewName, len(parsedView.Definition))
+ }
+}
+
+// compareMaterializedViews compares materialized view metadata
+func compareMaterializedViews(t *testing.T, dbMViews, parsedMViews []*storepb.MaterializedViewMetadata, _ string) {
+ // Create maps for easier lookup
+ dbMViewMap := make(map[string]*storepb.MaterializedViewMetadata)
+ for _, mv := range dbMViews {
+ dbMViewMap[mv.Name] = mv
+ }
+
+ parsedMViewMap := make(map[string]*storepb.MaterializedViewMetadata)
+ for _, mv := range parsedMViews {
+ parsedMViewMap[mv.Name] = mv
+ }
+
+ // Compare each parsed materialized view
+ for mvName, parsedMV := range parsedMViewMap {
+ dbMV, exists := dbMViewMap[mvName]
+ require.True(t, exists, "parsed materialized view %s not found in database metadata", mvName)
+
+ // Compare materialized view properties
+ require.Equal(t, dbMV.Name, parsedMV.Name, "materialized view names should match")
+ require.NotEmpty(t, parsedMV.Definition, "parsed materialized view definition should not be empty")
+ require.NotEmpty(t, dbMV.Definition, "database materialized view definition should not be empty")
+
+ t.Logf("✓ Materialized View %s: definition length=%d", mvName, len(parsedMV.Definition))
+ }
+}
+
+// compareSequences compares sequence metadata
+func compareSequences(t *testing.T, dbSequences, parsedSequences []*storepb.SequenceMetadata, _ string) {
+ // Create maps for easier lookup
+ dbSeqMap := make(map[string]*storepb.SequenceMetadata)
+ for _, seq := range dbSequences {
+ dbSeqMap[seq.Name] = seq
+ }
+
+ parsedSeqMap := make(map[string]*storepb.SequenceMetadata)
+ for _, seq := range parsedSequences {
+ parsedSeqMap[seq.Name] = seq
+ }
+
+ // Compare each parsed sequence
+ for seqName, parsedSeq := range parsedSeqMap {
+ dbSeq, exists := dbSeqMap[seqName]
+ require.True(t, exists, "parsed sequence %s not found in database metadata", seqName)
+
+ // Compare sequence properties
+ require.Equal(t, dbSeq.Name, parsedSeq.Name, "sequence names should match")
+
+ // Compare sequence properties if available
+ // Oracle may not store sequence metadata consistently, so be flexible
+ if parsedSeq.Start != "" && dbSeq.Start != "" {
+ require.Equal(t, dbSeq.Start, parsedSeq.Start, "sequence start values should match for %s", seqName)
+ } else if parsedSeq.Start != dbSeq.Start {
+ // One has value, other is empty - this is acceptable for Oracle sequences
+ t.Logf("Info: sequence %s has different start values - DB: '%s', Parsed: '%s' (Oracle sequence metadata can be inconsistent)",
+ seqName, dbSeq.Start, parsedSeq.Start)
+ }
+
+ if parsedSeq.Increment != "" && dbSeq.Increment != "" {
+ require.Equal(t, dbSeq.Increment, parsedSeq.Increment, "sequence increment values should match for %s", seqName)
+ } else if parsedSeq.Increment != dbSeq.Increment {
+ // One has value, other is empty - this is acceptable for Oracle sequences
+ t.Logf("Info: sequence %s has different increment values - DB: '%s', Parsed: '%s' (Oracle sequence metadata can be inconsistent)",
+ seqName, dbSeq.Increment, parsedSeq.Increment)
+ }
+
+ t.Logf("✓ Sequence %s: start=%s, increment=%s", seqName, parsedSeq.Start, parsedSeq.Increment)
+ }
+}
+
+// compareFunctions compares function metadata
+func compareFunctions(t *testing.T, dbFunctions, parsedFunctions []*storepb.FunctionMetadata, _ string) {
+ // Create maps for easier lookup
+ dbFuncMap := make(map[string]*storepb.FunctionMetadata)
+ for _, fn := range dbFunctions {
+ dbFuncMap[fn.Name] = fn
+ }
+
+ parsedFuncMap := make(map[string]*storepb.FunctionMetadata)
+ for _, fn := range parsedFunctions {
+ parsedFuncMap[fn.Name] = fn
+ }
+
+ // Compare each parsed function
+ for funcName, parsedFunc := range parsedFuncMap {
+ dbFunc, exists := dbFuncMap[funcName]
+ require.True(t, exists, "parsed function %s not found in database metadata", funcName)
+
+ // Compare function properties
+ require.Equal(t, dbFunc.Name, parsedFunc.Name, "function names should match")
+ require.NotEmpty(t, parsedFunc.Definition, "parsed function definition should not be empty")
+ require.NotEmpty(t, dbFunc.Definition, "database function definition should not be empty")
+
+ t.Logf("✓ Function %s: definition length=%d", funcName, len(parsedFunc.Definition))
+ }
+}
+
+// compareProcedures compares procedure metadata
+func compareProcedures(t *testing.T, dbProcedures, parsedProcedures []*storepb.ProcedureMetadata, _ string) {
+ // Create maps for easier lookup
+ dbProcMap := make(map[string]*storepb.ProcedureMetadata)
+ for _, proc := range dbProcedures {
+ dbProcMap[proc.Name] = proc
+ }
+
+ parsedProcMap := make(map[string]*storepb.ProcedureMetadata)
+ for _, proc := range parsedProcedures {
+ parsedProcMap[proc.Name] = proc
+ }
+
+ // Compare each parsed procedure
+ for procName, parsedProc := range parsedProcMap {
+ dbProc, exists := dbProcMap[procName]
+ require.True(t, exists, "parsed procedure %s not found in database metadata", procName)
+
+ // Compare procedure properties
+ require.Equal(t, dbProc.Name, parsedProc.Name, "procedure names should match")
+ require.NotEmpty(t, parsedProc.Definition, "parsed procedure definition should not be empty")
+ require.NotEmpty(t, dbProc.Definition, "database procedure definition should not be empty")
+
+ t.Logf("✓ Procedure %s: definition length=%d", procName, len(parsedProc.Definition))
+ }
+}
+
+// Helper functions for normalization
+
+func normalizeOracleDataType(dataType string) string {
+ // Normalize NUMBER types - Oracle may return NUMBER(*,0) as NUMBER
+ if strings.HasPrefix(dataType, "NUMBER(") && strings.Contains(dataType, "*") {
+ dataType = "NUMBER"
+ }
+
+ return dataType
+}
+
+func normalizeIndexType(indexType string) string {
+ indexType = strings.ToUpper(strings.TrimSpace(indexType))
+ if indexType == "" {
+ return "NORMAL" // Oracle default
+ }
+ // Oracle uses "NORMAL" for standard B-tree indexes
+ if indexType == "BTREE" {
+ return "NORMAL"
+ }
+ return indexType
+}
+
+func normalizeExpression(expr string) string {
+ expr = strings.ToUpper(strings.TrimSpace(expr))
+ expr = strings.ReplaceAll(expr, "\"", "")
+ expr = strings.ReplaceAll(expr, " ", "")
+ return expr
+}
+
+func normalizeCheckExpression(expr string) string {
+ expr = strings.TrimSpace(expr)
+
+ // Remove outer parentheses if present
+ if strings.HasPrefix(expr, "(") && strings.HasSuffix(expr, ")") {
+ expr = expr[1 : len(expr)-1]
+ }
+
+ // Normalize spaces and quotes
+ expr = strings.ReplaceAll(expr, "\"", "")
+ expr = strings.ReplaceAll(expr, " ", "")
+ expr = strings.ToUpper(expr)
+
+ return expr
+}
+
+func compareColumnDefaults(t *testing.T, dbCol, parsedCol *storepb.ColumnMetadata, tableName, colName string) {
+ // Compare default expressions
+ dbDefault := getColumnDefaultString(dbCol)
+ parsedDefault := getColumnDefaultString(parsedCol)
+
+ // Normalize for comparison
+ dbDefault = normalizeDefaultExpression(dbDefault)
+ parsedDefault = normalizeDefaultExpression(parsedDefault)
+
+ if dbDefault != "" || parsedDefault != "" {
+ require.Equal(t, dbDefault, parsedDefault,
+ "default values should match for column %s.%s: db='%s', parsed='%s'",
+ tableName, colName, dbDefault, parsedDefault)
+ }
+}
+
+func getColumnDefaultString(col *storepb.ColumnMetadata) string {
+ if col.DefaultExpression != "" {
+ return col.DefaultExpression
+ }
+ if col.Default != "" {
+ return col.Default
+ }
+ if col.DefaultNull {
+ return "NULL"
+ }
+ return ""
+}
+
+func normalizeDefaultExpression(expr string) string {
+ expr = strings.TrimSpace(expr)
+ expr = strings.ToUpper(expr)
+
+ // Handle Oracle system functions
+ if strings.Contains(expr, "SYSDATE") {
+ return "SYSDATE"
+ }
+ if strings.Contains(expr, "CURRENT_TIMESTAMP") {
+ return "CURRENT_TIMESTAMP"
+ }
+ if strings.Contains(expr, "USER") {
+ return "USER"
+ }
+
+ // Remove quotes from simple values
+ if strings.HasPrefix(expr, "'") && strings.HasSuffix(expr, "'") {
+ expr = expr[1 : len(expr)-1]
+ }
+
+ // Remove parentheses from simple expressions
+ if strings.HasPrefix(expr, "(") && strings.HasSuffix(expr, ")") {
+ expr = expr[1 : len(expr)-1]
+ }
+
+ return expr
+}