rails_db_admin 3.0.0 → 3.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. checksums.yaml +4 -4
  2. data/{public → app/assets}/images/icons/rails_db_admin/rails_db_admin_16x16.png +0 -0
  3. data/{public → app/assets}/images/icons/rails_db_admin/rails_db_admin_24x24.png +0 -0
  4. data/{public → app/assets}/images/icons/rails_db_admin/rails_db_admin_32x32.png +0 -0
  5. data/{public → app/assets}/images/icons/rails_db_admin/rails_db_admin_64x64.png +0 -0
  6. data/{public → app/assets}/images/icons/rails_db_admin/rails_db_admin_light_16x16.png +0 -0
  7. data/app/assets/images/splash/db_admin_splash.png +0 -0
  8. data/{public → app/assets}/images/splash/images/console-icon.png +0 -0
  9. data/{public → app/assets}/images/splash/images/data-model-icon.png +0 -0
  10. data/app/assets/javascripts/erp_app/desktop/applications/rails_db_admin/app.js +20 -0
  11. data/{public → app/assets}/javascripts/erp_app/desktop/applications/rails_db_admin/database_combo.js +1 -1
  12. data/{public → app/assets}/javascripts/erp_app/desktop/applications/rails_db_admin/module.js +227 -135
  13. data/{public → app/assets}/javascripts/erp_app/desktop/applications/rails_db_admin/queries_tree_menu.js +6 -6
  14. data/app/assets/javascripts/erp_app/desktop/applications/rails_db_admin/query_panel.js +383 -0
  15. data/{public → app/assets}/javascripts/erp_app/desktop/applications/rails_db_admin/readonly_table_data_grid.js +0 -0
  16. data/app/assets/javascripts/erp_app/desktop/applications/rails_db_admin/reports/ParamWindow.js +426 -0
  17. data/app/assets/javascripts/erp_app/desktop/applications/rails_db_admin/reports/ParamsManager.js +177 -0
  18. data/app/assets/javascripts/erp_app/desktop/applications/rails_db_admin/reports/RolesPanel.js +95 -0
  19. data/app/assets/javascripts/erp_app/desktop/applications/rails_db_admin/reports/SetDefaultWindow.js +244 -0
  20. data/app/assets/javascripts/erp_app/desktop/applications/rails_db_admin/reports/Settings.js +155 -0
  21. data/app/assets/javascripts/erp_app/desktop/applications/rails_db_admin/reports/TreePanel.js +391 -0
  22. data/app/assets/javascripts/erp_app/desktop/applications/rails_db_admin/splash_screen.js +163 -0
  23. data/{public → app/assets}/javascripts/erp_app/desktop/applications/rails_db_admin/tables_tree_menu.js +5 -3
  24. data/app/assets/javascripts/erp_app/shared/MultiOption.js +151 -0
  25. data/app/assets/javascripts/erp_app/shared/ReportsParams.js +289 -0
  26. data/app/assets/javascripts/erp_app/shared/rails_db_admin_shared.js +13 -0
  27. data/{public/stylesheets/erp_app/desktop/applications/rails_db_admin/rails_db_admin.css → app/assets/stylesheets/erp_app/shared/rails_db_admin_shared.css} +5 -10
  28. data/app/controllers/rails_db_admin/erp_app/desktop/base_controller.rb +7 -6
  29. data/app/controllers/rails_db_admin/erp_app/desktop/queries_controller.rb +88 -74
  30. data/app/controllers/rails_db_admin/erp_app/desktop/reports_controller.rb +361 -30
  31. data/app/controllers/rails_db_admin/reports/base_controller.rb +105 -22
  32. data/app/mailers/report_mailer.rb +15 -0
  33. data/app/models/report.rb +227 -14
  34. data/app/views/rails_db_admin/reports/base/error_report.erb +27 -0
  35. data/app/views/report_mailer/email_report.html.erb +255 -0
  36. data/config/routes.rb +2 -1
  37. data/db/data_migrations/20110816005525_rails_db_admin_application.rb +6 -14
  38. data/db/data_migrations/20151015093724_add_report_role_type.rb +15 -0
  39. data/db/migrate/20121210160131_add_reports.rb +2 -0
  40. data/db/migrate/20150926061259_add_meta_data_to_reports.rb +9 -0
  41. data/lib/rails_db_admin.rb +4 -2
  42. data/lib/rails_db_admin/engine.rb +12 -4
  43. data/lib/rails_db_admin/erp_string_parser.rb +26 -0
  44. data/lib/rails_db_admin/extensions/railties/action_view/helpers/report_helper.rb +167 -10
  45. data/lib/rails_db_admin/extjs/json_data_builder.rb +36 -20
  46. data/lib/rails_db_admin/query_params.rb +14 -0
  47. data/lib/rails_db_admin/query_support.rb +56 -46
  48. data/lib/rails_db_admin/services/report_helper.rb +241 -0
  49. data/lib/rails_db_admin/version.rb +1 -1
  50. metadata +41 -30
  51. data/app/assets/javascripts/rails_db_admin/application.js +0 -9
  52. data/app/assets/stylesheets/rails_db_admin/application.css +0 -7
  53. data/app/views/rails_db_admin/reports/base/show_report.erb +0 -11
  54. data/db/migrate/20131112013053_add_rails_db_admin_missing_indexes.rb +0 -17
  55. data/lib/rails_db_admin/report_support.rb +0 -52
  56. data/public/images/splash/splash.png +0 -0
  57. data/public/javascripts/erp_app/desktop/applications/rails_db_admin/query_panel.js +0 -223
  58. data/public/javascripts/erp_app/desktop/applications/rails_db_admin/report_panel.js +0 -78
  59. data/public/javascripts/erp_app/desktop/applications/rails_db_admin/reports_tree_panel.js +0 -237
  60. data/public/javascripts/erp_app/desktop/applications/rails_db_admin/splash_screen.js +0 -136
@@ -1,5 +1,6 @@
1
1
  Rails.application.routes.draw do
2
- match '/reports/display/:iid(.:format)' => "rails_db_admin/reports/base#index"
2
+ match '/reports/display/:iid(.:format)' => "rails_db_admin/reports/base#show"
3
+ match '/reports/email/:iid' => "rails_db_admin/reports/base#email"
3
4
  end
4
5
 
5
6
  RailsDbAdmin::Engine.routes.draw do
@@ -1,29 +1,21 @@
1
1
  class RailsDbAdminApplication
2
-
2
+
3
3
  def self.up
4
4
  if DesktopApplication.find_by_internal_identifier('rails_db_admin').nil?
5
5
  rails_db_admin_app = DesktopApplication.create(
6
6
  :description => 'RailsDbAdmin',
7
7
  :icon => 'icon-rails_db_admin',
8
- :javascript_class_name => 'Compass.ErpApp.Desktop.Applications.RailsDbAdmin',
9
- :internal_identifier => 'rails_db_admin',
10
- :shortcut_id => 'rails_db_admin-win'
8
+ :internal_identifier => 'rails_db_admin'
11
9
  )
12
10
 
13
- rails_db_admin_app.preference_types << PreferenceType.iid('desktop_shortcut')
14
- rails_db_admin_app.preference_types << PreferenceType.iid('autoload_application')
15
- rails_db_admin_app.save
16
-
17
11
  admin_user = User.find_by_username('admin')
18
- admin_user.desktop.applications << rails_db_admin_app
12
+ admin_user.desktop_applications << rails_db_admin_app
19
13
  admin_user.save
20
-
21
- truenorth_user = User.find_by_username('truenorth')
22
- truenorth_user.desktop.applications << rails_db_admin_app
23
- truenorth_user.save
14
+
15
+ rails_db_admin_app.add_party_with_role(admin_user.party.dba_organization, RoleType.iid('dba_org'))
24
16
  end
25
17
  end
26
-
18
+
27
19
  def self.down
28
20
  DesktopApplication.find_by_internal_identifier('rails_db_admin').destroy
29
21
  end
@@ -0,0 +1,15 @@
1
+ class AddReportRoleType
2
+
3
+ def self.up
4
+ RoleType.create(
5
+ description: 'Report',
6
+ internal_identifier: 'report'
7
+ ) unless RoleType.iid('report')
8
+ end
9
+
10
+ def self.down
11
+ report_role = RoleType.iid('report')
12
+ report_role.destroy if report_role
13
+ end
14
+
15
+ end
@@ -9,6 +9,8 @@ class AddReports < ActiveRecord::Migration
9
9
 
10
10
  t.timestamps
11
11
  end
12
+
13
+ add_index :reports, :internal_identifier, :name => 'reports_internal_identifier_idx'
12
14
  end
13
15
  end
14
16
 
@@ -0,0 +1,9 @@
1
+ class AddMetaDataToReports < ActiveRecord::Migration
2
+ def up
3
+ add_column :reports, :meta_data, :text unless column_exists? :reports, :meta_data
4
+ end
5
+
6
+ def down
7
+ remove_column :reports, :meta_data if column_exists? :reports, :meta_data
8
+ end
9
+ end
@@ -6,10 +6,12 @@ require "rails_db_admin/version"
6
6
  require 'rails_db_admin/config'
7
7
  require 'rails_db_admin/extjs'
8
8
  require 'rails_db_admin/connection_handler'
9
+ require 'rails_db_admin/query_params'
10
+ require 'rails_db_admin/erp_string_parser'
9
11
  require 'rails_db_admin/query_support'
10
12
  require 'rails_db_admin/table_support'
11
- require 'rails_db_admin/report_support'
12
- require "rails_db_admin/engine"
13
+ require 'rails_db_admin/engine'
14
+ require 'rails_db_admin/services/report_helper'
13
15
 
14
16
  module RailsDbAdmin
15
17
  end
@@ -3,13 +3,21 @@ module RailsDbAdmin
3
3
  isolate_namespace RailsDbAdmin
4
4
 
5
5
  config.rails_db_admin = RailsDbAdmin::Config
6
-
7
- initializer "rails_db_admin.merge_public" do |app|
8
- app.middleware.insert_before Rack::Runtime, ::ActionDispatch::Static, "#{root}/public"
6
+
7
+ initializer :assets do |config|
8
+ Rails.application.config.assets.paths << root.join("app", "assets", "images")
9
+ Rails.application.config.assets.precompile += %w{ erp_app/desktop/applications/rails_db_admin/app.js }
10
+ Rails.application.config.assets.precompile += ['bootstrap.css']
11
+
12
+ Rails.application.config.assets.precompile += %w{ erp_app/shared/rails_db_admin_shared.js }
13
+ Rails.application.config.assets.precompile += %w{ erp_app/shared/rails_db_admin_shared.css }
14
+
15
+ ErpApp::Config.shared_js_assets += %w{ erp_app/shared/rails_db_admin_shared.js }
16
+ ErpApp::Config.shared_css_assets += %w{ erp_app/shared/rails_db_admin_shared.css }
9
17
  end
10
18
 
11
19
  ErpBaseErpSvcs.register_as_compass_ae_engine(config, self)
12
20
  ::ErpApp::Widgets::Loader.load_compass_ae_widgets(config, self)
13
-
21
+
14
22
  end
15
23
  end
@@ -0,0 +1,26 @@
1
+ module RailsDbAdmin
2
+ class ErbStringParser
3
+ ERB_REGEX = /\<\%\=\s*(\$|@{1,2})?[A-Za-z][A-Za-z0-9_]*\s*\%\>/
4
+
5
+ class << self
6
+ def render(str, options = {})
7
+ locals = options[:locals] || {}
8
+
9
+ # convert all variables to underscore as
10
+ # ERB parser has issues with capitalized variables
11
+ _str = str.gsub(ERB_REGEX) do |w|
12
+ w.delete(' ').underscore
13
+ end
14
+ # convert all to locals to underscore
15
+ _locals = {}
16
+ locals.each do |k, v|
17
+ _locals[k.to_s.delete(' ').underscore] = v
18
+ end
19
+
20
+ query_params = RailsDbAdmin::QueryParams.new(_locals)
21
+ ERB.new(_str).result(query_params.get_binding)
22
+ end
23
+ end
24
+
25
+ end # ErbStringParser
26
+ end # RailsDbadmin
@@ -1,18 +1,12 @@
1
+ require 'base64'
2
+
1
3
  module RailsDbAdmin
2
4
  module Extensions
3
5
  module Railties
4
6
  module ActionView
5
7
  module Helpers
6
8
  module ReportHelper
7
-
8
- def render_report(report_iid)
9
- RailsDbAdmin::ReportSupport.new.render_report(report_iid)
10
- end
11
-
12
- def get_report_data(report_iid)
13
- RailsDbAdmin::ReportSupport.new.get_report_data(report_iid)
14
- end
15
-
9
+
16
10
  def report_download_url(report_iid, format)
17
11
  raw "/reports/display/#{report_iid}.#{format}"
18
12
  end
@@ -22,9 +16,172 @@ module RailsDbAdmin
22
16
  raw "<a target='_blank' href='#{report_download_url(report_iid, format)}'>#{display}</a>"
23
17
  end
24
18
 
19
+ def render_template(template, locals=nil)
20
+ if request && request.format.symbol == :html
21
+ locals.nil? ? (render :partial => "/#{template}") : (render :partial => "/#{template}",:locals => locals[:locals])
22
+ elsif request && request.format.symbol == :pdf
23
+ locals.nil? ? (render :partial => "/#{template}.html.erb") : (render :partial => "/#{template}.html.erb" , :locals => locals[:locals])
24
+ else
25
+ locals.nil? ? (render :partial => "/#{template}.html.erb") : (render :partial => "/#{template}.html.erb" , :locals => locals[:locals])
26
+ end
27
+ end
28
+
29
+ def report_stylesheet_link_tag(report_id, *sources)
30
+ report = Report.iid(report_id)
31
+ return("could not find report with the id #{report_id}") unless report
32
+ if request && request.format.symbol == :pdf
33
+ file_support = ErpTechSvcs::FileSupport::Base.new(:storage => ErpTechSvcs::Config.file_storage)
34
+ css = sources.collect do |source|
35
+ css_path = report.stylesheet_path(source)
36
+ css_text = "<style type='text/css'>#{file_support.get_contents(css_path).first}</style>"
37
+ css_text.respond_to?(:html_safe) ? css_text.html_safe : css_text
38
+ end.join("\n")
39
+ raw css
40
+ else
41
+ options = sources.extract_options!.stringify_keys
42
+ cache = options.delete("cache")
43
+ recursive = options.delete("recursive")
44
+ sources = report_expand_stylesheet_sources(report, sources, recursive).collect do |source|
45
+ report_stylesheet_tag(report, source, options)
46
+ end.join("\n")
47
+ raw sources
48
+ end
49
+ end
50
+
51
+ def report_javascript_include_tag(report_id, *sources)
52
+ report = Report.iid(report_id)
53
+ return("could not find report with the id #{report_id}") unless report
54
+ if request && request.format.symbol == :pdf
55
+ file_support = ErpTechSvcs::FileSupport::Base.new(:storage => ErpTechSvcs::Config.file_storage)
56
+ js = sources.collect do |source|
57
+ js_path = report.javascript_path(source)
58
+ js_text = "<script>#{file_support.get_contents(js_path).first}</script>"
59
+ js_text.respond_to?(:html_safe) ? js_text.html_safe : js_text
60
+ end.join("\n")
61
+ raw js
62
+ else
63
+ options = sources.extract_options!.stringify_keys
64
+ cache = options.delete("cache")
65
+ recursive = options.delete("recursive")
66
+ sources = report_expand_javascript_sources(report, sources, recursive).collect do |source|
67
+ report_javascript_src_tag(report, source, options)
68
+ end.join("\n")
69
+ raw sources
70
+ end
71
+ end
72
+
73
+ def report_stylesheet_path(report, source)
74
+ report = Report.iid(report) unless report.is_a?(Report)
75
+
76
+ name, directory = name_and_path_from_source(source, "#{report.url}/stylesheets")
77
+
78
+ file = report.files.where('name = ? and directory = ?', name, directory).first
79
+
80
+ file.nil? ? '' : file.fully_qualified_url
81
+ end
82
+
83
+ def report_javascript_path(report, source)
84
+ report = Report.iid(report) unless report.is_a?(Report)
85
+
86
+ name, directory = name_and_path_from_source(source, "#{report.url}/javascripts")
87
+
88
+ file = report.files.where('name = ? and directory = ?', name, directory).first
89
+
90
+ file.nil? ? '' : file.fully_qualified_url
91
+ end
92
+
93
+ def name_and_path_from_source(source, base_directory)
94
+ path = source.split('/')
95
+ name = path.last
96
+
97
+ directory = if path.length > 1
98
+ #remove last element
99
+ path.pop
100
+
101
+ "#{base_directory}/#{path.join('/')}"
102
+ else
103
+ base_directory
104
+ end
105
+
106
+ return name, directory
107
+ end
108
+
109
+ def report_expand_stylesheet_sources(report, sources, recursive = false)
110
+ if sources.include?(:all)
111
+ all_stylesheet_files = collect_asset_files(report.base_dir + '/stylesheets', ('**' if recursive), '*.css').uniq
112
+ else
113
+ sources.flatten
114
+ end
115
+ end
116
+
117
+ def report_expand_javascript_sources(report, sources, recursive = false)
118
+ if sources.include?(:all)
119
+ collect_asset_files(report.base_dir + '/javascripts', ('**' if recursive), '*.js').uniq
120
+ else
121
+ sources.flatten
122
+ end
123
+ end
124
+
125
+ def report_stylesheet_tag(report, source, options)
126
+ options = {"rel" => "stylesheet", "type" => Mime::CSS, "media" => "screen",
127
+ "href" => html_escape(report_stylesheet_path(report, source))}.merge(options)
128
+ tag("link", options, false, false)
129
+ end
130
+
131
+ def report_javascript_src_tag(report, source, options)
132
+ options = {"type" => Mime::JS, "src" => report_javascript_path(report, source)}.merge(options)
133
+ content_tag("script", "", options)
134
+ end
135
+
136
+ def report_image_tag(report_id, source, options = {})
137
+ report = Report.iid(report_id)
138
+ return("could not find report with the id #{report_id}") unless report
139
+
140
+ if request && request.format.symbol == :pdf
141
+ img_path = report.image_path(source)
142
+
143
+ file_support = ErpTechSvcs::FileSupport::Base.new(:storage => ErpTechSvcs::Config.file_storage)
144
+
145
+ image_tag "data:image/png;base64,#{Base64.encode64(file_support.get_contents(img_path).first)}"
146
+ else
147
+ options.symbolize_keys!
148
+ options[:src] = report_image_path(report, source)
149
+ options[:alt] ||= File.basename(options[:src], '.*').split('.').first.to_s.capitalize
150
+
151
+ if size = options.delete(:size)
152
+ options[:width], options[:height] = size.split("x") if size =~ %r{^\d+x\d+$}
153
+ end
154
+
155
+ if mouseover = options.delete(:mouseover)
156
+ options[:onmouseover] = "this.src='#{report_image_path(report, mouseover)}'"
157
+ options[:onmouseout] = "this.src='#{report_image_path(report, options[:src])}'"
158
+ end
159
+
160
+ tag("img", options)
161
+ end
162
+ end
163
+
164
+ def report_image_path(report, source)
165
+ report = Report.iid(report) unless report.is_a?(Report)
166
+
167
+ name, directory = name_and_path_from_source(source, "#{report.url}/images")
168
+
169
+ file = report.files.where('name = ? and directory = ?', name, directory).first
170
+
171
+ file.nil? ? '' : file.fully_qualified_url
172
+ end
173
+
174
+ def bootstrap_load
175
+ stylesheet_link_tag "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"
176
+ end
177
+
178
+ def jquery_load
179
+ javascript_include_tag "https://code.jquery.com/jquery-1.10.0.min.js"
180
+ end
181
+
25
182
  end #ReportHelper
26
183
  end #Helpers
27
184
  end #ActionView
28
185
  end #Railties
29
186
  end #Extensions
30
- end #RailsDbAdmin
187
+ end #RailsDbAdmin
@@ -14,27 +14,43 @@ module RailsDbAdmin
14
14
  total_count = self.get_total_count(options[:table])
15
15
  arel_table = Arel::Table::new(options[:table])
16
16
 
17
- if options[:limit] && options[:offset] && options[:order]
18
- query = arel_table.project(Arel.sql('*')).order(options[:order]).
19
- take(@connection.sanitize_limit(options[:limit])).
20
- skip(options[:offset].to_i)
21
- elsif options[:limit] && options[:order]
22
- query = arel_table.project(Arel.sql('*')).order(options[:order]).
23
- take(@connection.sanitize_limit(options[:limit]))
24
- elsif options[:limit] && !options[:order]
25
- query = arel_table.project(Arel.sql('*')).
26
- take(@connection.sanitize_limit(options[:limit]))
27
- elsif !options[:limit] && options[:order]
28
- query = arel_table.project(Arel.sql('*')).order(options[:order])
17
+ if options[:query_filter].present?
18
+ table_name = options[:table].classify.constantize
19
+ accepted_columns = table_name.columns.select {|column| column.type == :string or column.type == :text}
20
+ where_clause = ""
21
+ accepted_columns.each_with_index do |column, index|
22
+ if index == 0
23
+ where_clause = table_name.arel_table[column.name.to_sym].matches("%#{options[:query_filter]}%")
24
+ else
25
+ where_clause = where_clause.or(table_name.arel_table[column.name.to_sym].matches("%#{options[:query_filter]}%"))
26
+ end
27
+ end
28
+ rows = table_name.where(where_clause)
29
29
  else
30
- query = arel_table.project(Arel.sql('*'))
31
- end
32
30
 
33
- # This is a temporary partial fix to handle postgres boolean columns which is use activerecord when possible
34
- begin
35
- rows = options[:table].classify.constantize.find_by_sql(query.to_sql)
36
- rescue
37
- rows = @connection.select_all(query.to_sql)
31
+ if options[:limit] && options[:offset] && options[:order]
32
+ query = arel_table.project(Arel.sql('*')).order(options[:order]).
33
+ take(@connection.sanitize_limit(options[:limit])).
34
+ skip(options[:offset].to_i)
35
+ elsif options[:limit] && options[:order]
36
+ query = arel_table.project(Arel.sql('*')).order(options[:order]).
37
+ take(@connection.sanitize_limit(options[:limit]))
38
+ elsif options[:limit] && !options[:order]
39
+ query = arel_table.project(Arel.sql('*')).
40
+ take(@connection.sanitize_limit(options[:limit]))
41
+ elsif !options[:limit] && options[:order]
42
+ query = arel_table.project(Arel.sql('*')).order(options[:order])
43
+ else
44
+ query = arel_table.project(Arel.sql('*'))
45
+ end
46
+
47
+ # This is a temporary partial fix to handle postgres boolean columns which is use activerecord when possible
48
+ begin
49
+ rows = options[:table].classify.constantize.find_by_sql(query.to_sql)
50
+ rescue
51
+ rows = @connection.select_all(query.to_sql)
52
+ end
53
+
38
54
  end
39
55
 
40
56
  records = RailsDbAdmin::TableSupport.database_rows_to_hash(rows)
@@ -79,7 +95,7 @@ module RailsDbAdmin
79
95
  rows = @connection.select_all("SELECT COUNT(*) as count FROM #{table}")
80
96
  records = RailsDbAdmin::TableSupport.database_rows_to_hash(rows)
81
97
  total_count = records[0][:count]
82
-
98
+
83
99
  total_count
84
100
  end
85
101
  end
@@ -0,0 +1,14 @@
1
+ module RailsDbAdmin
2
+ class QueryParams
3
+ def initialize(hash)
4
+ hash.each do |key, value|
5
+ singleton_class.send(:define_method, key) { value }
6
+ end
7
+ end
8
+
9
+ def get_binding
10
+ binding
11
+ end
12
+
13
+ end
14
+ end
@@ -1,34 +1,43 @@
1
1
  require 'fileutils'
2
2
 
3
3
  module RailsDbAdmin
4
- class QuerySupport
4
+ class QuerySupport
5
+
5
6
  def initialize(database_connection_class, database_connection_name)
6
7
  @path = File.join(Rails.root, Rails.application.config.rails_db_admin.query_location, database_connection_name)
7
8
  @connection = database_connection_class.connection
8
9
  end
9
-
10
- def execute_sql(sql)
11
- begin
12
- rows = @connection.select_all(sql)
13
- rescue => ex
14
- return nil, nil, ex.message
15
- end
16
-
17
- values = []
18
- columns = []
19
-
20
- unless rows.nil? || rows.empty?
21
- columns = rows[0].keys
22
- rows.each do |row|
23
- values << row
24
- end
25
- end
26
-
27
- return columns, values, nil
28
- end
10
+
11
+ def execute_sql(sql)
12
+ begin
13
+ result = @connection.execute(sql)
14
+ rescue => ex
15
+ return nil, nil, ex.message
16
+ end
17
+
18
+ values = []
19
+ columns = []
20
+
21
+ if @connection.class == ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
22
+ columns = result.fields
23
+
24
+ if result && result.count > 0
25
+ result.each do |row|
26
+ values << HashWithIndifferentAccess.new(row)
27
+ end
28
+ end
29
+ else
30
+ columns = result[0].keys
31
+ result.each do |row|
32
+ values << HashWithIndifferentAccess.new(row)
33
+ end
34
+ end
35
+
36
+ return columns, values, nil
37
+ end
29
38
 
30
39
  def select_top_fifty(table)
31
- #Actually, sanitizing here is pretty redundent since it's a constant...
40
+ #Actually, sanitizing here is pretty redundant since it's a constant...
32
41
  ar = Arel::Table::new(table)
33
42
  query = ar.project(Arel.sql('*')).take(50)
34
43
  #query = "SELECT * FROM #{table} LIMIT #{@connection.sanitize_limit(50)}"
@@ -45,34 +54,35 @@ module RailsDbAdmin
45
54
  return query.to_sql, records
46
55
  end
47
56
 
48
- def get_saved_query_names
49
- query_files = []
50
-
51
- if File.directory? @path
52
- query_files = Dir.entries(@path)
53
- query_files.delete_if{|name| name =~ /^\./}
54
- query_files.each do |file_name|
55
- file_name.gsub!('.sql', '')
56
- end
57
- end
58
-
59
- query_files
60
- end
61
-
62
- def save_query(query, name)
63
- FileUtils.mkdir_p(@path) unless File.directory? @path
57
+ def get_saved_query_names
58
+ query_files = []
59
+
60
+ if File.directory? @path
61
+ query_files = Dir.entries(@path)
62
+ query_files.delete_if { |name| name =~ /^\./ }
63
+ query_files.each do |file_name|
64
+ file_name.gsub!('.sql', '')
65
+ end
66
+ end
67
+
68
+ query_files
69
+ end
70
+
71
+ def save_query(query, name)
72
+ FileUtils.mkdir_p(@path) unless File.directory? @path
64
73
 
65
74
  file_path = File.join(@path, "#{name}.sql")
66
- File.new(file_path, 'w') unless File.exist?(File.join(file_path))
67
- File.open(file_path, 'w+'){|f| f.puts(query) }
68
- end
69
-
70
- def delete_query(name)
71
- FileUtils.rm(File.join(@path, "#{name}.sql")) if File.exist?(File.join(@path, "#{name}.sql"))
72
- end
73
-
75
+ File.new(file_path, 'w') unless File.exist?(File.join(file_path))
76
+ File.open(file_path, 'w+') { |f| f.puts(query) }
77
+ end
78
+
79
+ def delete_query(name)
80
+ FileUtils.rm(File.join(@path, "#{name}.sql")) if File.exist?(File.join(@path, "#{name}.sql"))
81
+ end
82
+
74
83
  def get_query(name)
75
84
  File.open(File.join(@path, "#{name}.sql")) { |f| f.read } if File.exist?(File.join(@path, "#{name}.sql"))
76
85
  end
86
+
77
87
  end
78
88
  end