Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/backend/postgres/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ mod tests {
for b in 0u8..=31 {
let c = b as char;
if !matches!(c, '\x00' | '\x08' | '\x09' | '\x0A' | '\x0C' | '\x0D') {
let octal = format!("\\{:03o}", b);
let octal = format!("\\{b:03o}");
assert!(escaped.contains(&octal));
}
}
Expand Down
11 changes: 11 additions & 0 deletions src/backend/postgres/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,17 @@ impl QueryBuilder for PostgresQueryBuilder {
PgFunc::Any => "ANY",
PgFunc::Some => "SOME",
PgFunc::All => "ALL",
PgFunc::AdvisoryLock => "PG_ADVISORY_LOCK",
PgFunc::AdvisoryLockShared => "PG_ADVISORY_LOCK_SHARED",
PgFunc::TryAdvisoryLock => "PG_TRY_ADVISORY_LOCK",
PgFunc::TryAdvisoryLockShared => "PG_TRY_ADVISORY_LOCK_SHARED",
PgFunc::AdvisoryUnlock => "PG_ADVISORY_UNLOCK",
PgFunc::AdvisoryUnlockShared => "PG_ADVISORY_UNLOCK_SHARED",
PgFunc::AdvisoryUnlockAll => "PG_ADVISORY_UNLOCK_ALL",
PgFunc::AdvisoryXactLock => "PG_ADVISORY_XACT_LOCK",
PgFunc::AdvisoryXactLockShared => "PG_ADVISORY_XACT_LOCK_SHARED",
PgFunc::TryAdvisoryXactLock => "PG_TRY_ADVISORY_XACT_LOCK",
PgFunc::TryAdvisoryXactLockShared => "PG_TRY_ADVISORY_XACT_LOCK_SHARED",
})
.unwrap(),
_ => self.prepare_function_name_common(function, sql),
Expand Down
261 changes: 261 additions & 0 deletions src/extension/postgres/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ pub enum PgFunc {
Any,
Some,
All,
AdvisoryLock,
AdvisoryLockShared,
TryAdvisoryLock,
TryAdvisoryLockShared,
AdvisoryUnlock,
AdvisoryUnlockShared,
AdvisoryUnlockAll,
AdvisoryXactLock,
AdvisoryXactLockShared,
TryAdvisoryXactLock,
TryAdvisoryXactLockShared,
}

impl From<PgFunc> for Func {
Expand Down Expand Up @@ -522,4 +533,254 @@ impl PgFunc {
{
FunctionCall::new(PgFunc::ArrayAgg).arg_with(expr, FuncArgMod { distinct: true })
}

/// Call `PG_ADVISORY_LOCK` function. Postgres only.
///
/// # Examples
///
/// ```
/// use sea_query::{tests_cfg::*, *};
///
/// let query = Query::select()
/// .expr(PgFunc::advisory_lock(Expr::val(12345_i64)))
/// .to_owned();
///
/// assert_eq!(
/// query.to_string(PostgresQueryBuilder),
/// r#"SELECT PG_ADVISORY_LOCK(12345)"#
/// );
/// ```
pub fn advisory_lock<T>(key: T) -> FunctionCall
where
T: Into<Expr>,
{
FunctionCall::new(PgFunc::AdvisoryLock).arg(key)
}

/// Call `PG_ADVISORY_LOCK_SHARED` function. Postgres only.
///
/// # Examples
///
/// ```
/// use sea_query::{tests_cfg::*, *};
///
/// let query = Query::select()
/// .expr(PgFunc::advisory_lock_shared(Expr::val(12345_i64)))
/// .to_owned();
///
/// assert_eq!(
/// query.to_string(PostgresQueryBuilder),
/// r#"SELECT PG_ADVISORY_LOCK_SHARED(12345)"#
/// );
/// ```
pub fn advisory_lock_shared<T>(key: T) -> FunctionCall
where
T: Into<Expr>,
{
FunctionCall::new(PgFunc::AdvisoryLockShared).arg(key)
}

/// Call `PG_TRY_ADVISORY_LOCK` function. Postgres only.
///
/// # Examples
///
/// ```
/// use sea_query::{tests_cfg::*, *};
///
/// let query = Query::select()
/// .expr(PgFunc::try_advisory_lock(Expr::val(12345_i64)))
/// .to_owned();
///
/// assert_eq!(
/// query.to_string(PostgresQueryBuilder),
/// r#"SELECT PG_TRY_ADVISORY_LOCK(12345)"#
/// );
/// ```
pub fn try_advisory_lock<T>(key: T) -> FunctionCall
where
T: Into<Expr>,
{
FunctionCall::new(PgFunc::TryAdvisoryLock).arg(key)
}

/// Call `PG_TRY_ADVISORY_LOCK_SHARED` function. Postgres only.
///
/// # Examples
///
/// ```
/// use sea_query::{tests_cfg::*, *};
///
/// let query = Query::select()
/// .expr(PgFunc::try_advisory_lock_shared(Expr::val(12345_i64)))
/// .to_owned();
///
/// assert_eq!(
/// query.to_string(PostgresQueryBuilder),
/// r#"SELECT PG_TRY_ADVISORY_LOCK_SHARED(12345)"#
/// );
/// ```
pub fn try_advisory_lock_shared<T>(key: T) -> FunctionCall
where
T: Into<Expr>,
{
FunctionCall::new(PgFunc::TryAdvisoryLockShared).arg(key)
}

/// Call `PG_ADVISORY_UNLOCK` function. Postgres only.
///
/// # Examples
///
/// ```
/// use sea_query::{tests_cfg::*, *};
///
/// let query = Query::select()
/// .expr(PgFunc::advisory_unlock(Expr::val(12345_i64)))
/// .to_owned();
///
/// assert_eq!(
/// query.to_string(PostgresQueryBuilder),
/// r#"SELECT PG_ADVISORY_UNLOCK(12345)"#
/// );
/// ```
pub fn advisory_unlock<T>(key: T) -> FunctionCall
where
T: Into<Expr>,
{
FunctionCall::new(PgFunc::AdvisoryUnlock).arg(key)
}

/// Call `PG_ADVISORY_UNLOCK_SHARED` function. Postgres only.
///
/// # Examples
///
/// ```
/// use sea_query::{tests_cfg::*, *};
///
/// let query = Query::select()
/// .expr(PgFunc::advisory_unlock_shared(Expr::val(12345_i64)))
/// .to_owned();
///
/// assert_eq!(
/// query.to_string(PostgresQueryBuilder),
/// r#"SELECT PG_ADVISORY_UNLOCK_SHARED(12345)"#
/// );
/// ```
pub fn advisory_unlock_shared<T>(key: T) -> FunctionCall
where
T: Into<Expr>,
{
FunctionCall::new(PgFunc::AdvisoryUnlockShared).arg(key)
}

/// Call `PG_ADVISORY_UNLOCK_ALL` function. Postgres only.
///
/// # Examples
///
/// ```
/// use sea_query::{tests_cfg::*, *};
///
/// let query = Query::select()
/// .expr(PgFunc::advisory_unlock_all())
/// .to_owned();
///
/// assert_eq!(
/// query.to_string(PostgresQueryBuilder),
/// r#"SELECT PG_ADVISORY_UNLOCK_ALL()"#
/// );
/// ```
pub fn advisory_unlock_all() -> FunctionCall {
FunctionCall::new(PgFunc::AdvisoryUnlockAll)
}

/// Call `PG_ADVISORY_XACT_LOCK` function. Postgres only.
///
/// # Examples
///
/// ```
/// use sea_query::{tests_cfg::*, *};
///
/// let query = Query::select()
/// .expr(PgFunc::advisory_xact_lock(Expr::val(12345_i64)))
/// .to_owned();
///
/// assert_eq!(
/// query.to_string(PostgresQueryBuilder),
/// r#"SELECT PG_ADVISORY_XACT_LOCK(12345)"#
/// );
/// ```
pub fn advisory_xact_lock<T>(key: T) -> FunctionCall
where
T: Into<Expr>,
{
FunctionCall::new(PgFunc::AdvisoryXactLock).arg(key)
}

/// Call `PG_ADVISORY_XACT_LOCK_SHARED` function. Postgres only.
///
/// # Examples
///
/// ```
/// use sea_query::{tests_cfg::*, *};
///
/// let query = Query::select()
/// .expr(PgFunc::advisory_xact_lock_shared(Expr::val(12345_i64)))
/// .to_owned();
///
/// assert_eq!(
/// query.to_string(PostgresQueryBuilder),
/// r#"SELECT PG_ADVISORY_XACT_LOCK_SHARED(12345)"#
/// );
/// ```
pub fn advisory_xact_lock_shared<T>(key: T) -> FunctionCall
where
T: Into<Expr>,
{
FunctionCall::new(PgFunc::AdvisoryXactLockShared).arg(key)
}

/// Call `PG_TRY_ADVISORY_XACT_LOCK` function. Postgres only.
///
/// # Examples
///
/// ```
/// use sea_query::{tests_cfg::*, *};
///
/// let query = Query::select()
/// .expr(PgFunc::try_advisory_xact_lock(Expr::val(12345_i64)))
/// .to_owned();
///
/// assert_eq!(
/// query.to_string(PostgresQueryBuilder),
/// r#"SELECT PG_TRY_ADVISORY_XACT_LOCK(12345)"#
/// );
/// ```
pub fn try_advisory_xact_lock<T>(key: T) -> FunctionCall
where
T: Into<Expr>,
{
FunctionCall::new(PgFunc::TryAdvisoryXactLock).arg(key)
}

/// Call `PG_TRY_ADVISORY_XACT_LOCK_SHARED` function. Postgres only.
///
/// # Examples
///
/// ```
/// use sea_query::{tests_cfg::*, *};
///
/// let query = Query::select()
/// .expr(PgFunc::try_advisory_xact_lock_shared(Expr::val(12345_i64)))
/// .to_owned();
///
/// assert_eq!(
/// query.to_string(PostgresQueryBuilder),
/// r#"SELECT PG_TRY_ADVISORY_XACT_LOCK_SHARED(12345)"#
/// );
/// ```
pub fn try_advisory_xact_lock_shared<T>(key: T) -> FunctionCall
where
T: Into<Expr>,
{
FunctionCall::new(PgFunc::TryAdvisoryXactLockShared).arg(key)
}
}
10 changes: 5 additions & 5 deletions src/raw_sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ mod test {
#[test]
fn test_raw_sql_1() {
let a = 1;
let b = vec![2i32, 3];
let b = [2i32, 3];
let c = [4i32, 5, 6];

let mut builder = RawSqlQueryBuilder::new(PostgresQueryBuilder);
Expand All @@ -90,18 +90,18 @@ mod test {
.push_fragment(" ")
.push_parameters(1)
.push_fragment(", ")
.push_parameters((&b).len())
.push_parameters(b.len())
.push_fragment(", ")
.push_parameters((&c).len());
.push_parameters(c.len());

assert_eq!(builder.finish(), "SELECT $1, $2, $3, $4, $5, $6");

let mut values = Values::default();
values.bind(a);
for v in (&b).iter() {
for v in b.iter() {
values.bind(v);
}
for v in (&c).iter() {
for v in c.iter() {
values.bind(v);
}

Expand Down
4 changes: 3 additions & 1 deletion tests/mysql/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1092,7 +1092,9 @@ fn insert_4() {
Query::insert()
.into_table(Glyph::Table)
.columns([Glyph::Image])
.values_panic([chrono::NaiveDateTime::from_timestamp_opt(0, 0)
.values_panic([chrono::NaiveDate::from_ymd_opt(1970, 1, 1)
.unwrap()
.and_hms_opt(0, 0, 0)
.unwrap()
.into()])
.to_string(MysqlQueryBuilder),
Expand Down
Loading
Loading