Class: DB::Query
- Inherits:
-
Object
- Object
- DB::Query
- Defined in:
- lib/db/query.rb
Overview
A mutable query builder.
Instance Method Summary collapse
-
#call ⇒ Object
Send the query to the remote server to be executed.
-
#clause(value) ⇒ Object
Append a raw textual clause to the query buffer.
-
#identifier(value) ⇒ Object
Append an identifier value to the query buffer.
-
#initialize(session, buffer = String.new) ⇒ Query
constructor
Create a new query builder attached to the specified session.
- #inspect ⇒ Object
-
#interpolate(fragment, **parameters) ⇒ Object
Interpolate a query fragment with the specified parameters.
- #key_column(*arguments, **options) ⇒ Object
-
#literal(value) ⇒ Object
Append a literal value to the query buffer.
-
#send ⇒ Object
Send the query to the remote server to be executed.
- #to_s ⇒ Object
Constructor Details
#initialize(session, buffer = String.new) ⇒ Query
Create a new query builder attached to the specified session.
48 49 50 51 52 |
# File 'lib/db/query.rb', line 48 def initialize(session, buffer = String.new) @session = session @connection = session.connection @buffer = +buffer end |
Instance Method Details
#call ⇒ Object
Send the query to the remote server to be executed. See Context::Session#call for more details.
123 124 125 |
# File 'lib/db/query.rb', line 123 def call @session.call(@buffer) end |
#clause(value) ⇒ Object
Append a raw textual clause to the query buffer.
57 58 59 60 61 62 63 |
# File 'lib/db/query.rb', line 57 def clause(value) @buffer << ' ' unless @buffer.end_with?(' ') || @buffer.empty? @buffer << value return self end |
#identifier(value) ⇒ Object
Append an identifier value to the query buffer. Escapes the field according to the requirements of the underlying connection.
81 82 83 84 85 86 87 |
# File 'lib/db/query.rb', line 81 def identifier(value) @buffer << ' ' unless @buffer.end_with?(' ') @connection.append_identifier(value, @buffer) return self end |
#inspect ⇒ Object
131 132 133 |
# File 'lib/db/query.rb', line 131 def inspect "\#<#{self.class} #{@buffer.inspect}>" end |
#interpolate(fragment, **parameters) ⇒ Object
Interpolate a query fragment with the specified parameters. The parameters are escaped before being appended.
95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/db/query.rb', line 95 def interpolate(fragment, **parameters) parameters.transform_values! do |value| case value when Symbol, Identifier @connection.append_identifier(value) else @connection.append_literal(value) end end @buffer << sprintf(fragment, parameters) return self end |
#key_column(*arguments, **options) ⇒ Object
110 111 112 113 114 |
# File 'lib/db/query.rb', line 110 def key_column(*arguments, **) @buffer << @connection.key_column(*arguments, **) return self end |
#literal(value) ⇒ Object
Append a literal value to the query buffer. Escapes the field according to the requirements of the underlying connection.
69 70 71 72 73 74 75 |
# File 'lib/db/query.rb', line 69 def literal(value) @buffer << ' ' unless @buffer.end_with?(' ') @connection.append_literal(value, @buffer) return self end |
#send ⇒ Object
Send the query to the remote server to be executed. See Context::Session#send_query for more details.
117 118 119 |
# File 'lib/db/query.rb', line 117 def send @session.send_query(@buffer) end |
#to_s ⇒ Object
127 128 129 |
# File 'lib/db/query.rb', line 127 def to_s @buffer end |