use alloc::string::String;
use alloc::vec::Vec;
use core::str::FromStr;
use super::*;
#[derive(Debug, Clone, Default)]
pub struct TableBlock {
pub span_range: SpanRange,
pub cols: Vec<TableColumn>,
pub ident: TableIdent,
pub note: Option<NoteBlock>,
pub indexes: Option<IndexesBlock>,
pub settings: Option<TableSettings>,
}
#[derive(Debug, Clone, Default)]
pub struct TableSettings {
pub span_range: SpanRange,
pub attributes: Vec<Attribute>,
}
#[derive(Debug, Clone, Default)]
pub struct TableColumn {
pub span_range: SpanRange,
pub name: Ident,
pub r#type: ColumnType,
pub settings: Option<ColumnSettings>,
}
#[derive(Debug, Clone, Default)]
pub struct ColumnType {
pub span_range: SpanRange,
pub raw: String,
pub type_name: ColumnTypeName,
pub args: Vec<Value>,
pub arrays: Vec<Option<u32>>,
}
#[derive(Debug, PartialEq, Eq, Clone, Default)]
pub enum ColumnTypeName {
#[default]
Undef,
Raw(String),
Enum(String),
Bit,
Varbit,
Char,
VarChar,
Box,
Cidr,
Circle,
Inet,
Line,
LineSegment,
MacAddr,
MacAddr8,
Money,
Path,
PGLongSequenceNumber,
PGSnapshot,
Point,
Polygon,
TSQuery,
TSVector,
SmallSerial,
Serial,
BigSerial,
SmallInt,
Integer,
BigInt,
Real,
DoublePrecision,
Bool,
ByteArray,
Date,
Text,
Time,
Timetz,
Timestamp,
Timestamptz,
Uuid,
Json,
Jsonb,
Decimal,
Xml,
}
impl FromStr for ColumnTypeName {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
let v = match s {
"bit" => Self::Bit,
"varbit" | "bit varying" => Self::Varbit,
"char" | "character" => Self::Char,
"varchar" | "character varying" => Self::VarChar,
"box" => Self::Box,
"cidr" => Self::Cidr,
"circle" => Self::Circle,
"inet" => Self::Inet,
"line" => Self::Line,
"lseg" => Self::LineSegment,
"macaddr" => Self::MacAddr,
"macaddr8" => Self::MacAddr8,
"money" => Self::Money,
"path" => Self::Path,
"pg_lsn" => Self::PGLongSequenceNumber,
"pg_snapshot" => Self::PGSnapshot,
"point" => Self::Point,
"polygon" => Self::Polygon,
"tsquery" => Self::TSQuery,
"tsvector" => Self::TSVector,
"smallserial" | "serial2" => Self::SmallSerial,
"serial" | "serial4" => Self::Serial,
"bigserial" | "serial8" => Self::BigSerial,
"smallint" | "int2" => Self::SmallInt,
"integer" | "int" | "int4" => Self::Integer,
"bigint" | "int8" => Self::BigInt,
"real" | "float4" => Self::Real,
"double precision" | "float8" => Self::DoublePrecision,
"bool" | "boolean" => Self::Bool,
"bytea" => Self::ByteArray,
"date" => Self::Date,
"text" => Self::Text,
"time" | "time without time zone" => Self::Time,
"timetz" | "time with time zone" => Self::Timetz,
"timestamp" | "timestamp without time zone" => Self::Timestamp,
"timestamptz" | "timestamp with time zone" => Self::Timestamptz,
"uuid" => Self::Uuid,
"json" => Self::Json,
"jsonb" => Self::Jsonb,
"decimal" | "numeric" => Self::Decimal,
"xml" => Self::Xml,
_ => return Err(()),
};
Ok(v)
}
}
#[derive(Debug, Clone, Default)]
pub struct ColumnSettings {
pub span_range: SpanRange,
pub attributes: Vec<Attribute>,
pub is_pk: bool,
pub is_unique: bool,
pub nullable: Option<Nullable>,
pub is_incremental: bool,
pub note: Option<String>,
pub default: Option<Value>,
pub refs: Vec<RefInline>,
}
#[derive(Debug, Clone, Default)]
pub struct TableIdent {
pub span_range: SpanRange,
pub name: Ident,
pub schema: Option<Ident>,
pub alias: Option<Ident>,
}