Skip to content
This repository has been archived by the owner on Apr 25, 2023. It is now read-only.

Prevent decimal/float conversions where unstable. Allow casting of expressions. #292

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

pimeys
Copy link
Contributor

@pimeys pimeys commented May 11, 2021

  • JSON serialization from decimal should be a string
  • Do not allow writing to a decimal field with floats, or a float field with decimals
  • API to cast expressions.

Part of: prisma/prisma#6902

@pimeys pimeys added this to the 2.23.0 milestone May 11, 2021
@pimeys pimeys force-pushed the serialize-decimals-as-strings branch from 6481e1a to 9d1fc75 Compare May 11, 2021 15:48
@pimeys pimeys changed the title The JSON representation of a BigDecimal should be string Prevent decimal/float conversions where unstable May 12, 2021
@pimeys pimeys force-pushed the serialize-decimals-as-strings branch from 5acae5a to 042e341 Compare May 12, 2021 08:36
@pimeys pimeys changed the title Prevent decimal/float conversions where unstable Prevent decimal/float conversions where unstable. Allow casting of expressions. May 18, 2021
Julius de Bruijn added 4 commits May 19, 2021 09:49
It's unstable and will lead to tears.
Introduces a new trait `Castable`, implemented so far by anything that
can be converted to an `Expression`.

Usage:

```rust
Select::default().value(1.cast_to(CastType::int2()))
```

Will convert the given value to an `int2` or a corresponding type in the
database. Casting will happen on all visitors except SQLite. To
whitelist where casting should happen, it should happen in the
`CastType`:

```rust
Select::default().value(1.cast_to(CastType::int2().on_postgres()))
```

This will only cast on PostgreSQL. The methods can be chained:

```
1.cast_to(CastType::int2().on_postgres().on_sql_server());
```

There are certain restrictions what can be casted, and the casting
mechanism tries to find the closest allowed type from each database.
@pimeys pimeys force-pushed the serialize-decimals-as-strings branch from 2bee954 to 7588c4b Compare May 19, 2021 07:59
@pimeys pimeys force-pushed the serialize-decimals-as-strings branch from fdc5b01 to 1276f6a Compare May 19, 2021 08:41
@sumeetattree
Copy link

@pimeys This looks great! Just what I have been looking for to add query support to enum types. Anything I can do to help fast track this PR?

@sumeetattree
Copy link

sumeetattree commented Jul 1, 2021

@pimeys Slight correction in postgres visitor (Not sure about other databases), the custom type name should be double quoted.

let comparison = "kind".equals(value.cast_as("UserKind"));

The query output is:

SELECT    
  "public"."users".*    
FROM    
  "public"."users"    
WHERE    
  "kind" = ($1) :: UserKind # I know crazy type name, but sometimes external databases have stuff like this

UserKind will actually be converted to userkind by postgres. To avoid this the type must be quoted. "\"UserKind\"". The CastType::Custom should automatically quote the typenames.

@pimeys
Copy link
Contributor Author

pimeys commented Jul 1, 2021

This whole issue is on hold now due to not having time in any team and if we merge this, we'll break our main product (it needs to get the corresponding PR finished and merged)...

@sumeetattree
Copy link

Ah, understood. Thanks, I'll try something at my end.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants