Seaquery 0.28.0的新功能
#showdev #database #rust #githunt

ð我们很高兴发布Seaquery koude0!这是一些功能亮点ð:

静态标识符的新的IdenStatic特征

[#508]代表带有&'static str的标识符。 IdenStatic特征看起来像这样:

pub trait IdenStatic: Iden + Copy + 'static {
    fn as_str(&self) -> &'static str;
}

您可以轻松地为现有的Iden得出它。只是将#[derive(Iden)]更改为#[derive(IdenStatic)]

#[derive(IdenStatic)]
enum User {
    Table,
    Id,
    FirstName,
    LastName,
    #[iden = "_email"]
    Email,
}

assert_eq!(User::Email.as_str(), "_email");

新的PgExprSqliteExpr特征用于后端特定表达

[#519] Postgres特定和SQLite特定表达式正在移动到其相应的性状中。您需要在使用这些后端特定方法构造表达式之前将特征导入范围。

// Importing `PgExpr` trait before constructing Postgres expression
use sea_query::{extension::postgres::PgExpr, tests_cfg::*, *};

let query = Query::select()
    .columns([Font::Name, Font::Variant, Font::Language])
    .from(Font::Table)
    .and_where(Expr::val("a").concatenate("b").concat("c").concat("d"))
    .to_owned();

assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"SELECT "name", "variant", "language" FROM "font" WHERE 'a' || 'b' || 'c' || 'd'"#
);
// Importing `SqliteExpr` trait before constructing SQLite expression
 use sea_query::{extension::sqlite::SqliteExpr, tests_cfg::*, *};

 let query = Query::select()
    .column(Font::Name)
    .from(Font::Table)
    .and_where(Expr::col(Font::Name).matches("a"))
    .to_owned();

 assert_eq!(
    query.to_string(SqliteQueryBuilder),
    r#"SELECT "name" FROM "font" WHERE "name" MATCH 'a'"#
 );

Bug修复

// given
let (statement, values) = sea_query::Query::select()
    .column(Glyph::Id)
    .from(Glyph::Table)
    .cond_where(Cond::any()
        .add(Cond::all()) // empty all() => TRUE
        .add(Cond::any()) // empty any() => FALSE
    )
    .build(sea_query::MysqlQueryBuilder);

// old behavior
assert_eq!(statement, r#"SELECT `id` FROM `glyph`"#);

// new behavior
assert_eq!(
    statement,
    r#"SELECT `id` FROM `glyph` WHERE (TRUE) OR (FALSE)"#
);

// a complex example
let (statement, values) = Query::select()
    .column(Glyph::Id)
    .from(Glyph::Table)
    .cond_where(
        Cond::all()
            .add(Cond::all().not())
            .add(Cond::any().not())
            .not(),
    )
    .build(MysqlQueryBuilder);

assert_eq!(
    statement,
    r#"SELECT `id` FROM `glyph` WHERE NOT ((NOT TRUE) AND (NOT FALSE))"#
);

打破变化

  • [#535] MSRV高达1.62
# Make sure you're running SeaQuery with Rust 1.62+ 🦀
$ rustup update
  • [#492] ColumnType::Array定义从Array(SeaRc<Box<ColumnType>>)更改为Array(SeaRc<ColumnType>)1111
  • [#475] Func::*现在返回FunctionCall而不是SimpleExpr
  • [#475] Func::coalesce现在接受IntoIterator<Item = SimpleExpr>而不是IntoIterator<Item = Into<SimpleExpr>
  • [#475]删除了Expr::argExpr::args-不再需要这些功能
  • [#507]将所有Postgres特定的操作员移至PgBinOper
  • [#476] Expr用于接受Into<Value>的方法现在接受Into<SimpleExpr>
  • [#476] Expr::is_inExpr::is_not_in现在接受Into<SimpleExpr>而不是Into<Value>并将其转换为SimpleExpr::Tuple而不是SimpleExpr::Values
  • [#475] Expr::expr现在接受Into<SimpleExpr>而不是SimpleExpr
  • [#519]将Postgres特定的Expr方法移至新特征PgExpr
  • [#528] Expr::equals现在接受C: IntoColumnRef而不是T: IntoIden, C: IntoIden
use sea_query::{*, tests_cfg::*};

let query = Query::select()
    .columns([Char::Character, Char::SizeW, Char::SizeH])
    .from(Char::Table)
    .and_where(
        Expr::col((Char::Table, Char::FontId))
-           .equals(Font::Table, Font::Id)
+           .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`"#
);
  • [#525]删除了整数和日期时间列类型的显示长度 /精度选项< / li>

API添加

  • [#475]添加了SelectStatement::from_function
use sea_query::{tests_cfg::*, *};

let query = Query::select()
    .column(ColumnRef::Asterisk)
    .from_function(Func::random(), Alias::new("func"))
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"SELECT * FROM RAND() AS `func`"#
);
  • [#486]添加了Postgres pg_trgm扩展的二进制运算符
use sea_query::extension::postgres::PgBinOper;

assert_eq!(
    Query::select()
        .expr(Expr::col(Font::Name).binary(PgBinOper::WordSimilarity, Expr::value("serif")))
        .from(Font::Table)
        .to_string(PostgresQueryBuilder),
    r#"SELECT "name" <% 'serif' FROM "font""#
);
  • [#473]添加了ILIKENOT ILIKE操作员
  • [#510]添加了muldiv SimpleExpr的方法
  • [#513] Sqlite添加了MATCH->->>操作员
use sea_query::extension::sqlite::SqliteBinOper;

assert_eq!(
    Query::select()
        .column(Char::Character)
        .from(Char::Table)
        .and_where(Expr::col(Char::Character).binary(SqliteBinOper::Match, Expr::val("test")))
        .build(SqliteQueryBuilder),
    (
        r#"SELECT "character" FROM "character" WHERE "character" MATCH ?"#.to_owned(),
        Values(vec!["test".into()])
    )
);
  • [#497]添加了FULL OUTER JOIN
  • [#530]添加了PgFunc::get_random_uuid
  • [#528]添加了SimpleExpr::eqSimpleExpr::neExpr::not_equals
  • [#529]添加了PgFunc::starts_with
  • [#535]添加了Expr::custom_keywordSimpleExpr::not
use sea_query::*;

let query = Query::select()
    .expr(Expr::custom_keyword(Alias::new("test")))
    .to_owned();

assert_eq!(query.to_string(MysqlQueryBuilder), r#"SELECT test"#);
assert_eq!(query.to_string(PostgresQueryBuilder), r#"SELECT test"#);
assert_eq!(query.to_string(SqliteQueryBuilder), r#"SELECT test"#);
  • [#539]添加了SimpleExpr::likeSimpleExpr::not_likeExpr::cast_as
  • [#532]添加了对Postgres的NULLS NOT DISTINCT条款的支持
  • [#531]添加了Expr::cust_with_exprExpr::cust_with_exprs
use sea_query::{tests_cfg::*, *};

let query = Query::select()
    .expr(Expr::cust_with_expr("data @? ($1::JSONPATH)", "hello"))
    .to_owned();

assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"SELECT data @? ('hello'::JSONPATH)"#
);
  • [#538]增加了将&String转换为Value
  • 的支持

杂项增强

  • [#475]保持功能和参数的新结构FunctionCall
  • [#503]支持BigDecimalIpNetworkMacAddress for sea-query-postgres
  • [#511]使value::with_array模块公开,因此将NotU8特质公开
  • [#524]删除SizedSchemaBuilders实施者的要求

集成示例

Seaquery与Rust生态系统中的其他板条箱一起运行得很好。

社区

SEAQL是一个社区驱动的项目。我们欢迎您参加,为Rust的未来做出贡献和共同建立。