Class: Sequel::JDBC::Database
- Defined in:
- lib/sequel_core/adapters/jdbc.rb
Overview
JDBC Databases offer a fairly uniform interface that does not change much based on the sub adapter.
Constant Summary
Constants inherited from Database
Database::ADAPTERS, Database::SQL_BEGIN, Database::SQL_COMMIT, Database::SQL_ROLLBACK
Constants included from Schema::SQL
Schema::SQL::AUTOINCREMENT, Schema::SQL::CASCADE, Schema::SQL::COMMA_SEPARATOR, Schema::SQL::NOT_NULL, Schema::SQL::NO_ACTION, Schema::SQL::NULL, Schema::SQL::PRIMARY_KEY, Schema::SQL::RESTRICT, Schema::SQL::SET_DEFAULT, Schema::SQL::SET_NULL, Schema::SQL::TYPES, Schema::SQL::UNDERSCORE, Schema::SQL::UNIQUE, Schema::SQL::UNSIGNED
Instance Attribute Summary collapse
-
#database_type ⇒ Object
readonly
The type of database we are connecting to.
Attributes inherited from Database
#default_schema, #loggers, #opts, #pool, #prepared_statements, #quote_identifiers, #upcase_identifiers
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.
-
#dataset(opts = nil) ⇒ Object
Return instances of JDBC::Dataset with the given opts.
-
#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.
-
#initialize(opts) ⇒ Database
constructor
Call the DATABASE_SETUP proc directly after initialization, so the object always uses sub adapter specific code.
-
#transaction(server = nil) ⇒ Object
Default transaction method that should work on most JDBC databases.
-
#uri(opts = {}) ⇒ Object
(also: #url)
The uri for this connection.
Methods inherited from Database
#<<, #[], adapter_class, adapter_scheme, #add_column, #add_index, #alter_table, #call, connect, #create_or_replace_view, #create_table, #create_table!, #create_view, #disconnect, #drop_column, #drop_index, #drop_table, #drop_view, #fetch, #from, #get, #inspect, #log_info, #logger, #logger=, #multi_threaded?, #query, quote_identifiers=, #quote_identifiers?, #rename_column, #rename_table, #select, #serial_primary_key_options, #set_column_default, #set_column_type, single_threaded=, #single_threaded?, #synchronize, #table_exists?, #test_connection, #typecast_value, upcase_identifiers=, #upcase_identifiers?
Methods included from Schema::SQL
#alter_table_sql, #alter_table_sql_list, #auto_increment_sql, #column_definition_sql, #column_list_sql, #column_references_sql, #constraint_definition_sql, #create_table_sql_list, #default_index_name, #drop_index_sql, #drop_table_sql, #filter_expr, #index_definition_sql, #index_list_sql_list, #literal, #on_delete_clause, #quote_identifier, #quote_schema_table, #rename_table_sql, #schema, #schema_utility_dataset
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.
89 90 91 92 93 94 95 |
# File 'lib/sequel_core/adapters/jdbc.rb', line 89 def initialize(opts) super(opts) raise(Error, "No connection string specified") unless uri if match = /\Ajdbc:([^:]+)/.match(uri) and prok = DATABASE_SETUP[match[1].to_sym] prok.call(self) end end |
Instance Attribute Details
#database_type ⇒ Object (readonly)
The type of database we are connecting to
83 84 85 |
# File 'lib/sequel_core/adapters/jdbc.rb', line 83 def database_type @database_type 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.
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/sequel_core/adapters/jdbc.rb', line 99 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 cps.executeQuery else case opts[:type] when :insert cps.executeUpdate last_insert_id(conn, opts) else 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.
129 130 131 |
# File 'lib/sequel_core/adapters/jdbc.rb', line 129 def connect(server) setup_connection(JavaSQL::DriverManager.getConnection(uri(server_opts(server)))) end |
#dataset(opts = nil) ⇒ Object
Return instances of JDBC::Dataset with the given opts.
134 135 136 |
# File 'lib/sequel_core/adapters/jdbc.rb', line 134 def dataset(opts = nil) JDBC::Dataset.new(self, opts) 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.
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/sequel_core/adapters/jdbc.rb', line 140 def execute(sql, opts={}, &block) return call_sproc(sql, opts, &block) if opts[:sproc] return execute_prepared_statement(sql, opts, &block) if sql.is_one_of?(Symbol, Dataset) log_info(sql) synchronize(opts[:server]) do |conn| stmt = conn.createStatement begin if block_given? yield stmt.executeQuery(sql) else case opts[:type] when :ddl stmt.execute(sql) when :insert stmt.executeUpdate(sql) last_insert_id(conn, opts) else stmt.executeUpdate(sql) end end rescue NativeException, JavaSQL::SQLException => e raise_error(e) ensure stmt.close end end end |
#execute_ddl(sql, opts = {}) ⇒ Object
Execute the given DDL SQL, which should not return any values or rows.
171 172 173 |
# File 'lib/sequel_core/adapters/jdbc.rb', line 171 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.
177 178 179 |
# File 'lib/sequel_core/adapters/jdbc.rb', line 177 def execute_insert(sql, opts={}) execute(sql, {:type=>:insert}.merge(opts)) end |
#transaction(server = nil) ⇒ Object
Default transaction method that should work on most JDBC databases. Does not use the JDBC transaction methods, uses SQL BEGIN/ROLLBACK/COMMIT statements instead.
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/sequel_core/adapters/jdbc.rb', line 184 def transaction(server=nil) synchronize(server) do |conn| return yield(conn) if @transactions.include?(Thread.current) stmt = conn.createStatement begin log_info(begin_transaction_sql) stmt.execute(begin_transaction_sql) @transactions << Thread.current yield(conn) rescue Exception => e log_info(rollback_transaction_sql) stmt.execute(rollback_transaction_sql) transaction_error(e) ensure unless e log_info(commit_transaction_sql) stmt.execute(commit_transaction_sql) end stmt.close @transactions.delete(Thread.current) end end end |
#uri(opts = {}) ⇒ Object Also known as: url
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.
212 213 214 215 216 |
# File 'lib/sequel_core/adapters/jdbc.rb', line 212 def uri(opts={}) opts = @opts.merge(opts) ur = opts[:uri] || opts[:url] || opts[:database] ur =~ /^\Ajdbc:/ ? ur : "jdbc:#{ur}" end |