Skip to content

Commit ad0fc2c

Browse files
committed
Fix bug with variable assignment
fixes #138
1 parent 23a49d1 commit ad0fc2c

File tree

6 files changed

+26
-6
lines changed

6 files changed

+26
-6
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# CHANGELOG.md
22

3-
## unreleased
3+
## 0.16.1
44

5+
- fix a bug where setting a variable to a non-string value would always set it to null
56
- clearer debug logs (https://github.com/wooorm/markdown-rs/pull/92)
67
- update compiler to rust 1.74
78
- use user id and group id 1000 in docker image (this is the default user id in most linux distributions)

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "sqlpage"
3-
version = "0.16.0"
3+
version = "0.16.1"
44
edition = "2021"
55
description = "A SQL-only web application framework. Takes .sql files and formats the query result using pre-made configurable professional-looking components."
66
keywords = ["web", "sql", "framework"]

src/webserver/database/execute_queries.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ use std::collections::HashMap;
66

77
use super::sql::{ParsedSqlFile, ParsedStatement, StmtWithParams};
88
use crate::webserver::database::sql_pseudofunctions::extract_req_param;
9+
use crate::webserver::database::sql_to_json::row_to_string;
910
use crate::webserver::http::{RequestInfo, SingleOrVec};
1011

1112
use sqlx::any::{AnyArguments, AnyQueryResult, AnyRow, AnyStatement, AnyTypeInfo};
1213
use sqlx::pool::PoolConnection;
13-
use sqlx::{Any, AnyConnection, Arguments, Either, Executor, Row, Statement};
14+
use sqlx::{Any, AnyConnection, Arguments, Either, Executor, Statement};
1415

1516
use super::sql_pseudofunctions::StmtParam;
1617
use super::{highlight_sql_error, Database, DbItem};
@@ -54,8 +55,7 @@ pub fn stream_query_results<'a>(
5455
let query = bind_parameters(value, request).await?;
5556
let connection = take_connection(db, &mut connection_opt).await?;
5657
log::debug!("Executing query to set the {variable:?} variable: {:?}", query.sql);
57-
let value: Option<String> = connection.fetch_optional(query).await?
58-
.and_then(|row| row.try_get::<Option<String>, _>(0).ok().flatten());
58+
let value: Option<String> = connection.fetch_optional(query).await?.as_ref().and_then(row_to_string);
5959
let (vars, name) = vars_and_name(request, variable)?;
6060
if let Some(value) = value {
6161
log::debug!("Setting variable {name} to {value:?}");

src/webserver/database/sql_to_json.rs

+10
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,16 @@ pub fn sql_nonnull_to_json<'r>(mut get_ref: impl FnMut() -> sqlx::any::AnyValueR
9999
}
100100
}
101101

102+
/// Takes the first column of a row and converts it to a string.
103+
pub fn row_to_string(row: &AnyRow) -> Option<String> {
104+
let col = row.columns().get(0)?;
105+
match sql_to_json(row, col) {
106+
serde_json::Value::String(s) => Some(s),
107+
serde_json::Value::Null => None,
108+
other => Some(other.to_string()),
109+
}
110+
}
111+
102112
#[actix_web::test]
103113
async fn test_row_to_json() -> anyhow::Result<()> {
104114
use sqlx::Connection;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
set two = 2;
2+
select 'text' as component,
3+
CASE
4+
WHEN $two = '2' -- All variables are strings
5+
THEN
6+
'It works !'
7+
ELSE
8+
'error: expected "2", got: ' || COALESCE($two, 'null')
9+
END as contents;

0 commit comments

Comments
 (0)