Skip to main content

Connection

Struct Connection 

Source
pub struct Connection { /* private fields */ }
Expand description

已连接会话的封装。

Implementations§

Source§

impl Connection

Source

pub fn connect(cfg: ClientConfig) -> Result<Self>

Source

pub fn cursor(&mut self) -> Cursor<'_>

Examples found in repository?
examples/cursor_pool.rs (line 19)
5fn main() -> Result<(), Box<dyn std::error::Error>> {
6    let cfg = ClientConfig {
7        host: std::env::var("DM_HOST").unwrap_or_else(|_| "127.0.0.1".into()),
8        port: std::env::var("DM_PORT").unwrap_or_else(|_| "5236".into()).parse()?,
9        user: std::env::var("DM_USER").unwrap_or_else(|_| "SYSDBA".into()),
10        password: std::env::var("DM_PWD").unwrap_or_else(|_| "password".into()),
11        ..Default::default()
12    };
13
14    // 连接池(最小 1 / 最大 5)
15    let pool = ConnectionPool::new(cfg, 1, 5)?;
16    {
17        let mut conn = pool.acquire()?;
18        {
19            let mut cur = conn.cursor();
20            cur.execute("select 1", &[])?;
21            println!("description: {:?}", cur.description());
22            while let Some(row) = cur.fetchone()? {
23                println!("row: {:?}", row);
24            }
25        }
26        // 惰性游标:大结果集自动分批拉取
27        {
28            let mut cur: Cursor = conn.cursor();
29            cur.execute("SELECT id FROM proto_test WHERE id >= ?", &[Param::Int(0)])?;
30            let rows = cur.fetchall()?;
31            println!("fetchall {} 行", rows.len());
32        }
33    } // Drop 自动归还连接
34
35    // executemany 批量插入
36    {
37        let mut conn = pool.acquire()?;
38        let batch: Vec<Vec<Param>> = (0..10)
39            .map(|i| vec![Param::Int(i), Param::Str(format!("name-{i}"))])
40            .collect();
41        let mut cur = conn.cursor();
42        let n = cur.executemany("INSERT INTO batch_demo VALUES (?, ?)", &batch)?;
43        println!("executemany 影响行数: {n}");
44    }
45
46    pool.close();
47    println!("✅ 游标/连接池示例结束");
48    Ok(())
49}
Source

pub fn execute(&mut self, sql: &str, params: &[Param]) -> Result<ExecuteResult>

Source

pub fn begin(&mut self) -> Result<()>

Source

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

Source

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

Source

pub fn close(&mut self)

Auto Trait Implementations§

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V