Example #1
0
        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.");
                }
            }

        }
Example #2
0
        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 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_SUBSTRING()
        {
            DbFunctionTools tools = new DbFunctionTools();

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

                List<DbValue> args = new List<DbValue>();
                args.Add(tools.AllocValue(mstring.Prepare("HELLO WORLD")));
                int si = 6;
                int len = 5;
                args.Add(tools.AllocValue(si));
                args.Add(tools.AllocValue(len));
                DbFunctionArguments fargs = new DbFunctionArguments(args);

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

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

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

        }
Example #5
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);
            }            
        }
Example #6
0
        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 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.");
                }
            }

        }
Example #8
0
        public static void DbFunctions_CONCAT()
        {
            DbFunctionTools tools = new DbFunctionTools();

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

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

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

                mstring expected = mstring.Prepare("hello world");

                if (expected != output)
                {
                    throw new Exception("DbFunctions.CONCAT(String,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.");
                }
            }

        }
Example #10
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));
        }
        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);
            }
        }
Example #12
0
        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.");
                }
            }

        }
Example #13
0
        public static void DbFunctions_NVL()
        {
            DbFunctionTools tools = new DbFunctionTools();

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

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

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

                DateTime expected = DateTime.Parse("12/1/2000 10:00:00 AM");

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

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

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

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

                DateTime expected = DateTime.Parse("1/1/2009 10:00:00 AM");

                if (expected != output)
                {
                    throw new Exception("DbFunctions.NVL(DateTime) has failed.  Expected result: " + expected.ToString() + ", but received: " + output.ToString());
                }
                else
                {
                    Console.WriteLine("Expected results received.");
                }
            }
        }
Example #14
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);
        }
 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 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));
        }
Example #17
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 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));
        }
Example #19
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);
        }
Example #20
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();
 }
Example #21
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);
            }
        }
Example #22
0
            ByteSlice Conv_LongToDouble(ByteSlice value, int ResultSize, DbFunctionTools tools)
            {
                long    x = tools.GetLong(value);
                DbValue v = tools.AllocValue((double)x);

                return(v.Eval());
            }
Example #23
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));
        }
Example #24
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 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);
        }
Example #26
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));
        }
Example #27
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));
        }
Example #28
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);
        }
Example #29
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;

        }
Example #30
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);
        }
Example #31
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.
            }
        }
Example #32
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.
            }

        }
Example #33
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());
            }
Example #34
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));
            }
        }
Example #35
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);
            }

        }
Example #36
0
        public static DbValue ISLIKE(DbFunctionTools tools, DbFunctionArguments args)
        {
            string FunctionName = "ISLIKE";

            args.EnsureCount(FunctionName, 2);

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

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

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

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

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

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

            return(tools.AllocValue(ismatch));
        }
Example #37
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);
        }
Example #38
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));
        }
Example #39
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));
        }
Example #40
0
        public static DbValue ISLIKE(DbFunctionTools tools, DbFunctionArguments args)
        {
            string FunctionName = "ISLIKE";

            args.EnsureCount(FunctionName, 2);

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

            DbType arg1type;
            ByteSlice arg1 = args[1].Eval(out arg1type);
            if (arg1type.ID != DbTypeID.CHARS)
            {
                args.Expected(FunctionName, 1, "pattern CHAR(n), got " + arg1type.Name.ToUpper());
            }

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

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

            return tools.AllocValue(ismatch);
        }
Example #41
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);
        }
Example #43
0
        public static DbValue SPACE(DbFunctionTools tools, DbFunctionArguments args)
        {
            string FunctionName = "SPACE";

            args.EnsureCount(FunctionName, 1);

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

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

            int len = tools.GetInt(arg0);

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

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

                return(tools.AllocValue(s));
            }
        }
        public static DbValue 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);
        }
Example #45
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));
        }
Example #46
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);
        }
Example #47
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));
        }
Example #48
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));
        }
Example #49
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);
        }
Example #50
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));
        }
Example #51
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);   
        }
Example #52
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));
        }
Example #53
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));
        }
Example #54
0
        public static DbValue COUNTDISTINCT(DbFunctionTools tools, DbAggregatorArguments args)
        {
            string AggregatorName = "COUNTDISTINCT";

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

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

                if (!dict.ContainsKey(arg0))
                {
                    dict.Add(arg0, 0);
                }
            }
            return(tools.AllocValue((long)dict.Count));
        }
Example #55
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));
        }
Example #56
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));
        }
Example #57
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));
        }
Example #58
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)));
        }
        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));
        }
Example #60
0
        public static DbValue RAND(DbFunctionTools tools, DbFunctionArguments args)
        {
            string FunctionName = "RAND";

            if (args.Length == 1)
            {
                DbType    arg0type;
                ByteSlice arg0 = args[0].Eval(out arg0type);
                if (Types.IsNullValue(arg0))
                {
                    if (null == _rnd)
                    {
                        _rnd = new Random();
                    }
                }
                else
                {
                    if (arg0type.ID != DbTypeID.INT)
                    {
                        args.Expected(FunctionName, 0, "input INT, got " + arg0type.Name.ToUpper());
                    }

                    int seed = tools.GetInt(arg0);
                    _rnd = new Random(seed);
                }
            }
            else
            {
                if (null == _rnd)
                {
                    _rnd = new Random();
                }
            }

            return(tools.AllocValue(_rnd.NextDouble()));
        }