Class: CsvImportAnalyzer::SqlQueryBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/csv-import-analyzer/sql_query_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ SqlQueryBuilder

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



13
14
15
16
17
18
19
20
21
22
# 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

Instance Attribute Details

#create_queryObject

Returns the value of attribute create_query.



7
8
9
# File 'lib/csv-import-analyzer/sql_query_builder.rb', line 7

def create_query
  @create_query
end

#csv_column_datatypesObject

Returns the value of attribute csv_column_datatypes.



7
8
9
# File 'lib/csv-import-analyzer/sql_query_builder.rb', line 7

def csv_column_datatypes
  @csv_column_datatypes
end

#import_queryObject

Returns the value of attribute import_query.



7
8
9
# File 'lib/csv-import-analyzer/sql_query_builder.rb', line 7

def import_query
  @import_query
end

#min_max_boundsObject

Returns the value of attribute min_max_bounds.



7
8
9
# File 'lib/csv-import-analyzer/sql_query_builder.rb', line 7

def min_max_bounds
  @min_max_bounds
end

#nullableObject

Returns the value of attribute nullable.



7
8
9
# File 'lib/csv-import-analyzer/sql_query_builder.rb', line 7

def nullable
  @nullable
end

#sql_helper_optionsObject

Returns the value of attribute sql_helper_options.



7
8
9
# File 'lib/csv-import-analyzer/sql_query_builder.rb', line 7

def sql_helper_options
  @sql_helper_options
end

Instance Method Details

#databasesObject



28
29
30
# File 'lib/csv-import-analyzer/sql_query_builder.rb', line 28

def databases
  options[:database]
end

#delimiterObject



45
46
47
# File 'lib/csv-import-analyzer/sql_query_builder.rb', line 45

def delimiter
  options[:delimiter]
end

#filenameObject



32
33
34
# File 'lib/csv-import-analyzer/sql_query_builder.rb', line 32

def filename
  return options[:filename]
end

#generate_queryObject

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



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/csv-import-analyzer/sql_query_builder.rb', line 63

def generate_query
  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
  
end

#mysql_helperObject



49
50
51
# File 'lib/csv-import-analyzer/sql_query_builder.rb', line 49

def mysql_helper
  @mysql_helper
end

#optionsObject



24
25
26
# File 'lib/csv-import-analyzer/sql_query_builder.rb', line 24

def options
  @options
end

#pg_helperObject



53
54
55
# File 'lib/csv-import-analyzer/sql_query_builder.rb', line 53

def pg_helper
  @pg_helper
end

#tablenameObject



36
37
38
39
40
41
42
43
# 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[:filename])
  tablename.gsub!(" ", "_")
  tablename.downcase!
  return tablename
end