Skip to content

Commit

Permalink
Fix bug with variable assignment
Browse files Browse the repository at this point in the history
fixes #138
  • Loading branch information
lovasoa committed Nov 22, 2023
1 parent 23a49d1 commit ad0fc2c
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 6 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# CHANGELOG.md

## unreleased
## 0.16.1

- fix a bug where setting a variable to a non-string value would always set it to null
- clearer debug logs (https://github.com/wooorm/markdown-rs/pull/92)
- update compiler to rust 1.74
- use user id and group id 1000 in docker image (this is the default user id in most linux distributions)
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sqlpage"
version = "0.16.0"
version = "0.16.1"
edition = "2021"
description = "A SQL-only web application framework. Takes .sql files and formats the query result using pre-made configurable professional-looking components."
keywords = ["web", "sql", "framework"]
Expand Down
6 changes: 3 additions & 3 deletions src/webserver/database/execute_queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ use std::collections::HashMap;

use super::sql::{ParsedSqlFile, ParsedStatement, StmtWithParams};
use crate::webserver::database::sql_pseudofunctions::extract_req_param;
use crate::webserver::database::sql_to_json::row_to_string;
use crate::webserver::http::{RequestInfo, SingleOrVec};

use sqlx::any::{AnyArguments, AnyQueryResult, AnyRow, AnyStatement, AnyTypeInfo};
use sqlx::pool::PoolConnection;
use sqlx::{Any, AnyConnection, Arguments, Either, Executor, Row, Statement};
use sqlx::{Any, AnyConnection, Arguments, Either, Executor, Statement};

use super::sql_pseudofunctions::StmtParam;
use super::{highlight_sql_error, Database, DbItem};
Expand Down Expand Up @@ -54,8 +55,7 @@ pub fn stream_query_results<'a>(
let query = bind_parameters(value, request).await?;
let connection = take_connection(db, &mut connection_opt).await?;
log::debug!("Executing query to set the {variable:?} variable: {:?}", query.sql);
let value: Option<String> = connection.fetch_optional(query).await?
.and_then(|row| row.try_get::<Option<String>, _>(0).ok().flatten());
let value: Option<String> = connection.fetch_optional(query).await?.as_ref().and_then(row_to_string);
let (vars, name) = vars_and_name(request, variable)?;
if let Some(value) = value {
log::debug!("Setting variable {name} to {value:?}");
Expand Down
10 changes: 10 additions & 0 deletions src/webserver/database/sql_to_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@ pub fn sql_nonnull_to_json<'r>(mut get_ref: impl FnMut() -> sqlx::any::AnyValueR
}
}

/// Takes the first column of a row and converts it to a string.
pub fn row_to_string(row: &AnyRow) -> Option<String> {
let col = row.columns().get(0)?;
match sql_to_json(row, col) {
serde_json::Value::String(s) => Some(s),
serde_json::Value::Null => None,
other => Some(other.to_string()),
}
}

#[actix_web::test]
async fn test_row_to_json() -> anyhow::Result<()> {
use sqlx::Connection;
Expand Down
9 changes: 9 additions & 0 deletions tests/sql_test_files/it_works_set_variable_numeric.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
set two = 2;
select 'text' as component,
CASE
WHEN $two = '2' -- All variables are strings
THEN
'It works !'
ELSE
'error: expected "2", got: ' || COALESCE($two, 'null')
END as contents;

0 comments on commit ad0fc2c

Please sign in to comment.