| 6 | ) |
| 7 | |
| 8 | func TestToSQLLiteral(t *testing.T) { |
| 9 | fixedTime := time.Date(2024, 3, 15, 10, 30, 45, 123456789, time.UTC) |
| 10 | |
| 11 | tests := []struct { |
| 12 | name string |
| 13 | val any |
| 14 | want string |
| 15 | }{ |
| 16 | // Nil |
| 17 | {"nil", nil, "NULL"}, |
| 18 | |
| 19 | // Strings |
| 20 | {"string", "hello", "'hello'"}, |
| 21 | {"string with quote", "it's", "'it''s'"}, |
| 22 | {"string multiple quotes", "a''b", "'a''''b'"}, |
| 23 | {"empty string", "", "''"}, |
| 24 | |
| 25 | // Integers |
| 26 | {"int", 42, "42"}, |
| 27 | {"int negative", -42, "-42"}, |
| 28 | {"int8", int8(127), "127"}, |
| 29 | {"int16", int16(-32000), "-32000"}, |
| 30 | {"int32", int32(2147483647), "2147483647"}, |
| 31 | {"int64", int64(9223372036854775807), "9223372036854775807"}, |
| 32 | |
| 33 | // Unsigned integers |
| 34 | {"uint", uint(42), "42"}, |
| 35 | {"uint8", uint8(255), "255"}, |
| 36 | {"uint16", uint16(65535), "65535"}, |
| 37 | {"uint32", uint32(4294967295), "4294967295"}, |
| 38 | {"uint64", uint64(18446744073709551615), "18446744073709551615"}, |
| 39 | |
| 40 | // Floats |
| 41 | {"float32", float32(3.14), "3.14"}, |
| 42 | {"float32 whole", float32(42), "42"}, |
| 43 | {"float64", 3.14159265359, "3.14159265359"}, |
| 44 | {"float64 scientific", 1.23e10, "12300000000"}, |
| 45 | {"float64 negative", -0.001, "-0.001"}, |
| 46 | |
| 47 | // Booleans |
| 48 | {"bool true", true, "TRUE"}, |
| 49 | {"bool false", false, "FALSE"}, |
| 50 | |
| 51 | // Time |
| 52 | {"time.Time", fixedTime, "'2024-03-15T10:30:45.123456789Z'"}, |
| 53 | {"*time.Time", &fixedTime, "'2024-03-15T10:30:45.123456789Z'"}, |
| 54 | {"*time.Time nil", (*time.Time)(nil), "NULL"}, |
| 55 | |
| 56 | // Pointers to primitives |
| 57 | {"*string", ptr("hello"), "'hello'"}, |
| 58 | {"*string nil", (*string)(nil), "NULL"}, |
| 59 | {"*int", ptr(42), "42"}, |
| 60 | {"*int nil", (*int)(nil), "NULL"}, |
| 61 | {"*bool", ptr(true), "TRUE"}, |
| 62 | {"**int", ptr(ptr(42)), "42"}, |
| 63 | {"**int outer nil", (**int)(nil), "NULL"}, |
| 64 | |
| 65 | // Slices |