Exemplo n.º 1
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.");
                }
            }

        }
Exemplo n.º 2
0
        public static DbValue SUBSTRING(DbFunctionTools tools, DbFunctionArguments args)
        {
            string FunctionName = "SUBSTRING";

            args.EnsureCount(FunctionName, 3);

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

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

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

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

            if (startIndex + len > x.Length)
            {
                return tools.AllocValue(mstring.Prepare());
            }
            else
            {
                mstring sub = x.SubstringM(startIndex, len);
                return tools.AllocValue(sub);
            }
        }
Exemplo n.º 3
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.");
                }
            }

        }
Exemplo n.º 4
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);
        }
Exemplo n.º 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);
            }            
        }
Exemplo n.º 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];
            }
        }
Exemplo n.º 7
0
        public static DbValue ADD_MONTHS(DbFunctionTools tools, DbFunctionArguments args)
        {
            string FunctionName = "ADD_MONTHS";

            args.EnsureCount(FunctionName, 2);

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

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

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

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

            dt = dt.AddMonths(months);
            return(tools.AllocValue(dt));
        }
Exemplo n.º 8
0
            ByteSlice Conv_IntToDouble(ByteSlice value, int ResultSize, DbFunctionTools tools)
            {
                int     x = tools.GetInt(value);
                DbValue v = tools.AllocValue((double)x);

                return(v.Eval());
            }
Exemplo n.º 9
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.");
                }
            }

        }
Exemplo n.º 10
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]);
            }
        }
Exemplo n.º 11
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);
        }
Exemplo n.º 12
0
        public static DbValue ROUND(DbFunctionTools tools, DbFunctionArguments args)
        {
            string FunctionName = "ROUND";

            args.EnsureCount(FunctionName, 2);

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

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

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

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

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

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

            args.EnsureCount(FunctionName, 2);

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

            DateTime dt = tools.GetDateTime(arg0);
            int months = tools.GetInt(arg1);
            dt = dt.AddMonths(months);            
            return tools.AllocValue(dt);
        }
Exemplo n.º 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));
        }
Exemplo n.º 15
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);
        }
Exemplo n.º 16
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);
 }
Exemplo n.º 17
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));
        }
Exemplo n.º 18
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);
        }
Exemplo n.º 19
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);
        }
Exemplo n.º 20
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));
        }
Exemplo n.º 21
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;

        }
Exemplo n.º 22
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;
        }
Exemplo n.º 23
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.
            }

        }
Exemplo n.º 24
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));
            }
        }
Exemplo n.º 25
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));
        }
Exemplo n.º 26
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);
            }

        }
Exemplo n.º 27
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);   
        }
Exemplo n.º 28
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));
            }
        }
Exemplo n.º 29
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());
        }
Exemplo n.º 30
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()));
        }
Exemplo n.º 31
0
        public static void DbFunctions_ABS()
        {
            DbFunctionTools tools = new DbFunctionTools();
            Random rnd = new Random();
      
            //Int32.
            {
                Console.WriteLine("Testing DbFunctions.ABS(Int32)...");

                int input = rnd.Next(Int32.MinValue, Int32.MaxValue);
                DbValue valInput = tools.AllocValue(input);
                List<DbValue> args = new List<DbValue>();
                args.Add(valInput);
                DbFunctionArguments fargs = new DbFunctionArguments(args);

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

                int expected = Math.Abs(input);

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

            //Int64.
            {
                Console.WriteLine("Testing DbFunctions.ABS(Int64)...");

                long input = DateTime.Now.Ticks - 1;
                if (input % 2 == 0)
                {
                    input = input * -1L;
                }

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

                DbValue valOutput = DbFunctions.ABS(tools, fargs);
                ByteSlice bs = valOutput.Eval();
                byte[] buf = bs.ToBytes();
                long output = Entry.ToInt64(Entry.BytesToULong(buf, 1));

                long expected = Math.Abs(input);

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

            //Double.
            {
                Console.WriteLine("Testing DbFunctions.ABS(Double)...");

                double input = rnd.NextDouble();

                if (input < 0.5)
                {
                    input = input * -1d;
                }
               
                List<DbValue> args = new List<DbValue>();
                args.Add(tools.AllocValue(input));
                DbFunctionArguments fargs = new DbFunctionArguments(args);

                DbValue valOutput = DbFunctions.ABS(tools, fargs);
                ByteSlice bs = valOutput.Eval();
                byte[] buf = bs.ToBytes();
                double output = Entry.BytesToDouble(buf, 1);

                double expected = Math.Abs(input);

                if (expected != output)
                {
                    throw new Exception("DbFunctions.ABS(Double) has failed.  Expected result: " + expected.ToString() + ", but received: " + output.ToString());
                }
                else
                {
                    Console.WriteLine("Expected results received.");
                }
            }
        }
Exemplo n.º 32
0
        public static DbValue SUB(DbFunctionTools tools, DbFunctionArguments args)
        {
            string FunctionName = "SUB";

            args.EnsureCount(FunctionName, 2);

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

            if (Types.IsNullValue(arg0))
            {
                return(tools.AllocValue(arg0, arg0type)); // Give a null, take a null.
            }
            DbType    arg1type;
            ByteSlice arg1 = args[1].Eval(out arg1type);

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

            switch (arg0type.ID)
            {
            case DbTypeID.INT:
            {
                int x = tools.GetInt(arg0);
                switch (arg1type.ID)
                {
                case DbTypeID.INT:
                {
                    int y = tools.GetInt(arg1);
                    return(tools.AllocValue(x - y));
                }
                break;

                case DbTypeID.LONG:
                {
                    long y = tools.GetLong(arg1);
                    return(tools.AllocValue(x - y));
                }
                break;

                case DbTypeID.DOUBLE:
                {
                    double y = tools.GetDouble(arg1);
                    return(tools.AllocValue(x - y));
                }
                break;

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

            case DbTypeID.LONG:
            {
                long x = tools.GetLong(arg0);
                switch (arg1type.ID)
                {
                case DbTypeID.INT:
                {
                    int y = tools.GetInt(arg1);
                    return(tools.AllocValue(x - y));
                }
                break;

                case DbTypeID.LONG:
                {
                    long y = tools.GetLong(arg1);
                    return(tools.AllocValue(x - y));
                }
                break;

                case DbTypeID.DOUBLE:
                {
                    double y = tools.GetDouble(arg1);
                    return(tools.AllocValue(x - y));
                }
                break;

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

            case DbTypeID.DOUBLE:
            {
                double x = tools.GetDouble(arg0);
                switch (arg1type.ID)
                {
                case DbTypeID.INT:
                {
                    int y = tools.GetInt(arg1);
                    return(tools.AllocValue(x - y));
                }
                break;

                case DbTypeID.LONG:
                {
                    long y = tools.GetLong(arg1);
                    return(tools.AllocValue(x - y));
                }
                break;

                case DbTypeID.DOUBLE:
                {
                    double y = tools.GetDouble(arg1);
                    return(tools.AllocValue(x - y));
                }
                break;

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

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

            args.EnsureCount(FunctionName, 2);

            DbType arg0type;
            ByteSlice arg0 = args[0].Eval(out arg0type);           
            if (Types.IsNullValue(arg0))
            {
                return tools.AllocNullValue(); // Give a null, take a null.
            }
            DbType arg1type;
            ByteSlice arg1 = args[1].Eval(out arg1type);
            if (Types.IsNullValue(arg1))
            {
                return tools.AllocNullValue(); // Give a null, take a null.
            }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            return null; // Doesn't reach here.
        }
Exemplo n.º 34
0
        public static DbValue CAST(DbFunctionTools tools, DbFunctionArguments args)
        {
#if DEBUG
            //System.Diagnostics.Debugger.Launch();
#endif

            string FunctionName = "CAST";

            args.EnsureCount(FunctionName, 2);

            DbType    type;
            ByteSlice bs = args[0].Eval(out type);

            mstring xas = tools.GetString(args[1]);
            if (!xas.StartsWith("AS "))
            {
#if DEBUG
                throw new Exception("Expected AS <type> in CAST, not: " + xas);
#endif
                throw new Exception("Expected AS <type> in CAST");
            }
            mstring msastype = xas.SubstringM(3);
            string  sastype  = msastype.ToString();  // Alloc.
            sastype = DbType.NormalizeName(sastype); // Alloc if char(n).
            DbType astype = DbType.Prepare(sastype); // Alloc if char(n).
            if (DbTypeID.NULL == astype.ID)
            {
                throw new Exception("Unknown AS type in CAST: " + sastype);
            }

            switch (astype.ID)
            {
            case DbTypeID.INT:
                switch (type.ID)
                {
                case DbTypeID.INT:         // as INT
                    if (astype.Size > type.Size)
                    {
                        throw new Exception("CAST: source value buffer too small");
                    }
                    return(tools.AllocValue(ByteSlice.Prepare(bs, 0, astype.Size), astype));

                case DbTypeID.LONG:         // as INT
                    return(tools.AllocValue((int)tools.GetLong(bs)));

                case DbTypeID.DOUBLE:         // as INT
                    return(tools.AllocValue((int)tools.GetDouble(bs)));

                case DbTypeID.DATETIME:         // as INT
                    return(tools.AllocValue((int)tools.GetDateTime(bs).Ticks));

                case DbTypeID.CHARS:         // as INT
                {
                    int to = tools.GetString(bs).NextItemToInt32(' ');
                    return(tools.AllocValue(to));
                }

                default:
                    throw new Exception("Cannot handle CAST value of type " + type.Name + " AS " + astype.Name);
                }
                break;

            case DbTypeID.LONG:
                switch (type.ID)
                {
                case DbTypeID.INT:         // as LONG
                    return(tools.AllocValue((long)tools.GetInt(bs)));

                case DbTypeID.LONG:         // as LONG
                    if (astype.Size > type.Size)
                    {
                        throw new Exception("CAST: source value buffer too small");
                    }
                    return(tools.AllocValue(ByteSlice.Prepare(bs, 0, astype.Size), astype));

                case DbTypeID.DOUBLE:         // as LONG
                    return(tools.AllocValue((long)tools.GetDouble(bs)));

                case DbTypeID.DATETIME:         // as LONG
                    return(tools.AllocValue((long)tools.GetDateTime(bs).Ticks));

                case DbTypeID.CHARS:         // as LONG
                {
                    long to = tools.GetString(bs).NextItemToInt64(' ');
                    return(tools.AllocValue(to));
                }

                default:
                    throw new Exception("Cannot handle CAST value of type " + type.Name + " AS " + astype.Name);
                }
                break;

            case DbTypeID.DOUBLE:
                switch (type.ID)
                {
                case DbTypeID.INT:         // as DOUBLE
                    return(tools.AllocValue((double)tools.GetInt(bs)));

                case DbTypeID.LONG:         // as DOUBLE
                    return(tools.AllocValue((double)tools.GetLong(bs)));

                case DbTypeID.DOUBLE:         // as DOUBLE
                    if (astype.Size > type.Size)
                    {
                        throw new Exception("CAST: source value buffer too small");
                    }
                    return(tools.AllocValue(ByteSlice.Prepare(bs, 0, astype.Size), astype));

                case DbTypeID.DATETIME:         // as DOUBLE
                    return(tools.AllocValue((double)tools.GetDateTime(bs).Ticks));

                case DbTypeID.CHARS:         // as DOUBLE
                {
                    double to = tools.GetString(bs).NextItemToDouble(' ');
                    return(tools.AllocValue(to));
                }

                default:
                    throw new Exception("Cannot handle CAST value of type " + type.Name + " AS " + astype.Name);
                }
                break;

            case DbTypeID.DATETIME:
                switch (type.ID)
                {
                case DbTypeID.INT:         // as DATETIME
                    return(tools.AllocValue(new DateTime((long)tools.GetInt(bs))));

                case DbTypeID.LONG:         // as DATETIME
                    return(tools.AllocValue(new DateTime((long)tools.GetLong(bs))));

                case DbTypeID.DOUBLE:         // as DATETIME
                    return(tools.AllocValue(new DateTime((long)tools.GetDouble(bs))));

                case DbTypeID.DATETIME:         // as DATETIME
                    if (astype.Size > type.Size)
                    {
                        throw new Exception("CAST: source value buffer too small");
                    }
                    return(tools.AllocValue(ByteSlice.Prepare(bs, 0, astype.Size), astype));

                case DbTypeID.CHARS:         // as DATETIME
                {
                    mstring to = tools.GetString(bs);
                    return(tools.AllocValue(DateTime.Parse(to.ToString())));
                }

                default:
                    throw new Exception("Cannot handle CAST value of type " + type.Name + " AS " + astype.Name);
                }
                break;

            case DbTypeID.CHARS:
                switch (type.ID)
                {
                case DbTypeID.INT:         // as CHAR(n)
                {
                    mstring ms = mstring.Prepare(tools.GetInt(bs));
                    return(tools.AllocValue(ms, astype.Size));
                }

                case DbTypeID.LONG:         // as CHAR(n)
                {
                    mstring ms = mstring.Prepare(tools.GetLong(bs));
                    return(tools.AllocValue(ms, astype.Size));
                }

                case DbTypeID.DOUBLE:         // as CHAR(n)
                {
                    mstring ms = mstring.Prepare(tools.GetDouble(bs));
                    return(tools.AllocValue(ms, astype.Size));
                }

                case DbTypeID.DATETIME:         // as CHAR(n)
                {
                    mstring ms = mstring.Prepare(tools.GetDateTime(bs).ToString());
                    return(tools.AllocValue(ms, astype.Size));
                }

                case DbTypeID.CHARS:         // as CHAR(n)
                    if (astype.Size > type.Size)
                    {
                        //throw new Exception("CAST: source value buffer too small");
                        return(tools.AllocValue(tools.GetString(bs), astype.Size));
                    }
                    return(tools.AllocValue(ByteSlice.Prepare(bs, 0, astype.Size), astype));

                default:
                    throw new Exception("Cannot handle CAST value of type " + type.Name + " AS " + astype.Name);
                }
                break;

            default:
                throw new Exception("Cannot handle CAST value of type " + type.Name + " AS " + astype.Name);
            }
        }
Exemplo n.º 35
0
        public static DbValue ATN2(DbFunctionTools tools, DbFunctionArguments args)
        {
            string FunctionName = "ATN2";

            args.EnsureCount(FunctionName, 2);

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

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

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

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

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

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

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

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

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

            d1 = Math.Atan2(d1, d2);
            return tools.AllocValue(d1);
        }
Exemplo n.º 36
0
        public static void DbAggregators_BIT_OR()
        {
            DbFunctionTools tools = new DbFunctionTools();
            Random rnd = new Random();
            const int rowcount = 20;

            //Double.
            {
                Console.WriteLine("Testing DbAggregators_BIT_OR(Double)...");

                DbFunctionArguments[] fargs = new DbFunctionArguments[rowcount];
                double[] values = new double[rowcount];

                for (int i = 0; i < fargs.Length; i++)
                {
                    double input = rnd.NextDouble();
                    if (input < 0.5)
                    {
                        input = input * -1d;
                    }
                    values[i] = input;
                    List<DbValue> args = new List<DbValue>();
                    args.Add(tools.AllocValue(input));
                    fargs[i] = new DbFunctionArguments(args);
                }
                DbValue valOutput = DbAggregators.BIT_OR(tools, new DbAggregatorArguments(fargs));
                ByteSlice bs = valOutput.Eval();
                double output = tools.GetDouble(bs);
                byte[] expected = bitop(values, 2);

                if (!compareBytes(expected, bs))
                {
                    throw new Exception("DbAggregators_BIT_OR(Double) has failed.");
                }
                else
                {
                    Console.WriteLine("Expected results received.");
                }
            }

            {
                Console.WriteLine("Testing DbAggregators_BIT_OR(Int32)...");

                DbFunctionArguments[] fargs = new DbFunctionArguments[rowcount];
                int[] values = new int[rowcount];

                for (int i = 0; i < fargs.Length; i++)
                {
                    int input = rnd.Next(Int32.MinValue, Int32.MaxValue);
                    values[i] = input;
                    List<DbValue> args = new List<DbValue>();
                    args.Add(tools.AllocValue(input));
                    fargs[i] = new DbFunctionArguments(args);
                }
                DbValue valOutput = DbAggregators.BIT_OR(tools, new DbAggregatorArguments(fargs));
                ByteSlice bs = valOutput.Eval();
                int output = tools.GetInt(bs);
                byte[] expected = bitop(values, 2);
                if (!compareBytes(expected, bs))
                {
                    throw new Exception("DbAggregators_BIT_OR(Int32) has failed.");
                }
                else
                {
                    Console.WriteLine("Expected results received.");
                }
            }

            {
                Console.WriteLine("Testing DbAggregators_BIT_OR(Int64)...");

                DbFunctionArguments[] fargs = new DbFunctionArguments[rowcount];
                long[] values = new long[rowcount];

                for (int i = 0; i < fargs.Length; i++)
                {
                    long input = DateTime.Now.Ticks;
                    if (input % 2 == 0)
                    {
                        input = input * -1;
                    }
                    values[i] = input;
                    List<DbValue> args = new List<DbValue>();
                    args.Add(tools.AllocValue(input));
                    fargs[i] = new DbFunctionArguments(args);
                }
                DbValue valOutput = DbAggregators.BIT_OR(tools, new DbAggregatorArguments(fargs));
                ByteSlice bs = valOutput.Eval();
                long output = tools.GetLong(bs);
                byte[] expected = bitop(values, 2);
                if (!compareBytes(expected, bs))
                {
                    throw new Exception("DbAggregators_BIT_OR(Int64) has failed.");
                }
                else
                {
                    Console.WriteLine("Expected results received.");
                }
            }

            {
                Console.WriteLine("Testing DbAggregators_BIT_OR(char(n))...");

                DbFunctionArguments[] fargs = new DbFunctionArguments[rowcount];
                mstring[] values = new mstring[rowcount];

                for (int i = 0; i < fargs.Length; i++)
                {
                    int strlen = 30;
                    mstring input = Utils.GenString(strlen);
                    values[i] = input;
                    List<DbValue> args = new List<DbValue>();
                    args.Add(tools.AllocValue(input));
                    fargs[i] = new DbFunctionArguments(args);
                }
                DbValue valOutput = DbAggregators.BIT_OR(tools, new DbAggregatorArguments(fargs));
                ByteSlice bs = valOutput.Eval();
                mstring output = tools.GetString(bs);
                byte[] expected = bitop(values, 2);
                if (!compareBytes(expected, bs))
                {
                    throw new Exception("DbAggregators_BIT_OR(char(n)) has failed.");
                }
                else
                {
                    Console.WriteLine("Expected results received.");
                }
            }
        }
Exemplo n.º 37
0
        public static DbValue MOD(DbFunctionTools tools, DbFunctionArguments args)
        {
            string FunctionName = "MOD";

            args.EnsureCount(FunctionName, 2);

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

            if (Types.IsNullValue(arg0))
            {
                return(tools.AllocNullValue()); // Give a null, take a null.
            }
            DbType    arg1type;
            ByteSlice arg1 = args[1].Eval(out arg1type);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            break;

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

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

            args.EnsureCount(FunctionName, 3);

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

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

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

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

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

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

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

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

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

            if (startIndex + len > x.Length)
            {
                return(tools.AllocValue(mstring.Prepare()));
            }
            else
            {
                mstring sub = x.SubstringM(startIndex, len);
                return(tools.AllocValue(sub));
            }
        }
Exemplo n.º 39
0
        public static DbValue DATEADD(DbFunctionTools tools, DbFunctionArguments args)
        {
            string FunctionName = "DATEADD";

            args.EnsureCount(FunctionName, 3);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            args.EnsureMinCount(FunctionName, 2);

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

            if (Types.IsNullValue(arg0) || Types.IsNullValue(arg1))
            {
                return(tools.AllocValue(index));
            }

            if (arg0type.ID != DbTypeID.CHARS)
            {
                args.Expected(FunctionName, 0, "input CHAR(n), got " + arg0type.Name.ToUpper());
                return(null);
            }
            if (arg1type.ID != DbTypeID.CHARS)
            {
                args.Expected(FunctionName, 0, "input CHAR(n), got " + arg1type.Name.ToUpper());
                return(null);
            }

            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());
                    return(null);
                }
                startIndex = tools.GetInt(arg2);
                if (startIndex < 0)
                {
                    startIndex = 0;
                }
            }

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

            if (startIndex > sentence.Length - 1)
            {
                index = -1;
            }
            else if (startIndex == 0)
            {
                index = sentence.IndexOf(word);
            }
            else
            {
                mstring partsentence = sentence.SubstringM(startIndex);
                int     ix           = partsentence.IndexOf(word);
                if (ix == -1)
                {
                    index = -1;
                }
                else
                {
                    index = startIndex + ix;
                }
            }
            return(tools.AllocValue(index));
        }
Exemplo n.º 41
0
        private static DbValue _PAD(string functionName, DbFunctionTools tools, DbFunctionArguments args, bool isLeft)
        {
            args.EnsureCount(functionName, 3);

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

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

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

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

            mstring str      = tools.GetString(arg0);
            int     totallen = tools.GetInt(arg1);
            mstring padstr   = tools.GetString(arg2);

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

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

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

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

            const int NUM_RUNS_EACH = 4;

            for (int it = 0; it < NUM_RUNS_EACH; it++)
            {
                Console.WriteLine("Testing DbFunctions.COMPARE(char(n), char(n)) ({0} of {1})...", it + 1, NUM_RUNS_EACH);

                mstring x = Utils.GenString(rnd.Next(1, 200));
                mstring y = Utils.GenString(rnd.Next(1, 200));
                if (1 == it)
                {
                    y = x;
                }

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

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

                int expected = string.Compare(x.ToString(), y.ToString(), StringComparison.OrdinalIgnoreCase);

                if (!_SameCompareRank(expected, output))
                {
                    if (0 == expected || 0 == output)
                    {
                        throw new Exception("DbFunctions.COMPARE(char(n), char(n)) has failed.  Expected result: " + _CompareRankToString(expected) + ", but received: " + _CompareRankToString(output));
                    }
                    Console.WriteLine("Warning: C# StringComparison.OrdinalIgnoreCase and DbFunctions.COMPARE do not agree on string ordering: C# says " + _CompareRankToString(expected) + " but I say " + _CompareRankToString(output) + " for:\r\n\t'{0}'\r\n\t'{1}'", x.ToString(), y.ToString());
                    System.Threading.Thread.Sleep(1000 * 3);
                }
                else
                {
                    Console.WriteLine("Expected results received: {0}", _CompareRankToString(expected));
                }
            }

            for (int it = 0; it < NUM_RUNS_EACH; it++)
            {
                Console.WriteLine("Testing DbFunctions.COMPARE(Int32, Int32) ({0} of {1})...", it + 1, NUM_RUNS_EACH);

                int x = rnd.Next(Int32.MinValue, Int32.MaxValue);
                int y = rnd.Next(Int32.MinValue, Int32.MaxValue);
                if (1 == it)
                {
                    y = x;
                }

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

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

                int expected = x.CompareTo(y);

                if (!_SameCompareRank(expected, output))
                {
                    throw new Exception("DbFunctions.COMPARE(Int32, Int32) has failed.  Expected result: " + _CompareRankToString(expected) + ", but received: " + _CompareRankToString(output));
                }
                else
                {
                    Console.WriteLine("Expected results received: {0}", _CompareRankToString(expected));
                }
            }

            for (int it = 0; it < NUM_RUNS_EACH; it++)
            {
                Console.WriteLine("Testing DbFunctions.COMPARE(Double, Double) ({0} of {1})...", it + 1, NUM_RUNS_EACH);

                double x = rnd.NextDouble();
                double y = rnd.NextDouble();
                if (1 == it)
                {
                    y = x;
                }

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

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

                int expected = x.CompareTo(y);

                if (!_SameCompareRank(expected, output))
                {
                    throw new Exception("DbFunctions.COMPARE(Double, Double) has failed.  Expected result: " + _CompareRankToString(expected) + ", but received: " + _CompareRankToString(output));
                }
                else
                {
                    Console.WriteLine("Expected results received: {0}", _CompareRankToString(expected));
                }
            }

            for (int it = 0; it < NUM_RUNS_EACH; it++)
            {
                Console.WriteLine("Testing DbFunctions.COMPARE(Long, Long) ({0} of {1})...", it + 1, NUM_RUNS_EACH);

                long x = unchecked((long)rnd.Next() * (long)rnd.Next());
                long y = unchecked((long)rnd.Next() * (long)rnd.Next());
                if (1 == it)
                {
                    y = x;
                }

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

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

                int expected = x.CompareTo(y);

                if (!_SameCompareRank(expected, output))
                {
                    throw new Exception("DbFunctions.COMPARE(Long, Long) has failed.  Expected result: " + _CompareRankToString(expected) + ", but received: " + _CompareRankToString(output));
                }
                else
                {
                    Console.WriteLine("Expected results received: {0}", _CompareRankToString(expected));
                }
            }

            for (int it = 0; it < NUM_RUNS_EACH; it++)
            {
                Console.WriteLine("Testing DbFunctions.COMPARE(Double, Int32) ({0} of {1})...", it + 1, NUM_RUNS_EACH);

                double x = rnd.NextDouble();
                int y = rnd.Next();
                if (1 == it)
                {
                    //y = x;
                    x = y;
                }

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

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

                int expected = x.CompareTo((double)y);

                if (!_SameCompareRank(expected, output))
                {
                    throw new Exception("DbFunctions.COMPARE(Double, Int32) has failed.  Expected result: " + _CompareRankToString(expected) + ", but received: " + _CompareRankToString(output));
                }
                else
                {
                    Console.WriteLine("Expected results received: {0}", _CompareRankToString(expected));
                }
            }

            for (int it = 0; it < NUM_RUNS_EACH; it++)
            {
                Console.WriteLine("Testing DbFunctions.COMPARE(Double, Long) ({0} of {1})...", it + 1, NUM_RUNS_EACH);

                double x = rnd.NextDouble();
                long y = unchecked((long)rnd.Next() * (long)rnd.Next());
                if (1 == it)
                {
                    //y = x;
                    x = y;
                }

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

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

                int expected = x.CompareTo((double)y);

                if (!_SameCompareRank(expected, output))
                {
                    throw new Exception("DbFunctions.COMPARE(Double, Long) has failed.  Expected result: " + _CompareRankToString(expected) + ", but received: " + _CompareRankToString(output));
                }
                else
                {
                    Console.WriteLine("Expected results received: {0}", _CompareRankToString(expected));
                }
            }

            for (int it = 0; it < NUM_RUNS_EACH; it++)
            {
                Console.WriteLine("Testing DbFunctions.COMPARE(DateTime, DateTime) ({0} of {1})...", it + 1, NUM_RUNS_EACH);

                DateTime x = DateTime.Now;
                DateTime y = x.AddDays(rnd.Next(-365, 365));
                if (1 == it)
                {
                    y = x;
                }

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

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

                int expected = x.CompareTo(y);

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

            for (int it = 0; it < NUM_RUNS_EACH; it++)
            {
                Console.WriteLine("Testing DbFunctions.COMPARE(DateTime, Char(n)) ({0} of {1})...", it + 1, NUM_RUNS_EACH);

                DateTime x = DateTime.Now;
                DateTime dt = x.AddDays(rnd.Next(-365, 365));
                mstring y = mstring.Prepare(dt.ToString());

                if (1 == it)
                {
                    y = mstring.Prepare(x.ToString());
                }

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

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

                int expected = x.CompareTo(DateTime.Parse(y.ToString()));

                if (!_SameCompareRank(expected, output))
                {
                    throw new Exception("DbFunctions.COMPARE(DateTime, Char(n)) has failed.  Expected result: " + _CompareRankToString(expected) + ", but received: " + _CompareRankToString(output));
                }
                else
                {
                    Console.WriteLine("Expected results received: {0}", _CompareRankToString(expected));
                }
            }

            for (int it = 0; it < NUM_RUNS_EACH; it++)
            {
                Console.WriteLine("Testing DbFunctions.COMPARE(Char(n), DateTime) ({0} of {1})...", it + 1, NUM_RUNS_EACH);

                DateTime y = DateTime.Now;
                DateTime dt = y.AddDays(rnd.Next(-365, 365));
                mstring x = mstring.Prepare(dt.ToString());

                if (1 == it)
                {
                    x = mstring.Prepare(y.ToString());
                }

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

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

                int expected = DateTime.Parse(x.ToString()).CompareTo(y);

                if (!_SameCompareRank(expected, output))
                {
                    throw new Exception("DbFunctions.COMPARE(Char(n), DateTime) has failed.  Expected result: " + _CompareRankToString(expected) + ", but received: " + _CompareRankToString(output));
                }
                else
                {
                    Console.WriteLine("Expected results received: {0}", _CompareRankToString(expected));
                }
            }
        }
Exemplo n.º 43
0
        public static DbValue IIF(DbFunctionTools tools, DbFunctionArguments args)
        {
            string FunctionName = "IIF";

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

            bool truecond;

            if (Types.IsNullValue(arg0))
            {
                truecond = false;
            }
            else if (DbTypeID.INT == arg0type.ID)
            {
                truecond = 0 != tools.GetInt(arg0);
            }
            else if (DbTypeID.LONG == arg0type.ID)
            {
                truecond = 0 != tools.GetLong(arg0);
            }
            else if (DbTypeID.DOUBLE == arg0type.ID)
            {
                truecond = 0 != tools.GetDouble(arg0);
            }
            else
            {
                bool allzero = true;
                for (int i = 1; i < arg0.Length; i++)
                {
                    if (arg0[i] != 0)
                    {
                        allzero = false;
                        break;
                    }
                }
                truecond = !allzero;
            }

            byte[] resultbuf = new byte[arg1.Length > arg2.Length ? arg1.Length : arg2.Length];
            DbType resulttype;

            if (truecond)
            {
                resulttype = DbType.Prepare(resultbuf.Length, arg1type.ID);
                for (int i = 0; i < arg1.Length; i++)
                {
                    resultbuf[i] = arg1[i];
                }
            }
            else
            {
                resulttype = DbType.Prepare(resultbuf.Length, arg2type.ID);
                for (int i = 0; i < arg2.Length; i++)
                {
                    resultbuf[i] = arg2[i];
                }
            }

            return(tools.AllocValue(ByteSlice.Prepare(resultbuf), resulttype));
        }
Exemplo n.º 44
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));
        }
Exemplo n.º 45
0
        public static DbValue DATEADD(DbFunctionTools tools, DbFunctionArguments args)
        {
            string FunctionName = "DATEADD";

            args.EnsureCount(FunctionName, 3);

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

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

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

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

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

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

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

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

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

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

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

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

            args.EnsureCount(FunctionName, 2);

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

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

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

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

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

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

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

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

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

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

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

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

            return(tools.AllocValue(r));
        }
Exemplo n.º 47
0
 int GetTableID(ByteSlice row)
 {
     return(ftools.GetInt(ByteSlice.Prepare(row, 0, 1 + 4)));
 }
Exemplo n.º 48
0
        public static DbValue COMPARE(DbFunctionTools tools, DbFunctionArguments args)
        {
            string FunctionName = "COMPARE";

            args.EnsureCount(FunctionName, 2);

            int result;

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

            if (Types.IsNullValue(arg0))
            {
                result = int.MaxValue;
                return(tools.AllocValue(result));
            }
            if (Types.IsNullValue(arg1))
            {
                result = int.MinValue;
                return(tools.AllocValue(result));
            }

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

            switch (arg0type.ID)
            {
            case DbTypeID.INT:
                i0 = tools.GetInt(arg0);
                break;

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

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

            case DbTypeID.CHARS:
            {
                if (arg1type.ID != DbTypeID.CHARS && arg1type.ID != DbTypeID.DATETIME)
                {
                    args.Expected(FunctionName, 0, "input CHAR(n) or DATETIME, got " + arg1type.Name.ToUpper());
                    return(null);        // Doesn't reach here.
                }

                if (arg1type.ID == DbTypeID.CHARS)
                {
                    for (int i = 1; ; i += 2)
                    {
                        bool b1end = (i + 2 > arg0.Length) || (arg0[i] == 0 && arg0[i + 1] == 0);
                        bool b2end = (i + 2 > arg1.Length) || (arg1[i] == 0 && arg1[i + 1] == 0);
                        if (b1end)
                        {
                            if (b2end)
                            {
                                result = 0;
                                break;
                            }
                            result = -1;
                            break;
                        }
                        else if (b2end)
                        {
                            result = 1;
                            break;
                        }
                        int diff = Types.UTF16BytesToLowerChar(arg0[i], arg0[i + 1])
                                   - Types.UTF16BytesToLowerChar(arg1[i], arg1[i + 1]);
                        if (0 != diff)
                        {
                            char ch0 = Types.UTF16BytesToChar(arg0[i], arg0[i + 1]);
                            char ch1 = Types.UTF16BytesToChar(arg1[i], arg1[i + 1]);
                            if (!Char.IsLetter(ch0) || !Char.IsLetter(ch1))
                            {
                                result = ch0 - ch1;
                            }
                            result = diff;
                            break;
                        }
                    }
                }
                else
                {
                    string   sdt = tools.GetString(arg0).ToString();
                    DateTime dt0 = DateTime.Parse(sdt);
                    DateTime dt1 = tools.GetDateTime(arg1);
                    result = dt0.CompareTo(dt1);
                }
                return(tools.AllocValue(result));
            }
            break;

            case DbTypeID.DATETIME:
            {
                if (arg1type.ID != DbTypeID.CHARS && arg1type.ID != DbTypeID.DATETIME)
                {
                    args.Expected(FunctionName, 0, "input CHAR(n) or DATETIME, got " + arg1type.Name.ToUpper());
                    return(null);        // Doesn't reach here.
                }

                if (arg1type.ID == DbTypeID.DATETIME)
                {
                    DateTime dt0 = tools.GetDateTime(arg0);
                    DateTime dt1 = tools.GetDateTime(arg1);
                    result = dt0.CompareTo(dt1);
                }
                else
                {
                    DateTime dt0 = tools.GetDateTime(arg0);
                    string   sdt = tools.GetString(arg1).ToString();
                    DateTime dt1 = DateTime.Parse(sdt);
                    result = dt0.CompareTo(dt1);
                }
                return(tools.AllocValue(result));
            }
            break;

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

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

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

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

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

            switch (arg0type.ID)
            {
            case DbTypeID.INT:
                switch (arg1type.ID)
                {
                case DbTypeID.INT:
                    result = i0.CompareTo(i1);
                    break;

                case DbTypeID.LONG:
                    result = ((long)i0).CompareTo(l1);
                    break;

                case DbTypeID.DOUBLE:
                    result = ((double)i0).CompareTo(d1);
                    break;

                default:
                    return(null);        // Should never happen.
                }
                break;

            case DbTypeID.LONG:
                switch (arg1type.ID)
                {
                case DbTypeID.INT:
                    result = l0.CompareTo((long)i1);
                    break;

                case DbTypeID.LONG:
                    result = l0.CompareTo(l1);
                    break;

                case DbTypeID.DOUBLE:
                    result = ((double)l0).CompareTo(d1);
                    break;

                default:
                    return(null);        // Should never happen.
                }
                break;

            case DbTypeID.DOUBLE:
                switch (arg1type.ID)
                {
                case DbTypeID.INT:
                    result = d0.CompareTo((double)i1);
                    break;

                case DbTypeID.LONG:
                    result = d0.CompareTo((double)l1);
                    break;

                case DbTypeID.DOUBLE:
                    result = d0.CompareTo(d1);
                    break;

                default:
                    return(null);        // Should never happen.
                }
                break;

            default:
                return(null);    // Should never happen.
            }

            return(tools.AllocValue(result));
        }
Exemplo n.º 49
0
        private static DbValue _PAD(string functionName, DbFunctionTools tools, DbFunctionArguments args, bool isLeft)
        {
            args.EnsureCount(functionName, 3);

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

            mstring str = tools.GetString(arg0);
            int totallen = tools.GetInt(arg1);
            mstring padstr = tools.GetString(arg2);

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

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

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

                if (isLeft)
                {
                    str = newstr.AppendM(str);
                }
                else
                {
                    str = str.AppendM(newstr);
                }
            }
            return tools.AllocValue(str);            
        }
Exemplo n.º 50
0
        public static void DbFunctions_ISLIKE()
        {
            DbFunctionTools tools = new DbFunctionTools();

            {
                Console.WriteLine("Testing positive DbFunctions.ISLIKE(String,'%xxx')...");

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

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

                int expected = 1;

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

            {
                Console.WriteLine("Testing case-insensitive positive DbFunctions.ISLIKE(String,'%xxx')...");

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

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

                int expected = 1;

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

            {
                Console.WriteLine("Testing negative DbFunctions.ISLIKE(String,'%xxx')...");

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

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

                int expected = 0;

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

            {
                Console.WriteLine("Testing positive DbFunctions.ISLIKE(String,'xx_x')...");

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

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

                int expected = 1;

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

            {
                Console.WriteLine("Testing negative DbFunctions.ISLIKE(String,'xx_x')...");

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

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

                int expected = 0;

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

            {
                Console.WriteLine("Testing positive DbFunctions.ISLIKE(String,'xx[a-d]xx')...");

                List<DbValue> args = new List<DbValue>();
                args.Add(tools.AllocValue(mstring.Prepare("comater")));
                args.Add(tools.AllocValue(mstring.Prepare("com[a-d]ter")));
                DbFunctionArguments fargs = new DbFunctionArguments(args);

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

                int expected = 1;

                if (expected != output)
                {
                    throw new Exception("DbFunctions.ISLIKE(String,'xx[a-d]xx') has failed.  Expected result: " + expected.ToString() + ", but received: " + output.ToString());
                }
                else
                {
                    Console.WriteLine("Expected results received.");
                }
            }

            {
                Console.WriteLine("Testing negative DbFunctions.ISLIKE(String,'xx[a-d]xx')...");

                List<DbValue> args = new List<DbValue>();
                args.Add(tools.AllocValue(mstring.Prepare("comxter")));
                args.Add(tools.AllocValue(mstring.Prepare("com[a-d]ter")));
                DbFunctionArguments fargs = new DbFunctionArguments(args);

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

                int expected = 0;

                if (expected != output)
                {
                    throw new Exception("DbFunctions.ISLIKE(String,'xx[a-d]xx') has failed.  Expected result: " + expected.ToString() + ", but received: " + output.ToString());
                }
                else
                {
                    Console.WriteLine("Expected results received.");
                }
            }

            {
                Console.WriteLine("Testing positive DbFunctions.ISLIKE(String,'xx[^a-d]xx')...");

                List<DbValue> args = new List<DbValue>();
                args.Add(tools.AllocValue(mstring.Prepare("comxter")));
                args.Add(tools.AllocValue(mstring.Prepare("com[^a-d]ter")));
                DbFunctionArguments fargs = new DbFunctionArguments(args);

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

                int expected = 1;

                if (expected != output)
                {
                    throw new Exception("DbFunctions.ISLIKE(String,'xx[a-d]xx') has failed.  Expected result: " + expected.ToString() + ", but received: " + output.ToString());
                }
                else
                {
                    Console.WriteLine("Expected results received.");
                }
            }

            {
                Console.WriteLine("Testing negative DbFunctions.ISLIKE(String,'xx[^a-d]xx')...");

                List<DbValue> args = new List<DbValue>();
                args.Add(tools.AllocValue(mstring.Prepare("comater")));
                args.Add(tools.AllocValue(mstring.Prepare("com[^a-d]ter")));
                DbFunctionArguments fargs = new DbFunctionArguments(args);

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

                int expected = 0;

                if (expected != output)
                {
                    throw new Exception("DbFunctions.ISLIKE(String,'xx[a-d]xx') has failed.  Expected result: " + expected.ToString() + ", but received: " + output.ToString());
                }
                else
                {
                    Console.WriteLine("Expected results received.");
                }
            }

            //Using Wildcard Characters As Literals
            {
                Console.WriteLine("Testing positive DbFunctions.ISLIKE(String,'x[%]')...");

                List<DbValue> args = new List<DbValue>();
                args.Add(tools.AllocValue(mstring.Prepare("5%")));
                args.Add(tools.AllocValue(mstring.Prepare("5[%]")));
                DbFunctionArguments fargs = new DbFunctionArguments(args);

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

                int expected = 1;

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

            {
                Console.WriteLine("Testing negative DbFunctions.ISLIKE(String,'x[%]')...");

                List<DbValue> args = new List<DbValue>();
                args.Add(tools.AllocValue(mstring.Prepare("54")));
                args.Add(tools.AllocValue(mstring.Prepare("5[%]")));
                DbFunctionArguments fargs = new DbFunctionArguments(args);

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

                int expected = 0;

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

            {
                Console.WriteLine("Testing positive DbFunctions.ISLIKE(String,'[_]n')...");

                List<DbValue> args = new List<DbValue>();
                args.Add(tools.AllocValue(mstring.Prepare("_n")));
                args.Add(tools.AllocValue(mstring.Prepare("[_]n")));
                DbFunctionArguments fargs = new DbFunctionArguments(args);

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

                int expected = 1;

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

            {
                Console.WriteLine("Testing negative DbFunctions.ISLIKE(String,'[_]n')...");

                List<DbValue> args = new List<DbValue>();
                args.Add(tools.AllocValue(mstring.Prepare("n")));
                args.Add(tools.AllocValue(mstring.Prepare("[_]n")));
                DbFunctionArguments fargs = new DbFunctionArguments(args);

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

                int expected = 0;

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

            {
                Console.WriteLine("Testing positive DbFunctions.ISLIKE(String,'[-acdf]')...");

                List<DbValue> args = new List<DbValue>();
                args.Add(tools.AllocValue(mstring.Prepare("-")));
                args.Add(tools.AllocValue(mstring.Prepare("[-acdf]")));
                DbFunctionArguments fargs = new DbFunctionArguments(args);

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

                int expected = 1;

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

            {
                Console.WriteLine("Testing negative DbFunctions.ISLIKE(String,'[-acdf]')...");

                List<DbValue> args = new List<DbValue>();
                args.Add(tools.AllocValue(mstring.Prepare("b")));
                args.Add(tools.AllocValue(mstring.Prepare("[-acdf]")));
                DbFunctionArguments fargs = new DbFunctionArguments(args);

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

                int expected = 0;

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

            {
                Console.WriteLine("Testing positive DbFunctions.ISLIKE(String,'[ [ ]')...");

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

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

                int expected = 1;

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

            {
                Console.WriteLine("Testing negative DbFunctions.ISLIKE(String,'[ [ ]')...");

                List<DbValue> args = new List<DbValue>();
                args.Add(tools.AllocValue(mstring.Prepare("p")));
                args.Add(tools.AllocValue(mstring.Prepare("[ [ ]")));
                DbFunctionArguments fargs = new DbFunctionArguments(args);

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

                int expected = 0;

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

            {
                Console.WriteLine("Testing negative DbFunctions.ISLIKE(String,'[*]')...");

                List<DbValue> args = new List<DbValue>();
                args.Add(tools.AllocValue(mstring.Prepare("*")));
                args.Add(tools.AllocValue(mstring.Prepare("[*]")));
                DbFunctionArguments fargs = new DbFunctionArguments(args);

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

                int expected = 1;

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

        }
Exemplo n.º 51
0
        public static DbValue CHARINDEX(DbFunctionTools tools, DbFunctionArguments args)
        {
            string FunctionName = "CHARINDEX";

            args.EnsureMinCount(FunctionName, 2);

            int index = -1;
            DbType arg0type;
            ByteSlice arg0 = args[0].Eval(out arg0type);
            DbType arg1type;
            ByteSlice arg1 = args[1].Eval(out arg1type);
            if (Types.IsNullValue(arg0) || Types.IsNullValue(arg1))
            {
                return tools.AllocValue(index);
            }

            if (arg0type.ID != DbTypeID.CHARS)
            {
                args.Expected(FunctionName, 0, "input CHAR(n), got " + arg0type.Name.ToUpper());
                return null;
            }
            if (arg1type.ID != DbTypeID.CHARS)
            {
                args.Expected(FunctionName, 0, "input CHAR(n), got " + arg1type.Name.ToUpper());
                return null;
            }

            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());
                    return null;
                }
                startIndex = tools.GetInt(arg2);
                if (startIndex < 0)
                {
                    startIndex = 0;
                }
            }
            
            mstring word = tools.GetString(arg0);
            mstring sentence = tools.GetString(arg1);

            if (startIndex > sentence.Length - 1)
            {
                index = -1;
            }
            else if (startIndex == 0)
            {
                index = sentence.IndexOf(word);
            }
            else
            {
                mstring partsentence = sentence.SubstringM(startIndex);
                int ix = partsentence.IndexOf(word);
                if (ix == -1)
                {
                    index = -1;
                }
                else
                {
                    index = startIndex + ix;
                }
            }
            return tools.AllocValue(index);
        }
Exemplo n.º 52
0
        public static void DbFunctions_NULLIF()
        {
            DbFunctionTools tools = new DbFunctionTools();

            {
                Console.WriteLine("Testing DbFunctions.NULLIF(DateTime, DateTime)...");

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

                DbValue valOutput = DbFunctions.NULLIF(tools, fargs);                
                ByteSlice bs = valOutput.Eval();
                bool output = Types.IsNullValue(bs);
                bool expected = true;

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

            {
                Console.WriteLine("Testing DbFunctions.NULLIF(DateTime, 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/2/2000 10:00:00 AM")));
                DbFunctionArguments fargs = new DbFunctionArguments(args);

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

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

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

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

                DbValue valOutput = DbFunctions.NULLIF(tools, fargs);
                ByteSlice bs = valOutput.Eval();
                bool output = Types.IsNullValue(bs);
                bool expected = true;

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

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

                List<DbValue> args = new List<DbValue>();
                int x = 10;
                args.Add(tools.AllocValue(x));
                args.Add(tools.AllocValue(11));
                DbFunctionArguments fargs = new DbFunctionArguments(args);

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

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

            {
                Console.WriteLine("Testing DbFunctions.NULLIF(Int32, long)...");

                List<DbValue> args = new List<DbValue>();
                int x = 10;
                long y = 10;
                args.Add(tools.AllocValue(x));
                args.Add(tools.AllocValue(y));
                DbFunctionArguments fargs = new DbFunctionArguments(args);

                DbValue valOutput = DbFunctions.NULLIF(tools, fargs);
                ByteSlice bs = valOutput.Eval();
                bool output = Types.IsNullValue(bs);
                bool expected = true;

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

            {
                Console.WriteLine("Testing DbFunctions.NULLIF(Int32, long)...");

                List<DbValue> args = new List<DbValue>();
                int x = 10;
                long y = 11;
                args.Add(tools.AllocValue(x));
                args.Add(tools.AllocValue(y));
                DbFunctionArguments fargs = new DbFunctionArguments(args);

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

                if (expected != output)
                {
                    throw new Exception("DbFunctions.NULLIF(Int32, long) has failed.  Expected result: " + expected.ToString() + ", but received: " + output.ToString());
                }
                else
                {
                    Console.WriteLine("Expected results received.");
                }
            }
        }
Exemplo n.º 53
0
        public static void DbAggregators_MIN()
        {
            DbFunctionTools tools = new DbFunctionTools();
            Random rnd = new Random();
            const int rowcount = 20;

            //Double.
            {
                Console.WriteLine("Testing DbAggregators_MIN(Double)...");

                DbFunctionArguments[] fargs = new DbFunctionArguments[rowcount];
                double expected = double.MaxValue;

                for (int i = 0; i < fargs.Length; i++)
                {
                    double input = rnd.NextDouble();
                    if (input < 0.5)
                    {
                        input = input * -1d;
                    }
                    if (input < expected)
                    {
                        expected = input;
                    }
                    List<DbValue> args = new List<DbValue>();
                    args.Add(tools.AllocValue(input));
                    fargs[i] = new DbFunctionArguments(args);                    
                }
                DbValue valOutput = DbAggregators.MIN(tools, new DbAggregatorArguments(fargs));
                ByteSlice bs = valOutput.Eval();
                double output = tools.GetDouble(bs);

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

            {
                Console.WriteLine("Testing DbAggregators_MIN(Int32)...");

                DbFunctionArguments[] fargs = new DbFunctionArguments[rowcount];
                int expected = Int32.MaxValue;

                for (int i = 0; i < fargs.Length; i++)
                {
                    int input = rnd.Next(Int32.MinValue, Int32.MaxValue);
                    if (input < expected)
                    {
                        expected = input;
                    }
                    List<DbValue> args = new List<DbValue>();
                    args.Add(tools.AllocValue(input));
                    fargs[i] = new DbFunctionArguments(args);
                }
                DbValue valOutput = DbAggregators.MIN(tools, new DbAggregatorArguments(fargs));
                ByteSlice bs = valOutput.Eval();
                int output = tools.GetInt(bs);

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

            {
                Console.WriteLine("Testing DbAggregators_MIN(Int64)...");

                DbFunctionArguments[] fargs = new DbFunctionArguments[rowcount];
                long expected = Int64.MaxValue;

                for (int i = 0; i < fargs.Length; i++)
                {
                    long input = DateTime.Now.Ticks;
                    if (input % 2 == 0)
                    {
                        input = input * -1;
                    }
                    if (input < expected)
                    {
                        expected = input;
                    }
                    List<DbValue> args = new List<DbValue>();
                    args.Add(tools.AllocValue(input));
                    fargs[i] = new DbFunctionArguments(args);
                }
                DbValue valOutput = DbAggregators.MIN(tools, new DbAggregatorArguments(fargs));
                ByteSlice bs = valOutput.Eval();
                long output = tools.GetLong(bs);

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

            {
                Console.WriteLine("Testing DbAggregators_MIN(char(n))...");

                DbFunctionArguments[] fargs = new DbFunctionArguments[rowcount];
                string expected = null;

                for (int i = 0; i < fargs.Length; i++)
                {
                    int strlen = rnd.Next(1, 100);
                    mstring input = Utils.GenString(strlen);
                    if (expected == null || input.ToString().CompareTo(expected) < 0)
                    {
                        expected = input.ToString();
                    }
                    List<DbValue> args = new List<DbValue>();
                    args.Add(tools.AllocValue(input));
                    fargs[i] = new DbFunctionArguments(args);
                }
                DbValue valOutput = DbAggregators.MIN(tools, new DbAggregatorArguments(fargs));
                ByteSlice bs = valOutput.Eval();
                mstring output = tools.GetString(bs);

                if (expected != output.ToString())
                {
                    throw new Exception("DbAggregators_MIN(char(n)) has failed.  Expected result: " + expected.ToString() + ", but received: " + output.ToString());
                }
                else
                {
                    Console.WriteLine("Expected results received.");
                }
            }
        }
Exemplo n.º 54
0
        public static void DbFunctions_EQUAL()
        {
            DbFunctionTools tools = new DbFunctionTools();
            Random rnd = new Random();

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

                mstring s1 = Utils.GenString(rnd.Next(1, 200));
                mstring s2 = Utils.GenString(rnd.Next(1, 200));

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

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

                int expected = (s1.ToString().CompareTo(s2.ToString()) == 0 ? 1 : 0);

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

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

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

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

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

                int expected = (x == y ? 1 : 0);

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

            {
                Console.WriteLine("Testing DbFunctions.EQUAL(Double, Double)...");

                double x = rnd.NextDouble();
                double y = rnd.NextDouble();

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

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

                int expected = (x == y ? 1 : 0);

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

            {
                Console.WriteLine("Testing DbFunctions.EQUAL(Long, Long)...");

                long x = DateTime.Now.Ticks;
                long y = DateTime.Now.Ticks;

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

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

                int expected = (x == y ? 1 : 0);

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

            {
                Console.WriteLine("Testing DbFunctions.EQUAL(Double, Int32)...");
                                
                double x = rnd.NextDouble();
                int y = rnd.Next();

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

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

                int expected = (x == y ? 1 : 0);

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

            {
                Console.WriteLine("Testing DbFunctions.EQUAL(Double, Long)...");

                double x = rnd.NextDouble();
                long y = DateTime.Now.Ticks;

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

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

                int expected = (x == y ? 1 : 0);

                if (expected != output)
                {
                    throw new Exception("DbFunctions.EQUAL(Double, Long) has failed.  Expected result: " + expected.ToString() + ", but received: " + output.ToString());
                }
                else
                {
                    Console.WriteLine("Expected results received.");
                }
            }
        }
Exemplo n.º 55
0
        public static DbValue IIF(DbFunctionTools tools, DbFunctionArguments args)
        {
            string FunctionName = "IIF";

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

            bool truecond;
            if (Types.IsNullValue(arg0))
            {
                truecond = false;
            }
            else if (DbTypeID.INT == arg0type.ID)
            {
                truecond = 0 != tools.GetInt(arg0);
            }
            else if (DbTypeID.LONG == arg0type.ID)
            {
                truecond = 0 != tools.GetLong(arg0);
            }
            else if (DbTypeID.DOUBLE == arg0type.ID)
            {
                truecond = 0 != tools.GetDouble(arg0);
            }
            else
            {
                bool allzero = true;
                for (int i = 1; i < arg0.Length; i++)
                {
                    if (arg0[i] != 0)
                    {
                        allzero = false;
                        break;
                    }
                }
                truecond = !allzero;
            }

            byte[] resultbuf = new byte[arg1.Length > arg2.Length ? arg1.Length : arg2.Length];
            DbType resulttype;
            if (truecond)
            {
                resulttype = DbType.Prepare(resultbuf.Length, arg1type.ID);
                for (int i = 0; i < arg1.Length; i++)
                {
                    resultbuf[i] = arg1[i];
                }
            }
            else
            {
                resulttype = DbType.Prepare(resultbuf.Length, arg2type.ID);
                for (int i = 0; i < arg2.Length; i++)
                {
                    resultbuf[i] = arg2[i];
                }
            }

            return tools.AllocValue(ByteSlice.Prepare(resultbuf), resulttype);

        }
Exemplo n.º 56
0
        public static DbValue SIGN(DbFunctionTools tools, DbFunctionArguments args)
        {
            string FunctionName = "SIGN";

            args.EnsureCount(FunctionName, 1);

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

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

            int sign = 0;

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

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

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

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