MCPcopy
hub / github.com/sqlc-dev/sqlc / createView

Method createView

internal/sql/catalog/view.go:8–52  ·  view source on GitHub ↗
(stmt *ast.ViewStmt, colGen columnGenerator)

Source from the content-addressed store, hash-verified

6)
7
8func (c *Catalog) createView(stmt *ast.ViewStmt, colGen columnGenerator) error {
9 cols, err := colGen.OutputColumns(stmt.Query)
10 if err != nil {
11 return err
12 }
13
14 catName := ""
15 if stmt.View.Catalogname != nil {
16 catName = *stmt.View.Catalogname
17 }
18 schemaName := ""
19 if stmt.View.Schemaname != nil {
20 schemaName = *stmt.View.Schemaname
21 }
22
23 tbl := Table{
24 Rel: &ast.TableName{
25 Catalog: catName,
26 Schema: schemaName,
27 Name: *stmt.View.Relname,
28 },
29 Columns: cols,
30 }
31
32 ns := tbl.Rel.Schema
33 if ns == "" {
34 ns = c.DefaultSchema
35 }
36 schema, err := c.getSchema(ns)
37 if err != nil {
38 return err
39 }
40 _, existingIdx, err := schema.getTable(tbl.Rel)
41 if err == nil && !stmt.Replace {
42 return sqlerr.RelationExists(tbl.Rel.Name)
43 }
44
45 if stmt.Replace && err == nil {
46 schema.Tables[existingIdx] = &tbl
47 } else {
48 schema.Tables = append(schema.Tables, &tbl)
49 }
50
51 return nil
52}

Callers 1

UpdateMethod · 0.95

Calls 4

getSchemaMethod · 0.95
RelationExistsFunction · 0.92
OutputColumnsMethod · 0.65
getTableMethod · 0.45

Tested by

no test coverage detected