Skip to content

Commit

Permalink
pop to .NET 7.0
Browse files Browse the repository at this point in the history
  • Loading branch information
wooly905 committed May 8, 2023
1 parent 982e3a6 commit ceee073
Show file tree
Hide file tree
Showing 16 changed files with 135 additions and 96 deletions.
2 changes: 1 addition & 1 deletion PRS/Commands/CommandHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static async Task<string> GetConnectionStringAsync(IDisplay display, IFil
}

IFileReader reader = fileProvider.GetFileReader(Global.ConnectionStringFilePath);
string output = await reader.ReadLineAsync().ConfigureAwait(false);
string output = await reader.ReadLineAsync();
reader.Dispose();

return output;
Expand Down
34 changes: 18 additions & 16 deletions PRS/Commands/DumpDatabaseSchemaCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,27 @@ public async Task RunAsync(string[] args)
}

// run database tool to get data
string connectionString = await CommandHelper.GetConnectionStringAsync(_display, _fileProvider).ConfigureAwait(false);
string connectionString = await CommandHelper.GetConnectionStringAsync(_display, _fileProvider);

if (connectionString == null)
{
_display.ShowError("Connection string is not found.");
return;
}

IEnumerable<TableModel> tables = await _database.GetTableModelsAsync(connectionString).ConfigureAwait(false);
IEnumerable<ColumnModel> columns = await _database.GetColumnModelsAsync(connectionString).ConfigureAwait(false);
IEnumerable<string> sps = await _database.GetStoredProcedureNamesAsync(connectionString).ConfigureAwait(false);
_display.ShowInfo("Starting to dump schema...");

IEnumerable<TableModel> tables = await _database.GetTableModelsAsync(connectionString);
IEnumerable<ColumnModel> columns = await _database.GetColumnModelsAsync(connectionString);
IEnumerable<string> sps = await _database.GetStoredProcedureNamesAsync(connectionString);

// insert connection string and all data to file
IFileWriter writer = _fileProvider.GetFileWriter(Path.Combine(Global.SchemaFileDirectory, Global.SchemaFileName));

await WriteConnectionStringAsync(writer, connectionString).ConfigureAwait(false);
await WriteTablesAsync(writer, tables).ConfigureAwait(false);
await WriteColumnsAsync(writer, columns).ConfigureAwait(false);
await WriteStoredProceduresAsync(writer, sps).ConfigureAwait(false);
await WriteConnectionStringAsync(writer, connectionString);
await WriteTablesAsync(writer, tables);
await WriteColumnsAsync(writer, columns);
await WriteStoredProceduresAsync(writer, sps);

writer.Dispose();

Expand All @@ -63,39 +65,39 @@ public async Task RunAsync(string[] args)

private async Task WriteConnectionStringAsync(IFileWriter writer, string connectionString)
{
await writer.WriteLineAsync(Global.ConnectionStringSectionName).ConfigureAwait(false);
await writer.WriteLineAsync(connectionString).ConfigureAwait(false);
await writer.WriteLineAsync(Global.ConnectionStringSectionName);
await writer.WriteLineAsync(connectionString);
}

private async Task WriteTablesAsync(IFileWriter writer, IEnumerable<TableModel> tables)
{
await writer.WriteLineAsync(Global.TableSectionName).ConfigureAwait(false);
await writer.WriteLineAsync(Global.TableSectionName);

foreach (TableModel m in tables)
{
string s = $"{m.TableSchema},{m.TableName},{m.TableType}";
await writer.WriteLineAsync(s).ConfigureAwait(false);
await writer.WriteLineAsync(s);
}
}

private async Task WriteColumnsAsync(IFileWriter writer, IEnumerable<ColumnModel> columns)
{
await writer.WriteLineAsync(Global.ColumnSectionName).ConfigureAwait(false);
await writer.WriteLineAsync(Global.ColumnSectionName);

foreach (ColumnModel m in columns)
{
string s = $"{m.TableSchema},{m.TableName},{m.ColumnName},{m.OrdinalPosition},{m.ColumnDefault},{m.IsNullable},{m.DataType},{m.CharacterMaximumLength}";
await writer.WriteLineAsync(s).ConfigureAwait(false);
await writer.WriteLineAsync(s);
}
}

private async Task WriteStoredProceduresAsync(IFileWriter writer, IEnumerable<string> sps)
{
await writer.WriteLineAsync(Global.StoredProcedureSectionName).ConfigureAwait(false);
await writer.WriteLineAsync(Global.StoredProcedureSectionName);

foreach (string m in sps)
{
await writer.WriteLineAsync(m).ConfigureAwait(false);
await writer.WriteLineAsync(m);
}
}
}
24 changes: 13 additions & 11 deletions PRS/Commands/FindColumnCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public async Task RunAsync(string[] args)

while (true)
{
string line = await reader.ReadLineAsync().ConfigureAwait(false);
string line = await reader.ReadLineAsync();

if (line == null)
{
Expand All @@ -68,7 +68,7 @@ public async Task RunAsync(string[] args)

while (true)
{
string line = await reader.ReadLineAsync().ConfigureAwait(false);
string line = await reader.ReadLineAsync();

if (line == null)
{
Expand All @@ -86,15 +86,17 @@ public async Task RunAsync(string[] args)

if (splits[2]?.IndexOf(args[1], StringComparison.OrdinalIgnoreCase) >= 0)
{
ColumnModel m = new();
m.TableSchema = splits[0];
m.TableName = splits[1];
m.ColumnName = splits[2];
m.OrdinalPosition = splits[3];
m.ColumnDefault = splits[4];
m.IsNullable = splits[5];
m.DataType = splits[6];
m.CharacterMaximumLength = splits[7];
ColumnModel m = new()
{
TableSchema = splits[0],
TableName = splits[1],
ColumnName = splits[2],
OrdinalPosition = splits[3],
ColumnDefault = splits[4],
IsNullable = splits[5],
DataType = splits[6],
CharacterMaximumLength = splits[7]
};
models.Add(m);
}
}
Expand Down
4 changes: 2 additions & 2 deletions PRS/Commands/FindStoredProcedureCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public async Task RunAsync(string[] args)

while (true)
{
string line = await reader.ReadLineAsync().ConfigureAwait(false);
string line = await reader.ReadLineAsync();

if (line == null)
{
Expand All @@ -67,7 +67,7 @@ public async Task RunAsync(string[] args)

while (true)
{
string line = await reader.ReadLineAsync().ConfigureAwait(false);
string line = await reader.ReadLineAsync();

if (line == null)
{
Expand Down
24 changes: 13 additions & 11 deletions PRS/Commands/FindTableColumnCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public async Task RunAsync(string[] args)

while (true)
{
string line = await reader.ReadLineAsync().ConfigureAwait(false);
string line = await reader.ReadLineAsync();

if (line == null)
{
Expand All @@ -68,7 +68,7 @@ public async Task RunAsync(string[] args)

while (true)
{
string line = await reader.ReadLineAsync().ConfigureAwait(false);
string line = await reader.ReadLineAsync();

if (line == null)
{
Expand All @@ -87,15 +87,17 @@ public async Task RunAsync(string[] args)
if (splits[1]?.IndexOf(args[1], StringComparison.OrdinalIgnoreCase) >= 0
&& splits[2]?.IndexOf(args[2], StringComparison.OrdinalIgnoreCase) >= 0)
{
ColumnModel m = new();
m.TableSchema = splits[0];
m.TableName = splits[1];
m.ColumnName = splits[2];
m.OrdinalPosition = splits[3];
m.ColumnDefault = splits[4];
m.IsNullable = splits[5];
m.DataType = splits[6];
m.CharacterMaximumLength = splits[7];
ColumnModel m = new()
{
TableSchema = splits[0],
TableName = splits[1],
ColumnName = splits[2],
OrdinalPosition = splits[3],
ColumnDefault = splits[4],
IsNullable = splits[5],
DataType = splits[6],
CharacterMaximumLength = splits[7]
};
models.Add(m);
}
}
Expand Down
14 changes: 8 additions & 6 deletions PRS/Commands/FindTableCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public async Task RunAsync(string[] args)

while (true)
{
string line = await reader.ReadLineAsync().ConfigureAwait(false);
string line = await reader.ReadLineAsync();

if (line == null)
{
Expand All @@ -68,7 +68,7 @@ public async Task RunAsync(string[] args)

while (true)
{
string line = await reader.ReadLineAsync().ConfigureAwait(false);
string line = await reader.ReadLineAsync();

if (line == null)
{
Expand All @@ -86,10 +86,12 @@ public async Task RunAsync(string[] args)

if (splits[1]?.IndexOf(args[1], StringComparison.OrdinalIgnoreCase) >= 0)
{
TableModel m = new();
m.TableSchema = splits[0];
m.TableName = splits[1];
m.TableType = splits[2];
TableModel m = new()
{
TableSchema = splits[0],
TableName = splits[1],
TableType = splits[2]
};
models.Add(m);
}
}
Expand Down
24 changes: 13 additions & 11 deletions PRS/Commands/ShowAllColumnsCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public async Task RunAsync(string[] args)

while (true)
{
string line = await reader.ReadLineAsync().ConfigureAwait(false);
string line = await reader.ReadLineAsync();

if (line == null)
{
Expand All @@ -68,7 +68,7 @@ public async Task RunAsync(string[] args)

while (true)
{
string line = await reader.ReadLineAsync().ConfigureAwait(false);
string line = await reader.ReadLineAsync();

if (line == null)
{
Expand All @@ -86,15 +86,17 @@ public async Task RunAsync(string[] args)

if (string.Equals(splits[1], args[1], StringComparison.OrdinalIgnoreCase))
{
ColumnModel m = new();
m.TableSchema = splits[0];
m.TableName = splits[1];
m.ColumnName = splits[2];
m.OrdinalPosition = splits[3];
m.ColumnDefault = splits[4];
m.IsNullable = splits[5];
m.DataType = splits[6];
m.CharacterMaximumLength = splits[7];
ColumnModel m = new()
{
TableSchema = splits[0],
TableName = splits[1],
ColumnName = splits[2],
OrdinalPosition = splits[3],
ColumnDefault = splits[4],
IsNullable = splits[5],
DataType = splits[6],
CharacterMaximumLength = splits[7]
};
models.Add(m);
}
}
Expand Down
2 changes: 1 addition & 1 deletion PRS/Commands/ShowConnectionStringCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public ShowConnectionStringCommand(IDisplay display, IFileProvider fileProvider)
public async Task RunAsync(string[] args)
{
// verify schema file exists. if not, show no schema file error and ask to run dump command.
string output = await CommandHelper.GetConnectionStringAsync(_display, _fileProvider).ConfigureAwait(false);
string output = await CommandHelper.GetConnectionStringAsync(_display, _fileProvider);

if (output == null)
{
Expand Down
2 changes: 1 addition & 1 deletion PRS/Commands/WriteConnectionStringCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public async Task RunAsync(string[] args)
}

IFileWriter writer = _fileProvider.GetFileWriter(Global.ConnectionStringFilePath);
await writer.WriteLineAsync(cs).ConfigureAwait(false);
await writer.WriteLineAsync(cs);
writer.Dispose();

_display.ShowInfo("Connection string has been set.");
Expand Down
46 changes: 29 additions & 17 deletions PRS/Database/PrsDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,36 @@ namespace PRS.Database;

internal class PrsDatabase : IDatabase
{
//private readonly string _connectionString;

public PrsDatabase()
{
// _connectionString = connectionString;
}

public async Task<IEnumerable<ColumnModel>> GetColumnModelsAsync(string connectionString)
{
using SqlConnection connection = new();
connection.ConnectionString = connectionString;
await connection.OpenAsync().ConfigureAwait(false);
await connection.OpenAsync();
using SqlCommand command = connection.CreateCommand();
command.CommandText = "SELECT TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, ORDINAL_POSITION, COLUMN_DEFAULT, IS_NULLABLE, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH FROM INFORMATION_SCHEMA.COLUMNS ORDER BY TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME";
command.CommandText = """
SELECT TABLE_SCHEMA,
TABLE_NAME,
COLUMN_NAME,
ORDINAL_POSITION,
COLUMN_DEFAULT,
IS_NULLABLE,
DATA_TYPE,
CHARACTER_MAXIMUM_LENGTH
FROM INFORMATION_SCHEMA.COLUMNS
ORDER BY TABLE_SCHEMA,
TABLE_NAME,
COLUMN_NAME
""";
command.CommandType = CommandType.Text;
using SqlDataReader reader = await command.ExecuteReaderAsync().ConfigureAwait(false);
using SqlDataReader reader = await command.ExecuteReaderAsync();

List<ColumnModel> columns = new();

while (await reader.ReadAsync().ConfigureAwait(false))
while (await reader.ReadAsync())
{
ColumnModel m = new();
m.TableSchema = reader[0].ToString();
Expand All @@ -50,15 +60,15 @@ public async Task<IEnumerable<string>> GetStoredProcedureNamesAsync(string conne
{
using SqlConnection connection = new();
connection.ConnectionString = connectionString;
await connection.OpenAsync().ConfigureAwait(false);
await connection.OpenAsync();
using SqlCommand command = connection.CreateCommand();
command.CommandText = "SELECT Name FROM SYSOBJECTS WHERE type = 'P' AND category = 0 ORDER BY name";
command.CommandType = CommandType.Text;
using SqlDataReader reader = await command.ExecuteReaderAsync().ConfigureAwait(false);
using SqlDataReader reader = await command.ExecuteReaderAsync();

List<string> names = new();

while (await reader.ReadAsync().ConfigureAwait(false))
while (await reader.ReadAsync())
{
names.Add(reader[0].ToString());
}
Expand All @@ -73,20 +83,22 @@ public async Task<IEnumerable<TableModel>> GetTableModelsAsync(string connection
{
using SqlConnection connection = new();
connection.ConnectionString = connectionString;
await connection.OpenAsync().ConfigureAwait(false);
await connection.OpenAsync();
using SqlCommand command = connection.CreateCommand();
command.CommandText = "SELECT TABLE_SCHEMA, TABLE_NAME, TABLE_TYPE FROM INFORMATION_SCHEMA.TABLES ORDER BY TABLE_NAME";
command.CommandType = CommandType.Text;
using SqlDataReader reader = await command.ExecuteReaderAsync().ConfigureAwait(false);
using SqlDataReader reader = await command.ExecuteReaderAsync();

List<TableModel> tables = new();

while (await reader.ReadAsync().ConfigureAwait(false))
while (await reader.ReadAsync())
{
TableModel m = new();
m.TableSchema = reader[0].ToString();
m.TableName = reader[1].ToString();
m.TableType = reader[2].ToString();
TableModel m = new()
{
TableSchema = reader[0].ToString(),
TableName = reader[1].ToString(),
TableType = reader[2].ToString()
};
tables.Add(m);
}

Expand Down
Loading

0 comments on commit ceee073

Please sign in to comment.