Skip to content

Commit

Permalink
HBW-80 Limit on the number of choices in lookup select
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxim Smirnov committed Oct 31, 2018
1 parent 1253264 commit 2552088
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
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*
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ v1.6.0 [unreleased]

### Features
- [#189](https://github.com/latera/homs/pull/189) Show alert if form with file upload field has no file list field.
- [#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
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

0 comments on commit 2552088

Please sign in to comment.