Skip to content

Commit

Permalink
feat(torii): sql playground (#2714)
Browse files Browse the repository at this point in the history
* feat(torii): sql playground

* fix: sql playground

* fmt

* fix: dynamic types

* auto completion & schema

* fix playground

* chore

* c

* fix

* chore: show fetch time in query result

* fmt

* better error hamdling

* add history and favoriting queries

* dedup
  • Loading branch information
Larkooo authored Nov 22, 2024
1 parent c5a4eaf commit da52a06
Show file tree
Hide file tree
Showing 2 changed files with 969 additions and 8 deletions.
52 changes: 44 additions & 8 deletions crates/torii/server/src/handlers/sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use base64::engine::general_purpose::STANDARD;
use base64::Engine;
use http::header::CONTENT_TYPE;
use hyper::{Body, Method, Request, Response, StatusCode};
use include_str;
use sqlx::{Column, Row, SqlitePool, TypeInfo};

use super::Handler;
Expand All @@ -29,7 +30,7 @@ impl SqlHandler {
"TEXT" => row
.get::<Option<String>, _>(i)
.map_or(serde_json::Value::Null, serde_json::Value::String),
"INTEGER" | "NULL" => row
"INTEGER" => row
.get::<Option<i64>, _>(i)
.map_or(serde_json::Value::Null, |n| {
serde_json::Value::Number(n.into())
Expand All @@ -48,9 +49,25 @@ impl SqlHandler {
.map_or(serde_json::Value::Null, |bytes| {
serde_json::Value::String(STANDARD.encode(bytes))
}),
_ => row
.get::<Option<String>, _>(i)
.map_or(serde_json::Value::Null, serde_json::Value::String),
_ => {
// Try different types in order
if let Ok(val) = row.try_get::<i64, _>(i) {
serde_json::Value::Number(val.into())
} else if let Ok(val) = row.try_get::<f64, _>(i) {
// Handle floating point numbers
serde_json::json!(val)
} else if let Ok(val) = row.try_get::<bool, _>(i) {
serde_json::Value::Bool(val)
} else if let Ok(val) = row.try_get::<String, _>(i) {
serde_json::Value::String(val)
} else {
// Handle or fallback to BLOB as base64
let val = row.get::<Option<Vec<u8>>, _>(i);
val.map_or(serde_json::Value::Null, |bytes| {
serde_json::Value::String(STANDARD.encode(bytes))
})
}
}
};
obj.insert(column.name().to_string(), value);
}
Expand Down Expand Up @@ -117,6 +134,28 @@ impl SqlHandler {
.unwrap()),
}
}

async fn serve_playground(&self) -> Response<Body> {
let html = include_str!("../../static/sql-playground.html");

Response::builder()
.status(StatusCode::OK)
.header(CONTENT_TYPE, "text/html")
.header("Access-Control-Allow-Origin", "*")
.body(Body::from(html))
.unwrap()
}

async fn handle_request(&self, req: Request<Body>) -> Response<Body> {
if req.method() == Method::GET && req.uri().query().unwrap_or_default().is_empty() {
self.serve_playground().await
} else {
match self.extract_query(req).await {
Ok(query) => self.execute_query(query).await,
Err(_) => self.serve_playground().await,
}
}
}
}

#[async_trait::async_trait]
Expand All @@ -126,9 +165,6 @@ impl Handler for SqlHandler {
}

async fn handle(&self, req: Request<Body>) -> Response<Body> {
match self.extract_query(req).await {
Ok(query) => self.execute_query(query).await,
Err(response) => response,
}
self.handle_request(req).await
}
}
Loading

0 comments on commit da52a06

Please sign in to comment.