# File lib/sequel/adapters/jdbc/h2.rb 180 def primary_key_index_re 181 /\Aprimary_key/i 182 end
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 20 def commit_prepared_transaction(transaction_id, opts=OPTS) 21 run("COMMIT TRANSACTION #{transaction_id}", opts) 22 end
database_type()
click to toggle source
# File lib/sequel/adapters/jdbc/h2.rb 24 def database_type 25 :h2 26 end
freeze()
click to toggle source
Calls superclass method
# File lib/sequel/adapters/jdbc/h2.rb 28 def freeze 29 h2_version 30 version2? 31 super 32 end
h2_version()
click to toggle source
# File lib/sequel/adapters/jdbc/h2.rb 34 def h2_version 35 @h2_version ||= get(Sequel.function(:H2VERSION)) 36 end
rollback_prepared_transaction(transaction_id, opts=OPTS)
click to toggle source
# File lib/sequel/adapters/jdbc/h2.rb 38 def rollback_prepared_transaction(transaction_id, opts=OPTS) 39 run("ROLLBACK TRANSACTION #{transaction_id}", opts) 40 end
serial_primary_key_options()
click to toggle source
H2
uses an IDENTITY type for primary keys
# File lib/sequel/adapters/jdbc/h2.rb 43 def serial_primary_key_options 44 {:primary_key => true, :type => :identity, :identity=>true} 45 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 48 def supports_create_table_if_not_exists? 49 true 50 end
supports_prepared_transactions?()
click to toggle source
H2
supports prepared transactions
# File lib/sequel/adapters/jdbc/h2.rb 53 def supports_prepared_transactions? 54 true 55 end
supports_savepoints?()
click to toggle source
H2
supports savepoints
# File lib/sequel/adapters/jdbc/h2.rb 58 def supports_savepoints? 59 true 60 end
Private Instance Methods
alter_table_sql(table, op)
click to toggle source
Calls superclass method
# File lib/sequel/adapters/jdbc/h2.rb 79 def alter_table_sql(table, op) 80 case op[:op] 81 when :add_column 82 if (pk = op.delete(:primary_key)) || (ref = op.delete(:table)) 83 if pk 84 op[:null] = false 85 end 86 87 sqls = [super(table, op)] 88 89 if pk && (h2_version >= '1.4' || op[:type] != :identity) 90 # H2 needs to add a primary key column as a constraint in this case 91 sqls << "ALTER TABLE #{quote_schema_table(table)} ADD PRIMARY KEY (#{quote_identifier(op[:name])})" 92 end 93 94 if ref 95 op[:table] = ref 96 constraint_name = op[:foreign_key_constraint_name] 97 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)}" 98 end 99 100 sqls 101 else 102 super(table, op) 103 end 104 when :rename_column 105 "ALTER TABLE #{quote_schema_table(table)} ALTER COLUMN #{quote_identifier(op[:name])} RENAME TO #{quote_identifier(op[:new_name])}" 106 when :set_column_null 107 "ALTER TABLE #{quote_schema_table(table)} ALTER COLUMN #{quote_identifier(op[:name])} SET#{' NOT' unless op[:null]} NULL" 108 when :set_column_type 109 if sch = schema(table) 110 if cs = sch.each{|k, v| break v if k == op[:name]; nil} 111 cs = cs.dup 112 cs[:default] = cs[:ruby_default] 113 op = cs.merge!(op) 114 end 115 end 116 sql = "ALTER TABLE #{quote_schema_table(table)} ALTER COLUMN #{quote_identifier(op[:name])} #{type_literal(op)}".dup 117 column_definition_order.each{|m| send(:"column_definition_#{m}_sql", sql, op)} 118 sql 119 when :drop_constraint 120 if op[:type] == :primary_key 121 "ALTER TABLE #{quote_schema_table(table)} DROP PRIMARY KEY" 122 else 123 super(table, op) 124 end 125 else 126 super(table, op) 127 end 128 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 65 def can_add_primary_key_constraint_on_nullable_columns? 66 false 67 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 71 def commit_transaction(conn, opts=OPTS) 72 if (s = opts[:prepare]) && savepoint_level(conn) <= 1 73 log_connection_execute(conn, "PREPARE COMMIT #{s}") 74 else 75 super 76 end 77 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 131 def connection_pool_default_options 132 o = super 133 uri == 'jdbc:h2:mem:' ? o.merge(:max_connections=>1) : o 134 end
database_error_regexps()
click to toggle source
# File lib/sequel/adapters/jdbc/h2.rb 143 def database_error_regexps 144 DATABASE_ERROR_REGEXPS 145 end
execute_statement_insert(stmt, sql)
click to toggle source
# File lib/sequel/adapters/jdbc/h2.rb 147 def execute_statement_insert(stmt, sql) 148 stmt.executeUpdate(sql, JavaSQL::Statement::RETURN_GENERATED_KEYS) 149 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 156 def last_insert_id(conn, opts=OPTS) 157 if stmt = opts[:stmt] 158 rs = stmt.getGeneratedKeys 159 begin 160 if rs.next 161 begin 162 rs.getLong(1) 163 rescue 164 rs.getObject(1) rescue nil 165 end 166 end 167 ensure 168 rs.close 169 end 170 elsif !version2? 171 statement(conn) do |stmt| 172 sql = 'SELECT IDENTITY()' 173 rs = log_connection_yield(sql, conn){stmt.executeQuery(sql)} 174 rs.next 175 rs.getLong(1) 176 end 177 end 178 end
prepare_jdbc_statement(conn, sql, opts)
click to toggle source
Calls superclass method
# File lib/sequel/adapters/jdbc/h2.rb 151 def prepare_jdbc_statement(conn, sql, opts) 152 opts[:type] == :insert ? conn.prepareStatement(sql, JavaSQL::Statement::RETURN_GENERATED_KEYS) : super 153 end
primary_key_index_re()
click to toggle source
supports_named_column_constraints?()
click to toggle source
H2
does not support named column constraints.
# File lib/sequel/adapters/jdbc/h2.rb 185 def supports_named_column_constraints? 186 false 187 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 190 def type_literal_generic_bignum_symbol(column) 191 column[:identity] ? 'BIGINT AUTO_INCREMENT' : super 192 end
version2?()
click to toggle source
# File lib/sequel/adapters/jdbc/h2.rb 194 def version2? 195 return @version2 if defined?(@version2) 196 @version2 = h2_version.to_i >= 2 197 end