예제 #1
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="format"></param>
        /// <param name="arrayLabel"></param>
        /// <param name="label"></param>
        /// <param name="parameter"></param>
        /// <param name="arrayLevel"></param>
        public static void LoadUsingDelegated <T>(SqlTagFormat format, ArrayLabel arrayLabel, TextLabel label, object parameter, int arrayLevel)
        {
            var para = new KeyValueEasySqlParameter <T>((T)parameter);

            label.FormatArray(format, arrayLabel, para, para.Convert(), arrayLevel);
        }
예제 #2
0
        /// <summary>
        /// 格式化sql语句,是数组使用的
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="format"></param>
        /// <param name="parameter"></param>
        /// <param name="convert"></param>
        /// <param name="arrayLabel"></param>
        public void FormatArray <T>(SqlTagFormat format, ArrayLabel arrayLabel, EasySqlParameter <T> parameter, KeyValueTuple <string, object> convert)
        {
            if (this.SqlText.IsNullOrEmpty())
            {
                return;
            }

            if (this.parameterPositions == null || this.parameterPositionCount == 0)
            {
                format.Write(this.SqlText);
                return;
            }

            var copy = this.Copy();

            for (var i = 0; i < this.SqlText.Length; i++)
            {
                var para = this.MathPosition(copy, i);
                if (para == null)
                {
                    format.Write(this.SqlText[i]);
                    continue;
                }

                var value = convert.Value;
                if (convert.Value is IReferceNullableParameter)
                {
                    value = ((IReferceNullableParameter)convert.Value).Value;
                }

                var item       = value as System.Collections.IEnumerable;
                var ator       = item.GetEnumerator();
                var hadA       = false;
                var arrayLevel = 0;
                if (format.IfTextParameter(para))
                {
                    while (ator.MoveNext())
                    {
                        if (ator.Current == null || ator.Current == DBNull.Value)
                        {
                            continue;
                        }

                        if (hadA)
                        {
                            format.WriteOnTextMode(arrayLabel.Split);
                        }

                        //format.WriteOnTextMode('\'');
                        format.WriteOnTextMode(ator.Current.ToString());
                        //format.WriteOnTextMode('\'');
                        hadA = true;
                    }

                    i += para.PositionLength + 1;
                    if (i < this.SqlText.Length)
                    {
                        format.WriteOnTextMode(this.SqlText[i]);
                    }
                }
                else
                {
                    format.Write(this.SqlText[i]);
                    while (ator.MoveNext())
                    {
                        if (ator.Current == null || ator.Current == DBNull.Value)
                        {
                            continue;
                        }

                        var newvalue = (ator.Current == null || ator.Current == DBNull.Value) ? DBNull.Value : ator.Current;
                        var newkey   = string.Format("{0}x{1}z", para.Name, arrayLevel);

                        if (hadA)
                        {
                            format.Write(arrayLabel.Split);
                            format.Write(para.ActualPrefix);
                        }

                        format.Write(newkey);
                        arrayLevel++;

                        format.AddParameter(newkey, newvalue);
                        hadA = true;
                    }

                    i += para.PositionLength + 1;
                    if (i < this.SqlText.Length)
                    {
                        format.Write(this.SqlText[i]);
                    }
                }
            }
        }
예제 #3
0
        /// <summary>
        /// 格式化sql语句,是数组使用的
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="format"></param>
        /// <param name="parameter"></param>
        /// <param name="convert"></param>
        /// <param name="arrayLevel"></param>
        /// <param name="arrayLabel"></param>
        public void FormatArray <T>(SqlTagFormat format, ArrayLabel arrayLabel, EasySqlParameter <T> parameter, IReadOnlyList <KeyValueTuple <string, object> > convert, int arrayLevel)
        {
            if (this.SqlText.IsNullOrEmpty())
            {
                return;
            }

            if (this.parameterPositions == null || this.parameterPositionCount == 0)
            {
                format.Write(this.SqlText);
                return;
            }

            var copy = this.Copy();
            var sb   = new StringBuilder(this.SqlText);

            for (var i = 0; i < this.SqlText.Length; i++)
            {
                var para = this.MathPosition(copy, i);
                if (para == null)
                {
                    format.Write(this.SqlText[i]);
                    continue;
                }

                if (format.IfTextParameter(para))
                {
                    var item = convert.FirstOrDefault(o => o.Key.Equals(para.Name));
                    if (item == null)
                    {
                        throw new InvalidException("the sql tag {0} need the {1} parameters;", format.Id, para.Name);
                    }

                    var value = item.Value;
                    if (item.Value is IReferceNullableParameter)
                    {
                        value = ((IReferceNullableParameter)item.Value).Value;
                    }

                    if (value == null || value == DBNull.Value)
                    {
                        format.WriteOnTextMode("null");
                        i += para.PositionLength + 1;
                    }
                    else
                    {
                        //format.WriteOnTextMode('\'');
                        format.WriteOnTextMode(value.ToString());
                        //format.WriteOnTextMode('\'');
                        i += para.PositionLength + 1;
                        if (i < this.SqlText.Length)
                        {
                            format.WriteOnTextMode(this.SqlText[i]);
                        }
                    }
                }
                else
                {
                    format.Write(this.SqlText[i]);
                    var item = convert.FirstOrDefault(o => o.Key.Equals(para.Name));
                    if (item == null)
                    {
                        throw new InvalidException("the sql tag {0} need the {1} parameters;", format.Id, para.Name);
                    }

                    var value = item.Value;
                    if (item.Value is IReferceNullableParameter)
                    {
                        value = ((IReferceNullableParameter)item.Value).Value;
                    }

                    var newvalue = (value == null || value == DBNull.Value) ? DBNull.Value : value;
                    var newkey   = string.Format("{0}x{1}z", para.Name, arrayLevel);
                    format.Write(newkey);
                    format.AddParameter(newkey, newvalue);
                    i += para.PositionLength + 1;
                    if (i < this.SqlText.Length)
                    {
                        format.Write(this.SqlText[i]);
                    }
                }
            }
        }
예제 #4
0
파일: SqlTag.cs 프로젝트: stonezhu870/never
        private void Build(XmlNode node, List <ILabel> lines, SequenceStringReader readerHelper, string parameterPrefix)
        {
            if (node.NodeType == XmlNodeType.Text)
            {
                var label = this.formatLine ? this.ReadTextNodeUsingFormatLine(node, new ThunderWriter(node.InnerXml.Length), readerHelper, parameterPrefix) : this.ReadTextNode(node, new ThunderWriter(node.InnerXml.Length), readerHelper, parameterPrefix);
                lines.Add(label);
                return;
            }

            if (node.NodeType == XmlNodeType.Element)
            {
                var parameter = node.Attributes.GetNamedItem("parameter");
                var then      = node.Attributes.GetNamedItem("then");

                switch (node.Name)
                {
                case "ifarray":
                {
                    var open  = node.Attributes.GetNamedItem("open");
                    var close = node.Attributes.GetNamedItem("close");

                    var split = node.Attributes.GetNamedItem("split");
                    var label = new ArrayLabel()
                    {
                        Open      = open == null ? null : open.Value,
                        Close     = close == null ? null : close.Value,
                        Split     = split == null ? null : split.Value,
                        Parameter = parameter == null ? null : parameter.Value,
                        Then      = then == null ? null : (this.formatLine ? string.Concat(then.Value.Trim(), ' ') : then.Value.Trim()),
                        Line      = this.formatLine ? this.ReadTextNodeUsingFormatLine(node, new ThunderWriter(node.InnerText.Length), readerHelper, parameterPrefix) : this.ReadTextNode(node, new ThunderWriter(node.InnerText.Length), readerHelper, parameterPrefix),
                    };

                    lines.Add(label);
                }
                break;

                case "if":
                {
                    var end   = node.Attributes.GetNamedItem("end");
                    var split = node.Attributes.GetNamedItem("split");
                    var label = new IfLabel()
                    {
                        Labels = new List <ILabel>(node.ChildNodes.Count),
                        Then   = then == null ? null : (this.formatLine ? string.Concat(then.Value.Trim(), ' ') : then.Value.Trim()),
                        End    = end == null ? null : end.Value.Trim(),
                        Split  = split == null ? null : split.Value,
                    };

                    if (node.HasChildNodes)
                    {
                        foreach (XmlNode s in node.ChildNodes)
                        {
                            this.Build(s, label.Labels, readerHelper, parameterPrefix);
                        }

                        if (then != null)
                        {
                            this.TextLength += then.Value.Length;
                        }
                    }

                    lines.Add(label);
                }
                break;

                case "ifnotnull":
                {
                    var label = new NotNullLabel()
                    {
                        Then      = then == null ? null : (this.formatLine ? string.Concat(then.Value.Trim(), ' ') : then.Value.Trim()),
                        Parameter = parameter == null ? null : parameter.Value,
                        Labels    = node.HasChildNodes ? new List <ILabel>(node.ChildNodes.Count) : new List <ILabel>(0),
                    };

                    if (node.HasChildNodes)
                    {
                        foreach (XmlNode s in node.ChildNodes)
                        {
                            this.Build(s, label.Labels, readerHelper, parameterPrefix);
                        }

                        if (then != null)
                        {
                            this.TextLength += then.Value.Length;
                        }
                    }

                    lines.Add(label);
                }
                break;

                case "ifnotempty":
                {
                    var label = new NotEmptyLabel()
                    {
                        Then      = then == null ? null : (this.formatLine ? string.Concat(then.Value.Trim(), ' ') : then.Value.Trim()),
                        Parameter = parameter == null ? null : parameter.Value,
                        Labels    = node.HasChildNodes ? new List <ILabel>(node.ChildNodes.Count) : new List <ILabel>(0),
                    };

                    if (node.HasChildNodes)
                    {
                        foreach (XmlNode s in node.ChildNodes)
                        {
                            this.Build(s, label.Labels, readerHelper, parameterPrefix);
                        }

                        if (then != null)
                        {
                            this.TextLength += then.Value.Length;
                        }
                    }

                    lines.Add(label);
                }
                break;

                case "ifnull":
                {
                    var label = new NullLabel()
                    {
                        Then      = then == null ? null : (this.formatLine ? string.Concat(then.Value.Trim(), ' ') : then.Value.Trim()),
                        Parameter = parameter == null ? null : parameter.Value,
                        Labels    = node.HasChildNodes ? new List <ILabel>(node.ChildNodes.Count) : new List <ILabel>(0),
                    };

                    if (node.HasChildNodes)
                    {
                        foreach (XmlNode s in node.ChildNodes)
                        {
                            this.Build(s, label.Labels, readerHelper, parameterPrefix);
                        }

                        if (then != null)
                        {
                            this.TextLength += then.Value.Length;
                        }
                    }

                    lines.Add(label);
                }
                break;

                case "ifempty":
                {
                    var label = new EmptyLabel()
                    {
                        Then      = then == null ? null : (this.formatLine ? string.Concat(then.Value.Trim(), ' ') : then.Value.Trim()),
                        Parameter = parameter == null ? null : parameter.Value,
                        Labels    = node.HasChildNodes ? new List <ILabel>(node.ChildNodes.Count) : new List <ILabel>(0),
                    };

                    if (node.HasChildNodes)
                    {
                        foreach (XmlNode s in node.ChildNodes)
                        {
                            this.Build(s, label.Labels, readerHelper, parameterPrefix);
                        }

                        if (then != null)
                        {
                            this.TextLength += then.Value.Length;
                        }
                    }

                    lines.Add(label);
                }
                break;

                case "ifcontain":
                {
                    var label = new ContainLabel()
                    {
                        Then      = then == null ? null : (this.formatLine ? string.Concat(then.Value.Trim(), ' ') : then.Value.Trim()),
                        Parameter = parameter == null ? null : parameter.Value,
                        Labels    = node.HasChildNodes ? new List <ILabel>(node.ChildNodes.Count) : new List <ILabel>(0),
                    };

                    if (node.HasChildNodes)
                    {
                        foreach (XmlNode s in node.ChildNodes)
                        {
                            this.Build(s, label.Labels, readerHelper, parameterPrefix);
                        }

                        if (then != null)
                        {
                            this.TextLength += then.Value.Length;
                        }
                    }

                    lines.Add(label);
                }
                break;

                case "ifnotexists":
                {
                    var label = new NotExistsLabel()
                    {
                        Then      = then == null ? null : (this.formatLine ? string.Concat(then.Value.Trim(), ' ') : then.Value.Trim()),
                        Parameter = parameter == null ? null : parameter.Value,
                        Labels    = node.HasChildNodes ? new List <ILabel>(node.ChildNodes.Count) : new List <ILabel>(0),
                    };

                    if (node.HasChildNodes)
                    {
                        foreach (XmlNode s in node.ChildNodes)
                        {
                            this.Build(s, label.Labels, readerHelper, parameterPrefix);
                        }

                        if (then != null)
                        {
                            this.TextLength += then.Value.Length;
                        }
                    }

                    lines.Add(label);
                }
                break;

                case "return":
                {
                    var type  = node.Attributes.GetNamedItem("type");
                    var label = new ReturnLabel()
                    {
                        Type = type == null ? null : type.Value,
                        Line = this.formatLine ?
                               this.ReadReturnNodeUsingFormatLine(node, new ThunderWriter(node.InnerXml.Length), readerHelper, parameterPrefix)
                                :
                               this.ReadReturnNode(node, new ThunderWriter(node.InnerXml.Length), readerHelper, parameterPrefix),
                    };

                    if (label.Line.SqlText != null)
                    {
                        this.TextLength += label.Line.SqlText.Length;
                    }

                    lines.Add(label);
                }
                break;
                }
            }
        }