Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit49bed4c

Browse files
author
Konstantin Salikhov
committed
Transaction support
1 parentbc7d3a6 commit49bed4c

File tree

5 files changed

+83
-2
lines changed

5 files changed

+83
-2
lines changed

‎examples/transactions.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
externcrate odbc;
2+
// Use this crate and set environmet variable RUST_LOG=odbc to see ODBC warnings
3+
externcrate env_logger;
4+
externcrate odbc_safe;
5+
6+
use odbc::*;
7+
use odbc_safe::{AutocommitOff};
8+
9+
fnmain(){
10+
11+
env_logger::init();
12+
13+
matchconnect(){
14+
Ok(()) =>println!("Success"),
15+
Err(diag) =>println!("Error: {}", diag),
16+
}
17+
}
18+
19+
fnconnect() -> std::result::Result<(),DiagnosticRecord>{
20+
let env =create_environment_v3().map_err(|e| e.unwrap())?;
21+
let conn = env.connect("TestDataSource","","").unwrap();
22+
letmut conn = conn.disable_autocommit().unwrap();
23+
list_tables(&mut conn)
24+
}
25+
26+
fnlist_tables(conn:&mutConnection<AutocommitOff>) ->Result<()>{
27+
let stmt =Statement::with_parent(conn)?;
28+
match stmt.exec_direct("SELECT 'HELLO' FROM MOVIES")?{
29+
Data(mut stmt) =>{
30+
let cols = stmt.num_result_cols()?;
31+
whileletSome(mut cursor) = stmt.fetch()?{
32+
for iin1..(cols +1){
33+
match cursor.get_data::<&str>(iasu16)?{
34+
Some(val) =>print!(" {}", val),
35+
None =>print!(" NULL"),
36+
}
37+
}
38+
println!("");
39+
}
40+
}
41+
NoData(_) =>println!("Query executed, no data returned"),
42+
}
43+
44+
conn.commit()
45+
}

‎src/connection.rs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
//! Holds implementation of odbc connection
22
usesuper::{ffi, safe,Environment,Handle,Result,Version3};
33
usesuper::result::{into_result, into_result_with};
4-
use odbc_safe::{AutocommitMode,AutocommitOn};
4+
use odbc_safe::{AutocommitMode,AutocommitOn,AutocommitOff};
55

66
/// Represents a connection to an ODBC data source
7+
#[derive(Debug)]
78
pubstructConnection<'env,AC:AutocommitMode>{
89
safe: safe::Connection<'env,AC>,
910
}
@@ -43,6 +44,39 @@ impl Environment<Version3> {
4344
}
4445
}
4546

47+
impl<'env>Connection<'env,AutocommitOn>{
48+
pubfndisable_autocommit(mutself) -> std::result::Result<Connection<'env,AutocommitOff>,Self>{
49+
let ret =self.safe.disable_autocommit();
50+
match ret{
51+
safe::Return::Success(value) =>Ok(Connection{safe: value}),
52+
safe::Return::Info(value) =>Ok(Connection{safe: value}),
53+
safe::Return::Error(value) =>Err(Connection{safe: value})
54+
}
55+
}
56+
}
57+
58+
impl<'env>Connection<'env,AutocommitOff>{
59+
pubfnenable_autocommit(mutself) -> std::result::Result<Connection<'env,AutocommitOn>,Self>{
60+
let ret =self.safe.enable_autocommit();
61+
match ret{
62+
safe::Return::Success(value) =>Ok(Connection{safe: value}),
63+
safe::Return::Info(value) =>Ok(Connection{safe: value}),
64+
safe::Return::Error(value) =>Err(Connection{safe: value})
65+
}
66+
}
67+
68+
pubfncommit(&mutself) ->Result<()>{
69+
let ret =self.safe.commit();
70+
into_result_with(&self.safe, ret)
71+
}
72+
73+
pubfnrollback(&mutself) ->Result<()>{
74+
let ret =self.safe.rollback();
75+
into_result_with(&self.safe, ret)
76+
}
77+
}
78+
79+
4680
impl<'env,AC:AutocommitMode>Connection<'env,AC>{
4781
/// `true` if the data source is set to READ ONLY mode, `false` otherwise.
4882
///

‎src/environment/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pub static mut DB_ENCODING: &encoding_rs::Encoding = encoding_rs::UTF_8;
1414
///
1515
/// Creating an instance of this type is the first thing you do then using ODBC. The environment
1616
/// must outlive all connections created with it.
17+
#[derive(Debug)]
1718
pubstructEnvironment<V>{
1819
safe: safe::Environment<V>,
1920
}

‎src/raii.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::ptr::null_mut;
44
/// Wrapper around handle types which ensures the wrapped value is always valid.
55
///
66
/// Resource Acquisition Is Initialization
7+
#[derive(Debug)]
78
pubstructRaii<T:OdbcObject>{
89
//Invariant: Should always point to a valid odbc Object
910
handle:*mutT,

‎src/statement/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ pub struct Cursor<'a, 'b: 'a, 'c: 'a, S: 'a, AC: AutocommitMode> {
8484
buffer:Vec<u8>,
8585
}
8686

87-
#[derive(Debug,Clone)]
87+
#[derive(Clone,Debug,Eq,PartialEq)]
8888
pubstructColumnDescriptor{
8989
pubname:String,
9090
pubdata_type: ffi::SqlDataType,

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp