Created
October 23, 2015 11:13
-
-
Save felipensp/b6f34673807c19f78f37 to your computer and use it in GitHub Desktop.
Oracle bulk insert for Dapper .NET
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static int OracleBulkExecuteImpl<T>(this IDbConnection cnn, ref CommandDefinition command) | |
{ | |
object param = command.Parameters; | |
IEnumerable multiExec = GetMultiExec(param); | |
Identity identity; | |
CacheInfo info = null; | |
if (multiExec != null) | |
{ | |
int total = 0; | |
bool wasClosed = cnn.State == ConnectionState.Closed; | |
try | |
{ | |
if (wasClosed) cnn.Open(); | |
using (var cmd = command.SetupCommand(cnn, null) as OracleCommand) | |
{ | |
var dataList = multiExec.Cast<T>(); | |
cmd.ArrayBindCount = dataList.Count(); | |
cmd.Parameters.Clear(); | |
foreach (var prop in typeof(T).GetProperties()) | |
{ | |
var data = dataList.Select(x => prop.GetValue(x)); | |
var propType = Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType; | |
if (propType == typeof(int)) | |
{ | |
cmd.Parameters.Add(":" + prop.Name, data.Cast<int>().ToArray()); | |
} | |
else if (propType == typeof(DateTime)) | |
{ | |
cmd.Parameters.Add(":" + prop.Name, data.Cast<DateTime>().ToArray()); | |
} | |
else if (propType == typeof(string)) | |
{ | |
cmd.Parameters.Add(":" + prop.Name, data.Cast<string>().ToArray()); | |
} | |
else if (propType == typeof(double)) | |
{ | |
cmd.Parameters.Add(":" + prop.Name, data.Cast<double>().ToArray()); | |
} | |
else | |
{ | |
throw new ArgumentNullException("Unknown data type"); | |
} | |
} | |
total = cmd.ExecuteNonQuery(); | |
} | |
command.OnCompleted(); | |
} | |
finally | |
{ | |
if (wasClosed) cnn.Close(); | |
} | |
return total; | |
} | |
// nice and simple | |
if (param != null) | |
{ | |
identity = new Identity(command.CommandText, command.CommandType, cnn, null, param.GetType(), null); | |
info = GetCacheInfo(identity, param, command.AddToCache); | |
} | |
return ExecuteCommand(cnn, ref command, param == null ? null : info.ParamReader); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment