Class: RailsDbAdmin::QuerySupport
- Inherits:
-
Object
- Object
- RailsDbAdmin::QuerySupport
- Defined in:
- lib/rails_db_admin/query_support.rb
Direct Known Subclasses
Instance Method Summary collapse
- #delete_query(name) ⇒ Object
- #execute_sql(sql) ⇒ Object
- #get_query(name) ⇒ Object
- #get_saved_query_names ⇒ Object
-
#initialize(database_connection_class, database_connection_name) ⇒ QuerySupport
constructor
A new instance of QuerySupport.
- #save_query(query, name) ⇒ Object
- #select_top_fifty(table) ⇒ Object
Constructor Details
#initialize(database_connection_class, database_connection_name) ⇒ QuerySupport
Returns a new instance of QuerySupport.
5 6 7 8 |
# File 'lib/rails_db_admin/query_support.rb', line 5 def initialize(database_connection_class, database_connection_name) @path = File.join(Rails.root, Rails.application.config.rails_db_admin.query_location, database_connection_name) @connection = database_connection_class.connection end |
Instance Method Details
#delete_query(name) ⇒ Object
70 71 72 |
# File 'lib/rails_db_admin/query_support.rb', line 70 def delete_query(name) FileUtils.rm(File.join(@path, "#{name}.sql")) if File.exist?(File.join(@path, "#{name}.sql")) end |
#execute_sql(sql) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/rails_db_admin/query_support.rb', line 10 def execute_sql(sql) begin rows = @connection.select_all(sql) rescue => ex return nil, nil, ex. end values = [] columns = [] unless rows.nil? || rows.empty? columns = rows[0].keys rows.each do |row| values << row end end return columns, values, nil end |
#get_query(name) ⇒ Object
74 75 76 |
# File 'lib/rails_db_admin/query_support.rb', line 74 def get_query(name) File.open(File.join(@path, "#{name}.sql")) { |f| f.read } if File.exist?(File.join(@path, "#{name}.sql")) end |
#get_saved_query_names ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/rails_db_admin/query_support.rb', line 48 def get_saved_query_names query_files = [] if File.directory? @path query_files = Dir.entries(@path) query_files.delete_if{|name| name =~ /^\./} query_files.each do |file_name| file_name.gsub!('.sql', '') end end query_files end |
#save_query(query, name) ⇒ Object
62 63 64 65 66 67 68 |
# File 'lib/rails_db_admin/query_support.rb', line 62 def save_query(query, name) FileUtils.mkdir_p(@path) unless File.directory? @path file_path = File.join(@path, "#{name}.sql") File.new(file_path, 'w') unless File.exist?(File.join(file_path)) File.open(file_path, 'w+'){|f| f.puts(query) } end |
#select_top_fifty(table) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/rails_db_admin/query_support.rb', line 30 def select_top_fifty(table) #Actually, sanitizing here is pretty redundent since it's a constant... ar = Arel::Table::new(table) query = ar.project(Arel.sql('*')).take(50) #query = "SELECT * FROM #{table} LIMIT #{@connection.sanitize_limit(50)}" # This is a temporary partial fix to handle postgres boolean columns which is use activerecord when possible begin rows = table.classify.constantize.find_by_sql(query.to_sql) rescue rows = @connection.select_all(query.to_sql) end records = RailsDbAdmin::TableSupport.database_rows_to_hash(rows) return query.to_sql, records end |