Class: Sequel::JDBC::Database

Inherits:
Database show all
Defined in:
lib/sequel/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::AUTOINCREMENT, Database::CASCADE, Database::COMMA_SEPARATOR, Database::MYSQL_TIMESTAMP_RE, Database::NOT_NULL, Database::NO_ACTION, Database::NULL, Database::POSTGRES_DEFAULT_RE, Database::PRIMARY_KEY, Database::RESTRICT, Database::SET_DEFAULT, Database::SET_NULL, 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_ROLLBACK, Database::UNDERSCORE, Database::UNIQUE, Database::UNSIGNED

Instance Attribute Summary collapse

Attributes inherited from Database

#default_schema, #loggers, #opts, #pool, #prepared_statements

Instance Method Summary collapse

Methods inherited from Database

#<<, #[], adapter_class, adapter_scheme, #add_column, #add_index, #alter_table, #call, #cast_type_literal, connect, #create_or_replace_view, #create_table, #create_table!, #create_table?, #create_view, #disconnect, #drop_column, #drop_index, #drop_table, #drop_view, #dump_indexes_migration, #dump_schema_migration, #dump_table_schema, #fetch, #from, #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=, #inspect, #literal, #log_info, #logger=, #query, #quote_identifiers=, quote_identifiers=, #quote_identifiers?, #rename_column, #rename_table, #schema, #select, #serial_primary_key_options, #set_column_default, #set_column_type, single_threaded=, #single_threaded?, #supports_savepoints?, #synchronize, #table_exists?, #test_connection, #transaction, #typecast_value, #url

Methods included from Metaprogramming

#meta_def

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.

Raises:



95
96
97
98
99
100
101
102
# File 'lib/sequel/adapters/jdbc.rb', line 95

def initialize(opts)
  @opts = 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
  super(opts)
end

Instance Attribute Details

#database_typeObject (readonly)

The type of database we are connecting to



89
90
91
# File 'lib/sequel/adapters/jdbc.rb', line 89

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.



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/sequel/adapters/jdbc.rb', line 106

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.



136
137
138
139
140
# File 'lib/sequel/adapters/jdbc.rb', line 136

def connect(server)
  args = [uri(server_opts(server))]
  args.concat([opts[:user], opts[:password]]) if opts[:user] && opts[:password]
  setup_connection(JavaSQL::DriverManager.getConnection(*args))
end

#dataset(opts = nil) ⇒ Object

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



143
144
145
# File 'lib/sequel/adapters/jdbc.rb', line 143

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.



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/sequel/adapters/jdbc.rb', line 149

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)}
  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.



180
181
182
# File 'lib/sequel/adapters/jdbc.rb', line 180

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.



186
187
188
# File 'lib/sequel/adapters/jdbc.rb', line 186

def execute_insert(sql, opts={})
  execute(sql, {:type=>:insert}.merge(opts))
end

#indexes(table) ⇒ Object

Return a hash containing index information. Hash keys are index name symbols. Values are subhashes with two keys, :columns and :unique. The value of :columns is an array of symbols of column names. The value of :unique is true or false depending on if the index is unique.



194
195
196
197
198
199
200
201
202
203
204
# File 'lib/sequel/adapters/jdbc.rb', line 194

def indexes(table)
  indexes = {}
  m = output_identifier_meth
  (:getIndexInfo, nil, nil, input_identifier_meth.call(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=>!r[:non_unique]}
    i[:columns] << m.call(name)
  end
  indexes
end

#tablesObject

All tables in this database



207
208
209
210
211
212
# File 'lib/sequel/adapters/jdbc.rb', line 207

def tables
  ts = []
  m = output_identifier_meth
  (:getTables, nil, nil, nil, ['TABLE'].to_java(:string)){|h| ts << m.call(h[:table_name])}
  ts
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.



218
219
220
221
222
# File 'lib/sequel/adapters/jdbc.rb', line 218

def uri(opts={})
  opts = @opts.merge(opts)
  ur = opts[:uri] || opts[:url] || opts[:database]
  ur =~ /^\Ajdbc:/ ? ur : "jdbc:#{ur}"
end