FrmDatabase: Verify button, for procedures, functions and views

This commit is contained in:
2021-01-22 15:12:37 +01:00
parent 9b68ff797f
commit c789cfd3d4
7 changed files with 181 additions and 42 deletions

View File

@@ -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); string routineName = string.Format("{0}.{1}", routine.Schema, routine.Name);
if (routine.Type.ToUpper() == "PROCEDURE") if (routine.Type.ToUpper() == "PROCEDURE")
@@ -114,5 +114,24 @@ namespace VAR.DatabaseExplorer.Code.BusinessLogic
txtWriter.WriteLine("GO"); txtWriter.WriteLine("GO");
txtWriter.WriteLine(); txtWriter.WriteLine();
} }
public static List<Procedure> Procedure_VerifyAndGetInvalidProcedures(string connectionString)
{
List<Procedure> procedures = ProcedureDA.GetAllProcedures(connectionString);
List<Procedure> invalidProcedures = new List<Procedure>();
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<Procedure> procedures)
{
ProcedureDA.Procedure_DeleteProcedures(connectionString, procedures);
}
} }
} }

View File

@@ -181,5 +181,25 @@ namespace VAR.DatabaseExplorer.Code.BusinessLogic
txtWriter.WriteLine("GO"); txtWriter.WriteLine("GO");
txtWriter.WriteLine(); txtWriter.WriteLine();
} }
public static List<Table> View_VerifyAndGetInvalidViews(string connectionString)
{
List<Table> tables = TableDA.GetAllTables(connectionString);
List<Table> invalidViews = new List<Table>();
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<Table> views)
{
TableDA.View_DeleteViews(connectionString, views);
}
} }
} }

View File

@@ -73,5 +73,54 @@ namespace VAR.DatabaseExplorer.Code.DataAccess
List<string> schemas = dt.AsEnumerable().Select(dr => Convert.ToString(dr["schema_name"])).ToList(); List<string> schemas = dt.AsEnumerable().Select(dr => Convert.ToString(dr["schema_name"])).ToList();
return schemas; 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;
}
} }
} }

View File

@@ -35,26 +35,13 @@ namespace VAR.DatabaseExplorer.Code.DataAccess
return procedures; 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) public static string GetProcedureDefinition(string connectionString, string schema, string name)
{ {
SqlDataAdapter dataAdapter; SqlDataAdapter dataAdapter;
var cnx = new SqlConnection(connectionString); var cnx = new SqlConnection(connectionString);
cnx.Open(); cnx.Open();
if (SupportedSqlServerVersion(cnx.ServerVersion)) if (DatabaseDA.SupportedSqlServerVersion(cnx.ServerVersion))
{ {
dataAdapter = new SqlDataAdapter(@" dataAdapter = new SqlDataAdapter(@"
SELECT SELECT
@@ -101,7 +88,7 @@ namespace VAR.DatabaseExplorer.Code.DataAccess
var cnx = new SqlConnection(connectionString); var cnx = new SqlConnection(connectionString);
cnx.Open(); cnx.Open();
if (SupportedSqlServerVersion(cnx.ServerVersion)) if (DatabaseDA.SupportedSqlServerVersion(cnx.ServerVersion))
{ {
dataAdapter = new SqlDataAdapter(@" dataAdapter = new SqlDataAdapter(@"
SELECT SELECT
@@ -143,5 +130,28 @@ namespace VAR.DatabaseExplorer.Code.DataAccess
return definitions; return definitions;
} }
public static void Procedure_DeleteProcedures(string connectionString, List<Procedure> 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();
}
} }
} }

View File

@@ -34,26 +34,13 @@ namespace VAR.DatabaseExplorer.Code.DataAccess
return tables; 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<ViewDefinition> GetViewDefinitions(string connectionString, string schema = null, string name = null) public static List<ViewDefinition> GetViewDefinitions(string connectionString, string schema = null, string name = null)
{ {
SqlDataAdapter dataAdapter; SqlDataAdapter dataAdapter;
var cnx = new SqlConnection(connectionString); var cnx = new SqlConnection(connectionString);
cnx.Open(); cnx.Open();
if (SupportedSqlServerVersion(cnx.ServerVersion)) if (DatabaseDA.SupportedSqlServerVersion(cnx.ServerVersion))
{ {
dataAdapter = new SqlDataAdapter(@" dataAdapter = new SqlDataAdapter(@"
SELECT SELECT
@@ -108,5 +95,20 @@ namespace VAR.DatabaseExplorer.Code.DataAccess
} }
cnx.Close(); cnx.Close();
} }
public static void View_DeleteViews(string connectionString, List<Table> 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();
}
} }
} }

View File

@@ -43,6 +43,7 @@
this.colEsquema = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); this.colEsquema = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this.colNombreTabla = ((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.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.btnExportData = new System.Windows.Forms.Button();
this.lsvColumnas = new VAR.DatabaseExplorer.Controls.CustomListView(); this.lsvColumnas = new VAR.DatabaseExplorer.Controls.CustomListView();
this.colNombreColumna = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); 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.colClave = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this.colNullable = ((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.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(); ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit();
this.splitContainer1.Panel1.SuspendLayout(); this.splitContainer1.Panel1.SuspendLayout();
this.splitContainer1.Panel2.SuspendLayout(); this.splitContainer1.Panel2.SuspendLayout();
@@ -175,6 +176,7 @@
// //
// splitContainer1.Panel2 // splitContainer1.Panel2
// //
this.splitContainer1.Panel2.Controls.Add(this.btnVerify);
this.splitContainer1.Panel2.Controls.Add(this.btnExportSchema); this.splitContainer1.Panel2.Controls.Add(this.btnExportSchema);
this.splitContainer1.Panel2.Controls.Add(this.btnExportData); this.splitContainer1.Panel2.Controls.Add(this.btnExportData);
this.splitContainer1.Panel2.Controls.Add(this.lblTituloTabla); this.splitContainer1.Panel2.Controls.Add(this.lblTituloTabla);
@@ -208,6 +210,7 @@
this.colNombreTabla, this.colNombreTabla,
this.colTipo}); this.colTipo});
this.lsvTablas.FullRowSelect = true; this.lsvTablas.FullRowSelect = true;
this.lsvTablas.HideSelection = false;
this.lsvTablas.Location = new System.Drawing.Point(3, 40); this.lsvTablas.Location = new System.Drawing.Point(3, 40);
this.lsvTablas.Name = "lsvTablas"; this.lsvTablas.Name = "lsvTablas";
this.lsvTablas.Size = new System.Drawing.Size(326, 439); this.lsvTablas.Size = new System.Drawing.Size(326, 439);
@@ -231,6 +234,17 @@
this.colTipo.Text = "Tipo"; this.colTipo.Text = "Tipo";
this.colTipo.Width = 71; 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 // btnExportData
// //
this.btnExportData.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); this.btnExportData.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
@@ -254,6 +268,7 @@
this.colClave, this.colClave,
this.colNullable}); this.colNullable});
this.lsvColumnas.FullRowSelect = true; this.lsvColumnas.FullRowSelect = true;
this.lsvColumnas.HideSelection = false;
this.lsvColumnas.Location = new System.Drawing.Point(3, 40); this.lsvColumnas.Location = new System.Drawing.Point(3, 40);
this.lsvColumnas.Name = "lsvColumnas"; this.lsvColumnas.Name = "lsvColumnas";
this.lsvColumnas.Size = new System.Drawing.Size(464, 439); this.lsvColumnas.Size = new System.Drawing.Size(464, 439);
@@ -291,18 +306,17 @@
this.txtConString.TabIndex = 0; this.txtConString.TabIndex = 0;
this.txtConString.TabWidth = 8; this.txtConString.TabWidth = 8;
// //
// btnExportSchema // btnVerify
// //
this.btnExportSchema.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); this.btnVerify.Location = new System.Drawing.Point(262, 485);
this.btnExportSchema.Location = new System.Drawing.Point(84, 485); this.btnVerify.Name = "btnVerify";
this.btnExportSchema.Name = "btnExportSchema"; this.btnVerify.Size = new System.Drawing.Size(75, 23);
this.btnExportSchema.Size = new System.Drawing.Size(91, 23); this.btnVerify.TabIndex = 17;
this.btnExportSchema.TabIndex = 16; this.btnVerify.Text = "Verify";
this.btnExportSchema.Text = "ExportSchema"; this.btnVerify.UseVisualStyleBackColor = true;
this.btnExportSchema.UseVisualStyleBackColor = true; this.btnVerify.Click += new System.EventHandler(this.btnVerify_Click);
this.btnExportSchema.Click += new System.EventHandler(this.BtnExportSchema_Click);
// //
// FrmBaseDatos // FrmDatabase
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
@@ -313,7 +327,7 @@
this.Controls.Add(this.txtConString); this.Controls.Add(this.txtConString);
this.Controls.Add(this.menuBaseDatos); this.Controls.Add(this.menuBaseDatos);
this.MainMenuStrip = this.menuBaseDatos; this.MainMenuStrip = this.menuBaseDatos;
this.Name = "FrmBaseDatos"; this.Name = "FrmDatabase";
this.Text = "Base de Datos"; this.Text = "Base de Datos";
this.Load += new System.EventHandler(this.frmBaseDatos_Load); this.Load += new System.EventHandler(this.frmBaseDatos_Load);
this.splitContainer1.Panel1.ResumeLayout(false); this.splitContainer1.Panel1.ResumeLayout(false);
@@ -350,5 +364,6 @@
private System.Windows.Forms.Button btnRefresh; private System.Windows.Forms.Button btnRefresh;
private System.Windows.Forms.Button btnExportData; private System.Windows.Forms.Button btnExportData;
private System.Windows.Forms.Button btnExportSchema; private System.Windows.Forms.Button btnExportSchema;
private System.Windows.Forms.Button btnVerify;
} }
} }

View File

@@ -154,5 +154,29 @@ namespace VAR.DatabaseExplorer.UI
Parent.Enabled = true; Parent.Enabled = true;
} }
private void btnVerify_Click(object sender, EventArgs e)
{
List<Procedure> invalidProcedures = ProcedureBL.Procedure_VerifyAndGetInvalidProcedures(connectionString: _connectionString);
List<Table> 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);
}
}
}
} }
} }