### prepare_with_clause_start Source: https://docs.rs/sea-query/latest/sea_query/backend/trait.QueryBuilder.html Prepares the start of a WITH clause. ```APIDOC ## prepare_with_clause_start ### Description Prepares the start of a WITH clause. ### Signature `fn prepare_with_clause_start(&self, with_clause: &WithClause, sql: &mut dyn SqlWriter)` ``` -------------------------------- ### Example Usage of WithClause Source: https://docs.rs/sea-query/latest/sea_query/query/struct.WithClause.html A comprehensive example showing the creation of a recursive WITH clause with a CTE, cycle detection, and its application to a SelectStatement. ```APIDOC ## Example: Recursive WITH Clause with CTE and Cycle ```rust use sea_query::{*, IntoCondition, IntoIden, tests_cfg::*}; let base_query = SelectStatement::new() .column("id") .expr(1i32) .column("next") .column("value") .from("table") .to_owned(); let cte_referencing = SelectStatement::new() .column("id") .expr(Expr::col("depth").add(1i32)) .column("next") .column("value") .from("table") .join( JoinType::InnerJoin, "cte_traversal", Expr::col(("cte_traversal", "next")).equals(("table", "id")), ) .to_owned(); let common_table_expression = CommonTableExpression::new() .query( base_query.clone().union(UnionType::All, cte_referencing).to_owned() ) .column("id") .column("depth") .column("next") .column("value") .table_name("cte_traversal") .to_owned(); let select = SelectStatement::new() .column(ColumnRef::Asterisk) .from("cte_traversal") .to_owned(); let with_clause = WithClause::new() .recursive(true) .cte(common_table_expression) .cycle(Cycle::new_from_expr_set_using(SimpleExpr::Column(ColumnRef::Column("id".into_iden())), "looped", "traversal_path")) .to_owned(); let query = select.with(with_clause).to_owned(); // Assertions for different database query builders // assert_eq!(query.to_string(MysqlQueryBuilder), r#"..."#); // assert_eq!(query.to_string(PostgresQueryBuilder), r#"..."#); // assert_eq!(query.to_string(SqliteQueryBuilder), r#"..."#); ``` ``` -------------------------------- ### Parameter Binding Example Source: https://docs.rs/sea-query/latest/sea_query/index.html Demonstrates how SeaQuery handles parameter binding for SQL queries, preventing SQL injection vulnerabilities. This example uses PostgresQueryBuilder. ```rust assert_eq!( Query::select() .column(Glyph::Image) .from(Glyph::Table) .and_where(Expr::col(Glyph::Image).like("A")) .and_where(Expr::col(Glyph::Id).is_in([1, 2, 3])) .build(PostgresQueryBuilder), ( r#"SELECT \"image\" FROM \"glyph\" WHERE \"image\" LIKE $1 AND \"id\" IN ($2, $3, $4)"# .to_owned(), Values(vec![ Value::String(Some(Box::new("A".to_owned()))), Value::Int(Some(1)), Value::Int(Some(2)), Value::Int(Some(3)) ]) ) ); ``` -------------------------------- ### Window Function Example Source: https://docs.rs/sea-query/latest/sea_query/query/struct.SelectStatement.html Demonstrates how to use `expr_window_name_as` and `window` to define and apply window functions in a SELECT statement. ```APIDOC ## SELECT with Window Function ### Description This example shows how to create a SELECT statement that includes a window function with a specified name and alias, and defines the window's partitioning. ### Method `Query::select()` followed by `expr_window_name_as()` and `window()` ### Endpoint N/A (SDK method) ### Parameters None directly on the method, but `Expr::col` and `WindowStatement::partition_by` are used. ### Request Example ```rust use sea_query::{tests_cfg::*, *}; let query = Query::select() .from(Char::Table) .expr_window_name_as(Expr::col(Char::Character), "w", "C") .window("w", WindowStatement::partition_by(Char::FontSize)) .to_owned(); ``` ### Response #### Success Response (String Representation of Query) - **query.to_string(builder)** (string) - The generated SQL query string for a given SQL dialect. #### Response Example ```sql -- MySQL SELECT `character` OVER `w` AS `C` FROM `character` WINDOW `w` AS (PARTITION BY `font_size`) -- PostgreSQL/SQLite SELECT "character" OVER "w" AS "C" FROM "character" WINDOW "w" AS (PARTITION BY "font_size") ``` ``` -------------------------------- ### Create a Foreign Key Constraint with Composite Key Source: https://docs.rs/sea-query/latest/sea_query/foreign_key/struct.ForeignKeyCreateStatement.html This example shows how to create a foreign key constraint involving composite keys. It is similar to the single key example but specifies multiple columns for both the source and target keys. Examples are provided for both MySQL and PostgreSQL. ```rust use sea_query::{tests_cfg::*}; use sea_query::*; let foreign_key = ForeignKey::create() .name("FK_character_glyph") .from(Char::Table, (Char::FontId, Char::Id)) .to(Glyph::Table, (Char::FontId, Glyph::Id)) .on_delete(ForeignKeyAction::Cascade) .on_update(ForeignKeyAction::Cascade) .to_owned(); assert_eq!( foreign_key.to_string(MysqlQueryBuilder), [ r#"ALTER TABLE `character`"#, r#"ADD CONSTRAINT `FK_character_glyph`"#, r#"FOREIGN KEY (`font_id`, `id`) REFERENCES `glyph` (`font_id`, `id`)"#, r#"ON DELETE CASCADE ON UPDATE CASCADE"#, ] .join(" ") ); assert_eq!( foreign_key.to_string(PostgresQueryBuilder), [ r#"ALTER TABLE "character" ADD CONSTRAINT "FK_character_glyph""#, r#"FOREIGN KEY ("font_id", "id") REFERENCES "glyph" ("font_id", "id")"#, r#"ON DELETE CASCADE ON UPDATE CASCADE"#, ] .join(" ") ); ``` -------------------------------- ### CHAR_LENGTH Function Example Source: https://docs.rs/sea-query/latest/sea_query/func/struct.Func.html Shows how to use the CHAR_LENGTH function, noting that SQLite uses LENGTH. ```rust use sea_query::{tests_cfg::*, *}; let query = Query::select() .expr(Func::char_length(Expr::col((Char::Table, Char::Character)))) .from(Char::Table) .to_owned(); assert_eq!( query.to_string(MysqlQueryBuilder), r#"SELECT CHAR_LENGTH(`character`.`character`) FROM `character`"# ); assert_eq!( query.to_string(PostgresQueryBuilder), r#"SELECT CHAR_LENGTH(\"character\".\"character\") FROM \"character\""# ); assert_eq!( query.to_string(SqliteQueryBuilder), r#"SELECT LENGTH(\"character\".\"character\") FROM \"character\""# ); ``` -------------------------------- ### Inner Join Example Source: https://docs.rs/sea-query/latest/sea_query/query/struct.SelectStatement.html Shows how to create a SELECT statement with an INNER JOIN clause. Compatible with various SQL dialects. ```rust use sea_query::{*, tests_cfg::*}; let query = Query::select() .column(Char::Character) .column((Font::Table, Font::Name)) .from(Char::Table) .inner_join(Font::Table, Expr::col((Char::Table, Char::FontId)).equals((Font::Table, Font::Id))) .to_owned(); assert_eq!( query.to_string(MysqlQueryBuilder), r#"SELECT `character`, `font`.`name` FROM `character` INNER JOIN `font` ON `character`.`font_id` = `font`.`id`"# ); assert_eq!( query.to_string(PostgresQueryBuilder), r#"SELECT \"character\", \"font\".\"name\" FROM \"character\" INNER JOIN \"font\" ON \"character\".\"font_id\" = \"font\".\"id\""# ); assert_eq!( query.to_string(SqliteQueryBuilder), r#"SELECT \"character\", \"font\".\"name\" FROM \"character\" INNER JOIN \"font\" ON \"character\".\"font_id\" = \"font\".\"id\""# ); ``` -------------------------------- ### Create Temporary Table Source: https://docs.rs/sea-query/latest/sea_query/table/struct.TableCreateStatement.html Demonstrates how to create a temporary table using the .temporary() method. This example shows different SQL dialects for temporary table creation. ```rust use sea_query::{tests_cfg::*, *}; let statement = Table::create() .table(Font::Table) .temporary() .col( ColumnDef::new(Font::Id) .integer() .not_null() .primary_key() .auto_increment(), ) .col(ColumnDef::new(Font::Name).string().not_null()) .take(); assert_eq!( statement.to_string(MysqlQueryBuilder), [ "CREATE TEMPORARY TABLE `font` (", "`id` int NOT NULL PRIMARY KEY AUTO_INCREMENT,", "`name` varchar(255) NOT NULL", ")", ] .join(" ") ); assert_eq!( statement.to_string(PostgresQueryBuilder), [ r#"CREATE TEMPORARY TABLE \"font\" ("#, r#"\"id\" serial NOT NULL PRIMARY KEY,"#, r#"\"name\" varchar NOT NULL"#, r#" )"#, ] .join(" ") ); assert_eq!( statement.to_string(SqliteQueryBuilder), [ r#"CREATE TEMPORARY TABLE \"font\" ("#, r#"\"id\" integer NOT NULL PRIMARY KEY AUTOINCREMENT,"#, r#"\"name\" varchar NOT NULL"#, r#" )"#, ] .join(" ") ); ``` -------------------------------- ### PgFunc::any Example Source: https://docs.rs/sea-query/latest/sea_query/extension/postgres/struct.PgFunc.html Demonstrates how to call the ANY function in PostgreSQL, typically used with arrays. This function requires the `postgres-array` feature to be enabled. ```rust use sea_query::{tests_cfg::*, *}; let query = Query::select().expr(PgFunc::any(vec![0, 1])).to_owned(); assert_eq!( query.to_string(PostgresQueryBuilder), r#"SELECT ANY(ARRAY [0,1])"# ); ``` -------------------------------- ### LEAST Function Example Source: https://docs.rs/sea-query/latest/sea_query/func/struct.Func.html Illustrates the LEAST function, noting that SQLite uses MIN for multiple arguments. ```rust use sea_query::{tests_cfg::*, *}; let query = Query::select() .expr(Func::least([ Expr::col(Char::SizeW).into(), Expr::col(Char::SizeH).into(), ])) .from(Char::Table) .to_owned(); assert_eq!( query.to_string(MysqlQueryBuilder), r#"SELECT LEAST(`size_w`, `size_h`) FROM `character`"# ); assert_eq!( query.to_string(PostgresQueryBuilder), r#"SELECT LEAST(\"size_w\", \"size_h\") FROM \"character\""# ); assert_eq!( query.to_string(SqliteQueryBuilder), r#"SELECT MIN(\"size_w\", \"size_h\") FROM \"character\""# ); ``` -------------------------------- ### Drop Table Statement Example Source: https://docs.rs/sea-query/latest/sea_query/table/struct.TableDropStatement.html Demonstrates how to create and build a DROP TABLE statement for different SQL dialects (MySQL, PostgreSQL, SQLite). ```rust use sea_query::{tests_cfg::*, *}; let table = Table::drop() .table(Glyph::Table) .table(Char::Table) .to_owned(); assert_eq!( table.to_string(MysqlQueryBuilder), r#"DROP TABLE `glyph`, `character`"# ); assert_eq!( table.to_string(PostgresQueryBuilder), r#"DROP TABLE \"glyph\", \"character\""# ); assert_eq!( table.to_string(SqliteQueryBuilder), r#"DROP TABLE \"glyph\", \"character\""# ); ``` -------------------------------- ### COUNT DISTINCT Function Example Source: https://docs.rs/sea-query/latest/sea_query/func/struct.Func.html Demonstrates how to use COUNT_DISTINCT with different SQL dialects. ```rust use sea_query::{tests_cfg::*, *}; let query = Query::select() .expr(Func::count_distinct(Expr::col((Char::Table, Char::Id)))) .from(Char::Table) .to_owned(); assert_eq!( query.to_string(MysqlQueryBuilder), r#"SELECT COUNT(DISTINCT `character`.`id`) FROM `character`"# ); assert_eq!( query.to_string(PostgresQueryBuilder), r#"SELECT COUNT(DISTINCT \"character\".\"id\") FROM \"character\""# ); assert_eq!( query.to_string(SqliteQueryBuilder), r#"SELECT COUNT(DISTINCT \"character\".\"id\") FROM \"character\""# ); ``` -------------------------------- ### IFNULL Function Example Source: https://docs.rs/sea-query/latest/sea_query/func/struct.Func.html Demonstrates the IFNULL function, noting that PostgreSQL uses COALESCE. ```rust use sea_query::{tests_cfg::*, *}; let query = Query::select() .expr(Func::if_null( Expr::col(Char::SizeW), Expr::col(Char::SizeH), )) .from(Char::Table) .to_owned(); assert_eq!( query.to_string(MysqlQueryBuilder), r#"SELECT IFNULL(`size_w`, `size_h`) FROM `character`"# ); assert_eq!( query.to_string(PostgresQueryBuilder), r#"SELECT COALESCE(\"size_w\", \"size_h\") FROM \"character\""# ); assert_eq!( query.to_string(SqliteQueryBuilder), r#"SELECT IFNULL(\"size_w\", \"size_h\") FROM \"character\""# ); ``` -------------------------------- ### PgFunc::some Example Source: https://docs.rs/sea-query/latest/sea_query/extension/postgres/struct.PgFunc.html Demonstrates how to call the SOME function in PostgreSQL, typically used with arrays. This function requires the `postgres-array` feature to be enabled. ```rust use sea_query::{tests_cfg::*, *}; let query = Query::select().expr(PgFunc::some(vec![0, 1])).to_owned(); assert_eq!( query.to_string(PostgresQueryBuilder), r#"SELECT SOME(ARRAY [0,1])"# ); ``` -------------------------------- ### Create Index Source: https://docs.rs/sea-query/latest/sea_query/index.html Creates an index on a specified column of a table. Examples provided for MySQL, PostgreSQL, and SQLite. ```rust let index = Index::create() .name("idx-glyph-aspect") .table(Glyph::Table) .col(Glyph::Aspect) .to_owned(); assert_eq!( index.to_string(MysqlQueryBuilder), r#"CREATE INDEX `idx-glyph-aspect` ON `glyph` (`aspect`)"# ); assert_eq!( index.to_string(PostgresQueryBuilder), r#"CREATE INDEX \"idx-glyph-aspect\" ON \"glyph\" (\"aspect\")"# ); assert_eq!( index.to_string(SqliteQueryBuilder), r#"CREATE INDEX \"idx-glyph-aspect\" ON \"glyph\" (\"aspect\")"# ); ``` -------------------------------- ### Right Join Example Source: https://docs.rs/sea-query/latest/sea_query/query/struct.SelectStatement.html Demonstrates constructing a SELECT statement with a RIGHT JOIN clause. Supports multiple SQL dialects. ```rust use sea_query::{*, tests_cfg::*}; let query = Query::select() .column(Char::Character) .column((Font::Table, Font::Name)) .from(Char::Table) .right_join(Font::Table, Expr::col((Char::Table, Char::FontId)).equals((Font::Table, Font::Id))) .to_owned(); assert_eq!( query.to_string(MysqlQueryBuilder), r#"SELECT `character`, `font`.`name` FROM `character` RIGHT JOIN `font` ON `character`.`font_id` = `font`.`id`"# ); assert_eq!( query.to_string(PostgresQueryBuilder), r#"SELECT \"character\", \"font\".\"name\" FROM \"character\" RIGHT JOIN \"font\" ON \"character\".\"font_id\" = \"font\".\"id\""# ); assert_eq!( query.to_string(SqliteQueryBuilder), r#"SELECT \"character\", \"font\".\"name\" FROM \"character\" RIGHT JOIN \"font\" ON \"character\".\"font_id\" = \"font\".\"id\""# ); ``` -------------------------------- ### Query Insert Statement Source: https://docs.rs/sea-query/latest/sea_query/index.html Demonstrates how to build an `INSERT` statement with specified columns and values, providing examples for MySQL, PostgreSQL, and SQLite. ```APIDOC ## Query Insert ```rust let query = Query::insert() .into_table(Glyph::Table) .columns([Glyph::Aspect, Glyph::Image]) .values_panic([5.15.into(), "12A".into()]) .values_panic([4.21.into(), "123".into()]) .to_owned(); assert_eq!( query.to_string(MysqlQueryBuilder), r#"INSERT INTO `glyph` (`aspect`, `image`) VALUES (5.15, '12A'), (4.21, '123')"# ); assert_eq!( query.to_string(PostgresQueryBuilder), r#"INSERT INTO \"glyph\" (\"aspect\", \"image\") VALUES (5.15, '12A'), (4.21, '123')"# ); assert_eq!( query.to_string(SqliteQueryBuilder), r#"INSERT INTO \"glyph\" (\"aspect\", \"image\") VALUES (5.15, '12A'), (4.21, '123')"# ); ``` ``` -------------------------------- ### Initialize TableForeignKey Source: https://docs.rs/sea-query/latest/sea_query/foreign_key/struct.TableForeignKey.html Construct a new instance of TableForeignKey. This is the starting point for defining a foreign key. ```rust pub fn new() -> Self ``` -------------------------------- ### Constructing Left Join Statements Source: https://docs.rs/sea-query/latest/sea_query/query/struct.SelectStatement.html Illustrates how to build SELECT statements with LEFT JOIN clauses. Examples are provided for MySQL, PostgreSQL, and SQLite. ```rust use sea_query::{*, tests_cfg::*}; let query = Query::select() .column(Char::Character) .column((Font::Table, Font::Name)) .from(Char::Table) .left_join(Font::Table, Expr::col((Char::Table, Char::FontId)).equals((Font::Table, Font::Id))) .to_owned(); assert_eq!( query.to_string(MysqlQueryBuilder), r#"SELECT `character`, `font`.`name` FROM `character` LEFT JOIN `font` ON `character`.`font_id` = `font`.`id`"# ); assert_eq!( query.to_string(PostgresQueryBuilder), r#"SELECT \"character\", \"font\".\"name\" FROM \"character\" LEFT JOIN \"font\" ON \"character\".\"font_id\" = \"font\".\"id\""# ); assert_eq!( query.to_string(SqliteQueryBuilder), r#"SELECT \"character\", \"font\".\"name\" FROM \"character\" LEFT JOIN \"font\" ON \"character\".\"font_id\" = \"font\".\"id\""# ); ``` ```rust let query = Query::select() .column(Char::Character) .column((Font::Table, Font::Name)) .from(Char::Table) .left_join( Font::Table, all![ Expr::col((Char::Table, Char::FontId)).equals((Font::Table, Font::Id)), Expr::col((Char::Table, Char::FontId)).equals((Font::Table, Font::Id)), ] ) .to_owned(); assert_eq!( query.to_string(MysqlQueryBuilder), r#"SELECT `character`, `font`.`name` FROM `character` LEFT JOIN `font` ON `character`.`font_id` = `font`.`id` AND `character`.`font_id` = `font`.`id`"# ); assert_eq!( query.to_string(PostgresQueryBuilder), r#"SELECT \"character\", \"font\".\"name\" FROM \"character\" LEFT JOIN \"font\" ON \"character\".\"font_id\" = \"font\".\"id\" AND \"character\".\"font_id\" = \"font\".\"id\""# ); assert_eq!( query.to_string(SqliteQueryBuilder), r#"SELECT \"character\", \"font\".\"name\" FROM \"character\" LEFT JOIN \"font\" ON \"character\".\"font_id\" = \"font\".\"id\" AND \"character\".\"font_id\" = \"font\".\"id\""# ); ``` -------------------------------- ### Tuple Comparison with Expr::tuple Source: https://docs.rs/sea-query/latest/sea_query/expr/struct.Expr.html Shows how to use Expr::tuple to wrap a tuple of SimpleExprs for tuple comparison. Examples cover MySQL, PostgreSQL, and SQLite. ```rust use sea_query::{tests_cfg::*, *}; let query = Query::select() .columns([Char::Character, Char::SizeW, Char::SizeH]) .from(Char::Table) .and_where( Expr::tuple([Expr::col(Char::SizeW).into(), Expr::value(100)]) .lt(Expr::tuple([Expr::value(500), Expr::value(100)]))) .to_owned(); assert_eq!( query.to_string(MysqlQueryBuilder), r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE (`size_w`, 100) < (500, 100)"# ); assert_eq!( query.to_string(PostgresQueryBuilder), r#"SELECT \"character\", \"size_w\", \"size_h\" FROM \"character\" WHERE (\"size_w\", 100) < (500, 100)"# ); assert_eq!( query.to_string(SqliteQueryBuilder), r#"SELECT \"character\", \"size_w\", \"size_h\" FROM \"character\" WHERE (\"size_w\", 100) < (500, 100)"# ); ``` -------------------------------- ### Basic DeleteStatement Example Source: https://docs.rs/sea-query/latest/sea_query/query/struct.DeleteStatement.html Constructs a DELETE statement with a WHERE clause using OR conditions. Demonstrates generating SQL for MySQL, PostgreSQL, and SQLite. ```rust use sea_query::{tests_cfg::*, *}; let query = Query::delete() .from_table(Glyph::Table) .cond_where(any![ Expr::col(Glyph::Id).lt(1), Expr::col(Glyph::Id).gt(10), ]) .to_owned(); assert_eq!( query.to_string(MysqlQueryBuilder), r#"DELETE FROM `glyph` WHERE `id` < 1 OR `id` > 10"# ); assert_eq!( query.to_string(PostgresQueryBuilder), r#"DELETE FROM \"glyph\" WHERE \"id\" < 1 OR \"id\" > 10"# ); assert_eq!( query.to_string(SqliteQueryBuilder), r#"DELETE FROM \"glyph\" WHERE \"id\" < 1 OR \"id\" > 10"# ); ``` -------------------------------- ### GREATEST Function Example Source: https://docs.rs/sea-query/latest/sea_query/func/struct.Func.html Demonstrates the GREATEST function, noting that SQLite uses MAX for multiple arguments. ```rust use sea_query::{tests_cfg::*, *}; let query = Query::select() .expr(Func::greatest([ Expr::col(Char::SizeW).into(), Expr::col(Char::SizeH).into(), ])) .from(Char::Table) .to_owned(); assert_eq!( query.to_string(MysqlQueryBuilder), r#"SELECT GREATEST(`size_w`, `size_h`) FROM `character`"# ); assert_eq!( query.to_string(PostgresQueryBuilder), r#"SELECT GREATEST(\"size_w\", \"size_h\") FROM \"character\""# ); assert_eq!( query.to_string(SqliteQueryBuilder), r#"SELECT MAX(\"size_w\", \"size_h\") FROM \"character\""# ); ``` -------------------------------- ### ExprTrait::equals Example Source: https://docs.rs/sea-query/latest/sea_query/expr/trait.ExprTrait.html Shows how to express equality between two table columns using ExprTrait::equals. ```rust use sea_query::{*, tests_cfg::*}; let query = Query::select() .columns([Char::Character, Char::SizeW, Char::SizeH]) .from(Char::Table) .and_where((Char::Table, Char::FontId).into_column_ref().equals((Font::Table, Font::Id))) .to_owned(); assert_eq!( query.to_string(MysqlQueryBuilder), r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `character`.`font_id` = `font`.`id`"# ); assert_eq!( query.to_string(PostgresQueryBuilder), r#"SELECT \"character\", \"size_w\", \"size_h\" FROM \"character\" WHERE \"character\".\"font_id\" = \"font\".\"id\""# ); assert_eq!( query.to_string(SqliteQueryBuilder), r#"SELECT \"character\", \"size_w\", \"size_h\" FROM \"character\" WHERE \"character\".\"font_id\" = \"font\".\"id\""# ); ``` -------------------------------- ### Insert with Columns and Values Source: https://docs.rs/sea-query/latest/sea_query/query/struct.InsertStatement.html Construct an INSERT statement specifying columns and their corresponding values. This example inserts a single value into the 'image' column. ```rust use sea_query::{tests_cfg::*, *}; // Ordinary insert as columns and values are supplied let query = Query::insert() .into_table(Glyph::Table) .or_default_values_many(3) .columns([Glyph::Image]) .values_panic(["ABC".into()]) .to_owned(); assert_eq!( query.to_string(MysqlQueryBuilder), r#"INSERT INTO `glyph` (`image`) VALUES ('ABC')"# ); assert_eq!( query.to_string(PostgresQueryBuilder), r#"INSERT INTO \"glyph\" (\"image\") VALUES ('ABC')"# ); assert_eq!( query.to_string(SqliteQueryBuilder), r#"INSERT INTO \"glyph\" (\"image\") VALUES ('ABC')"# ); ``` -------------------------------- ### Create Table with Column Definitions Source: https://docs.rs/sea-query/latest/sea_query/table/struct.ColumnDef.html Demonstrates creating a table with multiple columns, specifying their types, default values, and constraints like NOT NULL. This example shows usage with both MySQL and PostgreSQL query builders. ```rust use sea_query::{tests_cfg::*}; use sea_query::*; let table = Table::create() .table(Char::Table) .col(ColumnDef::new(Char::FontId).integer().default(12i32)) .col( ColumnDef::new(Char::CreatedAt) .timestamp() .default(Expr::current_timestamp()) .not_null(), ) .to_owned(); assert_eq!( table.to_string(MysqlQueryBuilder), [ "CREATE TABLE `character` (", "`font_id` int DEFAULT 12,", "`created_at` timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL", ")", ] .join(" ") ); assert_eq!( table.to_string(PostgresQueryBuilder), [ r#"CREATE TABLE \"character\" ("#, r#"\"font_id\" integer DEFAULT 12,"#, r#"\"created_at\" timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL"#, r#" )"#, ] .join(" ") ); ``` -------------------------------- ### Constructing an UPDATE statement Source: https://docs.rs/sea-query/latest/sea_query/query/struct.UpdateStatement.html Use Query::update() to start building an UPDATE statement. Specify the table, values to set, and a WHERE clause. Examples show output for MySQL, PostgreSQL, and SQLite. ```rust use sea_query::{tests_cfg::*, *}; let query = Query::update() .table(Glyph::Table) .values([(Glyph::Aspect, 1.23.into()), (Glyph::Image, "123".into())]) .and_where(Expr::col(Glyph::Id).eq(1)) .to_owned(); assert_eq!( query.to_string(MysqlQueryBuilder), r#"UPDATE `glyph` SET `aspect` = 1.23, `image` = '123' WHERE `id` = 1"# ); assert_eq!( query.to_string(PostgresQueryBuilder), r#"UPDATE "glyph" SET "aspect" = 1.23, "image" = '123' WHERE "id" = 1"# ); assert_eq!( query.to_string(SqliteQueryBuilder), r#"UPDATE "glyph" SET "aspect" = 1.23, "image" = '123' WHERE "id" = 1"# ); ``` -------------------------------- ### Joining with a Subquery Source: https://docs.rs/sea-query/latest/sea_query/query/struct.SelectStatement.html Demonstrates how to perform a LEFT JOIN with a subquery, aliasing the subquery and specifying the join condition. Examples are provided for MySQL, PostgreSQL, and SQLite. ```rust use sea_query::{*, tests_cfg::*}; let sub_glyph: DynIden = SeaRc::new("sub_glyph"); let query = Query::select() .column(Font::Name) .from(Font::Table) .join_subquery( JoinType::LeftJoin, Query::select().column(Glyph::Id).from(Glyph::Table).take(), sub_glyph.clone(), Expr::col((Font::Table, Font::Id)).equals((sub_glyph.clone(), Glyph::Id)) ) .to_owned(); assert_eq!( query.to_string(MysqlQueryBuilder), r#"SELECT `name` FROM `font` LEFT JOIN (SELECT `id` FROM `glyph`) AS `sub_glyph` ON `font`.`id` = `sub_glyph`.`id`"# ); assert_eq!( query.to_string(PostgresQueryBuilder), r#"SELECT \"name\" FROM \"font\" LEFT JOIN (SELECT \"id\" FROM \"glyph\") AS \"sub_glyph\" ON \"font\".\"id\" = \"sub_glyph\".\"id\""# ); assert_eq!( query.to_string(SqliteQueryBuilder), r#"SELECT \"name\" FROM \"font\" LEFT JOIN (SELECT \"id\" FROM \"glyph\") AS \"sub_glyph\" ON \"font\".\"id\" = \"sub_glyph\".\"id\""# ); ``` ```rust // Constructing chained join conditions assert_eq!( Query::select() .column(Font::Name) .from(Font::Table) .join_subquery( JoinType::LeftJoin, Query::select().column(Glyph::Id).from(Glyph::Table).take(), sub_glyph.clone(), Condition::all() .add(Expr::col((Font::Table, Font::Id)).equals((sub_glyph.clone(), Glyph::Id))) .add(Expr::col((Font::Table, Font::Id)).equals((sub_glyph.clone(), Glyph::Id))) ) .to_string(MysqlQueryBuilder), r#"SELECT `name` FROM `font` LEFT JOIN (SELECT `id` FROM `glyph`) AS `sub_glyph` ON `font`.`id` = `sub_glyph`.`id` AND `font`.`id` = `sub_glyph`.`id`"# ); ``` -------------------------------- ### WindowStatement Construction Source: https://docs.rs/sea-query/latest/sea_query/query/struct.WindowStatement.html Demonstrates how to create a new WindowStatement and add partitioning clauses. ```APIDOC ## `WindowStatement::new()` Constructs a new, empty `WindowStatement`. ## `WindowStatement::partition_by(col: T)` Adds a `PARTITION BY` clause to the window statement with the specified column. ## `WindowStatement::partition_by_custom(col: T)` Adds a `PARTITION BY` clause to the window statement with a custom string representation of a column. ``` -------------------------------- ### type_id Source: https://docs.rs/sea-query/latest/sea_query/table/struct.ColumnDef.html Gets the TypeId of the ColumnDef. ```APIDOC ## type_id ### Description Gets the `TypeId` of `self`. ### Method `type_id(&self) -> TypeId` ### Returns The `TypeId` of the `ColumnDef`. ``` -------------------------------- ### Create Basic Table Source: https://docs.rs/sea-query/latest/sea_query/table/struct.TableCreateStatement.html Demonstrates creating a basic table with integer and string columns, and a composite primary key. Supports MySQL, PostgreSQL, and SQLite. ```rust use sea_query::{tests_cfg::*, *}; let mut statement = Table::create(); statement .table(Glyph::Table) .col(ColumnDef::new(Glyph::Id).integer().not_null()) .col(ColumnDef::new(Glyph::Image).string().not_null()) .primary_key(Index::create().col(Glyph::Id).col(Glyph::Image)); assert_eq!( statement.to_string(MysqlQueryBuilder), [ "CREATE TABLE `glyph` (", "`id` int NOT NULL,", "`image` varchar(255) NOT NULL,", "PRIMARY KEY (`id`, `image`)", ")", ] .join(" ") ); assert_eq!( statement.to_string(PostgresQueryBuilder), [ "CREATE TABLE \"glyph\" (", "\"id\" integer NOT NULL,", "\"image\" varchar NOT NULL,", "PRIMARY KEY (\"id\", \"image\")", ")", ] .join(" ") ); assert_eq!( statement.to_string(SqliteQueryBuilder), [ r#"CREATE TABLE \"glyph\" ("#, r#"\"id\" integer NOT NULL,"#, r#"\"image\" varchar NOT NULL,"#, r# ``` ```rust use sea_query::{tests_cfg::*, *}; let mut statement = Table::create(); statement .table(Glyph::Table) .col(ColumnDef::new(Glyph::Id).integer().not_null()) .col(ColumnDef::new(Glyph::Image).string().not_null()) .primary_key(Index::create().col(Glyph::Id).col(Glyph::Image)); assert_eq!( statement.to_string(MysqlQueryBuilder), [ "CREATE TABLE `glyph` (", "`id` int NOT NULL,", "`image` varchar(255) NOT NULL,", "PRIMARY KEY (`id`, `image`)", ")", ] .join(" ") ); assert_eq!( statement.to_string(PostgresQueryBuilder), [ "CREATE TABLE \"glyph\" (", "\"id\" integer NOT NULL,", "\"image\" varchar NOT NULL,", "PRIMARY KEY (\"id\", \"image\")", ")", ] .join(" ") ); assert_eq!( statement.to_string(SqliteQueryBuilder), [ r#"CREATE TABLE \"glyph\" ("#, r#"\"id\" integer NOT NULL,"#, r#"\"image\" varchar NOT NULL,"#, r#"PRIMARY KEY (\"id\", \"image\")"#, r#" )"#, ] .join(" ") ); ``` -------------------------------- ### Add Column to Table Source: https://docs.rs/sea-query/latest/sea_query/table/struct.TableAlterStatement.html Demonstrates adding a new column to an existing table. This example shows the generated SQL for MySQL, PostgreSQL, and SQLite. ```rust use sea_query::{tests_cfg::*, *}; let table = Table::alter() .table(Font::Table) .add_column(ColumnDef::new("new_col").integer().not_null().default(100)) .to_owned(); assert_eq!( table.to_string(MysqlQueryBuilder), r#"ALTER TABLE `font` ADD COLUMN `new_col` int NOT NULL DEFAULT 100"# ); assert_eq!( table.to_string(PostgresQueryBuilder), r#"ALTER TABLE \"font\" ADD COLUMN \"new_col\" integer NOT NULL DEFAULT 100"# ); assert_eq!( table.to_string(SqliteQueryBuilder), r#"ALTER TABLE \"font\" ADD COLUMN \"new_col\" integer NOT NULL DEFAULT 100"#, ); ``` -------------------------------- ### Prepare Table Partition Source: https://docs.rs/sea-query/latest/sea_query/backend/trait.TableBuilder.html Provides a default implementation for translating `TablePartition` information into SQL. This method can be overridden. ```rust fn prepare_table_partition( &self, _table_partition: &TablePartition, _sql: &mut dyn SqlWriter, ) { ... } ``` -------------------------------- ### Get Column Name Source: https://docs.rs/sea-query/latest/sea_query/table/struct.ColumnDef.html Retrieves the name of the column. ```APIDOC ## pub fn get_column_name(&self) -> String ### Description Get the name of the column. ``` -------------------------------- ### ExprTrait::in_subquery Example Source: https://docs.rs/sea-query/latest/sea_query/expr/trait.ExprTrait.html Shows how to create an 'IN' subquery expression. ```rust use sea_query::{*, tests_cfg::*}; let query = Query::select() .columns([Char::Character, Char::SizeW, Char::SizeH]) .from(Char::Table) .and_where(Char::SizeW.into_column_ref().in_subquery( Query::select() .expr(Expr::cust("3 + 2 * 2")) .take() )) .to_owned(); assert_eq!( query.to_string(MysqlQueryBuilder), r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `size_w` IN (SELECT 3 + 2 * 2)"# ); assert_eq!( query.to_string(PostgresQueryBuilder), r#"SELECT \"character\", \"size_w\", \"size_h\" FROM \"character\" WHERE \"size_w\" IN (SELECT 3 + 2 * 2)"# ); assert_eq!( query.to_string(SqliteQueryBuilder), r#"SELECT \"character\", \"size_w\", \"size_h\" FROM \"character\" WHERE \"size_w\" IN (SELECT 3 + 2 * 2)"# ); ``` -------------------------------- ### ExprTrait::gte Example Source: https://docs.rs/sea-query/latest/sea_query/expr/trait.ExprTrait.html Demonstrates creating a 'greater than or equal to' (>=) expression. ```rust use sea_query::{tests_cfg::*, *}; let query = Query::select() .columns([Char::Character, Char::SizeW, Char::SizeH]) .from(Char::Table) .and_where((Char::Table, Char::SizeW).into_column_ref().gte(2)) .to_owned(); assert_eq!( query.to_string(MysqlQueryBuilder), r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `character`.`size_w` >= 2"# ); assert_eq!( query.to_string(PostgresQueryBuilder), r#"SELECT \"character\", \"size_w\", \"size_h\" FROM \"character\" WHERE \"character\".\"size_w\" >= 2"# ); assert_eq!( query.to_string(SqliteQueryBuilder), r#"SELECT \"character\", \"size_w\", \"size_h\" FROM \"character\" WHERE \"character\".\"size_w\" >= 2"# ); ``` -------------------------------- ### Create a Standard Index Source: https://docs.rs/sea-query/latest/sea_query/index/struct.IndexCreateStatement.html Demonstrates the creation of a basic index on a single column. The generated SQL varies slightly between MySQL, PostgreSQL, and SQLite. ```Rust use sea_query::{tests_cfg::*, *}; let index = Index::create() .name("idx-glyph-aspect") .table(Glyph::Table) .col(Glyph::Aspect) .to_owned(); assert_eq!( index.to_string(MysqlQueryBuilder), r#"CREATE INDEX `idx-glyph-aspect` ON `glyph` (`aspect`)"# ); assert_eq!( index.to_string(PostgresQueryBuilder), r#"CREATE INDEX \"idx-glyph-aspect\" ON \"glyph\" (\"aspect\")"# ); assert_eq!( index.to_string(SqliteQueryBuilder), r#"CREATE INDEX \"idx-glyph-aspect\" ON \"glyph\" (\"aspect\")"# ); ``` -------------------------------- ### Func::char_length Source: https://docs.rs/sea-query/latest/sea_query/func/struct.Func.html Calls the CHAR_LENGTH function to get the length of a string. ```APIDOC ## Func::char_length ### Description Calls the CHAR_LENGTH function to get the length of a string. ### Method Signature `pub fn char_length(expr: T) -> FunctionCall` where T: Into ### Example ```rust use sea_query::{tests_cfg::*, *}; let query = Query::select() .expr(Func::char_length(Expr::col((Char::Table, Char::Character)))) .from(Char::Table) .to_owned(); assert_eq!( query.to_string(MysqlQueryBuilder), r#"SELECT CHAR_LENGTH(`character`.`character`) FROM `character`"# ); assert_eq!( query.to_string(PostgresQueryBuilder), r#"SELECT CHAR_LENGTH(\"character\".\"character\") FROM \"character\""# ); assert_eq!( query.to_string(SqliteQueryBuilder), r#"SELECT LENGTH(\"character\".\"character\") FROM \"character\""# ); ``` ``` -------------------------------- ### WithClause Construction and Configuration Source: https://docs.rs/sea-query/latest/sea_query/query/struct.WithClause.html Demonstrates how to create a WithClause, set it as recursive, add CommonTableExpressions (CTEs), and configure cycle detection. ```APIDOC ## WithClause Methods ### `new()` Constructs a new WithClause. ### `recursive(recursive: bool)` Sets whether this clause is a recursive WITH clause. If set to true, it will generate a ‘WITH RECURSIVE’ query. You can only specify a single CommonTableExpression containing a union query if this is set to true. ### `search(search: Search)` For recursive WITH queries, you can specify the Search clause. This setting is not meaningful if the query is not recursive. Some databases don’t support this clause. In that case, this option will be silently ignored. ### `cycle(cycle: Cycle)` For recursive WITH queries, you can specify the Cycle clause. This setting is not meaningful if the query is not recursive. Some databases don’t support this clause. In that case, this option will be silently ignored. ### `cte(cte: CommonTableExpression)` Adds a CommonTableExpression to this with clause. ### `query(query: T)` Turns the WithClause into a WithQuery. The resulting WITH query will execute the argument query with this WITH clause. ``` -------------------------------- ### ExprTrait::gt Example Source: https://docs.rs/sea-query/latest/sea_query/expr/trait.ExprTrait.html Illustrates creating a 'greater than' (>) expression between a column and a value. ```rust use sea_query::{tests_cfg::*, *}; let query = Query::select() .columns([Char::Character, Char::SizeW, Char::SizeH]) .from(Char::Table) .and_where((Char::Table, Char::SizeW).into_column_ref().gt(2)) .to_owned(); assert_eq!( query.to_string(MysqlQueryBuilder), r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `character`.`size_w` > 2"# ); assert_eq!( query.to_string(PostgresQueryBuilder), r#"SELECT \"character\", \"size_w\", \"size_h\" FROM \"character\" WHERE \"character\".\"size_w\" > 2"# ); assert_eq!( query.to_string(SqliteQueryBuilder), r#"SELECT \"character\", \"size_w\", \"size_h\" FROM \"character\" WHERE \"character\".\"size_w\" > 2"# ); ``` -------------------------------- ### Drop Index Source: https://docs.rs/sea-query/latest/sea_query/index.html Removes an index from a table. Examples provided for MySQL, PostgreSQL, and SQLite. ```rust let index = Index::drop() .name("idx-glyph-aspect") .table(Glyph::Table) .to_owned(); assert_eq!( index.to_string(MysqlQueryBuilder), r#"DROP INDEX `idx-glyph-aspect` ON `glyph`"# ); assert_eq!( index.to_string(PostgresQueryBuilder), r#"DROP INDEX \"idx-glyph-aspect\""# ); assert_eq!( index.to_string(SqliteQueryBuilder), r#"DROP INDEX \"idx-glyph-aspect\""# ); ``` -------------------------------- ### CAST AS Function Example Source: https://docs.rs/sea-query/latest/sea_query/func/struct.Func.html Shows how to cast an expression to a custom type using CAST AS. ```rust use sea_query::{tests_cfg::*, *}; let query = Query::select() .expr(Func::cast_as("hello", "MyType")) .to_owned(); assert_eq!( query.to_string(MysqlQueryBuilder), r#"SELECT CAST('hello' AS MyType)" # ); assert_eq!( query.to_string(PostgresQueryBuilder), r#"SELECT CAST('hello' AS MyType)" # ); assert_eq!( query.to_string(SqliteQueryBuilder), r#"SELECT CAST('hello' AS MyType)" # ); ``` -------------------------------- ### Prepare Create Temporary Table Source: https://docs.rs/sea-query/latest/sea_query/backend/trait.TableBuilder.html Provides a default implementation for generating the SQL for creating a temporary table. This method can be overridden. ```rust fn prepare_create_temporary_table( &self, create: &TableCreateStatement, sql: &mut dyn SqlWriter, ) { ... } ``` -------------------------------- ### SELECT with Window Function Source: https://docs.rs/sea-query/latest/sea_query/query/struct.SelectStatement.html Demonstrates how to use window functions in a SELECT statement. Requires specifying the window name, its definition (e.g., PARTITION BY), and aliasing the window function expression. ```rust use sea_query::{tests_cfg::*}; use sea_query::*; let query = Query::select() .from(Char::Table) .expr_window_name_as(Expr::col(Char::Character), "w", "C") .window("w", WindowStatement::partition_by(Char::FontSize)) .to_owned(); assert_eq!( query.to_string(MysqlQueryBuilder), r#"SELECT `character` OVER `w` AS `C` FROM `character` WINDOW `w` AS (PARTITION BY `font_size`)"# ); assert_eq!( query.to_string(PostgresQueryBuilder), r#"SELECT \"character\" OVER \"w\" AS \"C\" FROM \"character\" WINDOW \"w\" AS (PARTITION BY \"font_size\")"# ); assert_eq!( query.to_string(SqliteQueryBuilder), r#"SELECT \"character\" OVER \"w\" AS \"C\" FROM \"character\" WINDOW \"w\" AS (PARTITION BY \"font_size\")"# ); ``` -------------------------------- ### Basic UPDATE Statement with RETURNING Columns Source: https://docs.rs/sea-query/latest/sea_query/query/struct.UpdateStatement.html Constructs a basic UPDATE statement and demonstrates how to specify columns to be returned using the `returning` method. This example shows the generated SQL for MySQL, PostgreSQL, and SQLite. ```rust use sea_query::{tests_cfg::*}; use sea_query::*; let query = Query::update() .table(Glyph::Table) .value(Glyph::Aspect, 2.1345) .value(Glyph::Image, "235m") .returning(Query::returning().columns([Glyph::Id])) .to_owned(); assert_eq!( query.to_string(MysqlQueryBuilder), r#"UPDATE `glyph` SET `aspect` = 2.1345, `image` = '235m'"# ); assert_eq!( query.to_string(PostgresQueryBuilder), r#"UPDATE "glyph" SET "aspect" = 2.1345, "image" = '235m' RETURNING "id""# ); assert_eq!( query.to_string(SqliteQueryBuilder), r#"UPDATE "glyph" SET "aspect" = 2.1345, "image" = '235m' RETURNING "id""# ); ``` -------------------------------- ### RegexCaseInsensitive Operator Example Source: https://docs.rs/sea-query/latest/sea_query/extension/postgres/enum.PgBinOper.html Illustrates the `RegexCaseInsensitive` operator (`~*`) for case-insensitive regular expression matching. ```rust `~*`. Regex operator with case insensitive matching. ``` -------------------------------- ### Create PostgreSQL Extension Statement Source: https://docs.rs/sea-query/latest/sea_query/extension/postgres/struct.ExtensionCreateStatement.html Demonstrates how to construct a "CREATE EXTENSION" statement with various options including name, schema, version, cascade, and if_not_exists. ```rust use sea_query::{extension::postgres::Extension, *}; assert_eq!( Extension::create() .name("ltree") .schema("public") .version("v0.1.0") .cascade() .if_not_exists() .to_string(PostgresQueryBuilder), r#"CREATE EXTENSION IF NOT EXISTS ltree WITH SCHEMA public VERSION v0.1.0 CASCADE"# ); ``` -------------------------------- ### Prepare Create Table If Not Exists Source: https://docs.rs/sea-query/latest/sea_query/backend/trait.TableBuilder.html Provides a default implementation for generating the SQL for creating a table only if it does not already exist. This method can be overridden. ```rust fn prepare_create_table_if_not_exists( &self, create: &TableCreateStatement, sql: &mut dyn SqlWriter, ) { ... } ``` -------------------------------- ### Regex Operator Example Source: https://docs.rs/sea-query/latest/sea_query/extension/postgres/enum.PgBinOper.html Shows the `Regex` operator (`~`) for performing regular expression matching. ```rust `~` Regex operator. ```