File: sql.rb

package info (click to toggle)
ruby-sequel 5.63.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 10,408 kB
  • sloc: ruby: 113,747; makefile: 3
file content (2049 lines) | stat: -rw-r--r-- 75,685 bytes parent folder | download | duplicates (2)
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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
# frozen-string-literal: true

module Sequel
  # The <tt>Sequel::BasicObject</tt> class is just like the
  # default +BasicObject+ class, except that missing constants are resolved in
  # +Object+.  This allows the virtual row support to work with classes
  # without prefixing them with ::, such as:
  #
  #   DB[:bonds].where{maturity_date > Time.now}
  class BasicObject < ::BasicObject
    # Lookup missing constants in <tt>::Object</tt>
    def self.const_missing(name)
      ::Object.const_get(name)
    end
  end

  class LiteralString < ::String
  end

  # Time subclass that gets literalized with only the time value, so it operates
  # like a standard SQL time type.  This type does not support timezones, by design,
  # so it will not work correctly with <tt>time with time zone</tt> types.
  class SQLTime < ::Time
    @date = nil

    class << self
      # Set the date used for SQLTime instances.
      attr_writer :date

      # Use the date explicitly set, or the current date if there is not a
      # date set.
      def date
        @date || now
      end

      # Set the correct date and timezone when parsing times.
      def parse(*)
        t = super

        utc = Sequel.application_timezone == :utc
        d = @date
        if d || utc
          meth = utc ? :utc : :local
          d ||= t
          t = public_send(meth, d.year, d.month, d.day, t.hour, t.min, t.sec, t.usec)
        end

        t
      end

      # Create a new SQLTime instance given an hour, minute, second, and usec.
      def create(hour, minute, second, usec = 0)
        t = date
        meth = Sequel.application_timezone == :utc ? :utc : :local
        public_send(meth, t.year, t.month, t.day, hour, minute, second, usec)
      end
    end

    # Show that this is an SQLTime, and the time represented
    def inspect
     "#<#{self.class} #{to_s}>"
    end

    # Return a string in HH:MM:SS format representing the time.
    def to_s(*args)
      if args.empty?
        strftime('%H:%M:%S')
      else
        # Superclass may have defined a method that takes a format string,
        # and we shouldn't override in that case.
        super
      end
    end
  end

  # The SQL module holds classes whose instances represent SQL fragments.
  # It also holds modules that are used by these classes.
  module SQL
    # Base class for all SQL expression objects.
    class Expression
      @comparison_attrs = [].freeze

      class << self
        # All attributes used for equality and hash methods.
        attr_reader :comparison_attrs

        # Expression objects are assumed to be value objects, where their
        # attribute values can't change after assignment.  In order to make
        # it easy to define equality and hash methods, subclass
        # instances assume that the only values that affect the results of
        # such methods are the values of the object's attributes.
        def attr_reader(*args)
          super
          comparison_attrs.concat(args)
        end

        # Copy the comparison_attrs into the subclass.
        def inherited(subclass)
          super
          subclass.instance_variable_set(:@comparison_attrs, comparison_attrs.dup)
        end

        private

        # Create a to_s instance method that takes a dataset, and calls
        # the method provided on the dataset with args as the argument (self by default).
        # Used to DRY up some code.
        #
        # Do not call this method with untrusted input, as that can result in
        # arbitrary code execution.
        def to_s_method(meth, args=:self) # :nodoc:
          class_eval("def to_s_append(ds, sql) ds.#{meth}_append(sql, #{args}) end", __FILE__, __LINE__)
          @comparison_attrs.freeze
        end
      end

      # Make clone/dup return self, since Expression objects are supposed to
      # be frozen value objects
      def clone
        self
      end
      alias dup clone

      # Alias of <tt>eql?</tt>
      def ==(other)
        eql?(other)
      end

      # Returns true if the receiver is the same expression as the
      # the +other+ expression.
      def eql?(other)
        other.is_a?(self.class) && !self.class.comparison_attrs.find{|a| public_send(a) != other.public_send(a)}
      end

      # Make sure that the hash value is the same if the attributes are the same.
      def hash
        ([self.class] + self.class.comparison_attrs.map{|x| public_send(x)}).hash
      end

      # Show the class name and instance variables for the object.
      def inspect
        "#<#{self.class} #{instance_variables.map{|iv| "#{iv}=>#{instance_variable_get(iv).inspect}"}.join(', ')}>"
      end
    end

    # Represents a SQL expression, with a given operator and one
    # or more attributes (which may also be ComplexExpressions, forming
    # a tree).  This class is the backbone of Sequel's ruby expression DSL.
    #
    # This is an abstract class that is not that useful by itself.  The
    # subclasses +BooleanExpression+, +NumericExpression+, and +StringExpression+
    # define the behavior of the DSL via operators.
    class ComplexExpression < Expression
      # A hash of the opposite for each operator symbol, used for inverting
      # objects.
      OPERTATOR_INVERSIONS = {:AND => :OR, :OR => :AND, :< => :>=, :> => :<=,
        :<= => :>, :>= => :<, :'=' => :'!=' , :'!=' => :'=', :LIKE => :'NOT LIKE',
        :'NOT LIKE' => :LIKE, :~ => :'!~', :'!~' => :~, :IN => :'NOT IN',
        :'NOT IN' => :IN, :IS => :'IS NOT', :'IS NOT' => :IS, :'~*' => :'!~*',
        :'!~*' => :'~*', :NOT => :NOOP, :NOOP => :NOT, :ILIKE => :'NOT ILIKE',
        :'NOT ILIKE'=>:ILIKE}.freeze

      # Standard mathematical operators used in +NumericMethods+
      MATHEMATICAL_OPERATORS = [:+, :-, :/, :*, :**].freeze

      # Bitwise mathematical operators used in +BitwiseMethods+
      BITWISE_OPERATORS = [:&, :|, :^, :<<, :>>, :%].freeze

      # Operators that check for equality
      EQUALITY_OPERATORS = [:'=', :'!='].freeze

      # Inequality operators used in +InequalityMethods+
      INEQUALITY_OPERATORS = [:<, :>, :<=, :>=].freeze

      # Hash of ruby operator symbols to SQL operators, used in +BooleanMethods+
      BOOLEAN_OPERATOR_METHODS = {:& => :AND, :| =>:OR}.freeze

      # Operators that use IN/NOT IN for inclusion/exclusion
      IN_OPERATORS = [:IN, :'NOT IN'].freeze

      # Operators that use IS, used for special casing to override literal true/false values
      IS_OPERATORS = [:IS, :'IS NOT'].freeze

      # Operators that do pattern matching via regular expressions
      REGEXP_OPERATORS = [:~, :'!~', :'~*', :'!~*'].freeze
      
      # Operators that do pattern matching via LIKE
      LIKE_OPERATORS = [:LIKE, :'NOT LIKE', :ILIKE, :'NOT ILIKE'].freeze

      # Operator symbols that take exactly two arguments
      TWO_ARITY_OPERATORS = (EQUALITY_OPERATORS + INEQUALITY_OPERATORS + IS_OPERATORS + IN_OPERATORS + REGEXP_OPERATORS + LIKE_OPERATORS + [:**]).freeze

      # Operator symbols that take one or more arguments
      N_ARITY_OPERATORS = ([:AND, :OR, :'||'] + MATHEMATICAL_OPERATORS + BITWISE_OPERATORS - [:**]).freeze

      # Operator symbols that are associative
      ASSOCIATIVE_OPERATORS = [:AND, :OR, :'||', :+, :*, :&, :|].freeze

      # Operator symbols that take only a single argument
      ONE_ARITY_OPERATORS = [:NOT, :NOOP, :'B~'].freeze

      # Custom expressions that may have different syntax on different databases
      CUSTOM_EXPRESSIONS = [:extract].freeze

      # The operator symbol for this object
      attr_reader :op
      
      # An array of args for this object
      attr_reader :args

      # Set the operator symbol and arguments for this object to the ones given.
      # Convert all args that are hashes or arrays of two element arrays to +BooleanExpressions+,
      # other than the second arg for an IN/NOT IN operator.
      # Raise an +Error+ if the operator doesn't allow boolean input and a boolean argument is given.
      # Raise an +Error+ if the wrong number of arguments for a given operator is used.
      def initialize(op, *args)
        orig_args = args
        args = args.map{|a| Sequel.condition_specifier?(a) ? SQL::BooleanExpression.from_value_pairs(a) : a}
        case op
        when *N_ARITY_OPERATORS
          raise(Error, "The #{op} operator requires at least 1 argument") unless args.length >= 1
          args.map!{|a| a.is_a?(self.class) && a.op == :NOOP ? a.args.first : a}
          if ASSOCIATIVE_OPERATORS.include?(op)
            old_args = args
            args = []
            old_args.each{|a| a.is_a?(self.class) && a.op == op ? args.concat(a.args) : args.push(a)}
          end
        when *TWO_ARITY_OPERATORS
          raise(Error, "The #{op} operator requires precisely 2 arguments") unless args.length == 2
          # With IN/NOT IN, even if the second argument is an array of two element arrays,
          # don't convert it into a boolean expression, since it's definitely being used
          # as a value list.
          args[1] = orig_args[1] if IN_OPERATORS.include?(op)
        when *ONE_ARITY_OPERATORS
          raise(Error, "The #{op} operator requires a single argument") unless args.length == 1
        when *CUSTOM_EXPRESSIONS
          # nothing
        else
          raise(Error, "Invalid operator #{op}")
        end
        @op = op
        @args = args.freeze
        freeze
      end
      
      to_s_method :complex_expression_sql, '@op, @args'
    end

    # The base class for expressions that can be used in multiple places in
    # an SQL query.  
    class GenericExpression < Expression
    end
    
    # Includes an +as+ method that creates an SQL alias.
    module AliasMethods
      # Create an SQL alias (+AliasedExpression+) of the receiving column or expression to the given alias.
      #
      #   Sequel.function(:func).as(:alias) # func() AS "alias"
      #   Sequel.function(:func).as(:alias, [:col_alias1, :col_alias2]) # func() AS "alias"("col_alias1", "col_alias2")
      def as(aliaz, columns=nil)
        AliasedExpression.new(self, aliaz, columns)
      end
    end

    # This defines the bitwise methods: &, |, ^, ~, <<, and >>.  Because these
    # methods overlap with the standard +BooleanMethods methods+, and they only
    # make sense for integers, they are only included in +NumericExpression+.
    #
    #   Sequel[:a].sql_number & :b # "a" & "b"
    #   Sequel[:a].sql_number | :b # "a" | "b"
    #   Sequel[:a].sql_number ^ :b # "a" ^ "b"
    #   Sequel[:a].sql_number << :b # "a" << "b"
    #   Sequel[:a].sql_number >> :b # "a" >> "b"
    #   ~Sequel[:a].sql_number # ~"a"
    module BitwiseMethods
      ComplexExpression::BITWISE_OPERATORS.each do |o|
        module_eval("def #{o}(o) NumericExpression.new(#{o.inspect}, self, o) end", __FILE__, __LINE__)
      end

      # Do the bitwise compliment of the self
      #
      #   ~(Sequel[:a].sql_number) # ~"a"
      def ~
        NumericExpression.new(:'B~', self)
      end
    end

    # This module includes the boolean/logical AND (&), OR (|) and NOT (~) operators
    # that are defined on objects that can be used in a boolean context in SQL.
    #
    #   Sequel[:a] & Sequel[:b] # "a" AND "b"
    #   Sequel[:a] | Sequel[:b] # "a" OR "b"
    #   ~Sequel[:a] # NOT "a"
    #
    # One exception to this is when a NumericExpression or Integer is the argument
    # to & or |, in which case a bitwise method will be used:
    #
    #   Sequel[:a] & 1 # "a" & 1 
    #   Sequel[:a] | (Sequel[:b] + 1) # "a" | ("b" + 1)
    module BooleanMethods
      ComplexExpression::BOOLEAN_OPERATOR_METHODS.each do |m, o|
        module_eval(<<-END, __FILE__, __LINE__+1)
          def #{m}(o)
            case o
            when NumericExpression, Integer
              NumericExpression.new(#{m.inspect}, self, o)
            else  
              BooleanExpression.new(#{o.inspect}, self, o)
            end
          end
        END
      end
      
      # Create a new BooleanExpression with NOT, representing the inversion of whatever self represents.
      #
      #   ~Sequel[:a] # NOT :a
      def ~
        BooleanExpression.invert(self)
      end
    end

    # These methods make it easier to create Sequel expressions without
    # using the core extensions.
    module Builders
      # Create an SQL::AliasedExpression for the given expression and alias.
      #
      #   Sequel.as(:column, :alias) # "column" AS "alias"
      #   Sequel.as(:column, :alias, [:col_alias1, :col_alias2]) # "column" AS "alias"("col_alias1", "col_alias2")
      def as(exp, aliaz, columns=nil)
        SQL::AliasedExpression.new(exp, aliaz, columns)
      end

      # Order the given argument ascending.
      # Options:
      #
      # :nulls :: Set to :first to use NULLS FIRST (so NULL values are ordered
      #           before other values), or :last to use NULLS LAST (so NULL values
      #           are ordered after other values).
      #
      #   Sequel.asc(:a) # a ASC
      #   Sequel.asc(:b, nulls: :last) # b ASC NULLS LAST
      def asc(arg, opts=OPTS)
        SQL::OrderedExpression.new(arg, false, opts)
      end

      # Return an <tt>SQL::Blob</tt> that holds the same data as this string.
      # Blobs provide proper escaping of binary data.  If given a blob, returns it
      # directly.
      def blob(s)
        if s.is_a?(SQL::Blob)
          s
        else
          SQL::Blob.new(s)
        end
      end

      # Return an <tt>SQL::CaseExpression</tt> created with the given arguments.
      # The first argument are the <tt>WHEN</tt>/<tt>THEN</tt> conditions,
      # specified as an array or a hash.  The second argument is the
      # <tt>ELSE</tt> default value.  The third optional argument is the
      # <tt>CASE</tt> expression.
      #
      #   Sequel.case({a: 1}, 0) # SQL: CASE WHEN a THEN 1 ELSE 0 END
      #   Sequel.case({a: 1}, 0, :b) # SQL: CASE b WHEN a THEN 1 ELSE 0 END
      #   Sequel.case({{a: [2,3]} => 1}, 0) # SQL: CASE WHEN a IN (2, 3) THEN 1 ELSE 0 END
      #   Sequel.case([[{a: [2,3]}, 1]], 0) # SQL: CASE WHEN a IN (2, 3) THEN 1 ELSE 0 END
      def case(*args)
        SQL::CaseExpression.new(*args)
      end

      # Cast the reciever to the given SQL type.  You can specify a ruby class as a type,
      # and it is handled similarly to using a database independent type in the schema methods.
      #
      #   Sequel.cast(:a, :integer) # CAST(a AS integer)
      #   Sequel.cast(:a, String) # CAST(a AS varchar(255))
      def cast(arg, sql_type)
        SQL::Cast.new(arg, sql_type)
      end

      # Cast the reciever to the given SQL type (or the database's default Integer type if none given),
      # and return the result as a +NumericExpression+, so you can use the bitwise operators
      # on the result. 
      #
      #   Sequel.cast_numeric(:a) # CAST(a AS integer)
      #   Sequel.cast_numeric(:a, Float) # CAST(a AS double precision)
      def cast_numeric(arg, sql_type = nil)
        cast(arg, sql_type || Integer).sql_number
      end

      # Cast the reciever to the given SQL type (or the database's default String type if none given),
      # and return the result as a +StringExpression+, so you can use +
      # directly on the result for SQL string concatenation.
      #
      #   Sequel.cast_string(:a) # CAST(a AS varchar(255))
      #   Sequel.cast_string(:a, :text) # CAST(a AS text)
      def cast_string(arg, sql_type = nil)
        cast(arg, sql_type || String).sql_string
      end

      # Return an emulated function call for getting the number of characters
      # in the argument:
      #
      #   Sequel.char_length(:a) # char_length(a) -- Most databases
      #   Sequel.char_length(:a) # length(a) -- SQLite
      def char_length(arg)
        SQL::Function.new!(:char_length, [arg], :emulate=>true)
      end

      # Do a deep qualification of the argument using the qualifier.  This recurses into
      # nested structures.
      #
      #   Sequel.deep_qualify(:table, :column) # "table"."column"
      #   Sequel.deep_qualify(:table, Sequel[:column] + 1) # "table"."column" + 1
      #   Sequel.deep_qualify(:table, Sequel[:a].like('b')) # "table"."a" LIKE 'b' ESCAPE '\'
      def deep_qualify(qualifier, expr)
        Sequel::Qualifier.new(qualifier).transform(expr)
      end

      # Return a delayed evaluation that uses the passed block. This is used
      # to delay evaluations of the code to runtime.  For example, with
      # the following code:
      #
      #   ds = DB[:table].where{column > Time.now}
      #
      # The filter is fixed to the time that where was called. Unless you are
      # only using the dataset once immediately after creating it, that's
      # probably not desired.  If you just want to set it to the time when the
      # query is sent to the database, you can wrap it in Sequel.delay:
      #
      #   ds = DB[:table].where{column > Sequel.delay{Time.now}}
      #
      # Note that for dates and timestamps, you are probably better off using
      # Sequel::CURRENT_DATE and Sequel::CURRENT_TIMESTAMP instead of this
      # generic delayed evaluation facility.
      def delay(&block)
        raise(Error, "Sequel.delay requires a block") unless block
        SQL::DelayedEvaluation.new(block)
      end

      # Order the given argument descending.
      # Options:
      #
      # :nulls :: Set to :first to use NULLS FIRST (so NULL values are ordered
      #           before other values), or :last to use NULLS LAST (so NULL values
      #           are ordered after other values).
      #
      #   Sequel.desc(:a) # b DESC
      #   Sequel.desc(:b, nulls: :first) # b DESC NULLS FIRST
      def desc(arg, opts=OPTS)
        SQL::OrderedExpression.new(arg, true, opts)
      end

      # Wraps the given object in an appropriate Sequel wrapper.
      # If the given object is already a Sequel object, return it directly.
      # For condition specifiers (hashes and arrays of two pairs), true, and false,
      # return a boolean expressions.  For numeric objects, return a numeric
      # expression.  For strings, return a string expression.  For procs or when
      # the method is passed a block, evaluate it as a virtual row and wrap it
      # appropriately.  In all other cases, use a generic wrapper.
      #
      # This method allows you to construct SQL expressions that are difficult
      # to construct via other methods.  For example:
      #
      #   Sequel.expr(1) - :a # SQL: (1 - a)
      #
      # On the Sequel module, this is aliased as #[], for easier use:
      #
      #   Sequel[1] - :a # SQL: (1 - a)
      def expr(arg=(no_arg=true), &block)
        if defined?(yield)
          if no_arg
            return expr(block)
          else
            raise Error, 'cannot provide both an argument and a block to Sequel.expr'
          end
        elsif no_arg
          raise Error, 'must provide either an argument or a block to Sequel.expr'
        end

        case arg
        when Symbol
          t, c, a = Sequel.split_symbol(arg)

          arg = if t
            SQL::QualifiedIdentifier.new(t, c)
          else
            SQL::Identifier.new(c)
          end

          if a
            arg = SQL::AliasedExpression.new(arg, a)
          end

          arg
        when SQL::Expression, LiteralString, SQL::Blob
          arg
        when Hash
          SQL::BooleanExpression.from_value_pairs(arg, :AND)
        when Array
          if condition_specifier?(arg)
            SQL::BooleanExpression.from_value_pairs(arg, :AND)
          else
            SQL::Wrapper.new(arg)
          end
        when Numeric
          SQL::NumericExpression.new(:NOOP, arg)
        when String
          SQL::StringExpression.new(:NOOP, arg)
        when TrueClass, FalseClass
          SQL::BooleanExpression.new(:NOOP, arg)
        when Proc
          expr(virtual_row(&arg))
        else
          SQL::Wrapper.new(arg)
        end
      end

      # Extract a datetime_part (e.g. year, month) from the given
      # expression:
      #
      #   Sequel.extract(:year, :date) # extract(year FROM "date")
      def extract(datetime_part, exp)
        SQL::NumericExpression.new(:extract, datetime_part, exp)
      end

      # Returns a <tt>Sequel::SQL::Function</tt> with the function name
      # and the given arguments.
      #
      #   Sequel.function(:now) # SQL: now()
      #   Sequel.function(:substr, :a, 1) # SQL: substr(a, 1)
      def function(name, *args)
        SQL::Function.new(name, *args)
      end

      # Return the argument wrapped as an <tt>SQL::Identifier</tt>.
      #
      #   Sequel.identifier(:a) # "a"
      def identifier(name)
        SQL::Identifier.new(name)
      end

      # Return a <tt>Sequel::SQL::StringExpression</tt> representing an SQL string made up of the
      # concatenation of the given array's elements.  If an argument is passed,
      # it is used in between each element of the array in the SQL
      # concatenation.
      #
      #   Sequel.join([:a]) # SQL: a
      #   Sequel.join([:a, :b]) # SQL: a || b
      #   Sequel.join([:a, 'b']) # SQL: a || 'b'
      #   Sequel.join(['a', :b], ' ') # SQL: 'a' || ' ' || b
      def join(args, joiner=nil)
        raise Error, 'argument to Sequel.join must be an array' unless args.is_a?(Array)
        if joiner
          args = args.zip([joiner]*args.length).flatten
          args.pop
        end

        return SQL::StringExpression.new(:NOOP, '') if args.empty?

        args = args.map do |a|
          case a
          when Symbol, ::Sequel::SQL::Expression, ::Sequel::LiteralString, TrueClass, FalseClass, NilClass
            a
          else
            a.to_s
          end
        end
        SQL::StringExpression.new(:'||', *args)
      end

      # Create a <tt>BooleanExpression</tt> case insensitive (if the database supports it) pattern match of the receiver with
      # the given patterns.  See <tt>SQL::StringExpression.like</tt>.
      #
      #   Sequel.ilike(:a, 'A%') # "a" ILIKE 'A%' ESCAPE '\'
      def ilike(*args)
        SQL::StringExpression.like(*(args << {:case_insensitive=>true}))
      end

      # Create a <tt>SQL::BooleanExpression</tt> case sensitive (if the database supports it) pattern match of the receiver with
      # the given patterns.  See <tt>SQL::StringExpression.like</tt>.
      #
      #   Sequel.like(:a, 'A%') # "a" LIKE 'A%' ESCAPE '\'
      def like(*args)
        SQL::StringExpression.like(*args)
      end

      # Converts a string into a <tt>Sequel::LiteralString</tt>, in order to override string
      # literalization, e.g.:
      #
      #   DB[:items].where(abc: 'def').sql #=>
      #     "SELECT * FROM items WHERE (abc = 'def')"
      #
      #   DB[:items].where(abc: Sequel.lit('def')).sql #=>
      #     "SELECT * FROM items WHERE (abc = def)"
      #
      # You can also provide arguments, to create a <tt>Sequel::SQL::PlaceholderLiteralString</tt>:
      #
      #    DB[:items].select{|o| o.count(Sequel.lit('DISTINCT ?', :a))}.sql #=>
      #      "SELECT count(DISTINCT a) FROM items"
      def lit(s, *args)
        if args.empty?
          if s.is_a?(LiteralString)
            s
          else
            LiteralString.new(s)
          end
        else
          SQL::PlaceholderLiteralString.new(s, args) 
        end
      end

      # Return a <tt>Sequel::SQL::BooleanExpression</tt> created from the condition
      # specifier, matching none of the conditions.
      #
      #   Sequel.negate(a: true) # SQL: a IS NOT TRUE
      #   Sequel.negate([[:a, true]]) # SQL: a IS NOT TRUE
      #   Sequel.negate([[:a, 1], [:b, 2]]) # SQL: ((a != 1) AND (b != 2))
      def negate(arg)
        if condition_specifier?(arg)
          SQL::BooleanExpression.from_value_pairs(arg, :AND, true)
        else
          raise Error, 'must pass a conditions specifier to Sequel.negate'
        end
      end

      # Return a <tt>Sequel::SQL::BooleanExpression</tt> created from the condition
      # specifier, matching any of the conditions.
      #
      #   Sequel.or(a: true) # SQL: a IS TRUE
      #   Sequel.or([[:a, true]]) # SQL: a IS TRUE
      #   Sequel.or([[:a, 1], [:b, 2]]) # SQL: ((a = 1) OR (b = 2))
      def or(arg)
        if condition_specifier?(arg)
          SQL::BooleanExpression.from_value_pairs(arg, :OR, false)
        else
          raise Error, 'must pass a conditions specifier to Sequel.or'
        end
      end

      # Create a qualified identifier with the given qualifier and identifier
      #
      #   Sequel.qualify(:table, :column) # "table"."column"
      #   Sequel.qualify(:schema, :table) # "schema"."table"
      #   Sequel.qualify(:table, :column).qualify(:schema) # "schema"."table"."column"
      def qualify(qualifier, identifier)
        SQL::QualifiedIdentifier.new(qualifier, identifier)
      end

      # Return an <tt>SQL::Subscript</tt> with the given arguments, representing an
      # SQL array access.
      #
      #   Sequel.subscript(:array, 1) # array[1]
      #   Sequel.subscript(:array, 1, 2) # array[1, 2]
      #   Sequel.subscript(:array, [1, 2]) # array[1, 2]
      #   Sequel.subscript(:array, 1..2) # array[1:2]
      #   Sequel.subscript(:array, 1...3) # array[1:2]
      def subscript(exp, *subs)
        SQL::Subscript.new(exp, subs.flatten)
      end

      # Return an emulated function call for trimming a string of spaces from
      # both sides (similar to ruby's String#strip).
      #
      #   Sequel.trim(:a) # trim(a) -- Most databases
      #   Sequel.trim(:a) # ltrim(rtrim(a)) -- Microsoft SQL Server
      def trim(arg)
        SQL::Function.new!(:trim, [arg], :emulate=>true)
      end

      # Return a <tt>SQL::ValueList</tt> created from the given array.  Used if the array contains
      # all two element arrays and you want it treated as an SQL value list (IN predicate) 
      # instead of as a conditions specifier (similar to a hash).  This is not necessary if you are using
      # this array as a value in a filter, but may be necessary if you are using it as a
      # value with placeholder SQL:
      #
      #   DB[:a].where([:a, :b]=>[[1, 2], [3, 4]]) # SQL: (a, b) IN ((1, 2), (3, 4))
      #   DB[:a].where('(a, b) IN ?', [[1, 2], [3, 4]]) # SQL: (a, b) IN ((1 = 2) AND (3 = 4))
      #   DB[:a].where('(a, b) IN ?', Sequel.value_list([[1, 2], [3, 4]])) # SQL: (a, b) IN ((1, 2), (3, 4))
      def value_list(arg)
        raise Error, 'argument to Sequel.value_list must be an array' unless arg.is_a?(Array)
        SQL::ValueList.new(arg)
      end
    end

    # Holds methods that are used to cast objects to different SQL types.
    module CastMethods 
      # Cast the reciever to the given SQL type.  You can specify a ruby class as a type,
      # and it is handled similarly to using a database independent type in the schema methods.
      #
      #   Sequel.function(:func).cast(:integer) # CAST(func() AS integer)
      #   Sequel.function(:func).cast(String) # CAST(func() AS varchar(255))
      def cast(sql_type)
        Cast.new(self, sql_type)
      end

      # Cast the reciever to the given SQL type (or the database's default Integer type if none given),
      # and return the result as a +NumericExpression+, so you can use the bitwise operators
      # on the result. 
      #
      #   Sequel.function(:func).cast_numeric # CAST(func() AS integer)
      #   Sequel.function(:func).cast_numeric(Float) # CAST(func() AS double precision)
      def cast_numeric(sql_type = nil)
        Cast.new(self, sql_type || Integer).sql_number
      end

      # Cast the reciever to the given SQL type (or the database's default String type if none given),
      # and return the result as a +StringExpression+, so you can use +
      # directly on the result for SQL string concatenation.
      #
      #   Sequel.function(:func).cast_string # CAST(func() AS varchar(255))
      #   Sequel.function(:func).cast_string(:text) # CAST(func() AS text)
      def cast_string(sql_type = nil)
        Cast.new(self, sql_type || String).sql_string
      end
    end

    # Adds methods that allow you to treat an object as an instance of a specific
    # +ComplexExpression+ subclass.
    module ComplexExpressionMethods
      # Extract a datetime part (e.g. year, month) from self:
      #
      #   Sequel[:date].extract(:year) # extract(year FROM "date")
      #
      # Also has the benefit of returning the result as a
      # NumericExpression instead of a generic ComplexExpression.
      def extract(datetime_part)
        NumericExpression.new(:extract, datetime_part, self)
      end

      # Return a BooleanExpression representation of +self+.
      def sql_boolean
        BooleanExpression.new(:NOOP, self)
      end

      # Return a NumericExpression representation of +self+.
      # 
      #   ~Sequel[:a] # NOT "a"
      #   ~(Sequel[:a].sql_number) # ~"a"
      def sql_number
        NumericExpression.new(:NOOP, self)
      end

      # Return a StringExpression representation of +self+.
      #
      #   Sequel[:a] + :b # "a" + "b"
      #   Sequel[:a].sql_string + :b # "a" || "b"
      def sql_string
        StringExpression.new(:NOOP, self)
      end
    end

    # This module includes the inequality methods (>, <, >=, <=) that are defined on objects that can be 
    # used in a numeric or string context in SQL.
    #
    #   Sequel[:a] > :b # a > "b"
    #   Sequel[:a] < :b # a > "b"
    #   Sequel[:a] >= :b # a >= "b"
    #   Sequel[:a] <= :b # a <= "b"
    module InequalityMethods
      ComplexExpression::INEQUALITY_OPERATORS.each do |o|
        module_eval("def #{o}(o) BooleanExpression.new(#{o.inspect}, self, o) end", __FILE__, __LINE__)
      end
    end

    # This module includes the standard mathematical methods (+, -, *, and /)
    # that are defined on objects that can be used in a numeric context in SQL
    # (+Symbol+, +LiteralString+, and +SQL::GenericExpression+).
    #
    #   Sequel[:a] + :b # "a" + "b"
    #   Sequel[:a] - :b # "a" - "b"
    #   Sequel[:a] * :b # "a" * "b"
    #   Sequel[:a] / :b # "a" / "b"
    #
    # One exception to this is if + is called with a +String+ or +StringExpression+,
    # in which case the || operator is used instead of the + operator:
    #
    #   Sequel[:a] + 'b' # "a" || 'b'
    module NumericMethods
      (ComplexExpression::MATHEMATICAL_OPERATORS - [:+]).each do |o|
        module_eval("def #{o}(o) NumericExpression.new(#{o.inspect}, self, o) end", __FILE__, __LINE__)
      end

      # If the argument given is Numeric, treat it as a NumericExpression,
      # allowing code such as:
      #
      #   1 + Sequel[:x] # SQL: (1 + x)
      #   Sequel.expr{1 - x(y)} # SQL: (1 - x(y))
      def coerce(other)
        if other.is_a?(Numeric)
          [SQL::NumericExpression.new(:NOOP, other), self]
        elsif defined?(super)
          super 
        else
          [self, other]
        end
      end

      # Use || as the operator when called with StringExpression and String instances,
      # and the + operator for LiteralStrings and all other types.
      def +(ce)
        case ce
        when LiteralString
          NumericExpression.new(:+, self, ce)
        when StringExpression, String
          StringExpression.new(:'||', self, ce)
        else
          NumericExpression.new(:+, self, ce)
        end
      end
    end

    # This module includes methods for overriding the =~ method for SQL equality,
    # inclusion, and pattern matching.  It returns the same result that Sequel would
    # return when using a hash with a single entry, where the receiver was the key
    # and the argument was the value. Example:
    #
    #   Sequel[:a] =~ 1 # (a = 1)
    #   Sequel[:a] =~ [1, 2] # (a IN [1, 2])
    #   Sequel[:a] =~ nil # (a IS NULL)
    #
    # This also adds the !~ method, for easily setting up not equals,
    # exclusion, and inverse pattern matching.  This is the same as as inverting the
    # result of the =~ method
    #
    #   Sequel[:a] !~ 1 # (a != 1)
    #   Sequel[:a] !~ [1, 2] # (a NOT IN [1, 2])
    #   Sequel[:a] !~ nil # (a IS NOT NULL)
    module PatternMatchMethods
      # Set up an equality, inclusion, or pattern match operation, based on the type
      # of the argument.
      def =~(other)
        BooleanExpression.send(:from_value_pair, self, other)
      end

      def !~(other)
        ~(self =~ other)
      end
    end

    # This adds methods to create SQL expressions using operators:
    #
    #   Sequel.+(1, :a)   # (1 + a)
    #   Sequel.-(1, :a)   # (1 - a)
    #   Sequel.*(1, :a)   # (1 * a)
    #   Sequel./(1, :a)   # (1 / a)
    #   Sequel.&(:b, :a)   # (b AND a)
    #   Sequel.|(:b, :a)   # (b OR a)
    module OperatorBuilders
      {'::Sequel::SQL::NumericExpression'=>{'+'=>'+', '-'=>'-', '*'=>'*', '/'=>'/'},
       '::Sequel::SQL::BooleanExpression'=>{'&'=>'AND', '|'=>'OR'}}.each do |klass, ops|
        ops.each do |m, op|
          class_eval(<<-END, __FILE__, __LINE__ + 1)
            def #{m}(*args)
              if (args.length == 1)
                if (v = args.first).class.is_a?(#{klass})
                  v
                else
                  #{klass}.new(:NOOP, v)
                end
              else
                #{klass}.new(:#{op}, *args)
              end
            end
          END
        end
      end

      # Return NumericExpression for the exponentiation:
      #
      #   Sequel.**(2, 3) # SQL: power(2, 3)
      def **(a, b)
        SQL::NumericExpression.new(:**, a, b)
      end
      
      # Invert the given expression.  Returns a <tt>Sequel::SQL::BooleanExpression</tt>
      # created from this argument, not matching all of the conditions.
      #
      #   Sequel.~(nil) # SQL: NOT NULL
      #   Sequel.~([[:a, true]]) # SQL: a IS NOT TRUE
      #   Sequel.~([[:a, 1], [:b, [2, 3]]]) # SQL: a != 1 OR b NOT IN (2, 3)
      def ~(arg)
        if condition_specifier?(arg)
          SQL::BooleanExpression.from_value_pairs(arg, :OR, true)
        else
          SQL::BooleanExpression.invert(arg)
        end
      end
    end

    # Methods that create +OrderedExpressions+, used for sorting by columns
    # or more complex expressions.
    module OrderMethods
      # Mark the receiving SQL column as sorting in an ascending fashion (generally a no-op).
      # Options:
      #
      # :nulls :: Set to :first to use NULLS FIRST (so NULL values are ordered
      #           before other values), or :last to use NULLS LAST (so NULL values
      #           are ordered after other values).
      def asc(opts=OPTS)
        OrderedExpression.new(self, false, opts)
      end
      
      # Mark the receiving SQL column as sorting in a descending fashion.
      # Options:
      #
      # :nulls :: Set to :first to use NULLS FIRST (so NULL values are ordered
      #           before other values), or :last to use NULLS LAST (so NULL values
      #           are ordered after other values).
      def desc(opts=OPTS)
        OrderedExpression.new(self, true, opts)
      end
    end

    # Includes a +qualify+ and <tt>[]</tt> methods that create <tt>QualifiedIdentifier</tt>s, used for qualifying column
    # names with a table or table names with a schema, and the * method for returning all columns in
    # the identifier if no arguments are given.
    module QualifyingMethods
      # If no arguments are given, return an SQL::ColumnAll:
      #
      #   Sequel[:a].*  # a.*
      def *(ce=(arg=false;nil))
        if arg == false
          Sequel::SQL::ColumnAll.new(self)
        else
          super(ce)
        end
      end

      # Qualify the receiver with the given +qualifier+ (table for column/schema for table).
      #
      #   Sequel[:column].qualify(:table)                  # "table"."column"
      #   Sequel[:table].qualify(:schema)                  # "schema"."table"
      #   Sequel.qualify(:table, :column).qualify(:schema) # "schema"."table"."column"
      def qualify(qualifier)
        QualifiedIdentifier.new(qualifier, self)
      end

      # Qualify the receiver with the given +qualifier+ (table for column/schema for table).
      #
      #   Sequel[:table][:column]          # "table"."column"
      #   Sequel[:schema][:table]          # "schema"."table"
      #   Sequel[:schema][:table][:column] # "schema"."table"."column"
      def [](identifier)
        QualifiedIdentifier.new(self, identifier)
      end
    end

    # This module includes the +like+ and +ilike+ methods used for pattern matching that are defined on objects that can be 
    # used in a string context in SQL (+Symbol+, +LiteralString+, <tt>SQL::GenericExpression</tt>).
    module StringMethods
      # Create a +BooleanExpression+ case insensitive pattern match of the receiver
      # with the given patterns.  See <tt>StringExpression.like</tt>.
      #
      #   Sequel[:a].ilike('A%') # "a" ILIKE 'A%' ESCAPE '\'
      def ilike(*ces)
        StringExpression.like(self, *(ces << {:case_insensitive=>true}))
      end

      # Create a +BooleanExpression+ case sensitive (if the database supports it) pattern match of the receiver with
      # the given patterns.  See <tt>StringExpression.like</tt>.
      #
      #   Sequel[:a].like('A%') # "a" LIKE 'A%' ESCAPE '\'
      def like(*ces)
        StringExpression.like(self, *ces)
      end
    end

    # This module includes the <tt>+</tt> method.  It is included in +StringExpression+ and can be included elsewhere
    # to allow the use of the + operator to represent concatenation of SQL Strings:
    module StringConcatenationMethods
      # Return a +StringExpression+ representing the concatenation of the receiver
      # with the given argument.
      #
      #   Sequel[:x].sql_string + :y # => "x" || "y"
      def +(ce)
        StringExpression.new(:'||', self, ce)
      end
    end

    # This module includes the +sql_subscript+ method, representing SQL array accesses.
    module SubscriptMethods
      # Return a <tt>Subscript</tt> with the given arguments, representing an
      # SQL array access.
      #
      #   Sequel[:array].sql_subscript(1) # array[1]
      #   Sequel[:array].sql_subscript(1, 2) # array[1, 2]
      #   Sequel[:array].sql_subscript([1, 2]) # array[1, 2]
      #   Sequel[:array].sql_subscript(1..2) # array[1:2]
      #   Sequel[:array].sql_subscript(1...3) # array[1:2]
      def sql_subscript(*sub)
        Subscript.new(self, sub.flatten)
      end
    end

    # Represents an aliasing of an expression to a given alias.
    class AliasedExpression < Expression
      # The expression to alias
      attr_reader :expression

      # The alias to use for the expression.
      attr_reader :alias

      # The columns aliases (derived column list) to use, for when the aliased expression is
      # a record or set of records (such as a dataset). 
      attr_reader :columns

      # Create an object with the given expression, alias, and optional column aliases.
      def initialize(expression, aliaz, columns=nil)
        @expression = expression
        @alias = aliaz
        @columns = columns
        freeze
      end

      to_s_method :aliased_expression_sql
    end

    # +Blob+ is used to represent binary data in the Ruby environment that is
    # stored as a blob type in the database. Sequel represents binary data as a Blob object because 
    # most database engines require binary data to be escaped differently than regular strings.
    class Blob < ::String
      include SQL::AliasMethods
      include SQL::CastMethods

      class << self
        # Alias new to call for usage in conversion procs
        alias call new
      end

      # Return a LiteralString with the same content if no args are given, otherwise
      # return a SQL::PlaceholderLiteralString with the current string and the given args.
      def lit(*args)
        args.empty? ? LiteralString.new(self) : SQL::PlaceholderLiteralString.new(self, args)
      end

      # Return a string showing that this is a blob, the size, and the some or all of the content,
      # depending on the size.
      def inspect
        size = length

        content = if size > 20
          "start=#{self[0...10].to_s.inspect} end=#{self[-10..-1].to_s.inspect}"
        else
          "content=#{super}"
        end

        "#<#{self.class}:0x#{"%x" % object_id} bytes=#{size} #{content}>"
      end
      
      # Returns +self+, since it is already a blob.
      def to_sequel_blob
        self
      end
    end

    # Subclass of +ComplexExpression+ where the expression results
    # in a boolean value in SQL.
    class BooleanExpression < ComplexExpression
      include BooleanMethods
      
      # Take pairs of values (e.g. a hash or array of two element arrays)
      # and converts it to a +BooleanExpression+.  The operator and args
      # used depends on the case of the right (2nd) argument:
      #
      # 0..10 :: left >= 0 AND left <= 10
      # [1,2] :: left IN (1,2)
      # nil :: left IS NULL
      # true :: left IS TRUE 
      # false :: left IS FALSE 
      # /as/ :: left ~ 'as'
      # :blah :: left = blah
      # 'blah' :: left = 'blah'
      #
      # If multiple arguments are given, they are joined with the op given (AND
      # by default, OR possible).  If negate is set to true,
      # all subexpressions are inverted before used.  Therefore, the following
      # expressions are equivalent:
      #
      #   ~from_value_pairs(hash)
      #   from_value_pairs(hash, :OR, true)
      def self.from_value_pairs(pairs, op=:AND, negate=false)
        pairs = pairs.map{|l,r| from_value_pair(l, r)}
        pairs.map!{|ce| invert(ce)} if negate
        pairs.length == 1 ? pairs[0] : new(op, *pairs)
      end

      # Return a BooleanExpression based on the right side of the pair.
      def self.from_value_pair(l, r)
        case r
        when Range
          unless r.begin.nil?
            begin_expr = new(:>=, l, r.begin)
          end
          unless r.end.nil?
            end_expr = new(r.exclude_end? ? :< : :<=, l, r.end)
          end
          if begin_expr
            if end_expr
              new(:AND, begin_expr, end_expr)
            else
              begin_expr
            end
          elsif end_expr
            end_expr
          else
            new(:'=', 1, 1)
          end
        when ::Array
          r = r.dup.freeze unless r.frozen?
          new(:IN, l, r)
        when ::String
          r = r.dup.freeze unless r.frozen?
          new(:'=', l, r)
        when ::Sequel::Dataset
          new(:IN, l, r)
        when NegativeBooleanConstant
          new(:"IS NOT", l, r.constant)
        when BooleanConstant
          new(:IS, l, r.constant)
        when NilClass, TrueClass, FalseClass
          new(:IS, l, r)
        when Regexp
          StringExpression.like(l, r)
        when DelayedEvaluation
          Sequel.delay{|ds| from_value_pair(l, r.call(ds))}
        when Dataset::PlaceholderLiteralizer::Argument
          r.transform{|v| from_value_pair(l, v)}
        else
          new(:'=', l, r)
        end
      end
      private_class_method :from_value_pair
      
      # Invert the expression, if possible.  If the expression cannot
      # be inverted, raise an error.  An inverted expression should match everything that the
      # uninverted expression did not match, and vice-versa, except for possible issues with
      # SQL NULL (i.e. 1 == NULL is NULL and 1 != NULL is also NULL).
      #
      #   BooleanExpression.invert(:a) # NOT "a"
      def self.invert(ce)
        case ce
        when BooleanExpression
          case op = ce.op
          when :AND, :OR
            BooleanExpression.new(OPERTATOR_INVERSIONS[op], *ce.args.map{|a| BooleanExpression.invert(a)})
          when :IN, :"NOT IN"
            BooleanExpression.new(OPERTATOR_INVERSIONS[op], *ce.args.dup)
          else
            if ce.args.length == 2
              case ce.args[1]
              when Function, LiteralString, PlaceholderLiteralString
                # Special behavior to not push down inversion in this case because doing so
                # can result in incorrect behavior for ANY/SOME/ALL operators.
                BooleanExpression.new(:NOT, ce)
              else
                BooleanExpression.new(OPERTATOR_INVERSIONS[op], *ce.args.dup)
              end
            else
              BooleanExpression.new(OPERTATOR_INVERSIONS[op], *ce.args.dup)
            end
          end
        when StringExpression, NumericExpression
          raise(Sequel::Error, "cannot invert #{ce.inspect}")
        when Constant
          CONSTANT_INVERSIONS[ce] || raise(Sequel::Error, "cannot invert #{ce.inspect}")
        else
          BooleanExpression.new(:NOT, ce)
        end
      end

      # Always use an AND operator for & on BooleanExpressions
      def &(ce)
        BooleanExpression.new(:AND, self, ce)
      end

      # Always use an OR operator for | on BooleanExpressions
      def |(ce)
        BooleanExpression.new(:OR, self, ce)
      end

      # Return self instead of creating a new object to save on memory.
      def sql_boolean
        self
      end
    end

    # Represents an SQL CASE expression, used for conditional branching in SQL.
    class CaseExpression < GenericExpression
      # An array of all two pairs with the first element specifying the
      # condition and the second element specifying the result if the
      # condition matches.
      attr_reader :conditions

      # The default value if no conditions match. 
      attr_reader :default

      # An optional expression to test the conditions against
      attr_reader :expression

      # Create an object with the given conditions and
      # default value, and optional expression.  An expression can be provided to
      # test each condition against, instead of having
      # all conditions represent their own boolean expression.
      def initialize(conditions, default, expression=(no_expression=true; nil))
        raise(Sequel::Error, 'CaseExpression conditions must be a hash or array of all two pairs') unless Sequel.condition_specifier?(conditions)
        @conditions = conditions.to_a.dup.freeze
        @default = default
        @expression = expression
        @no_expression = no_expression
        freeze
      end

      # Whether to use an expression for this CASE expression.
      def expression?
        !@no_expression
      end

      # Merge the CASE expression into the conditions, useful for databases that
      # don't support CASE expressions.
      def with_merged_expression
        if expression?
          e = expression
          CaseExpression.new(conditions.map{|c, r| [::Sequel::SQL::BooleanExpression.new(:'=', e, c), r]}, default)
        else
          self
        end
      end

      to_s_method :case_expression_sql
    end

    # Represents a cast of an SQL expression to a specific type.
    class Cast < GenericExpression
      # The expression to cast
      attr_reader :expr

      # The type to which to cast the expression
      attr_reader :type
      
      # Set the expression and type for the cast
      def initialize(expr, type)
        @expr = expr
        @type = type
        freeze
      end

      to_s_method :cast_sql, '@expr, @type'
    end

    # Represents all columns in a given table, table.* in SQL
    class ColumnAll < Expression
      # The table containing the columns being selected
      attr_reader :table

      # Create an object with the given table
      def initialize(table)
        @table = table
        freeze
      end

      to_s_method :column_all_sql
    end
    
    class ComplexExpression
      include AliasMethods
      include CastMethods
      include OrderMethods
      include PatternMatchMethods
      include SubscriptMethods

      # Return a BooleanExpression with the same op and args.
      def sql_boolean
        BooleanExpression.new(op, *args)
      end

      # Return a NumericExpression with the same op and args.
      def sql_number
        NumericExpression.new(op, *args)
      end

      # Return a StringExpression with the same op and args.
      def sql_string
        StringExpression.new(op, *args)
      end
    end

    # Represents constants or psuedo-constants (e.g. +CURRENT_DATE+) in SQL.
    class Constant < GenericExpression
      # The underlying constant related to this object.
      attr_reader :constant

      # Create a constant with the given value
      def initialize(constant)
        @constant = constant
        freeze
      end
      
      to_s_method :constant_sql, '@constant'
    end

    # Represents boolean constants such as +NULL+, +TRUE+, and +FALSE+.
    class BooleanConstant < Constant
      to_s_method :boolean_constant_sql, '@constant'
    end
    
    # Represents inverse boolean constants (currently only +NOTNULL+). A
    # special class to allow for special behavior.
    class NegativeBooleanConstant < Constant
      to_s_method :negative_boolean_constant_sql, '@constant'
    end
    
    # Holds default generic constants that can be referenced.  These
    # are included in the Sequel top level module and are also available
    # in this module which can be required at the top level to get
    # direct access to the constants.
    module Constants
      CURRENT_DATE = Constant.new(:CURRENT_DATE)
      CURRENT_TIME = Constant.new(:CURRENT_TIME)
      CURRENT_TIMESTAMP = Constant.new(:CURRENT_TIMESTAMP)
      DEFAULT = Constant.new(:DEFAULT)
      SQLTRUE = TRUE = BooleanConstant.new(true)
      SQLFALSE = FALSE = BooleanConstant.new(false)
      NULL = BooleanConstant.new(nil)
      NOTNULL = NegativeBooleanConstant.new(nil)
    end

    class ComplexExpression
      # A hash of the opposite for each constant, used for inverting constants.
      CONSTANT_INVERSIONS = {Constants::TRUE=>Constants::FALSE, Constants::FALSE=>Constants::TRUE,
                             Constants::NULL=>Constants::NOTNULL, Constants::NOTNULL=>Constants::NULL}.freeze
    end

    # Represents a delayed evaluation, encapsulating a callable
    # object which returns the value to use when called.
    class DelayedEvaluation < GenericExpression
      # A callable object that returns the value of the evaluation
      # when called. 
      attr_reader :callable

      # Set the callable object
      def initialize(callable)
        @callable = callable
        freeze
      end

      # Call the underlying callable and return the result.  If the
      # underlying callable only accepts a single argument, call it
      # with the given dataset.
      def call(ds)
        if @callable.respond_to?(:arity) && @callable.arity == 1
          @callable.call(ds)
        else
          @callable.call
        end
      end

      to_s_method :delayed_evaluation_sql
    end

    # Represents an SQL function call.
    class Function < GenericExpression
      WILDCARD = LiteralString.new('*').freeze
      DISTINCT = ["DISTINCT ".freeze].freeze
      COMMA_ARRAY = [LiteralString.new(', ').freeze].freeze

      # The SQL function to call
      attr_reader :name

      # The array of arguments to pass to the function (may be blank)
      attr_reader :args

      # Options for this function
      attr_reader :opts

      # Set the name and args for the function
      def initialize(name, *args)
        _initialize(name, args, OPTS)
      end

      # Set the name, args, and options, for internal use only.
      def self.new!(name, args, opts) # :nodoc:
        allocate.send(:_initialize, name, args, opts)
      end

      # If no arguments are given, return a new function with the wildcard prepended to the arguments.
      #
      #   Sequel.function(:count).*  # count(*)
      def *(ce=(arg=false;nil))
        if arg == false
          raise Error, "Cannot apply * to functions with arguments" unless args.empty?
          with_opts(:"*"=>true)
        else
          super(ce)
        end
      end

      # Return a new function with DISTINCT before the method arguments.
      #
      #   Sequel.function(:count, :col).distinct # count(DISTINCT col)
      def distinct
        with_opts(:distinct=>true)
      end

      # Return a new function with FILTER added to it, for filtered
      # aggregate functions:
      #
      #   Sequel.function(:foo, :col).filter(a: 1) # foo(col) FILTER (WHERE (a = 1))
      def filter(*args, &block)
        if args.length == 1
          args = args.first
        else
          args.freeze
        end

        with_opts(:filter=>args, :filter_block=>block)
      end

      # Return a function which will use LATERAL when literalized:
      #
      #   Sequel.function(:foo, :col).lateral # LATERAL foo(col)
      def lateral
        with_opts(:lateral=>true)
      end

      # Return a new function where the function will be ordered.  Only useful for aggregate
      # functions that are order dependent.
      #
      #   Sequel.function(:foo, :a).order(:a, Sequel.desc(:b)) # foo(a ORDER BY a, b DESC)
      def order(*args)
        with_opts(:order=>args.freeze)
      end

      # Return a new function with an OVER clause (making it a window function).
      # See Sequel::SQL::Window for the list of options +over+ can receive.
      #
      #   Sequel.function(:row_number).over(partition: :col) # row_number() OVER (PARTITION BY col)
      def over(window=OPTS)
        raise Error, "function already has a window applied to it" if opts[:over]
        window = Window.new(window) unless window.is_a?(Window)
        with_opts(:over=>window)
      end

      # Return a new function where the function name will be quoted if the database supports
      # quoted functions:
      #
      #   Sequel.function(:foo).quoted # "foo"()
      def quoted
        with_opts(:quoted=>true)
      end

      # Return a new function where the function name will not be quoted even
      # if the database supports quoted functions:
      #
      #   Sequel[:foo][:bar].function.unquoted # foo.bar()
      def unquoted
        with_opts(:quoted=>false)
      end

      # Return a new function that will use WITH ORDINALITY to also return
      # a row number for every row the function returns:
      #
      #   Sequel.function(:foo).with_ordinality # foo() WITH ORDINALITY
      def with_ordinality
        with_opts(:with_ordinality=>true)
      end

      # Return a new function that uses WITHIN GROUP ordered by the given expression,
      # useful for ordered-set and hypothetical-set aggregate functions:
      #
      #   Sequel.function(:rank, :a).within_group(:b, :c)
      #   # rank(a) WITHIN GROUP (ORDER BY b, c)
      def within_group(*expressions)
        with_opts(:within_group=>expressions.freeze)
      end

      to_s_method :function_sql

      private

      # Set name, args, and opts
      def _initialize(name, args, opts)
        @name = name
        @args = args.freeze
        @opts = opts.freeze
        freeze
      end

      # Return a new function call with the given opts merged into the current opts.
      def with_opts(opts)
        self.class.new!(name, args, @opts.merge(opts))
      end
    end

    class GenericExpression
      include AliasMethods
      include BooleanMethods
      include CastMethods
      include ComplexExpressionMethods
      include InequalityMethods
      include NumericMethods
      include OrderMethods
      include PatternMatchMethods
      include StringMethods
      include SubscriptMethods
    end

    # Represents an identifier (column, table, schema, etc.).
    class Identifier < GenericExpression
      include QualifyingMethods

      # The identifier to reference
      attr_reader :value

      # Set the identifier to the given argument
      def initialize(value)
        @value = value
        freeze
      end

      # Create a Function using this identifier as the functions name, with
      # the given args.
      def function(*args)
        Function.new(self, *args)
      end
      
      to_s_method :quote_identifier, '@value'
    end
    
    # Represents an SQL JOIN clause, used for joining tables.
    class JoinClause < Expression
      # The type of join to do
      attr_reader :join_type

      # The expression representing the table/set related to the JOIN.
      # Is an AliasedExpression if the JOIN uses an alias.
      attr_reader :table_expr

      # Create an object with the given join_type and table expression.
      def initialize(join_type, table_expr)
        @join_type = join_type
        @table_expr = table_expr
        freeze
      end

      # The table/set related to the JOIN, without any alias.
      def table
        if @table_expr.is_a?(AliasedExpression)
          @table_expr.expression
        else
          @table_expr
        end
      end

      # The table alias to use for the JOIN , or nil if the
      # JOIN does not alias the table.
      def table_alias
        if @table_expr.is_a?(AliasedExpression)
          @table_expr.alias
        end
      end

      # The column aliases to use for the JOIN , or nil if the
      # JOIN does not use a derived column list.
      def column_aliases
        if @table_expr.is_a?(AliasedExpression)
          @table_expr.columns
        end
      end

      to_s_method :join_clause_sql
    end

    # Represents an SQL JOIN clause with ON conditions.
    class JoinOnClause < JoinClause
      # The conditions for the join
      attr_reader :on

      # Create an object with the ON conditions and call super with the
      # remaining args.
      def initialize(on, *args)
        @on = on
        super(*args)
      end

      to_s_method :join_on_clause_sql
    end

    # Represents an SQL JOIN clause with USING conditions.
    class JoinUsingClause < JoinClause
      # The columns that appear in both tables that should be equal 
      # for the conditions to match.
      attr_reader :using

      # Create an object with the given USING conditions and call super
      # with the remaining args.
      def initialize(cols, *args)
        @using = cols 
        super(*args)
      end

      to_s_method :join_using_clause_sql
    end

    # Represents a literal string with placeholders and arguments.
    # This is necessary to ensure delayed literalization of the arguments
    # required for the prepared statement support and for database-specific
    # literalization.
    class PlaceholderLiteralString < GenericExpression
      # The literal string containing placeholders.  This can also be an array
      # of strings, where each arg in args goes between the string elements. 
      attr_reader :str

      # The arguments that will be subsituted into the placeholders.
      # Either an array of unnamed placeholders (which will be substituted in
      # order for ? characters), or a hash of named placeholders (which will be
      # substituted for :key phrases).
      attr_reader :args

      # Whether to surround the expression with parantheses
      attr_reader :parens

      # Create an object with the given string, placeholder arguments, and parens flag.
      def initialize(str, args, parens=false)
        @str = str
        @args = args.is_a?(Array) && args.length == 1 && (v = args[0]).is_a?(Hash) ? v : args
        @parens = parens
        freeze
      end

      # Return a copy of the that will be surrounded by parantheses.
      def with_parens
        @parens ? self : self.class.new(@str, @args, true)
      end

      to_s_method :placeholder_literal_string_sql
    end

    # Subclass of +ComplexExpression+ where the expression results
    # in a numeric value in SQL.
    class NumericExpression < ComplexExpression
      include BitwiseMethods 
      include NumericMethods
      include InequalityMethods

      # Always use + for + operator for NumericExpressions.
      def +(ce)
        NumericExpression.new(:+, self, ce)
      end

      # Return self instead of creating a new object to save on memory.
      def sql_number
        self
      end
    end

    # Represents a column/expression to order the result set by.
    class OrderedExpression < Expression
      INVERT_NULLS = {:first=>:last, :last=>:first}.freeze

      # The expression to order the result set by.
      attr_reader :expression

      # Whether the expression should order the result set in a descending manner
      attr_reader :descending

      # Whether to sort NULLS FIRST/LAST
      attr_reader :nulls

      # Set the expression and descending attributes to the given values.
      # Options:
      #
      # :nulls :: Can be :first/:last for NULLS FIRST/LAST.
      def initialize(expression, descending = true, opts=OPTS)
        @expression = expression
        @descending = descending
        @nulls = opts[:nulls]
        freeze
      end

      # Return a copy that is ordered ASC
      def asc
        OrderedExpression.new(@expression, false, :nulls=>@nulls)
      end

      # Return a copy that is ordered DESC
      def desc
        OrderedExpression.new(@expression, true, :nulls=>@nulls)
      end

      # Return an inverted expression, changing ASC to DESC and NULLS FIRST to NULLS LAST.
      def invert
        OrderedExpression.new(@expression, !@descending, :nulls=>INVERT_NULLS.fetch(@nulls, @nulls))
      end

      to_s_method :ordered_expression_sql
    end

    # Represents a qualified identifier (column with table or table with schema).
    class QualifiedIdentifier < GenericExpression
      include QualifyingMethods

      # The table/schema qualifying the reference
      attr_reader :table

      # The column/table referenced
      attr_reader :column

      # Set the table and column to the given arguments
      def initialize(table, column)
        @table = convert_identifier(table)
        @column = convert_identifier(column)
        freeze
      end
      
      # Create a Function using this identifier as the functions name, with
      # the given args.
      def function(*args)
        Function.new(self, *args)
      end
      
      to_s_method :qualified_identifier_sql, "@table, @column"

      private

      # Automatically convert SQL::Identifiers to strings
      def convert_identifier(identifier)
        case identifier
        when SQL::Identifier
          identifier.value.to_s
        else
          identifier
        end
      end
    end
    
    # Subclass of +ComplexExpression+ where the expression results
    # in a text/string/varchar value in SQL.
    class StringExpression < ComplexExpression
      include StringMethods
      include StringConcatenationMethods
      include InequalityMethods

      # Map of [regexp, case_insenstive] to +ComplexExpression+ operator symbol
      LIKE_MAP = {[true, true]=>:'~*', [true, false]=>:~, [false, true]=>:ILIKE, [false, false]=>:LIKE}.freeze
      LIKE_MAP.each_key(&:freeze)
      
      # Creates a SQL pattern match exprssion. left (l) is the SQL string we
      # are matching against, and ces are the patterns we are matching.
      # The match succeeds if any of the patterns match (SQL OR).
      #
      # If a regular expression is used as a pattern, an SQL regular expression will be
      # used, which is currently only supported on some databases.  Be aware
      # that SQL regular expression syntax is similar to ruby
      # regular expression syntax, but it not exactly the same, especially for
      # advanced regular expression features.  Sequel just uses the source of the
      # ruby regular expression verbatim as the SQL regular expression string.
      #
      # If any other object is used as a regular expression, the SQL LIKE operator will
      # be used, and should be supported by most databases.  
      # 
      # The pattern match will be case insensitive if the last argument is a hash
      # with a key of :case_insensitive that is not false or nil. Also,
      # if a case insensitive regular expression is used (//i), that particular
      # pattern which will always be case insensitive.
      #
      #   StringExpression.like(:a, 'a%') # ("a" LIKE 'a%' ESCAPE '\')
      #   StringExpression.like(:a, 'a%', case_insensitive: true) # ("a" ILIKE 'a%' ESCAPE '\')
      #   StringExpression.like(:a, 'a%', /^a/i) # (("a" LIKE 'a%' ESCAPE '\') OR ("a" ~* '^a'))
      def self.like(l, *ces)
        l, lre, lci = like_element(l)
        lci = (ces.last.is_a?(Hash) ? ces.pop : OPTS)[:case_insensitive] ? true : lci
        ces.map! do |ce|
          r, rre, rci = like_element(ce)
          BooleanExpression.new(LIKE_MAP[[lre||rre, lci||rci]], l, r)
        end
        ces.length == 1 ? ces[0] : BooleanExpression.new(:OR, *ces)
      end
      
      # Returns a three element array, made up of:
      # * The object to use
      # * Whether it is a regular expression
      # * Whether it is case insensitive
      def self.like_element(re) # :nodoc:
        if re.is_a?(Regexp)
          [re.source, true, re.casefold?]
        else
          [re, false, false]
        end
      end
      private_class_method :like_element

      # Return self instead of creating a new object to save on memory.
      def sql_string
        self
      end
    end

    # Represents an SQL array access, with multiple possible arguments.
    class Subscript < GenericExpression
      # The SQL array column
      attr_reader :expression
      alias f expression

      # The array of subscripts to use (should be an array of numbers)
      attr_reader :sub

      # Set the array column and subscripts to the given arguments
      def initialize(expression, sub)
        @expression = expression
        @sub = sub
        freeze
      end

      # Create a new +Subscript+ appending the given subscript(s)
      # to the current array of subscripts.
      #
      #   Sequel[:a].sql_subscript(2) # a[2]
      #   Sequel[:a].sql_subscript(2) | 1 # a[2, 1]
      def |(sub)
        Subscript.new(@expression, @sub + Array(sub))
      end

      # Create a new +Subscript+ by accessing a subarray of a multidimensional
      # array.
      #
      #   Sequel[:a].sql_subscript(2) # a[2]
      #   Sequel[:a].sql_subscript(2)[1] # a[2][1]
      def [](sub)
        Subscript.new(self, Array(sub))
      end
      
      to_s_method :subscript_sql
    end

    # Represents an SQL value list (IN/NOT IN predicate value).  Added so it is possible to deal with a
    # ruby array of two element arrays as an SQL value list instead of an ordered
    # hash-like conditions specifier.
    class ValueList < ::Array
      # Show that this is a value list and not just an array
      def inspect
        "#<#{self.class} #{super}>"
      end
    end

    # The purpose of the +VirtualRow+ class is to allow the easy creation of SQL identifiers and functions,
    # in a way that leads to more compact code.
    #
    # An instance of this class is yielded to the block supplied to <tt>Dataset#where</tt>, <tt>Dataset#order</tt>, and <tt>Dataset#select</tt>
    # (and the other methods that accept a block and pass it to one of those methods).
    # If the block doesn't take an argument, the block is instance_execed in the context of
    # an instance of this class.
    #
    # +VirtualRow+ uses +method_missing+ to return either an +Identifier+, +Function+
    # depending on how it is called.
    #
    # +Function+ :: Returned if any arguments are supplied, using the method name
    #               as the function name, and the arguments as the function arguments.
    # +Identifier+ :: Returned otherwise, using the method name.
    #
    # If splitting symbols has been enabled (not the default), then method calls without
    # arguments will return +QualifiedIdentifier+ instances if the method call includes a
    # double underscore.
    #
    # Examples:
    #
    #   ds = DB[:t]
    #
    #   # Argument yielded to block
    #   ds.where{|r| r.name < 2} # SELECT * FROM t WHERE (name < 2)
    #
    #   # Block without argument (instance_exec)
    #   ds.where{name < 2} # SELECT * FROM t WHERE (name < 2)
    #
    #   # Functions
    #   ds.where{is_active(1, 'arg2')} # SELECT * FROM t WHERE is_active(1, 'arg2')
    #   ds.select{version.function} # SELECT version() FROM t
    #   ds.select{count.function.*} # SELECT count(*) FROM t
    #   ds.select{count(col1).distinct} # SELECT count(DISTINCT col1) FROM t
    #
    #   # Math Operators
    #   ds.select{|o| o.+(1, :a).as(:b)} # SELECT (1 + a) AS b FROM t
    #   ds.select{|o| o.-(2, :a).as(:b)} # SELECT (2 - a) AS b FROM t
    #   ds.select{|o| o.*(3, :a).as(:b)} # SELECT (3 * a) AS b FROM t
    #   ds.select{|o| o./(4, :a).as(:b)} # SELECT (4 / a) AS b FROM t
    #
    #   # Boolean Operators
    #   ds.where{|o| o.&({a: 1}, :b)}    # SELECT * FROM t WHERE ((a = 1) AND b)
    #   ds.where{|o| o.|({a: 1}, :b)}    # SELECT * FROM t WHERE ((a = 1) OR b)
    #   ds.where{|o| o.~(a: 1)}        # SELECT * FROM t WHERE (a != 1)
    #   ds.where{|o| o.~(a: 1, b: 2)} # SELECT * FROM t WHERE ((a != 1) OR (b != 2))
    #
    #   # Inequality Operators
    #   ds.where{|o| o.>(1, :a)}  # SELECT * FROM t WHERE (1 > a)
    #   ds.where{|o| o.<(2, :a)}  # SELECT * FROM t WHERE (2 < a)
    #   ds.where{|o| o.>=(3, :a)} # SELECT * FROM t WHERE (3 >= a)
    #   ds.where{|o| o.<=(4, :a)} # SELECT * FROM t WHERE (4 <= a)
    #
    # For a more detailed explanation, see the {Virtual Rows guide}[rdoc-ref:doc/virtual_rows.rdoc].
    class VirtualRow < BasicObject
      include OperatorBuilders

      %w'> < >= <='.each do |op|
        class_eval(<<-END, __FILE__, __LINE__ + 1)
          def #{op}(*args)
            SQL::BooleanExpression.new(:#{op}, *args)
          end
        END
      end

      def initialize
        freeze
      end

      m = Module.new do
        # Return an +Identifier+, +QualifiedIdentifier+, or +Function+, depending
        # on arguments and whether a block is provided.  Does not currently call the block.
        # See the class level documentation.
        def method_missing(m, *args)
          if args.empty?
            if Sequel.split_symbols?
              table, column = m.to_s.split('__', 2)
              column ? QualifiedIdentifier.new(table, column) : Identifier.new(m)
            else
              Identifier.new(m)
            end
          else
            Function.new(m, *args)
          end
        end
      end
      include m

      Sequel::VIRTUAL_ROW = new
    end

    # A +Window+ is part of a window function specifying the window over which a window function operates.
    #
    #   Sequel::SQL::Window.new(partition: :col1)
    #   # (PARTITION BY col1)
    #   Sequel::SQL::Window.new(partition: [:col2, :col3])
    #   # (PARTITION BY col2, col3)
    #
    #   Sequel::SQL::Window.new(order: :col4)
    #   # (ORDER BY col4)
    #   Sequel::SQL::Window.new(order: [:col5, Sequel.desc(:col6)])
    #   # (ORDER BY col5, col6 DESC)
    #
    #   Sequel::SQL::Window.new(partition: :col7, frame: :all)
    #   # (PARTITION BY col7 ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)
    #   Sequel::SQL::Window.new(partition: :col7, frame: :rows)
    #   # (PARTITION BY col7 ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
    #   Sequel::SQL::Window.new(partition: :col7, frame: {type: :range, start: current})
    #   # (PARTITION BY col7 RANGE CURRENT ROW)
    #   Sequel::SQL::Window.new(partition: :col7, frame: {type: :range, start: 1, end: 1})
    #   # (PARTITION BY col7 RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING)
    #   Sequel::SQL::Window.new(partition: :col7, frame: {type: :range, start: 2, end: [1, :preceding]})
    #   # (PARTITION BY col7 RANGE BETWEEN 2 PRECEDING AND 1 PRECEDING)
    #   Sequel::SQL::Window.new(partition: :col7, frame: {type: :range, start: 1, end: [2, :following]})
    #   # (PARTITION BY col7 RANGE BETWEEN 1 FOLLOWING AND 2 FOLLOWING)
    #   Sequel::SQL::Window.new(partition: :col7, frame: {type: :range, start: :preceding, exclude: :current})
    #   # (PARTITION BY col7 RANGE UNBOUNDED PRECEDING EXCLUDE CURRENT ROW)
    #
    #   Sequel::SQL::Window.new(window: :named_window) # you can create a named window with Dataset#window
    #   # (named_window)
    class Window < Expression
      # The options for this window.  Options currently supported:
      # :frame :: if specified, should be :all, :rows, :range, :groups, a String, or a Hash.
      #           :all :: Always operates over all rows in the partition
      #           :rows :: Includes rows in the partition up to and including the current row
      #           :range, :groups :: Includes rows in the partition up to and including the current group
      #           String :: Used as literal SQL code, try to avoid
      #           Hash :: Hash of options for the frame:
      #                   :type :: The type of frame, must be :rows, :range, or :groups (required)
      #                   :start :: The start of the frame (required).  Possible values:
      #                             :preceding :: UNBOUNDED PRECEDING
      #                             :following :: UNBOUNDED FOLLOWING
      #                             :current :: CURRENT ROW
      #                             String, Numeric, or Cast :: Used as the offset of rows/values preceding
      #                             Array :: Must have two elements, with first element being String, Numeric, or
      #                                      Cast and second element being :preceding or :following
      #                   :end :: The end of the frame.  Can be left out.  If present, takes the same values as
      #                           :start, except that when a String, Numeric, or Hash, it is used as the offset
      #                           for rows following
      #                   :exclude :: Which rows to exclude.  Possible values are :current, :ties, :group
      #                               :no_others.
      # :order :: order on the column(s) given
      # :partition :: partition/group on the column(s) given
      # :window :: base results on a previously specified named window
      attr_reader :opts

      # Set the options to the options given
      def initialize(opts=OPTS)
        @opts = opts.frozen? ? opts : Hash[opts].freeze
        freeze
      end

      to_s_method :window_sql, '@opts'
    end

    # A +Wrapper+ is a simple way to wrap an existing object so that it supports
    # the Sequel DSL.
    class Wrapper < GenericExpression
      # The underlying value wrapped by this object.
      attr_reader :value

      # Set the value wrapped by the object.
      def initialize(value)
        @value = value
        freeze
      end

      to_s_method :literal, '@value'
    end
  end

  # +LiteralString+ is used to represent literal SQL expressions. A 
  # +LiteralString+ is copied verbatim into an SQL statement. Instances of
  # +LiteralString+ can be created by calling <tt>Sequel.lit</tt>.
  class LiteralString
    include SQL::OrderMethods
    include SQL::ComplexExpressionMethods
    include SQL::BooleanMethods
    include SQL::NumericMethods
    include SQL::StringMethods
    include SQL::InequalityMethods
    include SQL::AliasMethods
    include SQL::CastMethods

    # Show that the current string is a literal string in addition to the output.
    def inspect
      "#<#{self.class} #{super}>"
    end
      
    # Return self if no args are given, otherwise return a SQL::PlaceholderLiteralString
    # with the current string and the given args.
    def lit(*args)
      args.empty? ? self : SQL::PlaceholderLiteralString.new(self, args)
    end
      
    # Convert a literal string to a SQL::Blob.
    def to_sequel_blob
      SQL::Blob.new(self)
    end
  end
  
  include SQL::Constants
  extend SQL::Builders
  extend SQL::OperatorBuilders
end