public static string CreateTrigger_CommandText(MethodInfo method, string schema, IDictionary <string, string> map)
        {
            StringBuilder sb = new StringBuilder();

            SqlTriggerAttribute triggerAttr = method.GetCustomAttribute <SqlTriggerAttribute>();

            if (triggerAttr == null)
            {
                throw new InvalidOperationException(string.Format("Method '{0}' is not sql clr trigger.", method.Name));
            }
            string name = triggerAttr.Name ?? method.Name;

            sb.AppendFormat(@"
CREATE TRIGGER ");
            if (schema != null)
            {
                sb.AppendFormat(@"[{0}].[{1}]", schema, name);
            }
            else
            {
                sb.AppendFormat(@"[{0}]", name);
            }
            sb.AppendFormat(@"
ON {0}
{1}
", triggerAttr.Target, triggerAttr.Event);
            sb.AppendFormat(@"
  EXTERNAL NAME [{0}].[{1}].[{2}];
GO", method.DeclaringType.Assembly.GetName().Name, method.DeclaringType.FullName, method.Name);
            return(sb.ToString());
        }
Exemple #2
0
        /// <summary>
        /// Creates a SqlTriggerBinding using the information provided in "context"
        /// </summary>
        /// <param name="context">
        /// Contains the SqlTriggerAttribute used to build up a SqlTriggerBinding
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// Thrown if context is null
        /// </exception>
        /// <exception cref="InvalidOperationException">
        /// If the SqlTriggerAttribute is bound to an invalid Type. Currently only IEnumerable<SqlChangeTrackingEntry<T>>
        /// is supported, where T is a user-defined POCO representing a row of their table
        /// </exception>
        /// <returns>
        /// Null if "context" does not contain a SqlTriggerAttribute. Otherwise returns a SqlTriggerBinding{T} associated
        /// with the SqlTriggerAttribute in "context", where T is the user-defined POCO
        /// </returns>
        public Task <ITriggerBinding> TryCreateAsync(TriggerBindingProviderContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            ParameterInfo       parameter = context.Parameter;
            SqlTriggerAttribute attribute = parameter.GetCustomAttribute <SqlTriggerAttribute>(inherit: false);

            if (attribute == null)
            {
                return(Task.FromResult <ITriggerBinding>(null));
            }

            if (!IsValidType(parameter.ParameterType))
            {
                throw new InvalidOperationException($"Can't bind SqlTriggerAttribute to type {parameter.ParameterType}. Only IEnumerable<SqlChangeTrackingEntry<T>>" +
                                                    $" is supported, where T is a user-defined POCO that matches the schema of the tracked table");
            }

            Type            type = parameter.ParameterType.GetGenericArguments()[0].GetGenericArguments()[0];
            Type            typeOfTriggerBinding = typeof(SqlTriggerBinding <>).MakeGenericType(type);
            ConstructorInfo constructor          = typeOfTriggerBinding.GetConstructor(new Type[] { typeof(string), typeof(string), typeof(ParameterInfo), typeof(ILogger) });

            return(Task.FromResult <ITriggerBinding>((ITriggerBinding)constructor.Invoke(new object[] { attribute.TableName,
                                                                                                        SqlBindingUtilities.GetConnectionString(attribute.ConnectionStringSetting, _configuration), parameter, _logger })));
        }
Exemple #3
0
        public DMSqlTrigger(SqlTriggerAttribute sql)
        {
            string stringName;

            _tableName            = Utility.GetSysName(sql.Target, out stringName);
            _triggerTypeAndAction = sql.Event;
            if (sql.Name != string.Empty)
            {
                _name = sql.Name;
            }
        }
        public static string DropTrigger_CommandText(MethodInfo method, string schema)
        {
            StringBuilder sb = new StringBuilder();

            SqlTriggerAttribute triggerAttr = method.GetCustomAttribute <SqlTriggerAttribute>();

            if (triggerAttr == null)
            {
                throw new InvalidOperationException(string.Format("Method '{0}' is not sql clr trigger.", method.Name));
            }
            string name = triggerAttr.Name ?? method.Name;

            sb.AppendFormat(@"
IF EXISTS (
  SELECT *
    FROM sys.objects
    WHERE [name] = N'{0}' AND [type] = 'TA'", 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 TRIGGER ");
            if (schema != null)
            {
                sb.AppendFormat(@"[{0}].[{1}]", schema, name);
            }
            else
            {
                sb.AppendFormat(@"[{0}]", name);
            }
            sb.Append(@";
GO");
            return(sb.ToString());
        }
Exemple #5
0
        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);
        }