Module: Ensql::Adapter Abstract
- Included in:
- ActiveRecordAdapter, PostgresAdapter, SequelAdapter
- Defined in:
- lib/ensql/adapter.rb
Overview
Do not use this module directly.
A common interface for executing SQL statements and retrieving (or not) their results. Some methods have predefined implementations for convenience that can be improved in the adapters.
1. Interface Methods collapse
-
#fetch_count(sql) ⇒ Integer
Execute the statement and return the number of rows affected.
-
#fetch_each_row(sql) {|Hash| ... } ⇒ Object
Execute the query and yield each resulting row.
-
#fetch_rows(sql) ⇒ Array<Hash>
Execute the query and return an array of rows represented by { column => field } hashes.
-
#literalize(value) ⇒ String
Convert a Ruby object into a string that can be safely interpolated into an SQL statement.
-
#run(sql) ⇒ void
Execute the statement on the database without returning any result.
2. Predefined Methods collapse
-
#fetch_first_column(sql) ⇒ Array
Execute the query and return only the first column of the result.
-
#fetch_first_field(sql) ⇒ Object
Execute the query and return only the first field of the first row of the result.
-
#fetch_first_row(sql) ⇒ Hash
Execute the query and return only the first row of the result.
Instance Method Details
#fetch_count(sql) ⇒ Integer
Execute the statement and return the number of rows affected. Typically used for DELETE, UPDATE, INSERT, but will work with SELECT on some databases.
|
# File 'lib/ensql/adapter.rb', line 91
|
#fetch_each_row(sql) {|Hash| ... } ⇒ Object
Execute the query and yield each resulting row. This should provide a more efficient method of iterating through large datasets.
|
# File 'lib/ensql/adapter.rb', line 84
|
#fetch_first_column(sql) ⇒ Array
Execute the query and return only the first column of the result.
116 117 118 |
# File 'lib/ensql/adapter.rb', line 116 def fetch_first_column(sql) fetch_rows(sql).map(&:values).map(&:first) end |
#fetch_first_field(sql) ⇒ Object
Execute the query and return only the first field of the first row of the result.
121 122 123 |
# File 'lib/ensql/adapter.rb', line 121 def fetch_first_field(sql) fetch_first_row(sql)&.values&.first end |
#fetch_first_row(sql) ⇒ Hash
Execute the query and return only the first row of the result.
110 111 112 |
# File 'lib/ensql/adapter.rb', line 110 def fetch_first_row(sql) fetch_each_row(sql).first end |
#fetch_rows(sql) ⇒ Array<Hash>
Execute the query and return an array of rows represented by { column => field } hashes. Fields should be deserialised depending on the column type.
|
# File 'lib/ensql/adapter.rb', line 77
|
#literalize(value) ⇒ String
Convert a Ruby object into a string that can be safely interpolated into an SQL statement. Strings will be correctly quoted. The precise result will depend on the adapter and the underlying database driver, but most RDBMs have limited ways to express literals.
|
# File 'lib/ensql/adapter.rb', line 57
|
#run(sql) ⇒ void
This method returns an undefined value.
Execute the statement on the database without returning any result. This can avoid the overhead of other fetch_* methods.
|
# File 'lib/ensql/adapter.rb', line 99
|