-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support postgres wire protocol (#62)
* feat: added basic support for postgres * fix: use proxy to chose pg or mysql --------- Co-authored-by: tanruixiang <tanruixiang0104@gmail.com> Co-authored-by: jiacai2050 <dev@liujiacai.net>
- Loading branch information
1 parent
fef48d1
commit 8e1887e
Showing
13 changed files
with
221 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
[workspace] | ||
members = ["sqlness", "sqlness-cli"] | ||
resolver = "2" | ||
|
||
[workspace.package] | ||
version = "0.5.0" | ||
|
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
File renamed without changes.
File renamed without changes.
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,38 @@ | ||
DROP TABLE if exists categories; | ||
|
||
(Empty response) | ||
|
||
CREATE TABLE categories ( | ||
category_id SERIAL NOT NULL PRIMARY KEY, | ||
category_name VARCHAR(255), | ||
description VARCHAR(255) | ||
); | ||
|
||
(Empty response) | ||
|
||
INSERT INTO categories (category_name, description) | ||
VALUES | ||
('Beverages', 'Soft drinks, coffees, teas, beers, and ales'), | ||
('Condiments', 'Sweet and savory sauces, relishes, spreads, and seasonings'), | ||
('Confections', 'Desserts, candies, and sweet breads'), | ||
('Dairy Products', 'Cheeses'), | ||
('Grains/Cereals', 'Breads, crackers, pasta, and cereal'), | ||
('Meat/Poultry', 'Prepared meats'), | ||
('Produce', 'Dried fruit and bean curd'), | ||
('Seafood', 'Seaweed and fish'); | ||
|
||
(Empty response) | ||
|
||
select * from categories; | ||
|
||
category_id,category_name,description, | ||
Row { columns: [Column { name: "category_id", type: Int4 }, Column { name: "category_name", type: Varchar }, Column { name: "description", type: Varchar }] } | ||
Row { columns: [Column { name: "category_id", type: Int4 }, Column { name: "category_name", type: Varchar }, Column { name: "description", type: Varchar }] } | ||
Row { columns: [Column { name: "category_id", type: Int4 }, Column { name: "category_name", type: Varchar }, Column { name: "description", type: Varchar }] } | ||
Row { columns: [Column { name: "category_id", type: Int4 }, Column { name: "category_name", type: Varchar }, Column { name: "description", type: Varchar }] } | ||
Row { columns: [Column { name: "category_id", type: Int4 }, Column { name: "category_name", type: Varchar }, Column { name: "description", type: Varchar }] } | ||
Row { columns: [Column { name: "category_id", type: Int4 }, Column { name: "category_name", type: Varchar }, Column { name: "description", type: Varchar }] } | ||
Row { columns: [Column { name: "category_id", type: Int4 }, Column { name: "category_name", type: Varchar }, Column { name: "description", type: Varchar }] } | ||
Row { columns: [Column { name: "category_id", type: Int4 }, Column { name: "category_name", type: Varchar }, Column { name: "description", type: Varchar }] } | ||
|
||
|
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,22 @@ | ||
|
||
DROP TABLE if exists categories; | ||
|
||
CREATE TABLE categories ( | ||
category_id SERIAL NOT NULL PRIMARY KEY, | ||
category_name VARCHAR(255), | ||
description VARCHAR(255) | ||
); | ||
|
||
INSERT INTO categories (category_name, description) | ||
VALUES | ||
('Beverages', 'Soft drinks, coffees, teas, beers, and ales'), | ||
('Condiments', 'Sweet and savory sauces, relishes, spreads, and seasonings'), | ||
('Confections', 'Desserts, candies, and sweet breads'), | ||
('Dairy Products', 'Cheeses'), | ||
('Grains/Cereals', 'Breads, crackers, pasta, and cereal'), | ||
('Meat/Poultry', 'Prepared meats'), | ||
('Produce', 'Dried fruit and bean curd'), | ||
('Seafood', 'Seaweed and fish'); | ||
|
||
|
||
select * from categories; |
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 |
---|---|---|
|
@@ -2,3 +2,5 @@ | |
|
||
#[cfg(feature = "mysql")] | ||
pub mod mysql; | ||
#[cfg(feature = "postgres")] | ||
pub mod postgresql; |
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,92 @@ | ||
// Copyright 2022 CeresDB Project Authors. Licensed under Apache-2.0. | ||
|
||
use async_trait::async_trait; | ||
use postgres::{Client, Config, NoTls, Row}; | ||
use std::{ | ||
fmt::Display, | ||
sync::{Arc, Mutex}, | ||
}; | ||
|
||
use crate::{Database, DatabaseConfig, QueryContext}; | ||
|
||
pub struct PostgresqlDatabase { | ||
client: Arc<Mutex<Client>>, | ||
} | ||
|
||
impl PostgresqlDatabase { | ||
pub fn try_new(config: &DatabaseConfig) -> Result<Self, postgres::Error> { | ||
let mut postgres_config = Config::new(); | ||
postgres_config | ||
.port(config.tcp_port) | ||
.host(&config.ip_or_host); | ||
|
||
if let Some(user) = &config.user { | ||
postgres_config.user(user); | ||
} | ||
if let Some(password) = &config.pass { | ||
postgres_config.password(password); | ||
} | ||
if let Some(dbname) = &config.db_name { | ||
postgres_config.dbname(dbname); | ||
} | ||
let client = postgres_config.connect(NoTls)?; | ||
Ok(PostgresqlDatabase { | ||
client: Arc::new(Mutex::new(client)), | ||
}) | ||
} | ||
|
||
pub fn execute(query: &str, client: Arc<Mutex<Client>>) -> Box<dyn Display> { | ||
let mut client = match client.lock() { | ||
Ok(client) => client, | ||
Err(err) => { | ||
return Box::new(format!("Failed to get connection, encountered: {:?}", err)) | ||
} | ||
}; | ||
|
||
let result = match client.query(query, &[]) { | ||
Ok(rows) => { | ||
format!("{}", PostgresqlFormatter { rows }) | ||
} | ||
Err(err) => format!("Failed to execute query, encountered: {:?}", err), | ||
}; | ||
|
||
Box::new(result) | ||
} | ||
} | ||
|
||
struct PostgresqlFormatter { | ||
pub rows: Vec<Row>, | ||
} | ||
|
||
impl Display for PostgresqlFormatter { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
if self.rows.is_empty() { | ||
return f.write_fmt(format_args!("(Empty response)")); | ||
} | ||
|
||
let top = &self.rows[0]; | ||
let columns = top | ||
.columns() | ||
.iter() | ||
.map(|column| column.name()) | ||
.collect::<Vec<_>>(); | ||
for col in &columns { | ||
f.write_fmt(format_args!("{},", col))?; | ||
} | ||
|
||
f.write_str("\n")?; | ||
|
||
for row in &self.rows { | ||
f.write_fmt(format_args!("{:?}\n", row))?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl Database for PostgresqlDatabase { | ||
async fn query(&self, _: QueryContext, query: String) -> Box<dyn Display> { | ||
Self::execute(&query, Arc::clone(&self.client)) | ||
} | ||
} |
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