Ejemplo n.º 1
0
        public static DbValue INSTR(DbFunctionTools tools, DbFunctionArguments args)
        {
            string FunctionName = "INSTR";

            args.EnsureMinCount(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());
            }

            int startIndex = 0;
            if (args.Length > 2)
            {
                DbType arg2type;
                ByteSlice arg2 = args[2].Eval(out arg2type);
                if (arg2type.ID != DbTypeID.INT)
                {
                    args.Expected(FunctionName, 0, "input INT, got " + arg2type.Name.ToUpper());
                }
                if (!Types.IsNullValue(arg2))
                {
                    startIndex = tools.GetInt(arg2);
                }
            }
            
            mstring sentence = tools.GetString(arg0);
            mstring word = tools.GetString(arg1);
            int index = -1;
            if (startIndex < sentence.Length)
            {
                if (startIndex > 0)
                {
                    sentence = sentence.SubstringM(startIndex);
                }
                index = sentence.IndexOf(word);
                if (index > -1)
                {
                    index += startIndex;
                }
            }
            return tools.AllocValue(index);
        }
Ejemplo n.º 2
0
        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));
        }
Ejemplo n.º 3
0
        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);
        }
Ejemplo n.º 4
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));
        }
Ejemplo n.º 5
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));
        }
Ejemplo n.º 6
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);
        }
Ejemplo n.º 7
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));
        }
        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);
        }
Ejemplo n.º 9
0
        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);
        }
Ejemplo n.º 10
0
        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));
        }
Ejemplo n.º 11
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);
            }            
        }
Ejemplo n.º 12
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));
        }
Ejemplo n.º 13
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);
        }
Ejemplo n.º 14
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));
        }
Ejemplo n.º 15
0
        public static DbValue SUBSTRING(DbFunctionTools tools, DbFunctionArguments args)
        {
            string FunctionName = "SUBSTRING";

            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());
                return null;
            }

            DbType arg1type;
            ByteSlice arg1 = args[1].Eval(out arg1type);
            if (arg1type.ID != DbTypeID.INT)
            {
                args.Expected(FunctionName, 1, "count INT, got " + arg1type.Name.ToUpper());
                return null;
            }

            DbType arg2type;
            ByteSlice arg2 = args[2].Eval(out arg2type);
            if (arg2type.ID != DbTypeID.INT)
            {
                args.Expected(FunctionName, 1, "count INT, got " + arg2type.Name.ToUpper());
                return null;
            }

            mstring x = tools.GetString(arg0);
            int startIndex = tools.GetInt(arg1);
            if (startIndex < 0)
            {
                startIndex = 0;
            }
            int len = tools.GetInt(arg2);
            if (len < 0)
            {
                throw new ArgumentException(FunctionName + " length cannot be negative");
            }

            if (startIndex + len > x.Length)
            {
                return tools.AllocValue(mstring.Prepare());
            }
            else
            {
                mstring sub = x.SubstringM(startIndex, len);
                return tools.AllocValue(sub);
            }
        }
Ejemplo n.º 16
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);
        }
Ejemplo n.º 17
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));
        }
Ejemplo n.º 18
0
        public static DbValue FIRST(DbFunctionTools tools, DbAggregatorArguments args)
        {
            string AggregatorName = "FIRST";

            if (args.Length > 0)
            {
                args[0].EnsureCount(AggregatorName, 1);
                return(args[0][0]);
            }
            else
            {
                return(tools.AllocNullValue());
            }
        }
Ejemplo n.º 19
0
        public static DbValue FIRST(DbFunctionTools tools, DbAggregatorArguments args)
        {
            string AggregatorName = "FIRST";

            if (args.Length > 0)
            {
                args[0].EnsureCount(AggregatorName, 1);
                return args[0][0];
            }
            else
            {
                return tools.AllocNullValue();
            }
        }
Ejemplo n.º 20
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);
        }
Ejemplo n.º 21
0
        public static DbValue LAST(DbFunctionTools tools, DbAggregatorArguments args)
        {
            string AggregatorName = "LAST";

            if (args.Length > 0)
            {
                int lastindex = args.Length - 1;
                args[lastindex].EnsureCount(AggregatorName, 1);
                return args[lastindex][0];
            }
            else
            {
                return tools.AllocNullValue();
            }
        }
Ejemplo n.º 22
0
        public static DbValue LAST(DbFunctionTools tools, DbAggregatorArguments args)
        {
            string AggregatorName = "LAST";

            if (args.Length > 0)
            {
                int lastindex = args.Length - 1;
                args[lastindex].EnsureCount(AggregatorName, 1);
                return(args[lastindex][0]);
            }
            else
            {
                return(tools.AllocNullValue());
            }
        }
Ejemplo n.º 23
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));
        }
Ejemplo n.º 24
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;

        }
Ejemplo n.º 25
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);
        }
Ejemplo n.º 26
0
        public static DbValue CHOOSERND(DbFunctionTools tools, DbAggregatorArguments args)
        {
            string AggregatorName = "CHOOSERND";

            if (args.Length == 0)
            {
                return tools.AllocNullValue();
            }
            if (anyRnd == -1)
            {
                Random rnd = new Random(System.DateTime.Now.Millisecond / 2 + System.Diagnostics.Process.GetCurrentProcess().Id / 2);
                anyRnd = rnd.Next();
            }
            int index = anyRnd % args.Length;
            args[index].EnsureCount(AggregatorName, 1);
            return args[index][0];
        }
Ejemplo n.º 27
0
        public static DbValue ABS(DbFunctionTools tools, DbFunctionArguments args)
        {
            string FunctionName = "ABS";

            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.
            }

            switch (arg0type.ID)
            {
                case DbTypeID.INT:
                    {
                        int x = tools.GetInt(arg0);
                        x = Math.Abs(x);
                        return tools.AllocValue(x);
                    }
                    break;

                case DbTypeID.LONG:
                    {
                        long x = tools.GetLong(arg0);
                        x = Math.Abs(x);
                        return tools.AllocValue(x);
                    }
                    break;

                case DbTypeID.DOUBLE:
                    {
                        double x = tools.GetDouble(arg0);
                        x = Math.Abs(x);
                        return tools.AllocValue(x);
                    }
                    break;

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

        }
Ejemplo n.º 28
0
        public static DbValue CHOOSERND(DbFunctionTools tools, DbAggregatorArguments args)
        {
            string AggregatorName = "CHOOSERND";

            if (args.Length == 0)
            {
                return(tools.AllocNullValue());
            }
            if (anyRnd == -1)
            {
                Random rnd = new Random(System.DateTime.Now.Millisecond / 2 + System.Diagnostics.Process.GetCurrentProcess().Id / 2);
                anyRnd = rnd.Next();
            }
            int index = anyRnd % args.Length;

            args[index].EnsureCount(AggregatorName, 1);
            return(args[index][0]);
        }
Ejemplo n.º 29
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));
            }
        }
Ejemplo n.º 30
0
        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));
        }
Ejemplo n.º 31
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);
            }

        }
Ejemplo n.º 32
0
        public static DbValue FLOOR(DbFunctionTools tools, DbFunctionArguments args)
        {
            string FunctionName = "FLOOR";

            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.Floor(x);
            return tools.AllocValue(x);
        }
Ejemplo n.º 33
0
        public static DbValue LOWER(DbFunctionTools tools, DbFunctionArguments args)
        {
            string FunctionName = "LOWER";

            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 lower = x.ToLowerM();
            return tools.AllocValue(lower);
        }
Ejemplo n.º 34
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));
        }
Ejemplo n.º 35
0
        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);
        }
Ejemplo n.º 36
0
        public static DbValue LAST_DAY(DbFunctionTools tools, DbFunctionArguments args)
        {
            string FunctionName = "LAST_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.AddMonths(1);
            dt = dt.AddDays(-1);
            return tools.AllocValue(dt);
        }
Ejemplo n.º 37
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));
        }
Ejemplo n.º 38
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)));
        }
Ejemplo n.º 39
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));
        }
Ejemplo n.º 40
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));
        }
Ejemplo n.º 41
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));
            }
        }
Ejemplo n.º 42
0
        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));
        }
Ejemplo n.º 43
0
        public static DbValue DATEPART_YEAR(DbFunctionTools tools, DbFunctionArguments args)
        {
            string FunctionName = "DATEPART_YEAR";

            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.Year);

        }
Ejemplo n.º 44
0
        public static DbValue DATEADD(DbFunctionTools tools, DbFunctionArguments args)
        {
            string FunctionName = "DATEADD";

            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 (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.INT)
            {
                args.Expected(FunctionName, 0, "input INT, got " + arg1type.Name.ToUpper());
            }
            if (Types.IsNullValue(arg2))
            {
                return(tools.AllocNullValue()); // Give a null, take a null.
            }
            if (arg2type.ID != DbTypeID.DATETIME)
            {
                args.Expected(FunctionName, 0, "input DATETIME, got " + arg2type.Name.ToUpper());
            }

            string   datepart = tools.GetString(arg0).ToUpperM().ToString();
            int      num      = tools.GetInt(arg1);
            DateTime dt       = tools.GetDateTime(arg2);
            DateTime newdt    = dt;

            switch (datepart)
            {
            case "YEAR":
            case "YY":
            case "YYYY":
                newdt = dt.AddYears(num);
                break;

            case "QUARTER":
            case "QQ":
            case "Q":
                newdt = dt.AddMonths(num * 3);
                break;

            case "MONTH":
            case "MM":
            case "M":
                newdt = dt.AddMonths(num);
                break;

            case "DAY":
            case "DD":
            case "D":
                newdt = dt.AddDays(num);
                break;

            case "WEEK":
            case "WK":
            case "WW":
                newdt = dt.AddDays(7 * num);
                break;

            case "HOUR":
            case "HH":
                newdt = dt.AddHours(num);
                break;

            case "MINUTE":
            case "MI":
            case "N":
                newdt = dt.AddMinutes(num);
                break;

            case "SECOND":
            case "SS":
            case "S":
                newdt = dt.AddSeconds(num);
                break;

            case "MILLISECOND":
            case "MS":
                newdt = dt.AddMilliseconds(num);
                break;

            default:
                args.Expected(FunctionName, 0, "input datepart invalid");
                return(null);
            }

            return(tools.AllocValue(newdt));
        }
Ejemplo n.º 45
0
        public static DbValue INSTR(DbFunctionTools tools, DbFunctionArguments args)
        {
            string FunctionName = "INSTR";

            args.EnsureMinCount(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());
            }

            int startIndex = 0;

            if (args.Length > 2)
            {
                DbType    arg2type;
                ByteSlice arg2 = args[2].Eval(out arg2type);
                if (arg2type.ID != DbTypeID.INT)
                {
                    args.Expected(FunctionName, 0, "input INT, got " + arg2type.Name.ToUpper());
                }
                if (!Types.IsNullValue(arg2))
                {
                    startIndex = tools.GetInt(arg2);
                }
            }

            mstring sentence = tools.GetString(arg0);
            mstring word     = tools.GetString(arg1);
            int     index    = -1;

            if (startIndex < sentence.Length)
            {
                if (startIndex > 0)
                {
                    sentence = sentence.SubstringM(startIndex);
                }
                index = sentence.IndexOf(word);
                if (index > -1)
                {
                    index += startIndex;
                }
            }
            return(tools.AllocValue(index));
        }
Ejemplo n.º 46
0
        public static DbValue DATEDIFF(DbFunctionTools tools, DbFunctionArguments args)
        {
            string FunctionName = "DATEDIFF";

            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 (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());
            }
            if (Types.IsNullValue(arg2))
            {
                return tools.AllocNullValue(); // Give a null, take a null.
            }
            if (arg2type.ID != DbTypeID.DATETIME)
            {
                args.Expected(FunctionName, 0, "input DATETIME, got " + arg2type.Name.ToUpper());
            }

            string datepart = tools.GetString(arg0).ToUpperM().ToString();
            DateTime startdate = tools.GetDateTime(arg1);            
            DateTime enddate = tools.GetDateTime(arg2);        
            double partdiff = 0;

            switch (datepart)
            {
                case "YEAR":
                case "YY":
                case "YYYY":
                    partdiff = enddate.Year - startdate.Year;
                    break;

                case "QUARTER":
                case "QQ":
                case "Q":                    
                    partdiff = GetMonthDiff(startdate.Year, startdate.Month, enddate.Year, enddate.Month) / 3;                                        
                    break;

                case "MONTH":
                case "MM":
                case "M":
                    partdiff = GetMonthDiff(startdate.Year, startdate.Month, enddate.Year, enddate.Month);
                    break;

                case "DAY":
                case "DD":
                case "D":
                    {
                        DateTime sdate = new DateTime(startdate.Year, startdate.Month, startdate.Day);
                        DateTime edate = new DateTime(enddate.Year, enddate.Month, enddate.Day);
                        TimeSpan sp = edate - sdate;
                        partdiff = sp.TotalDays;
                    }                    
                    break;

                case "WEEK":
                case "WK":
                case "WW":
                    {
                        DateTime sdate = new DateTime(startdate.Year, startdate.Month, startdate.Day);
                        DateTime edate = new DateTime(enddate.Year, enddate.Month, enddate.Day);
                        TimeSpan sp = edate - sdate;
                        partdiff = (int)sp.TotalDays / 7;
                    }     
                    break;

                case "HOUR":
                case "HH":
                    {
                        DateTime sdate = new DateTime(startdate.Year, startdate.Month, startdate.Day, startdate.Hour, 0, 0);
                        DateTime edate = new DateTime(enddate.Year, enddate.Month, enddate.Day, enddate.Hour, 0, 0);
                        TimeSpan sp = edate - sdate;
                        partdiff = sp.TotalHours;
                    }     
                    break;

                case "MINUTE":
                case "MI":
                case "N":
                    {
                        DateTime sdate = new DateTime(startdate.Year, startdate.Month, startdate.Day, startdate.Hour, startdate.Minute, 0);
                        DateTime edate = new DateTime(enddate.Year, enddate.Month, enddate.Day, enddate.Hour, enddate.Minute, 0);
                        TimeSpan sp = edate - sdate;
                        partdiff = sp.TotalMinutes;
                    }     
                    break;

                case "SECOND":
                case "SS":
                case "S":
                    {
                        DateTime sdate = new DateTime(startdate.Year, startdate.Month, startdate.Day, startdate.Hour, startdate.Minute, startdate.Second);
                        DateTime edate = new DateTime(enddate.Year, enddate.Month, enddate.Day, enddate.Hour, enddate.Minute, enddate.Second);
                        TimeSpan sp = edate - sdate;
                        partdiff = sp.TotalSeconds;
                    }  
                    break;

                case "MILLISECOND":
                case "MS":
                    {
                        TimeSpan sp = enddate - startdate;
                        partdiff = sp.TotalMilliseconds;
                    }                    
                    break;

                default:
                    args.Expected(FunctionName, 0, "input datepart invalid");
                    return null;
            }

            return tools.AllocValue(partdiff);
        }
Ejemplo n.º 47
0
        public static DbValue SIGN(DbFunctionTools tools, DbFunctionArguments args)
        {
            string FunctionName = "SIGN";

            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.
            }

            int sign = 0;

            switch (arg0type.ID)
            {
            case DbTypeID.INT:
            {
                int x = tools.GetInt(arg0);
                if (x > 0)
                {
                    sign = 1;
                }
                else if (x < 0)
                {
                    sign = -1;
                }
            }
            break;

            case DbTypeID.LONG:
            {
                long x = tools.GetLong(arg0);
                if (x > 0)
                {
                    sign = 1;
                }
                else if (x < 0)
                {
                    sign = -1;
                }
            }
            break;

            case DbTypeID.DOUBLE:
            {
                double x = tools.GetDouble(arg0);
                if (x > 0)
                {
                    sign = 1;
                }
                else if (x < 0)
                {
                    sign = -1;
                }
            }
            break;

            default:
                args.Expected(FunctionName, 0, "input INT, LONG or DOUBLE, got " + arg0type.Name.ToUpper());
                return(null);    // Doesn't reach here.
            }
            return(tools.AllocValue(sign));
        }
Ejemplo n.º 48
0
        public static DbValue DATEADD(DbFunctionTools tools, DbFunctionArguments args)
        {
            string FunctionName = "DATEADD";

            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 (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.INT)
            {
                args.Expected(FunctionName, 0, "input INT, got " + arg1type.Name.ToUpper());
            }
            if (Types.IsNullValue(arg2))
            {
                return tools.AllocNullValue(); // Give a null, take a null.
            }
            if (arg2type.ID != DbTypeID.DATETIME)
            {
                args.Expected(FunctionName, 0, "input DATETIME, got " + arg2type.Name.ToUpper());
            }

            string datepart = tools.GetString(arg0).ToUpperM().ToString();
            int num = tools.GetInt(arg1);
            DateTime dt = tools.GetDateTime(arg2);
            DateTime newdt = dt;

            switch (datepart)
            {
                case "YEAR":
                case "YY":
                case "YYYY":                    
                    newdt = dt.AddYears(num);
                    break;
                    
                case "QUARTER":
                case "QQ":
                case "Q":
                    newdt = dt.AddMonths(num * 3);
                    break;

                case "MONTH":
                case "MM":
                case "M":
                    newdt = dt.AddMonths(num);
                    break;

                case "DAY":
                case "DD":
                case "D":
                    newdt = dt.AddDays(num);
                    break;

                case "WEEK":
                case "WK":
                case "WW":
                    newdt = dt.AddDays(7 * num);
                    break;

                case "HOUR":
                case "HH":
                    newdt = dt.AddHours(num);
                    break;

                case "MINUTE":
                case "MI":
                case "N":
                    newdt = dt.AddMinutes(num);
                    break;

                case "SECOND":
                case "SS":
                case "S":
                    newdt = dt.AddSeconds(num);
                    break;

                case "MILLISECOND":
                case "MS":
                    newdt = dt.AddMilliseconds(num);
                    break;

                default:
                    args.Expected(FunctionName, 0, "input datepart invalid");
                    return null;
            }

            return tools.AllocValue(newdt);
        }
Ejemplo n.º 49
0
        private static DbValue _PAD(string functionName, DbFunctionTools tools, DbFunctionArguments args, bool isLeft)
        {
            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.INT)
            {
                args.Expected(functionName, 0, "input INT, 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 str = tools.GetString(arg0);
            int totallen = tools.GetInt(arg1);
            mstring padstr = tools.GetString(arg2);

            if (str.Length > totallen)
            {
                str = str.SubstringM(0, totallen);
            }
            else if(str.Length < totallen)
            {
                int delta = totallen - str.Length;
                int padlen = padstr.Length;
                mstring newstr = mstring.Prepare();

                for (int remain = delta; remain > 0; )
                {
                    newstr = newstr.AppendM(padstr);
                    remain -= padlen;
                }

                //if we go over, truncate.
                if (newstr.Length > delta)
                {
                    newstr = newstr.SubstringM(0, delta);
                }

                if (isLeft)
                {
                    str = newstr.AppendM(str);
                }
                else
                {
                    str = str.AppendM(newstr);
                }
            }
            return tools.AllocValue(str);            
        }
Ejemplo n.º 50
0
        public static DbValue ATN2(DbFunctionTools tools, DbFunctionArguments args)
        {
            string FunctionName = "ATN2";

            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) || Types.IsNullValue(arg1))
            {
                return tools.AllocNullValue(); // Give a null, take a null.
            }

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

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

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

                default:
                    args.Expected(FunctionName, 0, "input INT, LONG or DOUBLE, got " + arg0type.Name.ToUpper());
                    return null; // Doesn't reach here.
            }
            switch (arg1type.ID)
            {
                case DbTypeID.INT:
                    {
                        int x = tools.GetInt(arg1);
                        d2 = (double)x;
                    }
                    break;

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

                case DbTypeID.DOUBLE:
                    {
                        d2 = tools.GetDouble(arg1);
                    }
                    break;

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

            d1 = Math.Atan2(d1, d2);
            return tools.AllocValue(d1);
        }
Ejemplo n.º 51
0
        private static DbValue _PAD(string functionName, DbFunctionTools tools, DbFunctionArguments args, bool isLeft)
        {
            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.INT)
            {
                args.Expected(functionName, 0, "input INT, 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 str      = tools.GetString(arg0);
            int     totallen = tools.GetInt(arg1);
            mstring padstr   = tools.GetString(arg2);

            if (str.Length > totallen)
            {
                str = str.SubstringM(0, totallen);
            }
            else if (str.Length < totallen)
            {
                int     delta  = totallen - str.Length;
                int     padlen = padstr.Length;
                mstring newstr = mstring.Prepare();

                for (int remain = delta; remain > 0;)
                {
                    newstr  = newstr.AppendM(padstr);
                    remain -= padlen;
                }

                //if we go over, truncate.
                if (newstr.Length > delta)
                {
                    newstr = newstr.SubstringM(0, delta);
                }

                if (isLeft)
                {
                    str = newstr.AppendM(str);
                }
                else
                {
                    str = str.AppendM(newstr);
                }
            }
            return(tools.AllocValue(str));
        }
Ejemplo n.º 52
0
        public static void DbFunctions_IIF()
        {
            DbFunctionTools tools = new DbFunctionTools();
            Random rnd = new Random();

            // int
            {
                Console.WriteLine("Testing DbFunctions.IIF(1, 'First', 'Second')...");

                List<DbValue> args = new List<DbValue>();
                args.Add(tools.AllocValue(1));
                args.Add(tools.AllocValue(mstring.Prepare("First")));
                args.Add(tools.AllocValue(mstring.Prepare("Second")));
                DbFunctionArguments fargs = new DbFunctionArguments(args);

                DbValue valOutput = DbFunctions.IIF(tools, fargs);
                ByteSlice bs = valOutput.Eval();
                mstring output = tools.GetString(bs);

                mstring expected = mstring.Prepare("First");

                if (expected != output)
                {
                    throw new Exception("DbFunctions.IIF(1, 'First', 'Second') has failed.  Expected result: " + expected.ToString() + ", but received: " + output.ToString());
                }
                else
                {
                    Console.WriteLine("Expected results received.");
                }
            }
            {
                Console.WriteLine("Testing DbFunctions.IIF(0, 'First', 'Second')...");

                List<DbValue> args = new List<DbValue>();
                args.Add(tools.AllocValue(0));
                args.Add(tools.AllocValue(mstring.Prepare("First")));
                args.Add(tools.AllocValue(mstring.Prepare("Second")));
                DbFunctionArguments fargs = new DbFunctionArguments(args);

                DbValue valOutput = DbFunctions.IIF(tools, fargs);
                ByteSlice bs = valOutput.Eval();
                mstring output = tools.GetString(bs);

                mstring expected = mstring.Prepare("Second");

                if (expected != output)
                {
                    throw new Exception("DbFunctions.IIF(0, 'First', 'Second') has failed.  Expected result: " + expected.ToString() + ", but received: " + output.ToString());
                }
                else
                {
                    Console.WriteLine("Expected results received.");
                }
            }
            {
                Console.WriteLine("Testing DbFunctions.IIF(42, 'First', 'Second')...");

                List<DbValue> args = new List<DbValue>();
                args.Add(tools.AllocValue(42));
                args.Add(tools.AllocValue(mstring.Prepare("First")));
                args.Add(tools.AllocValue(mstring.Prepare("Second")));
                DbFunctionArguments fargs = new DbFunctionArguments(args);

                DbValue valOutput = DbFunctions.IIF(tools, fargs);
                ByteSlice bs = valOutput.Eval();
                mstring output = tools.GetString(bs);

                mstring expected = mstring.Prepare("First");

                if (expected != output)
                {
                    throw new Exception("DbFunctions.IIF(42, 'First', 'Second') has failed.  Expected result: " + expected.ToString() + ", but received: " + output.ToString());
                }
                else
                {
                    Console.WriteLine("Expected results received.");
                }
            }

            // NULL
            {
                Console.WriteLine("Testing DbFunctions.IIF(NULL, 'First', 'Second')...");

                List<DbValue> args = new List<DbValue>();
                args.Add(tools.AllocNullValue());
                args.Add(tools.AllocValue(mstring.Prepare("First")));
                args.Add(tools.AllocValue(mstring.Prepare("Second")));
                DbFunctionArguments fargs = new DbFunctionArguments(args);

                DbValue valOutput = DbFunctions.IIF(tools, fargs);
                ByteSlice bs = valOutput.Eval();
                mstring output = tools.GetString(bs);

                mstring expected = mstring.Prepare("Second");

                if (expected != output)
                {
                    throw new Exception("DbFunctions.IIF(NULL, 'First', 'Second') has failed.  Expected result: " + expected.ToString() + ", but received: " + output.ToString());
                }
                else
                {
                    Console.WriteLine("Expected results received.");
                }
            }

        }
Ejemplo n.º 53
0
        public static DbValue DATEDIFF(DbFunctionTools tools, DbFunctionArguments args)
        {
            string FunctionName = "DATEDIFF";

            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 (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());
            }
            if (Types.IsNullValue(arg2))
            {
                return(tools.AllocNullValue()); // Give a null, take a null.
            }
            if (arg2type.ID != DbTypeID.DATETIME)
            {
                args.Expected(FunctionName, 0, "input DATETIME, got " + arg2type.Name.ToUpper());
            }

            string   datepart  = tools.GetString(arg0).ToUpperM().ToString();
            DateTime startdate = tools.GetDateTime(arg1);
            DateTime enddate   = tools.GetDateTime(arg2);
            double   partdiff  = 0;

            switch (datepart)
            {
            case "YEAR":
            case "YY":
            case "YYYY":
                partdiff = enddate.Year - startdate.Year;
                break;

            case "QUARTER":
            case "QQ":
            case "Q":
                partdiff = GetMonthDiff(startdate.Year, startdate.Month, enddate.Year, enddate.Month) / 3;
                break;

            case "MONTH":
            case "MM":
            case "M":
                partdiff = GetMonthDiff(startdate.Year, startdate.Month, enddate.Year, enddate.Month);
                break;

            case "DAY":
            case "DD":
            case "D":
            {
                DateTime sdate = new DateTime(startdate.Year, startdate.Month, startdate.Day);
                DateTime edate = new DateTime(enddate.Year, enddate.Month, enddate.Day);
                TimeSpan sp    = edate - sdate;
                partdiff = sp.TotalDays;
            }
            break;

            case "WEEK":
            case "WK":
            case "WW":
            {
                DateTime sdate = new DateTime(startdate.Year, startdate.Month, startdate.Day);
                DateTime edate = new DateTime(enddate.Year, enddate.Month, enddate.Day);
                TimeSpan sp    = edate - sdate;
                partdiff = (int)sp.TotalDays / 7;
            }
            break;

            case "HOUR":
            case "HH":
            {
                DateTime sdate = new DateTime(startdate.Year, startdate.Month, startdate.Day, startdate.Hour, 0, 0);
                DateTime edate = new DateTime(enddate.Year, enddate.Month, enddate.Day, enddate.Hour, 0, 0);
                TimeSpan sp    = edate - sdate;
                partdiff = sp.TotalHours;
            }
            break;

            case "MINUTE":
            case "MI":
            case "N":
            {
                DateTime sdate = new DateTime(startdate.Year, startdate.Month, startdate.Day, startdate.Hour, startdate.Minute, 0);
                DateTime edate = new DateTime(enddate.Year, enddate.Month, enddate.Day, enddate.Hour, enddate.Minute, 0);
                TimeSpan sp    = edate - sdate;
                partdiff = sp.TotalMinutes;
            }
            break;

            case "SECOND":
            case "SS":
            case "S":
            {
                DateTime sdate = new DateTime(startdate.Year, startdate.Month, startdate.Day, startdate.Hour, startdate.Minute, startdate.Second);
                DateTime edate = new DateTime(enddate.Year, enddate.Month, enddate.Day, enddate.Hour, enddate.Minute, enddate.Second);
                TimeSpan sp    = edate - sdate;
                partdiff = sp.TotalSeconds;
            }
            break;

            case "MILLISECOND":
            case "MS":
            {
                TimeSpan sp = enddate - startdate;
                partdiff = sp.TotalMilliseconds;
            }
            break;

            default:
                args.Expected(FunctionName, 0, "input datepart invalid");
                return(null);
            }

            return(tools.AllocValue(partdiff));
        }
Ejemplo n.º 54
0
        public static DbValue MOD(DbFunctionTools tools, DbFunctionArguments args)
        {
            string FunctionName = "MOD";

            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.
            }
            DbType    arg1type;
            ByteSlice arg1 = args[1].Eval(out arg1type);

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

            int    i0, i1 = 0;
            long   l0, l1 = 0;
            double d0, d1 = 0;

            switch (arg1type.ID)
            {
            case DbTypeID.INT:
                i1 = tools.GetInt(arg1);
                break;

            case DbTypeID.LONG:
                l1 = tools.GetLong(arg1);
                break;

            case DbTypeID.DOUBLE:
                d1 = tools.GetDouble(arg1);
                break;

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

            if (i1 == 0 && l1 == 0 && d1 == 0)
            {
                args.Expected(FunctionName, 0, "Division by zero");
                return(null);
            }

            switch (arg0type.ID)
            {
            case DbTypeID.INT:
            {
                i0 = tools.GetInt(arg0);
                switch (arg1type.ID)
                {
                case DbTypeID.INT:
                {
                    int rem = i0 % i1;
                    return(tools.AllocValue(rem));
                }
                break;

                case DbTypeID.LONG:
                {
                    long rem = (long)i0 % l1;
                    return(tools.AllocValue(rem));
                }
                break;

                case DbTypeID.DOUBLE:
                {
                    double rem = (double)i0 % d1;
                    return(tools.AllocValue(rem));
                }
                break;
                }
            }
            break;

            case DbTypeID.LONG:
            {
                l0 = tools.GetLong(arg0);
                switch (arg1type.ID)
                {
                case DbTypeID.INT:
                {
                    long rem = l0 % (long)i1;
                    return(tools.AllocValue(rem));
                }
                break;

                case DbTypeID.LONG:
                {
                    long rem = l0 % l1;
                    return(tools.AllocValue(rem));
                }
                break;

                case DbTypeID.DOUBLE:
                {
                    double rem = (double)l0 % d1;
                    return(tools.AllocValue(rem));
                }
                break;
                }
            }
            break;

            case DbTypeID.DOUBLE:
            {
                d0 = tools.GetDouble(arg0);
                switch (arg1type.ID)
                {
                case DbTypeID.INT:
                {
                    double rem = d0 % (double)i1;
                    return(tools.AllocValue(rem));
                }
                break;

                case DbTypeID.LONG:
                {
                    double rem = d0 % (double)l1;
                    return(tools.AllocValue(rem));
                }
                break;

                case DbTypeID.DOUBLE:
                {
                    double rem = d0 % d1;
                    return(tools.AllocValue(rem));
                }
                break;
                }
            }

            break;

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

            return(null); // Doesn't reach here.
        }
Ejemplo n.º 55
0
        public static DbValue POWER(DbFunctionTools tools, DbFunctionArguments args)
        {
            string FunctionName = "POWER";

            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.
            }

            DbType    arg1type;
            ByteSlice arg1 = args[1].Eval(out arg1type);
            double    b    = 0;
            double    p    = 0;

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

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

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

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

            switch (arg1type.ID)
            {
            case DbTypeID.INT:
            {
                int x = tools.GetInt(arg1);
                p = (double)x;
            }
            break;

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

            case DbTypeID.DOUBLE:
            {
                p = tools.GetDouble(arg1);
            }
            break;

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

            double r = Math.Pow(b, p);

            return(tools.AllocValue(r));
        }
Ejemplo n.º 56
0
        public static DbValue MOD(DbFunctionTools tools, DbFunctionArguments args)
        {
            string FunctionName = "MOD";

            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.
            }
            DbType arg1type;
            ByteSlice arg1 = args[1].Eval(out arg1type);
            if (Types.IsNullValue(arg1))
            {
                return tools.AllocNullValue(); // Give a null, take a null.
            }

            int i0, i1 = 0;
            long l0, l1 = 0;
            double d0, d1 = 0;

            switch (arg1type.ID)
            {
                case DbTypeID.INT:
                    i1 = tools.GetInt(arg1);
                    break;

                case DbTypeID.LONG:
                    l1 = tools.GetLong(arg1);
                    break;

                case DbTypeID.DOUBLE:
                    d1 = tools.GetDouble(arg1);
                    break;

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

            if (i1 == 0 && l1 == 0 && d1 == 0)
            {
                args.Expected(FunctionName, 0, "Division by zero");
                return null; 
            }

            switch (arg0type.ID)
            {
                case DbTypeID.INT:
                    {
                        i0 = tools.GetInt(arg0);
                        switch (arg1type.ID)
                        {
                            case DbTypeID.INT:
                                {
                                    int rem = i0 % i1;
                                    return tools.AllocValue(rem);
                                }
                                break;

                            case DbTypeID.LONG:
                                {
                                    long rem = (long)i0 % l1;
                                    return tools.AllocValue(rem);
                                }
                                break;

                            case DbTypeID.DOUBLE:
                                {
                                    double rem = (double)i0 % d1;
                                    return tools.AllocValue(rem);
                                }
                                break;
                        }
                    }   
                    break;

                case DbTypeID.LONG:
                    {
                        l0 = tools.GetLong(arg0);
                        switch (arg1type.ID)
                        {
                            case DbTypeID.INT:
                                {
                                    long rem = l0 % (long)i1;
                                    return tools.AllocValue(rem);
                                }
                                break;

                            case DbTypeID.LONG:
                                {
                                    long rem = l0 % l1;
                                    return tools.AllocValue(rem);
                                }
                                break;

                            case DbTypeID.DOUBLE:
                                {
                                    double rem = (double)l0 % d1;
                                    return tools.AllocValue(rem);
                                }
                                break;
                        }
                    }                   
                    break;

                case DbTypeID.DOUBLE:
                    {
                        d0 = tools.GetDouble(arg0);
                        switch (arg1type.ID)
                        {
                            case DbTypeID.INT:
                                {
                                    double rem = d0 % (double)i1;
                                    return tools.AllocValue(rem);
                                }
                                break;

                            case DbTypeID.LONG:
                                {
                                    double rem = d0 % (double)l1;
                                    return tools.AllocValue(rem);
                                }
                                break;

                            case DbTypeID.DOUBLE:
                                {
                                    double rem = d0 % d1;
                                    return tools.AllocValue(rem);
                                }
                                break;
                        }
                    }
                    
                    break;

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

            return null; // Doesn't reach here.
        }
Ejemplo n.º 57
0
        public static DbValue SUBSTRING(DbFunctionTools tools, DbFunctionArguments args)
        {
            string FunctionName = "SUBSTRING";

            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());
                return(null);
            }

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

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

            DbType    arg2type;
            ByteSlice arg2 = args[2].Eval(out arg2type);

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

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

            if (startIndex < 0)
            {
                startIndex = 0;
            }
            int len = tools.GetInt(arg2);

            if (len < 0)
            {
                throw new ArgumentException(FunctionName + " length cannot be negative");
            }

            if (startIndex + len > x.Length)
            {
                return(tools.AllocValue(mstring.Prepare()));
            }
            else
            {
                mstring sub = x.SubstringM(startIndex, len);
                return(tools.AllocValue(sub));
            }
        }