Module: JdbcSpec::Derby
Defined Under Namespace
Modules: Column
Constant Summary
collapse
- COLUMN_INFO_STMT =
"SELECT C.COLUMNNAME, C.REFERENCEID, C.COLUMNNUMBER FROM SYS.SYSCOLUMNS C, SYS.SYSTABLES T WHERE T.TABLEID = '%s' AND T.TABLEID = C.REFERENCEID ORDER BY C.COLUMNNUMBER"
- COLUMN_TYPE_STMT =
"SELECT COLUMNDATATYPE, COLUMNDEFAULT FROM SYS.SYSCOLUMNS WHERE REFERENCEID = '%s' AND COLUMNNAME = '%s'"
- AUTO_INC_STMT =
"SELECT AUTOINCREMENTSTART, AUTOINCREMENTINC, COLUMNNAME, REFERENCEID, COLUMNDEFAULT FROM SYS.SYSCOLUMNS WHERE REFERENCEID = '%s' AND COLUMNNAME = '%s'"
- 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
-
#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
-
#create_column(name, refid, colno) ⇒ Object
I don’t think this method is ever called ??? (stepheneb).
-
#execute(sql, name = nil) ⇒ Object
-
#expand_double_quotes(name) ⇒ Object
-
#modify_types(tp) ⇒ Object
-
#primary_key(table_name) ⇒ Object
-
#primary_keys(table_name) ⇒ Object
-
#quote_column_name(name) ⇒ Object
For DDL it appears you can quote “” column names, but in queries (like insert it errors out?).
-
#quoted_false ⇒ Object
-
#quoted_true ⇒ Object
-
#recreate_database(db_name) ⇒ Object
-
#reinstate_auto_increment(name, refid, coldef) ⇒ 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 speficied column type to a SQL string.
#alter_table, #copy_table, #copy_table_contents, #copy_table_indexes, #move_table
Class Method Details
.adapter_matcher(name) ⇒ Object
14
15
16
|
# File 'lib/jdbc_adapter/jdbc_derby.rb', line 14
def self.adapter_matcher(name, *)
name =~ /derby/i ? self : false
end
|
.column_selector ⇒ Object
18
19
20
|
# File 'lib/jdbc_adapter/jdbc_derby.rb', line 18
def self.column_selector
[/derby/i, lambda {|cfg,col| col.extend(::JdbcSpec::Derby::Column)}]
end
|
.extended(*args) ⇒ Object
43
44
45
|
# File 'lib/jdbc_adapter/jdbc_derby.rb', line 43
def self.extended(*args)
monkey_rails
end
|
.included(*args) ⇒ Object
47
48
49
|
# File 'lib/jdbc_adapter/jdbc_derby.rb', line 47
def self.included(*args)
monkey_rails
end
|
.monkey_rails ⇒ Object
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/jdbc_adapter/jdbc_derby.rb', line 22
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
67
68
69
|
# File 'lib/jdbc_adapter/jdbc_derby.rb', line 67
def adapter_name 'Derby'
end
|
#add_column(table_name, column_name, type, options = {}) ⇒ Object
207
208
209
210
211
212
213
214
215
216
217
|
# File 'lib/jdbc_adapter/jdbc_derby.rb', line 207
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_column_options!(sql, options) ⇒ Object
Override default – fix case where ActiveRecord passes :default => nil, :null => true
91
92
93
94
95
|
# File 'lib/jdbc_adapter/jdbc_derby.rb', line 91
def add_column_options!(sql, options)
options.delete(:default) if options.has_key?(:default) && options[:default].nil?
options.delete(:null) if options.has_key?(:null) && (options[:null].nil? || options[:null] == true)
super
end
|
#add_limit_offset!(sql, options) ⇒ Object
404
405
406
407
408
409
410
411
412
|
# File 'lib/jdbc_adapter/jdbc_derby.rb', line 404
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
135
136
137
138
|
# File 'lib/jdbc_adapter/jdbc_derby.rb', line 135
def add_quotes(name)
return name unless name
%Q{"#{name}"}
end
|
#auto_increment_stmt(tname, cname) ⇒ Object
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
# File 'lib/jdbc_adapter/jdbc_derby.rb', line 187
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)
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
|
# File 'lib/jdbc_adapter/jdbc_derby.rb', line 317
def change_column(table_name, column_name, type, options = {})
if options.include?(:null)
if options.delete(:null) == false
execute "ALTER TABLE #{table_name} ALTER COLUMN #{column_name} NOT NULL"
else
execute "ALTER TABLE #{table_name} ALTER COLUMN #{column_name} NULL"
end
end
unless options.empty?
begin
execute "ALTER TABLE #{table_name} ALTER COLUMN #{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 #{table_name} SET #{temp_new_column_name} = CAST(#{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
97
98
99
|
# File 'lib/jdbc_adapter/jdbc_derby.rb', line 97
def classes_for_table_name(table)
ActiveRecord::Base.send(:subclasses).select {|klass| klass.table_name == table}
end
|
#columns(table_name, name = nil) ⇒ Object
366
367
368
|
# File 'lib/jdbc_adapter/jdbc_derby.rb', line 366
def columns(table_name, name=nil)
@connection.columns_internal(table_name.to_s, name, derby_schema)
end
|
#create_column(name, refid, colno) ⇒ Object
I don’t think this method is ever called ??? (stepheneb)
233
234
235
236
237
238
239
240
241
242
243
244
245
246
|
# File 'lib/jdbc_adapter/jdbc_derby.rb', line 233
def create_column(name, refid, colno)
stmt = COLUMN_TYPE_STMT % [refid, strip_quotes(name)]
coldef = ""
data = execute(stmt).first
if data
coldef << add_quotes(expand_double_quotes(strip_quotes(name)))
coldef << " "
coldef << data['columndatatype']
if !reinstate_auto_increment(name, refid, coldef) && data['columndefault']
coldef << " DEFAULT " << data['columndefault']
end
end
coldef
end
|
#execute(sql, name = nil) ⇒ Object
219
220
221
222
223
224
225
226
227
228
229
|
# File 'lib/jdbc_adapter/jdbc_derby.rb', line 219
def execute(sql, name = nil)
if sql =~ /^\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
146
147
148
149
|
# File 'lib/jdbc_adapter/jdbc_derby.rb', line 146
def expand_double_quotes(name)
return name unless name && name['"']
name.gsub(/"/,'""')
end
|
#modify_types(tp) ⇒ Object
82
83
84
85
86
87
88
|
# File 'lib/jdbc_adapter/jdbc_derby.rb', line 82
def modify_types(tp)
tp[:primary_key] = "int generated by default as identity NOT NULL PRIMARY KEY"
tp[:integer][:limit] = nil
tp[:string][:limit] = 256
tp[:boolean] = {:name => "smallint"}
tp
end
|
#primary_key(table_name) ⇒ Object
116
117
118
|
# File 'lib/jdbc_adapter/jdbc_derby.rb', line 116
def primary_key(table_name) primary_keys(table_name).first
end
|
#primary_keys(table_name) ⇒ Object
362
363
364
|
# File 'lib/jdbc_adapter/jdbc_derby.rb', line 362
def primary_keys(table_name)
@connection.primary_keys table_name.to_s.upcase
end
|
#quote_column_name(name) ⇒ Object
For DDL it appears you can quote “” column names, but in queries (like insert it errors out?)
381
382
383
384
385
386
387
388
389
390
391
392
393
394
|
# File 'lib/jdbc_adapter/jdbc_derby.rb', line 381
def quote_column_name(name) name = name.to_s
if /^(references|integer|key|group|year)$/i =~ name
%Q{"#{name.upcase}"}
elsif /[A-Z]/ =~ name && /[a-z]/ =~ name
%Q{"#{name}"}
elsif name =~ /[\s-]/
%Q{"#{name.upcase}"}
elsif name =~ /^[_\d]/
%Q{"#{name.upcase}"}
else
name
end
end
|
#quoted_false ⇒ Object
400
401
402
|
# File 'lib/jdbc_adapter/jdbc_derby.rb', line 400
def quoted_false
'0'
end
|
#quoted_true ⇒ Object
396
397
398
|
# File 'lib/jdbc_adapter/jdbc_derby.rb', line 396
def quoted_true
'1'
end
|
#recreate_database(db_name) ⇒ Object
374
375
376
377
378
|
# File 'lib/jdbc_adapter/jdbc_derby.rb', line 374
def recreate_database(db_name)
tables.each do |t|
drop_table t
end
end
|
#reinstate_auto_increment(name, refid, coldef) ⇒ Object
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
# File 'lib/jdbc_adapter/jdbc_derby.rb', line 151
def reinstate_auto_increment(name, refid, coldef)
stmt = AUTO_INC_STMT % [refid, strip_quotes(name)]
data = execute(stmt).first
if data
start = data['autoincrementstart']
if start
coldef << " GENERATED " << (data['columndefault'].nil? ? "ALWAYS" : "BY DEFAULT ")
coldef << "AS IDENTITY (START WITH "
coldef << start
coldef << ", INCREMENT BY "
coldef << data['autoincrementinc']
coldef << ")"
return true
end
end
false
end
|
#remove_column(table_name, column_name) ⇒ Object
Support for removing columns added via derby bug issue: issues.apache.org/jira/browse/DERBY-1489
This feature has not made it into a formal release and is not in Java 6. If the normal strategy fails we fall back on a strategy by creating a new table without the new column and there after moving the data to the new
299
300
301
302
303
304
305
306
307
|
# File 'lib/jdbc_adapter/jdbc_derby.rb', line 299
def remove_column(table_name, column_name)
begin
execute "ALTER TABLE #{table_name} DROP COLUMN #{column_name} RESTRICT"
rescue
alter_table(table_name) do |definition|
definition.columns.delete(definition[column_name])
end
end
end
|
#remove_index(table_name, options) ⇒ Object
120
121
122
|
# File 'lib/jdbc_adapter/jdbc_derby.rb', line 120
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
354
355
356
357
358
359
360
|
# File 'lib/jdbc_adapter/jdbc_derby.rb', line 354
def rename_column(table_name, column_name, new_column_name) begin
execute "ALTER TABLE #{table_name} ALTER RENAME COLUMN #{column_name} TO #{new_column_name}"
rescue
alter_table(table_name, :rename => {column_name => new_column_name})
end
end
|
#rename_table(name, new_name) ⇒ Object
124
125
126
|
# File 'lib/jdbc_adapter/jdbc_derby.rb', line 124
def rename_table(name, new_name)
execute "RENAME TABLE #{name} TO #{new_name}"
end
|
#reset_pk_sequence!(table, pk = nil, sequence = nil) ⇒ Object
107
108
109
110
111
112
113
114
|
# File 'lib/jdbc_adapter/jdbc_derby.rb', line 107
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.
102
103
104
105
|
# File 'lib/jdbc_adapter/jdbc_derby.rb', line 102
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
140
141
142
143
144
|
# File 'lib/jdbc_adapter/jdbc_derby.rb', line 140
def strip_quotes(str)
return str unless str
return str unless /^(["']).*\1$/ =~ str
str[1..-2]
end
|
#structure_dump ⇒ Object
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
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
|
# File 'lib/jdbc_adapter/jdbc_derby.rb', line 250
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
370
371
372
|
# File 'lib/jdbc_adapter/jdbc_derby.rb', line 370
def tables
@connection.tables(nil, derby_schema)
end
|
#type_to_sql(type, limit = nil, precision = nil, scale = nil) ⇒ Object
Convert the speficied column type to a SQL string. In Derby, :integers cannot specify a limit.
75
76
77
78
79
80
|
# File 'lib/jdbc_adapter/jdbc_derby.rb', line 75
def type_to_sql(type, limit = nil, precision = nil, scale = nil) return super unless type == :integer
native = native_database_types[type.to_s.downcase.to_sym]
native.is_a?(Hash) ? native[:name] : native
end
|