Add case-insensitive support for database columns

Some databases (maybe all of them) will set the database column
names to lowercase.

This was problematic because we needed to match these names
with the ksql tags, which were not lowercased.

Now we support both versions, lowercased or not.
new-query-chunks-api
Vinícius Garcia 2022-04-30 12:51:24 -03:00
parent 550ba9e8d9
commit 1ed7684b6d
3 changed files with 26 additions and 2 deletions

View File

@ -767,11 +767,11 @@ is configured to kill the containers after 20 seconds.
## TODO List
- Add tests for tables using composite keys
- Add tests for tables using composite keys (ongoing)
- Add support for serializing structs as other formats such as YAML
- Update `ksqltest.FillStructWith` to work with `ksql:"..,json"` tagged attributes
- Create a way for users to submit user defined dialects
- Improve error messages
- Improve error messages (ongoing)
- Add support for the Patch function to work with maps for partial updates
- Add support for the Insert function to work with maps
- Add support for a `ksql.Array(params ...interface{})` for allowing queries like this:

View File

@ -49,6 +49,12 @@ func (s StructInfo) add(field FieldInfo) {
field.Valid = true
s.byIndex[field.Index] = &field
s.byName[field.Name] = &field
// Make sure to save a lowercased version because
// some databases will set these keys to lowercase.
if _, found := s.byName[strings.ToLower(field.Name)]; !found {
s.byName[strings.ToLower(field.Name)] = &field
}
}
// NumFields ...

View File

@ -595,6 +595,24 @@ func QueryOneTest(
tt.AssertEqual(t, row.User.Name, "João Ribeiro")
tt.AssertEqual(t, row.Post.Title, "João Post1")
})
t.Run("should handle column tags as case-insensitive as SQL does", func(t *testing.T) {
db, closer := newDBAdapter(t)
defer closer.Close()
ctx := context.Background()
_, err := db.ExecContext(ctx, `INSERT INTO users (name, age, address) VALUES ('Count Olivia', 0, '{"country":"US"}')`)
tt.AssertNoErr(t, err)
c := newTestDB(db, driver)
var row struct {
CountAttr int `ksql:"myCount"`
}
err = c.QueryOne(ctx, &row, `SELECT count(*) as myCount FROM users WHERE name='Count Olivia'`)
tt.AssertNoErr(t, err)
tt.AssertEqual(t, row.CountAttr, 1)
})
})
}