Class: RailsDbAdmin::QuerySupport

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_db_admin/query_support.rb

Instance Method Summary collapse

Constructor Details

#initialize(database_connection_class, database_connection_name) ⇒ QuerySupport

Returns a new instance of QuerySupport.



6
7
8
9
# 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

Instance Method Details

#delete_query(name) ⇒ Object



79
80
81
# 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) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# 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) ⇒ Object



83
84
85
# 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_namesObject



57
58
59
60
61
62
63
64
65
66
67
68
69
# 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) ⇒ Object



71
72
73
74
75
76
77
# 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) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 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