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()); }
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()); }
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()); }
internal static SqlCommand BuildDeleteQuery(EntityQueryContext context, IEntity entity) { var query = new EntityQueryBuilder(context, entity.EntityType) .Delete(); query = BuildPrimaryKeyCondition(query, entity); return(query.ToSqlCommand()); }
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()); }
private static EntityQueryBuilder BuildPrimaryKeyCondition(EntityQueryBuilder query, IEntity entity) { var primaryKeys = PropertyUnity.GetPrimaryProperties(entity.EntityType.GetRootType()); if (primaryKeys.IsNullOrEmpty()) { throw new Exception(SR.GetString(SRKind.NotDefinedPrimaryKey)); } foreach (var property in primaryKeys) { query = query.And(property, entity.InternalGetValue(property)); } return(query); }
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()); }
private static EntityQueryBuilder BuildPrimaryKeyCondition(EntityQueryBuilder query, Type entityType, object[] primaryValues) { var primaryKeys = PropertyUnity.GetPrimaryProperties(entityType.GetRootType()); if (primaryKeys.IsNullOrEmpty()) { throw new Exception(SR.GetString(SRKind.NotDefinedPrimaryKey)); } var i = 0; foreach (var property in primaryKeys) { query = query.And(property, primaryValues[i++]); } return(query); }
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()); }
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()); }
internal EntityQueryBuilder From(EntityQueryBuilder builder) { m_builder.AppendFormat(" FROM ({0}) T", builder); return(this); }