|
| 1 | +use super::*; |
| 2 | + |
| 3 | +impl TableBuilder for BigQueryQueryBuilder { |
| 4 | + fn prepare_column_def(&self, column_def: &ColumnDef, sql: &mut dyn SqlWriter) { |
| 5 | + column_def.name.prepare(sql.as_writer(), self.quote()); |
| 6 | + |
| 7 | + if let Some(column_type) = &column_def.types { |
| 8 | + write!(sql, " ").unwrap(); |
| 9 | + self.prepare_column_type(column_type, sql); |
| 10 | + } |
| 11 | + |
| 12 | + for column_spec in column_def.spec.iter() { |
| 13 | + if let ColumnSpec::PrimaryKey = column_spec { |
| 14 | + continue; |
| 15 | + } |
| 16 | + if let ColumnSpec::AutoIncrement = column_spec { |
| 17 | + continue; |
| 18 | + } |
| 19 | + if let ColumnSpec::Comment(_) = column_spec { |
| 20 | + continue; |
| 21 | + } |
| 22 | + write!(sql, " ").unwrap(); |
| 23 | + self.prepare_column_spec(column_spec, sql); |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + fn prepare_column_type(&self, column_type: &ColumnType, sql: &mut dyn SqlWriter) { |
| 28 | + write!( |
| 29 | + sql, |
| 30 | + "{}", |
| 31 | + match column_type { |
| 32 | + ColumnType::Char(length) => match length { |
| 33 | + Some(length) => format!("STRING({length})"), |
| 34 | + None => "STRING".into(), |
| 35 | + }, |
| 36 | + ColumnType::String(length) => match length { |
| 37 | + Some(length) => format!("STRING({length})"), |
| 38 | + None => "STRING".into(), |
| 39 | + }, |
| 40 | + ColumnType::Text => "STRING".into(), |
| 41 | + ColumnType::TinyInteger | ColumnType::TinyUnsigned => "INT64".into(), |
| 42 | + ColumnType::SmallInteger | ColumnType::SmallUnsigned => "INT64".into(), |
| 43 | + ColumnType::Integer | ColumnType::Unsigned => "INT64".into(), |
| 44 | + ColumnType::BigInteger | ColumnType::BigUnsigned => "INT64".into(), |
| 45 | + ColumnType::Float => "FLOAT64".into(), |
| 46 | + ColumnType::Double => "FLOAT64".into(), |
| 47 | + ColumnType::Decimal(precision) | ColumnType::Money(precision) => match precision { |
| 48 | + Some((precision, scale)) => match scale { |
| 49 | + 0..=9 if precision.max(&1) <= precision && precision <= &(scale + 29) => |
| 50 | + format!("NUMERIC({precision}, {scale})"), |
| 51 | + 10..=38 if precision.max(&1) <= precision && precision <= &(scale + 38) => |
| 52 | + format!("BIGNUMERIC({precision}, {scale})"), |
| 53 | + _ => panic!("Invalid precision and scale for NUMERIC type"), |
| 54 | + }, |
| 55 | + None => "BIGNUMERIC".into(), |
| 56 | + }, |
| 57 | + ColumnType::DateTime => "DATETIME".into(), |
| 58 | + ColumnType::Timestamp => "TIMESTAMP".into(), |
| 59 | + ColumnType::TimestampWithTimeZone => "TIMESTAMP".into(), |
| 60 | + ColumnType::Time => "TIME".into(), |
| 61 | + ColumnType::Date => "DATE".into(), |
| 62 | + ColumnType::Interval(_, _) => "INTERVAL".into(), |
| 63 | + ColumnType::Binary(blob_size) => match blob_size { |
| 64 | + BlobSize::Blob(Some(length)) => format!("BYTES({length})"), |
| 65 | + _ => "BYTES".into(), |
| 66 | + }, |
| 67 | + ColumnType::VarBinary(length) => format!("BYTES({length})"), |
| 68 | + ColumnType::Boolean => "BOOL".into(), |
| 69 | + ColumnType::Json => "JSON".into(), |
| 70 | + ColumnType::JsonBinary => "JSON".into(), |
| 71 | + ColumnType::Uuid => "STRING(36)".into(), |
| 72 | + ColumnType::Custom(iden) => iden.to_string(), |
| 73 | + ColumnType::Enum { .. } => "STRING".into(), |
| 74 | + ColumnType::Array(col_type) => { |
| 75 | + let mut sql = String::new(); |
| 76 | + self.prepare_column_type(col_type, &mut sql); |
| 77 | + format!("ARRAY<{sql}>") |
| 78 | + } |
| 79 | + ColumnType::Cidr => unimplemented!("Cidr is not available in BigQuery."), |
| 80 | + ColumnType::Inet => unimplemented!("Inet is not available in BigQuery."), |
| 81 | + ColumnType::MacAddr => unimplemented!("MacAddr is not available in BigQuery."), |
| 82 | + ColumnType::Year(_) => unimplemented!("Year is not available in BigQuery."), |
| 83 | + ColumnType::Bit(_) => unimplemented!("Bit is not available in BigQuery."), |
| 84 | + ColumnType::VarBit(_) => unimplemented!("VarBit is not available in BigQuery."), |
| 85 | + } |
| 86 | + ) |
| 87 | + .unwrap() |
| 88 | + } |
| 89 | + |
| 90 | + fn column_spec_auto_increment_keyword(&self) -> &str { |
| 91 | + panic!("BigQuery does not support auto increment"); |
| 92 | + } |
| 93 | + |
| 94 | + fn prepare_table_drop_opt(&self, _drop_opt: &TableDropOpt, _sql: &mut dyn SqlWriter) { |
| 95 | + panic!("BigQuery does not support table drop option"); |
| 96 | + } |
| 97 | + |
| 98 | + fn prepare_table_alter_statement(&self, alter: &TableAlterStatement, sql: &mut dyn SqlWriter) { |
| 99 | + if alter.options.is_empty() { |
| 100 | + panic!("No alter option found") |
| 101 | + } |
| 102 | + write!(sql, "ALTER TABLE ").unwrap(); |
| 103 | + if let Some(table) = &alter.table { |
| 104 | + self.prepare_table_ref_table_stmt(table, sql); |
| 105 | + write!(sql, " ").unwrap(); |
| 106 | + } |
| 107 | + alter.options.iter().fold(true, |first, option| { |
| 108 | + if !first { |
| 109 | + write!(sql, ", ").unwrap(); |
| 110 | + }; |
| 111 | + match option { |
| 112 | + TableAlterOption::AddColumn(AddColumnOption { |
| 113 | + column, |
| 114 | + if_not_exists: _, |
| 115 | + }) => { |
| 116 | + write!(sql, "ADD COLUMN ").unwrap(); |
| 117 | + self.prepare_column_def(column, sql); |
| 118 | + } |
| 119 | + TableAlterOption::ModifyColumn(column_def) => { |
| 120 | + if let Some(types) = &column_def.types { |
| 121 | + write!(sql, "ALTER COLUMN ").unwrap(); |
| 122 | + column_def.name.prepare(sql.as_writer(), self.quote()); |
| 123 | + write!(sql, " SET DATA TYPE ").unwrap(); |
| 124 | + self.prepare_column_type(types, sql); |
| 125 | + } |
| 126 | + let first = column_def.types.is_none(); |
| 127 | + |
| 128 | + column_def.spec.iter().fold(first, |first, column_spec| { |
| 129 | + if !first { |
| 130 | + write!(sql, ", ").unwrap(); |
| 131 | + } |
| 132 | + match column_spec { |
| 133 | + ColumnSpec::AutoIncrement => {} |
| 134 | + ColumnSpec::Null => { |
| 135 | + write!(sql, "ALTER COLUMN ").unwrap(); |
| 136 | + column_def.name.prepare(sql.as_writer(), self.quote()); |
| 137 | + write!(sql, " DROP NOT NULL").unwrap(); |
| 138 | + } |
| 139 | + ColumnSpec::NotNull => { |
| 140 | + panic!("BigQuery doesn't support changing to REQUIRED") |
| 141 | + } |
| 142 | + ColumnSpec::Default(v) => { |
| 143 | + write!(sql, "ALTER COLUMN ").unwrap(); |
| 144 | + column_def.name.prepare(sql.as_writer(), self.quote()); |
| 145 | + write!(sql, " SET DEFAULT ").unwrap(); |
| 146 | + QueryBuilder::prepare_simple_expr(self, v, sql); |
| 147 | + } |
| 148 | + ColumnSpec::UniqueKey => { |
| 149 | + panic!("BigQuery doesn't support adding unique constraint") |
| 150 | + } |
| 151 | + ColumnSpec::PrimaryKey => { |
| 152 | + panic!("BigQuery doesn't support adding primary key constraint") |
| 153 | + } |
| 154 | + ColumnSpec::Check(_check) => { |
| 155 | + panic!("BigQuery doesn't support adding check constraint") |
| 156 | + } |
| 157 | + ColumnSpec::Generated { .. } => {} |
| 158 | + ColumnSpec::Extra(string) => write!(sql, "{string}").unwrap(), |
| 159 | + ColumnSpec::Comment(_) => {} |
| 160 | + } |
| 161 | + false |
| 162 | + }); |
| 163 | + } |
| 164 | + TableAlterOption::RenameColumn(from_name, to_name) => { |
| 165 | + write!(sql, "RENAME COLUMN ").unwrap(); |
| 166 | + from_name.prepare(sql.as_writer(), self.quote()); |
| 167 | + write!(sql, " TO ").unwrap(); |
| 168 | + to_name.prepare(sql.as_writer(), self.quote()); |
| 169 | + } |
| 170 | + TableAlterOption::DropColumn(col_name) => { |
| 171 | + write!(sql, "DROP COLUMN ").unwrap(); |
| 172 | + col_name.prepare(sql.as_writer(), self.quote()); |
| 173 | + } |
| 174 | + TableAlterOption::DropForeignKey(_) => { |
| 175 | + panic!("BigQuery does not support modification of foreign key constraints to existing tables"); |
| 176 | + } |
| 177 | + TableAlterOption::AddForeignKey(_) => { |
| 178 | + panic!("BigQuery does not support modification of foreign key constraints to existing tables"); |
| 179 | + } |
| 180 | + } |
| 181 | + false |
| 182 | + }); |
| 183 | + } |
| 184 | + |
| 185 | + fn prepare_table_rename_statement( |
| 186 | + &self, |
| 187 | + rename: &TableRenameStatement, |
| 188 | + sql: &mut dyn SqlWriter, |
| 189 | + ) { |
| 190 | + write!(sql, "ALTER TABLE ").unwrap(); |
| 191 | + if let Some(from_name) = &rename.from_name { |
| 192 | + self.prepare_table_ref_table_stmt(from_name, sql); |
| 193 | + } |
| 194 | + write!(sql, " RENAME TO ").unwrap(); |
| 195 | + if let Some(to_name) = &rename.to_name { |
| 196 | + self.prepare_table_ref_table_stmt(to_name, sql); |
| 197 | + } |
| 198 | + } |
| 199 | +} |
0 commit comments