Beispiel #1
0
        public static DbValue CONCAT(DbFunctionTools tools, DbFunctionArguments args)
        {
            string FunctionName = "CONCAT";

            args.EnsureCount(FunctionName, 2);

            DbType    arg0type;
            ByteSlice arg0 = args[0].Eval(out arg0type);

            if (Types.IsNullValue(arg0))
            {
                return(tools.AllocNullValue()); // Give a null, take a null.
            }
            if (arg0type.ID != DbTypeID.CHARS)
            {
                args.Expected(FunctionName, 0, "input CHAR(n), got " + arg0type.Name.ToUpper());
            }
            DbType    arg1type;
            ByteSlice arg1 = args[1].Eval(out arg1type);

            if (Types.IsNullValue(arg1))
            {
                return(tools.AllocNullValue()); // Give a null, take a null.
            }
            if (arg1type.ID != DbTypeID.CHARS)
            {
                args.Expected(FunctionName, 0, "input CHAR(n), got " + arg1type.Name.ToUpper());
            }

            mstring s0 = tools.GetString(arg0);
            mstring s1 = tools.GetString(arg1);

            s0 = s0.AppendM(s1);
            return(tools.AllocValue(s0));
        }
Beispiel #2
0
        public static DbValue ROUND(DbFunctionTools tools, DbFunctionArguments args)
        {
            string FunctionName = "ROUND";

            args.EnsureCount(FunctionName, 2);

            DbType    arg0type;
            ByteSlice arg0 = args[0].Eval(out arg0type);

            if (Types.IsNullValue(arg0))
            {
                return(tools.AllocNullValue()); // Give a null, take a null.
            }
            if (arg0type.ID != DbTypeID.DOUBLE)
            {
                args.Expected(FunctionName, 0, "input DOUBLE, got " + arg0type.Name.ToUpper());
            }

            DbType    arg1type;
            ByteSlice arg1 = args[1].Eval(out arg1type);

            if (arg1type.ID != DbTypeID.INT)
            {
                args.Expected(FunctionName, 1, "digits INT, got " + arg1type.Name.ToUpper());
            }

            int    digits = tools.GetInt(arg1);
            double x      = tools.GetDouble(arg0);

            x = Math.Round(x, digits);
            return(tools.AllocValue(x));
        }
        public static DbValue ADD_MONTHS(DbFunctionTools tools, DbFunctionArguments args)
        {
            string FunctionName = "ADD_MONTHS";

            args.EnsureCount(FunctionName, 2);

            DbType    arg0type;
            ByteSlice arg0 = args[0].Eval(out arg0type);

            if (Types.IsNullValue(arg0))
            {
                return(tools.AllocNullValue()); // Give a null, take a null.
            }
            if (arg0type.ID != DbTypeID.DATETIME)
            {
                args.Expected(FunctionName, 0, "input DATETIME, got " + arg0type.Name.ToUpper());
            }
            DbType    arg1type;
            ByteSlice arg1 = args[1].Eval(out arg1type);

            if (Types.IsNullValue(arg1))
            {
                return(tools.AllocNullValue()); // Give a null, take a null.
            }
            if (arg1type.ID != DbTypeID.INT)
            {
                args.Expected(FunctionName, 0, "input INT, got " + arg1type.Name.ToUpper());
            }

            DateTime dt     = tools.GetDateTime(arg0);
            int      months = tools.GetInt(arg1);

            dt = dt.AddMonths(months);
            return(tools.AllocValue(dt));
        }
Beispiel #4
0
        public static DbValue NVL2(DbFunctionTools tools, DbFunctionArguments args)
        {
            string FunctionName = "NVL2";

            args.EnsureCount(FunctionName, 3);

            DbType    arg0type;
            ByteSlice arg0 = args[0].Eval(out arg0type);
            DbType    arg1type;
            ByteSlice arg1 = args[1].Eval(out arg1type);
            DbType    arg2type;
            ByteSlice arg2 = args[2].Eval(out arg2type);

            if (arg0type.ID != arg1type.ID)
            {
                args.Expected(FunctionName, 0, "input " + arg0type.Name.ToString() + ", got " + arg1type.Name.ToUpper());
            }
            if (arg0type.ID != arg2type.ID)
            {
                args.Expected(FunctionName, 0, "input " + arg0type.Name.ToString() + ", got " + arg2type.Name.ToUpper());
            }

            if (Types.IsNullValue(arg0))
            {
                return(args[2]);
            }
            else
            {
                return(args[1]);
            }
        }
        public static DbValue REVERSE(DbFunctionTools tools, DbFunctionArguments args)
        {
            string FunctionName = "REVERSE";

            args.EnsureCount(FunctionName, 1);

            DbType    arg0type;
            ByteSlice arg0 = args[0].Eval(out arg0type);

            if (Types.IsNullValue(arg0))
            {
                return(tools.AllocNullValue()); // Give a null, take a null.
            }
            if (arg0type.ID != DbTypeID.CHARS)
            {
                args.Expected(FunctionName, 0, "input CHAR(n), got " + arg0type.Name.ToUpper());
            }

            mstring x = tools.GetString(arg0);

            mstring r = mstring.Prepare();

            for (int i = x.Length - 1; i >= 0; i--)
            {
                r = r.AppendM(x.SubstringM(i, 1));
            }

            return(tools.AllocValue(r));
        }
Beispiel #6
0
        public static DbValue SUM(DbFunctionTools tools, DbAggregatorArguments args)
        {
            string AggregatorName = "SUM";

            double sumd     = 0;
            int    sumi     = 0;
            long   suml     = 0;
            DbType arg0type = DbType.PrepareNull();

            for (int iarg = 0; iarg < args.Length; iarg++)
            {
                args[iarg].EnsureCount(AggregatorName, 1);
                ByteSlice arg0 = args[iarg][0].Eval(out arg0type);
                if (!Types.IsNullValue(arg0))   //ignore null
                {
                    switch (arg0type.ID)
                    {
                    case DbTypeID.INT:
                        sumi += tools.GetInt(arg0);
                        break;

                    case DbTypeID.LONG:
                        suml += tools.GetLong(arg0);
                        break;

                    case DbTypeID.DOUBLE:
                        sumd += tools.GetDouble(arg0);
                        break;

                    default:
                        args[iarg].Expected(AggregatorName, 0, "input INT, LONG or DOUBLE, got " + arg0type.Name.ToUpper());
                        return(null);    // Doesn't reach here.
                    }
                }
            }

            if (args.Length > 0)
            {
                switch (arg0type.ID)
                {
                case DbTypeID.INT:
                    return(tools.AllocValue(sumi));

                    break;

                case DbTypeID.LONG:
                    return(tools.AllocValue(suml));

                    break;

                case DbTypeID.DOUBLE:
                    return(tools.AllocValue(sumd));

                    break;
                }
            }

            return(tools.AllocValue(sumi));
        }
Beispiel #7
0
        private static DbValue _BITWISE(string aggregatorName, DbFunctionTools tools, DbAggregatorArguments args, int whatop)
        {
            if (args.Length == 0)
            {
                return(tools.AllocNullValue());
            }

            DbType arg0type = DbType.PrepareNull();

            for (int iarg = 0; iarg < args.Length; iarg++)
            {
                args[iarg].EnsureCount(aggregatorName, 1);
                ByteSlice arg0 = args[iarg][0].Eval(out arg0type);
                if (Types.IsNullValue(arg0))
                {
                    return(tools.AllocNullValue());
                }

                if (bitopbuf == null || bitopbuf.Length != arg0type.Size)
                {
                    bitopbuf = new byte[arg0type.Size];
                }

                if (iarg == 0)
                {
                    for (int ib = 0; ib < arg0.Length; ib++)
                    {
                        bitopbuf[ib] = arg0[ib];
                    }
                    continue;
                }

                for (int ib = 1; ib < arg0.Length; ib++)
                {
                    switch (whatop)
                    {
                    case 1:
                        bitopbuf[ib] = (byte)(bitopbuf[ib] & arg0[ib]);
                        break;

                    case 2:
                        bitopbuf[ib] = (byte)(bitopbuf[ib] | arg0[ib]);
                        break;

                    default:
                        bitopbuf[ib] = (byte)(bitopbuf[ib] ^ arg0[ib]);
                        break;
                    }
                }
            }
            ByteSlice bs = ByteSlice.Prepare(bitopbuf);

            return(tools.AllocValue(bs, arg0type));
        }
Beispiel #8
0
        public mstring GetString(ByteSlice input)
        {
#if DEBUG_FTGET
            if (Types.IsNullValue(input))
            {
                throw new Exception("DEBUG:  GetString: (Types.IsNullValue(input))");
            }
#endif
            mstring x = mstring.PrepareUTF16(ByteSlice.Prepare(input, 1, input.Length - 1));
            x = x.TrimM('\0');
            return(x);
        }
Beispiel #9
0
        public static DbValue ISNULL(DbFunctionTools tools, DbFunctionArguments args)
        {
            string FunctionName = "ISNULL";

            args.EnsureCount(FunctionName, 1);

            DbType    arg0type;
            ByteSlice arg0    = args[0].Eval(out arg0type);
            bool      bresult = Types.IsNullValue(arg0);

            return(tools.AllocValue(bresult ? 1 : 0));
        }
Beispiel #10
0
        public double GetDouble(ByteSlice input)
        {
#if DEBUG_FTGET
            if (Types.IsNullValue(input))
            {
                throw new Exception("DEBUG:  GetDouble: (Types.IsNullValue(input))");
            }
#endif
            recordset rs = recordset.Prepare(ByteSlice.Prepare(input, 1, input.Length - 1));
            double    x  = rs.GetDouble();
            return(x);
        }
Beispiel #11
0
        public static DbValue MONTHS_BETWEEN(DbFunctionTools tools, DbFunctionArguments args)
        {
            string FunctionName = "MONTHS_BETWEEN";

            args.EnsureCount(FunctionName, 2);

            DbType    arg0type;
            ByteSlice arg0 = args[0].Eval(out arg0type);

            if (Types.IsNullValue(arg0))
            {
                return(tools.AllocNullValue()); // Give a null, take a null.
            }
            if (arg0type.ID != DbTypeID.DATETIME)
            {
                args.Expected(FunctionName, 0, "input DATETIME, got " + arg0type.Name.ToUpper());
            }
            DbType    arg1type;
            ByteSlice arg1 = args[1].Eval(out arg1type);

            if (Types.IsNullValue(arg1))
            {
                return(tools.AllocNullValue()); // Give a null, take a null.
            }
            if (arg1type.ID != DbTypeID.DATETIME)
            {
                args.Expected(FunctionName, 0, "input DATETIME, got " + arg1type.Name.ToUpper());
            }

            DateTime dt0          = tools.GetDateTime(arg0);
            DateTime dt1          = tools.GetDateTime(arg1);
            int      daysInMonth0 = DateTime.DaysInMonth(dt0.Year, dt0.Month);
            int      daysInMonth1 = DateTime.DaysInMonth(dt1.Year, dt1.Month);
            double   btw          = 0;

            if (dt0.Year == dt1.Year && dt0.Month == dt1.Month) //same month and same year
            {
                btw = (double)(dt0.Day - dt1.Day) / (double)daysInMonth0;
            }
            else if (dt0.Day == daysInMonth0 && dt1.Day == daysInMonth1) //both fall on the last day of their months
            {
                btw = 12 * (dt0.Year - dt1.Year) + dt0.Month - dt1.Month;
            }
            else
            {
                TimeSpan sp = dt0 - dt1;
                btw = sp.TotalDays / 31d;
            }

            return(tools.AllocValue(btw));
        }
Beispiel #12
0
        public DateTime GetDateTime(ByteSlice input)
        {
#if DEBUG_FTGET
            if (Types.IsNullValue(input))
            {
                throw new Exception("DEBUG:  GetDateTime: (Types.IsNullValue(input))");
            }
#endif
            List <byte> buf = AllocBuffer(input.Length - 1);
            ByteSlice.Prepare(input, 1, input.Length - 1).AppendTo(buf);
            Int64 x = Entry.BytesToLong(buf);
            FreeLastBuffer(buf);
            return(new DateTime(x));
        }
Beispiel #13
0
        public int GetInt(ByteSlice input)
        {
#if DEBUG_FTGET
            if (Types.IsNullValue(input))
            {
                throw new Exception("DEBUG:  GetInt: (Types.IsNullValue(input))");
            }
#endif
            List <byte> buf = AllocBuffer(input.Length - 1);
            ByteSlice.Prepare(input, 1, input.Length - 1).AppendTo(buf);
            Int32 x = Entry.BytesToInt(buf);
            FreeLastBuffer(buf);
            x = (Int32)Entry.ToUInt32(x);
            return(x);
        }
Beispiel #14
0
        private static double _VAR(string aggregatorName, DbFunctionTools tools, DbAggregatorArguments args, bool sample)
        {
            List <double> values = new List <double>();
            double        x      = 0;
            double        sum    = 0;

            for (int iarg = 0; iarg < args.Length; iarg++)
            {
                args[iarg].EnsureCount(aggregatorName, 1);
                DbType    arg0type;
                ByteSlice arg0 = args[iarg][0].Eval(out arg0type);
                if (!Types.IsNullValue(arg0))   //ignore null
                {
                    switch (arg0type.ID)
                    {
                    case DbTypeID.INT:
                        x = (double)tools.GetInt(arg0);
                        break;

                    case DbTypeID.LONG:
                        x = (double)tools.GetLong(arg0);
                        break;

                    case DbTypeID.DOUBLE:
                        x = tools.GetDouble(arg0);
                        break;

                    default:
                        args[iarg].Expected(aggregatorName, 0, "input INT, LONG or DOUBLE, got " + arg0type.Name.ToUpper());
                        return(0);    // Doesn't reach here.
                    }
                    values.Add(x);
                    sum += x;
                }
            }
            double dev = 0;

            if (values.Count > 0)
            {
                double avg = sum / (double)values.Count;
                for (int i = 0; i < values.Count; i++)
                {
                    dev += Math.Pow(values[i] - avg, 2);
                }
                dev = dev / (double)(sample ? values.Count - 1 : values.Count);
            }
            return(dev);
        }
Beispiel #15
0
        public static DbValue DEGREES(DbFunctionTools tools, DbFunctionArguments args)
        {
            string FunctionName = "DEGREES";

            args.EnsureCount(FunctionName, 1);

            DbType    arg0type;
            ByteSlice arg0 = args[0].Eval(out arg0type);

            if (Types.IsNullValue(arg0))
            {
                return(tools.AllocNullValue()); // Give a null, take a null.
            }

            double d = 0;

            switch (arg0type.ID)
            {
            case DbTypeID.INT:
            {
                int x = tools.GetInt(arg0);
                d = (double)x;
            }
            break;

            case DbTypeID.LONG:
            {
                long x = tools.GetLong(arg0);
                d = (double)x;
            }
            break;

            case DbTypeID.DOUBLE:
            {
                d = tools.GetDouble(arg0);
            }
            break;

            default:
                args.Expected(FunctionName, 0, "input INT, LONG or DOUBLE, got " + arg0type.Name.ToUpper());
                return(null);    // Doesn't reach here.
            }

            double r = (d * 180d) / Math.PI;

            return(tools.AllocValue(r));
        }
Beispiel #16
0
        public static DbValue MAX(DbFunctionTools tools, DbAggregatorArguments args)
        {
            string AggregatorName = "MAX";

            DbValue             highest     = null;
            DbValue             nullest     = null;
            DbFunctionArguments compareargs = new DbFunctionArguments(new DbValue[2]);
            ImmediateValue      argval      = null;

            for (int iarg = 0; iarg < args.Length; iarg++)
            {
                args[iarg].EnsureCount(AggregatorName, 1);
                DbType    arg0type;
                ByteSlice arg0 = args[iarg][0].Eval(out arg0type);
                if (Types.IsNullValue(arg0))
                {
                    if (null == nullest)
                    {
                        nullest = tools.AllocValue(arg0, arg0type);
                    }
                }
                else
                {
                    if (null == argval)
                    {
                        argval = tools.AllocValue(arg0type.ID);
                    }
                    argval.SetValue(arg0);
                    compareargs[0] = argval;
                    compareargs[1] = highest;
                    if (null == highest || tools.GetInt(DbFunctions.COMPARE(tools, compareargs)) > 0)
                    {
                        highest = argval; // Keep this one.
                        argval  = null;   // New one next time.
                    }
                }
            }
            if (null == highest)
            {
                if (null == nullest)
                {
                    return(tools.AllocNullValue());
                }
                return(nullest);
            }
            return(highest);
        }
Beispiel #17
0
        public static DbValue REPLACE(DbFunctionTools tools, DbFunctionArguments args)
        {
            string FunctionName = "REPLACE";

            args.EnsureCount(FunctionName, 3);

            DbType    arg0type;
            ByteSlice arg0 = args[0].Eval(out arg0type);

            if (Types.IsNullValue(arg0))
            {
                return(tools.AllocNullValue()); // Give a null, take a null.
            }
            if (arg0type.ID != DbTypeID.CHARS)
            {
                args.Expected(FunctionName, 0, "input CHAR(n), got " + arg0type.Name.ToUpper());
            }
            DbType    arg1type;
            ByteSlice arg1 = args[1].Eval(out arg1type);

            if (Types.IsNullValue(arg1))
            {
                return(tools.AllocNullValue()); // Give a null, take a null.
            }
            if (arg1type.ID != DbTypeID.CHARS)
            {
                args.Expected(FunctionName, 0, "input CHAR(n), got " + arg1type.Name.ToUpper());
            }
            DbType    arg2type;
            ByteSlice arg2 = args[2].Eval(out arg2type);

            if (Types.IsNullValue(arg2))
            {
                return(tools.AllocNullValue()); // Give a null, take a null.
            }
            if (arg2type.ID != DbTypeID.CHARS)
            {
                args.Expected(FunctionName, 0, "input CHAR(n), got " + arg2type.Name.ToUpper());
            }

            mstring sentence    = tools.GetString(arg0);
            mstring word        = tools.GetString(arg1);
            mstring replacement = tools.GetString(arg2);

            sentence = sentence.ReplaceM(ref word, ref replacement);
            return(tools.AllocValue(sentence));
        }
Beispiel #18
0
        public static DbValue LEFT(DbFunctionTools tools, DbFunctionArguments args)
        {
            string FunctionName = "LEFT";

            args.EnsureCount(FunctionName, 2);

            DbType    arg0type;
            ByteSlice arg0 = args[0].Eval(out arg0type);

            if (Types.IsNullValue(arg0))
            {
                return(tools.AllocNullValue()); // Give a null, take a null.
            }
            if (arg0type.ID != DbTypeID.CHARS)
            {
                args.Expected(FunctionName, 0, "input CHAR(n), got " + arg0type.Name.ToUpper());
            }

            DbType    arg1type;
            ByteSlice arg1 = args[1].Eval(out arg1type);

            if (arg1type.ID != DbTypeID.INT)
            {
                args.Expected(FunctionName, 1, "count INT, got " + arg1type.Name.ToUpper());
            }

            mstring x         = tools.GetString(arg0);
            int     LeftCount = tools.GetInt(arg1);

            if (LeftCount >= x.Length)
            {
                // User requested the whole string.
                return(tools.AllocValue(arg0, arg0type));
            }
            else if (LeftCount < 0)
            {
                throw new ArgumentException(FunctionName + " count cannot be negative");
            }
            else
            {
                x = x.SubstringM(0, LeftCount);
                return(tools.AllocValue(x));
            }
        }
Beispiel #19
0
        public static DbValue AVG(DbFunctionTools tools, DbAggregatorArguments args)
        {
            string AggregatorName = "AVG";

            double sum   = 0;
            int    count = 0;

            for (int iarg = 0; iarg < args.Length; iarg++)
            {
                args[iarg].EnsureCount(AggregatorName, 1);
                DbType    arg0type;
                ByteSlice arg0 = args[iarg][0].Eval(out arg0type);
                if (!Types.IsNullValue(arg0))   //ignore null
                {
                    count++;
                    switch (arg0type.ID)
                    {
                    case DbTypeID.INT:
                        sum += (double)tools.GetInt(arg0);
                        break;

                    case DbTypeID.LONG:
                        sum += (double)tools.GetLong(arg0);
                        break;

                    case DbTypeID.DOUBLE:
                        sum += tools.GetDouble(arg0);
                        break;

                    default:
                        args[iarg].Expected(AggregatorName, 0, "input INT, LONG or DOUBLE, got " + arg0type.Name.ToUpper());
                        return(null);    // Doesn't reach here.
                    }
                }
            }
            double avg = 0;

            if (count > 0)
            {
                avg = sum / (double)count;
            }
            return(tools.AllocValue(avg));
        }
Beispiel #20
0
        public static DbValue COUNT(DbFunctionTools tools, DbAggregatorArguments args)
        {
            string AggregatorName = "COUNT";

            long count = 0;

            for (int iarg = 0; iarg < args.Length; iarg++)
            {
                args[iarg].EnsureCount(AggregatorName, 1);
                DbType    arg0type;
                ByteSlice arg0 = args[iarg][0].Eval(out arg0type);
                if (Types.IsNullValue(arg0))    //ignore null
                {
                    continue;
                }
                count++;
            }
            return(tools.AllocValue(count));
        }
        public static DbValue PATINDEX(DbFunctionTools tools, DbFunctionArguments args)
        {
            string FunctionName = "PATINDEX";

            args.EnsureCount(FunctionName, 2);

            DbType    arg0type;
            ByteSlice arg0 = args[0].Eval(out arg0type);
            DbType    arg1type;
            ByteSlice arg1 = args[1].Eval(out arg1type);

            if (Types.IsNullValue(arg0))
            {
                return(tools.AllocNullValue()); // Give a null, take a null.
            }
            if (arg0type.ID != DbTypeID.CHARS)
            {
                args.Expected(FunctionName, 0, "input CHAR(n), got " + arg0type.Name.ToUpper());
            }
            if (arg1type.ID != DbTypeID.CHARS)
            {
                args.Expected(FunctionName, 0, "input CHAR(n), got " + arg1type.Name.ToUpper());
            }

            string pat = tools.GetString(arg0).ToString();
            string str = tools.GetString(arg1).ToString();

            pat = pat.Trim('%');

            string rpat = GetRegexString(pat);

            System.Text.RegularExpressions.Regex regx  = new System.Text.RegularExpressions.Regex(rpat);
            System.Text.RegularExpressions.Match match = regx.Match(str);
            int indx = -1;

            if (match != null)
            {
                indx = match.Index;
            }

            return(tools.AllocValue(indx));
        }
Beispiel #22
0
        public static DbValue ISLIKE(DbFunctionTools tools, DbFunctionArguments args)
        {
            string FunctionName = "ISLIKE";

            args.EnsureCount(FunctionName, 2);

            int       ismatch = 0;
            DbType    arg0type;
            ByteSlice arg0 = args[0].Eval(out arg0type);

            if (Types.IsNullValue(arg0))
            {
                return(tools.AllocValue(ismatch));
            }
            if (arg0type.ID != DbTypeID.CHARS)
            {
                args.Expected(FunctionName, 0, "input CHAR(n), got " + arg0type.Name.ToUpper());
            }

            DbType    arg1type;
            ByteSlice arg1 = args[1].Eval(out arg1type);

            if (arg1type.ID != DbTypeID.CHARS)
            {
                args.Expected(FunctionName, 1, "pattern CHAR(n), got " + arg1type.Name.ToUpper());
            }

            string x       = tools.GetString(arg0).ToString();
            string y       = tools.GetString(arg1).ToString();
            string pattern = GetRegexString(y);

            if (pattern != null)
            {
                System.Text.RegularExpressions.Regex regx = new System.Text.RegularExpressions.Regex(pattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                if (regx.IsMatch(x))
                {
                    ismatch = 1;
                }
            }

            return(tools.AllocValue(ismatch));
        }
Beispiel #23
0
        public static DbValue FORMAT(DbFunctionTools tools, DbFunctionArguments args)
        {
            string FunctionName = "FORMAT";

            args.EnsureCount(FunctionName, 2);

            DbType    arg0type;
            ByteSlice arg0 = args[0].Eval(out arg0type);
            DbType    arg1type;
            ByteSlice arg1 = args[1].Eval(out arg1type);

            if (Types.IsNullValue(arg0))
            {
                return(tools.AllocNullValue()); // Give a null, take a null.
            }
            if (arg0type.ID != DbTypeID.CHARS)
            {
                args.Expected(FunctionName, 0, "input CHAR(n), got " + arg0type.Name.ToUpper());
            }
            if (Types.IsNullValue(arg1))
            {
                return(tools.AllocNullValue()); // Give a null, take a null.
            }
            if (arg1type.ID != DbTypeID.DATETIME)
            {
                args.Expected(FunctionName, 0, "input DATETIME, got " + arg1type.Name.ToUpper());
            }

            string   formatstr = tools.GetString(arg0).ToString();
            DateTime dt        = tools.GetDateTime(arg1);

            mstring result = mstring.Prepare(dt.ToString(formatstr));

            while (result.Length < 80)
            {
                result.MAppend('\0');
            }
            return(tools.AllocValue(result));
        }
Beispiel #24
0
        public static DbValue COUNTDISTINCT(DbFunctionTools tools, DbAggregatorArguments args)
        {
            string AggregatorName = "COUNTDISTINCT";

            Dictionary <ByteSlice, short> dict = new Dictionary <ByteSlice, short>(new ByteSliceEqualityComparer());

            for (int iarg = 0; iarg < args.Length; iarg++)
            {
                args[iarg].EnsureCount(AggregatorName, 1);
                DbType    arg0type;
                ByteSlice arg0 = args[iarg][0].Eval(out arg0type);
                if (Types.IsNullValue(arg0))    //ignore null
                {
                    continue;
                }

                if (!dict.ContainsKey(arg0))
                {
                    dict.Add(arg0, 0);
                }
            }
            return(tools.AllocValue((long)dict.Count));
        }
Beispiel #25
0
        public static DbValue CEILING(DbFunctionTools tools, DbFunctionArguments args)
        {
            string FunctionName = "CEILING";

            args.EnsureCount(FunctionName, 1);

            DbType    arg0type;
            ByteSlice arg0 = args[0].Eval(out arg0type);

            if (Types.IsNullValue(arg0))
            {
                return(tools.AllocNullValue()); // Give a null, take a null.
            }
            if (arg0type.ID != DbTypeID.DOUBLE)
            {
                args.Expected(FunctionName, 0, "input DOUBLE, got " + arg0type.Name.ToUpper());
            }

            double x = tools.GetDouble(arg0);

            x = Math.Ceiling(x);
            return(tools.AllocValue(x));
        }
Beispiel #26
0
        public static DbValue LEN(DbFunctionTools tools, DbFunctionArguments args)
        {
            string FunctionName = "LEN";

            args.EnsureCount(FunctionName, 1);

            DbType    arg0type;
            ByteSlice arg0 = args[0].Eval(out arg0type);

            if (Types.IsNullValue(arg0))
            {
                return(tools.AllocNullValue()); // Give a null, take a null.
            }
            if (arg0type.ID != DbTypeID.CHARS)
            {
                args.Expected(FunctionName, 0, "input CHAR(n), got " + arg0type.Name.ToUpper());
            }

            mstring x   = tools.GetString(arg0);
            int     len = x.Length;

            return(tools.AllocValue(len));
        }
Beispiel #27
0
        public static DbValue NEXT_DAY(DbFunctionTools tools, DbFunctionArguments args)
        {
            string FunctionName = "NEXT_DAY";

            args.EnsureCount(FunctionName, 1);

            DbType    arg0type;
            ByteSlice arg0 = args[0].Eval(out arg0type);

            if (Types.IsNullValue(arg0))
            {
                return(tools.AllocNullValue()); // Give a null, take a null.
            }
            if (arg0type.ID != DbTypeID.DATETIME)
            {
                args.Expected(FunctionName, 0, "input DATETIME, got " + arg0type.Name.ToUpper());
            }

            DateTime dt = tools.GetDateTime(arg0);

            dt = dt.AddDays(1);
            return(tools.AllocValue(dt));
        }
Beispiel #28
0
        public static DbValue LTRIM(DbFunctionTools tools, DbFunctionArguments args)
        {
            string FunctionName = "LTRIM";

            args.EnsureCount(FunctionName, 1);

            DbType    arg0type;
            ByteSlice arg0 = args[0].Eval(out arg0type);

            if (Types.IsNullValue(arg0))
            {
                return(tools.AllocNullValue()); // Give a null, take a null.
            }
            if (arg0type.ID != DbTypeID.CHARS)
            {
                args.Expected(FunctionName, 0, "input CHAR(n), got " + arg0type.Name.ToUpper());
            }

            string x = tools.GetString(arg0).ToString();
            string y = x.TrimStart();

            return(tools.AllocValue(mstring.Prepare(y)));
        }
Beispiel #29
0
        public static DbValue SPACE(DbFunctionTools tools, DbFunctionArguments args)
        {
            string FunctionName = "SPACE";

            args.EnsureCount(FunctionName, 1);

            DbType    arg0type;
            ByteSlice arg0 = args[0].Eval(out arg0type);

            if (Types.IsNullValue(arg0))
            {
                return(tools.AllocNullValue()); // Give a null, take a null.
            }
            if (arg0type.ID != DbTypeID.INT)
            {
                args.Expected(FunctionName, 0, "input INT, got " + arg0type.Name.ToUpper());
                return(null);
            }

            int len = tools.GetInt(arg0);

            if (len < 1)
            {
                return(tools.AllocValue(mstring.Prepare("")));
            }
            else
            {
                mstring s = mstring.Prepare();

                for (int i = 0; i < len; i++)
                {
                    s = s.AppendM(" ");
                }

                return(tools.AllocValue(s));
            }
        }
        public static DbValue DATEPART_HOUR(DbFunctionTools tools, DbFunctionArguments args)
        {
            string FunctionName = "DATEPART_HOUR";

            args.EnsureCount(FunctionName, 1);

            DbType    arg0type;
            ByteSlice arg0 = args[0].Eval(out arg0type);

            if (Types.IsNullValue(arg0))
            {
                return(tools.AllocNullValue()); // Give a null, take a null.
            }

            if (arg0type.ID != DbTypeID.DATETIME)
            {
                args.Expected(FunctionName, 0, "input DateTime, got " + arg0type.Name.ToUpper());
                return(null); // Doesn't reach here.
            }

            DateTime dt = tools.GetDateTime(arg0);

            return(tools.AllocValue(dt.Hour));
        }