205 lines
8.3 KiB
C#
205 lines
8.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using VAR.DatabaseExplorer.Code.DataAccess;
|
|
using VAR.DatabaseExplorer.Code.DataTransfer;
|
|
|
|
namespace VAR.DatabaseExplorer.Code.BusinessLogic
|
|
{
|
|
public class TableBL
|
|
{
|
|
public static List<Table> Table_GetAll(string connectionString, bool fillColumns = false, bool fillViewDefinitions = false)
|
|
{
|
|
List<Table> tables = TableDA.GetAllTables(connectionString);
|
|
if (fillColumns)
|
|
{
|
|
List<Column> allColumns = ColumnDA.GetColumns(connectionString, null, null);
|
|
foreach (Table table in tables)
|
|
{
|
|
table.Columns = allColumns.Where(c => c.TableName == table.Name && c.TableSchema == table.Schema).ToList();
|
|
}
|
|
}
|
|
if (fillViewDefinitions)
|
|
{
|
|
// Get all view definitions
|
|
List<ViewDefinition> definitions = TableDA.GetViewDefinitions(connectionString, null, null);
|
|
foreach (Table table in tables)
|
|
{
|
|
var sbDefinition = new StringBuilder();
|
|
foreach (ViewDefinition definition in definitions)
|
|
{
|
|
if (table.Schema == definition.ViewSchema && table.Name == definition.ViewName)
|
|
{
|
|
sbDefinition.Append(definition.Definition);
|
|
}
|
|
}
|
|
table.ViewDefinition = sbDefinition.ToString();
|
|
}
|
|
}
|
|
return tables;
|
|
}
|
|
|
|
public static void Table_ExportData(Table t, TextWriter txtWriter, string conectionString, Action<int, Action<TextWriter>> notifyLenght = null)
|
|
{
|
|
if (t.Type != "BASE TABLE") { return; }
|
|
|
|
string tableName = string.Format("[{0}].[{1}]", t.Schema, t.Name);
|
|
txtWriter.WriteLine(string.Format("PRINT '*** Importing data of {0}....';", tableName));
|
|
IEnumerable<TableRow> rows = TableDA.GetData(conectionString, tableName);
|
|
if (t.Columns.Any(c => c.Indentity))
|
|
{
|
|
txtWriter.WriteLine(string.Format("-- {0}", tableName));
|
|
txtWriter.WriteLine(string.Format("DELETE FROM {0};", tableName));
|
|
txtWriter.WriteLine(string.Format("DBCC CHECKIDENT ('{0}', RESEED, 0);", tableName));
|
|
txtWriter.WriteLine("GO");
|
|
txtWriter.WriteLine(string.Format("SET IDENTITY_INSERT {0} ON;", tableName));
|
|
foreach (TableRow row in rows)
|
|
{
|
|
int lenght = TableRowHelper.TableRow_GenerateInsert(txtWriter, row, tableName);
|
|
notifyLenght?.Invoke(lenght, (txt) =>
|
|
{
|
|
txtWriter.WriteLine("GO");
|
|
txtWriter.WriteLine(string.Format("SET IDENTITY_INSERT {0} ON;", tableName));
|
|
});
|
|
}
|
|
txtWriter.WriteLine("GO");
|
|
txtWriter.WriteLine(string.Format("SET IDENTITY_INSERT {0} OFF;", tableName));
|
|
txtWriter.WriteLine("GO");
|
|
txtWriter.WriteLine(string.Empty);
|
|
}
|
|
else
|
|
{
|
|
txtWriter.WriteLine(string.Format("-- {0}", tableName));
|
|
txtWriter.WriteLine(string.Format("DELETE FROM {0};", tableName));
|
|
txtWriter.WriteLine("GO");
|
|
foreach (TableRow row in rows)
|
|
{
|
|
int lenght = TableRowHelper.TableRow_GenerateInsert(txtWriter, row, tableName);
|
|
notifyLenght?.Invoke(lenght, null);
|
|
}
|
|
txtWriter.WriteLine("GO");
|
|
txtWriter.WriteLine(string.Empty);
|
|
}
|
|
}
|
|
|
|
public static void Table_GenerateCreate(Table table, TextWriter txtWriter)
|
|
{
|
|
if (table.Type != "BASE TABLE") { return; }
|
|
|
|
string tableName = string.Format("[{0}].[{1}]", table.Schema, table.Name);
|
|
txtWriter.WriteLine(string.Format("PRINT '*** Creating tabla {0}....';", tableName));
|
|
txtWriter.WriteLine(string.Format("IF EXISTS (SELECT TOP 1 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '{0}' AND TABLE_NAME = '{1}')", table.Schema, table.Name));
|
|
txtWriter.WriteLine("BEGIN");
|
|
txtWriter.WriteLine(string.Format(" DROP TABLE {0};", tableName));
|
|
txtWriter.WriteLine("END");
|
|
|
|
txtWriter.WriteLine("GO");
|
|
txtWriter.WriteLine(string.Format("CREATE TABLE {0} (", tableName));
|
|
bool firstColumn = true;
|
|
foreach (Column c in table.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" ||
|
|
false)
|
|
{
|
|
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 (string.IsNullOrEmpty(c.Colation) == false && (
|
|
c.Type == "nvarchar" ||
|
|
c.Type == "varchar" ||
|
|
c.Type == "char" ||
|
|
c.Type == "nchar" ||
|
|
false
|
|
))
|
|
{
|
|
txtWriter.Write(string.Format(" COLLATE {0}", c.Colation));
|
|
}
|
|
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();
|
|
}
|
|
|
|
public static void View_GenerateCreate(Table table, StreamWriter txtWriter)
|
|
{
|
|
if (table.Type != "VIEW") { return; }
|
|
|
|
string viewName = string.Format("[{0}].[{1}]", table.Schema, table.Name);
|
|
txtWriter.WriteLine(string.Format("PRINT '*** Creating view {0}....';", viewName));
|
|
txtWriter.WriteLine(string.Format("IF EXISTS (SELECT TOP 1 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '{0}' AND TABLE_NAME = '{1}')", table.Schema, table.Name));
|
|
txtWriter.WriteLine("BEGIN");
|
|
txtWriter.WriteLine(string.Format(" DROP VIEW {0};", viewName));
|
|
txtWriter.WriteLine("END");
|
|
|
|
txtWriter.WriteLine("GO");
|
|
txtWriter.WriteLine(table.ViewDefinition);
|
|
txtWriter.WriteLine("GO");
|
|
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);
|
|
}
|
|
}
|
|
} |