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);
            }
        }
        public List <AgentLanguageMap> 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} " +
                             AgentLanguageMapTable.GetColumnNames("[alm]") +
                             (this.Depth > 0 ? "," + AgentTable.GetColumnNames("[alm_a]") : string.Empty) +
                             (this.Depth > 1 ? "," + UserTable.GetColumnNames("[alm_a_u]") : string.Empty) +
                             (this.Depth > 0 ? "," + LanguageTable.GetColumnNames("[alm_l]") : string.Empty) +
                             " FROM [core].[AgentLanguageMap] AS [alm] ";
                if (this.Depth > 0)
                {
                    sqlCmdText += "INNER  JOIN [core].[Agent] AS [alm_a] ON [alm].[AgentID] = [alm_a].[AgentID] ";
                }
                if (this.Depth > 1)
                {
                    sqlCmdText += "INNER  JOIN [core].[User] AS [alm_a_u] ON [alm_a].[UserID] = [alm_a_u].[UserID] ";
                }
                if (this.Depth > 0)
                {
                    sqlCmdText += "INNER  JOIN [core].[Language] AS [alm_l] ON [alm].[LanguageID] = [alm_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("alm", "customloadmany", "notfound"), "AgentLanguageMap 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 <AgentLanguageMap>());
                }

                SqlQuery query = new SqlQuery(sqlReader);

                AgentLanguageMapTable almTable     = new AgentLanguageMapTable(query);
                AgentTable            alm_aTable   = (this.Depth > 0) ? new AgentTable(query) : null;
                UserTable             alm_a_uTable = (this.Depth > 1) ? new UserTable(query) : null;
                LanguageTable         alm_lTable   = (this.Depth > 0) ? new LanguageTable(query) : null;

                List <AgentLanguageMap> result = new List <AgentLanguageMap>();
                do
                {
                    User             alm_a_uObject = (this.Depth > 1) ? alm_a_uTable.CreateInstance() : null;
                    Agent            alm_aObject   = (this.Depth > 0) ? alm_aTable.CreateInstance(alm_a_uObject) : null;
                    Language         alm_lObject   = (this.Depth > 0) ? alm_lTable.CreateInstance() : null;
                    AgentLanguageMap almObject     = (this.Depth > -1) ? almTable.CreateInstance(alm_aObject, alm_lObject) : null;
                    result.Add(almObject);
                } while (sqlReader.Read());
                sqlReader.Close();

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