Module: Ensql::Adapter Abstract

Included in:
ActiveRecordAdapter, PostgresAdapter, SequelAdapter
Defined in:
lib/ensql/adapter.rb

Overview

This module is abstract.

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

2. Predefined Methods collapse

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.

Returns:

  • (Integer)

    the number of rows affected by the statement



# 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.

Yields:

  • (Hash)

    row



# File 'lib/ensql/adapter.rb', line 84

#fetch_first_column(sql) ⇒ Array

Execute the query and return only the first column of the result.

Returns:

  • (Array)


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.

Returns:

  • (Hash)


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.

Returns:

  • (Array<Hash>)

    rows as hashes keyed by column name



# 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.

Examples:

literalize("It's quoted") # => "'It''s quoted'"
literalize(1.23) # => "1.23"
literalize(true) # => "1"
literalize(nil) # => "NULL"
literalize(Time.now) # => "'2021-02-22 23:44:28.942947+1300'"

Returns:

  • (String)

    a properly quoted SQL literal

See Also:



# 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