Class Sequel::JDBC::Database

  1. lib/sequel_core/adapters/jdbc.rb

JDBC Databases offer a fairly uniform interface that does not change much based on the sub adapter.

Attributes

database_type [R] The type of database we are connecting to

Public class methods

new (opts)

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.

[show source]
     # File lib/sequel_core/adapters/jdbc.rb, line 95
 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

Public instance methods

call_sproc (name, opts = {}) {|cps.executeQuery| ...}

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

[show source]
     # File lib/sequel_core/adapters/jdbc.rb, line 106
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
connect (server)

Connect to the database using JavaSQL::DriverManager.getConnection.

[show source]
     # File lib/sequel_core/adapters/jdbc.rb, line 136
136:       def connect(server)
137:         setup_connection(JavaSQL::DriverManager.getConnection(uri(server_opts(server))))
138:       end
dataset (opts = nil)

Return instances of JDBC::Dataset with the given opts.

[show source]
     # File lib/sequel_core/adapters/jdbc.rb, line 141
141:       def dataset(opts = nil)
142:         JDBC::Dataset.new(self, opts)
143:       end
execute (sql, opts={}) {|stmt.executeQuery(sql)| ...}

Execute the given SQL. If a block is given, if should be a SELECT statement or something else that returns rows.

[show source]
     # File lib/sequel_core/adapters/jdbc.rb, line 147
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
execute_ddl (sql, opts={})

Execute the given DDL SQL, which should not return any values or rows.

[show source]
     # File lib/sequel_core/adapters/jdbc.rb, line 178
178:       def execute_ddl(sql, opts={})
179:         execute(sql, {:type=>:ddl}.merge(opts))
180:       end
execute_dui (sql, opts={}, &block)

Alias for execute

execute_insert (sql, opts={})

Execute the given INSERT SQL, returning the last inserted row id.

[show source]
     # File lib/sequel_core/adapters/jdbc.rb, line 184
184:       def execute_insert(sql, opts={})
185:         execute(sql, {:type=>:insert}.merge(opts))
186:       end
tables ()

All tables in this database

[show source]
     # File lib/sequel_core/adapters/jdbc.rb, line 189
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
transaction (server=nil) {|conn| ...}

Default transaction method that should work on most JDBC databases. Does not use the JDBC transaction methods, uses SQL BEGIN/ROLLBACK/COMMIT statements instead.

[show source]
     # File lib/sequel_core/adapters/jdbc.rb, line 199
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
uri (opts={})

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.

[show source]
     # File lib/sequel_core/adapters/jdbc.rb, line 227
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