Skip to content

Commit 3c1d71b

Browse files
Address code review feedback: improve documentation and test clarity
Co-authored-by: dlevy-msft-sql <[email protected]>
1 parent d50f988 commit 3c1d71b

2 files changed

Lines changed: 16 additions & 15 deletions

File tree

pkg/sqlcmd/sqlcmd.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -413,10 +413,6 @@ func (s *Sqlcmd) getRunnableQuery(q string) string {
413413
return b.String()
414414
}
415415

416-
// runQuery runs the query and prints the results
417-
// The return value is based on the first cell of the last column of the last result set.
418-
// If it's numeric, it will be converted to int
419-
// -100 : Error encountered prior to selecting return value
420416
// safeColumnTypes safely calls rows.ColumnTypes() and recovers from any panics
421417
// that might occur in the underlying driver, particularly for unsupported types
422418
// like GEOGRAPHY and GEOMETRY (SQL Server type 240).
@@ -431,6 +427,10 @@ func safeColumnTypes(rows *sql.Rows) (cols []*sql.ColumnType, err error) {
431427
return rows.ColumnTypes()
432428
}
433429

430+
// runQuery runs the query and prints the results
431+
// The return value is based on the first cell of the last column of the last result set.
432+
// If it's numeric, it will be converted to int
433+
// -100 : Error encountered prior to selecting return value
434434
// -101: No rows found
435435
// -102: Conversion error occurred when selecting return value
436436
func (s *Sqlcmd) runQuery(query string) (int, error) {

pkg/sqlcmd/sqlcmd_test.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -707,18 +707,19 @@ func TestSqlcmdPrefersSharedMemoryProtocol(t *testing.T) {
707707
}
708708

709709
// TestSafeColumnTypesHandlesPanic verifies that safeColumnTypes properly catches
710-
// panics from the underlying driver and converts them to errors
710+
// panics from the underlying driver and converts them to errors.
711+
//
712+
// This test validates the panic recovery mechanism by triggering a panic with a
713+
// nil Rows pointer. While this doesn't test the exact GEOGRAPHY/GEOMETRY type 240
714+
// panic from the driver, it proves that the defer/recover mechanism works correctly
715+
// and any panic (including the type 240 panic) will be caught and converted to an error.
716+
//
717+
// The actual GEOGRAPHY/GEOMETRY panic occurs deep inside the go-mssqldb driver's
718+
// makeGoLangScanType function when it encounters type 240. Our safeColumnTypes
719+
// wrapper ensures this panic is caught regardless of where in the ColumnTypes()
720+
// call stack it originates.
711721
func TestSafeColumnTypesHandlesPanic(t *testing.T) {
712-
// Create a mock Rows that will panic when ColumnTypes is called
713-
// Since we cannot easily mock sql.Rows, we test the panic recovery mechanism
714-
// by calling the function with a nil Rows pointer, which will panic
715-
716-
// This test verifies that the defer/recover mechanism works
717-
// In a real scenario with GEOGRAPHY/GEOMETRY types, the driver would panic
718-
// inside ColumnTypes() and our function should catch it
719-
720-
// We can't easily create a failing sql.Rows without a real database connection
721-
// but we can at least verify the function exists and doesn't panic with nil
722+
// Verify that the defer/recover mechanism works by triggering any panic
722723
defer func() {
723724
if r := recover(); r != nil {
724725
t.Errorf("safeColumnTypes should not panic, but got: %v", r)

0 commit comments

Comments
 (0)