lib/enrichment_db/db.rb in enrichment_db-0.1.4 vs lib/enrichment_db/db.rb in enrichment_db-0.1.5

- old
+ new

@@ -1,26 +1,47 @@ require 'pg' module EnrichmentDb + def self.db_connection @__db_connection ||= PGconn.connect( - host:'geo-mappings.cfmmyp7uix0j.ap-southeast-2.rds.amazonaws.com', - dbname:'geo', + host: 'geo-mappings.cfmmyp7uix0j.ap-southeast-2.rds.amazonaws.com', + dbname: 'geo', user: 'lexer', password: 'campl3x3r' ) end def self.reset_connection @__api_connection = nil end - def self.request(query, values) + def self.table_names(schema = 'public') + query = "SELECT table_name + FROM information_schema.tables + WHERE table_schema='#{schema}' + AND table_type='BASE TABLE'" + request(schema, query).collect do |h| + h['table_name'] + end + end + + def self.table_columns(table, schema = 'public') + query = "SELECT * + FROM information_schema.columns + WHERE table_schema = '#{schema}' + AND table_name = '#{table}'" + request(schema, query).collect do |h| + h['column_name'] + end + end + + def self.request(db, query, values = nil) type = request_type(query) case type - when :select, 'select' + when :select, 'select', '\d' result = if values EnrichmentDb::db_connection.exec(query, values) else EnrichmentDb::db_connection.exec(query) end @@ -30,10 +51,10 @@ fail EnrichmentDb::RequestError, "Postgres query '#{query}' is unsupported" end end def self.request_type(query) - result = query.downcase.match(/(^(?:drop|select|insert|delete))/) + result = query.downcase.match(/(^(?:drop|select|insert|delete|\\d))/) return unless result result[0] end end