motor-admin 0.1.25 → 0.1.27

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d25cd72aeb7206cf66b1d9c632491ad8e0e15a356c59e3234f4fcac2ab8e6960
4
- data.tar.gz: e4e0458c383689d39a8065b5c1d5ab48d57ebd19b5ea1f2c4f229bf1e070c466
3
+ metadata.gz: 696c86b5eeeb132c8216b7662999f671926e0fc9149d74ccb66feb2d120e5e4a
4
+ data.tar.gz: 00550110edef326dc5d063577bf29a936e4890031f02f9412fb03b431c310476
5
5
  SHA512:
6
- metadata.gz: 82f524ca428f34514ad3f6361287de3faf8430da549a371196abaf0dbee0bd02068127be712a208708252d8aa7a7c41c84e8df6bc8f96f8ada747bdebc26a076
7
- data.tar.gz: b1921a2b38711019965f178120bff24dbe2f2465e750cdc592b2cac558820c51ce6c0addd9ca414ed4d3c90b4bd2c9aa15c112ed4072181fa47f8534d84b8a2b
6
+ metadata.gz: c91831b3bca03a0f490fee511dbdb70e0635f7e639778b24df48fc248858ead0de39c0744659f388cfcacaa960f01e4d5622b32bab6d1ca1528819aa2da721d1
7
+ data.tar.gz: 010d5465953944c84189ac3c0a458df1ade18175bf2999faf4259e002fb37d7a49ec6428da755c6bac4de5efaa41193c955d0ca7f72c1bc0dc6c35a61210bd65
data/lib/motor/admin.rb CHANGED
@@ -22,10 +22,10 @@ module Motor
22
22
  ::Digest::SHA256.hexdigest(username),
23
23
  ::Digest::SHA256.hexdigest(ENV['MOTOR_AUTH_USERNAME'].to_s)
24
24
  ) &
25
- ActiveSupport::SecurityUtils.secure_compare(
26
- ::Digest::SHA256.hexdigest(password),
27
- ::Digest::SHA256.hexdigest(ENV['MOTOR_AUTH_PASSWORD'].to_s)
28
- )
25
+ ActiveSupport::SecurityUtils.secure_compare(
26
+ ::Digest::SHA256.hexdigest(password),
27
+ ::Digest::SHA256.hexdigest(ENV['MOTOR_AUTH_PASSWORD'].to_s)
28
+ )
29
29
  end
30
30
  end
31
31
 
data/lib/motor/queries.rb CHANGED
@@ -5,6 +5,7 @@ module Motor
5
5
  end
6
6
  end
7
7
 
8
+ require_relative './queries/render_sql_template'
8
9
  require_relative './queries/run_query'
9
10
  require_relative './queries/persistance'
10
11
  require_relative './queries/postgresql_exec_query'
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Motor
4
+ module Queries
5
+ module RenderSqlTemplate
6
+ SECTION_OPEN_REGEXP = /{{([#^])\s*(\w+)}}.*\z/m.freeze
7
+ VARIABLE_REGEXP = /{{\s*(\w+)\s*}}/m.freeze
8
+
9
+ module_function
10
+
11
+ def call(sql, variables)
12
+ result = render_sections(sql, variables)
13
+
14
+ interpolate_variables(result, variables)
15
+ end
16
+
17
+ def interpolate_variables(sql, variables)
18
+ selected_variables = []
19
+
20
+ rendered =
21
+ sql.gsub(VARIABLE_REGEXP) do
22
+ variable_name = Regexp.last_match[1]
23
+
24
+ index = selected_variables.index { |name, _| name == variable_name }
25
+ selected_variables << [variable_name, variables[variable_name]] unless index
26
+
27
+ "$#{selected_variables.size}"
28
+ end
29
+
30
+ [rendered, selected_variables]
31
+ end
32
+
33
+ def render_sections(sql, variables)
34
+ sql.sub(SECTION_OPEN_REGEXP) do |e|
35
+ variable_name = Regexp.last_match[2]
36
+ is_negative = Regexp.last_match[1] == '^'
37
+
38
+ _, content, rest = e.split(build_section_close_regexp(variable_name), 3)
39
+
40
+ is_present = variables[variable_name].present?
41
+
42
+ render_sections(is_present ^ is_negative ? content + rest.to_s : rest, variables)
43
+ end
44
+ end
45
+
46
+ def build_section_close_regexp(variable_name)
47
+ %r{{{[#^/]s*#{Regexp.escape(variable_name)}\s*}}}m
48
+ end
49
+ end
50
+ end
51
+ end
@@ -4,7 +4,6 @@ module Motor
4
4
  module Queries
5
5
  module RunQuery
6
6
  DEFAULT_LIMIT = 1_000_000
7
- INTERPOLATION_REGEXP = /{{(\w+)}}/.freeze
8
7
 
9
8
  QueryResult = Struct.new(:data, :columns, keyword_init: true)
10
9
 
@@ -48,26 +47,30 @@ module Motor
48
47
  end
49
48
 
50
49
  def prepare_sql_statement(query, limit, variables_hash)
51
- variables = query.preferences.fetch(:variables, []).pluck(:name, :default_value)
50
+ variables = merge_variable_default_values(query.preferences.fetch(:variables, []), variables_hash)
52
51
 
53
- sql =
54
- query.sql_body.gsub(INTERPOLATION_REGEXP) do
55
- index = variables.index { |name, _| name == (Regexp.last_match[1]) } + 1
52
+ sql, query_variables = RenderSqlTemplate.call(query.sql_body, variables)
56
53
 
57
- "$#{index}"
58
- end
59
-
60
- attributes =
61
- variables.map do |variable_name, default_value|
62
- ActiveRecord::Relation::QueryAttribute.new(
63
- variable_name,
64
- variables_hash[variable_name] || default_value,
65
- ActiveRecord::Type::Value.new
66
- )
67
- end
54
+ attributes = build_statement_attributes(query_variables)
68
55
 
69
56
  [format(WITH_STATEMENT_TEMPLATE, sql_body: sql.strip.gsub(/;\z/, ''), limit: limit), 'SQL', attributes]
70
57
  end
58
+
59
+ def build_statement_attributes(variables)
60
+ variables.map do |variable_name, value|
61
+ ActiveRecord::Relation::QueryAttribute.new(
62
+ variable_name,
63
+ value,
64
+ ActiveRecord::Type::Value.new
65
+ )
66
+ end
67
+ end
68
+
69
+ def merge_variable_default_values(variable_configs, variables_hash)
70
+ variable_configs.each_with_object({}) do |variable, acc|
71
+ acc[variable[:name]] = variables_hash[variable[:name]] || variable[:default_value]
72
+ end
73
+ end
71
74
  end
72
75
  end
73
76
  end
data/lib/motor/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Motor
4
- VERSION = '0.1.25'
4
+ VERSION = '0.1.27'
5
5
  end
@@ -5,9 +5,9 @@
5
5
  "fonts/ionicons.ttf?v=3.0.0-alpha.3": "fonts/ionicons.ttf",
6
6
  "fonts/ionicons.woff2?v=3.0.0-alpha.3": "fonts/ionicons.woff2",
7
7
  "fonts/ionicons.woff?v=3.0.0-alpha.3": "fonts/ionicons.woff",
8
- "main-07eb806daf063d260c47.css.gz": "main-07eb806daf063d260c47.css.gz",
9
- "main-07eb806daf063d260c47.js.LICENSE.txt": "main-07eb806daf063d260c47.js.LICENSE.txt",
10
- "main-07eb806daf063d260c47.js.gz": "main-07eb806daf063d260c47.js.gz",
11
- "main.css": "main-07eb806daf063d260c47.css",
12
- "main.js": "main-07eb806daf063d260c47.js"
8
+ "main-d6c1f237d3568a138874.css.gz": "main-d6c1f237d3568a138874.css.gz",
9
+ "main-d6c1f237d3568a138874.js.LICENSE.txt": "main-d6c1f237d3568a138874.js.LICENSE.txt",
10
+ "main-d6c1f237d3568a138874.js.gz": "main-d6c1f237d3568a138874.js.gz",
11
+ "main.css": "main-d6c1f237d3568a138874.css",
12
+ "main.js": "main-d6c1f237d3568a138874.js"
13
13
  }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: motor-admin
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.25
4
+ version: 0.1.27
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pete Matsyburka
@@ -208,13 +208,14 @@ files:
208
208
  - lib/motor/queries.rb
209
209
  - lib/motor/queries/persistance.rb
210
210
  - lib/motor/queries/postgresql_exec_query.rb
211
+ - lib/motor/queries/render_sql_template.rb
211
212
  - lib/motor/queries/run_query.rb
212
213
  - lib/motor/tags.rb
213
214
  - lib/motor/ui_configs.rb
214
215
  - lib/motor/version.rb
215
216
  - ui/dist/fonts/ionicons.woff2
216
- - ui/dist/main-07eb806daf063d260c47.css.gz
217
- - ui/dist/main-07eb806daf063d260c47.js.gz
217
+ - ui/dist/main-d6c1f237d3568a138874.css.gz
218
+ - ui/dist/main-d6c1f237d3568a138874.js.gz
218
219
  - ui/dist/manifest.json
219
220
  homepage:
220
221
  licenses: