scylla/statement/
mod.rs

1//! This module holds entities representing various kinds of CQL statements,
2//! together with their execution options.
3//! The following statements are supported:
4//! - Query (unprepared statements),
5//! - PreparedStatement,
6//! - Batch.
7
8use std::{sync::Arc, time::Duration};
9
10use thiserror::Error;
11
12use crate::client::execution_profile::ExecutionProfileHandle;
13use crate::observability::history::HistoryListener;
14use crate::policies::retry::RetryPolicy;
15
16pub mod batch;
17pub mod prepared;
18pub mod unprepared;
19
20pub use crate::frame::types::{Consistency, SerialConsistency};
21pub use unprepared::Statement;
22
23// This is the default common to drivers.
24const DEFAULT_PAGE_SIZE: i32 = 5000;
25
26#[derive(Debug, Clone, Default)]
27pub(crate) struct StatementConfig {
28    pub(crate) consistency: Option<Consistency>,
29    pub(crate) serial_consistency: Option<Option<SerialConsistency>>,
30
31    pub(crate) is_idempotent: bool,
32
33    pub(crate) skip_result_metadata: bool,
34    pub(crate) tracing: bool,
35    pub(crate) timestamp: Option<i64>,
36    pub(crate) request_timeout: Option<Duration>,
37
38    pub(crate) history_listener: Option<Arc<dyn HistoryListener>>,
39
40    pub(crate) execution_profile_handle: Option<ExecutionProfileHandle>,
41    pub(crate) retry_policy: Option<Arc<dyn RetryPolicy>>,
42}
43
44impl StatementConfig {
45    /// Determines the consistency of a query
46    #[must_use]
47    pub(crate) fn determine_consistency(&self, default_consistency: Consistency) -> Consistency {
48        self.consistency.unwrap_or(default_consistency)
49    }
50}
51
52#[derive(Debug, Clone, Copy, Error)]
53#[error("Invalid page size provided: {0}; valid values are [1, i32::MAX]")]
54/// Invalid page size was provided.
55pub(crate) struct InvalidPageSize(i32);
56
57/// Size of a single page when performing paged queries to the DB.
58/// Configurable on statements, used in `Session::{query,execute}_{iter,single_page}`.
59#[derive(Debug, Clone, Copy, PartialEq, Eq)]
60pub(crate) struct PageSize(i32);
61
62impl PageSize {
63    /// Creates a new positive page size. If a non-positive number is passed,
64    /// returns an [InvalidPageSize] error.
65    #[inline]
66    pub(crate) fn new(size: i32) -> Result<Self, InvalidPageSize> {
67        if size > 0 {
68            Ok(Self(size))
69        } else {
70            Err(InvalidPageSize(size))
71        }
72    }
73
74    #[inline]
75    pub(crate) fn inner(&self) -> i32 {
76        self.0
77    }
78}
79
80impl Default for PageSize {
81    #[inline]
82    fn default() -> Self {
83        Self(DEFAULT_PAGE_SIZE)
84    }
85}
86
87impl TryFrom<i32> for PageSize {
88    type Error = InvalidPageSize;
89
90    #[inline]
91    fn try_from(value: i32) -> Result<Self, Self::Error> {
92        Self::new(value)
93    }
94}
95
96impl From<PageSize> for i32 {
97    #[inline]
98    fn from(page_size: PageSize) -> Self {
99        page_size.inner()
100    }
101}