Commit 8ab6c18
authored
Introducing parameters' DbType before passing on to the database - MsSql (#1442)
## Why make this change?
Fixes #475
Most column types will accept a string null and the db will
automatically infer our intention, but for varbinary in MsSql the db
complains that it cannot convert varchar to varbinary. For more details
about the issue refer:
https://stackoverflow.com/questions/29254690/why-does-dbnull-value-require-a-proper-sqldbtype
## What is this change?
Changing how we form the `parameters` as we pass them to the database.
Previously we used to only pass the parameter's value. Now, we would
also pass the parameter's `DbType` along with the value as suggested in
the stack overflow article.
**Notes:**
1. We would only be passing the DbType for MsSql.
2. Usually, we haven't seen any need to pass the `DbType` for Sql Server
data types other than `varbinary`. However, we will still be supplying
the DbType for all other parameter's for which we are confirmed about
the 1:1 mapping from SystemType to DbType. Sql server datatypes like
`datetime`, `datetime2`, `datetimeoffset` are all treated as `datetime`
by graphql, so there is no 1:1 mapping for datetime datatypes. So we are
skipping supplying the DbType for these.
3. As explained in this stack overflow article
[here](https://stackoverflow.com/questions/6868300/net-guid-uppercase-string-format),
the GUID when converted to string, is output in lowercase. That's why
when the query to verify the MsSql test `InsertOneInSupportedTypes` is
written, we have explicitly cast `guid_types` to lower case.
## Additional change
1. The `real` sql server datatype was incorrectly mapped to `long`
dotnet type. It has been correctly mapped to `decimal` now.
## Issue with DateTime/DateTimeOffset Sql Server data types
For a mutation request like this, we get a response:
<img width="873" alt="image"
src="https://user-images.githubusercontent.com/34566234/235473783-87b810b1-bf10-44a6-8671-c54b74e44dac.png">
and the parameters are generated as:
<img width="510" alt="image"
src="https://user-images.githubusercontent.com/34566234/235470342-5331c755-abdc-4e05-9f9c-467e406d0038.png">
the parameter generated is of type `DateTimeOffset` (as evident from the
value highlighted) but the underlying Sql Server datatype for the
`datetime_types` field is `datetime`. Even though we supplied a value
`1999-01-08 09:20:00` in the mutation, it became `08-01-1999 09:20:00
+05:30`, because that's how graphql treats datetime/datetimoffset/date
types. Then this implicit conversion from a value of type DateTimeOffset
to DateTime cannot happen throwing an exception with message : `Failed
to convert parameter value from a DateTimeOffset to a DateTime.` Hence
we cannot add a mapping from `typeof(DateTime) = DbType.DateTime`.
Another option that came to my mind and would probably come to your mind
as well would be to have a mapping from`typeof(DateTime) =
DbType.DateTimeOffset`, since then there can be implicit conversion from
value of `DateTimeOffset` type to `DbType.DateTimeOffset`. But that has
its own flaws. Lets look at the below screenshots:
Consider this mutation:
<img width="885" alt="image"
src="https://user-images.githubusercontent.com/34566234/235477733-19c18d73-7eaa-42ba-9484-52984c62baad.png">
and the parameters generated for the query (mutation ran successfully
because the default value for the `instant` PK column was inserted into
the table):
<img width="425" alt="image"
src="https://user-images.githubusercontent.com/34566234/235478676-6c2b5bc7-80bc-497d-a53d-e3ecff80538e.png">
The parameter value is of sql server type datetime but the DbType is
populated as `DbType.DateTimeOffset`. This upcasting cannot happen
correctly and so we are returned with 0 records. So, this approach won't
work as well.
**Conclusion:** We won't populate DbType for these sql server types as
it is not required and would prevent data loss and any unforeseen
exceptions.
## How was this tested?
- [x] Integration Tests
- [x] Unit Tests - The unit tests that were skipped before saying that
bytearray datatype is not supported are now running successfully for
MsSql.
## Sample Request(s)
Request method: POST
Request URL: https://localhost:5001/api/SupportedType/
Request:
```json
{
"bytearray_types": null
}
```
Response:
```json
{
"value": [
{
"typeid": 5001,
"byte_types": null,
"short_types": null,
"int_types": null,
"long_types": null,
"string_types": null,
"single_types": null,
"float_types": null,
"decimal_types": null,
"boolean_types": null,
"date_types": null,
"datetime_types": null,
"datetime2_types": null,
"datetimeoffset_types": null,
"smalldatetime_types": null,
"bytearray_types": null,
"guid_types": "27742da6-99f2-4e58-9fd8-4f143aea46b0"
}
]
}
```1 parent 0cc0ee9 commit 8ab6c18
File tree
30 files changed
+362
-86
lines changed- src
- Config
- Service.Tests
- SqlTests
- GraphQLSupportedTypesTests
- RestApiTests/Insert
- Unittests
- Service
- Models
- Parsers
- Resolvers
- Sql Query Structures
- Services
- MetadataProviders
30 files changed
+362
-86
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
| 4 | + | |
| 5 | + | |
4 | 6 | | |
5 | 7 | | |
6 | 8 | | |
| |||
105 | 107 | | |
106 | 108 | | |
107 | 109 | | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
108 | 121 | | |
109 | 122 | | |
110 | 123 | | |
111 | 124 | | |
112 | 125 | | |
| 126 | + | |
113 | 127 | | |
114 | 128 | | |
115 | 129 | | |
| |||
153 | 167 | | |
154 | 168 | | |
155 | 169 | | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
156 | 188 | | |
157 | 189 | | |
158 | 190 | | |
| |||
178 | 210 | | |
179 | 211 | | |
180 | 212 | | |
| 213 | + | |
181 | 214 | | |
182 | 215 | | |
183 | 216 | | |
| |||
Lines changed: 0 additions & 15 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
7 | | - | |
8 | 7 | | |
9 | 8 | | |
10 | 9 | | |
| |||
34 | 33 | | |
35 | 34 | | |
36 | 35 | | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | | - | |
41 | | - | |
42 | | - | |
43 | | - | |
44 | | - | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | 36 | | |
52 | 37 | | |
Lines changed: 26 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
60 | 60 | | |
61 | 61 | | |
62 | 62 | | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
63 | 89 | | |
64 | 90 | | |
65 | 91 | | |
| |||
Lines changed: 8 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
30 | 38 | | |
31 | 39 | | |
32 | 40 | | |
| |||
Lines changed: 11 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
29 | 40 | | |
30 | 41 | | |
31 | 42 | | |
| |||
Lines changed: 12 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
30 | 42 | | |
31 | 43 | | |
32 | 44 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
346 | 346 | | |
347 | 347 | | |
348 | 348 | | |
349 | | - | |
| 349 | + | |
| 350 | + | |
350 | 351 | | |
351 | 352 | | |
352 | 353 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
6 | 7 | | |
7 | 8 | | |
8 | 9 | | |
9 | 10 | | |
10 | 11 | | |
11 | 12 | | |
12 | 13 | | |
| 14 | + | |
13 | 15 | | |
14 | 16 | | |
15 | 17 | | |
| |||
137 | 139 | | |
138 | 140 | | |
139 | 141 | | |
140 | | - | |
| 142 | + | |
141 | 143 | | |
142 | 144 | | |
143 | 145 | | |
| |||
146 | 148 | | |
147 | 149 | | |
148 | 150 | | |
149 | | - | |
| 151 | + | |
150 | 152 | | |
151 | 153 | | |
152 | 154 | | |
| |||
155 | 157 | | |
156 | 158 | | |
157 | 159 | | |
158 | | - | |
| 160 | + | |
159 | 161 | | |
160 | 162 | | |
161 | 163 | | |
| |||
189 | 191 | | |
190 | 192 | | |
191 | 193 | | |
192 | | - | |
| 194 | + | |
193 | 195 | | |
194 | 196 | | |
195 | 197 | | |
| |||
200 | 202 | | |
201 | 203 | | |
202 | 204 | | |
203 | | - | |
| 205 | + | |
204 | 206 | | |
205 | 207 | | |
206 | 208 | | |
| |||
209 | 211 | | |
210 | 212 | | |
211 | 213 | | |
212 | | - | |
| 214 | + | |
213 | 215 | | |
214 | 216 | | |
215 | 217 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
204 | 204 | | |
205 | 205 | | |
206 | 206 | | |
207 | | - | |
| 207 | + | |
208 | 208 | | |
209 | 209 | | |
210 | 210 | | |
| |||
300 | 300 | | |
301 | 301 | | |
302 | 302 | | |
303 | | - | |
| 303 | + | |
304 | 304 | | |
305 | 305 | | |
306 | 306 | | |
| |||
346 | 346 | | |
347 | 347 | | |
348 | 348 | | |
349 | | - | |
| 349 | + | |
350 | 350 | | |
351 | 351 | | |
352 | 352 | | |
| |||
472 | 472 | | |
473 | 473 | | |
474 | 474 | | |
475 | | - | |
| 475 | + | |
476 | 476 | | |
477 | 477 | | |
478 | 478 | | |
| |||
542 | 542 | | |
543 | 543 | | |
544 | 544 | | |
545 | | - | |
| 545 | + | |
546 | 546 | | |
547 | 547 | | |
548 | 548 | | |
| |||
0 commit comments