scylla/statement/
unprepared.rs

1use super::{PageSize, StatementConfig};
2use crate::client::execution_profile::ExecutionProfileHandle;
3use crate::frame::types::{Consistency, SerialConsistency};
4use crate::observability::history::HistoryListener;
5use crate::policies::retry::RetryPolicy;
6use std::sync::Arc;
7use std::time::Duration;
8
9/// **Unprepared** CQL statement.
10///
11/// This represents a CQL statement that can be executed on a server.
12#[derive(Clone)]
13pub struct Statement {
14    pub(crate) config: StatementConfig,
15
16    pub contents: String,
17    page_size: PageSize,
18}
19
20impl Statement {
21    /// Creates a new [`Statement`] from a CQL statement string.
22    pub fn new(query_text: impl Into<String>) -> Self {
23        Self {
24            contents: query_text.into(),
25            page_size: PageSize::default(),
26            config: Default::default(),
27        }
28    }
29
30    /// Returns self with page size set to the given value.
31    ///
32    /// Panics if given number is nonpositive.
33    pub fn with_page_size(mut self, page_size: i32) -> Self {
34        self.set_page_size(page_size);
35        self
36    }
37
38    /// Sets the page size for this CQL statement.
39    ///
40    /// Panics if given number is nonpositive.
41    pub fn set_page_size(&mut self, page_size: i32) {
42        self.page_size = page_size
43            .try_into()
44            .unwrap_or_else(|err| panic!("Query::set_page_size: {err}"));
45    }
46
47    /// Returns the page size for this CQL statement.
48    pub(crate) fn get_validated_page_size(&self) -> PageSize {
49        self.page_size
50    }
51
52    /// Returns the page size for this CQL statement.
53    pub fn get_page_size(&self) -> i32 {
54        self.page_size.inner()
55    }
56
57    /// Sets the consistency to be used when executing this statement.
58    pub fn set_consistency(&mut self, c: Consistency) {
59        self.config.consistency = Some(c);
60    }
61
62    /// Gets the consistency to be used when executing this statement if it is filled.
63    /// If this is empty, the default_consistency of the session will be used.
64    pub fn get_consistency(&self) -> Option<Consistency> {
65        self.config.consistency
66    }
67
68    /// Sets the serial consistency to be used when executing this statement.
69    /// (Ignored unless the statement is an LWT)
70    pub fn set_serial_consistency(&mut self, sc: Option<SerialConsistency>) {
71        self.config.serial_consistency = Some(sc);
72    }
73
74    /// Gets the serial consistency to be used when executing this statement.
75    /// (Ignored unless the statement is an LWT)
76    pub fn get_serial_consistency(&self) -> Option<SerialConsistency> {
77        self.config.serial_consistency.flatten()
78    }
79
80    /// Sets the idempotence of this statement
81    /// A statement is idempotent if it can be applied multiple times without changing the result of the initial application
82    /// If set to `true` we can be sure that it is idempotent
83    /// If set to `false` it is unknown whether it is idempotent
84    /// This is used in [`RetryPolicy`] to decide if retrying a statement execution is safe
85    pub fn set_is_idempotent(&mut self, is_idempotent: bool) {
86        self.config.is_idempotent = is_idempotent;
87    }
88
89    /// Gets the idempotence of this statement
90    pub fn get_is_idempotent(&self) -> bool {
91        self.config.is_idempotent
92    }
93
94    /// Enable or disable CQL Tracing for this statement
95    /// If enabled session.query() will return a QueryResult containing tracing_id
96    /// which can be used to query tracing information about the execution of this query
97    pub fn set_tracing(&mut self, should_trace: bool) {
98        self.config.tracing = should_trace;
99    }
100
101    /// Gets whether tracing is enabled for this statement
102    pub fn get_tracing(&self) -> bool {
103        self.config.tracing
104    }
105
106    /// Sets the default timestamp for this statement in microseconds.
107    /// If not None, it will replace the server side assigned timestamp as default timestamp
108    /// If a statement contains a `USING TIMESTAMP` clause, calling this method won't change
109    /// anything
110    pub fn set_timestamp(&mut self, timestamp: Option<i64>) {
111        self.config.timestamp = timestamp
112    }
113
114    /// Gets the default timestamp for this statement in microseconds.
115    pub fn get_timestamp(&self) -> Option<i64> {
116        self.config.timestamp
117    }
118
119    /// Sets the client-side timeout for this statement.
120    /// If not None, the driver will stop waiting for the request
121    /// to finish after `timeout` passed.
122    /// Otherwise, default session client timeout will be applied.
123    pub fn set_request_timeout(&mut self, timeout: Option<Duration>) {
124        self.config.request_timeout = timeout
125    }
126
127    /// Gets client timeout associated with this statement.
128    pub fn get_request_timeout(&self) -> Option<Duration> {
129        self.config.request_timeout
130    }
131
132    /// Set the retry policy for this statement, overriding the one from execution profile if not None.
133    #[inline]
134    pub fn set_retry_policy(&mut self, retry_policy: Option<Arc<dyn RetryPolicy>>) {
135        self.config.retry_policy = retry_policy;
136    }
137
138    /// Get the retry policy set for the statement.
139    #[inline]
140    pub fn get_retry_policy(&self) -> Option<&Arc<dyn RetryPolicy>> {
141        self.config.retry_policy.as_ref()
142    }
143
144    /// Sets the listener capable of listening what happens during statement execution.
145    pub fn set_history_listener(&mut self, history_listener: Arc<dyn HistoryListener>) {
146        self.config.history_listener = Some(history_listener);
147    }
148
149    /// Removes the listener set by `set_history_listener`.
150    pub fn remove_history_listener(&mut self) -> Option<Arc<dyn HistoryListener>> {
151        self.config.history_listener.take()
152    }
153
154    /// Associates the query with execution profile referred by the provided handle.
155    /// Handle may be later remapped to another profile, and query will reflect those changes.
156    pub fn set_execution_profile_handle(&mut self, profile_handle: Option<ExecutionProfileHandle>) {
157        self.config.execution_profile_handle = profile_handle;
158    }
159
160    /// Borrows the execution profile handle associated with this statement.
161    pub fn get_execution_profile_handle(&self) -> Option<&ExecutionProfileHandle> {
162        self.config.execution_profile_handle.as_ref()
163    }
164}
165
166impl From<String> for Statement {
167    fn from(s: String) -> Statement {
168        Statement::new(s)
169    }
170}
171
172impl<'a> From<&'a str> for Statement {
173    fn from(s: &'a str) -> Statement {
174        Statement::new(s.to_owned())
175    }
176}