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
|
# File 'lib/sqlserver/foreigner/sqlserver-adapter.rb', line 6
def foreign_keys(table_name)
fk_info = select_all %{
SELECT to_table.name AS to_table,
-- from_table.name AS from_table,
from_column.name AS from_column,
to_column.name AS primary_key,
foreign_key.name AS name,
CASE WHEN foreign_key.status & 4096 <> 0
THEN 'c' ELSE 'r'
END AS dependency
FROM sysobjects AS from_table,
sysforeignkeys AS f,
sysobjects AS to_table,
sysobjects AS foreign_key,
syscolumns AS from_column,
syscolumns AS to_column
WHERE from_table.type = 'U'
AND from_table.id = f.fkeyid
AND f.constid = foreign_key.id
AND f.rkeyid = to_table.id
AND f.fkey = from_column.colid
AND from_column.id = from_table.id
AND f.rkey = to_column.colid
AND to_column.id = to_table.id
AND from_table.name = '#{table_name}'
ORDER BY from_table.name, to_table.name, foreign_key.name, from_column.name
}
fk_info.map do |row|
options = {:column => row['from_column'], :name => row['name'], :primary_key => row['primary_key']}
options[:dependent] = case row['dependency']
when 'c' then :delete
when 'n' then :nullify end
ForeignKeyDefinition.new(table_name, row['to_table'], options)
end
end
|