GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(mssql): preserve caller SQL verbatim · zerodaycode/Canyon-SQL@86844d4

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • canyon_core/src/connection/clients
    • mssql.rs
  • tests/crud
    • read_operations.rs
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎canyon_core/src/connection/clients/mssql.rs‎

Copy file name to clipboardExpand all lines: canyon_core/src/connection/clients/mssql.rs
+12-21Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,10 @@ impl DbConnection for SqlServerConnector {
118118
}
119119

120120
async fn execute(&self, stmt: &str, params: &[&'_ dyn QueryParameter]) -> CanyonResult<u64> {
121-
let mssql_query = crate::connection::clients::mssql::sqlserver_query_launcher::generate_mssql_query_client(stmt, params).await;
121+
let mssql_query =
122+
crate::connection::clients::mssql::sqlserver_query_launcher::generate_mssql_query_client(
123+
stmt, params,
124+
);
122125
let mut conn = self.get_pooled().await?;
123126

124127
mssql_query
@@ -142,35 +145,23 @@ pub(crate) mod sqlserver_query_launcher {
142145
params: &[&dyn QueryParameter],
143146
conn: &'a mut bb8::PooledConnection<'_, bb8_tiberius::ConnectionManager>,
144147
) -> CanyonResult<QueryStream<'a>> {
145-
let mssql_query = generate_mssql_query_client(stmt, params).await;
148+
let mssql_query = generate_mssql_query_client(stmt, params);
146149
mssql_query
147150
.query(conn)
148151
.await
149152
.map_err(|source| QueryError::sql_server(source).into())
150153
}
151154

152-
pub(crate) async fn generate_mssql_query_client<'a>(
155+
/// Builds a Tiberius query without modifying the supplied SQL.
156+
///
157+
/// Statements produced by Canyon's query builder already use SQL Server's
158+
/// identifier, placeholder and `OUTPUT INSERTED` syntax. Callers providing
159+
/// raw statements are likewise responsible for supplying SQL Server SQL.
160+
pub(crate) fn generate_mssql_query_client<'a>(
153161
stmt: &str,
154162
params: &[&'a dyn QueryParameter],
155163
) -> Query<'a> {
156-
let mut stmt = String::from(stmt);
157-
158-
if stmt.contains("RETURNING") {
159-
// TODO: when the InsertQuerybuilder with a api on the builder for the returning clause
160-
let c = stmt.clone();
161-
let temp = c.split_once("RETURNING").unwrap();
162-
let temp2 = temp.0.split_once("VALUES").unwrap();
163-
164-
stmt = format!(
165-
"{} OUTPUT inserted.{} VALUES {}",
166-
temp2.0.trim(),
167-
temp.1.trim(),
168-
temp2.1.trim()
169-
);
170-
}
171-
172-
let stmt = stmt.replace('$', "@P"); // TODO: this should be solved by the querybuilder
173-
generate_query_and_bind_params(stmt, params)
164+
generate_query_and_bind_params(stmt.to_owned(), params)
174165
}
175166

176167
// Query and parameters are generated in this procedure together to avoid lifetime errors
Collapse file

‎tests/crud/read_operations.rs‎

Copy file name to clipboardExpand all lines: tests/crud/read_operations.rs
+16Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ use crate::tests_models::league::*;
1313
use crate::tests_models::player::*;
1414

1515
use canyon_sql::crud::ReadOperations;
16+
#[cfg(feature = "mssql")]
17+
use canyon_sql::{connection::DbConnection, core::Canyon};
1618

1719
/// Tests the behaviour of a SELECT * FROM {table_name} within Canyon, through the
1820
/// `::find_all()` associated function derived with the `CanyonCrud` derive proc-macro
@@ -110,6 +112,20 @@ fn test_crud_find_by_pk_with_mssql() {
110112
);
111113
}
112114

115+
/// Raw SQL sent to SQL Server must not be rewritten as though it had been
116+
/// generated for PostgreSQL. Dollar signs and SQL keywords inside literals
117+
/// are application data, not placeholder or `RETURNING` syntax.
118+
#[cfg(feature = "mssql")]
119+
#[canyon_sql::macros::canyon_tokio_test]
120+
fn test_mssql_raw_sql_preserves_dollar_signs_and_returning_literals() {
121+
let connection = Canyon::instance()?.get_connection(SQL_SERVER_DS)?;
122+
let value: String = connection
123+
.query_one_for("SELECT CAST('$1 RETURNING' AS NVARCHAR(32));", &[])
124+
.await?;
125+
126+
assert_eq!(value, "$1 RETURNING");
127+
}
128+
113129
/// Tests the behaviour of a SELECT * FROM {table_name} WHERE <pk> = <pk_value>, where the pk is
114130
/// defined with the #[primary_key] attribute over some field of the type.
115131
///

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.