public static string CreateAggregate_CommandText(Type type, string schema, IDictionary <string, string> map) { StringBuilder sb = new StringBuilder(); SqlUserDefinedAggregateAttribute aggregateAttr = type.GetCustomAttribute <SqlUserDefinedAggregateAttribute>(); if (aggregateAttr == null) { throw new ArgumentException(string.Format("Type '{0}' is not sql clr user defined aggregate.", type.FullName)); } string name = aggregateAttr.Name ?? type.Name; MethodInfo miAccumulate = type.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly) .SingleOrDefault(t => t.Name == "Accumulate" && t.ReturnType == typeof(void)); if (miAccumulate == null) { throw new InvalidOperationException("Accumulate method is not found"); } MethodInfo miTerminate = type.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly) .SingleOrDefault(t => t.Name == "Terminate" && t.GetParameters().Length == 0); if (miTerminate == null) { throw new InvalidOperationException("Terminate method is not found"); } sb.Append(@" CREATE AGGREGATE "); if (schema != null) { sb.AppendFormat(@"[{0}].[{1}]", schema, name); } else { sb.AppendFormat(@"[{0}]", name); } sb.AppendFormat(@"({0}) RETURNS {1} EXTERNAL NAME [{2}].[{3}]; GO", string.Join(", ", miAccumulate.GetParameters().Select(pi => GetSqlParameterArgument_Text(pi, map))), GetSqlType_Text(miTerminate.ReturnParameter.ParameterType, miTerminate.ReturnParameter, map), type.Assembly.GetName().Name, type.FullName); return(sb.ToString()); }
public static string DropAggregate_CommandText(Type type, string schema) { StringBuilder sb = new StringBuilder(); SqlUserDefinedAggregateAttribute aggregateAttr = type.GetCustomAttribute <SqlUserDefinedAggregateAttribute>(); if (aggregateAttr == null) { throw new InvalidOperationException(string.Format("Type '{0}' is not sql clr user defined aggregate.", type.FullName)); } string name = aggregateAttr.Name ?? type.Name; sb.AppendFormat(@" IF EXISTS ( SELECT * FROM sys.objects WHERE [name] = N'{0}' AND [type] = 'AF'", name); if (schema != null) { sb.AppendFormat(@" AND EXISTS ( SELECT * FROM sys.schemas WHERE sys.objects.schema_id = sys.schemas.schema_id AND sys.schemas.name = N'{0}')", schema); } sb.Append(@") DROP AGGREGATE "); if (schema != null) { sb.AppendFormat(@"[{0}].[{1}]", schema, name); } else { sb.AppendFormat(@"[{0}]", name); } sb.Append(@"; GO"); return(sb.ToString()); }
public void TestSqlUserDefinedAggregateAttributeMaxByteSize() { Func <int, SqlUserDefinedAggregateAttribute> create = (size) => new SqlUserDefinedAggregateAttribute(Format.UserDefined) { MaxByteSize = size }; SqlUserDefinedAggregateAttribute attribute1 = create(-1); SqlUserDefinedAggregateAttribute attribute2 = create(0); SqlUserDefinedAggregateAttribute attribute3 = create(SqlUserDefinedAggregateAttribute.MaxByteSizeValue); string udtError = SystemDataResourceManager.Instance.SQLUDT_MaxByteSizeValue; string errorMessage = (new ArgumentOutOfRangeException("MaxByteSize", 8001, udtError)).Message; DataTestUtility.AssertThrowsWrapper <ArgumentOutOfRangeException>( () => create(SqlUserDefinedAggregateAttribute.MaxByteSizeValue + 1), errorMessage); errorMessage = (new ArgumentOutOfRangeException("MaxByteSize", -2, udtError)).Message; DataTestUtility.AssertThrowsWrapper <ArgumentOutOfRangeException>( () => create(-2), errorMessage); }
public AggregatorWrapper(SqlUserDefinedAggregateAttribute functiondef, Type aggregatesyg, string schema) : base(aggregatesyg, schema) { _functiondef = functiondef; }
public ArrayList GetCreateString(string assemblyPath, string AssemblyName, bool IsInfer, ArrayList alMeths) { string parameters = ""; string retType; string[] retString = null; string stringName; ArrayList al = new ArrayList(); //Type attr = typeof(IDeployMethods); Assembly a = Assembly.LoadFile(assemblyPath); Type aggAttr = typeof(SqlUserDefinedAggregateAttribute); foreach (Type asmType in a.GetTypes()) { string aggName = ""; //check for aggregates if (asmType.IsDefined(aggAttr, false)) { object[] attrs = asmType.GetCustomAttributes(aggAttr, false); SqlUserDefinedAggregateAttribute agg = (SqlUserDefinedAggregateAttribute)attrs[0]; //string typeName = udt.Name; if (agg.Name != string.Empty && agg.Name != null) { aggName = agg.Name; } else { aggName = asmType.Name; } aggName = Utility.GetSysName(aggName, out stringName); if (alterAsm && CheckMethodExists(asmType.Name, aggName, alMeths)) { continue; } string dropString = "if exists(select * from sys.objects where name = '{0}')\nDROP AGGREGATE {1}"; dropString = string.Format(dropString, stringName, aggName); string createString = GetAggregate(asmType, aggName, AssemblyName); al.Add(dropString); al.Add(createString); } foreach (MethodInfo m in asmType.GetMethods(BindingFlags.Static | BindingFlags.Public)) { string realName = m.Name; string aliasName = m.Name; string sysName = ""; bool addString = false; //get the parameters parameters = ReflectParameters(m, out retType); //get custom attributes for the method object[] attr = m.GetCustomAttributes(false); if (attr.Length > 0) { for (int i = 0; i < attr.Length; i++) { addString = false; Type t = attr[i].GetType(); if (t == typeof(SqlProcedureAttribute)) { SqlProcedureAttribute sql = (SqlProcedureAttribute)attr[i]; if (sql.Name != string.Empty && sql.Name != null) { sysName = GetSysName(sql.Name, out aliasName); } DMSqlProcedure id = new DMSqlProcedure(sql); retString = id.GetCreateString(false, m, AssemblyName, parameters); addString = true; } if (t == typeof(SqlFunctionAttribute)) { SqlFunctionAttribute sql = (SqlFunctionAttribute)attr[i]; if (sql.Name != string.Empty && sql.Name != null) { sysName = GetSysName(sql.Name, out aliasName); } DMSqlFunction id = new DMSqlFunction(sql); retString = id.GetCreateString(false, m, AssemblyName, parameters, retType); addString = true; } if (t == typeof(SqlTriggerAttribute)) { SqlTriggerAttribute sql = (SqlTriggerAttribute)attr[i]; if (sql.Name != string.Empty && sql.Name != null) { sysName = GetSysName(sql.Name, out aliasName); } DMSqlTrigger id = new DMSqlTrigger(sql); retString = id.GetCreateString(false, m, AssemblyName); addString = true; } if (alterAsm && CheckMethodExists(realName, aliasName, alMeths)) { break; } if (addString) { foreach (String s in retString) { al.Add(s); } } } } else if (IsInfer) { //Debugger.Launch(); if (retType == "Void") { DMSqlProcedure id = new DMSqlProcedure(); retString = id.GetCreateString(true, m, AssemblyName, parameters); foreach (String s in retString) { al.Add(s); } } if (retType != "Void") { DMSqlFunction id = new DMSqlFunction(); retString = id.GetCreateString(true, m, AssemblyName, parameters, retType); foreach (String s in retString) { al.Add(s); } } } } } return(al); }