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
|
# File 'lib/core/yml_writers/yml_sql_structure_writer.rb', line 20
def write
Blufin::YmlSchemaValidator::VALID_SCHEMAS_GENERATE.each do |schema|
sfs = get_structure_files(schema)
next if sfs.nil? structure_file = sfs[0]
foreign_key_file = sfs[1]
Blufin::Files::delete_file(structure_file)
Blufin::Files::delete_file(foreign_key_file)
end
@schema_data.each do |schema, schema_data|
next if schema == Blufin::YmlSchemaValidator::MOCK
fk_count = 0
idx_count = 0
@structure = []
@alter_table_lines = []
@fks = []
@link_tables = []
schema_data.each_with_index do |(table, table_data), idx|
if table_data.length > 0
primary_key = nil
indexes = []
indexes_unique = []
indexes_fulltext = []
foreign_keys = []
@structure << "-- Create #{table} table"
@structure << "CREATE TABLE `#{table}` ("
table_data.each do |column_name, column_data|
next if column_name =~ /\A(#{Blufin::YmlSchemaValidator::VALID_SCHEMAS_REGEX})\.[a-z_]+\[\]\z/ || column_name =~ /\A(#{Blufin::YmlSchemaValidator::VALID_SCHEMAS_REGEX})\.[a-z_]+\z/
@type = column_data[Blufin::YmlSchemaValidator::TYPE]
@fkey = !column_data[Blufin::YmlSchemaValidator::FKEY].nil?
next if [Blufin::ScannerJavaEmbeddedObjects::OBJECT].include?(@type)
if column_name =~ /\A[a-z_.]+\[#{Blufin::YmlSchemaValidator::LINK}\]/
@link_tables << [
"#{schema}.#{table}",
column_name.dup.gsub("[#{Blufin::YmlSchemaValidator::LINK}]", '')
]
next
end
if column_data[Blufin::YmlSchemaValidator::FLAG].nil?
@flags = nil
indexes << " INDEX `#{schema}_#{table}_#{column_name}_idx` (`#{column_name}`)," if @type =~ Blufin::YmlSchemaValidator::REGEX_ENUM
else
@flags = Blufin::YmlCommon::(column_data[Blufin::YmlSchemaValidator::FLAG])[0]
primary_key = column_name if @flags.primary_key
if @flags.index
if [Blufin::YmlSchemaValidator::TYPE_TEXT, Blufin::YmlSchemaValidator::TYPE_TEXT_LONG].include?(@type)
indexes_fulltext << " FULLTEXT `#{schema}_#{table}_#{column_name}_idx_fulltext` (`#{column_name}`),"
else
indexes << " INDEX `#{schema}_#{table}_#{column_name}_idx` (`#{column_name}`)," if @flags.unique.nil?
indexes_unique << " UNIQUE INDEX `#{schema}_#{table}_#{column_name}_uniq_idx` (`#{column_name}`)," if @flags.unique
end
end
end
unless column_data[Blufin::YmlSchemaValidator::FKEY].nil?
fk_count = fk_count + 1
fk_ref = column_data[Blufin::YmlSchemaValidator::FKEY].split('.') foreign_key_lines = []
foreign_key_lines << "ADD CONSTRAINT `fk_#{fk_count}`"
foreign_key_lines << " FOREIGN KEY (`#{column_name}`)"
foreign_key_lines << " REFERENCES `#{fk_ref[1]}` (`#{fk_ref[2]}`)"
foreign_key_lines << ' ON DELETE RESTRICT'
foreign_key_lines << ' ON UPDATE CASCADE,'
foreign_keys << foreign_key_lines
end
column_definition = " `#{column_name}`"
determine_type(column_definition, column_data)
determine_not_null(column_definition, column_data)
determine_auto_increment(column_definition)
(column_data[Blufin::YmlSchemaValidator::DESCRIPTION], column_definition)
@structure << "#{column_definition},"
end
@structure << " PRIMARY KEY (`#{primary_key}`)," if primary_key != nil
indexes_fulltext.each { |index_fulltext| @structure << index_fulltext } if indexes_fulltext.any?
indexes_unique.each { |index_unique| @structure << index_unique } if indexes_unique.any?
indexes.each { |index| @structure << index } if indexes.any?
if foreign_keys.any?
@alter_table_lines << "-- Add #{table} FKs"
@alter_table_lines << "ALTER TABLE `#{table}`"
foreign_keys.each do |foreign_key_lines|
foreign_key_lines.each do |foreign_key_line|
@alter_table_lines << foreign_key_line
end
end
@alter_table_lines[@alter_table_lines.length - 1] = @alter_table_lines[@alter_table_lines.length - 1][0...-1]
@alter_table_lines[@alter_table_lines.length - 1] << ";\n"
end
@structure[@structure.length - 1] = @structure[@structure.length - 1][0...-1]
@structure << ") ENGINE = InnoDB;#{idx == (schema_data.length - 1) ? '' : "\n"}"
end
end
if @link_tables.any?
@link_tables.each_with_index do |link_data, idx|
source_table = link_data[0].split('.')
target_table = link_data[1].split('.')
link_table_name = Blufin::YmlCommon::get_link_table_name(source_table[1], link_data[1])
@structure << "-- Add table to link '#{source_table[1]}' with '#{target_table[1]}'"
@structure << "CREATE TABLE `#{link_table_name}` ("
@structure << " `#{source_table[1]}_id` INT NOT NULL,"
@structure << " `#{target_table[1]}_id` INT NOT NULL"
@structure << ") ENGINE = InnoDB;#{idx == (@link_tables.length - 1) ? '' : "\n"}"
@fks << "ALTER TABLE `#{link_table_name}`"
idx_count = idx_count + 1
@fks << " ADD INDEX `idx_#{idx_count}` (`#{source_table[1]}_id` DESC),"
idx_count = idx_count + 1
@fks << " ADD INDEX `idx_#{idx_count}` (`#{target_table[1]}_id` DESC),"
@fks << " ADD UNIQUE INDEX `#{source_table[1]}_to_#{target_table[1]}_uniq_idx` (`#{source_table[1]}_id` DESC, `#{target_table[1]}_id` DESC),"
fk_count = fk_count + 1
@fks << " ADD CONSTRAINT `fk_#{fk_count}`"
@fks << " FOREIGN KEY (`#{source_table[1]}_id`)"
@fks << " REFERENCES `#{source_table[1]}` (`id`)"
@fks << ' ON DELETE RESTRICT'
@fks << ' ON UPDATE RESTRICT,'
fk_count = fk_count + 1
@fks << " ADD CONSTRAINT `fk_#{fk_count}`"
@fks << " FOREIGN KEY (`#{target_table[1]}_id`)"
@fks << " REFERENCES `#{target_table[1]}` (`id`)"
@fks << ' ON DELETE RESTRICT'
@fks << ' ON UPDATE RESTRICT;'
@fks << '' unless idx == @link_tables.length - 1
end
end
sfs = get_structure_files(schema)
next if sfs.nil? structure_file = sfs[0]
foreign_key_file = sfs[1]
Blufin::Files.write_file(structure_file, @structure).gsub(@site_location, '') if @structure.any?
Blufin::Files.write_file(foreign_key_file, @alter_table_lines + @fks).gsub(@site_location, '') if @alter_table_lines.any? || @fks.any?
end
end
|