|
| 1 | +#if NET45 |
| 2 | +#define ASYNC |
| 3 | +#endif |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Data; |
| 7 | +using System.Reflection; |
| 8 | +using System.Reflection.Emit; |
| 9 | +using System.Threading; |
| 10 | + |
| 11 | +//Apache 2.0 License: https://github.com/StackExchange/dapper-dot-net/blob/master/License.txt |
| 12 | +namespace ServiceStack.OrmLite.Dapper |
| 13 | +{ |
| 14 | + /// <summary> |
| 15 | + /// Represents the key aspects of a sql operation |
| 16 | + /// </summary> |
| 17 | + public struct CommandDefinition |
| 18 | + { |
| 19 | + internal static CommandDefinition ForCallback(object parameters) |
| 20 | + { |
| 21 | + if (parameters is DynamicParameters) |
| 22 | + { |
| 23 | + return new CommandDefinition(parameters); |
| 24 | + } |
| 25 | + else |
| 26 | + { |
| 27 | + return default(CommandDefinition); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + internal void OnCompleted() |
| 32 | + { |
| 33 | + (Parameters as SqlMapper.IParameterCallbacks)?.OnCompleted(); |
| 34 | + } |
| 35 | + |
| 36 | + /// <summary> |
| 37 | + /// The command (sql or a stored-procedure name) to execute |
| 38 | + /// </summary> |
| 39 | + public string CommandText { get; } |
| 40 | + |
| 41 | + /// <summary> |
| 42 | + /// The parameters associated with the command |
| 43 | + /// </summary> |
| 44 | + public object Parameters { get; } |
| 45 | + |
| 46 | + /// <summary> |
| 47 | + /// The active transaction for the command |
| 48 | + /// </summary> |
| 49 | + public IDbTransaction Transaction { get; } |
| 50 | + |
| 51 | + /// <summary> |
| 52 | + /// The effective timeout for the command |
| 53 | + /// </summary> |
| 54 | + public int? CommandTimeout { get; } |
| 55 | + |
| 56 | + /// <summary> |
| 57 | + /// The type of command that the command-text represents |
| 58 | + /// </summary> |
| 59 | + public CommandType? CommandType { get; } |
| 60 | + |
| 61 | + /// <summary> |
| 62 | + /// Should data be buffered before returning? |
| 63 | + /// </summary> |
| 64 | + public bool Buffered => (Flags & CommandFlags.Buffered) != 0; |
| 65 | + |
| 66 | + /// <summary> |
| 67 | + /// Should the plan for this query be cached? |
| 68 | + /// </summary> |
| 69 | + internal bool AddToCache => (Flags & CommandFlags.NoCache) == 0; |
| 70 | + |
| 71 | + /// <summary> |
| 72 | + /// Additional state flags against this command |
| 73 | + /// </summary> |
| 74 | + public CommandFlags Flags { get; } |
| 75 | + |
| 76 | + /// <summary> |
| 77 | + /// Can async queries be pipelined? |
| 78 | + /// </summary> |
| 79 | + public bool Pipelined => (Flags & CommandFlags.Pipelined) != 0; |
| 80 | + |
| 81 | + /// <summary> |
| 82 | + /// Initialize the command definition |
| 83 | + /// </summary> |
| 84 | + public CommandDefinition(string commandText, object parameters = null, IDbTransaction transaction = null, int? commandTimeout = null, |
| 85 | + CommandType? commandType = null, CommandFlags flags = CommandFlags.Buffered |
| 86 | +#if ASYNC |
| 87 | + , CancellationToken cancellationToken = default(CancellationToken) |
| 88 | +#endif |
| 89 | + ) |
| 90 | + { |
| 91 | + CommandText = commandText; |
| 92 | + Parameters = parameters; |
| 93 | + Transaction = transaction; |
| 94 | + CommandTimeout = commandTimeout; |
| 95 | + CommandType = commandType; |
| 96 | + Flags = flags; |
| 97 | +#if ASYNC |
| 98 | + CancellationToken = cancellationToken; |
| 99 | +#endif |
| 100 | + } |
| 101 | + |
| 102 | + private CommandDefinition(object parameters) : this() |
| 103 | + { |
| 104 | + Parameters = parameters; |
| 105 | + } |
| 106 | + |
| 107 | +#if ASYNC |
| 108 | + |
| 109 | + /// <summary> |
| 110 | + /// For asynchronous operations, the cancellation-token |
| 111 | + /// </summary> |
| 112 | + public CancellationToken CancellationToken { get; } |
| 113 | +#endif |
| 114 | + |
| 115 | + internal IDbCommand SetupCommand(IDbConnection cnn, Action<IDbCommand, object> paramReader) |
| 116 | + { |
| 117 | + var cmd = cnn.CreateCommand(); |
| 118 | + var init = GetInit(cmd.GetType()); |
| 119 | + init?.Invoke(cmd); |
| 120 | + if (Transaction != null) |
| 121 | + cmd.Transaction = Transaction; |
| 122 | + cmd.CommandText = CommandText; |
| 123 | + if (CommandTimeout.HasValue) |
| 124 | + { |
| 125 | + cmd.CommandTimeout = CommandTimeout.Value; |
| 126 | + } |
| 127 | + else if (SqlMapper.Settings.CommandTimeout.HasValue) |
| 128 | + { |
| 129 | + cmd.CommandTimeout = SqlMapper.Settings.CommandTimeout.Value; |
| 130 | + } |
| 131 | + if (CommandType.HasValue) |
| 132 | + cmd.CommandType = CommandType.Value; |
| 133 | + paramReader?.Invoke(cmd, Parameters); |
| 134 | + return cmd; |
| 135 | + } |
| 136 | + |
| 137 | + private static SqlMapper.Link<Type, Action<IDbCommand>> commandInitCache; |
| 138 | + |
| 139 | + private static Action<IDbCommand> GetInit(Type commandType) |
| 140 | + { |
| 141 | + if (commandType == null) |
| 142 | + return null; // GIGO |
| 143 | + Action<IDbCommand> action; |
| 144 | + if (SqlMapper.Link<Type, Action<IDbCommand>>.TryGet(commandInitCache, commandType, out action)) |
| 145 | + { |
| 146 | + return action; |
| 147 | + } |
| 148 | + var bindByName = GetBasicPropertySetter(commandType, "BindByName", typeof(bool)); |
| 149 | + var initialLongFetchSize = GetBasicPropertySetter(commandType, "InitialLONGFetchSize", typeof(int)); |
| 150 | + |
| 151 | + action = null; |
| 152 | + if (bindByName != null || initialLongFetchSize != null) |
| 153 | + { |
| 154 | + var method = new DynamicMethod(commandType.Name + "_init", null, new Type[] { typeof(IDbCommand) }); |
| 155 | + var il = method.GetILGenerator(); |
| 156 | + |
| 157 | + if (bindByName != null) |
| 158 | + { |
| 159 | + // .BindByName = true |
| 160 | + il.Emit(OpCodes.Ldarg_0); |
| 161 | + il.Emit(OpCodes.Castclass, commandType); |
| 162 | + il.Emit(OpCodes.Ldc_I4_1); |
| 163 | + il.EmitCall(OpCodes.Callvirt, bindByName, null); |
| 164 | + } |
| 165 | + if (initialLongFetchSize != null) |
| 166 | + { |
| 167 | + // .InitialLONGFetchSize = -1 |
| 168 | + il.Emit(OpCodes.Ldarg_0); |
| 169 | + il.Emit(OpCodes.Castclass, commandType); |
| 170 | + il.Emit(OpCodes.Ldc_I4_M1); |
| 171 | + il.EmitCall(OpCodes.Callvirt, initialLongFetchSize, null); |
| 172 | + } |
| 173 | + il.Emit(OpCodes.Ret); |
| 174 | + action = (Action<IDbCommand>)method.CreateDelegate(typeof(Action<IDbCommand>)); |
| 175 | + } |
| 176 | + // cache it |
| 177 | + SqlMapper.Link<Type, Action<IDbCommand>>.TryAdd(ref commandInitCache, commandType, ref action); |
| 178 | + return action; |
| 179 | + } |
| 180 | + |
| 181 | + private static MethodInfo GetBasicPropertySetter(Type declaringType, string name, Type expectedType) |
| 182 | + { |
| 183 | + var prop = declaringType.GetProperty(name, BindingFlags.Public | BindingFlags.Instance); |
| 184 | + if (prop != null && prop.CanWrite && prop.PropertyType == expectedType && prop.GetIndexParameters().Length == 0) |
| 185 | + { |
| 186 | + return prop.GetSetMethod(); |
| 187 | + } |
| 188 | + return null; |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | +} |
0 commit comments