Skip to content

Commit

Permalink
Excluded computed columns
Browse files Browse the repository at this point in the history
  • Loading branch information
ArtemAvramenko committed May 24, 2021
1 parent 6e45d3e commit 8b66400
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
obj
bin

/.vs
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2019 Artem Avramenko
Copyright (c) 2021 Artem Avramenko

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
24 changes: 18 additions & 6 deletions Lib/Dumper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// <copyright file="Dumper.cs">
// SqlDump - Simple SQL Server database dumper
// (c) 2020 Artem Avramenko. https://github.com/ArtemAvramenko/SqlDump
// (c) 2021 Artem Avramenko. https://github.com/ArtemAvramenko/SqlDump
// License: MIT
// </copyright>

Expand All @@ -9,10 +9,15 @@
using System.Data;
using System.IO;
using System.Linq;

#if SQL_CLIENT_LEGACY

using System.Data.SqlClient;

#else

using Microsoft.Data.SqlClient;

#endif

namespace SqlDumper
Expand All @@ -23,6 +28,8 @@ internal class Dumper

private const string TableParam = "table";

private const string SchemaAndTableParam = "schematable";

private readonly string TablesCommand = @"
SELECT TABLE_SCHEMA, TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
Expand All @@ -35,13 +42,14 @@ FROM INFORMATION_SCHEMA.COLUMNS c
LEFT JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE k ON
c.TABLE_SCHEMA = k.TABLE_SCHEMA AND
c.TABLE_NAME = k.TABLE_NAME AND
c.COLUMN_NAME = k.COLUMN_NAME AND
c.COLUMN_NAME = k.COLUMN_NAME AND
OBJECTPROPERTY(OBJECT_ID(k.CONSTRAINT_SCHEMA + '.' + QUOTENAME(k.CONSTRAINT_NAME)), 'IsPrimaryKey') = 1
WHERE c.TABLE_SCHEMA = @{SchemaParam} AND c.TABLE_NAME = @{TableParam}
AND COLUMNPROPERTY(OBJECT_ID(@{SchemaAndTableParam}), c.COLUMN_NAME, 'IsComputed') = 0
ORDER BY ISNULL(k.ORDINAL_POSITION, 30000), 1
";

private SqlConnection _connection;
private readonly SqlConnection _connection;

private SqlCommand _columnsCommand;

Expand All @@ -65,7 +73,8 @@ public Dumper(SqlConnection connection)

public void Dump(TextWriter writer)
{
if (_connection.State == ConnectionState.Closed) {
if (_connection.State == ConnectionState.Closed)
{
_connection.Open();
}

Expand All @@ -74,6 +83,7 @@ public void Dump(TextWriter writer)
_columnsCommand.CommandText = ColumnsCommand;
_columnsCommand.Parameters.Add(SchemaParam, SqlDbType.NVarChar);
_columnsCommand.Parameters.Add(TableParam, SqlDbType.NVarChar);
_columnsCommand.Parameters.Add(SchemaAndTableParam, SqlDbType.NVarChar);

var tables = new List<(string, string)>();
var tablesCommand = _connection.CreateCommand();
Expand Down Expand Up @@ -104,9 +114,12 @@ public void Dump(TextWriter writer)

private void DumpTable(TextWriter writer, string schemaName, string tableName)
{
var fullTableName = $"{QuoteName(schemaName)}.{QuoteName(tableName)}";

var sortColumns = new List<string>();
_columnsCommand.Parameters[SchemaParam].Value = schemaName;
_columnsCommand.Parameters[TableParam].Value = tableName;
_columnsCommand.Parameters[SchemaAndTableParam].Value = fullTableName;
var columns = new List<string>();
var selectList = new List<string>();
using (var metaReader = _columnsCommand.ExecuteReader())
Expand All @@ -131,7 +144,6 @@ private void DumpTable(TextWriter writer, string schemaName, string tableName)
metaReader.Close();
}

var fullTableName = $"{QuoteName(schemaName)}.{QuoteName(tableName)}";
var command = _connection.CreateCommand();
command.CommandText = $"SELECT {string.Join(", ", selectList)} FROM {fullTableName}";
if (sortColumns.Count > 0)
Expand Down Expand Up @@ -228,4 +240,4 @@ private string QuoteName(string name)
return $"[{name}]";
}
}
}
}
4 changes: 2 additions & 2 deletions Lib/Formatters.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// <copyright file="Formatters.cs">
// SqlDump - Simple SQL Server database dumper
// (c) 2020 Artem Avramenko. https://github.com/ArtemAvramenko/SqlDump
// (c) 2021 Artem Avramenko. https://github.com/ArtemAvramenko/SqlDump
// License: MIT
// </copyright>

Expand Down Expand Up @@ -110,4 +110,4 @@ private static string FormatString(string value)
return $"{prefix}'{value.Replace("'", "''")}'";
}
}
}
}
4 changes: 2 additions & 2 deletions SqlDump.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<metadata>
<id>SqlDump.Sources</id>
<title>SqlDump (Source-Only)</title>
<version>1.1.0</version>
<version>1.2.0</version>
<authors>Artem Avramenko</authors>
<owners>ArtemA</owners>
<projectUrl>https://github.com/ArtemAvramenko/SqlDump</projectUrl>
Expand All @@ -16,7 +16,7 @@
<description>
Simple SQL Server database dumper. Shipped as source-only NuGet package.
</description>
<copyright>Copyright (c) 2020 Artem Avramenko</copyright>
<copyright>Copyright (c) 2021 Artem Avramenko</copyright>
<tags>sql dump dumper backup script generator</tags>
</metadata>
<files>
Expand Down

0 comments on commit 8b66400

Please sign in to comment.