182 lines
6.9 KiB
C#
182 lines
6.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Windows.Forms;
|
|
using VAR.DatabaseExplorer.Code;
|
|
using VAR.DatabaseExplorer.Code.BusinessLogic;
|
|
using VAR.DatabaseExplorer.Code.DataAccess;
|
|
using VAR.DatabaseExplorer.Code.DataTransfer;
|
|
|
|
namespace VAR.DatabaseExplorer.UI
|
|
{
|
|
public partial class FrmDatabase : Form
|
|
{
|
|
private string _connectionString;
|
|
|
|
private string _tableSchema;
|
|
private string _tableName;
|
|
|
|
private void Initialize()
|
|
{
|
|
List<Table> tables = TableBL.Table_GetAll(_connectionString);
|
|
lsvTablas.Items.Clear();
|
|
foreach (Table table in tables)
|
|
{
|
|
ListViewItem item = lsvTablas.Items.Add(table.Schema);
|
|
item.SubItems.Add(table.Name);
|
|
item.SubItems.Add(table.Type);
|
|
}
|
|
|
|
lsvColumnas.Items.Clear();
|
|
}
|
|
|
|
public FrmDatabase(string connectionString)
|
|
{
|
|
InitializeComponent();
|
|
_connectionString = connectionString;
|
|
txtConString.Text = connectionString;
|
|
}
|
|
|
|
private void frmBaseDatos_Load(object sender, EventArgs e)
|
|
{
|
|
Initialize();
|
|
Database database = DatabaseBL.Database_Get(_connectionString);
|
|
Text = database.Name;
|
|
lblTituloDB.Text = database.Name;
|
|
lblTituloTabla.Text = "";
|
|
}
|
|
|
|
private void btnCopiarConString_Click(object sender, EventArgs e)
|
|
{
|
|
Clipboard.SetText(txtConString.Text);
|
|
}
|
|
|
|
private void lsvTablas_SelectedIndexChanged(object sender, EventArgs e)
|
|
{
|
|
if (lsvTablas.SelectedItems.Count != 1) { return; }
|
|
|
|
ListViewItem item = lsvTablas.SelectedItems[0];
|
|
_tableSchema = item.SubItems[0].Text;
|
|
_tableName = item.SubItems[1].Text;
|
|
|
|
lblTituloTabla.Text = _tableSchema + @"." + _tableName;
|
|
|
|
List<Column> columns = ColumnDA.GetColumns(_connectionString, _tableSchema, _tableName);
|
|
lsvColumnas.Items.Clear();
|
|
foreach (Column col in columns)
|
|
{
|
|
ListViewItem subitem = lsvColumnas.Items.Add(col.Name);
|
|
string type = string.Format("{0}{1}", col.Type, col.Size > 0 ? string.Format("({0})", col.Size) : string.Empty);
|
|
subitem.SubItems.Add(type);
|
|
subitem.SubItems.Add(col.PK ? "PK" : string.Empty);
|
|
subitem.SubItems.Add(col.Nullable ? "Null" : "NotNull");
|
|
}
|
|
}
|
|
|
|
private void btnVerDatos_Click(object sender, EventArgs e)
|
|
{
|
|
if (string.IsNullOrEmpty(_tableSchema) || string.IsNullOrEmpty(_tableName)) { return; }
|
|
|
|
var form = new FrmData(_connectionString, _tableSchema, _tableName);
|
|
FrmMain.AddForm(form);
|
|
}
|
|
|
|
private void btnProcs_Click(object sender, EventArgs e)
|
|
{
|
|
var form = new FrmRoutines(_connectionString);
|
|
FrmMain.AddForm(form);
|
|
}
|
|
|
|
private void btnExec_Click(object sender, EventArgs e)
|
|
{
|
|
var form = new FrmExec(_connectionString);
|
|
FrmMain.AddForm(form);
|
|
}
|
|
|
|
private void btnDocGen_Click(object sender, EventArgs e)
|
|
{
|
|
Parent.Enabled = false;
|
|
|
|
Database database = DatabaseBL.Database_GetSchema(connectionString: _connectionString, fillTableDefinitions: true, fillProcDefinitions: true);
|
|
|
|
string fixedDatabaseName = database.Name.Replace(' ', '_');
|
|
var streamWriter = new StreamWriter(fixedDatabaseName + ".documentation.html");
|
|
DatabaseBL.Database_GenerateDocumentation(streamWriter, database);
|
|
streamWriter.Close();
|
|
|
|
Parent.Enabled = true;
|
|
}
|
|
|
|
private void btnRefresh_Click(object sender, EventArgs e)
|
|
{
|
|
Initialize();
|
|
}
|
|
|
|
private void btnExportData_Click(object sender, EventArgs e)
|
|
{
|
|
Parent.Enabled = false;
|
|
|
|
Database database = DatabaseBL.Database_GetSchema(connectionString: _connectionString, fillTableDefinitions: true);
|
|
string fixedDatabaseName = database.Name.Replace(' ', '_');
|
|
var streamWriter = new SplittingStreamWriter("05." + fixedDatabaseName + ".{0:000}.Data.sql");
|
|
DatabaseBL.Database_ExportData(streamWriter, _connectionString, database);
|
|
streamWriter.Close();
|
|
|
|
Parent.Enabled = true;
|
|
}
|
|
|
|
private void BtnExportSchema_Click(object sender, EventArgs e)
|
|
{
|
|
Parent.Enabled = false;
|
|
|
|
Database database = DatabaseBL.Database_GetSchema(connectionString: _connectionString, fillTableDefinitions: true, fillProcDefinitions: true, fillViewDefinitions: true);
|
|
string fixedDatabaseName = database.Name.Replace(' ', '_');
|
|
|
|
// Schemas
|
|
var streamWriterSchemas = new StreamWriter("01." + fixedDatabaseName + ".Schemas.sql");
|
|
DatabaseBL.Database_ExportSchemas(streamWriterSchemas, _connectionString, database);
|
|
streamWriterSchemas.Close();
|
|
|
|
// Tables
|
|
var streamWriterTables = new StreamWriter("02." + fixedDatabaseName + ".Tables.sql");
|
|
DatabaseBL.Database_ExportTables(streamWriterTables, _connectionString, database);
|
|
streamWriterTables.Close();
|
|
|
|
// Routines
|
|
var streamWriterRoutines = new StreamWriter("03." + fixedDatabaseName + ".Routines.sql");
|
|
DatabaseBL.Database_ExportFunctions(streamWriterRoutines, database);
|
|
streamWriterRoutines.Close();
|
|
|
|
// Views
|
|
var streamWriterViews = new StreamWriter("04." + fixedDatabaseName + ".Views.sql");
|
|
DatabaseBL.Database_ExportViews(streamWriterViews, database);
|
|
streamWriterViews.Close();
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |