Module: MDBTools
Constant Summary collapse
- DELIMITER =
'::'- LINEBREAK =
"\n"- SANITIZER =
dumb filter for SQL arguments
/^\w\.\_/- BACKENDS =
%w{ access mysql oracle postgres sybase }
Instance Method Summary collapse
- #arrays_to_hashes(headers, arrays) ⇒ Object
- #backends ⇒ Object
- #check_file(mdb_file) ⇒ Object
- #check_table(mdb_file, table_name) ⇒ Object
- #compile_conditions(conditions_hash, *args) ⇒ Object
- #delimited_to_arrays(text) ⇒ Object
- #describe_table(mdb_file, table_name) ⇒ Object
- #mdb_export(mdb_file, table_name, options = {}) ⇒ Object
- #mdb_schema(mdb_file, table_name) ⇒ Object
- #mdb_sql(mdb_file, sql) ⇒ Object
- #mdb_tables(mdb_file, options = {}) ⇒ Object
- #mdb_truth(value) ⇒ Object
- #methodize(table_name) ⇒ Object
- #sanitize!(string) ⇒ Object
- #sql_select(mdb_file, table_name, attributes = nil, conditions = {}) ⇒ Object
- #table_to_csv(mdb_file, table_name) ⇒ Object
Instance Method Details
#arrays_to_hashes(headers, arrays) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/active_mdb/mdb_tools.rb', line 106 def arrays_to_hashes(headers, arrays) arrays.collect do |record| record_hash = Hash.new until record.empty? do headers.each do |header| record_hash[header] = record.shift end end record_hash end end |
#backends ⇒ Object
124 125 126 |
# File 'lib/active_mdb/mdb_tools.rb', line 124 def backends BACKENDS end |
#check_file(mdb_file) ⇒ Object
9 10 11 12 13 14 15 16 |
# File 'lib/active_mdb/mdb_tools.rb', line 9 def check_file(mdb_file) raise ArgumentError, "File not found: #{mdb_file}" unless File.exist?(mdb_file) @mdb_version = `mdb-ver #{mdb_file} 2>&1`.chomp if $? != 0 raise ArgumentError, "mdbtools cannot access #{mdb_file}" end mdb_file end |
#check_table(mdb_file, table_name) ⇒ Object
18 19 20 21 22 23 |
# File 'lib/active_mdb/mdb_tools.rb', line 18 def check_table(mdb_file, table_name) unless mdb_tables(mdb_file).include?(table_name) raise ArgumentError, "mdbtools does not think a table named \"#{table_name}\" exists" end table_name end |
#compile_conditions(conditions_hash, *args) ⇒ Object
53 54 55 56 57 58 59 60 61 |
# File 'lib/active_mdb/mdb_tools.rb', line 53 def compile_conditions(conditions_hash, *args) conditions = conditions_hash.sort_by{|k,v| k.to_s}.map do |column_name, value| if block_given? yield column_name, value else "#{column_name} like '%#{value}%'" end end.join(' AND ') end |
#delimited_to_arrays(text) ⇒ Object
101 102 103 104 |
# File 'lib/active_mdb/mdb_tools.rb', line 101 def delimited_to_arrays(text) text.gsub!(/\r\n/,' ') text.split(LINEBREAK).collect { |row| row.split(DELIMITER)} end |
#describe_table(mdb_file, table_name) ⇒ Object
85 86 87 88 89 90 |
# File 'lib/active_mdb/mdb_tools.rb', line 85 def describe_table(mdb_file, table_name) command = "describe table \"#{table_name}\"".dump description = `echo -n #{command} | mdb-sql -Fp -d '#{DELIMITER}' #{mdb_file}`.strip arrays = delimited_to_arrays(description) arrays_to_hashes(arrays.shift, arrays) end |
#mdb_export(mdb_file, table_name, options = {}) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/active_mdb/mdb_tools.rb', line 63 def mdb_export(mdb_file, table_name, = {}) defaults = { :format => 'sql', :headers => false, :sanitize => true } = defaults.merge args = [] if [:delimiter] args << "-d #{[:delimiter].dump}" elsif [:format] == 'sql' args << "-I " elsif [:format] == 'csv' args << "-d ',' " else raise ArgumentError, "Unknown format: #{[:format]}" end args << "-H " unless [:headers] == true args << "-S" unless [:sanitize] == false `mdb-export #{args} #{mdb_file} #{table_name.to_s.dump}` end |
#mdb_schema(mdb_file, table_name) ⇒ Object
92 93 94 |
# File 'lib/active_mdb/mdb_tools.rb', line 92 def mdb_schema(mdb_file, table_name) schema = `mdb-schema -T #{table_name.dump} #{mdb_file}` end |
#mdb_sql(mdb_file, sql) ⇒ Object
47 48 49 50 51 |
# File 'lib/active_mdb/mdb_tools.rb', line 47 def mdb_sql(mdb_file, sql) # puts sql result = `echo -n #{sql} | mdb-sql -Fp -H -d '#{DELIMITER}' #{mdb_file}`.strip arrays = delimited_to_arrays(result) end |
#mdb_tables(mdb_file, options = {}) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/active_mdb/mdb_tools.rb', line 25 def mdb_tables(mdb_file, = {}) included, excluded = [:include], [:exclude] return `mdb-tables -1 #{mdb_file}`.split(LINEBREAK) if not (included || excluded) raise ArgumentError if ([:include] && [:exclude]) if [:exclude] regex = Regexp.new [:exclude].to_a.join('|') tables = `mdb-tables -1 #{mdb_file}`.split(LINEBREAK).delete_if { |name| name =~ regex } end if [:include] regex = Regexp.new [:include].to_a.join('|') tables = `mdb-tables -1 #{mdb_file}`.split(LINEBREAK).select { |name| name =~ regex } end tables end |
#mdb_truth(value) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/active_mdb/mdb_tools.rb', line 132 def mdb_truth(value) case value when false 0 when true 1 when 0 0 when 1 1 when "0" 0 when "1" 1 end end |
#methodize(table_name) ⇒ Object
120 121 122 |
# File 'lib/active_mdb/mdb_tools.rb', line 120 def methodize(table_name) underscore table_name end |
#sanitize!(string) ⇒ Object
128 129 130 |
# File 'lib/active_mdb/mdb_tools.rb', line 128 def sanitize!(string) string.gsub!(SANITIZER, '') end |
#sql_select(mdb_file, table_name, attributes = nil, conditions = {}) ⇒ Object
40 41 42 43 44 |
# File 'lib/active_mdb/mdb_tools.rb', line 40 def sql_select(mdb_file, table_name, attributes = nil, conditions ={}) attributes ||= ['*'] sql = "select #{attributes.join(' ')} from #{table_name} where #{conditions}".dump mdb_sql(mdb_file, sql) end |
#table_to_csv(mdb_file, table_name) ⇒ Object
97 98 99 |
# File 'lib/active_mdb/mdb_tools.rb', line 97 def table_to_csv(mdb_file, table_name) mdb_export(mdb_file, table_name, :format => 'csv', :headers => true) end |