Class: Sequel::JDBC::Database
- Defined in:
- lib/sequel/adapters/jdbc.rb,
lib/sequel/adapters/jdbc/db2.rb,
lib/sequel/adapters/jdbc/mssql.rb
Overview
JDBC Databases offer a fairly uniform interface that does not change much based on the sub adapter.
Constant Summary collapse
- DatasetClass =
self
Constants inherited from Database
Database::ADAPTERS, Database::AUTOINCREMENT, Database::COLUMN_DEFINITION_ORDER, Database::COMMA_SEPARATOR, Database::DEFAULT_JOIN_TABLE_COLUMN_OPTIONS, Database::MSSQL_DEFAULT_RE, Database::MYSQL_TIMESTAMP_RE, Database::NOT_NULL, Database::NULL, Database::POSTGRES_DEFAULT_RE, Database::PRIMARY_KEY, Database::SQL_BEGIN, Database::SQL_COMMIT, Database::SQL_RELEASE_SAVEPOINT, Database::SQL_ROLLBACK, Database::SQL_ROLLBACK_TO_SAVEPOINT, Database::SQL_SAVEPOINT, Database::STRING_DEFAULT_RE, Database::TEMPORARY, Database::TRANSACTION_BEGIN, Database::TRANSACTION_COMMIT, Database::TRANSACTION_ISOLATION_LEVELS, Database::TRANSACTION_ROLLBACK, Database::UNDERSCORE, Database::UNIQUE, Database::UNSIGNED
Instance Attribute Summary collapse
-
#convert_types ⇒ Object
Whether to convert some Java types to ruby types when retrieving rows.
-
#database_type ⇒ Object
readonly
The type of database we are connecting to.
-
#driver ⇒ Object
readonly
The Java database driver we are using.
Attributes inherited from Database
#cache_schema, #dataset_class, #default_schema, #log_warn_duration, #loggers, #opts, #pool, #prepared_statements, #sql_log_level, #timezone, #transaction_isolation_level
Instance Method Summary collapse
-
#call_sproc(name, opts = {}) ⇒ Object
Execute the given stored procedure with the give name.
-
#connect(server) ⇒ Object
Connect to the database using JavaSQL::DriverManager.getConnection.
-
#execute(sql, opts = {}, &block) ⇒ Object
(also: #execute_dui)
Execute the given SQL.
-
#execute_ddl(sql, opts = {}) ⇒ Object
Execute the given DDL SQL, which should not return any values or rows.
-
#execute_insert(sql, opts = {}) ⇒ Object
Execute the given INSERT SQL, returning the last inserted row id.
-
#indexes(table, opts = {}) ⇒ Object
(also: #jdbc_indexes)
Use the JDBC metadata to get the index information for the table.
-
#initialize(opts) ⇒ Database
constructor
Call the DATABASE_SETUP proc directly after initialization, so the object always uses sub adapter specific code.
-
#jndi? ⇒ Boolean
Whether or not JNDI is being used for this connection.
-
#tables(opts = {}) ⇒ Object
(also: #jdbc_tables)
All tables in this database.
-
#uri(opts = {}) ⇒ Object
The uri for this connection.
-
#views(opts = {}) ⇒ Object
(also: #jdbc_views)
All views in this database.
Methods inherited from Database
#<<, #[], adapter_class, #adapter_scheme, adapter_scheme, #add_column, #add_index, #add_servers, #after_commit, #after_rollback, #alter_table, #call, #cast_type_literal, connect, #create_join_table, #create_or_replace_view, #create_table, #create_table!, #create_table?, #create_view, #dataset, #disconnect, #drop_column, #drop_index, #drop_join_table, #drop_table, #drop_table?, #drop_view, #dump_foreign_key_migration, #dump_indexes_migration, #dump_schema_cache, #dump_schema_cache?, #dump_schema_migration, #dump_table_schema, #each_server, #extend_datasets, #fetch, #foreign_key_list, #from, #from_application_timestamp, #get, #identifier_input_method, identifier_input_method, #identifier_input_method=, identifier_input_method=, identifier_output_method, #identifier_output_method, identifier_output_method=, #identifier_output_method=, #in_transaction?, #inspect, #literal, #load_schema_cache, #load_schema_cache?, #log_exception, #log_info, #log_yield, #logger=, #prepared_statement, #query, #quote_identifiers=, quote_identifiers=, #quote_identifiers?, #remove_servers, #rename_column, #rename_table, #run, #schema, #select, #serial_primary_key_options, #servers, #set_column_default, #set_column_type, #set_prepared_statement, single_threaded=, #single_threaded?, #supports_create_table_if_not_exists?, #supports_drop_table_if_exists?, #supports_prepared_transactions?, #supports_savepoints?, #supports_savepoints_in_prepared_transactions?, #supports_transaction_isolation_levels?, #supports_transactional_ddl?, #synchronize, #table_exists?, #test_connection, #to_application_timestamp, #transaction, #typecast_value, #url
Methods included from Metaprogramming
Constructor Details
#initialize(opts) ⇒ Database
Call the DATABASE_SETUP proc directly after initialization, so the object always uses sub adapter specific code. Also, raise an error immediately if the connection doesn’t have a uri, since JDBC requires one.
152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/sequel/adapters/jdbc.rb', line 152 def initialize(opts) super @connection_prepared_statements = {} @connection_prepared_statements_mutex = Mutex.new @convert_types = typecast_value_boolean(@opts.fetch(:convert_types, true)) raise(Error, "No connection string specified") unless uri resolved_uri = jndi? ? get_uri_from_jndi : uri if match = /\Ajdbc:([^:]+)/.match(resolved_uri) and prok = DATABASE_SETUP[match[1].to_sym] @driver = prok.call(self) end end |
Instance Attribute Details
#convert_types ⇒ Object
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.
146 147 148 |
# File 'lib/sequel/adapters/jdbc.rb', line 146 def convert_types @convert_types end |
#database_type ⇒ Object (readonly)
The type of database we are connecting to
138 139 140 |
# File 'lib/sequel/adapters/jdbc.rb', line 138 def database_type @database_type end |
#driver ⇒ Object (readonly)
The Java database driver we are using
141 142 143 |
# File 'lib/sequel/adapters/jdbc.rb', line 141 def driver @driver end |
Instance Method Details
#call_sproc(name, opts = {}) ⇒ Object
Execute the given stored procedure with the give name. If a block is given, the stored procedure should return rows.
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/sequel/adapters/jdbc.rb', line 168 def call_sproc(name, opts = {}) args = opts[:args] || [] sql = "{call #{name}(#{args.map{'?'}.join(',')})}" synchronize(opts[:server]) do |conn| cps = conn.prepareCall(sql) i = 0 args.each{|arg| set_ps_arg(cps, arg, i+=1)} begin if block_given? yield log_yield(sql){cps.executeQuery} else case opts[:type] when :insert log_yield(sql){cps.executeUpdate} last_insert_id(conn, opts) else log_yield(sql){cps.executeUpdate} end end rescue NativeException, JavaSQL::SQLException => e raise_error(e) ensure cps.close end end end |
#connect(server) ⇒ Object
Connect to the database using JavaSQL::DriverManager.getConnection.
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/sequel/adapters/jdbc.rb', line 198 def connect(server) opts = server_opts(server) conn = if jndi? get_connection_from_jndi else args = [uri(opts)] args.concat([opts[:user], opts[:password]]) if opts[:user] && opts[:password] begin JavaSQL::DriverManager.setLoginTimeout(opts[:login_timeout]) if opts[:login_timeout] JavaSQL::DriverManager.getConnection(*args) rescue => e raise e unless driver # If the DriverManager can't get the connection - use the connect # method of the driver. (This happens under Tomcat for instance) props = java.util.Properties.new if opts && opts[:user] && opts[:password] props.setProperty("user", opts[:user]) props.setProperty("password", opts[:password]) end opts[:jdbc_properties].each{|k,v| props.setProperty(k.to_s, v)} if opts[:jdbc_properties] begin driver.new.connect(args[0], props) rescue => e2 e. << "\n#{e2.class.name}: #{e2.}" raise e end end end setup_connection(conn) end |
#execute(sql, opts = {}, &block) ⇒ Object Also known as: execute_dui
Execute the given SQL. If a block is given, if should be a SELECT statement or something else that returns rows.
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/sequel/adapters/jdbc.rb', line 231 def execute(sql, opts={}, &block) return call_sproc(sql, opts, &block) if opts[:sproc] return execute_prepared_statement(sql, opts, &block) if [Symbol, Dataset].any?{|c| sql.is_a?(c)} synchronize(opts[:server]) do |conn| statement(conn) do |stmt| if block yield log_yield(sql){stmt.executeQuery(sql)} else case opts[:type] when :ddl log_yield(sql){stmt.execute(sql)} when :insert log_yield(sql) do if requires_return_generated_keys? stmt.executeUpdate(sql, JavaSQL::Statement.RETURN_GENERATED_KEYS) else stmt.executeUpdate(sql) end end last_insert_id(conn, opts.merge(:stmt=>stmt)) else log_yield(sql){stmt.executeUpdate(sql)} end end end end end |
#execute_ddl(sql, opts = {}) ⇒ Object
Execute the given DDL SQL, which should not return any values or rows.
262 263 264 |
# File 'lib/sequel/adapters/jdbc.rb', line 262 def execute_ddl(sql, opts={}) execute(sql, {:type=>:ddl}.merge(opts)) end |
#execute_insert(sql, opts = {}) ⇒ Object
Execute the given INSERT SQL, returning the last inserted row id.
268 269 270 |
# File 'lib/sequel/adapters/jdbc.rb', line 268 def execute_insert(sql, opts={}) execute(sql, {:type=>:insert}.merge(opts)) end |
#indexes(table, opts = {}) ⇒ Object Also known as: jdbc_indexes
Use the JDBC metadata to get the index information for the table.
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
# File 'lib/sequel/adapters/jdbc.rb', line 273 def indexes(table, opts={}) m = output_identifier_meth im = input_identifier_meth schema, table = schema_and_table(table) schema ||= opts[:schema] schema = im.call(schema) if schema table = im.call(table) indexes = {} (:getIndexInfo, nil, schema, table, false, true) do |r| next unless name = r[:column_name] next if respond_to?(:primary_key_index_re, true) and r[:index_name] =~ primary_key_index_re i = indexes[m.call(r[:index_name])] ||= {:columns=>[], :unique=>[false, 0].include?(r[:non_unique])} i[:columns] << m.call(name) end indexes end |
#jndi? ⇒ Boolean
Whether or not JNDI is being used for this connection.
291 292 293 |
# File 'lib/sequel/adapters/jdbc.rb', line 291 def jndi? !!(uri =~ JNDI_URI_REGEXP) end |
#tables(opts = {}) ⇒ Object Also known as: jdbc_tables
All tables in this database
296 297 298 |
# File 'lib/sequel/adapters/jdbc.rb', line 296 def tables(opts={}) get_tables('TABLE', opts) end |
#uri(opts = {}) ⇒ Object
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.
304 305 306 307 308 |
# File 'lib/sequel/adapters/jdbc.rb', line 304 def uri(opts={}) opts = @opts.merge(opts) ur = opts[:uri] || opts[:url] || opts[:database] ur =~ /^\Ajdbc:/ ? ur : "jdbc:#{ur}" end |
#views(opts = {}) ⇒ Object Also known as: jdbc_views
All views in this database
311 312 313 |
# File 'lib/sequel/adapters/jdbc.rb', line 311 def views(opts={}) get_tables('VIEW', opts) end |