schotime / NPoco

Simple microORM that maps the results of a query onto a POCO object. Project based on Schotime's branch of PetaPoco
Apache License 2.0
848 stars 302 forks source link

Mappers are not applied when generating query WHERE clauses #668

Open bryanboettcher opened 2 years ago

bryanboettcher commented 2 years ago

For "legacy application" reasons our database primarily uses CHAR column types with a "Y" or "N" to represent boolean values. We created a BooleanMapper class that appropriately handles the conversion to and from the entity type and the database type.

Assume a table like this:

tbl_entities:
id  INT
enabled CHAR

If we query with: db.Query<TblEntities>().Where(e => e.Enabled), the generated SQL effectively becomes SELECT [id], [enabled] FROM tbl_entities WHERE enabled = 1. If the query were to succeed, the BooleanMapper we wrote will appropriately convert the dataset. But, because the enabled field is a char, the conversion to '1' fails during the query execution. If the query provider were aware of the registered mappers, the conversion to 'Y' could happen before the query is sent to the database provider.