Module: Ensql
- Defined in:
- lib/ensql.rb,
lib/ensql/sql.rb,
lib/ensql/adapter.rb,
lib/ensql/version.rb,
lib/ensql/sequel_adapter.rb,
lib/ensql/active_record_adapter.rb
Overview
Primary interface for loading, interpolating and executing SQL statements using your preferred database connection. See Ensql.sql for interpolation details.
Defined Under Namespace
Modules: ActiveRecordAdapter, Adapter, SequelAdapter Classes: Error, SQL
Constant Summary collapse
- VERSION =
Gem version
"0.6.1"- SUPPORTED_ACTIVERECORD_VERSIONS =
Versions of activerecord compatible with the ActiveRecordAdapter
['>= 5.0', '< 6.2'].freeze
- SUPPORTED_SEQUEL_VERSIONS =
Versions of sequel compatible with the SequelAdapter
'~> 5.9'
Class Attribute Summary collapse
-
.adapter ⇒ Object
Connection adapter to use.
-
.sql_path ⇒ Object
Path to search for *.sql queries in, defaults to "sql/".
Class Method Summary collapse
-
.load_sql(name, params = {}) ⇒ Ensql::SQL
Load SQL from a file within Ensql.sql_path.
-
.run(sql, params = {}) ⇒ void
Convenience method to interpolate and run the supplied SQL on the current adapter.
-
.sql(sql, params = {}) ⇒ Ensql::SQL
Encapsulates a plain-text SQL statement and optional parameters to interpolate.
Class Attribute Details
.adapter ⇒ Object
Connection adapter to use. Must implement the interface defined in Adapter. If not specified, it will try to autoload an adapter based on the availability of Sequel or ActiveRecord, in that order.
89 90 91 |
# File 'lib/ensql.rb', line 89 def adapter @adapter ||= autoload_adapter end |
.sql_path ⇒ Object
Path to search for *.sql queries in, defaults to "sql/". For example, if
sql_path is set to 'app/queries', load_sql('users/active') will read
'app/queries/users/active.sql'.
46 47 48 |
# File 'lib/ensql.rb', line 46 def sql_path @sql_path ||= 'sql' end |
Class Method Details
.load_sql(name, params = {}) ⇒ Ensql::SQL
62 63 64 65 |
# File 'lib/ensql.rb', line 62 def load_sql(name, params={}) path = File.join(sql_path, "#{name}.sql") SQL.new(File.read(path), params, name) end |
.run(sql, params = {}) ⇒ void
This method returns an undefined value.
Convenience method to interpolate and run the supplied SQL on the current adapter.
75 76 77 |
# File 'lib/ensql.rb', line 75 def run(sql, params={}) SQL.new(sql, params).run end |
.sql(sql, params = {}) ⇒ Ensql::SQL
Encapsulates a plain-text SQL statement and optional parameters to interpolate. Interpolation is indicated by one of the four placeholder formats:
Literal:
%{param}- Interpolates
paramas a quoted string or a numeric literal depending on the class. nilis interpolated as'NULL'.- Other objects depend on the database and the adapter, but most (like
Time) are serialised as a quoted SQL string.
- Interpolates
List Expansion:
%{(param)}- Expands an array to a list of quoted literals.
- Mostly useful for
column IN (1,2)or postgres row literals. - Empty arrays are interpolated as
(NULL)for SQL conformance. - The parameter will be converted to an Array.
Nested List:
%{param(nested sql)}- Takes an array of parameter hashes and interpolates the nested SQL for each Hash in the Array.
- Raises an error if param is nil or a non-hash array.
- Primary useful for SQL
VALUES ()clauses.
SQL Fragment:
%{!sql_param}- Interpolates the parameter without quoting, as a SQL fragment.
- The parameter must be an SQL object or this will raise an error.
nilwill not be interpolated.- Allows composition of SQL via subqueries.
Any placeholders in the SQL must be present in the params hash or a KeyError will be raised during interpolation. Interpolation occurs just before the SQL is executed.
34 35 36 |
# File 'lib/ensql.rb', line 34 def sql(sql, params={}) SQL.new(sql, params) end |