### Installation Source: https://hexdocs.pm/cake/index Install the Cake library for Gleam projects using the gleam add command. ```gleam gleam add cake@2 ``` -------------------------------- ### Run Test Suite Locally Source: https://hexdocs.pm/cake/index Instructions to run the project's test suite locally using Docker. It involves starting the Docker environment and then executing the Gleam test command. ```bash bin/docker/attached # wait a few seconds until everything is ready # if you run gleam test too early, it will crash gleam test ``` -------------------------------- ### Initialize Empty Select Query Source: https://hexdocs.pm/cake/cake/select Creates a new, empty `Select` query object. This is the starting point for building SQL SELECT statements. ```gleam pub fn new() -> @internal Select ``` -------------------------------- ### Demo Applications and Abstraction Source: https://hexdocs.pm/cake/changelog Introduces a demo app abstraction and provides the first runnable demo for `SELECT` and decode operations. Also includes added demos for `INSERT` and `DELETE` operations. ```gleam // Example of running a demo (conceptual) // import my_app/demos/select_demo // select_demo.run() ``` -------------------------------- ### Get INSERT Comment Source: https://hexdocs.pm/cake/cake/insert Retrieves any comment associated with an INSERT query. ```gleam pub fn get_comment( insert isrt: @internal Insert(a), ) -> @internal Comment ``` -------------------------------- ### Get INSERT Table Source: https://hexdocs.pm/cake/cake/insert Retrieves the target table name for an INSERT query. ```gleam pub fn get_table( insert isrt: @internal Insert(a), ) -> @internal InsertIntoTable ``` -------------------------------- ### Helper Commands Source: https://hexdocs.pm/cake/index A collection of helper commands for managing the Docker environment and running various development tasks, including test suite execution and code review processes. ```bash bin/docker/attached bin/docker/detached bin/docker/down bin/test bin/birdie/interactive-review bin/birdie/accept-all bin/birdie/reject-all ``` -------------------------------- ### Get INSERT Modifier Source: https://hexdocs.pm/cake/cake/insert Retrieves the modifier string applied to an INSERT query. ```gleam pub fn get_modifier(insert isrt: @internal Insert(a)) -> String ``` -------------------------------- ### Get INSERT Columns Source: https://hexdocs.pm/cake/cake/insert Retrieves the list of column names specified for an INSERT query. ```gleam pub fn get_columns( insert isrt: @internal Insert(a), ) -> @internal InsertColumns ``` -------------------------------- ### Cake Project Modules Overview Source: https://hexdocs.pm/cake/cake/dialect/sqlite_dialect Lists the available modules within the Cake project, each responsible for different aspects of SQL query construction and database dialect handling. ```APIDOC Modules: * cake * cake/combined * cake/delete * cake/dialect/maria_dialect * cake/dialect/mysql_dialect * cake/dialect/postgres_dialect * cake/dialect/sqlite_dialect * cake/fragment * cake/having * cake/insert * cake/join * cake/param * cake/select * cake/update * cake/where ``` -------------------------------- ### Cake Elixir SQL Query Builder Overview Source: https://hexdocs.pm/cake/cake/update This documentation provides an overview of the Cake Elixir library, a SQL query builder. It lists the available modules for constructing different parts of SQL queries, such as SELECT, INSERT, UPDATE, DELETE, JOIN, WHERE clauses, and fragments. It also highlights the types defined within the library, which are crucial for understanding how to build queries. ```APIDOC Cake Elixir SQL Query Builder Modules: - cake: Core functionality - cake/combined: Combining query parts - cake/delete: Building DELETE statements - cake/dialect/maria_dialect: MariaDB dialect support - cake/dialect/mysql_dialect: MySQL dialect support - cake/dialect/postgres_dialect: PostgreSQL dialect support - cake/dialect/sqlite_dialect: SQLite dialect support - cake/fragment: Creating SQL fragments - cake/having: Building HAVING clauses - cake/insert: Building INSERT statements - cake/join: Building JOIN clauses - cake/param: Handling query parameters - cake/select: Building SELECT statements - cake/update: Building UPDATE statements - cake/where: Building WHERE clauses Cake Elixir SQL Query Builder Types: - Comment - Epilog - From - Join - Joins - ReadQuery - Update - UpdateSet - UpdateSets - UpdateTable - Where - WriteQuery ``` -------------------------------- ### Get INSERT Epilog Source: https://hexdocs.pm/cake/cake/insert Retrieves the epilog (additional SQL clauses) for an INSERT query. ```gleam pub fn get_epilog( insert isrt: @internal Insert(a), ) -> @internal Epilog ``` -------------------------------- ### Cake API Documentation Source: https://hexdocs.pm/cake/changelog This section provides API documentation for the Cake library, covering various modules and their functions for building SQL queries. It includes details on SELECT, INSERT, UPDATE, DELETE, JOIN, WHERE clauses, and dialect-specific implementations. ```APIDOC cake/select: select_cols(columns: List[String]) -> Select Selects multiple columns by their names. replace_select_cols(columns: List[String]) -> Select Replaces the current selection of columns with a new list of column names. select_col(column: String) -> Select Selects a single column by its name. replace_select_col(column: String) -> Select Replaces the current selection of columns with a single column name. cake/insert: on_columns_conflict_ignore(columns: List[String]) -> Insert Specifies columns for conflict detection in INSERT statements, ignoring conflicts. on_columns_conflict_update(columns: List[String]) -> Insert Specifies columns for conflict detection in INSERT statements, updating on conflict. cake/join: left_lateral(table: String, on: String) -> Join Adds a LEFT LATERAL JOIN clause. inner_lateral(table: String, on: String) -> Join Adds an INNER LATERAL JOIN clause. cross_lateral(table: String, on: String) -> Join Adds a CROSS LATERAL JOIN clause. cake/dialect/postgres_dialect: cake_query_to_prepared_statement(query: Query) -> String Converts a Cake query to a PostgreSQL prepared statement string. read_query_to_prepared_statement(query: Query) -> String Converts a Cake read query to a PostgreSQL prepared statement string. cake/dialect/sqlite_dialect: cake_query_to_prepared_statement(query: Query) -> String Converts a Cake query to a SQLite prepared statement string. read_query_to_prepared_statement(query: Query) -> String Converts a Cake read query to a SQLite prepared statement string. cake: to_read_query(query: Query) -> ReadQuery Converts a general Cake query into a read-specific query. to_write_query(query: Query) -> WriteQuery Converts a general Cake query into a write-specific query. to_prepared_statement(query: Query) -> String Converts a Cake query into a prepared statement string. ``` -------------------------------- ### Get INSERT On Conflict Strategy Source: https://hexdocs.pm/cake/cake/insert Retrieves the conflict resolution strategy defined for an INSERT query. ```gleam pub fn get_on_conflict( insert isrt: @internal Insert(a), ) -> @internal InsertConflictStrategy(a) ``` -------------------------------- ### Get INSERT Source Source: https://hexdocs.pm/cake/cake/insert Retrieves the data source for an INSERT query, which can be a list of records or InsertRows. ```gleam pub fn get_source( insert isrt: @internal Insert(a), ) -> @internal InsertSource(a) ``` -------------------------------- ### Gleam Cake Select API Documentation Source: https://hexdocs.pm/cake/cake/select This section provides comprehensive documentation for the functions within the Gleam Cake 'select' module. It covers various aspects of building SQL SELECT queries, including defining FROM clauses, applying filters, grouping, ordering, and adding comments or epilogues. ```APIDOC alias: pub fn alias(value: SelectValue, alias: String) -> SelectValue Creates an alias off a `String`. all: pub fn all(select: Select) -> Select Sets the kind of the `Select` query to return duplicates, which is the default. bool: pub fn bool(value: Bool) -> SelectValue Creates a boolean `Param` off a `Bool`. col: pub fn col(name: String) -> SelectValue Creates a column identifier off a `String`. comment: pub fn comment(select: Select, comment: String) -> Select Appends a `Comment` to the `Select` query. distinct: pub fn distinct(select: Select) -> Select Sets the kind of the `Select` query to return distinct rows only. epilog: pub fn epilog(select: Select, epilog: String) -> Select Appends an `Epilog` to the `Select` query. float: pub fn float(value: Float) -> SelectValue Creates a float `Param` off a `Float`. fragment: pub fn fragment(fragment: Fragment) -> SelectValue Creates a `SelectFragment` off a `Fragment`. from_query: pub fn from_query(select: Select, sub_query: ReadQuery, alias: String) -> Select Sets the `FROM` clause of the `Select` query to an aliased sub-query. from_table: pub fn from_table(select: Select, name: String) -> Select Sets the `FROM` clause of the `Select` query to a table name. get_comment: pub fn get_comment(select: Select) -> Comment Gets the `Comment` from the `Select` query. get_epilog: pub fn get_epilog(select: Select) -> Epilog Gets the `Epilog` from the `Select` query. get_from: pub fn get_from(select: Select) -> From Gets the `FROM` clause of the `Select` query. get_group_by: pub fn get_group_by(select: Select) -> GroupBy Gets `GroupBy` in the `Select` query. get_having: pub fn get_having(select: Select) -> Where Gets `HAVING` in the `Select` query. See function `having` on details why this returns a `Where`. ``` -------------------------------- ### SQL Generation and Prepared Statements Source: https://hexdocs.pm/cake/changelog Provides public API access to generated SQL and prepared statement parameters through the base 'cake' and new 'dialect' modules. This allows for greater transparency and control over SQL execution. ```gleam import cake import cake/postgres_dialect let select_query = cake.select() let sql = cake.to_sql(select_query, postgres_dialect) let params = cake.to_params(select_query) ``` -------------------------------- ### Cake Select API Documentation Source: https://hexdocs.pm/cake/cake/select Provides documentation for various methods used to construct SQL SELECT queries within the Cake framework. This includes functions for adding WHERE clauses, ordering results, and grouping data. ```APIDOC or_where(slct: Select, whr: Where) -> Select Sets an `OrWhere` or appends into an existing `OrWhere`. * If the outermost `Where` is an `OrWhere`, the new `Where` is appended to the list within `OrWhere`. * If the query does not have a `Where` clause, the given `Where` is set instead. * If the outermost `Where` is any other kind of `Where`, this and the current outermost `Where` are wrapped in an `OrWhere`. ``` ```APIDOC order_by(slct: Select, ordb: String, dir: Direction) -> Select Creates or appends an `OrderBy` a column with a direction. The direction can either `ASC` or `DESC`. ``` ```APIDOC order_by_asc(slct: Select, ordb: String) -> Select Creates or appends an ascending `OrderBy`. ``` ```APIDOC order_by_asc_nulls_first(slct: Select, ordb: String) -> Select Creates or appends an ascending `OrderBy` with `NULLS FIRST`. NOTICE: 🦭MariaDB and 🐬MySQL do not support `NULLS FIRST` out of the box. ``` ```APIDOC order_by_asc_nulls_last(slct: Select, ordb: String) -> Select Creates or appends an ascending `OrderBy` with `NULLS LAST`. NOTICE: 🦭MariaDB and 🐬MySQL do not support `NULLS LAST` out of the box. ``` ```APIDOC order_by_desc(slct: Select, ordb: String) -> Select Creates or appends a descending `OrderBy`. ``` ```APIDOC order_by_desc_nulls_first(slct: Select, ordb: String) -> Select Creates or appends a descending order with `NULLS FIRST`. NOTICE: 🦭MariaDB and 🐬MySQL do not support `NULLS FIRST` out of the box. ``` ```APIDOC order_by_desc_nulls_last(slct: Select, ordb: String) -> Select Creates or appends a descending `OrderBy` with `NULLS LAST`. NOTICE: 🦭MariaDB and 🐬MySQL do not support `NULLS LAST` out of the box. ``` ```APIDOC replace_group_by(slct: Select, grpb: String) -> Select Replaces `GroupBy` with a single `GroupBy`. ``` ```APIDOC replace_group_bys(slct: Select, grpbs: List(String)) -> Select Replaces `GroupBy` with a list of `GroupBy`s. ``` ```APIDOC replace_having(slct: Select, whr: Where) -> Select Replaces `HAVING` in the `Select` query. See function `having` on details why this takes a `Where`. ``` -------------------------------- ### Database Dialect Renaming Source: https://hexdocs.pm/cake/changelog Database dialects have been renamed for consistency, for example, 'postgres' is now 'postgres_dialect'. This change aids in better organization and avoids potential naming conflicts. ```gleam import cake/postgres_dialect let dialect = postgres_dialect ``` -------------------------------- ### Create Prepared SQL Fragment Source: https://hexdocs.pm/cake/cake/fragment Creates a new SQL fragment from a string and a list of parameters. It handles potential mismatches between placeholders in the string and the number of provided parameters, with specific error handling for stderr. ```gleam pub fn prepared( string str: String, params prms: List([param.Param](https://hexdocs.pm/cake/cake/param.html#Param "cake/param.{type Param}")), ) -> @internal Fragment ``` -------------------------------- ### Gleam Cake Select API Documentation Source: https://hexdocs.pm/cake/cake/select This section provides comprehensive documentation for the functions within the Gleam Cake select module. It covers adding columns, replacing selections, setting WHERE clauses, and converting select statements to queries. ```APIDOC replace_select_cols(select: Select, select_cols: List(String)) -> Select Adds many column names as SelectValues to the Select query. If the query already has any SelectValues, the new ones are replaced. replace_selects(select: Select, select_values: List(SelectValue)) -> Select Adds many SelectValues to the Select query. If the query already has any SelectValues, they are replaced. replace_where(select: Select, where: Where) -> Select Replaces the Where in the Select query. select(select: Select, select_value: SelectValue) -> Select Add a SelectValue to the Select query. If the query already has any SelectValues, the new one is appended. select_col(select: Select, name: String) -> Select Add a column name to the Select query as a SelectValue. If the query already has any SelectValues, the new one is appended. select_cols(select: Select, select_cols: List(String)) -> Select Adds many column names as SelectValues to the Select query. If the query already has any SelectValues, the new ones are appended. selects(select: Select, select_values: List(SelectValue)) -> Select Adds many SelectValues to the Select query. If the query already has any SelectValues, the new ones are appended. string(value: String) -> SelectValue Creates a string Param off a String. to_query(select: Select) -> ReadQuery Creates a ReadQuery from a Select query. where(select: Select, where: Where) -> Select Sets an AndWhere or appends into an existing AndWhere. * If the outermost Where is an AndWhere, the new Where is appended to the list within AndWhere. * If the query does not have a Where clause, the given Where is set instead. * If the outermost Where is any other kind of Where, this and the current outermost Where are wrapped in an AndWhere. ``` -------------------------------- ### Cake SQL Query Building Modules Source: https://hexdocs.pm/cake/cake/select This section outlines the various modules available in the Cake library for constructing different parts of SQL queries. It includes modules for SELECT statements, INSERT, UPDATE, DELETE operations, JOIN clauses, WHERE conditions, and dialect-specific implementations. ```gleam cake/combined cake/delete cake/dialect/maria_dialect cake/dialect/mysql_dialect cake/dialect/postgres_dialect cake/dialect/sqlite_dialect cake/fragment cake/having cake/insert cake/join cake/param cake/select cake/update cake/where ``` -------------------------------- ### ReadQuery to PreparedStatement Conversion (SQLite) Source: https://hexdocs.pm/cake/cake/dialect/sqlite_dialect Illustrates the conversion of a `ReadQuery` to a `PreparedStatement` using the `read_query_to_prepared_statement` function within the SQLite dialect. This function is used for preparing read operations. ```gleam pub fn read_query_to_prepared_statement( query qry: @internal ReadQuery, ) -> @internal PreparedStatement ``` -------------------------------- ### CakeQuery to PreparedStatement Conversion (SQLite) Source: https://hexdocs.pm/cake/cake/dialect/sqlite_dialect Demonstrates the conversion of a `CakeQuery` to a `PreparedStatement` using the `cake_query_to_prepared_statement` function in the SQLite dialect. This is crucial for executing queries efficiently. ```gleam pub fn cake_query_to_prepared_statement( query qry: cake.CakeQuery(a), ) -> @internal PreparedStatement ``` -------------------------------- ### Cake SQL Query Builder Overview Source: https://hexdocs.pm/cake/cake/combined The Cake project offers a comprehensive SQL query builder for Gleam. It includes modules for constructing SELECT, INSERT, UPDATE, and DELETE statements, along with support for joins, where clauses, and dialect-specific configurations. ```gleam import cake import cake/select import cake/insert import cake/update import cake/delete // Example of building a SELECT query let select_query = select.new() \ |> select.columns(["name", "email"]) |> select.from_("users") |> select.where("age > 18") // Example of building an INSERT query let insert_query = insert.new("products", ["name", "price"]) |> insert.values(["Laptop", 1200]) // Example of building an UPDATE query let update_query = update.new("users") |> update.set("email", "new.email@example.com") |> update.where("id = 1") // Example of building a DELETE query let delete_query = delete.new("orders") |> delete.where("status = 'cancelled'") ``` -------------------------------- ### Cake Delete API Documentation Source: https://hexdocs.pm/cake/cake/delete Provides a comprehensive overview of the Cake library's DELETE query construction functions. This includes methods for setting the table, adding USING clauses (with tables or subqueries), replacing WHERE clauses, and specifying returning columns. ```APIDOC Cake Delete Operations: replace_join(dlt: Delete(a), jn: Join) -> Delete(a) Replaces any Joins of the Delete query with a single Join. NOTICE: On PostgreSQL and SQLite Joins are only allowed if the FROM clause is set as well. replace_joins(dlt: Delete(a), jns: List(Join)) -> Delete(a) Replaces any Joins of the Delete query with the given Joins. NOTICE: On PostgreSQL and SQLite Joins are only allowed if the FROM clause is set as well. replace_using_sub_query(dlt: Delete(a), qry: ReadQuery, als: String) -> Delete(a) Replaces the USING clause of the Delete query with a sub-query. replace_using_table(dlt: Delete(a), tbl_nm: String) -> Delete(a) Replaces the USING clause of the Delete query with a table. replace_where(dlt: Delete(a), whr: Where) -> Delete(a) Replaces the Where in the Delete query. returning(dlt: Delete(a), rtrn: List(String)) -> Delete(a) Specify the columns to return after the Delete query. table(dlt: Delete(a), tbl_nm: String) -> Delete(a) Sets the table name of the Delete query, aka the table where the rows will be deleted from. to_query(dlt: Delete(a)) -> WriteQuery(a) Creates a WriteQuery from a Delete query. using_sub_query(dlt: Delete(a), qry: ReadQuery, als: String) -> Delete(a) Adds a USING clause to the Delete query specifying a sub-query. The sub-query must be aliased. If the query already has a USING clause, the new USING clause will be appended to the existing one. The USING clause is used to specify additional tables that are used to filter the rows to be deleted. NOTICE: SQLite does not support USING. NOTICE: MariaDB and MySQL may not support sub-queries in the USING clause. In such case you may use a sub-query in a WHERE clause, or use a join instead. using_table(dlt: Delete(a), tbl_nm: String) -> Delete(a) Adds a USING clause to the Delete query specifying a table. If the query already has a USING clause, the new USING clause will be appended to the existing one. The USING clause is used to specify additional tables that are used to filter the rows to be deleted. NOTICE: SQLite does not support USING. NOTICE: For MariaDB and MySQL it is mandatory to specify the table set within the FROM clause in the USING clause, again - e.g. in raw SQL: DELETE * FROM a USING a, b, WHERE a.b_id = b.id; ``` -------------------------------- ### Create New INSERT Query Source: https://hexdocs.pm/cake/cake/insert Initializes an empty INSERT query builder. ```gleam pub fn new() -> @internal Insert(a) ``` -------------------------------- ### Cake Query Preparation Functions Source: https://hexdocs.pm/cake/cake This section details functions for converting various query types into prepared statements. It includes functions for read queries, write queries, and general Cake queries, supporting different dialects. These functions are essential for preparing database operations efficiently. ```APIDOC Cake Query Preparation: read_query_to_prepared_statement(query qry: @internal ReadQuery, dialect dlct: @internal Dialect) -> @internal PreparedStatement - Creates a prepared statement from a read query. to_prepared_statement(query qry: [CakeQuery](https://hexdocs.pm/cake/cake.html#CakeQuery)(a), dialect dlct: @internal Dialect) -> @internal PreparedStatement - Creates a prepared statement from a Cake query. - Also see `cake/dialect/*` for dialect specific implementations. write_query_to_prepared_statement(query qry: @internal WriteQuery(a), dialect dlct: @internal Dialect) -> @internal PreparedStatement - Creates a prepared statement from a write query. ``` -------------------------------- ### Flattened Query Interface/DSL Source: https://hexdocs.pm/cake/changelog The query interface and DSL modules have been flattened into the base namespace of `cake/`. Some modules previously used internally are now located in the `cake/internal/` namespace. ```gleam import cake // Accessing query functions directly from cake let select_query = cake.select() ``` -------------------------------- ### Insert, Update, and Delete Interface Modules Source: https://hexdocs.pm/cake/changelog Introduces dedicated interface modules for 'insert', 'update', and 'delete' operations, enhancing the builder's flexibility. This provides a more structured approach to data manipulation. ```gleam import cake/insert import cake/update import cake/delete let insert_statement = insert.new() let update_statement = update.new() let delete_statement = delete.new() ``` -------------------------------- ### Create JOIN target from sub-query Source: https://hexdocs.pm/cake/cake/join Creates a JOIN target from a sub-query. ```gleam pub fn sub_query( sub_query sq: @internal ReadQuery, ) -> @internal JoinTarget ``` -------------------------------- ### Database Support (MariaDB, MySQL, PostgreSQL) Source: https://hexdocs.pm/cake/changelog The library now offers first-class support for MariaDB and MySQL, independent of each other. It also maintains support for PostgreSQL. The hard dependency on specific RDBMS libraries has been removed, allowing for adapter choices like 'pog', 'sqlight', and 'gmysql'. ```gleam import cake/gmysql import cake/postgres_dialect let mysql_adapter = gmysql let postgres_adapter = postgres_dialect ``` -------------------------------- ### Cake.Select Module Values and Functions Source: https://hexdocs.pm/cake/cake/select This section provides a comprehensive list of values and functions available in the Cake.Select module. These functions are used for constructing and modifying SQL queries within the Cake framework. Each entry links to the detailed documentation for the specific function or value. ```APIDOC alias all bool col comment distinct epilog float fragment from_query from_table get_comment get_epilog get_from get_group_by get_having get_joins get_kind get_limit get_offset get_order_by get_select get_where group_by group_bys having int join joins limit new no_comment no_epilog no_from no_group_by no_having no_join no_limit no_offset no_order_by no_where null offset or_having or_where order_by order_by_asc order_by_asc_nulls_first order_by_asc_nulls_last order_by_desc order_by_desc_nulls_first order_by_desc_nulls_last replace_group_by replace_group_bys replace_having replace_join replace_joins replace_order_by replace_order_by_asc replace_order_by_asc_nulls_first replace_order_by_asc_nulls_last replace_order_by_desc replace_order_by_desc_nulls_first replace_order_by_desc_nulls_last replace_select replace_select_col replace_select_cols ``` -------------------------------- ### Cake Project Values (SQLite Dialect) Source: https://hexdocs.pm/cake/cake/dialect/sqlite_dialect Details the functions available in the SQLite dialect module for converting Cake queries into SQLite-specific prepared statements. ```APIDOC Values (SQLite Dialect): * cake_query_to_prepared_statement(query: CakeQuery(a)) -> PreparedStatement Converts a cake query to a SQLite prepared statement. * read_query_to_prepared_statement(query: ReadQuery) -> PreparedStatement Converts a read query to a SQLite prepared statement. ``` -------------------------------- ### Main Function Source: https://hexdocs.pm/cake/cake The main function for the Cake library. As a library, it cannot be invoked directly in a meaningful way. ```gleam pub fn main() -> Nil ``` -------------------------------- ### Gleam Cake Delete Query Construction Source: https://hexdocs.pm/cake/cake/delete Demonstrates how to construct DELETE queries using the Gleam Cake library. This includes setting the target table, modifying join conditions, and specifying WHERE clauses. ```gleam pub fn replace_join( delete dlt: @internal Delete(a), join jn: @internal Join, ) -> @internal Delete(a) ``` ```gleam pub fn replace_joins( delete dlt: @internal Delete(a), joins jns: List(@internal Join), ) -> @internal Delete(a) ``` ```gleam pub fn replace_using_sub_query( delete dlt: @internal Delete(a), query qry: @internal ReadQuery, alias als: String, ) -> @internal Delete(a) ``` ```gleam pub fn replace_using_table( delete dlt: @internal Delete(a), table_name tbl_nm: String, ) -> @internal Delete(a) ``` ```gleam pub fn replace_where( delete dlt: @internal Delete(a), where whr: @internal Where, ) -> @internal Delete(a) ``` ```gleam pub fn returning( delete dlt: @internal Delete(a), returning rtrn: List(String), ) -> @internal Delete(a) ``` ```gleam pub fn table( delete dlt: @internal Delete(a), table_name tbl_nm: String, ) -> @internal Delete(a) ``` ```gleam pub fn to_query( delete dlt: @internal Delete(a), ) -> @internal WriteQuery(a) ``` ```gleam pub fn using_sub_query( delete dlt: @internal Delete(a), query qry: @internal ReadQuery, alias als: String, ) -> @internal Delete(a) ``` ```gleam pub fn using_table( delete dlt: @internal Delete(a), table_name tbl_nm: String, ) -> @internal Delete(a) ``` -------------------------------- ### Create JOIN target from table name Source: https://hexdocs.pm/cake/cake/join Creates a JOIN target from a table name. ```gleam pub fn table(table_name tbl_nm: String) -> @internal JoinTarget ``` -------------------------------- ### Cake Dialect Support Source: https://hexdocs.pm/cake/cake/combined Cake provides support for multiple SQL dialects, including MariaDB, MySQL, PostgreSQL, and SQLite. This allows for database-agnostic query construction where possible, with specific handling for dialect differences. ```gleam import cake import cake/dialect/postgres_dialect import cake/dialect/mysql_dialect // Using PostgreSQL dialect let postgres_query = cake.new(postgres_dialect.new()) \ |> cake.select.columns(["id"]) |> cake.select.from_("items") // Using MySQL dialect let mysql_query = cake.new(mysql_dialect.new()) \ |> cake.select.columns(["id"]) |> cake.select.from_("items") ``` -------------------------------- ### Cake Update: Create New Query Source: https://hexdocs.pm/cake/cake/update Creates an empty `Update` query. ```gleam pub fn new() -> @internal Update(a) ``` -------------------------------- ### Prepared Statement Utility Functions Source: https://hexdocs.pm/cake/cake Provides functions to retrieve the SQL string and parameters from a prepared statement. These are essential for executing queries. ```APIDOC get_sql(prepared_statement: @internal PreparedStatement) -> String Get the SQL of the prepared statement. get_params(prepared_statement: @internal PreparedStatement) -> List(Param) Get the parameters of the prepared statement. Param: Represents a parameter in a prepared statement, likely containing type and value information. ``` -------------------------------- ### Cake Param Constructors Source: https://hexdocs.pm/cake/cake/param Provides functions to create `Param` types for various data types. These functions wrap literal values into the `Param` type for use in SQL queries. ```APIDOC Cake/Param: BoolParam(Bool) -> Param Creates a Param with a Bool value. FloatParam(Float) -> Param Creates a Param with a Float value. IntParam(Int) -> Param Creates a Param with an Int value. StringParam(String) -> Param Creates a Param with a String value. NullParam -> Param Creates a Param with an SQL NULL value. ``` -------------------------------- ### Cake Query Fragments and Clauses Source: https://hexdocs.pm/cake/cake/combined The library allows for building complex queries using fragments and various clauses like WHERE, HAVING, ORDER BY, and LIMIT. This modular approach enhances readability and maintainability of query construction. ```gleam import cake import cake/where import cake/having import cake/join import cake/param let query = cake.new(cake.default_dialect()) \ |> cake.select.columns(["u.name", "o.amount"]) |> cake.join.inner("orders o", "u.id = o.user_id") |> cake.where(where.and([where.equal("u.status", "active"), where.greater_than("o.amount", param.int(100))])) |> cake.having(having.count_greater_than("o.id", 5)) |> cake.order_by([cake.order_by("u.name", cake.OrderByDirection.Asc)]) |> cake.limit(10) ``` -------------------------------- ### Cake ORM INSERT DSL Source: https://hexdocs.pm/cake/cake/insert Provides a Domain Specific Language (DSL) for constructing SQL INSERT queries. This module allows for defining table names, columns, values, conflict strategies, and returning clauses. ```APIDOC cake/insert Module Documentation: This module provides a DSL for building `INSERT` queries. Key Types: - Insert: Represents an INSERT query builder. - InsertColumns: Defines the columns for an INSERT statement. - InsertConflictStrategy: Specifies how to handle conflicts during insertion. - InsertIntoTable: Specifies the table to insert into. - InsertRow: Represents a single row of data to be inserted. - InsertSource: Defines the source of data for insertion (values or records). - InsertValue: Represents a single value for insertion. - WriteQuery: Represents a complete SQL query that can be written. Key Functions/Values: - new(): Creates a new INSERT query builder. - table(table_name): Sets the target table for the INSERT query. - columns(column_names): Specifies the columns to insert data into. - row(values): Adds a single row of values to the INSERT query. - from_values(rows): Adds multiple rows of values to the INSERT query. - from_records(records): Adds multiple records (maps) to the INSERT query. - on_conflict_error(strategy): Specifies a conflict resolution strategy (e.g., IGNORE, UPDATE). - on_columns_conflict_ignore(columns): Ignore conflicts on specified columns. - on_columns_conflict_update(columns, updates): Update on conflicts for specified columns. - on_constraint_conflict_ignore(constraint_name): Ignore conflicts on a specific constraint. - on_constraint_conflict_update(constraint_name, updates): Update on conflicts for a specific constraint. - returning(columns): Specifies columns to return after the INSERT operation. - to_query(): Compiles the INSERT query into a string. Example Usage: ```gleam import cake/insert let query = insert.new() |> insert.table("users") |> insert.columns(["name", "email"]) |> insert.row([insert.string("Alice"), insert.string("alice@example.com")]) |> insert.row([insert.string("Bob"), insert.string("bob@example.com")]) |> insert.on_conflict_error(insert.on_columns_conflict_ignore(["email"])) |> insert.returning(["id"]) let sql = insert.to_query(query) // sql will be: INSERT INTO users (name, email) VALUES ($1, $2), ($3, $4) ON CONFLICT (email) DO NOTHING RETURNING id ``` ``` -------------------------------- ### Write Query to MariaDB Prepared Statement Source: https://hexdocs.pm/cake/cake/dialect/maria_dialect Converts a write query into a MariaDB prepared statement. This function is part of the MariaDB dialect in the Cake library. ```gleam pub fn write_query_to_prepared_statement( query qry: @internal WriteQuery(a), ) -> @internal PreparedStatement ``` -------------------------------- ### Cake XOR Having Clause Source: https://hexdocs.pm/cake/cake/select Sets an `XorWhere` or appends to an existing `XorWhere` for the HAVING clause. It handles different scenarios of existing WHERE clauses and notes database compatibility, generating equivalent SQL for unsupported databases. ```APIDOC xor_having(slct: Select, whr: Where) -> Select Sets an `XorWhere` or appends into an existing `XorWhere` for the HAVING clause. - If the outermost `Where` is an `XorWhere`, the new `Where` is appended to the list within `XorWhere`. - If the query does not have a `Where` clause, the given `Where` is set instead. - If the outermost `Where` is any other kind of `Where`, this and the current outermost `Where` are wrapped in an `XorWhere`. See function `having` on details why this takes a `Where`. NOTICE: This operator does not exist in 🐘PostgreSQL or 🪶SQLite, and _Cake_ generates equivalent SQL using `OR` and `AND` and `NOT`. This operator exists in 🦭MariaDB and 🐬MySQL. ``` -------------------------------- ### Common Aliases for Cake Modules Source: https://hexdocs.pm/cake/index Provides ergonomic aliases for frequently used modules within the Cake library, such as select, where, join, update, insert, delete, combined, and fragment. ```gleam import cake/select as s // SELECT statements import cake/where as w // WHERE clauses import cake/join as j // JOIN clauses import cake/update as u // UPDATE statements import cake/insert as i // INSERT statements import cake/delete as d // DELETE statements import cake/combined as c // For combined queries such as UNION import cake/fragment as f // For arbitrary SQL code including functions ``` -------------------------------- ### Write Query to Prepared Statement (MySQL) Source: https://hexdocs.pm/cake/cake/dialect/mysql_dialect Converts a write query into a MySQL prepared statement. This function is part of the MySQL dialect in the Cake library. ```gleam pub fn write_query_to_prepared_statement( query qry: @internal WriteQuery(a), ) -> @internal PreparedStatement ``` -------------------------------- ### Cake Query Conversion Functions Renamed Source: https://hexdocs.pm/cake/changelog Version 2.0.0 introduced breaking changes by renaming several core query conversion functions in the `cake` module. `cake.cake_read_query` is now `cake.to_read_query`, `cake.cake_write_query` is now `cake.to_write_query`, and `cake.cake_query_to_prepared_statement` is now `cake.to_prepared_statement`. ```gleam to_read_query to_write_query to_prepared_statement ``` -------------------------------- ### Cake XOR Where Clause Source: https://hexdocs.pm/cake/cake/select Sets an `XorWhere` or appends to an existing `XorWhere` for the WHERE clause. It manages the nesting of WHERE conditions and highlights database support, providing SQL alternatives for incompatible systems. ```APIDOC xor_where(slct: Select, whr: Where) -> Select Sets an `XorWhere` or appends into an existing `XorWhere` for the WHERE clause. - If the outermost `Where` is an `XorWhere`, the new `Where` is appended to the list within `XorWhere`. - If the query does not have a `Where` clause, the given `Where` is set instead. - If the outermost `Where` is any other kind of `Where`, this and the current outermost `Where` are wrapped in an `XorWhere`. NOTICE: This operator does not exist in 🐘PostgreSQL or 🪶SQLite, and _Cake_ generates equivalent SQL using `OR` and `AND` and `NOT`. This operator exists in 🦭MariaDB and 🐬MySQL. ``` -------------------------------- ### Cake Combined Query DSL Source: https://hexdocs.pm/cake/cake/combined This section outlines the API for building combined SQL queries using the Cake DSL. It covers functions for UNION, EXCEPT, INTERSECT, and related operations like ordering, limiting, and commenting. ```APIDOC Cake Combined Query DSL: UNION, UNION ALL, EXCEPT, EXCEPT ALL, INTERSECT, INTERSECT ALL: Functions to construct set operations between queries. Ordering: order_by(field: String, direction: Direction) | order_by_asc | order_by_desc: Specifies the ordering of the results. order_by_asc_nulls_first | order_by_asc_nulls_last: Ascending order with specific null handling. order_by_desc_nulls_first | order_by_desc_nulls_last: Descending order with specific null handling. replace_order_by(...): Replaces existing order by clauses. Limiting and Offsetting: limit(count: Int) | no_limit: Limits the number of results. offset(count: Int) | no_offset: Skips a specified number of results. get_limit() | get_offset(): Retrieves the current limit or offset. Commenting: comment(text: String) | no_comment(): Adds a comment to the query. get_comment(): Retrieves the current comment. Epilog: epilog(text: String) | no_epilog(): Adds an epilog to the query. get_epilog(): Retrieves the current epilog. Querying: to_query(): Converts the combined query structure to a string. get_queries(): Retrieves the underlying queries. Compatibility: SQLite does not support `EXCEPT ALL` and `INTERSECT ALL`. Types: Combined: Represents a combined query structure. Comment: Represents a comment in the query. Direction: Enum for ordering direction (Asc, Desc). Epilog: Represents an epilog in the query. Limit: Represents a limit clause in the query. ``` -------------------------------- ### PostgreSQL Dialect Value Conversions Source: https://hexdocs.pm/cake/cake/dialect/postgres_dialect Provides functions to convert various query types into PostgreSQL prepared statements. These functions are essential for executing Cake-generated queries against a PostgreSQL database. ```APIDOC cake_query_to_prepared_statement(query qry: CakeQuery(a)) -> PreparedStatement Converts a cake query to a 🐘PostgreSQL prepared statement. read_query_to_prepared_statement(query qry: ReadQuery) -> PreparedStatement Converts read query to a 🐘PostgreSQL prepared statement. write_query_to_prepared_statement(query qry: WriteQuery(a)) -> PreparedStatement Converts write query to a 🐘PostgreSQL prepared statement. ``` -------------------------------- ### Cake Query Conversion Functions Source: https://hexdocs.pm/cake/cake This section covers functions for converting different query representations into the CakeQuery format. It includes functions to convert read queries and write queries into the standard CakeQuery type, facilitating consistent query handling within the library. Dialect-specific implementations are also noted. ```APIDOC Cake Query Conversion: to_read_query(query qry: @internal ReadQuery) -> [CakeQuery](https://hexdocs.pm/cake/cake.html#CakeQuery)(a) - Creates a Cake read query from a read query. - Also see `cake/dialect/*` for dialect specific implementations. to_write_query(query qry: @internal WriteQuery(a)) -> [CakeQuery](https://hexdocs.pm/cake/cake.html#CakeQuery)(a) - Creates a Cake write query from a write query. - Also see `cake/dialect/*` for dialect specific implementations. ```