Skip to main content

odbc_api/
connection.rs

1use crate::{
2    BlockCursorIterator, ColumnsRow, CursorImpl, CursorPolling, Error, ForeignKeysRow, OwnedCursor,
3    ParameterCollectionRef, Preallocated, Prepared, PrimaryKeysRow, Sleep, TablesRow,
4    buffers::BufferDesc,
5    execute::execute_with_parameters_polling,
6    handles::{
7        self, SqlText, State, Statement, StatementConnection, StatementImpl, StatementParent,
8        slice_to_utf8,
9    },
10};
11use log::error;
12use std::{
13    borrow::Cow,
14    fmt::{self, Debug, Display},
15    mem::{ManuallyDrop, MaybeUninit},
16    ptr, str,
17    sync::Arc,
18    thread::panicking,
19};
20
21impl Drop for Connection<'_> {
22    fn drop(&mut self) {
23        match self.connection.disconnect().into_result(&self.connection) {
24            Ok(()) => (),
25            Err(Error::Diagnostics {
26                record,
27                function: _,
28            }) if record.state == State::INVALID_STATE_TRANSACTION => {
29                // Invalid transaction state. Let's rollback the current transaction and try again.
30                if let Err(e) = self.rollback() {
31                    // Connection might be in a suspended state. See documentation about suspended
32                    // state here:
33                    // <https://learn.microsoft.com/sql/odbc/reference/syntax/sqlendtran-function>
34                    //
35                    // See also issue:
36                    // <https://github.com/pacman82/odbc-api/issues/574#issuecomment-2286449125>
37
38                    #[cfg(not(feature = "structured_logging"))]
39                    error!(
40                        "Error rolling back transaction (in order to recover from invalid \
41                        transaction state during disconnect): {e}"
42                    );
43                    #[cfg(feature = "structured_logging")]
44                    error!(
45                        target: "odbc_api",
46                        error:err = e;
47                        "Failed rollback on disconnect"
48                    );
49                }
50                // Transaction might be rolled back or suspended. Now let's try again to disconnect.
51                if let Err(e) = self.connection.disconnect().into_result(&self.connection) {
52                    // Avoid panicking, if we already have a panic. We don't want to mask the
53                    // original error.
54                    if !panicking() {
55                        panic!("Unexpected error disconnecting (after rollback attempt): {e:?}")
56                    }
57                }
58            }
59            Err(e) => {
60                // Avoid panicking, if we already have a panic. We don't want to mask the original
61                // error.
62                if !panicking() {
63                    panic!("Unexpected error disconnecting: {e:?}")
64                }
65            }
66        }
67    }
68}
69
70/// The connection handle references storage of all information about the connection to the data
71/// source, including status, transaction state, and error information.
72///
73/// If you want to enable the connection pooling support build into the ODBC driver manager have a
74/// look at [`crate::Environment::set_connection_pooling`].
75///
76/// In order to create multiple statements with the same connection and for other use cases,
77/// operations like [`Self::execute`] or [`Self::prepare`] are taking a shared reference of `self`
78/// rather than `&mut self`. However, since error handling is done through state changes of the
79/// underlying connection managed by the ODBC driver, this implies that `Connection` must not be
80/// `Sync`.
81pub struct Connection<'c> {
82    connection: handles::Connection<'c>,
83}
84
85impl<'c> Connection<'c> {
86    pub(crate) fn new(connection: handles::Connection<'c>) -> Self {
87        Self { connection }
88    }
89
90    /// Transfer ownership of this open connection to a wrapper around the raw ODBC pointer. The
91    /// wrapper allows you to call ODBC functions on the handle, but doesn't care if the connection
92    /// is in the right state.
93    ///
94    /// You should not have a need to call this method if your use case is covered by this library,
95    /// but, in case it is not, this may help you to break out of the type structure which might be
96    /// to rigid for you, while simultaneously abondoning its safeguards.
97    pub fn into_handle(self) -> handles::Connection<'c> {
98        // We do not want the compiler to invoke `Drop`, since drop would disconnect, yet we want to
99        // transfer ownership to the connection handle.
100        let dont_drop_me = MaybeUninit::new(self);
101        let self_ptr = dont_drop_me.as_ptr();
102
103        // Safety: We know `dont_drop_me` is (still) valid at this point so reading the ptr is okay
104        unsafe { ptr::read(&(*self_ptr).connection) }
105    }
106
107    /// Executes an SQL statement. This is the fastest way to submit an SQL statement for one-time
108    /// execution. In case you do **not** want to execute more statements on this connection, you
109    /// may want to use [`Self::into_cursor`] instead, which would create a cursor taking ownership
110    /// of the connection.
111    ///
112    /// # Parameters
113    ///
114    /// * `query`: The text representation of the SQL statement. E.g. "SELECT * FROM my_table;".
115    /// * `params`: `?` may be used as a placeholder in the statement text. You can use `()` to
116    ///   represent no parameters. See the [`crate::parameter`] module level documentation for more
117    ///   information on how to pass parameters.
118    /// * `query_timeout_sec`: Use this to limit the time the query is allowed to take, before
119    ///   responding with data to the application. The driver may replace the number of seconds you
120    ///   provide with a minimum or maximum value.
121    ///
122    ///   For the timeout to work the driver must support this feature. E.g. PostgreSQL, and
123    ///   Microsoft SQL Server do, but SQLite or MariaDB do not.
124    ///
125    ///   You can specify ``0``, to deactivate the timeout, this is the default. So if you want no
126    ///   timeout, just leave it at `None`. Only reason to specify ``0`` is if for some reason your
127    ///   datasource does not have ``0`` as default.
128    ///
129    ///   This corresponds to `SQL_ATTR_QUERY_TIMEOUT` in the ODBC C API.
130    ///
131    ///   See: <https://learn.microsoft.com/en-us/sql/odbc/reference/syntax/sqlsetstmtattr-function>
132    ///
133    /// # Return
134    ///
135    /// Returns `Some` if a cursor is created. If `None` is returned no cursor has been created (
136    /// e.g. the query came back empty). Note that an empty query may also create a cursor with zero
137    /// rows.
138    ///
139    /// # Example
140    ///
141    /// ```no_run
142    /// use odbc_api::{Environment, ConnectionOptions};
143    ///
144    /// let env = Environment::new()?;
145    ///
146    /// let mut conn = env.connect(
147    ///     "YourDatabase", "SA", "My@Test@Password1",
148    ///     ConnectionOptions::default()
149    /// )?;
150    /// // This query does not use any parameters.
151    /// let query_params = ();
152    /// let timeout_sec = None;
153    /// if let Some(cursor) = conn.execute(
154    ///     "SELECT year, name FROM Birthdays;",
155    ///     query_params,
156    ///     timeout_sec)?
157    /// {
158    ///     // Use cursor to process query results.
159    /// }
160    /// # Ok::<(), odbc_api::Error>(())
161    /// ```
162    pub fn execute(
163        &self,
164        query: &str,
165        params: impl ParameterCollectionRef,
166        query_timeout_sec: Option<usize>,
167    ) -> Result<Option<CursorImpl<StatementImpl<'_>>>, Error> {
168        // Only allocate the statement, if we know we are going to execute something.
169        if params.parameter_set_size() == 0 {
170            return Ok(None);
171        }
172        let mut statement = self.preallocate()?;
173        if let Some(seconds) = query_timeout_sec {
174            statement.set_query_timeout_sec(seconds)?;
175        }
176        statement.into_cursor(query, params)
177    }
178
179    /// Executes an SQL statement asynchronously using polling mode. ⚠️**Attention**⚠️: Please read
180    /// [Asynchronous execution using polling
181    /// mode](crate::guide#asynchronous-execution-using-polling-mode) before using this
182    /// functions.
183    ///
184    /// Asynchronous sibling of [`Self::execute`]. Each time the driver returns control to your
185    /// application the future returned by `sleep` is awaited, before the driver is polled again.
186    /// This avoids a busy loop. `sleep` is a synchronous factor for a future which is awaited.
187    /// `sleep` should not be implemented using a sleep which blocks the system thread, but rather
188    /// use methods provided by your asynchronous runtime. E.g.:
189    ///
190    /// ```
191    /// use odbc_api::{Connection, IntoParameter, Error};
192    /// use std::time::Duration;
193    ///
194    /// async fn insert_post<'a>(
195    ///     connection: &'a Connection<'a>,
196    ///     user: &str,
197    ///     post: &str,
198    /// ) -> Result<(), Error> {
199    ///     // Poll every 50 ms.
200    ///     let sleep = || tokio::time::sleep(Duration::from_millis(50));
201    ///     let sql = "INSERT INTO POSTS (user, post) VALUES (?, ?)";
202    ///     // Execute query using ODBC polling method
203    ///     let params = (&user.into_parameter(), &post.into_parameter());
204    ///     connection.execute_polling(&sql, params, sleep).await?;
205    ///     Ok(())
206    /// }
207    /// ```
208    ///
209    /// **Attention**: This feature requires driver support, otherwise the calls will just block
210    /// until they are finished. At the time of writing this out of Microsoft SQL Server,
211    /// PostgerSQL, SQLite and MariaDB this worked only with Microsoft SQL Server. For code generic
212    /// over every driver you may still use this. The functions will return with the correct results
213    /// just be aware that may block until they are finished.
214    ///
215    /// This uses the ODBC polling mode under the hood. See:
216    /// <https://learn.microsoft.com/sql/odbc/reference/develop-app/asynchronous-execution-polling-method>
217    pub async fn execute_polling(
218        &self,
219        query: &str,
220        params: impl ParameterCollectionRef,
221        sleep: impl Sleep,
222    ) -> Result<Option<CursorPolling<StatementImpl<'_>>>, Error> {
223        // Only allocate the statement, if we know we are going to execute something.
224        if params.parameter_set_size() == 0 {
225            return Ok(None);
226        }
227        let query = SqlText::new(query);
228        let mut statement = self.allocate_statement()?;
229        statement.set_async_enable(true).into_result(&statement)?;
230        execute_with_parameters_polling(statement, Some(&query), params, sleep).await
231    }
232
233    /// Similar to [`Self::execute`], but takes ownership of the connection. This is useful if e.g.
234    /// youwant to open a connection and execute a query in a function and return a self containing
235    /// cursor.
236    ///
237    /// # Parameters
238    ///
239    /// * `query`: The text representation of the SQL statement. E.g. "SELECT * FROM my_table;".
240    /// * `params`: `?` may be used as a placeholder in the statement text. You can use `()` to
241    ///   represent no parameters. See the [`crate::parameter`] module level documentation for more
242    ///   information on how to pass parameters.
243    /// * `query_timeout_sec`: Use this to limit the time the query is allowed to take, before
244    ///   responding with data to the application. The driver may replace the number of seconds you
245    ///   provide with a minimum or maximum value.
246    ///
247    ///   For the timeout to work the driver must support this feature. E.g. PostgreSQL, and
248    ///   Microsoft SQL Server do, but SQLite or MariaDB do not.
249    ///
250    ///   You can specify ``0``, to deactivate the timeout, this is the default. So if you want no
251    ///   timeout, just leave it at `None`. Only reason to specify ``0`` is if for some reason your
252    ///   datasource does not have ``0`` as default.
253    ///
254    ///   This corresponds to `SQL_ATTR_QUERY_TIMEOUT` in the ODBC C API.
255    ///
256    ///   See: <https://learn.microsoft.com/en-us/sql/odbc/reference/syntax/sqlsetstmtattr-function>
257    ///
258    /// ```no_run
259    /// use odbc_api::{environment, Error, Cursor, ConnectionOptions};
260    ///
261    ///
262    /// const CONNECTION_STRING: &str =
263    ///     "Driver={ODBC Driver 18 for SQL Server};\
264    ///     Server=localhost;UID=SA;\
265    ///     PWD=My@Test@Password1;";
266    ///
267    /// fn execute_query(query: &str) -> Result<Option<impl Cursor>, Error> {
268    ///     let env = environment()?;
269    ///     let conn = env.connect_with_connection_string(
270    ///         CONNECTION_STRING,
271    ///         ConnectionOptions::default()
272    ///     )?;
273    ///
274    ///     // connect.execute(&query, (), None) // Compiler error: Would return local ref to
275    ///                                          // `conn`.
276    ///
277    ///     let maybe_cursor = conn.into_cursor(&query, (), None)?;
278    ///     Ok(maybe_cursor)
279    /// }
280    /// ```
281    pub fn into_cursor(
282        self,
283        query: &str,
284        params: impl ParameterCollectionRef,
285        query_timeout_sec: Option<usize>,
286    ) -> Result<Option<OwnedCursor<Connection<'c>>>, ConnectionAndError<'c>> {
287        // With the current Rust version the borrow checker needs some convincing, so that it allows
288        // us to return the Connection, even though the Result of execute borrows it.
289        let mut error = None;
290        let mut cursor = None;
291        match self.execute(query, params, query_timeout_sec) {
292            Ok(Some(c)) => cursor = Some(c),
293            Ok(None) => return Ok(None),
294            Err(e) => error = Some(e),
295        };
296        if let Some(e) = error {
297            drop(cursor);
298            return Err(ConnectionAndError {
299                error: e,
300                previous: self,
301            });
302        }
303        let cursor = cursor.unwrap();
304        // The rust compiler needs some help here. It assumes otherwise that the lifetime of the
305        // resulting cursor would depend on the lifetime of `params`.
306        let mut cursor = ManuallyDrop::new(cursor);
307        let handle = cursor.as_sys();
308        // Safe: `handle` is a valid statement, and we are giving up ownership of `self`.
309        let statement = unsafe { StatementConnection::new(handle, self) };
310        // Safe: `statement is in the cursor state`.
311        let cursor = unsafe { CursorImpl::new(statement) };
312        Ok(Some(cursor))
313    }
314
315    /// Prepares an SQL statement. This is recommended for repeated execution of similar queries.
316    ///
317    /// Should your use case require you to execute the same query several times with different
318    /// parameters, prepared queries are the way to go. These give the database a chance to cache
319    /// the access plan associated with your SQL statement. It is not unlike compiling your program
320    /// once and executing it several times.
321    ///
322    /// ```
323    /// use odbc_api::{Connection, Error, IntoParameter};
324    /// use std::io::{self, stdin, Read};
325    ///
326    /// fn interactive(conn: &Connection) -> io::Result<()>{
327    ///     let mut prepared = conn.prepare("SELECT * FROM Movies WHERE title=?;").unwrap();
328    ///     let mut title = String::new();
329    ///     stdin().read_line(&mut title)?;
330    ///     while !title.is_empty() {
331    ///         match prepared.execute(&title.as_str().into_parameter()) {
332    ///             Err(e) => println!("{}", e),
333    ///             // Most drivers would return a result set even if no Movie with the title is found,
334    ///             // the result set would just be empty. Well, most drivers.
335    ///             Ok(None) => println!("No result set generated."),
336    ///             Ok(Some(cursor)) => {
337    ///                 // ...print cursor contents...
338    ///             }
339    ///         }
340    ///         stdin().read_line(&mut title)?;
341    ///     }
342    ///     Ok(())
343    /// }
344    /// ```
345    ///
346    /// # Parameters
347    ///
348    /// * `query`: The text representation of the SQL statement. E.g. "SELECT * FROM my_table;". `?`
349    ///   may be used as a placeholder in the statement text, to be replaced with parameters during
350    ///   execution.
351    pub fn prepare(&self, query: &str) -> Result<Prepared<StatementImpl<'_>>, Error> {
352        let query = SqlText::new(query);
353        let mut stmt = self.allocate_statement()?;
354        stmt.prepare(&query).into_result(&stmt)?;
355        Ok(Prepared::new(stmt))
356    }
357
358    /// Prepares an SQL statement which takes ownership of the connection. The advantage over
359    /// [`Self::prepare`] is, that you do not need to keep track of the lifetime of the connection
360    /// seperatly and can create types which do own the prepared query and only depend on the
361    /// lifetime of the environment. The downside is that you can not use the connection for
362    /// anything else anymore.
363    ///
364    /// # Parameters
365    ///
366    /// * `query`: The text representation of the SQL statement. E.g. "SELECT * FROM my_table;". `?`
367    ///   may be used as a placeholder in the statement text, to be replaced with parameters during
368    ///   execution.
369    ///
370    /// ```no_run
371    /// use odbc_api::{
372    ///     environment, Error, ColumnarBulkInserter, handles::StatementConnection, BindParamDesc,
373    ///     buffers::BoxColumnBuffer, ConnectionOptions, Connection, parameter::WithDataType,
374    /// };
375    ///
376    /// const CONNECTION_STRING: &str =
377    ///     "Driver={ODBC Driver 18 for SQL Server};\
378    ///     Server=localhost;UID=SA;\
379    ///     PWD=My@Test@Password1;";
380    ///
381    /// /// Supports columnar bulk inserts on a heterogenous schema (columns have different types),
382    /// /// takes ownership of a connection created using an environment with static lifetime.
383    /// type Inserter = ColumnarBulkInserter<
384    ///     StatementConnection<Connection<'static>>,
385    ///     WithDataType<BoxColumnBuffer>
386    /// >;
387    ///
388    /// /// Creates an inserter which can be reused to bulk insert birthyears with static lifetime.
389    /// fn make_inserter(query: &str) -> Result<Inserter, Error> {
390    ///     let env = environment()?;
391    ///     let conn = env.connect_with_connection_string(
392    ///         CONNECTION_STRING,
393    ///         ConnectionOptions::default()
394    ///     )?;
395    ///     let prepared = conn.into_prepared("INSERT INTO Birthyear (name, year) VALUES (?, ?)")?;
396    ///     let params = [
397    ///         BindParamDesc::text(255),
398    ///         BindParamDesc::i16(false),
399    ///     ];
400    ///     let capacity = 400;
401    ///     prepared.into_column_inserter(capacity, params)
402    /// }
403    /// ```
404    pub fn into_prepared(
405        self,
406        query: &str,
407    ) -> Result<Prepared<StatementConnection<Connection<'c>>>, Error> {
408        let query = SqlText::new(query);
409        let mut stmt = self.allocate_statement()?;
410        stmt.prepare(&query).into_result(&stmt)?;
411        // Safe: `handle` is a valid statement, and we are giving up ownership of `self`.
412        let stmt = unsafe { StatementConnection::new(stmt.into_sys(), self) };
413        Ok(Prepared::new(stmt))
414    }
415
416    /// Allocates an SQL statement handle. This is recommended if you want to sequentially execute
417    /// different queries over the same connection, as you avoid the overhead of allocating a
418    /// statement handle for each query.
419    ///
420    /// Should you want to repeatedly execute the same query with different parameters try
421    /// [`Self::prepare`] instead.
422    ///
423    /// # Example
424    ///
425    /// ```
426    /// use odbc_api::{Connection, Error};
427    /// use std::io::{self, stdin, Read};
428    ///
429    /// fn interactive(conn: &Connection) -> io::Result<()>{
430    ///     let mut statement = conn.preallocate().unwrap();
431    ///     let mut query = String::new();
432    ///     stdin().read_line(&mut query)?;
433    ///     while !query.is_empty() {
434    ///         match statement.execute(&query, ()) {
435    ///             Err(e) => println!("{}", e),
436    ///             Ok(None) => println!("No results set generated."),
437    ///             Ok(Some(cursor)) => {
438    ///                 // ...print cursor contents...
439    ///             },
440    ///         }
441    ///         stdin().read_line(&mut query)?;
442    ///     }
443    ///     Ok(())
444    /// }
445    /// ```
446    pub fn preallocate(&self) -> Result<Preallocated<StatementImpl<'_>>, Error> {
447        let stmt = self.allocate_statement()?;
448        unsafe { Ok(Preallocated::new(stmt)) }
449    }
450
451    /// Creates a preallocated statement handle like [`Self::preallocate`]. Yet the statement handle
452    /// also takes ownership of the connection.
453    pub fn into_preallocated(
454        self,
455    ) -> Result<Preallocated<StatementConnection<Connection<'c>>>, Error> {
456        let stmt = self.allocate_statement()?;
457        // Safe: We know `stmt` is a valid statement handle and self is the connection which has
458        // been used to allocate it.
459        unsafe {
460            let stmt = StatementConnection::new(stmt.into_sys(), self);
461            Ok(Preallocated::new(stmt))
462        }
463    }
464
465    /// Specify the transaction mode. By default, ODBC transactions are in auto-commit mode.
466    /// Switching from manual-commit mode to auto-commit mode automatically commits any open
467    /// transaction on the connection. There is no open or begin transaction method. Each statement
468    /// execution automatically starts a new transaction or adds to the existing one.
469    ///
470    /// In manual commit mode you can use [`Connection::commit`] or [`Connection::rollback`]. Keep
471    /// in mind, that even `SELECT` statements can open new transactions. This library will rollback
472    /// open transactions if a connection goes out of SCOPE. This however will log an error, since
473    /// the transaction state is only discovered during a failed disconnect. It is preferable that
474    /// the application makes sure all transactions are closed if in manual commit mode.
475    pub fn set_autocommit(&self, enabled: bool) -> Result<(), Error> {
476        self.connection
477            .set_autocommit(enabled)
478            .into_result(&self.connection)
479    }
480
481    /// To commit a transaction in manual-commit mode.
482    pub fn commit(&self) -> Result<(), Error> {
483        self.connection.commit().into_result(&self.connection)
484    }
485
486    /// To rollback a transaction in manual-commit mode.
487    pub fn rollback(&self) -> Result<(), Error> {
488        self.connection.rollback().into_result(&self.connection)
489    }
490
491    /// Indicates the state of the connection. If `true` the connection has been lost. If `false`,
492    /// the connection is still active.
493    pub fn is_dead(&self) -> Result<bool, Error> {
494        self.connection.is_dead().into_result(&self.connection)
495    }
496
497    /// Network packet size in bytes. Requries driver support.
498    pub fn packet_size(&self) -> Result<u32, Error> {
499        self.connection.packet_size().into_result(&self.connection)
500    }
501
502    /// Get the name of the database management system used by the connection.
503    pub fn database_management_system_name(&self) -> Result<String, Error> {
504        let mut buf = Vec::new();
505        self.connection
506            .fetch_database_management_system_name(&mut buf)
507            .into_result(&self.connection)?;
508        let name = slice_to_utf8(&buf).unwrap();
509        Ok(name)
510    }
511
512    /// Maximum length of catalog names.
513    pub fn max_catalog_name_len(&self) -> Result<u16, Error> {
514        self.connection
515            .max_catalog_name_len()
516            .into_result(&self.connection)
517    }
518
519    /// Maximum length of schema names.
520    pub fn max_schema_name_len(&self) -> Result<u16, Error> {
521        self.connection
522            .max_schema_name_len()
523            .into_result(&self.connection)
524    }
525
526    /// Maximum length of table names.
527    pub fn max_table_name_len(&self) -> Result<u16, Error> {
528        self.connection
529            .max_table_name_len()
530            .into_result(&self.connection)
531    }
532
533    /// Maximum length of column names.
534    pub fn max_column_name_len(&self) -> Result<u16, Error> {
535        self.connection
536            .max_column_name_len()
537            .into_result(&self.connection)
538    }
539
540    /// Get the name of the current catalog being used by the connection.
541    pub fn current_catalog(&self) -> Result<String, Error> {
542        let mut buf = Vec::new();
543        self.connection
544            .fetch_current_catalog(&mut buf)
545            .into_result(&self.connection)?;
546        let name = slice_to_utf8(&buf).expect("Return catalog must be correctly encoded");
547        Ok(name)
548    }
549
550    /// An iterator over the columns of tables matching the patterns. Patterns support `%` for
551    /// multiple characters or `_` for a single character. Use `\` to escape.
552    ///
553    /// Returns an iterator over [`ColumnsRow`] items. If you need the raw cursor (e.g. to access
554    /// driver-specific columns beyond the standard 18), use [`Preallocated::columns_cursor`]
555    /// instead.
556    pub fn columns(
557        &self,
558        catalog_name: &str,
559        schema_name: &str,
560        table_name: &str,
561        column_name: &str,
562    ) -> Result<BlockCursorIterator<CursorImpl<StatementImpl<'_>>, ColumnsRow>, Error> {
563        let stmt = self.preallocate()?;
564        stmt.into_columns(catalog_name, schema_name, table_name, column_name)
565    }
566
567    /// List tables, schemas, views and catalogs of a datasource.
568    ///
569    /// # Parameters
570    ///
571    /// * `catalog_name`: Filter result by catalog name. Accept search patterns. Use `%` to match
572    ///   any number of characters. Use `_` to match exactly on character. Use `\` to escape
573    ///   characeters.
574    /// * `schema_name`: Filter result by schema. Accepts patterns in the same way as
575    ///   `catalog_name`.
576    /// * `table_name`: Filter result by table. Accepts patterns in the same way as `catalog_name`.
577    /// * `table_type`: Filters results by table type. E.g: 'TABLE', 'VIEW'. This argument accepts a
578    ///   comma separeted list of table types. Omit it to not filter the result by table type at
579    ///   all.
580    ///
581    /// # Example
582    ///
583    /// ```
584    /// use odbc_api::{Connection, Error, TablesRow};
585    ///
586    /// fn print_all_tables(conn: &Connection<'_>) -> Result<(), Error> {
587    ///     for row in conn.tables("", "", "", "")? {
588    ///         let row: TablesRow = row?;
589    ///         let table = row.table.as_str().unwrap().unwrap_or("NULL");
590    ///         let catalog = row.catalog.as_str().unwrap().unwrap_or("NULL");
591    ///         let schema = row.schema.as_str().unwrap().unwrap_or("NULL");
592    ///         println!("{catalog}.{schema}.{table}");
593    ///     }
594    ///     Ok(())
595    /// }
596    /// ```
597    pub fn tables(
598        &self,
599        catalog_name: &str,
600        schema_name: &str,
601        table_name: &str,
602        table_type: &str,
603    ) -> Result<BlockCursorIterator<CursorImpl<StatementImpl<'_>>, TablesRow>, Error> {
604        let statement = self.preallocate()?;
605        statement.into_tables(catalog_name, schema_name, table_name, table_type)
606    }
607
608    /// Create a result set which contains the column names that make up the primary key for the
609    /// table.
610    ///
611    /// # Parameters
612    ///
613    /// * `catalog_name`: Catalog name. If a driver supports catalogs for some tables but not for
614    ///   others, such as when the driver retrieves data from different DBMSs, an empty string ("")
615    ///   denotes those tables that do not have catalogs. `catalog_name` must not contain a string
616    ///   search pattern.
617    /// * `schema_name`: Schema name. If a driver supports schemas for some tables but not for
618    ///   others, such as when the driver retrieves data from different DBMSs, an empty string ("")
619    ///   denotes those tables that do not have schemas. `schema_name` must not contain a string
620    ///   search pattern.
621    /// * `table_name`: Table name. `table_name` must not contain a string search pattern.
622    ///
623    /// The resulting result set contains the following columns:
624    ///
625    /// * `TABLE_CAT`: Primary key table catalog name. NULL if not applicable to the data source. If
626    ///   a driver supports catalogs for some tables but not for others, such as when the driver
627    ///   retrieves data from different DBMSs, it returns an empty string ("") for those tables that
628    ///   do not have catalogs. `VARCHAR`
629    /// * `TABLE_SCHEM`: Primary key table schema name; NULL if not applicable to the data source.
630    ///   If a driver supports schemas for some tables but not for others, such as when the driver
631    ///   retrieves data from different DBMSs, it returns an empty string ("") for those tables that
632    ///   do not have schemas. `VARCHAR`
633    /// * `TABLE_NAME`: Primary key table name. `VARCHAR NOT NULL`
634    /// * `COLUMN_NAME`: Primary key column name. The driver returns an empty string for a column
635    ///   that does not have a name. `VARCHAR NOT NULL`
636    /// * `KEY_SEQ`: Column sequence number in key (starting with 1). `SMALLINT NOT NULL`
637    /// * `PK_NAME`: Primary key name. NULL if not applicable to the data source. `VARCHAR`
638    ///
639    /// The maximum length of the VARCHAR columns is driver specific.
640    ///
641    /// If [`crate::sys::StatementAttribute::MetadataId`] statement attribute is set to true,
642    /// catalog, schema and table name parameters are treated as an identifiers and their case is
643    /// not significant. If it is false, they are ordinary arguments. As such they treated literally
644    /// and their case is significant.
645    ///
646    /// See: <https://learn.microsoft.com/sql/odbc/reference/syntax/sqlprimarykeys-function>
647    pub fn primary_keys(
648        &self,
649        catalog_name: Option<&str>,
650        schema_name: Option<&str>,
651        table_name: &str,
652    ) -> Result<BlockCursorIterator<CursorImpl<StatementImpl<'_>>, PrimaryKeysRow>, Error> {
653        let stmt = self.preallocate()?;
654        stmt.into_primary_keys(catalog_name, schema_name, table_name)
655    }
656
657    /// This can be used to retrieve either a list of foreign keys in the specified table or a list
658    /// of foreign keys in other table that refer to the primary key of the specified table.
659    ///
660    /// See: <https://learn.microsoft.com/en-us/sql/odbc/reference/syntax/sqlforeignkeys-function>
661    pub fn foreign_keys(
662        &self,
663        pk_catalog_name: &str,
664        pk_schema_name: &str,
665        pk_table_name: &str,
666        fk_catalog_name: &str,
667        fk_schema_name: &str,
668        fk_table_name: &str,
669    ) -> Result<BlockCursorIterator<CursorImpl<StatementImpl<'_>>, ForeignKeysRow>, Error> {
670        let statement = self.preallocate()?;
671        statement.into_foreign_keys(
672            pk_catalog_name,
673            pk_schema_name,
674            pk_table_name,
675            fk_catalog_name,
676            fk_schema_name,
677            fk_table_name,
678        )
679    }
680
681    /// The buffer descriptions for all standard buffers (not including extensions) returned in the
682    /// columns query (e.g. [`Preallocated::columns_cursor`]).
683    ///
684    /// # Arguments
685    ///
686    /// * `type_name_max_len` - The maximum expected length of type names.
687    /// * `remarks_max_len` - The maximum expected length of remarks.
688    /// * `column_default_max_len` - The maximum expected length of column defaults.
689    #[deprecated(
690        note = "Use `Connection::columns` or `Preallocated::columns` which return strongly typed \
691        `ColumnsRow` items instead."
692    )]
693    pub fn columns_buffer_descs(
694        &self,
695        type_name_max_len: usize,
696        remarks_max_len: usize,
697        column_default_max_len: usize,
698    ) -> Result<Vec<BufferDesc>, Error> {
699        let null_i16 = BufferDesc::I16 { nullable: true };
700
701        let not_null_i16 = BufferDesc::I16 { nullable: false };
702
703        let null_i32 = BufferDesc::I32 { nullable: true };
704
705        // The definitions for these descriptions are taken from the documentation of `SQLColumns`
706        // located at https://docs.microsoft.com/en-us/sql/odbc/reference/syntax/sqlcolumns-function
707        let catalog_name_desc = BufferDesc::Text {
708            max_str_len: self.max_catalog_name_len()? as usize,
709        };
710
711        let schema_name_desc = BufferDesc::Text {
712            max_str_len: self.max_schema_name_len()? as usize,
713        };
714
715        let table_name_desc = BufferDesc::Text {
716            max_str_len: self.max_table_name_len()? as usize,
717        };
718
719        let column_name_desc = BufferDesc::Text {
720            max_str_len: self.max_column_name_len()? as usize,
721        };
722
723        let data_type_desc = not_null_i16;
724
725        let type_name_desc = BufferDesc::Text {
726            max_str_len: type_name_max_len,
727        };
728
729        let column_size_desc = null_i32;
730        let buffer_len_desc = null_i32;
731        let decimal_digits_desc = null_i16;
732        let precision_radix_desc = null_i16;
733        let nullable_desc = not_null_i16;
734
735        let remarks_desc = BufferDesc::Text {
736            max_str_len: remarks_max_len,
737        };
738
739        let column_default_desc = BufferDesc::Text {
740            max_str_len: column_default_max_len,
741        };
742
743        let sql_data_type_desc = not_null_i16;
744        let sql_datetime_sub_desc = null_i16;
745        let char_octet_len_desc = null_i32;
746        let ordinal_pos_desc = BufferDesc::I32 { nullable: false };
747
748        // We expect strings to be `YES`, `NO`, or a zero-length string, so `3` should be
749        // sufficient.
750        const IS_NULLABLE_LEN_MAX_LEN: usize = 3;
751        let is_nullable_desc = BufferDesc::Text {
752            max_str_len: IS_NULLABLE_LEN_MAX_LEN,
753        };
754
755        Ok(vec![
756            catalog_name_desc,
757            schema_name_desc,
758            table_name_desc,
759            column_name_desc,
760            data_type_desc,
761            type_name_desc,
762            column_size_desc,
763            buffer_len_desc,
764            decimal_digits_desc,
765            precision_radix_desc,
766            nullable_desc,
767            remarks_desc,
768            column_default_desc,
769            sql_data_type_desc,
770            sql_datetime_sub_desc,
771            char_octet_len_desc,
772            ordinal_pos_desc,
773            is_nullable_desc,
774        ])
775    }
776
777    fn allocate_statement(&self) -> Result<StatementImpl<'_>, Error> {
778        self.connection
779            .allocate_statement()
780            .into_result(&self.connection)
781    }
782}
783
784/// Implement `Debug` for [`Connection`], in order to play nice with derive Debugs for struct
785/// holding a [`Connection`].
786impl Debug for Connection<'_> {
787    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
788        write!(f, "Connection")
789    }
790}
791
792/// We need to implement [`StatementParent`] for [`Connection`] in order to express ownership of a
793/// connection for a statement handle. This is e.g. needed for [`Connection::into_cursor`].
794///
795/// # Safety:
796///
797/// Connection wraps an open Connection. It keeps the handle alive and valid during its lifetime.
798unsafe impl StatementParent for Connection<'_> {}
799
800/// We need to implement [`StatementParent`] for `Arc<Connection>` in order to be able to express
801/// ownership of a shared connection from a statement handle. This is e.g. needed for
802/// [`ConnectionTransitions::into_cursor`].
803///
804/// # Safety:
805///
806/// `Arc<Connection>` wraps an open Connection. It keeps the handle alive and valid during its
807/// lifetime.
808unsafe impl StatementParent for Arc<Connection<'_>> {}
809
810/// Options to be passed then opening a connection to a datasource.
811#[derive(Default, Clone, Copy)]
812pub struct ConnectionOptions {
813    /// Number of seconds to wait for a login request to complete before returning to the
814    /// application. The default is driver-dependent. If `0` the timeout is disabled and a
815    /// connection attempt will wait indefinitely.
816    ///
817    /// If the specified timeout exceeds the maximum login timeout in the data source, the driver
818    /// substitutes that value and uses the maximum login timeout instead.
819    ///
820    /// This corresponds to the `SQL_ATTR_LOGIN_TIMEOUT` attribute in the ODBC specification.
821    ///
822    /// See:
823    /// <https://learn.microsoft.com/en-us/sql/odbc/reference/syntax/sqlsetconnectattr-function>
824    pub login_timeout_sec: Option<u32>,
825    /// Packet size in bytes. Not all drivers support this option.
826    pub packet_size: Option<u32>,
827}
828
829impl ConnectionOptions {
830    /// Set the attributes corresponding to the connection options to an allocated connection
831    /// handle. Usually you would rather provide the options then creating the connection with e.g.
832    /// [`crate::Environment::connect_with_connection_string`] rather than calling this method
833    /// yourself.
834    pub fn apply(&self, handle: &handles::Connection) -> Result<(), Error> {
835        if let Some(timeout) = self.login_timeout_sec {
836            handle.set_login_timeout_sec(timeout).into_result(handle)?;
837        }
838        if let Some(packet_size) = self.packet_size {
839            handle.set_packet_size(packet_size).into_result(handle)?;
840        }
841        Ok(())
842    }
843}
844
845/// You can use this method to escape a password so it is suitable to be appended to an ODBC
846/// connection string as the value for the `PWD` attribute. This method is only of interest for
847/// application in need to create their own connection strings.
848///
849/// See:
850///
851/// * <https://stackoverflow.com/questions/22398212/escape-semicolon-in-odbc-connection-string-in-app-config-file>
852/// * <https://docs.microsoft.com/en-us/dotnet/api/system.data.odbc.odbcconnection.connectionstring>
853///
854/// # Example
855///
856/// ```
857/// use odbc_api::escape_attribute_value;
858///
859/// let password = "abc;123}";
860/// let user = "SA";
861/// let mut connection_string_without_credentials =
862///     "Driver={ODBC Driver 18 for SQL Server};Server=localhost;";
863///
864/// let connection_string = format!(
865///     "{}UID={};PWD={};",
866///     connection_string_without_credentials,
867///     user,
868///     escape_attribute_value(password)
869/// );
870///
871/// assert_eq!(
872///     "Driver={ODBC Driver 18 for SQL Server};Server=localhost;UID=SA;PWD={abc;123}}};",
873///     connection_string
874/// );
875/// ```
876///
877/// ```
878/// use odbc_api::escape_attribute_value;
879/// assert_eq!("abc", escape_attribute_value("abc"));
880/// assert_eq!("ab}c", escape_attribute_value("ab}c"));
881/// assert_eq!("{ab;c}", escape_attribute_value("ab;c"));
882/// assert_eq!("{a}}b;c}", escape_attribute_value("a}b;c"));
883/// assert_eq!("{ab+c}", escape_attribute_value("ab+c"));
884/// ```
885pub fn escape_attribute_value(unescaped: &str) -> Cow<'_, str> {
886    // Search the string for semicolon (';') if we do not find any, nothing is to do and we can work
887    // without an extra allocation.
888    //
889    // * We escape ';' because it serves as a separator between key=value pairs
890    // * We escape '+' because passwords with `+` must be escaped on PostgreSQL for some reason.
891    if unescaped.contains(&[';', '+'][..]) {
892        // Surround the string with curly braces ('{','}') and escape every closing curly brace by
893        // repeating it.
894        let escaped = unescaped.replace('}', "}}");
895        Cow::Owned(format!("{{{escaped}}}"))
896    } else {
897        Cow::Borrowed(unescaped)
898    }
899}
900
901/// A pair of the error and the previous state, before the operation caused the error.
902///
903/// Some functions in this crate take a `self` and return another type in the result to express a
904/// state transitions in the underlying ODBC handle. In order to make such operations retryable, or
905/// offer other alternatives of recovery, they may return this error type instead of a plain
906/// [`Error`].
907#[derive(Debug)]
908pub struct FailedStateTransition<S> {
909    /// The ODBC error which caused the state transition to fail.
910    pub error: Error,
911    /// The state before the transition failed. This is useful to e.g. retry the operation, or
912    /// recover in another way.
913    pub previous: S,
914}
915
916impl<S> From<FailedStateTransition<S>> for Error {
917    fn from(value: FailedStateTransition<S>) -> Self {
918        value.error
919    }
920}
921
922impl<S> Display for FailedStateTransition<S> {
923    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
924        write!(f, "{}", self.error)
925    }
926}
927
928impl<S> std::error::Error for FailedStateTransition<S>
929where
930    S: Debug,
931{
932    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
933        self.error.source()
934    }
935}
936
937/// An error type wrapping an [`Error`] and a [`Connection`]. It is used by
938/// [`Connection::into_cursor`], so that in case of failure the user can reuse the connection to try
939/// again. [`Connection::into_cursor`] could achieve the same by returning a tuple in case of an
940/// error, but this type causes less friction in most scenarios because [`Error`] implements
941/// [`From`] [`ConnectionAndError`] and it therfore works with the question mark operater (`?`).
942type ConnectionAndError<'conn> = FailedStateTransition<Connection<'conn>>;
943
944/// Ability to transition ownership of the connection to various children which represent statement
945/// handles in various states. E.g. [`crate::Prepared`] or [`crate::Cursor`]. Transfering ownership
946/// of the connection could e.g. be useful if you want to clean the connection after you are done
947/// with the child.
948///
949/// Having this in a trait rather than directly on [`Connection`] allows us to be generic over the
950/// type of ownership we express. E.g. we can express shared ownership of a connection by
951/// using an `Arc<Mutex<Connection>>` or `Arc<Connection>`. Or a still exclusive ownership using
952/// a plain [`Connection`].
953pub trait ConnectionTransitions: Sized {
954    // Note to self. This might eveolve into a `Connection` trait. Which expresses ownership
955    // of a connection (shared or not). It could allow to get a dereferened borrowed conection
956    // which does not allow for state transtions as of now (like StatementRef). I may not want to
957    // rock the boat that much right now.
958
959    /// The type passed to [crate::handles::StatementConnection] to express ownership of the
960    /// connection.
961    type StatementParent: StatementParent;
962
963    /// Similar to [`crate::Connection::into_cursor`], yet it operates on an
964    /// `Arc<Mutex<Connection>>`. `Arc<Connection>` can be used if you want shared ownership of
965    /// connections. However, `Arc<Connection>` is not `Send` due to `Connection` not being `Sync`.
966    /// So sometimes you may want to wrap your `Connection` into an `Arc<Mutex<Connection>>` to
967    /// allow shared ownership of the connection across threads. This function allows you to create
968    /// a cursor from such a shared which also holds a strong reference to it.
969    ///
970    /// # Parameters
971    ///
972    /// * `query`: The text representation of the SQL statement. E.g. "SELECT * FROM my_table;".
973    /// * `params`: `?` may be used as a placeholder in the statement text. You can use `()` to
974    ///   represent no parameters. See the [`crate::parameter`] module level documentation for more
975    ///   information on how to pass parameters.
976    /// * `query_timeout_sec`: Use this to limit the time the query is allowed to take, before
977    ///   responding with data to the application. The driver may replace the number of seconds you
978    ///   provide with a minimum or maximum value.
979    ///
980    ///   For the timeout to work the driver must support this feature. E.g. PostgreSQL, and
981    ///   Microsoft SQL Server do, but SQLite or MariaDB do not.
982    ///
983    ///   You can specify ``0``, to deactivate the timeout, this is the default. So if you want no
984    ///   timeout, just leave it at `None`. Only reason to specify ``0`` is if for some reason your
985    ///   datasource does not have ``0`` as default.
986    ///
987    ///   This corresponds to `SQL_ATTR_QUERY_TIMEOUT` in the ODBC C API.
988    ///
989    ///   See: <https://learn.microsoft.com/en-us/sql/odbc/reference/syntax/sqlsetstmtattr-function>
990    fn into_cursor(
991        self,
992        query: &str,
993        params: impl ParameterCollectionRef,
994        query_timeout_sec: Option<usize>,
995    ) -> Result<Option<OwnedCursor<Self::StatementParent>>, FailedStateTransition<Self>>;
996
997    /// Prepares an SQL statement which takes ownership of the connection. The advantage over
998    /// [`Connection::prepare`] is, that you do not need to keep track of the lifetime of the
999    /// connection seperatly and can create types which do own the prepared query and only depend on
1000    /// the lifetime of the environment.
1001    ///
1002    /// # Parameters
1003    ///
1004    /// * `query`: The text representation of the SQL statement. E.g. "SELECT * FROM my_table;". `?`
1005    ///   may be used as a placeholder in the statement text, to be replaced with parameters during
1006    ///   execution.
1007    ///
1008    /// ```no_run
1009    /// use odbc_api::{
1010    ///     environment, Error, ColumnarBulkInserter, ConnectionTransitions, Connection,
1011    ///     handles::StatementConnection, buffers::BoxColumnBuffer, ConnectionOptions,
1012    ///     BindParamDesc, parameter::WithDataType,
1013    /// };
1014    ///
1015    /// const CONNECTION_STRING: &str =
1016    ///     "Driver={ODBC Driver 18 for SQL Server};\
1017    ///     Server=localhost;UID=SA;\
1018    ///     PWD=My@Test@Password1;";
1019    ///
1020    /// /// Supports columnar bulk inserts on a heterogenous schema (columns have different types),
1021    /// /// takes ownership of a connection created using an environment with static lifetime.
1022    /// type Inserter = ColumnarBulkInserter<
1023    ///     StatementConnection<Connection<'static>>,
1024    ///     WithDataType<BoxColumnBuffer>
1025    /// >;
1026    ///
1027    /// /// Creates an inserter which can be reused to bulk insert birthyears with static lifetime.
1028    /// fn make_inserter(query: &str) -> Result<Inserter, Error> {
1029    ///     let env = environment()?;
1030    ///     let conn = env.connect_with_connection_string(
1031    ///         CONNECTION_STRING,
1032    ///         ConnectionOptions::default()
1033    ///     )?;
1034    ///     let prepared = conn.into_prepared("INSERT INTO Birthyear (name, year) VALUES (?, ?)")?;
1035    ///     let buffers = [
1036    ///         BindParamDesc::text(255),
1037    ///         BindParamDesc::i16(false),
1038    ///     ];
1039    ///     let capacity = 400;
1040    ///     prepared.into_column_inserter(capacity, buffers)
1041    /// }
1042    /// ```
1043    fn into_prepared(
1044        self,
1045        query: &str,
1046    ) -> Result<Prepared<StatementConnection<Self::StatementParent>>, Error>;
1047
1048    /// Creates a preallocated statement handle like [`Connection::preallocate`]. Yet the statement
1049    /// also takes ownership of the connection.
1050    fn into_preallocated(
1051        self,
1052    ) -> Result<Preallocated<StatementConnection<Self::StatementParent>>, Error>;
1053}
1054
1055impl<'env> ConnectionTransitions for Connection<'env> {
1056    type StatementParent = Self;
1057
1058    fn into_cursor(
1059        self,
1060        query: &str,
1061        params: impl ParameterCollectionRef,
1062        query_timeout_sec: Option<usize>,
1063    ) -> Result<Option<OwnedCursor<Self>>, FailedStateTransition<Self>> {
1064        self.into_cursor(query, params, query_timeout_sec)
1065    }
1066
1067    fn into_prepared(self, query: &str) -> Result<Prepared<StatementConnection<Self>>, Error> {
1068        self.into_prepared(query)
1069    }
1070
1071    fn into_preallocated(self) -> Result<Preallocated<StatementConnection<Self>>, Error> {
1072        self.into_preallocated()
1073    }
1074}
1075
1076impl<'env> ConnectionTransitions for Arc<Connection<'env>> {
1077    type StatementParent = Self;
1078
1079    fn into_cursor(
1080        self,
1081        query: &str,
1082        params: impl ParameterCollectionRef,
1083        query_timeout_sec: Option<usize>,
1084    ) -> Result<Option<OwnedCursor<Self>>, FailedStateTransition<Self>> {
1085        // Result borrows the connection. We convert the cursor into a raw pointer, to not confuse
1086        // the borrow checker.
1087        let result = self.execute(query, params, query_timeout_sec);
1088        let maybe_stmt_ptr = result
1089            .map(|opt| opt.map(|cursor| cursor.into_stmt().into_sys()))
1090            .map_err(|error| {
1091                // If the execute fails, we return a FailedStateTransition with the error and the
1092                // connection.
1093                FailedStateTransition {
1094                    error,
1095                    previous: Arc::clone(&self),
1096                }
1097            })?;
1098        let Some(stmt_ptr) = maybe_stmt_ptr else {
1099            return Ok(None);
1100        };
1101        // Safe: The connection is the parent of the statement referenced by `stmt_ptr`.
1102        let stmt = unsafe { StatementConnection::new(stmt_ptr, self) };
1103        // Safe: `stmt` is valid and in cursor state.
1104        let cursor = unsafe { CursorImpl::new(stmt) };
1105        Ok(Some(cursor))
1106    }
1107
1108    fn into_prepared(self, query: &str) -> Result<Prepared<StatementConnection<Self>>, Error> {
1109        let stmt = self.prepare(query)?;
1110        let stmt_ptr = stmt.into_handle().into_sys();
1111        // Safe: The connection is the parent of the statement referenced by `stmt_ptr`.
1112        let stmt = unsafe { StatementConnection::new(stmt_ptr, self) };
1113        // `stmt` is valid and in prepared state.
1114        let prepared = Prepared::new(stmt);
1115        Ok(prepared)
1116    }
1117
1118    fn into_preallocated(self) -> Result<Preallocated<StatementConnection<Self>>, Error> {
1119        let stmt = self.preallocate()?;
1120        let stmt_ptr = stmt.into_handle().into_sys();
1121        // Safe: The connection is the parent of the statement referenced by `stmt_ptr`.
1122        let stmt = unsafe { StatementConnection::new(stmt_ptr, self) };
1123        // Safe: `stmt` is valid and its state is allocated.
1124        let preallocated = unsafe { Preallocated::new(stmt) };
1125        Ok(preallocated)
1126    }
1127}