Module: JdbcSpec::HSQLDB

Included in:
H2
Defined in:
lib/jdbc_adapter/jdbc_hsqldb.rb

Defined Under Namespace

Modules: Column

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.adapter_matcher(name) ⇒ Object



25
26
27
# File 'lib/jdbc_adapter/jdbc_hsqldb.rb', line 25

def self.adapter_matcher(name, *)
  name =~ /hsqldb/i ? self : false
end

.column_selectorObject



29
30
31
# File 'lib/jdbc_adapter/jdbc_hsqldb.rb', line 29

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

.extended(mod) ⇒ Object



19
20
21
22
23
# File 'lib/jdbc_adapter/jdbc_hsqldb.rb', line 19

def self.extended(mod)
  mod.class.class_eval do
    alias_chained_method :insert, :query_dirty, :insert
  end
end

Instance Method Details

#_execute(sql, name = nil) ⇒ Object

Override normal #_execute: See Rubyforge #11567



175
176
177
178
179
180
181
182
183
# File 'lib/jdbc_adapter/jdbc_hsqldb.rb', line 175

def _execute(sql, name = nil)
  if ::ActiveRecord::ConnectionAdapters::JdbcConnection::select?(sql)
    @connection.execute_query(sql)
  elsif ::ActiveRecord::ConnectionAdapters::JdbcConnection::insert?(sql)
    insert(sql, name)
  else
    @connection.execute_update(sql)
  end
end

#adapter_nameObject

:nodoc:



65
66
67
# File 'lib/jdbc_adapter/jdbc_hsqldb.rb', line 65

def adapter_name #:nodoc:
  defined?(::Jdbc::H2) ? 'H2' : 'Hsqldb'
end

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



127
128
129
130
131
132
133
134
135
136
137
# File 'lib/jdbc_adapter/jdbc_hsqldb.rb', line 127

def add_column(table_name, column_name, type, options = {})
  if option_not_null = options[:null] == false
    option_not_null = options.delete(:null)
  end
  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)
  if option_not_null
    alter_column_sql = "ALTER TABLE #{quote_table_name(table_name)} ALTER #{quote_column_name(column_name)} NOT NULL"
  end
end

#add_limit_offset!(sql, options) ⇒ Object

:nodoc:



185
186
187
188
189
190
191
192
193
# File 'lib/jdbc_adapter/jdbc_hsqldb.rb', line 185

def add_limit_offset!(sql, options) #:nodoc:
  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

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

:nodoc:



139
140
141
# File 'lib/jdbc_adapter/jdbc_hsqldb.rb', line 139

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:



143
144
145
# File 'lib/jdbc_adapter/jdbc_hsqldb.rb', line 143

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

#insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) ⇒ Object

:nodoc:



162
163
164
165
166
167
168
# File 'lib/jdbc_adapter/jdbc_hsqldb.rb', line 162

def insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) #:nodoc:
  log(sql,name) do
    @connection.execute_update(sql)
  end
  table = sql.split(" ", 4)[2]
  id_value || last_insert_id(table, nil)
end

#last_insert_id(table, sequence_name) ⇒ Object



170
171
172
# File 'lib/jdbc_adapter/jdbc_hsqldb.rb', line 170

def last_insert_id(table, sequence_name)
  Integer(select_value("CALL IDENTITY()"))
end

#modify_types(tp) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/jdbc_adapter/jdbc_hsqldb.rb', line 69

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:



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/jdbc_adapter/jdbc_hsqldb.rb', line 86

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*")}'"
    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:



106
107
108
109
110
111
112
113
# File 'lib/jdbc_adapter/jdbc_hsqldb.rb', line 106

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

#quote_string(str) ⇒ Object



115
116
117
# File 'lib/jdbc_adapter/jdbc_hsqldb.rb', line 115

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

#quoted_falseObject



123
124
125
# File 'lib/jdbc_adapter/jdbc_hsqldb.rb', line 123

def quoted_false
  '0'
end

#quoted_trueObject



119
120
121
# File 'lib/jdbc_adapter/jdbc_hsqldb.rb', line 119

def quoted_true
  '1'
end

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



205
206
207
# File 'lib/jdbc_adapter/jdbc_hsqldb.rb', line 205

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:



147
148
149
# File 'lib/jdbc_adapter/jdbc_hsqldb.rb', line 147

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



158
159
160
# File 'lib/jdbc_adapter/jdbc_hsqldb.rb', line 158

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



201
202
203
# File 'lib/jdbc_adapter/jdbc_hsqldb.rb', line 201

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.



152
153
154
155
156
# File 'lib/jdbc_adapter/jdbc_hsqldb.rb', line 152

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