protected override ActionContext 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 " +
                             ActionContextTable.GetColumnNames("[ac]") +
                             (this.Depth > 0 ? "," + ActionContextGroupTable.GetColumnNames("[ac_acg]") : string.Empty) +
                             " FROM [core].[ActionContext] AS [ac] ";
                if (this.Depth > 0)
                {
                    sqlCmdText += "INNER  JOIN [core].[ActionContextGroup] AS [ac_acg] ON [ac].[ActionContextGroupID] = [ac_acg].[ActionContextGroupID] ";
                }
                sqlCmdText += "WHERE [ac].[ActionContextID] = @ActionContextID;";

                SqlCommand sqlCmd = database.Add(sqlCmdText) as SqlCommand;
                sqlCmd.Parameters.AddWithValue("@ActionContextID", id);
                SqlDataReader sqlReader = database.Add(sqlCmd) as SqlDataReader;

                if (!sqlReader.HasRows || !sqlReader.Read())
                {
                    IMessageBuilder builder = new DbLogMessageBuilder(new LogErrorCode("ac", "loadinternal", "notfound"), "ActionContext 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);

                ActionContextTable      acTable     = new ActionContextTable(query);
                ActionContextGroupTable ac_acgTable = (this.Depth > 0) ? new ActionContextGroupTable(query) : null;


                ActionContextGroup ac_acgObject = (this.Depth > 0) ? ac_acgTable.CreateInstance() : null;
                ActionContext      acObject     = acTable.CreateInstance(ac_acgObject);
                sqlReader.Close();

                return(acObject);
            }
            catch (Exception ex)
            {
                database.HandleException(ex);
                IMessageBuilder builder = new DbLogMessageBuilder(new LogErrorCode("ac", "loadinternal", "exception"), "ActionContext 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, "ActionContext", "Exception while loading ActionContext object from database. See inner exception for details.", ex);
            }
        }
Beispiel #2
0
        public ActionContextGroup 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} " +
                             ActionContextGroupTable.GetColumnNames("[acg]") +
                             " FROM [core].[ActionContextGroup] AS [acg] ";


                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("acg", "customload", "notfound"), "ActionContextGroup 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);

                ActionContextGroupTable acgTable = new ActionContextGroupTable(query);


                ActionContextGroup acgObject = acgTable.CreateInstance();
                sqlReader.Close();

                return(acgObject);
            }
            catch (Exception ex)
            {
                database.HandleException(ex);
                IMessageBuilder builder = new DbLogMessageBuilder(new LogErrorCode("acg", "customload", "exception"), "ActionContextGroup 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, "ActionContextGroup", "Exception while loading (custom/single) ActionContextGroup object from database. See inner exception for details.", ex);
            }
        }
        public List <ActionContext> 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} " +
                             ActionContextTable.GetColumnNames("[ac]") +
                             (this.Depth > 0 ? "," + ActionContextGroupTable.GetColumnNames("[ac_acg]") : string.Empty) +
                             " FROM [core].[ActionContext] AS [ac] ";
                if (this.Depth > 0)
                {
                    sqlCmdText += "INNER  JOIN [core].[ActionContextGroup] AS [ac_acg] ON [ac].[ActionContextGroupID] = [ac_acg].[ActionContextGroupID] ";
                }


                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("ac", "customloadmany", "notfound"), "ActionContext 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 <ActionContext>());
                }

                SqlQuery query = new SqlQuery(sqlReader);

                ActionContextTable      acTable     = new ActionContextTable(query);
                ActionContextGroupTable ac_acgTable = (this.Depth > 0) ? new ActionContextGroupTable(query) : null;

                List <ActionContext> result = new List <ActionContext>();
                do
                {
                    ActionContextGroup ac_acgObject = (this.Depth > 0) ? ac_acgTable.CreateInstance() : null;
                    ActionContext      acObject     = (this.Depth > -1) ? acTable.CreateInstance(ac_acgObject) : null;
                    result.Add(acObject);
                } while (sqlReader.Read());
                sqlReader.Close();

                return(result);
            }
            catch (Exception ex)
            {
                database.HandleException(ex);
                IMessageBuilder builder = new DbLogMessageBuilder(new LogErrorCode("ac", "customloadmany", "exception"), "ActionContext 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, "ActionContext", "Exception while loading (custom/many) ActionContext object from database. See inner exception for details.", ex);
            }
        }