public virtual Net.Vpc.Upa.Expressions.Expression GetViewElementExpressionAt(int updatableTableIndex, Net.Vpc.Upa.Expressions.Expression expression) /* throws Net.Vpc.Upa.Exceptions.UPAException */ { System.Collections.Generic.IList <Net.Vpc.Upa.Field> pf = GetEntity().GetPrimaryFields(); System.Collections.Generic.IList <Net.Vpc.Upa.Field> pft = updatableTables[updatableTableIndex].GetPrimaryFields(); Net.Vpc.Upa.Expressions.Expression[] pfte = new Net.Vpc.Upa.Expressions.Expression[(pft).Count]; for (int i = 0; i < pfte.Length; i++) { Net.Vpc.Upa.Field f = pft[i]; pfte[i] = new Net.Vpc.Upa.Expressions.Var(f.GetName()); } Net.Vpc.Upa.Expressions.Uplet ut = new Net.Vpc.Upa.Expressions.Uplet(pfte); Net.Vpc.Upa.Expressions.Expression[] pfe = new Net.Vpc.Upa.Expressions.Expression[(pf).Count - 1]; for (int i = 0; i < pfe.Length; i++) { Net.Vpc.Upa.Field f = pf[i + 1]; pfe[i] = new Net.Vpc.Upa.Expressions.Var(f.GetName()); } Net.Vpc.Upa.Expressions.Expression w = new Net.Vpc.Upa.Expressions.Equals(new Net.Vpc.Upa.Expressions.Var(pf[0].GetName()), new Net.Vpc.Upa.Expressions.Literal(updatableTables[updatableTableIndex].GetName())); if (expression != null) { w = new Net.Vpc.Upa.Expressions.And(w, expression); } Net.Vpc.Upa.Expressions.Select q = new Net.Vpc.Upa.Expressions.Select().From(GetEntity().GetName()).Uplet(pfe).Where(w); return(new Net.Vpc.Upa.Expressions.InSelection(ut, q)); }
public virtual Net.Vpc.Upa.Impl.Persistence.RebuildExpressionInfo RebuildExpression(Net.Vpc.Upa.ExpressionFormula persistFormula) { Net.Vpc.Upa.Expressions.Expression e = ((Net.Vpc.Upa.ExpressionFormula)persistFormula).GetExpression(); Net.Vpc.Upa.Impl.Persistence.RebuildExpressionInfo rr = new Net.Vpc.Upa.Impl.Persistence.RebuildExpressionInfo(); rr.initialFormula = ((Net.Vpc.Upa.ExpressionFormula)persistFormula); Net.Vpc.Upa.Expressions.Expression e0 = e; Net.Vpc.Upa.Persistence.ExpressionCompilerConfig config = new Net.Vpc.Upa.Persistence.ExpressionCompilerConfig(); config.SetExpandEntityFilter(false); //this is needed not to fire "this" alias usage exception config.SetThisAlias("this"); config.BindAliastoEntity("this", field.GetEntity().GetName()); Net.Vpc.Upa.Impl.Uql.Compiledexpression.DefaultCompiledExpression ce = null; Net.Vpc.Upa.ExpressionManager expressionManager = field.GetEntity().GetPersistenceUnit().GetExpressionManager(); try { ce = (Net.Vpc.Upa.Impl.Uql.Compiledexpression.DefaultCompiledExpression)expressionManager.CompileExpression(e, config); } catch (System.ArgumentException ex) { if ((ex).Message.StartsWith("No enclosing Select found for")) { Net.Vpc.Upa.Expressions.Select ss = new Net.Vpc.Upa.Expressions.Select(); ss.Field(e).From(field.GetEntity().GetName(), "this2"); Net.Vpc.Upa.Expressions.Expression w = null; foreach (Net.Vpc.Upa.Field primaryField in field.GetEntity().GetPrimaryFields()) { Net.Vpc.Upa.Expressions.Expression pfe = new Net.Vpc.Upa.Expressions.Equals(new Net.Vpc.Upa.Expressions.UserExpression("this." + primaryField.GetName()), new Net.Vpc.Upa.Expressions.UserExpression("this2." + primaryField.GetName())); if (w == null) { w = pfe; } else { w = new Net.Vpc.Upa.Expressions.And(w, pfe); } } Net.Vpc.Upa.Impl.Uql.Util.UQLUtils.ReplaceThisVar(ss, "this2", expressionManager); ss.Where(w); e = ss; } else { throw ex; } } //throw new IllegalArgumentException("No enclosing Select found for " + v) if (ce == null) { ce = (Net.Vpc.Upa.Impl.Uql.Compiledexpression.DefaultCompiledExpression)expressionManager.CompileExpression(e, config); rr.compiledExpression = ce; rr.rebuiltFormula = (new Net.Vpc.Upa.ExpressionFormula(e)); } else { rr.compiledExpression = ce; } rr.expression = e; return(rr); }
public override void OnPreUpdateFormula(Net.Vpc.Upa.Callbacks.UpdateFormulaEvent @event) /* throws Net.Vpc.Upa.Exceptions.UPAException */ { Net.Vpc.Upa.Persistence.EntityExecutionContext executionContext = @event.GetContext(); Net.Vpc.Upa.Relationship r = relation; System.Collections.Generic.IList <Net.Vpc.Upa.Field> fs = r.GetSourceRole().GetFields(); Net.Vpc.Upa.Expressions.Expression cond = new Net.Vpc.Upa.Expressions.Equals(new Net.Vpc.Upa.Expressions.Var(fs[0].GetName()), Net.Vpc.Upa.Expressions.Literal.NULL); if (@event.GetFilterExpression() != null) { cond = new Net.Vpc.Upa.Expressions.And(cond, @event.GetFilterExpression()); } System.Collections.Generic.IList <object> keys = relation.GetSourceRole().GetEntity().CreateQueryBuilder().ByExpression(cond).GetIdList <K>(); foreach (object key in keys) { support.ValidatePathField(key, executionContext); support.ValidateChildren(key, executionContext); } }
public virtual Net.Vpc.Upa.Expressions.Expression CreateFindEntityByMainPathExpression(string mainFieldPath, string entityAlias) { Net.Vpc.Upa.Entity entity = GetEntity(); Net.Vpc.Upa.RelationshipRole detailRole = GetTreeRelationship().GetSourceRole(); if (Net.Vpc.Upa.Impl.Util.StringUtils.IsNullOrEmpty(mainFieldPath)) { return(null); } string mainFieldName = entity.GetMainField().GetName(); object mainFieldValue = null; object parent = null; string[] parentAndName = Net.Vpc.Upa.Impl.Util.StringUtils.Split(mainFieldPath, GetHierarchyPathSeparator()[0], false); if (parentAndName != null) { parent = FindEntityByMainPath(parentAndName[0]); mainFieldValue = parentAndName[1]; } else { mainFieldValue = mainFieldPath; } Net.Vpc.Upa.Expressions.Expression expr = null; if (entityAlias == null) { entityAlias = entity.GetName(); } expr = new Net.Vpc.Upa.Expressions.Equals(new Net.Vpc.Upa.Expressions.Var(new Net.Vpc.Upa.Expressions.Var(entityAlias), mainFieldName), mainFieldValue); Net.Vpc.Upa.Key entityToKey = parent == null ? null : entity.GetBuilder().ObjectToKey(parent); System.Collections.Generic.IList <Net.Vpc.Upa.Field> primaryFields = detailRole.GetFields(); for (int index = 0; index < (primaryFields).Count; index++) { Net.Vpc.Upa.Field field = primaryFields[index]; expr = new Net.Vpc.Upa.Expressions.And(expr, new Net.Vpc.Upa.Expressions.Equals(new Net.Vpc.Upa.Expressions.Var(new Net.Vpc.Upa.Expressions.Var(entityAlias), field.GetName()), entityToKey == null ? null : entityToKey.GetObjectAt(index))); } return(expr); }
public virtual Net.Vpc.Upa.Expressions.Expression RecordToExpression(Net.Vpc.Upa.Record record, string alias) /* throws Net.Vpc.Upa.Exceptions.UPAException */ { if (record == null) { return(null); } Net.Vpc.Upa.Expressions.Expression a = null; foreach (System.Collections.Generic.KeyValuePair <string, object> entry in record.EntrySet()) { string key = (entry).Key; object @value = (entry).Value; Net.Vpc.Upa.Field field = GetEntity().GetField(key); if (!field.IsUnspecifiedValue(@value)) { Net.Vpc.Upa.Expressions.Expression e = null; Net.Vpc.Upa.Expressions.Var p = new Net.Vpc.Upa.Expressions.Var(Net.Vpc.Upa.Impl.Util.StringUtils.IsNullOrEmpty(alias) ? GetEntity().GetName() : alias); switch (field.GetSearchOperator()) { case Net.Vpc.Upa.SearchOperator.DEFAULT: case Net.Vpc.Upa.SearchOperator.EQ: { if (field.GetDataType() is Net.Vpc.Upa.Types.ManyToOneType) { Net.Vpc.Upa.Types.ManyToOneType et = (Net.Vpc.Upa.Types.ManyToOneType)field.GetDataType(); Net.Vpc.Upa.Key foreignKey = et.GetRelationship().GetTargetRole().GetEntity().GetBuilder().ObjectToKey(@value); Net.Vpc.Upa.Expressions.Expression b = null; int i = 0; foreach (Net.Vpc.Upa.Field df in et.GetRelationship().GetSourceRole().GetFields()) { e = new Net.Vpc.Upa.Expressions.Equals(new Net.Vpc.Upa.Expressions.Var((Net.Vpc.Upa.Expressions.Var)p.Copy(), df.GetName()), Net.Vpc.Upa.Expressions.ExpressionFactory.ToLiteral(foreignKey.GetObjectAt(i))); b = b == null ? ((Net.Vpc.Upa.Expressions.Expression)(b)) : new Net.Vpc.Upa.Expressions.And(b, e); i++; } } else { e = new Net.Vpc.Upa.Expressions.Equals(new Net.Vpc.Upa.Expressions.Var(p, key), Net.Vpc.Upa.Expressions.ExpressionFactory.ToLiteral(@value)); } break; } case Net.Vpc.Upa.SearchOperator.NE: { e = new Net.Vpc.Upa.Expressions.Different(new Net.Vpc.Upa.Expressions.Var(p, key), Net.Vpc.Upa.Expressions.ExpressionFactory.ToLiteral(@value)); break; } case Net.Vpc.Upa.SearchOperator.GT: { e = new Net.Vpc.Upa.Expressions.GreaterThan(new Net.Vpc.Upa.Expressions.Var(p, key), Net.Vpc.Upa.Expressions.ExpressionFactory.ToLiteral(@value)); break; } case Net.Vpc.Upa.SearchOperator.GTE: { e = new Net.Vpc.Upa.Expressions.GreaterEqualThan(new Net.Vpc.Upa.Expressions.Var(p, key), Net.Vpc.Upa.Expressions.ExpressionFactory.ToLiteral(@value)); break; } case Net.Vpc.Upa.SearchOperator.LT: { e = new Net.Vpc.Upa.Expressions.LessThan(new Net.Vpc.Upa.Expressions.Var(p, key), Net.Vpc.Upa.Expressions.ExpressionFactory.ToLiteral(@value)); break; } case Net.Vpc.Upa.SearchOperator.LTE: { e = new Net.Vpc.Upa.Expressions.LessEqualThan(new Net.Vpc.Upa.Expressions.Var(p, key), Net.Vpc.Upa.Expressions.ExpressionFactory.ToLiteral(@value)); break; } case Net.Vpc.Upa.SearchOperator.CONTAINS: { e = new Net.Vpc.Upa.Expressions.Like(new Net.Vpc.Upa.Expressions.Var(p, key), Net.Vpc.Upa.Expressions.ExpressionFactory.ToLiteral("%" + @value + "%")); break; } case Net.Vpc.Upa.SearchOperator.STARTS_WITH: { e = new Net.Vpc.Upa.Expressions.Like(new Net.Vpc.Upa.Expressions.Var(p, key), Net.Vpc.Upa.Expressions.ExpressionFactory.ToLiteral(@value + "%")); break; } case Net.Vpc.Upa.SearchOperator.ENDS_WITH: { e = new Net.Vpc.Upa.Expressions.Like(new Net.Vpc.Upa.Expressions.Var(p, key), Net.Vpc.Upa.Expressions.ExpressionFactory.ToLiteral("%" + @value)); break; } } if (e != null) { a = a == null ? ((Net.Vpc.Upa.Expressions.Expression)(e)) : new Net.Vpc.Upa.Expressions.And(a, e); } } } return(a); }
protected internal virtual Net.Vpc.Upa.Impl.Uql.Compiledexpression.CompiledEquals CompileEquals(Net.Vpc.Upa.Expressions.Equals v, Net.Vpc.Upa.Impl.Uql.ExpressionTranslationManager manager, Net.Vpc.Upa.Impl.Uql.ExpressionDeclarationList declarations) { if (v == null) { return(null); } Net.Vpc.Upa.Impl.Uql.Compiledexpression.DefaultCompiledExpression left = manager.TranslateAny(v.GetLeft(), declarations); Net.Vpc.Upa.Impl.Uql.Compiledexpression.DefaultCompiledExpression right = manager.TranslateAny(v.GetRight(), declarations); Net.Vpc.Upa.Impl.Uql.Compiledexpression.CompiledEquals s = new Net.Vpc.Upa.Impl.Uql.Compiledexpression.CompiledEquals(left, right); // s.setDeclarationList(new ExpressionDeclarationList(declarations)); return(s); }
public virtual void CheckPersist(Net.Vpc.Upa.Record record) /* throws Net.Vpc.Upa.Exceptions.UPAException */ { if (!entity.GetPersistenceUnit().GetSecurityManager().IsAllowedPersist(entity)) { throw new Net.Vpc.Upa.Exceptions.PersistRecordNotAllowedException(entity); } if (!IsPersistSupported()) { throw new Net.Vpc.Upa.Exceptions.PersistRecordNotAllowedException(entity); } if (record != null) { // check parent is not read only if (entity.GetParentEntity() != null) { Net.Vpc.Upa.Expressions.Expression parentUnupdatable = entity.GetParentEntity().GetShield().GetFullNonUpdatableRecordsExpression(); if (parentUnupdatable != null && parentUnupdatable.IsValid()) { Net.Vpc.Upa.Relationship r = entity.GetCompositionRelation(); System.Collections.Generic.IList <Net.Vpc.Upa.Field> df = r.GetSourceRole().GetFields(); System.Collections.Generic.IList <Net.Vpc.Upa.Field> mf = r.GetTargetRole().GetFields(); object[] pko = new object[(mf).Count]; for (int i = 0; i < pko.Length; i++) { pko[i] = record.GetObject <T>(df[i].GetName()); } object pk = entity.CreateId(pko); long c = entity.GetParentEntity().GetEntityCount(new Net.Vpc.Upa.Expressions.And(parentUnupdatable, entity.GetParentEntity().GetBuilder().IdToExpression(pk, null))); if (c > 0) { throw new Net.Vpc.Upa.Exceptions.UnupdatableRecordException(entity.GetParentEntity()); } } } bool keyGenerated = IsGeneratedId(); Net.Vpc.Upa.Expressions.Expression keyExpresson = null; if (!keyGenerated) { object key = entity.GetBuilder().RecordToId(record); keyExpresson = entity.GetBuilder().IdToExpression(key, null); } Net.Vpc.Upa.Entity p = entity.GetParentEntity(); if (p != null) { //Expression ss = childToParentExpression(toExpression(key)); Net.Vpc.Upa.Expressions.Expression ss = entity.ChildToParentExpression(record); if (ss != null) { p.GetShield().CheckUpdate(null, ss); } } System.Collections.Generic.IList <Net.Vpc.Upa.Index> uniqueIndexes = entity.GetIndexes(true); if ((uniqueIndexes.Count == 0)) { if (!keyGenerated) { if (entity.GetEntityCount(keyExpresson) > 0) { throw new Net.Vpc.Upa.Exceptions.InsertRecordDuplicateKeyException(entity); } } } else { Net.Vpc.Upa.Expressions.Expression or = null; if (!keyGenerated) { or = keyExpresson; } foreach (Net.Vpc.Upa.Index index in uniqueIndexes) { Net.Vpc.Upa.Field[] f = index.GetFields(); Net.Vpc.Upa.Expressions.Expression e1 = null; if (f.Length == 1) { e1 = new Net.Vpc.Upa.Expressions.Equals(new Net.Vpc.Upa.Expressions.Var(f[0].GetName()), Net.Vpc.Upa.Expressions.ExpressionFactory.ToLiteral(record.GetObject <T>(f[0].GetName()))); } else { Net.Vpc.Upa.Expressions.Expression a = null; foreach (Net.Vpc.Upa.Field aF in f) { Net.Vpc.Upa.Expressions.Expression b = (new Net.Vpc.Upa.Expressions.Equals(new Net.Vpc.Upa.Expressions.Var(aF.GetName()), Net.Vpc.Upa.Expressions.ExpressionFactory.ToLiteral(record.GetObject <T>(aF.GetName())))); a = a == null ? ((Net.Vpc.Upa.Expressions.Expression)(b)) : new Net.Vpc.Upa.Expressions.And(a, b); } e1 = a; } or = or == null ? ((Net.Vpc.Upa.Expressions.Expression)(e1)) : new Net.Vpc.Upa.Expressions.Or(or, e1); } if (entity.GetEntityCount(or) > 0) { // finer lookup of problem if (!keyGenerated) { if (entity.GetEntityCount(keyExpresson) > 0) { throw new Net.Vpc.Upa.Exceptions.InsertRecordDuplicateKeyException(entity); } } foreach (Net.Vpc.Upa.Index index in uniqueIndexes) { Net.Vpc.Upa.Field[] f = index.GetFields(); Net.Vpc.Upa.Expressions.Expression e1 = null; if (f.Length == 1) { e1 = new Net.Vpc.Upa.Expressions.Equals(new Net.Vpc.Upa.Expressions.Var(f[0].GetName()), Net.Vpc.Upa.Expressions.ExpressionFactory.ToLiteral(record.GetObject <T>(f[0].GetName()))); } else { Net.Vpc.Upa.Expressions.Expression a = null; foreach (Net.Vpc.Upa.Field aF in f) { Net.Vpc.Upa.Expressions.Expression b = (new Net.Vpc.Upa.Expressions.Equals(new Net.Vpc.Upa.Expressions.Var(aF.GetName()), Net.Vpc.Upa.Expressions.ExpressionFactory.ToLiteral(record.GetObject <T>(aF.GetName())))); a = a == null ? ((Net.Vpc.Upa.Expressions.Expression)(b)) : new Net.Vpc.Upa.Expressions.And(a, b); } e1 = a; } if (entity.GetEntityCount(e1) > 0) { throw new Net.Vpc.Upa.Exceptions.InsertRecordDuplicateUniqueFieldsException(entity, index, record.GetObject <T>(f[0].GetName())); } } throw new System.Exception("WouldNeverBeThrownException"); } } } CheckVeto(Net.Vpc.Upa.VetoableOperation.checkPersist, record); }
public virtual Net.Vpc.Upa.Expressions.Expression GetSourceCondition(Net.Vpc.Upa.Expressions.Expression targetCondition, string sourceAlias, string targetAlias) /* throws Net.Vpc.Upa.Exceptions.UPAException */ { //Key Rkey=getTargetTable().getKeyForExpression(targetCondition); Net.Vpc.Upa.Field[] sourceFields = GetSourceRole().GetFields().ToArray(); Net.Vpc.Upa.Field[] targetFields = GetTargetRole().GetFields().ToArray(); if (targetCondition is Net.Vpc.Upa.Expressions.IdExpression) { Net.Vpc.Upa.Key Rkey = targetRole.GetEntity().GetBuilder().IdToKey(((Net.Vpc.Upa.Expressions.IdExpression)targetCondition).GetId()); if (sourceFields.Length == 1) { Net.Vpc.Upa.Expressions.Var lvar = (sourceAlias == null) ? new Net.Vpc.Upa.Expressions.Var(sourceFields[0].GetName()) : new Net.Vpc.Upa.Expressions.Var(new Net.Vpc.Upa.Expressions.Var(sourceAlias), sourceFields[0].GetName()); return(new Net.Vpc.Upa.Expressions.Equals(lvar, new Net.Vpc.Upa.Expressions.Literal(Rkey == null ? null : Rkey.GetValue()[0], targetFields[0].GetDataType()))); } else { Net.Vpc.Upa.Expressions.Expression a = null; for (int i = 0; i < sourceFields.Length; i++) { Net.Vpc.Upa.Expressions.Var lvar = (sourceAlias == null) ? new Net.Vpc.Upa.Expressions.Var(sourceFields[i].GetName()) : new Net.Vpc.Upa.Expressions.Var(new Net.Vpc.Upa.Expressions.Var(sourceAlias), sourceFields[i].GetName()); Net.Vpc.Upa.Expressions.Expression rvar = new Net.Vpc.Upa.Expressions.Literal(Rkey == null ? null : Rkey.GetObjectAt(i), targetFields[i].GetDataType()); Net.Vpc.Upa.Expressions.Expression e = new Net.Vpc.Upa.Expressions.Equals(lvar, rvar); a = a == null ? e : a; } return(a); } } else if (tuningMaxInline > 0) { Net.Vpc.Upa.Expressions.Var[] lvar = new Net.Vpc.Upa.Expressions.Var[sourceFields.Length]; Net.Vpc.Upa.Expressions.Var[] rvar = new Net.Vpc.Upa.Expressions.Var[targetFields.Length]; for (int i = 0; i < lvar.Length; i++) { lvar[i] = new Net.Vpc.Upa.Expressions.Var((sourceAlias == null) ? null : new Net.Vpc.Upa.Expressions.Var(sourceAlias), sourceFields[i].GetName()); rvar[i] = new Net.Vpc.Upa.Expressions.Var((targetAlias == null) ? null : new Net.Vpc.Upa.Expressions.Var(targetAlias), targetFields[i].GetName()); } try { System.Collections.Generic.IList <Net.Vpc.Upa.Record> list = GetTargetRole().GetEntity().CreateQuery(new Net.Vpc.Upa.Expressions.Select().Uplet(rvar, "rval").Where(targetCondition)).GetRecordList(); int j = 0; Net.Vpc.Upa.Expressions.IdCollectionExpression inCollection = new Net.Vpc.Upa.Expressions.IdCollectionExpression(lvar); foreach (Net.Vpc.Upa.Record r in list) { j++; if (j > tuningMaxInline) { return(new Net.Vpc.Upa.Expressions.InSelection(lvar, new Net.Vpc.Upa.Expressions.Select().From(GetTargetRole().GetEntity().GetName()).Uplet(rvar).Where(targetCondition))); } inCollection.Add(new Net.Vpc.Upa.Expressions.Param(null, r.GetSingleResult <T>())); } //inCollection.add(new Litteral(rs.getObject(1))); return(inCollection); } catch (Net.Vpc.Upa.Exceptions.UPAException e) { return(new Net.Vpc.Upa.Expressions.InSelection(lvar, new Net.Vpc.Upa.Expressions.Select().From(GetTargetRole().GetEntity().GetName()).Uplet(rvar).Where(targetCondition))); } } else { Net.Vpc.Upa.Expressions.Var[] lvar = new Net.Vpc.Upa.Expressions.Var[sourceFields.Length]; Net.Vpc.Upa.Expressions.Var[] rvar = new Net.Vpc.Upa.Expressions.Var[targetFields.Length]; for (int i = 0; i < lvar.Length; i++) { lvar[i] = new Net.Vpc.Upa.Expressions.Var((sourceAlias == null) ? null : new Net.Vpc.Upa.Expressions.Var(sourceAlias), sourceFields[i].GetName()); rvar[i] = new Net.Vpc.Upa.Expressions.Var((targetAlias == null) ? null : new Net.Vpc.Upa.Expressions.Var(targetAlias), targetFields[i].GetName()); } return(new Net.Vpc.Upa.Expressions.InSelection(lvar, new Net.Vpc.Upa.Expressions.Select().From(GetTargetRole().GetEntity().GetName()).Uplet(rvar).Where(targetCondition))); } }