module Clear::SQL::Query::Pluck

Direct including types

Defined in:

clear/sql/query/pluck.cr

Instance Method Summary

Instance Method Detail

def pluck(fields : Tuple(*T)) forall T #

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

[View source]
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

[View source]
def pluck(**fields : **T) forall T #

Select specifics columns and returns on array of tuple of type of the named tuple passed as parameter:

User.query.pluck(id: Int64, "UPPER(last_name)": String).each do #...

[View source]
def pluck_col(field : Clear::SQL::Symbolic, type : T.class) forall T #

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

[View source]
def pluck_col(field : Clear::SQL::Symbolic) #

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

[View source]