Module: ArJdbc::Derby
Defined Under Namespace
Modules: Column
Constant Summary
collapse
- AUTO_INC_STMT2 =
"SELECT AUTOINCREMENTSTART, AUTOINCREMENTINC, COLUMNNAME, REFERENCEID, COLUMNDEFAULT FROM SYS.SYSCOLUMNS WHERE REFERENCEID = (SELECT T.TABLEID FROM SYS.SYSTABLES T WHERE T.TABLENAME = '%s') AND COLUMNNAME = '%s'"
- SIZEABLE =
%w(VARCHAR CLOB BLOB)
Class Method Summary
collapse
Instance Method Summary
collapse
-
#adapter_name ⇒ Object
-
#add_column(table_name, column_name, type, options = {}) ⇒ Object
-
#add_column_options!(sql, options) ⇒ Object
Override default – fix case where ActiveRecord passes :default => nil, :null => true.
-
#add_limit_offset!(sql, options) ⇒ Object
-
#add_quotes(name) ⇒ Object
-
#arel2_visitors ⇒ Object
-
#auto_increment_stmt(tname, cname) ⇒ Object
-
#change_column(table_name, column_name, type, options = {}) ⇒ Object
-
#classes_for_table_name(table) ⇒ Object
-
#columns(table_name, name = nil) ⇒ Object
-
#distinct(columns, order_by) ⇒ Object
SELECT DISTINCT clause for a given set of columns and a given ORDER BY clause.
-
#execute(sql, name = nil) ⇒ Object
-
#expand_double_quotes(name) ⇒ Object
-
#index_name_length ⇒ Object
-
#modify_types(tp) ⇒ Object
-
#primary_keys(table_name) ⇒ Object
-
#quote_column_name(name) ⇒ Object
-
#quoted_false ⇒ Object
-
#quoted_true ⇒ Object
-
#recreate_database(db_name) ⇒ Object
-
#remove_column(table_name, column_name) ⇒ Object
-
#remove_index(table_name, options) ⇒ Object
-
#rename_column(table_name, column_name, new_column_name) ⇒ Object
-
#rename_table(name, new_name) ⇒ Object
-
#reset_pk_sequence!(table, pk = nil, sequence = nil) ⇒ Object
-
#reset_sequence!(table, column, sequence = nil) ⇒ Object
Set the sequence to the max value of the table’s column.
-
#strip_quotes(str) ⇒ Object
-
#structure_dump ⇒ Object
-
#tables ⇒ Object
-
#type_to_sql(type, limit = nil, precision = nil, scale = nil) ⇒ Object
Convert the specified column type to a SQL string.
#alter_table, #copy_table, #copy_table_contents, #copy_table_indexes, #move_table
Class Method Details
.column_selector ⇒ Object
5
6
7
|
# File 'lib/arjdbc/derby/adapter.rb', line 5
def self.column_selector
[/derby/i, lambda {|cfg,col| col.extend(::ArJdbc::Derby::Column)}]
end
|
.extended(*args) ⇒ Object
30
31
32
|
# File 'lib/arjdbc/derby/adapter.rb', line 30
def self.extended(*args)
monkey_rails
end
|
.included(*args) ⇒ Object
34
35
36
|
# File 'lib/arjdbc/derby/adapter.rb', line 34
def self.included(*args)
monkey_rails
end
|
.monkey_rails ⇒ Object
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
# File 'lib/arjdbc/derby/adapter.rb', line 9
def self.monkey_rails
unless @already_monkeyd
::ActiveRecord::Associations::ClassMethods.module_eval do
private
def select_limited_ids_list(options, join_dependency)
connection.select_all(
construct_finder_sql_for_association_limiting(options, join_dependency),
"#{name} Load IDs For Limited Eager Loading"
).collect { |row| connection.quote(row[primary_key], columns_hash[primary_key]) }.join(", ")
end
end
@already_monkeyd = true
end
end
|
Instance Method Details
#adapter_name ⇒ Object
58
59
60
|
# File 'lib/arjdbc/derby/adapter.rb', line 58
def adapter_name 'Derby'
end
|
#add_column(table_name, column_name, type, options = {}) ⇒ Object
168
169
170
171
172
|
# File 'lib/arjdbc/derby/adapter.rb', line 168
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_column_options!(sql, options) ⇒ Object
Override default – fix case where ActiveRecord passes :default => nil, :null => true
97
98
99
100
101
|
# File 'lib/arjdbc/derby/adapter.rb', line 97
def add_column_options!(sql, options)
options.delete(:default) if options.has_key?(:default) && options[:default].nil?
sql << " DEFAULT #{quote(options.delete(:default))}" if options.has_key?(:default)
super
end
|
#add_limit_offset!(sql, options) ⇒ Object
330
331
332
333
334
335
336
337
338
|
# File 'lib/arjdbc/derby/adapter.rb', line 330
def add_limit_offset!(sql, options) if options[:offset]
sql << " OFFSET #{options[:offset]} ROWS"
end
if options[:limit]
sql << " FETCH FIRST #{options[:limit]} ROWS ONLY"
end
end
|
#add_quotes(name) ⇒ Object
132
133
134
135
|
# File 'lib/arjdbc/derby/adapter.rb', line 132
def add_quotes(name)
return name unless name
%Q{"#{name}"}
end
|
#arel2_visitors ⇒ Object
62
63
64
65
|
# File 'lib/arjdbc/derby/adapter.rb', line 62
def arel2_visitors
require 'arel/visitors/derby'
{'derby' => ::Arel::Visitors::Derby, 'jdbcderby' => ::Arel::Visitors::Derby}
end
|
#auto_increment_stmt(tname, cname) ⇒ Object
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
# File 'lib/arjdbc/derby/adapter.rb', line 148
def auto_increment_stmt(tname, cname)
stmt = AUTO_INC_STMT2 % [tname, strip_quotes(cname)]
data = execute(stmt).first
if data
start = data['autoincrementstart']
if start
coldef = ""
coldef << " GENERATED " << (data['columndefault'].nil? ? "ALWAYS" : "BY DEFAULT ")
coldef << "AS IDENTITY (START WITH "
coldef << start
coldef << ", INCREMENT BY "
coldef << data['autoincrementinc']
coldef << ")"
return coldef
end
end
""
end
|
#change_column(table_name, column_name, type, options = {}) ⇒ Object
Notes about changing in Derby:
http://db.apache.org/derby/docs/10.2/ref/rrefsqlj81859.html
We support changing columns using the strategy outlined in:
https://issues.apache.org/jira/browse/DERBY-1515
This feature has not made it into a formal release and is not in Java 6. We will need to conditionally support this somehow (supposed to arrive for 10.3.0.0)
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
|
# File 'lib/arjdbc/derby/adapter.rb', line 265
def change_column(table_name, column_name, type, options = {})
if options.include?(:null)
if options.delete(:null) == false
execute "ALTER TABLE #{quote_table_name(table_name)} ALTER COLUMN #{quote_column_name(column_name)} NOT NULL"
else
execute "ALTER TABLE #{quote_table_name(table_name)} ALTER COLUMN #{quote_column_name(column_name)} NULL"
end
end
unless options.empty?
begin
execute "ALTER TABLE #{quote_table_name(table_name)} ALTER COLUMN #{quote_column_name(column_name)} SET DATA TYPE #{type_to_sql(type, options[:limit])}"
rescue
transaction do
temp_new_column_name = "#{column_name}_newtype"
add_column table_name, temp_new_column_name, type, options
execute "UPDATE #{quote_table_name(table_name)} SET #{quote_column_name(temp_new_column_name)} = CAST(#{quote_column_name(column_name)} AS #{type_to_sql(type, options[:limit])})"
remove_column table_name, column_name
rename_column table_name, temp_new_column_name, column_name
end
end
end
end
|
#classes_for_table_name(table) ⇒ Object
103
104
105
|
# File 'lib/arjdbc/derby/adapter.rb', line 103
def classes_for_table_name(table)
ActiveRecord::Base.send(:subclasses).select {|klass| klass.table_name == table}
end
|
#columns(table_name, name = nil) ⇒ Object
304
305
306
|
# File 'lib/arjdbc/derby/adapter.rb', line 304
def columns(table_name, name=nil)
@connection.columns_internal(table_name.to_s, name, derby_schema)
end
|
#distinct(columns, order_by) ⇒ Object
SELECT DISTINCT clause for a given set of columns and a given ORDER BY clause.
Derby requires the ORDER BY columns in the select list for distinct queries, and requires that the ORDER BY include the distinct column.
distinct("posts.id", "posts.created_at desc")
Based on distinct method for PostgreSQL Adapter
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
# File 'lib/arjdbc/derby/adapter.rb', line 194
def distinct(columns, order_by)
return "DISTINCT #{columns}" if order_by.blank?
order_columns = order_by.split(',').collect { |s| s.split.first }
order_columns.delete_if(&:blank?)
order_columns = order_columns.zip((0...order_columns.size).to_a).map { |s,i| "#{s} AS alias_#{i}" }
sql = "DISTINCT #{columns}, #{order_columns * ', '}"
sql
end
|
#execute(sql, name = nil) ⇒ Object
174
175
176
177
178
179
180
181
182
183
184
|
# File 'lib/arjdbc/derby/adapter.rb', line 174
def execute(sql, name = nil)
if sql =~ /\A\s*(UPDATE|INSERT)/i
i = sql =~ /\swhere\s/im
if i
sql[i..-1] = sql[i..-1].gsub(/!=\s*NULL/, 'IS NOT NULL').gsub(/=\sNULL/i, 'IS NULL')
end
else
sql.gsub!(/= NULL/i, 'IS NULL')
end
super
end
|
#expand_double_quotes(name) ⇒ Object
143
144
145
146
|
# File 'lib/arjdbc/derby/adapter.rb', line 143
def expand_double_quotes(name)
return name unless name && name['"']
name.gsub(/"/,'""')
end
|
#index_name_length ⇒ Object
69
70
71
|
# File 'lib/arjdbc/derby/adapter.rb', line 69
def index_name_length
128
end
|
#modify_types(tp) ⇒ Object
86
87
88
89
90
91
92
93
94
|
# File 'lib/arjdbc/derby/adapter.rb', line 86
def modify_types(tp)
tp[:primary_key] = "int generated by default as identity NOT NULL PRIMARY KEY"
tp[:string][:limit] = 256
tp[:integer][:limit] = nil
tp[:boolean] = {:name => "smallint"}
tp[:timestamp][:limit] = nil
tp[:date][:limit] = nil
tp
end
|
#primary_keys(table_name) ⇒ Object
300
301
302
|
# File 'lib/arjdbc/derby/adapter.rb', line 300
def primary_keys(table_name)
@connection.primary_keys table_name.to_s.upcase
end
|
#quote_column_name(name) ⇒ Object
318
319
320
|
# File 'lib/arjdbc/derby/adapter.rb', line 318
def quote_column_name(name) %Q{"#{name.to_s.upcase.gsub(/"/, '""')}"}
end
|
#quoted_false ⇒ Object
326
327
328
|
# File 'lib/arjdbc/derby/adapter.rb', line 326
def quoted_false
'0'
end
|
#quoted_true ⇒ Object
322
323
324
|
# File 'lib/arjdbc/derby/adapter.rb', line 322
def quoted_true
'1'
end
|
#recreate_database(db_name) ⇒ Object
312
313
314
315
316
|
# File 'lib/arjdbc/derby/adapter.rb', line 312
def recreate_database(db_name)
tables.each do |t|
drop_table t
end
end
|
#remove_column(table_name, column_name) ⇒ Object
253
254
255
|
# File 'lib/arjdbc/derby/adapter.rb', line 253
def remove_column(table_name, column_name)
execute "ALTER TABLE #{quote_table_name(table_name)} DROP COLUMN #{quote_column_name(column_name)} RESTRICT"
end
|
#remove_index(table_name, options) ⇒ Object
122
123
124
|
# File 'lib/arjdbc/derby/adapter.rb', line 122
def remove_index(table_name, options) execute "DROP INDEX #{index_name(table_name, options)}"
end
|
#rename_column(table_name, column_name, new_column_name) ⇒ Object
296
297
298
|
# File 'lib/arjdbc/derby/adapter.rb', line 296
def rename_column(table_name, column_name, new_column_name) execute "RENAME COLUMN #{quote_table_name(table_name)}.#{quote_column_name(column_name)} TO #{quote_column_name(new_column_name)}"
end
|
#rename_table(name, new_name) ⇒ Object
126
127
128
|
# File 'lib/arjdbc/derby/adapter.rb', line 126
def rename_table(name, new_name)
execute "RENAME TABLE #{quote_table_name(name)} TO #{quote_table_name(new_name)}"
end
|
#reset_pk_sequence!(table, pk = nil, sequence = nil) ⇒ Object
113
114
115
116
117
118
119
120
|
# File 'lib/arjdbc/derby/adapter.rb', line 113
def reset_pk_sequence!(table, pk = nil, sequence = nil)
klasses = classes_for_table_name(table)
klass = klasses.nil? ? nil : klasses.first
pk = klass.primary_key unless klass.nil?
if pk && klass.columns_hash[pk].type == :integer
reset_sequence!(klass.table_name, pk)
end
end
|
#reset_sequence!(table, column, sequence = nil) ⇒ Object
Set the sequence to the max value of the table’s column.
108
109
110
111
|
# File 'lib/arjdbc/derby/adapter.rb', line 108
def reset_sequence!(table, column, sequence = nil)
mpk = select_value("SELECT MAX(#{quote_column_name(column)}) FROM #{quote_table_name(table)}")
execute("ALTER TABLE #{quote_table_name(table)} ALTER COLUMN #{quote_column_name(column)} RESTART WITH #{mpk.to_i + 1}")
end
|
#strip_quotes(str) ⇒ Object
137
138
139
140
141
|
# File 'lib/arjdbc/derby/adapter.rb', line 137
def strip_quotes(str)
return str unless str
return str unless /^(["']).*\1$/ =~ str
str[1..-2]
end
|
#structure_dump ⇒ Object
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
|
# File 'lib/arjdbc/derby/adapter.rb', line 211
def structure_dump definition=""
rs = @connection.connection.meta_data.getTables(nil,nil,nil,["TABLE"].to_java(:string))
while rs.next
tname = rs.getString(3)
definition << "CREATE TABLE #{tname} (\n"
rs2 = @connection.connection.meta_data.getColumns(nil,nil,tname,nil)
first_col = true
while rs2.next
col_name = add_quotes(rs2.getString(4));
default = ""
d1 = rs2.getString(13)
if d1 =~ /^GENERATED_/
default = auto_increment_stmt(tname, col_name)
elsif d1
default = " DEFAULT #{d1}"
end
type = rs2.getString(6)
col_size = rs2.getString(7)
nulling = (rs2.getString(18) == 'NO' ? " NOT NULL" : "")
create_col_string = add_quotes(expand_double_quotes(strip_quotes(col_name))) +
" " +
type +
(SIZEABLE.include?(type) ? "(#{col_size})" : "") +
nulling +
default
if !first_col
create_col_string = ",\n #{create_col_string}"
else
create_col_string = " #{create_col_string}"
end
definition << create_col_string
first_col = false
end
definition << ");\n\n"
end
definition
end
|
#tables ⇒ Object
308
309
310
|
# File 'lib/arjdbc/derby/adapter.rb', line 308
def tables
@connection.tables(nil, derby_schema)
end
|
#type_to_sql(type, limit = nil, precision = nil, scale = nil) ⇒ Object
Convert the specified column type to a SQL string. In Derby, the following cannot specify a limit:
-
integer
-
boolean (smallint)
-
timestamp
-
date
79
80
81
82
83
84
|
# File 'lib/arjdbc/derby/adapter.rb', line 79
def type_to_sql(type, limit = nil, precision = nil, scale = nil) return super unless [:integer, :boolean, :timestamp, :date].include? type
native = native_database_types[type.to_s.downcase.to_sym]
native.is_a?(Hash) ? native[:name] : native
end
|