Skip to content

Commit 953996c

Browse files
authored
Document how to get column count (#569)
Which can be a bit tricky. Fixes #376
2 parents b498a90 + d2e42d3 commit 953996c

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

crates/duckdb/src/column.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,26 @@ impl Statement<'_> {
5050
/// sure that current statement has already been stepped once before
5151
/// calling this method.
5252
///
53+
/// # Example
54+
///
55+
/// ```rust,no_run
56+
/// # use duckdb::{Connection, Result};
57+
/// fn get_column_count(conn: &Connection) -> Result<usize> {
58+
/// let mut stmt = conn.prepare("SELECT id, name FROM people")?;
59+
///
60+
/// // Option 1: Execute first, then get column count
61+
/// stmt.execute([])?;
62+
/// let count = stmt.column_count();
63+
///
64+
/// // Option 2: Get column count from rows (avoids borrowing issues)
65+
/// let mut stmt2 = conn.prepare("SELECT id, name FROM people")?;
66+
/// let rows = stmt2.query([])?;
67+
/// let count2 = rows.as_ref().unwrap().column_count();
68+
///
69+
/// Ok(count)
70+
/// }
71+
/// ```
72+
///
5373
/// # Caveats
5474
/// Panics if the query has not been [`execute`](Statement::execute)d yet.
5575
#[inline]

crates/duckdb/src/row.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,29 @@ impl<'stmt> Rows<'stmt> {
101101
AndThenRows { rows: self, map: f }
102102
}
103103

104-
/// Give access to the underlying statement
104+
/// Access the underlying statement
105+
///
106+
/// This method provides a way to access the `Statement` that created these `Rows`
107+
/// without additional borrowing conflicts. This is particularly useful when you need
108+
/// to access statement metadata (like column count or names) while iterating over results.
109+
///
110+
/// # Example
111+
///
112+
/// ```rust,no_run
113+
/// # use duckdb::{Connection, Result};
114+
/// fn process_results(conn: &Connection) -> Result<()> {
115+
/// let mut stmt = conn.prepare("SELECT id, name FROM people")?;
116+
/// let mut rows = stmt.query([])?;
117+
///
118+
/// let column_count = rows.as_ref().unwrap().column_count();
119+
/// println!("Processing {} columns", column_count);
120+
///
121+
/// while let Some(row) = rows.next()? {
122+
/// // Process row...
123+
/// }
124+
/// Ok(())
125+
/// }
126+
/// ```
105127
pub fn as_ref(&self) -> Option<&Statement<'stmt>> {
106128
self.stmt
107129
}

0 commit comments

Comments
 (0)