Skip to content

Commit

Permalink
backwards compatibility for package-query
Browse files Browse the repository at this point in the history
  • Loading branch information
harshdoesdev committed Nov 7, 2023
1 parent f4c7939 commit 16876a1
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
5 changes: 4 additions & 1 deletion fastn-core/src/library2022/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,10 @@ impl Library2022 {
kind,
doc,
&self.config,
&fastn_core::library2022::processor::sql::get_db_config()?,
&fastn_core::library2022::processor::sql::DatabaseConfig {
db_type: "sqlite".to_string(),
db_url: "".to_string(),
},
)
.await
}
Expand Down
49 changes: 46 additions & 3 deletions fastn-core/src/library2022/processor/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,44 @@ pub async fn process(
) -> ftd::interpreter::Result<ftd::interpreter::Value> {
let (headers, query) = get_p1_data("package-data", &value, doc.name)?;

let use_db = match headers.get_optional_string_by_key("use", doc.name, value.line_number()) {
Ok(Some(k)) => Some(k),
_ => match headers
.get_optional_string_by_key("db", doc.name, value.line_number())
.ok()
{
Some(k) => {
println!("Warning: `db` header is deprecated, use `use` instead.");
k
}
None => None,
},
};

let sqlite_database_path = match use_db {
Some(k) => {
let mut db_path = camino::Utf8PathBuf::new().join(k.as_str());
if !db_path.exists() {
if !config.root.join(db_path.as_path()).exists() {
return ftd::interpreter::utils::e2(
"`use` does not exists for sql processor".to_string(),
doc.name,
value.line_number(),
);
}
db_path = config.root.join(db_path.as_path());
}
db_path
}
None => {
assert!(
!db_config.db_url.is_empty(),
"Neither `use` or `db` is present."
);
config.root.join(&db_config.db_url)
}
};

// need the query params
// question is they can be multiple
// so lets say start with passing attributes from ftd file
Expand All @@ -41,9 +79,14 @@ pub async fn process(
// for now they wil be ordered
// select * from users where

let db_path = config.root.join(&db_config.db_url);
let query_response =
execute_query(&db_path, query.as_str(), doc, headers, value.line_number()).await;
let query_response = execute_query(
&sqlite_database_path,
query.as_str(),
doc,
headers,
value.line_number(),
)
.await;

match query_response {
Ok(result) => result_to_value(Ok(result), kind, doc, &value, super::sql::STATUS_OK),
Expand Down

0 comments on commit 16876a1

Please sign in to comment.