1
+ local _ , datetime = pcall (require , ' datetime' )
2
+
1
3
local errors = require (' errors' )
2
4
3
5
local utils = require (' crud.common.utils' )
@@ -159,6 +161,8 @@ local function format_value(value)
159
161
return tostring (value )
160
162
elseif utils .is_uuid (value ) then
161
163
return (" %q" ):format (value )
164
+ elseif datetime .is_datetime (value ) then
165
+ return (" %q" ):format (value :format ())
162
166
elseif type (value ) == ' cdata' then
163
167
return tostring (value )
164
168
end
@@ -283,6 +287,8 @@ local function format_eq(cond)
283
287
end
284
288
elseif value_type == ' uuid' then
285
289
func_name = ' eq_uuid'
290
+ elseif value_type == ' datetime' then
291
+ func_name = ' eq_datetime'
286
292
end
287
293
288
294
table.insert (cond_strings , format_comp_with_value (field , func_name , value ))
@@ -309,6 +315,8 @@ local function format_lt(cond)
309
315
func_name = add_collation_postfix (' lt' , value_opts )
310
316
elseif value_type == ' uuid' then
311
317
func_name = ' lt_uuid'
318
+ elseif value_type == ' datetime' then
319
+ func_name = ' lt_datetime'
312
320
end
313
321
314
322
func_name = add_strict_postfix (func_name , value_opts )
@@ -533,6 +541,30 @@ local function lt_uuid_strict(lhs, rhs)
533
541
return tostring (lhs ) < tostring (rhs )
534
542
end
535
543
544
+ local function opt_datetime_parse (v )
545
+ if type (v ) == ' string' then
546
+ return datetime .parse (v )
547
+ end
548
+
549
+ return v
550
+ end
551
+
552
+ local function lt_datetime_nullable (lhs , rhs )
553
+ if lhs == nil and rhs ~= nil then
554
+ return true
555
+ elseif rhs == nil then
556
+ return false
557
+ end
558
+ return opt_datetime_parse (lhs ) < opt_datetime_parse (rhs )
559
+ end
560
+
561
+ local function lt_datetime_strict (lhs , rhs )
562
+ if rhs == nil then
563
+ return false
564
+ end
565
+ return opt_datetime_parse (lhs ) < opt_datetime_parse (rhs )
566
+ end
567
+
536
568
local function lt_unicode_ci_nullable (lhs , rhs )
537
569
if lhs == nil and rhs ~= nil then
538
570
return true
@@ -567,6 +599,20 @@ local function eq_uuid_strict(lhs, rhs)
567
599
return tostring (lhs ) == tostring (rhs )
568
600
end
569
601
602
+ local function eq_datetime (lhs , rhs )
603
+ if lhs == nil then
604
+ return rhs == nil
605
+ end
606
+ return opt_datetime_parse (lhs ) == opt_datetime_parse (rhs )
607
+ end
608
+
609
+ local function eq_datetime_strict (lhs , rhs )
610
+ if rhs == nil then
611
+ return false
612
+ end
613
+ return opt_datetime_parse (lhs ) == opt_datetime_parse (rhs )
614
+ end
615
+
570
616
local function eq_unicode_nullable (lhs , rhs )
571
617
if lhs == nil and rhs == nil then
572
618
return true
@@ -604,6 +650,8 @@ local library = {
604
650
eq = eq ,
605
651
eq_uuid = eq_uuid ,
606
652
eq_uuid_strict = eq_uuid_strict ,
653
+ eq_datetime = eq_datetime ,
654
+ eq_datetime_strict = eq_datetime_strict ,
607
655
-- nullable
608
656
eq_unicode = eq_unicode_nullable ,
609
657
eq_unicode_ci = eq_unicode_ci_nullable ,
@@ -618,12 +666,14 @@ local library = {
618
666
lt_unicode_ci = lt_unicode_ci_nullable ,
619
667
lt_boolean = lt_boolean_nullable ,
620
668
lt_uuid = lt_uuid_nullable ,
669
+ lt_datetime = lt_datetime_nullable ,
621
670
-- strict
622
671
lt_strict = lt_strict ,
623
672
lt_unicode_strict = lt_unicode_strict ,
624
673
lt_unicode_ci_strict = lt_unicode_ci_strict ,
625
674
lt_boolean_strict = lt_boolean_strict ,
626
675
lt_uuid_strict = lt_uuid_strict ,
676
+ lt_datetime_strict = lt_datetime_strict ,
627
677
628
678
utf8 = utf8 ,
629
679
0 commit comments