/// <summary> /// Adds a mapping between the type of <see cref="DbConnection"/> and an instance of <see cref="IDbOperation"/> object. /// </summary> /// <param name="type">The type of <see cref="DbConnection"/> object.</param> /// <param name="statementBuilder">The statement builder to be mapped.</param> /// <param name="override">Set to true if to override the existing mapping, otherwise an exception will be thrown if the mapping is already present.</param> public static void Add(Type type, IDbOperation statementBuilder, bool @override) { // Guard the type Guard(type); // Variables var value = (IDbOperation)null; var key = type.FullName.GetHashCode(); // Try get the mappings if (m_maps.TryGetValue(key, out value)) { if (@override) { // Override the existing one m_maps.TryUpdate(key, statementBuilder, value); } else { // Throw an exception throw new MappingAlreadyExistsException($"The database operation mapping to provider '{type.FullName}' already exists."); } } else { // Add to mapping m_maps.TryAdd(key, statementBuilder); } }
protected NDbUnitTest(IDbConnection connection) { _passedconnection = true; _connection = connection; _connectionString = connection.ConnectionString; _dbOperation = CreateDbOperation(); }
private void ProcessRecord(IDataRecord record, IDbOperation dbOperation) { long id = record.GetInt64(0); ScaleoutMessage message = OraclePayload.FromBytes(record); _traceSource.TraceVerbose("OracleReceiver last payload ID={0}, new payload ID={1}", _lastPayloadId, id); if (id > _lastPayloadId + 1) { _traceSource.TraceError("Missed message(s) from Oracle. Expected payload ID {0} but got {1}.", _lastPayloadId + 1, id); } else if (id <= _lastPayloadId) { _traceSource.TraceInformation("Duplicate message(s) or payload ID reset from Oracle. Last payload ID {0}, this payload ID {1}", _lastPayloadId, id); } _lastPayloadId = id; dbOperation.Parameters[0].Value = _lastPayloadId; _traceSource.TraceVerbose("Updated receive reader initial payload ID parameter={0}", _observableDbOperation.Parameters[0].Value); _traceSource.TraceVerbose("Payload {0} containing {1} message(s) received", id, message.Messages.Count); if (Received != null) { Received((ulong)id, message); } }
public static TAssociation Execute(IDbOperation <TAssociation> operation) { using (DbOperationScope scope = new DbOperationScope()) { return(scope.Execute <TAssociation>(operation)); } }
public EdgarService(IHttpDataAgent httpDataAgent, IUtil util, IDbOperation dbOperation, string[] ciks, string edgarUri, string posPage, string outputPath) { _httpDataAgent = httpDataAgent; _util = util; _edgarUri = edgarUri; _ciks = ciks; _outputPath = outputPath; _posPage = posPage; _dbOperation = dbOperation; }
public RSManager(IAvailabiltyRepository availabiltyRepository, IRoomRepository roomRepository, IEventRepository eventRepository, IPenaltyRepository penaltyRepository, IDbOperation dbOperation, IUserRoleRepository userRoleRepository, IUserRepository userRepository, IRoleRepository roleRepository) { this.availabilityRepository = availabiltyRepository; this.eventRepository = eventRepository; this.penaltyRepository = penaltyRepository; this.userRoleRepository = userRoleRepository; this.dbOperation = dbOperation; this.userRepository = userRepository; this.roleRepository = roleRepository; this.roomRepository = roomRepository; }
public void _SetUp() { _mockSchemaFileStream = new FileStream(GetXmlSchemaFilename(), FileMode.Open, FileAccess.Read, FileShare.Read); _mockDataFileStream = new FileStream(GetXmlFilename(), FileMode.Open, FileAccess.Read, FileShare.Read); _mocker = new MockRepository(); _mockDbCommandBuilder = _mocker.CreateMock <IDbCommandBuilder>(); _mockDbOperation = _mocker.CreateMock <IDbOperation>(); _nDbUnitTestStub = GetUnitTestStub(); _mockConnection = _mocker.CreateMock <IDbConnection>(); _mockTransaction = _mocker.CreateMock <IDbTransaction>(); }
public void _FixtureSetUp() { _commandBuilder = GetCommandBuilder(); string xmlSchemaFile = GetXmlSchemaFilename(); _xmlFile = GetXmlFilename(); _commandBuilder.BuildCommands(xmlSchemaFile); DataSet dsSchema = _commandBuilder.GetSchema(); _dsData = dsSchema.Clone(); _dsData.ReadXml(ReadOnlyStreamFromFilename(_xmlFile)); _dbOperation = GetDbOperation(); }
/// <summary> /// Executes the specified operation within the scope /// and return its result. /// </summary> /// <typeparam name="TResult">The type of the operation result.</typeparam> /// <param name="operation">The operation to execute.</param> public TResult Execute <TResult>(IDbOperation <TResult> operation) { Precondition.Require(!_disposed, () => Error.ObjectDisposed("provider")); Precondition.Require(operation, () => Error.ArgumentNull("operation")); try { EnsureTransaction(); return(operation.Execute(this)); } catch (Exception) { Rollback(); throw; } }
public static IDbOperation GetOperation(DbTypes type) { IDbOperation operation = null; switch (type) { case DbTypes.Mysql: operation = new MySqlOperation(); break; case DbTypes.Oracle: break; case DbTypes.Sqlserver: operation = new SqlserverOperation(); break; } return(operation); }
/// <summary> /// Adds a mapping between the type of <see cref="DbConnection"/> and an instance of <see cref="IDbOperation"/> object. /// </summary> /// <param name="type">The type of <see cref="DbConnection"/> object.</param> /// <param name="statementBuilder">The statement builder to be mapped.</param> /// <param name="override">Set to true if to override the existing mapping, otherwise an exception will be thrown if the mapping is already present.</param> public static void Add(Type type, IDbOperation statementBuilder, bool @override = false) { Guard(type); var value = (IDbOperation)null; var key = type.FullName.GetHashCode(); if (m_maps.TryGetValue(key, out value)) { if (@override) { m_maps.TryUpdate(key, statementBuilder, value); } else { throw new InvalidOperationException($"Database operation provider mapping already exists ('{type.Name}' = '{value?.GetType().Name}')."); } } else { m_maps.TryAdd(key, statementBuilder); } }
public void GetLastPayloadId() { if (!_lastPayloadId.HasValue) { IDataParameter parameter = _dbProviderFactory.CreateParameter(); parameter.DbType = DbType.Int64; parameter.Direction = ParameterDirection.InputOutput; parameter.ParameterName = "oPayloadId"; IDbOperation lastPayloadIdOperation = _dbOperationFactory.CreateDbOperation(_connectionString, _maxIdSql, _traceSource, _dbProviderFactory, parameter); try { lastPayloadIdOperation.ExecuteStoredProcedure(); _lastPayloadId = (long?)parameter.Value; if (Queried != null) { Queried(); } _traceSource.TraceVerbose("OracleReceiver started, initial payload id={0}", _lastPayloadId); } catch (Exception ex) { if (Faulted != null) { Faulted(ex); } _traceSource.TraceError("OracleReceiver error starting: {0}", ex); throw; } } }
protected NDbUnitTest(string connectionString) { _connectionString = connectionString; _dbOperation = CreateDbOperation(); }
protected NDbUnitTest(TDbConnection connection) { ConnectionManager = new DbConnectionManager <TDbConnection>(connection); _dbOperation = CreateDbOperation(); }
public void PerformDbOperation(DbOperationFlag dbOperationFlag) { checkInitialized(); if (dbOperationFlag == DbOperationFlag.None) { return; } IDbCommandBuilder dbCommandBuilder = GetDbCommandBuilder(); IDbOperation dbOperation = GetDbOperation(); DbTransaction dbTransaction = null; DbConnection dbConnection = dbCommandBuilder.Connection; using (new OpenConnectionGuard(dbConnection, ConnectionManager.HasExternallyManagedConnection)) { try { dbTransaction = dbConnection.BeginTransaction(); OperationEventArgs args = new OperationEventArgs(); args.DbTransaction = dbTransaction; if (null != PreOperation) { PreOperation(this, args); } switch (dbOperationFlag) { case DbOperationFlag.Insert: { dbOperation.Insert(_dataSet, dbCommandBuilder, dbTransaction); break; } case DbOperationFlag.InsertIdentity: { dbOperation.InsertIdentity(_dataSet, dbCommandBuilder, dbTransaction); break; } case DbOperationFlag.Delete: { dbOperation.Delete(_dataSet, dbCommandBuilder, dbTransaction); break; } case DbOperationFlag.DeleteAll: { dbOperation.DeleteAll(_dataSet, dbCommandBuilder, dbTransaction); break; } case DbOperationFlag.Refresh: { dbOperation.Refresh(_dataSet, dbCommandBuilder, dbTransaction); break; } case DbOperationFlag.Update: { dbOperation.Update(_dataSet, dbCommandBuilder, dbTransaction); break; } case DbOperationFlag.CleanInsert: { dbOperation.DeleteAll(_dataSet, dbCommandBuilder, dbTransaction); dbOperation.Insert(_dataSet, dbCommandBuilder, dbTransaction); break; } case DbOperationFlag.CleanInsertIdentity: { dbOperation.DeleteAll(_dataSet, dbCommandBuilder, dbTransaction); dbOperation.InsertIdentity(_dataSet, dbCommandBuilder, dbTransaction); break; } } if (null != PostOperation) { PostOperation(this, args); } dbTransaction.Commit(); } catch (Exception) { if (dbTransaction != null) { dbTransaction.Rollback(); } throw; } finally { if (dbTransaction != null) { dbTransaction.Dispose(); } } } }
private void btn_work_Click(object sender, EventArgs e) { var savePlace = this.txt_savePlace.Text; if (string.IsNullOrWhiteSpace(savePlace)) { MessageBox.Show("生成位置不能为空!", "Error Msg"); return; } Func <string, string> generateTableNameFunc = null, generatePropNameFunc = null; try { var dynamicAssembly = DynamicClassTools.DynamicAssembly(this.rtb_dynamicCode.Text); var instance = DynamicClassTools.GetClass(dynamicAssembly, "Dynamicly.GenerateHelper"); generateTableNameFunc = (str) => (string)DynamicClassTools.InvokeMethod(instance, "GetTableName", str); generatePropNameFunc = (str) => (string)DynamicClassTools.InvokeMethod(instance, "GetPropName", str); } catch (Exception exception) { MessageBox.Show(exception.Message, "Error Msg"); return; } var connStr = this.txt_connStr.Text; if (string.IsNullOrWhiteSpace(connStr)) { MessageBox.Show("连接字符串不能为空!", "Error Msg"); return; } try { Conn = DbTools.GetConnection(connStr, DbType); //测试连接 Conn.Open(); Conn.Close(); } catch (Exception exception) { MessageBox.Show(exception.Message, "Error Msg"); return; } Operation = DbTools.GetOperation(DbType); if (Operation == null) { MessageBox.Show($"暂不支持{DbType}", "Error Msg"); return; } CommandGenerateCode commandGenerate = new CommandGenerateCode(Conn, Operation); var dictionary = commandGenerate.Work(); if (dictionary.Count == 0) { MessageBox.Show("查询数据为空", "Confirm Msg"); return; } var templateField = this.rtb_templateField.Text; var templateTable = this.rtb_templateTable.Text; try { //并行写入文件 dictionary.AsParallel().ForAll((item => { var code = commandGenerate.GenerateCode(item.Key, item.Value, templateTable, templateField , generateTableNameFunc, generatePropNameFunc); FileTools.InFile(savePlace, $"{generateTableNameFunc.Invoke(item.Key.Name)}.class", code); } )); MessageBox.Show("Success!", "Confirm Msg"); } catch (Exception exception) { MessageBox.Show(exception.Message, "Error Msg"); } }
public void _SetUp() { _mockSchemaFileStream = new FileStream(GetXmlSchemaFilename(), FileMode.Open, FileAccess.Read, FileShare.Read); _mockDataFileStream = new FileStream(GetXmlFilename(), FileMode.Open, FileAccess.Read, FileShare.Read); _mocker = new MockRepository(); _mockDbCommandBuilder = _mocker.CreateMock<IDbCommandBuilder>(); _mockDbOperation = _mocker.CreateMock<IDbOperation>(); _nDbUnitTestStub = GetUnitTestStub(); _mockConnection = _mocker.CreateMock<IDbConnection>(); _mockTransaction = _mocker.CreateMock<IDbTransaction>(); }
public void PerformDbOperation(DbOperationFlag dbOperationFlag) { checkInitialized(); if (dbOperationFlag == DbOperationFlag.None) { return; } IDbCommandBuilder dbCommandBuilder = GetDbCommandBuilder(); IDbOperation dbOperation = GetDbOperation(); IDbTransaction dbTransaction = null; IDbConnection dbConnection = dbCommandBuilder.Connection; try { if (dbConnection.State != ConnectionState.Open) { dbConnection.Open(); } dbTransaction = dbConnection.BeginTransaction(); OperationEventArgs args = new OperationEventArgs(); args.DbTransaction = dbTransaction; if (null != PreOperation) { PreOperation(this, args); } switch (dbOperationFlag) { case DbOperationFlag.Insert: { dbOperation.Insert(_dataSet, dbCommandBuilder, dbTransaction); break; } //case DbOperationFlag.InsertIdentity: // { // dbOperation.InsertIdentity(_dataSet, dbCommandBuilder, dbTransaction); // break; // } //case DbOperationFlag.Delete: // { // dbOperation.Delete(_dataSet, dbCommandBuilder, dbTransaction); // break; // } case DbOperationFlag.DeleteAll: { dbOperation.DeleteAll(_dataSet, dbCommandBuilder, dbTransaction); break; } //case DbOperationFlag.Refresh: // { // dbOperation.Refresh(_dataSet, dbCommandBuilder, dbTransaction); // break; // } //case DbOperationFlag.Update: // { // dbOperation.Update(_dataSet, dbCommandBuilder, dbTransaction); // break; // } //case DbOperationFlag.CleanInsert: // { // dbOperation.DeleteAll(_dataSet, dbCommandBuilder, dbTransaction); // dbOperation.Insert(_dataSet, dbCommandBuilder, dbTransaction); // break; // } //case DbOperationFlag.CleanInsertIdentity: // { // dbOperation.DeleteAll(_dataSet, dbCommandBuilder, dbTransaction); // dbOperation.InsertIdentity(_dataSet, dbCommandBuilder, dbTransaction); // break; // } } if (null != PostOperation) { PostOperation(this, args); } dbTransaction.Commit(); } catch (Exception) { if (dbTransaction != null) { dbTransaction.Rollback(); } throw; } finally { if (dbTransaction != null) { dbTransaction.Dispose(); } //only close and release the connection if not externally-managed if (!ConnectionManager.HasExternallyManagedConnection) { if (ConnectionState.Open == dbConnection.State) { dbConnection.Close(); } ConnectionManager.ReleaseConnection(); } } }
protected NDbUnitTest(string connectionString) { ConnectionManager = new DbConnectionManager <TDbConnection>(connectionString); _dbOperation = CreateDbOperation(); }
public CommandGenerateCode(IDbConnection connection, IDbOperation operation) { this.connection = connection; this.operation = operation; }