Skip to content

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

feat(completion): support MySQL views (#9212)

rebeliceb32542cde955
diff --git a/backend/plugin/parser/mysql/completion.go b/backend/plugin/parser/mysql/completion.go
index 6b171d8be4..a142b8a8a3 100644
--- a/backend/plugin/parser/mysql/completion.go
+++ b/backend/plugin/parser/mysql/completion.go
@@ -451,6 +451,23 @@ func (c *Completer) convertCandidates(candidates *base.CandidatesCollection) ([]
}
// TODO: special handling for triggers.
+
+ case mysql.MySQLParserRULE_viewRef:
+ schema, _, flags := c.determineSchemaTableQualifier()
+
+ if flags&ObjectFlagsShowFirst != 0 {
+ schemaEntries.insertDatabases(c)
+ }
+
+ if flags&ObjectFlagsShowSecond != 0 {
+ schemas := make(map[string]bool)
+ if len(schema) != 0 {
+ schemas[schema] = true
+ } else {
+ schemas[c.defaultDatabase] = true
+ }
+ viewEntries.insertViews(c, schemas)
+ }
}
}
@@ -835,8 +852,15 @@ func (m CompletionMap) insertTables(c *Completer, schemas map[string]bool) {
}
}
-func (CompletionMap) insertViews(_ *Completer, _ map[string]bool) {
- // TODO: load views
+func (m CompletionMap) insertViews(c *Completer, schemas map[string]bool) {
+ for schema := range schemas {
+ for _, view := range c.listViews(schema) {
+ m.Insert(base.Candidate{
+ Type: base.CandidateTypeView,
+ Text: view,
+ })
+ }
+ }
}
func (m CompletionMap) insertColumns(c *Completer, schemas, tables map[string]bool) {
@@ -875,6 +899,18 @@ func (c *Completer) listTables(database string) []string {
return c.metadataCache[database].GetSchema("").ListTableNames()
}
+func (c *Completer) listViews(database string) []string {
+ if _, exists := c.metadataCache[database]; !exists {
+ metadata, err := c.getMetadata(c.ctx, database)
+ if err != nil || metadata == nil {
+ return nil
+ }
+ c.metadataCache[database] = metadata
+ }
+
+ return c.metadataCache[database].GetSchema("").ListViewNames()
+}
+
func (c *Completer) listColumns(databases, tables map[string]bool) []string {
var result []string
for database := range databases {
diff --git a/backend/plugin/parser/mysql/completion_test.go b/backend/plugin/parser/mysql/completion_test.go
index f737ce8be3..d7f2217d8e 100644
--- a/backend/plugin/parser/mysql/completion_test.go
+++ b/backend/plugin/parser/mysql/completion_test.go
@@ -97,6 +97,15 @@ func getMetadataForTest(_ context.Context, databaseName string) (*model.Database
},
},
},
+ Views: []*storepb.ViewMetadata{
+ {
+ Name: "v1",
+ Definition: `CREATE VIEW v1 AS
+ SELECT *
+ FROM t1
+ `,
+ },
+ },
},
},
}), nil
diff --git a/backend/plugin/parser/mysql/test-data/test_completion.yaml b/backend/plugin/parser/mysql/test-data/test_completion.yaml
index 1e0c3d3348..87720bd7ad 100644
--- a/backend/plugin/parser/mysql/test-data/test_completion.yaml
+++ b/backend/plugin/parser/mysql/test-data/test_completion.yaml
@@ -6,6 +6,8 @@
type: TABLE
- text: t2
type: TABLE
+ - text: v1
+ type: VIEW
- input: SELECT | FROM t1
want:
- text: runtimeFunction()
@@ -18,6 +20,8 @@
type: TABLE
- text: c1
type: COLUMN
+ - text: v1
+ type: VIEW
- input: SELECT | FROM t2 x
want:
- text: runtimeFunction()
@@ -34,3 +38,5 @@
type: COLUMN
- text: c2
type: COLUMN
+ - text: v1
+ type: VIEW
diff --git a/backend/store/model/database.go b/backend/store/model/database.go
index 3f98fe53c9..5a6920684b 100644
--- a/backend/store/model/database.go
+++ b/backend/store/model/database.go
@@ -174,7 +174,8 @@ func NewDatabaseMetadata(metadata *storepb.DatabaseSchemaMetadata) *DatabaseMeta
}
for _, schema := range metadata.Schemas {
schemaMetadata := &SchemaMetadata{
- internal: make(map[string]*TableMetadata),
+ internalTables: make(map[string]*TableMetadata),
+ internalViews: make(map[string]*ViewMetadata),
}
for _, table := range schema.Tables {
tableMetadata := &TableMetadata{
@@ -184,7 +185,10 @@ func NewDatabaseMetadata(metadata *storepb.DatabaseSchemaMetadata) *DatabaseMeta
tableMetadata.internal[column.Name] = column
tableMetadata.columns = append(tableMetadata.columns, column)
}
- schemaMetadata.internal[table.Name] = tableMetadata
+ schemaMetadata.internalTables[table.Name] = tableMetadata
+ }
+ for _, view := range schema.Views {
+ schemaMetadata.internalViews[view.Name] = &ViewMetadata{}
}
databaseMetadata.internal[schema.Name] = schemaMetadata
}
@@ -198,18 +202,19 @@ func (d *DatabaseMetadata) GetSchema(name string) *SchemaMetadata {
// SchemaMetadata is the metadata for a schema.
type SchemaMetadata struct {
- internal map[string]*TableMetadata
+ internalTables map[string]*TableMetadata
+ internalViews map[string]*ViewMetadata
}
// GetTable gets the schema by name.
func (s *SchemaMetadata) GetTable(name string) *TableMetadata {
- return s.internal[name]
+ return s.internalTables[name]
}
// ListTableNames lists the table names.
func (s *SchemaMetadata) ListTableNames() []string {
var result []string
- for tableName := range s.internal {
+ for tableName := range s.internalTables {
result = append(result, tableName)
}
@@ -217,6 +222,17 @@ func (s *SchemaMetadata) ListTableNames() []string {
return result
}
+// ListViewNames lists the view names.
+func (s *SchemaMetadata) ListViewNames() []string {
+ var result []string
+ for viewName := range s.internalViews {
+ result = append(result, viewName)
+ }
+
+ sort.Strings(result)
+ return result
+}
+
// TableMetadata is the metadata for a table.
type TableMetadata struct {
internal map[string]*storepb.ColumnMetadata
@@ -232,3 +248,7 @@ func (t *TableMetadata) GetColumn(name string) *storepb.ColumnMetadata {
func (t *TableMetadata) GetColumns() []*storepb.ColumnMetadata {
return t.columns
}
+
+// ViewMetadata is the metadata for a view.
+type ViewMetadata struct {
+}