-
-
Notifications
You must be signed in to change notification settings - Fork 192
/
column.rs
579 lines (507 loc) · 16.3 KB
/
column.rs
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
use crate::{expr::*, types::*};
/// Specification of a table column
#[derive(Debug, Clone)]
pub struct ColumnDef {
pub(crate) table: Option<TableRef>,
pub(crate) name: DynIden,
pub(crate) types: Option<ColumnType>,
pub(crate) spec: Vec<ColumnSpec>,
}
/// All column types
#[non_exhaustive]
#[derive(Debug, Clone)]
pub enum ColumnType {
Char(Option<u32>),
String(Option<u32>),
Text,
TinyInteger(Option<u32>),
SmallInteger(Option<u32>),
Integer(Option<u32>),
BigInteger(Option<u32>),
TinyUnsigned(Option<u32>),
SmallUnsigned(Option<u32>),
Unsigned(Option<u32>),
BigUnsigned(Option<u32>),
Float(Option<u32>),
Double(Option<u32>),
Decimal(Option<(u32, u32)>),
DateTime(Option<u32>),
Timestamp(Option<u32>),
TimestampWithTimeZone(Option<u32>),
Time(Option<u32>),
Date,
Interval(Option<PgInterval>, Option<u32>),
Binary(BlobSize),
VarBinary(u32),
Boolean,
Money(Option<(u32, u32)>),
Json,
JsonBinary,
Uuid,
Custom(DynIden),
Enum {
name: DynIden,
variants: Vec<DynIden>,
},
Array(Option<String>),
Cidr,
Inet,
MacAddr,
}
/// All column specification keywords
#[derive(Debug, Clone)]
pub enum ColumnSpec {
Null,
NotNull,
Default(SimpleExpr),
AutoIncrement,
UniqueKey,
PrimaryKey,
Extra(String),
}
// All interval fields
#[derive(Debug, Clone)]
pub enum PgInterval {
Year,
Month,
Day,
Hour,
Minute,
Second,
YearToMonth,
DayToHour,
DayToMinute,
DayToSecond,
HourToMinute,
HourToSecond,
MinuteToSecond,
}
#[derive(Debug, Clone)]
pub enum BlobSize {
Tiny,
/// MySQL & SQLite support `binary(length)` column type
Blob(Option<u32>),
Medium,
Long,
}
#[cfg(feature = "postgres-interval")]
impl quote::ToTokens for PgInterval {
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
use quote::{quote, TokenStreamExt};
tokens.append_all(match self {
PgInterval::Year => quote! { PgInterval::Year },
PgInterval::Month => quote! { PgInterval::Month },
PgInterval::Day => quote! { PgInterval::Day },
PgInterval::Hour => quote! { PgInterval::Hour },
PgInterval::Minute => quote! { PgInterval::Minute },
PgInterval::Second => quote! { PgInterval::Second },
PgInterval::YearToMonth => quote! { PgInterval::YearToMonth },
PgInterval::DayToHour => quote! { PgInterval::DayToHour },
PgInterval::DayToMinute => quote! { PgInterval::DayToMinute },
PgInterval::DayToSecond => quote! { PgInterval::DayToSecond },
PgInterval::HourToMinute => quote! { PgInterval::HourToMinute },
PgInterval::HourToSecond => quote! { PgInterval::HourToSecond },
PgInterval::MinuteToSecond => quote! { PgInterval::MinuteToSecond },
});
}
}
impl ColumnDef {
/// Construct a table column
pub fn new<T: 'static>(name: T) -> Self
where
T: Iden,
{
Self {
table: None,
name: SeaRc::new(name),
types: None,
spec: Vec::new(),
}
}
/// Construct a table column with column type
pub fn new_with_type<T: 'static>(name: T, types: ColumnType) -> Self
where
T: Iden,
{
Self {
table: None,
name: SeaRc::new(name),
types: Some(types),
spec: Vec::new(),
}
}
/// Set column not null
pub fn not_null(&mut self) -> &mut Self {
self.spec.push(ColumnSpec::NotNull);
self
}
/// Set column null
pub fn null(&mut self) -> &mut Self {
self.spec.push(ColumnSpec::Null);
self
}
/// Set default value of a column
pub fn default<T>(&mut self, value: T) -> &mut Self
where
T: Into<SimpleExpr>,
{
self.spec.push(ColumnSpec::Default(value.into()));
self
}
/// Set column auto increment
pub fn auto_increment(&mut self) -> &mut Self {
self.spec.push(ColumnSpec::AutoIncrement);
self
}
/// Set column unique constraint
pub fn unique_key(&mut self) -> &mut Self {
self.spec.push(ColumnSpec::UniqueKey);
self
}
/// Set column as primary key
pub fn primary_key(&mut self) -> &mut Self {
self.spec.push(ColumnSpec::PrimaryKey);
self
}
/// Set column type as char with custom length
pub fn char_len(&mut self, length: u32) -> &mut Self {
self.types = Some(ColumnType::Char(Some(length)));
self
}
/// Set column type as char
pub fn char(&mut self) -> &mut Self {
self.types = Some(ColumnType::Char(None));
self
}
/// Set column type as string with custom length
pub fn string_len(&mut self, length: u32) -> &mut Self {
self.types = Some(ColumnType::String(Some(length)));
self
}
/// Set column type as string
pub fn string(&mut self) -> &mut Self {
self.types = Some(ColumnType::String(None));
self
}
/// Set column type as text
pub fn text(&mut self) -> &mut Self {
self.types = Some(ColumnType::Text);
self
}
/// Set column type as tiny_integer with custom length
pub fn tiny_integer_len(&mut self, length: u32) -> &mut Self {
self.types = Some(ColumnType::TinyInteger(Some(length)));
self
}
/// Set column type as tiny_integer
pub fn tiny_integer(&mut self) -> &mut Self {
self.types = Some(ColumnType::TinyInteger(None));
self
}
/// Set column type as small_integer with custom length
pub fn small_integer_len(&mut self, length: u32) -> &mut Self {
self.types = Some(ColumnType::SmallInteger(Some(length)));
self
}
/// Set column type as small_integer
pub fn small_integer(&mut self) -> &mut Self {
self.types = Some(ColumnType::SmallInteger(None));
self
}
/// Set column type as integer with custom length
pub fn integer_len(&mut self, length: u32) -> &mut Self {
self.types = Some(ColumnType::Integer(Some(length)));
self
}
/// Set column type as integer
pub fn integer(&mut self) -> &mut Self {
self.types = Some(ColumnType::Integer(None));
self
}
/// Set column type as big_integer with custom length
pub fn big_integer_len(&mut self, length: u32) -> &mut Self {
self.types = Some(ColumnType::BigInteger(Some(length)));
self
}
/// Set column type as big_integer
pub fn big_integer(&mut self) -> &mut Self {
self.types = Some(ColumnType::BigInteger(None));
self
}
/// Set column type as tiny_unsigned with custom length
pub fn tiny_unsigned_len(&mut self, length: u32) -> &mut Self {
self.types = Some(ColumnType::TinyUnsigned(Some(length)));
self
}
/// Set column type as tiny_unsigned
pub fn tiny_unsigned(&mut self) -> &mut Self {
self.types = Some(ColumnType::TinyUnsigned(None));
self
}
/// Set column type as small_unsigned with custom length
pub fn small_unsigned_len(&mut self, length: u32) -> &mut Self {
self.types = Some(ColumnType::SmallUnsigned(Some(length)));
self
}
/// Set column type as small_unsigned
pub fn small_unsigned(&mut self) -> &mut Self {
self.types = Some(ColumnType::SmallUnsigned(None));
self
}
/// Set column type as unsigned with custom length
pub fn unsigned_len(&mut self, length: u32) -> &mut Self {
self.types = Some(ColumnType::Unsigned(Some(length)));
self
}
/// Set column type as unsigned
pub fn unsigned(&mut self) -> &mut Self {
self.types = Some(ColumnType::Unsigned(None));
self
}
/// Set column type as big_unsigned with custom length
pub fn big_unsigned_len(&mut self, length: u32) -> &mut Self {
self.types = Some(ColumnType::BigUnsigned(Some(length)));
self
}
/// Set column type as big_unsigned
pub fn big_unsigned(&mut self) -> &mut Self {
self.types = Some(ColumnType::BigUnsigned(None));
self
}
/// Set column type as float with custom precision
pub fn float_len(&mut self, precision: u32) -> &mut Self {
self.types = Some(ColumnType::Float(Some(precision)));
self
}
/// Set column type as float
pub fn float(&mut self) -> &mut Self {
self.types = Some(ColumnType::Float(None));
self
}
/// Set column type as double with custom precision
pub fn double_len(&mut self, precision: u32) -> &mut Self {
self.types = Some(ColumnType::Double(Some(precision)));
self
}
/// Set column type as double
pub fn double(&mut self) -> &mut Self {
self.types = Some(ColumnType::Double(None));
self
}
/// Set column type as decimal with custom precision and scale
pub fn decimal_len(&mut self, precision: u32, scale: u32) -> &mut Self {
self.types = Some(ColumnType::Decimal(Some((precision, scale))));
self
}
/// Set column type as decimal
pub fn decimal(&mut self) -> &mut Self {
self.types = Some(ColumnType::Decimal(None));
self
}
/// Set column type as date_time with custom precision
pub fn date_time_len(&mut self, precision: u32) -> &mut Self {
self.types = Some(ColumnType::DateTime(Some(precision)));
self
}
/// Set column type as date_time
pub fn date_time(&mut self) -> &mut Self {
self.types = Some(ColumnType::DateTime(None));
self
}
/// Set column type as interval type with optional fields and precision. Postgres only
///
/// ```
/// use sea_query::{tests_cfg::*, *};
/// assert_eq!(
/// Table::create()
/// .table(Glyph::Table)
/// .col(
/// ColumnDef::new(Alias::new("I1"))
/// .interval(None, None)
/// .not_null()
/// )
/// .col(
/// ColumnDef::new(Alias::new("I2"))
/// .interval(Some(PgInterval::YearToMonth), None)
/// .not_null()
/// )
/// .col(
/// ColumnDef::new(Alias::new("I3"))
/// .interval(None, Some(42))
/// .not_null()
/// )
/// .col(
/// ColumnDef::new(Alias::new("I4"))
/// .interval(Some(PgInterval::Hour), Some(43))
/// .not_null()
/// )
/// .to_string(PostgresQueryBuilder),
/// vec![
/// r#"CREATE TABLE "glyph" ("#,
/// r#""I1" interval NOT NULL,"#,
/// r#""I2" interval YEAR TO MONTH NOT NULL,"#,
/// r#""I3" interval(42) NOT NULL,"#,
/// r#""I4" interval HOUR(43) NOT NULL"#,
/// r#")"#,
/// ]
/// .join(" ")
/// );
/// ```
#[cfg(feature = "backend-postgres")]
pub fn interval(&mut self, fields: Option<PgInterval>, precision: Option<u32>) -> &mut Self {
self.types = Some(ColumnType::Interval(fields, precision));
self
}
/// Set column type as timestamp with custom precision
pub fn timestamp_len(&mut self, precision: u32) -> &mut Self {
self.types = Some(ColumnType::Timestamp(Some(precision)));
self
}
/// Set column type as timestamp
pub fn timestamp(&mut self) -> &mut Self {
self.types = Some(ColumnType::Timestamp(None));
self
}
/// Set column type as timestamp with time zone. Postgres only
pub fn timestamp_with_time_zone(&mut self) -> &mut Self {
self.types = Some(ColumnType::TimestampWithTimeZone(None));
self
}
/// Set column type as timestamp with time zone plus custom precision
pub fn timestamp_with_time_zone_len(&mut self, precision: u32) -> &mut Self {
self.types = Some(ColumnType::TimestampWithTimeZone(Some(precision)));
self
}
/// Set column type as time with custom precision
pub fn time_len(&mut self, precision: u32) -> &mut Self {
self.types = Some(ColumnType::Time(Some(precision)));
self
}
/// Set column type as time
pub fn time(&mut self) -> &mut Self {
self.types = Some(ColumnType::Time(None));
self
}
/// Set column type as date
pub fn date(&mut self) -> &mut Self {
self.types = Some(ColumnType::Date);
self
}
/// Set column type as binary with custom length
pub fn binary_len(&mut self, length: u32) -> &mut Self {
self.types = Some(ColumnType::Binary(BlobSize::Blob(Some(length))));
self
}
/// Set column type as binary
pub fn binary(&mut self) -> &mut Self {
self.types = Some(ColumnType::Binary(BlobSize::Blob(None)));
self
}
/// Set column type as blob, but when given BlobSize::Blob(size) argument, this column map to binary(size) type instead.
pub fn blob(&mut self, size: BlobSize) -> &mut Self {
self.types = Some(ColumnType::Binary(size));
self
}
/// Set column type as binary with variable length
pub fn var_binary(&mut self, length: u32) -> &mut Self {
self.types = Some(ColumnType::VarBinary(length));
self
}
/// Set column type as boolean
pub fn boolean(&mut self) -> &mut Self {
self.types = Some(ColumnType::Boolean);
self
}
/// Set column type as money with custom precision and scale
pub fn money_len(&mut self, precision: u32, scale: u32) -> &mut Self {
self.types = Some(ColumnType::Money(Some((precision, scale))));
self
}
/// Set column type as money
pub fn money(&mut self) -> &mut Self {
self.types = Some(ColumnType::Money(None));
self
}
/// Set column type as json.
pub fn json(&mut self) -> &mut Self {
self.types = Some(ColumnType::Json);
self
}
/// Set column type as json binary.
pub fn json_binary(&mut self) -> &mut Self {
self.types = Some(ColumnType::JsonBinary);
self
}
/// Set column type as uuid
pub fn uuid(&mut self) -> &mut Self {
self.types = Some(ColumnType::Uuid);
self
}
/// Use a custom type on this column.
pub fn custom<T>(&mut self, name: T) -> &mut Self
where
T: IntoIden,
{
self.types = Some(ColumnType::Custom(name.into_iden()));
self
}
/// Set column type as enum.
pub fn enumeration<N, S, V>(&mut self, name: N, variants: V) -> &mut Self
where
N: IntoIden,
S: IntoIden,
V: IntoIterator<Item = S>,
{
self.types = Some(ColumnType::Enum {
name: name.into_iden(),
variants: variants.into_iter().map(IntoIden::into_iden).collect(),
});
self
}
/// Set column type as an array with a specified element type.
/// This is only supported on Postgres.
pub fn array(&mut self, elem_type: String) -> &mut Self {
self.types = Some(ColumnType::Array(Some(elem_type)));
self
}
/// Set columnt type as cidr.
/// This is only supported on Postgres.
pub fn cidr(&mut self) -> &mut Self {
self.types = Some(ColumnType::Cidr);
self
}
/// Set columnt type as inet.
/// This is only supported on Postgres.
pub fn inet(&mut self) -> &mut Self {
self.types = Some(ColumnType::Inet);
self
}
/// Set columnt type as macaddr.
/// This is only supported on Postgres.
pub fn mac_address(&mut self) -> &mut Self {
self.types = Some(ColumnType::MacAddr);
self
}
/// Some extra options in custom string
pub fn extra(&mut self, string: String) -> &mut Self {
self.spec.push(ColumnSpec::Extra(string));
self
}
pub fn get_column_name(&self) -> String {
self.name.to_string()
}
pub fn get_column_type(&self) -> Option<&ColumnType> {
self.types.as_ref()
}
pub fn get_column_spec(&self) -> &Vec<ColumnSpec> {
self.spec.as_ref()
}
pub fn take(&mut self) -> Self {
Self {
table: self.table.take(),
name: std::mem::replace(&mut self.name, SeaRc::new(NullAlias::new())),
types: self.types.take(),
spec: std::mem::take(&mut self.spec),
}
}
}