pub struct Cursor<'a> {
pub columns: Vec<Column>,
pub rowcount: i64,
pub arraysize: usize,
/* private fields */
}Expand description
DB-API 风格游标。惰性查询(参数化 SELECT)只拉第一帧,fetch 时按需拉取剩余批次。
Fields§
§columns: Vec<Column>列描述(查询后填充)
rowcount: i64影响行数(DML)或已拉取行数(查询)
arraysize: usizefetchmany 默认大小
Implementations§
Source§impl<'a> Cursor<'a>
impl<'a> Cursor<'a>
pub fn new(client: &'a mut DmClient) -> Self
Sourcepub fn description(
&self,
) -> Vec<(String, i32, Option<i32>, Option<i32>, i32, i32, Option<bool>)>
pub fn description( &self, ) -> Vec<(String, i32, Option<i32>, Option<i32>, i32, i32, Option<bool>)>
DB-API description:(name, type_code, display_size, internal_size, precision, scale, null_ok)
Examples found in repository?
examples/cursor_pool.rs (line 21)
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}Sourcepub fn execute(&mut self, sql: &str, params: &[Param]) -> Result<&mut Self>
pub fn execute(&mut self, sql: &str, params: &[Param]) -> Result<&mut Self>
执行 SQL。大结果集(参数化查询)使用惰性分批 fetch。
Examples found in repository?
examples/cursor_pool.rs (line 20)
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}Sourcepub fn executemany(
&mut self,
sql: &str,
seq_of_params: &[Vec<Param>],
) -> Result<u64>
pub fn executemany( &mut self, sql: &str, seq_of_params: &[Vec<Param>], ) -> Result<u64>
批量执行(协议级 batch)。返回总影响行数。
Examples found in repository?
examples/cursor_pool.rs (line 42)
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}Sourcepub fn fetchone(&mut self) -> Result<Option<Vec<Value>>>
pub fn fetchone(&mut self) -> Result<Option<Vec<Value>>>
Examples found in repository?
examples/cursor_pool.rs (line 22)
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}pub fn fetchmany(&mut self, size: Option<usize>) -> Result<Vec<Vec<Value>>>
Sourcepub fn fetchall(&mut self) -> Result<Vec<Vec<Value>>>
pub fn fetchall(&mut self) -> Result<Vec<Vec<Value>>>
Examples found in repository?
examples/cursor_pool.rs (line 30)
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}Trait Implementations§
Auto Trait Implementations§
impl<'a> !UnwindSafe for Cursor<'a>
impl<'a> Freeze for Cursor<'a>
impl<'a> RefUnwindSafe for Cursor<'a>
impl<'a> Send for Cursor<'a>
impl<'a> Sync for Cursor<'a>
impl<'a> Unpin for Cursor<'a>
impl<'a> UnsafeUnpin for Cursor<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more