Skip to content

Commit

Permalink
Add ColumnType
Browse files Browse the repository at this point in the history
  • Loading branch information
oldani committed Sep 16, 2024
1 parent 4113687 commit 9738cce
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 1 deletion.
30 changes: 30 additions & 0 deletions sea_query.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,38 @@ class Index:
@staticmethod
def drop() -> IndexDropStatement: ...

class ColumnType(IntEnum):
Char = 1
String = 2
Text = 3
TinyInteger = 4
SmallInteger = 5
Integer = 6
BigInteger = 7
TinyUnsigned = 8
SmallUnsigned = 9
Unsigned = 10
BigUnsigned = 11
Float = 12
Double = 13
Decimal = 14
DateTime = 15
Timestamp = 16
TimestampWithTz = 17
Date = 18
Time = 19
Blob = 20
Boolean = 21
Json = 22
Jsonb = 23
Uuid = 24

class Column:
def __init__(self, name: str) -> None: ...
@staticmethod
def new_with_type(name: str, column_type: ColumnType) -> Column: ...
def get_name(self) -> str: ...
def get_type(self) -> ColumnType | None: ...
def not_null(self) -> Self: ...
def null(self) -> Self: ...
def default(self, expr: Expr) -> Self: ...
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ fn sea_query(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<types::LockType>()?;
m.add_class::<types::LockBehavior>()?;
m.add_class::<types::IndexType>()?;
m.add_class::<types::ColumnType>()?;
m.add_class::<types::DBEngine>()?;
m.add_class::<expr::SimpleExpr>()?;
m.add_class::<expr::Expr>()?;
Expand Down
18 changes: 17 additions & 1 deletion src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{
expr::{Expr, SimpleExpr},
foreign_key::ForeignKeyCreateStatement,
index::IndexCreateStatement,
types::DBEngine,
types::{ColumnType, DBEngine},
};

#[pyclass]
Expand All @@ -29,6 +29,22 @@ impl Column {
Self(ColumnDef::new(Alias::new(name)))
}

#[staticmethod]
fn new_with_type(name: &str, column_type: ColumnType) -> Self {
Self(ColumnDef::new_with_type(
Alias::new(name),
column_type.into(),
))
}

fn get_name(&self) -> String {
self.0.get_column_name()
}

fn get_type(&self) -> Option<ColumnType> {
self.0.get_column_type().cloned().map(Into::into)
}

fn not_null(mut slf: PyRefMut<Self>) -> PyRefMut<Self> {
slf.0.not_null();
slf
Expand Down
93 changes: 93 additions & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use sea_query::{
backend::{MysqlQueryBuilder, PostgresQueryBuilder, QueryBuilder, SqliteQueryBuilder},
index::{IndexOrder, IndexType as SeaIndexType},
query::{LockBehavior as SeaLockBehavior, LockType as SeaLockType, UnionType as SeaUnionType},
table::ColumnType as SeaColumnType,
value::Value,
NullOrdering as SeaNullOrdering, Order as SeaOrder,
};
Expand Down Expand Up @@ -200,3 +201,95 @@ impl From<IndexType> for SeaIndexType {
}
}
}

#[pyclass(eq, eq_int)]
#[derive(Clone, PartialEq)]
pub enum ColumnType {
Char,
String,
Text,
TinyInteger,
SmallInteger,
Integer,
BigInteger,
TinyUnsigned,
SmallUnsigned,
Unsigned,
BigUnsigned,
Float,
Double,
Decimal,
DateTime,
Timestamp,
TimestampWithTz,
Date,
Time,
Blob,
Boolean,
Json,
Jsonb,
Uuid,
}

impl From<ColumnType> for SeaColumnType {
fn from(val: ColumnType) -> Self {
match val {
ColumnType::Char => SeaColumnType::Char(None),
ColumnType::String => SeaColumnType::String(Default::default()),
ColumnType::Text => SeaColumnType::Text,
ColumnType::TinyInteger => SeaColumnType::TinyInteger,
ColumnType::SmallInteger => SeaColumnType::SmallInteger,
ColumnType::Integer => SeaColumnType::Integer,
ColumnType::BigInteger => SeaColumnType::BigInteger,
ColumnType::TinyUnsigned => SeaColumnType::TinyUnsigned,
ColumnType::SmallUnsigned => SeaColumnType::SmallUnsigned,
ColumnType::Unsigned => SeaColumnType::Unsigned,
ColumnType::BigUnsigned => SeaColumnType::BigUnsigned,
ColumnType::Float => SeaColumnType::Float,
ColumnType::Double => SeaColumnType::Double,
ColumnType::Decimal => SeaColumnType::Decimal(None),
ColumnType::DateTime => SeaColumnType::DateTime,
ColumnType::Timestamp => SeaColumnType::Timestamp,
ColumnType::TimestampWithTz => SeaColumnType::TimestampWithTimeZone,
ColumnType::Time => SeaColumnType::Time,
ColumnType::Date => SeaColumnType::Date,
ColumnType::Blob => SeaColumnType::Blob,
ColumnType::Boolean => SeaColumnType::Boolean,
ColumnType::Json => SeaColumnType::Json,
ColumnType::Jsonb => SeaColumnType::JsonBinary,
ColumnType::Uuid => SeaColumnType::Uuid,
}
}
}

impl From<SeaColumnType> for ColumnType {
fn from(val: SeaColumnType) -> ColumnType {
match val {
SeaColumnType::Char(_) => ColumnType::Char,
SeaColumnType::String(_) => ColumnType::String,
SeaColumnType::Text => ColumnType::Text,
SeaColumnType::TinyInteger => ColumnType::TinyInteger,
SeaColumnType::SmallInteger => ColumnType::SmallInteger,
SeaColumnType::Integer => ColumnType::Integer,
SeaColumnType::BigInteger => ColumnType::BigInteger,
SeaColumnType::TinyUnsigned => ColumnType::TinyUnsigned,
SeaColumnType::SmallUnsigned => ColumnType::SmallUnsigned,
SeaColumnType::Unsigned => ColumnType::Unsigned,
SeaColumnType::BigUnsigned => ColumnType::BigUnsigned,
SeaColumnType::Float => ColumnType::Float,
SeaColumnType::Double => ColumnType::Double,
SeaColumnType::Decimal(_) => ColumnType::Decimal,
SeaColumnType::DateTime => ColumnType::DateTime,
SeaColumnType::Timestamp => ColumnType::Timestamp,
SeaColumnType::TimestampWithTimeZone => ColumnType::TimestampWithTz,
SeaColumnType::Time => ColumnType::Time,
SeaColumnType::Date => ColumnType::Date,
SeaColumnType::Blob => ColumnType::Blob,
SeaColumnType::Boolean => ColumnType::Boolean,
SeaColumnType::Json => ColumnType::Json,
SeaColumnType::JsonBinary => ColumnType::Jsonb,
SeaColumnType::Uuid => ColumnType::Uuid,
_ => unimplemented!(),
}
}
}

0 comments on commit 9738cce

Please sign in to comment.