class ToARFF::SQLiteDB
Attributes
column_type[RW]
columns[RW]
db[RW]
db_file_path[RW]
tables[RW]
Public Class Methods
new(path)
click to toggle source
# File lib/to-arff/sqlitedb.rb, line 15 def initialize(path) @db_file_path = path @tables = [] @columns = {} @column_type = {} process_db_file set_all_tables set_all_columns end
Public Instance Methods
check_given_columns_validity(given_columns)
click to toggle source
# File lib/to-arff/sqlitedb.rb, line 152 def check_given_columns_validity(given_columns) given_tables = given_columns.keys check_given_tables_validity(given_tables) given_tables.each do |elem| dif = downcase_array(given_columns[elem]) - downcase_array(@columns[elem]) if !dif.empty? # If @tables doesn't contain all elements of given_tables raise ArgumentError.new("\"#{dif.first}\" does not exist.") end end end
check_given_tables_validity(given_tables)
click to toggle source
# File lib/to-arff/sqlitedb.rb, line 145 def check_given_tables_validity(given_tables) dif = downcase_array(given_tables) - downcase_array(@tables) if !dif.empty? # If @tables doesn't contain all elements of given_tables raise ArgumentError.new("\"#{dif.first}\" does not exist.") end end
convert(options={})
click to toggle source
# File lib/to-arff/sqlitedb.rb, line 203 def convert(options={}) temp_tables = options.fetch(:tables, Array.new) temp_columns = options.fetch(:columns, Hash.new) temp_column_types = options.fetch(:column_types, Hash.new) res = "" param_count = options.keys.length if param_count == 0 @tables.each do |t| res << convert_table(t) end elsif param_count == 1 if valid_option_given(options) raise ArgumentError.new("Wrong parameter name \":#{options.keys.first}\"") else deal_with_valid_option(temp_tables, stringify_all_keys(temp_columns), stringify_all_keys(temp_column_types), res) end elsif param_count > 1 raise ArgumentError.new("You can specify only one out of the three parameters: table, columns, column_types.") end res end
convert_from_column_types_hash(col_types_hash)
click to toggle source
# File lib/to-arff/sqlitedb.rb, line 137 def convert_from_column_types_hash(col_types_hash) rel = "" col_types_hash.keys.each do |table| rel << convert_table_with_columns(table, col_types_hash[table].keys, col_types_hash) end rel end
convert_from_columns_hash(cols_hash)
click to toggle source
# File lib/to-arff/sqlitedb.rb, line 129 def convert_from_columns_hash(cols_hash) rel = "" cols_hash.keys.each do |table| rel << convert_table_with_columns(table, cols_hash[table]) end rel end
convert_table(table_name)
click to toggle source
Converts a table to ARFF.
# File lib/to-arff/sqlitedb.rb, line 71 def convert_table(table_name) convert_table_with_columns(table_name, get_columns(table_name)) end
convert_table_with_columns(table_name, columns, column_types=nil)
click to toggle source
# File lib/to-arff/sqlitedb.rb, line 122 def convert_table_with_columns(table_name, columns, column_types=nil) rel = write_relation(table_name) rel << write_attributes(table_name, columns, column_types) rel << write_data(table_name, columns) rel end
deal_with_valid_option(temp_tables, temp_columns, temp_column_types, res)
click to toggle source
If valid option was provided in convert method
# File lib/to-arff/sqlitedb.rb, line 180 def deal_with_valid_option(temp_tables, temp_columns, temp_column_types, res) if !temp_tables.empty? check_given_tables_validity(temp_tables) temp_tables.each do |t| res << convert_table(t) end elsif !temp_columns.keys.empty? check_given_columns_validity(temp_columns) res << convert_from_columns_hash(temp_columns) elsif !temp_column_types.empty? check_given_columns_validity(temp_column_types) res << convert_from_column_types_hash(temp_column_types) end end
downcase_array(arr)
click to toggle source
# File lib/to-arff/sqlitedb.rb, line 163 def downcase_array(arr) downcased_array = Array.new arr.each do |elem| if elem.is_a? String downcased_array.push(elem.downcase) elsif elem.is_a? Array downcased_array.push(elem.first.downcase) end end downcased_array end
get_columns(table_name)
click to toggle source
Get all colums for a given table.
# File lib/to-arff/sqlitedb.rb, line 46 def get_columns(table_name) columns_arr = [] pst = @db.prepare "SELECT * FROM #{table_name} LIMIT 6" pst.columns.each do |c| columns_arr.push(c) end columns_arr end
is_numeric(table_name, column_name)
click to toggle source
If the column type is nominal return true.
# File lib/to-arff/sqlitedb.rb, line 62 def is_numeric(table_name, column_name) if @db.execute("SELECT #{column_name} from #{table_name} LIMIT 1").first.first.is_a? Numeric return true else return false end end
process_db_file()
click to toggle source
# File lib/to-arff/sqlitedb.rb, line 25 def process_db_file if @db_file_path != '' if File.exist? "#{@db_file_path}" @db = SQLite3::Database.open "#{@db_file_path}" else raise "#{@db_file_path} doesn't exist. Enter a valid file path." end else raise "Database File Path cannot be empty." end end
set_all_columns()
click to toggle source
# File lib/to-arff/sqlitedb.rb, line 55 def set_all_columns @tables.each do |t| @columns[t] = get_columns(t) end end
set_all_tables()
click to toggle source
Get all the tables' name and store them in an array (@tables).
# File lib/to-arff/sqlitedb.rb, line 38 def set_all_tables tables_arr = @db.execute("SELECT name FROM sqlite_master WHERE type='table';") tables_arr.each do |elem| @tables.push(elem.first) end end
stringify_all_keys(hash)
click to toggle source
# File lib/to-arff/sqlitedb.rb, line 195 def stringify_all_keys(hash) stringified_hash = {} hash.each do |k, v| stringified_hash[k.to_s] = v.is_a?(Hash) ? stringify_all_keys(v) : v end stringified_hash end
valid_option_given(options)
click to toggle source
# File lib/to-arff/sqlitedb.rb, line 175 def valid_option_given(options) return options.keys.first.to_s != "tables" && options.keys.first.to_s != "columns" && options.keys.first.to_s != "column_types" end
write_attributes(table_name, columns, column_types)
click to toggle source
# File lib/to-arff/sqlitedb.rb, line 79 def write_attributes(table_name, columns, column_types) rel = "" if column_types.nil? columns.each do |col| if is_numeric(table_name, col) rel << "#{ATTRIBUTE_MARKER} #{col} #{ATTRIBUTE_TYPE_NUMERIC}\n" else rel << "#{ATTRIBUTE_MARKER} #{col} #{ATTRIBUTE_TYPE_STRING}\n" end end else columns.each do |col| rel << "#{ATTRIBUTE_MARKER} #{col} #{column_types[table_name][col]}\n" end end rel end
write_data(table_name, columns)
click to toggle source
# File lib/to-arff/sqlitedb.rb, line 97 def write_data(table_name, columns) rel = '' columns_str = '' columns.each do |col| columns_str += col + ', ' end columns_str = columns_str.chomp(', ') rel << "\n#{DATA_MARKER}\n" data = @db.prepare "SELECT #{columns_str} FROM #{table_name}" data.each do |elem| row = '' elem.each do |val| if val.is_a? Numeric row = row + "#{val}" + "," else row = row + "\"#{val}\"" + "," end end rel << row.strip.chomp(",") rel << "\n" end rel << "\n\n\n" rel end
write_relation(table_name)
click to toggle source
# File lib/to-arff/sqlitedb.rb, line 75 def write_relation(table_name) "#{RELATION_MARKER} #{table_name}\n\n" end