# 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
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
Source
# File lib/sequel/adapters/jdbc/h2.rb 28 def freeze 29 h2_version 30 version2? 31 super 32 end
Calls superclass method
Source
# File lib/sequel/adapters/jdbc/h2.rb 34 def h2_version 35 @h2_version ||= get(Sequel.function(:H2VERSION)) 36 end
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
Source
# File lib/sequel/adapters/jdbc/h2.rb 43 def serial_primary_key_options 44 {:primary_key => true, :type => :identity, :identity=>true} 45 end
H2
uses an IDENTITY type for primary keys
Source
# File lib/sequel/adapters/jdbc/h2.rb 48 def supports_create_table_if_not_exists? 49 true 50 end
H2
supports CREATE TABLE IF NOT EXISTS syntax
Source
# File lib/sequel/adapters/jdbc/h2.rb 53 def supports_prepared_transactions? 54 true 55 end
H2
supports prepared transactions
Source
# File lib/sequel/adapters/jdbc/h2.rb 58 def supports_savepoints? 59 true 60 end
H2
supports savepoints
Private Instance Methods
Source
# 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
Calls superclass method
Source
# File lib/sequel/adapters/jdbc/h2.rb 65 def can_add_primary_key_constraint_on_nullable_columns? 66 false 67 end
H2
does not allow adding primary key constraints to NULLable columns.
Source
# 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
If the :prepare option is given and we aren’t in a savepoint, prepare the transaction for a two-phase commit.
Calls superclass method
Source
# 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
Default to a single connection for a memory database.
Calls superclass method
Source
# File lib/sequel/adapters/jdbc/h2.rb 143 def database_error_regexps 144 DATABASE_ERROR_REGEXPS 145 end
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
Source
# 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
Get the last inserted id using getGeneratedKeys, scope_identity, or identity.
Source
# 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
Calls superclass method
Source
Source
# File lib/sequel/adapters/jdbc/h2.rb 185 def supports_named_column_constraints? 186 false 187 end
H2
does not support named column constraints.
Source
# File lib/sequel/adapters/jdbc/h2.rb 190 def type_literal_generic_bignum_symbol(column) 191 column[:identity] ? 'BIGINT AUTO_INCREMENT' : super 192 end
Use BIGINT IDENTITY for identity columns that use :Bignum type
Calls superclass method
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