DataSource

Struct DataSource 

Source
pub struct DataSource<'env, S: HDbcWrapper<'env> = Unconnected<'env>> { /* private fields */ }
Expand description

A DataSource is used to query and manipulate a data source.

  • The state of the connection
  • The current connection-level diagnostics
  • The handles of statements and descriptors currently allocated on the connection
  • The current settings of each connection attribute

§States

A DataSource is in one of two states Connected or Unconnected. These are modeled in the type at compile time. Every new DataSource starts out as Unconnected. To execute a query it needs to be Connected. You can achieve this by calling e.g. connect and capture the result in a new binding which will be of type DataSource::<'env, Connected<'env>>.

See [Connection Handles in the ODBC Reference][1] [1]: https://docs.microsoft.com/sql/odbc/reference/develop-app/connection-handles

Implementations§

Source§

impl<'env, Any> DataSource<'env, Any>
where Any: HDbcWrapper<'env>,

Source

pub fn into_raw(self) -> SQLHDBC

Consumes the DataSource, returning the wrapped raw SQLHDBC

Leaks the Connection Handle. This is usually done in order to pass ownership from Rust to another language. After calling this method, the caller is responsible for invoking SQLFreeHandle.

Source

pub fn as_raw(&self) -> SQLHDBC

Provides access to the raw ODBC Connection Handle

Source

pub unsafe fn from_raw(raw: SQLHDBC) -> Self

May only be invoked with a valid Statement Handle which has been allocated using SQLAllocHandle. Special care must be taken that the Connection Handle passed is in a State which matches the type.

Source§

impl<'env> DataSource<'env, Unconnected<'env>>

Source

pub fn with_parent<V>(parent: &'env Environment<V>) -> Return<Self>
where V: Version,

Allocates a new DataSource. A DataSource may not outlive its parent Environment.

See [Allocating a Connection Handle ODBC][1] [1]: https://docs.microsoft.com/sql/odbc/reference/develop-app/allocating-a-connection-handle-odbc

Examples found in repository?
examples/prepared_query.rs (line 23)
19fn connect<V>(env: &Environment<V>) -> Connection<impl AutocommitMode>
20where
21    V: Version,
22{
23    let conn = DataSource::with_parent(env).unwrap();
24    conn.connect("TestDataSource", "", "").unwrap()
25}
More examples
Hide additional examples
examples/bind_columns.rs (line 67)
63fn connect<V>(env: &Environment<V>) -> MyResult<Connection<impl AutocommitMode>>
64where
65    V: Version,
66{
67    let conn = DataSource::with_parent(env).unwrap();
68    conn.connect("TestDataSource", "", "").into_result()
69}
examples/print_table.rs (line 67)
63fn connect<V>(env: &Environment<V>) -> MyResult<Connection<impl AutocommitMode>>
64where
65    V: Version,
66{
67    let conn = DataSource::with_parent(env).unwrap();
68    conn.connect("TestDataSource", "", "").into_result()
69}
examples/affected_row_count.rs (line 9)
6fn main() {
7    let env = Environment::new().unwrap();
8    let env = env.declare_version_3().unwrap();
9    let conn = DataSource::with_parent(&env).unwrap();
10    let conn = conn.connect("TestDataSource", "", "").unwrap();
11    exec(&conn, "INSERT INTO movies (title, year) VALUES ('TEST movie', 9999), ('TEST movie', 9998)");
12    exec(&conn, "DELETE FROM movies WHERE title = 'TEST movie'");
13}
examples/transactions.rs (line 9)
5fn main() {
6    let env = Environment::new().unwrap();
7    let env = env.declare_version_3().unwrap();
8
9    let ds = DataSource::with_parent(&env).unwrap();
10    let conn = ds.connect("TestDataSource", "", "").unwrap();
11    let mut conn = conn.disable_autocommit().unwrap();
12
13    {
14        //Any statement now will start transaction which could be ended with conn.commit() or conn.rollback()
15        //If either commit or rollback was not called before connection drop automatic rollback will be issued
16        let stmt = Statement::with_parent(&conn).unwrap();
17        let res = stmt.exec_direct("SELECT 'HELLO' FROM MOVIES");
18        println!("Result {:?}", res);
19    }
20
21    let end_tx_result = conn.commit();
22
23    println!("End TX result {:?}", end_tx_result);
24}
Source

pub fn connect<DSN, U, P>( self, data_source_name: &DSN, user: &U, pwd: &P, ) -> Return<Connection<'env, AutocommitOn>, DataSource<'env, Unconnected<'env>>>
where DSN: SqlStr + ?Sized, U: SqlStr + ?Sized, P: SqlStr + ?Sized,

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

  • See [Connecting with SQLConnect][1]
  • See [SQLConnectFunction][2]
§State transition

On success this method changes the Connection handles state from Allocated to Connected . Since this state change is expressed in the type system, the method consumes self. And returns a new instance in the result type.

§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 - Authenticatien string (typically the password). [1]: https://docs.microsoft.com/sql/odbc/reference/syntax/sqlconnect-function [2]: https://docs.microsoft.com/sql/odbc/reference/syntax/sqlconnect-function
Examples found in repository?
examples/prepared_query.rs (line 24)
19fn connect<V>(env: &Environment<V>) -> Connection<impl AutocommitMode>
20where
21    V: Version,
22{
23    let conn = DataSource::with_parent(env).unwrap();
24    conn.connect("TestDataSource", "", "").unwrap()
25}
More examples
Hide additional examples
examples/bind_columns.rs (line 68)
63fn connect<V>(env: &Environment<V>) -> MyResult<Connection<impl AutocommitMode>>
64where
65    V: Version,
66{
67    let conn = DataSource::with_parent(env).unwrap();
68    conn.connect("TestDataSource", "", "").into_result()
69}
examples/print_table.rs (line 68)
63fn connect<V>(env: &Environment<V>) -> MyResult<Connection<impl AutocommitMode>>
64where
65    V: Version,
66{
67    let conn = DataSource::with_parent(env).unwrap();
68    conn.connect("TestDataSource", "", "").into_result()
69}
examples/affected_row_count.rs (line 10)
6fn main() {
7    let env = Environment::new().unwrap();
8    let env = env.declare_version_3().unwrap();
9    let conn = DataSource::with_parent(&env).unwrap();
10    let conn = conn.connect("TestDataSource", "", "").unwrap();
11    exec(&conn, "INSERT INTO movies (title, year) VALUES ('TEST movie', 9999), ('TEST movie', 9998)");
12    exec(&conn, "DELETE FROM movies WHERE title = 'TEST movie'");
13}
examples/transactions.rs (line 10)
5fn main() {
6    let env = Environment::new().unwrap();
7    let env = env.declare_version_3().unwrap();
8
9    let ds = DataSource::with_parent(&env).unwrap();
10    let conn = ds.connect("TestDataSource", "", "").unwrap();
11    let mut conn = conn.disable_autocommit().unwrap();
12
13    {
14        //Any statement now will start transaction which could be ended with conn.commit() or conn.rollback()
15        //If either commit or rollback was not called before connection drop automatic rollback will be issued
16        let stmt = Statement::with_parent(&conn).unwrap();
17        let res = stmt.exec_direct("SELECT 'HELLO' FROM MOVIES");
18        println!("Result {:?}", res);
19    }
20
21    let end_tx_result = conn.commit();
22
23    println!("End TX result {:?}", end_tx_result);
24}
Source

pub fn connect_with_connection_string<C>( self, connection_string: &C, ) -> Return<Connection<'env, AutocommitOn>, Self>
where C: SqlStr + ?Sized,

Connects to a data source using a connection string.

For the syntax regarding the connections string see [SQLDriverConnect][1]. This method is equivalent of calling odbc_sys::SQLDriverConnect with the SQL_DRIVER_NOPROMPT parameter.

See [Choosing a Data Source or Driver][2] [1]: https://docs.microsoft.com/sql/odbc/reference/syntax/sqldriverconnect-function [2]: https://docs.microsoft.com/sql/odbc/reference/develop-app/choosing-a-data-source-or-driver

Source§

impl<'env, AC: AutocommitMode> DataSource<'env, Connected<'env, AC>>

Source

pub fn disconnect( self, ) -> Return<DataSource<'env, Unconnected<'env>>, Connection<'env, AC>>

When an application has finished using a data source, it calls disconnect. disconnect disconnects the driver from the data source.

  • See [Disconnecting from a Data Source or Driver][1]
  • See [SQLDisconnect Function][2] [1]: https://docs.microsoft.com/sql/odbc/reference/develop-app/disconnecting-from-a-data-source-or-driver [2]: https://docs.microsoft.com/sql/odbc/reference/syntax/sqldisconnect-function
Source

pub fn is_read_only(&mut self) -> Return<bool>

true if the data source is set to READ ONLY mode, false otherwise.

Source§

impl<'env> DataSource<'env, Connected<'env, AutocommitOff>>

Source

pub fn enable_autocommit(self) -> Return<Connection<'env, AutocommitOn>, Self>

Set autocommit mode on, per ODBC spec triggers implicit commit of any running transaction

Source

pub fn commit(&mut self) -> Return<()>

Commit transaction if any, can be safely called and will be no-op if no transaction present or autocommit mode is enabled

Examples found in repository?
examples/transactions.rs (line 21)
5fn main() {
6    let env = Environment::new().unwrap();
7    let env = env.declare_version_3().unwrap();
8
9    let ds = DataSource::with_parent(&env).unwrap();
10    let conn = ds.connect("TestDataSource", "", "").unwrap();
11    let mut conn = conn.disable_autocommit().unwrap();
12
13    {
14        //Any statement now will start transaction which could be ended with conn.commit() or conn.rollback()
15        //If either commit or rollback was not called before connection drop automatic rollback will be issued
16        let stmt = Statement::with_parent(&conn).unwrap();
17        let res = stmt.exec_direct("SELECT 'HELLO' FROM MOVIES");
18        println!("Result {:?}", res);
19    }
20
21    let end_tx_result = conn.commit();
22
23    println!("End TX result {:?}", end_tx_result);
24}
Source

pub fn rollback(&mut self) -> Return<()>

Rollback transaction if any, can be safely called and will be no-op if no transaction present or autocommit mode is enabled

Source§

impl<'env> DataSource<'env, Connected<'env, AutocommitOn>>

Source

pub fn disable_autocommit(self) -> Return<Connection<'env, AutocommitOff>, Self>

Set autocommit mode off

Examples found in repository?
examples/transactions.rs (line 11)
5fn main() {
6    let env = Environment::new().unwrap();
7    let env = env.declare_version_3().unwrap();
8
9    let ds = DataSource::with_parent(&env).unwrap();
10    let conn = ds.connect("TestDataSource", "", "").unwrap();
11    let mut conn = conn.disable_autocommit().unwrap();
12
13    {
14        //Any statement now will start transaction which could be ended with conn.commit() or conn.rollback()
15        //If either commit or rollback was not called before connection drop automatic rollback will be issued
16        let stmt = Statement::with_parent(&conn).unwrap();
17        let res = stmt.exec_direct("SELECT 'HELLO' FROM MOVIES");
18        println!("Result {:?}", res);
19    }
20
21    let end_tx_result = conn.commit();
22
23    println!("End TX result {:?}", end_tx_result);
24}

Trait Implementations§

Source§

impl<'env, S: Debug + HDbcWrapper<'env>> Debug for DataSource<'env, S>
where S::Handle: Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'env, S> Diagnostics for DataSource<'env, S>
where S: HDbcWrapper<'env>,

Source§

fn diagnostics( &self, rec_number: SQLSMALLINT, message_text: &mut [SQLCHAR], ) -> ReturnOption<DiagResult>

Returns the current values of multiple fields of a diagnostic record that contains error, warning, and status information. Read more

Auto Trait Implementations§

§

impl<'env, S> Freeze for DataSource<'env, S>
where <S as HDbcWrapper<'env>>::Handle: Freeze,

§

impl<'env, S> RefUnwindSafe for DataSource<'env, S>
where <S as HDbcWrapper<'env>>::Handle: RefUnwindSafe,

§

impl<'env, S> Send for DataSource<'env, S>
where <S as HDbcWrapper<'env>>::Handle: Send,

§

impl<'env, S> Sync for DataSource<'env, S>
where <S as HDbcWrapper<'env>>::Handle: Sync,

§

impl<'env, S> Unpin for DataSource<'env, S>
where <S as HDbcWrapper<'env>>::Handle: Unpin,

§

impl<'env, S> UnwindSafe for DataSource<'env, S>
where <S as HDbcWrapper<'env>>::Handle: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.