nquery 0.1.3 → 0.1.4
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 +4 -4
- data/CHANGELOG.md +17 -0
- data/app/assets/builds/nquery/application.css +88 -4
- data/app/assets/builds/nquery/application.js +353 -3
- data/app/controllers/concerns/nquery/chart_actions.rb +1 -1
- data/app/controllers/concerns/nquery/chart_results.rb +54 -7
- data/app/controllers/nquery/dashboard/cards_controller.rb +28 -0
- data/app/controllers/nquery/dashboards_controller.rb +49 -9
- data/app/controllers/nquery/embed/dashboards_controller.rb +17 -1
- data/app/controllers/nquery/public/dashboards_controller.rb +14 -1
- data/app/controllers/nquery/queries_controller.rb +23 -2
- data/app/models/nquery/chart.rb +4 -0
- data/app/models/nquery/dashboard.rb +129 -0
- data/app/models/nquery/query.rb +68 -0
- data/app/services/nquery/application_service.rb +9 -0
- data/app/services/nquery/dashboards/card_parameters.rb +37 -0
- data/app/services/nquery/dashboards/filters.rb +75 -0
- data/app/services/nquery/dashboards/parameter_charts.rb +31 -0
- data/app/services/nquery/dashboards/parameter_rows.rb +60 -0
- data/app/views/layouts/nquery/application.html.erb +8 -0
- data/app/views/nquery/charts/_builder_form.html.erb +12 -0
- data/app/views/nquery/charts/_variable_row.html.erb +10 -0
- data/app/views/nquery/charts/_variables.html.erb +28 -0
- data/app/views/nquery/charts/update.turbo_stream.erb +3 -0
- data/app/views/nquery/dashboards/_chart_card.html.erb +27 -1
- data/app/views/nquery/dashboards/_parameter_chart_ids.html.erb +6 -0
- data/app/views/nquery/dashboards/_parameters.html.erb +159 -0
- data/app/views/nquery/dashboards/edit.html.erb +10 -0
- data/app/views/nquery/dashboards/show.html.erb +37 -1
- data/app/views/nquery/shared/_icon.html.erb +4 -0
- data/app/views/nquery/shared/_sidebar.html.erb +1 -1
- data/app/views/nquery/shared/_topbar.html.erb +3 -0
- data/config/routes.rb +4 -0
- data/db/migrate/20261005145000_add_parameters_to_nquery_dashboards.rb +7 -0
- data/db/migrate/20261005190000_add_parameters_to_nquery_queries.rb +7 -0
- data/lib/nquery/dashboard_parameters.rb +56 -0
- data/lib/nquery/data_sources/adapter.rb +10 -1
- data/lib/nquery/data_sources/mysql_adapter.rb +2 -2
- data/lib/nquery/data_sources/postgresql_adapter.rb +2 -2
- data/lib/nquery/data_sources/rails_adapter.rb +2 -7
- data/lib/nquery/data_sources/row_limit.rb +211 -0
- data/lib/nquery/parameter_value.rb +97 -0
- data/lib/nquery/query_runner.rb +37 -4
- data/lib/nquery/seeder.rb +9 -1
- data/lib/nquery/statement_binds.rb +112 -0
- data/lib/nquery/version.rb +1 -1
- data/lib/nquery.rb +4 -0
- data/nquery.gemspec +2 -2
- metadata +22 -6
|
@@ -18,13 +18,13 @@ module Nquery
|
|
|
18
18
|
true
|
|
19
19
|
end
|
|
20
20
|
|
|
21
|
-
def execute_readonly(statement, timeout: 15, row_limit: 10_000)
|
|
21
|
+
def execute_readonly(statement, timeout: 15, row_limit: 10_000, binds: [])
|
|
22
22
|
rows = []
|
|
23
23
|
columns = []
|
|
24
24
|
duration = Benchmark.realtime do
|
|
25
25
|
connection.transaction do
|
|
26
26
|
connection.execute("SET TRANSACTION READ ONLY") if postgresql?
|
|
27
|
-
result = connection
|
|
27
|
+
result = exec_limited_query(connection, statement, row_limit, binds)
|
|
28
28
|
columns = result.columns
|
|
29
29
|
rows = result.rows
|
|
30
30
|
raise ActiveRecord::Rollback
|
|
@@ -49,11 +49,6 @@ module Nquery
|
|
|
49
49
|
def postgresql?
|
|
50
50
|
connection.adapter_name.downcase.include?("postgres")
|
|
51
51
|
end
|
|
52
|
-
|
|
53
|
-
def sanitize_limit(statement, limit)
|
|
54
|
-
stripped = statement.strip.sub(/;\s*\z/, "")
|
|
55
|
-
"#{stripped} LIMIT #{limit.to_i}"
|
|
56
|
-
end
|
|
57
52
|
end
|
|
58
53
|
end
|
|
59
54
|
end
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Nquery
|
|
4
|
+
module DataSources
|
|
5
|
+
# Applies a row cap to a user statement without emitting a second LIMIT.
|
|
6
|
+
class RowLimit
|
|
7
|
+
def self.apply(statement, limit)
|
|
8
|
+
new(statement, limit).apply
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def initialize(statement, limit)
|
|
12
|
+
@statement = statement.to_s.strip.sub(/;\s*\z/, "")
|
|
13
|
+
@limit = limit.to_i
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def apply
|
|
17
|
+
scan = Scan.new(@statement)
|
|
18
|
+
prefix, suffix = split_unclosed_comment(scan)
|
|
19
|
+
clause = TrailingClause.find(prefix, scan)
|
|
20
|
+
sql = capped_sql(prefix, suffix, clause)
|
|
21
|
+
# SQLite does not tokenize a terminal /* as a comment unless another character follows.
|
|
22
|
+
return sql unless sql.end_with?("/*")
|
|
23
|
+
|
|
24
|
+
"#{sql}\n"
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
private
|
|
28
|
+
|
|
29
|
+
def capped_sql(prefix, suffix, clause)
|
|
30
|
+
return append_limit(prefix, suffix) if clause.blank?
|
|
31
|
+
return @statement unless clause.exceeds?(@limit)
|
|
32
|
+
|
|
33
|
+
"#{clause.rewrite(@limit)}#{suffix}"
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def append_limit(prefix, suffix)
|
|
37
|
+
capped = "#{prefix.rstrip}\nLIMIT #{@limit}"
|
|
38
|
+
return capped if suffix.empty?
|
|
39
|
+
|
|
40
|
+
"#{capped} #{suffix.lstrip}"
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def split_unclosed_comment(scan)
|
|
44
|
+
index = scan.unclosed_comment_index
|
|
45
|
+
return [@statement, ""] unless index
|
|
46
|
+
|
|
47
|
+
[@statement[0...index], @statement[index..]]
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# One left-to-right pass. A /* or $$ inside a string or comment is not an opener.
|
|
51
|
+
class Scan
|
|
52
|
+
DOLLAR_TAG = /\A\$(?:[A-Za-z_][A-Za-z0-9_]*)?\$/
|
|
53
|
+
private_constant :DOLLAR_TAG
|
|
54
|
+
|
|
55
|
+
def initialize(statement)
|
|
56
|
+
@statement = statement
|
|
57
|
+
@index = 0
|
|
58
|
+
@length = statement.length
|
|
59
|
+
@unclosed_comment_index = nil
|
|
60
|
+
@line_comments = []
|
|
61
|
+
advance
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
attr_reader :unclosed_comment_index
|
|
65
|
+
|
|
66
|
+
def inside_line_comment?(position)
|
|
67
|
+
@line_comments.any? { |range| range.cover?(position) }
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
private
|
|
71
|
+
|
|
72
|
+
def advance
|
|
73
|
+
while @index < @length
|
|
74
|
+
next if skip_token
|
|
75
|
+
|
|
76
|
+
@index += 1
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def skip_token
|
|
81
|
+
consume_block_comment || consume_line_comment || consume_single_quote ||
|
|
82
|
+
consume_double_quote || consume_dollar_quote
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def consume_block_comment
|
|
86
|
+
return false unless starts_with?("/*")
|
|
87
|
+
|
|
88
|
+
close_at = @statement.index("*/", @index + 2)
|
|
89
|
+
if close_at
|
|
90
|
+
@index = close_at + 2
|
|
91
|
+
else
|
|
92
|
+
@unclosed_comment_index = @index
|
|
93
|
+
@index = @length
|
|
94
|
+
end
|
|
95
|
+
true
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def consume_line_comment
|
|
99
|
+
return false unless starts_with?("--")
|
|
100
|
+
|
|
101
|
+
finish = @statement.index("\n", @index) || @length
|
|
102
|
+
@line_comments << (@index...finish)
|
|
103
|
+
@index = finish < @length ? finish + 1 : finish
|
|
104
|
+
true
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def consume_single_quote
|
|
108
|
+
return false unless @statement[@index] == "'"
|
|
109
|
+
|
|
110
|
+
@index += 1
|
|
111
|
+
while @index < @length
|
|
112
|
+
if @statement[@index] == "'" && @statement[@index + 1] == "'"
|
|
113
|
+
@index += 2
|
|
114
|
+
elsif @statement[@index] == "'"
|
|
115
|
+
@index += 1
|
|
116
|
+
break
|
|
117
|
+
else
|
|
118
|
+
@index += 1
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
true
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
def consume_double_quote
|
|
125
|
+
return false unless @statement[@index] == '"'
|
|
126
|
+
|
|
127
|
+
@index += 1
|
|
128
|
+
while @index < @length
|
|
129
|
+
if @statement[@index] == "\\"
|
|
130
|
+
@index += 2
|
|
131
|
+
elsif @statement[@index] == '"'
|
|
132
|
+
@index += 1
|
|
133
|
+
break
|
|
134
|
+
else
|
|
135
|
+
@index += 1
|
|
136
|
+
end
|
|
137
|
+
end
|
|
138
|
+
true
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
def consume_dollar_quote
|
|
142
|
+
return false unless @statement[@index] == "$"
|
|
143
|
+
|
|
144
|
+
tag = @statement[@index..].match(DOLLAR_TAG)
|
|
145
|
+
return false unless tag
|
|
146
|
+
|
|
147
|
+
close_at = @statement.index(tag[0], @index + tag[0].length)
|
|
148
|
+
@index = close_at ? close_at + tag[0].length : @length
|
|
149
|
+
true
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
def starts_with?(token)
|
|
153
|
+
@statement[@index, token.length] == token
|
|
154
|
+
end
|
|
155
|
+
end
|
|
156
|
+
private_constant :Scan
|
|
157
|
+
|
|
158
|
+
# A trailing LIMIT / OFFSET that is real SQL, not a limit hidden by `--`.
|
|
159
|
+
class TrailingClause
|
|
160
|
+
PATTERN = %r{
|
|
161
|
+
\sLIMIT\s+
|
|
162
|
+
(?:
|
|
163
|
+
\d+\s*,\s*(?<mysql_count>\d+)
|
|
164
|
+
|
|
|
165
|
+
(?<count>\d+)(?:\s+OFFSET\s+\d+)?
|
|
166
|
+
)
|
|
167
|
+
(?:
|
|
168
|
+
\s*/\*.*?\*/
|
|
169
|
+
|
|
|
170
|
+
\s*--[^\n]*
|
|
171
|
+
)?
|
|
172
|
+
\s*\z
|
|
173
|
+
}ix
|
|
174
|
+
private_constant :PATTERN
|
|
175
|
+
|
|
176
|
+
def self.find(statement, scan)
|
|
177
|
+
match = statement.match(PATTERN)
|
|
178
|
+
return unless match
|
|
179
|
+
return if scan.inside_line_comment?(match.begin(0))
|
|
180
|
+
|
|
181
|
+
new(statement, match)
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
def initialize(statement, match)
|
|
185
|
+
@statement = statement
|
|
186
|
+
@match = match
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
def exceeds?(limit)
|
|
190
|
+
count > limit
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
def rewrite(limit)
|
|
194
|
+
start_at = @match.begin(count_key)
|
|
195
|
+
"#{@statement[0...start_at]}#{limit}#{@statement[@match.end(count_key)..]}"
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
private
|
|
199
|
+
|
|
200
|
+
def count
|
|
201
|
+
@match[count_key].to_i
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
def count_key
|
|
205
|
+
@match[:mysql_count] ? :mysql_count : :count
|
|
206
|
+
end
|
|
207
|
+
end
|
|
208
|
+
private_constant :TrailingClause
|
|
209
|
+
end
|
|
210
|
+
end
|
|
211
|
+
end
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Nquery
|
|
4
|
+
# Turns a dashboard parameter string into a value that can be bound into SQL.
|
|
5
|
+
class ParameterValue
|
|
6
|
+
TYPES = %w[string integer date datetime uuid boolean].freeze
|
|
7
|
+
TYPE_LABELS = {
|
|
8
|
+
"string" => "String",
|
|
9
|
+
"integer" => "Integer",
|
|
10
|
+
"date" => "Date",
|
|
11
|
+
"datetime" => "Datetime",
|
|
12
|
+
"uuid" => "UUID",
|
|
13
|
+
"boolean" => "Boolean"
|
|
14
|
+
}.freeze
|
|
15
|
+
TRUE_VALUES = %w[true 1 yes].freeze
|
|
16
|
+
FALSE_VALUES = %w[false 0 no].freeze
|
|
17
|
+
ISO_DATE = /\A\d{4}-\d{2}-\d{2}\z/
|
|
18
|
+
INTEGER = /\A-?\d+\z/
|
|
19
|
+
DATETIME = /\A\d{4}-\d{2}-\d{2}[T ]\d{2}:\d{2}(:\d{2})?(Z|[+-]\d{2}:?\d{2})?\z/
|
|
20
|
+
UUID = /\A[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\z/i
|
|
21
|
+
|
|
22
|
+
def self.type_options
|
|
23
|
+
TYPE_LABELS.map { |value, label| [label, value] }
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def self.valid_default?(type, value)
|
|
27
|
+
parse(type, value) != :invalid
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def self.boolean_form_value(value)
|
|
31
|
+
case parse("boolean", value)
|
|
32
|
+
when true then "true"
|
|
33
|
+
when false then "false"
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def self.parse(type, value)
|
|
38
|
+
text = value.to_s.strip
|
|
39
|
+
return :omitted if text.blank?
|
|
40
|
+
|
|
41
|
+
case type.to_s
|
|
42
|
+
when "string" then text
|
|
43
|
+
when "integer" then parse_integer(text)
|
|
44
|
+
when "date" then parse_date(text)
|
|
45
|
+
when "datetime" then parse_datetime(text)
|
|
46
|
+
when "uuid" then parse_uuid(text)
|
|
47
|
+
when "boolean" then parse_boolean(text)
|
|
48
|
+
else :invalid
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def self.parse_integer(text)
|
|
53
|
+
return :invalid unless text.match?(INTEGER)
|
|
54
|
+
|
|
55
|
+
Integer(text, 10)
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def self.parse_date(text)
|
|
59
|
+
return :invalid unless text.match?(ISO_DATE)
|
|
60
|
+
|
|
61
|
+
Date.iso8601(text)
|
|
62
|
+
rescue ArgumentError
|
|
63
|
+
:invalid
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def self.parse_datetime(text)
|
|
67
|
+
return :invalid unless text.match?(DATETIME)
|
|
68
|
+
|
|
69
|
+
date_text, time_text = text.split(/[T ]/, 2)
|
|
70
|
+
Date.iso8601(date_text)
|
|
71
|
+
normalized = "#{date_text}T#{time_text}"
|
|
72
|
+
if normalized.match?(/Z\z|[+-]\d{2}:?\d{2}\z/)
|
|
73
|
+
Time.iso8601(normalized)
|
|
74
|
+
else
|
|
75
|
+
Time.zone.parse(normalized) || :invalid
|
|
76
|
+
end
|
|
77
|
+
rescue ArgumentError, TypeError
|
|
78
|
+
:invalid
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def self.parse_uuid(text)
|
|
82
|
+
return :invalid unless text.match?(UUID)
|
|
83
|
+
|
|
84
|
+
text.downcase
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def self.parse_boolean(text)
|
|
88
|
+
normalized = text.downcase
|
|
89
|
+
return true if TRUE_VALUES.include?(normalized)
|
|
90
|
+
return false if FALSE_VALUES.include?(normalized)
|
|
91
|
+
|
|
92
|
+
:invalid
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
private_class_method :parse_integer, :parse_date, :parse_datetime, :parse_uuid, :parse_boolean
|
|
96
|
+
end
|
|
97
|
+
end
|
data/lib/nquery/query_runner.rb
CHANGED
|
@@ -4,28 +4,32 @@ module Nquery
|
|
|
4
4
|
class QueryRunner
|
|
5
5
|
class Error < StandardError; end
|
|
6
6
|
class PermissionError < Error; end
|
|
7
|
+
class ParameterError < Error; end
|
|
7
8
|
|
|
8
|
-
def initialize(data_source:, statement:, user: nil, query: nil)
|
|
9
|
+
def initialize(data_source:, statement:, user: nil, query: nil, parameters: nil)
|
|
9
10
|
@data_source = data_source
|
|
10
11
|
@statement = statement.to_s.strip
|
|
11
12
|
@user = user
|
|
12
13
|
@query = query
|
|
14
|
+
@parameters = parameters
|
|
13
15
|
end
|
|
14
16
|
|
|
15
17
|
def run(audit: true)
|
|
16
18
|
validate_statement!
|
|
17
19
|
check_permissions!
|
|
20
|
+
sql, binds = prepared_statement
|
|
18
21
|
|
|
19
22
|
adapter = DataSources::Adapter.for(@data_source)
|
|
20
23
|
result = adapter.execute_readonly(
|
|
21
|
-
|
|
24
|
+
sql,
|
|
22
25
|
timeout: Nquery.configuration.query_timeout,
|
|
23
|
-
row_limit: Nquery.configuration.query_row_limit
|
|
26
|
+
row_limit: Nquery.configuration.query_row_limit,
|
|
27
|
+
binds: binds
|
|
24
28
|
)
|
|
25
29
|
|
|
26
30
|
record_audit!(result, "success") if audit
|
|
27
31
|
result
|
|
28
|
-
rescue PermissionError
|
|
32
|
+
rescue PermissionError, ParameterError
|
|
29
33
|
raise
|
|
30
34
|
rescue StandardError => e
|
|
31
35
|
record_audit!({}, "error", e.message) if audit
|
|
@@ -39,6 +43,35 @@ module Nquery
|
|
|
39
43
|
raise Error, message if message
|
|
40
44
|
end
|
|
41
45
|
|
|
46
|
+
def prepared_statement
|
|
47
|
+
return [@statement, []] unless @statement.include?("{{")
|
|
48
|
+
|
|
49
|
+
bound = StatementBinds.call(
|
|
50
|
+
statement: @statement,
|
|
51
|
+
parameters: parameter_values,
|
|
52
|
+
invalid_names: invalid_parameter_names,
|
|
53
|
+
adapter: @data_source.adapter
|
|
54
|
+
)
|
|
55
|
+
[bound.sql, bound.binds]
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def parameter_values
|
|
59
|
+
case @parameters
|
|
60
|
+
when DashboardParameters::Result
|
|
61
|
+
@parameters.values
|
|
62
|
+
when Hash
|
|
63
|
+
@parameters
|
|
64
|
+
else
|
|
65
|
+
{}
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def invalid_parameter_names
|
|
70
|
+
return @parameters.invalid_names if @parameters.is_a?(DashboardParameters::Result)
|
|
71
|
+
|
|
72
|
+
[]
|
|
73
|
+
end
|
|
74
|
+
|
|
42
75
|
def check_permissions!
|
|
43
76
|
return unless @user
|
|
44
77
|
|
data/lib/nquery/seeder.rb
CHANGED
|
@@ -40,12 +40,19 @@ module Nquery
|
|
|
40
40
|
ROUND(SUM(#{sample_tables[:order_items]}.quantity * #{sample_tables[:order_items]}.unit_price), 2) AS revenue
|
|
41
41
|
FROM #{sample_tables[:orders]}
|
|
42
42
|
INNER JOIN #{sample_tables[:order_items]} ON #{sample_tables[:order_items]}.order_id = #{sample_tables[:orders]}.id
|
|
43
|
+
WHERE 1 = 1
|
|
44
|
+
[[AND date(#{sample_tables[:orders]}.ordered_at) >= {{start_date}}]]
|
|
45
|
+
[[AND date(#{sample_tables[:orders]}.ordered_at) <= {{end_date}}]]
|
|
43
46
|
GROUP BY strftime('%Y-%m', #{sample_tables[:orders]}.ordered_at)
|
|
44
47
|
ORDER BY month
|
|
45
48
|
SQL
|
|
46
49
|
data_source: data_source,
|
|
47
50
|
creator: admin,
|
|
48
|
-
collection: root_collection
|
|
51
|
+
collection: root_collection,
|
|
52
|
+
parameters: [
|
|
53
|
+
{ "name" => "start_date", "type" => "date", "default" => "" },
|
|
54
|
+
{ "name" => "end_date", "type" => "date", "default" => "" }
|
|
55
|
+
]
|
|
49
56
|
)
|
|
50
57
|
query.save!
|
|
51
58
|
|
|
@@ -55,6 +62,7 @@ module Nquery
|
|
|
55
62
|
c.creator = admin
|
|
56
63
|
c.visualization = { "type" => "bar", "x" => "month", "y" => "revenue" }
|
|
57
64
|
end
|
|
65
|
+
chart.update!(query: query) if chart.query_id != query.id
|
|
58
66
|
|
|
59
67
|
dashboard = Dashboard.find_or_create_by!(name: "Executive overview") do |d|
|
|
60
68
|
d.description = "Key metrics at a glance"
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Nquery
|
|
4
|
+
# Replaces {{name}} placeholders with adapter bind markers.
|
|
5
|
+
class StatementBinds
|
|
6
|
+
PLACEHOLDER = /\{\{\s*([a-z][a-z0-9_]*)\s*\}\}/
|
|
7
|
+
OPTIONAL_CLAUSE = /\[\[(.*?)\]\]/m
|
|
8
|
+
Result = Data.define(:sql, :binds)
|
|
9
|
+
|
|
10
|
+
def self.call(statement:, parameters:, adapter:, invalid_names: [])
|
|
11
|
+
new(statement:, parameters:, adapter:, invalid_names:).call
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def self.missing_names(statement:, parameters:, invalid_names: [])
|
|
15
|
+
new(statement:, parameters:, adapter: nil, invalid_names:).missing_names
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def initialize(statement:, parameters:, adapter:, invalid_names: [])
|
|
19
|
+
@statement = statement.to_s
|
|
20
|
+
@parameters = parameters
|
|
21
|
+
@adapter = adapter.to_s
|
|
22
|
+
@invalid_names = Array(invalid_names).map(&:to_s)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def call
|
|
26
|
+
binds = []
|
|
27
|
+
sql = without_empty_clauses.gsub(PLACEHOLDER) do
|
|
28
|
+
binds << attribute_for(Regexp.last_match(1))
|
|
29
|
+
marker_for(binds.size)
|
|
30
|
+
end
|
|
31
|
+
raise QueryRunner::ParameterError, "Invalid parameter placeholder" if sql.include?("{{")
|
|
32
|
+
|
|
33
|
+
Result.new(sql: sql, binds: binds)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def missing_names
|
|
37
|
+
without_empty_clauses.scan(PLACEHOLDER).flatten.uniq.reject { |name| supplied?(name) || @invalid_names.include?(name) }
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
private
|
|
41
|
+
|
|
42
|
+
def without_empty_clauses
|
|
43
|
+
@statement.gsub(OPTIONAL_CLAUSE) do
|
|
44
|
+
inner = Regexp.last_match(1)
|
|
45
|
+
names = inner.scan(PLACEHOLDER).flatten
|
|
46
|
+
if names.any? { |name| @invalid_names.include?(name) }
|
|
47
|
+
inner
|
|
48
|
+
elsif names.empty? || names.any? { |name| !supplied?(name) }
|
|
49
|
+
""
|
|
50
|
+
else
|
|
51
|
+
inner
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def supplied?(name)
|
|
57
|
+
return false unless values.key?(name)
|
|
58
|
+
|
|
59
|
+
value = values[name]
|
|
60
|
+
!(value.nil? || (value.is_a?(String) && value.strip.empty?))
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def attribute_for(name)
|
|
64
|
+
if @invalid_names.include?(name)
|
|
65
|
+
raise QueryRunner::ParameterError, "Invalid value for #{name}"
|
|
66
|
+
end
|
|
67
|
+
unless values.key?(name)
|
|
68
|
+
raise QueryRunner::ParameterError, "Unknown parameter #{name}"
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
value = values[name]
|
|
72
|
+
if value.nil? || value.is_a?(String) && value.strip.empty?
|
|
73
|
+
raise QueryRunner::ParameterError, "Missing value for #{name}"
|
|
74
|
+
end
|
|
75
|
+
ActiveRecord::Relation::QueryAttribute.new(name, value, bind_type_for(name, value))
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def bind_type_for(name, value)
|
|
79
|
+
case value
|
|
80
|
+
when String then ActiveRecord::Type::String.new
|
|
81
|
+
when Integer then ActiveRecord::Type::Integer.new
|
|
82
|
+
when DateTime, Time then ActiveRecord::Type::DateTime.new
|
|
83
|
+
when Date then ActiveRecord::Type::Date.new
|
|
84
|
+
when TrueClass, FalseClass then ActiveRecord::Type::Boolean.new
|
|
85
|
+
else
|
|
86
|
+
raise QueryRunner::ParameterError, "Invalid value for #{name}"
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def values
|
|
91
|
+
@values ||= case @parameters
|
|
92
|
+
when Hash
|
|
93
|
+
@parameters.stringify_keys
|
|
94
|
+
else
|
|
95
|
+
{}
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def marker_for(position)
|
|
100
|
+
return "$#{position}" if postgres_markers?
|
|
101
|
+
|
|
102
|
+
"?"
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def postgres_markers?
|
|
106
|
+
return true if @adapter == "postgresql"
|
|
107
|
+
return false unless @adapter == "rails"
|
|
108
|
+
|
|
109
|
+
ActiveRecord::Base.connection.adapter_name.downcase.include?("postgres")
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
end
|
data/lib/nquery/version.rb
CHANGED
data/lib/nquery.rb
CHANGED
|
@@ -11,12 +11,16 @@ require "nquery/encryption"
|
|
|
11
11
|
require "nquery/permissions/resolver"
|
|
12
12
|
require "nquery/authorizes_collection"
|
|
13
13
|
require "nquery/data_sources/adapter"
|
|
14
|
+
require "nquery/data_sources/row_limit"
|
|
14
15
|
require "nquery/data_sources/rails_adapter"
|
|
15
16
|
require "nquery/data_sources/postgresql_adapter"
|
|
16
17
|
require "nquery/data_sources/mysql_adapter"
|
|
17
18
|
require "nquery/data_sources/sqlite_adapter"
|
|
18
19
|
require "nquery/data_sources/syncer"
|
|
19
20
|
require "nquery/readonly_sql"
|
|
21
|
+
require "nquery/statement_binds"
|
|
22
|
+
require "nquery/dashboard_parameters"
|
|
23
|
+
require "nquery/parameter_value"
|
|
20
24
|
require "nquery/query_runner"
|
|
21
25
|
require "nquery/schema_explorer"
|
|
22
26
|
require "nquery/embed_token_service"
|
data/nquery.gemspec
CHANGED
|
@@ -35,9 +35,9 @@ Gem::Specification.new do |spec|
|
|
|
35
35
|
|
|
36
36
|
spec.add_development_dependency "rspec-rails", "~> 7.0"
|
|
37
37
|
spec.add_development_dependency "capybara", "~> 3.40"
|
|
38
|
-
spec.add_development_dependency "sqlite3", ">= 2.
|
|
38
|
+
spec.add_development_dependency "sqlite3", ">= 2.10.2", "< 3"
|
|
39
39
|
spec.add_development_dependency "pg", "~> 1.5"
|
|
40
|
-
spec.add_development_dependency "puma", ">= 7.2.
|
|
40
|
+
spec.add_development_dependency "puma", ">= 7.2.2", "< 8"
|
|
41
41
|
spec.add_development_dependency "simplecov", "~> 0.22"
|
|
42
42
|
spec.add_development_dependency "rubocop", "~> 1.75"
|
|
43
43
|
spec.add_development_dependency "rubocop-rails-omakase", "~> 1.1"
|