Added functions to SqlBuilder for generating more SQL scripts on differences. Versioned up NuGet.
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
@@ -37,6 +38,73 @@ namespace DbTools {
|
||||
return tables.ToArray();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Compares the tables in two database connections and returns the names of tables that exist in the second
|
||||
/// database but are missing from the first.
|
||||
/// </summary>
|
||||
/// <remarks>This method ensures that both database connections are open before performing the
|
||||
/// comparison. It retrieves the table names from each database and identifies the tables that are missing
|
||||
/// from the first database.</remarks>
|
||||
/// <param name="db1">The database connection representing the first database. The connection must be valid and open, or it will
|
||||
/// be opened automatically.</param>
|
||||
/// <param name="db2">The database connection representing the second database. The connection must be valid and open, or it will
|
||||
/// be opened automatically.</param>
|
||||
/// <returns>An array of strings containing the names of tables that are present in the second database but not in the
|
||||
/// first. Returns an empty array if no such tables are found.</returns>
|
||||
/// <exception cref="ArgumentNullException">Thrown if <paramref name="db1"/> or <paramref name="db2"/> is <see langword="null"/>.</exception>
|
||||
public string[] GetMissingTables(IDbConnection db1, IDbConnection db2) {
|
||||
if (db1 == null) {
|
||||
throw new ArgumentNullException("Must provide a valid DBConnection for db1");
|
||||
}
|
||||
if (db2 == null) {
|
||||
throw new ArgumentNullException("Must provide a valid DBConnection for db2");
|
||||
}
|
||||
if (db1.State != ConnectionState.Open) {
|
||||
db1.Open();
|
||||
}
|
||||
if (db2.State != ConnectionState.Open) {
|
||||
db2.Open();
|
||||
}
|
||||
|
||||
string[] db1Tables = GetTableNames(db1);
|
||||
string[] db2Tables = GetTableNames(db2);
|
||||
|
||||
return db2Tables.Where(f => !db1Tables.Contains(f)).ToArray();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves the names of tables that exist in the first database but not in the second database.
|
||||
/// </summary>
|
||||
/// <remarks>This method compares the table names in the two databases and identifies tables that
|
||||
/// are unique to the first database. The connections provided will be opened if they are not already
|
||||
/// open.</remarks>
|
||||
/// <param name="db1">The connection to the first database. The connection must be valid and open, or it will be opened
|
||||
/// automatically.</param>
|
||||
/// <param name="db2">The connection to the second database. The connection must be valid and open, or it will be opened
|
||||
/// automatically.</param>
|
||||
/// <returns>An array of strings containing the names of tables that are present in the first database but not in the
|
||||
/// second database. Returns an empty array if no such tables exist.</returns>
|
||||
/// <exception cref="ArgumentNullException">Thrown if <paramref name="db1"/> or <paramref name="db2"/> is <see langword="null"/>.</exception>
|
||||
public string[] GetUnusedTables(IDbConnection db1, IDbConnection db2) {
|
||||
if (db1 == null) {
|
||||
throw new ArgumentNullException("Must provide a valid DBConnection for db1");
|
||||
}
|
||||
if (db2 == null) {
|
||||
throw new ArgumentNullException("Must provide a valid DBConnection for db2");
|
||||
}
|
||||
if (db1.State != ConnectionState.Open) {
|
||||
db1.Open();
|
||||
}
|
||||
if (db2.State != ConnectionState.Open) {
|
||||
db2.Open();
|
||||
}
|
||||
|
||||
string[] db1Tables = GetTableNames(db1);
|
||||
string[] db2Tables = GetTableNames(db2);
|
||||
|
||||
return db1Tables.Where(f => !db2Tables.Contains(f)).ToArray();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generates the SQL script to recreate the specified table, including its schema, indexes, triggers, and
|
||||
/// optionally its data.
|
||||
@@ -115,6 +183,36 @@ namespace DbTools {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public string GetTableDropSql(IDbConnection cn, string tableName) {
|
||||
if (cn == null) {
|
||||
throw new ArgumentNullException("cn");
|
||||
}
|
||||
if (string.IsNullOrEmpty(tableName)) {
|
||||
throw new ArgumentNullException("tableName");
|
||||
}
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
string[] indexes = GetIndexCreateSqlAsync(cn, tableName).Result.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries);
|
||||
foreach (string index in indexes) {
|
||||
Match m = Regex.Match(index, "CREATE INDEX (\\S+) ON .*", RegexOptions.Singleline);
|
||||
if (m.Success) {
|
||||
sb.AppendLine($"DROP INDEX IF EXISTS {m.Groups[1].Value};");
|
||||
}
|
||||
}
|
||||
|
||||
string[] triggers = GetTriggerCreateSqlAsync(cn, tableName).Result.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries);
|
||||
foreach (string trigger in triggers) {
|
||||
Match m = Regex.Match(trigger, "CREATE TRIGGER (\\S+) .*", RegexOptions.Singleline);
|
||||
if (m.Success) {
|
||||
sb.AppendLine($"DROP TRIGGER IF EXISTS {m.Groups[1].Value};");
|
||||
}
|
||||
}
|
||||
|
||||
sb.Append($"DROP TABLE IF EXISTS {tableName};");
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves the SQL statements used to create all indexes for the specified table in a SQLite database.
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user