Postgres requires parameters to be correctly typed

Signed-off-by: itowlson <ivan.towlson@fermyon.com>
This commit is contained in:
itowlson 2022-08-08 17:37:01 +12:00
parent 14faf6c480
commit 69a45f49a7
No known key found for this signature in database
GPG Key ID: E3B130561698B2F8
3 changed files with 26 additions and 6 deletions

View File

@ -37,14 +37,14 @@ impl outbound_pg::OutboundPg for OutboundPg {
&mut self,
address: &str,
statement: &str,
params: Vec<&str>,
params: Vec<ParameterValue<'_>>,
) -> Result<u64, PgError> {
let mut client = Client::connect(address, NoTls)
.map_err(|e| PgError::ConnectionFailed(format!("{:?}", e)))?;
let params: Vec<&(dyn ToSql + Sync)> = params
.iter()
.map(|item| item as &(dyn ToSql + Sync))
.map(to_sql_parameter)
.collect();
let nrow = client
@ -58,14 +58,14 @@ impl outbound_pg::OutboundPg for OutboundPg {
&mut self,
address: &str,
statement: &str,
params: Vec<&str>,
params: Vec<ParameterValue<'_>>,
) -> Result<RowSet, PgError> {
let mut client = Client::connect(address, NoTls)
.map_err(|e| PgError::ConnectionFailed(format!("{:?}", e)))?;
let params: Vec<&(dyn ToSql + Sync)> = params
.iter()
.map(|item| item as &(dyn ToSql + Sync))
.map(to_sql_parameter)
.collect();
let results = client
@ -86,6 +86,18 @@ impl outbound_pg::OutboundPg for OutboundPg {
}
}
const DB_NULL: Option<i32> = None;
fn to_sql_parameter<'a>(value: &'a ParameterValue) -> &'a (dyn ToSql + Sync) {
match value {
ParameterValue::Boolean(v) => v,
ParameterValue::Int32(v) => v,
ParameterValue::Int64(v) => v,
ParameterValue::DbString(v) => v,
ParameterValue::DbNull => &DB_NULL,
}
}
fn infer_columns(row: &Row) -> Vec<Column> {
let mut result = Vec::with_capacity(row.len());
for index in 0..row.len() {

View File

@ -2,7 +2,7 @@ use * from pg-types
use * from rdbms-types
// query the database: select
query: func(address: string, statement: string, params: list<string>) -> expected<row-set, pg-error>
query: func(address: string, statement: string, params: list<parameter-value>) -> expected<row-set, pg-error>
// execute command to the database: insert, update, delete
execute: func(address: string, statement: string, params: list<string>) -> expected<u64, pg-error>
execute: func(address: string, statement: string, params: list<parameter-value>) -> expected<u64, pg-error>

View File

@ -15,6 +15,14 @@ variant db-value {
unsupported,
}
variant parameter-value {
boolean(bool),
int32(s32),
int64(s64),
db-string(string),
db-null,
}
record column {
name: string,
data-type: db-data-type,