-
Notifications
You must be signed in to change notification settings - Fork 558
/
schema_statements.rb
765 lines (657 loc) · 32.5 KB
/
schema_statements.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
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
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
# frozen_string_literal: true
module ActiveRecord
module ConnectionAdapters
module SQLServer
module SchemaStatements
def native_database_types
@native_database_types ||= initialize_native_database_types.freeze
end
def create_table(table_name, **options)
res = super
clear_cache!
res
end
def drop_table(table_name, **options)
# Mimic CASCADE option as best we can.
if options[:force] == :cascade
execute_procedure(:sp_fkeys, pktable_name: table_name).each do |fkdata|
fktable = fkdata["FKTABLE_NAME"]
fkcolmn = fkdata["FKCOLUMN_NAME"]
pktable = fkdata["PKTABLE_NAME"]
pkcolmn = fkdata["PKCOLUMN_NAME"]
remove_foreign_key fktable, name: fkdata["FK_NAME"]
execute "DELETE FROM #{quote_table_name(fktable)} WHERE #{quote_column_name(fkcolmn)} IN ( SELECT #{quote_column_name(pkcolmn)} FROM #{quote_table_name(pktable)} )"
end
end
if options[:if_exists] && version_year < 2016
execute "IF EXISTS(SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = #{quote(table_name)}) DROP TABLE #{quote_table_name(table_name)}", "SCHEMA"
else
super
end
end
def indexes(table_name)
data = select("EXEC sp_helpindex #{quote(table_name)}", "SCHEMA") rescue []
data.reduce([]) do |indexes, index|
if index['index_description'].match?(/primary key/)
indexes
else
name = index['index_name']
unique = index['index_description'].match?(/unique/)
where = select_value("SELECT [filter_definition] FROM sys.indexes WHERE name = #{quote(name)}", "SCHEMA")
orders = {}
columns = []
index['index_keys'].split(",").each do |column|
column.strip!
if column.end_with?("(-)")
column.gsub! "(-)", ""
orders[column] = :desc
end
columns << column
end
indexes << IndexDefinition.new(table_name, name, unique, columns, where: where, orders: orders)
end
end
end
def columns(table_name)
return [] if table_name.blank?
column_definitions(table_name).map do |ci|
sqlserver_options = ci.slice :ordinal_position, :is_primary, :is_identity, :table_name
sql_type_metadata = fetch_type_metadata ci[:type], sqlserver_options
new_column(
ci[:name],
ci[:default_value],
sql_type_metadata,
ci[:null],
ci[:default_function],
ci[:collation],
nil,
sqlserver_options
)
end
end
def new_column(name, default, sql_type_metadata, null, default_function = nil, collation = nil, comment = nil, sqlserver_options = {})
SQLServer::Column.new(
name,
default,
sql_type_metadata,
null,
default_function,
collation: collation,
comment: comment,
**sqlserver_options
)
end
def primary_keys(table_name)
primaries = primary_keys_select(table_name)
primaries.present? ? primaries : identity_columns(table_name).map(&:name)
end
def primary_keys_select(table_name)
identifier = database_prefix_identifier(table_name)
database = identifier.fully_qualified_database_quoted
sql = %{
SELECT #{lowercase_schema_reflection_sql('KCU.COLUMN_NAME')} AS [name]
FROM #{database}.INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS KCU
LEFT OUTER JOIN #{database}.INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS TC
ON KCU.CONSTRAINT_NAME = TC.CONSTRAINT_NAME
AND KCU.CONSTRAINT_NAME = TC.CONSTRAINT_NAME
AND KCU.CONSTRAINT_CATALOG = TC.CONSTRAINT_CATALOG
AND KCU.CONSTRAINT_SCHEMA = TC.CONSTRAINT_SCHEMA
AND TC.CONSTRAINT_TYPE = N'PRIMARY KEY'
WHERE KCU.TABLE_NAME = #{prepared_statements ? '@0' : quote(identifier.object)}
AND KCU.TABLE_SCHEMA = #{identifier.schema.blank? ? 'schema_name()' : (prepared_statements ? '@1' : quote(identifier.schema))}
AND TC.CONSTRAINT_TYPE = N'PRIMARY KEY'
ORDER BY KCU.ORDINAL_POSITION ASC
}.gsub(/[[:space:]]/, " ")
binds = []
nv128 = SQLServer::Type::UnicodeVarchar.new limit: 128
binds << Relation::QueryAttribute.new("TABLE_NAME", identifier.object, nv128)
binds << Relation::QueryAttribute.new("TABLE_SCHEMA", identifier.schema, nv128) unless identifier.schema.blank?
internal_exec_query(sql, "SCHEMA", binds).map { |row| row["name"] }
end
def rename_table(table_name, new_name, **options)
validate_table_length!(new_name) unless options[:_uses_legacy_table_name]
schema_cache.clear_data_source_cache!(table_name.to_s)
schema_cache.clear_data_source_cache!(new_name.to_s)
execute "EXEC sp_rename '#{table_name}', '#{new_name}'"
rename_table_indexes(table_name, new_name, **options)
end
def remove_column(table_name, column_name, type = nil, **options)
raise ArgumentError.new("You must specify at least one column name. Example: remove_column(:people, :first_name)") if column_name.is_a? Array
return if options[:if_exists] == true && !column_exists?(table_name, column_name)
remove_check_constraints(table_name, column_name)
remove_default_constraint(table_name, column_name)
remove_indexes(table_name, column_name)
execute "ALTER TABLE #{quote_table_name(table_name)} DROP COLUMN #{quote_column_name(column_name)}"
end
def change_column(table_name, column_name, type, options = {})
sql_commands = []
indexes = []
if type == :datetime
# If no precision then default it to 6.
options[:precision] = 6 unless options.key?(:precision)
# If there is precision then column must be of type 'datetime2'.
type = :datetime2 unless options[:precision].nil?
end
column_object = schema_cache.columns(table_name).find { |c| c.name.to_s == column_name.to_s }
without_constraints = options.key?(:default) || options.key?(:limit)
default = if !options.key?(:default) && column_object
column_object.default
else
options[:default]
end
if without_constraints || (column_object && column_object.type != type.to_sym)
remove_default_constraint(table_name, column_name)
indexes = indexes(table_name).select { |index| index.columns.include?(column_name.to_s) }
remove_indexes(table_name, column_name)
end
sql_commands << "UPDATE #{quote_table_name(table_name)} SET #{quote_column_name(column_name)}=#{quote_default_expression(options[:default], column_object)} WHERE #{quote_column_name(column_name)} IS NULL" if !options[:null].nil? && options[:null] == false && !options[:default].nil?
alter_command = "ALTER TABLE #{quote_table_name(table_name)} ALTER COLUMN #{quote_column_name(column_name)} #{type_to_sql(type, limit: options[:limit], precision: options[:precision], scale: options[:scale])}"
alter_command += " COLLATE #{options[:collation]}" if options[:collation].present?
alter_command += " NOT NULL" if !options[:null].nil? && options[:null] == false
sql_commands << alter_command
if without_constraints
default = quote_default_expression(default, column_object || column_for(table_name, column_name))
sql_commands << "ALTER TABLE #{quote_table_name(table_name)} ADD CONSTRAINT #{default_constraint_name(table_name, column_name)} DEFAULT #{default} FOR #{quote_column_name(column_name)}"
end
# Add any removed indexes back
indexes.each do |index|
sql_commands << "CREATE INDEX #{quote_table_name(index.name)} ON #{quote_table_name(table_name)} (#{index.columns.map { |c| quote_column_name(c) }.join(', ')})"
end
sql_commands.each { |c| execute(c) }
clear_cache!
end
def change_column_default(table_name, column_name, default_or_changes)
clear_cache!
column = column_for(table_name, column_name)
return unless column
remove_default_constraint(table_name, column_name)
default = extract_new_default_value(default_or_changes)
execute "ALTER TABLE #{quote_table_name(table_name)} ADD CONSTRAINT #{default_constraint_name(table_name, column_name)} DEFAULT #{quote_default_expression(default, column)} FOR #{quote_column_name(column_name)}"
clear_cache!
end
def rename_column(table_name, column_name, new_column_name)
clear_cache!
identifier = SQLServer::Utils.extract_identifiers("#{table_name}.#{column_name}")
execute_procedure :sp_rename, identifier.quoted, new_column_name, "COLUMN"
rename_column_indexes(table_name, column_name, new_column_name)
clear_cache!
end
def rename_index(table_name, old_name, new_name)
raise ArgumentError, "Index name '#{new_name}' on table '#{table_name}' is too long; the limit is #{index_name_length} characters" if new_name.length > index_name_length
identifier = SQLServer::Utils.extract_identifiers("#{table_name}.#{old_name}")
execute_procedure :sp_rename, identifier.quoted, new_name, "INDEX"
end
def remove_index!(table_name, index_name)
execute "DROP INDEX #{quote_column_name(index_name)} ON #{quote_table_name(table_name)}"
end
def build_change_column_definition(table_name, column_name, type, **options) # :nodoc:
td = create_table_definition(table_name)
cd = td.new_column_definition(column_name, type, **options)
ChangeColumnDefinition.new(cd, column_name)
end
def build_change_column_default_definition(table_name, column_name, default_or_changes) # :nodoc:
column = column_for(table_name, column_name)
return unless column
default = extract_new_default_value(default_or_changes)
ChangeColumnDefaultDefinition.new(column, default)
end
def foreign_keys(table_name)
identifier = SQLServer::Utils.extract_identifiers(table_name)
fk_info = execute_procedure :sp_fkeys, nil, identifier.schema, nil, identifier.object, identifier.schema
grouped_fk = fk_info.group_by { |row| row["FK_NAME"] }.values.each { |group| group.sort_by! { |row| row["KEY_SEQ"] } }
grouped_fk.map do |group|
row = group.first
options = {
name: row["FK_NAME"],
on_update: extract_foreign_key_action("update", row["FK_NAME"]),
on_delete: extract_foreign_key_action("delete", row["FK_NAME"])
}
if group.one?
options[:column] = row["FKCOLUMN_NAME"]
options[:primary_key] = row["PKCOLUMN_NAME"]
else
options[:column] = group.map { |row| row["FKCOLUMN_NAME"] }
options[:primary_key] = group.map { |row| row["PKCOLUMN_NAME"] }
end
ForeignKeyDefinition.new(identifier.object, row["PKTABLE_NAME"], options)
end
end
def extract_foreign_key_action(action, fk_name)
case select_value("SELECT #{action}_referential_action_desc FROM sys.foreign_keys WHERE name = '#{fk_name}'")
when "CASCADE" then :cascade
when "SET_NULL" then :nullify
end
end
def check_constraints(table_name)
sql = <<~SQL
select chk.name AS 'name',
chk.definition AS 'expression'
from sys.check_constraints chk
inner join sys.tables st on chk.parent_object_id = st.object_id
where
st.name = '#{table_name}'
SQL
chk_info = internal_exec_query(sql, "SCHEMA")
chk_info.map do |row|
options = {
name: row["name"]
}
expression = row["expression"]
expression = expression[1..-2] if expression.start_with?("(") && expression.end_with?(")")
CheckConstraintDefinition.new(table_name, expression, options)
end
end
def type_to_sql(type, limit: nil, precision: nil, scale: nil, **)
type_limitable = %w(string integer float char nchar varchar nvarchar).include?(type.to_s)
limit = nil unless type_limitable
case type.to_s
when "integer"
case limit
when 1 then "tinyint"
when 2 then "smallint"
when 3..4, nil then "integer"
when 5..8 then "bigint"
else raise(ActiveRecordError, "No integer type has byte size #{limit}. Use a numeric with precision 0 instead.")
end
when "time" # https://learn.microsoft.com/en-us/sql/t-sql/data-types/time-transact-sql
column_type_sql = type.to_s
if precision
if (0..7) === precision
column_type_sql << "(#{precision})"
else
raise(ActiveRecordError, "The time type has precision of #{precision}. The allowed range of precision is from 0 to 7")
end
end
column_type_sql
when "datetime2"
column_type_sql = super
if precision
if (0..7) === precision
column_type_sql << "(#{precision})"
else
raise(ActiveRecordError, "The datetime2 type has precision of #{precision}. The allowed range of precision is from 0 to 7")
end
end
column_type_sql
when "datetimeoffset"
column_type_sql = super
if precision
if (0..7) === precision
column_type_sql << "(#{precision})"
else
raise(ActiveRecordError, "The datetimeoffset type has precision of #{precision}. The allowed range of precision is from 0 to 7")
end
end
column_type_sql
else
super
end
end
# In SQL Server only the first column added should have the `ADD` keyword.
def add_timestamps(table_name, **options)
fragments = add_timestamps_for_alter(table_name, **options)
fragments[1..].each { |fragment| fragment.sub!('ADD ', '') }
execute "ALTER TABLE #{quote_table_name(table_name)} #{fragments.join(', ')}"
end
def columns_for_distinct(columns, orders)
order_columns = orders.reject(&:blank?).map { |s|
s = s.to_sql unless s.is_a?(String)
s.gsub(/\s+(?:ASC|DESC)\b/i, "")
.gsub(/\s+NULLS\s+(?:FIRST|LAST)\b/i, "")
}.reject(&:blank?).map.with_index { |column, i| "#{column} AS alias_#{i}" }
(order_columns << super).join(", ")
end
def update_table_definition(table_name, base)
SQLServer::Table.new(table_name, base)
end
def change_column_null(table_name, column_name, null, default = nil)
validate_change_column_null_argument!(null)
table_id = SQLServer::Utils.extract_identifiers(table_name)
column_id = SQLServer::Utils.extract_identifiers(column_name)
column = column_for(table_name, column_name)
if !null.nil? && null == false && !default.nil?
execute("UPDATE #{table_id} SET #{column_id}=#{quote(default)} WHERE #{column_id} IS NULL")
end
sql = "ALTER TABLE #{table_id} ALTER COLUMN #{column_id} #{type_to_sql column.type, limit: column.limit, precision: column.precision, scale: column.scale}"
sql += " NOT NULL" if !null.nil? && null == false
execute sql
end
def create_schema_dumper(options)
SQLServer::SchemaDumper.create(self, options)
end
def create_schema(schema_name, authorization = nil)
sql = "CREATE SCHEMA [#{schema_name}]"
sql += " AUTHORIZATION [#{authorization}]" if authorization
execute sql
end
def change_table_schema(schema_name, table_name)
execute "ALTER SCHEMA [#{schema_name}] TRANSFER [#{table_name}]"
end
def drop_schema(schema_name)
execute "DROP SCHEMA [#{schema_name}]"
end
# Returns an array of schema names.
def schema_names
sql = <<~SQL.squish
SELECT name
FROM sys.schemas
WHERE
name NOT LIKE 'db_%' AND
name NOT IN ('INFORMATION_SCHEMA', 'sys', 'guest')
SQL
query_values(sql, "SCHEMA")
end
private
def data_source_sql(name = nil, type: nil)
scope = quoted_scope(name, type: type)
table_schema = lowercase_schema_reflection_sql('TABLE_SCHEMA')
table_name = lowercase_schema_reflection_sql('TABLE_NAME')
database = scope[:database].present? ? "#{scope[:database]}." : ""
table_catalog = scope[:database].present? ? quote(scope[:database]) : "DB_NAME()"
sql = "SELECT "
sql += " CASE"
sql += " WHEN #{table_schema} = 'dbo' THEN #{table_name}"
sql += " ELSE CONCAT(#{table_schema}, '.', #{table_name})"
sql += " END"
sql += " FROM #{database}INFORMATION_SCHEMA.TABLES WITH (NOLOCK)"
sql += " WHERE TABLE_CATALOG = #{table_catalog}"
sql += " AND TABLE_SCHEMA = #{quote(scope[:schema])}" if scope[:schema]
sql += " AND TABLE_NAME = #{quote(scope[:name])}" if scope[:name]
sql += " AND TABLE_TYPE = #{quote(scope[:type])}" if scope[:type]
sql += " ORDER BY #{table_name}"
sql
end
def quoted_scope(name = nil, type: nil)
identifier = SQLServer::Utils.extract_identifiers(name)
{}.tap do |scope|
scope[:database] = identifier.database if identifier.database
scope[:schema] = identifier.schema || "dbo" if name.present?
scope[:name] = identifier.object if identifier.object
scope[:type] = type if type
end
end
# === SQLServer Specific ======================================== #
def initialize_native_database_types
{
primary_key: "bigint NOT NULL IDENTITY(1,1) PRIMARY KEY",
primary_key_nonclustered: "bigint NOT NULL IDENTITY(1,1) PRIMARY KEY NONCLUSTERED",
integer: { name: "int", limit: 4 },
bigint: { name: "bigint" },
boolean: { name: "bit" },
decimal: { name: "decimal" },
money: { name: "money" },
smallmoney: { name: "smallmoney" },
float: { name: "float" },
real: { name: "real" },
date: { name: "date" },
datetime: { name: "datetime" },
datetime2: { name: "datetime2" },
datetimeoffset: { name: "datetimeoffset" },
smalldatetime: { name: "smalldatetime" },
timestamp: { name: "datetime2(6)" },
time: { name: "time" },
char: { name: "char" },
varchar: { name: "varchar", limit: 8000 },
varchar_max: { name: "varchar(max)" },
text_basic: { name: "text" },
nchar: { name: "nchar" },
string: { name: "nvarchar", limit: 4000 },
text: { name: "nvarchar(max)" },
ntext: { name: "ntext" },
binary_basic: { name: "binary" },
varbinary: { name: "varbinary", limit: 8000 },
binary: { name: "varbinary(max)" },
uuid: { name: "uniqueidentifier" },
ss_timestamp: { name: "timestamp" },
json: { name: "nvarchar(max)" }
}
end
def column_definitions(table_name)
identifier = database_prefix_identifier(table_name)
database = identifier.fully_qualified_database_quoted
view_exists = view_exists?(table_name)
if view_exists
sql = <<~SQL
SELECT LOWER(c.COLUMN_NAME) AS [name], c.COLUMN_DEFAULT AS [default]
FROM #{database}.INFORMATION_SCHEMA.COLUMNS c
WHERE c.TABLE_NAME = #{quote(view_table_name(table_name))}
SQL
results = internal_exec_query(sql, "SCHEMA")
default_functions = results.each.with_object({}) { |row, out| out[row["name"]] = row["default"] }.compact
end
sql = column_definitions_sql(database, identifier)
binds = []
nv128 = SQLServer::Type::UnicodeVarchar.new(limit: 128)
binds << Relation::QueryAttribute.new("TABLE_NAME", identifier.object, nv128)
binds << Relation::QueryAttribute.new("TABLE_SCHEMA", identifier.schema, nv128) unless identifier.schema.blank?
results = internal_exec_query(sql, "SCHEMA", binds)
raise ActiveRecord::StatementInvalid, "Table '#{table_name}' doesn't exist" if results.empty?
columns = results.map do |ci|
col = {
name: ci["name"],
numeric_scale: ci["numeric_scale"],
numeric_precision: ci["numeric_precision"],
datetime_precision: ci["datetime_precision"],
collation: ci["collation"],
ordinal_position: ci["ordinal_position"],
length: ci["length"]
}
col[:table_name] = view_exists ? view_table_name(table_name) : table_name
col[:type] = column_type(ci: ci)
col[:default_value], col[:default_function] = default_value_and_function(default: ci['default_value'],
name: ci['name'],
type: col[:type],
original_type: ci['type'],
view_exists: view_exists,
table_name: table_name,
default_functions: default_functions)
col[:null] = ci['is_nullable'].to_i == 1
col[:is_primary] = ci['is_primary'].to_i == 1
if [true, false].include?(ci['is_identity'])
col[:is_identity] = ci['is_identity']
else
col[:is_identity] = ci['is_identity'].to_i == 1
end
col
end
columns
end
def default_value_and_function(default:, name:, type:, original_type:, view_exists:, table_name:, default_functions:)
if default.nil? && view_exists
view_column = views_real_column_name(table_name, name).downcase
default = default_functions[view_column] if view_column.present?
end
case default
when nil
[nil, nil]
when /\A\((\w+\(\))\)\Z/
default_function = Regexp.last_match[1]
[nil, default_function]
when /\A\(N'(.*)'\)\Z/m
string_literal = SQLServer::Utils.unquote_string(Regexp.last_match[1])
[string_literal, nil]
when /CREATE DEFAULT/mi
[nil, nil]
else
type = case type
when /smallint|int|bigint/ then original_type
else type
end
value = default.match(/\A\((.*)\)\Z/m)[1]
value = select_value("SELECT CAST(#{value} AS #{type}) AS value", "SCHEMA")
[value, nil]
end
end
def column_type(ci:)
case ci['type']
when /^bit|image|text|ntext|datetime$/
ci['type']
when /^datetime2|datetimeoffset$/i
"#{ci['type']}(#{ci['datetime_precision']})"
when /^time$/i
"#{ci['type']}(#{ci['datetime_precision']})"
when /^numeric|decimal$/i
"#{ci['type']}(#{ci['numeric_precision']},#{ci['numeric_scale']})"
when /^float|real$/i
"#{ci['type']}"
when /^char|nchar|varchar|nvarchar|binary|varbinary|bigint|int|smallint$/
ci['length'].to_i == -1 ? "#{ci['type']}(max)" : "#{ci['type']}(#{ci['length']})"
else
ci['type']
end
end
def column_definitions_sql(database, identifier)
object_name = prepared_statements ? "@0" : quote(identifier.object)
schema_name = if identifier.schema.blank?
"schema_name()"
else
prepared_statements ? "@1" : quote(identifier.schema)
end
%{
SELECT
#{lowercase_schema_reflection_sql('o.name')} AS [table_name],
#{lowercase_schema_reflection_sql('c.name')} AS [name],
t.name AS [type],
d.definition AS [default_value],
CASE
WHEN t.name IN ('decimal', 'bigint', 'int', 'money', 'numeric', 'smallint', 'smallmoney', 'tinyint')
THEN c.scale
END AS [numeric_scale],
CASE
WHEN t.name IN ('decimal', 'bigint', 'int', 'money', 'numeric', 'smallint', 'smallmoney', 'tinyint', 'real', 'float')
THEN c.precision
END AS [numeric_precision],
CASE
WHEN t.name IN ('date', 'datetime', 'datetime2', 'datetimeoffset', 'smalldatetime', 'time')
THEN c.scale
END AS [datetime_precision],
c.collation_name AS [collation],
ROW_NUMBER() OVER (ORDER BY c.column_id) AS [ordinal_position],
CASE
WHEN t.name IN ('nchar', 'nvarchar') AND c.max_length > 0
THEN c.max_length / 2
ELSE c.max_length
END AS [length],
CASE c.is_nullable
WHEN 1
THEN 1
END AS [is_nullable],
CASE
WHEN ic.object_id IS NOT NULL
THEN 1
END AS [is_primary],
c.is_identity AS [is_identity]
FROM #{database}.sys.columns c
INNER JOIN #{database}.sys.objects o
ON c.object_id = o.object_id
INNER JOIN #{database}.sys.schemas s
ON o.schema_id = s.schema_id
INNER JOIN #{database}.sys.types t
ON c.system_type_id = t.system_type_id
AND c.user_type_id = t.user_type_id
LEFT OUTER JOIN #{database}.sys.default_constraints d
ON c.object_id = d.parent_object_id
AND c.default_object_id = d.object_id
LEFT OUTER JOIN #{database}.sys.key_constraints k
ON c.object_id = k.parent_object_id
AND k.type = 'PK'
LEFT OUTER JOIN #{database}.sys.index_columns ic
ON k.parent_object_id = ic.object_id
AND k.unique_index_id = ic.index_id
AND c.column_id = ic.column_id
WHERE
o.name = #{object_name}
AND s.name = #{schema_name}
ORDER BY
c.column_id
}.gsub(/[ \t\r\n]+/, " ").strip
end
def remove_columns_for_alter(table_name, *column_names, **options)
first, *rest = column_names
# return an array like this [DROP COLUMN col_1, col_2, col_3]. Abstract adapter joins fragments with ", "
[remove_column_for_alter(table_name, first)] + rest.map { |column_name| quote_column_name(column_name) }
end
def remove_check_constraints(table_name, column_name)
constraints = select_values "SELECT CONSTRAINT_NAME FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE where TABLE_NAME = '#{quote_string(table_name)}' and COLUMN_NAME = '#{quote_string(column_name)}'", "SCHEMA"
constraints.each do |constraint|
execute "ALTER TABLE #{quote_table_name(table_name)} DROP CONSTRAINT #{quote_column_name(constraint)}"
end
end
def remove_default_constraint(table_name, column_name)
# If their are foreign keys in this table, we could still get back a 2D array, so flatten just in case.
execute_procedure(:sp_helpconstraint, table_name, "nomsg").flatten.select do |row|
row["constraint_type"] == "DEFAULT on column #{column_name}"
end.each do |row|
execute "ALTER TABLE #{quote_table_name(table_name)} DROP CONSTRAINT #{row['constraint_name']}"
end
end
def remove_indexes(table_name, column_name)
indexes(table_name).select { |index| index.columns.include?(column_name.to_s) }.each do |index|
remove_index(table_name, name: index.name)
end
end
# === SQLServer Specific (Misc Helpers) ========================= #
# Parses just the table name from the SQL. Table name does not include database/schema/etc.
def get_table_name(sql)
tn = get_raw_table_name(sql)
SQLServer::Utils.extract_identifiers(tn).object
end
# Parses the raw table name that is used in the SQL. Table name could include database/schema/etc.
def get_raw_table_name(sql)
return if sql.blank?
s = sql.gsub(/^\s*EXEC sp_executesql N'/i, "")
if s.match?(/^\s*INSERT INTO.*/i)
s.split(/INSERT INTO/i)[1]
.split(/OUTPUT INSERTED/i)[0]
.split(/(DEFAULT)?\s+VALUES/i)[0]
.match(/\s*([^(]*)/i)[0]
elsif s.match?(/^\s*UPDATE\s+.*/i)
s.match(/UPDATE\s+([^\(\s]+)\s*/i)[1]
else
s.match(/FROM\s+((\[[^\(\]]+\])|[^\(\s]+)\s*/i)[1]
end.strip
end
def default_constraint_name(table_name, column_name)
"DF_#{table_name}_#{column_name}"
end
def lowercase_schema_reflection_sql(node)
lowercase_schema_reflection ? "LOWER(#{node})" : node
end
# === SQLServer Specific (View Reflection) ====================== #
def view_table_name(table_name)
view_info = view_information(table_name)
view_info.present? ? get_table_name(view_info["VIEW_DEFINITION"]) : table_name
end
def view_information(table_name)
@view_information ||= {}
@view_information[table_name] ||= begin
identifier = SQLServer::Utils.extract_identifiers(table_name)
information_query_table = identifier.database.present? ? "[#{identifier.database}].[INFORMATION_SCHEMA].[VIEWS]" : "[INFORMATION_SCHEMA].[VIEWS]"
view_info = select_one("SELECT * FROM #{information_query_table} WITH (NOLOCK) WHERE TABLE_NAME = #{quote(identifier.object)}", "SCHEMA").to_h
if view_info.present?
if view_info['VIEW_DEFINITION'].blank? || view_info['VIEW_DEFINITION'].length == 4000
view_info['VIEW_DEFINITION'] = begin
select_values("EXEC sp_helptext #{identifier.object_quoted}", "SCHEMA").join
rescue
warn "No view definition found, possible permissions problem.\nPlease run GRANT VIEW DEFINITION TO your_user;"
nil
end
end
end
view_info
end
end
def views_real_column_name(table_name, column_name)
view_definition = view_information(table_name)['VIEW_DEFINITION']
return column_name if view_definition.blank?
# Remove "CREATE VIEW ... AS SELECT ..." and then match the column name.
match_data = view_definition.sub(/CREATE\s+VIEW.*AS\s+SELECT\s/, '').match(/([\w-]*)\s+AS\s+#{column_name}\W/im)
match_data ? match_data[1] : column_name
end
def create_table_definition(*args, **options)
SQLServer::TableDefinition.new(self, *args, **options)
end
end
end
end
end