public void CreateCacheKey_Types() { var safeTypes = new[] { typeof(int), typeof(string), typeof(long), typeof(double), typeof(char), typeof(bool), typeof(DateTime), typeof(Guid), }; var intParam = new SpParameter("int", 55); var stringParam = new SpParameter("string", "foo"); var longParam = new SpParameter("long", 55L); var doubleParam = new SpParameter("double", 3.14); var charParam = new SpParameter("char", '*'); var boolParam = new SpParameter("bool", true); var dateTimeParam = new SpParameter("DateTime", new DateTime(2000, 1, 1)); var guidParam = new SpParameter("Guid", new Guid("dddddddd-dddd-dddd-dddd-dddddddddddd")); var parameters = new[] { intParam, stringParam, longParam, doubleParam, charParam, boolParam, dateTimeParam, guidParam }; var key = parameters.CreateCacheKey("sp_foo"); Assert.AreEqual( "db.v1.sp_foo:" + "@bool+True&" + "@char+*&" + "@DateTime+630822816000000000&" + "@double+3.14&" + "@Guid+dddddddd-dddd-dddd-dddd-dddddddddddd&" + "@int+55&" + "@long+55&" + "@string+foo&", key); var ctors = typeof(SpParameter).GetConstructors(); foreach (var info in ctors) { Assert.IsTrue( safeTypes.Any(t => t == info.GetParameters()[1].ParameterType), "All ctor types for SpParameter need to add " + "instance to the list above and update the test."); } Assert.AreEqual(safeTypes.Length, ctors.Length); }
public static Dictionary <string, SpParameter> ParameterFromDTO(object ParameterObj) { Dictionary <string, SpParameter> Result = new Dictionary <string, SpParameter>(); Type PDTOType = ParameterObj.GetType(); var ParameterList = PDTOType.GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (var item in ParameterList) { string SqlParamName = ParameterValueName(item); SpParameter paramObj = new SpParameter(); paramObj.PValue = item.GetValue(ParameterObj, null); paramObj.PropertyName = item.Name; Result.Add(SqlParamName, paramObj); } return(Result); }
public GetAdditionalInfoCommand(AdditionalInfoClass additionalInfoClass) { base.StoredProcedureName = AdminStoredProcedures.SpGetAdditionalInformationType; SpParameter = (int)additionalInfoClass; ParameterName = "@AdditionalInformationClassId"; DescriptionColumnName = "Description"; IdColumnName = "AdditionalInformationTypeId"; base.CacheKey = CachedBaseCommand <List <KeyValuePair <int, string> > > .GetCacheKey(base.StoredProcedureName, SpParameter.ToString(), ParameterName, DescriptionColumnName, IdColumnName); }
public static string GenerateStoredProcedureCode(StoredProcedure pProc, string columnPrefix, bool pIncludeSchema) { string newLine = Environment.NewLine; string str = "\t"; StringBuilder stringBuilder = new StringBuilder(); stringBuilder.Append("using System;").Append(newLine); stringBuilder.Append("using System.Data;"); stringBuilder.Append("using System.Data.SqlClient;"); stringBuilder.Append("namespace Tables.").Append(pProc.Name).Append(" {").Append(newLine); stringBuilder.Append(newLine); stringBuilder.Append(str).Append("public sealed class Proc : ").Append(typeof(StoredProcBase).ToString()).Append(" {").Append(newLine); stringBuilder.Append(newLine); stringBuilder.Append(str).Append(str).Append("public static readonly Proc Instance = new Proc();").Append(newLine); stringBuilder.Append(newLine); stringBuilder.Append(str).Append(str).Append("public Proc() : base(DATABASE, \"").Append(!pIncludeSchema || string.IsNullOrEmpty(pProc.Schema) ? string.Empty : pProc.Schema + ".").Append(pProc.Name).Append("\", typeof(Row)) {").Append(newLine); stringBuilder.Append(newLine); stringBuilder.Append(str).Append(str).Append(str).Append("//AddColumns("); stringBuilder.Append(");").Append(newLine); stringBuilder.Append(str).Append(str).Append("}").Append(newLine); stringBuilder.Append(newLine); stringBuilder.Append(str).Append(str).Append("public Sql.IResult Execute("); int index1 = 0; while (index1 < pProc.Parameters.Count) { SpParameter spParameter = pProc.Parameters[index1]; if (index1 > 0) { stringBuilder.Append(", "); } if (spParameter.Direction == ParameterDirection.Output || spParameter.Direction == ParameterDirection.ReturnValue) { stringBuilder.Append("out "); } stringBuilder.Append(DbColumnFactory.GetReturnType(spParameter.ParamType, false)).Append(" ").Append(spParameter.Name); checked { ++index1; } } if (pProc.Parameters.Count > 0) { stringBuilder.Append(", "); } stringBuilder.Append("Sql.Transaction transaction){").Append(newLine).Append(newLine); int index2 = 0; while (index2 < pProc.Parameters.Count) { SpParameter spParameter = pProc.Parameters[index2]; stringBuilder.Append(str).Append(str).Append(str).Append("SqlParameter p").Append(index2.ToString()).Append(" = new SqlParameter(\"").Append(spParameter.Name).Append("\", SqlDbType.").Append(DbColumnFactory.GetSqlType(spParameter.ParamType).ToString()).Append(");").Append(newLine); stringBuilder.Append(str).Append(str).Append(str).Append("p").Append(index2.ToString()).Append(".Direction = ParameterDirection.").Append(spParameter.Direction.ToString()).Append(";").Append(newLine); if (spParameter.Direction == ParameterDirection.Input || spParameter.Direction == ParameterDirection.InputOutput) { stringBuilder.Append(str).Append(str).Append(str).Append("p").Append(index2.ToString()).Append(".Value = ").Append(spParameter.Name).Append(";").Append(newLine); } stringBuilder.Append(newLine); checked { ++index2; } } stringBuilder.Append(str).Append(str).Append(str).Append("Sql.IResult result = ExecuteProcedure(transaction"); int num = 0; while (num < pProc.Parameters.Count) { stringBuilder.Append(", p").Append(num.ToString()); checked { ++num; } } stringBuilder.Append(");").Append(newLine).Append(newLine); int index3 = 0; while (index3 < pProc.Parameters.Count) { SpParameter spParameter = pProc.Parameters[index3]; if (spParameter.Direction == ParameterDirection.InputOutput || spParameter.Direction == ParameterDirection.Output || spParameter.Direction == ParameterDirection.ReturnValue) { stringBuilder.Append(str).Append(str).Append(str).Append(spParameter.Name).Append(" = (").Append(DbColumnFactory.GetReturnType(spParameter.ParamType, false)).Append(")").Append("p").Append(index3.ToString()).Append(".Value;").Append(newLine); } checked { ++index3; } } stringBuilder.Append(str).Append(str).Append(str).Append("return result;").Append(newLine); stringBuilder.Append(str).Append(str).Append("}").Append(newLine).Append(newLine); stringBuilder.Append(str).Append(str).Append("public Row this[int pIndex, Sql.IResult pResult]{").Append(newLine); stringBuilder.Append(str).Append(str).Append(str).Append("get { return (Row)pResult.GetRow(this, pIndex); }").Append(newLine); stringBuilder.Append(str).Append(str).Append("}").Append(newLine); stringBuilder.Append(str).Append("}").Append(newLine); stringBuilder.Append(newLine); stringBuilder.Append(str).Append("public sealed class Row : ").Append(typeof(Record).ToString()).Append(" {").Append(newLine); stringBuilder.Append(newLine); stringBuilder.Append(str).Append(str).Append("private new Proc Tbl {").Append(newLine); stringBuilder.Append(str).Append(str).Append(str).Append("get { return (Proc)base.Tbl; }").Append(newLine); stringBuilder.Append(str).Append(str).Append("}").Append(newLine); stringBuilder.Append(newLine); stringBuilder.Append(str).Append(str).Append("public Row() : base(Proc.Instance) {").Append(newLine); stringBuilder.Append(str).Append(str).Append("}").Append(newLine); stringBuilder.Append(str).Append("}").Append(newLine); stringBuilder.Append("}"); return(stringBuilder.ToString()); }