Beispiel #1
0
        protected override Agent 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 " +
                             AgentTable.GetColumnNames("[a]") +
                             (this.Depth > 0 ? "," + UserTable.GetColumnNames("[a_u]") : string.Empty) +
                             (this.Depth > 1 ? "," + UserTypeTable.GetColumnNames("[a_u_ut]") : string.Empty) +
                             " FROM [core].[Agent] AS [a] ";
                if (this.Depth > 0)
                {
                    sqlCmdText += "INNER  JOIN [core].[User] AS [a_u] ON [a].[UserID] = [a_u].[UserID] ";
                }
                if (this.Depth > 1)
                {
                    sqlCmdText += "INNER  JOIN [core].[UserType] AS [a_u_ut] ON [a_u].[UserTypeID] = [a_u_ut].[UserTypeID] ";
                }
                sqlCmdText += "WHERE [a].[AgentID] = @AgentID;";

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

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

                AgentTable    aTable      = new AgentTable(query);
                UserTable     a_uTable    = (this.Depth > 0) ? new UserTable(query) : null;
                UserTypeTable a_u_utTable = (this.Depth > 1) ? new UserTypeTable(query) : null;


                UserType a_u_utObject = (this.Depth > 1) ? a_u_utTable.CreateInstance() : null;
                User     a_uObject    = (this.Depth > 0) ? a_uTable.CreateInstance(a_u_utObject) : null;
                Agent    aObject      = aTable.CreateInstance(a_uObject);
                sqlReader.Close();

                return(aObject);
            }
            catch (Exception ex)
            {
                database.HandleException(ex);
                IMessageBuilder builder = new DbLogMessageBuilder(new LogErrorCode("a", "loadinternal", "exception"), "Agent 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, "Agent", "Exception while loading Agent object from database. See inner exception for details.", ex);
            }
        }
Beispiel #2
0
        public Agent 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} " +
                             AgentTable.GetColumnNames("[a]") +
                             (this.Depth > 0 ? "," + UserTable.GetColumnNames("[a_u]") : string.Empty) +
                             (this.Depth > 1 ? "," + UserTypeTable.GetColumnNames("[a_u_ut]") : string.Empty) +
                             " FROM [core].[Agent] AS [a] ";
                if (this.Depth > 0)
                {
                    sqlCmdText += "INNER  JOIN [core].[User] AS [a_u] ON [a].[UserID] = [a_u].[UserID] ";
                }
                if (this.Depth > 1)
                {
                    sqlCmdText += "INNER  JOIN [core].[UserType] AS [a_u_ut] ON [a_u].[UserTypeID] = [a_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("a", "customload", "notfound"), "Agent 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);

                AgentTable    aTable      = new AgentTable(query);
                UserTable     a_uTable    = (this.Depth > 0) ? new UserTable(query) : null;
                UserTypeTable a_u_utTable = (this.Depth > 1) ? new UserTypeTable(query) : null;


                UserType a_u_utObject = (this.Depth > 1) ? a_u_utTable.CreateInstance() : null;
                User     a_uObject    = (this.Depth > 0) ? a_uTable.CreateInstance(a_u_utObject) : null;
                Agent    aObject      = aTable.CreateInstance(a_uObject);
                sqlReader.Close();

                return(aObject);
            }
            catch (Exception ex)
            {
                database.HandleException(ex);
                IMessageBuilder builder = new DbLogMessageBuilder(new LogErrorCode("a", "customload", "exception"), "Agent 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, "Agent", "Exception while loading (custom/single) Agent object from database. See inner exception for details.", ex);
            }
        }
Beispiel #3
0
        public List <UserDetail> 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} " +
                             UserDetailTable.GetColumnNames("[ud]") +
                             (this.Depth > 0 ? "," + UserTable.GetColumnNames("[ud_u]") : string.Empty) +
                             (this.Depth > 1 ? "," + UserTypeTable.GetColumnNames("[ud_u_ut]") : string.Empty) +
                             (this.Depth > 0 ? "," + GenderTable.GetColumnNames("[ud_g]") : string.Empty) +
                             " FROM [core].[UserDetail] AS [ud] ";
                if (this.Depth > 0)
                {
                    sqlCmdText += "INNER  JOIN [core].[User] AS [ud_u] ON [ud].[UserID] = [ud_u].[UserID] ";
                }
                if (this.Depth > 1)
                {
                    sqlCmdText += "INNER  JOIN [core].[UserType] AS [ud_u_ut] ON [ud_u].[UserTypeID] = [ud_u_ut].[UserTypeID] ";
                }
                if (this.Depth > 0)
                {
                    sqlCmdText += "INNER  JOIN [core].[Gender] AS [ud_g] ON [ud].[GenderID] = [ud_g].[GenderID] ";
                }


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

                SqlQuery query = new SqlQuery(sqlReader);

                UserDetailTable udTable      = new UserDetailTable(query);
                UserTable       ud_uTable    = (this.Depth > 0) ? new UserTable(query) : null;
                UserTypeTable   ud_u_utTable = (this.Depth > 1) ? new UserTypeTable(query) : null;
                GenderTable     ud_gTable    = (this.Depth > 0) ? new GenderTable(query) : null;

                List <UserDetail> result = new List <UserDetail>();
                do
                {
                    UserType   ud_u_utObject = (this.Depth > 1) ? ud_u_utTable.CreateInstance() : null;
                    User       ud_uObject    = (this.Depth > 0) ? ud_uTable.CreateInstance(ud_u_utObject) : null;
                    Gender     ud_gObject    = (this.Depth > 0) ? ud_gTable.CreateInstance() : null;
                    UserDetail udObject      = (this.Depth > -1) ? udTable.CreateInstance(ud_uObject, ud_gObject) : null;
                    result.Add(udObject);
                } while (sqlReader.Read());
                sqlReader.Close();

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