Struct oracle::Connection [−][src]
pub struct Connection { /* fields omitted */ }Expand description
Connection to an Oracle database
Implementations
Connects to an Oracle server using username, password and connect string.
If you need to connect the server with additional parameters
such as SYSDBA privilege, use Connector instead.
Examples
Connect to a local database.
let conn = Connection::connect("scott", "tiger", "")?;
Connect to a remote database specified by easy connect naming.
let conn = Connection::connect("scott", "tiger", "server_name:1521/service_name")?;
Closes the connection before the end of lifetime.
This fails when open statements or LOBs exist.
Creates StatementBuilder to create a Statement
Examples
Executes a SQL statement with different parameters.
let mut stmt = conn.statement("insert into emp(empno, ename) values (:id, :name)").build()?; let emp_list = [ (7369, "Smith"), (7499, "Allen"), (7521, "Ward"), ]; // insert rows using positional parameters for emp in &emp_list { stmt.execute(&[&emp.0, &emp.1])?; } let emp_list = [ (7566, "Jones"), (7654, "Martin"), (7698, "Blake"), ]; // insert rows using named parameters for emp in &emp_list { stmt.execute_named(&[("id", &emp.0), ("name", &emp.1)])?; }
Query methods in Connection allocate memory for 100 rows by default
to reduce the number of network round trips in case that many rows are
fetched. When 100 isn’t preferable, use StatementBuilder::fetch_array_size
to customize it.
// fetch top 10 rows. let mut stmt = conn .statement("select empno, ename from emp order by empno fetch first 10 rows only") .fetch_array_size(10) .build()?; for row_result in stmt.query_as::<(i32, String)>(&[])? { let (empno, ename) = row_result?; println!("empno: {}, ename: {}", empno, ename); }
By default, a maximum of 2 rows are returned when the query is first
executed. To modify this, use StatementBuilder::prefetch_rows to customize
it. For more information on the difference between this and fetch_array_size,
see this writeup
or this description.
// fetch top 10 rows. let mut stmt = conn .statement("select empno, ename from emp order by empno fetch first 10 rows only") .prefetch_rows(11) // add one to avoid a round-trip to check for end-of-fetch .build()?; for row_result in stmt.query_as::<(i32, String)>(&[])? { let (empno, ename) = row_result?; println!("empno: {}, ename: {}", empno, ename); }
Creates a Statement with parameters
This will be marked as deprecated at version 0.6.x. Please use statement(sql).build()
instead. The statement() method was added to follow this API guideline.
Examples
Executes a SQL statement with different parameters.
let mut stmt = conn.prepare("insert into emp(empno, ename) values (:id, :name)", &[])?; let emp_list = [ (7369, "Smith"), (7499, "Allen"), (7521, "Ward"), ]; // insert rows using positional parameters for emp in &emp_list { stmt.execute(&[&emp.0, &emp.1])?; } let emp_list = [ (7566, "Jones"), (7654, "Martin"), (7698, "Blake"), ]; // insert rows using named parameters for emp in &emp_list { stmt.execute_named(&[("id", &emp.0), ("name", &emp.1)])?; }
Query methods in Connection allocate memory for 100 rows by default
to reduce the number of network round trips in case that many rows are
fetched. When 100 isn’t preferable, use StmtParam::FetchArraySize(u32)
to customize it.
// fetch top 10 rows. let mut stmt = conn.prepare("select * from (select empno, ename from emp order by empno) where rownum <= 10", &[StmtParam::FetchArraySize(10)])?; for row_result in stmt.query_as::<(i32, String)>(&[])? { let (empno, ename) = row_result?; println!("empno: {}, ename: {}", empno, ename); }
pub fn batch<'conn, 'sql>(
    &'conn self, 
    sql: &'sql str, 
    max_batch_size: usize
) -> BatchBuilder<'conn, 'sql>
pub fn batch<'conn, 'sql>(
    &'conn self, 
    sql: &'sql str, 
    max_batch_size: usize
) -> BatchBuilder<'conn, 'sql>
Creates BatchBuilder
See Batch.
Executes a select statement and returns a result set containing Rows.
See Query Methods.
Executes a select statement using named parameters and returns a result set containing Rows.
See Query Methods.
Executes a select statement and returns a result set containing RowValues.
See Query Methods.
Executes a select statement using named parameters and returns a result set containing RowValues.
See Query Methods.
Gets one row from a query using positoinal bind parameters.
See Query Methods.
Gets one row from a query using named bind parameters.
See Query Methods.
Gets one row from a query as specified type.
See Query Methods.
Gets one row from a query with named bind parameters as specified type.
See Query Methods.
Creates a statement, binds values by position and executes it in one call.
It will retunrs Err when the statemnet is a select statement.
Examples
let conn = Connection::connect("scott", "tiger", "")?; // execute a statement without bind parameters conn.execute("insert into emp(empno, ename) values (113, 'John')", &[])?; // execute a statement with binding parameters by position conn.execute("insert into emp(empno, ename) values (:1, :2)", &[&114, &"Smith"])?;
Creates a statement, binds values by name and executes it in one call.
It will retunrs Err when the statemnet is a select statement.
The bind variable names are compared case-insensitively.
Examples
let conn = Connection::connect("scott", "tiger", "")?; // execute a statement with binding parameters by name conn.execute_named("insert into emp(empno, ename) values (:id, :name)", &[("id", &114), ("name", &"Smith")])?;
Gets autocommit mode. It is false by default.
Enables or disables autocommit mode. It is disabled by default.
Cancels execution of running statements in the connection
Gets an object type information from name
let conn = Connection::connect("scott", "tiger", "")?; let objtype = conn.object_type("MDSYS.SDO_GEOMETRY");
Note that the object type is cached in the connection. However when “CREATE TYPE”, “ALTER TYPE” or “DROP TYPE” is executed, the cache clears.
Clear the object type cache in the connection.
See also object_type.
Gets information about the server version
NOTE: if you connect to Oracle Database 18 or higher with Oracle client libraries 12.2 or lower, it gets the base version (such as 18.0.0.0.0) instead of the full version (such as 18.3.0.0.0).
Examples
let conn = Connection::connect("scott", "tiger", "")?; let (version, banner) = conn.server_version()?; println!("Oracle Version: {}", version); println!("--- Version Banner ---"); println!("{}", banner); println!("---------------------");
Changes the password for the specified user
Pings the connection to see if it is still alive.
It checks the connection by making a network round-trip between the client and the server.
See also Connection::status.
Gets the status of the connection.
It returns Ok(ConnStatus::Closed) when the connection was closed
by Connection::close.
Otherwise see bellow.
Oracle client 12.2 and later:
It checks whether the underlying TCP socket has disconnected by the server. There is no guarantee that the server is alive and the network between the client and server has no trouble.
For example, it returns Ok(ConnStatus::NotConnected) when the
database on the server-side OS stopped and the client received
a FIN or RST packet. However it returns Ok(ConnStatus::Normal)
when the server-side OS itself crashes or the network is in
trouble.
Oracle client 11.2 and 12.1:
It returns Ok(ConnStatus::Normal) when the last network
round-trip between the client and server went through. Otherwise,
Ok(ConnStatus::NotConnected). There is no guarantee that the
next network round-trip will go through.
See also Connection::ping.
Gets the statement cache size
Sets the statement cache size
Gets the current call timeout used for round-trips to
the database made with this connection. None means that no timeouts
will take place.
Sets the call timeout to be used for round-trips to the database made with this connection. None means that no timeouts will take place.
The call timeout value applies to each database round-trip individually, not to the sum of all round-trips. Time spent processing in rust-oracle before or after the completion of each round-trip is not counted.
- 
If the time from the start of any one round-trip to the completion of that same round-trip exceeds call timeout, then the operation is halted and an exception occurs. 
- 
In the case where an rust-oracle operation requires more than one round-trip and each round-trip takes less than call timeout, then no timeout will occur, even if the sum of all round-trip calls exceeds call timeout. 
- 
If no round-trip is required, the operation will never be interrupted. 
After a timeout is triggered, rust-oracle attempts to clean up the
internal connection state. The cleanup is allowed to take another
duration.
If the cleanup was successful, an exception DPI-1067 will be raised but the application can continue to use the connection.
For small values of call timeout, the connection cleanup may not complete successfully within the additional call timeout period. In this case an exception ORA-3114 is raised and the connection will no longer be usable. It should be closed.
Gets current schema associated with the connection
Sets current schema associated with the connection
Gets external name associated with the connection
Sets external name associated with the connection
Gets internal name associated with the connection
Sets internal name associated with the connection
Sets module associated with the connection
This is same with calling DBMS_APPLICATION_INFO.SET_MODULE but without executing a statement. The module name is piggybacked to the server with the next network round-trip.
Sets action associated with the connection
This is same with calling DBMS_APPLICATION_INFO.SET_ACTION but without executing a statement. The action name is piggybacked to the server with the next network round-trip.
Sets client info associated with the connection
This is same with calling DBMS_APPLICATION_INFO.SET_CLIENT_INFO but without executing a statement. The client info is piggybacked to the server with the next network round-trip.
Sets client identifier associated with the connection
This is same with calling DBMS_SESSION.SET_IDENTIFIER but without executing a statement. The client identifier is piggybacked to the server with the next network round-trip.
Sets name of the database operation to be monitored in the database.
Sets to '' if you want to end monitoring the current running database operation.
This is same with calling DBMS_SQL_MONITOR.BEGIN_OPERATION but without executing a statement. The database operation name is piggybacked to the server with the next network round-trip.
See Monitoring Database Operations in Oracle Database SQL Tuning Guide
Starts up a database
This corresponds to sqlplus command startup nomount.
You need to connect the databas as system privilege in prelim_auth
mode in advance.
After this method is executed, you need to reconnect the server
as system privilege without prelim_auth and executes
alter database mount and then alter database open.
Examples
Connect to an idle instance as sysdba and start up a database
// connect as sysdba with prelim_auth mode let conn = Connector::new("sys", "change_on_install", "") .privilege(Privilege::Sysdba) .prelim_auth(true) .connect()?; // start the instance conn.startup_database(&[])?; conn.close()?; // connect again without prelim_auth let conn = Connector::new("sys", "change_on_install", "") .privilege(Privilege::Sysdba) .connect()?; // mount and open a database conn.execute("alter database mount", &[])?; conn.execute("alter database open", &[])?;
Start up a database in restricted mode
... conn.startup_database(&[StartupMode::Restrict])?; ...
If the database is running, shut it down with mode ABORT and then start up in restricted mode
... conn.startup_database(&[StartupMode::Force, StartupMode::Restrict])?; ...
Shuts down a database
When this method is called with ShutdownMode::Default,
ShutdownMode::Transactional, ShutdownMode::TransactionalLocal
or ShutdownMode::Immediate, execute “alter database close normal”
and “alter database dismount” and call this method again with
ShutdownMode::Final.
When this method is called with ShutdownMode::Abort,
the database is aborted immediately.
Examples
Same with shutdown immediate on sqlplus.
// connect as sysdba let conn = Connector::new("sys", "change_on_install", "") .privilege(Privilege::Sysdba) .connect()?; // begin 'shutdown immediate' conn.shutdown_database(ShutdownMode::Immediate)?; // close and dismount the database conn.execute("alter database close normal", &[])?; conn.execute("alter database dismount", &[])?; // finish shutdown conn.shutdown_database(ShutdownMode::Final)?;
Same with shutdown abort on sqlplus.
// connect as sysdba let conn = Connector::new("sys", "change_on_install", "") .privilege(Privilege::Sysdba).connect()?; // 'shutdown abort' conn.shutdown_database(ShutdownMode::Abort)?; // The database is aborted here.
Gets an OCI handle attribute corresponding to the specified type parameter
See the oci_attr module for details.
pub fn set_oci_attr<T>(
    &mut self, 
    value: &<T::DataType as DataType>::Type
) -> Result<()> where
    T: OciAttr,
    T::HandleType: ConnHandle,
    T::Mode: WriteMode, 
pub fn set_oci_attr<T>(
    &mut self, 
    value: &<T::DataType as DataType>::Type
) -> Result<()> where
    T: OciAttr,
    T::HandleType: ConnHandle,
    T::Mode: WriteMode, 
Sets an OCI handle attribute corresponding to the specified type parameter
See the oci_attr module for details.