epugh-sequel 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +652 -0
- data/VERSION.yml +4 -0
- data/bin/sequel +104 -0
- data/lib/sequel.rb +1 -0
- data/lib/sequel/adapters/ado.rb +85 -0
- data/lib/sequel/adapters/db2.rb +132 -0
- data/lib/sequel/adapters/dbi.rb +101 -0
- data/lib/sequel/adapters/do.rb +197 -0
- data/lib/sequel/adapters/do/mysql.rb +38 -0
- data/lib/sequel/adapters/do/postgres.rb +92 -0
- data/lib/sequel/adapters/do/sqlite.rb +31 -0
- data/lib/sequel/adapters/firebird.rb +307 -0
- data/lib/sequel/adapters/informix.rb +75 -0
- data/lib/sequel/adapters/jdbc.rb +485 -0
- data/lib/sequel/adapters/jdbc/h2.rb +62 -0
- data/lib/sequel/adapters/jdbc/mysql.rb +56 -0
- data/lib/sequel/adapters/jdbc/oracle.rb +23 -0
- data/lib/sequel/adapters/jdbc/postgresql.rb +101 -0
- data/lib/sequel/adapters/jdbc/sqlite.rb +43 -0
- data/lib/sequel/adapters/mysql.rb +370 -0
- data/lib/sequel/adapters/odbc.rb +184 -0
- data/lib/sequel/adapters/openbase.rb +57 -0
- data/lib/sequel/adapters/oracle.rb +140 -0
- data/lib/sequel/adapters/postgres.rb +453 -0
- data/lib/sequel/adapters/shared/mssql.rb +93 -0
- data/lib/sequel/adapters/shared/mysql.rb +341 -0
- data/lib/sequel/adapters/shared/oracle.rb +62 -0
- data/lib/sequel/adapters/shared/postgres.rb +743 -0
- data/lib/sequel/adapters/shared/progress.rb +34 -0
- data/lib/sequel/adapters/shared/sqlite.rb +263 -0
- data/lib/sequel/adapters/sqlite.rb +243 -0
- data/lib/sequel/adapters/utils/date_format.rb +21 -0
- data/lib/sequel/adapters/utils/stored_procedures.rb +75 -0
- data/lib/sequel/adapters/utils/unsupported.rb +62 -0
- data/lib/sequel/connection_pool.rb +258 -0
- data/lib/sequel/core.rb +204 -0
- data/lib/sequel/core_sql.rb +185 -0
- data/lib/sequel/database.rb +687 -0
- data/lib/sequel/database/schema_generator.rb +324 -0
- data/lib/sequel/database/schema_methods.rb +164 -0
- data/lib/sequel/database/schema_sql.rb +324 -0
- data/lib/sequel/dataset.rb +422 -0
- data/lib/sequel/dataset/convenience.rb +237 -0
- data/lib/sequel/dataset/prepared_statements.rb +220 -0
- data/lib/sequel/dataset/sql.rb +1105 -0
- data/lib/sequel/deprecated.rb +529 -0
- data/lib/sequel/exceptions.rb +44 -0
- data/lib/sequel/extensions/blank.rb +42 -0
- data/lib/sequel/extensions/inflector.rb +288 -0
- data/lib/sequel/extensions/pagination.rb +96 -0
- data/lib/sequel/extensions/pretty_table.rb +78 -0
- data/lib/sequel/extensions/query.rb +48 -0
- data/lib/sequel/extensions/string_date_time.rb +47 -0
- data/lib/sequel/metaprogramming.rb +44 -0
- data/lib/sequel/migration.rb +212 -0
- data/lib/sequel/model.rb +142 -0
- data/lib/sequel/model/association_reflection.rb +263 -0
- data/lib/sequel/model/associations.rb +1024 -0
- data/lib/sequel/model/base.rb +911 -0
- data/lib/sequel/model/deprecated.rb +188 -0
- data/lib/sequel/model/deprecated_hooks.rb +103 -0
- data/lib/sequel/model/deprecated_inflector.rb +335 -0
- data/lib/sequel/model/deprecated_validations.rb +384 -0
- data/lib/sequel/model/errors.rb +37 -0
- data/lib/sequel/model/exceptions.rb +7 -0
- data/lib/sequel/model/inflections.rb +230 -0
- data/lib/sequel/model/plugins.rb +74 -0
- data/lib/sequel/object_graph.rb +230 -0
- data/lib/sequel/plugins/caching.rb +122 -0
- data/lib/sequel/plugins/hook_class_methods.rb +122 -0
- data/lib/sequel/plugins/schema.rb +53 -0
- data/lib/sequel/plugins/single_table_inheritance.rb +63 -0
- data/lib/sequel/plugins/validation_class_methods.rb +373 -0
- data/lib/sequel/sql.rb +854 -0
- data/lib/sequel/version.rb +11 -0
- data/lib/sequel_core.rb +1 -0
- data/lib/sequel_model.rb +1 -0
- data/spec/adapters/ado_spec.rb +46 -0
- data/spec/adapters/firebird_spec.rb +376 -0
- data/spec/adapters/informix_spec.rb +96 -0
- data/spec/adapters/mysql_spec.rb +875 -0
- data/spec/adapters/oracle_spec.rb +272 -0
- data/spec/adapters/postgres_spec.rb +692 -0
- data/spec/adapters/spec_helper.rb +10 -0
- data/spec/adapters/sqlite_spec.rb +550 -0
- data/spec/core/connection_pool_spec.rb +526 -0
- data/spec/core/core_ext_spec.rb +156 -0
- data/spec/core/core_sql_spec.rb +528 -0
- data/spec/core/database_spec.rb +1214 -0
- data/spec/core/dataset_spec.rb +3513 -0
- data/spec/core/expression_filters_spec.rb +363 -0
- data/spec/core/migration_spec.rb +261 -0
- data/spec/core/object_graph_spec.rb +280 -0
- data/spec/core/pretty_table_spec.rb +58 -0
- data/spec/core/schema_generator_spec.rb +167 -0
- data/spec/core/schema_spec.rb +778 -0
- data/spec/core/spec_helper.rb +82 -0
- data/spec/core/version_spec.rb +7 -0
- data/spec/extensions/blank_spec.rb +67 -0
- data/spec/extensions/caching_spec.rb +201 -0
- data/spec/extensions/hook_class_methods_spec.rb +470 -0
- data/spec/extensions/inflector_spec.rb +122 -0
- data/spec/extensions/pagination_spec.rb +99 -0
- data/spec/extensions/pretty_table_spec.rb +91 -0
- data/spec/extensions/query_spec.rb +85 -0
- data/spec/extensions/schema_spec.rb +111 -0
- data/spec/extensions/single_table_inheritance_spec.rb +53 -0
- data/spec/extensions/spec_helper.rb +90 -0
- data/spec/extensions/string_date_time_spec.rb +93 -0
- data/spec/extensions/validation_class_methods_spec.rb +1054 -0
- data/spec/integration/dataset_test.rb +160 -0
- data/spec/integration/eager_loader_test.rb +683 -0
- data/spec/integration/prepared_statement_test.rb +130 -0
- data/spec/integration/schema_test.rb +183 -0
- data/spec/integration/spec_helper.rb +75 -0
- data/spec/integration/type_test.rb +96 -0
- data/spec/model/association_reflection_spec.rb +93 -0
- data/spec/model/associations_spec.rb +1780 -0
- data/spec/model/base_spec.rb +494 -0
- data/spec/model/caching_spec.rb +217 -0
- data/spec/model/dataset_methods_spec.rb +78 -0
- data/spec/model/eager_loading_spec.rb +1165 -0
- data/spec/model/hooks_spec.rb +472 -0
- data/spec/model/inflector_spec.rb +126 -0
- data/spec/model/model_spec.rb +588 -0
- data/spec/model/plugins_spec.rb +142 -0
- data/spec/model/record_spec.rb +1243 -0
- data/spec/model/schema_spec.rb +92 -0
- data/spec/model/spec_helper.rb +124 -0
- data/spec/model/validations_spec.rb +1080 -0
- data/spec/rcov.opts +6 -0
- data/spec/spec.opts +0 -0
- data/spec/spec_config.rb.example +10 -0
- metadata +202 -0
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'spec_helper')
|
|
2
|
+
|
|
3
|
+
context "Array#extract_options!" do
|
|
4
|
+
deprec_specify "should pop the last item if it is a hash" do
|
|
5
|
+
a = [1,2,{1=>2}]
|
|
6
|
+
a.extract_options!.should == {1=>2}
|
|
7
|
+
a.should == [1,2]
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
deprec_specify "should return an empty hash if the last item is not a hash" do
|
|
11
|
+
a = [1,2]
|
|
12
|
+
a.extract_options!.should == {}
|
|
13
|
+
a.should == [1,2]
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
context "Enumerable#send_each" do
|
|
18
|
+
deprec_specify "should send the supplied method to each item" do
|
|
19
|
+
a = ['abbc', 'bbccdd', 'hebtre']
|
|
20
|
+
a.send_each(:gsub!, 'b', '_')
|
|
21
|
+
a.should == ['a__c', '__ccdd', 'he_tre']
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
context "Range#interval" do
|
|
26
|
+
deprec_specify "should return the interval between the beginning and end for an inclusive range" do
|
|
27
|
+
(1..10).interval.should == 9
|
|
28
|
+
|
|
29
|
+
r = rand(100000) + 10
|
|
30
|
+
t1 = Time.now.to_i; t2 = t1 + r
|
|
31
|
+
(t1..t2).interval.should == r
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
deprec_specify "should return the interval between the beginning and end for an exclusive range" do
|
|
35
|
+
(1...10).interval.should == 8
|
|
36
|
+
|
|
37
|
+
r = rand(100000) + 10
|
|
38
|
+
t1 = Time.now.to_i; t2 = t1 + r
|
|
39
|
+
(t1...t2).interval.should == r - 1
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
context "Module#class_attr_reader" do
|
|
44
|
+
deprec_specify "it should create instance methods that call class methods of the same name" do
|
|
45
|
+
@c = Class.new do
|
|
46
|
+
def self.x; 1; end
|
|
47
|
+
class_attr_reader :x
|
|
48
|
+
end
|
|
49
|
+
@c.new.x.should == 1
|
|
50
|
+
def @c.x; 2; end
|
|
51
|
+
@c.new.x.should == 2
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
context "Module#metaalias" do
|
|
56
|
+
deprec_specify "it should create aliases of singleton/class methods" do
|
|
57
|
+
@c = Class.new do
|
|
58
|
+
def self.x; 1; end
|
|
59
|
+
metaalias :y, :x
|
|
60
|
+
end
|
|
61
|
+
@c.y.should == 1
|
|
62
|
+
def @c.x; 2; end
|
|
63
|
+
@c.y.should == 1
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
context "Module#metaattr_reader" do
|
|
68
|
+
deprec_specify "it should create attr_readers of singleton/class methods" do
|
|
69
|
+
@c = Class.new do
|
|
70
|
+
@y = 1
|
|
71
|
+
@x = 2
|
|
72
|
+
metaattr_reader :y, :x
|
|
73
|
+
end
|
|
74
|
+
@c.y.should == 1
|
|
75
|
+
@c.x.should == 2
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
context "Object#is_one_of?" do
|
|
80
|
+
deprec_specify "it should be true if the object is one of the classes" do
|
|
81
|
+
1.is_one_of?(Numeric, Array).should == true
|
|
82
|
+
[].is_one_of?(Numeric, Array).should == true
|
|
83
|
+
{}.is_one_of?(Numeric, Enumerable).should == true
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
deprec_specify "it should be false if the object is not one of the classes" do
|
|
87
|
+
'a'.is_one_of?(Numeric, Array).should == false
|
|
88
|
+
Object.new.is_one_of?(Numeric, Array).should == false
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
context "Object#blank?" do
|
|
93
|
+
deprec_specify "it should be true if the object responds true to empty?" do
|
|
94
|
+
[].blank?.should == true
|
|
95
|
+
{}.blank?.should == true
|
|
96
|
+
o = Object.new
|
|
97
|
+
def o.empty?; true; end
|
|
98
|
+
o.blank?.should == true
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
deprec_specify "it should be false if the object doesn't respond true to empty?" do
|
|
102
|
+
[2].blank?.should == false
|
|
103
|
+
{1=>2}.blank?.should == false
|
|
104
|
+
Object.new.blank?.should == false
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
context "Numeric#blank?" do
|
|
109
|
+
deprec_specify "it should always be false" do
|
|
110
|
+
1.blank?.should == false
|
|
111
|
+
0.blank?.should == false
|
|
112
|
+
-1.blank?.should == false
|
|
113
|
+
1.0.blank?.should == false
|
|
114
|
+
0.0.blank?.should == false
|
|
115
|
+
-1.0.blank?.should == false
|
|
116
|
+
10000000000000000.blank?.should == false
|
|
117
|
+
-10000000000000000.blank?.should == false
|
|
118
|
+
10000000000000000.0.blank?.should == false
|
|
119
|
+
-10000000000000000.0.blank?.should == false
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
context "NilClass#blank?" do
|
|
124
|
+
deprec_specify "it should always be true" do
|
|
125
|
+
nil.blank?.should == true
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
context "TrueClass#blank?" do
|
|
130
|
+
deprec_specify "it should always be false" do
|
|
131
|
+
true.blank?.should == false
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
context "FalseClass#blank?" do
|
|
136
|
+
deprec_specify "it should always be true" do
|
|
137
|
+
false.blank?.should == true
|
|
138
|
+
end
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
context "FalseClass#blank?" do
|
|
142
|
+
deprec_specify "it should be true if the string is empty" do
|
|
143
|
+
''.blank?.should == true
|
|
144
|
+
end
|
|
145
|
+
deprec_specify "it should be true if the string is composed of just whitespace" do
|
|
146
|
+
' '.blank?.should == true
|
|
147
|
+
"\r\n\t".blank?.should == true
|
|
148
|
+
(' '*4000).blank?.should == true
|
|
149
|
+
("\r\n\t"*4000).blank?.should == true
|
|
150
|
+
end
|
|
151
|
+
deprec_specify "it should be false if the string has any non whitespace characters" do
|
|
152
|
+
'1'.blank?.should == false
|
|
153
|
+
("\r\n\t"*4000 + 'a').blank?.should == false
|
|
154
|
+
("\r\na\t"*4000).blank?.should == false
|
|
155
|
+
end
|
|
156
|
+
end
|
|
@@ -0,0 +1,528 @@
|
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'spec_helper')
|
|
2
|
+
|
|
3
|
+
context "Array#all_two_pairs?" do
|
|
4
|
+
specify "should return false if empty" do
|
|
5
|
+
[].all_two_pairs?.should == false
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
specify "should return false if any of the elements is not an array" do
|
|
9
|
+
[1].all_two_pairs?.should == false
|
|
10
|
+
[[1,2],1].all_two_pairs?.should == false
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
specify "should return false if any of the elements has a length other than two" do
|
|
14
|
+
[[1,2],[]].all_two_pairs?.should == false
|
|
15
|
+
[[1,2],[1]].all_two_pairs?.should == false
|
|
16
|
+
[[1,2],[1,2,3]].all_two_pairs?.should == false
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
specify "should return true if all of the elements are arrays with a length of two" do
|
|
20
|
+
[[1,2]].all_two_pairs?.should == true
|
|
21
|
+
[[1,2],[1,2]].all_two_pairs?.should == true
|
|
22
|
+
[[1,2],[1,2],[1,2]].all_two_pairs?.should == true
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
context "Array#case and Hash#case" do
|
|
27
|
+
setup do
|
|
28
|
+
@d = Sequel::Dataset.new(nil)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
specify "should return SQL CASE expression" do
|
|
32
|
+
@d.literal({:x=>:y}.case(:z)).should == '(CASE WHEN x THEN y ELSE z END)'
|
|
33
|
+
@d.literal({:x=>:y}.case(:z, :exp)).should == '(CASE exp WHEN x THEN y ELSE z END)'
|
|
34
|
+
['(CASE WHEN x THEN y WHEN a THEN b ELSE z END)',
|
|
35
|
+
'(CASE WHEN a THEN b WHEN x THEN y ELSE z END)'].should(include(@d.literal({:x=>:y, :a=>:b}.case(:z))))
|
|
36
|
+
@d.literal([[:x, :y]].case(:z)).should == '(CASE WHEN x THEN y ELSE z END)'
|
|
37
|
+
@d.literal([[:x, :y], [:a, :b]].case(:z)).should == '(CASE WHEN x THEN y WHEN a THEN b ELSE z END)'
|
|
38
|
+
@d.literal([[:x, :y], [:a, :b]].case(:z, :exp)).should == '(CASE exp WHEN x THEN y WHEN a THEN b ELSE z END)'
|
|
39
|
+
@d.literal([[:x, :y], [:a, :b]].case(:z, :exp__w)).should == '(CASE exp.w WHEN x THEN y WHEN a THEN b ELSE z END)'
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
specify "should raise an error if an array that isn't all two pairs is used" do
|
|
43
|
+
proc{[:b].case(:a)}.should raise_error(Sequel::Error)
|
|
44
|
+
proc{[:b, :c].case(:a)}.should raise_error(Sequel::Error)
|
|
45
|
+
proc{[[:b, :c], :d].case(:a)}.should raise_error(Sequel::Error)
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
specify "should raise an error if an empty array/hash is used" do
|
|
49
|
+
proc{[].case(:a)}.should raise_error(Sequel::Error)
|
|
50
|
+
proc{{}.case(:a)}.should raise_error(Sequel::Error)
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
context "Array#sql_array" do
|
|
55
|
+
setup do
|
|
56
|
+
@d = Sequel::Dataset.new(nil)
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
specify "should treat the array as an SQL array instead of conditions" do
|
|
60
|
+
@d.literal([[:x, 1], [:y, 2]]).should == '((x = 1) AND (y = 2))'
|
|
61
|
+
@d.literal([[:x, 1], [:y, 2]].sql_array).should == '((x, 1), (y, 2))'
|
|
62
|
+
@d.filter([:a, :b]=>[[:x, 1], [:y, 2]].sql_array).sql.should == 'SELECT * WHERE ((a, b) IN ((x, 1), (y, 2)))'
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
context "Array#to_sql" do
|
|
67
|
+
deprec_specify "should concatenate multiple lines into a single string" do
|
|
68
|
+
"SELECT * \r\nFROM items\r\n WHERE a = 1".split.to_sql. \
|
|
69
|
+
should == 'SELECT * FROM items WHERE a = 1'
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
deprec_specify "should remove superfluous white space and line breaks" do
|
|
73
|
+
"\tSELECT * \n FROM items ".split.to_sql. \
|
|
74
|
+
should == 'SELECT * FROM items'
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
deprec_specify "should remove ANSI SQL comments" do
|
|
78
|
+
"SELECT * --comment\r\n FROM items\r\n --comment".split.to_sql. \
|
|
79
|
+
should == 'SELECT * FROM items'
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
deprec_specify "should remove C-style comments" do
|
|
83
|
+
"SELECT * \r\n /* comment comment\r\n comment\r\n FROM items */\r\n FROM items\r\n--comment".split.to_sql. \
|
|
84
|
+
should == 'SELECT * FROM items'
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
context "String#to_sql" do
|
|
89
|
+
deprec_specify "should concatenate multiple lines into a single string" do
|
|
90
|
+
"SELECT * \r\nFROM items\r\nWHERE a = 1".to_sql. \
|
|
91
|
+
should == 'SELECT * FROM items WHERE a = 1'
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
deprec_specify "should remove superfluous white space and line breaks" do
|
|
95
|
+
"\tSELECT * \r\n FROM items ".to_sql. \
|
|
96
|
+
should == 'SELECT * FROM items'
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
deprec_specify "should remove ANSI SQL comments" do
|
|
100
|
+
"SELECT * --comment \r\n FROM items\r\n --comment".to_sql. \
|
|
101
|
+
should == 'SELECT * FROM items'
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
deprec_specify "should remove C-style comments" do
|
|
105
|
+
"SELECT * \r\n/* comment comment\r\ncomment\r\nFROM items */\r\nFROM items\r\n--comment".to_sql. \
|
|
106
|
+
should == 'SELECT * FROM items'
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
context "String#lit" do
|
|
111
|
+
specify "should return an LiteralString object" do
|
|
112
|
+
'xyz'.lit.should be_a_kind_of(Sequel::LiteralString)
|
|
113
|
+
'xyz'.lit.to_s.should == 'xyz'
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
specify "should inhibit string literalization" do
|
|
117
|
+
Sequel::Database.new[:t].update_sql(:stamp => "NOW()".lit).should == \
|
|
118
|
+
"UPDATE t SET stamp = NOW()"
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
deprec_specify "should be aliased as expr" do
|
|
122
|
+
'xyz'.expr.should be_a_kind_of(Sequel::LiteralString)
|
|
123
|
+
'xyz'.expr.to_s.should == 'xyz'
|
|
124
|
+
Sequel::Database.new[:t].update_sql(:stamp => "NOW()".expr).should == \
|
|
125
|
+
"UPDATE t SET stamp = NOW()"
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
specify "should return a PlaceholderLiteralString object if args are given" do
|
|
129
|
+
a = 'DISTINCT ?'.lit(:a)
|
|
130
|
+
a.should be_a_kind_of(Sequel::SQL::PlaceholderLiteralString)
|
|
131
|
+
ds = MockDatabase.new.dataset
|
|
132
|
+
ds.literal(a).should == 'DISTINCT a'
|
|
133
|
+
ds.quote_identifiers = true
|
|
134
|
+
ds.literal(a).should == 'DISTINCT "a"'
|
|
135
|
+
end
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
context "String#to_sequel_blob" do
|
|
139
|
+
specify "should return a Blob object" do
|
|
140
|
+
'xyz'.to_sequel_blob.should be_a_kind_of(::Sequel::SQL::Blob)
|
|
141
|
+
'xyz'.to_sequel_blob.should == 'xyz'
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
specify "should retain binary data" do
|
|
145
|
+
"\1\2\3\4".to_sequel_blob.should == "\1\2\3\4"
|
|
146
|
+
end
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
context "String#split_sql" do
|
|
150
|
+
deprec_specify "should split a string containing multiple statements" do
|
|
151
|
+
"DROP TABLE a; DROP TABLE c".split_sql.should == \
|
|
152
|
+
['DROP TABLE a', 'DROP TABLE c']
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
deprec_specify "should remove comments from the string" do
|
|
156
|
+
"DROP TABLE a;/* DROP TABLE b; DROP TABLE c;*/DROP TABLE d".split_sql.should == \
|
|
157
|
+
['DROP TABLE a', 'DROP TABLE d']
|
|
158
|
+
end
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
context "#desc" do
|
|
162
|
+
setup do
|
|
163
|
+
@ds = Sequel::Dataset.new(nil)
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
specify "should format a DESC clause for a column ref" do
|
|
167
|
+
:test.desc.to_s(@ds).should == 'test DESC'
|
|
168
|
+
|
|
169
|
+
:items__price.desc.to_s(@ds).should == 'items.price DESC'
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
specify "should format a DESC clause for a function" do
|
|
173
|
+
:avg.sql_function(:test).desc.to_s(@ds).should == 'avg(test) DESC'
|
|
174
|
+
end
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
context "#asc" do
|
|
178
|
+
setup do
|
|
179
|
+
@ds = Sequel::Dataset.new(nil)
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
specify "should format a ASC clause for a column ref" do
|
|
183
|
+
:test.asc.to_s(@ds).should == 'test ASC'
|
|
184
|
+
|
|
185
|
+
:items__price.asc.to_s(@ds).should == 'items.price ASC'
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
specify "should format a ASC clause for a function" do
|
|
189
|
+
:avg.sql_function(:test).asc.to_s(@ds).should == 'avg(test) ASC'
|
|
190
|
+
end
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
context "#as" do
|
|
194
|
+
setup do
|
|
195
|
+
@ds = Sequel::Dataset.new(nil)
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
specify "should format a AS clause for a column ref" do
|
|
199
|
+
:test.as(:t).to_s(@ds).should == 'test AS t'
|
|
200
|
+
|
|
201
|
+
:items__price.as(:p).to_s(@ds).should == 'items.price AS p'
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
specify "should format a AS clause for a function" do
|
|
205
|
+
:avg.sql_function(:test).as(:avg).to_s(@ds).should == 'avg(test) AS avg'
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
specify "should format a AS clause for a literal value" do
|
|
209
|
+
'abc'.as(:abc).to_s(@ds).should == "'abc' AS abc"
|
|
210
|
+
end
|
|
211
|
+
end
|
|
212
|
+
|
|
213
|
+
context "Column references" do
|
|
214
|
+
setup do
|
|
215
|
+
@c = Class.new(Sequel::Dataset) do
|
|
216
|
+
def quoted_identifier(c); "`#{c}`"; end
|
|
217
|
+
end
|
|
218
|
+
@ds = @c.new(MockDatabase.new)
|
|
219
|
+
@ds.quote_identifiers = true
|
|
220
|
+
end
|
|
221
|
+
|
|
222
|
+
specify "should be quoted properly" do
|
|
223
|
+
@ds.literal(:xyz).should == "`xyz`"
|
|
224
|
+
@ds.literal(:xyz__abc).should == "`xyz`.`abc`"
|
|
225
|
+
|
|
226
|
+
@ds.literal(:xyz.as(:x)).should == "`xyz` AS `x`"
|
|
227
|
+
@ds.literal(:xyz__abc.as(:x)).should == "`xyz`.`abc` AS `x`"
|
|
228
|
+
|
|
229
|
+
@ds.literal(:xyz___x).should == "`xyz` AS `x`"
|
|
230
|
+
@ds.literal(:xyz__abc___x).should == "`xyz`.`abc` AS `x`"
|
|
231
|
+
end
|
|
232
|
+
|
|
233
|
+
specify "should be quoted properly in SQL functions" do
|
|
234
|
+
@ds.literal(:avg.sql_function(:xyz)).should == "avg(`xyz`)"
|
|
235
|
+
@ds.literal(:avg.sql_function(:xyz, 1)).should == "avg(`xyz`, 1)"
|
|
236
|
+
@ds.literal(:avg.sql_function(:xyz).as(:a)).should == "avg(`xyz`) AS `a`"
|
|
237
|
+
end
|
|
238
|
+
|
|
239
|
+
specify "should be quoted properly in ASC/DESC clauses" do
|
|
240
|
+
@ds.literal(:xyz.asc).should == "`xyz` ASC"
|
|
241
|
+
@ds.literal(:avg.sql_function(:xyz, 1).desc).should == "avg(`xyz`, 1) DESC"
|
|
242
|
+
end
|
|
243
|
+
|
|
244
|
+
specify "should be quoted properly in a cast function" do
|
|
245
|
+
@ds.literal(:x.cast(:integer)).should == "CAST(`x` AS integer)"
|
|
246
|
+
@ds.literal(:x__y.cast('varchar(20)')).should == "CAST(`x`.`y` AS varchar(20))"
|
|
247
|
+
end
|
|
248
|
+
end
|
|
249
|
+
|
|
250
|
+
context "Blob" do
|
|
251
|
+
specify "#to_sequel_blob should return self" do
|
|
252
|
+
blob = "x".to_sequel_blob
|
|
253
|
+
blob.to_sequel_blob.object_id.should == blob.object_id
|
|
254
|
+
end
|
|
255
|
+
end
|
|
256
|
+
|
|
257
|
+
if RUBY_VERSION < '1.9.0'
|
|
258
|
+
context "Symbol#[]" do
|
|
259
|
+
specify "should format an SQL Function" do
|
|
260
|
+
ds = Sequel::Dataset.new(nil)
|
|
261
|
+
ds.literal(:xyz[]).should == 'xyz()'
|
|
262
|
+
ds.literal(:xyz[1]).should == 'xyz(1)'
|
|
263
|
+
ds.literal(:xyz[1, 2, :abc[3]]).should == 'xyz(1, 2, abc(3))'
|
|
264
|
+
end
|
|
265
|
+
end
|
|
266
|
+
end
|
|
267
|
+
|
|
268
|
+
context "Symbol#*" do
|
|
269
|
+
setup do
|
|
270
|
+
@ds = Sequel::Dataset.new(nil)
|
|
271
|
+
end
|
|
272
|
+
|
|
273
|
+
specify "should format a qualified wildcard if no argument" do
|
|
274
|
+
:xyz.*.to_s(@ds).should == 'xyz.*'
|
|
275
|
+
:abc.*.to_s(@ds).should == 'abc.*'
|
|
276
|
+
end
|
|
277
|
+
|
|
278
|
+
specify "should format a filter expression if an argument" do
|
|
279
|
+
:xyz.*(3).to_s(@ds).should == '(xyz * 3)'
|
|
280
|
+
:abc.*(5).to_s(@ds).should == '(abc * 5)'
|
|
281
|
+
end
|
|
282
|
+
|
|
283
|
+
specify "should support qualified symbols if no argument" do
|
|
284
|
+
:xyz__abc.*.to_s(@ds).should == 'xyz.abc.*'
|
|
285
|
+
end
|
|
286
|
+
end
|
|
287
|
+
|
|
288
|
+
context "Symbol" do
|
|
289
|
+
before do
|
|
290
|
+
@ds = Sequel::Dataset.new(nil)
|
|
291
|
+
@ds.quote_identifiers = true
|
|
292
|
+
@ds.identifier_input_method = :upcase
|
|
293
|
+
end
|
|
294
|
+
|
|
295
|
+
specify "#identifier should format an identifier" do
|
|
296
|
+
@ds.literal(:xyz__abc.identifier).should == '"XYZ__ABC"'
|
|
297
|
+
end
|
|
298
|
+
|
|
299
|
+
specify "#qualify should format a qualified column" do
|
|
300
|
+
@ds.literal(:xyz.qualify(:abc)).should == '"ABC"."XYZ"'
|
|
301
|
+
end
|
|
302
|
+
|
|
303
|
+
specify "should be able to qualify an identifier" do
|
|
304
|
+
@ds.literal(:xyz.identifier.qualify(:xyz__abc)).should == '"XYZ"."ABC"."XYZ"'
|
|
305
|
+
end
|
|
306
|
+
|
|
307
|
+
specify "should be able to specify a schema.table.column" do
|
|
308
|
+
@ds.literal(:column.qualify(:table.qualify(:schema))).should == '"SCHEMA"."TABLE"."COLUMN"'
|
|
309
|
+
@ds.literal(:column.qualify(:table__name.identifier.qualify(:schema))).should == '"SCHEMA"."TABLE__NAME"."COLUMN"'
|
|
310
|
+
end
|
|
311
|
+
end
|
|
312
|
+
|
|
313
|
+
context "Dataset#literal" do
|
|
314
|
+
setup do
|
|
315
|
+
@ds = MockDataset.new(nil)
|
|
316
|
+
end
|
|
317
|
+
|
|
318
|
+
specify "should convert qualified symbol notation into dot notation" do
|
|
319
|
+
@ds.literal(:abc__def).should == 'abc.def'
|
|
320
|
+
end
|
|
321
|
+
|
|
322
|
+
specify "should convert AS symbol notation into SQL AS notation" do
|
|
323
|
+
@ds.literal(:xyz___x).should == 'xyz AS x'
|
|
324
|
+
@ds.literal(:abc__def___x).should == 'abc.def AS x'
|
|
325
|
+
end
|
|
326
|
+
|
|
327
|
+
specify "should support names with digits" do
|
|
328
|
+
@ds.literal(:abc2).should == 'abc2'
|
|
329
|
+
@ds.literal(:xx__yy3).should == 'xx.yy3'
|
|
330
|
+
@ds.literal(:ab34__temp3_4ax).should == 'ab34.temp3_4ax'
|
|
331
|
+
@ds.literal(:x1___y2).should == 'x1 AS y2'
|
|
332
|
+
@ds.literal(:abc2__def3___ggg4).should == 'abc2.def3 AS ggg4'
|
|
333
|
+
end
|
|
334
|
+
|
|
335
|
+
specify "should support upper case and lower case" do
|
|
336
|
+
@ds.literal(:ABC).should == 'ABC'
|
|
337
|
+
@ds.literal(:Zvashtoy__aBcD).should == 'Zvashtoy.aBcD'
|
|
338
|
+
end
|
|
339
|
+
|
|
340
|
+
specify "should support spaces inside column names" do
|
|
341
|
+
@ds.quote_identifiers = true
|
|
342
|
+
@ds.literal(:"AB C").should == '"AB C"'
|
|
343
|
+
@ds.literal(:"Zvas htoy__aB cD").should == '"Zvas htoy"."aB cD"'
|
|
344
|
+
@ds.literal(:"aB cD___XX XX").should == '"aB cD" AS "XX XX"'
|
|
345
|
+
@ds.literal(:"Zva shtoy__aB cD___XX XX").should == '"Zva shtoy"."aB cD" AS "XX XX"'
|
|
346
|
+
end
|
|
347
|
+
end
|
|
348
|
+
|
|
349
|
+
context "Symbol" do
|
|
350
|
+
setup do
|
|
351
|
+
@ds = Sequel::Dataset.new(MockDatabase.new)
|
|
352
|
+
end
|
|
353
|
+
|
|
354
|
+
specify "should support upper case outer functions" do
|
|
355
|
+
:COUNT.sql_function('1').to_s(@ds).should == "COUNT('1')"
|
|
356
|
+
end
|
|
357
|
+
|
|
358
|
+
specify "should inhibit string literalization" do
|
|
359
|
+
db = Sequel::Database.new
|
|
360
|
+
ds = db[:t]
|
|
361
|
+
ds.select(:COUNT.sql_function('1')).sql.should == "SELECT COUNT('1') FROM t"
|
|
362
|
+
end
|
|
363
|
+
|
|
364
|
+
specify "should support cast method" do
|
|
365
|
+
:abc.cast(:integer).to_s(@ds).should == "CAST(abc AS integer)"
|
|
366
|
+
end
|
|
367
|
+
|
|
368
|
+
deprec_specify "should support cast_as method" do
|
|
369
|
+
:abc.cast_as(:integer).to_s(@ds).should == "CAST(abc AS integer)"
|
|
370
|
+
end
|
|
371
|
+
|
|
372
|
+
specify "should support cast_numeric and cast_string" do
|
|
373
|
+
x = :abc.cast_numeric
|
|
374
|
+
x.should be_a_kind_of(Sequel::SQL::NumericExpression)
|
|
375
|
+
x.to_s(@ds).should == "CAST(abc AS integer)"
|
|
376
|
+
|
|
377
|
+
x = :abc.cast_numeric(:real)
|
|
378
|
+
x.should be_a_kind_of(Sequel::SQL::NumericExpression)
|
|
379
|
+
x.to_s(@ds).should == "CAST(abc AS real)"
|
|
380
|
+
|
|
381
|
+
x = :abc.cast_string
|
|
382
|
+
x.should be_a_kind_of(Sequel::SQL::StringExpression)
|
|
383
|
+
x.to_s(@ds).should == "CAST(abc AS varchar(255))"
|
|
384
|
+
|
|
385
|
+
x = :abc.cast_string(:varchar)
|
|
386
|
+
x.should be_a_kind_of(Sequel::SQL::StringExpression)
|
|
387
|
+
x.to_s(@ds).should == "CAST(abc AS varchar)"
|
|
388
|
+
end
|
|
389
|
+
|
|
390
|
+
specify "should allow database independent types when casting" do
|
|
391
|
+
m = MockDatabase.new
|
|
392
|
+
m.instance_eval do
|
|
393
|
+
def type_literal_base(column)
|
|
394
|
+
return :foo if column[:type] == Integer
|
|
395
|
+
return :bar if column[:type] == String
|
|
396
|
+
column
|
|
397
|
+
end
|
|
398
|
+
end
|
|
399
|
+
@ds2 = Sequel::Dataset.new(m)
|
|
400
|
+
:abc.cast(String).to_s(@ds).should == "CAST(abc AS varchar(255))"
|
|
401
|
+
:abc.cast(String).to_s(@ds2).should == "CAST(abc AS bar)"
|
|
402
|
+
:abc.cast(String).to_s(@ds2).should == "CAST(abc AS bar)"
|
|
403
|
+
:abc.cast_string.to_s(@ds2).should == "CAST(abc AS bar)"
|
|
404
|
+
:abc.cast_string(Integer).to_s(@ds2).should == "CAST(abc AS foo)"
|
|
405
|
+
:abc.cast_numeric.to_s(@ds2).should == "CAST(abc AS foo)"
|
|
406
|
+
:abc.cast_numeric(String).to_s(@ds2).should == "CAST(abc AS bar)"
|
|
407
|
+
end
|
|
408
|
+
|
|
409
|
+
deprec_specify "should support subscript access using | operator" do
|
|
410
|
+
(:abc|1).to_s(@ds).should == 'abc[1]'
|
|
411
|
+
(:abc|[1]).to_s(@ds).should == 'abc[1]'
|
|
412
|
+
(:abc|[1, 2]).to_s(@ds).should == 'abc[1, 2]'
|
|
413
|
+
(:abc|1|2).to_s(@ds).should == 'abc[1, 2]'
|
|
414
|
+
end
|
|
415
|
+
|
|
416
|
+
specify "should support SQL EXTRACT function via #extract " do
|
|
417
|
+
:abc.extract(:year).to_s(@ds).should == "extract(year FROM abc)"
|
|
418
|
+
end
|
|
419
|
+
end
|
|
420
|
+
|
|
421
|
+
context "String#to_time" do
|
|
422
|
+
deprec_specify "should convert the string into a Time object" do
|
|
423
|
+
"2007-07-11".to_time.should == Time.parse("2007-07-11")
|
|
424
|
+
"06:30".to_time.should == Time.parse("06:30")
|
|
425
|
+
end
|
|
426
|
+
|
|
427
|
+
deprec_specify "should raise Error::InvalidValue for an invalid time" do
|
|
428
|
+
proc {'0000-00-00'.to_time}.should raise_error(Sequel::Error::InvalidValue)
|
|
429
|
+
end
|
|
430
|
+
end
|
|
431
|
+
|
|
432
|
+
context "String#to_date" do
|
|
433
|
+
after do
|
|
434
|
+
Sequel.convert_two_digit_years = true
|
|
435
|
+
end
|
|
436
|
+
|
|
437
|
+
deprec_specify "should convert the string into a Date object" do
|
|
438
|
+
"2007-07-11".to_date.should == Date.parse("2007-07-11")
|
|
439
|
+
end
|
|
440
|
+
|
|
441
|
+
deprec_specify "should convert 2 digit years by default" do
|
|
442
|
+
"July 11, 07".to_date.should == Date.parse("2007-07-11")
|
|
443
|
+
end
|
|
444
|
+
|
|
445
|
+
deprec_specify "should not convert 2 digit years if set not to" do
|
|
446
|
+
Sequel.convert_two_digit_years = false
|
|
447
|
+
"July 11, 07".to_date.should == Date.parse("0007-07-11")
|
|
448
|
+
end
|
|
449
|
+
|
|
450
|
+
deprec_specify "should raise Error::InvalidValue for an invalid date" do
|
|
451
|
+
proc {'0000-00-00'.to_date}.should raise_error(Sequel::Error::InvalidValue)
|
|
452
|
+
end
|
|
453
|
+
end
|
|
454
|
+
|
|
455
|
+
context "String#to_datetime" do
|
|
456
|
+
after do
|
|
457
|
+
Sequel.convert_two_digit_years = true
|
|
458
|
+
end
|
|
459
|
+
|
|
460
|
+
deprec_specify "should convert the string into a DateTime object" do
|
|
461
|
+
"2007-07-11 10:11:12a".to_datetime.should == DateTime.parse("2007-07-11 10:11:12a")
|
|
462
|
+
end
|
|
463
|
+
|
|
464
|
+
deprec_specify "should convert 2 digit years by default" do
|
|
465
|
+
"July 11, 07 10:11:12a".to_datetime.should == DateTime.parse("2007-07-11 10:11:12a")
|
|
466
|
+
end
|
|
467
|
+
|
|
468
|
+
deprec_specify "should not convert 2 digit years if set not to" do
|
|
469
|
+
Sequel.convert_two_digit_years = false
|
|
470
|
+
"July 11, 07 10:11:12a".to_datetime.should == DateTime.parse("0007-07-11 10:11:12a")
|
|
471
|
+
end
|
|
472
|
+
|
|
473
|
+
deprec_specify "should raise Error::InvalidValue for an invalid date" do
|
|
474
|
+
proc {'0000-00-00'.to_datetime}.should raise_error(Sequel::Error::InvalidValue)
|
|
475
|
+
end
|
|
476
|
+
end
|
|
477
|
+
|
|
478
|
+
context "String#to_sequel_time" do
|
|
479
|
+
after do
|
|
480
|
+
Sequel.datetime_class = Time
|
|
481
|
+
Sequel.convert_two_digit_years = true
|
|
482
|
+
end
|
|
483
|
+
|
|
484
|
+
deprec_specify "should convert the string into a Time object by default" do
|
|
485
|
+
"2007-07-11 10:11:12a".to_sequel_time.class.should == Time
|
|
486
|
+
"2007-07-11 10:11:12a".to_sequel_time.should == Time.parse("2007-07-11 10:11:12a")
|
|
487
|
+
end
|
|
488
|
+
|
|
489
|
+
deprec_specify "should convert the string into a DateTime object if that is set" do
|
|
490
|
+
Sequel.datetime_class = DateTime
|
|
491
|
+
"2007-07-11 10:11:12a".to_sequel_time.class.should == DateTime
|
|
492
|
+
"2007-07-11 10:11:12a".to_sequel_time.should == DateTime.parse("2007-07-11 10:11:12a")
|
|
493
|
+
end
|
|
494
|
+
|
|
495
|
+
deprec_specify "should convert 2 digit years by default if using DateTime class" do
|
|
496
|
+
Sequel.datetime_class = DateTime
|
|
497
|
+
"July 11, 07 10:11:12a".to_sequel_time.should == DateTime.parse("2007-07-11 10:11:12a")
|
|
498
|
+
end
|
|
499
|
+
|
|
500
|
+
deprec_specify "should not convert 2 digit years if set not to when using DateTime class" do
|
|
501
|
+
Sequel.datetime_class = DateTime
|
|
502
|
+
Sequel.convert_two_digit_years = false
|
|
503
|
+
"July 11, 07 10:11:12a".to_sequel_time.should == DateTime.parse("0007-07-11 10:11:12a")
|
|
504
|
+
end
|
|
505
|
+
|
|
506
|
+
deprec_specify "should raise Error::InvalidValue for an invalid time" do
|
|
507
|
+
proc {'0000-00-00'.to_sequel_time}.should raise_error(Sequel::Error::InvalidValue)
|
|
508
|
+
Sequel.datetime_class = DateTime
|
|
509
|
+
proc {'0000-00-00'.to_sequel_time}.should raise_error(Sequel::Error::InvalidValue)
|
|
510
|
+
end
|
|
511
|
+
end
|
|
512
|
+
|
|
513
|
+
context "Sequel::SQL::Function#==" do
|
|
514
|
+
specify "should be true for functions with the same name and arguments, false otherwise" do
|
|
515
|
+
a = :date.sql_function(:t)
|
|
516
|
+
b = :date.sql_function(:t)
|
|
517
|
+
a.should == b
|
|
518
|
+
(a == b).should == true
|
|
519
|
+
c = :date.sql_function(:c)
|
|
520
|
+
a.should_not == c
|
|
521
|
+
(a == c).should == false
|
|
522
|
+
d = :time.sql_function(:c)
|
|
523
|
+
a.should_not == d
|
|
524
|
+
c.should_not == d
|
|
525
|
+
(a == d).should == false
|
|
526
|
+
(c == d).should == false
|
|
527
|
+
end
|
|
528
|
+
end
|