# File lib/sequel/adapters/jdbc/h2.rb 177 def primary_key_index_re 178 /\Aprimary_key/i 179 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 17 def commit_prepared_transaction(transaction_id, opts=OPTS) 18 run("COMMIT TRANSACTION #{transaction_id}", opts) 19 end
database_type()
click to toggle source
# File lib/sequel/adapters/jdbc/h2.rb 21 def database_type 22 :h2 23 end
freeze()
click to toggle source
Calls superclass method
# File lib/sequel/adapters/jdbc/h2.rb 25 def freeze 26 h2_version 27 version2? 28 super 29 end
h2_version()
click to toggle source
# File lib/sequel/adapters/jdbc/h2.rb 31 def h2_version 32 @h2_version ||= get(Sequel.function(:H2VERSION)) 33 end
rollback_prepared_transaction(transaction_id, opts=OPTS)
click to toggle source
# File lib/sequel/adapters/jdbc/h2.rb 35 def rollback_prepared_transaction(transaction_id, opts=OPTS) 36 run("ROLLBACK TRANSACTION #{transaction_id}", opts) 37 end
serial_primary_key_options()
click to toggle source
H2
uses an IDENTITY type for primary keys
# File lib/sequel/adapters/jdbc/h2.rb 40 def serial_primary_key_options 41 {:primary_key => true, :type => :identity, :identity=>true} 42 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 45 def supports_create_table_if_not_exists? 46 true 47 end
supports_prepared_transactions?()
click to toggle source
H2
supports prepared transactions
# File lib/sequel/adapters/jdbc/h2.rb 50 def supports_prepared_transactions? 51 true 52 end
supports_savepoints?()
click to toggle source
H2
supports savepoints
# File lib/sequel/adapters/jdbc/h2.rb 55 def supports_savepoints? 56 true 57 end
Private Instance Methods
alter_table_sql(table, op)
click to toggle source
Calls superclass method
# File lib/sequel/adapters/jdbc/h2.rb 76 def alter_table_sql(table, op) 77 case op[:op] 78 when :add_column 79 if (pk = op.delete(:primary_key)) || (ref = op.delete(:table)) 80 if pk 81 op[:null] = false 82 end 83 84 sqls = [super(table, op)] 85 86 if pk && (h2_version >= '1.4' || op[:type] != :identity) 87 # H2 needs to add a primary key column as a constraint in this case 88 sqls << "ALTER TABLE #{quote_schema_table(table)} ADD PRIMARY KEY (#{quote_identifier(op[:name])})" 89 end 90 91 if ref 92 op[:table] = ref 93 constraint_name = op[:foreign_key_constraint_name] 94 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)}" 95 end 96 97 sqls 98 else 99 super(table, op) 100 end 101 when :rename_column 102 "ALTER TABLE #{quote_schema_table(table)} ALTER COLUMN #{quote_identifier(op[:name])} RENAME TO #{quote_identifier(op[:new_name])}" 103 when :set_column_null 104 "ALTER TABLE #{quote_schema_table(table)} ALTER COLUMN #{quote_identifier(op[:name])} SET#{' NOT' unless op[:null]} NULL" 105 when :set_column_type 106 if sch = schema(table) 107 if cs = sch.each{|k, v| break v if k == op[:name]; nil} 108 cs = cs.dup 109 cs[:default] = cs[:ruby_default] 110 op = cs.merge!(op) 111 end 112 end 113 sql = "ALTER TABLE #{quote_schema_table(table)} ALTER COLUMN #{quote_identifier(op[:name])} #{type_literal(op)}".dup 114 column_definition_order.each{|m| send(:"column_definition_#{m}_sql", sql, op)} 115 sql 116 when :drop_constraint 117 if op[:type] == :primary_key 118 "ALTER TABLE #{quote_schema_table(table)} DROP PRIMARY KEY" 119 else 120 super(table, op) 121 end 122 else 123 super(table, op) 124 end 125 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 62 def can_add_primary_key_constraint_on_nullable_columns? 63 false 64 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 68 def commit_transaction(conn, opts=OPTS) 69 if (s = opts[:prepare]) && savepoint_level(conn) <= 1 70 log_connection_execute(conn, "PREPARE COMMIT #{s}") 71 else 72 super 73 end 74 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 128 def connection_pool_default_options 129 o = super 130 uri == 'jdbc:h2:mem:' ? o.merge(:max_connections=>1) : o 131 end
database_error_regexps()
click to toggle source
# File lib/sequel/adapters/jdbc/h2.rb 140 def database_error_regexps 141 DATABASE_ERROR_REGEXPS 142 end
execute_statement_insert(stmt, sql)
click to toggle source
# File lib/sequel/adapters/jdbc/h2.rb 144 def execute_statement_insert(stmt, sql) 145 stmt.executeUpdate(sql, JavaSQL::Statement::RETURN_GENERATED_KEYS) 146 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 153 def last_insert_id(conn, opts=OPTS) 154 if stmt = opts[:stmt] 155 rs = stmt.getGeneratedKeys 156 begin 157 if rs.next 158 begin 159 rs.getLong(1) 160 rescue 161 rs.getObject(1) rescue nil 162 end 163 end 164 ensure 165 rs.close 166 end 167 elsif !version2? 168 statement(conn) do |stmt| 169 sql = 'SELECT IDENTITY()' 170 rs = log_connection_yield(sql, conn){stmt.executeQuery(sql)} 171 rs.next 172 rs.getLong(1) 173 end 174 end 175 end
prepare_jdbc_statement(conn, sql, opts)
click to toggle source
Calls superclass method
# File lib/sequel/adapters/jdbc/h2.rb 148 def prepare_jdbc_statement(conn, sql, opts) 149 opts[:type] == :insert ? conn.prepareStatement(sql, JavaSQL::Statement::RETURN_GENERATED_KEYS) : super 150 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 182 def supports_named_column_constraints? 183 false 184 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 187 def type_literal_generic_bignum_symbol(column) 188 column[:identity] ? 'BIGINT AUTO_INCREMENT' : super 189 end
version2?()
click to toggle source
# File lib/sequel/adapters/jdbc/h2.rb 191 def version2? 192 return @version2 if defined?(@version2) 193 @version2 = h2_version.to_i >= 2 194 end