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,491 @@
|
|
1
|
+
require 'java'
|
2
|
+
require 'sequel_core/dataset/stored_procedures'
|
3
|
+
|
4
|
+
module Sequel
|
5
|
+
# Houses Sequel's JDBC support when running on JRuby.
|
6
|
+
# Support for individual database types is done using sub adapters.
|
7
|
+
# PostgreSQL, MySQL, SQLite, Oracle, and MSSQL all have relatively good support,
|
8
|
+
# close the the level supported by the native adapter.
|
9
|
+
# PostgreSQL, MySQL, SQLite can load necessary support using
|
10
|
+
# the jdbc-* gem, if it is installed, though they will work if you
|
11
|
+
# have the correct .jar in your CLASSPATH. Oracle and MSSQL should
|
12
|
+
# load the necessary support if you have the .jar in your CLASSPATH.
|
13
|
+
# For all other databases, the Java class should be loaded manually
|
14
|
+
# before calling Sequel.connect.
|
15
|
+
#
|
16
|
+
# Note that when using a JDBC adapter, the best way to use Sequel
|
17
|
+
# is via Sequel.connect, NOT Sequel.jdbc. Use the JDBC connection
|
18
|
+
# string when connecting, which will be in a different format than
|
19
|
+
# the native connection string. The connection string should start
|
20
|
+
# with 'jdbc:'. For PostgreSQL, use 'jdbc:postgresql:', and for
|
21
|
+
# SQLite you do not need 2 preceding slashes for the database name
|
22
|
+
# (use no preceding slashes for a relative path, and one preceding
|
23
|
+
# slash for an absolute path).
|
24
|
+
module JDBC
|
25
|
+
# Make it accesing the java.lang hierarchy more ruby friendly.
|
26
|
+
module JavaLang
|
27
|
+
include_package 'java.lang'
|
28
|
+
end
|
29
|
+
|
30
|
+
# Make it accesing the java.sql hierarchy more ruby friendly.
|
31
|
+
module JavaSQL
|
32
|
+
include_package 'java.sql'
|
33
|
+
end
|
34
|
+
|
35
|
+
# Contains procs keyed on sub adapter type that extend the
|
36
|
+
# given database object so it supports the correct database type.
|
37
|
+
DATABASE_SETUP = {:postgresql=>proc do |db|
|
38
|
+
require 'sequel_core/adapters/jdbc/postgresql'
|
39
|
+
db.extend(Sequel::JDBC::Postgres::DatabaseMethods)
|
40
|
+
JDBC.load_gem('postgres')
|
41
|
+
org.postgresql.Driver
|
42
|
+
end,
|
43
|
+
:mysql=>proc do |db|
|
44
|
+
require 'sequel_core/adapters/jdbc/mysql'
|
45
|
+
db.extend(Sequel::JDBC::MySQL::DatabaseMethods)
|
46
|
+
JDBC.load_gem('mysql')
|
47
|
+
com.mysql.jdbc.Driver
|
48
|
+
end,
|
49
|
+
:sqlite=>proc do |db|
|
50
|
+
require 'sequel_core/adapters/jdbc/sqlite'
|
51
|
+
db.extend(Sequel::JDBC::SQLite::DatabaseMethods)
|
52
|
+
JDBC.load_gem('sqlite3')
|
53
|
+
org.sqlite.JDBC
|
54
|
+
end,
|
55
|
+
:oracle=>proc do |db|
|
56
|
+
require 'sequel_core/adapters/jdbc/oracle'
|
57
|
+
db.extend(Sequel::JDBC::Oracle::DatabaseMethods)
|
58
|
+
Java::oracle.jdbc.driver.OracleDriver
|
59
|
+
end,
|
60
|
+
:sqlserver=>proc do |db|
|
61
|
+
require 'sequel_core/adapters/shared/mssql'
|
62
|
+
db.extend(Sequel::MSSQL::DatabaseMethods)
|
63
|
+
com.microsoft.sqlserver.jdbc.SQLServerDriver
|
64
|
+
end,
|
65
|
+
:h2=>proc do |db|
|
66
|
+
require 'sequel_core/adapters/jdbc/h2'
|
67
|
+
db.extend(Sequel::JDBC::H2::DatabaseMethods)
|
68
|
+
JDBC.load_gem('h2')
|
69
|
+
org.h2.Driver
|
70
|
+
end
|
71
|
+
}
|
72
|
+
|
73
|
+
# Allowing loading the necessary JDBC support via a gem, which
|
74
|
+
# works for PostgreSQL, MySQL, and SQLite.
|
75
|
+
def self.load_gem(name)
|
76
|
+
begin
|
77
|
+
require "jdbc/#{name}"
|
78
|
+
rescue LoadError
|
79
|
+
# jdbc gem not used, hopefully the user has the .jar in their CLASSPATH
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
# JDBC Databases offer a fairly uniform interface that does not change
|
84
|
+
# much based on the sub adapter.
|
85
|
+
class Database < Sequel::Database
|
86
|
+
set_adapter_scheme :jdbc
|
87
|
+
|
88
|
+
# The type of database we are connecting to
|
89
|
+
attr_reader :database_type
|
90
|
+
|
91
|
+
# Call the DATABASE_SETUP proc directly after initialization,
|
92
|
+
# so the object always uses sub adapter specific code. Also,
|
93
|
+
# raise an error immediately if the connection doesn't have a
|
94
|
+
# uri, since JDBC requires one.
|
95
|
+
def initialize(opts)
|
96
|
+
@opts = opts
|
97
|
+
raise(Error, "No connection string specified") unless uri
|
98
|
+
if match = /\Ajdbc:([^:]+)/.match(uri) and prok = DATABASE_SETUP[match[1].to_sym]
|
99
|
+
prok.call(self)
|
100
|
+
end
|
101
|
+
super(opts)
|
102
|
+
end
|
103
|
+
|
104
|
+
# Execute the given stored procedure with the give name. If a block is
|
105
|
+
# given, the stored procedure should return rows.
|
106
|
+
def call_sproc(name, opts = {})
|
107
|
+
args = opts[:args] || []
|
108
|
+
sql = "{call #{name}(#{args.map{'?'}.join(',')})}"
|
109
|
+
synchronize(opts[:server]) do |conn|
|
110
|
+
cps = conn.prepareCall(sql)
|
111
|
+
|
112
|
+
i = 0
|
113
|
+
args.each{|arg| set_ps_arg(cps, arg, i+=1)}
|
114
|
+
|
115
|
+
begin
|
116
|
+
if block_given?
|
117
|
+
yield cps.executeQuery
|
118
|
+
else
|
119
|
+
case opts[:type]
|
120
|
+
when :insert
|
121
|
+
cps.executeUpdate
|
122
|
+
last_insert_id(conn, opts)
|
123
|
+
else
|
124
|
+
cps.executeUpdate
|
125
|
+
end
|
126
|
+
end
|
127
|
+
rescue NativeException, JavaSQL::SQLException => e
|
128
|
+
raise_error(e)
|
129
|
+
ensure
|
130
|
+
cps.close
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
# Connect to the database using JavaSQL::DriverManager.getConnection.
|
136
|
+
def connect(server)
|
137
|
+
setup_connection(JavaSQL::DriverManager.getConnection(uri(server_opts(server))))
|
138
|
+
end
|
139
|
+
|
140
|
+
# Return instances of JDBC::Dataset with the given opts.
|
141
|
+
def dataset(opts = nil)
|
142
|
+
JDBC::Dataset.new(self, opts)
|
143
|
+
end
|
144
|
+
|
145
|
+
# Execute the given SQL. If a block is given, if should be a SELECT
|
146
|
+
# statement or something else that returns rows.
|
147
|
+
def execute(sql, opts={}, &block)
|
148
|
+
return call_sproc(sql, opts, &block) if opts[:sproc]
|
149
|
+
return execute_prepared_statement(sql, opts, &block) if sql.is_one_of?(Symbol, Dataset)
|
150
|
+
log_info(sql)
|
151
|
+
synchronize(opts[:server]) do |conn|
|
152
|
+
stmt = conn.createStatement
|
153
|
+
begin
|
154
|
+
if block_given?
|
155
|
+
yield stmt.executeQuery(sql)
|
156
|
+
else
|
157
|
+
case opts[:type]
|
158
|
+
when :ddl
|
159
|
+
stmt.execute(sql)
|
160
|
+
when :insert
|
161
|
+
stmt.executeUpdate(sql)
|
162
|
+
last_insert_id(conn, opts)
|
163
|
+
else
|
164
|
+
stmt.executeUpdate(sql)
|
165
|
+
end
|
166
|
+
end
|
167
|
+
rescue NativeException, JavaSQL::SQLException => e
|
168
|
+
raise_error(e)
|
169
|
+
ensure
|
170
|
+
stmt.close
|
171
|
+
end
|
172
|
+
end
|
173
|
+
end
|
174
|
+
alias execute_dui execute
|
175
|
+
|
176
|
+
# Execute the given DDL SQL, which should not return any
|
177
|
+
# values or rows.
|
178
|
+
def execute_ddl(sql, opts={})
|
179
|
+
execute(sql, {:type=>:ddl}.merge(opts))
|
180
|
+
end
|
181
|
+
|
182
|
+
# Execute the given INSERT SQL, returning the last inserted
|
183
|
+
# row id.
|
184
|
+
def execute_insert(sql, opts={})
|
185
|
+
execute(sql, {:type=>:insert}.merge(opts))
|
186
|
+
end
|
187
|
+
|
188
|
+
# All tables in this database
|
189
|
+
def tables
|
190
|
+
ts = []
|
191
|
+
ds = dataset
|
192
|
+
metadata(:getTables, nil, nil, nil, ['TABLE'].to_java(:string)){|h| ts << ds.send(:output_identifier, h[:table_name])}
|
193
|
+
ts
|
194
|
+
end
|
195
|
+
|
196
|
+
# Default transaction method that should work on most JDBC
|
197
|
+
# databases. Does not use the JDBC transaction methods, uses
|
198
|
+
# SQL BEGIN/ROLLBACK/COMMIT statements instead.
|
199
|
+
def transaction(server=nil)
|
200
|
+
synchronize(server) do |conn|
|
201
|
+
return yield(conn) if @transactions.include?(Thread.current)
|
202
|
+
stmt = conn.createStatement
|
203
|
+
begin
|
204
|
+
log_info(begin_transaction_sql)
|
205
|
+
stmt.execute(begin_transaction_sql)
|
206
|
+
@transactions << Thread.current
|
207
|
+
yield(conn)
|
208
|
+
rescue Exception => e
|
209
|
+
log_info(rollback_transaction_sql)
|
210
|
+
stmt.execute(rollback_transaction_sql)
|
211
|
+
transaction_error(e)
|
212
|
+
ensure
|
213
|
+
unless e
|
214
|
+
log_info(commit_transaction_sql)
|
215
|
+
stmt.execute(commit_transaction_sql)
|
216
|
+
end
|
217
|
+
stmt.close
|
218
|
+
@transactions.delete(Thread.current)
|
219
|
+
end
|
220
|
+
end
|
221
|
+
end
|
222
|
+
|
223
|
+
# The uri for this connection. You can specify the uri
|
224
|
+
# using the :uri, :url, or :database options. You don't
|
225
|
+
# need to worry about this if you use Sequel.connect
|
226
|
+
# with the JDBC connectrion strings.
|
227
|
+
def uri(opts={})
|
228
|
+
opts = @opts.merge(opts)
|
229
|
+
ur = opts[:uri] || opts[:url] || opts[:database]
|
230
|
+
ur =~ /^\Ajdbc:/ ? ur : "jdbc:#{ur}"
|
231
|
+
end
|
232
|
+
|
233
|
+
private
|
234
|
+
|
235
|
+
# The JDBC adapter should not need the pool to convert exceptions.
|
236
|
+
def connection_pool_default_options
|
237
|
+
super.merge(:pool_convert_exceptions=>false)
|
238
|
+
end
|
239
|
+
|
240
|
+
# Close given adapter connections
|
241
|
+
def disconnect_connection(c)
|
242
|
+
c.close
|
243
|
+
end
|
244
|
+
|
245
|
+
# Execute the prepared statement. If the provided name is a
|
246
|
+
# dataset, use that as the prepared statement, otherwise use
|
247
|
+
# it as a key to look it up in the prepared_statements hash.
|
248
|
+
# If the connection we are using has already prepared an identical
|
249
|
+
# statement, use that statement instead of creating another.
|
250
|
+
# Otherwise, prepare a new statement for the connection, bind the
|
251
|
+
# variables, and execute it.
|
252
|
+
def execute_prepared_statement(name, opts={})
|
253
|
+
args = opts[:arguments]
|
254
|
+
if Dataset === name
|
255
|
+
ps = name
|
256
|
+
name = ps.prepared_statement_name
|
257
|
+
else
|
258
|
+
ps = prepared_statements[name]
|
259
|
+
end
|
260
|
+
sql = ps.prepared_sql
|
261
|
+
synchronize(opts[:server]) do |conn|
|
262
|
+
if name and cps = conn.prepared_statements[name] and cps[0] == sql
|
263
|
+
cps = cps[1]
|
264
|
+
else
|
265
|
+
if cps
|
266
|
+
log_info("Closing #{name}")
|
267
|
+
cps[1].close
|
268
|
+
end
|
269
|
+
log_info("Preparing#{" #{name}:" if name} #{sql}")
|
270
|
+
cps = conn.prepareStatement(sql)
|
271
|
+
conn.prepared_statements[name] = [sql, cps] if name
|
272
|
+
end
|
273
|
+
i = 0
|
274
|
+
args.each{|arg| set_ps_arg(cps, arg, i+=1)}
|
275
|
+
log_info("Executing#{" #{name}" if name}", args)
|
276
|
+
begin
|
277
|
+
if block_given?
|
278
|
+
yield cps.executeQuery
|
279
|
+
else
|
280
|
+
case opts[:type]
|
281
|
+
when :ddl
|
282
|
+
cps.execute
|
283
|
+
when :insert
|
284
|
+
cps.executeUpdate
|
285
|
+
last_insert_id(conn, opts)
|
286
|
+
else
|
287
|
+
cps.executeUpdate
|
288
|
+
end
|
289
|
+
end
|
290
|
+
rescue NativeException, JavaSQL::SQLException => e
|
291
|
+
raise_error(e)
|
292
|
+
ensure
|
293
|
+
cps.close unless name
|
294
|
+
end
|
295
|
+
end
|
296
|
+
end
|
297
|
+
|
298
|
+
# By default, there is no support for determining the last inserted
|
299
|
+
# id, so return nil. This method should be overridden in
|
300
|
+
# sub adapters.
|
301
|
+
def last_insert_id(conn, opts)
|
302
|
+
nil
|
303
|
+
end
|
304
|
+
|
305
|
+
# Yield the metadata for this database
|
306
|
+
def metadata(*args, &block)
|
307
|
+
ds = dataset
|
308
|
+
ds.identifier_output_method = :downcase
|
309
|
+
synchronize{|c| ds.send(:process_result_set, c.getMetaData.send(*args), &block)}
|
310
|
+
end
|
311
|
+
|
312
|
+
# Java being java, you need to specify the type of each argument
|
313
|
+
# for the prepared statement, and bind it individually. This
|
314
|
+
# guesses which JDBC method to use, and hopefully JRuby will convert
|
315
|
+
# things properly for us.
|
316
|
+
def set_ps_arg(cps, arg, i)
|
317
|
+
case arg
|
318
|
+
when Integer
|
319
|
+
cps.setInt(i, arg)
|
320
|
+
when String
|
321
|
+
cps.setString(i, arg)
|
322
|
+
when Date, Java::JavaSql::Date
|
323
|
+
cps.setDate(i, arg)
|
324
|
+
when Time, DateTime, Java::JavaSql::Timestamp
|
325
|
+
cps.setTimestamp(i, arg)
|
326
|
+
when Float
|
327
|
+
cps.setDouble(i, arg)
|
328
|
+
when nil
|
329
|
+
cps.setNull(i, JavaSQL::Types::NULL)
|
330
|
+
end
|
331
|
+
end
|
332
|
+
|
333
|
+
# Add a prepared_statements accessor to the connection,
|
334
|
+
# and set it to an empty hash. This is used to store
|
335
|
+
# adapter specific prepared statements.
|
336
|
+
def setup_connection(conn)
|
337
|
+
conn.meta_eval{attr_accessor :prepared_statements}
|
338
|
+
conn.prepared_statements = {}
|
339
|
+
conn
|
340
|
+
end
|
341
|
+
|
342
|
+
# All tables in this database
|
343
|
+
def schema_parse_table(table, opts={})
|
344
|
+
ds = dataset
|
345
|
+
schema, table = schema_and_table(table)
|
346
|
+
schema = ds.send(:input_identifier, schema) if schema
|
347
|
+
table = ds.send(:input_identifier, table)
|
348
|
+
pks, ts = [], []
|
349
|
+
metadata(:getPrimaryKeys, nil, schema, table) do |h|
|
350
|
+
pks << h[:column_name]
|
351
|
+
end
|
352
|
+
metadata(:getColumns, nil, schema, table, nil) do |h|
|
353
|
+
ts << [ds.send(:output_identifier, h[:column_name]), {:type=>schema_column_type(h[:type_name]), :db_type=>h[:type_name], :default=>(h[:column_def] == '' ? nil : h[:column_def]), :allow_null=>(h[:nullable] != 0), :primary_key=>pks.include?(h[:column_name])}]
|
354
|
+
end
|
355
|
+
ts
|
356
|
+
end
|
357
|
+
end
|
358
|
+
|
359
|
+
class Dataset < Sequel::Dataset
|
360
|
+
include StoredProcedures
|
361
|
+
|
362
|
+
# Use JDBC PreparedStatements instead of emulated ones. Statements
|
363
|
+
# created using #prepare are cached at the connection level to allow
|
364
|
+
# reuse. This also supports bind variables by using unnamed
|
365
|
+
# prepared statements created using #call.
|
366
|
+
module PreparedStatementMethods
|
367
|
+
include Sequel::Dataset::UnnumberedArgumentMapper
|
368
|
+
|
369
|
+
private
|
370
|
+
|
371
|
+
# Execute the prepared SQL using the stored type and
|
372
|
+
# arguments derived from the hash passed to call.
|
373
|
+
def execute(sql, opts={}, &block)
|
374
|
+
super(self, {:arguments=>bind_arguments, :type=>sql_query_type}.merge(opts), &block)
|
375
|
+
end
|
376
|
+
|
377
|
+
# Same as execute, explicit due to intricacies of alias and super.
|
378
|
+
def execute_dui(sql, opts={}, &block)
|
379
|
+
super(self, {:arguments=>bind_arguments, :type=>sql_query_type}.merge(opts), &block)
|
380
|
+
end
|
381
|
+
|
382
|
+
# Same as execute, explicit due to intricacies of alias and super.
|
383
|
+
def execute_insert(sql, opts={}, &block)
|
384
|
+
super(self, {:arguments=>bind_arguments, :type=>sql_query_type}.merge(opts), &block)
|
385
|
+
end
|
386
|
+
end
|
387
|
+
|
388
|
+
# Use JDBC CallableStatements to execute stored procedures. Only supported
|
389
|
+
# if the underlying database has stored procedure support.
|
390
|
+
module StoredProcedureMethods
|
391
|
+
include Sequel::Dataset::StoredProcedureMethods
|
392
|
+
|
393
|
+
private
|
394
|
+
|
395
|
+
# Execute the database stored procedure with the stored arguments.
|
396
|
+
def execute(sql, opts={}, &block)
|
397
|
+
super(@sproc_name, {:args=>@sproc_args, :sproc=>true, :type=>sql_query_type}.merge(opts), &block)
|
398
|
+
end
|
399
|
+
|
400
|
+
# Same as execute, explicit due to intricacies of alias and super.
|
401
|
+
def execute_dui(sql, opts={}, &block)
|
402
|
+
super(@sproc_name, {:args=>@sproc_args, :sproc=>true, :type=>sql_query_type}.merge(opts), &block)
|
403
|
+
end
|
404
|
+
|
405
|
+
# Same as execute, explicit due to intricacies of alias and super.
|
406
|
+
def execute_insert(sql, opts={}, &block)
|
407
|
+
super(@sproc_name, {:args=>@sproc_args, :sproc=>true, :type=>sql_query_type}.merge(opts), &block)
|
408
|
+
end
|
409
|
+
end
|
410
|
+
|
411
|
+
# Correctly return rows from the database and return them as hashes.
|
412
|
+
def fetch_rows(sql, &block)
|
413
|
+
execute(sql){|result| process_result_set(result, &block)}
|
414
|
+
self
|
415
|
+
end
|
416
|
+
|
417
|
+
# Use the ISO values for dates and times.
|
418
|
+
def literal(v)
|
419
|
+
case v
|
420
|
+
when Time
|
421
|
+
literal(v.iso8601)
|
422
|
+
when Date, DateTime, Java::JavaSql::Timestamp, Java::JavaSql::Date
|
423
|
+
literal(v.to_s)
|
424
|
+
else
|
425
|
+
super
|
426
|
+
end
|
427
|
+
end
|
428
|
+
|
429
|
+
# Create a named prepared statement that is stored in the
|
430
|
+
# database (and connection) for reuse.
|
431
|
+
def prepare(type, name=nil, values=nil)
|
432
|
+
ps = to_prepared_statement(type, values)
|
433
|
+
ps.extend(PreparedStatementMethods)
|
434
|
+
if name
|
435
|
+
ps.prepared_statement_name = name
|
436
|
+
db.prepared_statements[name] = ps
|
437
|
+
end
|
438
|
+
ps
|
439
|
+
end
|
440
|
+
|
441
|
+
private
|
442
|
+
|
443
|
+
# Convert the type. Used for converting Java types to ruby types.
|
444
|
+
def convert_type(v)
|
445
|
+
case v
|
446
|
+
when Java::JavaSQL::Timestamp, Java::JavaSQL::Time
|
447
|
+
v.to_string.to_sequel_time
|
448
|
+
when Java::JavaSQL::Date
|
449
|
+
v.to_string.to_date
|
450
|
+
when Java::JavaIo::BufferedReader
|
451
|
+
lines = []
|
452
|
+
while(line = v.read_line) do lines << line end
|
453
|
+
lines.join("\n")
|
454
|
+
when Java::JavaMath::BigDecimal
|
455
|
+
BigDecimal.new(v.to_string)
|
456
|
+
else
|
457
|
+
v
|
458
|
+
end
|
459
|
+
end
|
460
|
+
|
461
|
+
# Extend the dataset with the JDBC stored procedure methods.
|
462
|
+
def prepare_extend_sproc(ds)
|
463
|
+
ds.extend(StoredProcedureMethods)
|
464
|
+
end
|
465
|
+
|
466
|
+
# Split out from fetch rows to allow processing of JDBC result sets
|
467
|
+
# that don't come from issuing an SQL string.
|
468
|
+
def process_result_set(result)
|
469
|
+
# get column names
|
470
|
+
meta = result.getMetaData
|
471
|
+
column_count = meta.getColumnCount
|
472
|
+
@columns = []
|
473
|
+
column_count.times{|i| @columns << output_identifier(meta.getColumnName(i+1))}
|
474
|
+
|
475
|
+
# get rows
|
476
|
+
while result.next
|
477
|
+
row = {}
|
478
|
+
@columns.each_with_index{|v, i| row[v] = convert_type(result.getObject(i+1))}
|
479
|
+
yield row
|
480
|
+
end
|
481
|
+
end
|
482
|
+
end
|
483
|
+
end
|
484
|
+
end
|
485
|
+
|
486
|
+
class Java::JavaSQL::Timestamp
|
487
|
+
# Add a usec method in order to emulate Time values.
|
488
|
+
def usec
|
489
|
+
getNanos/1000
|
490
|
+
end
|
491
|
+
end
|