Module: Ensql::Adapter Abstract
- Included in:
- ActiveRecordAdapter, 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 51
|
#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 44
|
#fetch_first_column(sql) ⇒ Array
Execute the query and return only the first column of the result.
77 78 79 |
# File 'lib/ensql/adapter.rb', line 77 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.
82 83 84 |
# File 'lib/ensql/adapter.rb', line 82 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.
71 72 73 |
# File 'lib/ensql/adapter.rb', line 71 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 37
|
#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 17
|
#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 59
|