Пример #1
0
        internal void SetLoaded()
        {
            _IsLoaded = true;
            _IsDirty = false;
            _IsNew = false;
            _IsDeleted = false;

            _cleanObject = null;
        }
Пример #2
0
 protected void SetDirty()
 {
     _log.Debug("SetDirty: Setting dirty flag");
     _IsDirty = true;
     if (_cleanObject == null)
     {
         // Save a copy of the clean object for later updating
         _cleanObject = (TimeSheetBase)this.MemberwiseClone();
     }
 }
Пример #3
0
 public void LoadSingle(TimeSheetBase obj, string where, params object[] args)
 {
     GetDataLink().LoadSingle(obj, (this.User == null) ? null : this.User.userContext, where, args);
 }
Пример #4
0
        public bool Update(TimeSheetContext context)
        {
            if (!_IsDirty)
            {
                return false;
            }

            bool result = GetDataLink().Update(this, context);
            _IsLoaded = true;
            _IsDirty = false;
            _IsNew = false;
            _IsDeleted = false;

            _cleanObject = null;
            return result;
        }
Пример #5
0
        private string CreateUpdateStatement(TimeSheetBase o, IList fieldNamesToSet)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("UPDATE ").Append(TableName);
            sql.Append(LockingHintClause);
            sql.Append(" SET ");
            bool first = true;
            foreach (TimeSheetFieldMap mapping in _fieldMappings.Values)
            {
                if (mapping.FieldName == PrimaryKey)
                {
                    continue;
                }
                if (o.CleanObject == null ||
                    fieldNamesToSet.Contains(mapping.FieldName))
                {
                    if (first)
                    {
                        first = false;
                    }
                    else
                    {
                        sql.Append(", ");
                    }
                    sql.Append(mapping.FieldName).Append(" = @").Append(mapping.FieldName);
                }
            }

            sql.Append(" WHERE ").Append(PrimaryKey).Append(" = @guidfield");

            string sqlString = sql.ToString();
            _log.Debug("SQL Update Statement: " + sqlString);
            return sqlString;
        }
Пример #6
0
        public virtual bool Insert(TimeSheetContext context)
        {
            bool result = false;
            result = GetDataLink().Insert(this, context);

            _IsLoaded = true;
            _IsDirty = false;
            _IsNew = false;
            _IsDeleted = false;

            _cleanObject = null;

            return result;
        }
Пример #7
0
        public bool Update(TimeSheetBase o, TimeSheetContext contextParam)
        {
            bool result = false;
            TimeSheetContext context = TimeSheetContext.Create(contextParam);
            SqlCommand command = null;
            string sql = string.Empty;
            try
            {
                command = context.CreateCommand(string.Empty);

                TimeSheetBase clean = o.CleanObject;
                ArrayList fieldNamesToSet = new ArrayList();
                object guidvalue = null;

                foreach (TimeSheetFieldMap fieldMap in _fieldMappings.Values)
                {
                    _log.Debug("Update: fieldMap.AttributeName = " + fieldMap.AttributeName);
                    if (fieldMap.FieldName == PrimaryKey)
                    {
                        guidvalue = fieldMap.FieldInfo.GetValue(o);
                    }
                    // if the attribute value is the same as the clean object's value, don't add the parameter
                    object attributeValue = fieldMap.FieldInfo.GetValue(o);
                    if (clean == null ||
                        !object.Equals(attributeValue, fieldMap.FieldInfo.GetValue(clean)))
                    {
                        fieldNamesToSet.Add(fieldMap.FieldName);
                        command.Parameters.Add(new SqlParameter("@" + fieldMap.FieldName, attributeValue));
                        _log.Debug("Update(): Fieldname: " + fieldMap.FieldName + ", Value: " + attributeValue);
                    }
                }

                if (fieldNamesToSet.Count == 0)
                {
                    return false;  // No changes persisted
                }

                // Add the guidfield parameter
                SqlParameter guidParam = new SqlParameter("@guidfield", SqlDbType.UniqueIdentifier, 40);
                guidParam.Value = guidvalue;
                command.Parameters.Add(guidParam);

                // Set the sql for the command to run
                command.CommandText = CreateUpdateStatement(o, fieldNamesToSet);
                context.OpenConnection();
                ExecuteNonQuery(command);
                result = true;
            }
            catch (SqlException e)
            {
                LogSqlException("Exception caught in Update(TimeSheetBase, TimeSheetContext).", e);
            }
            catch (Exception)
            {
                LogSqlCommand(LogLevel.Error, sql, command);
            }
            finally
            {
                context.CloseConnection(contextParam);
            }

            return result;
        }
Пример #8
0
        private string CreateInsertStatement(TimeSheetBase o)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("INSERT INTO ").Append(TableName);
            sql.Append(LockingHintClause);
            sql.Append(" ( ");
            TimeSheetBase clean = o.CleanObject;
            bool first = true;
            foreach (TimeSheetFieldMap mapping in _fieldMappings.Values)
            {
                object value = mapping.FieldInfo.GetValue(o);
                if (clean == null ||
                    !object.Equals(value, mapping.FieldInfo.GetValue(clean)))
                {
                    if (first)
                    {
                        first = false;
                    }
                    else
                    {
                        sql.Append(", ");
                    }
                    sql.Append(mapping.FieldName);
                }
            }

            sql.Append(" ) VALUES ( ");

            first = true;
            foreach (TimeSheetFieldMap mapping in _fieldMappings.Values)
            {
                object value = mapping.FieldInfo.GetValue(o);
                if (clean == null ||
                    !object.Equals(value, mapping.FieldInfo.GetValue(clean)))
                {
                    if (first)
                    {
                        first = false;
                    }
                    else
                    {
                        sql.Append(", ");
                    }
                    sql.Append("@").Append(mapping.FieldName);
                }
            }
            sql.Append(" )");

            string sqlString = sql.ToString();
            _log.Debug("SQL Insert Statement: " + sqlString);
            return sqlString;
        }
Пример #9
0
        public void LoadSingle(TimeSheetBase o, TimeSheetContext contextParam, string where, params object[] args)
        {
            _log.Debug("Load stack trace: " + new StackTrace(true));
            TimeSheetContext context = TimeSheetContext.Create(contextParam);
            string sql = string.Empty;
            try
            {
                sql = CreateSelectStatement(where);
                SqlCommand command = context.CreateCommand(sql);
                if (args.Length > 1)
                {
                    int maxLength = (args.Length / 2);
                    maxLength = maxLength != 0 ? maxLength - 1 : 0;
                    int Cnt = 0;
                    for (int PCnt = 0; PCnt <= maxLength; PCnt++)
                    {
                        SqlParameter oParam = new SqlParameter("@" + args[Cnt].ToString(), args[Cnt + 1]);
                        command.Parameters.Add(oParam);
                        Cnt += 2;
                    }
                }
                context.OpenConnection();

                using (IDataReader dataReader = ExecuteReader(command))
                {
                    if (dataReader.Read())
                    {
                        o.SetAttributes(dataReader);
                        o.SetLoaded();
                    }
                    else
                    {
                        string message = string.Format("No object read from database; table = {0}, guidfield = {1}", TableName, PrimaryKey);
                    }
                }
            }
            catch (SqlException e)
            {
                LogSqlException("Exception caught in Load().", e);
            }
            catch (Exception)
            {
                LogSqlCommand(LogLevel.Error, sql, null);
            }
            finally
            {
                context.CloseConnection(contextParam);
            }
        }
Пример #10
0
 public TimeSheetFieldMap TimeSheetField(TimeSheetBase obj, string fieldName)
 {
     TimeSheetFieldMap result = null;
     foreach (TimeSheetFieldMap mapping in _fieldMappings.Values)
     {
         if (mapping.FieldName.Equals(fieldName, StringComparison.InvariantCultureIgnoreCase))
         {
             result = mapping;
             break;
         }
     }
     return result;
 }
Пример #11
0
        public List<TimeSheetBase> Load(TimeSheetBase baseObj, TimeSheetContext contextParam, string where, string orderby, params object[] args)
        {
            List<TimeSheetBase> Result = new List<TimeSheetBase>();
            TimeSheetContext context = TimeSheetContext.Create(contextParam);
            string sql = string.Empty;
            try
            {
                sql = CreateSelectStatement(where, orderby);
                SqlCommand command = context.CreateCommand(sql);
                if (args.Length > 1)
                {
                    int maxLength = (args.Length / 2);
                    maxLength = maxLength != 0 ? maxLength - 1 : 0;
                    int SCnt = 0;
                    for (int PCnt = 0; PCnt <= maxLength; PCnt++)
                    {
                        SqlParameter oParam = new SqlParameter("@" + args[SCnt].ToString(), args[SCnt + 1]);
                        command.Parameters.Add(oParam);
                        SCnt += 2;
                    }
                }
                context.OpenConnection();

                using (IDataReader dataReader = ExecuteReader(command))
                {
                    while (dataReader.Read())
                    {
                        TimeSheetBase obj = (TimeSheetBase)Activator.CreateInstance(baseObj.GetType(), new object[] { baseObj.User });
                        obj.SetAttributes(dataReader);
                        obj.SetLoaded();
                        Result.Add(obj);
                    }
                }
            }
            catch (SqlException e)
            {
                LogSqlException("Exception caught in Load().", e);
            }
            catch (Exception)
            {
                LogSqlCommand(LogLevel.Error, sql, null);
            }
            finally
            {
                context.CloseConnection(contextParam);
            }
            return Result;
        }
Пример #12
0
        // Inserts a MQ object into the database
        public bool Insert(TimeSheetBase o, TimeSheetContext contextParam)
        {
            bool result = false;
            TimeSheetContext context = TimeSheetContext.Create(contextParam);
            SqlCommand command = null;
            string sql = string.Empty;

            try
            {
                TimeSheetBase clean = o.CleanObject;
                sql = CreateInsertStatement(o);
                command = context.CreateCommand(sql);
                foreach (TimeSheetFieldMap mapping in _fieldMappings.Values)
                {
                    _log.Debug("Create(): Getting direct value for " + mapping.FieldName);
                    object value = mapping.FieldInfo.GetValue(o);
                    if (clean == null ||
                        !object.Equals(value, mapping.FieldInfo.GetValue(clean)))
                    {
                        if (value == null)
                        {
                            value = DBNull.Value;
                        }
                        string paramName = "@" + mapping.FieldName;
                        SqlParameter param = new SqlParameter(paramName, value);
                        _log.Debug("Create(): SqlParam = " + param);
                        command.Parameters.Add(param);
                        _log.Debug("Create(): SqlParameter added");
                    }
                }

                context.OpenConnection();
                ExecuteNonQuery(command);
                result = true;
            }
            catch (SqlException e)
            {
                LogSqlException("Exception caught in Create().", e);
            }
            catch (Exception)
            {
                LogSqlCommand(LogLevel.Error, sql, command);
            }
            finally
            {
                context.CloseConnection(contextParam);
            }

            return result;
        }
Пример #13
0
 public virtual ICollection FindReferencesTo(TimeSheetContext context, TimeSheetBase obj)
 {
     return EmptyList;
 }
Пример #14
0
 public object fieldValue(TimeSheetBase obj, string fieldName)
 {
     bool result = false;
     object fieldVal = null;
     foreach (TimeSheetFieldMap mapping in _fieldMappings.Values)
     {
         result = mapping.FieldName.Equals(fieldName, StringComparison.InvariantCultureIgnoreCase);
         if (result)
         {
             fieldVal = mapping.FieldInfo.GetValue(obj);
             break;
         }
     }
     return fieldVal;
 }
Пример #15
0
        public bool Delete(TimeSheetBase o, TimeSheetContext contextParam)
        {
            bool result = false;
            TimeSheetContext context = TimeSheetContext.Create(contextParam);
            SqlCommand command = null;
            string deleteStatement = string.Empty;
            try
            {
                deleteStatement = CreateDeleteStatement();
                command = context.CreateCommand(deleteStatement);
                SqlParameter guidParam = new SqlParameter("@guidfield", SqlDbType.UniqueIdentifier, 40);
                guidParam.Value = this.fieldValue(o, this.PrimaryKey);
                command.Parameters.Add(guidParam);

                context.OpenConnection();
                ExecuteNonQuery(command);
                result = true;
            }
            catch (SqlException e)
            {
                LogSqlException("Exception caught in Delete().", e);
            }
            catch (Exception)
            {
                LogSqlCommand(LogLevel.Error, deleteStatement, command);
            }
            finally
            {
                context.CloseConnection(contextParam);
            }

            return result;
        }