/// <summary> /// Gets the stringified query expression format of the current instance. A formatted string for field-operation-parameter will be /// conjuncted by the value of the <see cref="Conjunction"/> property. /// </summary> /// <returns>A stringified formatted-text of the current instance.</returns> public string GetString() { var groupList = new List <string>(); var conjunction = GetConjunctionText(); var separator = string.Concat(" ", conjunction, " "); if (QueryFields?.Count() > 0) { var fieldList = new List <string>(); foreach (var queryField in QueryFields) { fieldList.Add(queryField.AsFieldAndParameter()); } groupList.Add(fieldList.Join(separator)); } if (QueryGroups?.Count() > 0) { var fieldList = new List <string>(); foreach (var queryGroup in QueryGroups) { fieldList.Add(queryGroup.GetString()); } groupList.Add(fieldList.Join(separator)); } return(IsNot ? string.Concat("NOT (", groupList.Join(conjunction), ")") : string.Concat("(", groupList.Join(separator), ")")); }
/// <summary> /// Gets the stringified query expression format of the current instance. A formatted string for field-operation-parameter will be /// conjuncted by the value of the <see cref="RepoDb.Enumerations.Conjunction"/> property. /// </summary> /// <param name="index">The parameter index for batch operation.</param> /// <param name="dbSetting">The currently in used <see cref="IDbSetting"/> object.</param> /// <returns>A stringified formatted-text of the current instance.</returns> public string GetString(int index, IDbSetting dbSetting) { // Fix first the parameters Fix(); // Variables var groupList = new List <string>(); var conjunction = Conjunction.GetText(); var separator = string.Concat(" ", conjunction, " "); // Check the instance fields var queryFields = QueryFields.AsList(); if (queryFields?.Count > 0) { var fields = QueryFields .Select(qf => qf.AsFieldAndParameter(index, dbSetting)).Join(separator); groupList.Add(fields); } // Check the instance groups var queryGroups = QueryGroups.AsList(); if (queryGroups?.Count > 0) { var groups = QueryGroups .Select(qg => qg.GetString(index, dbSetting)).Join(separator); groupList.Add(groups); } // Return the value return(IsNot ? string.Concat("NOT (", groupList.Join(conjunction), ")") : string.Concat("(", groupList.Join(separator), ")")); }
/// <summary> /// Gets the stringified query expression format of the current instance. A formatted string for field-operation-parameter will be /// conjuncted by the value of the <see cref="Conjunction"/> property. /// </summary> /// <returns>A stringified formatted-text of the current instance.</returns> public string GetString() { var groupList = new List <string>(); var conjunction = GetConjunctionText(); var separator = string.Concat(" ", conjunction, " "); if (QueryFields != null && QueryFields.Any()) { var fieldList = new List <string>(); QueryFields.ToList().ForEach(queryField => { fieldList.Add(queryField.AsFieldAndParameter()); }); groupList.Add(fieldList.Join(separator)); } if (QueryGroups != null && QueryGroups.Any()) { var fieldList = new List <string>(); QueryGroups.ToList().ForEach(queryGroup => { fieldList.Add(queryGroup.GetString()); }); groupList.Add(fieldList.Join(separator)); } return(IsNot ? string.Concat("NOT (", groupList.Join(conjunction), ")") : string.Concat("(", groupList.Join(separator), ")")); }
/// <summary> /// Gets the stringified query expression format of the current instance. A formatted string for field-operation-parameter will be /// conjuncted by the value of the <see cref="Conjunction"/> property. /// </summary> /// <returns>A stringified formatted-text of the current instance.</returns> public string GetString() { var groupList = new List <string>(); var conjunction = GetConjunctionText(); if (QueryFields != null && QueryFields.Any()) { var fieldList = new List <string>(); QueryFields.ToList().ForEach(queryField => { fieldList.Add(queryField.AsFieldAndParameter()); }); groupList.Add(fieldList.Join($" {conjunction} ")); } if (QueryGroups != null && QueryGroups.Any()) { var fieldList = new List <string>(); QueryGroups.ToList().ForEach(queryGroup => { fieldList.Add(queryGroup.GetString()); }); groupList.Add(fieldList.Join($" {conjunction} ")); } return($"({groupList.Join($" {conjunction} ")})"); }
/// <summary> /// Reset the <see cref="QueryGroup"/> back to its default state (as is newly instantiated). /// </summary> public void Reset() { // Rest all fields if (QueryFields?.Any() == true) { foreach (var field in QueryFields) { field.Reset(); } } // Rest all groups if (QueryGroups?.Any() == true) { foreach (var group in QueryGroups) { group.Reset(); } } // Reset the attribute m_conjuctionTextAttribute = null; m_isFixed = false; // Reset the hash code m_hashCode = null; }
/// <summary> /// Add query fields /// </summary> /// <param name="fields">New query fields</param> void AddQueryFields(IEnumerable <string> fields) { if (fields.IsNullOrEmpty()) { return; } QueryFields.AddRange(fields); }
/// <summary> /// Join,并返回筛选值 /// </summary> /// <typeparam name="TInner">关联类型</typeparam> /// <param name="expression">关关表达式</param> /// <param name="resultSelector">返回值</param> /// <param name="joinType">join类型,默认Left</param> /// <returns></returns> public LambdaQuery <T> Join <TInner>( Expression <Func <T, TInner, bool> > expression, Expression <Func <T, TInner, object> > resultSelector, JoinType joinType = JoinType.Left) where TInner : IModel, new() { var innerType = typeof(TInner); //TypeCache.SetDBAdapterCache(innerType, dBAdapter); string condition = FormatJoinExpression(expression); var allFilds1 = TypeCache.GetProperties(typeof(T), true); var allFilds2 = TypeCache.GetProperties(innerType, true); var dic = new Dictionary <string, Type>(); foreach (var item in resultSelector.Parameters) { dic.Add(item.Name, item.Type); } QueryFields.Clear(); List <Attribute.FieldAttribute> resultFields = new List <Attribute.FieldAttribute>(); var newExpression = resultSelector.Body as NewExpression; int i = 0; foreach (var item in newExpression.Arguments) { var arry = item.ToString().Split('.'); var aliasName = arry[0]; //like a var type1 = dic[aliasName]; aliasName = GetPrefix(type1); //前缀 Attribute.FieldAttribute f; string name = newExpression.Members[i].Name; if (type1 == innerType)//关联 { //f = allFilds2.Find(b => b.Name == arry[1]); f = allFilds2[arry[1]]; f.AliasesName = name; if (resultFields.Find(b => b.ToString() == f.ToString()) != null) { throw new Exception("不能指定多次相同的字段" + f.Name); } resultFields.Add(f); } else//自已 { //f = allFilds1.Find(b => b.Name == arry[1]); f = allFilds1[arry[1]]; if (QueryFields.Find(b => b.ToString() == f.ToString()) != null) { throw new Exception("不能指定多次相同的字段" + f.Name); } f.AliasesName = name; f.SetFieldQueryScript(GetPrefix(), true, true); QueryFields.Add(f); } i += 1; } AddInnerRelation(innerType, condition, resultFields, true, joinType); return(this); }
/// <summary> /// Reset all the query fields. /// </summary> private void ResetQueryFields() { if (QueryFields?.Any() != true) { return; } foreach (var field in QueryFields) { field.Reset(); } }
public void AddFieldMapping(string field, int location) { if (QueryFields == null) { QueryFields = new Dictionary <string, int>(); } field = field.Split('.').Last().Replace("\"", "").ToUpper(); if (!QueryFields.ContainsKey(field)) { QueryFields.Add(field, location); } }
// Equality and comparers /// <summary> /// Returns the hashcode for this <see cref="QueryGroup"/>. /// </summary> /// <returns>The hashcode value.</returns> public override int GetHashCode() { // Make sure to check if this is already taken if (!ReferenceEquals(null, m_hashCode)) { return(m_hashCode.Value); } // Set the default value (should not be nullable for better performance) var hashCode = 0; // Iterates the child query field if (!ReferenceEquals(null, QueryFields)) { QueryFields.ToList().ForEach(queryField => { hashCode += queryField.GetHashCode(); }); } // Iterates the child query groups if (!ReferenceEquals(null, QueryGroups)) { QueryGroups.ToList().ForEach(queryGroup => { hashCode += queryGroup.GetHashCode(); }); } // Set with conjunction hashCode += Conjunction.GetHashCode(); // Set back the hashcode value m_hashCode = hashCode; // Return the actual hash code return(hashCode); }
public void SetUp() { DatabaseContext = SetupInMemoryDatabase(); DatabaseContext.Database.EnsureCreated(); _queries = new QueryFields(DatabaseContext); }
public override string GenerateQuery() { var qryProprties = this.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public).Where(p => p.CanRead && p.CanWrite && QueryFields.Contains(p.Name)).ToArray(); var isFirstMatch = true; StringBuilder qry = new StringBuilder(); qry.Append("{"); qry.Append("\"query\":"); qry.Append("{"); //query qry.Append("\"bool\":"); qry.Append("{"); //bool qry.Append("\"must\":"); qry.Append("["); //must for (var i = 0; i < qryProprties.Count(); i++) { var property = qryProprties[i]; var value = property.GetValue(this, null); var type = property.GetType(); //if value not set then continue if (value == null) { continue; } if (!isFirstMatch) { qry.Append(","); } else { isFirstMatch = false; } var matchString = type == typeof(int?) ? "{{\"match\":{{\"{0}\":{1}}}}}" : "{{\"match\":{{\"{0}\":\"{1}\"}}}}"; qry.AppendFormat(matchString, property.Name, value); } qry.Append("]");//must //set filter on start/end date if (StartDate.HasValue || EndDate.HasValue) { qry.Append(",\"filter\":"); qry.Append("{"); //filter qry.Append("\"range\":"); qry.Append("{"); //range qry.AppendFormat("\"{0}\":", "LastUpdateDateTicks"); qry.Append("{"); //orderticks if (StartDate.HasValue && EndDate.HasValue) { qry.AppendFormat("\"gte\":{0},\"lte\":{1}", StartDate.Value.Ticks, EndDate.Value.Ticks); } else if (StartDate.HasValue) { qry.AppendFormat("\"gte\":{0}", StartDate.Value.Ticks); } else { qry.AppendFormat("\"lte\":{0}", EndDate.Value.Ticks); } qry.Append("}"); //orderticks qry.Append("}"); //range qry.Append("}"); //filter } qry.Append("}"); //bool qry.Append("}"); //query qry.Append("}"); return(qry.ToString()); }
/// <summary> /// Force to append prefixes on the bound parameter objects. /// </summary> internal void AppendParametersPrefix() { QueryFields? .ToList() .ForEach(queryField => queryField.AppendParameterPrefix()); }