Module: ArJdbc::H2

Includes:
HSQLDB
Included in:
ActiveRecord::ConnectionAdapters::H2Adapter
Defined in:
lib/arjdbc/h2/adapter.rb

Defined Under Namespace

Modules: Column

Constant Summary collapse

ADAPTER_NAME =
'H2'.freeze
NATIVE_DATABASE_TYPES =
{
  # "integer GENERATED BY DEFAULT AS IDENTITY(START WITH 0) PRIMARY KEY"
  :primary_key => "bigint identity",
  :boolean     => { :name => "boolean" },
  :tinyint     => { :name => "tinyint", :limit => 1 },
  :smallint    => { :name => "smallint", :limit => 2 },
  :bigint      => { :name => "bigint", :limit => 8 },
  :integer     => { :name => "int", :limit => 4 },
  :decimal     => { :name => "decimal" }, # :limit => 2147483647
  :numeric     => { :name => "numeric" }, # :limit => 2147483647
  :float       => { :name => "float", :limit => 8 },
  :double      => { :name => "double", :limit => 8 },
  :real        => { :name => "real", :limit => 4 }, # :limit => 8
  :date        => { :name => "date" },
  :time        => { :name => "time" },
  :timestamp   => { :name => "timestamp" },
  :datetime    => { :name => "timestamp" },
  :binary      => { :name => "binary" },
  :string      => { :name => "varchar", :limit => 255 },
  :char        => { :name => "char" }, # :limit => 2147483647
  :blob        => { :name => "blob" },
  :text        => { :name => "clob" },
  :clob        => { :name => "clob" },
  :uuid        => { :name => "uuid" }, # :limit => 2147483647
  :other       => { :name => "other" }, # java.lang.Object
  :array       => { :name => "array" }, # java.lang.Object[]
  # NOTE: would be great if AR allowed as to refactor as :
  #   t.column :string, :ignorecase => true
  :varchar_casesensitive => { :name => 'varchar_casesensitive' },
  :varchar_ignorecase => { :name => 'varchar_ignorecase' },
  # :identity : { :name=>"identity", :limit => 19 }
  # :result_set : { :name=>"result_set" }
}

Constants included from HSQLDB

ArJdbc::HSQLDB::SchemaCreation

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HSQLDB

#add_column, #add_limit_offset!, #change_column_default, #last_insert_id, #quote_column_name, #quoted_date, #remove_index, #rename_column, #rename_table, #schema_creation, #supports_foreign_keys?, #truncate

Class Method Details

.column_selectorObject

See Also:

  • ActiveRecord::ConnectionAdapters::JdbcColumn#column_types


15
16
17
# File 'lib/arjdbc/h2/adapter.rb', line 15

def self.column_selector
  [ /\.h2\./i, lambda { |config, column| column.extend(Column) } ]
end

.jdbc_connection_classObject



10
11
12
# File 'lib/arjdbc/h2/adapter.rb', line 10

def self.jdbc_connection_class
  ::ActiveRecord::ConnectionAdapters::H2JdbcConnection
end

Instance Method Details

#adapter_nameObject



79
80
81
# File 'lib/arjdbc/h2/adapter.rb', line 79

def adapter_name
  ADAPTER_NAME
end

#change_column(table_name, column_name, type, options = {}) ⇒ Object



171
172
173
174
175
# File 'lib/arjdbc/h2/adapter.rb', line 171

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) ⇒ Object



166
167
168
# File 'lib/arjdbc/h2/adapter.rb', line 166

def columns(table_name, name = nil)
  @connection.columns_internal(table_name.to_s, nil, h2_schema)
end

#current_schemaObject



177
178
179
# File 'lib/arjdbc/h2/adapter.rb', line 177

def current_schema
  execute('CALL SCHEMA()')[0].values[0]
end

#empty_insert_statement_valueObject



156
157
158
# File 'lib/arjdbc/h2/adapter.rb', line 156

def empty_insert_statement_value
  "VALUES ()"
end

#explain(arel, binds = []) ⇒ Object



204
205
206
207
208
# File 'lib/arjdbc/h2/adapter.rb', line 204

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_adapterObject

Deprecated.

no longer used. only here for backwards compatibility with 1.2



84
85
86
# File 'lib/arjdbc/h2/adapter.rb', line 84

def h2_adapter
  true
end

#jdbc_connection(unwrap = nil) ⇒ Object



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/arjdbc/h2/adapter.rb', line 256

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_typesObject



123
124
125
# File 'lib/arjdbc/h2/adapter.rb', line 123

def native_database_types
  NATIVE_DATABASE_TYPES
end

#quote(value, column = nil) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/arjdbc/h2/adapter.rb', line 182

def quote(value, column = nil)
  case value
  when String
    if value.empty?
      "''"
    else
      super
    end
  else
    super
  end
end

#shutdownObject



226
227
228
# File 'lib/arjdbc/h2/adapter.rb', line 226

def shutdown
  execute 'SHUTDOWN COMPACT'
end

#structure_dumpObject



211
212
213
214
215
216
217
218
219
# File 'lib/arjdbc/h2/adapter.rb', line 211

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) ⇒ Object

See Also:



222
223
224
# File 'lib/arjdbc/h2/adapter.rb', line 222

def structure_load(dump)
  dump.each_line("\n\n") { |ddl| execute(ddl) }
end

#supports_explain?Boolean

Returns:

  • (Boolean)


201
# File 'lib/arjdbc/h2/adapter.rb', line 201

def supports_explain?; true end

#supports_views?Boolean

Returns:

  • (Boolean)


196
# File 'lib/arjdbc/h2/adapter.rb', line 196

def supports_views?; true end

#tablesObject



161
162
163
# File 'lib/arjdbc/h2/adapter.rb', line 161

def tables
  @connection.tables(nil, h2_schema)
end

#type_to_sql(type, limit = nil, precision = nil, scale = nil) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/arjdbc/h2/adapter.rb', line 128

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