Skip to content

Commit

Permalink
docs(query): add just a few docs
Browse files Browse the repository at this point in the history
Closes #61
  • Loading branch information
droundy authored May 14, 2023
1 parent bc2808e commit 1c6865c
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,41 @@ impl Query {
}
}

/// Bind `value` to the first `?` in the query.
///
/// The `value`, which must either implement [`Serialize`](serde::Serialize)
/// or be an [`Identifier`], will be appropriately escaped.
///
/// WARNING: This means that the query must not have any extra `?`, even if
/// they are in a string literal!
pub fn bind(mut self, value: impl Bind) -> Self {
self.sql.bind_arg(value);
self
}

/// Execute the query.
pub async fn execute(self) -> Result<()> {
self.do_execute(false)?.finish().await
}

// TODO: docs, panics
/// Execute the query, returning a [`RowCursor`] to obtain results.
///
/// # Example
/// ```norun
/// #[derive(clickhouse::Row, serde::Deserialize)]
/// struct MyRow<'a> {
/// no: u32,
/// name: &'a str,
/// }
///
/// let mut cursor = clickhouse::Client::default()
/// .query("SELECT ?fields FROM some WHERE no BETWEEN 0 AND 1")
/// .fetch::<MyRow<'_>>()?;
///
/// while let Some(MyRow { name, no }) = cursor.next().await? {
/// println!("{name}: {no}");
/// }
/// ```
pub fn fetch<T: Row>(mut self) -> Result<RowCursor<T>> {
self.sql.bind_fields::<T>();
self.sql.append(" FORMAT RowBinary");
Expand All @@ -46,6 +71,9 @@ impl Query {
Ok(RowCursor(RowBinaryCursor::new(response)))
}

/// Executes the query and returns just a single row.
///
/// Note that `T` must be owned.
pub async fn fetch_one<T>(self) -> Result<T>
where
T: Row + for<'b> Deserialize<'b>,
Expand All @@ -58,6 +86,7 @@ impl Query {
}

/// Executes the query and returns all the generated results, collected into a Vec.
///
/// Note that `T` must be owned.
pub async fn fetch_all<T>(self) -> Result<Vec<T>>
where
Expand Down

0 comments on commit 1c6865c

Please sign in to comment.