Last active
October 31, 2016 12:17
-
-
Save yuka1984/92961d2239de03b1d055 to your computer and use it in GitHub Desktop.
PetaPoco使いやすいよ。 ref: http://qiita.com/yu_ka1984/items/b59aafe078b53f278619
This file contains 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
var sql = PetaPoco.Sql.Builder | |
.Select("HG.*") | |
.From("HOGE HG"); | |
.Where("HG.id = @0", 5) | |
.Where("HG.name = @Name" new { Name = "hogehoge" } ); | |
This file contains 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
public class ParamDictionary : Dictionary<string, object> { | |
public ParamDictionary() | |
{ | |
} | |
public ParamDictionary(IDictionary<string, object> dictionary) : base(dictionary) | |
{ | |
} | |
} | |
This file contains 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
foreach (var o in args_src) | |
{ | |
// Custom Start | |
if(o.GetType() == typeof(ParamDictionary)) | |
{ | |
var dic = (ParamDictionary)o; | |
if(dic.ContainsKey(param)) | |
{ | |
arg_val = dic[param]; | |
found = true; | |
break; | |
} | |
} | |
// Custom End | |
var pi = o.GetType().GetProperty(param); | |
if (pi != null) | |
{ | |
arg_val = pi.GetValue(o, null); | |
found = true; | |
break; | |
} | |
} |
This file contains 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
var hoges = new List<string> { "hoge1", "hage2", "hige3" }; | |
var dichoges = (ParamDictionary)hoges.Select((x, i) => new { Key = "Names" + i, Value = (object)x }).ToDictionary(x => x.Key, x => x.Value); | |
sql.Where("Name IN (" + String.Join(" ,", dichoges.Select(x => "@" + x.Key)) + " )", dichoges); |
This file contains 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
public T ExcuteProcedure<T>(string ProcedureName, ParamDictionary in_args, ParamDictionary out_args) | |
{ | |
try | |
{ | |
OpenSharedConnection(); | |
try | |
{ | |
using(var cmd = Connection.CreateCommand()) | |
{ | |
cmd.Connection = Connection; | |
cmd.CommandText = ProcedureName; | |
cmd.Transaction = _transaction; | |
cmd.CommandType = CommandType.StoredProcedure; | |
if (in_args != null) | |
{ | |
foreach (var item in in_args) | |
{ | |
var idbParam = item.Value as IDbDataParameter; | |
if (idbParam != null) | |
{ | |
idbParam.ParameterName = string.Format("{0}{1}", _paramPrefix, item.Key); | |
cmd.Parameters.Add(idbParam); | |
continue; | |
} | |
var p = GetProcParam(string.Format("{0}{1}", _paramPrefix, item.Key), item.Value, ParameterDirection.Input); | |
cmd.Parameters.Add(p); | |
} | |
} | |
if(out_args != null) | |
{ | |
foreach (var item in out_args) | |
{ | |
var idbParam = item.Value as IDbDataParameter; | |
if (idbParam != null) | |
{ | |
idbParam.ParameterName = string.Format("{0}{1}", _paramPrefix, item.Key); | |
cmd.Parameters.Add(idbParam); | |
continue; | |
} | |
var p = GetProcParam(string.Format("{0}{1}", _paramPrefix, item.Key), item.Value, ParameterDirection.Output); | |
cmd.Parameters.Add(p); | |
} | |
} | |
var returnvalue = cmd.CreateParameter(); | |
returnvalue.ParameterName = "returnVal"; | |
returnvalue.Direction = ParameterDirection.ReturnValue; | |
cmd.Parameters.Add(returnvalue); | |
_dbType.PreExecute(cmd); | |
DoPreExecute(cmd); | |
var retv = cmd.ExecuteNonQuery(); | |
OnExecutedCommand(cmd); | |
foreach(IDataParameter param in cmd.Parameters) | |
{ | |
if(param.Direction == ParameterDirection.Output) | |
{ | |
var beforekey = param.ParameterName.Replace(_paramPrefix, ""); | |
if(out_args.ContainsKey(beforekey)) | |
{ | |
out_args[beforekey] = param.Value; | |
} | |
} | |
} | |
object val = returnvalue.Value; | |
Type u = Nullable.GetUnderlyingType(typeof(T)); | |
if (u != null && val == null) | |
return default(T); | |
return (T)Convert.ChangeType(val, u == null ? typeof(T) : u); | |
} | |
} | |
finally | |
{ | |
CloseSharedConnection(); | |
} | |
} | |
catch (Exception x) | |
{ | |
if (OnException(x)) | |
throw; | |
return default(T); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment