class CsvImportAnalyzer::SqlQueryBuilder
Attributes
create_query[RW]
csv_column_datatypes[RW]
import_query[RW]
min_max_bounds[RW]
nullable[RW]
sql_helper_options[RW]
Public Class Methods
new(args)
click to toggle source
Since Building SQL is dependent on multiple things, decided to go with an arguments hash that gets passed when creating an object for the class
# File lib/csv-import-analyzer/sql_query_builder.rb, line 13 def initialize(args) @options = args @create_query = {} @import_query = {} @csv_column_datatypes = args[:csv_column_datatypes] @nullable = args[:nullable] @sql_helper_options = {:tablename => tablename, :filename => @options[:filename], :delimiter => @options[:delimiter]} @mysql_helper.extend(CsvImportAnalyzer::MysqlQueryHelper) @pg_helper.extend(CsvImportAnalyzer::PgQueryHelper) end
Public Instance Methods
databases()
click to toggle source
# File lib/csv-import-analyzer/sql_query_builder.rb, line 28 def databases options[:database] end
delimiter()
click to toggle source
# File lib/csv-import-analyzer/sql_query_builder.rb, line 45 def delimiter options[:delimiter] end
filename()
click to toggle source
# File lib/csv-import-analyzer/sql_query_builder.rb, line 32 def filename return options[:filename] end
generate_query()
click to toggle source
Goes through each of the columns datatypes and prepares SQL statements for
1. Importing CSV files to database 2. Create table schema for the files
Makes a function call to return the metadata analysis of the file
# File lib/csv-import-analyzer/sql_query_builder.rb, line 63 def generate_query unless databases.nil? databases.each do |db| create_query[db] = ["create table #{tablename} ("] end csv_column_datatypes.each do |header, datatype| append_to_query = build_query_for_datatype(header, datatype) append_to_query.each do |key, value| create_query[key].push(value) end end prepare_sql_statements prepare_import_csv # Pass the prepared statements to options varaible. # Which gets passed on to print_metadata_analysis options[:create_query] = create_query options[:import_query] = import_query end print_metadata_analysis end
mysql_helper()
click to toggle source
# File lib/csv-import-analyzer/sql_query_builder.rb, line 49 def mysql_helper @mysql_helper end
options()
click to toggle source
# File lib/csv-import-analyzer/sql_query_builder.rb, line 24 def options @options end
pg_helper()
click to toggle source
# File lib/csv-import-analyzer/sql_query_builder.rb, line 53 def pg_helper @pg_helper end
tablename()
click to toggle source
# File lib/csv-import-analyzer/sql_query_builder.rb, line 36 def tablename # May be optimize this, not run all three operations everytime filename method is called ?? # May be creating filename as instance variable and using a double pipe will relive it from running everytime doesn't it?? tablename = File.basename(options[:original_filename]) tablename.gsub!(" ", "_") tablename.downcase! return tablename end
Private Instance Methods
build_query_for_datatype(header, datatype)
click to toggle source
Based on the database type set in options returns query part for the header (column name)
# File lib/csv-import-analyzer/sql_query_builder.rb, line 90 def build_query_for_datatype(header, datatype) query = {} databases.each do |db| if db == :mysql query[db] = mysql_helper.form_query_for_datatype(header: header, datatype: datatype) else query[db] = pg_helper.form_query_for_datatype(header: header, datatype: datatype) end end unless nullable.include?(header) query.keys.each do |db| query[db] << " not null" end end return query end
prepare_import_csv()
click to toggle source
based on database type set in options returns import query for the database
# File lib/csv-import-analyzer/sql_query_builder.rb, line 111 def prepare_import_csv databases.each do |db| if db == :mysql import_query[db] = mysql_helper.import_csv(tablename: tablename, filename: filename, delimiter: delimiter) elsif db == :pg import_query[db] = pg_helper.import_csv(tablename: tablename, filename: filename, delimiter: delimiter) end end end
prepare_sql_statements()
click to toggle source
prepares sql statements based on the query for each header formed earlier
# File lib/csv-import-analyzer/sql_query_builder.rb, line 124 def prepare_sql_statements begin databases.each do |db| create_query[db][0] = create_query[db][0] + " " + create_query[db][1] create_query[db].delete_at(1) create_query[db] = create_query[db].join(", ") create_query[db] << ");" end rescue TypeError => e end end
print_metadata_analysis()
click to toggle source
set’s the create query and import query’s in options these fields will be added to the metadata later instantiates MetadataAnalysis
and passes options hash
# File lib/csv-import-analyzer/sql_query_builder.rb, line 141 def print_metadata_analysis # options[:create_query] = create_query # options[:import_query] = import_query export = CsvImportAnalyzer::MetadataAnalysis.new(options) export.metadata_print end