1//! SQLite function behavior flags for custom SQL functions.
23#[cfg(not(all(target_family = "wasm", target_os = "unknown")))]
4extern crate libsqlite3_sys as ffi;
56#[cfg(all(target_family = "wasm", target_os = "unknown"))]
7use sqlite_wasm_rs as ffi;
89#[doc = r" Flags controlling SQLite custom function behavior."]
#[doc = r""]
#[doc =
r" These flags are passed to `sqlite3_create_function_v2()` and control"]
#[doc = r" how SQLite treats the function in various contexts."]
#[doc = r""]
#[doc = r" # Availability"]
#[doc = r""]
#[doc = r" - [`DETERMINISTIC`][Self::DETERMINISTIC]: SQLite 3.8.3 (2014-02)"]
#[doc =
r" - [`DIRECTONLY`][Self::DIRECTONLY], [`INNOCUOUS`][Self::INNOCUOUS]: SQLite 3.31.0 (2020-01)"]
#[doc = r" - [`SUBTYPE`][Self::SUBTYPE]: SQLite 3.30.0 (2019-10)"]
#[doc = r""]
#[doc = r" # Security Considerations"]
#[doc = r""]
#[doc =
r" When using [`SqliteConnection::set_trusted_schema(false)`][crate::sqlite::SqliteConnection::set_trusted_schema]"]
#[doc =
r" for security hardening, custom functions must be marked with [`INNOCUOUS`][Self::INNOCUOUS]"]
#[doc =
r" to be callable from views, triggers, CHECK constraints, DEFAULT expressions,"]
#[doc = r" generated columns, and expression indexes."]
#[doc = r""]
#[doc =
r" Conversely, functions with side effects or that expose sensitive state"]
#[doc =
r" should be marked with [`DIRECTONLY`][Self::DIRECTONLY] to prevent them from being called"]
#[doc = r" via malicious schema objects in untrusted database files."]
#[doc = r""]
#[doc = r" # Example"]
#[doc = r""]
#[doc = r" ```rust"]
#[doc = r" use diesel::sqlite::SqliteFunctionBehavior;"]
#[doc = r""]
#[doc = r" // Deterministic function (most common case)"]
#[doc = r" let flags = SqliteFunctionBehavior::DETERMINISTIC;"]
#[doc = r" assert!(flags.contains(SqliteFunctionBehavior::DETERMINISTIC));"]
#[doc = r""]
#[doc = r" // Non-deterministic function (e.g., random())"]
#[doc = r" let flags = SqliteFunctionBehavior::empty();"]
#[doc = r" assert!(flags.is_empty());"]
#[doc = r""]
#[doc = r" // Safe for use in untrusted schemas (views, triggers, etc.)"]
#[doc =
r" let flags = SqliteFunctionBehavior::DETERMINISTIC | SqliteFunctionBehavior::INNOCUOUS;"]
#[doc = r" assert!(flags.contains(SqliteFunctionBehavior::DETERMINISTIC));"]
#[doc = r" assert!(flags.contains(SqliteFunctionBehavior::INNOCUOUS));"]
#[doc = r""]
#[doc = r" // Has side effects, block from schema objects"]
#[doc = r" let flags = SqliteFunctionBehavior::DIRECTONLY;"]
#[doc = r" assert!(flags.contains(SqliteFunctionBehavior::DIRECTONLY));"]
#[doc = r" ```"]
#[doc = r""]
#[doc = r" When registering a custom SQL function:"]
#[doc = r""]
#[doc = r" ```rust"]
#[doc = r#" # include!("../doctest_setup.rs");"#]
#[doc = r" use diesel::sqlite::SqliteFunctionBehavior;"]
#[doc = r" use diesel::prelude::*;"]
#[doc = r""]
#[doc = r" # fn main() {"]
#[doc = r" # run_test().unwrap();"]
#[doc = r" # }"]
#[doc = r" #"]
#[doc = r" # fn run_test() -> diesel::result::QueryResult<()> {"]
#[doc = r" # let mut conn = establish_connection();"]
#[doc = r" // Use SqliteFunctionBehavior when registering SQL functions"]
#[doc =
r" conn.register_sql_function::<diesel::sql_types::Text, diesel::sql_types::Text, _, _, _>("]
#[doc = r#" "my_upper","#]
#[doc =
r" SqliteFunctionBehavior::DETERMINISTIC | SqliteFunctionBehavior::INNOCUOUS,"]
#[doc = r" |x: String| x.to_uppercase(),"]
#[doc = r" )?;"]
#[doc = r" # Ok(())"]
#[doc = r" # }"]
#[doc = r" ```"]
pub struct SqliteFunctionBehavior(<SqliteFunctionBehavior as
::bitflags::__private::PublicFlags>::Internal);
#[automatically_derived]
impl ::core::fmt::Debug for SqliteFunctionBehavior {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"SqliteFunctionBehavior", &&self.0)
}
}
#[automatically_derived]
#[doc(hidden)]
unsafe impl ::core::clone::TrivialClone for SqliteFunctionBehavior { }
#[automatically_derived]
impl ::core::clone::Clone for SqliteFunctionBehavior {
#[inline]
fn clone(&self) -> SqliteFunctionBehavior {
let _:
::core::clone::AssertParamIsClone<<SqliteFunctionBehavior as
::bitflags::__private::PublicFlags>::Internal>;
*self
}
}
#[automatically_derived]
impl ::core::marker::Copy for SqliteFunctionBehavior { }
#[automatically_derived]
impl ::core::marker::StructuralPartialEq for SqliteFunctionBehavior { }
#[automatically_derived]
impl ::core::cmp::PartialEq for SqliteFunctionBehavior {
#[inline]
fn eq(&self, other: &SqliteFunctionBehavior) -> bool { self.0 == other.0 }
}
#[automatically_derived]
impl ::core::cmp::Eq for SqliteFunctionBehavior {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _:
::core::cmp::AssertParamIsEq<<SqliteFunctionBehavior as
::bitflags::__private::PublicFlags>::Internal>;
}
}
#[automatically_derived]
impl ::core::hash::Hash for SqliteFunctionBehavior {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
::core::hash::Hash::hash(&self.0, state)
}
}
#[allow(dead_code, deprecated, unused_doc_comments, unused_attributes,
unused_mut, unused_imports, non_upper_case_globals, clippy :: min_ident_chars,
clippy :: assign_op_pattern, clippy :: indexing_slicing, clippy ::
same_name_method, clippy :: iter_without_into_iter,)]
const _: () =
{
#[repr(transparent)]
pub struct InternalBitFlags(i32);
#[automatically_derived]
#[doc(hidden)]
unsafe impl ::core::clone::TrivialClone for InternalBitFlags { }
#[automatically_derived]
impl ::core::clone::Clone for InternalBitFlags {
#[inline]
fn clone(&self) -> InternalBitFlags {
let _: ::core::clone::AssertParamIsClone<i32>;
*self
}
}
#[automatically_derived]
impl ::core::marker::Copy for InternalBitFlags { }
#[automatically_derived]
impl ::core::marker::StructuralPartialEq for InternalBitFlags { }
#[automatically_derived]
impl ::core::cmp::PartialEq for InternalBitFlags {
#[inline]
fn eq(&self, other: &InternalBitFlags) -> bool {
self.0 == other.0
}
}
#[automatically_derived]
impl ::core::cmp::Eq for InternalBitFlags {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<i32>;
}
}
#[automatically_derived]
impl ::core::cmp::PartialOrd for InternalBitFlags {
#[inline]
fn partial_cmp(&self, other: &InternalBitFlags)
-> ::core::option::Option<::core::cmp::Ordering> {
::core::option::Option::Some(::core::cmp::Ord::cmp(self,
other))
}
}
#[automatically_derived]
impl ::core::cmp::Ord for InternalBitFlags {
#[inline]
fn cmp(&self, other: &InternalBitFlags) -> ::core::cmp::Ordering {
::core::cmp::Ord::cmp(&self.0, &other.0)
}
}
#[automatically_derived]
impl ::core::hash::Hash for InternalBitFlags {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
::core::hash::Hash::hash(&self.0, state)
}
}
impl SqliteFunctionBehavior {
#[doc =
r" The function always returns the same result given the same inputs"]
#[doc = r" within a single SQL statement."]
#[doc = r""]
#[doc =
r" This allows SQLite to optimize by caching results and factoring"]
#[doc =
r" the function out of inner loops. Most pure functions should use this flag."]
#[doc = r""]
#[doc = r" # Availability"]
#[doc = r""]
#[doc = r" Requires SQLite 3.8.3 (2014-02) or later."]
#[doc = r""]
#[doc = r" # Example"]
#[doc = r""]
#[doc = r" `abs(x)` is deterministic. `random()` is not."]
pub const DETERMINISTIC: Self =
Self::from_bits_retain(ffi::SQLITE_DETERMINISTIC);
#[doc =
r" The function cannot be called from schema objects (views, triggers,"]
#[doc =
r" CHECK constraints, DEFAULT expressions, generated columns, or"]
#[doc = r" expression indexes)."]
#[doc = r""]
#[doc = r" Use this for functions that:"]
#[doc =
r" - Have side effects (network, file I/O, logging, etc.)"]
#[doc = r" - Return sensitive application state"]
#[doc =
r" - Should not be triggered by opening an untrusted database"]
#[doc = r""]
#[doc = r" # Availability"]
#[doc = r""]
#[doc = r" Requires SQLite 3.31.0 (2020-01) or later."]
#[doc = r""]
#[doc = r" # Security Recommendation"]
#[doc = r""]
#[doc =
r" Mark all functions with side effects or that expose internal state"]
#[doc =
r" with `DIRECTONLY` to prevent schema injection attacks."]
pub const DIRECTONLY: Self =
Self::from_bits_retain(ffi::SQLITE_DIRECTONLY);
#[doc =
r" The function is safe to call from untrusted schema contexts."]
#[doc = r""]
#[doc =
r" When [`SqliteConnection::set_trusted_schema(false)`][crate::sqlite::SqliteConnection::set_trusted_schema]"]
#[doc =
r" is set, only functions marked `INNOCUOUS` can be called from schema"]
#[doc =
r" objects (views, triggers, CHECK constraints, DEFAULT expressions,"]
#[doc = r" generated columns, expression indexes)."]
#[doc = r""]
#[doc = r" # Availability"]
#[doc = r""]
#[doc = r" Requires SQLite 3.31.0 (2020-01) or later."]
#[doc = r""]
#[doc = r" # Safety Requirements"]
#[doc = r""]
#[doc = r" **Only mark a function as INNOCUOUS if it:**"]
#[doc = r" - Has no side effects"]
#[doc = r" - Does not reveal internal application state"]
#[doc = r" - Output depends solely on its input parameters"]
#[doc = r""]
#[doc = r" # Security Warning"]
#[doc = r""]
#[doc =
r" Incorrectly marking a function as `INNOCUOUS` can create security"]
#[doc =
r" vulnerabilities when processing untrusted database files. An attacker"]
#[doc =
r" could craft a database with malicious views or triggers that invoke"]
#[doc = r" your function in unexpected ways."]
pub const INNOCUOUS: Self =
Self::from_bits_retain(ffi::SQLITE_INNOCUOUS);
#[doc =
r" The function may call `sqlite3_value_subtype()` to inspect the"]
#[doc = r" subtype of its arguments."]
#[doc = r""]
#[doc = r" # Availability"]
#[doc = r""]
#[doc = r" Requires SQLite 3.30.0 (2019-10) or later."]
pub const SUBTYPE: Self =
Self::from_bits_retain(ffi::SQLITE_SUBTYPE);
}
impl ::bitflags::Flags for SqliteFunctionBehavior {
const FLAGS: &'static [::bitflags::Flag<SqliteFunctionBehavior>] =
{
mod __bitflags_flag_names {
use super::*;
pub(super) const DETERMINISTIC: &'static str =
"DETERMINISTIC";
pub(super) const DIRECTONLY: &'static str = "DIRECTONLY";
pub(super) const INNOCUOUS: &'static str = "INNOCUOUS";
pub(super) const SUBTYPE: &'static str = "SUBTYPE";
}
&[{
::bitflags::Flag::new(__bitflags_flag_names::DETERMINISTIC,
SqliteFunctionBehavior::DETERMINISTIC)
},
{
::bitflags::Flag::new(__bitflags_flag_names::DIRECTONLY,
SqliteFunctionBehavior::DIRECTONLY)
},
{
::bitflags::Flag::new(__bitflags_flag_names::INNOCUOUS,
SqliteFunctionBehavior::INNOCUOUS)
},
{
::bitflags::Flag::new(__bitflags_flag_names::SUBTYPE,
SqliteFunctionBehavior::SUBTYPE)
}]
};
type Bits = i32;
fn bits(&self) -> i32 { SqliteFunctionBehavior::bits(self) }
fn from_bits_retain(bits: i32) -> SqliteFunctionBehavior {
SqliteFunctionBehavior::from_bits_retain(bits)
}
fn all_named() -> SqliteFunctionBehavior {
const ALL_NAMED: i32 =
{
let mut truncated = <i32 as ::bitflags::Bits>::EMPTY;
let mut i = 0;
{
{
let flag =
&<SqliteFunctionBehavior as ::bitflags::Flags>::FLAGS[i];
if flag.is_named() {
truncated = truncated | flag.value().bits();
}
i += 1;
}
};
{
{
let flag =
&<SqliteFunctionBehavior as ::bitflags::Flags>::FLAGS[i];
if flag.is_named() {
truncated = truncated | flag.value().bits();
}
i += 1;
}
};
{
{
let flag =
&<SqliteFunctionBehavior as ::bitflags::Flags>::FLAGS[i];
if flag.is_named() {
truncated = truncated | flag.value().bits();
}
i += 1;
}
};
{
{
let flag =
&<SqliteFunctionBehavior as ::bitflags::Flags>::FLAGS[i];
if flag.is_named() {
truncated = truncated | flag.value().bits();
}
i += 1;
}
};
let _ = i;
truncated
};
SqliteFunctionBehavior::from_bits_retain(ALL_NAMED)
}
}
impl ::bitflags::__private::PublicFlags for SqliteFunctionBehavior {
type Primitive = i32;
type Internal = InternalBitFlags;
}
impl ::bitflags::__private::core::default::Default for
InternalBitFlags {
#[inline]
fn default() -> Self { InternalBitFlags::empty() }
}
impl ::bitflags::__private::core::fmt::Debug for InternalBitFlags {
fn fmt(&self,
f: &mut ::bitflags::__private::core::fmt::Formatter<'_>)
-> ::bitflags::__private::core::fmt::Result {
if self.is_empty() {
f.write_fmt(format_args!("{0:#x}",
<i32 as ::bitflags::Bits>::EMPTY))
} else {
::bitflags::__private::core::fmt::Display::fmt(self, f)
}
}
}
impl ::bitflags::__private::core::fmt::Display for InternalBitFlags {
fn fmt(&self,
f: &mut ::bitflags::__private::core::fmt::Formatter<'_>)
-> ::bitflags::__private::core::fmt::Result {
::bitflags::parser::to_writer(&SqliteFunctionBehavior(*self),
f)
}
}
impl ::bitflags::__private::core::str::FromStr for InternalBitFlags {
type Err = ::bitflags::parser::ParseError;
fn from_str(s: &str)
->
::bitflags::__private::core::result::Result<Self,
Self::Err> {
::bitflags::parser::from_str::<SqliteFunctionBehavior>(s).map(|flags|
flags.0)
}
}
impl ::bitflags::__private::core::convert::AsRef<i32> for
InternalBitFlags {
fn as_ref(&self) -> &i32 { &self.0 }
}
impl ::bitflags::__private::core::convert::From<i32> for
InternalBitFlags {
fn from(bits: i32) -> Self { Self::from_bits_retain(bits) }
}
impl InternalBitFlags {
/// Get a flags value with all bits unset.
#[inline]
pub const fn empty() -> Self {
Self(<i32 as ::bitflags::Bits>::EMPTY)
}
/// Get a flags value with all known bits set.
#[inline]
pub const fn all() -> Self {
let mut truncated = <i32 as ::bitflags::Bits>::EMPTY;
let mut i = 0;
{
{
let flag =
<SqliteFunctionBehavior as
::bitflags::Flags>::FLAGS[i].value().bits();
truncated = truncated | flag;
i += 1;
}
};
{
{
let flag =
<SqliteFunctionBehavior as
::bitflags::Flags>::FLAGS[i].value().bits();
truncated = truncated | flag;
i += 1;
}
};
{
{
let flag =
<SqliteFunctionBehavior as
::bitflags::Flags>::FLAGS[i].value().bits();
truncated = truncated | flag;
i += 1;
}
};
{
{
let flag =
<SqliteFunctionBehavior as
::bitflags::Flags>::FLAGS[i].value().bits();
truncated = truncated | flag;
i += 1;
}
};
let _ = i;
Self(truncated)
}
/// Get the underlying bits value.
///
/// The returned value is exactly the bits set in this flags value.
#[inline]
pub const fn bits(&self) -> i32 { self.0 }
/// Convert from a bits value.
///
/// This method will return `None` if any unknown bits are set.
#[inline]
pub const fn from_bits(bits: i32)
-> ::bitflags::__private::core::option::Option<Self> {
let truncated = Self::from_bits_truncate(bits).0;
if truncated == bits {
::bitflags::__private::core::option::Option::Some(Self(bits))
} else { ::bitflags::__private::core::option::Option::None }
}
/// Convert from a bits value, unsetting any unknown bits.
#[inline]
pub const fn from_bits_truncate(bits: i32) -> Self {
Self(bits & Self::all().0)
}
/// Convert from a bits value exactly.
#[inline]
pub const fn from_bits_retain(bits: i32) -> Self { Self(bits) }
/// Get a flags value with the bits of a flag with the given name set.
///
/// This method will return `None` if `name` is empty or doesn't
/// correspond to any named flag.
#[inline]
pub fn from_name(name: &str)
-> ::bitflags::__private::core::option::Option<Self> {
mod __bitflags_flag_names {
use super::*;
pub(super) const DETERMINISTIC: &'static str =
"DETERMINISTIC";
pub(super) const DIRECTONLY: &'static str = "DIRECTONLY";
pub(super) const INNOCUOUS: &'static str = "INNOCUOUS";
pub(super) const SUBTYPE: &'static str = "SUBTYPE";
}
{
{
if name == __bitflags_flag_names::DETERMINISTIC {
return ::bitflags::__private::core::option::Option::Some(Self(SqliteFunctionBehavior::DETERMINISTIC.bits()));
}
};
};
{
{
if name == __bitflags_flag_names::DIRECTONLY {
return ::bitflags::__private::core::option::Option::Some(Self(SqliteFunctionBehavior::DIRECTONLY.bits()));
}
};
};
{
{
if name == __bitflags_flag_names::INNOCUOUS {
return ::bitflags::__private::core::option::Option::Some(Self(SqliteFunctionBehavior::INNOCUOUS.bits()));
}
};
};
{
{
if name == __bitflags_flag_names::SUBTYPE {
return ::bitflags::__private::core::option::Option::Some(Self(SqliteFunctionBehavior::SUBTYPE.bits()));
}
};
};
let _ = name;
::bitflags::__private::core::option::Option::None
}
/// Whether all bits in `self` are unset.
#[inline]
pub const fn is_empty(&self) -> bool {
self.0 == <i32 as ::bitflags::Bits>::EMPTY
}
/// Whether all known bits in this flags value are set.
#[inline]
pub const fn is_all(&self) -> bool {
Self::all().0 | self.0 == self.0
}
/// Whether any set bits in `other` are also set in `self`.
#[inline]
pub const fn intersects(&self, other: Self) -> bool {
self.0 & other.0 != <i32 as ::bitflags::Bits>::EMPTY
}
/// Whether all set bits in `other` are also set in `self`.
#[inline]
pub const fn contains(&self, other: Self) -> bool {
self.0 & other.0 == other.0
}
/// The bitwise or (`|`) of the bits in `self` and `other`.
#[inline]
pub fn insert(&mut self, other: Self) {
*self = Self(self.0).union(other);
}
/// The intersection of `self` with the complement of `other` (`&!`).
///
/// This method is not equivalent to `self & !other` when `other` has unknown bits set.
/// `remove` won't truncate `other`, but the `!` operator will.
#[inline]
pub fn remove(&mut self, other: Self) {
*self = Self(self.0).difference(other);
}
/// The bitwise exclusive-or (`^`) of the bits in `self` and `other`.
#[inline]
pub fn toggle(&mut self, other: Self) {
*self = Self(self.0).symmetric_difference(other);
}
/// Call `insert` when `value` is `true` or `remove` when `value` is `false`.
#[inline]
pub fn set(&mut self, other: Self, value: bool) {
if value { self.insert(other); } else { self.remove(other); }
}
/// The bitwise and (`&`) of the bits in `self` and `other`.
#[inline]
#[must_use]
pub const fn intersection(self, other: Self) -> Self {
Self(self.0 & other.0)
}
/// The bitwise or (`|`) of the bits in `self` and `other`.
#[inline]
#[must_use]
pub const fn union(self, other: Self) -> Self {
Self(self.0 | other.0)
}
/// The intersection of `self` with the complement of `other` (`&!`).
///
/// This method is not equivalent to `self & !other` when `other` has unknown bits set.
/// `difference` won't truncate `other`, but the `!` operator will.
#[inline]
#[must_use]
pub const fn difference(self, other: Self) -> Self {
Self(self.0 & !other.0)
}
/// The bitwise exclusive-or (`^`) of the bits in `self` and `other`.
#[inline]
#[must_use]
pub const fn symmetric_difference(self, other: Self) -> Self {
Self(self.0 ^ other.0)
}
/// The bitwise negation (`!`) of the bits in `self`, truncating the result.
#[inline]
#[must_use]
pub const fn complement(self) -> Self {
Self::from_bits_truncate(!self.0)
}
}
impl ::bitflags::__private::core::fmt::Binary for InternalBitFlags {
fn fmt(&self, f: &mut ::bitflags::__private::core::fmt::Formatter)
-> ::bitflags::__private::core::fmt::Result {
let inner = self.0;
::bitflags::__private::core::fmt::Binary::fmt(&inner, f)
}
}
impl ::bitflags::__private::core::fmt::Octal for InternalBitFlags {
fn fmt(&self, f: &mut ::bitflags::__private::core::fmt::Formatter)
-> ::bitflags::__private::core::fmt::Result {
let inner = self.0;
::bitflags::__private::core::fmt::Octal::fmt(&inner, f)
}
}
impl ::bitflags::__private::core::fmt::LowerHex for InternalBitFlags {
fn fmt(&self, f: &mut ::bitflags::__private::core::fmt::Formatter)
-> ::bitflags::__private::core::fmt::Result {
let inner = self.0;
::bitflags::__private::core::fmt::LowerHex::fmt(&inner, f)
}
}
impl ::bitflags::__private::core::fmt::UpperHex for InternalBitFlags {
fn fmt(&self, f: &mut ::bitflags::__private::core::fmt::Formatter)
-> ::bitflags::__private::core::fmt::Result {
let inner = self.0;
::bitflags::__private::core::fmt::UpperHex::fmt(&inner, f)
}
}
impl ::bitflags::__private::core::ops::BitOr for InternalBitFlags {
type Output = Self;
/// The bitwise or (`|`) of the bits in `self` and `other`.
#[inline]
fn bitor(self, other: InternalBitFlags) -> Self {
self.union(other)
}
}
impl ::bitflags::__private::core::ops::BitOrAssign for
InternalBitFlags {
/// The bitwise or (`|`) of the bits in `self` and `other`.
#[inline]
fn bitor_assign(&mut self, other: Self) { self.insert(other); }
}
impl ::bitflags::__private::core::ops::BitXor for InternalBitFlags {
type Output = Self;
/// The bitwise exclusive-or (`^`) of the bits in `self` and `other`.
#[inline]
fn bitxor(self, other: Self) -> Self {
self.symmetric_difference(other)
}
}
impl ::bitflags::__private::core::ops::BitXorAssign for
InternalBitFlags {
/// The bitwise exclusive-or (`^`) of the bits in `self` and `other`.
#[inline]
fn bitxor_assign(&mut self, other: Self) { self.toggle(other); }
}
impl ::bitflags::__private::core::ops::BitAnd for InternalBitFlags {
type Output = Self;
/// The bitwise and (`&`) of the bits in `self` and `other`.
#[inline]
fn bitand(self, other: Self) -> Self { self.intersection(other) }
}
impl ::bitflags::__private::core::ops::BitAndAssign for
InternalBitFlags {
/// The bitwise and (`&`) of the bits in `self` and `other`.
#[inline]
fn bitand_assign(&mut self, other: Self) {
*self =
Self::from_bits_retain(self.bits()).intersection(other);
}
}
impl ::bitflags::__private::core::ops::Sub for InternalBitFlags {
type Output = Self;
/// The intersection of `self` with the complement of `other` (`&!`).
///
/// This method is not equivalent to `self & !other` when `other` has unknown bits set.
/// `difference` won't truncate `other`, but the `!` operator will.
#[inline]
fn sub(self, other: Self) -> Self { self.difference(other) }
}
impl ::bitflags::__private::core::ops::SubAssign for InternalBitFlags
{
/// The intersection of `self` with the complement of `other` (`&!`).
///
/// This method is not equivalent to `self & !other` when `other` has unknown bits set.
/// `difference` won't truncate `other`, but the `!` operator will.
#[inline]
fn sub_assign(&mut self, other: Self) { self.remove(other); }
}
impl ::bitflags::__private::core::ops::Not for InternalBitFlags {
type Output = Self;
/// The bitwise negation (`!`) of the bits in `self`, truncating the result.
#[inline]
fn not(self) -> Self { self.complement() }
}
impl ::bitflags::__private::core::iter::Extend<InternalBitFlags> for
InternalBitFlags {
/// The bitwise or (`|`) of the bits in each flags value.
fn extend<T: ::bitflags::__private::core::iter::IntoIterator<Item
= Self>>(&mut self, iterator: T) {
for item in iterator { self.insert(item) }
}
}
impl ::bitflags::__private::core::iter::FromIterator<InternalBitFlags>
for InternalBitFlags {
/// The bitwise or (`|`) of the bits in each flags value.
fn from_iter<T: ::bitflags::__private::core::iter::IntoIterator<Item
= Self>>(iterator: T) -> Self {
use ::bitflags::__private::core::iter::Extend;
let mut result = Self::empty();
result.extend(iterator);
result
}
}
impl InternalBitFlags {
/// Yield a set of contained flags values.
///
/// Each yielded flags value will correspond to a defined named flag. Any unknown bits
/// will be yielded together as a final flags value.
#[inline]
pub const fn iter(&self)
-> ::bitflags::iter::Iter<SqliteFunctionBehavior> {
::bitflags::iter::Iter::__private_const_new(<SqliteFunctionBehavior
as ::bitflags::Flags>::FLAGS,
SqliteFunctionBehavior::from_bits_retain(self.bits()),
SqliteFunctionBehavior::from_bits_retain(self.bits()))
}
/// Yield a set of contained named flags values.
///
/// This method is like [`iter`](#method.iter), except only yields bits in contained named flags.
/// Any unknown bits, or bits not corresponding to a contained flag will not be yielded.
#[inline]
pub const fn iter_names(&self)
-> ::bitflags::iter::IterNames<SqliteFunctionBehavior> {
::bitflags::iter::IterNames::__private_const_new(<SqliteFunctionBehavior
as ::bitflags::Flags>::FLAGS,
SqliteFunctionBehavior::from_bits_retain(self.bits()),
SqliteFunctionBehavior::from_bits_retain(self.bits()))
}
}
impl ::bitflags::__private::core::iter::IntoIterator for
InternalBitFlags {
type Item = SqliteFunctionBehavior;
type IntoIter = ::bitflags::iter::Iter<SqliteFunctionBehavior>;
fn into_iter(self) -> Self::IntoIter { self.iter() }
}
impl InternalBitFlags {
/// Returns a mutable reference to the raw value of the flags currently stored.
#[inline]
pub fn bits_mut(&mut self) -> &mut i32 { &mut self.0 }
}
impl SqliteFunctionBehavior {
/// Get a flags value with all bits unset.
#[inline]
pub const fn empty() -> Self { Self(InternalBitFlags::empty()) }
/// Get a flags value with all known bits set.
#[inline]
pub const fn all() -> Self { Self(InternalBitFlags::all()) }
/// Get the underlying bits value.
///
/// The returned value is exactly the bits set in this flags value.
#[inline]
pub const fn bits(&self) -> i32 { self.0.bits() }
/// Convert from a bits value.
///
/// This method will return `None` if any unknown bits are set.
#[inline]
pub const fn from_bits(bits: i32)
-> ::bitflags::__private::core::option::Option<Self> {
match InternalBitFlags::from_bits(bits) {
::bitflags::__private::core::option::Option::Some(bits) =>
::bitflags::__private::core::option::Option::Some(Self(bits)),
::bitflags::__private::core::option::Option::None =>
::bitflags::__private::core::option::Option::None,
}
}
/// Convert from a bits value, unsetting any unknown bits.
#[inline]
pub const fn from_bits_truncate(bits: i32) -> Self {
Self(InternalBitFlags::from_bits_truncate(bits))
}
/// Convert from a bits value exactly.
#[inline]
pub const fn from_bits_retain(bits: i32) -> Self {
Self(InternalBitFlags::from_bits_retain(bits))
}
/// Get a flags value with the bits of a flag with the given name set.
///
/// This method will return `None` if `name` is empty or doesn't
/// correspond to any named flag.
#[inline]
pub fn from_name(name: &str)
-> ::bitflags::__private::core::option::Option<Self> {
match InternalBitFlags::from_name(name) {
::bitflags::__private::core::option::Option::Some(bits) =>
::bitflags::__private::core::option::Option::Some(Self(bits)),
::bitflags::__private::core::option::Option::None =>
::bitflags::__private::core::option::Option::None,
}
}
/// Whether all bits in `self` are unset.
#[inline]
pub const fn is_empty(&self) -> bool { self.0.is_empty() }
/// Whether all known bits in this flags value are set.
#[inline]
pub const fn is_all(&self) -> bool { self.0.is_all() }
/// Whether any set bits in `other` are also set in `self`.
#[inline]
pub const fn intersects(&self, other: Self) -> bool {
self.0.intersects(other.0)
}
/// Whether all set bits in `other` are also set in `self`.
#[inline]
pub const fn contains(&self, other: Self) -> bool {
self.0.contains(other.0)
}
/// The bitwise or (`|`) of the bits in `self` and `other`.
#[inline]
pub fn insert(&mut self, other: Self) { self.0.insert(other.0) }
/// The intersection of `self` with the complement of `other` (`&!`).
///
/// This method is not equivalent to `self & !other` when `other` has unknown bits set.
/// `remove` won't truncate `other`, but the `!` operator will.
#[inline]
pub fn remove(&mut self, other: Self) { self.0.remove(other.0) }
/// The bitwise exclusive-or (`^`) of the bits in `self` and `other`.
#[inline]
pub fn toggle(&mut self, other: Self) { self.0.toggle(other.0) }
/// Call `insert` when `value` is `true` or `remove` when `value` is `false`.
#[inline]
pub fn set(&mut self, other: Self, value: bool) {
self.0.set(other.0, value)
}
/// The bitwise and (`&`) of the bits in `self` and `other`.
#[inline]
#[must_use]
pub const fn intersection(self, other: Self) -> Self {
Self(self.0.intersection(other.0))
}
/// The bitwise or (`|`) of the bits in `self` and `other`.
#[inline]
#[must_use]
pub const fn union(self, other: Self) -> Self {
Self(self.0.union(other.0))
}
/// The intersection of `self` with the complement of `other` (`&!`).
///
/// This method is not equivalent to `self & !other` when `other` has unknown bits set.
/// `difference` won't truncate `other`, but the `!` operator will.
#[inline]
#[must_use]
pub const fn difference(self, other: Self) -> Self {
Self(self.0.difference(other.0))
}
/// The bitwise exclusive-or (`^`) of the bits in `self` and `other`.
#[inline]
#[must_use]
pub const fn symmetric_difference(self, other: Self) -> Self {
Self(self.0.symmetric_difference(other.0))
}
/// The bitwise negation (`!`) of the bits in `self`, truncating the result.
#[inline]
#[must_use]
pub const fn complement(self) -> Self {
Self(self.0.complement())
}
}
impl ::bitflags::__private::core::fmt::Binary for
SqliteFunctionBehavior {
fn fmt(&self, f: &mut ::bitflags::__private::core::fmt::Formatter)
-> ::bitflags::__private::core::fmt::Result {
let inner = self.0;
::bitflags::__private::core::fmt::Binary::fmt(&inner, f)
}
}
impl ::bitflags::__private::core::fmt::Octal for
SqliteFunctionBehavior {
fn fmt(&self, f: &mut ::bitflags::__private::core::fmt::Formatter)
-> ::bitflags::__private::core::fmt::Result {
let inner = self.0;
::bitflags::__private::core::fmt::Octal::fmt(&inner, f)
}
}
impl ::bitflags::__private::core::fmt::LowerHex for
SqliteFunctionBehavior {
fn fmt(&self, f: &mut ::bitflags::__private::core::fmt::Formatter)
-> ::bitflags::__private::core::fmt::Result {
let inner = self.0;
::bitflags::__private::core::fmt::LowerHex::fmt(&inner, f)
}
}
impl ::bitflags::__private::core::fmt::UpperHex for
SqliteFunctionBehavior {
fn fmt(&self, f: &mut ::bitflags::__private::core::fmt::Formatter)
-> ::bitflags::__private::core::fmt::Result {
let inner = self.0;
::bitflags::__private::core::fmt::UpperHex::fmt(&inner, f)
}
}
impl ::bitflags::__private::core::ops::BitOr for
SqliteFunctionBehavior {
type Output = Self;
/// The bitwise or (`|`) of the bits in `self` and `other`.
#[inline]
fn bitor(self, other: SqliteFunctionBehavior) -> Self {
self.union(other)
}
}
impl ::bitflags::__private::core::ops::BitOrAssign for
SqliteFunctionBehavior {
/// The bitwise or (`|`) of the bits in `self` and `other`.
#[inline]
fn bitor_assign(&mut self, other: Self) { self.insert(other); }
}
impl ::bitflags::__private::core::ops::BitXor for
SqliteFunctionBehavior {
type Output = Self;
/// The bitwise exclusive-or (`^`) of the bits in `self` and `other`.
#[inline]
fn bitxor(self, other: Self) -> Self {
self.symmetric_difference(other)
}
}
impl ::bitflags::__private::core::ops::BitXorAssign for
SqliteFunctionBehavior {
/// The bitwise exclusive-or (`^`) of the bits in `self` and `other`.
#[inline]
fn bitxor_assign(&mut self, other: Self) { self.toggle(other); }
}
impl ::bitflags::__private::core::ops::BitAnd for
SqliteFunctionBehavior {
type Output = Self;
/// The bitwise and (`&`) of the bits in `self` and `other`.
#[inline]
fn bitand(self, other: Self) -> Self { self.intersection(other) }
}
impl ::bitflags::__private::core::ops::BitAndAssign for
SqliteFunctionBehavior {
/// The bitwise and (`&`) of the bits in `self` and `other`.
#[inline]
fn bitand_assign(&mut self, other: Self) {
*self =
Self::from_bits_retain(self.bits()).intersection(other);
}
}
impl ::bitflags::__private::core::ops::Sub for SqliteFunctionBehavior
{
type Output = Self;
/// The intersection of `self` with the complement of `other` (`&!`).
///
/// This method is not equivalent to `self & !other` when `other` has unknown bits set.
/// `difference` won't truncate `other`, but the `!` operator will.
#[inline]
fn sub(self, other: Self) -> Self { self.difference(other) }
}
impl ::bitflags::__private::core::ops::SubAssign for
SqliteFunctionBehavior {
/// The intersection of `self` with the complement of `other` (`&!`).
///
/// This method is not equivalent to `self & !other` when `other` has unknown bits set.
/// `difference` won't truncate `other`, but the `!` operator will.
#[inline]
fn sub_assign(&mut self, other: Self) { self.remove(other); }
}
impl ::bitflags::__private::core::ops::Not for SqliteFunctionBehavior
{
type Output = Self;
/// The bitwise negation (`!`) of the bits in `self`, truncating the result.
#[inline]
fn not(self) -> Self { self.complement() }
}
impl ::bitflags::__private::core::iter::Extend<SqliteFunctionBehavior>
for SqliteFunctionBehavior {
/// The bitwise or (`|`) of the bits in each flags value.
fn extend<T: ::bitflags::__private::core::iter::IntoIterator<Item
= Self>>(&mut self, iterator: T) {
for item in iterator { self.insert(item) }
}
}
impl ::bitflags::__private::core::iter::FromIterator<SqliteFunctionBehavior>
for SqliteFunctionBehavior {
/// The bitwise or (`|`) of the bits in each flags value.
fn from_iter<T: ::bitflags::__private::core::iter::IntoIterator<Item
= Self>>(iterator: T) -> Self {
use ::bitflags::__private::core::iter::Extend;
let mut result = Self::empty();
result.extend(iterator);
result
}
}
impl SqliteFunctionBehavior {
/// Yield a set of contained flags values.
///
/// Each yielded flags value will correspond to a defined named flag. Any unknown bits
/// will be yielded together as a final flags value.
#[inline]
pub const fn iter(&self)
-> ::bitflags::iter::Iter<SqliteFunctionBehavior> {
::bitflags::iter::Iter::__private_const_new(<SqliteFunctionBehavior
as ::bitflags::Flags>::FLAGS,
SqliteFunctionBehavior::from_bits_retain(self.bits()),
SqliteFunctionBehavior::from_bits_retain(self.bits()))
}
/// Yield a set of contained named flags values.
///
/// This method is like [`iter`](#method.iter), except only yields bits in contained named flags.
/// Any unknown bits, or bits not corresponding to a contained flag will not be yielded.
#[inline]
pub const fn iter_names(&self)
-> ::bitflags::iter::IterNames<SqliteFunctionBehavior> {
::bitflags::iter::IterNames::__private_const_new(<SqliteFunctionBehavior
as ::bitflags::Flags>::FLAGS,
SqliteFunctionBehavior::from_bits_retain(self.bits()),
SqliteFunctionBehavior::from_bits_retain(self.bits()))
}
}
impl ::bitflags::__private::core::iter::IntoIterator for
SqliteFunctionBehavior {
type Item = SqliteFunctionBehavior;
type IntoIter = ::bitflags::iter::Iter<SqliteFunctionBehavior>;
fn into_iter(self) -> Self::IntoIter { self.iter() }
}
};bitflags::bitflags! {
10/// Flags controlling SQLite custom function behavior.
11 ///
12 /// These flags are passed to `sqlite3_create_function_v2()` and control
13 /// how SQLite treats the function in various contexts.
14 ///
15 /// # Availability
16 ///
17 /// - [`DETERMINISTIC`][Self::DETERMINISTIC]: SQLite 3.8.3 (2014-02)
18 /// - [`DIRECTONLY`][Self::DIRECTONLY], [`INNOCUOUS`][Self::INNOCUOUS]: SQLite 3.31.0 (2020-01)
19 /// - [`SUBTYPE`][Self::SUBTYPE]: SQLite 3.30.0 (2019-10)
20 ///
21 /// # Security Considerations
22 ///
23 /// When using [`SqliteConnection::set_trusted_schema(false)`][crate::sqlite::SqliteConnection::set_trusted_schema]
24 /// for security hardening, custom functions must be marked with [`INNOCUOUS`][Self::INNOCUOUS]
25 /// to be callable from views, triggers, CHECK constraints, DEFAULT expressions,
26 /// generated columns, and expression indexes.
27 ///
28 /// Conversely, functions with side effects or that expose sensitive state
29 /// should be marked with [`DIRECTONLY`][Self::DIRECTONLY] to prevent them from being called
30 /// via malicious schema objects in untrusted database files.
31 ///
32 /// # Example
33 ///
34 /// ```rust
35 /// use diesel::sqlite::SqliteFunctionBehavior;
36 ///
37 /// // Deterministic function (most common case)
38 /// let flags = SqliteFunctionBehavior::DETERMINISTIC;
39 /// assert!(flags.contains(SqliteFunctionBehavior::DETERMINISTIC));
40 ///
41 /// // Non-deterministic function (e.g., random())
42 /// let flags = SqliteFunctionBehavior::empty();
43 /// assert!(flags.is_empty());
44 ///
45 /// // Safe for use in untrusted schemas (views, triggers, etc.)
46 /// let flags = SqliteFunctionBehavior::DETERMINISTIC | SqliteFunctionBehavior::INNOCUOUS;
47 /// assert!(flags.contains(SqliteFunctionBehavior::DETERMINISTIC));
48 /// assert!(flags.contains(SqliteFunctionBehavior::INNOCUOUS));
49 ///
50 /// // Has side effects, block from schema objects
51 /// let flags = SqliteFunctionBehavior::DIRECTONLY;
52 /// assert!(flags.contains(SqliteFunctionBehavior::DIRECTONLY));
53 /// ```
54 ///
55 /// When registering a custom SQL function:
56 ///
57 /// ```rust
58 /// # include!("../doctest_setup.rs");
59 /// use diesel::sqlite::SqliteFunctionBehavior;
60 /// use diesel::prelude::*;
61 ///
62 /// # fn main() {
63 /// # run_test().unwrap();
64 /// # }
65 /// #
66 /// # fn run_test() -> diesel::result::QueryResult<()> {
67 /// # let mut conn = establish_connection();
68 /// // Use SqliteFunctionBehavior when registering SQL functions
69 /// conn.register_sql_function::<diesel::sql_types::Text, diesel::sql_types::Text, _, _, _>(
70 /// "my_upper",
71 /// SqliteFunctionBehavior::DETERMINISTIC | SqliteFunctionBehavior::INNOCUOUS,
72 /// |x: String| x.to_uppercase(),
73 /// )?;
74 /// # Ok(())
75 /// # }
76 /// ```
77#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
78pub struct SqliteFunctionBehavior: i32 {
79/// The function always returns the same result given the same inputs
80 /// within a single SQL statement.
81 ///
82 /// This allows SQLite to optimize by caching results and factoring
83 /// the function out of inner loops. Most pure functions should use this flag.
84 ///
85 /// # Availability
86 ///
87 /// Requires SQLite 3.8.3 (2014-02) or later.
88 ///
89 /// # Example
90 ///
91 /// `abs(x)` is deterministic. `random()` is not.
92const DETERMINISTIC = ffi::SQLITE_DETERMINISTIC;
9394/// The function cannot be called from schema objects (views, triggers,
95 /// CHECK constraints, DEFAULT expressions, generated columns, or
96 /// expression indexes).
97 ///
98 /// Use this for functions that:
99 /// - Have side effects (network, file I/O, logging, etc.)
100 /// - Return sensitive application state
101 /// - Should not be triggered by opening an untrusted database
102 ///
103 /// # Availability
104 ///
105 /// Requires SQLite 3.31.0 (2020-01) or later.
106 ///
107 /// # Security Recommendation
108 ///
109 /// Mark all functions with side effects or that expose internal state
110 /// with `DIRECTONLY` to prevent schema injection attacks.
111const DIRECTONLY = ffi::SQLITE_DIRECTONLY;
112113/// The function is safe to call from untrusted schema contexts.
114 ///
115 /// When [`SqliteConnection::set_trusted_schema(false)`][crate::sqlite::SqliteConnection::set_trusted_schema]
116 /// is set, only functions marked `INNOCUOUS` can be called from schema
117 /// objects (views, triggers, CHECK constraints, DEFAULT expressions,
118 /// generated columns, expression indexes).
119 ///
120 /// # Availability
121 ///
122 /// Requires SQLite 3.31.0 (2020-01) or later.
123 ///
124 /// # Safety Requirements
125 ///
126 /// **Only mark a function as INNOCUOUS if it:**
127 /// - Has no side effects
128 /// - Does not reveal internal application state
129 /// - Output depends solely on its input parameters
130 ///
131 /// # Security Warning
132 ///
133 /// Incorrectly marking a function as `INNOCUOUS` can create security
134 /// vulnerabilities when processing untrusted database files. An attacker
135 /// could craft a database with malicious views or triggers that invoke
136 /// your function in unexpected ways.
137const INNOCUOUS = ffi::SQLITE_INNOCUOUS;
138139/// The function may call `sqlite3_value_subtype()` to inspect the
140 /// subtype of its arguments.
141 ///
142 /// # Availability
143 ///
144 /// Requires SQLite 3.30.0 (2019-10) or later.
145const SUBTYPE = ffi::SQLITE_SUBTYPE;
146 }
147}148149impl Defaultfor SqliteFunctionBehavior {
150/// Returns `Self::empty()`
151fn default() -> Self {
152Self::empty()
153 }
154}
155156impl SqliteFunctionBehavior {
157/// Returns the raw flags value including UTF8 encoding flag.
158 ///
159 /// This is used internally when calling `sqlite3_create_function_v2`.
160pub(crate) fn to_flags(self) -> i32 {
161 ffi::SQLITE_UTF8 | self.bits()
162 }
163}