module Clear::SQL::Query::Fetch

Direct including types

Defined in:

clear/sql/query/fetch.cr

Instance Method Summary

Instance Method Detail

def fetch(fetch_all = false, &block : Hash(String, Clear::SQL::Any) -> Nil) #

Fetch the result set row per row fetch_all optional parameter is helpful in transactional environment, so it stores the result and close the resultset before starting to call yield over the data preventing creation of a new connection if you need to call SQL into the yielded block.

# This is wrong: The connection is still busy retrieving the users:
Clear::SQL.select.from("users").fetch do |u|
  Clear::SQL.select.from("posts").where { u["id"] == posts.id }
end

# Instead, use `fetch_all`
# Clear will store the value of the result set in memory
# before calling the block, and the connection is now ready to handle
# another query.
Clear::SQL.select.from("users").fetch(fetch_all:true) do |u|
  Clear::SQL.select.from("posts").where { u["id"] == posts.id }
end

[View source]
def fetch_first #

Alias for #first because first is redefined in Collection::Base object to return a model instead.


[View source]
def fetch_first! #

[View source]
def fetch_with_cursor(count = 1000, &block : Hash(String, Clear::SQL::Any) -> Nil) #

Fetch the data using CURSOR. This will prevent Clear to load all the data from the database into memory. This is useful if you need to retrieve and update a large dataset.


[View source]
def first #

Return the first line of the query as Hash(String, ::Clear::SQL::Any), or nil if not found


[View source]
def first! #

[View source]
def scalar(type : T.class) forall T #

Helpers to fetch a SELECT with only one row and one column return.


[View source]
def to_a : Array(Hash(String, Clear::SQL::Any)) #

Return an array with all the rows fetched.


[View source]