tideorm-sql-builder 0.3.0

SQL builder for TideORM
Documentation
use super::*;

#[test]
fn update_1() {
    assert_eq!(
        Query::update()
            .table("glyph")
            .values([
                ("aspect", 2.1345.into()),
                ("image", "24B0E11951B03B07F8300FD003983F03F0780060".into()),
            ])
            .and_where(Expr::col("id").eq(1))
            .order_by("id", Order::Asc)
            .limit(1)
            .to_string(MysqlQueryBuilder)
            .unwrap(),
        "UPDATE `glyph` SET `aspect` = 2.1345, `image` = '24B0E11951B03B07F8300FD003983F03F0780060' WHERE `id` = 1 ORDER BY `id` ASC LIMIT 1"
    );
}

#[test]
fn update_3() {
    assert_eq!(
        Query::update()
            .table("glyph")
            .value("aspect", Expr::cust("60 * 24 * 24"))
            .values([("image", "24B0E11951B03B07F8300FD003983F03F0780060".into()),])
            .and_where(Expr::col("id").eq(1))
            .order_by("id", Order::Asc)
            .limit(1)
            .to_string(MysqlQueryBuilder)
            .unwrap(),
        "UPDATE `glyph` SET `aspect` = 60 * 24 * 24, `image` = '24B0E11951B03B07F8300FD003983F03F0780060' WHERE `id` = 1 ORDER BY `id` ASC LIMIT 1"
    );
}

#[test]
fn update_4() {
    assert_eq!(
        Query::update()
            .table("glyph")
            .value("aspect", Expr::col("aspect").add(1))
            .values([("image", "24B0E11951B03B07F8300FD003983F03F0780060".into()),])
            .and_where(Expr::col("id").eq(1))
            .order_by("id", Order::Asc)
            .limit(1)
            .to_string(MysqlQueryBuilder)
            .unwrap(),
        "UPDATE `glyph` SET `aspect` = `aspect` + 1, `image` = '24B0E11951B03B07F8300FD003983F03F0780060' WHERE `id` = 1 ORDER BY `id` ASC LIMIT 1"
    );
}

#[test]
fn update_from_two_tables_joins_both() {
    assert_eq!(
        Query::update()
            .table("glyph")
            .value("tokens", Expr::column(("character", "character")))
            .from("character")
            .from("font")
            .cond_where(
                Expr::col(("glyph", "image"))
                    .eq(Expr::col(("character", "user_data")))
                    .and(Expr::col(("character", "font_id")).eq(Expr::col(("font", "id")))),
            )
            .to_string(MysqlQueryBuilder)
            .unwrap(),
        "UPDATE `glyph` JOIN `character` JOIN `font` ON `glyph`.`image` = `character`.`user_data` AND `character`.`font_id` = `font`.`id` SET `glyph`.`tokens` = `character`.`character`"
    );
}

#[test]
fn a_joined_update_is_ordered_and_limited_on_mariadb_only() {
    let update = Query::update()
        .table("glyph")
        .value("tokens", Expr::column(("character", "character")))
        .from("character")
        .cond_where(Expr::col(("glyph", "id")).eq(Expr::col(("character", "id"))))
        .order_by(("glyph", "id"), Order::Desc)
        .limit(1)
        .to_owned();
    assert_eq!(
        update.to_string(MariaDbDialect).unwrap(),
        "UPDATE `glyph` JOIN `character` ON `glyph`.`id` = `character`.`id` SET `glyph`.`tokens` = `character`.`character` ORDER BY `glyph`.`id` DESC LIMIT 1"
    );
    assert_eq!(
        update.to_string(MysqlQueryBuilder).unwrap_err(),
        RenderError::Unsupported(
            "MySQL has no ORDER BY or LIMIT in an UPDATE that joins other tables".into()
        )
    );
    // Either clause alone is refused too, and a joined UPDATE without them renders.
    let joined = Query::update()
        .table("glyph")
        .value("tokens", Expr::column(("character", "character")))
        .from("character")
        .cond_where(Expr::col(("glyph", "id")).eq(Expr::col(("character", "id"))))
        .to_owned();
    for update in [
        joined
            .clone()
            .order_by(("glyph", "id"), Order::Desc)
            .to_owned(),
        joined.clone().limit(1).to_owned(),
    ] {
        assert!(update.to_string(MysqlQueryBuilder).is_err());
        assert!(update.to_string(MariaDbDialect).is_ok());
    }
    assert!(joined.to_string(MysqlQueryBuilder).is_ok());
}

#[test]
fn mariadb_returns_from_a_single_table_update_only() {
    let update = Query::update()
        .table("glyph")
        .value("aspect", 1)
        .and_where(Expr::col(("glyph", "id")).eq(1))
        .to_owned();
    assert_eq!(
        update
            .clone()
            .returning_col("id")
            .to_string(MariaDbDialect)
            .unwrap(),
        "UPDATE `glyph` SET `aspect` = 1 WHERE `glyph`.`id` = 1 RETURNING `id`"
    );
    let mut joined = update;
    joined.from("character");
    assert!(joined.to_string(MariaDbDialect).is_ok());
    let mut joined_returning = joined;
    joined_returning.returning_col(("glyph", "id"));
    assert_eq!(
        joined_returning.to_string(MariaDbDialect).unwrap_err(),
        RenderError::Unsupported(
            "MariaDB has no RETURNING in an UPDATE that joins other tables".into()
        )
    );
    assert_eq!(
        joined_returning.to_string(MysqlQueryBuilder).unwrap_err(),
        RenderError::Unsupported("MySQL has no RETURNING".into())
    );
    // PostgreSQL returns from an UPDATE .. FROM.
    assert!(
        joined_returning
            .to_string(PostgresQueryBuilder)
            .unwrap()
            .ends_with(r#"RETURNING "glyph"."id""#)
    );
}

#[test]
fn update_from_names_an_aliased_or_qualified_target() {
    let update = |target: TableRef, qualifier: &str| {
        Query::update()
            .table(target)
            .value("tokens", Expr::column(("character", "character")))
            .from("character")
            .cond_where(Expr::col(("character", "id")).eq(1))
            .to_string(MysqlQueryBuilder)
            .unwrap()
            .ends_with(&format!(
                "SET {qualifier}.`tokens` = `character`.`character`"
            ))
    };
    assert!(update(
        ("library", "glyph").into_table_ref(),
        "`library`.`glyph`"
    ));
    assert!(update("glyph".into_table_ref().alias("g"), "`g`"));
}