#[cfg(feature = "chrono")]
mod chrono_impl;
#[cfg(feature = "json")]
mod json;
mod serial;
mod time;
mod var_binary;
mod var_char;
#[cfg(feature = "json")]
pub use json::*;
use models_parser::ast::DataType;
pub use serial::Serial;
pub use time::*;
pub use var_binary::VarBinary;
pub use var_char::VarChar;
use crate::prelude::*;
pub trait IntoSQL {
fn into_sql() -> DataType;
const IS_NULLABLE: bool = false;
}
impl IntoSQL for i32 {
fn into_sql() -> DataType {
DataType::Int(None)
}
}
impl IntoSQL for i16 {
fn into_sql() -> DataType {
match *DIALECT {
SQLite => DataType::Int(None),
PostgreSQL => DataType::SmallInt(None),
_ => DataType::SmallInt(None),
}
}
}
impl IntoSQL for i8 {
fn into_sql() -> DataType {
match *DIALECT {
SQLite => DataType::Int(None),
PostgreSQL => DataType::SmallInt(None),
_ => DataType::TinyInt(None),
}
}
}
impl IntoSQL for u32 {
fn into_sql() -> DataType {
match *DIALECT {
MySQL => DataType::BigInt(None),
PostgreSQL => DataType::BigInt(None),
_ => DataType::Int(None),
}
}
}
impl IntoSQL for i64 {
fn into_sql() -> DataType {
match *DIALECT {
SQLite => DataType::Int(None),
_ => DataType::BigInt(None),
}
}
}
impl IntoSQL for f64 {
fn into_sql() -> DataType {
match *DIALECT {
PostgreSQL => DataType::Double,
_ => DataType::Real,
}
}
}
impl IntoSQL for f32 {
fn into_sql() -> DataType {
match *DIALECT {
MySQL => DataType::Real,
_ => DataType::Real,
}
}
}
impl IntoSQL for String {
fn into_sql() -> DataType {
DataType::Text
}
}
impl<const N: usize> IntoSQL for [u8; N] {
fn into_sql() -> DataType {
match *DIALECT {
PostgreSQL => DataType::Bytea,
SQLite => DataType::Blob(None),
_ => DataType::Blob(Some(N as u64)),
}
}
}
impl IntoSQL for Vec<u8> {
fn into_sql() -> DataType {
match *DIALECT {
PostgreSQL => DataType::Bytea,
_ => DataType::Blob(None),
}
}
}
impl<T: IntoSQL> IntoSQL for Option<T> {
fn into_sql() -> DataType {
T::into_sql()
}
const IS_NULLABLE: bool = false;
}
impl IntoSQL for bool {
fn into_sql() -> DataType {
DataType::Boolean
}
}
#[test]
fn func() {
let x = &models_parser::parser::Parser::parse_sql(
&models_parser::dialect::GenericDialect {},
"
CREATE TABLE Persons (
Personid int NOT NULL AUTO_INCREMENT,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
PRIMARY KEY (Personid)
);
",
)
.unwrap()[0];
println!("{}", x);
}