@@ -9,6 +9,9 @@ use regex::Regex;
9
9
use sea_query:: { Cond , Condition , Expr , IntoColumnRef , IntoLikeExpr , LikeExpr } ;
10
10
use std:: sync:: LazyLock ;
11
11
12
+ #[ cfg( feature = "with-sea-orm" ) ]
13
+ use sea_orm:: ColumnTrait ;
14
+
12
15
/// Represents a keyword used for `LIKE` search with different matching types.
13
16
#[ derive( Debug , Clone , Eq , PartialEq ) ]
14
17
pub struct Keyword {
@@ -99,6 +102,10 @@ static SEPARATOR_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\p{Zs}++
99
102
/// ),
100
103
/// );
101
104
/// ```
105
+ ///
106
+ /// # Examples (`with-sea-orm`)
107
+ ///
108
+ /// See [`fuzzy_separated`] examples.
102
109
pub fn prefix < T : Into < String > > ( text : T ) -> Keyword {
103
110
Keyword {
104
111
ty : KeywordType :: Prefix ,
@@ -163,6 +170,10 @@ pub fn prefix<T: Into<String>>(text: T) -> Keyword {
163
170
/// ),
164
171
/// );
165
172
/// ```
173
+ ///
174
+ /// # Examples (`with-sea-orm`)
175
+ ///
176
+ /// See [`fuzzy_separated`] examples.
166
177
pub fn suffix < T : Into < String > > ( text : T ) -> Keyword {
167
178
Keyword {
168
179
ty : KeywordType :: Suffix ,
@@ -229,6 +240,10 @@ pub fn suffix<T: Into<String>>(text: T) -> Keyword {
229
240
/// ),
230
241
/// );
231
242
/// ```
243
+ ///
244
+ /// # Examples (`with-sea-orm`)
245
+ ///
246
+ /// See [`fuzzy_separated`] examples.
232
247
pub fn fuzzy < T : Into < String > > ( text : T ) -> Keyword {
233
248
Keyword {
234
249
ty : KeywordType :: Fuzzy ,
@@ -306,6 +321,83 @@ pub fn fuzzy<T: Into<String>>(text: T) -> Keyword {
306
321
/// ),
307
322
/// );
308
323
/// ```
324
+ ///
325
+ /// # Examples (`with-sea-orm`)
326
+ ///
327
+ /// ```
328
+ /// use sea_query::all;
329
+ /// use sea_query_common_like::fuzzy_separated;
330
+ /// use sqlformat::{format, FormatOptions, QueryParams};
331
+ ///
332
+ /// #[cfg(feature = "with-sea-orm")]
333
+ /// use sea_orm::{ColumnTrait, DbBackend, EntityTrait, QueryFilter, QueryTrait};
334
+ ///
335
+ /// #[cfg(feature = "with-sea-orm")]
336
+ /// mod book {
337
+ /// use sea_orm::entity::prelude::*;
338
+ ///
339
+ /// #[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
340
+ /// #[sea_orm(schema_name = "book", table_name = "books")]
341
+ /// pub struct Model {
342
+ /// #[sea_orm(primary_key, auto_increment = false)]
343
+ /// pub id: Uuid,
344
+ /// #[sea_orm(column_type = "Text")]
345
+ /// pub title: String,
346
+ /// #[sea_orm(column_type = "Text")]
347
+ /// pub author: String,
348
+ /// pub deleted_at: Option<DateTimeWithTimeZone>,
349
+ /// }
350
+ ///
351
+ /// #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
352
+ /// pub enum Relation {
353
+ /// }
354
+ ///
355
+ /// impl ActiveModelBehavior for ActiveModel {}
356
+ /// }
357
+ ///
358
+ /// #[cfg(feature = "with-sea-orm")]
359
+ /// assert_eq!(
360
+ /// format(
361
+ /// book::Entity::find()
362
+ /// .filter(all![
363
+ /// fuzzy_separated("1% 99% Edison").into_condition_for_orm_columns([book::Column::Title, book::Column::Author]),
364
+ /// book::Column::DeletedAt.is_null(),
365
+ /// ])
366
+ /// .build(DbBackend::Postgres)
367
+ /// .to_string()
368
+ /// .as_str(),
369
+ /// &QueryParams::default(),
370
+ /// FormatOptions::default(),
371
+ /// ),
372
+ /// format(
373
+ /// r#"
374
+ /// SELECT
375
+ /// "books"."id",
376
+ /// "books"."title",
377
+ /// "books"."author",
378
+ /// "books"."deleted_at"
379
+ /// FROM
380
+ /// "book"."books"
381
+ /// WHERE
382
+ /// (
383
+ /// "books"."title" LIKE '%1!%%' ESCAPE '!'
384
+ /// OR "books"."author" LIKE '%1!%%' ESCAPE '!'
385
+ /// )
386
+ /// AND (
387
+ /// "books"."title" LIKE '%99!%%' ESCAPE '!'
388
+ /// OR "books"."author" LIKE '%99!%%' ESCAPE '!'
389
+ /// )
390
+ /// AND (
391
+ /// "books"."title" LIKE '%Edison%' ESCAPE '!'
392
+ /// OR "books"."author" LIKE '%Edison%' ESCAPE '!'
393
+ /// )
394
+ /// AND "books"."deleted_at" IS NULL
395
+ /// "#,
396
+ /// &QueryParams::default(),
397
+ /// FormatOptions::default(),
398
+ /// ),
399
+ /// );
400
+ /// ```
309
401
pub fn fuzzy_separated < T : Into < String > > ( text : T ) -> Keywords {
310
402
keywords (
311
403
SEPARATOR_REGEX
@@ -382,6 +474,10 @@ pub fn fuzzy_separated<T: Into<String>>(text: T) -> Keywords {
382
474
/// ),
383
475
/// );
384
476
/// ```
477
+ ///
478
+ /// # Examples (`with-sea-orm`)
479
+ ///
480
+ /// See [`fuzzy_separated`] examples.
385
481
pub fn keywords < T : Into < Keyword > , Iter : IntoIterator < Item = T > > ( texts : Iter ) -> Keywords {
386
482
Keywords ( texts. into_iter ( ) . map ( Into :: into) . collect ( ) )
387
483
}
@@ -436,6 +532,25 @@ impl Keyword {
436
532
. map ( |col| Expr :: col ( col) . like ( self . clone ( ) ) )
437
533
. fold ( Cond :: all ( ) , Cond :: add)
438
534
}
535
+
536
+ /// Generate a [`Condition`] for a single column with the `LIKE` pattern using a fully-qualified column name with `sea-orm`.
537
+ #[ cfg( feature = "with-sea-orm" ) ]
538
+ pub fn into_condition_for_orm_column < C > ( self , column : C ) -> Condition
539
+ where
540
+ C : ColumnTrait ,
541
+ {
542
+ self . into_condition_for_orm_columns ( [ column] )
543
+ }
544
+
545
+ /// Generate a [`Condition`] for multiple columns with the `LIKE` pattern using fully-qualified column names with `sea-orm`.
546
+ #[ cfg( feature = "with-sea-orm" ) ]
547
+ pub fn into_condition_for_orm_columns < C , Iter > ( self , columns : Iter ) -> Condition
548
+ where
549
+ C : ColumnTrait ,
550
+ Iter : IntoIterator < Item = C > ,
551
+ {
552
+ self . into_condition_for_columns ( columns. into_iter ( ) . map ( |col| col. as_column_ref ( ) ) )
553
+ }
439
554
}
440
555
441
556
/// Methods for converting [`Keywords`] into [`sea_query::Condition`] for a single or multiple columns.
@@ -461,4 +576,23 @@ impl Keywords {
461
576
} )
462
577
. fold ( Cond :: all ( ) , Cond :: add)
463
578
}
579
+
580
+ /// Generate a [`Condition`] for a single column with multiple `LIKE` patterns using a fully-qualified column name with `sea-orm`.
581
+ #[ cfg( feature = "with-sea-orm" ) ]
582
+ pub fn into_condition_for_orm_column < C > ( self , column : C ) -> Condition
583
+ where
584
+ C : ColumnTrait ,
585
+ {
586
+ self . into_condition_for_orm_columns ( [ column] )
587
+ }
588
+
589
+ /// Generate a [`Condition`] for multiple columns with multiple `LIKE` patterns using fully-qualified column names with `sea-orm`.
590
+ #[ cfg( feature = "with-sea-orm" ) ]
591
+ pub fn into_condition_for_orm_columns < C , Iter > ( self , columns : Iter ) -> Condition
592
+ where
593
+ C : ColumnTrait ,
594
+ Iter : IntoIterator < Item = C > ,
595
+ {
596
+ self . into_condition_for_columns ( columns. into_iter ( ) . map ( |col| col. as_column_ref ( ) ) )
597
+ }
464
598
}
0 commit comments