|
| 1 | +// Copyright 2026 Dolthub, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package information_schema |
| 16 | + |
| 17 | +import ( |
| 18 | + "github.com/dolthub/go-mysql-server/sql" |
| 19 | + "github.com/dolthub/go-mysql-server/sql/information_schema" |
| 20 | + |
| 21 | + "github.com/dolthub/doltgresql/server/functions" |
| 22 | +) |
| 23 | + |
| 24 | +// ConstraintColumnUsageViewName is the name of the CONSTRAINT_COLUMN_USAGE view. |
| 25 | +const ConstraintColumnUsageViewName = "constraint_column_usage" |
| 26 | + |
| 27 | +// newConstraintColumnUsageView creates a new information_schema.CONSTRAINT_COLUMN_USAGE view. |
| 28 | +func newConstraintColumnUsageView() *information_schema.InformationSchemaTable { |
| 29 | + return &information_schema.InformationSchemaTable{ |
| 30 | + TableName: ConstraintColumnUsageViewName, |
| 31 | + TableSchema: constraintColumnUsageSchema, |
| 32 | + Reader: constraintColumnUsageRowIter, |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +// constraintColumnUsage is the schema for the information_schema.CONSTRAINT_COLUMN_USAGE view. |
| 37 | +var constraintColumnUsageSchema = sql.Schema{ |
| 38 | + {Name: "table_catalog", Type: sql_identifier, Default: nil, Nullable: true, Source: ConstraintColumnUsageViewName}, |
| 39 | + {Name: "table_schema", Type: sql_identifier, Default: nil, Nullable: true, Source: ConstraintColumnUsageViewName}, |
| 40 | + {Name: "table_name", Type: sql_identifier, Default: nil, Nullable: true, Source: ConstraintColumnUsageViewName}, |
| 41 | + {Name: "column_name", Type: character_data, Default: nil, Nullable: true, Source: ConstraintColumnUsageViewName}, |
| 42 | + {Name: "constraint_catalog", Type: character_data, Default: nil, Nullable: true, Source: ConstraintColumnUsageViewName}, |
| 43 | + {Name: "constraint_schema", Type: yes_or_no, Default: nil, Nullable: true, Source: ConstraintColumnUsageViewName}, |
| 44 | + {Name: "constraint_name", Type: yes_or_no, Default: nil, Nullable: true, Source: ConstraintColumnUsageViewName}, |
| 45 | +} |
| 46 | + |
| 47 | +// constraintColumnUsageRowIter implements the sql.RowIter for the information_schema.CONSTRAINT_COLUMN_USAGE view. |
| 48 | +func constraintColumnUsageRowIter(ctx *sql.Context, catalog sql.Catalog) (sql.RowIter, error) { |
| 49 | + var rows []sql.Row |
| 50 | + |
| 51 | + err := functions.IterateCurrentDatabase(ctx, functions.Callbacks{ |
| 52 | + Check: func(ctx *sql.Context, schema functions.ItemSchema, table functions.ItemTable, check functions.ItemCheck) (cont bool, err error) { |
| 53 | + |
| 54 | + // TODO: Fill out the rest of the columns. |
| 55 | + rows = append(rows, sql.Row{ |
| 56 | + schema.Item.Name(), // table_catalog |
| 57 | + schema.Item.SchemaName(), // table_schema |
| 58 | + table.Item.Name(), // table_name |
| 59 | + nil, // column_name |
| 60 | + nil, // constraint_catalog |
| 61 | + nil, // constraint_schema |
| 62 | + check.Item.Name, // constraint_name |
| 63 | + }) |
| 64 | + return true, nil |
| 65 | + }, |
| 66 | + }) |
| 67 | + if err != nil { |
| 68 | + return nil, err |
| 69 | + } |
| 70 | + |
| 71 | + return sql.RowsToRowIter(rows...), nil |
| 72 | +} |
0 commit comments