Ejemplo n.º 1
0
        public override object GetValue(DBItem row = null)
        {
            QItem item = items.Count == 0 ? null : items[0];

            switch (type)
            {
            case QFunctionType.lower:
                return(item.GetValue(row)?.ToString().ToLowerInvariant());

            case QFunctionType.upper:
                return(item.GetValue(row)?.ToString().ToUpperInvariant());

            case QFunctionType.initcap:
                return(Helper.ToInitcap(item.GetValue(row)?.ToString(), ' '));

            case QFunctionType.trim:
                return(item.GetValue(row)?.ToString().Trim());

            case QFunctionType.ltrim:
                return(item.GetValue(row)?.ToString().TrimStart());

            case QFunctionType.rtrim:
                return(item.GetValue(row)?.ToString().TrimEnd());

            case QFunctionType.getdate:
                return(DateTime.Now);

            case QFunctionType.concat:
                var sb = new StringBuilder();
                foreach (var parameter in items)
                {
                    sb.Append(parameter.GetValue(row));
                }
                return(sb.ToString());

            case QFunctionType.coalesce:
                foreach (var parameter in items)
                {
                    var value = parameter.GetValue(row);
                    if (value != null)
                    {
                        return(value);
                    }
                }
                return(null);

            case QFunctionType.convert:
            {
                var paramType = items[0] as QType;
                var column    = ((QColumn)items[1]).Column;
                var val       = row[column];

                if (paramType.Type == DBDataType.Date)
                {
                    if (val == DBNull.Value)
                    {
                        return(val);
                    }
                    else if (!(val is DateTime))
                    {
                        val = DateTime.Parse(val.ToString());
                    }
                    return(((DateTime)val).Date);
                }
                else if (paramType.Type == DBDataType.String)
                {
                    string temp = string.Empty;
                    if (val == DBNull.Value)
                    {
                        return(string.Empty);
                    }
                    else if (val is DateTime)
                    {
                        string format = "yyyy-MM-dd HH:mm:ss.fff";
                        if (items.Count == 3)
                        {
                            string f = items[2].Text;
                            if (f == "112")
                            {
                                format = "yyyyMMdd";
                            }
                            else if (f == "12")
                            {
                                format = "yyMMdd";
                            }
                            else if (f == "6")
                            {
                                format = "dd MMM yyyy";
                            }
                        }
                        temp = ((DateTime)val).ToString(format);
                    }
                    else
                    {
                        temp = DBSystem.FormatText(val);
                    }
                    if (paramType.Size > 0 && temp.Length > paramType.Size)
                    {
                        temp = temp.Substring(0, (int)paramType.Size);
                    }
                    return(temp);
                }
                else
                {
                }

                break;
            }

            case QFunctionType.parse:
            {
                var param = items[0].Text.Trim('\'');
                var local = items.Count > 3 ? System.Globalization.CultureInfo.GetCultureInfo(items[4].Text.Trim('\'')) : System.Globalization.CultureInfo.CurrentCulture;
                var t     = items[2] as QType;
                if (t.Type == DBDataType.Date || t.Type == DBDataType.DateTime)
                {
                    return(DateTime.Parse(param, local));
                }
                else if (t.Type == DBDataType.Decimal)
                {
                    return(decimal.Parse(param, local));
                }

                break;
            }

            case QFunctionType.datename:
            {
                var param  = items[0].Text;
                var column = ((QColumn)items[1]).Column;
                var val    = row[column];
                if (!(val is DateTime))
                {
                    val = DateTime.Parse(val.ToString());
                }
                return(((DateTime)val).ToString(param));
            }
            }
            return(null);
        }
Ejemplo n.º 2
0
        public override object GetValue(DBItem row = null)
        {
            QItem item = items.Count == 0 ? null : items[0];

            if (type == QFunctionType.lower)
            {
                return(item.GetValue(row).ToString().ToLowerInvariant());
            }
            if (type == QFunctionType.upper)
            {
                return(item.GetValue(row).ToString().ToUpperInvariant());
            }
            if (type == QFunctionType.getdate)
            {
                return(DateTime.Now);
            }
            if (type == QFunctionType.convert)
            {
                var param  = items[0] as QType;
                var column = ((QColumn)items[1]).Column;
                var val    = row[column];

                if (param.Type == DBDataType.Date)
                {
                    if (val == DBNull.Value)
                    {
                        return(val);
                    }
                    else if (!(val is DateTime))
                    {
                        val = DateTime.Parse(val.ToString());
                    }
                    return(((DateTime)val).Date);
                }
                else if (param.Type == DBDataType.String)
                {
                    string temp = string.Empty;
                    if (val == DBNull.Value)
                    {
                        return(string.Empty);
                    }
                    else if (val is DateTime)
                    {
                        string format = "yyyy-MM-dd HH:mm:ss.fff";
                        if (items.Count == 3)
                        {
                            string f = items[2].Text;
                            if (f == "112")
                            {
                                format = "yyyyMMdd";
                            }
                            else if (f == "12")
                            {
                                format = "yyMMdd";
                            }
                            else if (f == "6")
                            {
                                format = "dd MMM yyyy";
                            }
                        }
                        temp = ((DateTime)val).ToString(format);
                    }
                    else
                    {
                        temp = DBSystem.FormatText(val);
                    }
                    if (param.Size > 0 && temp.Length > param.Size)
                    {
                        temp = temp.Substring(0, (int)param.Size);
                    }
                    return(temp);
                }
                else
                {
                }
            }
            if (type == QFunctionType.parse)
            {
                var param = items[0].Text.Trim('\'');
                var local = items.Count > 3 ? System.Globalization.CultureInfo.GetCultureInfo(items[4].Text.Trim('\'')) : System.Globalization.CultureInfo.CurrentCulture;
                var t     = items[2] as QType;
                if (t.Type == DBDataType.Date || t.Type == DBDataType.DateTime)
                {
                    return(DateTime.Parse(param, local));
                }
                else if (t.Type == DBDataType.Decimal)
                {
                    return(decimal.Parse(param, local));
                }
            }
            if (type == QFunctionType.datename)
            {
                var param  = items[0].Text;
                var column = ((QColumn)items[1]).Column;
                var val    = row[column];
                if (!(val is DateTime))
                {
                    val = DateTime.Parse(val.ToString());
                }
                return(((DateTime)val).ToString(param));
            }
            return(null);
        }