예제 #1
0
        public IPCountryMap 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} " +
                             IPCountryMapTable.GetColumnNames("[ipcm]") +
                             (this.Depth > 0 ? "," + CountryTable.GetColumnNames("[ipcm_c]") : string.Empty) +
                             (this.Depth > 1 ? "," + LanguageTable.GetColumnNames("[ipcm_c_l]") : string.Empty) +
                             " FROM [core].[IPCountryMap] AS [ipcm] ";
                if (this.Depth > 0)
                {
                    sqlCmdText += "LEFT OUTER  JOIN [core].[Country] AS [ipcm_c] ON [ipcm].[CountryID] = [ipcm_c].[CountryID] ";
                }
                if (this.Depth > 1)
                {
                    sqlCmdText += "LEFT OUTER  JOIN [core].[Language] AS [ipcm_c_l] ON [ipcm_c].[LanguageID] = [ipcm_c_l].[LanguageID] ";
                }


                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("ipcm", "customload", "notfound"), "IPCountryMap 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);

                IPCountryMapTable ipcmTable     = new IPCountryMapTable(query);
                CountryTable      ipcm_cTable   = (this.Depth > 0) ? new CountryTable(query) : null;
                LanguageTable     ipcm_c_lTable = (this.Depth > 1) ? new LanguageTable(query) : null;


                Language     ipcm_c_lObject = (this.Depth > 1) ? ipcm_c_lTable.CreateInstance() : null;
                Country      ipcm_cObject   = (this.Depth > 0) ? ipcm_cTable.CreateInstance(ipcm_c_lObject) : null;
                IPCountryMap ipcmObject     = ipcmTable.CreateInstance(ipcm_cObject);
                sqlReader.Close();

                return(ipcmObject);
            }
            catch (Exception ex)
            {
                database.HandleException(ex);
                IMessageBuilder builder = new DbLogMessageBuilder(new LogErrorCode("ipcm", "customload", "exception"), "IPCountryMap 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, "IPCountryMap", "Exception while loading (custom/single) IPCountryMap object from database. See inner exception for details.", ex);
            }
        }
예제 #2
0
        protected override IPCountryMap 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 " +
                             IPCountryMapTable.GetColumnNames("[ipcm]") +
                             (this.Depth > 0 ? "," + CountryTable.GetColumnNames("[ipcm_c]") : string.Empty) +
                             (this.Depth > 1 ? "," + LanguageTable.GetColumnNames("[ipcm_c_l]") : string.Empty) +
                             " FROM [core].[IPCountryMap] AS [ipcm] ";
                if (this.Depth > 0)
                {
                    sqlCmdText += "LEFT OUTER  JOIN [core].[Country] AS [ipcm_c] ON [ipcm].[CountryID] = [ipcm_c].[CountryID] ";
                }
                if (this.Depth > 1)
                {
                    sqlCmdText += "LEFT OUTER  JOIN [core].[Language] AS [ipcm_c_l] ON [ipcm_c].[LanguageID] = [ipcm_c_l].[LanguageID] ";
                }
                sqlCmdText += "WHERE [ipcm].[IPCountryMapID] = @IPCountryMapID;";

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

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

                IPCountryMapTable ipcmTable     = new IPCountryMapTable(query);
                CountryTable      ipcm_cTable   = (this.Depth > 0) ? new CountryTable(query) : null;
                LanguageTable     ipcm_c_lTable = (this.Depth > 1) ? new LanguageTable(query) : null;


                Language     ipcm_c_lObject = (this.Depth > 1) ? ipcm_c_lTable.CreateInstance() : null;
                Country      ipcm_cObject   = (this.Depth > 0) ? ipcm_cTable.CreateInstance(ipcm_c_lObject) : null;
                IPCountryMap ipcmObject     = ipcmTable.CreateInstance(ipcm_cObject);
                sqlReader.Close();

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