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,130 @@
|
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'spec_helper.rb')
|
|
2
|
+
|
|
3
|
+
describe "Prepared Statements and Bound Arguments" do
|
|
4
|
+
before do
|
|
5
|
+
INTEGRATION_DB.create_table!(:items) do
|
|
6
|
+
primary_key :id
|
|
7
|
+
integer :number
|
|
8
|
+
end
|
|
9
|
+
@c = Class.new(Sequel::Model(:items))
|
|
10
|
+
@ds = INTEGRATION_DB[:items]
|
|
11
|
+
@ds.insert(:number=>10)
|
|
12
|
+
@ds.meta_def(:ba) do |sym|
|
|
13
|
+
prepared_arg_placeholder == '$' ? :"#{sym}__int" : sym
|
|
14
|
+
end
|
|
15
|
+
clear_sqls
|
|
16
|
+
end
|
|
17
|
+
after do
|
|
18
|
+
INTEGRATION_DB.drop_table(:items)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
specify "should support bound variables with select, all, and first" do
|
|
22
|
+
@ds.filter(:number=>@ds.ba(:$n)).call(:select, :n=>10).should == [{:id=>1, :number=>10}]
|
|
23
|
+
@ds.filter(:number=>@ds.ba(:$n)).call(:all, :n=>10).should == [{:id=>1, :number=>10}]
|
|
24
|
+
@ds.filter(:number=>@ds.ba(:$n)).call(:first, :n=>10).should == {:id=>1, :number=>10}
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
specify "should support placeholder literal strings" do
|
|
28
|
+
@ds.filter("number = ?", @ds.ba(:$n)).call(:select, :n=>10).should == [{:id=>1, :number=>10}]
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
specify "should support datasets with static sql and placeholders" do
|
|
32
|
+
INTEGRATION_DB["SELECT * FROM items WHERE number = ?", @ds.ba(:$n)].call(:select, :n=>10).should == [{:id=>1, :number=>10}]
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
specify "should support subselects" do
|
|
36
|
+
@ds.filter(:id=>:$i).filter(:number=>@ds.select(:number).filter(:number=>@ds.ba(:$n))).filter(:id=>:$j).call(:select, :n=>10, :i=>1, :j=>1).should == [{:id=>1, :number=>10}]
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
specify "should support subselects with literal strings" do
|
|
40
|
+
@ds.filter(:id=>:$i, :number=>@ds.select(:number).filter("number = ?", @ds.ba(:$n))).call(:select, :n=>10, :i=>1).should == [{:id=>1, :number=>10}]
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
specify "should support subselects with static sql and placeholders" do
|
|
44
|
+
@ds.filter(:id=>:$i, :number=>INTEGRATION_DB["SELECT number FROM items WHERE number = ?", @ds.ba(:$n)]).call(:select, :n=>10, :i=>1).should == [{:id=>1, :number=>10}]
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
specify "should support subselects of subselects" do
|
|
48
|
+
@ds.filter(:id=>:$i).filter(:number=>@ds.select(:number).filter(:number=>@ds.select(:number).filter(:number=>@ds.ba(:$n)))).filter(:id=>:$j).call(:select, :n=>10, :i=>1, :j=>1).should == [{:id=>1, :number=>10}]
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
specify "should support bound variables with insert" do
|
|
52
|
+
@ds.call(:insert, {:n=>20, :i=>100}, :id=>@ds.ba(:$i), :number=>@ds.ba(:$n))
|
|
53
|
+
@ds.count.should == 2
|
|
54
|
+
@ds.order(:id).all.should == [{:id=>1, :number=>10}, {:id=>100, :number=>20}]
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
specify "should have insert return primary key value when using bound arguments" do
|
|
58
|
+
@ds.call(:insert, {:n=>20}, :number=>@ds.ba(:$n)).should == 2
|
|
59
|
+
@ds.filter(:id=>2).first[:number].should == 20
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
specify "should support bound variables with delete" do
|
|
63
|
+
@ds.filter(:number=>@ds.ba(:$n)).call(:delete, :n=>10).should == 1
|
|
64
|
+
@ds.count.should == 0
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
specify "should support bound variables with update" do
|
|
68
|
+
@ds.filter(:number=>@ds.ba(:$n)).call(:update, {:n=>10, :nn=>20}, :number=>:number+@ds.ba(:$nn)).should == 1
|
|
69
|
+
@ds.all.should == [{:id=>1, :number=>30}]
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
specify "should support prepared statements with select, first, and all" do
|
|
73
|
+
@ds.filter(:number=>@ds.ba(:$n)).prepare(:select, :select_n)
|
|
74
|
+
INTEGRATION_DB.call(:select_n, :n=>10).should == [{:id=>1, :number=>10}]
|
|
75
|
+
@ds.filter(:number=>@ds.ba(:$n)).prepare(:all, :select_n)
|
|
76
|
+
INTEGRATION_DB.call(:select_n, :n=>10).should == [{:id=>1, :number=>10}]
|
|
77
|
+
@ds.filter(:number=>@ds.ba(:$n)).prepare(:first, :select_n)
|
|
78
|
+
INTEGRATION_DB.call(:select_n, :n=>10).should == {:id=>1, :number=>10}
|
|
79
|
+
if INTEGRATION_DB.uri =~ /jdbc:sqlite:/
|
|
80
|
+
# Work around for open prepared statements on a table not allowing the
|
|
81
|
+
# dropping of a table when using SQLite over JDBC
|
|
82
|
+
INTEGRATION_DB.synchronize{|c| c.prepared_statements[:select_n][1].close}
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
specify "should support prepared statements with insert" do
|
|
87
|
+
@ds.prepare(:insert, :insert_n, :id=>@ds.ba(:$i), :number=>@ds.ba(:$n))
|
|
88
|
+
INTEGRATION_DB.call(:insert_n, :n=>20, :i=>100)
|
|
89
|
+
@ds.count.should == 2
|
|
90
|
+
@ds.order(:id).all.should == [{:id=>1, :number=>10}, {:id=>100, :number=>20}]
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
specify "should have insert return primary key value when using prepared statements" do
|
|
94
|
+
@ds.prepare(:insert, :insert_n, :number=>@ds.ba(:$n))
|
|
95
|
+
INTEGRATION_DB.call(:insert_n, :n=>20).should == 2
|
|
96
|
+
@ds.filter(:id=>2).first[:number].should == 20
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
specify "should support prepared statements with delete" do
|
|
100
|
+
@ds.filter(:number=>@ds.ba(:$n)).prepare(:delete, :delete_n)
|
|
101
|
+
INTEGRATION_DB.call(:delete_n, :n=>10).should == 1
|
|
102
|
+
@ds.count.should == 0
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
specify "should support prepared statements with update" do
|
|
106
|
+
@ds.filter(:number=>@ds.ba(:$n)).prepare(:update, :update_n, :number=>:number+@ds.ba(:$nn))
|
|
107
|
+
INTEGRATION_DB.call(:update_n, :n=>10, :nn=>20).should == 1
|
|
108
|
+
@ds.all.should == [{:id=>1, :number=>30}]
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
specify "model datasets should return model instances when using select, all, and first with bound variables" do
|
|
112
|
+
@c.filter(:number=>@ds.ba(:$n)).call(:select, :n=>10).should == [@c.load(:id=>1, :number=>10)]
|
|
113
|
+
@c.filter(:number=>@ds.ba(:$n)).call(:all, :n=>10).should == [@c.load(:id=>1, :number=>10)]
|
|
114
|
+
@c.filter(:number=>@ds.ba(:$n)).call(:first, :n=>10).should == @c.load(:id=>1, :number=>10)
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
specify "model datasets should return model instances when using select, all, and first with prepared statements" do
|
|
118
|
+
@c.filter(:number=>@ds.ba(:$n)).prepare(:select, :select_n)
|
|
119
|
+
INTEGRATION_DB.call(:select_n, :n=>10).should == [@c.load(:id=>1, :number=>10)]
|
|
120
|
+
@c.filter(:number=>@ds.ba(:$n)).prepare(:all, :select_n)
|
|
121
|
+
INTEGRATION_DB.call(:select_n, :n=>10).should == [@c.load(:id=>1, :number=>10)]
|
|
122
|
+
@c.filter(:number=>@ds.ba(:$n)).prepare(:first, :select_n)
|
|
123
|
+
INTEGRATION_DB.call(:select_n, :n=>10).should == @c.load(:id=>1, :number=>10)
|
|
124
|
+
if INTEGRATION_DB.uri =~ /jdbc:sqlite:/
|
|
125
|
+
# Work around for open prepared statements on a table not allowing the
|
|
126
|
+
# dropping of a table when using SQLite over JDBC
|
|
127
|
+
INTEGRATION_DB.synchronize{|c| c.prepared_statements[:select_n][1].close}
|
|
128
|
+
end
|
|
129
|
+
end
|
|
130
|
+
end
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'spec_helper.rb')
|
|
2
|
+
|
|
3
|
+
if INTEGRATION_DB.respond_to?(:schema_parse_table, true)
|
|
4
|
+
describe "Database schema parser" do
|
|
5
|
+
before do
|
|
6
|
+
@iom = INTEGRATION_DB.identifier_output_method
|
|
7
|
+
@iim = INTEGRATION_DB.identifier_input_method
|
|
8
|
+
@defsch = INTEGRATION_DB.default_schema
|
|
9
|
+
clear_sqls
|
|
10
|
+
end
|
|
11
|
+
after do
|
|
12
|
+
INTEGRATION_DB.drop_table(:items) if INTEGRATION_DB.table_exists?(:items)
|
|
13
|
+
INTEGRATION_DB.identifier_output_method = @iom
|
|
14
|
+
INTEGRATION_DB.identifier_input_method = @iim
|
|
15
|
+
INTEGRATION_DB.default_schema = @defsch
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
specify "should handle a database with a identifier_output_method" do
|
|
19
|
+
INTEGRATION_DB.identifier_output_method = :reverse
|
|
20
|
+
INTEGRATION_DB.identifier_input_method = :reverse
|
|
21
|
+
INTEGRATION_DB.default_schema = nil if INTEGRATION_DB.default_schema
|
|
22
|
+
INTEGRATION_DB.create_table!(:items){integer :number}
|
|
23
|
+
INTEGRATION_DB.schema(:items, :reload=>true).should be_a_kind_of(Array)
|
|
24
|
+
INTEGRATION_DB.schema(:items, :reload=>true).first.first.should == :number
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
deprec_specify "should be a hash with table_names as symbols" do
|
|
28
|
+
INTEGRATION_DB.create_table!(:items){integer :number}
|
|
29
|
+
schema = INTEGRATION_DB.schema(nil, :reload=>true)
|
|
30
|
+
schema.should be_a_kind_of(Hash)
|
|
31
|
+
schema[:items].should_not == nil
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
specify "should not issue an sql query if the schema has been loaded unless :reload is true" do
|
|
35
|
+
INTEGRATION_DB.create_table!(:items){integer :number}
|
|
36
|
+
INTEGRATION_DB.schema(:items, :reload=>true)
|
|
37
|
+
clear_sqls
|
|
38
|
+
INTEGRATION_DB.schema(:items)
|
|
39
|
+
sqls_should_be
|
|
40
|
+
clear_sqls
|
|
41
|
+
INTEGRATION_DB.schema(:items, :reload=>true)
|
|
42
|
+
sqls_should_be "PRAGMA table_info('items')"
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
deprec_specify "should give the same result for a single table regardless of whether schema was called for a single table" do
|
|
46
|
+
INTEGRATION_DB.create_table!(:items){integer :number}
|
|
47
|
+
INTEGRATION_DB.schema(:items, :reload=>true).should == INTEGRATION_DB.schema(nil, :reload=>true)[:items]
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
specify "should raise an error when the table doesn't exist" do
|
|
51
|
+
proc{INTEGRATION_DB.schema(:no_table)}.should raise_error(Sequel::Error)
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
specify "should return the schema correctly" do
|
|
55
|
+
INTEGRATION_DB.create_table!(:items){integer :number}
|
|
56
|
+
schema = INTEGRATION_DB.schema(:items, :reload=>true)
|
|
57
|
+
schema.should be_a_kind_of(Array)
|
|
58
|
+
schema.length.should == 1
|
|
59
|
+
col = schema.first
|
|
60
|
+
col.should be_a_kind_of(Array)
|
|
61
|
+
col.length.should == 2
|
|
62
|
+
col.first.should == :number
|
|
63
|
+
col_info = col.last
|
|
64
|
+
col_info.should be_a_kind_of(Hash)
|
|
65
|
+
col_info[:type].should == :integer
|
|
66
|
+
clear_sqls
|
|
67
|
+
INTEGRATION_DB.schema(:items)
|
|
68
|
+
sqls_should_be
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
specify "should parse primary keys from the schema properly" do
|
|
72
|
+
INTEGRATION_DB.create_table!(:items){integer :number}
|
|
73
|
+
INTEGRATION_DB.schema(:items).collect{|k,v| k if v[:primary_key]}.compact.should == []
|
|
74
|
+
INTEGRATION_DB.create_table!(:items){primary_key :number}
|
|
75
|
+
INTEGRATION_DB.schema(:items).collect{|k,v| k if v[:primary_key]}.compact.should == [:number]
|
|
76
|
+
INTEGRATION_DB.create_table!(:items){integer :number1; integer :number2; primary_key [:number1, :number2]}
|
|
77
|
+
INTEGRATION_DB.schema(:items).collect{|k,v| k if v[:primary_key]}.compact.should == [:number1, :number2]
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
specify "should parse NULL/NOT NULL from the schema properly" do
|
|
81
|
+
INTEGRATION_DB.create_table!(:items){integer :number, :null=>true}
|
|
82
|
+
INTEGRATION_DB.schema(:items).first.last[:allow_null].should == true
|
|
83
|
+
INTEGRATION_DB.create_table!(:items){integer :number, :null=>false}
|
|
84
|
+
INTEGRATION_DB.schema(:items).first.last[:allow_null].should == false
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
specify "should parse defaults from the schema properly" do
|
|
88
|
+
INTEGRATION_DB.create_table!(:items){integer :number}
|
|
89
|
+
INTEGRATION_DB.schema(:items).first.last[:default].should == nil
|
|
90
|
+
INTEGRATION_DB.create_table!(:items){integer :number, :default=>0}
|
|
91
|
+
INTEGRATION_DB.schema(:items).first.last[:default].to_s.should == "0"
|
|
92
|
+
INTEGRATION_DB.create_table!(:items){varchar :a, :default=>"blah", :size=>4}
|
|
93
|
+
INTEGRATION_DB.schema(:items).first.last[:default].gsub(/::character varying\z/, '').gsub("'", '').should == "blah"
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
describe "Database schema modifiers" do
|
|
99
|
+
before do
|
|
100
|
+
INTEGRATION_DB.create_table!(:items){integer :number}
|
|
101
|
+
@ds = INTEGRATION_DB[:items]
|
|
102
|
+
@ds.insert([10])
|
|
103
|
+
clear_sqls
|
|
104
|
+
end
|
|
105
|
+
after do
|
|
106
|
+
INTEGRATION_DB.drop_table(:items) if INTEGRATION_DB.table_exists?(:items)
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
specify "should create tables correctly" do
|
|
110
|
+
INTEGRATION_DB.table_exists?(:items).should == true
|
|
111
|
+
INTEGRATION_DB.schema(:items, :reload=>true).map{|x| x.first}.should == [:number]
|
|
112
|
+
@ds.columns!.should == [:number]
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
specify "should add columns to tables correctly" do
|
|
116
|
+
INTEGRATION_DB.schema(:items, :reload=>true).map{|x| x.first}.should == [:number]
|
|
117
|
+
@ds.columns!.should == [:number]
|
|
118
|
+
INTEGRATION_DB.alter_table(:items){add_column :name, :text}
|
|
119
|
+
INTEGRATION_DB.schema(:items, :reload=>true).map{|x| x.first}.should == [:number, :name]
|
|
120
|
+
@ds.columns!.should == [:number, :name]
|
|
121
|
+
unless INTEGRATION_DB.url =~ /sqlite/
|
|
122
|
+
INTEGRATION_DB.alter_table(:items){add_primary_key :id}
|
|
123
|
+
INTEGRATION_DB.schema(:items, :reload=>true).map{|x| x.first}.should == [:number, :name, :id]
|
|
124
|
+
@ds.columns!.should == [:number, :name, :id]
|
|
125
|
+
INTEGRATION_DB.alter_table(:items){add_foreign_key :item_id, :items}
|
|
126
|
+
INTEGRATION_DB.schema(:items, :reload=>true).map{|x| x.first}.should == [:number, :name, :id, :item_id]
|
|
127
|
+
@ds.columns!.should == [:number, :name, :id, :item_id]
|
|
128
|
+
end
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
specify "should remove columns from tables correctly" do
|
|
132
|
+
INTEGRATION_DB.create_table!(:items) do
|
|
133
|
+
primary_key :id
|
|
134
|
+
text :name
|
|
135
|
+
integer :number
|
|
136
|
+
foreign_key :item_id, :items
|
|
137
|
+
end
|
|
138
|
+
@ds.insert(:number=>10)
|
|
139
|
+
INTEGRATION_DB.schema(:items, :reload=>true).map{|x| x.first}.should == [:id, :name, :number, :item_id]
|
|
140
|
+
@ds.columns!.should == [:id, :name, :number, :item_id]
|
|
141
|
+
INTEGRATION_DB.drop_column(:items, :number)
|
|
142
|
+
INTEGRATION_DB.schema(:items, :reload=>true).map{|x| x.first}.should == [:id, :name, :item_id]
|
|
143
|
+
@ds.columns!.should == [:id, :name, :item_id]
|
|
144
|
+
INTEGRATION_DB.drop_column(:items, :name)
|
|
145
|
+
INTEGRATION_DB.schema(:items, :reload=>true).map{|x| x.first}.should == [:id, :item_id]
|
|
146
|
+
@ds.columns!.should == [:id, :item_id]
|
|
147
|
+
INTEGRATION_DB.drop_column(:items, :item_id)
|
|
148
|
+
INTEGRATION_DB.schema(:items, :reload=>true).map{|x| x.first}.should == [:id]
|
|
149
|
+
@ds.columns!.should == [:id]
|
|
150
|
+
end
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
if INTEGRATION_DB.respond_to?(:tables)
|
|
154
|
+
describe "Database#tables" do
|
|
155
|
+
before do
|
|
156
|
+
class ::String
|
|
157
|
+
@@xxxxx = 0
|
|
158
|
+
def xxxxx
|
|
159
|
+
"xxxxx#{@@xxxxx += 1}"
|
|
160
|
+
end
|
|
161
|
+
end
|
|
162
|
+
@iom = INTEGRATION_DB.identifier_output_method
|
|
163
|
+
@iim = INTEGRATION_DB.identifier_input_method
|
|
164
|
+
clear_sqls
|
|
165
|
+
end
|
|
166
|
+
after do
|
|
167
|
+
INTEGRATION_DB.identifier_output_method = @iom
|
|
168
|
+
INTEGRATION_DB.identifier_input_method = @iim
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
specify "should return an array of symbols" do
|
|
172
|
+
ts = INTEGRATION_DB.tables
|
|
173
|
+
ts.should be_a_kind_of(Array)
|
|
174
|
+
ts.each{|t| t.should be_a_kind_of(Symbol)}
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
specify "should respect the database's identifier_output_method" do
|
|
178
|
+
INTEGRATION_DB.identifier_output_method = :xxxxx
|
|
179
|
+
INTEGRATION_DB.identifier_input_method = :xxxxx
|
|
180
|
+
INTEGRATION_DB.tables.each{|t| t.to_s.should =~ /\Ax{5}\d+\z/}
|
|
181
|
+
end
|
|
182
|
+
end
|
|
183
|
+
end
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
require 'rubygems'
|
|
2
|
+
unless Object.const_defined?('Sequel')
|
|
3
|
+
$:.unshift(File.join(File.dirname(__FILE__), "../../lib/"))
|
|
4
|
+
require 'sequel'
|
|
5
|
+
end
|
|
6
|
+
begin
|
|
7
|
+
require File.join(File.dirname(__FILE__), '../spec_config.rb')
|
|
8
|
+
rescue LoadError
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
Sequel.virtual_row_instance_eval = true
|
|
12
|
+
Sequel::Model.use_transactions = false
|
|
13
|
+
|
|
14
|
+
module Spec::Example::ExampleGroupMethods
|
|
15
|
+
def deprec_specify(*args, &block)
|
|
16
|
+
specify(*args) do
|
|
17
|
+
output = Sequel::Deprecation.output
|
|
18
|
+
Sequel::Deprecation.output = nil
|
|
19
|
+
begin
|
|
20
|
+
instance_eval(&block)
|
|
21
|
+
ensure
|
|
22
|
+
Sequel::Deprecation.output = output
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
$sqls = []
|
|
29
|
+
def clear_sqls
|
|
30
|
+
$sqls.clear
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
class Spec::Example::ExampleGroup
|
|
34
|
+
def start_logging
|
|
35
|
+
require 'logger'
|
|
36
|
+
INTEGRATION_DB.loggers << Logger.new(STDOUT)
|
|
37
|
+
end
|
|
38
|
+
def stop_logging
|
|
39
|
+
INTEGRATION_DB.loggers.clear
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
if defined?(INTEGRATION_DB) || defined?(INTEGRATION_URL) || ENV['SEQUEL_INTEGRATION_URL']
|
|
44
|
+
unless defined?(INTEGRATION_DB)
|
|
45
|
+
url = defined?(INTEGRATION_URL) ? INTEGRATION_URL : ENV['SEQUEL_INTEGRATION_URL']
|
|
46
|
+
INTEGRATION_DB = Sequel.connect(url)
|
|
47
|
+
#INTEGRATION_DB.instance_variable_set(:@server_version, 80100)
|
|
48
|
+
end
|
|
49
|
+
class Spec::Example::ExampleGroup
|
|
50
|
+
def sqls_should_be(*args)
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
else
|
|
54
|
+
sql_logger = Object.new
|
|
55
|
+
def sql_logger.info(str)
|
|
56
|
+
$sqls << str
|
|
57
|
+
end
|
|
58
|
+
INTEGRATION_DB = Sequel.sqlite('', :loggers=>[sql_logger], :quote_identifiers=>false)
|
|
59
|
+
class Spec::Example::ExampleGroup
|
|
60
|
+
def sqls_should_be(*sqls)
|
|
61
|
+
sqls.zip($sqls).each do |should_be, is|
|
|
62
|
+
case should_be
|
|
63
|
+
when String
|
|
64
|
+
is.should == should_be
|
|
65
|
+
when Regexp
|
|
66
|
+
is.should =~ should_be
|
|
67
|
+
else
|
|
68
|
+
raise ArgumentError, "need String or RegExp"
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
$sqls.length.should == sqls.length
|
|
72
|
+
clear_sqls
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'spec_helper.rb')
|
|
2
|
+
|
|
3
|
+
describe "Supported types" do
|
|
4
|
+
def create_items_table_with_column(name, type, opts={})
|
|
5
|
+
INTEGRATION_DB.create_table!(:items){column name, type, opts}
|
|
6
|
+
INTEGRATION_DB[:items]
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
specify "should support casting correctly" do
|
|
10
|
+
ds = create_items_table_with_column(:number, Integer)
|
|
11
|
+
ds.insert(:number => 1)
|
|
12
|
+
ds.select(:number.cast_string.as(:n)).map(:n).should == %w'1'
|
|
13
|
+
ds = create_items_table_with_column(:name, String)
|
|
14
|
+
ds.insert(:name=> '1')
|
|
15
|
+
ds.select(:name.cast_numeric.as(:n)).map(:n).should == [1]
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
specify "should support NULL correctly" do
|
|
19
|
+
ds = create_items_table_with_column(:number, Integer)
|
|
20
|
+
ds.insert(:number => nil)
|
|
21
|
+
ds.all.should == [{:number=>nil}]
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
specify "should support generic integer type" do
|
|
25
|
+
ds = create_items_table_with_column(:number, Integer)
|
|
26
|
+
ds.insert(:number => 2)
|
|
27
|
+
ds.all.should == [{:number=>2}]
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
specify "should support generic fixnum type" do
|
|
31
|
+
ds = create_items_table_with_column(:number, Fixnum)
|
|
32
|
+
ds.insert(:number => 2)
|
|
33
|
+
ds.all.should == [{:number=>2}]
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
specify "should support generic bignum type" do
|
|
37
|
+
ds = create_items_table_with_column(:number, Bignum)
|
|
38
|
+
ds.insert(:number => 2**34)
|
|
39
|
+
ds.all.should == [{:number=>2**34}]
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
specify "should support generic float type" do
|
|
43
|
+
ds = create_items_table_with_column(:number, Float)
|
|
44
|
+
ds.insert(:number => 2.1)
|
|
45
|
+
ds.all.should == [{:number=>2.1}]
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
specify "should support generic numeric type" do
|
|
49
|
+
ds = create_items_table_with_column(:number, Numeric, :size=>[15, 10])
|
|
50
|
+
ds.insert(:number => BigDecimal.new('2.123456789'))
|
|
51
|
+
ds.all.should == [{:number=>BigDecimal.new('2.123456789')}]
|
|
52
|
+
ds = create_items_table_with_column(:number, BigDecimal, :size=>[15, 10])
|
|
53
|
+
ds.insert(:number => BigDecimal.new('2.123456789'))
|
|
54
|
+
ds.all.should == [{:number=>BigDecimal.new('2.123456789')}]
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
specify "should support generic string type" do
|
|
58
|
+
ds = create_items_table_with_column(:name, String)
|
|
59
|
+
ds.insert(:name => 'Test User')
|
|
60
|
+
ds.all.should == [{:name=>'Test User'}]
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
specify "should support generic date type" do
|
|
64
|
+
ds = create_items_table_with_column(:dat, Date)
|
|
65
|
+
d = Date.today
|
|
66
|
+
ds.insert(:dat => d)
|
|
67
|
+
ds.first[:dat].should == d
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
specify "should support generic datetime type" do
|
|
71
|
+
ds = create_items_table_with_column(:tim, DateTime)
|
|
72
|
+
t = DateTime.now
|
|
73
|
+
ds.insert(:tim => t)
|
|
74
|
+
ds.first[:tim].strftime('%Y%m%d%H%M%S').should == t.strftime('%Y%m%d%H%M%S')
|
|
75
|
+
ds = create_items_table_with_column(:tim, Time)
|
|
76
|
+
t = Time.now
|
|
77
|
+
ds.insert(:tim => t)
|
|
78
|
+
ds.first[:tim].strftime('%Y%m%d%H%M%S').should == t.strftime('%Y%m%d%H%M%S')
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
specify "should support generic file type" do
|
|
82
|
+
ds = create_items_table_with_column(:name, File)
|
|
83
|
+
ds.insert(:name => ("a\0"*300).to_sequel_blob)
|
|
84
|
+
ds.all.should == [{:name=>("a\0"*300).to_sequel_blob}]
|
|
85
|
+
ds.first[:name].should be_a_kind_of(::Sequel::SQL::Blob)
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
specify "should support generic boolean type" do
|
|
89
|
+
ds = create_items_table_with_column(:number, TrueClass)
|
|
90
|
+
ds.insert(:number => true)
|
|
91
|
+
ds.all.should == [{:number=>true}]
|
|
92
|
+
ds = create_items_table_with_column(:number, FalseClass)
|
|
93
|
+
ds.insert(:number => true)
|
|
94
|
+
ds.all.should == [{:number=>true}]
|
|
95
|
+
end
|
|
96
|
+
end
|