Class Sequel::JDBC::Database
In: lib/sequel/adapters/jdbc.rb
Parent: Sequel::Database

Methods

Attributes

basic_type_convertor_map  [R]  Map of JDBC type ids to callable objects that return appropriate ruby or java values.
convert_types  [RW]  Whether to convert some Java types to ruby types when retrieving rows. True by default, can be set to false to roughly double performance when fetching rows.
driver  [R]  The Java database driver we are using (should be a Java class)
fetch_size  [RW]  The fetch size to use for JDBC Statement objects created by this database. By default, this is nil so a fetch size is not set explicitly.
type_convertor_map  [R]  Map of JDBC type ids to callable objects that return appropriate ruby values.

Public Instance methods

Execute the given stored procedure with the give name. If a block is given, the stored procedure should return rows.

[Source]

     # File lib/sequel/adapters/jdbc.rb, line 153
153:       def call_sproc(name, opts = OPTS)
154:         args = opts[:args] || []
155:         sql = "{call #{name}(#{args.map{'?'}.join(',')})}"
156:         synchronize(opts[:server]) do |conn|
157:           cps = conn.prepareCall(sql)
158: 
159:           i = 0
160:           args.each{|arg| set_ps_arg(cps, arg, i+=1)}
161: 
162:           begin
163:             if block_given?
164:               yield log_connection_yield(sql, conn){cps.executeQuery}
165:             else
166:               log_connection_yield(sql, conn){cps.executeUpdate}
167:               if opts[:type] == :insert
168:                 last_insert_id(conn, opts)
169:               end
170:             end
171:           rescue NativeException, JavaSQL::SQLException => e
172:             raise_error(e)
173:           ensure
174:             cps.close
175:           end
176:         end
177:       end

Connect to the database using JavaSQL::DriverManager.getConnection, and falling back to driver.new.connect if the driver is known.

[Source]

     # File lib/sequel/adapters/jdbc.rb, line 181
181:       def connect(server)
182:         opts = server_opts(server)
183:         conn = if jndi?
184:           get_connection_from_jndi
185:         else
186:           args = [uri(opts)]
187:           args.concat([opts[:user], opts[:password]]) if opts[:user] && opts[:password]
188:           begin
189:             JavaSQL::DriverManager.setLoginTimeout(opts[:login_timeout]) if opts[:login_timeout]
190:             raise StandardError, "skipping regular connection" if opts[:jdbc_properties]
191:             JavaSQL::DriverManager.getConnection(*args)
192:           rescue JavaSQL::SQLException, NativeException, StandardError => e
193:             raise e unless driver
194:             # If the DriverManager can't get the connection - use the connect
195:             # method of the driver. (This happens under Tomcat for instance)
196:             props = java.util.Properties.new
197:             if opts && opts[:user] && opts[:password]
198:               props.setProperty("user", opts[:user])
199:               props.setProperty("password", opts[:password])
200:             end
201:             opts[:jdbc_properties].each{|k,v| props.setProperty(k.to_s, v)} if opts[:jdbc_properties]
202:             begin
203:               c = driver.new.connect(args[0], props)
204:               raise(Sequel::DatabaseError, 'driver.new.connect returned nil: probably bad JDBC connection string') unless c
205:               c
206:             rescue JavaSQL::SQLException, NativeException, StandardError => e2
207:               if e2.respond_to?(:message=) && e2.message != e.message
208:                 e2.message = "#{e2.message}\n#{e.class.name}: #{e.message}"
209:               end
210:               raise e2
211:             end
212:           end
213:         end
214:         setup_connection(conn)
215:       end

Close given adapter connections, and delete any related prepared statements.

[Source]

     # File lib/sequel/adapters/jdbc.rb, line 218
218:       def disconnect_connection(c)
219:         @connection_prepared_statements_mutex.synchronize{@connection_prepared_statements.delete(c)}
220:         c.close
221:       end

[Source]

     # File lib/sequel/adapters/jdbc.rb, line 223
223:       def execute(sql, opts=OPTS, &block)
224:         return call_sproc(sql, opts, &block) if opts[:sproc]
225:         return execute_prepared_statement(sql, opts, &block) if [Symbol, Dataset].any?{|c| sql.is_a?(c)}
226:         synchronize(opts[:server]) do |conn|
227:           statement(conn) do |stmt|
228:             if block
229:               if size = fetch_size
230:                 stmt.setFetchSize(size)
231:               end
232:               yield log_connection_yield(sql, conn){stmt.executeQuery(sql)}
233:             else
234:               case opts[:type]
235:               when :ddl
236:                 log_connection_yield(sql, conn){stmt.execute(sql)}
237:               when :insert
238:                 log_connection_yield(sql, conn){execute_statement_insert(stmt, sql)}
239:                 last_insert_id(conn, Hash[opts].merge!(:stmt=>stmt))
240:               else
241:                 log_connection_yield(sql, conn){stmt.executeUpdate(sql)}
242:               end
243:             end
244:           end
245:         end
246:       end

[Source]

     # File lib/sequel/adapters/jdbc.rb, line 249
249:       def execute_ddl(sql, opts=OPTS)
250:         opts = Hash[opts]
251:         opts[:type] = :ddl
252:         execute(sql, opts)
253:       end
execute_dui(sql, opts=OPTS, &block)

Alias for execute

[Source]

     # File lib/sequel/adapters/jdbc.rb, line 255
255:       def execute_insert(sql, opts=OPTS)
256:         opts = Hash[opts]
257:         opts[:type] = :insert
258:         execute(sql, opts)
259:       end

Use the JDBC metadata to get a list of foreign keys for the table.

[Source]

     # File lib/sequel/adapters/jdbc.rb, line 268
268:       def foreign_key_list(table, opts=OPTS)
269:         m = output_identifier_meth
270:         schema, table = metadata_schema_and_table(table, opts)
271:         foreign_keys = {}
272:         metadata(:getImportedKeys, nil, schema, table) do |r|
273:           if fk = foreign_keys[r[:fk_name]]
274:             fk[:columns] << [r[:key_seq], m.call(r[:fkcolumn_name])]
275:             fk[:key] << [r[:key_seq], m.call(r[:pkcolumn_name])]
276:           elsif r[:fk_name]
277:             foreign_keys[r[:fk_name]] = {:name=>m.call(r[:fk_name]), :columns=>[[r[:key_seq], m.call(r[:fkcolumn_name])]], :table=>m.call(r[:pktable_name]), :key=>[[r[:key_seq], m.call(r[:pkcolumn_name])]]}
278:           end
279:         end
280:         foreign_keys.values.each do |fk|
281:           [:columns, :key].each do |k|
282:             fk[k] = fk[k].sort.map{|_, v| v}
283:           end
284:         end
285:       end

[Source]

     # File lib/sequel/adapters/jdbc.rb, line 261
261:       def freeze
262:         @type_convertor_map.freeze
263:         @basic_type_convertor_map.freeze
264:         super
265:       end

Use the JDBC metadata to get the index information for the table.

[Source]

     # File lib/sequel/adapters/jdbc.rb, line 288
288:       def indexes(table, opts=OPTS)
289:         m = output_identifier_meth
290:         schema, table = metadata_schema_and_table(table, opts)
291:         indexes = {}
292:         metadata(:getIndexInfo, nil, schema, table, false, true) do |r|
293:           next unless name = r[:column_name]
294:           next if respond_to?(:primary_key_index_re, true) and r[:index_name] =~ primary_key_index_re 
295:           i = indexes[m.call(r[:index_name])] ||= {:columns=>[], :unique=>[false, 0].include?(r[:non_unique])}
296:           i[:columns] << m.call(name)
297:         end
298:         indexes
299:       end

Whether or not JNDI is being used for this connection.

[Source]

     # File lib/sequel/adapters/jdbc.rb, line 302
302:       def jndi?
303:         !!(uri =~ JNDI_URI_REGEXP)
304:       end

All tables in this database

[Source]

     # File lib/sequel/adapters/jdbc.rb, line 307
307:       def tables(opts=OPTS)
308:         get_tables('TABLE', opts)
309:       end

The uri for this connection. You can specify the uri using the :uri, :url, or :database options. You don‘t need to worry about this if you use Sequel.connect with the JDBC connectrion strings.

[Source]

     # File lib/sequel/adapters/jdbc.rb, line 315
315:       def uri(opts=OPTS)
316:         opts = @opts.merge(opts)
317:         ur = opts[:uri] || opts[:url] || opts[:database]
318:         ur =~ /^\Ajdbc:/ ? ur : "jdbc:#{ur}"
319:       end

All views in this database

[Source]

     # File lib/sequel/adapters/jdbc.rb, line 322
322:       def views(opts=OPTS)
323:         get_tables('VIEW', opts)
324:       end

[Validate]