private void AddRelationalJoins(RbacRelation relation, List <RbacCondition> conditions) { //was relation already referred in the query? RbacTable relationTable = TablesReferred.Find(relation.WithTable); if (relationTable == null) { //NO: //Is there already join with that table? RbacJoin join = JoinClauses.JoinExists(relation.WithTable, relation.SelfName); if (join == null) { //add new join, at the end of operation we will stringify sequentially join = RbacJoin.AddNewJoin(this, relation.SelfName, relation.SelfColumnName, relation.WithTable, relation.WithColumn); } //add into referred table as 'ReferencedOnly' relationTable = new RbacTable(string.Empty, relation.WithTable, false); relationTable.Conditions.AddRange(conditions); relationTable.ReferencedOnly = true; relationTable.TempAlias = join.WithTableAlias; TablesReferred.Add(relationTable); } //add condition ApplyCondition(relationTable); }
public JoinClause <TEntity> Join <TJoin>(JoinType joinType = JoinType.InnerJoin, string alias = null) { var joinClause = new JoinClause <TEntity>(_builder); joinClause.Init <TJoin>(joinType, alias); JoinClauses.Add(joinClause); return(joinClause); }
/// <summary> /// Addds the default join. /// </summary> /// <param name="join">The join.</param> /// <param name="toTableName">Name of to table.</param> /// <param name="toTableAlias">To table alias.</param> /// <returns>The created join clause.</returns> public JoinClause AdddDefaultJoin(JoinType join, string toTableName, string toTableAlias) { var newJoin = new JoinClause(join, toTableName, toTableAlias); JoinClauses.Add(newJoin); return(newJoin); }
/// <summary> /// Adds the join. /// </summary> /// <param name="join">The join.</param> /// <param name="toTableName">Name of to table.</param> /// <param name="toTableAlias">To table alias.</param> /// <param name="toColumnName">Name of to column.</param> /// <param name="operator">The operator.</param> /// <param name="fromTableAlias">From table alias.</param> /// <param name="fromColumnName">Name of from column.</param> /// <returns>The created join clause.</returns> public JoinClause AddJoin(JoinType join, string toTableName, string toTableAlias, string toColumnName, Comparison @operator, string fromTableAlias, string fromColumnName) { var newJoin = new JoinClause(join, toTableName, toTableAlias, toColumnName, @operator, fromTableAlias, fromColumnName); JoinClauses.Add(newJoin); return(newJoin); }
public SQLClause(SelectClauses _select, object _from, JoinClauses _join, WhereClauses _where, OrderByClauses _orderby) { this._select = _select; this._from = _from; this._join = _join; this._where = _where; this._orderby = _orderby; }
internal void Add <TProperty>(JoinClauses clause, bool isNegation, Match type, Expression <Func <T, TProperty> > selector, params TProperty[] values) { var colName = _config.GetColumnName(selector.GetPropertyName()); var termInternal = new TermInternal { IsNegation = isNegation, Type = type, JoinClause = _terms.Any() ? clause : JoinClauses.None, ColumnName = colName, Keywords = ToKeywords(values) }; _terms.Add(termInternal); }
private void ParseQuery(string query) { if (string.IsNullOrEmpty(query)) { RbacException.Raise("Cannot parse an empty query!"); } OriginalQuery = query; ParsedQuery = query; if (!IsParsingSkipped) { ExecutionTime.Start(ExecutionTimeTrackers.ParseQuery); ParseQueryType(); ParseInternal(query); ExecutionTime.Stop(ExecutionTimeTrackers.ParseQuery); if (!IsParsed) { return; } if (QueryType == RbacQueryTypes.Select) { ExecutionTime.Start(ExecutionTimeTrackers.ConditionsNRelations); ApplyConditions(); ApplyConditionsRelational(); ParsedQuery = JoinClauses.ParseQuery(ParsedQuery); ExecutionTime.Stop(ExecutionTimeTrackers.ConditionsNRelations); } } else { Errors.Add("Parsing skipped!"); } ExecutionTime.Start(ExecutionTimeTrackers.ApplyPermissions); ApplyPermission(); ExecutionTime.Stop(ExecutionTimeTrackers.ApplyPermissions); if ((QueryType != RbacQueryTypes.Insert) && (QueryType != RbacQueryTypes.Delete)) { ExecutionTime.Start(ExecutionTimeTrackers.ApplyParameters); if (!ApplyParameters()) { RbacException.Raise(ParamErrors.ToLine()); } ExecutionTime.Stop(ExecutionTimeTrackers.ApplyParameters); } WriteLogParseDetails(); }
private void AddRelationalJoin(RbacTable table) { if (table.Relations.Count != 0) { foreach (RbacRelation relation in table.Relations) { RbacTable withTable = Context.User.Role.CrudPermissions.Find(relation.WithTable); if (withTable != null) { AddRelationalJoins(relation, withTable.Conditions); } AddRelationalJoin(withTable); } } else { //we have reached end of relation (linked list), there is no further relations ahead of us, time to cleanup //let's see if we need those joins, if not let's remove those. List <RbacTable> tables = new List <RbacTable>(TablesReferred); tables = tables.Where(t => t.ReferencedOnly).ToList(); tables.Reverse(); foreach (RbacTable t in tables) { if (t.Conditions.Count == 0) { var joinTobeRemoved = JoinClauses.Where(jc => jc.WithTableName == t.Name).SingleOrDefault(); JoinClauses.Remove(joinTobeRemoved); } else { //oops while reversing back we found a condition, let's stop here! break; } } } }
protected object VisitMember(MemberExpression m, bool columnOnly) { try { if (m.Expression != null && m.Expression.NodeType == ExpressionType.Constant && (_overridedNodeType == null)) { VisitConstantFixed(m.Expression as ConstantExpression, m.Member?.Name); return(m); } else if (m.Expression != null && (m.Expression.NodeType == ExpressionType.Parameter || (m.ToString().EndsWith(".HasValue") && m.Expression.NodeType == ExpressionType.MemberAccess)) && (_overridedNodeType == null)) { var hasValueAttr = m.ToString().EndsWith(".HasValue"); _overridedNodeType = null; var cl = hasValueAttr ? (m.Expression as MemberExpression).Expression.Type : m.Expression.Type; var prop = DeepCloner.GetFastDeepClonerProperties(cl).First(x => x.Name == (hasValueAttr ? (m.Expression as MemberExpression).Member.Name : m.Member.Name)); var name = prop.GetPropertyName(); var table = cl.TableName(); var columnName = string.Format("[{0}].[{1}]", table, name).CleanValidSqlName(DataBaseTypes); var dataEncode = prop.GetCustomAttribute <DataEncode>(); if (columnOnly) { return(columnName); } bool isNot = sb.ToString().EndsWith("NOT "); if (prop.PropertyType.IsEnum && prop.ContainAttribute <Stringify>()) { if (!SavedTypes.ContainsKey(prop.FullName)) { SavedTypes.TryAdd(prop.FullName, prop.PropertyType); } columnName += stringyFy.Replace("#", prop.FullName); } if (isNot) { if (!hasValueAttr) { columnName = $"(CASE WHEN { columnName } = 0 THEN 1 ELSE 0 END) {boolString.Replace("#", "0")}"; } else { columnName = $"(CASE WHEN {columnName} IS NULL THEN 1 ELSE 0 END) {boolString.Replace("#", "0")}"; } } else if (hasValueAttr) { columnName = $"(CASE WHEN {columnName} IS NULL THEN 0 ELSE 1 END) {boolString.Replace("#", "1")}"; } else if (prop.PropertyType == typeof(bool) || prop.PropertyType == typeof(bool?)) { columnName = columnName + boolString.Replace("#", "1"); } if (dataEncode != null) { columnName = columnName + dataEncodeString.Replace("#", dataEncode.Key + "|" + ((int)dataEncode.KeySize).ToString()); } sb.Append(columnName); return(m); } else if (m.Expression != null && (m.Expression.NodeType == ExpressionType.MemberAccess)) { _overridedNodeType = null; var key = string.Join("", m.ToString().Split('.').Take(m.ToString().Split('.').Length - 1)); var cl = m.Expression.Type; var prop = DeepCloner.GetFastDeepClonerProperties(cl).First(x => x.Name == m.Member.Name); var name = prop.GetPropertyName(); var table = cl.TableName(); var randomTableName = JoinClauses.ContainsKey(key) ? JoinClauses[key].Item1 : RandomKey(); var primaryId = DeepCloner.GetFastDeepClonerProperties(cl).First(x => x.ContainAttribute <PrimaryKey>()).GetPropertyName(); var columnName = string.Format("[{0}].[{1}]", randomTableName, name).CleanValidSqlName(DataBaseTypes); if (columnOnly) { return(columnName); } sb.Append(columnName); if (JoinClauses.ContainsKey(key)) { return(m); } // Ok lets build inner join var parentType = (m.Expression as MemberExpression).Expression.Type; var parentTable = parentType.TableName(); prop = DeepCloner.GetFastDeepClonerProperties(parentType).FirstOrDefault(x => x.ContainAttribute <ForeignKey>() && x.GetCustomAttribute <ForeignKey>().Type == cl); var v = ""; if (prop != null) { v += string.Format("LEFT JOIN [{0}] {1} ON [{2}].[{3}] = [{4}].[{5}]", table, randomTableName, randomTableName, primaryId, parentTable, prop.GetPropertyName()).CleanValidSqlName(DataBaseTypes); } else { prop = DeepCloner.GetFastDeepClonerProperties(cl).FirstOrDefault(x => x.ContainAttribute <ForeignKey>() && x.GetCustomAttribute <ForeignKey>().Type == parentType); if (prop != null) { v += string.Format("LEFT JOIN [{0}] {1} ON [{2}].[{3}] = [{4}].[{5}]", table, randomTableName, randomTableName, prop.GetPropertyName(), parentTable, primaryId).CleanValidSqlName(DataBaseTypes); } } if (string.IsNullOrEmpty(v)) { sb = sb.Remove(sb.Length - columnName.Length, columnName.Length); sb.Append(ValuetoSql(GetValue(m))); } else { JoinClauses.TryAdd(key, new Tuple <string, string>(randomTableName, v)); } return(m); } else if (m.Expression != null && _overridedNodeType == ExpressionType.MemberAccess) { _overridedNodeType = null; var key = string.Join("", m.ToString().Split('.').Take(m.ToString().Split('.').Length - 1)); var cl = m.Expression.Type; var prop = DeepCloner.GetFastDeepClonerProperties(cl).First(x => x.Name == m.Member.Name); var table = cl.TableName(); var randomTableName = JoinClauses.ContainsKey(key) ? JoinClauses[key].Item1 : RandomKey(); var primaryId = DeepCloner.GetFastDeepClonerProperties(cl).First(x => x.ContainAttribute <PrimaryKey>()).GetPropertyName(); if (JoinClauses.ContainsKey(key)) { return(m); } // Ok lets build inner join var parentType = (m as MemberExpression).Type.GetActualType(); var parentTable = parentType.TableName(); prop = DeepCloner.GetFastDeepClonerProperties(parentType).FirstOrDefault(x => x.ContainAttribute <ForeignKey>() && x.GetCustomAttribute <ForeignKey>().Type == cl); var v = ""; if (prop != null) { v += string.Format("INNER JOIN [{0}] {1} ON [{2}].[{3}] = [{4}].[{5}]", parentTable, randomTableName, table, primaryId, randomTableName, prop.GetPropertyName()).CleanValidSqlName(DataBaseTypes); } else { throw new NotSupportedException(string.Format("Expression STRUCTURE IS NOT SUPPORTED MEMBER{0} for EntityWorker", m.Member.Name)); } if (!string.IsNullOrEmpty(v)) { JoinClauses.TryAdd(key, new Tuple <string, string>(randomTableName, v)); } return(m); } } catch { throw new NotSupportedException(string.Format("Expression '{0}' is not supported", m.ToString())); } if (m.Type.IsInternalType() && m.Expression.NodeType == ExpressionType.Call) { sb.Append(ValuetoSql(Expression.Lambda(m).Compile().DynamicInvoke())); return(m); } throw new NotSupportedException(string.Format("The member '{0}' is not supported", m.Member.Name)); }
protected dynamic VisitMember(MemberExpression m, bool columnOnly) { if (m.Expression != null && m.Expression.NodeType == ExpressionType.Constant && (_overridedNodeType == null)) { VisitConstantFixed(m.Expression as ConstantExpression, m.Member?.Name); return(m); } else if (m.Expression != null && m.Expression.NodeType == ExpressionType.Parameter && (_overridedNodeType == null)) { _overridedNodeType = null; var cl = m.Expression.Type; var prop = FastDeepCloner.DeepCloner.GetFastDeepClonerProperties(cl).First(x => x.Name == m.Member.Name); var name = prop.GetPropertyName(); var table = cl.GetCustomAttribute <Table>()?.Name ?? cl.Name; var columnName = string.Format("[{0}].[{1}]", table, name); if (columnOnly) { return(columnName); } sb.Append(columnName); return(m); } else if (m.Expression != null && (m.Expression.NodeType == ExpressionType.MemberAccess)) { _overridedNodeType = null; var key = string.Join("", m.ToString().Split('.').Take(m.ToString().Split('.').Length - 1)); var cl = m.Expression.Type; var prop = FastDeepCloner.DeepCloner.GetFastDeepClonerProperties(cl).First(x => x.Name == m.Member.Name); var name = prop.GetPropertyName(); var table = cl.GetCustomAttribute <Table>()?.Name ?? cl.Name; var randomTableName = JoinClauses.ContainsKey(key) ? JoinClauses[key].Item1 : RandomKey(); var primaryId = FastDeepCloner.DeepCloner.GetFastDeepClonerProperties(cl).First(x => x.ContainAttribute <PrimaryKey>()).GetPropertyName(); var columnName = string.Format("[{0}].[{1}]", randomTableName, name); if (columnOnly) { return(columnName); } sb.Append(columnName); if (JoinClauses.ContainsKey(key)) { return(m); } // Ok lets build inner join var parentType = (m.Expression as MemberExpression).Expression.Type; var parentTable = parentType.GetCustomAttribute <Table>()?.Name ?? parentType.Name; prop = FastDeepCloner.DeepCloner.GetFastDeepClonerProperties(parentType).FirstOrDefault(x => x.ContainAttribute <ForeignKey>() && x.GetCustomAttribute <ForeignKey>().Type == cl); var v = ""; if (prop != null) { v += string.Format("LEFT JOIN [{0}] {1} ON [{2}].[{3}] = [{4}].[{5}]", table, randomTableName, randomTableName, primaryId, parentTable, prop.GetPropertyName()); } else { prop = FastDeepCloner.DeepCloner.GetFastDeepClonerProperties(cl).FirstOrDefault(x => x.ContainAttribute <ForeignKey>() && x.GetCustomAttribute <ForeignKey>().Type == parentType); if (prop != null) { v += string.Format("LEFT JOIN [{0}] {1} ON [{2}].[{3}] = [{4}].[{5}]", table, randomTableName, randomTableName, prop.GetPropertyName(), parentTable, primaryId); } } JoinClauses.Add(key, new Tuple <string, string>(randomTableName, v)); return(m); } else if (m.Expression != null && _overridedNodeType == ExpressionType.MemberAccess) { _overridedNodeType = null; var key = string.Join("", m.ToString().Split('.').Take(m.ToString().Split('.').Length - 1)); var cl = m.Expression.Type; var prop = FastDeepCloner.DeepCloner.GetFastDeepClonerProperties(cl).First(x => x.Name == m.Member.Name); var table = cl.GetCustomAttribute <Table>()?.Name ?? cl.Name; var randomTableName = JoinClauses.ContainsKey(key) ? JoinClauses[key].Item1 : RandomKey(); var primaryId = FastDeepCloner.DeepCloner.GetFastDeepClonerProperties(cl).First(x => x.ContainAttribute <PrimaryKey>()).GetPropertyName(); if (JoinClauses.ContainsKey(key)) { return(m); } // Ok lets build inner join var parentType = (m as MemberExpression).Type.GetActualType(); var parentTable = parentType.GetCustomAttribute <Table>()?.Name ?? parentType.Name; prop = FastDeepCloner.DeepCloner.GetFastDeepClonerProperties(parentType).FirstOrDefault(x => x.ContainAttribute <ForeignKey>() && x.GetCustomAttribute <ForeignKey>().Type == cl); var v = ""; if (prop != null) { v += string.Format("INNER JOIN [{0}] {1} ON [{2}].[{3}] = [{4}].[{5}]", parentTable, randomTableName, table, primaryId, randomTableName, prop.GetPropertyName()); } else { throw new NotSupportedException(string.Format("CLASS STRUCTURE IS NOT SUPPORTED MEMBER{0}", m.Member.Name)); } if (!string.IsNullOrEmpty(v)) { JoinClauses.Add(key, new Tuple <string, string>(randomTableName, v)); } return(m); } throw new NotSupportedException(string.Format("The member '{0}' is not supported", m.Member.Name)); }
/// <summary> /// Adds a table or view to the FROM list of the statement. /// </summary> public FromBuilder Join(JoinType joinType, String objectName, String onClause = null, params Object[] arguments) { JoinClauses.Add(new JoinClause(joinType, objectName, onClause != null ? String.Format(onClause, arguments) : null)); return(this); }
/// <summary> /// Adds the join. /// </summary> /// <param name="newJoin">The new join.</param> public void AddJoin(JoinClause newJoin) { JoinClauses.Add(newJoin); }
public Join On(JoinClause joinClause) { JoinClauses.Add(joinClause); return(this); }
//public IAcceptsJoin Join(TableOrSubQuery source, string constraint) //{ // this.Accept(this.Source.Join(source, constraint)); // return this; //} //public IAcceptsJoin Join(string table, string constraint, string alias) //{ // this.Accept(this.Source.Join(table, constraint, alias)); // return this; //} //public IAcceptsGroupBy Where(string expression) //{ // this.Accept(new WhereClause(expression)); // return this; //} //public IAcceptsOrderBy GroupBy(params string[] termArgs) //{ // return GroupBy((IEnumerable<string>)termArgs); //} //public IAcceptsOrderBy GroupBy(IEnumerable<string> terms) //{ // this.Accept(new GroupByClause(terms)); // return this; //} //public IAcceptsLimit OrderBy(IEnumerable<string> terms) //{ // this.Accept(new OrderByClause(terms)); // return this; //} //public IAcceptsLimit OrderBy(params string[] termArgs) //{ // this.Accept(new OrderByClause(termArgs)); // return this; //} //public ISelectElement Limit(int limit, int offset) //{ // this.Accept(new LimitClause(limit, offset)); // return this; //} //public void Accept(ISelectElement parent) //{ // throw new NotSupportedException("select can't have parent"); //} public void Accept(JoinClause joinClause) { JoinClauses.Add(joinClause); }