-
-
Notifications
You must be signed in to change notification settings - Fork 27
Add provider specific column types #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 16 commits
c656d78
cf557ea
a0f01b3
ba702b7
525a05c
b2aecb9
bc80625
c08a55d
268bb58
3b32f80
602d507
e646095
4e4db59
a88f445
f38df21
f1d2d95
fd0e9a6
feaf2ef
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| namespace SqlHydra | ||
|
|
||
| open System | ||
|
|
||
| [<AttributeUsage(AttributeTargets.Property | ||
| ||| AttributeTargets.Field)>] | ||
| type ProviderDbTypeAttribute(providerDbTypeName: string) = | ||
| inherit Attribute() | ||
|
|
||
| member this.ProviderDbTypeName = providerDbTypeName |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>netstandard2.0</TargetFramework> | ||
| <GenerateDocumentationFile>true</GenerateDocumentationFile> | ||
| <WarnOn>3390;$(WarnOn)</WarnOn> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <Compile Include="Domain.fs" /> | ||
| <Compile Include="ProviderDbTypeAttribute.fs" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,7 +5,21 @@ open SqlKata | |
|
|
||
| /// Contains methods that compile and read a query. | ||
| type QueryContext(conn: DbConnection, compiler: SqlKata.Compilers.Compiler) = | ||
|
|
||
| let setProviderDbType (param: DbParameter) (propertyName: string) (providerDbType: string) = | ||
| let property = param.GetType().GetProperty(propertyName) | ||
| let dbTypeSetter = property.GetSetMethod() | ||
|
|
||
| let value = System.Enum.Parse(property.PropertyType, providerDbType) | ||
| dbTypeSetter.Invoke(param, [|value|]) |> ignore | ||
|
|
||
| let setParameterDbType (param: DbParameter) (qp: QueryParameter) = | ||
| match qp.ProviderDbType, compiler with | ||
| | Some type', :? SqlKata.Compilers.PostgresCompiler -> | ||
| setProviderDbType param "NpgsqlDbType" type' | ||
| | Some type', :? SqlKata.Compilers.SqlServerCompiler -> | ||
| setProviderDbType param "SqlDbType" type' | ||
| | _ -> () | ||
|
|
||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be nice to add a handler here for SQL Server as well: | Some type', :? SqlKata.Compilers.SqlServerCompiler when type'.TypeName = "SqlDbType" ->
let property = param.GetType().GetProperty("SqlDbType")
...
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What you have in So this should work: let setParameterDbType (param: DbParameter) (qp: QueryParameter) =
match qp.ProviderDbType, compiler with
| Some dbType, :? SqlKata.Compilers.PostgresCompiler ->
let property = param.GetType().GetProperty("NpgsqlDbType")
let dbTypeSetter = property.GetSetMethod()
let value = System.Enum.Parse(property.PropertyType, dbType)
dbTypeSetter.Invoke(param, [|value|]) |> ignore
| Some dbType, :? SqlKata.Compilers.SqlServerCompiler ->
let property = param.GetType().GetProperty("SqlDbType")
let dbTypeSetter = property.GetSetMethod()
let value = System.Enum.Parse(property.PropertyType, dbType)
dbTypeSetter.Invoke(param, [|value|]) |> ignore
| _ -> ()
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, the code now looks almost as yours, just with a helper function to hide the reflection ugliness 🙂 |
||
| interface System.IDisposable with | ||
| member this.Dispose() = | ||
| conn.Dispose() | ||
|
|
@@ -44,7 +58,13 @@ type QueryContext(conn: DbConnection, compiler: SqlKata.Compilers.Compiler) = | |
| for kvp in compiledQuery.NamedBindings do | ||
| let p = cmd.CreateParameter() | ||
| p.ParameterName <- kvp.Key | ||
| p.Value <- kvp.Value | ||
|
|
||
| match kvp.Value with | ||
| | :? QueryParameter as qp -> | ||
| do setParameterDbType p qp | ||
| p.Value <- qp.Value | ||
| | _ -> | ||
| p.Value <- kvp.Value | ||
| cmd.Parameters.Add(p) |> ignore | ||
| cmd | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I noticed that every time you run the generator the order of the fields changes, and then one of the tests for insert query failed because the order of the fields has changed (see the test here) so I thought I would add sorting by ordinal position (I assume it is the order columns were added to the table) to avoid random order after every re-generation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good idea, but I'm not sure that "ORDINAL_POSITION" is given for the other database providers.
If not, then I think a reasonable alternative would be to sort by column name.