コード例 #1
0
        public MobileOperatorCode 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} " +
                             MobileOperatorCodeTable.GetColumnNames("[moc]") +
                             (this.Depth > 0 ? "," + MobileOperatorTable.GetColumnNames("[moc_mo]") : string.Empty) +
                             (this.Depth > 1 ? "," + CountryTable.GetColumnNames("[moc_mo_c]") : string.Empty) +
                             " FROM [core].[MobileOperatorCode] AS [moc] ";
                if (this.Depth > 0)
                {
                    sqlCmdText += "INNER  JOIN [core].[MobileOperator] AS [moc_mo] ON [moc].[MobileOperatorID] = [moc_mo].[MobileOperatorID] ";
                }
                if (this.Depth > 1)
                {
                    sqlCmdText += "INNER  JOIN [core].[Country] AS [moc_mo_c] ON [moc_mo].[CountryID] = [moc_mo_c].[CountryID] ";
                }


                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("moc", "customload", "notfound"), "MobileOperatorCode 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);

                MobileOperatorCodeTable mocTable      = new MobileOperatorCodeTable(query);
                MobileOperatorTable     moc_moTable   = (this.Depth > 0) ? new MobileOperatorTable(query) : null;
                CountryTable            moc_mo_cTable = (this.Depth > 1) ? new CountryTable(query) : null;


                Country            moc_mo_cObject = (this.Depth > 1) ? moc_mo_cTable.CreateInstance() : null;
                MobileOperator     moc_moObject   = (this.Depth > 0) ? moc_moTable.CreateInstance(moc_mo_cObject) : null;
                MobileOperatorCode mocObject      = mocTable.CreateInstance(moc_moObject);
                sqlReader.Close();

                return(mocObject);
            }
            catch (Exception ex)
            {
                database.HandleException(ex);
                IMessageBuilder builder = new DbLogMessageBuilder(new LogErrorCode("moc", "customload", "exception"), "MobileOperatorCode 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, "MobileOperatorCode", "Exception while loading (custom/single) MobileOperatorCode object from database. See inner exception for details.", ex);
            }
        }
コード例 #2
0
        protected override MobileOperatorCode 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 " +
                             MobileOperatorCodeTable.GetColumnNames("[moc]") +
                             (this.Depth > 0 ? "," + MobileOperatorTable.GetColumnNames("[moc_mo]") : string.Empty) +
                             (this.Depth > 1 ? "," + CountryTable.GetColumnNames("[moc_mo_c]") : string.Empty) +
                             " FROM [core].[MobileOperatorCode] AS [moc] ";
                if (this.Depth > 0)
                {
                    sqlCmdText += "INNER  JOIN [core].[MobileOperator] AS [moc_mo] ON [moc].[MobileOperatorID] = [moc_mo].[MobileOperatorID] ";
                }
                if (this.Depth > 1)
                {
                    sqlCmdText += "INNER  JOIN [core].[Country] AS [moc_mo_c] ON [moc_mo].[CountryID] = [moc_mo_c].[CountryID] ";
                }
                sqlCmdText += "WHERE [moc].[MobileOperatorCodeID] = @MobileOperatorCodeID;";

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

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

                MobileOperatorCodeTable mocTable      = new MobileOperatorCodeTable(query);
                MobileOperatorTable     moc_moTable   = (this.Depth > 0) ? new MobileOperatorTable(query) : null;
                CountryTable            moc_mo_cTable = (this.Depth > 1) ? new CountryTable(query) : null;


                Country            moc_mo_cObject = (this.Depth > 1) ? moc_mo_cTable.CreateInstance() : null;
                MobileOperator     moc_moObject   = (this.Depth > 0) ? moc_moTable.CreateInstance(moc_mo_cObject) : null;
                MobileOperatorCode mocObject      = mocTable.CreateInstance(moc_moObject);
                sqlReader.Close();

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