/
reference.cr
111 lines (96 loc) · 3.51 KB
/
reference.cr
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
module Marten
module DB
module Management
module Column
class Reference < Base
include IsBuiltInColumn
@target_column : Base? = nil
@to_table : ::String
@to_column : ::String
getter to_column
getter to_table
def initialize(
@name : ::String,
to_table : ::String | Symbol,
to_column : ::String | Symbol,
@primary_key = false,
@foreign_key = true,
@null = false,
@unique = false,
@index = true
)
@to_table = to_table.to_s
@to_column = to_column.to_s
end
def clone
cloned = self.class.new(
name: name,
to_table: to_table,
to_column: to_column,
primary_key: primary_key?,
foreign_key: foreign_key?,
null: null?,
unique: unique?,
index: index?
)
# Ensures the target column is cloned too.
if !@target_column.nil?
cloned.target_column = @target_column.try(&.clone)
end
cloned
end
# Returns a boolean indicating whether the column is a foreign key.
def foreign_key?
@foreign_key
end
def same_config?(other : Base)
other.is_a?(Reference) &&
to_table == other.to_table &&
to_column == other.to_column &&
primary_key? == other.primary_key? &&
foreign_key? == other.foreign_key? &&
null? == other.null? &&
unique? == other.unique? &&
index? == other.index? &&
default == other.default
end
def serialize_args : ::String
args = [%{#{format_string_or_symbol(name)}}, %{#{format_string_or_symbol(type)}}]
args << %{to_table: #{format_string_or_symbol(to_table)}}
args << %{to_column: #{format_string_or_symbol(to_column)}}
args << %{primary_key: #{@primary_key}} if primary_key?
args << %{foreign_key: #{@foreign_key}} if !foreign_key?
args << %{null: #{@null}} if null?
args << %{unique: #{@unique}} if unique?
args << %{index: #{@index}} if !index?
args << %{default: #{default.inspect}} if !default.nil?
args.join(", ")
end
def sql_type(connection : Connection::Base) : ::String
ensured_target_column.sql_type(connection)
end
def sql_type_suffix(connection : Connection::Base) : ::String?
ensured_target_column.sql_type_suffix(connection)
end
# :nodoc:
def contribute_to_project(project : ProjectState) : Nil
target_table = project.tables.values.find! { |t| t.name == to_table }
@target_column = target_table.get_column(to_column).clone
@target_column.not_nil!.primary_key = false
if @target_column.is_a?(BigInt)
@target_column.as(BigInt).primary_key = false
@target_column.as(BigInt).auto = false
elsif @target_column.is_a?(Int)
@target_column.as(Int).primary_key = false
@target_column.as(Int).auto = false
end
end
protected setter target_column
private def ensured_target_column
@target_column.not_nil!
end
end
end
end
end
end