-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
|
||
use clickhouse::Client; | ||
|
||
// use crate::{ | ||
// conversions::{table_row::TableRow, Cell}, | ||
// table::{ColumnSchema, TableId, TableName, TableSchema}, | ||
// }; | ||
|
||
|
||
pub struct ClickhouseClient { | ||
client: Client, | ||
} | ||
|
||
pub struct ClickhouseConfig { | ||
url: String, | ||
user: String, | ||
pasword: String, | ||
database: String | ||
} | ||
|
||
impl ClickhouseClient { | ||
pub fn create_client(config: &ClickhouseConfig) -> Result<ClickhouseClient, clickhouse::error::Error> { | ||
let client = Client::default() | ||
// should include both protocol and port | ||
.with_url(config.url) | ||
.with_user(config.user) | ||
.with_password(config.password) | ||
.with_database(config.database); | ||
Ok(ClickhouseClient { | ||
client | ||
}) | ||
} | ||
|
||
// fn async_insert(client: &Client) -> | ||
|
||
|
||
|
||
pub fn create_table_if_missing( | ||
&self, | ||
table_name: &TableName, | ||
column_schemas: &[ColumnSchema], | ||
) -> Result<bool, clickhouse::error::Error> { | ||
if self.table_exists(table_name)? { | ||
Ok(false) | ||
} else { | ||
self.create_table(table_name, column_schemas)?; | ||
Ok(true) | ||
} | ||
} | ||
|
||
|
||
|
||
pub fn create_table( | ||
&self, | ||
table_name: &TableName, | ||
column_schemas: &[ColumnSchema], | ||
) -> Result<(), clickhouse::error::Error> { | ||
let columns_spec = Self::create_columns_spec(column_schemas); | ||
let query = format!( | ||
"create table {}.{} {}", | ||
table_name.schema, table_name.name, columns_spec | ||
); | ||
self.conn.execute(&query, [])?; | ||
Ok(()) | ||
} | ||
|
||
pub fn table_exists(&self, table_name: &TableName) -> Result<bool, clickhouse::error::Error> { | ||
let query = | ||
"select * from information_schema.tables where table_catalog = ? and table_schema = ? and table_name = ?;"; | ||
let mut stmt = self.conn.prepare(query)?; | ||
let exists = stmt.exists([&self.current_database, &table_name.schema, &table_name.name])?; | ||
Ok(exists) | ||
} | ||
|
||
pub fn insert_rows( | ||
&self, | ||
table_name: &TableName, | ||
table_rows: &Vec<TableRow>, | ||
) -> Result<(), clickhouse::error::Error> { | ||
let table_name = format!("{}.{}", table_name.schema, table_name.name); | ||
let column_count = table_row.values.len(); | ||
let query = Self::create_insert_row_query(&table_name, column_count); | ||
let mut stmt = self.conn.prepare(&query)?; | ||
stmt.execute(params_from_iter(table_row.values.iter()))?; | ||
|
||
Ok(()) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters