public UserStatusHistory Load(ISqlConnectionInfo connection, SqlQueryParameters parameters) { IDatabase database = connection.Database; if (database == null) { throw new ArgumentNullException("database", "Error initializing database connection."); } if (parameters == null) { throw new ArgumentNullException("parameters"); } string sqlCmdText = string.Empty; try { sqlCmdText = "SELECT {0} " + UserStatusHistoryTable.GetColumnNames("[ush]") + (this.Depth > 0 ? "," + UserTable.GetColumnNames("[ush_u]") : string.Empty) + (this.Depth > 1 ? "," + UserTypeTable.GetColumnNames("[ush_u_ut]") : string.Empty) + " FROM [core].[UserStatusHistory] AS [ush] "; if (this.Depth > 0) { sqlCmdText += "INNER JOIN [core].[User] AS [ush_u] ON [ush].[UserID] = [ush_u].[UserID] "; } if (this.Depth > 1) { sqlCmdText += "INNER JOIN [core].[UserType] AS [ush_u_ut] ON [ush_u].[UserTypeID] = [ush_u_ut].[UserTypeID] "; } parameters.Top = 1; sqlCmdText = parameters.BuildQuery(sqlCmdText); SqlCommand sqlCmd = database.Add(sqlCmdText) as SqlCommand; foreach (KeyValuePair <string, object> argument in parameters.Arguments) { sqlCmd.Parameters.AddWithValue("@" + argument.Key, argument.Value); } SqlDataReader sqlReader = database.Add(sqlCmd) as SqlDataReader; if (!sqlReader.HasRows || !sqlReader.Read()) { IMessageBuilder builder = new DbLogMessageBuilder(new LogErrorCode("ush", "customload", "notfound"), "UserStatusHistory could not be loaded using custom logic as it was not found.", sqlCmdText, this, connection, parameters); if (this.Logger.IsDebugEnabled) { this.Logger.Debug(builder.ToString()); } sqlReader.Close(); return(null); } SqlQuery query = new SqlQuery(sqlReader); UserStatusHistoryTable ushTable = new UserStatusHistoryTable(query); UserTable ush_uTable = (this.Depth > 0) ? new UserTable(query) : null; UserTypeTable ush_u_utTable = (this.Depth > 1) ? new UserTypeTable(query) : null; UserType ush_u_utObject = (this.Depth > 1) ? ush_u_utTable.CreateInstance() : null; User ush_uObject = (this.Depth > 0) ? ush_uTable.CreateInstance(ush_u_utObject) : null; UserStatusHistory ushObject = ushTable.CreateInstance(ush_uObject); sqlReader.Close(); return(ushObject); } catch (Exception ex) { database.HandleException(ex); IMessageBuilder builder = new DbLogMessageBuilder(new LogErrorCode("ush", "customload", "exception"), "UserStatusHistory could not be loaded using custom logic. See exception for details.", sqlCmdText, ex, this, connection, parameters); if (this.Logger.IsErrorEnabled) { this.Logger.Error(builder.ToString(), ex); } throw new DataOperationException(DataOperation.Load, "UserStatusHistory", "Exception while loading (custom/single) UserStatusHistory object from database. See inner exception for details.", ex); } }
protected override UserStatusHistory LoadInternal(ISqlConnectionInfo connection, int id) { IDatabase database = connection.Database; if (database == null) { throw new ArgumentNullException("database", "Error initializing database connection."); } string sqlCmdText = string.Empty; try { sqlCmdText = "SELECT " + UserStatusHistoryTable.GetColumnNames("[ush]") + (this.Depth > 0 ? "," + UserTable.GetColumnNames("[ush_u]") : string.Empty) + (this.Depth > 1 ? "," + UserTypeTable.GetColumnNames("[ush_u_ut]") : string.Empty) + " FROM [core].[UserStatusHistory] AS [ush] "; if (this.Depth > 0) { sqlCmdText += "INNER JOIN [core].[User] AS [ush_u] ON [ush].[UserID] = [ush_u].[UserID] "; } if (this.Depth > 1) { sqlCmdText += "INNER JOIN [core].[UserType] AS [ush_u_ut] ON [ush_u].[UserTypeID] = [ush_u_ut].[UserTypeID] "; } sqlCmdText += "WHERE [ush].[UserStatusHistoryID] = @UserStatusHistoryID;"; SqlCommand sqlCmd = database.Add(sqlCmdText) as SqlCommand; sqlCmd.Parameters.AddWithValue("@UserStatusHistoryID", id); SqlDataReader sqlReader = database.Add(sqlCmd) as SqlDataReader; if (!sqlReader.HasRows || !sqlReader.Read()) { IMessageBuilder builder = new DbLogMessageBuilder(new LogErrorCode("ush", "loadinternal", "notfound"), "UserStatusHistory could not be loaded by id as it was not found.", sqlCmdText, this, connection, id); if (this.Logger.IsWarnEnabled) { this.Logger.Warn(builder.ToString()); } sqlReader.Close(); return(null); } SqlQuery query = new SqlQuery(sqlReader); UserStatusHistoryTable ushTable = new UserStatusHistoryTable(query); UserTable ush_uTable = (this.Depth > 0) ? new UserTable(query) : null; UserTypeTable ush_u_utTable = (this.Depth > 1) ? new UserTypeTable(query) : null; UserType ush_u_utObject = (this.Depth > 1) ? ush_u_utTable.CreateInstance() : null; User ush_uObject = (this.Depth > 0) ? ush_uTable.CreateInstance(ush_u_utObject) : null; UserStatusHistory ushObject = ushTable.CreateInstance(ush_uObject); sqlReader.Close(); return(ushObject); } catch (Exception ex) { database.HandleException(ex); IMessageBuilder builder = new DbLogMessageBuilder(new LogErrorCode("ush", "loadinternal", "exception"), "UserStatusHistory could not be loaded by id. See exception for details.", sqlCmdText, ex, this, connection, id); if (this.Logger.IsErrorEnabled) { this.Logger.Error(builder.ToString(), ex); } throw new DataOperationException(DataOperation.Load, "UserStatusHistory", "Exception while loading UserStatusHistory object from database. See inner exception for details.", ex); } }