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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
//! `PostgreSQL` column type definitions
//!
//! Defines the core `PostgreSQL` data types for schema definition.
#[cfg(feature = "serde")]
use crate::alloc_prelude::String;
/// Enum representing supported `PostgreSQL` column types.
///
/// These correspond to `PostgreSQL` data types.
/// See: <https://www.postgresql.org/docs/current/datatype.html>
///
/// # Examples
///
/// ```
/// use drizzle_types::postgres::PostgreSQLType;
///
/// let int_type = PostgreSQLType::Integer;
/// assert_eq!(int_type.to_sql_type(), "INTEGER");
///
/// let serial = PostgreSQLType::Serial;
/// assert!(serial.is_serial());
/// ```
#[derive(Default, Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum PostgreSQLType {
/// `PostgreSQL` INTEGER type - 32-bit signed integer
///
/// See: <https://www.postgresql.org/docs/current/datatype-numeric.html#DATATYPE-INT>
Integer,
/// `PostgreSQL` BIGINT type - 64-bit signed integer
///
/// See: <https://www.postgresql.org/docs/current/datatype-numeric.html#DATATYPE-INT>
Bigint,
/// `PostgreSQL` SMALLINT type - 16-bit signed integer
///
/// See: <https://www.postgresql.org/docs/current/datatype-numeric.html#DATATYPE-INT>
Smallint,
/// `PostgreSQL` SERIAL type - auto-incrementing 32-bit integer
///
/// See: <https://www.postgresql.org/docs/current/datatype-numeric.html#DATATYPE-SERIAL>
Serial,
/// `PostgreSQL` SMALLSERIAL type - auto-incrementing 16-bit integer
///
/// See: <https://www.postgresql.org/docs/current/datatype-numeric.html#DATATYPE-SERIAL>
Smallserial,
/// `PostgreSQL` BIGSERIAL type - auto-incrementing 64-bit integer
///
/// See: <https://www.postgresql.org/docs/current/datatype-numeric.html#DATATYPE-SERIAL>
Bigserial,
/// `PostgreSQL` TEXT type - variable-length character string
///
/// See: <https://www.postgresql.org/docs/current/datatype-character.html>
#[default]
Text,
/// `PostgreSQL` VARCHAR type - variable-length character string with limit
///
/// See: <https://www.postgresql.org/docs/current/datatype-character.html>
Varchar,
/// `PostgreSQL` CHAR type - fixed-length character string
///
/// See: <https://www.postgresql.org/docs/current/datatype-character.html>
Char,
/// `PostgreSQL` REAL type - single precision floating-point number
///
/// See: <https://www.postgresql.org/docs/current/datatype-numeric.html#DATATYPE-FLOAT>
Real,
/// `PostgreSQL` DOUBLE PRECISION type - double precision floating-point number
///
/// See: <https://www.postgresql.org/docs/current/datatype-numeric.html#DATATYPE-FLOAT>
DoublePrecision,
/// `PostgreSQL` NUMERIC type - exact numeric with selectable precision
///
/// See: <https://www.postgresql.org/docs/current/datatype-numeric.html#DATATYPE-NUMERIC-DECIMAL>
Numeric,
/// `PostgreSQL` BOOLEAN type - true/false
///
/// See: <https://www.postgresql.org/docs/current/datatype-boolean.html>
Boolean,
/// `PostgreSQL` BYTEA type - binary data
///
/// See: <https://www.postgresql.org/docs/current/datatype-binary.html>
Bytea,
/// `PostgreSQL` UUID type - universally unique identifier
///
/// See: <https://www.postgresql.org/docs/current/datatype-uuid.html>
#[cfg(feature = "uuid")]
Uuid,
/// `PostgreSQL` JSON type - JSON data
///
/// See: <https://www.postgresql.org/docs/current/datatype-json.html>
#[cfg(feature = "serde")]
Json,
/// `PostgreSQL` JSONB type - binary JSON data
///
/// See: <https://www.postgresql.org/docs/current/datatype-json.html>
#[cfg(feature = "serde")]
Jsonb,
/// `PostgreSQL` TIMESTAMP type - date and time
///
/// See: <https://www.postgresql.org/docs/current/datatype-datetime.html>
Timestamp,
/// `PostgreSQL` TIMESTAMPTZ type - date and time with time zone
///
/// See: <https://www.postgresql.org/docs/current/datatype-datetime.html>
Timestamptz,
/// `PostgreSQL` DATE type - calendar date
///
/// See: <https://www.postgresql.org/docs/current/datatype-datetime.html>
Date,
/// `PostgreSQL` TIME type - time of day
///
/// See: <https://www.postgresql.org/docs/current/datatype-datetime.html>
Time,
/// `PostgreSQL` TIMETZ type - time of day with time zone
///
/// See: <https://www.postgresql.org/docs/current/datatype-datetime.html>
Timetz,
/// `PostgreSQL` INTERVAL type - time interval
///
/// See: <https://www.postgresql.org/docs/current/datatype-datetime.html>
#[cfg(feature = "chrono")]
Interval,
/// `PostgreSQL` INET type - IPv4 or IPv6 host address
///
/// See: <https://www.postgresql.org/docs/current/datatype-net-types.html>
#[cfg(feature = "cidr")]
Inet,
/// `PostgreSQL` CIDR type - IPv4 or IPv6 network address
///
/// See: <https://www.postgresql.org/docs/current/datatype-net-types.html>
#[cfg(feature = "cidr")]
Cidr,
/// `PostgreSQL` MACADDR type - MAC address
///
/// See: <https://www.postgresql.org/docs/current/datatype-net-types.html>
#[cfg(feature = "cidr")]
MacAddr,
/// `PostgreSQL` MACADDR8 type - EUI-64 MAC address
///
/// See: <https://www.postgresql.org/docs/current/datatype-net-types.html>
#[cfg(feature = "cidr")]
MacAddr8,
/// `PostgreSQL` POINT type - geometric point
///
/// See: <https://www.postgresql.org/docs/current/datatype-geometric.html>
#[cfg(feature = "geo-types")]
Point,
/// `PostgreSQL` LINE type - geometric line (infinite)
///
/// See: <https://www.postgresql.org/docs/current/datatype-geometric.html>
#[cfg(feature = "geo-types")]
Line,
/// `PostgreSQL` LSEG type - geometric line segment
///
/// See: <https://www.postgresql.org/docs/current/datatype-geometric.html>
#[cfg(feature = "geo-types")]
Lseg,
/// `PostgreSQL` BOX type - geometric box
///
/// See: <https://www.postgresql.org/docs/current/datatype-geometric.html>
#[cfg(feature = "geo-types")]
Box,
/// `PostgreSQL` PATH type - geometric path
///
/// See: <https://www.postgresql.org/docs/current/datatype-geometric.html>
#[cfg(feature = "geo-types")]
Path,
/// `PostgreSQL` POLYGON type - geometric polygon
///
/// See: <https://www.postgresql.org/docs/current/datatype-geometric.html>
#[cfg(feature = "geo-types")]
Polygon,
/// `PostgreSQL` CIRCLE type - geometric circle
///
/// See: <https://www.postgresql.org/docs/current/datatype-geometric.html>
#[cfg(feature = "geo-types")]
Circle,
/// `PostgreSQL` BIT type - fixed-length bit string
///
/// See: <https://www.postgresql.org/docs/current/datatype-bit.html>
#[cfg(feature = "bit-vec")]
Bit,
/// `PostgreSQL` BIT VARYING type - variable-length bit string
///
/// See: <https://www.postgresql.org/docs/current/datatype-bit.html>
#[cfg(feature = "bit-vec")]
Varbit,
/// `PostgreSQL` custom ENUM type - user-defined enumerated type
///
/// See: <https://www.postgresql.org/docs/current/datatype-enum.html>
#[cfg(feature = "serde")]
Enum(String),
}
impl PostgreSQLType {
/// Convert from attribute name to enum variant
///
/// For native enums, use [`Self::from_enum_attribute`] instead.
#[must_use]
pub fn from_attribute_name(name: &str) -> Option<Self> {
match name {
// Integer types and aliases
"integer" | "int" | "int4" => Some(Self::Integer),
"bigint" | "int8" => Some(Self::Bigint),
"smallint" | "int2" => Some(Self::Smallint),
"smallserial" | "serial2" => Some(Self::Smallserial),
"serial" | "serial4" => Some(Self::Serial),
"bigserial" | "serial8" => Some(Self::Bigserial),
// Text types and aliases
"text" => Some(Self::Text),
"varchar" | "character_varying" => Some(Self::Varchar),
"char" | "character" => Some(Self::Char),
// Float types and aliases
"real" | "float4" => Some(Self::Real),
"double_precision" | "float8" | "double" => Some(Self::DoublePrecision),
"numeric" | "decimal" => Some(Self::Numeric),
// Other basic types
"boolean" | "bool" => Some(Self::Boolean),
"bytea" => Some(Self::Bytea),
// UUID
#[cfg(feature = "uuid")]
"uuid" => Some(Self::Uuid),
// JSON types
#[cfg(feature = "serde")]
"json" => Some(Self::Json),
#[cfg(feature = "serde")]
"jsonb" => Some(Self::Jsonb),
// Date/time types and aliases
"timestamp" | "timestamp_without_time_zone" => Some(Self::Timestamp),
"timestamptz" | "timestamp_with_time_zone" => Some(Self::Timestamptz),
"date" => Some(Self::Date),
"time" | "time_without_time_zone" => Some(Self::Time),
"timetz" | "time_with_time_zone" => Some(Self::Timetz),
#[cfg(feature = "chrono")]
"interval" => Some(Self::Interval),
// Network address types
#[cfg(feature = "cidr")]
"inet" => Some(Self::Inet),
#[cfg(feature = "cidr")]
"cidr" => Some(Self::Cidr),
#[cfg(feature = "cidr")]
"macaddr" => Some(Self::MacAddr),
#[cfg(feature = "cidr")]
"macaddr8" => Some(Self::MacAddr8),
// Geometric types
#[cfg(feature = "geo-types")]
"point" => Some(Self::Point),
#[cfg(feature = "geo-types")]
"line" => Some(Self::Line),
#[cfg(feature = "geo-types")]
"lseg" => Some(Self::Lseg),
#[cfg(feature = "geo-types")]
"box" => Some(Self::Box),
#[cfg(feature = "geo-types")]
"path" => Some(Self::Path),
#[cfg(feature = "geo-types")]
"polygon" => Some(Self::Polygon),
#[cfg(feature = "geo-types")]
"circle" => Some(Self::Circle),
// Bit string types
#[cfg(feature = "bit-vec")]
"bit" => Some(Self::Bit),
#[cfg(feature = "bit-vec")]
"varbit" | "bit_varying" => Some(Self::Varbit),
"enum" => None, // enum() requires a parameter, handled separately
_ => None,
}
}
/// Create a native `PostgreSQL` enum type from enum attribute
///
/// Used for `#[enum(MyEnum)]` syntax.
#[cfg(feature = "serde")]
#[must_use]
pub fn from_enum_attribute(enum_name: &str) -> Self {
Self::Enum(String::from(enum_name))
}
/// Get the SQL type string for this type
#[must_use]
pub const fn to_sql_type(&self) -> &str {
match self {
Self::Integer => "INTEGER",
Self::Bigint => "BIGINT",
Self::Smallint => "SMALLINT",
Self::Smallserial => "SMALLSERIAL",
Self::Serial => "SERIAL",
Self::Bigserial => "BIGSERIAL",
Self::Text => "TEXT",
Self::Varchar => "VARCHAR",
Self::Char => "CHAR",
Self::Real => "REAL",
Self::DoublePrecision => "DOUBLE PRECISION",
Self::Numeric => "NUMERIC",
Self::Boolean => "BOOLEAN",
Self::Bytea => "BYTEA",
#[cfg(feature = "uuid")]
Self::Uuid => "UUID",
#[cfg(feature = "serde")]
Self::Json => "JSON",
#[cfg(feature = "serde")]
Self::Jsonb => "JSONB",
Self::Timestamp => "TIMESTAMP",
Self::Timestamptz => "TIMESTAMPTZ",
Self::Date => "DATE",
Self::Time => "TIME",
Self::Timetz => "TIMETZ",
#[cfg(feature = "chrono")]
Self::Interval => "INTERVAL",
#[cfg(feature = "cidr")]
Self::Inet => "INET",
#[cfg(feature = "cidr")]
Self::Cidr => "CIDR",
#[cfg(feature = "cidr")]
Self::MacAddr => "MACADDR",
#[cfg(feature = "cidr")]
Self::MacAddr8 => "MACADDR8",
#[cfg(feature = "geo-types")]
Self::Point => "POINT",
#[cfg(feature = "geo-types")]
Self::Line => "LINE",
#[cfg(feature = "geo-types")]
Self::Lseg => "LSEG",
#[cfg(feature = "geo-types")]
Self::Box => "BOX",
#[cfg(feature = "geo-types")]
Self::Path => "PATH",
#[cfg(feature = "geo-types")]
Self::Polygon => "POLYGON",
#[cfg(feature = "geo-types")]
Self::Circle => "CIRCLE",
#[cfg(feature = "bit-vec")]
Self::Bit => "BIT",
#[cfg(feature = "bit-vec")]
Self::Varbit => "VARBIT",
#[cfg(feature = "serde")]
Self::Enum(name) => name.as_str(), // Custom enum type name
}
}
/// Check if this type is an auto-incrementing type
#[must_use]
pub const fn is_serial(&self) -> bool {
matches!(self, Self::Smallserial | Self::Serial | Self::Bigserial)
}
/// Check if this type supports primary keys
#[must_use]
pub const fn supports_primary_key(&self) -> bool {
core::cfg_select! {
feature = "serde" => !matches!(self, Self::Json | Self::Jsonb),
_ => true,
}
}
/// Check if a flag is valid for this column type
#[must_use]
pub fn is_valid_flag(&self, flag: &str) -> bool {
match (self, flag) {
(Self::Smallserial | Self::Serial | Self::Bigserial, "identity") => true,
(Self::Text | Self::Bytea, "json") => true,
#[cfg(feature = "serde")]
(Self::Json | Self::Jsonb, "json") => true,
(Self::Text | Self::Integer | Self::Smallint | Self::Bigint, "enum") => true,
#[cfg(feature = "serde")]
(Self::Enum(_), "enum") => true,
(_, "primary" | "primary_key" | "unique" | "not_null" | "check") => true,
_ => false,
}
}
}
impl core::fmt::Display for PostgreSQLType {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str(self.to_sql_type())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_from_attribute_name() {
assert_eq!(
PostgreSQLType::from_attribute_name("integer"),
Some(PostgreSQLType::Integer)
);
assert_eq!(
PostgreSQLType::from_attribute_name("int"),
Some(PostgreSQLType::Integer)
);
assert_eq!(
PostgreSQLType::from_attribute_name("text"),
Some(PostgreSQLType::Text)
);
assert_eq!(
PostgreSQLType::from_attribute_name("varchar"),
Some(PostgreSQLType::Varchar)
);
assert_eq!(
PostgreSQLType::from_attribute_name("serial"),
Some(PostgreSQLType::Serial)
);
assert_eq!(
PostgreSQLType::from_attribute_name("smallserial"),
Some(PostgreSQLType::Smallserial)
);
assert_eq!(PostgreSQLType::from_attribute_name("unknown"), None);
}
#[test]
fn test_to_sql_type() {
assert_eq!(PostgreSQLType::Integer.to_sql_type(), "INTEGER");
assert_eq!(PostgreSQLType::Smallserial.to_sql_type(), "SMALLSERIAL");
assert_eq!(PostgreSQLType::Bigint.to_sql_type(), "BIGINT");
assert_eq!(PostgreSQLType::Text.to_sql_type(), "TEXT");
assert_eq!(
PostgreSQLType::DoublePrecision.to_sql_type(),
"DOUBLE PRECISION"
);
assert_eq!(PostgreSQLType::Boolean.to_sql_type(), "BOOLEAN");
}
#[test]
fn test_is_serial() {
assert!(PostgreSQLType::Smallserial.is_serial());
assert!(PostgreSQLType::Serial.is_serial());
assert!(PostgreSQLType::Bigserial.is_serial());
assert!(!PostgreSQLType::Integer.is_serial());
assert!(!PostgreSQLType::Text.is_serial());
}
}