Пример #1
0
        /// <summary>
        /// Creates a primary key filter based on an entity.
        /// </summary>
        /// <param name="entity">The entity to read from.</param>
        /// <returns></returns>
        public ClassFilter CreatePrimaryKeyFilter(object entity)
        {
            if (_PrimaryKeys.Count == 0)
            {
                return(null);
            }
            else
            {
                ClassFilterTerm first = new ClassFilterTerm(
                    this,
                    _PrimaryKeys.First.Value,
                    base[_PrimaryKeys.First.Value].GetValue(entity));

                if (_PrimaryKeys.Count == 1)
                {
                    return(first);
                }
                else
                {
                    ClassFilterExpression exp = new ClassFilterExpression(first);
                    for (LinkedListNode <int> node = _PrimaryKeys.First.Next; node != null; node = node.Next)
                    {
                        exp.And(node.Value, base[node.Value].GetValue(entity));
                    }
                    return(exp);
                }
            }
        }
Пример #2
0
 public virtual void AppendFilterTerm(StringBuilder builder, DataAspect aspect, ClassFilterTerm term)
 {
     AppendName(builder, aspect[term.Ordinal].StoredName);
     switch (term.Comparison)
     {
         case FilterComparison.EqualTo:
             if (term.Value == null)
                 builder.Append(" IS NULL");
             else
             {
                 builder.Append(" = ");
                 AppendValue(builder, term.Value);
             }
             break;
         case FilterComparison.GreaterOrEqual:
             builder.Append(" >= ");
             AppendValue(builder, term.Value);
             break;
         case FilterComparison.GreaterThan:
             builder.Append(" > ");
             AppendValue(builder, term.Value);
             break;
         case FilterComparison.InCollection:
             builder.Append(" IN ");
             AppendValue(builder, term.Value);
             break;
         case FilterComparison.Like:
             builder.Append(" LIKE ");
             AppendValue(builder, term.Value);
             break;
         case FilterComparison.LowerOrEqual:
             builder.Append(" <= ");
             AppendValue(builder, term.Value);
             break;
         case FilterComparison.LowerThan:
             builder.Append(" < ");
             AppendValue(builder, term.Value);
             break;
         case FilterComparison.NotEqualTo:
             if (term.Value == null)
                 builder.Append(" IS NOT NULL");
             else
             {
                 builder.Append(" != ");
                 AppendValue(builder, term.Value);
             }
             break;
         case FilterComparison.NotInCollection:
             builder.Append(" NOT IN ");
             AppendValue(builder, term.Value);
             break;
         case FilterComparison.NotLike:
             builder.Append(" NOT LIKE ");
             AppendValue(builder, term.Value);
             break;
         default:
             throw new ArgumentException("Unknown filter comparion " + term.Comparison, "term");
     }
 }
Пример #3
0
        public virtual void AppendFilterTerm(StringBuilder builder, DataAspect aspect, ClassFilterTerm term)
        {
            AppendName(builder, aspect[term.Ordinal].StoredName);
            switch (term.Comparison)
            {
            case FilterComparison.EqualTo:
                if (term.Value == null)
                {
                    builder.Append(" IS NULL");
                }
                else
                {
                    builder.Append(" = ");
                    AppendValue(builder, term.Value);
                }
                break;

            case FilterComparison.GreaterOrEqual:
                builder.Append(" >= ");
                AppendValue(builder, term.Value);
                break;

            case FilterComparison.GreaterThan:
                builder.Append(" > ");
                AppendValue(builder, term.Value);
                break;

            case FilterComparison.InCollection:
                builder.Append(" IN ");
                AppendValue(builder, term.Value);
                break;

            case FilterComparison.Like:
                builder.Append(" LIKE ");
                AppendValue(builder, term.Value);
                break;

            case FilterComparison.LowerOrEqual:
                builder.Append(" <= ");
                AppendValue(builder, term.Value);
                break;

            case FilterComparison.LowerThan:
                builder.Append(" < ");
                AppendValue(builder, term.Value);
                break;

            case FilterComparison.NotEqualTo:
                if (term.Value == null)
                {
                    builder.Append(" IS NOT NULL");
                }
                else
                {
                    builder.Append(" != ");
                    AppendValue(builder, term.Value);
                }
                break;

            case FilterComparison.NotInCollection:
                builder.Append(" NOT IN ");
                AppendValue(builder, term.Value);
                break;

            case FilterComparison.NotLike:
                builder.Append(" NOT LIKE ");
                AppendValue(builder, term.Value);
                break;

            default:
                throw new ArgumentException("Unknown filter comparion " + term.Comparison, "term");
            }
        }
Пример #4
0
        /// <summary>
        /// Saves (updates or inserts) an entity.
        /// </summary>
        /// <param name="entity">The entity to save.</param>
        /// <returns>The number of records affected.</returns>
        public int Save(T entity)
        {
            int    identityOrdinal = _Aspect.IdentityOrdinal;
            object identityValue;

            _Mutex.WaitOne();
            try
            {
                //has identity ?
                if (identityOrdinal >= 0)
                {
                    if (EnsureIdentityValue(entity))
                    {
                        return(_Engine.Update(
                                   _Aspect,
                                   EnumerateValuesExcludingMember(_Aspect, entity, DataScope.Update, identityOrdinal),
                                   new ClassFilterTerm(_Aspect, identityOrdinal, _Aspect[identityOrdinal].GetValue(entity))));
                    }
                    else //insert with identity
                    {
                        int result = _Engine.Insert(
                            _Aspect,
                            EnumerateValuesExcludingMember(_Aspect, entity, DataScope.Insert, identityOrdinal),
                            out identityValue);

                        _Aspect[identityOrdinal].SetValue(entity, identityValue);
                        return(result);
                    }
                }
                else //non identity update
                {
                    foreach (int uniqueOrdinal in _Aspect.UniqueMemberOrdinals)
                    {
                        object uniqueValue = _Aspect[uniqueOrdinal].GetValue(entity);
                        if (!ValidationHelper.IsNullOrEmpty(uniqueValue))
                        {
                            ClassFilterTerm uniqueFilter = new ClassFilterTerm(_Aspect, uniqueOrdinal, uniqueValue);
                            if (_Engine.Exists(_Aspect, uniqueFilter))
                            {
                                return(_Engine.Update(
                                           _Aspect,
                                           EnumerateValuesExcludingMember(_Aspect, entity, DataScope.Update, uniqueOrdinal),
                                           uniqueFilter));
                            }
                        }
                    }

                    //primary key update
                    ClassFilter pkFilter = _Aspect.CreatePrimaryKeyFilter(entity);
                    if (pkFilter != null)
                    {
                        if (_Engine.Exists(_Aspect, pkFilter))
                        {
                            return(_Engine.Update(
                                       _Aspect,
                                       EnumerateValuesForUpdateByPrimaryKey(_Aspect, entity),
                                       pkFilter));
                        }
                    }

                    //just can't update
                    return(_Engine.Insert(
                               _Aspect,
                               EnumerateValuesExcludingMember(_Aspect, entity, DataScope.Insert, -1),
                               out identityValue));
                }
            }
            finally
            {
                _Mutex.ReleaseMutex();
            }
        }