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: bind unsigned integers safely in mssql · zerodaycode/Canyon-SQL@b3ea39e

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • canyon_core/src/query
    • parameters.rs
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎canyon_core/src/query/parameters.rs‎

Copy file name to clipboardExpand all lines: canyon_core/src/query/parameters.rs
+37-2Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,8 @@ impl QueryParameter for u32 {
146146
}
147147
#[cfg(feature = "mssql")]
148148
fn as_sqlserver_param(&self) -> ColumnData<'_> {
149-
panic!("Unsupported sqlserver parameter type <u32>");
149+
// SQL Server has no unsigned integer type. BIGINT represents every u32 losslessly.
150+
ColumnData::I64(Some(i64::from(*self)))
150151
}
151152
#[cfg(feature = "mysql")]
152153
fn as_mysql_param(&self) -> Value {
@@ -165,7 +166,7 @@ impl QueryParameter for Option<u32> {
165166
}
166167
#[cfg(feature = "mssql")]
167168
fn as_sqlserver_param(&self) -> ColumnData<'_> {
168-
panic!("Unsupported sqlserver parameter type <u32>");
169+
ColumnData::I64(self.map(i64::from))
169170
}
170171
#[cfg(feature = "mysql")]
171172
fn as_mysql_param(&self) -> Value {
@@ -599,6 +600,40 @@ mod mssql_tests {
599600
));
600601
assert!(matches!(none.as_sqlserver_param(), ColumnData::I16(None)));
601602
}
603+
604+
#[test]
605+
fn u32_parameter_uses_lossless_sql_server_bigint() {
606+
let first_value_beyond_i32 = i32::MAX as u32 + 1;
607+
608+
assert!(matches!(
609+
0_u32.as_sqlserver_param(),
610+
ColumnData::I64(Some(0))
611+
));
612+
assert!(matches!(
613+
(i32::MAX as u32).as_sqlserver_param(),
614+
ColumnData::I64(Some(value)) if value == i64::from(i32::MAX)
615+
));
616+
assert!(matches!(
617+
first_value_beyond_i32.as_sqlserver_param(),
618+
ColumnData::I64(Some(value)) if value == i64::from(first_value_beyond_i32)
619+
));
620+
assert!(matches!(
621+
u32::MAX.as_sqlserver_param(),
622+
ColumnData::I64(Some(value)) if value == i64::from(u32::MAX)
623+
));
624+
}
625+
626+
#[test]
627+
fn optional_u32_parameter_preserves_some_and_none() {
628+
assert!(matches!(
629+
Some(u32::MAX).as_sqlserver_param(),
630+
ColumnData::I64(Some(value)) if value == i64::from(u32::MAX)
631+
));
632+
assert!(matches!(
633+
Option::<u32>::None.as_sqlserver_param(),
634+
ColumnData::I64(None)
635+
));
636+
}
602637
}
603638

604639
#[cfg(all(test, feature = "postgres"))]

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.