Module: ArJdbc::HSQLDB

Included in:
H2
Defined in:
lib/arjdbc/hsqldb/adapter.rb

Defined Under Namespace

Modules: Column

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.arel2_visitors(config) ⇒ Object



43
44
45
46
# File 'lib/arjdbc/hsqldb/adapter.rb', line 43

def self.arel2_visitors(config)
  require 'arel/visitors/hsqldb'
  {}.tap {|v| %w(hsqldb jdbchsqldb).each {|a| v[a] = ::Arel::Visitors::HSQLDB } }
end

.column_selectorObject



3
4
5
# File 'lib/arjdbc/hsqldb/adapter.rb', line 3

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

Instance Method Details

#_execute(sql, name = nil) ⇒ Object



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

def _execute(sql, name = nil)
  result = super
  ActiveRecord::ConnectionAdapters::JdbcConnection::insert?(sql) ? last_insert_id : result
end

#adapter_nameObject

:nodoc:



39
40
41
# File 'lib/arjdbc/hsqldb/adapter.rb', line 39

def adapter_name #:nodoc:
  'Hsqldb'
end

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



106
107
108
109
110
# File 'lib/arjdbc/hsqldb/adapter.rb', line 106

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:



144
145
146
147
148
149
150
151
152
153
154
# File 'lib/arjdbc/hsqldb/adapter.rb', line 144

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:



112
113
114
# File 'lib/arjdbc/hsqldb/adapter.rb', line 112

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:



116
117
118
# File 'lib/arjdbc/hsqldb/adapter.rb', line 116

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



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

def create_database(name)
end

#drop_database(name) ⇒ Object



180
181
182
# File 'lib/arjdbc/hsqldb/adapter.rb', line 180

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

#last_insert_idObject



135
136
137
# File 'lib/arjdbc/hsqldb/adapter.rb', line 135

def last_insert_id
  Integer(select_value("CALL IDENTITY()"))
end

#modify_types(tp) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/arjdbc/hsqldb/adapter.rb', line 48

def modify_types(tp)
  tp[:primary_key] = "INTEGER GENERATED BY DEFAULT AS IDENTITY(START WITH 0) PRIMARY KEY"
  tp[:integer][:limit] = nil
  tp[:boolean][:limit] = nil
  # set text and float limits so we don't see odd scales tacked on
  # in migrations
  tp[:boolean] = { :name => "tinyint" }
  tp[:text][:limit] = nil
  tp[:float][:limit] = 17 if defined?(::Jdbc::H2)
  tp[:string][:limit] = 255
  tp[:datetime] = { :name => "DATETIME" }
  tp[:timestamp] = { :name => "DATETIME" }
  tp[:time] = { :name => "TIME" }
  tp[:date] = { :name => "DATE" }
  tp
end

#quote(value, column = nil) ⇒ Object

:nodoc:



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/arjdbc/hsqldb/adapter.rb', line 65

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

  case value
  when String
    if respond_to?(:h2_adapter) && value.empty?
      "''"
    elsif column && column.type == :binary
      "'#{value.unpack("H*")[0]}'"
    elsif column && (column.type == :integer ||
                     column.respond_to?(:primary) && column.primary && column.klass != String)
      value.to_i.to_s
    else
      "'#{quote_string(value)}'"
    end
  else
    super
  end
end

#quote_column_name(name) ⇒ Object

:nodoc:



85
86
87
88
89
90
91
92
# File 'lib/arjdbc/hsqldb/adapter.rb', line 85

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

#quote_string(str) ⇒ Object



94
95
96
# File 'lib/arjdbc/hsqldb/adapter.rb', line 94

def quote_string(str)
  str.gsub(/'/, "''")
end

#quoted_falseObject



102
103
104
# File 'lib/arjdbc/hsqldb/adapter.rb', line 102

def quoted_false
  '0'
end

#quoted_trueObject



98
99
100
# File 'lib/arjdbc/hsqldb/adapter.rb', line 98

def quoted_true
  '1'
end

#recreate_database(name) ⇒ Object



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

def recreate_database(name)
  drop_database(name)
end

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



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

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:



120
121
122
# File 'lib/arjdbc/hsqldb/adapter.rb', line 120

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



131
132
133
# File 'lib/arjdbc/hsqldb/adapter.rb', line 131

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

#tablesObject

override to filter out system tables that otherwise end up in db/schema.rb during migrations. 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



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

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.



125
126
127
128
129
# File 'lib/arjdbc/hsqldb/adapter.rb', line 125

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