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); }
protected internal virtual Net.Vpc.Upa.Impl.Uql.Compiledexpression.CompiledEntityStatement CreateCompiledEntityStatement() { Net.Vpc.Upa.Persistence.ExpressionCompilerConfig config = new Net.Vpc.Upa.Persistence.ExpressionCompilerConfig(); string alias = null; string ent = null; if (query is Net.Vpc.Upa.Expressions.Select) { Net.Vpc.Upa.Expressions.Select d = (Net.Vpc.Upa.Expressions.Select)query; string entityAlias = d.GetEntityAlias(); Net.Vpc.Upa.Expressions.EntityName entityName = (d.GetEntity() is Net.Vpc.Upa.Expressions.EntityName) ? ((Net.Vpc.Upa.Expressions.EntityName)d.GetEntity()) : null; if (entityAlias != null) { alias = entityAlias; ent = entityName == null ? null : entityName.GetName(); } else { ent = entityName == null ? null : entityName.GetName(); alias = ent; } } if (alias != null) { config.SetThisAlias(alias); } config.SetExpandFields(false); config.SetExpandEntityFilter(false); config.SetValidate(false); return((Net.Vpc.Upa.Impl.Uql.Compiledexpression.CompiledEntityStatement)context.GetPersistenceUnit().GetExpressionManager().CompileExpression(query, config)); }
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.Expressions.Select AddQuery(Net.Vpc.Upa.Expressions.Select other) { if (other == null) { return(this); } if (other.queryEntity != null) { queryEntity = other.queryEntity; } if (other.queryEntityAlias != null) { queryEntityAlias = other.queryEntityAlias; } for (int i = 0; i < (other.joinsEntities).Count; i++) { Net.Vpc.Upa.Expressions.JoinCriteria j = other.GetJoin(i); Join(j.GetJoinType(), j.GetEntity(), j.GetEntityAlias(), j.GetCondition()); } foreach (Net.Vpc.Upa.Expressions.QueryField field in other.fields) { fields.Add(new Net.Vpc.Upa.Expressions.QueryField(field.GetAlias(), field.GetExpression().Copy())); } order.AddOrder(other.order.Copy()); group.AddGroup(other.group.Copy()); this.where = (other.where != null) ? other.where.Copy() : null; this.having = (other.having != null) ? other.having.Copy() : null; return(this); }
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 static string[] ResolveEntityAndAlias(Net.Vpc.Upa.Expressions.Select select) { string e = select.GetEntityName(); if (e != null) { return(new string[] { e, select.GetEntityAlias() }); } return(null); }
public override void SetChild(Net.Vpc.Upa.Expressions.Expression e, Net.Vpc.Upa.Expressions.ExpressionTag tag) { if (tag is Net.Vpc.Upa.Expressions.IndexedTag) { this.left[((Net.Vpc.Upa.Expressions.IndexedTag)tag).GetIndex()] = e; } else { this.query = (Net.Vpc.Upa.Expressions.Select)e; } }
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); } }
public DefaultQuery(Net.Vpc.Upa.Expressions.EntityStatement query, Net.Vpc.Upa.Entity defaultEntity, Net.Vpc.Upa.Persistence.EntityExecutionContext context) { this.query = query; if (defaultEntity != null) { if (query is Net.Vpc.Upa.Expressions.Select) { Net.Vpc.Upa.Expressions.Select select = (Net.Vpc.Upa.Expressions.Select)query; if (select.GetEntity() == null) { select.From(defaultEntity.GetName()); } } else if (query is Net.Vpc.Upa.Expressions.Insert) { if (((Net.Vpc.Upa.Expressions.Insert)query).GetEntity() == null) { ((Net.Vpc.Upa.Expressions.Insert)query).Into(defaultEntity.GetName()); } } else if (query is Net.Vpc.Upa.Expressions.Update) { if (((Net.Vpc.Upa.Expressions.Update)query).GetEntity() == null) { ((Net.Vpc.Upa.Expressions.Update)query).Entity(defaultEntity.GetName()); } } else if (query is Net.Vpc.Upa.Expressions.Delete) { if (((Net.Vpc.Upa.Expressions.Delete)query).GetEntity() == null) { ((Net.Vpc.Upa.Expressions.Delete)query).From(defaultEntity.GetName()); } } } this.context = context; // this.cquery = (CompiledEntityStatement) query.copy(); // this.defaultEntity = defaultEntity; store = (Net.Vpc.Upa.Impl.Persistence.DefaultPersistenceStore)context.GetPersistenceStore(); }
private System.Collections.Generic.IDictionary <string, Net.Vpc.Upa.Expressions.NameOrQuery> FindDeclarations(Net.Vpc.Upa.Expressions.QueryStatement queryStatement) { System.Collections.Generic.IDictionary <string, Net.Vpc.Upa.Expressions.NameOrQuery> names = new System.Collections.Generic.Dictionary <string, Net.Vpc.Upa.Expressions.NameOrQuery>(); if (queryStatement is Net.Vpc.Upa.Expressions.Select) { Net.Vpc.Upa.Expressions.Select s = (Net.Vpc.Upa.Expressions.Select)queryStatement; if (!Net.Vpc.Upa.Impl.Util.StringUtils.IsNullOrEmpty(s.GetEntityAlias())) { names[s.GetEntityAlias()] = s.GetEntity(); } else { string t = s.GetEntityName(); if (!Net.Vpc.Upa.Impl.Util.StringUtils.IsNullOrEmpty(t)) { names[s.GetEntityAlias()] = s.GetEntity(); } } foreach (Net.Vpc.Upa.Expressions.JoinCriteria j in s.GetJoins()) { if (!Net.Vpc.Upa.Impl.Util.StringUtils.IsNullOrEmpty(j.GetEntityAlias())) { names[j.GetEntityAlias()] = j.GetEntity(); } else { string t = j.GetEntityName(); if (!Net.Vpc.Upa.Impl.Util.StringUtils.IsNullOrEmpty(t)) { names[j.GetEntityAlias()] = j.GetEntity(); } } } } else if (queryStatement is Net.Vpc.Upa.Expressions.Union) { } // do nothing return(names); }
public virtual bool Accept(Net.Vpc.Upa.Expressions.Expression expression) { if (typeof(Net.Vpc.Upa.Expressions.Select).IsInstanceOfType(expression)) { Net.Vpc.Upa.Expressions.Select ss = (Net.Vpc.Upa.Expressions.Select)expression; if (isUpdateComplexValuesStatementSupported) { if (ss.GetEntity() != null) { bool meFound = false; string ssentityName = ss.GetEntityName(); if (ssentityName != null && ssentityName.Equals(entityName)) { meFound = true; } if (!meFound) { foreach (Net.Vpc.Upa.Expressions.JoinCriteria join in ss.GetJoins()) { string jentityName = join.GetEntityName(); if (jentityName != null && jentityName.Equals(entityName)) { meFound = true; } } } if (meFound) { return(true); } } } else { return(true); } } return(false); }
public virtual Net.Vpc.Upa.Expressions.Select FullJoin(Net.Vpc.Upa.Expressions.Select entity, string alias, Net.Vpc.Upa.Expressions.Expression condition) { return(Join(Net.Vpc.Upa.Expressions.JoinType.FULL_JOIN, entity, alias, condition)); }
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()); }
public InSelection(Net.Vpc.Upa.Expressions.Expression[] left, Net.Vpc.Upa.Expressions.Select query) { this.left = left; this.query = query; }
protected internal virtual Net.Vpc.Upa.Impl.Uql.Compiledexpression.CompiledSelect CompileSelect(Net.Vpc.Upa.Expressions.Select 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.CompiledSelect s = new Net.Vpc.Upa.Impl.Uql.Compiledexpression.CompiledSelect(); s.SetDistinct(v.IsDistinct()); s.SetTop(v.GetTop()); Net.Vpc.Upa.Impl.Uql.Compiledexpression.CompiledNameOrSelect nameOrSelect = (Net.Vpc.Upa.Impl.Uql.Compiledexpression.CompiledNameOrSelect)manager.TranslateAny(v.GetEntity(), declarations); string entityAlias = v.GetEntityAlias(); System.Collections.Generic.HashSet <string> aliases = new System.Collections.Generic.HashSet <string>(); if (entityAlias == null) { if (nameOrSelect is Net.Vpc.Upa.Impl.Uql.Compiledexpression.CompiledEntityName) { entityAlias = ((Net.Vpc.Upa.Impl.Uql.Compiledexpression.CompiledEntityName)nameOrSelect).GetName(); } else { entityAlias = "ALIAS"; } int i = 0; while (true) { string a2 = i == 0 ? entityAlias : (entityAlias + i); if (!aliases.Contains(a2)) { aliases.Add(a2); entityAlias = a2; break; } i++; } } s.From(nameOrSelect, entityAlias); for (int i = 0; i < v.CountJoins(); i++) { Net.Vpc.Upa.Expressions.JoinCriteria c = v.GetJoin(i); Net.Vpc.Upa.Impl.Uql.Compiledexpression.CompiledNameOrSelect jnameOrSelect = (Net.Vpc.Upa.Impl.Uql.Compiledexpression.CompiledNameOrSelect)manager.TranslateAny(c.GetEntity(), declarations); entityAlias = c.GetEntityAlias(); if (entityAlias == null) { if (nameOrSelect is Net.Vpc.Upa.Impl.Uql.Compiledexpression.CompiledEntityName) { entityAlias = ((Net.Vpc.Upa.Impl.Uql.Compiledexpression.CompiledEntityName)nameOrSelect).GetName(); } else { entityAlias = "ALIAS"; } i = 0; while (true) { string a2 = i == 0 ? entityAlias : (entityAlias + i); if (!aliases.Contains(a2)) { aliases.Add(a2); entityAlias = a2; break; } i++; } } Net.Vpc.Upa.Impl.Uql.Compiledexpression.CompiledJoinCriteria cc = new Net.Vpc.Upa.Impl.Uql.Compiledexpression.CompiledJoinCriteria(c.GetJoinType(), jnameOrSelect, entityAlias, manager.TranslateAny(c.GetCondition(), declarations)); s.Join(cc); } for (int i = 0; i < v.CountFields(); i++) { Net.Vpc.Upa.Expressions.QueryField field = v.GetField(i); s.Field(manager.TranslateAny(field.GetExpression(), declarations), field.GetAlias()); } s.Where(manager.TranslateAny(v.GetWhere(), declarations)); s.Having(manager.TranslateAny(v.GetHaving(), declarations)); for (int i = 0; i < v.CountGroupByItems(); i++) { Net.Vpc.Upa.Expressions.Expression c = v.GetGroupBy(i); s.GroupBy(manager.TranslateAny(c, declarations)); } for (int i = 0; i < v.CountOrderByItems(); i++) { Net.Vpc.Upa.Expressions.Expression c = v.GetOrderBy(i); s.OrderBy(manager.TranslateAny(c, declarations), v.IsOrderAscending(i)); } return(s); }
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); }
public virtual Net.Vpc.Upa.Expressions.Select From(Net.Vpc.Upa.Expressions.Select view) { return(From(view, null)); }
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 InSelection(Net.Vpc.Upa.Expressions.Expression left, Net.Vpc.Upa.Expressions.Select query) : this(new Net.Vpc.Upa.Expressions.Expression[] { left }, query) { }
public override Net.Vpc.Upa.Expressions.Expression Copy() { Net.Vpc.Upa.Expressions.Select o = new Net.Vpc.Upa.Expressions.Select(); o.AddQuery(this); return(o); }
public Select(Net.Vpc.Upa.Expressions.Select other) : this() { AddQuery(other); }
public virtual Net.Vpc.Upa.Expressions.Select CrossJoin(Net.Vpc.Upa.Expressions.Select entity, string alias) { return(Join(Net.Vpc.Upa.Expressions.JoinType.CROSS_JOIN, entity, alias, null)); }
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); }