예제 #1
0
        public Report 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} " +
                             ReportTable.GetColumnNames("[r]") +
                             (this.Depth > 0 ? "," + ReportLinkTable.GetColumnNames("[r_rl]") : string.Empty) +
                             (this.Depth > 1 ? "," + ReportLinkGroupTable.GetColumnNames("[r_rl_rlg]") : string.Empty) +
                             " FROM [core].[Report] AS [r] ";
                if (this.Depth > 0)
                {
                    sqlCmdText += "INNER  JOIN [core].[ReportLink] AS [r_rl] ON [r].[ReportLinkID] = [r_rl].[ReportLinkID] ";
                }
                if (this.Depth > 1)
                {
                    sqlCmdText += "INNER  JOIN [core].[ReportLinkGroup] AS [r_rl_rlg] ON [r_rl].[ReportLinkGroupID] = [r_rl_rlg].[ReportLinkGroupID] ";
                }


                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("r", "customload", "notfound"), "Report 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);

                ReportTable          rTable        = new ReportTable(query);
                ReportLinkTable      r_rlTable     = (this.Depth > 0) ? new ReportLinkTable(query) : null;
                ReportLinkGroupTable r_rl_rlgTable = (this.Depth > 1) ? new ReportLinkGroupTable(query) : null;


                ReportLinkGroup r_rl_rlgObject = (this.Depth > 1) ? r_rl_rlgTable.CreateInstance() : null;
                ReportLink      r_rlObject     = (this.Depth > 0) ? r_rlTable.CreateInstance(r_rl_rlgObject) : null;
                Report          rObject        = rTable.CreateInstance(r_rlObject);
                sqlReader.Close();

                return(rObject);
            }
            catch (Exception ex)
            {
                database.HandleException(ex);
                IMessageBuilder builder = new DbLogMessageBuilder(new LogErrorCode("r", "customload", "exception"), "Report 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, "Report", "Exception while loading (custom/single) Report object from database. See inner exception for details.", ex);
            }
        }
예제 #2
0
        protected override Report 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 " +
                             ReportTable.GetColumnNames("[r]") +
                             (this.Depth > 0 ? "," + ReportLinkTable.GetColumnNames("[r_rl]") : string.Empty) +
                             (this.Depth > 1 ? "," + ReportLinkGroupTable.GetColumnNames("[r_rl_rlg]") : string.Empty) +
                             " FROM [core].[Report] AS [r] ";
                if (this.Depth > 0)
                {
                    sqlCmdText += "INNER  JOIN [core].[ReportLink] AS [r_rl] ON [r].[ReportLinkID] = [r_rl].[ReportLinkID] ";
                }
                if (this.Depth > 1)
                {
                    sqlCmdText += "INNER  JOIN [core].[ReportLinkGroup] AS [r_rl_rlg] ON [r_rl].[ReportLinkGroupID] = [r_rl_rlg].[ReportLinkGroupID] ";
                }
                sqlCmdText += "WHERE [r].[ReportID] = @ReportID;";

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

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

                ReportTable          rTable        = new ReportTable(query);
                ReportLinkTable      r_rlTable     = (this.Depth > 0) ? new ReportLinkTable(query) : null;
                ReportLinkGroupTable r_rl_rlgTable = (this.Depth > 1) ? new ReportLinkGroupTable(query) : null;


                ReportLinkGroup r_rl_rlgObject = (this.Depth > 1) ? r_rl_rlgTable.CreateInstance() : null;
                ReportLink      r_rlObject     = (this.Depth > 0) ? r_rlTable.CreateInstance(r_rl_rlgObject) : null;
                Report          rObject        = rTable.CreateInstance(r_rlObject);
                sqlReader.Close();

                return(rObject);
            }
            catch (Exception ex)
            {
                database.HandleException(ex);
                IMessageBuilder builder = new DbLogMessageBuilder(new LogErrorCode("r", "loadinternal", "exception"), "Report 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, "Report", "Exception while loading Report object from database. See inner exception for details.", ex);
            }
        }