Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Value from Row #194

Open
gusinacio opened this issue Sep 11, 2023 · 5 comments
Open

Value from Row #194

gusinacio opened this issue Sep 11, 2023 · 5 comments

Comments

@gusinacio
Copy link
Contributor

Is it possible to get an Iterator from a Row?

I'm currently converting back every value for my own Value so I can return them as json on my http server.

@suharev7
Copy link
Owner

If you needed a convent Row in JSON, you could write something like this:

use clickhouse_rs::{
    errors::Result,
    types::{Row, SqlType, ColumnType},
};
use serde_json::{Number, Value, Map};

fn row_to_json<C>(row: &Row<'_, C>) -> Result<Value>
where
    C: ColumnType,
{
    let mut result = Map::with_capacity(row.len());
    for i in 0..row.len() {
        let value = match row.sql_type(i)? {
            SqlType::UInt32 => serde_json::Value::Number(Number::from(row.get::<u32, _>(i)?)),
            SqlType::String | SqlType::FixedString(_) => serde_json::Value::String(row.get(i)?),
            _ => todo!(),
        };
        result.insert(row.name(i)?.into(), value);
    }
    Ok(Value::Object(result))
}

or if you needed iterator:

fn row_to_iter<'a, C>(row: &'a Row<'_, C>) -> impl Iterator<Item = Result<Value>> + 'a
where
    C: ColumnType,
{
    (0..row.len()).map(|i| {
        Ok(match row.sql_type(i)? {
            SqlType::UInt32 => serde_json::Value::Number(Number::from(row.get::<u32, _>(i)?)),
            SqlType::String | SqlType::FixedString(_) => serde_json::Value::String(row.get(i)?),
            _ => todo!(),
        })
    })
}

@gusinacio
Copy link
Contributor Author

Yeah, I'm currently doing part of this I was just wondering if the lib already had this provided. Do you think it's worth it a PR with a enum Cell which implements the FromSql trait and is a wrap for every CH type?

@suharev7
Copy link
Owner

What's the difference between enum Cell and clickhouse_rs::Value? Maybe it's better to export clickhouse_rs::Value from the crate and implement the FromSql trait for it?

@gusinacio
Copy link
Contributor Author

That's a good one too! The current only difference between my implementation of Cell and clickhouse_rs::Value is that it has some serialization macros and also implements complex struct like String, UUID, chrono::DateTime instead of primitives.

If I use clickhouse_rs::Value, it would only work for me if I can serialize it via serde.

Do you have plans to add a feature serde within the clickhouse-rs crate?

@suharev7
Copy link
Owner

I have no plans to add serde because I don't know how to make universal serialization suitable for most applications.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants