Trait quaint::ast::Comparable
source · pub trait Comparable<'a> {
Show 27 methods
// Required methods
fn equals<T>(self, comparison: T) -> Compare<'a>
where T: Into<Expression<'a>>;
fn not_equals<T>(self, comparison: T) -> Compare<'a>
where T: Into<Expression<'a>>;
fn less_than<T>(self, comparison: T) -> Compare<'a>
where T: Into<Expression<'a>>;
fn less_than_or_equals<T>(self, comparison: T) -> Compare<'a>
where T: Into<Expression<'a>>;
fn greater_than<T>(self, comparison: T) -> Compare<'a>
where T: Into<Expression<'a>>;
fn greater_than_or_equals<T>(self, comparison: T) -> Compare<'a>
where T: Into<Expression<'a>>;
fn in_selection<T>(self, selection: T) -> Compare<'a>
where T: Into<Expression<'a>>;
fn not_in_selection<T>(self, selection: T) -> Compare<'a>
where T: Into<Expression<'a>>;
fn like<T>(self, pattern: T) -> Compare<'a>
where T: Into<Expression<'a>>;
fn not_like<T>(self, pattern: T) -> Compare<'a>
where T: Into<Expression<'a>>;
fn is_null(self) -> Compare<'a>;
fn is_not_null(self) -> Compare<'a>;
fn between<T, V>(self, left: T, right: V) -> Compare<'a>
where T: Into<Expression<'a>>,
V: Into<Expression<'a>>;
fn not_between<T, V>(self, left: T, right: V) -> Compare<'a>
where T: Into<Expression<'a>>,
V: Into<Expression<'a>>;
fn json_array_contains<T>(self, item: T) -> Compare<'a>
where T: Into<Expression<'a>>;
fn json_array_not_contains<T>(self, item: T) -> Compare<'a>
where T: Into<Expression<'a>>;
fn json_array_begins_with<T>(self, item: T) -> Compare<'a>
where T: Into<Expression<'a>>;
fn json_array_not_begins_with<T>(self, item: T) -> Compare<'a>
where T: Into<Expression<'a>>;
fn json_array_ends_into<T>(self, item: T) -> Compare<'a>
where T: Into<Expression<'a>>;
fn json_array_not_ends_into<T>(self, item: T) -> Compare<'a>
where T: Into<Expression<'a>>;
fn json_type_equals<T>(self, json_type: T) -> Compare<'a>
where T: Into<JsonType<'a>>;
fn json_type_not_equals<T>(self, json_type: T) -> Compare<'a>
where T: Into<JsonType<'a>>;
fn matches<T>(self, query: T) -> Compare<'a>
where T: Into<Cow<'a, str>>;
fn not_matches<T>(self, query: T) -> Compare<'a>
where T: Into<Cow<'a, str>>;
fn any(self) -> Compare<'a>;
fn all(self) -> Compare<'a>;
fn compare_raw<T, V>(self, raw_comparator: T, right: V) -> Compare<'a>
where T: Into<Cow<'a, str>>,
V: Into<Expression<'a>>;
}
Expand description
An item that can be compared against other values in the database.
Required Methods§
sourcefn equals<T>(self, comparison: T) -> Compare<'a>where
T: Into<Expression<'a>>,
fn equals<T>(self, comparison: T) -> Compare<'a>where T: Into<Expression<'a>>,
Tests if both sides are the same value.
let query = Select::from_table("users").so_that("foo".equals("bar"));
let (sql, params) = Sqlite::build(query)?;
assert_eq!("SELECT `users`.* FROM `users` WHERE `foo` = ?", sql);
assert_eq!(
vec![
Value::from("bar"),
],
params
);
sourcefn not_equals<T>(self, comparison: T) -> Compare<'a>where
T: Into<Expression<'a>>,
fn not_equals<T>(self, comparison: T) -> Compare<'a>where T: Into<Expression<'a>>,
Tests if both sides are not the same value.
let query = Select::from_table("users").so_that("foo".not_equals("bar"));
let (sql, params) = Sqlite::build(query)?;
assert_eq!("SELECT `users`.* FROM `users` WHERE `foo` <> ?", sql);
assert_eq!(
vec![
Value::from("bar"),
],
params
);
sourcefn less_than<T>(self, comparison: T) -> Compare<'a>where
T: Into<Expression<'a>>,
fn less_than<T>(self, comparison: T) -> Compare<'a>where T: Into<Expression<'a>>,
Tests if the left side is smaller than the right side.
let query = Select::from_table("users").so_that("foo".less_than(10));
let (sql, params) = Sqlite::build(query)?;
assert_eq!("SELECT `users`.* FROM `users` WHERE `foo` < ?", sql);
assert_eq!(
vec![
Value::from(10),
],
params
);
sourcefn less_than_or_equals<T>(self, comparison: T) -> Compare<'a>where
T: Into<Expression<'a>>,
fn less_than_or_equals<T>(self, comparison: T) -> Compare<'a>where T: Into<Expression<'a>>,
Tests if the left side is smaller than the right side or the same.
let query = Select::from_table("users").so_that("foo".less_than_or_equals(10));
let (sql, params) = Sqlite::build(query)?;
assert_eq!("SELECT `users`.* FROM `users` WHERE `foo` <= ?", sql);
assert_eq!(
vec![
Value::from(10),
],
params
);
sourcefn greater_than<T>(self, comparison: T) -> Compare<'a>where
T: Into<Expression<'a>>,
fn greater_than<T>(self, comparison: T) -> Compare<'a>where T: Into<Expression<'a>>,
Tests if the left side is bigger than the right side.
let query = Select::from_table("users").so_that("foo".greater_than(10));
let (sql, params) = Sqlite::build(query)?;
assert_eq!("SELECT `users`.* FROM `users` WHERE `foo` > ?", sql);
assert_eq!(
vec![
Value::from(10),
],
params
);
sourcefn greater_than_or_equals<T>(self, comparison: T) -> Compare<'a>where
T: Into<Expression<'a>>,
fn greater_than_or_equals<T>(self, comparison: T) -> Compare<'a>where T: Into<Expression<'a>>,
Tests if the left side is bigger than the right side or the same.
let query = Select::from_table("users").so_that("foo".greater_than_or_equals(10));
let (sql, params) = Sqlite::build(query)?;
assert_eq!("SELECT `users`.* FROM `users` WHERE `foo` >= ?", sql);
assert_eq!(
vec![
Value::from(10),
],
params
);
sourcefn in_selection<T>(self, selection: T) -> Compare<'a>where
T: Into<Expression<'a>>,
fn in_selection<T>(self, selection: T) -> Compare<'a>where T: Into<Expression<'a>>,
Tests if the left side is included in the right side collection.
let query = Select::from_table("users").so_that("foo".in_selection(vec![1, 2]));
let (sql, params) = Sqlite::build(query)?;
assert_eq!("SELECT `users`.* FROM `users` WHERE `foo` IN (?,?)", sql);
assert_eq!(vec![
Value::from(1),
Value::from(2),
], params);
sourcefn not_in_selection<T>(self, selection: T) -> Compare<'a>where
T: Into<Expression<'a>>,
fn not_in_selection<T>(self, selection: T) -> Compare<'a>where T: Into<Expression<'a>>,
Tests if the left side is not included in the right side collection.
let query = Select::from_table("users").so_that("foo".not_in_selection(vec![1, 2]));
let (sql, params) = Sqlite::build(query)?;
assert_eq!("SELECT `users`.* FROM `users` WHERE `foo` NOT IN (?,?)", sql);
assert_eq!(vec![
Value::from(1),
Value::from(2),
], params);
sourcefn like<T>(self, pattern: T) -> Compare<'a>where
T: Into<Expression<'a>>,
fn like<T>(self, pattern: T) -> Compare<'a>where T: Into<Expression<'a>>,
Tests if the left side includes the right side string.
let query = Select::from_table("users").so_that("foo".like("%bar%"));
let (sql, params) = Sqlite::build(query)?;
assert_eq!("SELECT `users`.* FROM `users` WHERE `foo` LIKE ?", sql);
assert_eq!(
vec![
Value::from("%bar%"),
],
params
);
sourcefn not_like<T>(self, pattern: T) -> Compare<'a>where
T: Into<Expression<'a>>,
fn not_like<T>(self, pattern: T) -> Compare<'a>where T: Into<Expression<'a>>,
Tests if the left side does not include the right side string.
let query = Select::from_table("users").so_that("foo".not_like("%bar%"));
let (sql, params) = Sqlite::build(query)?;
assert_eq!("SELECT `users`.* FROM `users` WHERE `foo` NOT LIKE ?", sql);
assert_eq!(
vec![
Value::from("%bar%"),
],
params
);
sourcefn is_null(self) -> Compare<'a>
fn is_null(self) -> Compare<'a>
Tests if the left side is NULL
.
let query = Select::from_table("users").so_that("foo".is_null());
let (sql, _) = Sqlite::build(query)?;
assert_eq!("SELECT `users`.* FROM `users` WHERE `foo` IS NULL", sql);
sourcefn is_not_null(self) -> Compare<'a>
fn is_not_null(self) -> Compare<'a>
Tests if the left side is not NULL
.
let query = Select::from_table("users").so_that("foo".is_not_null());
let (sql, _) = Sqlite::build(query)?;
assert_eq!("SELECT `users`.* FROM `users` WHERE `foo` IS NOT NULL", sql);
sourcefn between<T, V>(self, left: T, right: V) -> Compare<'a>where
T: Into<Expression<'a>>,
V: Into<Expression<'a>>,
fn between<T, V>(self, left: T, right: V) -> Compare<'a>where T: Into<Expression<'a>>, V: Into<Expression<'a>>,
Tests if the value is between two given values.
let query = Select::from_table("users").so_that("foo".between(420, 666));
let (sql, params) = Sqlite::build(query)?;
assert_eq!("SELECT `users`.* FROM `users` WHERE `foo` BETWEEN ? AND ?", sql);
assert_eq!(vec![
Value::from(420),
Value::from(666),
], params);
sourcefn not_between<T, V>(self, left: T, right: V) -> Compare<'a>where
T: Into<Expression<'a>>,
V: Into<Expression<'a>>,
fn not_between<T, V>(self, left: T, right: V) -> Compare<'a>where T: Into<Expression<'a>>, V: Into<Expression<'a>>,
Tests if the value is not between two given values.
let query = Select::from_table("users").so_that("foo".not_between(420, 666));
let (sql, params) = Sqlite::build(query)?;
assert_eq!("SELECT `users`.* FROM `users` WHERE `foo` NOT BETWEEN ? AND ?", sql);
assert_eq!(vec![
Value::from(420),
Value::from(666),
], params);
sourcefn json_array_contains<T>(self, item: T) -> Compare<'a>where
T: Into<Expression<'a>>,
fn json_array_contains<T>(self, item: T) -> Compare<'a>where T: Into<Expression<'a>>,
Tests if the JSON array contains a value.
let query = Select::from_table("users")
.so_that("json".json_array_contains(serde_json::json!(1)));
let (sql, params) = Mysql::build(query)?;
assert_eq!("SELECT `users`.* FROM `users` WHERE JSON_CONTAINS(`json`, ?)", sql);
assert_eq!(vec![Value::from(serde_json::json!(1))], params);
sourcefn json_array_not_contains<T>(self, item: T) -> Compare<'a>where
T: Into<Expression<'a>>,
fn json_array_not_contains<T>(self, item: T) -> Compare<'a>where T: Into<Expression<'a>>,
Tests if the JSON array does not contain a value.
let query = Select::from_table("users")
.so_that("json".json_array_not_contains(serde_json::json!(1)));
let (sql, params) = Mysql::build(query)?;
assert_eq!("SELECT `users`.* FROM `users` WHERE JSON_CONTAINS(`json`, ?) = FALSE", sql);
assert_eq!(vec![Value::from(serde_json::json!(1))], params);
sourcefn json_array_begins_with<T>(self, item: T) -> Compare<'a>where
T: Into<Expression<'a>>,
fn json_array_begins_with<T>(self, item: T) -> Compare<'a>where T: Into<Expression<'a>>,
Tests if the JSON array starts with a value.
let query = Select::from_table("users")
.so_that("json".json_array_begins_with(serde_json::json!(1)));
let (sql, params) = Mysql::build(query)?;
assert_eq!(
"SELECT `users`.* FROM `users` WHERE \
(JSON_CONTAINS(JSON_EXTRACT(`json`, ?), ?) AND \
JSON_CONTAINS(?, JSON_EXTRACT(`json`, ?)))",
sql
);
assert_eq!(vec![
Value::from("$[0]"),
Value::from(serde_json::json!(1)),
Value::from(serde_json::json!(1)),
Value::from("$[0]"),
], params);
sourcefn json_array_not_begins_with<T>(self, item: T) -> Compare<'a>where
T: Into<Expression<'a>>,
fn json_array_not_begins_with<T>(self, item: T) -> Compare<'a>where T: Into<Expression<'a>>,
Tests if the JSON array does not start with a value.
let query = Select::from_table("users")
.so_that("json".json_array_not_begins_with(serde_json::json!(1)));
let (sql, params) = Mysql::build(query)?;
assert_eq!(
"SELECT `users`.* FROM `users` WHERE \
(NOT JSON_CONTAINS(JSON_EXTRACT(`json`, ?), ?) OR \
NOT JSON_CONTAINS(?, JSON_EXTRACT(`json`, ?)))",
sql
);
assert_eq!(vec![
Value::from("$[0]"),
Value::from(serde_json::json!(1)),
Value::from(serde_json::json!(1)),
Value::from("$[0]"),
], params);
sourcefn json_array_ends_into<T>(self, item: T) -> Compare<'a>where
T: Into<Expression<'a>>,
fn json_array_ends_into<T>(self, item: T) -> Compare<'a>where T: Into<Expression<'a>>,
Tests if the JSON array ends with a value.
let query = Select::from_table("users")
.so_that("json".json_array_ends_into(serde_json::json!(1)));
let (sql, params) = Mysql::build(query)?;
assert_eq!(
"SELECT `users`.* FROM `users` WHERE \
(JSON_CONTAINS(JSON_EXTRACT(`json`, CONCAT('$[', JSON_LENGTH(`json`) - 1, ']')), ?) AND \
JSON_CONTAINS(?, JSON_EXTRACT(`json`, CONCAT('$[', JSON_LENGTH(`json`) - 1, ']'))))",
sql
);
assert_eq!(vec![
Value::from(serde_json::json!(1)),
Value::from(serde_json::json!(1)),
], params);
sourcefn json_array_not_ends_into<T>(self, item: T) -> Compare<'a>where
T: Into<Expression<'a>>,
fn json_array_not_ends_into<T>(self, item: T) -> Compare<'a>where T: Into<Expression<'a>>,
Tests if the JSON array does not end with a value.
let query = Select::from_table("users").so_that("json".json_array_not_ends_into(serde_json::json!(1)));
let (sql, params) = Mysql::build(query)?;
assert_eq!(
"SELECT `users`.* FROM `users` WHERE \
(NOT JSON_CONTAINS(JSON_EXTRACT(`json`, CONCAT('$[', JSON_LENGTH(`json`) - 1, ']')), ?) OR \
NOT JSON_CONTAINS(?, JSON_EXTRACT(`json`, CONCAT('$[', JSON_LENGTH(`json`) - 1, ']'))))",
sql
);
assert_eq!(vec![
Value::from(serde_json::json!(1)),
Value::from(serde_json::json!(1)),
], params);
sourcefn json_type_equals<T>(self, json_type: T) -> Compare<'a>where
T: Into<JsonType<'a>>,
fn json_type_equals<T>(self, json_type: T) -> Compare<'a>where T: Into<JsonType<'a>>,
Tests if the JSON value is of a certain type.
let query = Select::from_table("users").so_that("json".json_type_equals(JsonType::Array));
let (sql, params) = Mysql::build(query)?;
assert_eq!("SELECT `users`.* FROM `users` WHERE (JSON_TYPE(`json`) = ?)", sql);
assert_eq!(vec![Value::from("ARRAY")], params);
sourcefn json_type_not_equals<T>(self, json_type: T) -> Compare<'a>where
T: Into<JsonType<'a>>,
fn json_type_not_equals<T>(self, json_type: T) -> Compare<'a>where T: Into<JsonType<'a>>,
Tests if the JSON value is not of a certain type.
let query = Select::from_table("users").so_that("json".json_type_not_equals(JsonType::Array));
let (sql, params) = Mysql::build(query)?;
assert_eq!("SELECT `users`.* FROM `users` WHERE (JSON_TYPE(`json`) != ?)", sql);
assert_eq!(vec![Value::from("ARRAY")], params);
sourcefn matches<T>(self, query: T) -> Compare<'a>where
T: Into<Cow<'a, str>>,
fn matches<T>(self, query: T) -> Compare<'a>where T: Into<Cow<'a, str>>,
Tests if a full-text search matches a certain query. Use it in combination with the text_search()
function
let search: Expression = text_search(&[Column::from("name"), Column::from("ingredients")]).into();
let query = Select::from_table("recipes").so_that(search.matches("chicken"));
let (sql, params) = Postgres::build(query)?;
assert_eq!(
"SELECT \"recipes\".* FROM \"recipes\" \
WHERE to_tsvector(concat_ws(' ', \"name\",\"ingredients\")) @@ to_tsquery($1)", sql
);
assert_eq!(params, vec![Value::from("chicken")]);
sourcefn not_matches<T>(self, query: T) -> Compare<'a>where
T: Into<Cow<'a, str>>,
fn not_matches<T>(self, query: T) -> Compare<'a>where T: Into<Cow<'a, str>>,
Tests if a full-text search does not match a certain query. Use it in combination with the text_search()
function
let search: Expression = text_search(&[Column::from("name"), Column::from("ingredients")]).into();
let query = Select::from_table("recipes").so_that(search.not_matches("chicken"));
let (sql, params) = Postgres::build(query)?;
assert_eq!(
"SELECT \"recipes\".* FROM \"recipes\" \
WHERE (NOT to_tsvector(concat_ws(' ', \"name\",\"ingredients\")) @@ to_tsquery($1))", sql
);
assert_eq!(params, vec![Value::from("chicken")]);
sourcefn any(self) -> Compare<'a>
fn any(self) -> Compare<'a>
Matches at least one elem of a list of values.
let query = Select::from_table("users").so_that(col!("name").equals(col!("list").any()));
let (sql, _) = Postgres::build(query)?;
assert_eq!(r#"SELECT "users".* FROM "users" WHERE "name" = ANY("list")"#, sql);
sourcefn all(self) -> Compare<'a>
fn all(self) -> Compare<'a>
Matches all elem of a list of values.
let query = Select::from_table("users").so_that(col!("name").equals(col!("list").all()));
let (sql, _) = Postgres::build(query)?;
assert_eq!(r#"SELECT "users".* FROM "users" WHERE "name" = ALL("list")"#, sql);
sourcefn compare_raw<T, V>(self, raw_comparator: T, right: V) -> Compare<'a>where
T: Into<Cow<'a, str>>,
V: Into<Expression<'a>>,
fn compare_raw<T, V>(self, raw_comparator: T, right: V) -> Compare<'a>where T: Into<Cow<'a, str>>, V: Into<Expression<'a>>,
Compares two expressions with a custom operator.
let query = Select::from_table("users").so_that("foo".compare_raw("ILIKE", "%bar%"));
let (sql, params) = Sqlite::build(query)?;
assert_eq!("SELECT `users`.* FROM `users` WHERE `foo` ILIKE ?", sql);
assert_eq!(vec![
Value::from("%bar%"),
], params);