public static DbValue NULLIF(DbFunctionTools tools, DbFunctionArguments args)
        {
            string FunctionName = "NULLIF";

            args.EnsureCount(FunctionName, 2);
            DbType arg0type;
            ByteSlice arg0 = args[0].Eval(out arg0type);

            ImmediateValue argval = null;
            argval = tools.AllocValue(arg0type.ID);
            argval.SetValue(arg0);

            DbFunctionArguments compareargs = new DbFunctionArguments(new DbValue[2]);
            compareargs[0] = argval;
            compareargs[1] = args[1];
            DbValue result = COMPARE(tools, compareargs);
            int iresult = tools.GetInt(result);
            if (iresult == 0)
            {
                List<byte> buf = tools.AllocBuffer(arg0type.Size);
                buf.Add(1);
                for (int i = 0; i < arg0type.Size - 1; i++)                
                {
                    buf.Add(0);
                }
                return tools.AllocValue(ByteSlice.Prepare(buf), arg0type);
            }
            else
            {
                return args[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));
        }
        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);
        }
        public static void DbFunctions_CHARINDEX()
        {
            DbFunctionTools tools = new DbFunctionTools();

            //String,Int32.
            {
                Console.WriteLine("Testing DbFunctions.CHARINDEX(char(n), char(n)...");

                mstring word = mstring.Prepare("apple");
                mstring sentence = mstring.Prepare("Red is apple");
                List<DbValue> args = new List<DbValue>();
                args.Add(tools.AllocValue(word));
                args.Add(tools.AllocValue(sentence));
                DbFunctionArguments fargs = new DbFunctionArguments(args);

                DbValue valOutput = DbFunctions.CHARINDEX(tools, fargs);
                ByteSlice bs = valOutput.Eval();
                int output = tools.GetInt(bs);

                int expected = 7;

                if (expected != output)
                {
                    throw new Exception("DbFunctions.CHARINDEX(char(n), char(n) has failed.  Expected result: " + expected.ToString() + ", but received: " + output.ToString());
                }
                else
                {
                    Console.WriteLine("Expected results received.");
                }
            }

            {
                Console.WriteLine("Testing DbFunctions.CHARINDEX(char(n), char(n), Int32...");

                mstring word = mstring.Prepare("apple");
                mstring sentence = mstring.Prepare("Red is apple, or more apples.");
                int startIndex = 8;
                List<DbValue> args = new List<DbValue>();
                args.Add(tools.AllocValue(word));
                args.Add(tools.AllocValue(sentence));
                args.Add(tools.AllocValue(startIndex));
                DbFunctionArguments fargs = new DbFunctionArguments(args);

                DbValue valOutput = DbFunctions.CHARINDEX(tools, fargs);
                ByteSlice bs = valOutput.Eval();
                int output = tools.GetInt(bs);

                int expected = 22;

                if (expected != output)
                {
                    throw new Exception("DbFunctions.CHARINDEX(char(n), char(n), Int32 has failed.  Expected result: " + expected.ToString() + ", but received: " + output.ToString());
                }
                else
                {
                    Console.WriteLine("Expected results received.");
                }
            }

        }
        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));
        }
        public static void DbFunctions_UPPER()
        {
            DbFunctionTools tools = new DbFunctionTools();

            //String,Int32.
            {
                Console.WriteLine("Testing DbFunctions.UPPER(String)...");

                List<DbValue> args = new List<DbValue>();
                args.Add(tools.AllocValue(mstring.Prepare("hello world")));
                DbFunctionArguments fargs = new DbFunctionArguments(args);

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

                mstring expected = mstring.Prepare("HELLO WORLD");

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

        }
Exemple #7
0
            ByteSlice Conv_IntToDouble(ByteSlice value, int ResultSize, DbFunctionTools tools)
            {
                int     x = tools.GetInt(value);
                DbValue v = tools.AllocValue((double)x);

                return(v.Eval());
            }
        public static DbValue ROUND(DbFunctionTools tools, DbFunctionArguments args)
        {
            string FunctionName = "ROUND";

            args.EnsureCount(FunctionName, 2);

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

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

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

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

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

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

            args.EnsureCount(FunctionName, 3);

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

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

            if (Types.IsNullValue(arg0))
            {
                return args[2];
            }
            else
            {
                return args[1];
            }
        }
Exemple #10
0
        public static void DbFunctions_NVL2()
        {
            DbFunctionTools tools = new DbFunctionTools();

            {
                Console.WriteLine("Testing DbFunctions.NVL2(DateTime)...");

                List<DbValue> args = new List<DbValue>();
                DateTime dt = DateTime.Parse("12/1/2000 10:00:00 AM");
                DateTime ifnull = DateTime.Parse("12/11/2000 10:00:00 AM");
                DateTime notnull = DateTime.Parse("12/14/2000 10:00:00 AM");
                args.Add(tools.AllocValue(dt));
                args.Add(tools.AllocValue(notnull));
                args.Add(tools.AllocValue(ifnull));
                DbFunctionArguments fargs = new DbFunctionArguments(args);

                DbValue valOutput = DbFunctions.NVL2(tools, fargs);
                ByteSlice bs = valOutput.Eval();
                DateTime output = tools.GetDateTime(bs);

                DateTime expected = notnull;

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

            {
                Console.WriteLine("Testing DbFunctions.NVL2(DateTime)...");

                List<DbValue> args = new List<DbValue>();

                byte[] buf = new byte[9];
                buf[0] = 1; //is null
                DateTime ifnull = DateTime.Parse("12/11/2000 10:00:00 AM");
                DateTime notnull = DateTime.Parse("12/14/2000 10:00:00 AM");
                args.Add(tools.AllocValue(ByteSlice.Prepare(buf), DbType.Prepare("DateTime", 9)));
                args.Add(tools.AllocValue(notnull));
                args.Add(tools.AllocValue(ifnull));
                DbFunctionArguments fargs = new DbFunctionArguments(args);
                DbValue valOutput = DbFunctions.NVL2(tools, fargs);
                ByteSlice bs = valOutput.Eval();
                DateTime output = tools.GetDateTime(bs);

                DateTime expected = ifnull;
                if (expected != output)
                {
                    throw new Exception("DbFunctions.NVL2(DateTime) has failed.  Expected result: " + expected.ToString() + ", but received: " + output.ToString());
                }
                else
                {
                    Console.WriteLine("Expected results received.");
                }
            }
        }
        public static void DbFunctions_SYSDATE()
        {
            DbFunctionTools tools = new DbFunctionTools();

            //String,Int32.
            {
                Console.WriteLine("Testing DbFunctions.SYSDATE...");

                DbValue valOutput = DbFunctions.SYSDATE(tools, new DbFunctionArguments());
                ByteSlice bs = valOutput.Eval();
                DateTime output = tools.GetDateTime(bs);

                DateTime expected = DateTime.Now;
                TimeSpan sp = output - expected;

                if (sp.TotalMinutes > 5)
                {
                    throw new Exception("DbFunctions.SYSDATE has failed.  Expected result: " + expected.ToString() + ", but received: " + output.ToString());
                }
                else
                {
                    Console.WriteLine("Expected results received.");
                }
            }

        }
Exemple #12
0
        public static DbValue NVL2(DbFunctionTools tools, DbFunctionArguments args)
        {
            string FunctionName = "NVL2";

            args.EnsureCount(FunctionName, 3);

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

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

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

            args.EnsureCount(FunctionName, 2);
            DbType    arg0type;
            ByteSlice arg0 = args[0].Eval(out arg0type);

            ImmediateValue argval = null;

            argval = tools.AllocValue(arg0type.ID);
            argval.SetValue(arg0);

            DbFunctionArguments compareargs = new DbFunctionArguments(new DbValue[2]);

            compareargs[0] = argval;
            compareargs[1] = args[1];
            DbValue result  = COMPARE(tools, compareargs);
            int     iresult = tools.GetInt(result);

            if (iresult == 0)
            {
                List <byte> buf = tools.AllocBuffer(arg0type.Size);
                buf.Add(1);
                for (int i = 0; i < arg0type.Size - 1; i++)
                {
                    buf.Add(0);
                }
                return(tools.AllocValue(ByteSlice.Prepare(buf), arg0type));
            }
            else
            {
                return(args[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);
        }
Exemple #15
0
            ByteSlice Conv_LongToDouble(ByteSlice value, int ResultSize, DbFunctionTools tools)
            {
                long    x = tools.GetLong(value);
                DbValue v = tools.AllocValue((double)x);

                return(v.Eval());
            }
        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));
        }
Exemple #17
0
        public static void DbFunctions_RAND()
        {
            DbFunctionTools tools = new DbFunctionTools();
            Random rnd = new Random();

       
            {
                Console.WriteLine("Testing DbFunctions.RAND(Int32)...");

                int input = rnd.Next(Int32.MinValue, Int32.MaxValue);

                List<DbValue> args = new List<DbValue>();
                args.Add(tools.AllocValue(input));
                DbFunctionArguments fargs = new DbFunctionArguments(args);

                DbValue valOutput = DbFunctions.RAND(tools, fargs);
                ByteSlice bs = valOutput.Eval();
                double output = tools.GetDouble(bs);

                Console.WriteLine("Expected results received: {0}", output);               
            }

            {
                Console.WriteLine("Testing DbFunctions.RAND()...");

                List<DbValue> args = new List<DbValue>();
                DbFunctionArguments fargs = new DbFunctionArguments(args);

                DbValue valOutput = DbFunctions.RAND(tools, fargs);
                ByteSlice bs = valOutput.Eval();
                double output = tools.GetDouble(bs);

                Console.WriteLine("Expected results received: {0}", output);
            }
        }
        public static DbValue SPACE(DbFunctionTools tools, DbFunctionArguments args)
        {
            string FunctionName = "SPACE";

            args.EnsureCount(FunctionName, 1);

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

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

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

                return tools.AllocValue(s);
            }            
        }
        public static DbValue 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);
        }
        public static void DbFunctions_RIGHT()
        {
            DbFunctionTools tools = new DbFunctionTools();

            //String,Int32.
            {
                Console.WriteLine("Testing DbFunctions.RIGHT(String,Int32)...");

                mstring input = Utils.GenString(100);
                List<DbValue> args = new List<DbValue>();
                args.Add(tools.AllocValue(input));
                args.Add(tools.AllocValue(5));
                DbFunctionArguments fargs = new DbFunctionArguments(args);

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

                string str = input.ToString();
                string expected = str.Substring(str.Length - 5, 5);

                if (expected != output.ToString())
                {
                    throw new Exception("DbFunctions.RIGHT(String,Int32) has failed.  Expected result: " + expected.ToString() + ", but received: " + output.ToString());
                }
                else
                {
                    Console.WriteLine("Expected results received.");
                }
            }

        }
        public static void DbFunctions_PATINDEX()
        {
            DbFunctionTools tools = new DbFunctionTools();

            //String,Int32.
            {
                Console.WriteLine("Testing DbFunctions.PATINDEX(String, string)...");

                List<DbValue> args = new List<DbValue>();
                args.Add(tools.AllocValue(mstring.Prepare("%ap_pl[e]%")));
                args.Add(tools.AllocValue(mstring.Prepare("red is ap5ple, my favourite.")));
                DbFunctionArguments fargs = new DbFunctionArguments(args);

                DbValue valOutput = DbFunctions.PATINDEX(tools, fargs);
                ByteSlice bs = valOutput.Eval();
                int output = tools.GetInt(bs);

                int expected = 7;

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

        }
        public static void DbFunctions_ADD_MONTHS()
        {
            DbFunctionTools tools = new DbFunctionTools();

            //String,Int32.
            {
                Console.WriteLine("Testing DbFunctions.ADD_MONTHS...");

                List<DbValue> args = new List<DbValue>();
                DateTime dt = DateTime.Now;
                int months = 9;
                args.Add(tools.AllocValue(dt));
                args.Add(tools.AllocValue(months));
                DbFunctionArguments fargs = new DbFunctionArguments(args);

                DbValue valOutput = DbFunctions.ADD_MONTHS(tools, fargs);
                ByteSlice bs = valOutput.Eval();
                DateTime output = tools.GetDateTime(bs);

                DateTime expected = dt.AddMonths(months);

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

        }
Exemple #23
0
 ByteSlice Conv_String(ByteSlice value, int ResultSize, DbFunctionTools tools)
 {
     mstring x = tools.GetString(value);
     x = x.ToUpperM();
     DbValue v = tools.AllocValue(x, ResultSize);
     return v.Eval();
 }
        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);
        }
Exemple #25
0
        public static DbValue SUM(DbFunctionTools tools, DbAggregatorArguments args)
        {
            string AggregatorName = "SUM";

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

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

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

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

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

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

                    break;

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

                    break;

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

                    break;
                }
            }

            return(tools.AllocValue(sumi));
        }
Exemple #26
0
            ByteSlice Conv_String(ByteSlice value, int ResultSize, DbFunctionTools tools)
            {
                mstring x = tools.GetString(value);

                x = x.ToUpperM();
                DbValue v = tools.AllocValue(x, ResultSize);

                return(v.Eval());
            }
        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);
            }
        }
Exemple #28
0
        public static DbValue ExecDbScalarFunction(string name, DbFunctionTools tools, DbFunctionArguments args)
        {
            DbValue result = TryExecDbScalarFunction(name, tools, args);

            if (null == result)
            {
                throw new DbExecException("No such scalar function named '" + name + "'");
            }
            return(result);
        }
Exemple #29
0
        public static DbValue ExecDbAggregator(string name, DbFunctionTools tools, DbAggregatorArguments args)
        {
            DbValue result = TryExecDbAggregator(name, tools, args);

            if (null == result)
            {
                throw new DbExecException("No such aggregate function named '" + name + "'");
            }
            return(result);
        }
        public static DbValue SUM(DbFunctionTools tools, DbAggregatorArguments args)
        {
            string AggregatorName = "SUM";

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

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

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

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

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

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

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

            return tools.AllocValue(sumi);
        }
        public static void DbFunctions_INSTR()
        {
            DbFunctionTools tools = new DbFunctionTools();
           
            {
                Console.WriteLine("Testing DbFunctions.SUBSTR(String,String)...");

                List<DbValue> args = new List<DbValue>();
                args.Add(tools.AllocValue(mstring.Prepare("apple is red")));
                args.Add(tools.AllocValue(mstring.Prepare("red")));
                DbFunctionArguments fargs = new DbFunctionArguments(args);

                DbValue valOutput = DbFunctions.INSTR(tools, fargs);
                ByteSlice bs = valOutput.Eval();
                int output = tools.GetInt(bs);

                int expected = 9;

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

            {
                Console.WriteLine("Testing DbFunctions.SUBSTR(String,String, Int32)...");

                List<DbValue> args = new List<DbValue>();
                args.Add(tools.AllocValue(mstring.Prepare("apple is red")));
                args.Add(tools.AllocValue(mstring.Prepare("red")));
                int startindex = 2;
                args.Add(tools.AllocValue(startindex));
                DbFunctionArguments fargs = new DbFunctionArguments(args);

                DbValue valOutput = DbFunctions.INSTR(tools, fargs);
                ByteSlice bs = valOutput.Eval();
                int output = tools.GetInt(bs);

                int expected = 9;

                if (expected != output)
                {
                    throw new Exception("DbFunctions.SUBSTR(String,String, Int32) has failed.  Expected result: " + expected.ToString() + ", but received: " + output.ToString());
                }
                else
                {
                    Console.WriteLine("Expected results received.");
                }
            }

        }
Exemple #32
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));
        }
        public static DbValue ISNULL(DbFunctionTools tools, DbFunctionArguments args)
        {
            string FunctionName = "ISNULL";

            args.EnsureCount(FunctionName, 1);

            DbType arg0type;
            ByteSlice arg0 = args[0].Eval(out arg0type);
            bool bresult = Types.IsNullValue(arg0);
            return tools.AllocValue(bresult ? 1 : 0);
        }
 public static DbValue LESSEREQUAL(DbFunctionTools tools, DbFunctionArguments args)
 {
     DbValue dbvcompare = COMPARE(tools, args);
     DbType typecompare;
     ByteSlice bsresult = dbvcompare.Eval(out typecompare);
     if (DbTypeID.INT != typecompare.ID)
     {
         return tools.AllocValue(bsresult, typecompare);
     }
     int compare = tools.GetInt(bsresult);
     return tools.AllocValue(compare <= 0 ? 1 : 0);
 }
        public static DbValue ISNULL(DbFunctionTools tools, DbFunctionArguments args)
        {
            string FunctionName = "ISNULL";

            args.EnsureCount(FunctionName, 1);

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

            return(tools.AllocValue(bresult ? 1 : 0));
        }
Exemple #36
0
        // Returns null if no such function.
        public static DbValue TryExecDbAggregator(string name, DbFunctionTools tools, DbAggregatorArguments args)
        {
            EnsureDbAggregators();

            if (!DbAggregatorExists(name))
            {
                return(null);
            }

            DbAggregatorDelegate dbagg = dbaggregators[name];

            return(dbagg(tools, args));
        }
Exemple #37
0
        // Returns null if no such function.
        public static DbValue TryExecDbScalarFunction(string name, DbFunctionTools tools, DbFunctionArguments args)
        {
            EnsureDbFunctions();

            if (!DbScalarFunctionExists(name))
            {
                return(null);
            }

            DbScalarFunctionDelegate dbf = dbscalarfuncs[name];

            return(dbf(tools, args));
        }
Exemple #38
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));
        }
        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);
        }
        public static DbValue LESSEREQUAL(DbFunctionTools tools, DbFunctionArguments args)
        {
            DbValue   dbvcompare = COMPARE(tools, args);
            DbType    typecompare;
            ByteSlice bsresult = dbvcompare.Eval(out typecompare);

            if (DbTypeID.INT != typecompare.ID)
            {
                return(tools.AllocValue(bsresult, typecompare));
            }
            int compare = tools.GetInt(bsresult);

            return(tools.AllocValue(compare <= 0 ? 1 : 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());
            }
        }
        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();
            }
        }
        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);
        }
Exemple #44
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());
            }
        }
        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();
            }
        }
Exemple #46
0
        private static double _VAR(string aggregatorName, DbFunctionTools tools, DbAggregatorArguments args, bool sample)
        {
            List <double> values = new List <double>();
            double        x      = 0;
            double        sum    = 0;

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

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

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

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

            if (values.Count > 0)
            {
                double avg = sum / (double)values.Count;
                for (int i = 0; i < values.Count; i++)
                {
                    dev += Math.Pow(values[i] - avg, 2);
                }
                dev = dev / (double)(sample ? values.Count - 1 : values.Count);
            }
            return(dev);
        }
Exemple #47
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));
        }
        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));
        }
Exemple #49
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);
        }
        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;

        }
        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];
        }
        private static double _VAR(string aggregatorName, DbFunctionTools tools, DbAggregatorArguments args, bool sample)
        {
            List<double> values = new List<double>();
            double x = 0;
            double sum = 0;
            for (int iarg = 0; iarg < args.Length; iarg++)
            {
                args[iarg].EnsureCount(aggregatorName, 1);
                DbType arg0type;
                ByteSlice arg0 = args[iarg][0].Eval(out arg0type);
                if (!Types.IsNullValue(arg0))   //ignore null
                {
                    switch (arg0type.ID)
                    {
                        case DbTypeID.INT:
                            x = (double)tools.GetInt(arg0);
                            break;

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

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

                        default:
                            args[iarg].Expected(aggregatorName, 0, "input INT, LONG or DOUBLE, got " + arg0type.Name.ToUpper());
                            return 0; // Doesn't reach here.
                    }
                    values.Add(x);
                    sum += x;
                }
            }
            double dev = 0;
            if (values.Count > 0)
            {
                double avg = sum / (double)values.Count;
                for (int i = 0; i < values.Count; i++)
                {
                    dev += Math.Pow(values[i] - avg, 2);
                }
                dev = dev / (double)(sample ? values.Count - 1 : values.Count);
            }
            return dev;
        }
Exemple #53
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.
            }

        }
Exemple #54
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]);
        }
        public static DbValue COUNT(DbFunctionTools tools, DbAggregatorArguments args)
        {
            string AggregatorName = "COUNT";

            long count = 0;
            for (int iarg = 0; iarg < args.Length; iarg++)
            {
                args[iarg].EnsureCount(AggregatorName, 1);
                DbType arg0type;
                ByteSlice arg0 = args[iarg][0].Eval(out arg0type);
                if (Types.IsNullValue(arg0))    //ignore null
                {
                    continue;
                }
                count++;
            }
            return tools.AllocValue(count);
        }
Exemple #56
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));
            }
        }
Exemple #57
0
        public static DbValue COUNT(DbFunctionTools tools, DbAggregatorArguments args)
        {
            string AggregatorName = "COUNT";

            long count = 0;

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

            double sum   = 0;
            int    count = 0;

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

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

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

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

            if (count > 0)
            {
                avg = sum / (double)count;
            }
            return(tools.AllocValue(avg));
        }
        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));
        }
        public static DbValue ISLIKE(DbFunctionTools tools, DbFunctionArguments args)
        {
            string FunctionName = "ISLIKE";

            args.EnsureCount(FunctionName, 2);

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

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

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

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

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

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

            return(tools.AllocValue(ismatch));
        }