Constants

Constants

Using a #+const block, you can declare a constant and assign values to them. Those constants could be then used as an interpolation expressions to substitute value inside a code, be used in loops and conditions statements or be passed as arguments to other blocks.

Constants declared in this way are considered top-level and defined in the scope of the package. If name of the constant starts with capital letter then this constant is exported from the package as part of its public API and could be referenced from other packages.

CoginitiScript supports following built-int data types that you can use to assign to constants:

Integers
Integer values, like 1, 2, 3, etc… up to max 64bit integer value.
Floats
Float values, like 0.75, 2.5, etc… up to max 64bit float value.
Strings
String values
Keywords
Symbolic identifiers that evaluate to themselves. They provide very fast equality tests and recommend to be used as keys in Maps. Examples: :name:doc.
Lists
List of elements
Maps
Key / value pairs stored as a hash map.
#+const
  fields = ["name", "email", "postal_code"];
  num_users = 100;
  email_domain = "gmail.com";
  maps = { :key "value" };
#+end

SELECT
#+for f : fields separator "," do
  {{ f }}
#+end
FROM
  customers
WHERE
  email LIKE "%@{{ email_domain }}"
LIMIT
  {{ num_users }}
;

 

You could also pass constant as argument into your blocks.

#+const
companyDomain = "acme.com";
#+end

#+src sql customersWithDomain(emailDomain)
#+begin
SELECT
id,
name,
email
FROM
customers
WHERE
email LIKE "%@{{ emailDomain }}"
#+end

SELECT * FROM {{ customersWithDomain(companyDomain) }};

 

Was this article helpful?
0 out of 0 found this helpful
Have more questions? Submit a request