module ArJdbc::H2

Constants

ADAPTER_NAME
NATIVE_DATABASE_TYPES

Public Class Methods

arel_visitor_type(config = nil) click to toggle source

@see ActiveRecord::ConnectionAdapters::Jdbc::ArelSupport

# File lib/arjdbc/h2/adapter.rb, line 76
def self.arel_visitor_type(config = nil)
  require 'arel/visitors/h2'; ::Arel::Visitors::H2
end
column_selector() click to toggle source

@see ActiveRecord::ConnectionAdapters::JdbcColumn#column_types

# File lib/arjdbc/h2/adapter.rb, line 14
def self.column_selector
  [ /\.h2\./i, lambda { |config, column| column.extend(Column) } ]
end
jdbc_connection_class() click to toggle source

@see ActiveRecord::ConnectionAdapters::JdbcAdapter#jdbc_connection_class

# File lib/arjdbc/h2/adapter.rb, line 9
def self.jdbc_connection_class
  ::ActiveRecord::ConnectionAdapters::H2JdbcConnection
end

Public Instance Methods

adapter_name() click to toggle source

@override

# File lib/arjdbc/h2/adapter.rb, line 83
def adapter_name
  ADAPTER_NAME
end
change_column(table_name, column_name, type, options = {}) click to toggle source

@override

# File lib/arjdbc/h2/adapter.rb, line 175
def change_column(table_name, column_name, type, options = {})
  execute "ALTER TABLE #{table_name} ALTER COLUMN #{column_name} #{type_to_sql(type, options[:limit])}"
  change_column_default(table_name, column_name, options[:default]) if options_include_default?(options)
  change_column_null(table_name, column_name, options[:null], options[:default]) if options.key?(:null)
end
columns(table_name, name = nil) click to toggle source

@override

# File lib/arjdbc/h2/adapter.rb, line 170
def columns(table_name, name = nil)
  @connection.columns_internal(table_name.to_s, nil, h2_schema)
end
create_database(name = nil, options = {}) click to toggle source

@private

# File lib/arjdbc/h2/adapter.rb, line 241
def create_database(name = nil, options = {}); end
current_schema() click to toggle source
# File lib/arjdbc/h2/adapter.rb, line 181
def current_schema
  execute('CALL SCHEMA()')[0].values[0]
end
database_path(base_only = false) click to toggle source

@private

# File lib/arjdbc/h2/adapter.rb, line 249
def database_path(base_only = false)
  db_path = jdbc_connection(true).getSession.getDataHandler.getDatabasePath
  return db_path if base_only
  if File.exist?(mv_path = "#{db_path}.mv.db")
    return mv_path
  else
    "#{db_path}.h2.db"
  end
end
drop_database(name = nil) click to toggle source

@private

# File lib/arjdbc/h2/adapter.rb, line 244
def drop_database(name = nil)
  execute('DROP ALL OBJECTS')
end
empty_insert_statement_value() click to toggle source

@override

# File lib/arjdbc/h2/adapter.rb, line 160
def empty_insert_statement_value
  "VALUES ()"
end
explain(arel, binds = []) click to toggle source

@override

# File lib/arjdbc/h2/adapter.rb, line 208
def explain(arel, binds = [])
  sql = "EXPLAIN #{to_sql(arel, binds)}"
  raw_result = exec_query_raw(sql, "EXPLAIN", binds)
  raw_result[0].values.join("\n") # [ "SELECT \n ..." ].to_s
end
h2_adapter() click to toggle source

@deprecated no longer used. only here for backwards compatibility with 1.2

# File lib/arjdbc/h2/adapter.rb, line 88
def h2_adapter
  true
end
jdbc_connection(unwrap = nil) click to toggle source

@override

# File lib/arjdbc/h2/adapter.rb, line 260
def jdbc_connection(unwrap = nil)
  java_connection = raw_connection.connection
  return java_connection unless unwrap
  if java_connection.java_class.name == 'org.h2.jdbc.JdbcConnection'
    return java_connection
  end
  connection_class = java.sql.Connection.java_class
  if java_connection.wrapper_for?(connection_class)
    java_connection.unwrap(connection_class) # java.sql.Wrapper.unwrap
  elsif java_connection.respond_to?(:connection)
    # e.g. org.apache.tomcat.jdbc.pool.PooledConnection
    java_connection.connection # getConnection
  else
    java_connection
  end
end
native_database_types() click to toggle source

@override

# File lib/arjdbc/h2/adapter.rb, line 127
def native_database_types
  NATIVE_DATABASE_TYPES
end
quote(value, column = nil) click to toggle source

@override

Calls superclass method ArJdbc::HSQLDB#quote
# File lib/arjdbc/h2/adapter.rb, line 186
def quote(value, column = nil)
  case value
  when String
    if value.empty?
      "''"
    else
      super
    end
  else
    super
  end
end
recreate_database(name = nil, options = {}) click to toggle source

@private

# File lib/arjdbc/h2/adapter.rb, line 235
def recreate_database(name = nil, options = {})
  drop_database(name)
  create_database(name, options)
end
shutdown() click to toggle source
# File lib/arjdbc/h2/adapter.rb, line 230
def shutdown
  execute 'SHUTDOWN COMPACT'
end
structure_dump() click to toggle source

@override

# File lib/arjdbc/h2/adapter.rb, line 215
def structure_dump
  execute('SCRIPT SIMPLE').map do |result|
    # [ { 'script' => SQL }, { 'script' ... }, ... ]
    case sql = result.first[1] # ['script']
    when /CREATE USER IF NOT EXISTS SA/i then nil
    else sql
    end
  end.compact.join("\n\n")
end
structure_load(dump) click to toggle source

@see structure_dump

# File lib/arjdbc/h2/adapter.rb, line 226
def structure_load(dump)
  dump.each_line("\n\n") { |ddl| execute(ddl) }
end
supports_explain?() click to toggle source

@override

# File lib/arjdbc/h2/adapter.rb, line 205
def supports_explain?; true end
supports_views?() click to toggle source

@override

# File lib/arjdbc/h2/adapter.rb, line 200
def supports_views?; true end
tables() click to toggle source

@override

# File lib/arjdbc/h2/adapter.rb, line 165
def tables
  @connection.tables(nil, h2_schema)
end
type_to_sql(type, limit = nil, precision = nil, scale = nil) click to toggle source

@override

Calls superclass method ArJdbc::HSQLDB#type_to_sql
# File lib/arjdbc/h2/adapter.rb, line 132
def type_to_sql(type, limit = nil, precision = nil, scale = nil)
  case type.to_sym
  when :integer
    case limit
    when 1; 'tinyint'
    when 2; 'smallint'
    when nil, 3, 4; 'int'
    when 5..8; 'bigint'
    else raise(ActiveRecordError, "No integer type has byte size #{limit}")
    end
  when :float
    case limit
    when 1..4; 'real'
    when 5..8; 'double'
    else raise(ActiveRecordError, "No float type has byte size #{limit}")
    end
  when :binary
    if limit && limit < 2 * 1024 * 1024
      'binary'
    else
      'blob'
    end
  else
    super
  end
end

Private Instance Methods

change_column_null(table_name, column_name, null, default = nil) click to toggle source
# File lib/arjdbc/h2/adapter.rb, line 279
def change_column_null(table_name, column_name, null, default = nil)
  if !null && !default.nil?
    execute("UPDATE #{table_name} SET #{column_name}=#{quote(default)} WHERE #{column_name} IS NULL")
  end
  if null
    execute "ALTER TABLE #{table_name} ALTER COLUMN #{column_name} SET NULL"
  else
    execute "ALTER TABLE #{table_name} ALTER COLUMN #{column_name} SET NOT NULL"
  end
end
h2_schema() click to toggle source
# File lib/arjdbc/h2/adapter.rb, line 290
def h2_schema
  @config[:schema] || ''
end