Class: Simple::SQL::Scope
- Inherits:
-
Object
- Object
- Simple::SQL::Scope
- Defined in:
- lib/simple/sql/scope.rb,
lib/simple/sql/scope/order.rb,
lib/simple/sql/scope/filters.rb,
lib/simple/sql/scope/pagination.rb
Overview
rubocop:disable Style/Not
Defined Under Namespace
Modules: PageInfo
Constant Summary collapse
- SELF =
self
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#page ⇒ Object
readonly
Returns the value of attribute page.
-
#per ⇒ Object
readonly
Returns the value of attribute per.
Instance Method Summary collapse
-
#initialize(sql) ⇒ Scope
constructor
Build a scope object.
- #order_by(sql_fragment) ⇒ Object
-
#paginate(per:, page:) ⇒ Object
Set pagination.
-
#paginated? ⇒ Boolean
Is this a paginated scope?.
-
#to_sql(pagination: :auto) ⇒ Object
generate a sql query.
-
#where(sql_fragment, arg = :__dummy__no__arg, placeholder: "?", jsonb: true) ⇒ Object
scope = Scope.new("SELECT * FROM tablename") scope = scope.where(id: 12) scope = scope.where("id > ?", 12).
Constructor Details
#initialize(sql) ⇒ Scope
Build a scope object
17 18 19 20 21 22 |
# File 'lib/simple/sql/scope.rb', line 17 def initialize(sql) @sql = sql @args = [] @filters = [] @order_by = nil end |
Instance Attribute Details
#args ⇒ Object (readonly)
Returns the value of attribute args.
13 14 15 |
# File 'lib/simple/sql/scope.rb', line 13 def args @args end |
#page ⇒ Object (readonly)
Returns the value of attribute page.
14 15 16 |
# File 'lib/simple/sql/scope.rb', line 14 def page @page end |
#per ⇒ Object (readonly)
Returns the value of attribute per.
14 15 16 |
# File 'lib/simple/sql/scope.rb', line 14 def per @per end |
Instance Method Details
#order_by(sql_fragment) ⇒ Object
4 5 6 |
# File 'lib/simple/sql/scope/order.rb', line 4 def order_by(sql_fragment) duplicate.send(:order_by!, sql_fragment) end |
#paginate(per:, page:) ⇒ Object
Set pagination
5 6 7 |
# File 'lib/simple/sql/scope/pagination.rb', line 5 def paginate(per:, page:) duplicate.send(:paginate!, per: per, page: page) end |
#paginated? ⇒ Boolean
Is this a paginated scope?
10 11 12 |
# File 'lib/simple/sql/scope/pagination.rb', line 10 def paginated? not @per.nil? end |
#to_sql(pagination: :auto) ⇒ Object
generate a sql query
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/simple/sql/scope.rb', line 39 def to_sql(pagination: :auto) raise ArgumentError unless pagination == :auto || pagination == false sql = @sql sql = apply_filters(sql) sql = apply_order(sql) sql = apply_pagination(sql, pagination: pagination) sql end |
#where(sql_fragment, arg = :__dummy__no__arg, placeholder: "?", jsonb: true) ⇒ Object
scope = Scope.new("SELECT * FROM tablename") scope = scope.where(id: 12) scope = scope.where("id > ?", 12)
In the second form the placeholder (usually a '?') is being replaced with the numbered argument (since postgres is using $1, $2, etc.) If your SQL fragment uses '?' as part of some fixed text you must use an alternative placeholder symbol:
scope = scope.where("foo | '?' = '^'", match, placeholder: '^')
If a hash is passed in as a search condition and the value to match is a hash, this is translated into a JSONB query, which matches each of the passed in keys against one of the passed in values.
scope = scope.where(metadata: { uid: 1, type: ["foo", "bar", "baz"] })
This feature can be disabled using the jsonb: false option.
scope = scope.where(metadata: { uid: 1 }, jsonb: false)
26 27 28 |
# File 'lib/simple/sql/scope/filters.rb', line 26 def where(sql_fragment, arg = :__dummy__no__arg, placeholder: "?", jsonb: true) duplicate.send(:where!, sql_fragment, arg, placeholder: placeholder, jsonb: jsonb) end |