1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
//!
//! # Types
//!
//! | Rust             | PostgreSQL    | MySQL                    | SQLite              |
//! |------------      |---------------|--------------------------|---------------------|
//! | `bool`           | BOOLEAN       | BOOLEAN                  | BOOLEAN             |
//! | `i8`             | SMALLINT      | TINYINT                  | INTEGER             |
//! | `i16`            | SMALLINT      | SMALLINT                 | INTEGER             |
//! | `i32`            | INT           | INT                      | INTEGER             |
//! | `i64`            | BIGINT        | BIGINT                   | INTEGER             |
//! | `f32`            | REAL          | FLOAT                    | REAL                |
//! | `f64`            | REAL          | REAL                     | REAL                |
//! | `String`         | TEXT          | TEXT                     | TEXT                |
//! | `VarChar<SIZE>`  | VARCHAR(SIZE) | VARCHAR(SIZE)            | TEXT                |
//! | `VarBinary<SIZE>`| BYTEA         | VARBINARY(SIZE)          | BLOB                |
//! | `Vec<u8>`        | BYTEA         | BLOB                     | BLOB                |
//! | `[u8; SIZE]`     | BYTEA         | BLOB(SIZE)               | BLOB                |
//! |
//!
//! ### [`chrono`](https://crates.io/crates/chrono)
//!
//! Requires the `chrono` Cargo feature flag.
//!
//! | Rust type                    | Postgres         | MySQL            | SQLite             |
//! |------------------------------|------------------|------------------|--------------------|
//! | `chrono::DateTime<Utc>`      | TIMESTAMPTZ      | TIMESTAMP        | DATETIME           |
//! | `chrono::DateTime<Local>`    | TIMESTAMPTZ      | TIMESTAMP        | DATETIME           |
//! | `chrono::NaiveDateTime`      | TIMESTAMP        | DATETIME         | DATETIME           |
//! | `chrono::NaiveDate`          | DATE             | DATE             | DATETIME           |
//! | `chrono::NaiveTime`          | TIME             | TIME             | DATETIME           |
//!
#[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::*;

/// Do not use this trait in your production code.
/// Its intended use is for migration generation only.
/// It will panic if used outside its intended API.
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 u16 {
//     fn into_sql() -> DataType {
//         match *DIALECT {
//             MySQL => DataType::Int(None),
//             _ => DataType::Int(None),
//         }
//     }
// }
// impl IntoSQL for u8 {
//     fn into_sql() -> DataType {
//         match *DIALECT {
//             MySQL => DataType::Int(None),
//             PostgreSQL => DataType::custom("SMALLINT"),
//             _ => DataType::Int(None),
//         }
//     }
// }

// impl IntoSQL for u64 {
//     fn into_sql() -> DataType {
//         DataType::BigInt(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);
}