コード例 #1
0
        public override bool Execute()
        {
            SqlConnection conn2 = null;
            //Debugger.Launch();

            ArrayList al = null;

            try {
                //check that we have the necessary info
                Validate();

                //create the file
                if (toScript)
                {
                    string deploymentString = "--Deployment script for methods in assembly: " + asmName;
                    string testString       = "--Testing statements script for methods in assembly: " + asmName;

                    if (alterAsm)
                    {
                        deploymentString = "--Deployment script for new methods in altered assembly: " + asmName;
                        testString       = "--Testing statements script for new methods in altered assembly: " + asmName;
                    }
                    sw    = Utility.OpenFile(scriptFile, !alterAsm);
                    swRun = Utility.OpenFile(runFile, !alterAsm);
                    Utility.WriteToFile(sw, deploymentString, false, false);
                    Utility.WriteToFile(sw, "--Autogenerated at: " + DateTime.Now, false, true);
                    Utility.WriteToFile(swRun, testString, false, false);
                    Utility.WriteToFile(swRun, "--Autogenerated at: " + DateTime.Now, false, true);
                }

                if (toConnect)
                {
                    //open the connection
                    conn = new SqlConnection(connString);
                    conn.Open();
                    //start the tx
                    tx = conn.BeginTransaction();
                    if (!alterAsm)
                    {
                        //drop any relevant tables/columns, procs, views etc.
                        DropAssembly d = new DropAssembly(sw, conn, tx, asmName, toDropTable, toScript, this);
                        d.DropDependents();
                    }
                    else
                    {
                        conn2 = new SqlConnection(connString);
                        conn2.Open();
                        SqlCommand cmd = conn2.CreateCommand();
                        cmd.CommandText = @"Select isnull(sam.assembly_method, sam.assembly_class) clrname,
                                so.name alias 
                                from sys.objects so with (nolock) 
                                join sys.assembly_modules sam with (nolock) 
                                on so.object_id = sam.object_id 
                                join sys.assemblies sa with (nolock) 
                                on sam.assembly_id = sa.assembly_id 
                                where sa.name = '" + asmName + "'";

                        SqlDataReader dr = cmd.ExecuteReader();
                        //if we have data load it into the arraylist
                        if (dr != null && dr.HasRows)
                        {
                            alMeths = new ArrayList();
                            while (dr.Read())
                            {
                                alMeths.Add(dr.GetString(0) + ", " + dr.GetString(1));
                            }

                            dr.Close();
                            conn2.Close();
                        }
                    }
                }

                //instantiate the Utility class
                Utility u = new Utility(TypeConversionFilePath, alterAsm);
                if (alterAsm && alMeths != null)
                {
                    al = u.GetCreateString(asmPath, asmName, inferMeth, alMeths);
                }
                else
                {
                    al = u.GetCreateString(asmPath, asmName, inferMeth);
                }

                if (al.Count > 0 && toScript)
                {
                    Utility.WriteToFile(sw, "--About to drop and recreate CLR Methods and Aggregates", false, false);
                }

                for (int i = 0; i < al.Count; i++)
                {
                    string cmd = (string)al[i];

                    Utility.LogMyComment(this, "About to execute:\n" + cmd + "\n");

                    if (toScript)
                    {
                        Utility.WriteToFile(sw, cmd, true, true);
                        if (cmd.Contains("CREATE"))
                        {
                            Utility.WriteToFile(swRun, Utility.GetExecString(cmd), false, false);
                        }
                    }

                    if (toConnect)
                    {
                        Utility.WriteToDb(cmd, conn, tx);
                    }

                    Utility.LogMyComment(this, "Executed with success\n");
                }

                if (toConnect)
                {
                    Utility.LogMyComment(this, "Comitting Transaction");
                    tx.Commit();
                }

                Utility.LogMyComment(this, "Deployment Succeeded!\n");
                return(true);
            }


            catch (Exception e) {
                Utility.LogMyComment(this, "Error(s) Occured");
                Utility.LogMyComment(this, "Creating Method(s)/Aggregates Failed");
                Utility.LogMyErrorFromException(this, e);
                if (toConnect)
                {
                    if (tx != null)
                    {
                        Utility.LogMyComment(this, "Rolling Back Transaction\n");
                        if (tx.Connection != null)
                        {
                            tx.Rollback();
                        }
                    }
                }


                return(false);
            }

            finally {
                if (toConnect)
                {
                    if (conn != null && conn.State != ConnectionState.Closed)
                    {
                        conn.Close();
                    }
                    if (conn2 != null && conn2.State != ConnectionState.Closed)
                    {
                        conn2.Close();
                    }
                }
                if (toScript && sw != null)
                {
                    sw.Flush();
                    sw.Close();
                }

                if (toScript && swRun != null)
                {
                    swRun.Flush();
                    swRun.Close();
                }
            }
        }
コード例 #2
0
        public override bool Execute()
        {
            SqlConnection conn2 = null;
            //Debugger.Launch();
            bool   typeExist  = false;
            string stringName = "";
            string sysName    = "";

            try {
                //check that we have the necessary info
                Validate();
                if (toConnect)
                {
                    //open the connection
                    conn = new SqlConnection(connString);
                    conn.Open();

                    //start the tx
                    tx = conn.BeginTransaction();
                }

                //create the file
                if (toScript)
                {
                    string deploymentString = "--Deployment script for UDT's in assembly: " + asmName;
                    string testString       = "--Testing statements script for UDT's in assembly: " + asmName;

                    if (alterAsm)
                    {
                        deploymentString = "--Deployment script for new UDT's in altered assembly: " + asmName;
                        testString       = "--Testing statements script for new UDT's in altered assembly: " + asmName;
                    }
                    sw    = Utility.OpenFile(scriptFile, !alterAsm);
                    swRun = Utility.OpenFile(runFile, !alterAsm);

                    Utility.WriteToFile(sw, deploymentString, false, false);
                    Utility.WriteToFile(sw, "--Autogenerated at: " + DateTime.Now, false, true);
                    Utility.WriteToFile(swRun, testString, false, false);
                    Utility.WriteToFile(swRun, "--Autogenerated at: " + DateTime.Now, false, true);
                }

                if (!alterAsm)
                {
                    //drop types
                    DropAssembly d = new DropAssembly(sw, conn, tx, asmName, toDropTable, castType, toScript, this, Assembly.LoadFile(asmPath));
                    //drop types and dependent stuff
                    d.DropTypes(toConnect);
                }
                else if (alterAsm)
                {
                    conn2 = new SqlConnection(connString);
                    conn2.Open();
                    SqlCommand cmd = conn2.CreateCommand();
                    cmd.CommandText = @"select t.assembly_class realname, 
                              t.name alias
                              from sys.assembly_types t with (nolock)
                              join sys.assemblies a with (nolock)
                              on t.assembly_id = a.assembly_id
                              where a.name = '" + asmName + "'";
                    SqlDataReader dr = cmd.ExecuteReader();
                    if (dr != null && dr.HasRows)
                    {
                        alTypes = new ArrayList();
                        while (dr.Read())
                        {
                            alTypes.Add(dr.GetString(0) + ", " + dr.GetString(1));
                        }

                        dr.Close();
                        conn2.Close();
                    }
                }

                Type attr = typeof(SqlUserDefinedTypeAttribute);

                //get the assembly
                Assembly asm = Assembly.LoadFile(asmPath);

                if (toScript)
                {
                    Utility.WriteToFile(sw, "--About to create UDT's", false, false);
                }

                foreach (Type t in asm.GetTypes())
                {
                    string typeName = t.Name;
                    //check for the UDT attribute
                    if (t.IsDefined(attr, false))
                    {
                        object[] attrs = t.GetCustomAttributes(attr, false);
                        SqlUserDefinedTypeAttribute udt = (SqlUserDefinedTypeAttribute)attrs[0];
                        //string typeName = udt.Name;
                        if (udt.Name != string.Empty && udt.Name != null)
                        {
                            typeName = udt.Name;
                        }

                        sysName = Utility.GetSysName(typeName, out stringName);

                        string fullName = t.FullName;
                        if (alterAsm && CheckTypeExists(t.Name, stringName, alTypes))
                        {
                            continue;
                        }

                        typeExist = true;
                        string createText = "CREATE TYPE " + sysName + "\nEXTERNAL NAME [" + asmName + "].[" + fullName + "]";

                        if (toScript)
                        {
                            Utility.WriteToFile(sw, createText, true, true);
                            if (createText.Contains("CREATE"))
                            {
                                Utility.WriteToFile(swRun, Utility.GetExecString(createText), false, false);
                            }
                        }


                        if (toConnect)
                        {
                            Utility.LogMyComment(this, "About to execute:\n" + createText + "\n");
                            Utility.WriteToDb(createText, conn, tx);
                            Utility.LogMyComment(this, "Executed with success\n");
                        }
                    }
                }
                if (typeExist)
                {
                    if (toConnect)
                    {
                        Utility.LogMyComment(this, "Comitting Transaction");
                        tx.Commit();
                    }
                    Utility.LogMyComment(this, "Deployment Succeeded!\n");
                    return(true);
                }
            }

            catch (Exception e) {
                Utility.LogMyComment(this, "Error(s) Occured");
                Utility.LogMyComment(this, "Creating Type(s) Failed");
                Utility.LogMyErrorFromException(this, e);
                if (toConnect)
                {
                    if (tx != null)
                    {
                        Utility.LogMyComment(this, "Rolling Back Transaction\n");
                        if (tx.Connection != null)
                        {
                            tx.Rollback();
                        }
                    }
                }


                return(false);
            }
            finally {
                if (toConnect)
                {
                    if (tx != null)
                    {
                        if (tx.Connection != null)
                        {
                            tx.Rollback();
                        }
                    }
                    if (conn != null && conn.State != ConnectionState.Closed)
                    {
                        conn.Close();
                    }
                }

                if (toScript && sw != null)
                {
                    sw.Flush();
                    sw.Close();
                }

                if (toScript && swRun != null)
                {
                    swRun.Flush();
                    swRun.Close();
                }
            }
            return(true);
        }