public static DbManager Create(GenTablesOptions options) { SqlConnection connection; try { connection = new SqlConnection(options.ConnectionString); } catch (ArgumentException e) { throw new SqExpressCodeGenException($"MsSQL connection string has incorrect format \"{options.ConnectionString}\"", e); } if (string.IsNullOrEmpty(connection.Database)) { throw new SqExpressCodeGenException("MsSQL connection string has to contain \"database\" attribute"); } try { var database = new SqDatabase <SqlConnection>(connection, ConnectionFactory, new TSqlExporter(SqlBuilderOptions.Default)); return(new DbManager(new MsSqlDbManager(database, connection.Database), connection, options)); } catch { connection.Dispose(); throw; }
private static async Task RunPostgreSql(string connectionString) { Console.WriteLine("Running on PostgreSQL database..."); int commandCounter = 0; DbCommand NpgsqlCommandFactory(NpgsqlConnection connection, string sqlText) { Console.WriteLine($"Command #{++commandCounter}"); Console.WriteLine(sqlText); Console.WriteLine(); return(new NpgsqlCommand(sqlText, connection)); } using (var connection = new NpgsqlConnection(connectionString)) { if (!await CheckConnection(connection, "PostgreSQL")) { return; } using (var database = new SqDatabase <NpgsqlConnection>( connection: connection, commandFactory: NpgsqlCommandFactory, sqlExporter: new PgSqlExporter(builderOptions: SqlBuilderOptions.Default .WithSchemaMap(schemaMap: new[] { new SchemaMap(@from: "dbo", to: "public") })))) { await Script(database : database, isMsSql : false); } } }
private static async Task RunMsSql(string connectionString) { Console.WriteLine("Running on MsSQL database..."); int commandCounter = 0; DbCommand SqlCommandFactory(SqlConnection connection, string sqlText) { Console.WriteLine($"Command #{++commandCounter}"); Console.WriteLine(sqlText); Console.WriteLine(); return(new SqlCommand(sqlText, connection)); } using (var connection = new SqlConnection(connectionString)) { if (!await CheckConnection(connection, "MsSQL")) { return; } await connection.OpenAsync(); using (var database = new SqDatabase <SqlConnection>( connection: connection, commandFactory: SqlCommandFactory, sqlExporter: TSqlExporter.Default)) { await Script(database, isMsSql : true); } } }
private static async Task RunMySql(string connectionString) { Console.WriteLine("Running on MySQL database..."); int commandCounter = 0; DbCommand MySqlCommandFactory(MySqlConnection connection, string sqlText) { Console.WriteLine($"Command #{++commandCounter}"); Console.WriteLine(sqlText); Console.WriteLine(); return(new MySqlCommand(sqlText, connection)); } using (var connection = new MySqlConnection(connectionString)) { if (!await CheckConnection(connection, "MySQL")) { return; } using (var database = new SqDatabase <MySqlConnection>( connection: connection, commandFactory: MySqlCommandFactory, sqlExporter: new MySqlExporter(builderOptions: SqlBuilderOptions.Default))) { await Script(database : database, isMsSql : false); } } }
private static async Task ExecNpgSql(IScenario scenario, string connectionString) { var connection = new NpgsqlConnection(connectionString); using var database = new SqDatabase <NpgsqlConnection>( connection, (conn, sql) => new NpgsqlCommand(sql, conn), new PgSqlExporter(SqlBuilderOptions.Default.WithSchemaMap(new[] { new SchemaMap("dbo", "public") })), disposeConnection: true); await scenario.Exec(new ScenarioContext(database, SqlDialect.PgSql)); }
private static async Task ExecMsSql(IScenario scenario, string connectionString) { var connection = new SqlConnection(connectionString); using var database = new SqDatabase <SqlConnection>( connection, (conn, sql) => new SqlCommand(sql, conn), TSqlExporter.Default, disposeConnection: true); await scenario.Exec(new ScenarioContext(database, SqlDialect.TSql)); }