FrmBaseDatos: ExportSchema button.

This commit is contained in:
2019-07-29 02:39:52 +02:00
parent 4da1292698
commit 70f0eeb7ee
6 changed files with 159 additions and 33 deletions

View File

@@ -12,11 +12,11 @@ namespace VAR.DatabaseExplorer.Code.BusinessLogic
return database;
}
public static Database Database_GetSchema(string connectionString, bool fillDefinitions = false)
public static Database Database_GetSchema(string connectionString, bool fillTableDefinitions = false, bool fillProcDefinitions = false)
{
Database database = DatabaseDA.GetInfo(connectionString);
database.Tables = TableBL.Table_GetAll(connectionString, fillDefinitions);
database.Procedures = ProcedureBL.Procedure_GetAll(connectionString, fillDefinitions);
database.Tables = TableBL.Table_GetAll(connectionString, fillTableDefinitions);
database.Procedures = ProcedureBL.Procedure_GetAll(connectionString, fillProcDefinitions);
return database;
}
@@ -89,5 +89,73 @@ namespace VAR.DatabaseExplorer.Code.BusinessLogic
txtWriter.WriteLine("GO");
txtWriter.WriteLine(string.Empty);
}
public static void Database_ExportTables(TextWriter txtWriter, string connectionString, Database database)
{
foreach (Table t in database.Tables)
{
if (t.Type != "BASE TABLE") { continue; }
string tableName = string.Format("{0}.{1}", t.Schema, t.Name);
txtWriter.WriteLine(string.Format("PRINT '*** Creating tabla {0}....';", tableName));
txtWriter.WriteLine(string.Format("CREATE TABLE {0} (", tableName));
bool firstColumn = true;
foreach (Column c in t.Columns)
{
if (firstColumn == false)
{
txtWriter.WriteLine(",");
}
txtWriter.Write(" [");
txtWriter.Write(c.Name);
txtWriter.Write("] ");
if (
c.Type == "nvarchar" ||
c.Type == "varchar" ||
c.Type == "char" ||
c.Type == "nchar" ||
c.Type == "binary" ||
c.Type == "varbinary" ||
c.Type == "image")
{
if (c.Size < 0)
{
txtWriter.Write(string.Format("{0}(MAX)", c.Type));
}
else
{
txtWriter.Write(string.Format("{0}({1})", c.Type, c.Size));
}
}
else
{
txtWriter.Write(c.Type);
}
if (c.Nullable)
{
txtWriter.Write(" NULL");
}
else
{
txtWriter.Write(" NOT NULL");
}
if (c.PK)
{
txtWriter.Write(" PRIMARY KEY");
}
if (c.Indentity)
{
txtWriter.Write(" IDENTITY(1,1)");
}
firstColumn = false;
}
txtWriter.WriteLine();
txtWriter.WriteLine(string.Format(");"));
txtWriter.WriteLine("GO");
txtWriter.WriteLine();
}
}
}
}

View File

@@ -13,9 +13,10 @@ namespace VAR.DatabaseExplorer.Code.BusinessLogic
List<Table> tables = TableDA.GetAllTables(connectionString);
if (fillColumns)
{
List<Column> allColumns = ColumnDA.GetColumns(connectionString, null, null);
foreach (Table table in tables)
{
table.Columns = ColumnDA.GetColumns(connectionString, table.Schema, table.Name);
table.Columns = allColumns.Where(c => c.TableName == table.Name && c.TableSchema == table.Schema).ToList();
}
}
return tables;

View File

@@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using VAR.DatabaseExplorer.Code.DataTransfer;
namespace VAR.DatabaseExplorer.Code.DataAccess
@@ -16,25 +15,39 @@ namespace VAR.DatabaseExplorer.Code.DataAccess
// Preparar comando y parametros
var da = new SqlDataAdapter(@"
SELECT col.COLUMN_NAME AS ColumnName,
col.DATA_TYPE AS Type,
col.CHARACTER_MAXIMUM_LENGTH AS Size,
c.CONSTRAINT_TYPE AS KeyType,
col.IS_NULLABLE AS Nullable
FROM INFORMATION_SCHEMA.COLUMNS AS col
LEFT JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS k
ON col.COLUMN_NAME=k.COLUMN_NAME AND
col.TABLE_NAME=k.TABLE_NAME AND
col.TABLE_SCHEMA=k.TABLE_SCHEMA
SELECT
col.TABLE_SCHEMA TableSchema,
col.TABLE_NAME TableName,
col.COLUMN_NAME AS ColumnName,
col.DATA_TYPE AS Type,
col.CHARACTER_MAXIMUM_LENGTH AS Size,
c.KeyType AS KeyType,
col.IS_NULLABLE AS Nullable,
CASE WHEN (COLUMNPROPERTY(object_id(col.TABLE_SCHEMA+'.'+col.TABLE_NAME), col.COLUMN_NAME, 'IsIdentity') = 1) THEN 1 ELSE 0 END IsIdentity,
NULL EmptyColumn
FROM INFORMATION_SCHEMA.COLUMNS AS col
LEFT JOIN (
SELECT
k.TABLE_SCHEMA TableSchema,
k.TABLE_NAME TableName,
k.COLUMN_NAME ColumnName,
c.CONSTRAINT_TYPE KeyType
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS k
LEFT JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS c
ON k.CONSTRAINT_NAME=c.CONSTRAINT_NAME
WHERE col.TABLE_NAME=@TableName AND
col.TABLE_SCHEMA=@TableSchema
ORDER BY col.ORDINAL_POSITION
", cnx);
var prm = new SqlParameter("@TableName", SqlDbType.VarChar, 100) { Value = tableName };
ON k.CONSTRAINT_NAME=c.CONSTRAINT_NAME AND
k.TABLE_NAME=c.TABLE_NAME AND
k.TABLE_SCHEMA=c.TABLE_SCHEMA
) c ON col.COLUMN_NAME=c.ColumnName AND
col.TABLE_NAME=c.TableName AND
col.TABLE_SCHEMA=c.TableSchema AND
c.KeyType = 'PRIMARY KEY'
WHERE (@TableName IS NULL OR col.TABLE_NAME=@TableName) AND
(@TableSchema IS NULL OR col.TABLE_SCHEMA=@TableSchema)
ORDER BY col.TABLE_SCHEMA, col.TABLE_NAME, col.ORDINAL_POSITION
", cnx);
var prm = new SqlParameter("@TableName", SqlDbType.VarChar, 100) { Value = (object)tableName ?? DBNull.Value };
da.SelectCommand.Parameters.Add(prm);
prm = new SqlParameter("@TableSchema", SqlDbType.VarChar, 100) { Value = tableSchema };
prm = new SqlParameter("@TableSchema", SqlDbType.VarChar, 100) { Value = (object)tableSchema ?? DBNull.Value };
da.SelectCommand.Parameters.Add(prm);
// Obtener datatable con las columnas
@@ -46,15 +59,11 @@ namespace VAR.DatabaseExplorer.Code.DataAccess
// Recorrer datatable estableciendo la lista de columnas
foreach (DataRow dr in dt.Rows)
{
string columnName = Convert.ToString(dr["ColumnName"]);
Column col = columns.FirstOrDefault(c => c.Name == columnName);
if (col == null)
{
col = new Column();
columns.Add(col);
}
Column col = new Column();
col.Name = columnName;
col.TableSchema = Convert.ToString(dr["TableSchema"]);
col.TableName = Convert.ToString(dr["TableName"]);
col.Name = Convert.ToString(dr["ColumnName"]);
col.Type = Convert.ToString(dr["Type"]);
@@ -71,6 +80,9 @@ namespace VAR.DatabaseExplorer.Code.DataAccess
{
col.PK = true;
}
col.Indentity = Convert.ToBoolean(dr["IsIdentity"]);
columns.Add(col);
}
return columns;

View File

@@ -6,6 +6,12 @@ namespace VAR.DatabaseExplorer.Code.DataTransfer
[Serializable]
public class Column
{
[XmlAttribute]
public string TableSchema { get; set; }
[XmlAttribute]
public string TableName { get; set; }
[XmlAttribute]
public string Name { get; set; }
@@ -20,5 +26,8 @@ namespace VAR.DatabaseExplorer.Code.DataTransfer
[XmlAttribute]
public bool PK { get; set; }
[XmlAttribute]
public bool Indentity { get; set; }
}
}

View File

@@ -50,6 +50,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();
((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit();
this.splitContainer1.Panel1.SuspendLayout();
this.splitContainer1.Panel2.SuspendLayout();
@@ -125,7 +126,7 @@
// btnDocGen
//
this.btnDocGen.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.btnDocGen.Location = new System.Drawing.Point(84, 485);
this.btnDocGen.Location = new System.Drawing.Point(181, 485);
this.btnDocGen.Name = "btnDocGen";
this.btnDocGen.Size = new System.Drawing.Size(75, 23);
this.btnDocGen.TabIndex = 14;
@@ -174,6 +175,7 @@
//
// splitContainer1.Panel2
//
this.splitContainer1.Panel2.Controls.Add(this.btnExportSchema);
this.splitContainer1.Panel2.Controls.Add(this.btnExportData);
this.splitContainer1.Panel2.Controls.Add(this.lblTituloTabla);
this.splitContainer1.Panel2.Controls.Add(this.btnDocGen);
@@ -289,6 +291,17 @@
this.txtConString.TabIndex = 0;
this.txtConString.TabWidth = 8;
//
// 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);
//
// FrmBaseDatos
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
@@ -336,5 +349,6 @@
private System.Windows.Forms.ColumnHeader colNullable;
private System.Windows.Forms.Button btnRefresh;
private System.Windows.Forms.Button btnExportData;
private System.Windows.Forms.Button btnExportSchema;
}
}

View File

@@ -95,7 +95,7 @@ namespace VAR.DatabaseExplorer.UI
{
Parent.Enabled = false;
Database database = DatabaseBL.Database_GetSchema(connectionString: _connectionString, fillDefinitions: true);
Database database = DatabaseBL.Database_GetSchema(connectionString: _connectionString, fillTableDefinitions: true, fillProcDefinitions: true);
string fixedDatabaseName = database.Name.Replace(' ', '_');
var streamWriter = new StreamWriter(fixedDatabaseName + ".documentation.html");
@@ -114,12 +114,34 @@ namespace VAR.DatabaseExplorer.UI
{
Parent.Enabled = false;
Database database = DatabaseBL.Database_GetSchema(connectionString: _connectionString, fillDefinitions: false);
Database database = DatabaseBL.Database_GetSchema(connectionString: _connectionString, fillTableDefinitions: true);
string fixedDatabaseName = database.Name.Replace(' ', '_');
var streamWriter = new StreamWriter(fixedDatabaseName + ".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);
string fixedDatabaseName = database.Name.Replace(' ', '_');
// Tables
var streamWriter = new StreamWriter(fixedDatabaseName + ".Tables.sql");
DatabaseBL.Database_ExportTables(streamWriter, _connectionString, database);
streamWriter.Close();
// TODO Functions
// TODO Views
// TODO Procedures
Parent.Enabled = true;
}
}