module Clear::SQL::Query::Pluck
Direct including types
Defined in:
clear/sql/query/pluck.crInstance Method Summary
-
#pluck(fields : Tuple(*T)) forall T
See
#pluck(*fields)
-
#pluck(*fields)
Select specifics columns and return an array of Tuple(*Clear::SQL::Any) containing the columns in the order of the selected arguments:
- #pluck(**fields : **T) forall T
- #pluck_col(field : String, type : T.class) forall T
-
#pluck_col(field : String)
Select a specific column of your SQL query, execute the query and return an array containing this field.
Instance Method Detail
def pluck(*fields)
#
Select specifics columns and return an array of Tuple(*Clear::SQL::Any) containing the columns in the order of the selected arguments:
User.query.pluck("first_name", "last_name").each do |(first_name, last_name)|
# ...
end
def pluck_col(field : String)
#
Select a specific column of your SQL query, execute the query and return an array containing this field.
User.query.pluck_col("id") # [1,2,3,4...]
Note: It returns an array of Clear::SQL::Any
. Therefore, you may want to use #pluck_col(str, Type)
to return
an array of Type
:
User.query.pluck_col("id", Int64)
The field argument is a SQL fragment; it's not escaped (beware SQL injection) and allow call to functions and aggregate methods:
# ...
User.query.pluck_col("CASE WHEN id % 2 = 0 THEN id ELSE NULL END AS id").each do
# ...