CQL Cheatsheet

Queries are executed with Archeometer.Repo.all/3. The easiest way to load the query macros is by importing the Archoemeter.Query module. The predefined schemas are under the Archeometer.Schema.* name. For brevity, all examples assume you have an alias to those schemas.

import Archeometer.Query
alias Archeometer.Schema.Module

Archeometer.Repo.all(
    from m in Module, 
        where: m.num_lines > 50, 
        select: [m.name, m.app.name])

cql-keywords

CQL Keywords

available-keywords

Available Keywords

  • :select
  • :where
  • :order_by
  • :group_by
  • :having
  • :limit

select

Select

Select all modules name

from m in Module, 
    select: [app: m.name]

Select all applications and the average number of lines per module

from m in Module,
    select: [app: m.app.name,  
    avg_size: avg(m.num_lines)]

where

Where

Select all modules that belongs to Archeometer app

from m in Module, 
    select: [m.name],
    where: m.app.name == "archeometer"

order-by

Order By

Select all modules from project ordered by coverage (ascending)

from m in Module, 
    select: [m.name], 
    order_by: m.coverage

Select all modules from project ordered by coverage (descending)

from m in Module,
    select: [m.name],
    order_by: [desc: m.coverage]

group-by

Group By

Select functions name, coverage, and module name where coverage is less than 70% and grouped by module name

from f in Function, 
    select: [f.name, f.coverage, f.module.name],
    where: f.coverage < 0.7, 
    group_by: f.module.name

having

Having

Select module name and average functions cyclomatic complexity and selects only modules with an average cyclomatic complexity greater than 2

from f in Function,
    select: [module: f.module.name, avg_cc: avg(f.cc)], 
    group_by: f.module.name, having: avg_cc > 2

limit

Limit

Select the 10 functions with the highest cyclomatic complexity

from f in Function, 
    select: [f.name, f.cc], order_by: [desc: f.cc],
    limit: 10

cql-schemas

CQL Schemas

available-schemas

Available Schemas

cql-terms

CQL Terms

available-literals

Available Literals

  • Integers
  • Floating point numbers
  • Bitstrings
  • Booleans
  • Lists

available-functions

Available Functions

  • Aggregation functions: avg, count, max, min, sum
  • Search functions: like
  • Arithmetic functions: round
  • Subquery expressions: exists

available-operators

Available Operators

  • Boolean operators: and, or, not
  • Comparison operators: ==, !=, <, >, <=, >=
  • Arithmetic operators: +, -, *, /
  • Null checking: is_nil, not is_nil
  • in operator

available-term-containers

Available Term Containers

  • Tuples
  • Lists
  • Keyword lists
  • Maps

Containers can not be nested

boolean-operator

Boolean Operator

Select all modules name that are not defining an Ecto schema

from m in Module, 
    select: [module: m.name], 
    where: not m.has_ecto_schema

Select all modules name that are a defining an Ecto schema or are defining a struct

from m in Module, 
    select: [module: m.name], 
    where: m.has_ecto_schema or m.has_struct

comparison-operators

Comparison Operators

Select modules that are used by other modules at least once

from m in Module, 
    select: [name: m.name, num_usages: count(m.in_refs.caller.name)],
    having: num_usages > 0,
    group_by: m.name

arithmetic-operators

Arithmetic Operators

Select all modules and the average cyclomatic complexity of their functions

from m in Module, 
    select: [name: m.name, average_cc: round(sum(m.functions.cc) / count(m.functions.name), 2)], 
    group_by: m.name

aritmetic-functions

Aritmetic Functions

Select the 10 modules with the highest number of uncovered lines

from m in Module, 
    select: [name: m.name, uncovered_lines: round(m.num_lines * (1 - m.coverage), 0)], 
    order_by: [desc: uncovered_lines], 
    limit: 10)

search-functions

Search Functions

Select functions from modules that are in the namespace 'WarewareWeb'

from f in Function,
    select: [name: f.name, arity: f.num_args],
    where: like(f.module.name, "WarewareWeb.%")

null-checking

Null Checking

Select functions name, module name and arity where the function coverage is not nil

from f in Function,
    select: [f.module.name, f.name, f.num_args],
    where: not is_nil(f.coverage)

aggregation-functions

Aggregation Functions

Select all modules name and their respective number of usages in other modules

from m in Module,
    select: [name: m.name, num_usages: count(m.in_refs.caller.name)],
    group_by: name

Select the modules with a function with cyclomatic complexity greater than 5

from m in Module,
    select: [module: m.name, max_cc: max(m.functions.cc)],
    group_by: m.id,
    having: max_cc > 5

subquery

Subquery

Select all modules that define macros

from m in Module, 
    select: m.name,
    where: exists(m.macros.name)

in-operator

IN Operator

Select modules that are implementing OTP behaviours

from b in Behaviour,   
    select: b.module.name,
    where: b.name in ["Application", "Agent", "GenServer", "GenEvent", "Supervisor"]