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



64
65
66
# File 'lib/arjdbc/h2/adapter.rb', line 64

def adapter_name
  ADAPTER_NAME
end

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



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

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



151
152
153
# File 'lib/arjdbc/h2/adapter.rb', line 151

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

#current_schemaObject



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

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

#empty_insert_statement_valueObject



141
142
143
# File 'lib/arjdbc/h2/adapter.rb', line 141

def empty_insert_statement_value
  "VALUES ()"
end

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



189
190
191
192
193
# File 'lib/arjdbc/h2/adapter.rb', line 189

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



69
70
71
# File 'lib/arjdbc/h2/adapter.rb', line 69

def h2_adapter
  true
end

#jdbc_connection(unwrap = nil) ⇒ Object



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/arjdbc/h2/adapter.rb', line 241

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



108
109
110
# File 'lib/arjdbc/h2/adapter.rb', line 108

def native_database_types
  NATIVE_DATABASE_TYPES
end

#quote(value, column = nil) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/arjdbc/h2/adapter.rb', line 167

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

#shutdownObject



211
212
213
# File 'lib/arjdbc/h2/adapter.rb', line 211

def shutdown
  execute 'SHUTDOWN COMPACT'
end

#structure_dumpObject



196
197
198
199
200
201
202
203
204
# File 'lib/arjdbc/h2/adapter.rb', line 196

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:



207
208
209
# File 'lib/arjdbc/h2/adapter.rb', line 207

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

#supports_explain?Boolean

Returns:

  • (Boolean)


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

def supports_explain?; true end

#supports_views?Boolean

Returns:

  • (Boolean)


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

def supports_views?; true end

#tablesObject



146
147
148
# File 'lib/arjdbc/h2/adapter.rb', line 146

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

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



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/arjdbc/h2/adapter.rb', line 113

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