This module implements macro-based SQL query generation for Ozark. It can be used to construct complex SQL queries in a type-safe manner, with compile-time checks for model and column existence.
Types
OzarkModelDefect = object of CatchableError
- Exception type for errors related to model definitions, such as referencing an unknown model or column in a query
Procs
proc `$`(sql: SqlQuery): string {....raises: [], tags: [], forbids: [].}
proc getPlaceholder(modelTuple: NimNode; idx: int = 1): string {....raises: [], tags: [], forbids: [].}
- Get the appropriate parameter placeholder for the specified SQL driver. This is used in the insert and update macros to generate the correct SQL syntax for parameter placeholders based on the configured driver.
proc ozarkCreateTableResult(sql: static[string]): NimNode {.compileTime.}
proc ozarkHoldModel[T: typedesc](t: T) {.compileTime.}
proc ozarkHoldModel[T](t: T) {.compileTime.}
proc ozarkInsertResult(sql: static[string]; vals: seq[string]): NimNode {. compileTime.}
proc ozarkLimitResult(sql: static[string]): NimNode {.compileTime.}
proc ozarkOrderByResult(sql: static[string]; vals: varargs[string]): NimNode {. compileTime.}
proc ozarkRawSQLResult(sql: static[string]; vals: varargs[string]): NimNode {. compileTime.}
proc ozarkRemoveResult(sql: static[string]): NimNode {.compileTime.}
proc ozarkSelectResult(sql: static[string]): NimNode {.compileTime.}
proc ozarkUpdateResult(sql: static[string]; vals: seq[string]): NimNode {. compileTime.}
proc ozarkWhereInResult(sql: static[string]; vals: varargs[string]): NimNode {. compileTime.}
proc ozarkWhereResult(sql: static[string]; val: varargs[string]): NimNode {. compileTime.}
Macros
macro dropTable(modelTuple: untyped; cascade: static bool = false): untyped
-
Compile-time macro to drop a model's table from the database.
Cascade option is included for compatibility with databases that support it, but will be ignored for SQLite since it does not support CASCADE with DROP TABLE
macro extractSQL(sql: NimNode): untyped
- Extracts the SQL NimNode to string for use in code generation
macro fromSQL(sql: untyped): untyped
- Macro to parse a resumed SQL string back into a NimNode for further manipulation This is used in the where macros to allow chaining multiple clauses based on runtime computations.
macro insert(modelTuple, data: untyped): untyped
- Placeholder for an INSERT statement. This macro generates the SQL string for the INSERT statement. This macro performs compile-time checks for the existence of the specified model and the validity of the column names.
macro orWhere(sql: untyped; col: static string; val: untyped): untyped
- Define OR in WHERE clause
macro orWhereNot(sql: untyped; col: static string; val: untyped): untyped
- Define a OR condition with NOT in WHERE clause
macro prepareTable(modelTuple): untyped
-
Compile-time macro to prepare a model's table in the database.
This macro generates the SQL string for creating the table based on the model definition and executes it at compile time to ensure the table exists before any queries are made against it.
macro removeRow(modelTuple: untyped): untyped
- Placeholder for a DELETE statement. This macro generates the SQL string for the DELETE statement. This macro performs compile-time checks for the existence of the specified model.
macro select(modelTuple: untyped; col: static string): untyped
- Define SELECT clause
macro select(modelTuple: untyped; cols: static openArray[string]): untyped
- Define SELECT clause with specific columns.
macro selectAll(modelTuple: untyped): untyped
- Define SELECT * clause
macro update(modelTuple, data: untyped): untyped
- Placeholder for an UPDATE statement. This macro generates the SQL string for the UPDATE statement. This macro performs compile-time checks for the existence of the specified model and the validity of the column names.
macro where(sql: untyped; col: static string; vals: untyped): untyped
- Define WHERE clause
macro whereEndsLike(sql: untyped; col: static string; val: untyped): untyped
- Define WHERE clause with LIKE for suffix matching
macro whereIn(sql: untyped; col: static string; vals: untyped): untyped
- Define WHERE clause with IN operator
macro whereLike(sql: untyped; col: static string; val: untyped): untyped
- Define WHERE clause with LIKE for any position
macro whereNot(sql: untyped; col: static string; val: untyped): untyped
- Define WHERE clause with NOT
macro whereNotEndsLike(sql: untyped; col: static string; val: untyped): untyped
- Define WHERE clause with NOT LIKE for suffix matching
macro whereNotIn(sql: untyped; col: static string; vals: untyped): untyped
- Define WHERE clause with NOT IN operator
macro whereNotLike(sql: untyped; col: static string; val: untyped): untyped
- Define WHERE clause with NOT LIKE for any position
macro whereNotStartsLike(sql: untyped; col: static string; val: untyped): untyped
- Define WHERE clause with NOT LIKE for prefix matching
macro whereStartsLike(sql: untyped; col: static string; val: untyped): untyped
- Define WHERE clause with LIKE for prefix matching
Templates
template table(models: ptr ModelsTable; name): untyped
- Define SQL statement for a table
template withColumn(x: NimNode; col: string; body)
template withColumnCheck(model: NimNode; col: string; body)
-
Check if the specified column exists in the model definition
This template is used by the query macros to perform compile-time checks for the existence of columns in the specified model. It ensures that any column referenced in a query actually exists in the model definition.
template withColumnsCheck(model: NimNode; cols: openArray[string]; body)
- Check if the specified columns exist in the model definition
template withTableCheck(modelTuple: NimNode; body)
- Check if a model with the given name exists in the Models table.