private void IfSameColumnConditionExistsRemoveCondition(string tableOrAliasName, RbacCondition condition) { bool somethingReplaced = false; foreach (string column in condition.Columns) { RbacWhereClause aWhereClause = WhereClauses.Find(tableOrAliasName, column); if (aWhereClause != null) { //this column is referred as condition in original query ParsedQuery = ParsedQuery.Replace(aWhereClause.WhereClauseString, string.Empty); somethingReplaced = true; } } if (somethingReplaced) { ParsedQuery = ParsedQuery.TrimEnd(); string[] words = new string[] { "and", "where" }; foreach (string word in words) { if ((ParsedQuery.Length > word.Length) && (ParsedQuery.Substring(ParsedQuery.Length - word.Length, word.Length).Equals(word, StringComparison.OrdinalIgnoreCase))) { ParsedQuery = ParsedQuery.Remove(ParsedQuery.Length - word.Length, word.Length); } } } }
protected override ISqlFragment buildWhereFragment(IMartenSession session) { if (WhereClauses.Count == 0) { return(_storage.DefaultWhereFragment()); } var parser = new WhereClauseParser(session, this); ISqlFragment where = null; switch (WhereClauses.Count) { case 0: where = _storage.DefaultWhereFragment(); break; case 1: where = parser.Build(WhereClauses.Single()); break; default: var wheres = WhereClauses.Select(x => parser.Build(x)).ToArray(); where = new CompoundWhereFragment("and", wheres); break; } return(_storage.FilterDocuments(null, where)); }
public override string ToString() { string connectionString; if (!string.IsNullOrWhiteSpace(LogicalConnectionString)) { connectionString = LogicalConnectionString; var regParams = new Regex(@"(@[\w\d]+)", RegexOptions.Compiled); MatchCollection matches = regParams.Matches(LogicalConnectionString); int padding = 0; foreach (Match match in matches) { if (!_whereClauses.ContainsKey(match.Value)) { throw new ArgumentException( string.Format("The Placeholder with the name '{0}' couldn't be found.", match.Value)); } string stringBefore = connectionString.Substring(0, padding + match.Index); connectionString = stringBefore + _whereClauses[match.Value] + connectionString.Substring(padding + match.Index + match.Length); padding += _whereClauses[match.Value].Length - match.Length; } } else { connectionString = string.Join(" AND ", WhereClauses.Select(x => x.Value)); } return(string.Format("WHERE {0}", connectionString)); }
private void AppendWhereClauses() { if (WhereClauses.Any()) { var combinedWhereClauses = string.Join("\n", WhereClauses); resultStringBuilder.Append(string.Format("\nWHERE {0}", combinedWhereClauses)); } }
//[DataMember(Order = 13)] //public new string FolderName { get; set; } public override Content.Query.IContentQuery <Content.Models.TextContent> Execute(DataRuleContext dataRuleContext) { var site = dataRuleContext.Site; var repository = Sites.Models.ModelExtensions.GetRepository(site); if (repository == null) { throw new SiteRepositoryNotExists(); } var folder = FolderHelper.Parse <TextFolder>(repository, FolderName).AsActual(); if (folder == null) { throw new KoobooException(string.Format("The folder does not exists.\"{0}\"".Localize(), FolderName)); } Content.Query.IContentQuery <Content.Models.TextContent> contentQuery = null; if (folder is TextFolder) { var textFolder = (TextFolder)folder; contentQuery = textFolder.CreateQuery(); } //else //{ // var binaryFolder = (MediaFolder)folder; // contentQuery = binaryFolder.CreateQuery(); //} if (WhereClauses != null) { contentQuery = contentQuery.Where(WhereClauses.Parse(folder.GetSchema(), dataRuleContext.ValueProvider)); } if (!string.IsNullOrEmpty(CategoryFolderName) && this.CategoryClauses != null && this.CategoryClauses.Length > 0) { var categoryFolder = FolderHelper.Parse <TextFolder>(repository, CategoryFolderName); var categoryQuery = categoryFolder.CreateQuery(); var expression = CategoryClauses.Parse(categoryFolder.GetSchema(), dataRuleContext.ValueProvider); if (expression != null) { categoryQuery = categoryQuery.Where(expression); contentQuery = contentQuery.WhereCategory(categoryQuery); } } //query the Published=null content for inline editor.. if (Page_Context.Current.EnabledInlineEditing(EditingType.Content)) { //contentQuery = contentQuery.Where( // new Content.Query.Expressions.OrElseExpression( // new Content.Query.Expressions.WhereEqualsExpression(null, "Published", true), // new Content.Query.Expressions.WhereEqualsExpression(null, "Published", null))); } else { contentQuery = contentQuery.WhereEquals("Published", true); //default query published=true. } return(contentQuery); }
public IEnumerable <Event> GetByQuery(IEnumerable <WhereClause> whereList) { var whereClauses = new WhereClauses(whereList); var query = GetBaseQuery().Where(whereClauses.Resolve()); var result = GetListSql(query, whereClauses.Parameters); return(result); }
/// <summary> /// Adds the where. /// </summary> /// <param name="clause">The clause.</param> public void AddWhere(WhereClause clause) { if (!WhereClauses.Any()) { clause.LogicOperator = null; } WhereClauses.Add(clause); }
public void ApplyFiltering <T>(DocumentSessionBase session, Expression <Func <T, bool> > expression) { var queryExpression = session.Query <T>().Where(expression).Expression; var model = MartenQueryParser.Flyweight.GetParsedQuery(queryExpression); var where = model.BodyClauses.OfType <WhereClause>().Single(); WhereClauses.Add(where); CompileLocal(session); }
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; }
private string BuildWhereString() { var result = ""; if (WhereClauses.Count > 0) { result += " WHERE ("; result += String.Join(" AND ", WhereClauses.Select(wc => wc.Get(UnderlyingQueryType))); result += ")"; } return(result); }
protected override IWhereFragment buildWhereFragment(MartenExpressionParser parser) { if (WhereClauses.Count == 0) { return(_storage.DefaultWhereFragment()); } var where = WhereClauses.Count == 1 ? parser.ParseWhereFragment(Fields, WhereClauses.Single().Predicate) : new CompoundWhereFragment(parser, Fields, "and", WhereClauses); return(_storage.FilterDocuments(null, where)); }
public override string Build() { var combinedSets = string.Join(", ", sets); if (!WhereClauses.Any()) { return(string.Format("UPDATE '{0}'\nSET {1}", tableName, combinedSets)); } var combinedWhereClauses = string.Join("\n", WhereClauses); return(string.Format("UPDATE '{0}'\nSET {1}\nWHERE {2}", tableName, combinedSets, combinedWhereClauses)); }
public void RenderWhereClausesOr() { Column column1 = new Column("Column1"); Column column2 = new Column("Column2"); StringLiteralValue stringLiteralValue1 = new StringLiteralValue("Value1"); StringLiteralValue stringLiteralValue2 = new StringLiteralValue("Value2"); ComparisonPredicate comparisonPredicate1 = new ComparisonPredicate(column1, ComparisonPredicateType.Equals, stringLiteralValue1); ComparisonPredicate comparisonPredicate2 = new ComparisonPredicate(column2, ComparisonPredicateType.Equals, stringLiteralValue2); WhereClause whereClause1 = new WhereClause(comparisonPredicate1); WhereClause whereClause2 = new WhereClause(comparisonPredicate2); WhereClauses whereClauses = new WhereClauses(whereClause1, BooleanOperatorType.Or, whereClause2); Assert.AreEqual("([Column1] = 'Value1' OR [Column2] = 'Value2')", sqlClientRenderer.Render(whereClauses)); }
protected override IWhereFragment buildWhereFragment(MartenExpressionParser parser) { // TODO -- this logic is duplicated. Pull it into a helper somewhere. // Find the duplication of DefaultWhereFragment/ FilterDocuments in other places if (WhereClauses.Count == 0) { return(_storage.DefaultWhereFragment()); } var where = WhereClauses.Count == 1 ? parser.ParseWhereFragment(Fields, WhereClauses.Single().Predicate) : new CompoundWhereFragment(parser, Fields, "and", WhereClauses); return(_storage.FilterDocuments(null, where)); }
public void AddWhereClauses() { Column column1 = new Column("Column1"); Column column2 = new Column("Column2"); StringLiteralValue stringLiteralValue1 = new StringLiteralValue("Value1"); StringLiteralValue stringLiteralValue2 = new StringLiteralValue("Value2"); ComparisonPredicate comparisonPredicate1 = new ComparisonPredicate(column1, ComparisonPredicateType.Equals, stringLiteralValue1); ComparisonPredicate comparisonPredicate2 = new ComparisonPredicate(column2, ComparisonPredicateType.Equals, stringLiteralValue2); WhereClause whereClause1 = new WhereClause(comparisonPredicate1); WhereClause whereClause2 = new WhereClause(comparisonPredicate2); WhereClauses whereClauses = new WhereClauses(whereClause1, BooleanOperatorType.And, whereClause2); Assert.AreEqual(whereClause1, whereClauses.LeftClause); Assert.AreEqual(BooleanOperatorType.And, whereClauses.OperatorType); Assert.AreEqual(whereClause2, whereClauses.RightClause); }
public override IContentQuery <Content.Models.TextContent> GetContentQuery(DataRuleContext dataRuleContext) { var site = dataRuleContext.Site; var repositoryName = site.Repository; if (string.IsNullOrEmpty(repositoryName)) { throw new KoobooException("The repository for site is null."); } var repository = new Repository(repositoryName); var schema = new Schema(repository, SchemaName); var contentQuery = (IContentQuery <Content.Models.TextContent>)schema.CreateQuery(); contentQuery.Where(WhereClauses.Parse(schema, dataRuleContext.ValueProvider)); return(contentQuery); }
public override string Render(WhereClauses whereClauses) { if (whereClauses.LeftClause == null) { return(whereClauses.RightClause.Render(this)); } else if (whereClauses.RightClause == null) { return(whereClauses.LeftClause.Render(this)); } else { StringBuilder text = new StringBuilder(); text.AppendFormat("({0} ", whereClauses.LeftClause.Render(this)); text.AppendFormat("{0} ", whereClauses.OperatorType.ToString().ToUpper()); text.AppendFormat("{0}) ", whereClauses.RightClause.Render(this)); return(text.ToString().Trim()); } }
protected virtual IWhereFragment buildWhereFragment(MartenExpressionParser parser) { return(WhereClauses.Count == 1 ? parser.ParseWhereFragment(Fields, WhereClauses.Single().Predicate) : new CompoundWhereFragment(parser, Fields, "and", WhereClauses)); }
public abstract string Render(WhereClauses whereClauses);
public override void AddWhere(ArrayWhereParameter whereParams) { WhereClauses.Add(new ArrayWhereClause(new AliasedWhereFieldReference(From.Source.Alias, new WhereFieldReference(whereParams.FieldName)), whereParams.Operand, whereParams.ParameterNames)); }
public override void AddWhere(BinaryWhereParameter whereParams) { WhereClauses.Add(new BinaryWhereClause(new AliasedWhereFieldReference(From.Source.Alias, new WhereFieldReference(whereParams.FieldName)), whereParams.Operand, whereParams.FirstParameterName, whereParams.SecondParameterName)); }
public override ISelect GenerateSelectWithoutDefaultOrderBy() { var hasNoConfiguration = customAlias == null && !OrderByClauses.Any() && !WhereClauses.Any() && ColumnSelection == null && RowSelection == null; return(hasNoConfiguration ? innerSelect : base.GenerateSelectWithoutDefaultOrderBy()); }
public bool WillEverHaveResults() { return(Select.IsValid() && WhereClauses.All(fragment => fragment.IsValid()) && ThenOrderClauses.All(fragment => fragment.IsValid())); }
private void ParseSqlSelectStatement(TSqlStatement sqlStatement) { if (sqlStatement.GetType() == typeof(SelectStatement)) { SelectStatement aSelectStatement = (SelectStatement)sqlStatement; QueryExpression aQueryExpression = aSelectStatement.QueryExpression; if (aQueryExpression.GetType() == typeof(QuerySpecification)) { QuerySpecification aQuerySpecification = (QuerySpecification)aQueryExpression; if (aQuerySpecification.FromClause == null) { return; //'select *' is valid query, but not of our interest } //tables NamedTableReferenceVisitor ntVisitor = new NamedTableReferenceVisitor(Context); aQuerySpecification.FromClause.Accept(ntVisitor); TablesReferred = ntVisitor.Tables; //columns ScalarExpressionVisitor seVisitor = null; foreach (SelectElement selectStatement in aQuerySpecification.SelectElements) { seVisitor = new ScalarExpressionVisitor(Context, TablesReferred[0]); selectStatement.Accept(seVisitor); Columns.AddRange(seVisitor.Columns); } if (!string.IsNullOrEmpty(seVisitor.ParsedQuery)) { ParsedQuery = seVisitor.ParsedQuery; //todo } //else // ParsedQuery = Columns.GetParsedQuery(ParsedQuery); UpdateReferredTables(Columns); //ensure unique tables TablesReferred = new List <RbacTable>(TablesReferred.DistinctBy(t => t.Name)); //joins JoinClauseVisitor jcVisitor = new JoinClauseVisitor(Context); aQueryExpression.AcceptChildren(jcVisitor); JoinClauses = jcVisitor.JoinClauses; //where clause if (aQuerySpecification.WhereClause != null) { EqualVisitor cidv = new EqualVisitor(ParsedQuery); InPredicateVisitor inpv = new InPredicateVisitor(ParsedQuery); aQuerySpecification.WhereClause.AcceptChildren(cidv); aQuerySpecification.WhereClause.AcceptChildren(inpv); WhereClauses.AddRange(cidv.WhereClauses); WhereClauses.AddRange(inpv.WhereClauses); WhereClauses.ParseReferenceTableNames(JoinClauses); } } } else { Errors.Add("Not a select statement!"); } }