From 645d41bdb38b03556cc6ffd1500aa49f76505b65 Mon Sep 17 00:00:00 2001 From: Russ Kollmansberger Date: Fri, 5 Sep 2025 20:22:19 -0500 Subject: [PATCH] Made SqlBuilder methods async --- DbToolsTester/Forms/MainForm.cs | 4 +- SqlBuilder.cs | 126 +++++++++++++++++--------------- 2 files changed, 69 insertions(+), 61 deletions(-) diff --git a/DbToolsTester/Forms/MainForm.cs b/DbToolsTester/Forms/MainForm.cs index 8ee03e5..45de827 100644 --- a/DbToolsTester/Forms/MainForm.cs +++ b/DbToolsTester/Forms/MainForm.cs @@ -55,7 +55,7 @@ namespace DbToolsTester.Forms { return null; } - private void ToolbarItemClicked(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { + private async void ToolbarItemClicked(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { if (e.Item.Equals(bbiDelta)) { SQLiteConnection db1 = new SQLiteConnection("Data Source=" + Database1File + ";Version=3;"); SQLiteConnection db2 = new SQLiteConnection("Data Source=" + Database2File + ";Version=3;"); @@ -85,7 +85,7 @@ namespace DbToolsTester.Forms { if (frm.ShowDialog(this) == DialogResult.OK) { using (var cn = new SQLiteConnection("Data Source=" + (frm.SelectedDatabase == Path.GetFileName(Database1File) ? Database1File : Database2File) + ";Version=3;")) { SqlBuilder builder = new SqlBuilder(); - string sql = builder.GetTableCreateSql(cn, frm.SelectedTable); + string sql = await builder.GetTableCreateSqlAsync(cn, frm.SelectedTable); if (!string.IsNullOrEmpty(sql)) { deltaSql.Text = sql; } else { diff --git a/SqlBuilder.cs b/SqlBuilder.cs index 30c6009..9919920 100644 --- a/SqlBuilder.cs +++ b/SqlBuilder.cs @@ -4,6 +4,7 @@ using System.Data; using System.Diagnostics; using System.Text; using System.Text.RegularExpressions; +using System.Threading.Tasks; namespace DbTools { public class SqlBuilder { @@ -52,7 +53,7 @@ namespace DbTools { /// optionally its data. Returns an empty string if the table's schema cannot be determined. /// Thrown if is or if is or empty. - public string GetTableCreateSql(IDbConnection cn, string tableName, string selectStatement = null) { + public async Task GetTableCreateSqlAsync(IDbConnection cn, string tableName, string selectStatement = null) { if (cn == null) { throw new ArgumentNullException("cn"); } @@ -86,14 +87,14 @@ namespace DbTools { sb.AppendLine("\t" + columns.Trim()); sb.AppendLine(sql.Substring(startIndex + length) + ";"); - string indexes = GetIndexCreateSql(cn, tableName); + string indexes = await GetIndexCreateSqlAsync(cn, tableName); if (!string.IsNullOrEmpty(indexes)) { sb.AppendLine(); sb.AppendLine("-- INDEXES --"); sb.AppendLine(indexes); } - string triggers = GetTriggerCreateSql(cn, tableName); + string triggers = await GetTriggerCreateSqlAsync(cn, tableName); if (!string.IsNullOrEmpty(triggers)) { sb.AppendLine(); sb.AppendLine("-- TRIGGERS --"); @@ -101,7 +102,7 @@ namespace DbTools { } if (!string.IsNullOrEmpty(selectStatement)) { - string data = GetTableDataSql(cn, tableName, selectStatement); + string data = await GetTableDataSqlAsync(cn, tableName, selectStatement); if (!string.IsNullOrEmpty(data)) { sb.AppendLine(); sb.AppendLine("-- DATA --"); @@ -125,7 +126,7 @@ namespace DbTools { /// A string containing the SQL statements for creating all indexes on the specified table, separated by /// newlines. Returns an empty string if no indexes are found. /// Thrown if is null or if is null or empty. - public string GetIndexCreateSql(IDbConnection cn, string tableName) { + public async Task GetIndexCreateSqlAsync(IDbConnection cn, string tableName) { if (cn == null) { throw new ArgumentNullException("cn"); } @@ -135,16 +136,19 @@ namespace DbTools { if (cn.State != ConnectionState.Open) { cn.Open(); } - using (IDbCommand cmd = cn.CreateCommand()) { - cmd.CommandText = $"SELECT sql FROM sqlite_master WHERE type='index' AND tbl_name='{tableName}' AND sql NOT NULL;"; - StringBuilder sb = new StringBuilder(); - using (IDataReader reader = cmd.ExecuteReader()) { - while (reader.Read()) { - sb.AppendLine(reader.GetString(0) + ";"); + StringBuilder sb = new StringBuilder(); + + await Task.Run(() => { + using (IDbCommand cmd = cn.CreateCommand()) { + cmd.CommandText = $"SELECT sql FROM sqlite_master WHERE type='index' AND tbl_name='{tableName}' AND sql NOT NULL;"; + using (IDataReader reader = cmd.ExecuteReader()) { + while (reader.Read()) { + sb.AppendLine(reader.GetString(0) + ";"); + } } } - return sb.ToString(); - } + }); + return sb.ToString(); } /// @@ -159,7 +163,7 @@ namespace DbTools { /// A string containing the SQL definitions of all triggers for the specified table, separated by semicolons. /// Returns an empty string if no triggers are found. /// Thrown if is null or if is null or empty. - public string GetTriggerCreateSql(IDbConnection cn, string tableName) { + public async Task GetTriggerCreateSqlAsync(IDbConnection cn, string tableName) { if (cn == null) { throw new ArgumentNullException("cn"); } @@ -169,16 +173,18 @@ namespace DbTools { if (cn.State != ConnectionState.Open) { cn.Open(); } - using (IDbCommand cmd = cn.CreateCommand()) { - cmd.CommandText = $"SELECT sql FROM sqlite_master WHERE type='trigger' AND tbl_name='{tableName}' AND sql NOT NULL;"; - StringBuilder sb = new StringBuilder(); - using (IDataReader reader = cmd.ExecuteReader()) { - while (reader.Read()) { - sb.AppendLine(reader.GetString(0) + ";"); + StringBuilder sb = new StringBuilder(); + await Task.Run(() => { + using (IDbCommand cmd = cn.CreateCommand()) { + cmd.CommandText = $"SELECT sql FROM sqlite_master WHERE type='trigger' AND tbl_name='{tableName}' AND sql NOT NULL;"; + using (IDataReader reader = cmd.ExecuteReader()) { + while (reader.Read()) { + sb.AppendLine(reader.GetString(0) + ";"); + } } } - return sb.ToString(); - } + }); + return sb.ToString(); } /// @@ -196,7 +202,7 @@ namespace DbTools { /// A string containing the generated SQL script. The script includes INSERT statements for the data retrieved /// by the . /// Thrown if is null or is null or empty. - public string GetTableDataSql(IDbConnection cn, string tableName, string selectStatement) { + public async Task GetTableDataSqlAsync(IDbConnection cn, string tableName, string selectStatement) { if (cn == null) { throw new ArgumentNullException("cn"); } @@ -208,48 +214,50 @@ namespace DbTools { cn.Open(); } + StringBuilder sb = new StringBuilder(); selectStatement = selectStatement.Replace("$table", tableName); - using (IDbCommand cmd = cn.CreateCommand()) { - cmd.CommandText = selectStatement; - using (IDataReader reader = cmd.ExecuteReader()) { - StringBuilder sb = new StringBuilder(); - int rowCount = 0; - while (reader.Read()) { - List values = new List(); - for (int i = 0; i < reader.FieldCount; i++) { - object value = DBNull.Value; - try { - value = reader.GetValue(i); - } catch { } - if (value == DBNull.Value) { - values.Add("NULL"); - } else if (value is string || value is DateTime) { - values.Add("'" + value.ToString().Replace("'", "''") + "'"); - } else if (value is bool) { - values.Add((bool)value ? "1" : "0"); - } else { - values.Add(value.ToString()); + await Task.Run(() => { + using (IDbCommand cmd = cn.CreateCommand()) { + cmd.CommandText = selectStatement; + using (IDataReader reader = cmd.ExecuteReader()) { + int rowCount = 0; + while (reader.Read()) { + List values = new List(); + for (int i = 0; i < reader.FieldCount; i++) { + object value = DBNull.Value; + try { + value = reader.GetValue(i); + } catch { } + if (value == DBNull.Value) { + values.Add("NULL"); + } else if (value is string || value is DateTime) { + values.Add("'" + value.ToString().Replace("'", "''") + "'"); + } else if (value is bool) { + values.Add((bool)value ? "1" : "0"); + } else { + values.Add(value.ToString()); + } } - } - if (rowCount % 100 == 0) { - if (rowCount != 0) - sb.AppendLine(";"); - sb.Append($"INSERT INTO {tableName} ({string.Join(", ", GetColumnNames(reader))}) VALUES"); - sb.AppendLine(); - sb.Append($" ({string.Join(", ", values)})"); - } else { - sb.AppendLine(","); - sb.Append($" ({string.Join(", ", values)})"); + if (rowCount % 100 == 0) { + if (rowCount != 0) + sb.AppendLine(";"); + sb.Append($"INSERT INTO {tableName} ({string.Join(", ", GetColumnNames(reader))}) VALUES"); + sb.AppendLine(); + sb.Append($" ({string.Join(", ", values)})"); + } else { + sb.AppendLine(","); + sb.Append($" ({string.Join(", ", values)})"); + } + rowCount++; } - rowCount++; + if (rowCount > 0) + sb.AppendLine(";"); + } - if (rowCount > 0) - sb.AppendLine(";"); - - return sb.ToString(); } - } + }); + return sb.ToString(); } ///