Skip to content

Commit

Permalink
applied recommended changes
Browse files Browse the repository at this point in the history
  • Loading branch information
harshdoesdev committed Nov 7, 2023
1 parent e04c182 commit 9087f0d
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 38 deletions.
2 changes: 1 addition & 1 deletion fastn-core/src/library2022/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ impl Library2022 {
"package-query" => {
processor::package_query::process(value, kind, doc, &self.config).await
}
"pg" => processor::pg::process(value, kind, doc, true).await,
"pg" => processor::pg::process(value, kind, doc).await,
"package-tree" => {
processor::package_tree::process(value, kind, doc, &self.config).await
}
Expand Down
42 changes: 15 additions & 27 deletions fastn-core/src/library2022/processor/package_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,27 @@ pub async fn process(
"`package-query` has been deprecated, use `sql` processor instead.",
);

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) => {
fastn_core::library2022::utils::log_deprecation_warning(
"`db` header is deprecated, use `use` instead.",
);
k
let sqlite_database =
match headers.get_optional_string_by_key("db", doc.name, value.line_number())? {
Some(k) => k,
None => {
return ftd::interpreter::utils::e2(
"`db` is not specified".to_string(),
doc.name,
value.line_number(),
)
}
None => None,
},
};
};

let sqlite_database_path = if let Some(db_path) = use_db {
let db_path = config.root.join(db_path.as_str());
if !db_path.exists() {
return ftd::interpreter::utils::e2(
"database file does not exist for package-query processor".to_string(),
doc.name,
value.line_number(),
);
}
db_path
} else {
let sqlite_database_path = config.root.join(sqlite_database.as_str());

if !sqlite_database_path.exists() {
return ftd::interpreter::utils::e2(
"Neither `use` nor `db` is specified for package-query processor".to_string(),
"`db` does not exists for package-query processor".to_string(),
doc.name,
value.line_number(),
);
};
}

let query_response = fastn_core::library2022::processor::sqlite::execute_query(
&sqlite_database_path,
Expand Down
7 changes: 0 additions & 7 deletions fastn-core/src/library2022/processor/pg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,9 @@ pub async fn process(
value: ftd::ast::VariableValue,
kind: ftd::interpreter::Kind,
doc: &ftd::interpreter::TDoc<'_>,
deprecated_usage: bool,
) -> ftd::interpreter::Result<ftd::interpreter::Value> {
let (headers, query) = super::sqlite::get_p1_data("pg", &value, doc.name)?;

if deprecated_usage {
fastn_core::library2022::utils::log_deprecation_warning(
"`pg` has been deprecated, use `sql` processor instead.",
);
}

let query_response = execute_query(query.as_str(), doc, value.line_number(), headers).await;

match query_response {
Expand Down
4 changes: 1 addition & 3 deletions fastn-core/src/library2022/processor/sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ pub async fn process(
let db_type = db_config.db_type.as_str();

match db_type {
"postgres" => {
Ok(fastn_core::library2022::processor::pg::process(value, kind, doc, false).await?)
}
"postgres" => Ok(fastn_core::library2022::processor::pg::process(value, kind, doc).await?),
"sqlite" => Ok(fastn_core::library2022::processor::sqlite::process(
value, kind, doc, config, &db_config,
)
Expand Down

0 comments on commit 9087f0d

Please sign in to comment.