Module: ArJdbc::HSQLDB

Includes:
ExplainSupport
Included in:
H2
Defined in:
lib/arjdbc/hsqldb/adapter.rb,
lib/arjdbc/hsqldb/explain_support.rb

Defined Under Namespace

Modules: Column, ExplainSupport

Constant Summary collapse

ADAPTER_NAME =

:nodoc:

'HSQLDB'
NATIVE_DATABASE_TYPES =
{
  :primary_key => "integer GENERATED BY DEFAULT AS IDENTITY(START WITH 0) PRIMARY KEY",
  :string => { :name => "varchar", :limit => 255 },
  :text => { :name => "clob" },
  :binary => { :name => "blob" },
  :boolean => { :name => "boolean" },
  :integer => { :name => "integer", :limit => 4 },
  :float => { :name => "float", :limit => 8 },
  # NOTE: fix incorrectly detected limits :
  :tinyint => { :name => "tinyint", :limit => 1 },
  :smallint => { :name => "smallint", :limit => 2 },
  :bigint => { :name => "bigint", :limit => 8 },
  :double => { :name => "double", :limit => 8 },
  :real => { :name => "real", :limit => 8 },
}

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ExplainSupport

#explain, #supports_explain?

Class Method Details

.arel2_visitors(config) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/arjdbc/hsqldb/adapter.rb', line 67

def self.arel2_visitors(config)
  require 'arel/visitors/hsqldb'
  {
    'hsqldb' => ::Arel::Visitors::HSQLDB,
    'jdbchsqldb' => ::Arel::Visitors::HSQLDB,
  }
end

.column_selectorObject



7
8
9
# File 'lib/arjdbc/hsqldb/adapter.rb', line 7

def self.column_selector
  [ /hsqldb/i, lambda { |cfg, column| column.extend(::ArJdbc::HSQLDB::Column) } ]
end

Instance Method Details

#adapter_nameObject

:nodoc:



63
64
65
# File 'lib/arjdbc/hsqldb/adapter.rb', line 63

def adapter_name # :nodoc:
  ADAPTER_NAME
end

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



139
140
141
142
143
# File 'lib/arjdbc/hsqldb/adapter.rb', line 139

def add_column(table_name, column_name, type, options = {})
  add_column_sql = "ALTER TABLE #{quote_table_name(table_name)} ADD #{quote_column_name(column_name)} #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}"
  add_column_options!(add_column_sql, options)
  execute(add_column_sql)
end

#add_limit_offset!(sql, options) ⇒ Object

:nodoc:



179
180
181
182
183
184
185
186
187
188
189
# File 'lib/arjdbc/hsqldb/adapter.rb', line 179

def add_limit_offset!(sql, options) #:nodoc:
  if sql =~ /^select/i
    offset = options[:offset] || 0
    bef = sql[7..-1]
    if limit = options[:limit]
      sql.replace "SELECT LIMIT #{offset} #{limit} #{bef}"
    elsif offset > 0
      sql.replace "SELECT LIMIT #{offset} 0 #{bef}"
    end
  end
end

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

:nodoc:



145
146
147
# File 'lib/arjdbc/hsqldb/adapter.rb', line 145

def change_column(table_name, column_name, type, options = {}) #:nodoc:
  execute "ALTER TABLE #{table_name} ALTER COLUMN #{column_name} #{type_to_sql(type, options[:limit])}"
end

#change_column_default(table_name, column_name, default) ⇒ Object

:nodoc:



149
150
151
# File 'lib/arjdbc/hsqldb/adapter.rb', line 149

def change_column_default(table_name, column_name, default) #:nodoc:
  execute "ALTER TABLE #{table_name} ALTER COLUMN #{column_name} SET DEFAULT #{quote(default)}"
end

#create_database(name) ⇒ Object

do nothing since database gets created upon connection. However this method gets called by rails db rake tasks so now we’re avoiding method_missing error



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

def create_database(name)
end

#drop_database(name) ⇒ Object



215
216
217
# File 'lib/arjdbc/hsqldb/adapter.rb', line 215

def drop_database(name)
  execute("DROP ALL OBJECTS")
end

#last_insert_idObject



168
169
170
171
# File 'lib/arjdbc/hsqldb/adapter.rb', line 168

def last_insert_id
  identity = select_value("CALL IDENTITY()")
  Integer(identity.nil? ? 0 : identity)
end

#modify_types(types) ⇒ Object



95
96
97
98
99
100
101
102
103
# File 'lib/arjdbc/hsqldb/adapter.rb', line 95

def modify_types(types)
  super(types)
  types[:primary_key] = NATIVE_DATABASE_TYPES[:primary_key]
  types[:string] = NATIVE_DATABASE_TYPES[:string].dup
  #types[:integer][:limit] = nil
  #types[:boolean][:limit] = nil
  types[:text][:limit] = nil
  types
end

#native_database_typesObject



91
92
93
# File 'lib/arjdbc/hsqldb/adapter.rb', line 91

def native_database_types
  super.merge NATIVE_DATABASE_TYPES
end

#quote(value, column = nil) ⇒ Object

:nodoc:



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/arjdbc/hsqldb/adapter.rb', line 105

def quote(value, column = nil) # :nodoc:
  return value.quoted_id if value.respond_to?(:quoted_id)

  case value
  when String
    column_type = column && column.type
    if column_type == :binary
      "X'#{value.unpack("H*")[0]}'"
    elsif column_type == :integer ||
        column.respond_to?(:primary) && column.primary && column.klass != String
      value.to_i.to_s
    else
      "'#{quote_string(value)}'"
    end
  when Time
    if column && column.type == :time
      "'#{value.strftime("%H:%M:%S")}'"
    else
      super
    end
  else
    super
  end
end

#quote_column_name(name) ⇒ Object

:nodoc:



130
131
132
133
134
135
136
137
# File 'lib/arjdbc/hsqldb/adapter.rb', line 130

def quote_column_name(name) # :nodoc:
  name = name.to_s
  if name =~ /[-]/
    %Q{"#{name.upcase}"}
  else
    name
  end
end

#recreate_database(name, options = {}) ⇒ Object



205
206
207
# File 'lib/arjdbc/hsqldb/adapter.rb', line 205

def recreate_database(name, options = {})
  drop_database(name)
end

#remove_index(table_name, options = {}) ⇒ Object



201
202
203
# File 'lib/arjdbc/hsqldb/adapter.rb', line 201

def remove_index(table_name, options = {})
  execute "DROP INDEX #{quote_column_name(index_name(table_name, options))}"
end

#rename_column(table_name, column_name, new_column_name) ⇒ Object

:nodoc:



153
154
155
# File 'lib/arjdbc/hsqldb/adapter.rb', line 153

def rename_column(table_name, column_name, new_column_name) #:nodoc:
  execute "ALTER TABLE #{table_name} ALTER COLUMN #{column_name} RENAME TO #{new_column_name}"
end

#rename_table(name, new_name) ⇒ Object



164
165
166
# File 'lib/arjdbc/hsqldb/adapter.rb', line 164

def rename_table(name, new_name)
  execute "ALTER TABLE #{name} RENAME TO #{new_name}"
end

#tablesObject

filter out system tables (that otherwise end up in db/schema.rb) JdbcConnection#tables now takes an optional block filter so we can screen out rows corresponding to system tables. HSQLDB names its system tables SYSTEM.*, but H2 seems to name them without any kind of convention



197
198
199
# File 'lib/arjdbc/hsqldb/adapter.rb', line 197

def tables
  @connection.tables.select { |row| row.to_s !~ /^system_/i }
end

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

Maps logical Rails types to MySQL-specific data types.



158
159
160
161
162
# File 'lib/arjdbc/hsqldb/adapter.rb', line 158

def type_to_sql(type, limit = nil, precision = nil, scale = nil)
  return super if defined?(::Jdbc::H2) || type.to_s != 'integer' || limit == nil

  type
end