Exemplo n.º 1
0
        internal static SqlCommand BuildEntitySetQuery(EntityQueryContext context, IEntity entity, RelationProperty relationProperty)
        {
            var relationKey = RelationshipUnity.GetRelationship(relationProperty);

            if (relationKey == null || relationKey.Style != RelationshipStyle.One2Many)
            {
                return(null);
            }

            var query = new EntityQueryBuilder(context, relationProperty.RelationType)
                        .Select().All().From();

            var valid = true;

            foreach (var key in relationKey.Keys)
            {
                var val = entity.InternalGetValue(key.ThisProperty);
                if (PropertyValue.IsNullOrEmpty(val) || !val.IsValid)
                {
                    valid = false;
                    continue;
                }

                query = query.And(key.OtherProperty, entity.InternalGetValue(key.ThisProperty));
            }

            if (!valid)
            {
                return(string.Empty);
            }

            return(query.ToSqlCommand());
        }
Exemplo n.º 2
0
        internal static SqlCommand BuildGetFirstQuery(EntityQueryContext context, Type entityType, object[] primaryValues)
        {
            var query = new EntityQueryBuilder(context, entityType)
                        .Select().All().From();

            query = BuildPrimaryKeyCondition(query, entityType, primaryValues);
            return(query.ToSqlCommand());
        }
Exemplo n.º 3
0
        internal static SqlCommand BuildDeleteQuery(EntityQueryContext context, Type entityType, object[] primaryValues)
        {
            var query = new EntityQueryBuilder(context, entityType)
                        .Delete();

            query = BuildPrimaryKeyCondition(query, entityType, primaryValues);
            return(query.ToSqlCommand());
        }
Exemplo n.º 4
0
        internal static SqlCommand BuildDeleteQuery(EntityQueryContext context, IEntity entity)
        {
            var query = new EntityQueryBuilder(context, entity.EntityType)
                        .Delete();

            query = BuildPrimaryKeyCondition(query, entity);
            return(query.ToSqlCommand());
        }
Exemplo n.º 5
0
        internal static SqlCommand BuildUpdateFakeDeleteQuery(EntityQueryContext context, IEntity entity, IProperty fakeProperty)
        {
            var query = new EntityQueryBuilder(context, entity.EntityType)
                        .Update().Set(new QueryValue {
                Property = fakeProperty, Value = 1
            });

            query = BuildPrimaryKeyCondition(query, entity);
            return(query.ToSqlCommand());
        }
Exemplo n.º 6
0
        internal static SqlCommand BuildReferenceQuery(EntityQueryContext context, IEntity entity, ReferenceProperty referenceProperty)
        {
            var relationKey = RelationshipUnity.GetRelationship(referenceProperty);

            if (relationKey == null)
            {
                return(null);
            }
            var query = new EntityQueryBuilder(context, referenceProperty.RelationType)
                        .Select().Single(referenceProperty.Reference).From();

            foreach (var key in relationKey.Keys)
            {
                query = query.And(key.ThisProperty, entity.InternalGetValue(key.OtherProperty));
            }
            return(query.ToSqlCommand());
        }
Exemplo n.º 7
0
        internal static SqlCommand BuildEntityQuery(EntityQueryContext context, IEntity entity, RelationProperty relationProperty)
        {
            var relationKey = RelationshipUnity.GetRelationship(relationProperty);

            if (relationKey == null)
            {
                return(null);
            }

            Type relationType;
            Func <RelationshipKey, IProperty> func1, func2;

            if (entity.EntityType == relationKey.ThisType)
            {
                relationType = relationKey.OtherType;
                func1        = key => key.OtherProperty;
                func2        = key => key.ThisProperty;
            }
            else
            {
                relationType = relationKey.ThisType;
                func1        = key => key.ThisProperty;
                func2        = key => key.OtherProperty;
            }

            var query = new EntityQueryBuilder(context, relationType)
                        .Select().All().From();

            foreach (var key in relationKey.Keys)
            {
                var val = entity.InternalGetValue(func2(key));
                if (PropertyValue.IsNullOrEmpty(val) || !val.IsValid)
                {
                    return(string.Empty);
                }
                query = query.And(func1(key), val);
            }
            return(query.ToSqlCommand());
        }
Exemplo n.º 8
0
        internal static SqlCommand BuidCreateQuery(EntityQueryContext context, IEntity entity)
        {
            var properties = GetInsertProperties(entity);

            if (properties.Count == 0)
            {
                return(string.Empty);
            }

            var values = new QueryValue[properties.Count];

            for (var i = 0; i < values.Length; i++)
            {
                values[i] = new QueryValue {
                    Property = properties[i], Value = GetInsertValue(context, entity, properties[i])
                };
            }

            var query = new EntityQueryBuilder(context, entity.EntityType)
                        .Insert().Set(values);

            return(query.ToSqlCommand());
        }