1+ /// Provides helpers for building raw SQL commands and parameters.
2+ /// This stuff does * NOT* go through RZSQL parsing/typechecking/translation.
3+ /// It should be a last resort for when you absolutely can't accomplish what you're doing statically.
4+ module Rezoom.SQL.Raw
5+ open System
6+ open System.Data
7+ open Rezoom.SQL .Mapping
8+ open System.Collections .Generic
9+
10+ let sql text = CommandText text
11+
12+ let argOfType dbType o =
13+ InlineParameter ( dbType, o)
14+
15+ let private typeMap =
16+ [| typeof< byte>, DbType.Byte
17+ typeof< sbyte>, DbType.SByte
18+ typeof< int16>, DbType.Int16
19+ typeof< uint16>, DbType.UInt16
20+ typeof< int>, DbType.Int32
21+ typeof< uint32>, DbType.UInt32
22+ typeof< int64>, DbType.Int64
23+ typeof< uint64>, DbType.UInt64
24+ typeof< string>, DbType.String
25+ typeof< double>, DbType.Double
26+ typeof< single>, DbType.Single
27+ typeof< bool>, DbType.Boolean
28+ typeof< Guid>, DbType.Guid
29+ typeof< decimal>, DbType.Decimal
30+ typeof< DateTime>, DbType.DateTime
31+ typeof< DateTimeOffset>, DbType.DateTimeOffset
32+ |] |> dict
33+
34+ let private guessDbType ( ty : Type ) =
35+ let succ , found = typeMap.TryGetValue( ty)
36+ if succ then found else DbType.Object
37+
38+ let arg ( o : obj ) =
39+ let dbType =
40+ if isNull o then DbType.Object
41+ else guessDbType ( o.GetType())
42+ argOfType dbType o
43+
44+ let connectionDynamicCommand < 'row > connectionName ( sql : CommandFragment array ) =
45+ let cmdData =
46+ { ConnectionName = connectionName // should match the one in rzsql.json/App.config
47+ Fragments = sql
48+ Identity = " "
49+ DependencyMask = Rezoom.BitMask.Full
50+ InvalidationMask = Rezoom.BitMask.Full
51+ Cacheable = false
52+ ResultSetCount = None // not statically known
53+ }
54+ CommandConstructor.Command1< 'row IReadOnlyList>( cmdData, [||])
55+
56+ let dynamicCommand < 'row > ( sql : CommandFragment array ) =
57+ connectionDynamicCommand< 'row> " rzsql" sql
0 commit comments