/// <summary> /// <paramref name="npgsqlCommand"/>를 실행하여, 결과 셋을 Persistent Object의 컬렉션으로 매핑합니다. /// </summary> /// <typeparam name="T">Persistent Object의 수형</typeparam> /// <param name="npgsqlDatabase">NpgsqlDatabase 인스턴스</param> /// <param name="npgsqlCommand">npgsqlCommand 인스턴스</param> /// <param name="mapObjectFactory">PersistentObject 생성 Factory</param> /// <param name="nameMap">NameMap 인스턴스</param> /// <param name="firstResult">첫번째 결과 인덱스 (0부터 시작)</param> /// <param name="maxResults">최대 결과 갯수</param> /// <param name="additionalMapping">부가적인 매핑 작업을 수행할 델리게이트</param> /// <param name="parameters">OracleCommand에 설정할 Parameter 정보</param> /// <returns>DataReader로부터 인스턴싱된 Persistent Object의 컬렉션을 결과로 반환하는 Task</returns> public static Task <IList <T> > ExecuteMapObject <T>(this NpgsqlDatabase npgsqlDatabase, NpgsqlCommand npgsqlCommand, Func <T> mapObjectFactory, INameMap nameMap, int firstResult, int maxResults, Action <IDataReader, T> additionalMapping, params IAdoParameter[] parameters) { npgsqlCommand.ShouldNotBeNull("npgsqlCommand"); if (IsDebugEnabled) { log.Debug("DataReader를 열어, 수형[{2}]로 빌드합니다... CommandText=[{0}], Parameters=[{1}]", npgsqlCommand.CommandText, npgsqlCommand.Parameters.CollectionToString(), typeof(T).FullName); } return (ExecuteReaderAsync(npgsqlDatabase, npgsqlCommand, parameters) .ContinueWith(task => { using (var reader = task.Result) return AdoTool.Map <T>(reader, mapObjectFactory, nameMap, firstResult, maxResults, additionalMapping); }, TaskContinuationOptions.ExecuteSynchronously)); }
/// <summary> /// <paramref name="cmd"/> 를 비동기 방식으로 실행하여, <see cref="Task{NpgsqlDataReader}"/>를 반환받습니다. /// 받환받은 DataReader는 꼭 Dispose() 해 주어야 Connection이 닫힙니다. /// </summary> /// <param name="db">DAAB의 MySQL 용 Database</param> /// <param name="cmd">실행할 <see cref="NpgsqlCommand"/> 인스턴스</param> /// <param name="parameters">Command Parameters</param> /// <returns><see cref="NpgsqlDataReader"/>를 결과로 반환하는 <see cref="Task"/></returns> public static Task <NpgsqlDataReader> ExecuteReaderAsync(this NpgsqlDatabase db, NpgsqlCommand cmd, params IAdoParameter[] parameters) { cmd.ShouldNotBeNull("cmd"); if (IsDebugEnabled) { log.Debug("NpgsqlCommand.ExecuteReader를 비동기 방식으로 실행합니다. CommandText=[{0}], Parameters=[{1}]", cmd.CommandText, parameters.CollectionToString()); } var newConnectionCreated = false; if (cmd.Connection == null) { cmd.Connection = db.CreateNpgsqlConnection(ref newConnectionCreated); } if (parameters != null) { AdoTool.SetParameterValues(db, cmd, parameters); } var commandBehavior = newConnectionCreated ? CommandBehavior.CloseConnection : CommandBehavior.Default; return(Task.Factory.StartNew(() => cmd.ExecuteReader(commandBehavior), TaskCreationOptions.PreferFairness)); }
/// <summary> /// <paramref name="spName"/> 를 비동기 방식으로 실행하여, <see cref="Task{NpgsqlDataReader}"/>를 반환받습니다. /// 받환받은 DataReader는 꼭 Dispose() 해 주어야 Connection이 닫힙니다. /// </summary> /// <param name="db">DAAB의 MySQL 용 Database</param> /// <param name="spName">실행할 Procedure 명</param> /// <param name="parameters">Command Parameters</param> /// <returns><see cref="NpgsqlDataReader"/>를 결과로 반환하는 <see cref="Task"/></returns> public static Task <NpgsqlDataReader> ExecuteReaderByProcedureAsync(this NpgsqlDatabase db, string spName, params IAdoParameter[] parameters) { var cmd = GetProcedureNpgsqlCommand(db, spName, AdoTool.DEFAULT_DISCOVER_PARAMETER); return(ExecuteReaderAsync(db, cmd, parameters)); }
/// <summary> /// 쿼리 문 또는 Procedure Name을 실행할 <see cref="NpgsqlCommand"/>를 생성합니다. /// </summary> /// <param name="db">DAAB NpgsqlDatabase</param> /// <param name="query">수행할 쿼리문 또는 Procedure Name</param> /// <param name="discoverParams">Procedure일 경우 Parameter 빌드</param> /// <returns>생성한 <see cref="NpgsqlCommand"/></returns> /// <seealso cref="AdoRepositoryImplBase.GetCommand(string,bool)"/> public static NpgsqlCommand GetNpgsqlCommand(this NpgsqlDatabase db, string query, bool discoverParams) { query.ShouldNotBeWhiteSpace("query"); return((AdoTool.IsSqlString(query)) ? GetSqlStringNpgsqlCommand(db, query) : GetProcedureNpgsqlCommand(db, query, discoverParams)); }
/// <summary> /// <paramref name="sqlString"/> 를 비동기 방식으로 실행하여, <see cref="Task{NpgsqlDataReader}"/>를 반환받습니다. /// 받환받은 DataReader는 꼭 Dispose() 해 주어야 Connection이 닫힙니다. /// </summary> /// <param name="db">DAAB의 MySQL 용 Database</param> /// <param name="sqlString">실행할 SQL 문</param> /// <param name="parameters">Command Parameters</param> /// <returns><see cref="NpgsqlDataReader"/>를 결과로 반환하는 <see cref="Task"/></returns> public static Task <NpgsqlDataReader> ExecuteReaderBySqlStringAsync(this NpgsqlDatabase db, string sqlString, params IAdoParameter[] parameters) { var cmd = GetSqlStringNpgsqlCommand(db, sqlString); return(ExecuteReaderAsync(db, cmd, parameters)); }
/// <summary> /// 쿼리 문 <paramref name="sqlString"/>을 수행할 <see cref="NpgsqlCommand"/>를 생성합니다. /// </summary> /// <param name="db">DAAB NpgsqlDatabase</param> /// <param name="sqlString">수행할 쿼리문</param> /// <returns>생성한 <see cref="NpgsqlCommand"/></returns> public static NpgsqlCommand GetSqlStringNpgsqlCommand(this NpgsqlDatabase db, string sqlString) { if (IsDebugEnabled) { log.Debug("쿼리문을 수행할 NpgsqlCommand를 생성합니다. sqlString=[{0}]", sqlString); } return((NpgsqlCommand)db.GetSqlStringCommand(sqlString)); }
/// <summary> /// <paramref name="spName"/>을 ExecuteNonQuery 메소드로 비동기 실행을 하도록 하는 <see cref="Task{Int32}"/>를 빌드합니다. /// </summary> /// <param name="db">DAAB의 MySQL 용 Database</param> /// <param name="spName">실행할 프로시져 명</param> /// <param name="parameters">파리미터 정보</param> /// <returns>실행에 영향을 받은 행의 수를 결과로 가지는 <see cref="Task{Int32}"/></returns> public static Task <int> ExecuteNonQueryByProcedureAsync(this NpgsqlDatabase db, string spName, params IAdoParameter[] parameters) { var cmd = GetProcedureNpgsqlCommand(db, spName, AdoTool.DEFAULT_DISCOVER_PARAMETER); return (ExecuteNonQueryAsync(db, cmd, parameters) .ContinueWith(task => { With.TryAction(() => cmd.Dispose()); return task; }, TaskContinuationOptions.ExecuteSynchronously) .Unwrap()); }
/// <summary> /// <paramref name="query"/>을 ExecuteNonQuery 메소드로 비동기 실행을 하도록 하는 <see cref="Task{Int32}"/>를 빌드합니다. /// </summary> /// <param name="db">DAAB의 MySQL 용 Database</param> /// <param name="query">실행할 쿼리문 또는 Procecedure 명</param> /// <param name="parameters">파리미터 정보</param> /// <returns>실행에 영향을 받은 행의 수를 결과로 가지는 <see cref="Task{Int32}"/></returns> public static Task <int> ExecuteNonQueryAsync(this NpgsqlDatabase db, string query, params IAdoParameter[] parameters) { var cmd = GetNpgsqlCommand(db, query); return (ExecuteNonQueryAsync(db, cmd, parameters) .ContinueWith(task => { With.TryAction(() => cmd.Dispose()); return task; }, TaskContinuationOptions.ExecuteSynchronously) .Unwrap()); }
private static void HandleConnectionError(NpgsqlDatabase db, Exception ex, int?tryCount) { if (log.IsErrorEnabled) { log.Error("Database Connection 생성 및 Open 수행 시에 예외가 발생했습니다. ConnectionString=[{0}]", db.ConnectionString); log.Error(ex); } With.TryAction(() => NpgsqlConnection.ClearAllPools()); var timeout = Math.Min(MaxTimeout, Math.Abs(tryCount.GetValueOrDefault(1)) * 50); Thread.Sleep(timeout); }
public static HashSet <Database> Setup(Action <Database> initializer = null, string databaseName = "DaoRef") { if (initializer == null) { initializer = db => Db.TryEnsureSchema <TestTable>(db); } HashSet <Database> testDatabases = new HashSet <Database>(); MsSqlDatabase msDatabase = new MsSqlDatabase("chumsql2", databaseName, new MsSqlCredentials { UserName = "******", Password = "******" }); initializer(msDatabase); testDatabases.Add(msDatabase); SQLiteDatabase sqliteDatabase = new SQLiteDatabase(".\\Chumsql2", databaseName); initializer(sqliteDatabase); testDatabases.Add(sqliteDatabase); OleDbDatabase oleDatabase = new OleDbDatabase("Microsoft.ACE.OLEDB.12.0", databaseName.RandomLetters(4)); initializer(oleDatabase); testDatabases.Add(oleDatabase); OracleDatabase oracleDatabase = new OracleDatabase("chumsql2", databaseName, new OracleCredentials { UserId = "C##ORACLEUSER", Password = "******" }); initializer(oracleDatabase); testDatabases.Add(oracleDatabase); MySqlDatabase mySqlDatabase = new MySqlDatabase("chumsql2", databaseName, new MySqlCredentials { UserId = "mysql", Password = "******" }); initializer(mySqlDatabase); testDatabases.Add(mySqlDatabase); NpgsqlDatabase npgsqlDatabase = new NpgsqlDatabase("chumsql2", databaseName, new NpgsqlCredentials { UserId = "postgres", Password = "******" }); initializer(npgsqlDatabase); testDatabases.Add(npgsqlDatabase); return(testDatabases); }
/// <summary> /// <paramref name="sqlString"/> 를 비동기 방식으로 실행하여, Scalar 값을 반환하는 <see cref="Task{Object}"/>를 빌드합니다. /// </summary> /// <param name="db">DAAB의 MySQL 용 Database</param> /// <param name="sqlString">실행할 쿼리문</param> /// <param name="parameters">Command Parameters</param> /// <returns>결과 Scalar 값을 가지는 Task의 인스턴스</returns> public static Task <object> ExecuteScalarBySqlStringAsync(this NpgsqlDatabase db, string sqlString, params IAdoParameter[] parameters) { var cmd = GetSqlStringNpgsqlCommand(db, sqlString); return (ExecuteScalarAsync(db, cmd, parameters) .ContinueWith(task => { With.TryAction(() => cmd.Dispose()); return task; }, TaskContinuationOptions.ExecuteSynchronously) .Unwrap()); }
/// <summary> /// Procedure <paramref name="spName"/>를 수행할 <see cref="NpgsqlCommand"/> 를 생성합니다. /// </summary> /// <param name="db">DAAB NpgsqlDatabase</param> /// <param name="spName">Procedure name</param> /// <param name="discoverParams">discover parameters</param> /// <returns>생성한 <see cref="NpgsqlCommand"/></returns> public static NpgsqlCommand GetProcedureNpgsqlCommand(this NpgsqlDatabase db, string spName, bool discoverParams) { if (IsDebugEnabled) { log.Debug("Procedure를 수행할 NpgsqlCommand를 생성합니다. spName=[{0}], discoverParams=[{1}]", spName, discoverParams); } var cmd = db.GetStoredProcCommand(spName); if (discoverParams) { db.DiscoverParameters(cmd); } return((NpgsqlCommand)cmd); }
/// <summary> /// <paramref name="spName"/>를 비동기 방식으로 실행하여, 결과 셋을 <see cref="Task{DataTable}"/>로 반환합니다. /// </summary> public static Task <DataTable> ExecuteDataTableAsyncByProcedure(this NpgsqlDatabase db, string spName, int firstResult, int maxResults, params IAdoParameter[] parameters) { var cmd = db.GetProcedureNpgsqlCommand(spName, AdoTool.DEFAULT_DISCOVER_PARAMETER); return (ExecuteDataTableAsync(db, cmd, firstResult, maxResults, parameters) .ContinueWith(task => { With.TryAction(() => cmd.Dispose()); return task.Result; }, TaskContinuationOptions.ExecuteSynchronously)); }
/// <summary> /// <paramref name="sqlString"/>를 비동기 방식으로 실행하여, 결과 셋을 <see cref="Task{DataTable}"/>로 반환합니다. /// </summary> public static Task <DataTable> ExecuteDataTableAsyncBySqlString(this NpgsqlDatabase db, string sqlString, int firstResult, int maxResults, params IAdoParameter[] parameters) { var cmd = db.GetSqlStringNpgsqlCommand(sqlString); return (ExecuteDataTableAsync(db, cmd, firstResult, maxResults, parameters) .ContinueWith(task => { With.TryAction(() => cmd.Dispose()); return task; }, TaskContinuationOptions.ExecuteSynchronously) .Unwrap()); }
/// <summary> /// Multi-ResultSet일 경우에 DataTable 컬렉션으로 반환합니다. /// </summary> public static Task <IList <DataTable> > ExecuteDataTableAsListAsync(this NpgsqlDatabase db, NpgsqlCommand cmd, int firstResult, int maxResults, params IAdoParameter[] parameters) { cmd.ShouldNotBeNull("cmd"); firstResult.ShouldBePositiveOrZero("firstResult"); maxResults.ShouldBePositiveOrZero("maxResults"); if (IsDebugEnabled) { log.Debug( "비동기 방식으로 ExecuteDataTableAsListAsync을 실행합니다... CommandText=[{0}], firstResult=[{1}], maxResults=[{2}], Parameters=[{3}]", cmd.CommandText, firstResult, maxResults, parameters.CollectionToString()); } return (ExecuteReaderAsync(db, cmd, parameters) .ContinueWith(task => { IList <DataTable> tables = new List <DataTable>(); using (var reader = task.Result) using (var adapter = new AdoDataAdapter(db.GetDataAdapter())) { do { var dataTable = new DataTable { Locale = CultureInfo.InvariantCulture }; adapter.Fill(new[] { dataTable }, reader, firstResult, maxResults); if (IsDebugEnabled) { log.Debug("비동기 방식으로 DataReader를 가져와 DataTable에 Load 했습니다!!!"); } tables.Add(dataTable); } while(reader.IsClosed == false && reader.NextResult()); } return tables; }, TaskContinuationOptions.ExecuteSynchronously)); }
/// <summary> /// <paramref name="cmd"/>를 비동기 방식으로 실행하여, 결과 셋을 <see cref="Task{DataTable}"/>로 반환합니다. /// </summary> public static Task <DataTable> ExecuteDataTableAsync(this NpgsqlDatabase db, NpgsqlCommand cmd, int firstResult, int maxResults, params IAdoParameter[] parameters) { cmd.ShouldNotBeNull("cmd"); firstResult.ShouldBePositiveOrZero("firstResult"); maxResults.ShouldBePositiveOrZero("maxResults"); if (IsDebugEnabled) { log.Debug("비동기 방식으로 ExecuteDataTable을 실행합니다.. CommandText=[{0}], firstResult=[{1}], maxResults=[{2}], Parameters=[{3}]", cmd.CommandText, firstResult, maxResults, parameters.CollectionToString()); } return (ExecuteReaderAsync(db, cmd, parameters) .ContinueWith(task => AdoTool.BuildDataTableFromDataReader(db, task.Result, firstResult, maxResults), TaskContinuationOptions.ExecuteSynchronously)); }
public static HashSet <Database> Setup(Action <Database> initializer = null, string databaseName = "DaoRef") { if (initializer == null) { initializer = db => Db.TryEnsureSchema <TestTable>(db); } /*MsSqlDatabase msDatabase = new MsSqlDatabase("Chumsql2", databaseName, new MsSqlCredentials { UserId = "mssqluser", Password = "******" }); * initializer(msDatabase); * _testDatabases.Add(msDatabase);*/ NpgsqlDatabase npgsqlDatabase = new NpgsqlDatabase("chumsql2", databaseName, new NpgsqlCredentials { UserId = "postgres", Password = "******" }); initializer(npgsqlDatabase); _testDatabases.Add(npgsqlDatabase); PostgresDatabase postgresDatabase = new PostgresDatabase("chumsql2", databaseName, new PostgresCredentials { UserId = "postgres", Password = "******" }); initializer(postgresDatabase); _testDatabases.Add(postgresDatabase); /*SQLiteDatabase sqliteDatabase = new SQLiteDatabase("./chumsql2", databaseName); * initializer(sqliteDatabase); * _testDatabases.Add(sqliteDatabase); * * OracleDatabase oracleDatabase = new OracleDatabase("chumsql2", databaseName, new OracleCredentials { UserId = "C##ORACLEUSER", Password = "******" }); * initializer(oracleDatabase); * _testDatabases.Add(oracleDatabase);*/ /*MySqlDatabase mySqlDatabase = new MySqlDatabase("chumsql2", databaseName, new MySqlCredentials { UserId = "mysql", Password = "******" }, false); * initializer(mySqlDatabase); * _testDatabases.Add(mySqlDatabase);*/ return(_testDatabases); }
/// <summary> /// MySQL Connection을 연결합니다. /// </summary> /// <param name="db">DAAB MySqlProvider 인스턴스</param> /// <param name="tryCount">연결 실패 시, 재 시도 횟수</param> /// <returns>새로 연결된 <see cref="NpgsqlConnection"/>의 인스턴스, 만약 연결에 실패했다면 null을 반환합니다.</returns> internal static NpgsqlConnection OpenConnection(this NpgsqlDatabase db, int tryCount) { if (IsDebugEnabled) { log.Debug("PostgreSql Database에 연결하려고 합니다..."); } NpgsqlConnection connection = null; var count = 0; for (var i = 0; i < tryCount; i++) { try { if (IsDebugEnabled) { log.Debug("NpgsqlConnection을 생성하고, 비동기 방식으로 Open합니다. 시도 횟수=[{0}]", ++count); } connection = (NpgsqlConnection)db.CreateConnection(); connection.Open(); if (connection.State == ConnectionState.Open) { if (IsDebugEnabled) { log.Debug("NpgsqlConnection을 연결했습니다!!!"); } return(connection); } } catch (Exception ex) { HandleConnectionError(db, ex, count); } } Guard.Assert(connection != null && connection.State != ConnectionState.Closed, "NpgsqlConnection을 열지 못했습니다!!!"); return(connection); }
/// <summary> /// <paramref name="cmd"/> 를 비동기 방식으로 실행하여, Scalar 값을 반환하는 <see cref="Task{Object}"/>를 빌드합니다. /// </summary> /// <param name="db">DAAB의 MySQL 용 Database</param> /// <param name="cmd">실행할 NpgsqlCommand 인스턴스</param> /// <param name="parameters">Command Parameters</param> /// <returns>Scalar 값을 결과로 가지는 Task의 인스턴스</returns> public static Task <object> ExecuteScalarAsync(this NpgsqlDatabase db, NpgsqlCommand cmd, params IAdoParameter[] parameters) { cmd.ShouldNotBeNull("cmd"); if (IsDebugEnabled) { log.Debug("NpgsqlCommand.ExecuteScalar를 비동기 방식으로 실행합니다... CommandText=[{0}], Parameters=[{1}]", cmd.CommandText, parameters.CollectionToString()); } var newConnectionCreated = false; if (cmd.Connection == null) { cmd.Connection = db.CreateNpgsqlConnection(ref newConnectionCreated); } if (parameters != null) { AdoTool.SetParameterValues(db, cmd, parameters); } return (Task.Factory .StartNew(() => cmd.ExecuteScalar(), TaskCreationOptions.PreferFairness) .ContinueWith(task => { if (newConnectionCreated) { AdoTool.ForceCloseConnection(cmd); } return task; }, TaskContinuationOptions.ExecuteSynchronously) .Unwrap()); }
/// <summary> /// Oracle Connection을 비동기 방식으로 엽니다. /// </summary> /// <param name="db">DAAB MySqlProvider 인스턴스</param> /// <param name="newConnectionCreated">새로운 Connenction이 생성되었는지 여부</param> /// <returns>새로 연결된 <see cref="NpgsqlConnection"/>의 인스턴스, 만약 연결에 실패했다면 null을 반환합니다.</returns> public static NpgsqlConnection CreateNpgsqlConnection(this NpgsqlDatabase db, ref bool newConnectionCreated) { return((NpgsqlConnection)AdoTool.CreateTransactionScopeConnection(db, ref newConnectionCreated, database => ((NpgsqlDatabase)database).OpenConnection(15))); }
/// <summary> /// <paramref name="sqlString"/>를 비동기 방식으로 실행하여, 결과 셋을 <see cref="Task{DataTable}"/>로 반환합니다. /// </summary> public static Task <DataTable> ExecuteDataTableAsyncBySqlString(this NpgsqlDatabase db, string sqlString, params IAdoParameter[] parameters) { return(ExecuteDataTableAsyncBySqlString(db, sqlString, 0, 0, parameters)); }
/// <summary> /// <paramref name="cmd"/>를 비동기 방식으로 실행하여, 결과 셋을 <see cref="Task{DataTable}"/>로 반환합니다. /// </summary> public static Task <DataTable> ExecuteDataTableAsync(this NpgsqlDatabase db, NpgsqlCommand cmd, params IAdoParameter[] parameters) { return(ExecuteDataTableAsync(db, cmd, 0, 0, parameters)); }
/// <summary> /// 쿼리 문 또는 Procedure Name을 실행할 <see cref="NpgsqlCommand"/>를 생성합니다. /// </summary> /// <param name="db">DAAB db</param> /// <param name="query">수행할 쿼리문 또는 Procedure Name</param> /// <returns>생성한 <see cref="NpgsqlCommand"/></returns> public static NpgsqlCommand GetNpgsqlCommand(this NpgsqlDatabase db, string query) { query.ShouldNotBeWhiteSpace("query"); return(GetNpgsqlCommand(db, query, AdoTool.DEFAULT_DISCOVER_PARAMETER)); }
/// <summary> /// <paramref name="query"/>를 비동기 방식으로 실행하여, 결과 셋을 <see cref="Task{DataTable}"/>로 반환합니다. /// </summary> public static Task <DataTable> ExecuteDataTableAsync(this NpgsqlDatabase db, string query, params IAdoParameter[] parameters) { return(ExecuteDataTableAsync(db, query, 0, 0, parameters)); }
/// <summary> /// <paramref name="spName"/>를 비동기 방식으로 실행하여, 결과 셋을 <see cref="Task{DataTable}"/>로 반환합니다. /// </summary> public static Task <DataTable> ExecuteDataTableAsyncByProcedure(this NpgsqlDatabase db, string spName, params IAdoParameter[] parameters) { return(ExecuteDataTableAsyncByProcedure(db, spName, 0, 0, parameters)); }
/// <summary> /// <paramref name="query"/> 를 비동기 방식으로 실행하여, <see cref="Task{NpgsqlDataReader}"/>를 반환받습니다. /// 받환받은 DataReader는 꼭 Dispose() 해 주어야 Connection이 닫힙니다. /// </summary> /// <param name="db">DAAB의 MySQL 용 Database</param> /// <param name="query">실행할 SQL 문</param> /// <param name="parameters">Command Parameters</param> /// <returns><see cref="NpgsqlDataReader"/>를 결과로 반환하는 <see cref="Task"/></returns> public static Task <NpgsqlDataReader> ExecuteReaderAsync(this NpgsqlDatabase db, string query, params IAdoParameter[] parameters) { var cmd = GetNpgsqlCommand(db, query); return(ExecuteReaderAsync(db, cmd, parameters)); }