private static Net.Vpc.Upa.Expressions.QueryStatement CreateViewQuery(string name, string[] tables, string updatableTableFieldName, System.Collections.Generic.IList <string> viewFields, string[][] fieldsMapping) { // StringBuilder sb=new StringBuilder(""); if (tables.Length < 2) { if (tables.Length == 0) { throw new System.ArgumentException("UnionTableUpdatableView must be over at least a couple of entities"); } System.Console.Error.WriteLine("[WARNING] UnionTableUpdatableView must be over at least a couple of tables"); } if (fieldsMapping != null && tables.Length != fieldsMapping.Length) { throw new System.ArgumentException("tables.length!=fieldsMapping.length"); } Net.Vpc.Upa.Expressions.Union u = new Net.Vpc.Upa.Expressions.Union(); for (int i = 0; i < tables.Length; i++) { if (fieldsMapping != null && (viewFields).Count != fieldsMapping[i].Length) { throw new System.ArgumentException("UnionTableUpdatableView " + name + " requires " + (viewFields).Count + " fields but got " + fieldsMapping[i].Length + " for " + tables[i]); } Net.Vpc.Upa.Expressions.Select s = new Net.Vpc.Upa.Expressions.Select(); s.Field(new Net.Vpc.Upa.Expressions.Literal(tables[i]), updatableTableFieldName); for (int j = 0; j < (viewFields).Count; j++) { string viewField = viewFields[j]; string varName = fieldsMapping == null ? viewField : fieldsMapping[i][j]; s.Field(new Net.Vpc.Upa.Expressions.Var(varName), viewField); } s.From(tables[i]); u.Add(s); } return(u); }
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 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 virtual string Convert(string v) { if (v.Equals("#")) { return(System.Convert.ToString(replacement)); } if (record != null && record.IsSet(v)) { return(System.Convert.ToString(record.GetObject <T>(v))); } Net.Vpc.Upa.Expressions.Select s = new Net.Vpc.Upa.Expressions.Select(); s.Field(new Net.Vpc.Upa.Expressions.UserExpression(v), "customValue"); return(System.Convert.ToString(field.GetEntity().GetPersistenceUnit().CreateQuery(s).GetSingleValue())); }
public virtual long GetNewValue(Net.Vpc.Upa.Field field) /* throws Net.Vpc.Upa.Exceptions.UPAException */ { Net.Vpc.Upa.Entity entity = field.GetEntity(); Net.Vpc.Upa.Expressions.Select s = new Net.Vpc.Upa.Expressions.Select().From(entity.GetName()); s.Field(new Net.Vpc.Upa.Expressions.Plus(new Net.Vpc.Upa.Expressions.Coalesce(new Net.Vpc.Upa.Expressions.Max(new Net.Vpc.Upa.Expressions.Var(field.GetName())), new Net.Vpc.Upa.Expressions.Literal(0)), new Net.Vpc.Upa.Expressions.Literal(1)), "next"); Net.Vpc.Upa.Record next = field.GetPersistenceUnit().CreateQuery(s).GetRecord(); if (next != null) { return(next.GetLong("next")); } else { return(0); } }
private Net.Vpc.Upa.Query Build() { // if (query == null) { string entityName = entity.GetName(); Net.Vpc.Upa.Expressions.Select s = (new Net.Vpc.Upa.Expressions.Select()).From(entityName, entityAlias); if (GetFieldFilter() != null) { foreach (Net.Vpc.Upa.Field field in entity.GetFields(GetFieldFilter())) { if (field != null) { s.Field(new Net.Vpc.Upa.Expressions.Var(field.GetName()), field.GetName()); } } } Net.Vpc.Upa.Expressions.Expression criteria = null; if (GetId() != null) { Net.Vpc.Upa.Expressions.Expression e = entity.GetBuilder().IdToExpression(GetId(), entityName); criteria = criteria == null ? ((Net.Vpc.Upa.Expressions.Expression)(e)) : new Net.Vpc.Upa.Expressions.And(criteria, e); } if (GetKey() != null) { Net.Vpc.Upa.Expressions.Expression e = (entity.GetBuilder().IdToExpression(entity.GetBuilder().KeyToId(GetKey()), entityName)); criteria = criteria == null ? ((Net.Vpc.Upa.Expressions.Expression)(e)) : new Net.Vpc.Upa.Expressions.And(criteria, e); } if (GetPrototype() != null) { Net.Vpc.Upa.Expressions.Expression e = entity.GetBuilder().ObjectToExpression(GetPrototype(), true, entityName); criteria = criteria == null ? ((Net.Vpc.Upa.Expressions.Expression)(e)) : new Net.Vpc.Upa.Expressions.And(criteria, e); } if (GetRecordPrototype() != null) { Net.Vpc.Upa.Expressions.Expression e = (entity.GetBuilder().RecordToExpression(GetRecordPrototype(), entityName)); criteria = criteria == null ? ((Net.Vpc.Upa.Expressions.Expression)(e)) : new Net.Vpc.Upa.Expressions.And(criteria, e); } if (GetExpression() != null) { Net.Vpc.Upa.Expressions.Expression e = GetExpression(); criteria = criteria == null ? ((Net.Vpc.Upa.Expressions.Expression)(e)) : new Net.Vpc.Upa.Expressions.And(criteria, e); } s.SetWhere(criteria); s.OrderBy(GetOrder()); query = entity.CreateQuery(s); foreach (System.Collections.Generic.KeyValuePair <string, object> e in new System.Collections.Generic.HashSet <System.Collections.Generic.KeyValuePair <string, object> >(paramsByName)) { query.SetParameter((e).Key, (e).Value); } foreach (System.Collections.Generic.KeyValuePair <int?, object> e in new System.Collections.Generic.HashSet <System.Collections.Generic.KeyValuePair <int?, object> >(paramsByIndex)) { query.SetParameter(((e).Key).Value, (e).Value); } query.SetUpdatable(this.IsUpdatable()); if (hints != null) { foreach (System.Collections.Generic.KeyValuePair <string, object> h in new System.Collections.Generic.HashSet <System.Collections.Generic.KeyValuePair <string, object> >(hints)) { query.SetHint((h).Key, (h).Value); } } // } return(query); }
public virtual Net.Vpc.Upa.Persistence.ResultMetaData CreateResultMetaData(Net.Vpc.Upa.Expressions.Expression baseExpression, Net.Vpc.Upa.Filters.FieldFilter fieldFilter, System.Collections.Generic.IList <Net.Vpc.Upa.Expressions.QueryStatement> context) { baseExpression = Net.Vpc.Upa.Impl.Uql.Util.UQLUtils.ParseUserExpressions(baseExpression, pu); Net.Vpc.Upa.Impl.Persistence.DefaultResultMetaData m = new Net.Vpc.Upa.Impl.Persistence.DefaultResultMetaData(); if (baseExpression is Net.Vpc.Upa.Expressions.NonQueryStatement) { m.SetStatement((Net.Vpc.Upa.Expressions.EntityStatement)baseExpression); m.AddField(new Net.Vpc.Upa.Impl.Persistence.DefaultResultField(null, "result", Net.Vpc.Upa.Types.TypesFactory.INT, null, null)); return(m); } else { Net.Vpc.Upa.Expressions.QueryStatement q = (Net.Vpc.Upa.Expressions.QueryStatement)baseExpression; if (q is Net.Vpc.Upa.Expressions.Select) { Net.Vpc.Upa.Expressions.Select qs = (Net.Vpc.Upa.Expressions.Select)q; if ((qs.GetFields()).Count == 0) { if (!Net.Vpc.Upa.Impl.Util.StringUtils.IsNullOrEmpty(qs.GetEntityAlias())) { qs.Field(new Net.Vpc.Upa.Expressions.Var(qs.GetEntityAlias())); } else if (qs.GetEntityName() != null) { qs.Field(new Net.Vpc.Upa.Expressions.Var(qs.GetEntityName())); } else { throw new Net.Vpc.Upa.Exceptions.UPAException("MissingAlias"); } foreach (Net.Vpc.Upa.Expressions.JoinCriteria joinCriteria in qs.GetJoins()) { if (!Net.Vpc.Upa.Impl.Util.StringUtils.IsNullOrEmpty(joinCriteria.GetEntityAlias())) { qs.Field(new Net.Vpc.Upa.Expressions.Var(joinCriteria.GetEntityAlias())); } else if (joinCriteria.GetEntityName() != null) { qs.Field(new Net.Vpc.Upa.Expressions.Var(joinCriteria.GetEntityName())); } else { throw new Net.Vpc.Upa.Exceptions.UPAException("MissingAlias"); } } } System.Collections.Generic.IList <Net.Vpc.Upa.Expressions.QueryField> oldFields = new System.Collections.Generic.List <Net.Vpc.Upa.Expressions.QueryField>(q.GetFields()); System.Collections.Generic.IList <Net.Vpc.Upa.Expressions.QueryField> newFields = new System.Collections.Generic.List <Net.Vpc.Upa.Expressions.QueryField>(); System.Collections.Generic.IList <Net.Vpc.Upa.Persistence.ResultField> newResults = new System.Collections.Generic.List <Net.Vpc.Upa.Persistence.ResultField>(); System.Collections.Generic.IList <Net.Vpc.Upa.Expressions.QueryStatement> context2 = new System.Collections.Generic.List <Net.Vpc.Upa.Expressions.QueryStatement>(); Net.Vpc.Upa.Impl.FwkConvertUtils.ListAddRange(context2, context); context2.Add(q); foreach (Net.Vpc.Upa.Expressions.QueryField f in oldFields) { Net.Vpc.Upa.Expressions.Expression expression = f.GetExpression(); string oldAlias = f.GetAlias(); System.Collections.Generic.IList <Net.Vpc.Upa.Persistence.ResultField> newVal = CreateResultFields(expression, oldAlias, fieldFilter, context2); Net.Vpc.Upa.Impl.FwkConvertUtils.ListAddRange(newResults, newVal); if ((newVal).Count == 0) { } else if ((newVal).Count == 1) { f.SetExpression(newVal[0].GetExpression()); f.SetAlias(Net.Vpc.Upa.Impl.Util.StringUtils.IsNullOrEmpty(oldAlias) ? oldAlias : newVal[0].GetAlias()); newFields.Add(f); } else { foreach (Net.Vpc.Upa.Persistence.ResultField nf in newVal) { Net.Vpc.Upa.Expressions.QueryField f2 = new Net.Vpc.Upa.Expressions.QueryField(Net.Vpc.Upa.Impl.Util.StringUtils.IsNullOrEmpty(oldAlias) ? oldAlias : nf.GetAlias(), nf.GetExpression()); newFields.Add(f2); } } } qs.ClearFields(); foreach (Net.Vpc.Upa.Expressions.QueryField newField in newFields) { qs.Field(newField); } m.SetStatement(qs); foreach (Net.Vpc.Upa.Persistence.ResultField newResult in newResults) { m.AddField(newResult); } } else if (q is Net.Vpc.Upa.Expressions.Union) { System.Collections.Generic.IList <Net.Vpc.Upa.Expressions.QueryStatement> context2 = new System.Collections.Generic.List <Net.Vpc.Upa.Expressions.QueryStatement>(); Net.Vpc.Upa.Impl.FwkConvertUtils.ListAddRange(context2, context); context2.Add(q); Net.Vpc.Upa.Expressions.Union u0 = (Net.Vpc.Upa.Expressions.Union)q; Net.Vpc.Upa.Expressions.Union u = new Net.Vpc.Upa.Expressions.Union(); Net.Vpc.Upa.Persistence.ResultField[] fields = null; foreach (Net.Vpc.Upa.Expressions.QueryStatement qs in u0.GetQueryStatements()) { Net.Vpc.Upa.Persistence.ResultMetaData resultMetaData = CreateResultMetaData(qs, fieldFilter, context2); u.Add((Net.Vpc.Upa.Expressions.QueryStatement)resultMetaData.GetStatement()); System.Collections.Generic.IList <Net.Vpc.Upa.Persistence.ResultField> f = resultMetaData.GetFields(); if (fields == null) { fields = f.ToArray(); } else { if (fields.Length != (f).Count) { throw new Net.Vpc.Upa.Exceptions.UPAException("InvalidUnion"); } for (int i = 0; i < fields.Length; i++) { fields[i] = Merge(fields[i], f[i]); } } } m.SetStatement(u); if (fields != null) { foreach (Net.Vpc.Upa.Persistence.ResultField field in fields) { m.AddField(field); } } } else { throw new System.Exception(); } } return(m); }
private object GetNavigateKey(Net.Vpc.Upa.Entity entity, object id, char @operator) /* throws Net.Vpc.Upa.Exceptions.UPAException */ { System.Collections.Generic.IList <Net.Vpc.Upa.Field> pk = entity.GetPrimaryFields(); if ((pk).Count == 1) { Net.Vpc.Upa.Expressions.Select s = new Net.Vpc.Upa.Expressions.Select().From(entity.GetName()); s.From(entity.GetName()); string fieldName = pk[0].GetName(); if (id != null) { object[] @value = entity.GetBuilder().IdToKey(id).GetValue(); if (@operator == '<') { s.Field(new Net.Vpc.Upa.Expressions.Max(new Net.Vpc.Upa.Expressions.Var(fieldName)), "next"); s.SetWhere(new Net.Vpc.Upa.Expressions.LessThan(new Net.Vpc.Upa.Expressions.Var(fieldName), new Net.Vpc.Upa.Expressions.Param(null, @value[0]))); } else if (@operator == '>') { s.Field(new Net.Vpc.Upa.Expressions.Min(new Net.Vpc.Upa.Expressions.Var(fieldName)), "next"); s.SetWhere(new Net.Vpc.Upa.Expressions.GreaterThan(new Net.Vpc.Upa.Expressions.Var(fieldName), new Net.Vpc.Upa.Expressions.Param(null, @value[0]))); } else { throw new System.Exception("WouldNeverBeThrownException"); } } else { if (@operator == '<') { s.Field(new Net.Vpc.Upa.Expressions.Min(new Net.Vpc.Upa.Expressions.Var(fieldName)), "next"); } else if (@operator == '>') { s.Field(new Net.Vpc.Upa.Expressions.Max(new Net.Vpc.Upa.Expressions.Var(fieldName)), "next"); } else { throw new System.Exception("WouldNeverBeThrownException"); } } Net.Vpc.Upa.Record next = entity.GetPersistenceUnit().CreateQuery(s).GetRecord(); if (next != null) { object o = next.GetObject <T>("next"); if (o != null) { return(entity.CreateId(o)); } } return(null); } else { object[] v; Net.Vpc.Upa.Expressions.Select sb = new Net.Vpc.Upa.Expressions.Select(); sb.Top(1); foreach (Net.Vpc.Upa.Field aPk in pk) { sb.Field(new Net.Vpc.Upa.Expressions.Var(aPk.GetName())); } sb.From(entity.GetName()); if (id != null) { object[] @value = entity.GetBuilder().IdToKey(id).GetValue(); Net.Vpc.Upa.Expressions.Expression or = null; for (int i = 0; i < (pk).Count; i++) { Net.Vpc.Upa.Field pki = pk[i]; Net.Vpc.Upa.Expressions.Expression a = null; for (int j = 0; j < i; j++) { Net.Vpc.Upa.Field pkj = pk[j]; Net.Vpc.Upa.Expressions.Expression e = (new Net.Vpc.Upa.Expressions.Equals(new Net.Vpc.Upa.Expressions.Var(pkj.GetName()), (new Net.Vpc.Upa.Expressions.Param(null, @value[j])))); a = (a == null) ? ((Net.Vpc.Upa.Expressions.Expression)(e)) : new Net.Vpc.Upa.Expressions.And(a, e); } Net.Vpc.Upa.Expressions.Expression e2 = new Net.Vpc.Upa.Expressions.LessThan(new Net.Vpc.Upa.Expressions.Var(pki.GetName()), new Net.Vpc.Upa.Expressions.Param(null, @value[i])); a = (a == null) ? ((Net.Vpc.Upa.Expressions.Expression)(e2)) : new Net.Vpc.Upa.Expressions.And(a, e2); or = or == null ? ((Net.Vpc.Upa.Expressions.Expression)(a)) : new Net.Vpc.Upa.Expressions.Or(or, a); } sb.SetWhere(or); } foreach (Net.Vpc.Upa.Field aPk in pk) { sb.OrderBy(new Net.Vpc.Upa.Expressions.Var(aPk.GetName()), @operator == '>'); } Net.Vpc.Upa.Record r = entity.GetPersistenceUnit().CreateQuery(sb).GetRecord(); if (r != null) { object[] k = new object[(pk).Count]; for (int i = 0; i < k.Length; i++) { k[i] = r.GetObject <T>(pk[i].GetName()); } return(entity.CreateId(k)); } } return(null); }