Skip to content

Commit

Permalink
make FromSql trait public (for #35)
Browse files Browse the repository at this point in the history
  • Loading branch information
suharev7 committed May 2, 2019
1 parent b8156da commit 6459945
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ pub use self::{
block::{Block, Row, Rows},
column::Column,
options::Options,
from_sql::FromSql,
query::Query,
query_result::QueryResult,
};
pub(crate) use self::{
cmd::Cmd,
date_converter::DateConverter,
from_sql::FromSql,
marshal::Marshal,
options::{IntoOptions, OptionsSource},
stat_buffer::StatBuffer,
Expand Down
34 changes: 31 additions & 3 deletions tests/clickhouse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ extern crate chrono_tz;
extern crate clickhouse_rs;
extern crate tokio;

use std::env;
use std::{env, f64::EPSILON, fmt::Debug};

use chrono::prelude::*;
use chrono_tz::Tz::{self, UTC};
use tokio::prelude::*;

use clickhouse_rs::{errors::Error, types::Block, ClientHandle, Pool};
use std::f64::EPSILON;
use clickhouse_rs::{errors::Error, types::Block, types::FromSql, ClientHandle, Pool};

type BoxFuture<T> = Box<Future<Item = T, Error = Error> + Send>;

Expand Down Expand Up @@ -484,3 +483,32 @@ fn test_nullable() {

run(done).unwrap();
}

#[test]
fn test_generic_column() {
fn extract_to_vec<'a, T>(name: &str, block: &'a Block) -> Vec<T>
where
T: FromSql<'a> + Debug + 'static,
{
let n = block.row_count();
let mut result = Vec::with_capacity(n);
for row_index in 0..n {
let value: T = block.get(row_index, name).unwrap();
result.push(value)
}
result
}

let block = Block::new()
.add_column("int", vec![1u32, 2, 3])
.add_column("str", vec!["A", "B", "C"]);

let int_vec: Vec<u32> = extract_to_vec("int", &block);
let str_vec: Vec<String> = extract_to_vec("str", &block);

assert_eq!(int_vec, vec![1u32, 2, 3]);
assert_eq!(
str_vec,
vec!["A".to_string(), "B".to_string(), "C".to_string()]
);
}

0 comments on commit 6459945

Please sign in to comment.