colincasey-sequel 2.10.0 → 2.10.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +7 -1
- data/doc/advanced_associations.rdoc +614 -0
- data/doc/cheat_sheet.rdoc +223 -0
- data/doc/dataset_filtering.rdoc +158 -0
- data/doc/prepared_statements.rdoc +104 -0
- data/doc/release_notes/1.0.txt +38 -0
- data/doc/release_notes/1.1.txt +143 -0
- data/doc/release_notes/1.3.txt +101 -0
- data/doc/release_notes/1.4.0.txt +53 -0
- data/doc/release_notes/1.5.0.txt +155 -0
- data/doc/release_notes/2.0.0.txt +298 -0
- data/doc/release_notes/2.1.0.txt +271 -0
- data/doc/release_notes/2.10.0.txt +328 -0
- data/doc/release_notes/2.2.0.txt +253 -0
- data/doc/release_notes/2.3.0.txt +88 -0
- data/doc/release_notes/2.4.0.txt +106 -0
- data/doc/release_notes/2.5.0.txt +137 -0
- data/doc/release_notes/2.6.0.txt +157 -0
- data/doc/release_notes/2.7.0.txt +166 -0
- data/doc/release_notes/2.8.0.txt +171 -0
- data/doc/release_notes/2.9.0.txt +97 -0
- data/doc/schema.rdoc +29 -0
- data/doc/sharding.rdoc +113 -0
- data/lib/sequel.rb +1 -0
- data/lib/sequel_core/adapters/ado.rb +89 -0
- data/lib/sequel_core/adapters/db2.rb +143 -0
- data/lib/sequel_core/adapters/dbi.rb +112 -0
- data/lib/sequel_core/adapters/do/mysql.rb +38 -0
- data/lib/sequel_core/adapters/do/postgres.rb +92 -0
- data/lib/sequel_core/adapters/do/sqlite.rb +31 -0
- data/lib/sequel_core/adapters/do.rb +205 -0
- data/lib/sequel_core/adapters/firebird.rb +298 -0
- data/lib/sequel_core/adapters/informix.rb +85 -0
- data/lib/sequel_core/adapters/jdbc/h2.rb +69 -0
- data/lib/sequel_core/adapters/jdbc/mysql.rb +66 -0
- data/lib/sequel_core/adapters/jdbc/oracle.rb +23 -0
- data/lib/sequel_core/adapters/jdbc/postgresql.rb +113 -0
- data/lib/sequel_core/adapters/jdbc/sqlite.rb +43 -0
- data/lib/sequel_core/adapters/jdbc.rb +491 -0
- data/lib/sequel_core/adapters/mysql.rb +369 -0
- data/lib/sequel_core/adapters/odbc.rb +174 -0
- data/lib/sequel_core/adapters/openbase.rb +68 -0
- data/lib/sequel_core/adapters/oracle.rb +107 -0
- data/lib/sequel_core/adapters/postgres.rb +456 -0
- data/lib/sequel_core/adapters/shared/ms_access.rb +110 -0
- data/lib/sequel_core/adapters/shared/mssql.rb +102 -0
- data/lib/sequel_core/adapters/shared/mysql.rb +325 -0
- data/lib/sequel_core/adapters/shared/oracle.rb +61 -0
- data/lib/sequel_core/adapters/shared/postgres.rb +715 -0
- data/lib/sequel_core/adapters/shared/progress.rb +31 -0
- data/lib/sequel_core/adapters/shared/sqlite.rb +265 -0
- data/lib/sequel_core/adapters/sqlite.rb +248 -0
- data/lib/sequel_core/connection_pool.rb +258 -0
- data/lib/sequel_core/core_ext.rb +217 -0
- data/lib/sequel_core/core_sql.rb +202 -0
- data/lib/sequel_core/database/schema.rb +164 -0
- data/lib/sequel_core/database.rb +691 -0
- data/lib/sequel_core/dataset/callback.rb +13 -0
- data/lib/sequel_core/dataset/convenience.rb +237 -0
- data/lib/sequel_core/dataset/pagination.rb +96 -0
- data/lib/sequel_core/dataset/prepared_statements.rb +220 -0
- data/lib/sequel_core/dataset/query.rb +41 -0
- data/lib/sequel_core/dataset/schema.rb +15 -0
- data/lib/sequel_core/dataset/sql.rb +1010 -0
- data/lib/sequel_core/dataset/stored_procedures.rb +75 -0
- data/lib/sequel_core/dataset/unsupported.rb +43 -0
- data/lib/sequel_core/dataset.rb +511 -0
- data/lib/sequel_core/deprecated.rb +26 -0
- data/lib/sequel_core/exceptions.rb +44 -0
- data/lib/sequel_core/migration.rb +212 -0
- data/lib/sequel_core/object_graph.rb +230 -0
- data/lib/sequel_core/pretty_table.rb +71 -0
- data/lib/sequel_core/schema/generator.rb +320 -0
- data/lib/sequel_core/schema/sql.rb +325 -0
- data/lib/sequel_core/schema.rb +2 -0
- data/lib/sequel_core/sql.rb +887 -0
- data/lib/sequel_core/version.rb +11 -0
- data/lib/sequel_core.rb +172 -0
- data/lib/sequel_model/association_reflection.rb +267 -0
- data/lib/sequel_model/associations.rb +499 -0
- data/lib/sequel_model/base.rb +523 -0
- data/lib/sequel_model/caching.rb +82 -0
- data/lib/sequel_model/dataset_methods.rb +26 -0
- data/lib/sequel_model/eager_loading.rb +370 -0
- data/lib/sequel_model/exceptions.rb +7 -0
- data/lib/sequel_model/hooks.rb +101 -0
- data/lib/sequel_model/inflector.rb +281 -0
- data/lib/sequel_model/plugins.rb +62 -0
- data/lib/sequel_model/record.rb +568 -0
- data/lib/sequel_model/schema.rb +49 -0
- data/lib/sequel_model/validations.rb +429 -0
- data/lib/sequel_model.rb +91 -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 +881 -0
- data/spec/adapters/oracle_spec.rb +244 -0
- data/spec/adapters/postgres_spec.rb +687 -0
- data/spec/adapters/spec_helper.rb +10 -0
- data/spec/adapters/sqlite_spec.rb +555 -0
- data/spec/integration/dataset_test.rb +134 -0
- data/spec/integration/eager_loader_test.rb +696 -0
- data/spec/integration/prepared_statement_test.rb +130 -0
- data/spec/integration/schema_test.rb +180 -0
- data/spec/integration/spec_helper.rb +58 -0
- data/spec/integration/type_test.rb +96 -0
- data/spec/rcov.opts +6 -0
- data/spec/sequel_core/connection_pool_spec.rb +526 -0
- data/spec/sequel_core/core_ext_spec.rb +156 -0
- data/spec/sequel_core/core_sql_spec.rb +522 -0
- data/spec/sequel_core/database_spec.rb +1188 -0
- data/spec/sequel_core/dataset_spec.rb +3481 -0
- data/spec/sequel_core/expression_filters_spec.rb +363 -0
- data/spec/sequel_core/migration_spec.rb +261 -0
- data/spec/sequel_core/object_graph_spec.rb +272 -0
- data/spec/sequel_core/pretty_table_spec.rb +58 -0
- data/spec/sequel_core/schema_generator_spec.rb +167 -0
- data/spec/sequel_core/schema_spec.rb +780 -0
- data/spec/sequel_core/spec_helper.rb +55 -0
- data/spec/sequel_core/version_spec.rb +7 -0
- data/spec/sequel_model/association_reflection_spec.rb +93 -0
- data/spec/sequel_model/associations_spec.rb +1767 -0
- data/spec/sequel_model/base_spec.rb +419 -0
- data/spec/sequel_model/caching_spec.rb +215 -0
- data/spec/sequel_model/dataset_methods_spec.rb +78 -0
- data/spec/sequel_model/eager_loading_spec.rb +1165 -0
- data/spec/sequel_model/hooks_spec.rb +485 -0
- data/spec/sequel_model/inflector_spec.rb +119 -0
- data/spec/sequel_model/model_spec.rb +588 -0
- data/spec/sequel_model/plugins_spec.rb +80 -0
- data/spec/sequel_model/record_spec.rb +1184 -0
- data/spec/sequel_model/schema_spec.rb +90 -0
- data/spec/sequel_model/spec_helper.rb +78 -0
- data/spec/sequel_model/validations_spec.rb +1067 -0
- data/spec/spec.opts +0 -0
- data/spec/spec_config.rb.example +10 -0
- metadata +177 -3
@@ -0,0 +1,75 @@
|
|
1
|
+
module Sequel
|
2
|
+
class Dataset
|
3
|
+
module StoredProcedureMethods
|
4
|
+
SQL_QUERY_TYPE = Hash.new{|h,k| h[k] = k}
|
5
|
+
SQL_QUERY_TYPE[:first] = SQL_QUERY_TYPE[:all] = :select
|
6
|
+
|
7
|
+
# The name of the stored procedure to call
|
8
|
+
attr_accessor :sproc_name
|
9
|
+
|
10
|
+
# Call the prepared statement
|
11
|
+
def call(*args, &block)
|
12
|
+
@sproc_args = args
|
13
|
+
case @sproc_type
|
14
|
+
when :select, :all
|
15
|
+
all(&block)
|
16
|
+
when :first
|
17
|
+
first
|
18
|
+
when :insert
|
19
|
+
insert
|
20
|
+
when :update
|
21
|
+
update
|
22
|
+
when :delete
|
23
|
+
delete
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
# Programmer friendly string showing this is a stored procedure,
|
28
|
+
# showing the name of the procedure.
|
29
|
+
def inspect
|
30
|
+
"<#{self.class.name}/StoredProcedure name=#{@sproc_name}>"
|
31
|
+
end
|
32
|
+
|
33
|
+
# Set the type of the sproc and override the corresponding _sql
|
34
|
+
# method to return the empty string (since the result will be
|
35
|
+
# ignored anyway).
|
36
|
+
def sproc_type=(type)
|
37
|
+
@sproc_type = type
|
38
|
+
meta_def("#{sql_query_type}_sql"){|*a| ''}
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
# The type of query (:select, :insert, :delete, :update).
|
44
|
+
def sql_query_type
|
45
|
+
SQL_QUERY_TYPE[@sproc_type]
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
module StoredProcedures
|
50
|
+
# For the given type (:select, :first, :insert, :update, or :delete),
|
51
|
+
# run the database stored procedure with the given name with the given
|
52
|
+
# arguments.
|
53
|
+
def call_sproc(type, name, *args)
|
54
|
+
prepare_sproc(type, name).call(*args)
|
55
|
+
end
|
56
|
+
|
57
|
+
# Transform this dataset into a stored procedure that you can call
|
58
|
+
# multiple times with new arguments.
|
59
|
+
def prepare_sproc(type, name)
|
60
|
+
sp = clone
|
61
|
+
prepare_extend_sproc(sp)
|
62
|
+
sp.sproc_type = type
|
63
|
+
sp.sproc_name = name
|
64
|
+
sp
|
65
|
+
end
|
66
|
+
|
67
|
+
private
|
68
|
+
|
69
|
+
# Extend the dataset with the stored procedure methods.
|
70
|
+
def prepare_extend_sproc(ds)
|
71
|
+
ds.extend(StoredProcedureMethods)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
class Sequel::Dataset
|
2
|
+
# This module should be included in the dataset class for all databases that
|
3
|
+
# don't support INTERSECT or EXCEPT.
|
4
|
+
module UnsupportedIntersectExcept
|
5
|
+
# Raise an Error if EXCEPT is used
|
6
|
+
def except(ds, all=false)
|
7
|
+
raise(Sequel::Error, "EXCEPT not supported")
|
8
|
+
end
|
9
|
+
|
10
|
+
# Raise an Error if INTERSECT is used
|
11
|
+
def intersect(ds, all=false)
|
12
|
+
raise(Sequel::Error, "INTERSECT not supported")
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
# Since EXCEPT and INTERSECT are not supported, and order shouldn't matter
|
18
|
+
# when UNION is used, don't worry about parantheses. This may potentially
|
19
|
+
# give incorrect results if UNION ALL is used.
|
20
|
+
def select_compounds_sql(sql, opts)
|
21
|
+
return unless opts[:compounds]
|
22
|
+
opts[:compounds].each do |type, dataset, all|
|
23
|
+
sql << " #{type.to_s.upcase}#{' ALL' if all} #{subselect_sql(dataset)}"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
# This module should be included in the dataset class for all databases that
|
29
|
+
# don't support INTERSECT ALL or EXCEPT ALL.
|
30
|
+
module UnsupportedIntersectExceptAll
|
31
|
+
# Raise an Error if EXCEPT is used
|
32
|
+
def except(ds, all=false)
|
33
|
+
raise(Sequel::Error, "EXCEPT ALL not supported") if all
|
34
|
+
super(ds)
|
35
|
+
end
|
36
|
+
|
37
|
+
# Raise an Error if INTERSECT is used
|
38
|
+
def intersect(ds, all=false)
|
39
|
+
raise(Sequel::Error, "INTERSECT ALL not supported") if all
|
40
|
+
super(ds)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,511 @@
|
|
1
|
+
%w'callback convenience pagination prepared_statements query schema sql unsupported'.each do |f|
|
2
|
+
require "sequel_core/dataset/#{f}"
|
3
|
+
end
|
4
|
+
|
5
|
+
module Sequel
|
6
|
+
# A Dataset represents a view of a the data in a database, constrained by
|
7
|
+
# specific parameters such as filtering conditions, order, etc. Datasets
|
8
|
+
# can be used to create, retrieve, update and delete records.
|
9
|
+
#
|
10
|
+
# Query results are always retrieved on demand, so a dataset can be kept
|
11
|
+
# around and reused indefinitely:
|
12
|
+
#
|
13
|
+
# my_posts = DB[:posts].filter(:author => 'david') # no records are retrieved
|
14
|
+
# p my_posts.all # records are now retrieved
|
15
|
+
# ...
|
16
|
+
# p my_posts.all # records are retrieved again
|
17
|
+
#
|
18
|
+
# In order to provide this functionality, dataset methods such as where,
|
19
|
+
# select, order, etc. return modified copies of the dataset, so you can
|
20
|
+
# use different datasets to access data:
|
21
|
+
#
|
22
|
+
# posts = DB[:posts]
|
23
|
+
# davids_posts = posts.filter(:author => 'david')
|
24
|
+
# old_posts = posts.filter('stamp < ?', Date.today - 7)
|
25
|
+
#
|
26
|
+
# Datasets are Enumerable objects, so they can be manipulated using any
|
27
|
+
# of the Enumerable methods, such as map, inject, etc.
|
28
|
+
#
|
29
|
+
# === Methods added via metaprogramming
|
30
|
+
#
|
31
|
+
# Some methods are added via metaprogramming:
|
32
|
+
#
|
33
|
+
# * ! methods - These methods are the same as their non-! counterparts,
|
34
|
+
# but they modify the receiver instead of returning a modified copy
|
35
|
+
# of the dataset.
|
36
|
+
# * inner_join, full_outer_join, right_outer_join, left_outer_join -
|
37
|
+
# This methods are shortcuts to join_table with the join type
|
38
|
+
# already specified.
|
39
|
+
class Dataset
|
40
|
+
include Enumerable
|
41
|
+
|
42
|
+
# The dataset options that require the removal of cached columns
|
43
|
+
# if changed.
|
44
|
+
COLUMN_CHANGE_OPTS = [:select, :sql, :from, :join].freeze
|
45
|
+
|
46
|
+
# Array of all subclasses of Dataset
|
47
|
+
DATASET_CLASSES = []
|
48
|
+
|
49
|
+
# All methods that should have a ! method added that modifies
|
50
|
+
# the receiver.
|
51
|
+
MUTATION_METHODS = %w'add_graph_aliases and distinct exclude exists
|
52
|
+
filter from from_self full_outer_join graph
|
53
|
+
group group_and_count group_by having inner_join intersect invert join
|
54
|
+
left_outer_join limit naked or order order_by order_more paginate query reject
|
55
|
+
reverse reverse_order right_outer_join select select_all select_more
|
56
|
+
set_defaults set_graph_aliases set_model set_overrides sort sort_by
|
57
|
+
unfiltered union unordered where'.collect{|x| x.to_sym}
|
58
|
+
|
59
|
+
NOTIMPL_MSG = "This method must be overridden in Sequel adapters".freeze
|
60
|
+
STOCK_TRANSFORMS = {
|
61
|
+
:marshal => [
|
62
|
+
# for backwards-compatibility we support also non-base64-encoded values.
|
63
|
+
proc {|v| Marshal.load(v.unpack('m')[0]) rescue Marshal.load(v)},
|
64
|
+
proc {|v| [Marshal.dump(v)].pack('m')}
|
65
|
+
],
|
66
|
+
:yaml => [
|
67
|
+
proc {|v| YAML.load v if v},
|
68
|
+
proc {|v| v.to_yaml}
|
69
|
+
]
|
70
|
+
}
|
71
|
+
|
72
|
+
# The database that corresponds to this dataset
|
73
|
+
attr_accessor :db
|
74
|
+
|
75
|
+
# Set the method to call on identifiers going into the database for this dataset
|
76
|
+
attr_accessor :identifier_input_method
|
77
|
+
|
78
|
+
# Set the method to call on identifiers coming the database for this dataset
|
79
|
+
attr_accessor :identifier_output_method
|
80
|
+
|
81
|
+
# The hash of options for this dataset, keys are symbols.
|
82
|
+
attr_accessor :opts
|
83
|
+
|
84
|
+
# Whether to quote identifiers for this dataset
|
85
|
+
attr_writer :quote_identifiers
|
86
|
+
|
87
|
+
# The row_proc for this database, should be a Proc that takes
|
88
|
+
# a single hash argument and returns the object you want to
|
89
|
+
# fetch_rows to return.
|
90
|
+
attr_accessor :row_proc
|
91
|
+
|
92
|
+
# Constructs a new instance of a dataset with an associated database and
|
93
|
+
# options. Datasets are usually constructed by invoking Database methods:
|
94
|
+
#
|
95
|
+
# DB[:posts]
|
96
|
+
#
|
97
|
+
# Or:
|
98
|
+
#
|
99
|
+
# DB.dataset # the returned dataset is blank
|
100
|
+
#
|
101
|
+
# Sequel::Dataset is an abstract class that is not useful by itself. Each
|
102
|
+
# database adaptor should provide a descendant class of Sequel::Dataset.
|
103
|
+
def initialize(db, opts = nil)
|
104
|
+
@db = db
|
105
|
+
@quote_identifiers = db.quote_identifiers? if db.respond_to?(:quote_identifiers?)
|
106
|
+
@identifier_input_method = db.identifier_input_method if db.respond_to?(:identifier_input_method)
|
107
|
+
@identifier_output_method = db.identifier_output_method if db.respond_to?(:identifier_output_method)
|
108
|
+
@opts = opts || {}
|
109
|
+
@row_proc = nil
|
110
|
+
@transform = nil
|
111
|
+
end
|
112
|
+
|
113
|
+
### Class Methods ###
|
114
|
+
|
115
|
+
# The array of dataset subclasses.
|
116
|
+
def self.dataset_classes
|
117
|
+
DATASET_CLASSES
|
118
|
+
end
|
119
|
+
|
120
|
+
# Setup mutation (e.g. filter!) methods. These operate the same as the
|
121
|
+
# non-! methods, but replace the options of the current dataset with the
|
122
|
+
# options of the resulting dataset.
|
123
|
+
def self.def_mutation_method(*meths)
|
124
|
+
meths.each do |meth|
|
125
|
+
class_eval("def #{meth}!(*args, &block); mutation_method(:#{meth}, *args, &block) end")
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
# Add the subclass to the array of subclasses.
|
130
|
+
def self.inherited(c)
|
131
|
+
DATASET_CLASSES << c
|
132
|
+
end
|
133
|
+
|
134
|
+
### Instance Methods ###
|
135
|
+
|
136
|
+
# Alias for insert, but not aliased directly so subclasses
|
137
|
+
# don't have to override both methods.
|
138
|
+
def <<(*args)
|
139
|
+
insert(*args)
|
140
|
+
end
|
141
|
+
|
142
|
+
# Return the dataset as a column with the given alias, so it can be used in the
|
143
|
+
# SELECT clause. This dataset should result in a single row and a single column.
|
144
|
+
def as(aliaz)
|
145
|
+
::Sequel::SQL::AliasedExpression.new(self, aliaz)
|
146
|
+
end
|
147
|
+
|
148
|
+
# Returns an array with all records in the dataset. If a block is given,
|
149
|
+
# the array is iterated over after all items have been loaded.
|
150
|
+
def all(opts = nil, &block)
|
151
|
+
a = []
|
152
|
+
each(opts) {|r| a << r}
|
153
|
+
post_load(a)
|
154
|
+
a.each(&block) if block
|
155
|
+
a
|
156
|
+
end
|
157
|
+
|
158
|
+
# Returns a new clone of the dataset with with the given options merged.
|
159
|
+
# If the options changed include options in COLUMN_CHANGE_OPTS, the cached
|
160
|
+
# columns are deleted.
|
161
|
+
def clone(opts = {})
|
162
|
+
c = super()
|
163
|
+
c.opts = @opts.merge(opts)
|
164
|
+
c.instance_variable_set(:@columns, nil) if opts.keys.any?{|o| COLUMN_CHANGE_OPTS.include?(o)}
|
165
|
+
c
|
166
|
+
end
|
167
|
+
|
168
|
+
# Returns the columns in the result set in their true order.
|
169
|
+
# If the columns are currently cached, returns the cached value. Otherwise,
|
170
|
+
# a SELECT query is performed to get a single row. Adapters are expected
|
171
|
+
# to fill the columns cache with the column information when a query is performed.
|
172
|
+
# If the dataset does not have any rows, this will be an empty array.
|
173
|
+
# If you are looking for all columns for a single table, see Schema::SQL#schema.
|
174
|
+
def columns
|
175
|
+
return @columns if @columns
|
176
|
+
ds = unfiltered.unordered.clone(:distinct => nil)
|
177
|
+
ds.single_record
|
178
|
+
@columns = ds.instance_variable_get(:@columns)
|
179
|
+
@columns || []
|
180
|
+
end
|
181
|
+
|
182
|
+
# Remove the cached list of columns and do a SELECT query to find
|
183
|
+
# the columns.
|
184
|
+
def columns!
|
185
|
+
@columns = nil
|
186
|
+
columns
|
187
|
+
end
|
188
|
+
|
189
|
+
# Add a mutation method to this dataset instance.
|
190
|
+
def def_mutation_method(*meths)
|
191
|
+
meths.each do |meth|
|
192
|
+
instance_eval("def #{meth}!(*args, &block); mutation_method(:#{meth}, *args, &block) end")
|
193
|
+
end
|
194
|
+
end
|
195
|
+
|
196
|
+
# Deletes the records in the dataset. The returned value is generally the
|
197
|
+
# number of records deleted, but that is adapter dependent.
|
198
|
+
def delete(*args)
|
199
|
+
execute_dui(delete_sql(*args))
|
200
|
+
end
|
201
|
+
|
202
|
+
# Iterates over the records in the dataset and returns set. If opts
|
203
|
+
# have been passed that modify the columns, reset the column information.
|
204
|
+
def each(opts = nil, &block)
|
205
|
+
if opts && opts.keys.any?{|o| COLUMN_CHANGE_OPTS.include?(o)}
|
206
|
+
prev_columns = @columns
|
207
|
+
begin
|
208
|
+
_each(opts, &block)
|
209
|
+
ensure
|
210
|
+
@columns = prev_columns
|
211
|
+
end
|
212
|
+
else
|
213
|
+
_each(opts, &block)
|
214
|
+
end
|
215
|
+
self
|
216
|
+
end
|
217
|
+
|
218
|
+
# Executes a select query and fetches records, passing each record to the
|
219
|
+
# supplied block. The yielded records are generally hashes with symbol keys,
|
220
|
+
# but that is adapter dependent.
|
221
|
+
def fetch_rows(sql, &block)
|
222
|
+
raise NotImplementedError, NOTIMPL_MSG
|
223
|
+
end
|
224
|
+
|
225
|
+
# Inserts values into the associated table. The returned value is generally
|
226
|
+
# the value of the primary key for the inserted row, but that is adapter dependent.
|
227
|
+
def insert(*values)
|
228
|
+
execute_insert(insert_sql(*values))
|
229
|
+
end
|
230
|
+
|
231
|
+
# Returns a string representation of the dataset including the class name
|
232
|
+
# and the corresponding SQL select statement.
|
233
|
+
def inspect
|
234
|
+
"#<#{self.class}: #{sql.inspect}>"
|
235
|
+
end
|
236
|
+
|
237
|
+
# Returns the the model classes associated with the dataset as a hash.
|
238
|
+
# If the dataset is associated with a single model class, a key of nil
|
239
|
+
# is used. For datasets with polymorphic models, the keys are
|
240
|
+
# values of the polymorphic column and the values are the corresponding
|
241
|
+
# model classes to which they map.
|
242
|
+
def model_classes
|
243
|
+
@opts[:models]
|
244
|
+
end
|
245
|
+
|
246
|
+
# Returns a naked dataset clone - i.e. a dataset that returns records as
|
247
|
+
# hashes rather than model objects.
|
248
|
+
def naked
|
249
|
+
clone.set_model(nil)
|
250
|
+
end
|
251
|
+
|
252
|
+
# Returns the column name for the polymorphic key.
|
253
|
+
def polymorphic_key
|
254
|
+
@opts[:polymorphic_key]
|
255
|
+
end
|
256
|
+
|
257
|
+
# Whether this dataset quotes identifiers.
|
258
|
+
def quote_identifiers?
|
259
|
+
@quote_identifiers
|
260
|
+
end
|
261
|
+
|
262
|
+
# Set the server for this dataset to use. Used to pick a specific database
|
263
|
+
# shard to run a query against, or to override the default SELECT uses
|
264
|
+
# :read_only database and all other queries use the :default database.
|
265
|
+
def server(servr)
|
266
|
+
clone(:server=>servr)
|
267
|
+
end
|
268
|
+
|
269
|
+
# Alias for set, but not aliased directly so subclasses
|
270
|
+
# don't have to override both methods.
|
271
|
+
def set(*args)
|
272
|
+
update(*args)
|
273
|
+
end
|
274
|
+
|
275
|
+
# Set the default values for insert and update statements. The values passed
|
276
|
+
# to insert or update are merged into this hash.
|
277
|
+
def set_defaults(hash)
|
278
|
+
clone(:defaults=>(@opts[:defaults]||{}).merge(hash))
|
279
|
+
end
|
280
|
+
|
281
|
+
# Associates or disassociates the dataset with a model(s). If
|
282
|
+
# nil is specified, the dataset is turned into a naked dataset and returns
|
283
|
+
# records as hashes. If a model class specified, the dataset is modified
|
284
|
+
# to return records as instances of the model class, e.g:
|
285
|
+
#
|
286
|
+
# class MyModel
|
287
|
+
# def initialize(values)
|
288
|
+
# @values = values
|
289
|
+
# ...
|
290
|
+
# end
|
291
|
+
# end
|
292
|
+
#
|
293
|
+
# dataset.set_model(MyModel)
|
294
|
+
#
|
295
|
+
# You can also provide additional arguments to be passed to the model's
|
296
|
+
# initialize method:
|
297
|
+
#
|
298
|
+
# class MyModel
|
299
|
+
# def initialize(values, options)
|
300
|
+
# @values = values
|
301
|
+
# ...
|
302
|
+
# end
|
303
|
+
# end
|
304
|
+
#
|
305
|
+
# dataset.set_model(MyModel, :allow_delete => false)
|
306
|
+
#
|
307
|
+
# The dataset can be made polymorphic by specifying a column name as the
|
308
|
+
# polymorphic key and a hash mapping column values to model classes.
|
309
|
+
#
|
310
|
+
# dataset.set_model(:kind, {1 => Person, 2 => Business})
|
311
|
+
#
|
312
|
+
# You can also set a default model class to fall back on by specifying a
|
313
|
+
# class corresponding to nil:
|
314
|
+
#
|
315
|
+
# dataset.set_model(:kind, {nil => DefaultClass, 1 => Person, 2 => Business})
|
316
|
+
#
|
317
|
+
# To make sure that there is always a default model class, the hash provided
|
318
|
+
# should have a default value. To make the dataset map string values to
|
319
|
+
# model classes, and keep a good default, try:
|
320
|
+
#
|
321
|
+
# dataset.set_model(:kind, Hash.new{|h,k| h[k] = (k.constantize rescue DefaultClass)})
|
322
|
+
def set_model(key, *args)
|
323
|
+
# This code is more verbose then necessary for performance reasons
|
324
|
+
case key
|
325
|
+
when nil # set_model(nil) => no argument provided, so the dataset is denuded
|
326
|
+
@opts.merge!(:naked => true, :models => nil, :polymorphic_key => nil)
|
327
|
+
self.row_proc = nil
|
328
|
+
when Class
|
329
|
+
# isomorphic model
|
330
|
+
@opts.merge!(:naked => nil, :models => {nil => key}, :polymorphic_key => nil)
|
331
|
+
if key.respond_to?(:load)
|
332
|
+
# the class has a values setter method, so we use it
|
333
|
+
self.row_proc = proc{|h| key.load(h, *args)}
|
334
|
+
else
|
335
|
+
# otherwise we just pass the hash to the constructor
|
336
|
+
self.row_proc = proc{|h| key.new(h, *args)}
|
337
|
+
end
|
338
|
+
when Symbol
|
339
|
+
# polymorphic model
|
340
|
+
hash = args.shift || raise(ArgumentError, "No class hash supplied for polymorphic model")
|
341
|
+
@opts.merge!(:naked => true, :models => hash, :polymorphic_key => key)
|
342
|
+
if (hash.empty? ? (hash[nil] rescue nil) : hash.values.first).respond_to?(:load)
|
343
|
+
# the class has a values setter method, so we use it
|
344
|
+
self.row_proc = proc do |h|
|
345
|
+
c = hash[h[key]] || hash[nil] || \
|
346
|
+
raise(Error, "No matching model class for record (#{polymorphic_key} => #{h[polymorphic_key].inspect})")
|
347
|
+
c.load(h, *args)
|
348
|
+
end
|
349
|
+
else
|
350
|
+
# otherwise we just pass the hash to the constructor
|
351
|
+
self.row_proc = proc do |h|
|
352
|
+
c = hash[h[key]] || hash[nil] || \
|
353
|
+
raise(Error, "No matching model class for record (#{polymorphic_key} => #{h[polymorphic_key].inspect})")
|
354
|
+
c.new(h, *args)
|
355
|
+
end
|
356
|
+
end
|
357
|
+
else
|
358
|
+
raise ArgumentError, "Invalid model specified"
|
359
|
+
end
|
360
|
+
self
|
361
|
+
end
|
362
|
+
|
363
|
+
# Set values that override hash arguments given to insert and update statements.
|
364
|
+
# This hash is merged into the hash provided to insert or update.
|
365
|
+
def set_overrides(hash)
|
366
|
+
clone(:overrides=>hash.merge(@opts[:overrides]||{}))
|
367
|
+
end
|
368
|
+
|
369
|
+
# Sets a value transform which is used to convert values loaded and saved
|
370
|
+
# to/from the database. The transform should be supplied as a hash. Each
|
371
|
+
# value in the hash should be an array containing two proc objects - one
|
372
|
+
# for transforming loaded values, and one for transforming saved values.
|
373
|
+
# The following example demonstrates how to store Ruby objects in a dataset
|
374
|
+
# using Marshal serialization:
|
375
|
+
#
|
376
|
+
# dataset.transform(:obj => [
|
377
|
+
# proc {|v| Marshal.load(v)},
|
378
|
+
# proc {|v| Marshal.dump(v)}
|
379
|
+
# ])
|
380
|
+
#
|
381
|
+
# dataset.insert_sql(:obj => 1234) #=>
|
382
|
+
# "INSERT INTO items (obj) VALUES ('\004\bi\002\322\004')"
|
383
|
+
#
|
384
|
+
# Another form of using transform is by specifying stock transforms:
|
385
|
+
#
|
386
|
+
# dataset.transform(:obj => :marshal)
|
387
|
+
#
|
388
|
+
# The currently supported stock transforms are :marshal and :yaml.
|
389
|
+
def transform(t)
|
390
|
+
@transform = t
|
391
|
+
t.each do |k, v|
|
392
|
+
case v
|
393
|
+
when Array
|
394
|
+
if (v.size != 2) || !v.first.is_a?(Proc) && !v.last.is_a?(Proc)
|
395
|
+
raise Error::InvalidTransform, "Invalid transform specified"
|
396
|
+
end
|
397
|
+
else
|
398
|
+
unless v = STOCK_TRANSFORMS[v]
|
399
|
+
raise Error::InvalidTransform, "Invalid transform specified"
|
400
|
+
else
|
401
|
+
t[k] = v
|
402
|
+
end
|
403
|
+
end
|
404
|
+
end
|
405
|
+
self
|
406
|
+
end
|
407
|
+
|
408
|
+
# Applies the value transform for data loaded from the database.
|
409
|
+
def transform_load(r)
|
410
|
+
r.inject({}) do |m, kv|
|
411
|
+
k, v = *kv
|
412
|
+
m[k] = (tt = @transform[k]) ? tt[0][v] : v
|
413
|
+
m
|
414
|
+
end
|
415
|
+
end
|
416
|
+
|
417
|
+
# Applies the value transform for data saved to the database.
|
418
|
+
def transform_save(r)
|
419
|
+
r.inject({}) do |m, kv|
|
420
|
+
k, v = *kv
|
421
|
+
m[k] = (tt = @transform[k]) ? tt[1][v] : v
|
422
|
+
m
|
423
|
+
end
|
424
|
+
end
|
425
|
+
|
426
|
+
def upcase_identifiers=(v)
|
427
|
+
@identifier_input_method = v ? :upcase : nil
|
428
|
+
end
|
429
|
+
|
430
|
+
# Whether this dataset upcases identifiers.
|
431
|
+
def upcase_identifiers?
|
432
|
+
@identifier_input_method == :upcase
|
433
|
+
end
|
434
|
+
|
435
|
+
# Updates values for the dataset. The returned value is generally the
|
436
|
+
# number of rows updated, but that is adapter dependent.
|
437
|
+
def update(*args)
|
438
|
+
execute_dui(update_sql(*args))
|
439
|
+
end
|
440
|
+
|
441
|
+
# Add the mutation methods via metaprogramming
|
442
|
+
def_mutation_method(*MUTATION_METHODS)
|
443
|
+
|
444
|
+
protected
|
445
|
+
|
446
|
+
# Return true if the dataset has a non-nil value for any key in opts.
|
447
|
+
def options_overlap(opts)
|
448
|
+
!(@opts.collect{|k,v| k unless v.nil?}.compact & opts).empty?
|
449
|
+
end
|
450
|
+
|
451
|
+
private
|
452
|
+
|
453
|
+
# Runs #graph_each if graphing. Otherwise, iterates through the records
|
454
|
+
# yielded by #fetch_rows, applying any row_proc or transform if necessary,
|
455
|
+
# and yielding the result.
|
456
|
+
def _each(opts, &block)
|
457
|
+
if @opts[:graph] and !(opts && opts[:graph] == false)
|
458
|
+
graph_each(opts, &block)
|
459
|
+
else
|
460
|
+
row_proc = @row_proc unless opts && opts[:naked]
|
461
|
+
transform = @transform
|
462
|
+
fetch_rows(select_sql(opts)) do |r|
|
463
|
+
r = transform_load(r) if transform
|
464
|
+
r = row_proc[r] if row_proc
|
465
|
+
yield r
|
466
|
+
end
|
467
|
+
end
|
468
|
+
end
|
469
|
+
|
470
|
+
# Set the server to use to :default unless it is already set in the passed opts
|
471
|
+
def default_server_opts(opts)
|
472
|
+
{:server=>@opts[:server] || :default}.merge(opts)
|
473
|
+
end
|
474
|
+
|
475
|
+
# Execute the given SQL on the database using execute.
|
476
|
+
def execute(sql, opts={}, &block)
|
477
|
+
@db.execute(sql, {:server=>@opts[:server] || :read_only}.merge(opts), &block)
|
478
|
+
end
|
479
|
+
|
480
|
+
# Execute the given SQL on the database using execute_dui.
|
481
|
+
def execute_dui(sql, opts={}, &block)
|
482
|
+
@db.execute_dui(sql, default_server_opts(opts), &block)
|
483
|
+
end
|
484
|
+
|
485
|
+
# Execute the given SQL on the database using execute_insert.
|
486
|
+
def execute_insert(sql, opts={}, &block)
|
487
|
+
@db.execute_insert(sql, default_server_opts(opts), &block)
|
488
|
+
end
|
489
|
+
|
490
|
+
# Modify the identifier returned from the database based on the
|
491
|
+
# identifier_output_method.
|
492
|
+
def input_identifier(v)
|
493
|
+
(i = identifier_input_method) ? v.to_s.send(i) : v.to_s
|
494
|
+
end
|
495
|
+
|
496
|
+
# Modify the receiver with the results of sending the meth, args, and block
|
497
|
+
# to the receiver and merging the options of the resulting dataset into
|
498
|
+
# the receiver's options.
|
499
|
+
def mutation_method(meth, *args, &block)
|
500
|
+
copy = send(meth, *args, &block)
|
501
|
+
@opts.merge!(copy.opts)
|
502
|
+
self
|
503
|
+
end
|
504
|
+
|
505
|
+
# Modify the identifier returned from the database based on the
|
506
|
+
# identifier_output_method.
|
507
|
+
def output_identifier(v)
|
508
|
+
(i = identifier_output_method) ? v.to_s.send(i).to_sym : v.to_sym
|
509
|
+
end
|
510
|
+
end
|
511
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module Sequel
|
2
|
+
# This module makes it easy to add deprecation functionality to other classes.
|
3
|
+
module Deprecation # :nodoc:
|
4
|
+
# This sets the output stream for the deprecation messages. Set it to an IO
|
5
|
+
# (or any object that responds to puts) and it will call puts on that
|
6
|
+
# object with the deprecation message. Set to nil to ignore deprecation messages.
|
7
|
+
def self.deprecation_message_stream=(file)
|
8
|
+
@dms = file
|
9
|
+
end
|
10
|
+
|
11
|
+
# Set this to true to print tracebacks with every deprecation message,
|
12
|
+
# so you can see exactly where in your code the deprecated methods are
|
13
|
+
# being called.
|
14
|
+
def self.print_tracebacks=(pt)
|
15
|
+
@pt = pt
|
16
|
+
end
|
17
|
+
|
18
|
+
# Puts the messages unaltered to the deprecation message stream
|
19
|
+
def self.deprecate(message)
|
20
|
+
if @dms
|
21
|
+
@dms.puts(message)
|
22
|
+
caller.each{|c| @dms.puts(c)} if @pt
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module Sequel
|
2
|
+
# Represents an error raised in Sequel code.
|
3
|
+
class Error < ::StandardError
|
4
|
+
|
5
|
+
# Raised when Sequel is unable to load a specified adapter.
|
6
|
+
class AdapterNotFound < Error ; end
|
7
|
+
|
8
|
+
# Raised on an invalid operation.
|
9
|
+
class InvalidOperation < Error; end
|
10
|
+
|
11
|
+
# Error raised when an invalid statement is executed.
|
12
|
+
class InvalidStatement < Error; end
|
13
|
+
|
14
|
+
# Represents an Invalid transform.
|
15
|
+
class InvalidTransform < Error ; end
|
16
|
+
|
17
|
+
# Represents an invalid value stored in the database.
|
18
|
+
class InvalidValue < Error ; end
|
19
|
+
|
20
|
+
# Represents an attempt to performing filter operations when no filter has been specified yet.
|
21
|
+
class NoExistingFilter < Error ; end
|
22
|
+
|
23
|
+
# There was an error while waiting on a connection from the connection pool
|
24
|
+
class PoolTimeoutError < Error ; end
|
25
|
+
|
26
|
+
# Rollback is a special error used to rollback a transactions.
|
27
|
+
# A transaction block will catch this error and won't pass further up the stack.
|
28
|
+
class Rollback < Error ; end
|
29
|
+
end
|
30
|
+
|
31
|
+
# Generic error raised by the database adapters, indicating a
|
32
|
+
# problem originating from the database server.
|
33
|
+
class DatabaseError < Error; end
|
34
|
+
|
35
|
+
# Error raised when the Sequel is unable to connect to the database with the
|
36
|
+
# connection parameters it was given.
|
37
|
+
class DatabaseConnectionError < DatabaseError; end
|
38
|
+
|
39
|
+
# Error that should be raised by adapters when they determine that the connection
|
40
|
+
# to the database has been lost. Instructs the connection pool code to
|
41
|
+
# remove that connection from the pool so that other connections can be acquired
|
42
|
+
# automatically.
|
43
|
+
class DatabaseDisconnectError < DatabaseError; end
|
44
|
+
end
|