Class: Blufin::YmlSqlTemplateWriter
- Inherits:
-
YmlWriterBase
- Object
- YmlWriterBase
- Blufin::YmlSqlTemplateWriter
- Defined in:
- lib/core/yml_writers/yml_sql_template_writer.rb
Constant Summary
Constants inherited from YmlWriterBase
Blufin::YmlWriterBase::AUTO_TYPES, Blufin::YmlWriterBase::PLACEHOLDER_CLASS, Blufin::YmlWriterBase::PLACEHOLDER_IMPORT, Blufin::YmlWriterBase::PLACEHOLDER_PACKAGE, Blufin::YmlWriterBase::PLACEHOLDER_SCHEMA
Instance Method Summary collapse
-
#get_data_path(schema) ⇒ Object
Array.
-
#get_structure_files(schema) ⇒ Object
Array.
-
#get_template_path(schema) ⇒ Object
Array.
-
#initialize(site, schema_data) ⇒ Object
constructor
Void.
-
#write ⇒ Object
Void.
Methods inherited from YmlWriterBase
#get_base_path, #get_java_path, #get_package, #get_package_from_file, #write_file_java
Constructor Details
#initialize(site, schema_data) ⇒ Object
Returns void.
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 |
# File 'lib/core/yml_writers/yml_sql_template_writer.rb', line 6 def initialize(site, schema_data) @schema_data = schema_data raise RuntimeError, 'Could not find valid @schema_data.' if @schema_data.nil? || !@schema_data.is_a?(Hash) @site = Blufin::SiteResolver::validate_site(site) @site_name = Blufin::SiteResolver::get_site_name(@site) @site_path = Blufin::SiteResolver::get_site_location(@site) @site_domain = Blufin::SiteResolver::get_site_domain(@site) @site_location = "#{Blufin::SiteResolver::get_site_location(@site)}/" @yml_enum_scanner = Blufin::ScannerJavaEnums.new(@site) # Wipe out all previous files. Blufin::YmlSchemaValidator::VALID_SCHEMAS_GENERATE.each do |schema| path_to_wipe_out = "#{get_template_path(schema)}/#{schema}" if Blufin::Files::path_exists(path_to_wipe_out) if Blufin::Files::get_files_in_dir(path_to_wipe_out).any? Blufin::Terminal::command('rm *', path_to_wipe_out, false, false) end end end end |
Instance Method Details
#get_data_path(schema) ⇒ Object
Returns Array.
208 209 210 211 212 213 214 215 216 217 |
# File 'lib/core/yml_writers/yml_sql_template_writer.rb', line 208 def get_data_path(schema) if schema == Blufin::YmlSchemaValidator::CONFIG || schema == Blufin::YmlSchemaValidator::MOCK path_to_blufin_ruby = Blufin::Config::get_path('Paths', 'BlufinRuby') return nil if path_to_blufin_ruby.nil? "#{path_to_blufin_ruby}/#{App::Opt::BLUFIN}#{App::Opt::OPT_PATH_SQL}/data" else "#{get_base_path(@site)}/#{Blufin::Site::PATH_TO_SQL_DATA}" end end |
#get_structure_files(schema) ⇒ Object
Returns Array.
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/core/yml_writers/yml_sql_template_writer.rb', line 220 def get_structure_files(schema) if schema == Blufin::YmlSchemaValidator::CONFIG path_to_blufin_ruby = Blufin::Config::get_path('Paths', 'BlufinRuby') return nil if path_to_blufin_ruby.nil? structure_file = "#{path_to_blufin_ruby}/#{App::Opt::BLUFIN}#{App::Opt::OPT_PATH_SQL}/structure/#{App::MySQL::CONFIG_FILENAME_STRUCTURE}" foreign_key_file = "#{path_to_blufin_ruby}/#{App::Opt::BLUFIN}#{App::Opt::OPT_PATH_SQL}/structure/#{App::MySQL::CONFIG_FILENAME_STRUCTURE_FKS}" elsif schema == Blufin::YmlSchemaValidator::MOCK path_to_blufin_ruby = Blufin::Config::get_path('Paths', 'BlufinRuby') return nil if path_to_blufin_ruby.nil? structure_file = "#{path_to_blufin_ruby}/#{App::Opt::BLUFIN}#{App::Opt::OPT_PATH_SQL}/structure/#{App::MySQL::MOCK_FILENAME_STRUCTURE}" foreign_key_file = "#{path_to_blufin_ruby}/#{App::Opt::BLUFIN}#{App::Opt::OPT_PATH_SQL}/structure/#{App::MySQL::MOCK_FILENAME_STRUCTURE_FKS}" else structure_file = "#{get_base_path(@site)}/#{Blufin::Site::PATH_TO_SQL_STRUCTURE}/#{Blufin::SiteResolver::get_site_name(@site)}-#{schema}.sql" foreign_key_file = "#{get_base_path(@site)}/#{Blufin::Site::PATH_TO_SQL_STRUCTURE}/#{Blufin::SiteResolver::get_site_name(@site)}-fks-#{schema}.sql" end [structure_file, foreign_key_file] end |
#get_template_path(schema) ⇒ Object
Returns Array.
197 198 199 200 201 202 203 204 205 |
# File 'lib/core/yml_writers/yml_sql_template_writer.rb', line 197 def get_template_path(schema) if schema == Blufin::YmlSchemaValidator::CONFIG || schema == Blufin::YmlSchemaValidator::MOCK path_to_blufin_ruby = Blufin::Config::get_path('Paths', 'BlufinRuby') return nil if path_to_blufin_ruby.nil? "#{path_to_blufin_ruby}/#{App::Opt::BLUFIN}#{App::Opt::OPT_PATH_SQL}/templates" else "#{get_base_path(@site)}/#{Blufin::Site::PATH_TO_SQL_TEMPLATES}" end end |
#write ⇒ Object
Returns void.
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 |
# File 'lib/core/yml_writers/yml_sql_template_writer.rb', line 33 def write valid_sql_data_files = [] @schema_data.each do |schema, schema_data| # This filters out the "mock" schema used for testing. next if schema == Blufin::YmlSchemaValidator::MOCK schema_data.each do |table, table_data| if table_data.length > 0 @mock_data = [] @column_names = [] fkey_ref_last = '' template_path = get_template_path(schema) data_path = get_data_path(schema) # Skip if user doesn't have the PATH_TO_RUBY config value. next if template_path.nil? || data_path.nil? if schema == Blufin::YmlSchemaValidator::CONFIG site_schema = App::MySQL::CONFIG_SCHEMA elsif schema == Blufin::YmlSchemaValidator::MOCK site_schema = App::MySQL::MOCK_SCHEMA else site_schema = "#{@site_name}-#{schema}" end table_data.each do |column_name, column_data| # Skip Placeholders 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] # Skip Transient Objects. next if [Blufin::ScannerJavaEmbeddedObjects::OBJECT].include?(@type) # Create FKEY LINK tables entry templates if column_name =~ /\A[a-z_.]+\[#{Blufin::YmlSchemaValidator::LINK}\]/ target_table = column_name.dup.gsub("[#{Blufin::YmlSchemaValidator::LINK}]", '').split('.') table_name = "#{table}_to_#{target_table[1]}_link" fk_link_lines = [] fk_link_lines << "INSERT IGNORE INTO `#{site_schema}`.#{table_name} (" fk_link_lines << " `#{table}_id`," fk_link_lines << " `#{target_table[1]}_id`" fk_link_lines << ') VALUES (' fk_link_lines << " '0'," fk_link_lines << " '0'" fk_link_lines << ');' # Write template file. full_file_path = "#{template_path}/#{schema}/template-#{table_name.gsub('_', '-')}.sql" Blufin::Files.write_file(full_file_path, fk_link_lines) # Write data file. sql_data_path = "#{data_path}/#{schema}/" sql_data_file = "data-#{table_name.gsub('_', '-')}.sql" sql_data_file_final = "#{sql_data_path}#{sql_data_file}" Blufin::Files.write_file(sql_data_file_final, ['']) unless Blufin::Files::file_exists(sql_data_file_final) Blufin::Terminal::command("git add #{sql_data_file_final}", get_base_path(@site), false, false) if schema != Blufin::YmlSchemaValidator::CONFIG && schema != Blufin::YmlSchemaValidator::MOCK valid_sql_data_files << sql_data_file_final next end fkey_ref = column_data[Blufin::YmlSchemaValidator::FKEY].nil? ? ',' : ", -- FK: #{column_data[Blufin::YmlSchemaValidator::FKEY]}" fkey_ref_last = fkey_ref.dup fkey_ref_last[0] = '' if @type == Blufin::YmlSchemaValidator::TYPE_BOOLEAN @mock_data << " 0#{fkey_ref}" elsif @type == Blufin::YmlSchemaValidator::TYPE_DATE # MUST BE BEFORE DATETIME TYPES @mock_data << " '#{Blufin::DateTimeUtils::now('%Y-%m-%d')}'#{fkey_ref}" elsif Blufin::YmlSchemaValidator::DATETIME_TYPES.include?(@type) @mock_data << " '#{Blufin::DateTimeUtils::now('%Y-%m-%d %H:%I:%S')}'#{fkey_ref}" elsif @type =~ Blufin::YmlSchemaValidator::REGEX_DECIMAL @mock_data << " 0.00#{fkey_ref}" elsif Blufin::YmlSchemaValidator::INT_TYPES.include?(@type) || @type == Blufin::YmlSchemaValidator::TYPE_INT_TINY || @type == Blufin::YmlSchemaValidator::TYPE_INT_SMALL || @type == Blufin::YmlSchemaValidator::TYPE_INT_BIG @mock_data << " 0#{fkey_ref}" elsif @type == Blufin::YmlSchemaValidator::TYPE_INT_BIG @mock_data << " 0#{fkey_ref}" elsif Blufin::YmlSchemaValidator::TEXT_TYPES.include?(@type) @mock_data << " '#{column_name.upcase}'#{fkey_ref}" elsif @type =~ Blufin::YmlSchemaValidator::REGEX_ENUM @mock_data << " '#{Blufin::YmlCommon::enum_value_extractor(@type, @site).join('|')}'#{fkey_ref}" elsif @type =~ Blufin::YmlSchemaValidator::REGEX_ENUM_CUSTOM @mock_data << " '#{@yml_enum_scanner.get_enum_custom_values_for(Blufin::YmlCommon::enum_name_extractor(@type)).join('|')}'#{fkey_ref}" elsif @type =~ Blufin::YmlSchemaValidator::REGEX_ENUM_SYSTEM @mock_data << " '#{@yml_enum_scanner.get_enum_system_values_for(Blufin::YmlCommon::enum_name_extractor(@type)).join('|')}'#{fkey_ref}" elsif @type =~ Blufin::YmlSchemaValidator::REGEX_VARCHAR @mock_data << " '#{column_name.upcase}'#{fkey_ref}" else raise RuntimeError, "Unrecognized type: #{@type}" end @column_names << " `#{column_name}`#{fkey_ref}" end # Remove the last comma (',') from SQL insert statements -- @mock_data if @mock_data[@mock_data.length - 1] =~ /,\s--\sFK/ @mock_data[@mock_data.length - 1].gsub!(/,\s--\sFK/, ' -- FK') else @mock_data[@mock_data.length - 1] = @mock_data[@mock_data.length - 1][0...-1] end # Remove the last comma (',') from SQL insert statements -- @@column_names if @column_names[@column_names.length - 1] =~ /,\s--\sFK/ @column_names[@column_names.length - 1].gsub!(/,\s--\sFK/, ' -- FK') else @column_names[@column_names.length - 1] = @column_names[@column_names.length - 1][0...-1] end @final_lines = [] @final_lines << "INSERT IGNORE INTO `#{site_schema}`.#{table} (" @final_lines.push(*@column_names) @final_lines << ') VALUES (' @final_lines.push(*@mock_data) @final_lines << ');' full_file_path = "#{template_path}/#{schema}/template-#{table.gsub('_', '-')}.sql" Blufin::Files.write_file(full_file_path, @final_lines).gsub(@site_location, '') # Create Data file if it doesn't exist. sql_data_path = "#{data_path}/#{schema}/" sql_data_file = "data-#{table.gsub('_', '-')}.sql" sql_data_file_final = "#{sql_data_path}#{sql_data_file}" Blufin::YmlCommon::validate_file_and_contents(@site, sql_data_file_final, [], [], nil, schema != Blufin::YmlSchemaValidator::CONFIG) && schema != Blufin::YmlSchemaValidator::MOCK valid_sql_data_files << sql_data_file_final end end end data_path_config = get_data_path(Blufin::YmlSchemaValidator::CONFIG) data_path_mock = get_data_path(Blufin::YmlSchemaValidator::MOCK) # Check for ROGUE file(s). all_data_files = [] all_data_files = all_data_files + Blufin::Files::get_files_in_dir(get_data_path(Blufin::YmlSchemaValidator::APP)) all_data_files = all_data_files + Blufin::Files::get_files_in_dir(get_data_path(Blufin::YmlSchemaValidator::COMMON)) all_data_files = all_data_files + Blufin::Files::get_files_in_dir(data_path_config) unless data_path_config.nil? all_data_files = all_data_files + Blufin::Files::get_files_in_dir(data_path_mock) unless data_path_mock.nil? all_data_files.uniq! all_data_files.each do |existing_data_file| Blufin::Terminal::output("Found rogue file: #{Blufin::Terminal::format_directory(existing_data_file)}", Blufin::Terminal::MSG_WARNING) unless valid_sql_data_files.include?(existing_data_file) end end |