public virtual Net.Vpc.Upa.Impl.Persistence.QueryExecutor Execute() /* throws Net.Vpc.Upa.Exceptions.UPAException */ { int c1 = 0; int c2 = 0; string oldAlias = baseExpression.GetEntityAlias(); if (oldAlias == null) { oldAlias = entity.GetName(); } bool replaceThis = !"this".Equals(oldAlias); if (baseExpression.CountFields() > 0) { if (replaceThis) { Net.Vpc.Upa.Impl.Uql.Util.UQLUtils.ReplaceThisVar(baseExpression, oldAlias, context.GetPersistenceUnit()); } c1 = defaultPersistenceStore.CreateDefaultExecutor(baseExpression, parametersByName, parametersByIndex, updatable, defaultFieldFilter, context).Execute().GetResultCount(); } if ((complexVals).Count > 0) { Net.Vpc.Upa.Expressions.Select q = new Net.Vpc.Upa.Expressions.Select(); foreach (Net.Vpc.Upa.Field primaryField in entity.GetPrimaryFields()) { q.Field(primaryField.GetName()); } foreach (Net.Vpc.Upa.Expressions.VarVal f in complexVals) { Net.Vpc.Upa.Expressions.Expression fieldExpression = f.GetVal(); if (replaceThis) { Net.Vpc.Upa.Impl.Uql.Util.UQLUtils.ReplaceThisVar(fieldExpression, oldAlias, context.GetPersistenceUnit()); } q.Field(fieldExpression, f.GetVar().GetName()); } q.From(entity.GetName(), oldAlias); Net.Vpc.Upa.Expressions.Expression cond = baseExpression.GetCondition(); q.SetWhere(cond == null ? null : cond.Copy()); Net.Vpc.Upa.EntityBuilder eb = entity.GetBuilder(); foreach (Net.Vpc.Upa.Record record in entity.GetPersistenceUnit().CreateQuery(q).GetRecordList()) { Net.Vpc.Upa.Expressions.Update u2 = new Net.Vpc.Upa.Expressions.Update(); u2.Entity(entityName); foreach (Net.Vpc.Upa.Expressions.VarVal f in complexVals) { string fname = f.GetVar().GetName(); u2.Set(fname, record.GetObject <T>(fname)); } Net.Vpc.Upa.Expressions.Expression exprId = eb.ObjectToIdExpression(record, oldAlias); u2.Where(exprId); c2 += defaultPersistenceStore.CreateDefaultExecutor(u2, parametersByName, parametersByIndex, updatable, defaultFieldFilter, context).Execute().GetResultCount(); } } resultCount = System.Math.Max(c1, c2); return(this); }
public virtual int Update(Net.Vpc.Upa.Entity entity, Net.Vpc.Upa.Persistence.EntityExecutionContext context, Net.Vpc.Upa.Record updates, Net.Vpc.Upa.Expressions.Expression condition) /* throws Net.Vpc.Upa.Exceptions.UPAException */ { Net.Vpc.Upa.Expressions.Update u = new Net.Vpc.Upa.Expressions.Update().Entity(entity.GetName()); foreach (string fieldName in updates.KeySet()) { Net.Vpc.Upa.Field f = entity.FindField(fieldName); if (f != null && Net.Vpc.Upa.Impl.Util.Filters.Fields2.UPDATE.Accept(f)) { object @value = updates.GetObject <T>(fieldName); if ((f.GetDataType() is Net.Vpc.Upa.Types.ManyToOneType)) { Net.Vpc.Upa.Types.ManyToOneType e = (Net.Vpc.Upa.Types.ManyToOneType)f.GetDataType(); if (e.IsUpdatable()) { Net.Vpc.Upa.Entity masterEntity = context.GetPersistenceUnit().GetEntity(e.GetTargetEntityName()); Net.Vpc.Upa.EntityBuilder mbuilder = masterEntity.GetBuilder(); if (@value is Net.Vpc.Upa.Expressions.Expression) { Net.Vpc.Upa.Expressions.Expression evalue; System.Collections.Generic.IList <Net.Vpc.Upa.Field> sfields = e.GetRelationship().GetSourceRole().GetFields(); System.Collections.Generic.IList <Net.Vpc.Upa.Field> tfields = e.GetRelationship().GetTargetRole().GetFields(); for (int i = 0; i < (sfields).Count; i++) { Net.Vpc.Upa.Field fk = sfields[i]; Net.Vpc.Upa.Field fid = tfields[i]; evalue = ((Net.Vpc.Upa.Expressions.Expression)@value).Copy(); evalue = context.GetPersistenceUnit().GetExpressionManager().ParseExpression(evalue); if (evalue is Net.Vpc.Upa.Expressions.Select) { Net.Vpc.Upa.Expressions.Select svalue = (Net.Vpc.Upa.Expressions.Select)evalue; if (svalue.CountFields() != 1) { throw new System.Exception("Invalid Expression " + svalue + " as formula for field " + f.GetAbsoluteName()); } if (svalue.GetField(0).GetExpression() is Net.Vpc.Upa.Expressions.Var) { svalue.GetField(0).SetExpression(new Net.Vpc.Upa.Expressions.Var((Net.Vpc.Upa.Expressions.Var)svalue.GetField(0).GetExpression(), fid.GetName())); } else { throw new System.Exception("Invalid Expression " + svalue + " as formula for field " + f.GetAbsoluteName()); } } else if (evalue is Net.Vpc.Upa.Expressions.Var) { evalue = (new Net.Vpc.Upa.Expressions.Var((Net.Vpc.Upa.Expressions.Var)evalue, fk.GetName())); } else if (evalue is Net.Vpc.Upa.Expressions.Param) { } else if (evalue is Net.Vpc.Upa.Expressions.Literal) { } else { throw new System.Exception("Invalid Expression " + evalue + " as formula for field " + f.GetAbsoluteName()); } u.Set(fk.GetName(), evalue); } } else { Net.Vpc.Upa.Key k = null; if (@value is Net.Vpc.Upa.Record) { k = mbuilder.RecordToKey((Net.Vpc.Upa.Record)@value); } else { k = mbuilder.ObjectToKey(@value); } int x = 0; foreach (Net.Vpc.Upa.Field fk in e.GetRelationship().GetSourceRole().GetFields()) { u.Set(fk.GetName(), new Net.Vpc.Upa.Expressions.Param(fk.GetName(), k == null ? null : k.GetObjectAt(x))); x++; } } } } else { Net.Vpc.Upa.Expressions.Expression expression = (@value is Net.Vpc.Upa.Expressions.Expression) ? ((Net.Vpc.Upa.Expressions.Expression)((Net.Vpc.Upa.Expressions.Expression)@value)) : new Net.Vpc.Upa.Expressions.Param(null, @value); u.Set(fieldName, expression); } } } u.Where(condition); return(context.GetPersistenceStore().CreateQuery(u, context).ExecuteNonQuery()); }