|
| 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 | + "strconv" |
| 19 | + |
| 20 | + "github.com/dolthub/go-mysql-server/sql" |
| 21 | + "github.com/dolthub/go-mysql-server/sql/information_schema" |
| 22 | + |
| 23 | + "github.com/dolthub/doltgresql/server/functions" |
| 24 | + pgtypes "github.com/dolthub/doltgresql/server/types" |
| 25 | +) |
| 26 | + |
| 27 | +const SequencesTableName = "sequences" |
| 28 | + |
| 29 | +// newSequencesTable returns a InformationSchemaTable for MySQL. |
| 30 | +func newSequencesTable() *information_schema.InformationSchemaTable { |
| 31 | + return &information_schema.InformationSchemaTable{ |
| 32 | + TableName: SequencesTableName, |
| 33 | + TableSchema: sequencesSchema, |
| 34 | + Reader: sequencesRowIter, |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +// tablesSchema is the schema for the information_schema.TABLES table. |
| 39 | +var sequencesSchema = sql.Schema{ |
| 40 | + {Name: "sequence_catalog", Type: sql_identifier, Default: nil, Nullable: true, Source: SequencesTableName}, |
| 41 | + {Name: "sequence_schema", Type: sql_identifier, Default: nil, Nullable: true, Source: SequencesTableName}, |
| 42 | + {Name: "sequence_name", Type: sql_identifier, Default: nil, Nullable: true, Source: SequencesTableName}, |
| 43 | + {Name: "data_type", Type: character_data, Default: nil, Nullable: true, Source: SequencesTableName}, |
| 44 | + {Name: "numeric_precision", Type: cardinal_number, Default: nil, Nullable: true, Source: SequencesTableName}, |
| 45 | + {Name: "numeric_precision_radix", Type: cardinal_number, Default: nil, Nullable: true, Source: SequencesTableName}, |
| 46 | + {Name: "numeric_scale", Type: cardinal_number, Default: nil, Nullable: true, Source: SequencesTableName}, |
| 47 | + {Name: "start_value", Type: character_data, Default: nil, Nullable: true, Source: SequencesTableName}, |
| 48 | + {Name: "minimum_value", Type: character_data, Default: nil, Nullable: true, Source: SequencesTableName}, |
| 49 | + {Name: "maximum_value", Type: character_data, Default: nil, Nullable: true, Source: SequencesTableName}, |
| 50 | + {Name: "increment", Type: character_data, Default: nil, Nullable: true, Source: SequencesTableName}, |
| 51 | + {Name: "cycle_option", Type: yes_or_no, Default: nil, Nullable: true, Source: SequencesTableName}, |
| 52 | +} |
| 53 | + |
| 54 | +// sequencesRowIter implements the sql.RowIter for the information_schema.Sequences table. |
| 55 | +func sequencesRowIter(ctx *sql.Context, _ sql.Catalog) (sql.RowIter, error) { |
| 56 | + var rows []sql.Row |
| 57 | + |
| 58 | + err := functions.IterateCurrentDatabase(ctx, functions.Callbacks{ |
| 59 | + Sequence: func(_ *sql.Context, schema functions.ItemSchema, sequence functions.ItemSequence) (cont bool, err error) { |
| 60 | + sequenceType := pgtypes.GetTypeByID(sequence.Item.DataTypeID) |
| 61 | + |
| 62 | + var precision, radix, scale interface{} |
| 63 | + if sequenceType != nil { |
| 64 | + precision, radix, scale = getColumnPrecisionAndScale(sequenceType) |
| 65 | + } |
| 66 | + |
| 67 | + cycleOption := "NO" |
| 68 | + if sequence.Item.Cycle { |
| 69 | + cycleOption = "YES" |
| 70 | + } |
| 71 | + |
| 72 | + rows = append(rows, sql.Row{ |
| 73 | + schema.Item.Name(), //sequence_catalog |
| 74 | + schema.Item.SchemaName(), //sequence_schema |
| 75 | + sequence.Item.Id.SequenceName(), //sequence_name |
| 76 | + sequenceType.String(), //data_type |
| 77 | + precision, //numeric_precision |
| 78 | + radix, //numeric_precision_radix |
| 79 | + scale, //numeric_scale |
| 80 | + strconv.FormatInt(sequence.Item.Start, 10), //start_value |
| 81 | + strconv.FormatInt(sequence.Item.Minimum, 10), //minimum_value |
| 82 | + strconv.FormatInt(sequence.Item.Maximum, 10), //maximum_value |
| 83 | + strconv.FormatInt(sequence.Item.Increment, 10), //increment |
| 84 | + cycleOption, //cycle_option |
| 85 | + }) |
| 86 | + return true, nil |
| 87 | + }, |
| 88 | + }) |
| 89 | + if err != nil { |
| 90 | + return nil, err |
| 91 | + } |
| 92 | + |
| 93 | + return sql.RowsToRowIter(rows...), nil |
| 94 | +} |
0 commit comments