Skip to content

Commit 1a8926c

Browse files
committed
Change result iter to fetch_row
1 parent e94482f commit 1a8926c

File tree

3 files changed

+24
-33
lines changed

3 files changed

+24
-33
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ fn main() {
1616
println!("Column {i}: {} {}", rst.column_name(i), rst.column_type(i));
1717
}
1818

19-
while let Some(row) = (&mut rst).next() {
19+
while let Some(row) = rst.fetch_row() {
2020
dbg!(row);
2121
}
2222
}

examples/basic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ fn main() {
88
println!("Column {i}: {} {}", rst.column_name(i), rst.column_type(i));
99
}
1010

11-
while let Some(row) = (&mut rst).next() {
11+
while let Some(row) = rst.fetch_row() {
1212
dbg!(row);
1313
}
1414
}

src/lib.rs

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -54,26 +54,17 @@ impl Connection {
5454
let msg = CStr::from_ptr(xdb_errmsg(ptr)).to_str()?.to_string();
5555
return Err(Error::Query(res.errcode, msg));
5656
}
57-
Ok(ExecResult {
58-
ptr,
59-
col_meta: res.col_meta,
60-
column_count: res.col_count as usize,
61-
row_count: res.row_count as usize,
62-
column_types: ColumnType::all(&res),
63-
row_index: 0,
64-
})
57+
let types = ColumnType::all(&res);
58+
Ok(ExecResult { res, ptr, types })
6559
}
6660
}
6761
}
6862

6963
#[derive(Debug)]
7064
pub struct ExecResult {
65+
res: xdb_res_t,
7166
ptr: *mut xdb_res_t,
72-
col_meta: u64,
73-
column_count: usize,
74-
row_count: usize,
75-
column_types: Vec<ColumnType>,
76-
row_index: usize,
67+
types: Vec<ColumnType>,
7768
}
7869

7970
impl Drop for ExecResult {
@@ -86,41 +77,41 @@ impl Drop for ExecResult {
8677

8778
impl ExecResult {
8879
pub fn column_count(&self) -> usize {
89-
self.column_count
80+
self.res.col_count as usize
9081
}
9182

9283
pub fn row_count(&self) -> usize {
93-
self.row_count
84+
self.res.row_count as usize
85+
}
86+
87+
pub fn affected_rows(&self) -> u64 {
88+
self.res.affected_rows
9489
}
9590

9691
pub fn column_name<'a>(&'a self, i: usize) -> &'a str {
9792
unsafe {
98-
let name = xdb_column_name(self.col_meta, i as u16);
93+
let name = xdb_column_name(self.res.col_meta, i as u16);
9994
CStr::from_ptr(name).to_str().unwrap()
10095
}
10196
}
10297

10398
pub fn column_type(&self, i: usize) -> ColumnType {
104-
self.column_types[i]
99+
self.types[i]
105100
}
106-
}
107101

108-
impl<'a> Iterator for &'a mut ExecResult {
109-
type Item = Vec<Value<'a>>;
110-
111-
fn next(&mut self) -> Option<Self::Item> {
112-
if self.row_count <= self.row_index {
113-
return None;
114-
}
115-
let mut values = Vec::with_capacity(self.column_count);
102+
pub fn fetch_row(&mut self) -> Option<Vec<Value<'_>>> {
116103
unsafe {
117-
let y = xdb_fetch_row(self.ptr);
118-
for x in 0..self.column_count {
119-
let value = Value::from_result(self.col_meta, y, x as u16, self.column_type(x));
104+
let row = xdb_fetch_row(self.ptr);
105+
if row.is_null() {
106+
return None;
107+
}
108+
let mut values = Vec::with_capacity(self.column_count());
109+
for col in 0..self.column_count() {
110+
let value =
111+
Value::from_result(self.res.col_meta, row, col as u16, self.column_type(col));
120112
values.push(value);
121113
}
114+
Some(values)
122115
}
123-
self.row_index += 1;
124-
Some(values)
125116
}
126117
}

0 commit comments

Comments
 (0)