/*================================================================================================================================== * Properties *=================================================================================================================================*/ #endregion #region Public Methods /*================================================================================================================================== * Public Methods *=================================================================================================================================*/ public questStatus Create(Quest.Functional.Logging.BulkInsertLog bulkInsertLog, out Quest.Functional.Logging.BulkInsertLogId bulkInsertLogId) { // Initialize bulkInsertLogId = null; questStatus status = null; // Date/time stamp it bulkInsertLog.Created = DateTime.Now; // Create bulkInsertLog status = _dbBulkInsertLogsMgr.Create(bulkInsertLog, out bulkInsertLogId); if (!questStatusDef.IsSuccess(status)) { return(status); } return(new questStatus(Severity.Success)); }
public questStatus PerformBulkInsertFilterProcedure(BulkInsertRequest bulkInsertRequest, FilterProcedure filterProcedure) { // Initialize questStatus status = null; BulkInsertLog bulkInsertLog = bLogging ? new BulkInsertLog() : null; BulkInsertLogId bulkInsertLogId = null; int numRows = 0; string logMessage = null; List <string> logParameterList = null; try { // Initialize log if (bLogging) { bulkInsertLog.Event = "Initialize"; bulkInsertLog.UserSessionId = this.UserSession.Id; bulkInsertLog.Username = this.UserSession.User.Username; bulkInsertLog.Batch = Guid.NewGuid().ToString(); string Filter = null; status = _dbBulkInsertLogsMgr.SetFilter(bulkInsertRequest.Filter, out Filter); if (!questStatusDef.IsSuccess(status)) { return(status); } bulkInsertLog.Filter = Filter; } // Get database connection string TablesetId tablesetId = new TablesetId(bulkInsertRequest.TablesetId); Tableset tableset = null; DbTablesetsMgr dbTablesetsMgr = new DbTablesetsMgr(this.UserSession); status = dbTablesetsMgr.Read(tablesetId, out tableset); if (!questStatusDef.IsSuccess(status)) { return(status); } if (bLogging) { string Tableset = null; status = _dbBulkInsertLogsMgr.SetTableset(tableset, out Tableset); if (!questStatusDef.IsSuccess(status)) { return(status); } bulkInsertLog.Tableset = Tableset; } DatabaseId databaseId = new DatabaseId(tableset.DatabaseId); Quest.Functional.MasterPricing.Database database = null; DbDatabasesMgr dbDatabasesMgr = new DbDatabasesMgr(this.UserSession); status = dbDatabasesMgr.Read(databaseId, out database); if (!questStatusDef.IsSuccess(status)) { return(status); } if (bLogging) { string Database = null; status = _dbBulkInsertLogsMgr.SetDatabase(database, out Database); if (!questStatusDef.IsSuccess(status)) { return(status); } bulkInsertLog.Database = Database; } // Connect and execute bool bTransaction = false; bulkInsertLog.Event = "Open"; using (SqlConnection conn = new SqlConnection(database.ConnectionString)) { bulkInsertLog.Event = "Connect"; conn.Open(); SqlTransaction trans = null; if (bTransaction) { trans = conn.BeginTransaction(); if (bLogging) { bulkInsertLog.Event = "BeginTransaction"; } } numRows = 0; foreach (BulkInsertRow bulkInsertRow in bulkInsertRequest.Rows) { using (SqlCommand cmd = new SqlCommand(null, conn, trans)) { cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = filterProcedure.Name; // Initialize logging info if (bLogging) { logParameterList = new List <string>(); bulkInsertLog.Event = null; bulkInsertLog.NumRows = numRows; bulkInsertLog.Parameters = null; bulkInsertLog.Message = null; bulkInsertLog.Data = null; } // Build each parameter List <string> dataValueList = new List <string>(); foreach (FilterProcedureParameter filterParam in filterProcedure.ParameterList) { if (bLogging) { bulkInsertLog.Event = "Next Parameter: " + filterParam.ParameterName; } if (filterParam.Direction != "Input") { continue; } if (bLogging) { logParameterList.Add(String.Format(" Id: {0}, ParameterName: {1}, SqlDbType: {2} ", filterParam.Id, filterParam.ParameterName, filterParam.SqlDbType.ToString())); string parameterArray = null; _dbBulkInsertLogsMgr.SetArray(logParameterList, out parameterArray); bulkInsertLog.Parameters = parameterArray; } // If a meta-parameter, fill in its value and continue. bool bIsMetaParameter = false; SqlParameter sqlMetaParameter = null; if (filterParam.ParameterName.Equals("@_Username", StringComparison.InvariantCultureIgnoreCase)) { bIsMetaParameter = true; sqlMetaParameter = new SqlParameter(filterParam.ParameterName, SqlDbType.NVarChar); sqlMetaParameter.Value = this.UserSession.User.Username; } else if (filterParam.ParameterName.Equals("@_UserSessionId", StringComparison.InvariantCultureIgnoreCase)) { bIsMetaParameter = true; sqlMetaParameter = new SqlParameter(filterParam.ParameterName, SqlDbType.Int); sqlMetaParameter.Value = this.UserSession.Id; } else if (filterParam.ParameterName.StartsWith("@_", StringComparison.InvariantCultureIgnoreCase)) { logMessage = String.Format("ERROR: unknown meta-parameter: {0}", filterParam.ParameterName); if (bLogging) { bulkInsertLog.Message = logMessage; _dbBulkInsertLogsMgr.Create(bulkInsertLog, out bulkInsertLogId); } return(new questStatus(Severity.Error, logMessage)); } if (bIsMetaParameter) { cmd.Parameters.Add(sqlMetaParameter); continue; } // Not a meta-parameter, so the bulk insert value should be provided. BulkInsertColumnValue bulkInsertColumnValue = bulkInsertRow.Columns.Find(delegate(BulkInsertColumnValue cv) { return(cv.Name == filterParam.ParameterName); }); if (bulkInsertColumnValue == null) { if (bTransaction) { trans.Rollback(); } logMessage = String.Format("ERROR: sproc parameter {0} not found in bulk insert columns", filterParam.ParameterName); if (bLogging) { bulkInsertLog.Message = logMessage; _dbBulkInsertLogsMgr.Create(bulkInsertLog, out bulkInsertLogId); } return(new questStatus(Severity.Error, logMessage)); } if (bLogging) { bulkInsertLog.BulkInsertColumn = String.Format(" Name: {0}, Value: {1} ", bulkInsertColumnValue.Name, bulkInsertColumnValue.Value); } // Bind the parameter // TODO:REFACTOR SqlDbType sqlDbType = (SqlDbType)Enum.Parse(typeof(SqlDbType), filterParam.SqlDbType, true); SqlParameter sqlParameter = new SqlParameter(filterParam.ParameterName, sqlDbType); if (bLogging) { bulkInsertLog.Event = "Set Parameter Value"; } if (sqlDbType == SqlDbType.Bit) { bool bValue = bulkInsertColumnValue.Value != "0"; sqlParameter.Value = bValue; } else if (sqlDbType == SqlDbType.Int) { int intValue = Convert.ToInt32(bulkInsertColumnValue.Value); sqlParameter.Value = intValue; } else if (sqlDbType == SqlDbType.NVarChar) { if (bulkInsertColumnValue.Value == null) { sqlParameter.Value = DBNull.Value; } else { sqlParameter.Value = bulkInsertColumnValue.Value.ToString(); } } else if (sqlDbType == SqlDbType.VarChar) { if (bulkInsertColumnValue.Value == null) { sqlParameter.Value = DBNull.Value; } else { sqlParameter.Value = bulkInsertColumnValue.Value.ToString(); } } else if (sqlDbType == SqlDbType.DateTime) { if (bulkInsertColumnValue.Value == null) { sqlParameter.Value = DBNull.Value; } else { sqlParameter.Value = Convert.ToDateTime(bulkInsertColumnValue.Value); } } else if (sqlDbType == SqlDbType.DateTime2) { if (bulkInsertColumnValue.Value == null) { sqlParameter.Value = DBNull.Value; } else { sqlParameter.Value = Convert.ToDateTime(bulkInsertColumnValue.Value); } } else if (sqlDbType == SqlDbType.Date) { if (bulkInsertColumnValue.Value == null) { sqlParameter.Value = DBNull.Value; } else { sqlParameter.Value = Convert.ToDateTime(bulkInsertColumnValue.Value); } } else if (sqlDbType == SqlDbType.Decimal) { if (bulkInsertColumnValue.Value == null) { sqlParameter.Value = DBNull.Value; } else { sqlParameter.Value = Convert.ToDecimal(bulkInsertColumnValue.Value); } } else { if (bulkInsertColumnValue.Value == null) { sqlParameter.Value = DBNull.Value; } else { sqlParameter.Value = bulkInsertColumnValue.Value; } } cmd.Parameters.Add(sqlParameter); if (bLogging) { dataValueList.Add(String.Format(" Name: {0}, Value: {1} ", sqlParameter.ParameterName, sqlParameter.Value == DBNull.Value ? "null" : sqlParameter.Value.ToString())); } } try { if (bLogging) { bulkInsertLog.Event = "ExecuteNonQuery"; } int _numRows = cmd.ExecuteNonQuery(); if (_numRows != 1) { if (bTransaction) { trans.Rollback(); } logMessage = String.Format("ERROR: Bulk insert stored procedure failed: Rows: {0}", _numRows); if (bLogging) { bulkInsertLog.Message = logMessage; _dbBulkInsertLogsMgr.Create(bulkInsertLog, out bulkInsertLogId); } return(new questStatus(Severity.Error, logMessage)); } else if (bLoggingPerRow) { bulkInsertLog.Message = "Successful bulk insert"; bulkInsertLog.NumRows = _numRows; bulkInsertLog.BulkInsertColumn = null; string valueArray = null; _dbBulkInsertLogsMgr.SetArray(dataValueList, out valueArray); bulkInsertLog.Data = valueArray; _dbBulkInsertLogsMgr.Create(bulkInsertLog, out bulkInsertLogId); } } catch (SqlException ex) { if (bTransaction) { trans.Rollback(); } logMessage = String.Format("SQL EXCEPTION: Bulk insert stored procedure {1}: {2}", numRows, filterProcedure.Name, ex.Message); if (bLogging) { bulkInsertLog.Message = logMessage; _dbBulkInsertLogsMgr.Create(bulkInsertLog, out bulkInsertLogId); } return(new questStatus(Severity.Error, logMessage)); } catch (System.Exception ex) { if (bTransaction) { trans.Rollback(); } logMessage = String.Format("After {0} rows, EXCEPTION: Bulk insert stored procedure {0}: {1}", numRows, filterProcedure.Name, ex.Message); if (bLogging) { bulkInsertLog.Message = logMessage; _dbBulkInsertLogsMgr.Create(bulkInsertLog, out bulkInsertLogId); } return(new questStatus(Severity.Error, logMessage)); } } numRows += 1; } if (bTransaction) { trans.Commit(); } if (bLogging) { bulkInsertLog.Event = "COMMIT"; bulkInsertLog.NumRows = numRows; bulkInsertLog.BulkInsertColumn = null; bulkInsertLog.Message = "Bulk isnert operation successful"; bulkInsertLog.Data = null; _dbBulkInsertLogsMgr.Create(bulkInsertLog, out bulkInsertLogId); } } } catch (System.Exception ex) { logMessage = String.Format("EXCEPTION: Bulk Insert Operation: {0}.{1}: {2}", this.GetType().Name, MethodBase.GetCurrentMethod().Name, ex.InnerException != null ? ex.InnerException.Message : ex.Message); if (bLogging) { bulkInsertLog.Event = bulkInsertLog.Event == null ? "EXCEPTION" : bulkInsertLog.Event; bulkInsertLog.Message = logMessage; bulkInsertLog.NumRows = numRows; string parameterArray = null; _dbBulkInsertLogsMgr.SetArray(logParameterList, out parameterArray); bulkInsertLog.Parameters = parameterArray; string Exception = null; _dbBulkInsertLogsMgr.SetException(ex, out Exception); bulkInsertLog.Data = Exception; _dbBulkInsertLogsMgr.Create(bulkInsertLog, out bulkInsertLogId); } return(new questStatus(Severity.Fatal, logMessage)); } return(new questStatus(Severity.Success)); }