pub struct Connection<'c> { /* private fields */ }
Expand description

The connection handle references storage of all information about the connection to the data source, including status, transaction state, and error information.

Implementations§

Safety

Call this method only with a valid (successfully allocated) ODBC connection handle.

Examples found in repository?
src/connection.rs (line 81)
80
81
82
    pub fn into_handle(self) -> handles::Connection<'c> {
        unsafe { handles::Connection::new(ManuallyDrop::new(self).connection.as_sys()) }
    }
More examples
Hide additional examples
src/handles/environment.rs (line 132)
127
128
129
130
131
132
133
134
    pub fn allocate_connection(&self) -> SqlResult<Connection<'_>> {
        let mut handle = null_mut();
        unsafe {
            SQLAllocHandle(HandleType::Dbc, self.as_handle(), &mut handle)
                .into_sql_result("SQLAllocHandle")
                .on_success(|| Connection::new(handle as HDbc))
        }
    }

Directly acces the underlying ODBC handle.

Examples found in repository?
src/connection.rs (line 70)
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
    pub fn into_sys(self) -> HDbc {
        // We do not want to run the drop handler, but transfer ownership instead.
        ManuallyDrop::new(self).connection.as_sys()
    }

    /// Transfer ownership of this open connection to a wrapper around the raw ODBC pointer. The
    /// wrapper allows you to call ODBC functions on the handle, but doesn't care if the connection
    /// is in the right state.
    ///
    /// You should not have a need to call this method if your usecase is covered by this library,
    /// but, in case it is not, this may help you to break out of the type structure which might be
    /// to rigid for you, while simultaniously abondoning its safeguards.
    pub fn into_handle(self) -> handles::Connection<'c> {
        unsafe { handles::Connection::new(ManuallyDrop::new(self).connection.as_sys()) }
    }

Establishes connections to a driver and a data source.

Arguments
  • data_source_name - Data source name. The data might be located on the same computer as the program, or on another computer somewhere on a network.
  • user - User identifier.
  • pwd - Authentication string (typically the password).
Examples found in repository?
src/environment.rs (line 214)
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
    pub fn connect(
        &self,
        data_source_name: &str,
        user: &str,
        pwd: &str,
    ) -> Result<Connection<'_>, Error> {
        let data_source_name = SqlText::new(data_source_name);
        let user = SqlText::new(user);
        let pwd = SqlText::new(pwd);

        let mut connection = self.allocate_connection()?;
        connection
            .connect(&data_source_name, &user, &pwd)
            .into_result(&connection)?;
        Ok(Connection::new(connection))
    }

An alternative to connect. It supports data sources that require more connection information than the three arguments in connect and data sources that are not defined in the system information.

Examples found in repository?
src/environment.rs (line 251)
244
245
246
247
248
249
250
251
252
253
254
    pub fn connect_with_connection_string(
        &self,
        connection_string: &str,
    ) -> Result<Connection<'_>, Error> {
        let connection_string = SqlText::new(connection_string);
        let mut connection = self.allocate_connection()?;
        connection
            .connect_with_connection_string(&connection_string)
            .into_result(&connection)?;
        Ok(Connection::new(connection))
    }

An alternative to connect for connecting with a connection string. Allows for completing a connection string with a GUI prompt on windows.

Return

SqlResult::NoData in case the prompt completing the connection string has been aborted.

Safety

parent_window must either be a valid window handle or NULL.

Examples found in repository?
src/handles/connection.rs (lines 116-121)
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
    pub fn connect_with_connection_string(&mut self, connection_string: &SqlText) -> SqlResult<()> {
        unsafe {
            let parent_window = null_mut();
            let mut completed_connection_string = OutputStringBuffer::empty();

            self.driver_connect(
                connection_string,
                parent_window,
                &mut completed_connection_string,
                DriverConnectOption::NoPrompt,
            )
            // Since we did pass NoPrompt we know the user can not abort the prompt.
            .map(|_connection_string_is_complete| ())
        }
    }
More examples
Hide additional examples
src/environment.rs (lines 413-418)
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
    pub unsafe fn driver_connect_with_hwnd(
        &self,
        connection_string: &str,
        completed_connection_string: &mut OutputStringBuffer,
        driver_completion: DriverCompleteOption,
        parent_window: HWnd,
    ) -> Result<Connection<'_>, Error> {
        let mut connection = self.allocate_connection()?;
        let connection_string = SqlText::new(connection_string);

        let connection_string_is_complete = connection
            .driver_connect(
                &connection_string,
                parent_window,
                completed_connection_string,
                driver_completion.as_sys(),
            )
            .into_result_bool(&connection)?;
        if !connection_string_is_complete {
            return Err(Error::AbortedConnectionStringCompletion);
        }
        Ok(Connection::new(connection))
    }

Disconnect from an ODBC data source.

Examples found in repository?
src/connection.rs (line 15)
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
    fn drop(&mut self) {
        match self.connection.disconnect().into_result(&self.connection) {
            Ok(()) => (),
            Err(Error::Diagnostics {
                record,
                function: _,
            }) if record.state == State::INVALID_STATE_TRANSACTION => {
                // Invalid transaction state. Let's rollback the current transaction and try again.
                if let Err(e) = self.rollback() {
                    // Avoid panicking, if we already have a panic. We don't want to mask the original
                    // error.
                    if !panicking() {
                        panic!(
                            "Unexpected error rolling back transaction (In order to recover \
                                from invalid transaction state during disconnect): {:?}",
                            e
                        )
                    }
                }
                // Transaction is rolled back. Now let's try again to disconnect.
                if let Err(e) = self.connection.disconnect().into_result(&self.connection) {
                    // Avoid panicking, if we already have a panic. We don't want to mask the original
                    // error.
                    if !panicking() {
                        panic!("Unexpected error disconnecting): {:?}", e)
                    }
                }
            }
            Err(e) => {
                // Avoid panicking, if we already have a panic. We don't want to mask the original
                // error.
                if !panicking() {
                    panic!("Unexpected error disconnecting: {:?}", e)
                }
            }
        }
    }

Allocate a new statement handle. The Statement must not outlive the Connection.

Examples found in repository?
src/connection.rs (line 663)
661
662
663
664
665
    fn allocate_statement(&self) -> Result<StatementImpl<'_>, Error> {
        self.connection
            .allocate_statement()
            .into_result(&self.connection)
    }

Specify the transaction mode. By default, ODBC transactions are in auto-commit mode (unless SQLSetConnectAttr and SQLSetConnectOption are not supported, which is unlikely). Switching from manual-commit mode to auto-commit mode automatically commits any open transaction on the connection.

Examples found in repository?
src/connection.rs (line 346)
344
345
346
347
348
    pub fn set_autocommit(&self, enabled: bool) -> Result<(), Error> {
        self.connection
            .set_autocommit(enabled)
            .into_result(&self.connection)
    }

To commit a transaction in manual-commit mode.

Examples found in repository?
src/connection.rs (line 352)
351
352
353
    pub fn commit(&self) -> Result<(), Error> {
        self.connection.commit().into_result(&self.connection)
    }

Roll back a transaction in manual-commit mode.

Examples found in repository?
src/connection.rs (line 357)
356
357
358
    pub fn rollback(&self) -> Result<(), Error> {
        self.connection.rollback().into_result(&self.connection)
    }

Fetch the name of the database management system used by the connection and store it into the provided buf.

Examples found in repository?
src/connection.rs (line 425)
422
423
424
425
426
427
428
429
    pub fn database_management_system_name(&self) -> Result<String, Error> {
        let mut buf = Vec::new();
        self.connection
            .fetch_database_management_system_name(&mut buf)
            .into_result(&self.connection)?;
        let name = slice_to_utf8(&buf).unwrap();
        Ok(name)
    }

Maximum length of catalog names.

Examples found in repository?
src/connection.rs (line 434)
432
433
434
435
436
    pub fn max_catalog_name_len(&self) -> Result<u16, Error> {
        self.connection
            .max_catalog_name_len()
            .into_result(&self.connection)
    }

Maximum length of schema names.

Examples found in repository?
src/connection.rs (line 441)
439
440
441
442
443
    pub fn max_schema_name_len(&self) -> Result<u16, Error> {
        self.connection
            .max_schema_name_len()
            .into_result(&self.connection)
    }

Maximum length of table names.

Examples found in repository?
src/connection.rs (line 448)
446
447
448
449
450
    pub fn max_table_name_len(&self) -> Result<u16, Error> {
        self.connection
            .max_table_name_len()
            .into_result(&self.connection)
    }

Maximum length of column names.

Examples found in repository?
src/connection.rs (line 455)
453
454
455
456
457
    pub fn max_column_name_len(&self) -> Result<u16, Error> {
        self.connection
            .max_column_name_len()
            .into_result(&self.connection)
    }

Fetch the name of the current catalog being used by the connection and store it into the provided buf.

Examples found in repository?
src/connection.rs (line 463)
460
461
462
463
464
465
466
467
    pub fn current_catalog(&self) -> Result<String, Error> {
        let mut buf = Vec::new();
        self.connection
            .fetch_current_catalog(&mut buf)
            .into_result(&self.connection)?;
        let name = slice_to_utf8(&buf).expect("Return catalog must be correctly encoded");
        Ok(name)
    }

Indicates the state of the connection. If true the connection has been lost. If false, the connection is still active.

Examples found in repository?
src/connection.rs (line 363)
362
363
364
    pub fn is_dead(&self) -> Result<bool, Error> {
        self.connection.is_dead().into_result(&self.connection)
    }

Trait Implementations§

The raw underlying ODBC handle used to talk to the ODBC C API. The handle must be valid.
The type of the ODBC handle returned by as_handle. This is a method rather than a constant in order to make the type object safe.
Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.