Class: ToARFF::SQLiteDB

Inherits:
Object
  • Object
show all
Defined in:
lib/to-arff/sqlitedb.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ SQLiteDB

Returns a new instance of SQLiteDB.



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

Instance Attribute Details

#column_typeObject

Returns the value of attribute column_type.



13
14
15
# File 'lib/to-arff/sqlitedb.rb', line 13

def column_type
  @column_type
end

#columnsObject

Returns the value of attribute columns.



13
14
15
# File 'lib/to-arff/sqlitedb.rb', line 13

def columns
  @columns
end

#dbObject

Returns the value of attribute db.



13
14
15
# File 'lib/to-arff/sqlitedb.rb', line 13

def db
  @db
end

#db_file_pathObject

Returns the value of attribute db_file_path.



13
14
15
# File 'lib/to-arff/sqlitedb.rb', line 13

def db_file_path
  @db_file_path
end

#tablesObject

Returns the value of attribute tables.



13
14
15
# File 'lib/to-arff/sqlitedb.rb', line 13

def tables
  @tables
end

Instance Method Details

#check_given_columns_validity(given_columns) ⇒ Object



152
153
154
155
156
157
158
159
160
161
# 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) ⇒ Object



145
146
147
148
149
150
# 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 = {}) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# 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) ⇒ Object



137
138
139
140
141
142
143
# 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) ⇒ Object



129
130
131
132
133
134
135
# 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) ⇒ Object

Converts a table to ARFF.



71
72
73
# 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) ⇒ Object



122
123
124
125
126
127
# 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) ⇒ Object

If valid option was provided in convert method



180
181
182
183
184
185
186
187
188
189
190
191
192
193
# 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) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
# 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) ⇒ Object

Get all colums for a given table.



46
47
48
49
50
51
52
53
# 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) ⇒ Object

If the column type is nominal return true.



62
63
64
65
66
67
68
# 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_fileObject



25
26
27
28
29
30
31
32
33
34
35
# 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_columnsObject



55
56
57
58
59
# 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_tablesObject

Get all the tables’ name and store them in an array (@tables).



38
39
40
41
42
43
# 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) ⇒ Object



195
196
197
198
199
200
201
# 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) ⇒ Object



175
176
177
# 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) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# 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) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# 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) ⇒ Object



75
76
77
# File 'lib/to-arff/sqlitedb.rb', line 75

def write_relation(table_name)
	"#{RELATION_MARKER} #{table_name}\n\n"
end