|
| 1 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 2 | +// you may not use this file except in compliance with the License. |
| 3 | +// You may obtain a copy of the License at |
| 4 | +// |
| 5 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +// |
| 7 | +// Unless required by applicable law or agreed to in writing, software |
| 8 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +// See the License for the specific language governing permissions and |
| 11 | +// limitations under the License. |
| 12 | + |
| 13 | +use super::{SQLIdent, SQLObjectName}; |
| 14 | + |
| 15 | +#[derive(Debug, Clone, PartialEq)] |
| 16 | +pub enum AlterOperation { |
| 17 | + AddConstraint(TableKey), |
| 18 | + RemoveConstraint { name: SQLIdent }, |
| 19 | +} |
| 20 | + |
| 21 | +impl ToString for AlterOperation { |
| 22 | + fn to_string(&self) -> String { |
| 23 | + match self { |
| 24 | + AlterOperation::AddConstraint(table_key) => { |
| 25 | + format!("ADD CONSTRAINT {}", table_key.to_string()) |
| 26 | + } |
| 27 | + AlterOperation::RemoveConstraint { name } => format!("REMOVE CONSTRAINT {}", name), |
| 28 | + } |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +#[derive(Debug, Clone, PartialEq)] |
| 33 | +pub struct Key { |
| 34 | + pub name: SQLIdent, |
| 35 | + pub columns: Vec<SQLIdent>, |
| 36 | +} |
| 37 | + |
| 38 | +#[derive(Debug, Clone, PartialEq)] |
| 39 | +pub enum TableKey { |
| 40 | + PrimaryKey(Key), |
| 41 | + UniqueKey(Key), |
| 42 | + Key(Key), |
| 43 | + ForeignKey { |
| 44 | + key: Key, |
| 45 | + foreign_table: SQLObjectName, |
| 46 | + referred_columns: Vec<SQLIdent>, |
| 47 | + }, |
| 48 | +} |
| 49 | + |
| 50 | +impl ToString for TableKey { |
| 51 | + fn to_string(&self) -> String { |
| 52 | + match self { |
| 53 | + TableKey::PrimaryKey(ref key) => { |
| 54 | + format!("{} PRIMARY KEY ({})", key.name, key.columns.join(", ")) |
| 55 | + } |
| 56 | + TableKey::UniqueKey(ref key) => { |
| 57 | + format!("{} UNIQUE KEY ({})", key.name, key.columns.join(", ")) |
| 58 | + } |
| 59 | + TableKey::Key(ref key) => format!("{} KEY ({})", key.name, key.columns.join(", ")), |
| 60 | + TableKey::ForeignKey { |
| 61 | + key, |
| 62 | + foreign_table, |
| 63 | + referred_columns, |
| 64 | + } => format!( |
| 65 | + "{} FOREIGN KEY ({}) REFERENCES {}({})", |
| 66 | + key.name, |
| 67 | + key.columns.join(", "), |
| 68 | + foreign_table.to_string(), |
| 69 | + referred_columns.join(", ") |
| 70 | + ), |
| 71 | + } |
| 72 | + } |
| 73 | +} |
0 commit comments