Beispiel #1
0
        /// <summary>
        /// 得到名称
        /// </summary>
        /// <param name="queryCompiler"></param>
        /// <param name="match"></param>
        /// <returns></returns>
        protected virtual void AppendPropertyName(QueryCompilerInfo queryCompiler, Match match)
        {
            var propertyName    = match.Value.Trim(',').Trim();
            var chainProperties = queryCompiler.Object.GetChainProperties(propertyName);

            queryCompiler.AddJoins(chainProperties);
            var property = chainProperties[chainProperties.Count - 1];

            queryCompiler.Builder.Append(queryCompiler.GetFieldName(property, propertyName));
            if (match.Value.Contains(","))
            {
                queryCompiler.Builder.Append(",");
            }
        }
Beispiel #2
0
        /// <summary>
        /// 转换属性到字段
        /// </summary>
        /// <param name="property"></param>
        /// <param name="queryCompiler"></param>
        /// <param name="propertyName"></param>
        protected virtual string GetAsName(OrmPropertyInfo property,
                                           QueryCompilerInfo queryCompiler, string propertyName)
        {
            if (!property.AllowRead)
            {
                return(null);
            }
            var builder = new StringBuilder();

            builder.Append(queryCompiler.GetFieldName(property, propertyName));
            builder.Append(" as ");
            builder.AppendFormat("{0}_{1}", propertyName.Replace(".", "_"), queryCompiler.FieldCount);
            builder.Append(",");
            return(builder.ToString());
        }
Beispiel #3
0
 /// <summary>
 /// 设置Query的Orderby属性
 /// </summary>
 /// <param name="obj"></param>
 /// <param name="query"></param>
 /// <param name="orderbyCompiler"></param>
 protected virtual string GetDefaultOrderby(OrmObjectInfo obj, QueryInfo query, QueryCompilerInfo orderbyCompiler)
 {
     if (orderbyCompiler.Builder.Length > 0)
     {
         return(orderbyCompiler.Builder.ToString());
     }
     if (!string.IsNullOrEmpty(query.SelectExp))
     {
         var selectArray = query.SelectExp.Split(',');
         foreach (var property in obj.Properties)
         {
             if (selectArray.Contains(property.PropertyName))
             {
                 return(string.Format("{0} asc", orderbyCompiler.GetFieldName(property, property.PropertyName)));
             }
         }
     }
     return(string.Format("{0} asc", obj.PrimaryProperty != null && !query.IsDisinct ? orderbyCompiler.GetFieldName(obj.PrimaryProperty, obj.PrimaryProperty.PropertyName) : orderbyCompiler.GetFieldName(obj.Properties.First(), obj.Properties.First().PropertyName)));
 }
Beispiel #4
0
        /// <summary>
        /// 得到名称
        /// </summary>
        /// <param name="queryCompiler"></param>
        /// <param name="match"></param>
        /// <returns></returns>
        protected virtual void AppendPropertyName(QueryCompilerInfo queryCompiler, Match match)
        {
            var m               = Regex.Match(match.Value, PropertyPattern);
            var propertyName    = m.Value.Trim();
            var chainProperties = queryCompiler.Object.GetChainProperties(propertyName);

            queryCompiler.AddJoins(chainProperties);
            var property = chainProperties[chainProperties.Count - 1];

            queryCompiler.Builder.Append(queryCompiler.GetFieldName(property, propertyName));
            m = m.NextMatch();
            if (m.Length > 0)
            {
                queryCompiler.Builder.AppendFormat(" {0}", m.Value);
            }
            if (match.Value.Contains(","))
            {
                queryCompiler.Builder.Append(",");
            }
        }
Beispiel #5
0
        /// <summary>
        /// 得到属性开始的索引
        /// </summary>
        /// <param name="queryCompiler"></param>
        /// <param name="match"></param>
        /// <returns></returns>
        protected virtual bool AppendSelectManySql(QueryCompilerInfo queryCompiler, Match match)
        {
            var m = Regex.Match(match.Value, ManyKeyPattern);

            if (!m.Success)
            {
                return(false);
            }
            var    lastIndex = Regex.Match(match.Value, PropertyPattern).Value.LastIndexOf('.');
            string subSelect;

            if (Regex.IsMatch(match.Value, SubQueryPattern))
            {
                var index = match.Value.IndexOf(".Select");
                if (index == -1)
                {
                    index = match.Value.LastIndexOf(".");
                }
                else
                {
                    index = match.Value.LastIndexOf(".", index, index);
                }
                var value = match.Value.Substring(index,
                                                  match.Value.Length - index);
                subSelect = Regex.Match(value, BreakersPattern).Value;
            }
            else
            {
                subSelect = Regex.Match(match.Value, BreakersPattern).Value;
            }
            subSelect = subSelect.Substring(1, subSelect.Length - 2);
            var propertyName    = match.Value.Substring(0, lastIndex);
            var chainProperties = queryCompiler.Object.GetChainProperties(propertyName, true);
            var nameBuilder     = new StringBuilder();

            for (int i = 0; i < chainProperties.Count; i++)
            {
                if (nameBuilder.Length > 0)
                {
                    nameBuilder.AppendFormat(".{0}", chainProperties[i].PropertyName);
                }
                else
                {
                    nameBuilder.AppendFormat(chainProperties[i].PropertyName);
                }
                if (chainProperties[i].Map != null && chainProperties[i].Map.CheckRemote())
                {
                    var subText = match.Value.Substring(nameBuilder.Length + 1,
                                                        match.Value.Length - nameBuilder.Length - 1).Trim();
                    if (subText.StartsWith("Select("))
                    {
                        subText = subSelect;
                    }
                    if (FillRemoteQuery(queryCompiler, nameBuilder.ToString(), chainProperties[i], subText, match))
                    {
                        var tps     = new List <OrmPropertyInfo>();
                        var builder = new StringBuilder();
                        for (int j = 0; j <= i; j++)
                        {
                            tps.Add(chainProperties[j]);
                            if (j < i)
                            {
                                builder.AppendFormat("{0}.", chainProperties[j].PropertyName);
                            }
                        }
                        builder.AppendFormat("{0}", chainProperties[i].Map.ObjectProperty.PropertyName);
                        queryCompiler.AddJoins(tps);
                        var name = queryCompiler.GetFieldName(chainProperties[i].Map.ObjectProperty, builder.ToString());
                        if (!queryCompiler.Builder.ToString().Contains(name))
                        {
                            queryCompiler.Builder.Append(name);
                            queryCompiler.Builder.AppendFormat(" as {0}_{1},", builder.ToString().Replace(".", "_"), queryCompiler.FieldCount);
                        }
                        return(true);
                    }
                }
            }
            queryCompiler.AddJoins(chainProperties);
            var property = chainProperties[chainProperties.Count - 1];

            AppendManySql(queryCompiler, match, property, propertyName, subSelect);
            AppendAsSql(queryCompiler, match, propertyName);
            return(true);
        }
Beispiel #6
0
        /// <summary>
        /// 得到属性开始的索引
        /// </summary>
        /// <param name="queryCompiler"></param>
        /// <param name="match"></param>
        /// <returns></returns>
        protected virtual bool AppendSelectPropertySql(QueryCompilerInfo queryCompiler, Match match)
        {
            var m = Regex.Match(match.Value, PropertyKeyPattern);

            if (!m.Success)
            {
                return(false);
            }
            var propertyName    = m.Value.Trim().Trim(',');
            var chainProperties = queryCompiler.Object.GetChainProperties(propertyName);
            var property        = chainProperties[chainProperties.Count - 1];

            if (property.Map != null)
            {
                var subSelect = propertyName;
                if (property.PropertyName.Equals(propertyName))
                {
                    subSelect = "*";
                }
                else
                {
                    var name  = string.Format("{0}.", property.PropertyName);
                    var index = propertyName.IndexOf(name);
                    if (index >= 0)
                    {
                        subSelect = propertyName.Substring(index + name.Length);
                    }
                }
                var realPropertyName = propertyName.Replace(string.Format(".{0}", subSelect), "");
                if (FillRemoteQuery(queryCompiler, realPropertyName, property, subSelect, match))
                {
                    queryCompiler.AddJoins(chainProperties);
                    var builder = new StringBuilder();
                    for (int i = 0; i < chainProperties.Count - 1; i++)
                    {
                        builder.AppendFormat("{0}.", chainProperties[i].PropertyName);
                    }
                    builder.Append(property.Map.ObjectProperty.PropertyName);
                    var name = queryCompiler.GetFieldName(property.Map.ObjectProperty, builder.ToString());
                    if (!queryCompiler.Builder.ToString().Contains(name))
                    {
                        queryCompiler.Builder.Append(name);
                        var bd = new StringBuilder();
                        for (int i = 0; i < chainProperties.Count - 1; i++)
                        {
                            bd.AppendFormat("{0}_", chainProperties[i].PropertyName);
                        }
                        queryCompiler.Builder.AppendFormat(" as {0}{1}_{2},", bd,
                                                           property.Map.ObjectProperty.PropertyName.Replace(".", "_"),
                                                           queryCompiler.FieldCount);
                    }
                    return(true);
                }
            }
            queryCompiler.AddJoins(chainProperties);
            if (property.Map != null)
            {
                AppendFromProperty(queryCompiler, property.Map.GetMapObject(), propertyName, true);
                if (queryCompiler.Builder.Length > 0)
                {
                    queryCompiler.Builder.Remove(queryCompiler.Builder.Length - 1, 1);
                }
                AppendAsSql(queryCompiler, match, null);
                return(true);
            }
            queryCompiler.Builder.Append(queryCompiler.GetFieldName(property, propertyName));
            AppendAsSql(queryCompiler, match, propertyName);
            return(true);
        }