class RailsDbAdmin::QuerySupport

Public Class Methods

new(database_connection_class, database_connection_name) click to toggle source
# File lib/rails_db_admin/query_support.rb, line 6
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

Public Instance Methods

delete_query(name) click to toggle source
# File lib/rails_db_admin/query_support.rb, line 79
def delete_query(name)
  FileUtils.rm(File.join(@path, "#{name}.sql")) if File.exist?(File.join(@path, "#{name}.sql"))
end
execute_sql(sql) click to toggle source
# File lib/rails_db_admin/query_support.rb, line 11
def execute_sql(sql)
  begin
    result = @connection.execute(sql)
  rescue => ex
    return nil, nil, ex.message
  end

  values = []
  columns = []
 
  if @connection.class == ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
    columns = result.fields

    if result && result.count > 0
      result.each do |row|
        values << HashWithIndifferentAccess.new(row)
      end
    end
  else
    columns = result[0].keys
    result.each do |row|
      values << HashWithIndifferentAccess.new(row)
    end
  end

  return columns, values, nil
end
get_query(name) click to toggle source
# File lib/rails_db_admin/query_support.rb, line 83
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() click to toggle source
# File lib/rails_db_admin/query_support.rb, line 57
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) click to toggle source
# File lib/rails_db_admin/query_support.rb, line 71
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) click to toggle source
# File lib/rails_db_admin/query_support.rb, line 39
def select_top_fifty(table)
  #Actually, sanitizing here is pretty redundant 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