ExportSchema: Export Views.
This commit is contained in:
@@ -14,10 +14,10 @@ namespace VAR.DatabaseExplorer.Code.BusinessLogic
|
|||||||
return database;
|
return database;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Database Database_GetSchema(string connectionString, bool fillTableDefinitions = false, bool fillProcDefinitions = false)
|
public static Database Database_GetSchema(string connectionString, bool fillTableDefinitions = false, bool fillProcDefinitions = false, bool fillViewDefinitions = false)
|
||||||
{
|
{
|
||||||
Database database = DatabaseDA.GetInfo(connectionString);
|
Database database = DatabaseDA.GetInfo(connectionString);
|
||||||
database.Tables = TableBL.Table_GetAll(connectionString, fillTableDefinitions);
|
database.Tables = TableBL.Table_GetAll(connectionString, fillTableDefinitions, fillViewDefinitions);
|
||||||
database.Procedures = ProcedureBL.Procedure_GetAll(connectionString, fillProcDefinitions);
|
database.Procedures = ProcedureBL.Procedure_GetAll(connectionString, fillProcDefinitions);
|
||||||
return database;
|
return database;
|
||||||
}
|
}
|
||||||
@@ -151,5 +151,19 @@ namespace VAR.DatabaseExplorer.Code.BusinessLogic
|
|||||||
txtWriter.WriteLine(string.Empty);
|
txtWriter.WriteLine(string.Empty);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void Database_ExportViews(StreamWriter txtWriter, Database database)
|
||||||
|
{
|
||||||
|
// Preparar cabecera del script
|
||||||
|
txtWriter.WriteLine("SET NOCOUNT ON;");
|
||||||
|
txtWriter.WriteLine("GO");
|
||||||
|
txtWriter.WriteLine(string.Empty);
|
||||||
|
|
||||||
|
foreach (Table table in database.Tables)
|
||||||
|
{
|
||||||
|
if (table.Type != "VIEW") { continue; }
|
||||||
|
TableBL.View_GenerateCreate(table, txtWriter);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2,6 +2,7 @@
|
|||||||
using System.Data;
|
using System.Data;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
using VAR.DatabaseExplorer.Code.DataAccess;
|
using VAR.DatabaseExplorer.Code.DataAccess;
|
||||||
using VAR.DatabaseExplorer.Code.DataTransfer;
|
using VAR.DatabaseExplorer.Code.DataTransfer;
|
||||||
|
|
||||||
@@ -9,7 +10,7 @@ namespace VAR.DatabaseExplorer.Code.BusinessLogic
|
|||||||
{
|
{
|
||||||
public class TableBL
|
public class TableBL
|
||||||
{
|
{
|
||||||
public static List<Table> Table_GetAll(string connectionString, bool fillColumns = false)
|
public static List<Table> Table_GetAll(string connectionString, bool fillColumns = false, bool fillViewDefinitions = false)
|
||||||
{
|
{
|
||||||
List<Table> tables = TableDA.GetAllTables(connectionString);
|
List<Table> tables = TableDA.GetAllTables(connectionString);
|
||||||
if (fillColumns)
|
if (fillColumns)
|
||||||
@@ -20,6 +21,23 @@ namespace VAR.DatabaseExplorer.Code.BusinessLogic
|
|||||||
table.Columns = allColumns.Where(c => c.TableName == table.Name && c.TableSchema == table.Schema).ToList();
|
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;
|
return tables;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -123,5 +141,22 @@ namespace VAR.DatabaseExplorer.Code.BusinessLogic
|
|||||||
txtWriter.WriteLine("GO");
|
txtWriter.WriteLine("GO");
|
||||||
txtWriter.WriteLine();
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
using System.Collections.Generic;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Data;
|
using System.Data;
|
||||||
using System.Data.SqlClient;
|
using System.Data.SqlClient;
|
||||||
using VAR.DatabaseExplorer.Code.DataTransfer;
|
using VAR.DatabaseExplorer.Code.DataTransfer;
|
||||||
@@ -33,6 +34,53 @@ namespace VAR.DatabaseExplorer.Code.DataAccess
|
|||||||
return tables;
|
return tables;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static List<ViewDefinition> GetViewDefinitions(string connectionString, string schema = null, string name = null)
|
||||||
|
{
|
||||||
|
SqlDataAdapter dataAdapter;
|
||||||
|
var cnx = new SqlConnection(connectionString);
|
||||||
|
cnx.Open();
|
||||||
|
|
||||||
|
if (cnx.ServerVersion.StartsWith("10.") || cnx.ServerVersion.StartsWith("11.") || cnx.ServerVersion.StartsWith("13.") || cnx.ServerVersion.StartsWith("14."))
|
||||||
|
{
|
||||||
|
dataAdapter = new SqlDataAdapter(@"
|
||||||
|
SELECT
|
||||||
|
sn.name ViewSchema,
|
||||||
|
SO.name ViewName,
|
||||||
|
ISNULL(SM.definition, SM.definition) AS [Definition]
|
||||||
|
FROM sys.sql_modules SM
|
||||||
|
INNER JOIN sys.Objects SO ON SM.Object_id = SO.Object_id
|
||||||
|
INNER JOIN sys.schemas sn ON SO.schema_id = sn.schema_id
|
||||||
|
WHERE SO.type = 'v'
|
||||||
|
AND
|
||||||
|
(@name IS NULL OR SO.name = @name) AND
|
||||||
|
(@schema IS NULL OR sn.name = @schema)
|
||||||
|
", cnx);
|
||||||
|
dataAdapter.SelectCommand.Parameters.AddWithValue("@Name", (object)name ?? DBNull.Value);
|
||||||
|
dataAdapter.SelectCommand.Parameters.AddWithValue("@Schema", (object)schema ?? DBNull.Value);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
cnx.Close();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
var dt = new DataTable();
|
||||||
|
dataAdapter.Fill(dt);
|
||||||
|
cnx.Close();
|
||||||
|
|
||||||
|
var definitions = new List<ViewDefinition>();
|
||||||
|
foreach (DataRow dr in dt.Rows)
|
||||||
|
{
|
||||||
|
string strViewSchema = Convert.ToString(dr["ViewSchema"]);
|
||||||
|
string strViewName = Convert.ToString(dr["ViewName"]);
|
||||||
|
string strDefinition = Convert.ToString(dr["Definition"]);
|
||||||
|
strDefinition = strDefinition.Replace("\r", "").Replace("\n", "\r\n");
|
||||||
|
definitions.Add(new ViewDefinition { ViewSchema = strViewSchema, ViewName = strViewName, Definition = strDefinition });
|
||||||
|
}
|
||||||
|
|
||||||
|
return definitions;
|
||||||
|
}
|
||||||
|
|
||||||
public static DataTable GetData(string connectionString, string tableFullName)
|
public static DataTable GetData(string connectionString, string tableFullName)
|
||||||
{
|
{
|
||||||
var cnx = new SqlConnection(connectionString);
|
var cnx = new SqlConnection(connectionString);
|
||||||
|
|||||||
@@ -18,5 +18,8 @@ namespace VAR.DatabaseExplorer.Code.DataTransfer
|
|||||||
|
|
||||||
[XmlArray]
|
[XmlArray]
|
||||||
public List<Column> Columns { get; set; }
|
public List<Column> Columns { get; set; }
|
||||||
|
|
||||||
|
[XmlElement]
|
||||||
|
public string ViewDefinition { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
9
VAR.DatabaseExplorer/Code/DataTransfer/ViewDefinition.cs
Normal file
9
VAR.DatabaseExplorer/Code/DataTransfer/ViewDefinition.cs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
namespace VAR.DatabaseExplorer.Code.DataTransfer
|
||||||
|
{
|
||||||
|
public class ViewDefinition
|
||||||
|
{
|
||||||
|
public string ViewSchema { get; set; }
|
||||||
|
public string ViewName { get; set; }
|
||||||
|
public string Definition { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -116,7 +116,7 @@ namespace VAR.DatabaseExplorer.UI
|
|||||||
|
|
||||||
Database database = DatabaseBL.Database_GetSchema(connectionString: _connectionString, fillTableDefinitions: true);
|
Database database = DatabaseBL.Database_GetSchema(connectionString: _connectionString, fillTableDefinitions: true);
|
||||||
string fixedDatabaseName = database.Name.Replace(' ', '_');
|
string fixedDatabaseName = database.Name.Replace(' ', '_');
|
||||||
var streamWriter = new StreamWriter(fixedDatabaseName + ".Data.sql");
|
var streamWriter = new StreamWriter("05." + fixedDatabaseName + ".Data.sql");
|
||||||
DatabaseBL.Database_ExportData(streamWriter, _connectionString, database);
|
DatabaseBL.Database_ExportData(streamWriter, _connectionString, database);
|
||||||
streamWriter.Close();
|
streamWriter.Close();
|
||||||
|
|
||||||
@@ -127,7 +127,7 @@ namespace VAR.DatabaseExplorer.UI
|
|||||||
{
|
{
|
||||||
Parent.Enabled = false;
|
Parent.Enabled = false;
|
||||||
|
|
||||||
Database database = DatabaseBL.Database_GetSchema(connectionString: _connectionString, fillTableDefinitions: true, fillProcDefinitions: true);
|
Database database = DatabaseBL.Database_GetSchema(connectionString: _connectionString, fillTableDefinitions: true, fillProcDefinitions: true, fillViewDefinitions: true);
|
||||||
string fixedDatabaseName = database.Name.Replace(' ', '_');
|
string fixedDatabaseName = database.Name.Replace(' ', '_');
|
||||||
|
|
||||||
// Schemas
|
// Schemas
|
||||||
@@ -136,16 +136,19 @@ namespace VAR.DatabaseExplorer.UI
|
|||||||
streamWriterSchemas.Close();
|
streamWriterSchemas.Close();
|
||||||
|
|
||||||
// Tables
|
// Tables
|
||||||
var streamWriterTables = new StreamWriter(fixedDatabaseName + ".Tables.sql");
|
var streamWriterTables = new StreamWriter("02." + fixedDatabaseName + ".Tables.sql");
|
||||||
DatabaseBL.Database_ExportTables(streamWriterTables, _connectionString, database);
|
DatabaseBL.Database_ExportTables(streamWriterTables, _connectionString, database);
|
||||||
streamWriterTables.Close();
|
streamWriterTables.Close();
|
||||||
|
|
||||||
// Routines
|
// Routines
|
||||||
var streamWriterRoutines = new StreamWriter(fixedDatabaseName + ".Routines.sql");
|
var streamWriterRoutines = new StreamWriter("03." + fixedDatabaseName + ".Routines.sql");
|
||||||
DatabaseBL.Database_ExportFunctions(streamWriterRoutines, database);
|
DatabaseBL.Database_ExportFunctions(streamWriterRoutines, database);
|
||||||
streamWriterRoutines.Close();
|
streamWriterRoutines.Close();
|
||||||
|
|
||||||
// TODO Views
|
// Views
|
||||||
|
var streamWriterViews = new StreamWriter("04." + fixedDatabaseName + ".Views.sql");
|
||||||
|
DatabaseBL.Database_ExportViews(streamWriterViews, database);
|
||||||
|
streamWriterViews.Close();
|
||||||
|
|
||||||
Parent.Enabled = true;
|
Parent.Enabled = true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -95,6 +95,7 @@
|
|||||||
<Compile Include="Code\DataTransfer\Server.cs" />
|
<Compile Include="Code\DataTransfer\Server.cs" />
|
||||||
<Compile Include="Code\DataTransfer\Table.cs" />
|
<Compile Include="Code\DataTransfer\Table.cs" />
|
||||||
<Compile Include="Code\DataTransfer\User.cs" />
|
<Compile Include="Code\DataTransfer\User.cs" />
|
||||||
|
<Compile Include="Code\DataTransfer\ViewDefinition.cs" />
|
||||||
<Compile Include="Controls\CustomListView.cs">
|
<Compile Include="Controls\CustomListView.cs">
|
||||||
<SubType>Component</SubType>
|
<SubType>Component</SubType>
|
||||||
</Compile>
|
</Compile>
|
||||||
|
|||||||
Reference in New Issue
Block a user