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,715 @@
|
|
1
|
+
module Sequel
|
2
|
+
# Top level module for holding all PostgreSQL-related modules and classes
|
3
|
+
# for Sequel. There are a few module level accessors that are added via
|
4
|
+
# metaprogramming. These are:
|
5
|
+
# * client_min_messages (only available when using the native adapter) -
|
6
|
+
# Change the minimum level of messages that PostgreSQL will send to the
|
7
|
+
# the client. The PostgreSQL default is NOTICE, the Sequel default is
|
8
|
+
# WARNING. Set to nil to not change the server default.
|
9
|
+
# * force_standard_strings - Set to false to not force the use of
|
10
|
+
# standard strings
|
11
|
+
# * use_iso_date_format (only available when using the native adapter) -
|
12
|
+
# Set to false to not change the date format to
|
13
|
+
# ISO. This disables one of Sequel's optimizations.
|
14
|
+
#
|
15
|
+
# Changes in these settings only affect future connections. To make
|
16
|
+
# sure that they are applied, they should generally be called right
|
17
|
+
# after the Database object is instantiated and before a connection
|
18
|
+
# is actually made. For example, to use whatever the server defaults are:
|
19
|
+
#
|
20
|
+
# DB = Sequel.postgres(...)
|
21
|
+
# Sequel::Postgres.client_min_messages = nil
|
22
|
+
# Sequel::Postgres.force_standard_strings = false
|
23
|
+
# Sequel::Postgres.use_iso_date_format = false
|
24
|
+
# # A connection to the server is not made until here
|
25
|
+
# DB[:t].all
|
26
|
+
#
|
27
|
+
# The reason they can't be done earlier is that the Sequel::Postgres
|
28
|
+
# module is not loaded until a Database object which uses PostgreSQL
|
29
|
+
# is created.
|
30
|
+
module Postgres
|
31
|
+
# Array of exceptions that need to be converted. JDBC
|
32
|
+
# uses NativeExceptions, the native adapter uses PGError.
|
33
|
+
CONVERTED_EXCEPTIONS = []
|
34
|
+
|
35
|
+
@client_min_messages = :warning
|
36
|
+
@force_standard_strings = true
|
37
|
+
|
38
|
+
# By default, Sequel sets the minimum level of log messages sent to the client
|
39
|
+
# to WARNING, where PostgreSQL uses a default of NOTICE. This is to avoid a lot
|
40
|
+
# of mostly useless messages when running migrations, such as a couple of lines
|
41
|
+
# for every serial primary key field.
|
42
|
+
metaattr_accessor :client_min_messages
|
43
|
+
|
44
|
+
# By default, Sequel forces the use of standard strings, so that
|
45
|
+
# '\\' is interpreted as \\ and not \. While PostgreSQL defaults
|
46
|
+
# to interpreting plain strings as extended strings, this will change
|
47
|
+
# in a future version of PostgreSQL. Sequel assumes that SQL standard
|
48
|
+
# strings will be used.
|
49
|
+
metaattr_accessor :force_standard_strings
|
50
|
+
|
51
|
+
# Methods shared by adapter/connection instances.
|
52
|
+
module AdapterMethods
|
53
|
+
attr_writer :db
|
54
|
+
|
55
|
+
SELECT_CURRVAL = "SELECT currval('%s')".freeze
|
56
|
+
SELECT_CUSTOM_SEQUENCE = proc do |schema, table| <<-end_sql
|
57
|
+
SELECT '"' || name.nspname || '"."' || CASE
|
58
|
+
WHEN split_part(def.adsrc, '''', 2) ~ '.' THEN
|
59
|
+
substr(split_part(def.adsrc, '''', 2),
|
60
|
+
strpos(split_part(def.adsrc, '''', 2), '.')+1)
|
61
|
+
ELSE split_part(def.adsrc, '''', 2)
|
62
|
+
END || '"'
|
63
|
+
FROM pg_class t
|
64
|
+
JOIN pg_namespace name ON (t.relnamespace = name.oid)
|
65
|
+
JOIN pg_attribute attr ON (t.oid = attrelid)
|
66
|
+
JOIN pg_attrdef def ON (adrelid = attrelid AND adnum = attnum)
|
67
|
+
JOIN pg_constraint cons ON (conrelid = adrelid AND adnum = conkey[1])
|
68
|
+
WHERE cons.contype = 'p'
|
69
|
+
AND def.adsrc ~* 'nextval'
|
70
|
+
#{"AND name.nspname = '#{schema}'" if schema}
|
71
|
+
AND t.relname = '#{table}'
|
72
|
+
end_sql
|
73
|
+
end
|
74
|
+
SELECT_PK = proc do |schema, table| <<-end_sql
|
75
|
+
SELECT pg_attribute.attname
|
76
|
+
FROM pg_class, pg_attribute, pg_index, pg_namespace
|
77
|
+
WHERE pg_class.oid = pg_attribute.attrelid
|
78
|
+
AND pg_class.relnamespace = pg_namespace.oid
|
79
|
+
AND pg_class.oid = pg_index.indrelid
|
80
|
+
AND pg_index.indkey[0] = pg_attribute.attnum
|
81
|
+
AND pg_index.indisprimary = 't'
|
82
|
+
#{"AND pg_namespace.nspname = '#{schema}'" if schema}
|
83
|
+
AND pg_class.relname = '#{table}'
|
84
|
+
end_sql
|
85
|
+
end
|
86
|
+
SELECT_SERIAL_SEQUENCE = proc do |schema, table| <<-end_sql
|
87
|
+
SELECT '"' || name.nspname || '"."' || seq.relname || '"'
|
88
|
+
FROM pg_class seq, pg_attribute attr, pg_depend dep,
|
89
|
+
pg_namespace name, pg_constraint cons
|
90
|
+
WHERE seq.oid = dep.objid
|
91
|
+
AND seq.relnamespace = name.oid
|
92
|
+
AND seq.relkind = 'S'
|
93
|
+
AND attr.attrelid = dep.refobjid
|
94
|
+
AND attr.attnum = dep.refobjsubid
|
95
|
+
AND attr.attrelid = cons.conrelid
|
96
|
+
AND attr.attnum = cons.conkey[1]
|
97
|
+
AND cons.contype = 'p'
|
98
|
+
#{"AND name.nspname = '#{schema}'" if schema}
|
99
|
+
AND seq.relname = '#{table}'
|
100
|
+
end_sql
|
101
|
+
end
|
102
|
+
|
103
|
+
# Depth of the current transaction on this connection, used
|
104
|
+
# to implement multi-level transactions with savepoints.
|
105
|
+
attr_accessor :transaction_depth
|
106
|
+
|
107
|
+
# Apply connection settings for this connection. Currently, turns
|
108
|
+
# standard_conforming_strings ON if Postgres.force_standard_strings
|
109
|
+
# is true.
|
110
|
+
def apply_connection_settings
|
111
|
+
if Postgres.force_standard_strings
|
112
|
+
sql = "SET standard_conforming_strings = ON"
|
113
|
+
@db.log_info(sql)
|
114
|
+
# This setting will only work on PostgreSQL 8.2 or greater
|
115
|
+
# and we don't know the server version at this point, so
|
116
|
+
# try it unconditionally and rescue any errors.
|
117
|
+
execute(sql) rescue nil
|
118
|
+
end
|
119
|
+
if cmm = Postgres.client_min_messages
|
120
|
+
sql = "SET client_min_messages = '#{cmm.to_s.upcase}'"
|
121
|
+
@db.log_info(sql)
|
122
|
+
execute(sql)
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
# Get the last inserted value for the given sequence.
|
127
|
+
def last_insert_id(sequence)
|
128
|
+
sql = SELECT_CURRVAL % sequence
|
129
|
+
@db.log_info(sql)
|
130
|
+
execute(sql) do |r|
|
131
|
+
val = single_value(r)
|
132
|
+
return val.to_i if val
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
# Get the primary key for the given table.
|
137
|
+
def primary_key(schema, table)
|
138
|
+
sql = SELECT_PK[schema, table]
|
139
|
+
@db.log_info(sql)
|
140
|
+
execute(sql) do |r|
|
141
|
+
return single_value(r)
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
# Get the primary key and sequence for the given table.
|
146
|
+
def sequence(schema, table)
|
147
|
+
sql = SELECT_SERIAL_SEQUENCE[schema, table]
|
148
|
+
@db.log_info(sql)
|
149
|
+
execute(sql) do |r|
|
150
|
+
seq = single_value(r)
|
151
|
+
return seq if seq
|
152
|
+
end
|
153
|
+
|
154
|
+
sql = SELECT_CUSTOM_SEQUENCE[schema, table]
|
155
|
+
@db.log_info(sql)
|
156
|
+
execute(sql) do |r|
|
157
|
+
return single_value(r)
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
# Methods shared by Database instances that connect to PostgreSQL.
|
163
|
+
module DatabaseMethods
|
164
|
+
PREPARED_ARG_PLACEHOLDER = '$'.lit.freeze
|
165
|
+
RE_CURRVAL_ERROR = /currval of sequence "(.*)" is not yet defined in this session|relation "(.*)" does not exist/.freeze
|
166
|
+
SQL_BEGIN = 'BEGIN'.freeze
|
167
|
+
SQL_SAVEPOINT = 'SAVEPOINT autopoint_%d'.freeze
|
168
|
+
SQL_COMMIT = 'COMMIT'.freeze
|
169
|
+
SQL_ROLLBACK_TO_SAVEPOINT = 'ROLLBACK TO SAVEPOINT autopoint_%d'.freeze
|
170
|
+
SQL_ROLLBACK = 'ROLLBACK'.freeze
|
171
|
+
SQL_RELEASE_SAVEPOINT = 'RELEASE SAVEPOINT autopoint_%d'.freeze
|
172
|
+
SYSTEM_TABLE_REGEXP = /^pg|sql/.freeze
|
173
|
+
TYPES = Sequel::Schema::SQL::TYPES.merge(File=>'bytea', String=>'text')
|
174
|
+
|
175
|
+
# Creates the function in the database. See create_function_sql for arguments.
|
176
|
+
def create_function(*args)
|
177
|
+
self << create_function_sql(*args)
|
178
|
+
end
|
179
|
+
|
180
|
+
# SQL statement to create database function. Arguments:
|
181
|
+
# * name : name of the function to create
|
182
|
+
# * definition : string definition of the function, or object file for a dynamically loaded C function.
|
183
|
+
# * opts : options hash:
|
184
|
+
# * :args : function arguments, can be either a symbol or string specifying a type or an array of 1-3 elements:
|
185
|
+
# * element 1 : argument data type
|
186
|
+
# * element 2 : argument name
|
187
|
+
# * element 3 : argument mode (e.g. in, out, inout)
|
188
|
+
# * :behavior : Should be IMMUTABLE, STABLE, or VOLATILE. PostgreSQL assumes VOLATILE by default.
|
189
|
+
# * :cost : The estimated cost of the function, used by the query planner.
|
190
|
+
# * :language : The language the function uses. SQL is the default.
|
191
|
+
# * :link_symbol : For a dynamically loaded see function, the function's link symbol if different from the definition argument.
|
192
|
+
# * :returns : The data type returned by the function. If you are using OUT or INOUT argument modes, this is ignored.
|
193
|
+
# Otherwise, if this is not specified, void is used by default to specify the function is not supposed to return a value.
|
194
|
+
# * :rows : The estimated number of rows the function will return. Only use if the function returns SETOF something.
|
195
|
+
# * :security_definer : Makes the privileges of the function the same as the privileges of the user who defined the function instead of
|
196
|
+
# the privileges of the user who runs the function. There are security implications when doing this, see the PostgreSQL documentation.
|
197
|
+
# * :set : Configuration variables to set while the function is being run, can be a hash or an array of two pairs. search_path is
|
198
|
+
# often used here if :security_definer is used.
|
199
|
+
# * :strict : Makes the function return NULL when any argument is NULL.
|
200
|
+
def create_function_sql(name, definition, opts={})
|
201
|
+
args = opts[:args]
|
202
|
+
if !opts[:args].is_a?(Array) || !opts[:args].any?{|a| Array(a).length == 3 and %w'OUT INOUT'.include?(a[2].to_s)}
|
203
|
+
returns = opts[:returns] || 'void'
|
204
|
+
end
|
205
|
+
language = opts[:language] || 'SQL'
|
206
|
+
<<-END
|
207
|
+
CREATE#{' OR REPLACE' if opts[:replace]} FUNCTION #{name}#{sql_function_args(args)}
|
208
|
+
#{"RETURNS #{returns}" if returns}
|
209
|
+
LANGUAGE #{language}
|
210
|
+
#{opts[:behavior].to_s.upcase if opts[:behavior]}
|
211
|
+
#{'STRICT' if opts[:strict]}
|
212
|
+
#{'SECURITY DEFINER' if opts[:security_definer]}
|
213
|
+
#{"COST #{opts[:cost]}" if opts[:cost]}
|
214
|
+
#{"ROWS #{opts[:rows]}" if opts[:rows]}
|
215
|
+
#{opts[:set].map{|k,v| " SET #{k} = #{v}"}.join("\n") if opts[:set]}
|
216
|
+
AS #{literal(definition.to_s)}#{", #{literal(opts[:link_symbol].to_s)}" if opts[:link_symbol]}
|
217
|
+
END
|
218
|
+
end
|
219
|
+
|
220
|
+
# Create the procedural language in the database. See create_language_sql for arguments.
|
221
|
+
def create_language(*args)
|
222
|
+
self << create_language_sql(*args)
|
223
|
+
end
|
224
|
+
|
225
|
+
# SQL for creating a procedural language. Arguments:
|
226
|
+
# * name : Name of the procedural language (e.g. plpgsql)
|
227
|
+
# * opts : options hash:
|
228
|
+
# * :handler : The name of a previously registered function used as a call handler for this language.
|
229
|
+
# * :trusted : Marks the language being created as trusted, allowing unprivileged users to create functions using this language.
|
230
|
+
# * :validator : The name of previously registered function used as a validator of functions defined in this language.
|
231
|
+
def create_language_sql(name, opts={})
|
232
|
+
"CREATE#{' TRUSTED' if opts[:trusted]} LANGUAGE #{name}#{" HANDLER #{opts[:handler]}" if opts[:handler]}#{" VALIDATOR #{opts[:validator]}" if opts[:validator]}"
|
233
|
+
end
|
234
|
+
|
235
|
+
# Create a trigger in the database. See create_trigger_sql for arguments.
|
236
|
+
def create_trigger(*args)
|
237
|
+
self << create_trigger_sql(*args)
|
238
|
+
end
|
239
|
+
|
240
|
+
# SQL for creating a database trigger. Arguments:
|
241
|
+
# * table : the table on which this trigger operates
|
242
|
+
# * name : the name of this trigger
|
243
|
+
# * function : the function to call for this trigger, which should return type trigger.
|
244
|
+
# * opts : options hash:
|
245
|
+
# * :after : Calls the trigger after execution instead of before.
|
246
|
+
# * :args : An argument or array of arguments to pass to the function.
|
247
|
+
# * :each_row : Calls the trigger for each row instead of for each statement.
|
248
|
+
# * :events : Can be :insert, :update, :delete, or an array of any of those. Calls the trigger whenever that type of statement is used. By default,
|
249
|
+
# the trigger is called for insert, update, or delete.
|
250
|
+
def create_trigger_sql(table, name, function, opts={})
|
251
|
+
events = opts[:events] ? Array(opts[:events]) : [:insert, :update, :delete]
|
252
|
+
whence = opts[:after] ? 'AFTER' : 'BEFORE'
|
253
|
+
"CREATE TRIGGER #{name} #{whence} #{events.map{|e| e.to_s.upcase}.join(' OR ')} ON #{quote_schema_table(table)}#{' FOR EACH ROW' if opts[:each_row]} EXECUTE PROCEDURE #{function}(#{Array(opts[:args]).map{|a| literal(a)}.join(', ')})"
|
254
|
+
end
|
255
|
+
|
256
|
+
# Drops the function from the database. See drop_function_sql for arguments.
|
257
|
+
def drop_function(*args)
|
258
|
+
self << drop_function_sql(*args)
|
259
|
+
end
|
260
|
+
|
261
|
+
# SQL for dropping a function from the database. Arguments:
|
262
|
+
# * name : name of the function to drop
|
263
|
+
# * opts : options hash:
|
264
|
+
# * :args : The arguments for the function. See create_function_sql.
|
265
|
+
# * :cascade : Drop other objects depending on this function.
|
266
|
+
# * :if_exists : Don't raise an error if the function doesn't exist.
|
267
|
+
def drop_function_sql(name, opts={})
|
268
|
+
"DROP FUNCTION#{' IF EXISTS' if opts[:if_exists]} #{name}#{sql_function_args(opts[:args])}#{' CASCADE' if opts[:cascade]}"
|
269
|
+
end
|
270
|
+
|
271
|
+
# Drops a procedural language from the database. See drop_language_sql for arguments.
|
272
|
+
def drop_language(*args)
|
273
|
+
self << drop_language_sql(*args)
|
274
|
+
end
|
275
|
+
|
276
|
+
# SQL for dropping a procedural language from the database. Arguments:
|
277
|
+
# * name : name of the procedural language to drop
|
278
|
+
# * opts : options hash:
|
279
|
+
# * :cascade : Drop other objects depending on this function.
|
280
|
+
# * :if_exists : Don't raise an error if the function doesn't exist.
|
281
|
+
def drop_language_sql(name, opts={})
|
282
|
+
"DROP LANGUAGE#{' IF EXISTS' if opts[:if_exists]} #{name}#{' CASCADE' if opts[:cascade]}"
|
283
|
+
end
|
284
|
+
|
285
|
+
# Remove the cached entries for primary keys and sequences when dropping a table.
|
286
|
+
def drop_table(*names)
|
287
|
+
names.each do |name|
|
288
|
+
name = quote_schema_table(name)
|
289
|
+
@primary_keys.delete(name)
|
290
|
+
@primary_key_sequences.delete(name)
|
291
|
+
end
|
292
|
+
super
|
293
|
+
end
|
294
|
+
|
295
|
+
# Always CASCADE the table drop
|
296
|
+
def drop_table_sql(name)
|
297
|
+
"DROP TABLE #{quote_schema_table(name)} CASCADE"
|
298
|
+
end
|
299
|
+
|
300
|
+
# Drops a trigger from the database. See drop_trigger_sql for arguments.
|
301
|
+
def drop_trigger(*args)
|
302
|
+
self << drop_trigger_sql(*args)
|
303
|
+
end
|
304
|
+
|
305
|
+
# SQL for dropping a trigger from the database. Arguments:
|
306
|
+
# * table : table from which to drop the trigger
|
307
|
+
# * name : name of the trigger to drop
|
308
|
+
# * opts : options hash:
|
309
|
+
# * :cascade : Drop other objects depending on this function.
|
310
|
+
# * :if_exists : Don't raise an error if the function doesn't exist.
|
311
|
+
def drop_trigger_sql(table, name, opts={})
|
312
|
+
"DROP TRIGGER#{' IF EXISTS' if opts[:if_exists]} #{name} ON #{quote_schema_table(table)}#{' CASCADE' if opts[:cascade]}"
|
313
|
+
end
|
314
|
+
|
315
|
+
# PostgreSQL specific index SQL.
|
316
|
+
def index_definition_sql(table_name, index)
|
317
|
+
index_name = index[:name] || default_index_name(table_name, index[:columns])
|
318
|
+
expr = literal(Array(index[:columns]))
|
319
|
+
unique = "UNIQUE " if index[:unique]
|
320
|
+
index_type = index[:type]
|
321
|
+
filter = index[:where] || index[:filter]
|
322
|
+
filter = " WHERE #{filter_expr(filter)}" if filter
|
323
|
+
case index_type
|
324
|
+
when :full_text
|
325
|
+
cols = Array(index[:columns]).map{|x| SQL::Function.new(:COALESCE, x, '')}.sql_string_join(' ')
|
326
|
+
expr = "(to_tsvector(#{literal(index[:language] || 'simple')}, #{literal(cols)}))"
|
327
|
+
index_type = :gin
|
328
|
+
when :spatial
|
329
|
+
index_type = :gist
|
330
|
+
end
|
331
|
+
"CREATE #{unique}INDEX #{quote_identifier(index_name)} ON #{quote_schema_table(table_name)} #{"USING #{index_type} " if index_type}#{expr}#{filter}"
|
332
|
+
end
|
333
|
+
|
334
|
+
# Dataset containing all current database locks
|
335
|
+
def locks
|
336
|
+
dataset.from(:pg_class).join(:pg_locks, :relation=>:relfilenode).select(:pg_class__relname, Sequel::SQL::ColumnAll.new(:pg_locks))
|
337
|
+
end
|
338
|
+
|
339
|
+
# Return primary key for the given table.
|
340
|
+
def primary_key(table, opts={})
|
341
|
+
quoted_table = quote_schema_table(table)
|
342
|
+
return @primary_keys[quoted_table] if @primary_keys.include?(quoted_table)
|
343
|
+
@primary_keys[quoted_table] = if conn = opts[:conn]
|
344
|
+
conn.primary_key(*schema_and_table(table))
|
345
|
+
else
|
346
|
+
synchronize(opts[:server]){|con| con.primary_key(*schema_and_table(table))}
|
347
|
+
end
|
348
|
+
end
|
349
|
+
|
350
|
+
# Return the sequence providing the default for the primary key for the given table.
|
351
|
+
def primary_key_sequence(table, opts={})
|
352
|
+
quoted_table = quote_schema_table(table)
|
353
|
+
return @primary_key_sequences[quoted_table] if @primary_key_sequences.include?(quoted_table)
|
354
|
+
@primary_key_sequences[quoted_table] = if conn = opts[:conn]
|
355
|
+
conn.sequence(*schema_and_table(table))
|
356
|
+
else
|
357
|
+
synchronize(opts[:server]){|con| con.sequence(*schema_and_table(table))}
|
358
|
+
end
|
359
|
+
end
|
360
|
+
|
361
|
+
# SQL DDL statement for renaming a table. PostgreSQL doesn't allow you to change a table's schema in
|
362
|
+
# a rename table operation, so speciying a new schema in new_name will not have an effect.
|
363
|
+
def rename_table_sql(name, new_name)
|
364
|
+
"ALTER TABLE #{quote_schema_table(name)} RENAME TO #{quote_identifier(schema_and_table(new_name).last)}"
|
365
|
+
end
|
366
|
+
|
367
|
+
# PostgreSQL uses SERIAL psuedo-type instead of AUTOINCREMENT for
|
368
|
+
# managing incrementing primary keys.
|
369
|
+
def serial_primary_key_options
|
370
|
+
{:primary_key => true, :type => :serial}
|
371
|
+
end
|
372
|
+
|
373
|
+
# The version of the PostgreSQL server, used for determining capability.
|
374
|
+
def server_version(server=nil)
|
375
|
+
return @server_version if @server_version
|
376
|
+
@server_version = synchronize(server) do |conn|
|
377
|
+
(conn.server_version rescue nil) if conn.respond_to?(:server_version)
|
378
|
+
end
|
379
|
+
unless @server_version
|
380
|
+
m = /PostgreSQL (\d+)\.(\d+)\.(\d+)/.match(get(SQL::Function.new(:version)))
|
381
|
+
@server_version = (m[1].to_i * 10000) + (m[2].to_i * 100) + m[3].to_i
|
382
|
+
end
|
383
|
+
@server_version
|
384
|
+
end
|
385
|
+
|
386
|
+
# Whether the given table exists in the database
|
387
|
+
#
|
388
|
+
# Options:
|
389
|
+
# * :schema - The schema to search (default_schema by default)
|
390
|
+
# * :server - The server to use
|
391
|
+
def table_exists?(table, opts={})
|
392
|
+
schema, table = schema_and_table(table)
|
393
|
+
opts[:schema] ||= schema
|
394
|
+
tables(opts){|ds| !ds.first(:relname=>ds.send(:input_identifier, table)).nil?}
|
395
|
+
end
|
396
|
+
|
397
|
+
# Array of symbols specifying table names in the current database.
|
398
|
+
# The dataset used is yielded to the block if one is provided,
|
399
|
+
# otherwise, an array of symbols of table names is returned.
|
400
|
+
#
|
401
|
+
# Options:
|
402
|
+
# * :schema - The schema to search (default_schema by default)
|
403
|
+
# * :server - The server to use
|
404
|
+
def tables(opts={})
|
405
|
+
ds = self[:pg_class].filter(:relkind=>'r').select(:relname).exclude(:relname.like(SYSTEM_TABLE_REGEXP)).server(opts[:server])
|
406
|
+
ds.join!(:pg_namespace, :oid=>:relnamespace, :nspname=>(opts[:schema]||default_schema).to_s) if opts[:schema] || default_schema
|
407
|
+
ds.identifier_input_method = nil
|
408
|
+
ds.identifier_output_method = nil
|
409
|
+
ds2 = dataset
|
410
|
+
block_given? ? yield(ds) : ds.map{|r| ds2.send(:output_identifier, r[:relname])}
|
411
|
+
end
|
412
|
+
|
413
|
+
# PostgreSQL supports multi-level transactions using save points.
|
414
|
+
def transaction(server=nil)
|
415
|
+
synchronize(server) do |conn|
|
416
|
+
conn.transaction_depth = 0 if conn.transaction_depth.nil?
|
417
|
+
if conn.transaction_depth > 0
|
418
|
+
log_info(SQL_SAVEPOINT % conn.transaction_depth)
|
419
|
+
conn.execute(SQL_SAVEPOINT % conn.transaction_depth)
|
420
|
+
else
|
421
|
+
log_info(SQL_BEGIN)
|
422
|
+
conn.execute(SQL_BEGIN)
|
423
|
+
end
|
424
|
+
begin
|
425
|
+
conn.transaction_depth += 1
|
426
|
+
yield conn
|
427
|
+
rescue ::Exception => e
|
428
|
+
if conn.transaction_depth > 1
|
429
|
+
log_info(SQL_ROLLBACK_TO_SAVEPOINT % [conn.transaction_depth - 1])
|
430
|
+
conn.execute(SQL_ROLLBACK_TO_SAVEPOINT % [conn.transaction_depth - 1])
|
431
|
+
else
|
432
|
+
log_info(SQL_ROLLBACK)
|
433
|
+
conn.execute(SQL_ROLLBACK) rescue nil
|
434
|
+
end
|
435
|
+
transaction_error(e, *CONVERTED_EXCEPTIONS)
|
436
|
+
ensure
|
437
|
+
unless e
|
438
|
+
begin
|
439
|
+
if conn.transaction_depth < 2
|
440
|
+
log_info(SQL_COMMIT)
|
441
|
+
conn.execute(SQL_COMMIT)
|
442
|
+
else
|
443
|
+
log_info(SQL_RELEASE_SAVEPOINT % [conn.transaction_depth - 1])
|
444
|
+
conn.execute(SQL_RELEASE_SAVEPOINT % [conn.transaction_depth - 1])
|
445
|
+
end
|
446
|
+
rescue => e
|
447
|
+
log_info(e.message)
|
448
|
+
raise_error(e, :classes=>CONVERTED_EXCEPTIONS)
|
449
|
+
end
|
450
|
+
end
|
451
|
+
conn.transaction_depth -= 1
|
452
|
+
end
|
453
|
+
end
|
454
|
+
end
|
455
|
+
|
456
|
+
private
|
457
|
+
|
458
|
+
# PostgreSQL folds unquoted identifiers to lowercase, so it shouldn't need to upcase identifiers on input.
|
459
|
+
def identifier_input_method_default
|
460
|
+
nil
|
461
|
+
end
|
462
|
+
|
463
|
+
# PostgreSQL folds unquoted identifiers to lowercase, so it shouldn't need to upcase identifiers on output.
|
464
|
+
def identifier_output_method_default
|
465
|
+
nil
|
466
|
+
end
|
467
|
+
|
468
|
+
# The result of the insert for the given table and values. If values
|
469
|
+
# is an array, assume the first column is the primary key and return
|
470
|
+
# that. If values is a hash, lookup the primary key for the table. If
|
471
|
+
# the primary key is present in the hash, return its value. Otherwise,
|
472
|
+
# look up the sequence for the table's primary key. If one exists,
|
473
|
+
# return the last value the of the sequence for the connection.
|
474
|
+
def insert_result(conn, table, values)
|
475
|
+
case values
|
476
|
+
when Hash
|
477
|
+
return nil unless pk = primary_key(table, :conn=>conn)
|
478
|
+
if pk and pkv = values[pk.to_sym]
|
479
|
+
pkv
|
480
|
+
else
|
481
|
+
begin
|
482
|
+
if seq = primary_key_sequence(table, :conn=>conn)
|
483
|
+
conn.last_insert_id(seq)
|
484
|
+
end
|
485
|
+
rescue Exception => e
|
486
|
+
raise_error(e, :classes=>CONVERTED_EXCEPTIONS) unless RE_CURRVAL_ERROR.match(e.message)
|
487
|
+
end
|
488
|
+
end
|
489
|
+
when Array
|
490
|
+
values.first
|
491
|
+
else
|
492
|
+
nil
|
493
|
+
end
|
494
|
+
end
|
495
|
+
|
496
|
+
# Use a dollar sign instead of question mark for the argument
|
497
|
+
# placeholder.
|
498
|
+
def prepared_arg_placeholder
|
499
|
+
PREPARED_ARG_PLACEHOLDER
|
500
|
+
end
|
501
|
+
|
502
|
+
# The dataset used for parsing table schemas, using the pg_* system catalogs.
|
503
|
+
def schema_parse_table(table_name, opts)
|
504
|
+
ds2 = dataset
|
505
|
+
ds = dataset.select(:pg_attribute__attname___name,
|
506
|
+
SQL::Function.new(:format_type, :pg_type__oid, :pg_attribute__atttypmod).as(:db_type),
|
507
|
+
SQL::Function.new(:pg_get_expr, :pg_attrdef__adbin, :pg_class__oid).as(:default),
|
508
|
+
SQL::BooleanExpression.new(:NOT, :pg_attribute__attnotnull).as(:allow_null),
|
509
|
+
SQL::Function.new(:COALESCE, {:pg_attribute__attnum => SQL::Function.new(:ANY, :pg_index__indkey)}.sql_expr, false).as(:primary_key)).
|
510
|
+
from(:pg_class).
|
511
|
+
join(:pg_attribute, :attrelid=>:oid).
|
512
|
+
join(:pg_type, :oid=>:atttypid).
|
513
|
+
left_outer_join(:pg_attrdef, :adrelid=>:pg_class__oid, :adnum=>:pg_attribute__attnum).
|
514
|
+
left_outer_join(:pg_index, :indrelid=>:pg_class__oid, :indisprimary=>true).
|
515
|
+
filter(:pg_attribute__attisdropped=>false).
|
516
|
+
filter{|o| o.pg_attribute__attnum > 0}.
|
517
|
+
filter(:pg_class__relname=>ds2.send(:input_identifier, table_name)).
|
518
|
+
order(:pg_attribute__attnum)
|
519
|
+
ds.join!(:pg_namespace, :oid=>:pg_class__relnamespace, :nspname=>(opts[:schema] || default_schema).to_s) if opts[:schema] || default_schema
|
520
|
+
ds.identifier_input_method = nil
|
521
|
+
ds.identifier_output_method = nil
|
522
|
+
ds.map do |row|
|
523
|
+
row[:default] = nil if row[:default].blank?
|
524
|
+
row[:type] = schema_column_type(row[:db_type])
|
525
|
+
[ds2.send(:output_identifier, row.delete(:name)), row]
|
526
|
+
end
|
527
|
+
end
|
528
|
+
|
529
|
+
# Turns an array of argument specifiers into an SQL fragment used for function arguments. See create_function_sql.
|
530
|
+
def sql_function_args(args)
|
531
|
+
"(#{Array(args).map{|a| Array(a).reverse.join(' ')}.join(', ')})"
|
532
|
+
end
|
533
|
+
|
534
|
+
# Override the standard type conversions with PostgreSQL specific ones
|
535
|
+
def type_literal_base(column)
|
536
|
+
TYPES[column[:type]]
|
537
|
+
end
|
538
|
+
end
|
539
|
+
|
540
|
+
# Instance methods for datasets that connect to a PostgreSQL database.
|
541
|
+
module DatasetMethods
|
542
|
+
ACCESS_SHARE = 'ACCESS SHARE'.freeze
|
543
|
+
ACCESS_EXCLUSIVE = 'ACCESS EXCLUSIVE'.freeze
|
544
|
+
BOOL_FALSE = 'false'.freeze
|
545
|
+
BOOL_TRUE = 'true'.freeze
|
546
|
+
COMMA_SEPARATOR = ', '.freeze
|
547
|
+
EXCLUSIVE = 'EXCLUSIVE'.freeze
|
548
|
+
EXPLAIN = 'EXPLAIN '.freeze
|
549
|
+
EXPLAIN_ANALYZE = 'EXPLAIN ANALYZE '.freeze
|
550
|
+
FOR_SHARE = ' FOR SHARE'.freeze
|
551
|
+
FOR_UPDATE = ' FOR UPDATE'.freeze
|
552
|
+
LOCK = 'LOCK TABLE %s IN %s MODE'.freeze
|
553
|
+
PG_TIMESTAMP_FORMAT = "TIMESTAMP '%Y-%m-%d %H:%M:%S".freeze
|
554
|
+
QUERY_PLAN = 'QUERY PLAN'.to_sym
|
555
|
+
ROW_EXCLUSIVE = 'ROW EXCLUSIVE'.freeze
|
556
|
+
ROW_SHARE = 'ROW SHARE'.freeze
|
557
|
+
SELECT_CLAUSE_ORDER = %w'distinct columns from join where group having compounds order limit lock'.freeze
|
558
|
+
SHARE = 'SHARE'.freeze
|
559
|
+
SHARE_ROW_EXCLUSIVE = 'SHARE ROW EXCLUSIVE'.freeze
|
560
|
+
SHARE_UPDATE_EXCLUSIVE = 'SHARE UPDATE EXCLUSIVE'.freeze
|
561
|
+
|
562
|
+
# Shared methods for prepared statements when used with PostgreSQL databases.
|
563
|
+
module PreparedStatementMethods
|
564
|
+
# Override insert action to use RETURNING if the server supports it.
|
565
|
+
def prepared_sql
|
566
|
+
return @prepared_sql if @prepared_sql
|
567
|
+
super
|
568
|
+
if @prepared_type == :insert and server_version >= 80200
|
569
|
+
@prepared_sql = insert_returning_pk_sql(@prepared_modify_values)
|
570
|
+
meta_def(:insert_returning_pk_sql){|*args| prepared_sql}
|
571
|
+
end
|
572
|
+
@prepared_sql
|
573
|
+
end
|
574
|
+
end
|
575
|
+
|
576
|
+
# Return the results of an ANALYZE query as a string
|
577
|
+
def analyze(opts = nil)
|
578
|
+
analysis = []
|
579
|
+
fetch_rows(EXPLAIN_ANALYZE + select_sql(opts)) do |r|
|
580
|
+
analysis << r[QUERY_PLAN]
|
581
|
+
end
|
582
|
+
analysis.join("\r\n")
|
583
|
+
end
|
584
|
+
|
585
|
+
# Return the results of an EXPLAIN query as a string
|
586
|
+
def explain(opts = nil)
|
587
|
+
analysis = []
|
588
|
+
fetch_rows(EXPLAIN + select_sql(opts)) do |r|
|
589
|
+
analysis << r[QUERY_PLAN]
|
590
|
+
end
|
591
|
+
analysis.join("\r\n")
|
592
|
+
end
|
593
|
+
|
594
|
+
# Return a cloned dataset with a :share lock type.
|
595
|
+
def for_share
|
596
|
+
clone(:lock => :share)
|
597
|
+
end
|
598
|
+
|
599
|
+
# Return a cloned dataset with a :update lock type.
|
600
|
+
def for_update
|
601
|
+
clone(:lock => :update)
|
602
|
+
end
|
603
|
+
|
604
|
+
# PostgreSQL specific full text search syntax, using tsearch2 (included
|
605
|
+
# in 8.3 by default, and available for earlier versions as an add-on).
|
606
|
+
def full_text_search(cols, terms, opts = {})
|
607
|
+
lang = opts[:language] || 'simple'
|
608
|
+
cols = Array(cols).map{|x| SQL::Function.new(:COALESCE, x, '')}.sql_string_join(' ')
|
609
|
+
filter("to_tsvector(#{literal(lang)}, #{literal(cols)}) @@ to_tsquery(#{literal(lang)}, #{literal(Array(terms).join(' | '))})")
|
610
|
+
end
|
611
|
+
|
612
|
+
# Insert given values into the database.
|
613
|
+
def insert(*values)
|
614
|
+
if !@opts[:sql] and server_version >= 80200
|
615
|
+
single_value(default_server_opts(:sql=>insert_returning_pk_sql(*values)))
|
616
|
+
else
|
617
|
+
execute_insert(insert_sql(*values), :table=>opts[:from].first,
|
618
|
+
:values=>values.size == 1 ? values.first : values)
|
619
|
+
end
|
620
|
+
end
|
621
|
+
|
622
|
+
# Use the RETURNING clause to return the columns listed in returning.
|
623
|
+
def insert_returning_sql(returning, *values)
|
624
|
+
"#{insert_sql(*values)} RETURNING #{column_list(Array(returning))}"
|
625
|
+
end
|
626
|
+
|
627
|
+
# Insert a record returning the record inserted
|
628
|
+
def insert_select(*values)
|
629
|
+
single_record(default_server_opts(:naked=>true, :sql=>insert_returning_sql(nil, *values))) if server_version >= 80200
|
630
|
+
end
|
631
|
+
|
632
|
+
# Handle microseconds for Time and DateTime values, as well as PostgreSQL
|
633
|
+
# specific boolean values and string escaping.
|
634
|
+
def literal(v)
|
635
|
+
case v
|
636
|
+
when LiteralString
|
637
|
+
v
|
638
|
+
when SQL::Blob
|
639
|
+
"'#{v.gsub(/[\000-\037\047\134\177-\377]/){|b| "\\#{("%o" % b[0..1].unpack("C")[0]).rjust(3, '0')}"}}'"
|
640
|
+
when String
|
641
|
+
"'#{v.gsub("'", "''")}'"
|
642
|
+
when Time
|
643
|
+
"#{v.strftime(PG_TIMESTAMP_FORMAT)}.#{sprintf("%06d",v.usec)}'"
|
644
|
+
when DateTime
|
645
|
+
"#{v.strftime(PG_TIMESTAMP_FORMAT)}.#{sprintf("%06d", (v.sec_fraction * 86400000000).to_i)}'"
|
646
|
+
when TrueClass
|
647
|
+
BOOL_TRUE
|
648
|
+
when FalseClass
|
649
|
+
BOOL_FALSE
|
650
|
+
else
|
651
|
+
super
|
652
|
+
end
|
653
|
+
end
|
654
|
+
|
655
|
+
# Locks the table with the specified mode.
|
656
|
+
def lock(mode, server=nil)
|
657
|
+
sql = LOCK % [source_list(@opts[:from]), mode]
|
658
|
+
@db.synchronize(server) do
|
659
|
+
if block_given? # perform locking inside a transaction and yield to block
|
660
|
+
@db.transaction(server){@db.execute(sql, :server=>server); yield}
|
661
|
+
else
|
662
|
+
@db.execute(sql, :server=>server) # lock without a transaction
|
663
|
+
self
|
664
|
+
end
|
665
|
+
end
|
666
|
+
end
|
667
|
+
|
668
|
+
# For PostgreSQL version > 8.2, allow inserting multiple rows at once.
|
669
|
+
def multi_insert_sql(columns, values)
|
670
|
+
return super if server_version < 80200
|
671
|
+
|
672
|
+
# postgresql 8.2 introduces support for multi-row insert
|
673
|
+
values = values.map {|r| literal(Array(r))}.join(COMMA_SEPARATOR)
|
674
|
+
["INSERT INTO #{source_list(@opts[:from])} (#{identifier_list(columns)}) VALUES #{values}"]
|
675
|
+
end
|
676
|
+
|
677
|
+
private
|
678
|
+
|
679
|
+
# Use the RETURNING clause to return the primary key of the inserted record, if it exists
|
680
|
+
def insert_returning_pk_sql(*values)
|
681
|
+
pk = db.primary_key(opts[:from].first)
|
682
|
+
insert_returning_sql(pk ? Sequel::SQL::Identifier.new(pk) : 'NULL'.lit, *values)
|
683
|
+
end
|
684
|
+
|
685
|
+
# PostgreSQL is smart and can use parantheses around all datasets to get
|
686
|
+
# the correct answers.
|
687
|
+
def select_compounds_sql(sql, opts)
|
688
|
+
return unless opts[:compounds]
|
689
|
+
opts[:compounds].each do |type, dataset, all|
|
690
|
+
sql.replace("(#{sql} #{type.to_s.upcase}#{' ALL' if all} #{subselect_sql(dataset)})")
|
691
|
+
end
|
692
|
+
end
|
693
|
+
|
694
|
+
# The order of clauses in the SELECT SQL statement
|
695
|
+
def select_clause_order
|
696
|
+
SELECT_CLAUSE_ORDER
|
697
|
+
end
|
698
|
+
|
699
|
+
# Support lock mode, allowing FOR SHARE and FOR UPDATE queries.
|
700
|
+
def select_lock_sql(sql, opts)
|
701
|
+
case opts[:lock]
|
702
|
+
when :update
|
703
|
+
sql << FOR_UPDATE
|
704
|
+
when :share
|
705
|
+
sql << FOR_SHARE
|
706
|
+
end
|
707
|
+
end
|
708
|
+
|
709
|
+
# The version of the database server
|
710
|
+
def server_version
|
711
|
+
db.server_version(@opts[:server])
|
712
|
+
end
|
713
|
+
end
|
714
|
+
end
|
715
|
+
end
|