module Sequel::JDBC::H2::DatabaseMethods

Constants

DATABASE_ERROR_REGEXPS

Public Instance Methods

commit_prepared_transaction(transaction_id, opts=OPTS) click to toggle source
# File lib/sequel/adapters/jdbc/h2.rb, line 19
def commit_prepared_transaction(transaction_id, opts=OPTS)
  run("COMMIT TRANSACTION #{transaction_id}", opts)
end
database_type() click to toggle source
# File lib/sequel/adapters/jdbc/h2.rb, line 23
def database_type
  :h2
end
freeze() click to toggle source
Calls superclass method
# File lib/sequel/adapters/jdbc/h2.rb, line 27
def freeze
  h2_version
  version2?
  super
end
h2_version() click to toggle source
# File lib/sequel/adapters/jdbc/h2.rb, line 33
def h2_version
  @h2_version ||= get(Sequel.function(:H2VERSION))
end
rollback_prepared_transaction(transaction_id, opts=OPTS) click to toggle source
# File lib/sequel/adapters/jdbc/h2.rb, line 37
def rollback_prepared_transaction(transaction_id, opts=OPTS)
  run("ROLLBACK TRANSACTION #{transaction_id}", opts)
end
serial_primary_key_options() click to toggle source

H2 uses an IDENTITY type for primary keys

# File lib/sequel/adapters/jdbc/h2.rb, line 42
def serial_primary_key_options
  {:primary_key => true, :type => :identity, :identity=>true}
end
supports_create_table_if_not_exists?() click to toggle source

H2 supports CREATE TABLE IF NOT EXISTS syntax

# File lib/sequel/adapters/jdbc/h2.rb, line 47
def supports_create_table_if_not_exists?
  true
end
supports_prepared_transactions?() click to toggle source

H2 supports prepared transactions

# File lib/sequel/adapters/jdbc/h2.rb, line 52
def supports_prepared_transactions?
  true
end
supports_savepoints?() click to toggle source

H2 supports savepoints

# File lib/sequel/adapters/jdbc/h2.rb, line 57
def supports_savepoints?
  true
end

Private Instance Methods

alter_table_sql(table, op) click to toggle source
Calls superclass method
# File lib/sequel/adapters/jdbc/h2.rb, line 78
def alter_table_sql(table, op)
  case op[:op]
  when :add_column
    if (pk = op.delete(:primary_key)) || (ref = op.delete(:table))
      if pk
        op[:null] = false
      end

      sqls = [super(table, op)]

      if pk && (h2_version >= '1.4' || op[:type] != :identity)
        # H2 needs to add a primary key column as a constraint in this case
        sqls << "ALTER TABLE #{quote_schema_table(table)} ADD PRIMARY KEY (#{quote_identifier(op[:name])})"
      end

      if ref
        op[:table] = ref
        constraint_name = op[:foreign_key_constraint_name]
        sqls << "ALTER TABLE #{quote_schema_table(table)} ADD#{" CONSTRAINT #{quote_identifier(constraint_name)}" if constraint_name} FOREIGN KEY (#{quote_identifier(op[:name])}) #{column_references_sql(op)}"
      end

      sqls
    else
      super(table, op)
    end
  when :rename_column
    "ALTER TABLE #{quote_schema_table(table)} ALTER COLUMN #{quote_identifier(op[:name])} RENAME TO #{quote_identifier(op[:new_name])}"
  when :set_column_null
    "ALTER TABLE #{quote_schema_table(table)} ALTER COLUMN #{quote_identifier(op[:name])} SET#{' NOT' unless op[:null]} NULL"
  when :set_column_type
    if sch = schema(table)
      if cs = sch.each{|k, v| break v if k == op[:name]; nil}
        cs = cs.dup
        cs[:default] = cs[:ruby_default]
        op = cs.merge!(op)
      end
    end
    sql = "ALTER TABLE #{quote_schema_table(table)} ALTER COLUMN #{quote_identifier(op[:name])} #{type_literal(op)}".dup
    column_definition_order.each{|m| send(:"column_definition_#{m}_sql", sql, op)}
    sql
  when :drop_constraint
    if op[:type] == :primary_key
      "ALTER TABLE #{quote_schema_table(table)} DROP PRIMARY KEY"
    else
      super(table, op)
    end
  else
    super(table, op)
  end
end
can_add_primary_key_constraint_on_nullable_columns?() click to toggle source

H2 does not allow adding primary key constraints to NULLable columns.

# File lib/sequel/adapters/jdbc/h2.rb, line 64
def can_add_primary_key_constraint_on_nullable_columns?
  false
end
commit_transaction(conn, opts=OPTS) click to toggle source

If the :prepare option is given and we aren't in a savepoint, prepare the transaction for a two-phase commit.

Calls superclass method
# File lib/sequel/adapters/jdbc/h2.rb, line 70
def commit_transaction(conn, opts=OPTS)
  if (s = opts[:prepare]) && savepoint_level(conn) <= 1
    log_connection_execute(conn, "PREPARE COMMIT #{s}")
  else
    super
  end
end
connection_pool_default_options() click to toggle source

Default to a single connection for a memory database.

Calls superclass method
# File lib/sequel/adapters/jdbc/h2.rb, line 130
def connection_pool_default_options
  o = super
  uri == 'jdbc:h2:mem:' ? o.merge(:max_connections=>1) : o
end
database_error_regexps() click to toggle source
# File lib/sequel/adapters/jdbc/h2.rb, line 142
def database_error_regexps
  DATABASE_ERROR_REGEXPS
end
execute_statement_insert(stmt, sql) click to toggle source
# File lib/sequel/adapters/jdbc/h2.rb, line 146
def execute_statement_insert(stmt, sql)
  stmt.executeUpdate(sql, JavaSQL::Statement::RETURN_GENERATED_KEYS)
end
last_insert_id(conn, opts=OPTS) click to toggle source

Get the last inserted id using getGeneratedKeys, scope_identity, or identity.

# File lib/sequel/adapters/jdbc/h2.rb, line 155
def last_insert_id(conn, opts=OPTS)
  if stmt = opts[:stmt]
    rs = stmt.getGeneratedKeys
    begin
      if rs.next
        begin
          rs.getLong(1)
        rescue
          rs.getObject(1) rescue nil
        end
      end
    ensure
      rs.close
    end
  elsif !version2?
    statement(conn) do |stmt|
      sql = 'SELECT IDENTITY()'
      rs = log_connection_yield(sql, conn){stmt.executeQuery(sql)}
      rs.next
      rs.getLong(1)
    end
  end
end
prepare_jdbc_statement(conn, sql, opts) click to toggle source
Calls superclass method
# File lib/sequel/adapters/jdbc/h2.rb, line 150
def prepare_jdbc_statement(conn, sql, opts)
  opts[:type] == :insert ? conn.prepareStatement(sql, JavaSQL::Statement::RETURN_GENERATED_KEYS) : super
end
primary_key_index_re() click to toggle source
# File lib/sequel/adapters/jdbc/h2.rb, line 179
def primary_key_index_re
  /\Aprimary_key/i
end
supports_named_column_constraints?() click to toggle source

H2 does not support named column constraints.

# File lib/sequel/adapters/jdbc/h2.rb, line 184
def supports_named_column_constraints?
  false
end
type_literal_generic_bignum_symbol(column) click to toggle source

Use BIGINT IDENTITY for identity columns that use :Bignum type

Calls superclass method
# File lib/sequel/adapters/jdbc/h2.rb, line 189
def type_literal_generic_bignum_symbol(column)
  column[:identity] ? 'BIGINT AUTO_INCREMENT' : super
end
version2?() click to toggle source
# File lib/sequel/adapters/jdbc/h2.rb, line 193
def version2?
  return @version2 if defined?(@version2)
  @version2 = h2_version.to_i >= 2
end