Removed async methods (except when getting data) and versioned up NuGet
This commit is contained in:
@@ -155,14 +155,14 @@ namespace DbTools {
|
||||
sb.AppendLine("\t" + columns.Trim());
|
||||
sb.AppendLine(sql.Substring(startIndex + length) + ";");
|
||||
|
||||
string indexes = await GetIndexCreateSqlAsync(cn, tableName);
|
||||
string indexes = GetIndexCreateSql(cn, tableName);
|
||||
if (!string.IsNullOrEmpty(indexes)) {
|
||||
sb.AppendLine();
|
||||
sb.AppendLine("-- INDEXES --");
|
||||
sb.AppendLine(indexes);
|
||||
}
|
||||
|
||||
string triggers = await GetTriggerCreateSqlAsync(cn, tableName);
|
||||
string triggers = GetTriggerCreateSql(cn, tableName);
|
||||
if (!string.IsNullOrEmpty(triggers)) {
|
||||
sb.AppendLine();
|
||||
sb.AppendLine("-- TRIGGERS --");
|
||||
@@ -183,6 +183,60 @@ namespace DbTools {
|
||||
}
|
||||
}
|
||||
|
||||
public string GetTableCreateSql(IDbConnection cn, string tableName) {
|
||||
if (cn == null) {
|
||||
throw new ArgumentNullException("cn");
|
||||
}
|
||||
if (string.IsNullOrEmpty(tableName)) {
|
||||
throw new ArgumentNullException("tableName");
|
||||
}
|
||||
|
||||
if (cn.State != ConnectionState.Open) {
|
||||
cn.Open();
|
||||
}
|
||||
using (IDbCommand cmd = cn.CreateCommand()) {
|
||||
cmd.CommandText = $"select sql from sqlite_master where tbl_name='{tableName}' and type='table'";
|
||||
using (IDataReader reader = cmd.ExecuteReader()) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
reader.Read();
|
||||
string sql = reader.GetString(0);
|
||||
|
||||
Match m = Regex.Match(sql, "CREATE TABLE \\S+ \\((.*)\\)", RegexOptions.Singleline);
|
||||
if (!m.Success) {
|
||||
Trace.TraceWarning("Unable to match regex on table " + tableName);
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
sb.AppendLine("-- TABLE " + tableName + " --");
|
||||
int startIndex = m.Groups[1].Index;
|
||||
int length = m.Groups[1].Length;
|
||||
string columns = Regex.Replace(m.Groups[1].Value, "\\s{2,}", " ");
|
||||
columns = Regex.Replace(columns.Replace(", ", ",").Replace(",\n", ","), ",(?!\\d+\\))", ",\r\n\t");
|
||||
|
||||
sb.AppendLine(sql.Substring(0, startIndex));
|
||||
sb.AppendLine("\t" + columns.Trim());
|
||||
sb.AppendLine(sql.Substring(startIndex + length) + ";");
|
||||
|
||||
string indexes = GetIndexCreateSql(cn, tableName);
|
||||
if (!string.IsNullOrEmpty(indexes)) {
|
||||
sb.AppendLine();
|
||||
sb.AppendLine("-- INDEXES --");
|
||||
sb.AppendLine(indexes);
|
||||
}
|
||||
|
||||
string triggers = GetTriggerCreateSql(cn, tableName);
|
||||
if (!string.IsNullOrEmpty(triggers)) {
|
||||
sb.AppendLine();
|
||||
sb.AppendLine("-- TRIGGERS --");
|
||||
sb.AppendLine(triggers);
|
||||
}
|
||||
|
||||
sb.AppendLine("-- END TABLE " + tableName + " --");
|
||||
return sb.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public string GetTableDropSql(IDbConnection cn, string tableName) {
|
||||
if (cn == null) {
|
||||
@@ -193,7 +247,7 @@ namespace DbTools {
|
||||
}
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
string[] indexes = GetIndexCreateSqlAsync(cn, tableName).Result.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries);
|
||||
string[] indexes = GetIndexCreateSql(cn, tableName).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) {
|
||||
@@ -201,7 +255,7 @@ namespace DbTools {
|
||||
}
|
||||
}
|
||||
|
||||
string[] triggers = GetTriggerCreateSqlAsync(cn, tableName).Result.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries);
|
||||
string[] triggers = GetTriggerCreateSql(cn, tableName).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) {
|
||||
@@ -224,7 +278,7 @@ namespace DbTools {
|
||||
/// <returns>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.</returns>
|
||||
/// <exception cref="ArgumentNullException">Thrown if <paramref name="cn"/> is null or if <paramref name="tableName"/> is null or empty.</exception>
|
||||
public async Task<string> GetIndexCreateSqlAsync(IDbConnection cn, string tableName) {
|
||||
public string GetIndexCreateSql(IDbConnection cn, string tableName) {
|
||||
if (cn == null) {
|
||||
throw new ArgumentNullException("cn");
|
||||
}
|
||||
@@ -236,16 +290,14 @@ namespace DbTools {
|
||||
}
|
||||
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) + ";");
|
||||
}
|
||||
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();
|
||||
}
|
||||
|
||||
@@ -261,7 +313,7 @@ namespace DbTools {
|
||||
/// <returns>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.</returns>
|
||||
/// <exception cref="ArgumentNullException">Thrown if <paramref name="cn"/> is null or if <paramref name="tableName"/> is null or empty.</exception>
|
||||
public async Task<string> GetTriggerCreateSqlAsync(IDbConnection cn, string tableName) {
|
||||
public string GetTriggerCreateSql(IDbConnection cn, string tableName) {
|
||||
if (cn == null) {
|
||||
throw new ArgumentNullException("cn");
|
||||
}
|
||||
@@ -272,16 +324,14 @@ namespace DbTools {
|
||||
cn.Open();
|
||||
}
|
||||
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) + ";");
|
||||
}
|
||||
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();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user