From c789cfd3d4acb1b280f93582334f60579e42e09a Mon Sep 17 00:00:00 2001 From: "Valeriano A.R" Date: Fri, 22 Jan 2021 15:12:37 +0100 Subject: [PATCH] FrmDatabase: Verify button, for procedures, functions and views --- .../Code/BusinessLogic/ProcedureBL.cs | 21 +++++++- .../Code/BusinessLogic/TableBL.cs | 20 ++++++++ .../Code/DataAccess/DatabaseDA.cs | 49 +++++++++++++++++++ .../Code/DataAccess/ProcedureDA.cs | 40 +++++++++------ .../Code/DataAccess/TableDA.cs | 30 ++++++------ .../UI/FrmDatabase.Designer.cs | 39 ++++++++++----- VAR.DatabaseExplorer/UI/FrmDatabase.cs | 24 +++++++++ 7 files changed, 181 insertions(+), 42 deletions(-) diff --git a/VAR.DatabaseExplorer/Code/BusinessLogic/ProcedureBL.cs b/VAR.DatabaseExplorer/Code/BusinessLogic/ProcedureBL.cs index 51b870b..5b738cb 100644 --- a/VAR.DatabaseExplorer/Code/BusinessLogic/ProcedureBL.cs +++ b/VAR.DatabaseExplorer/Code/BusinessLogic/ProcedureBL.cs @@ -89,7 +89,7 @@ namespace VAR.DatabaseExplorer.Code.BusinessLogic } } - internal static void Procedure_GenerateCreate(Procedure routine, StreamWriter txtWriter) + public static void Procedure_GenerateCreate(Procedure routine, StreamWriter txtWriter) { string routineName = string.Format("{0}.{1}", routine.Schema, routine.Name); if (routine.Type.ToUpper() == "PROCEDURE") @@ -114,5 +114,24 @@ namespace VAR.DatabaseExplorer.Code.BusinessLogic txtWriter.WriteLine("GO"); txtWriter.WriteLine(); } + + public static List Procedure_VerifyAndGetInvalidProcedures(string connectionString) + { + List procedures = ProcedureDA.GetAllProcedures(connectionString); + List invalidProcedures = new List(); + foreach (Procedure procedure in procedures) + { + if (DatabaseDA.VerifyModule(connectionString, procedure.Schema, procedure.Name) == false) + { + invalidProcedures.Add(procedure); + } + } + return invalidProcedures; + } + + public static void Procedure_DeleteProcedures(string connectionString, List procedures) + { + ProcedureDA.Procedure_DeleteProcedures(connectionString, procedures); + } } } \ No newline at end of file diff --git a/VAR.DatabaseExplorer/Code/BusinessLogic/TableBL.cs b/VAR.DatabaseExplorer/Code/BusinessLogic/TableBL.cs index bebae1a..535d143 100644 --- a/VAR.DatabaseExplorer/Code/BusinessLogic/TableBL.cs +++ b/VAR.DatabaseExplorer/Code/BusinessLogic/TableBL.cs @@ -181,5 +181,25 @@ namespace VAR.DatabaseExplorer.Code.BusinessLogic txtWriter.WriteLine("GO"); txtWriter.WriteLine(); } + + public static List View_VerifyAndGetInvalidViews(string connectionString) + { + List
tables = TableDA.GetAllTables(connectionString); + List
invalidViews = new List
(); + foreach (Table table in tables) + { + if (table.Type != "VIEW") { continue; } + if (DatabaseDA.VerifyModule(connectionString, table.Schema, table.Name) == false) + { + invalidViews.Add(table); + } + } + return invalidViews; + } + + public static void View_DeleteViews(string connectionString, List
views) + { + TableDA.View_DeleteViews(connectionString, views); + } } } \ No newline at end of file diff --git a/VAR.DatabaseExplorer/Code/DataAccess/DatabaseDA.cs b/VAR.DatabaseExplorer/Code/DataAccess/DatabaseDA.cs index d107640..3bbef03 100644 --- a/VAR.DatabaseExplorer/Code/DataAccess/DatabaseDA.cs +++ b/VAR.DatabaseExplorer/Code/DataAccess/DatabaseDA.cs @@ -73,5 +73,54 @@ namespace VAR.DatabaseExplorer.Code.DataAccess List schemas = dt.AsEnumerable().Select(dr => Convert.ToString(dr["schema_name"])).ToList(); return schemas; } + + public static bool SupportedSqlServerVersion(string serverVersion) + { + return + serverVersion.StartsWith("10.") || + serverVersion.StartsWith("11.") || + serverVersion.StartsWith("12.") || + serverVersion.StartsWith("13.") || + serverVersion.StartsWith("14.") || + serverVersion.StartsWith("15.") || + serverVersion.StartsWith("16.") || + false; + } + + public static bool VerifyModule(string connectionString, string schema, string name) + { + SqlDataAdapter dataAdapter; + var cnx = new SqlConnection(connectionString); + cnx.Open(); + + string procedureName = string.Format("[{0}].[{1}]", schema, name); + + if (SupportedSqlServerVersion(cnx.ServerVersion)) + { + dataAdapter = new SqlDataAdapter(@" + BEGIN TRY + EXECUTE sys.sp_refreshsqlmodule @Name; + SELECT 1 'OK'; + END TRY + BEGIN CATCH + SELECT 0 'OK'; + END CATCH + ", cnx); + dataAdapter.SelectCommand.Parameters.AddWithValue("@Name", (object)procedureName ?? DBNull.Value); + } + else + { + cnx.Close(); + return true; + } + + var dt = new DataTable(); + dataAdapter.Fill(dt); + cnx.Close(); + + int isOK = Convert.ToInt32(dt.Rows[0][0]); + + return isOK != 0; + } } } \ No newline at end of file diff --git a/VAR.DatabaseExplorer/Code/DataAccess/ProcedureDA.cs b/VAR.DatabaseExplorer/Code/DataAccess/ProcedureDA.cs index 2f2e7f3..7fa8f26 100644 --- a/VAR.DatabaseExplorer/Code/DataAccess/ProcedureDA.cs +++ b/VAR.DatabaseExplorer/Code/DataAccess/ProcedureDA.cs @@ -35,26 +35,13 @@ namespace VAR.DatabaseExplorer.Code.DataAccess return procedures; } - private static bool SupportedSqlServerVersion(string serverVersion) - { - return - serverVersion.StartsWith("10.") || - serverVersion.StartsWith("11.") || - serverVersion.StartsWith("12.") || - serverVersion.StartsWith("13.") || - serverVersion.StartsWith("14.") || - serverVersion.StartsWith("15.") || - serverVersion.StartsWith("16.") || - false; - } - public static string GetProcedureDefinition(string connectionString, string schema, string name) { SqlDataAdapter dataAdapter; var cnx = new SqlConnection(connectionString); cnx.Open(); - if (SupportedSqlServerVersion(cnx.ServerVersion)) + if (DatabaseDA.SupportedSqlServerVersion(cnx.ServerVersion)) { dataAdapter = new SqlDataAdapter(@" SELECT @@ -101,7 +88,7 @@ namespace VAR.DatabaseExplorer.Code.DataAccess var cnx = new SqlConnection(connectionString); cnx.Open(); - if (SupportedSqlServerVersion(cnx.ServerVersion)) + if (DatabaseDA.SupportedSqlServerVersion(cnx.ServerVersion)) { dataAdapter = new SqlDataAdapter(@" SELECT @@ -143,5 +130,28 @@ namespace VAR.DatabaseExplorer.Code.DataAccess return definitions; } + + public static void Procedure_DeleteProcedures(string connectionString, List procedures) + { + var cnx = new SqlConnection(connectionString); + cnx.Open(); + + foreach (Procedure procedure in procedures) + { + string strCmd = null; + if (procedure.Type == "FUNCTION") + { + strCmd = string.Format("DROP FUNCTION [{0}].[{1}]", procedure.Schema, procedure.Name); + } + else + { + strCmd = string.Format("DROP PROCEDURE [{0}].[{1}]", procedure.Schema, procedure.Name); + } + var cmd = new SqlCommand(strCmd, cnx); + cmd.ExecuteNonQuery(); + } + + cnx.Close(); + } } } \ No newline at end of file diff --git a/VAR.DatabaseExplorer/Code/DataAccess/TableDA.cs b/VAR.DatabaseExplorer/Code/DataAccess/TableDA.cs index 533965a..b5eacf4 100644 --- a/VAR.DatabaseExplorer/Code/DataAccess/TableDA.cs +++ b/VAR.DatabaseExplorer/Code/DataAccess/TableDA.cs @@ -34,26 +34,13 @@ namespace VAR.DatabaseExplorer.Code.DataAccess return tables; } - private static bool SupportedSqlServerVersion(string serverVersion) - { - return - serverVersion.StartsWith("10.") || - serverVersion.StartsWith("11.") || - serverVersion.StartsWith("12.") || - serverVersion.StartsWith("13.") || - serverVersion.StartsWith("14.") || - serverVersion.StartsWith("15.") || - serverVersion.StartsWith("16.") || - false; - } - public static List GetViewDefinitions(string connectionString, string schema = null, string name = null) { SqlDataAdapter dataAdapter; var cnx = new SqlConnection(connectionString); cnx.Open(); - if (SupportedSqlServerVersion(cnx.ServerVersion)) + if (DatabaseDA.SupportedSqlServerVersion(cnx.ServerVersion)) { dataAdapter = new SqlDataAdapter(@" SELECT @@ -108,5 +95,20 @@ namespace VAR.DatabaseExplorer.Code.DataAccess } cnx.Close(); } + + public static void View_DeleteViews(string connectionString, List
views) + { + var cnx = new SqlConnection(connectionString); + cnx.Open(); + + foreach (Table view in views) + { + string strCmd = string.Format("DROP VIEW [{0}].[{1}]", view.Schema, view.Name); + var cmd = new SqlCommand(strCmd, cnx); + cmd.ExecuteNonQuery(); + } + + cnx.Close(); + } } } \ No newline at end of file diff --git a/VAR.DatabaseExplorer/UI/FrmDatabase.Designer.cs b/VAR.DatabaseExplorer/UI/FrmDatabase.Designer.cs index b7a8488..57a39e8 100644 --- a/VAR.DatabaseExplorer/UI/FrmDatabase.Designer.cs +++ b/VAR.DatabaseExplorer/UI/FrmDatabase.Designer.cs @@ -43,6 +43,7 @@ this.colEsquema = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); this.colNombreTabla = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); this.colTipo = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); + this.btnExportSchema = new System.Windows.Forms.Button(); this.btnExportData = new System.Windows.Forms.Button(); this.lsvColumnas = new VAR.DatabaseExplorer.Controls.CustomListView(); this.colNombreColumna = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); @@ -50,7 +51,7 @@ this.colClave = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); this.colNullable = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); this.txtConString = new VAR.DatabaseExplorer.Controls.CustomTextBox(); - this.btnExportSchema = new System.Windows.Forms.Button(); + this.btnVerify = new System.Windows.Forms.Button(); ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit(); this.splitContainer1.Panel1.SuspendLayout(); this.splitContainer1.Panel2.SuspendLayout(); @@ -175,6 +176,7 @@ // // splitContainer1.Panel2 // + this.splitContainer1.Panel2.Controls.Add(this.btnVerify); this.splitContainer1.Panel2.Controls.Add(this.btnExportSchema); this.splitContainer1.Panel2.Controls.Add(this.btnExportData); this.splitContainer1.Panel2.Controls.Add(this.lblTituloTabla); @@ -208,6 +210,7 @@ this.colNombreTabla, this.colTipo}); this.lsvTablas.FullRowSelect = true; + this.lsvTablas.HideSelection = false; this.lsvTablas.Location = new System.Drawing.Point(3, 40); this.lsvTablas.Name = "lsvTablas"; this.lsvTablas.Size = new System.Drawing.Size(326, 439); @@ -231,6 +234,17 @@ this.colTipo.Text = "Tipo"; this.colTipo.Width = 71; // + // btnExportSchema + // + this.btnExportSchema.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); + this.btnExportSchema.Location = new System.Drawing.Point(84, 485); + this.btnExportSchema.Name = "btnExportSchema"; + this.btnExportSchema.Size = new System.Drawing.Size(91, 23); + this.btnExportSchema.TabIndex = 16; + this.btnExportSchema.Text = "ExportSchema"; + this.btnExportSchema.UseVisualStyleBackColor = true; + this.btnExportSchema.Click += new System.EventHandler(this.BtnExportSchema_Click); + // // btnExportData // this.btnExportData.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); @@ -254,6 +268,7 @@ this.colClave, this.colNullable}); this.lsvColumnas.FullRowSelect = true; + this.lsvColumnas.HideSelection = false; this.lsvColumnas.Location = new System.Drawing.Point(3, 40); this.lsvColumnas.Name = "lsvColumnas"; this.lsvColumnas.Size = new System.Drawing.Size(464, 439); @@ -291,18 +306,17 @@ this.txtConString.TabIndex = 0; this.txtConString.TabWidth = 8; // - // btnExportSchema + // btnVerify // - this.btnExportSchema.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); - this.btnExportSchema.Location = new System.Drawing.Point(84, 485); - this.btnExportSchema.Name = "btnExportSchema"; - this.btnExportSchema.Size = new System.Drawing.Size(91, 23); - this.btnExportSchema.TabIndex = 16; - this.btnExportSchema.Text = "ExportSchema"; - this.btnExportSchema.UseVisualStyleBackColor = true; - this.btnExportSchema.Click += new System.EventHandler(this.BtnExportSchema_Click); + this.btnVerify.Location = new System.Drawing.Point(262, 485); + this.btnVerify.Name = "btnVerify"; + this.btnVerify.Size = new System.Drawing.Size(75, 23); + this.btnVerify.TabIndex = 17; + this.btnVerify.Text = "Verify"; + this.btnVerify.UseVisualStyleBackColor = true; + this.btnVerify.Click += new System.EventHandler(this.btnVerify_Click); // - // FrmBaseDatos + // FrmDatabase // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; @@ -313,7 +327,7 @@ this.Controls.Add(this.txtConString); this.Controls.Add(this.menuBaseDatos); this.MainMenuStrip = this.menuBaseDatos; - this.Name = "FrmBaseDatos"; + this.Name = "FrmDatabase"; this.Text = "Base de Datos"; this.Load += new System.EventHandler(this.frmBaseDatos_Load); this.splitContainer1.Panel1.ResumeLayout(false); @@ -350,5 +364,6 @@ private System.Windows.Forms.Button btnRefresh; private System.Windows.Forms.Button btnExportData; private System.Windows.Forms.Button btnExportSchema; + private System.Windows.Forms.Button btnVerify; } } \ No newline at end of file diff --git a/VAR.DatabaseExplorer/UI/FrmDatabase.cs b/VAR.DatabaseExplorer/UI/FrmDatabase.cs index 3ab86c5..93a1e9d 100644 --- a/VAR.DatabaseExplorer/UI/FrmDatabase.cs +++ b/VAR.DatabaseExplorer/UI/FrmDatabase.cs @@ -154,5 +154,29 @@ namespace VAR.DatabaseExplorer.UI Parent.Enabled = true; } + + private void btnVerify_Click(object sender, EventArgs e) + { + List invalidProcedures = ProcedureBL.Procedure_VerifyAndGetInvalidProcedures(connectionString: _connectionString); + List
invalidViews = TableBL.View_VerifyAndGetInvalidViews(connectionString: _connectionString); + + if (invalidProcedures.Count != 0) + { + string message = string.Format("Delete {0} Procedures?", invalidProcedures.Count); + if (MessageBox.Show(this, message, "Delete Procedures?", MessageBoxButtons.OKCancel) == DialogResult.OK) + { + ProcedureBL.Procedure_DeleteProcedures(_connectionString, invalidProcedures); + } + } + + if (invalidViews.Count != 0) + { + string message = string.Format("Delete {0} Views?", invalidViews.Count); + if (MessageBox.Show(this, message, "Delete Views?", MessageBoxButtons.OKCancel) == DialogResult.OK) + { + TableBL.View_DeleteViews(_connectionString, invalidViews); + } + } + } } } \ No newline at end of file