Skip to content

Commit

Permalink
Merge branch 'master' into range_is_contained_by
Browse files Browse the repository at this point in the history
  • Loading branch information
weiznich authored Jul 9, 2024
2 parents f1ef827 + bcbb879 commit 4eed3e3
Show file tree
Hide file tree
Showing 19 changed files with 179 additions and 15 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ Increasing the minimal supported Rust version will always be coupled at least wi

## Unreleased

### Added

* Support for libsqlite3-sys 0.29.0

## [2.2.0] 2024-05-31

### Added
Expand Down
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ members = [
rust-version = "1.78.0"
include = ["src/**/*.rs", "tests/**/*.rs", "LICENSE-*", "README.md"]

[workspace.dependencies]
libsqlite3-sys = "0.29"

# Config for 'cargo dist'
[workspace.metadata.dist]
# The preferred cargo-dist version to use in CI (Cargo.toml SemVer syntax)
Expand Down
2 changes: 1 addition & 1 deletion diesel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ include = [
byteorder = { version = "1.0", optional = true }
chrono = { version = "0.4.20", optional = true, default-features = false, features = ["clock", "std"] }
libc = { version = "0.2.0", optional = true }
libsqlite3-sys = { version = ">=0.17.2, <0.29.0", optional = true, features = ["bundled_bindings"] }
libsqlite3-sys = { version = ">=0.17.2, <0.30.0", optional = true, features = ["bundled_bindings"] }
mysqlclient-sys = { version = ">=0.2.5, <0.5.0", optional = true }
mysqlclient-src = { version = "0.1.0", optional = true }
pq-sys = { version = ">=0.4.0, <0.7.0", optional = true }
Expand Down
9 changes: 8 additions & 1 deletion diesel/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@
//! with the `bundled` feature as a dependency to your crate so SQLite will be bundled:
//! ```toml
//! [dependencies]
//! libsqlite3-sys = { version = "0.25.2", features = ["bundled"] }
//! libsqlite3-sys = { version = "0.29", features = ["bundled"] }
//! ```
//! - `postgres`: This feature enables the diesel postgres backend. Enabling this feature requires a compatible
//! copy of `libpq` for your target architecture. This features implies `postgres_backend`
Expand Down Expand Up @@ -673,6 +673,13 @@ pub mod helper_types {
<U as crate::query_builder::update_statement::UpdateAutoTypeHelper>::Where,
<V as crate::AsChangeset>::Changeset,
>;

/// Represents the return type of
/// [`InsertStatement::returning`](crate::query_builder::InsertStatement::returning),
/// [`UpdateStatement::returning`] and
/// [`DeleteStatement::returning`](crate::query_builder::DeleteStatement::returning)
pub type Returning<Q, S> =
<Q as crate::query_builder::returning_clause::ReturningClauseHelper<S>>::WithReturning;
}

pub mod prelude {
Expand Down
8 changes: 5 additions & 3 deletions diesel/src/pg/expression/expression_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2502,7 +2502,8 @@ where

pub(in crate::pg) mod private {
use crate::sql_types::{
Array, Binary, Cidr, Inet, Integer, Json, Jsonb, Nullable, Range, SqlType, Text,
Array, Binary, Cidr, Inet, Integer, Json, Jsonb, Nullable, Range, SingleValue, SqlType,
Text,
};
use crate::{Expression, IntoSql};

Expand Down Expand Up @@ -2544,13 +2545,14 @@ pub(in crate::pg) mod private {

/// Marker trait used to extract the inner type
/// of our `Range<T>` sql type, used to implement `PgRangeExpressionMethods`
pub trait RangeHelper: SqlType {
type Inner;
pub trait RangeHelper: SqlType + SingleValue {
type Inner: SingleValue;
}

impl<ST> RangeHelper for Range<ST>
where
Self: 'static,
ST: SingleValue,
{
type Inner = ST;
}
Expand Down
44 changes: 44 additions & 0 deletions diesel/src/pg/expression/functions.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! PostgreSQL specific functions

use super::expression_methods::InetOrCidr;
use super::expression_methods::RangeHelper;
use crate::expression::functions::define_sql_function;
use crate::sql_types::*;

Expand Down Expand Up @@ -62,3 +63,46 @@ define_sql_function! {
#[cfg(feature = "postgres_backend")]
fn set_masklen<T: InetOrCidr + SingleValue>(addr: T, len: Integer) -> T;
}

define_sql_function! {
/// Returns the lower bound of the range.
/// if the range is empty or has no lower bound, it returns NULL.
/// # Example
///
/// ```rust
/// # include!("../../doctest_setup.rs");
/// #
/// # table! {
/// # posts {
/// # id -> Integer,
/// # versions -> Range<Integer>,
/// # }
/// # }
/// #
/// # fn main() {
/// # run_test().unwrap();
/// # }
/// #
/// # fn run_test() -> QueryResult<()> {
/// # use self::posts::dsl::*;
/// # use std::collections::Bound;
/// # let conn = &mut establish_connection();
/// # diesel::sql_query("DROP TABLE IF EXISTS posts").execute(conn).unwrap();
/// # diesel::sql_query("CREATE TABLE posts (id SERIAL PRIMARY KEY, versions INT4RANGE NOT NULL)").execute(conn).unwrap();
/// #
/// use diesel::dsl::lower;
/// diesel::insert_into(posts)
/// .values(&[
/// versions.eq((Bound::Included(5), Bound::Included(7))),
/// versions.eq((Bound::Unbounded, Bound::Included(7)))
/// ]).execute(conn)?;
///
/// let cool_posts = posts.select(lower(versions))
/// .load::<Option<i32>>(conn)?;
/// assert_eq!(vec![Some(5), None], cool_posts);
/// # Ok(())
/// # }
/// ```
#[cfg(feature = "postgres_backend")]
fn lower<T: RangeHelper>(range: T) -> Nullable<<T as RangeHelper>::Inner>;
}
29 changes: 29 additions & 0 deletions diesel/src/query_builder/returning_clause.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use super::DeleteStatement;
use super::InsertStatement;
use super::UpdateStatement;
use super::{AstPass, QueryFragment};
use crate::backend::{Backend, DieselReserveSpecialization};
use crate::query_builder::QueryId;
use crate::result::QueryResult;
use crate::QuerySource;

#[derive(Debug, Clone, Copy, QueryId)]
pub struct NoReturningClause;
Expand Down Expand Up @@ -52,3 +56,28 @@ where
Ok(())
}
}

pub trait ReturningClauseHelper<S> {
type WithReturning;
}

impl<S, T, U, Op> ReturningClauseHelper<S> for InsertStatement<T, U, Op>
where
T: QuerySource,
{
type WithReturning = InsertStatement<T, U, Op, ReturningClause<S>>;
}

impl<S, T, U, V> ReturningClauseHelper<S> for UpdateStatement<T, U, V>
where
T: QuerySource,
{
type WithReturning = UpdateStatement<T, U, V, ReturningClause<S>>;
}

impl<S, T, U> ReturningClauseHelper<S> for DeleteStatement<T, U>
where
T: QuerySource,
{
type WithReturning = DeleteStatement<T, U, ReturningClause<S>>;
}
5 changes: 4 additions & 1 deletion diesel/src/sqlite/connection/serialized_database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ pub struct SerializedDatabase {

impl SerializedDatabase {
/// Creates a new `SerializedDatabase` with the given data pointer and length.
pub fn new(data: *mut u8, len: usize) -> Self {
///
/// SAFETY: The data pointer needs to be returned by sqlite
/// and the length must match the underlying buffer pointer
pub(crate) unsafe fn new(data: *mut u8, len: usize) -> Self {
Self { data, len }
}

Expand Down
4 changes: 2 additions & 2 deletions diesel_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ heck = "0.5.0"
serde = { version = "1.0.193", features = ["derive", "std"] }
toml = "0.8"
url = { version = "2.2.2" }
libsqlite3-sys = { version = ">=0.17.2, <0.29.0", optional = true }
libsqlite3-sys = { workspace = true, optional = true }
pq-sys = { version = ">=0.4, <0.7.0", optional = true }
openssl-sys = { version = "0.9.93", features = ["vendored"], optional = true }
mysqlclient-sys = { version = "0.4.0", optional = true }
diffy = "0.3.0"
diffy = "0.4.0"
regex = "1.0.6"
serde_regex = "1.1"
diesel_table_macro_syntax = { version = "0.2", path = "../diesel_table_macro_syntax" }
Expand Down
1 change: 1 addition & 0 deletions diesel_derives/src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,7 @@ fn is_numeric(ty: &syn::TypePath) -> bool {
"Int8",
"Bigint",
"BigSerial",
"Decimal",
"Float",
"Float4",
"Float8",
Expand Down
15 changes: 14 additions & 1 deletion diesel_derives/tests/auto_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,13 +284,14 @@ fn test_pg_range_expression_methods() -> _ {
pg_extras::range
.contains_range(my_range)
.and(pg_extras::range.is_contained_by(my_range))

// `.contains()` cannot be supported here as
// the type level constraints are slightly different
// for `Range<>` than for the other types that provide a `contains()`
// function. We could likely support it by
// renaming the function to `.range_contains()` (or something similar)
// .contains(42_i32)
// this kind of free standing functions is not supported by auto_type yet
//.select(lower(pg_extras::range))
}

#[cfg(feature = "postgres")]
Expand Down Expand Up @@ -380,6 +381,18 @@ fn with_const_generics<const N: i32>() -> _ {
users::id.eq(N)
}

#[auto_type]
fn insert_returning() -> _ {
insert_into(users::table)
.values(users::id.eq(42_i32))
.returning(users::id)
}

#[auto_type]
fn delete_returning() -> _ {
delete(users::table).returning(users::id)
}

// #[auto_type]
// fn test_sql_fragment() -> _ {
// sql("foo")
Expand Down
2 changes: 1 addition & 1 deletion diesel_tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ ipnet = { version = "2.5.0" }
ipnetwork = ">=0.12.2, <0.21.0"
bigdecimal = ">= 0.0.13, < 0.5.0"
rand = "0.8.4"
libsqlite3-sys = { version = "0.28", optional = true }
libsqlite3-sys = { workspace = true, optional = true }
pq-sys = { version = "0.6", optional = true }
pq-src = { version = "0.3", optional = true }
mysqlclient-sys = { version = "0.4", optional = true }
Expand Down
2 changes: 2 additions & 0 deletions diesel_tests/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ mod limit_offset;
mod macros;
#[cfg(feature = "postgres")]
mod only;
#[cfg(not(feature = "sqlite"))]
mod operations;
mod order;
mod perf_details;
#[cfg(feature = "postgres")]
Expand Down
56 changes: 56 additions & 0 deletions diesel_tests/tests/operations.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use std::str::FromStr;

use crate::schema::*;
use bigdecimal::BigDecimal;
use diesel::{insert_into, prelude::*, update};

diesel::table! {
bigdecimal_table (id) {
id -> Integer,
big_decimal -> Decimal,
}
}

#[derive(Debug, Insertable, Queryable, Selectable)]
#[diesel(table_name = bigdecimal_table)]
struct CustomBigDecimal {
pub id: i32,
pub big_decimal: BigDecimal,
}

#[test]
fn big_decimal_add() {
let connection = &mut connection();

let custom_value = CustomBigDecimal {
id: 1,
big_decimal: BigDecimal::from_str("0.80").unwrap(),
};

diesel::sql_query(
"
CREATE TEMPORARY TABLE bigdecimal_table (
id SERIAL PRIMARY KEY,
big_decimal DECIMAL(8,2) NOT NULL
);
",
)
.execute(connection)
.unwrap();

insert_into(bigdecimal_table::table)
.values(&custom_value)
.execute(connection)
.unwrap();

let val = BigDecimal::from_str("0.1").unwrap();

update(bigdecimal_table::table)
.set(bigdecimal_table::big_decimal.eq(bigdecimal_table::big_decimal + val))
.execute(connection)
.unwrap();

let updated: CustomBigDecimal = bigdecimal_table::table.first(connection).unwrap();

assert_eq!(BigDecimal::from_str("0.90").unwrap(), updated.big_decimal);
}
2 changes: 1 addition & 1 deletion examples/sqlite/all_about_inserts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ diesel = { version = "2.1.0", path = "../../../diesel", features = ["sqlite", "c
serde = { version = "1.0.130", features = ["derive"] }
serde_json = "1.0.68"
chrono = { version = "0.4.20", default-features = false, features = ["clock", "std"] }
libsqlite3-sys = { version = "0.28.0", features = ["bundled"] }
libsqlite3-sys = { workspace = true, features = ["bundled"] }

[lib]
doc = false
2 changes: 1 addition & 1 deletion examples/sqlite/getting_started_step_1/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ publish = false
[dependencies]
diesel = { version = "2.1.0", path = "../../../diesel", features = ["sqlite"] }
dotenvy = "0.15"
libsqlite3-sys = { version = "0.28.0", features = ["bundled"] }
libsqlite3-sys = { workspace = true, features = ["bundled"] }

[dev-dependencies]
assert_cmd = "2.0.14"
Expand Down
2 changes: 1 addition & 1 deletion examples/sqlite/getting_started_step_2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ publish = false
[dependencies]
diesel = { version = "2.1.0", path = "../../../diesel", features = ["sqlite", "returning_clauses_for_sqlite_3_35"] }
dotenvy = "0.15"
libsqlite3-sys = { version = "0.28.0", features = ["bundled"] }
libsqlite3-sys = { workspace = true, features = ["bundled"] }

[dev-dependencies]
assert_cmd = "2.0.14"
Expand Down
2 changes: 1 addition & 1 deletion examples/sqlite/getting_started_step_3/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ publish = false
[dependencies]
diesel = { version = "2.1.0", path = "../../../diesel", features = ["sqlite", "returning_clauses_for_sqlite_3_35"] }
dotenvy = "0.15"
libsqlite3-sys = { version = "0.28.0", features = ["bundled"] }
libsqlite3-sys = { workspace = true, features = ["bundled"] }

[dev-dependencies]
assert_cmd = "2.0.14"
Expand Down
2 changes: 1 addition & 1 deletion examples/sqlite/relations/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ publish = false

[dependencies]
diesel = { version = "2.1.0", features = ["sqlite", "returning_clauses_for_sqlite_3_35"], path = "../../../diesel" }
libsqlite3-sys = { version = "0.28.0", features = ["bundled"] }
libsqlite3-sys = { workspace = true, features = ["bundled"] }
dotenvy = "0.15.6"

[[bin]]
Expand Down

0 comments on commit 4eed3e3

Please sign in to comment.