-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
⚗️ Try to use proxy connection and gluesql.
- Loading branch information
Showing
10 changed files
with
258 additions
and
70 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod post; |
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,17 @@ | ||
use sea_orm::entity::prelude::*; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, Deserialize, Serialize)] | ||
#[sea_orm(table_name = "posts")] | ||
pub struct Model { | ||
#[sea_orm(primary_key)] | ||
pub id: i64, | ||
|
||
pub title: String, | ||
pub text: String, | ||
} | ||
|
||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] | ||
pub enum Relation {} | ||
|
||
impl ActiveModelBehavior for ActiveModel {} |
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,23 +1,127 @@ | ||
use anyhow::Result; | ||
use std::io; | ||
mod entity; | ||
|
||
use tairitsu_utils::types::proto::backend::Msg; | ||
use std::{ | ||
collections::BTreeMap, | ||
sync::{Arc, Mutex}, | ||
}; | ||
|
||
#[tokio::main(flavor = "current_thread")] | ||
async fn main() -> Result<()> { | ||
loop { | ||
let mut buffer = String::new(); | ||
let stdin = io::stdin(); | ||
stdin.read_line(&mut buffer)?; | ||
use sea_orm::{ | ||
ActiveValue::Set, Database, DbBackend, DbErr, EntityTrait, ProxyDatabaseTrait, ProxyExecResult, | ||
ProxyRow, Statement, | ||
}; | ||
|
||
use entity::post::{ActiveModel, Entity}; | ||
use tairitsu_utils::types::proto::backend::{RequestMsg, ResponseMsg}; | ||
|
||
#[derive(Debug)] | ||
struct ProxyDb {} | ||
|
||
let msg: Msg = ron::from_str(&buffer)?; | ||
impl ProxyDatabaseTrait for ProxyDb { | ||
fn query(&self, statement: Statement) -> Result<Vec<ProxyRow>, DbErr> { | ||
let sql = statement.sql.clone(); | ||
println!( | ||
"{}", | ||
serde_json::to_string(&RequestMsg::Query(sql)).unwrap() | ||
); | ||
|
||
let ret = Msg { | ||
id: msg.id + 1, | ||
data: msg.data + " hahaha", | ||
let mut input = String::new(); | ||
std::io::stdin().read_line(&mut input).unwrap(); | ||
let ret: ResponseMsg = serde_json::from_str(&input).unwrap(); | ||
let ret = match ret { | ||
ResponseMsg::Query(v) => v, | ||
_ => unreachable!("Not a query result"), | ||
}; | ||
let ret = ron::to_string(&ret)?; | ||
|
||
println!("{}", ret); | ||
let mut rows: Vec<ProxyRow> = vec![]; | ||
for row in ret { | ||
let mut map: BTreeMap<String, sea_orm::Value> = BTreeMap::new(); | ||
for (k, v) in row.iter() { | ||
map.insert(k.to_owned(), { | ||
if v.is_string() { | ||
sea_orm::Value::String(Some(Box::new(v.as_str().unwrap().to_string()))) | ||
} else if v.is_number() { | ||
sea_orm::Value::BigInt(Some(v.as_i64().unwrap())) | ||
} else if v.is_boolean() { | ||
sea_orm::Value::Bool(Some(v.as_bool().unwrap())) | ||
} else { | ||
unreachable!("Unknown json type") | ||
} | ||
}); | ||
} | ||
rows.push(ProxyRow { values: map }); | ||
} | ||
|
||
Ok(rows) | ||
} | ||
|
||
fn execute(&self, statement: Statement) -> Result<ProxyExecResult, DbErr> { | ||
let sql = { | ||
if let Some(values) = statement.values { | ||
// Replace all the '?' with the statement values | ||
let mut new_sql = statement.sql.clone(); | ||
let mark_count = new_sql.matches('?').count(); | ||
for (i, v) in values.0.iter().enumerate() { | ||
if i >= mark_count { | ||
break; | ||
} | ||
new_sql = new_sql.replacen('?', &v.to_string(), 1); | ||
} | ||
|
||
new_sql | ||
} else { | ||
statement.sql | ||
} | ||
}; | ||
|
||
// Send the query to stdout | ||
let msg = RequestMsg::Execute(sql); | ||
let msg = serde_json::to_string(&msg).unwrap(); | ||
println!("{}", msg); | ||
|
||
// Get the result from stdin | ||
let mut input = String::new(); | ||
std::io::stdin().read_line(&mut input).unwrap(); | ||
let ret: ResponseMsg = serde_json::from_str(&input).unwrap(); | ||
let ret = match ret { | ||
ResponseMsg::Execute(v) => v, | ||
_ => unreachable!(), | ||
}; | ||
|
||
Ok(ret) | ||
} | ||
} | ||
|
||
#[tokio::main(flavor = "current_thread")] | ||
async fn main() { | ||
let db = Database::connect_proxy( | ||
DbBackend::Sqlite, | ||
Arc::new(Mutex::new(Box::new(ProxyDb {}))), | ||
) | ||
.await | ||
.unwrap(); | ||
|
||
let data = ActiveModel { | ||
id: Set(11), | ||
title: Set("Homo".to_owned()), | ||
text: Set("いいよ、来いよ".to_owned()), | ||
}; | ||
Entity::insert(data).exec(&db).await.unwrap(); | ||
let data = ActiveModel { | ||
id: Set(45), | ||
title: Set("Homo".to_owned()), | ||
text: Set("そうだよ".to_owned()), | ||
}; | ||
Entity::insert(data).exec(&db).await.unwrap(); | ||
let data = ActiveModel { | ||
id: Set(14), | ||
title: Set("Homo".to_owned()), | ||
text: Set("悔い改めて".to_owned()), | ||
}; | ||
Entity::insert(data).exec(&db).await.unwrap(); | ||
|
||
let list = Entity::find().all(&db).await.unwrap().to_vec(); | ||
println!( | ||
"{}", | ||
serde_json::to_string(&RequestMsg::Debug(format!("{:?}", list))).unwrap() | ||
); | ||
} |
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,7 +1,20 @@ | ||
use std::collections::BTreeMap; | ||
|
||
use sea_orm::ProxyExecResult; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Clone, Debug, Serialize, Deserialize)] | ||
pub struct Msg { | ||
pub id: u32, | ||
pub data: String, | ||
pub enum RequestMsg { | ||
Query(String), | ||
Execute(String), | ||
|
||
Debug(String), | ||
} | ||
|
||
#[derive(Clone, Debug, Serialize, Deserialize)] | ||
pub enum ResponseMsg { | ||
Query(Vec<BTreeMap<String, serde_json::Value>>), | ||
Execute(ProxyExecResult), | ||
|
||
None, | ||
} |
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
Oops, something went wrong.