Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HBW-80 Limit on the number of choices in lookup select #194

Merged
merged 1 commit into from
Nov 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ db/*.sqlite3
/public/assets/*
/spec/tmp/*
/cache
/.cache
/capybara*
/capybara-*.html
/gems
Expand Down Expand Up @@ -107,3 +108,4 @@ lib/capistrano/
/public/assets/packs-test
/node_modules
yarn-debug.log*
yarn-error.log*
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ v1.6.0 [unreleased]
- [#189](https://github.com/latera/homs/pull/189) Show alert if form with file upload field has no file list field.
- [#197](https://github.com/latera/homs/pull/197) Use Camunda as BPMS in default docker-compose.

### Bugfixes
- [#194](https://github.com/latera/homs/pull/194) Set limit on the number of choices in lookup select from sql requests.

v1.5.0 [2018-10-29]
-------------------

Expand Down
8 changes: 5 additions & 3 deletions hbw/app/models/hbw/fields/select.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class Select < Ordinary
definition_reader :variable
definition_reader :entity_class

MAX_ROW_COUNT = 20

class SourceLoader
include HBW::Logger

Expand All @@ -21,8 +23,8 @@ def initialize(name, source, condition, variables)
@variables = variables
end

def load
values = source.select(condition, variables)
def load(limit = nil)
values = source.select(condition, variables, limit)

logger.debug { "Retrieved values for %s\nfrom source %s\ncondition: %s\nvariables: %s.\nResult: %s" %
[name, source, condition, variables, values.to_s] }
Expand Down Expand Up @@ -110,7 +112,7 @@ def nullable?
end

def lookup_values(query)
choices_to_array(loader(lookup_sql, task.variables_hash.merge(value: query)).load.uniq)
choices_to_array(loader(lookup_sql, task.variables_hash.merge(value: query)).load(MAX_ROW_COUNT).uniq)
end

private
Expand Down
2 changes: 1 addition & 1 deletion hbw/app/models/hbw/sources/activiti.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module HBW
module Sources
class Activiti < Base
def select(variable_name, variables)
def select(variable_name, variables, _ = nil)
value = variables.fetch(variable_name.to_sym)

if value.present?
Expand Down
17 changes: 12 additions & 5 deletions hbw/app/models/hbw/sources/oracle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@ class Oracle < Base
class ConnectionFailed < RuntimeError
end


def initialize(*)
require 'oci8' unless defined?(OCI8)

super
end

def select(sql, variables)
def select(sql, variables, limit = nil)
binds = parse_bind_names(sql)

execute(sql) do |cursor|
execute(sql, limit) do |cursor|
binds.each do |bind|
if variables.fetch(bind).nil?
cursor.bind_param(bind, nil, Integer)
Expand All @@ -31,11 +30,15 @@ def parse_bind_names(sql)
sql.scan(/:\w+/).map { |s| s[1..-1].to_sym }
end

def execute(sql)
def execute(sql, limit = nil)
2.times do
begin
establish_connection
return try_query(sql) { |c| yield(c) if block_given? }
if limit.nil?
return try_query(sql) { |c| yield(c) if block_given? }
else
return try_query(limit(sql, limit)) { |c| yield(c) if block_given? }
end
rescue ConnectionFailed
reconnect
end
Expand Down Expand Up @@ -85,6 +88,10 @@ def reconnect
@connection = nil
establish_connection
end

def limit(sql, max_row_count)
"SELECT * FROM (#{sql}) WHERE ROWNUM <= #{max_row_count}"
end
end
end
end
2 changes: 1 addition & 1 deletion hbw/app/models/hbw/sources/yml_oracle.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module HBW
module Sources
class YMLOracle < Base
def select(sql, variables)
def select(sql, variables, limit = nil)
find_response(build_params_key(sql, variables))
end

Expand Down