adhoq 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e44fcb3a79e299b2a337652f8439d58cd4babe71
4
- data.tar.gz: 5312feb5fee5890cae4b27f48c541a36b937ef0e
3
+ metadata.gz: 02a157e9e5f920c790dacf15b8922fc3788e72f5
4
+ data.tar.gz: 29ed73191c4f2446ee01b1c9d4963ad232c982a4
5
5
  SHA512:
6
- metadata.gz: 19546d4c89ab4cbb7da38b46e02a7cc4bfe091d1d00bbd3110d186a26586f929cdccdab8bf2bf12f322cfaf052297d102eefcfe3660c47012da952a0464baae9
7
- data.tar.gz: 914e24aabd3a6a896c62e3c276642f8a6ba37d85d8d6f285363e5c1dbf6b4a3025f714f065989475b4042edfb8ab513ef2462974fb45214fa754c5f640ef345e
6
+ metadata.gz: 930398b399c5a821fe2374ddc37c1563224d14a03add9a0523af2b0fa427801ca345d6c894674445fac1178f7be07aeed6a747a006883da1168d191370c9bdcb
7
+ data.tar.gz: c0e698cfd0ca88aff00437bd88c6fdeaf9ddeb72b6b8b3528638dea83cab0678b704100c1156920185f21d34d3557252f1bfb1d7501df7a7f5440f45e63d51ef
data/README.md CHANGED
@@ -60,6 +60,7 @@ Edit initialization file in `config/initializers/adhoq.rb`
60
60
 
61
61
  ```ruby
62
62
  Adhoq.configure do |config|
63
+ # if not set, use :on_the_fly.(default)
63
64
  config.storage = [:local_file, Rails.root + './path/to/store/report/files']
64
65
  config.authorization = ->(controller) { controller.signed_in? }
65
66
  end
@@ -7,13 +7,16 @@ $short-span: $font-size-base / 2
7
7
 
8
8
  // engine styles
9
9
 
10
- .new-execution form.form-inline
11
- .form-group.report_format
10
+ .new-execution form
11
+ .form-group.report_format, .form-group.query_parameters
12
12
  margin-right: $font-size-base
13
13
 
14
14
  label
15
15
  margin-right: $short-span
16
16
 
17
+ .query_parameter
18
+ margin-bottom: $short-span
19
+
17
20
  #main > section > .page-header, #main > section > form.query-form .page-header
18
21
  margin-top: 0
19
22
 
@@ -13,7 +13,7 @@ module Adhoq
13
13
  private
14
14
 
15
15
  def synced_create
16
- @execution = current_query.execute!(params[:execution][:report_format])
16
+ @execution = current_query.execute!(params[:execution][:report_format], query_parameters)
17
17
 
18
18
  if @execution.report.on_the_fly?
19
19
  respond_report(@execution.report)
@@ -23,7 +23,7 @@ module Adhoq
23
23
  end
24
24
 
25
25
  def asynced_create
26
- Adhoq::ExecuteJob.perform_later(current_query, params[:execution][:report_format])
26
+ Adhoq::ExecuteJob.perform_later(current_query, params[:execution][:report_format], query_parameters)
27
27
  redirect_to current_query
28
28
  end
29
29
 
@@ -42,5 +42,9 @@ module Adhoq
42
42
  def async_execution?
43
43
  Adhoq.config.async_execution? && !Adhoq.current_storage.is_a?(Adhoq::Storage::OnTheFly)
44
44
  end
45
+
46
+ def query_parameters
47
+ params[:parameters] || HashWithIndifferentAccess.new
48
+ end
45
49
  end
46
50
  end
@@ -4,7 +4,9 @@ module Adhoq
4
4
 
5
5
  def create
6
6
  begin
7
- @result = Adhoq::Executor.new(params[:query]).execute
7
+ query = Adhoq::Query.new(query: params[:query])
8
+ raw_query = query.substitute_query(params[:parameters] || {})
9
+ @result = Adhoq::Executor.new(raw_query).execute
8
10
  rescue ActiveRecord::StatementInvalid => @statement_invalid
9
11
  render 'statement_invalid', status: :unprocessable_entity
10
12
  end
@@ -29,6 +29,11 @@ module Adhoq
29
29
  redirect_to @query
30
30
  end
31
31
 
32
+ def destroy
33
+ Adhoq::Query.find(params[:id]).destroy!
34
+ redirect_to action: :index
35
+ end
36
+
32
37
  private
33
38
 
34
39
  def query_attributes
@@ -30,5 +30,9 @@ module Adhoq
30
30
  def table_order_key(ar_class)
31
31
  ar_class.primary_key || ar_class.columns.first.name
32
32
  end
33
+
34
+ def query_parameter_field(name)
35
+ text_field_tag "parameters[#{name}]", nil, class: "form-control"
36
+ end
33
37
  end
34
38
  end
@@ -4,11 +4,33 @@ module Adhoq
4
4
 
5
5
  has_many :executions, dependent: :destroy, inverse_of: :query
6
6
 
7
- def execute!(report_format)
7
+ PARAMETER_PATTERN = /\$(?<name>\w+)|\${(?<name>\w+)}/i.freeze
8
+
9
+ def execute!(report_format, query_parameters = {})
8
10
  executions.create! {|exe|
9
11
  exe.report_format = report_format
10
- exe.raw_sql = query
12
+ exe.raw_sql = substitute_query(query_parameters)
11
13
  }.tap(&:generate_report!)
12
14
  end
15
+
16
+ def parameters
17
+ return @parameters if @parameters
18
+
19
+ @parameters = query.scan(PARAMETER_PATTERN).each_with_object([]) do |(match1, match2), arr|
20
+ name = match1 || match2
21
+
22
+ arr << name.downcase
23
+ end
24
+ end
25
+
26
+ def substitute_query(query_parameters)
27
+ return query if parameters.empty?
28
+
29
+ query_parameters = query_parameters.with_indifferent_access
30
+ query.gsub(PARAMETER_PATTERN) do |_, arr|
31
+ name = Regexp.last_match["name"]
32
+ query_parameters[name]
33
+ end
34
+ end
13
35
  end
14
36
  end
@@ -6,6 +6,8 @@ section.query
6
6
  = link_to [:edit, query], class: 'btn btn-default btn-sm' do
7
7
  i.fa.fa-pencil.fa-pad-r
8
8
  | Edit
9
+ .pull-right
10
+ = link_to 'Delete', query, class: 'btn btn-default btn-sm', :method => :delete, data: { confirm: 'Are you sure?' }
9
11
  .clearfix
10
12
  small= "Updated at #{l(query.updated_at, format: :short)}"
11
13
  p.description= query.description
@@ -17,10 +19,17 @@ section.query
17
19
 
18
20
  section.new-execution
19
21
  h2 Create report
20
- = form_for [query, query.executions.build], html: {class: 'form-inline', role: 'form'} do |f|
21
- .form-group.report_format
22
+ = form_for [query, query.executions.build], html: {role: 'form'} do |f|
23
+ .form-inline.form-group.report_format
22
24
  = f.label :report_format
23
25
  = f.select :report_format, f.object.supported_formats, {}, class: 'form-control'
26
+ - if query.parameters.present?
27
+ h4 Query parameters
28
+ .form-group.query_parameters
29
+ - query.parameters.each do |param_name|
30
+ .form-inline.query_parameter
31
+ = label_tag "parameters_#{param_name}", "#{param_name}"
32
+ = query_parameter_field(param_name)
24
33
  .form-group
25
34
  = f.submit 'Create report', class: 'btn btn-default'
26
35
 
@@ -3,7 +3,9 @@ module Adhoq
3
3
  class Configuration
4
4
  include ActiveSupport::Configurable
5
5
 
6
- config_accessor :storage
6
+ config_accessor :storage do
7
+ [:on_the_fly]
8
+ end
7
9
 
8
10
  config_accessor :authorization
9
11
  config_accessor :authorization_failure_action
data/lib/adhoq/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Adhoq
2
- VERSION = "0.0.6"
2
+ VERSION = "0.0.7"
3
3
  end
@@ -1,4 +1,32 @@
1
1
  module Adhoq
2
2
  RSpec.describe Query, type: :model do
3
+ describe "#parameters" do
4
+ it "returns query parameter Array of Hash" do
5
+ query = Query.new(query: "SELECT * FROM users where created_at > '$time' AND activated = ${activated}")
6
+ expect(query.parameters).to eq [
7
+ "time",
8
+ "activated",
9
+ ]
10
+ end
11
+ end
12
+
13
+ describe "#substitute_query" do
14
+ it "returns query string with parameter binding" do
15
+ query = Query.new(query: "SELECT * FROM users where created_at > '$time' AND activated = ${activated}")
16
+ substitute = query.substitute_query({
17
+ time: "2010-10-01 10:00:00",
18
+ activated: "1",
19
+ })
20
+ expect(substitute).to eq "SELECT * FROM users where created_at > '2010-10-01 10:00:00' AND activated = 1"
21
+ end
22
+
23
+ context "query doesn't need parameter" do
24
+ it "returns query string" do
25
+ query = Query.new(query: "SELECT * FROM users")
26
+ substitute = query.substitute_query({})
27
+ expect(substitute).to eq "SELECT * FROM users"
28
+ end
29
+ end
30
+ end
3
31
  end
4
32
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: adhoq
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kyosuke MOROHASHI
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-16 00:00:00.000000000 Z
11
+ date: 2015-12-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails