protected override ProfileDetail 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 " + ProfileDetailTable.GetColumnNames("[pd]") + (this.Depth > 0 ? "," + ProfileTable.GetColumnNames("[pd_p]") : string.Empty) + (this.Depth > 1 ? "," + ProfileGroupTable.GetColumnNames("[pd_p_pg]") : string.Empty) + (this.Depth > 0 ? "," + LanguageTable.GetColumnNames("[pd_l]") : string.Empty) + " FROM [web].[ProfileDetail] AS [pd] "; if (this.Depth > 0) { sqlCmdText += "INNER JOIN [web].[Profile] AS [pd_p] ON [pd].[ProfileID] = [pd_p].[ProfileID] "; } if (this.Depth > 1) { sqlCmdText += "INNER JOIN [web].[ProfileGroup] AS [pd_p_pg] ON [pd_p].[ProfileGroupID] = [pd_p_pg].[ProfileGroupID] "; } if (this.Depth > 0) { sqlCmdText += "INNER JOIN [core].[Language] AS [pd_l] ON [pd].[LanguageID] = [pd_l].[LanguageID] "; } sqlCmdText += "WHERE [pd].[ProfileDetailID] = @ProfileDetailID;"; SqlCommand sqlCmd = database.Add(sqlCmdText) as SqlCommand; sqlCmd.Parameters.AddWithValue("@ProfileDetailID", id); SqlDataReader sqlReader = database.Add(sqlCmd) as SqlDataReader; if (!sqlReader.HasRows || !sqlReader.Read()) { IMessageBuilder builder = new DbLogMessageBuilder(new LogErrorCode("pd", "loadinternal", "notfound"), "ProfileDetail 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); ProfileDetailTable pdTable = new ProfileDetailTable(query); ProfileTable pd_pTable = (this.Depth > 0) ? new ProfileTable(query) : null; ProfileGroupTable pd_p_pgTable = (this.Depth > 1) ? new ProfileGroupTable(query) : null; LanguageTable pd_lTable = (this.Depth > 0) ? new LanguageTable(query) : null; ProfileGroup pd_p_pgObject = (this.Depth > 1) ? pd_p_pgTable.CreateInstance() : null; Profile pd_pObject = (this.Depth > 0) ? pd_pTable.CreateInstance(pd_p_pgObject) : null; Language pd_lObject = (this.Depth > 0) ? pd_lTable.CreateInstance() : null; ProfileDetail pdObject = pdTable.CreateInstance(pd_pObject, pd_lObject); sqlReader.Close(); return(pdObject); } catch (Exception ex) { database.HandleException(ex); IMessageBuilder builder = new DbLogMessageBuilder(new LogErrorCode("pd", "loadinternal", "exception"), "ProfileDetail 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, "ProfileDetail", "Exception while loading ProfileDetail object from database. See inner exception for details.", ex); } }
public List <ProfileDetail> LoadMany(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} " + ProfileDetailTable.GetColumnNames("[pd]") + (this.Depth > 0 ? "," + ProfileTable.GetColumnNames("[pd_p]") : string.Empty) + (this.Depth > 1 ? "," + ProfileGroupTable.GetColumnNames("[pd_p_pg]") : string.Empty) + (this.Depth > 0 ? "," + LanguageTable.GetColumnNames("[pd_l]") : string.Empty) + " FROM [web].[ProfileDetail] AS [pd] "; if (this.Depth > 0) { sqlCmdText += "INNER JOIN [web].[Profile] AS [pd_p] ON [pd].[ProfileID] = [pd_p].[ProfileID] "; } if (this.Depth > 1) { sqlCmdText += "INNER JOIN [web].[ProfileGroup] AS [pd_p_pg] ON [pd_p].[ProfileGroupID] = [pd_p_pg].[ProfileGroupID] "; } if (this.Depth > 0) { sqlCmdText += "INNER JOIN [core].[Language] AS [pd_l] ON [pd].[LanguageID] = [pd_l].[LanguageID] "; } 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("pd", "customloadmany", "notfound"), "ProfileDetail list could not be loaded using custom logic as no items were found.", sqlCmdText, this, connection, parameters); if (this.Logger.IsDebugEnabled) { this.Logger.Debug(builder.ToString()); } sqlReader.Close(); return(new List <ProfileDetail>()); } SqlQuery query = new SqlQuery(sqlReader); ProfileDetailTable pdTable = new ProfileDetailTable(query); ProfileTable pd_pTable = (this.Depth > 0) ? new ProfileTable(query) : null; ProfileGroupTable pd_p_pgTable = (this.Depth > 1) ? new ProfileGroupTable(query) : null; LanguageTable pd_lTable = (this.Depth > 0) ? new LanguageTable(query) : null; List <ProfileDetail> result = new List <ProfileDetail>(); do { ProfileGroup pd_p_pgObject = (this.Depth > 1) ? pd_p_pgTable.CreateInstance() : null; Profile pd_pObject = (this.Depth > 0) ? pd_pTable.CreateInstance(pd_p_pgObject) : null; Language pd_lObject = (this.Depth > 0) ? pd_lTable.CreateInstance() : null; ProfileDetail pdObject = (this.Depth > -1) ? pdTable.CreateInstance(pd_pObject, pd_lObject) : null; result.Add(pdObject); } while (sqlReader.Read()); sqlReader.Close(); return(result); } catch (Exception ex) { database.HandleException(ex); IMessageBuilder builder = new DbLogMessageBuilder(new LogErrorCode("pd", "customloadmany", "exception"), "ProfileDetail list 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, "ProfileDetail", "Exception while loading (custom/many) ProfileDetail object from database. See inner exception for details.", ex); } }