Skip to content

Commit da60348

Browse files
authored
Merge pull request #102 from hotdata-dev/feat/query-database-flag
feat(query): add --database flag and improve post-load UX
2 parents 827af79 + 3c9ed44 commit da60348

5 files changed

Lines changed: 61 additions & 5 deletions

File tree

src/api.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,12 @@ impl ApiClient {
122122
}
123123
}
124124

125+
/// Override the database ID for a single query without touching config.
126+
pub fn with_database(mut self, database_id: &str) -> Self {
127+
self.database_id = Some(database_id.to_string());
128+
self
129+
}
130+
125131
/// Test-only client (no config load). Used with a local mock HTTP server.
126132
#[cfg(test)]
127133
pub(crate) fn test_new(api_url: &str, api_key: &str, workspace_id: Option<&str>) -> Self {

src/command.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ pub enum Commands {
3838
#[arg(long)]
3939
connection: Option<String>,
4040

41+
/// Run query against a specific managed database (overrides the current database set via `databases set`)
42+
#[arg(long, short = 'd')]
43+
database: Option<String>,
44+
4145
/// Output format
4246
#[arg(long = "output", short = 'o', default_value = "table", value_parser = ["table", "json", "csv"])]
4347
output: String,

src/databases.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,22 @@ pub fn create(
463463
println!("description: {desc}");
464464
}
465465
println!("id: {}", result.id);
466+
println!();
467+
println!(
468+
"{}",
469+
format!(
470+
concat!(
471+
"Load a table:\n",
472+
" hotdata databases load --file <path.parquet> {}.<table_name>\n",
473+
"\nQuery with:\n",
474+
" hotdata query --database {} \"SELECT * FROM default.public.<table> LIMIT 10\"\n",
475+
"\n Tip: use 'default.<schema>.<table>' as the SQL prefix (not the database or connection id)\n",
476+
" Column names are case-sensitive — wrap uppercase names in double quotes",
477+
),
478+
result.id, result.id
479+
)
480+
.dark_grey()
481+
);
466482
}
467483
_ => unreachable!(),
468484
}
@@ -614,8 +630,22 @@ pub fn tables_load(
614630

615631
let full_name = format!("default.{}.{}", result.schema_name, result.table_name);
616632
println!("{}", "Table loaded".green());
617-
println!("full_name: {}", full_name.green());
633+
println!("full_name: {}", full_name.clone().green());
618634
println!("rows: {}", result.row_count);
635+
println!();
636+
println!(
637+
"{}",
638+
format!(
639+
concat!(
640+
"Query it now:\n",
641+
" hotdata query \"SELECT * FROM {} LIMIT 10\"\n",
642+
"\n Tip: column names are case-sensitive.\n",
643+
" Wrap uppercase names in double quotes: SELECT \"MyColumn\" FROM {} LIMIT 10",
644+
),
645+
full_name, full_name
646+
)
647+
.dark_grey()
648+
);
619649
}
620650

621651
pub fn tables_delete(workspace_id: &str, database: Option<&str>, table: &str, schema: Option<&str>) {

src/main.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ fn main() {
260260
sql,
261261
workspace_id,
262262
connection,
263+
database,
263264
output,
264265
command,
265266
} => {
@@ -268,7 +269,13 @@ fn main() {
268269
Some(QueryCommands::Status { id }) => query::poll(&id, &workspace_id, &output),
269270
None => match sql {
270271
Some(sql) => {
271-
query::execute(&sql, &workspace_id, connection.as_deref(), &output)
272+
query::execute(
273+
&sql,
274+
&workspace_id,
275+
connection.as_deref(),
276+
database.as_deref(),
277+
&output,
278+
)
272279
}
273280
None => {
274281
use clap::CommandFactory;
@@ -830,7 +837,7 @@ fn main() {
830837
),
831838
_ => unreachable!(),
832839
};
833-
query::execute(&sql, &workspace_id, None, &output)
840+
query::execute(&sql, &workspace_id, None, None, &output)
834841
}
835842
Commands::Queries {
836843
id,

src/query.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,17 @@ fn value_to_string(v: &Value) -> String {
4545
}
4646
}
4747

48-
pub fn execute(sql: &str, workspace_id: &str, connection: Option<&str>, format: &str) {
49-
let api = ApiClient::new(Some(workspace_id));
48+
pub fn execute(
49+
sql: &str,
50+
workspace_id: &str,
51+
connection: Option<&str>,
52+
database: Option<&str>,
53+
format: &str,
54+
) {
55+
let mut api = ApiClient::new(Some(workspace_id));
56+
if let Some(db_id) = database {
57+
api = api.with_database(db_id);
58+
}
5059

5160
let mut body = serde_json::json!({
5261
"sql": sql,

0 commit comments

Comments
 (0)