Cassandrax.Query behaviour (Cassandrax v0.4.0) View Source

Provides the query macros.

Queries are used to retrieve or manipulate data from a repository (see Cassandrax.Keyspace).

Link to this section Summary

Callbacks

A query expression that enables filtering in certain Cassandra queries.

A distinct query expression.

A group by query expression.

A limit query expression.

An order by query expression.

A per partition limit expression controls the number of results return from each partition.

A select query expression.

A where query expression that works like an AND operator.

Link to this section Types

Specs

t() :: %Cassandrax.Query{
  allow_filtering: term(),
  distinct: term(),
  from: term(),
  group_bys: term(),
  limit: term(),
  order_bys: term(),
  per_partition_limit: term(),
  schema: term(),
  select: term(),
  wheres: term()
}

Link to this section Functions

Link to this macro

allow_filtering(queryable)

View Source (macro)
Link to this macro

distinct(queryable, distinct \\ [])

View Source (macro)
Link to this macro

group_by(queryable, group_by \\ [])

View Source (macro)
Link to this macro

limit(queryable, limit \\ 100)

View Source (macro)
Link to this macro

order_by(queryable, order_by \\ [])

View Source (macro)
Link to this macro

per_partition_limit(queryable, per_partition_limit \\ 100)

View Source (macro)
Link to this macro

select(queryable, select \\ [])

View Source (macro)
Link to this macro

where(queryable, where \\ [])

View Source (macro)

Link to this section Callbacks

Link to this callback

allow_filtering(queryable, allow_filtering)

View Source

Specs

allow_filtering(
  queryable :: Cassandrax.Queryable.t(),
  allow_filtering :: Keyword.t()
) :: t()

A query expression that enables filtering in certain Cassandra queries.

CassandraDB doesn't allow certain queries to be executed for performance reasons, such as where. You need to set ALLOW FILTERING to bypass this block. More details in CassandraDB docs.

Example

query = User |> allow_filtering() |> where(:id > 1) |> where(:user_name != "alice")
Link to this callback

distinct(queryable, distinct)

View Source

Specs

distinct(queryable :: Cassandrax.Queryable.t(), distinct :: Keyword.t()) :: t()

A distinct query expression.

Only returns the distinct records from the result. Only works with a list of partition_key(s).

##Example

query = distinct(TestSchema, [:id])
Link to this callback

group_by(queryable, order_by)

View Source

Specs

group_by(queryable :: Cassandrax.Queryable.t(), order_by :: Keyword.t()) :: t()

A group by query expression.

Allows to condense into a single row all selected rows that share the same values for a set of columns. Only available for partition key level or at a clustering column level.

Example

query = User |> allow_filtering() |> group_by([:id])

Specs

limit(queryable :: Cassandrax.Queryable.t(), limit :: Keyword.t()) :: t()

A limit query expression.

Limits the number of rows to be returned from the result. Requires an integer, fields cannot be included. Default limit is 100.

Limit expressions are chainable, however, the last limit expression will take precedence.

Example

query = limit(User, 200)
Link to this callback

order_by(queryable, order_by)

View Source

Specs

order_by(queryable :: Cassandrax.Queryable.t(), order_by :: Keyword.t()) :: t()

An order by query expression.

Orders the fields based on a given key or list of keys. Order by needs to be paired with a where clause, specifically with where clauses that have equality or in. You also need to setup the table correctly to be able to perform order by queries.

Example Table Setup

statement = [
    "CREATE TABLE IF NOT EXISTS ",
    "MyKeyspace.ordered_(",
    "id int, ",
    "device_id int, ",
    "value text, ",
    "PRIMARY KEY (id, device_id))",
    "WITH CLUSTERING ORDER BY (device_id DESC)"
  ]

Cassandrax.cql(MyConn, statement)

Example

query = User |> allow_filtering() |> where(:id == 1) |> order_by([:device_id])

You can set an explicit order.

query = User |> allow_filtering() |> where(:id == 1) |> order_by([asc: :device_id])
Link to this callback

per_partition_limit( queryable, per_partition_limit )

View Source

Specs

per_partition_limit(
  queryable :: Cassandrax.Queryable.t(),
  per_partition_limit :: integer()
) :: t()

A per partition limit expression controls the number of results return from each partition.

Cassandra will then return only the first number of rows given in the per_partition_limit (clustered by the partition key) from that partition, regardless of how many ocurences of when may be present. More details in CassandraDB docs.

Example

Default per_partition_limit is 100.

query = per_partition_limit(User)

Or you can set a custom per_partition_limit

query = per_partition_limit(User, 10)
Link to this callback

select(queryable, select)

View Source

Specs

select(queryable :: Cassandrax.Queryable.t(), select :: Keyword.t()) :: t()

A select query expression.

Selects the fields from the schema and any transformations that should be performed on the fields. Any expression that is accepted in a query can be a select field.

Allows a list, tuple or a map. A full schema can also be selected. Only one select expression allowed in a query. If there is no select expression, the full schema will be selected by default.

Accepts a list of atoms where atoms refer to fields.

Example

query = select(User, [:id])
%Cassandrax.Query{from: "users", schema: Cassandrax.User, select: [:id]} = query

Specs

where(queryable :: Cassandrax.Queryable.t(), where :: Keyword.t()) :: t()

A where query expression that works like an AND operator.

Used to filter the results. You can chain where expressions.

Example

Single where clause.

query = where(User, id: 1)

You can chain where clauses.

query = User |> where(:id > 1) |> where(:user_name != "alice")

CassandraDB doesn't allow certain queries to be executed for performance reasons, such as where. You may need to use allow_filtering to bypass this.

query = User |> allow_filtering() |> where(:id > 1) |> where(:user_name != "alice")