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
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.
- #limit(count) ⇒ 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
This call supports a few variants:
Simple::SQL::Scope.new("SELECT * FROM mytable")
Simple::SQL::Scope.new(table: "mytable", select: "*")
The second option also allows one to pass in more options, like the following:
Simple::SQL::Scope.new(table: "mytable", select: "*", where: { id: 1, foo: "bar" }, order_by: "id desc")
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/simple/sql/scope.rb', line 27 def initialize(sql) expect! sql => [String, Hash] @sql = nil @args = [] @filters = [] case sql when String then @sql = sql when Hash then initialize_from_hash(sql) end 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
#limit(count) ⇒ Object
8 9 10 |
# File 'lib/simple/sql/scope/order.rb', line 8 def limit(count) duplicate.send(:limit!, count) end |
#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
76 77 78 79 80 81 82 83 84 85 |
# File 'lib/simple/sql/scope.rb', line 76 def to_sql(pagination: :auto) raise ArgumentError unless pagination == :auto || pagination == false sql = @sql sql = apply_filters(sql) sql = apply_order_and_limit(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 |