Пример #1
0
            ByteSlice Conv_IntToDouble(ByteSlice value, int ResultSize, DbFunctionTools tools)
            {
                int     x = tools.GetInt(value);
                DbValue v = tools.AllocValue((double)x);

                return(v.Eval());
            }
Пример #2
0
            ByteSlice Conv_LongToDouble(ByteSlice value, int ResultSize, DbFunctionTools tools)
            {
                long    x = tools.GetLong(value);
                DbValue v = tools.AllocValue((double)x);

                return(v.Eval());
            }
Пример #3
0
            ByteSlice Conv_String(ByteSlice value, int ResultSize, DbFunctionTools tools)
            {
                mstring x = tools.GetString(value);

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

                return(v.Eval());
            }
Пример #4
0
 public DateTime GetDateTime(DbValue input)
 {
     DbType type;
     ByteSlice bs = input.Eval(out type);
     if (DbTypeID.DATETIME != type.ID)
     {
         throw new Exception("Expected DATETIME, not " + type.Name.ToUpper());
     }
     return GetDateTime(bs);
 }
Пример #5
0
 public long GetLong(DbValue input)
 {
     DbType type;
     ByteSlice bs = input.Eval(out type);
     if (DbTypeID.LONG != type.ID)
     {
         throw new Exception("Expected LONG, not " + type.Name.ToUpper());
     }
     return GetLong(bs);
 }
Пример #6
0
 public int GetInt(DbValue input)
 {
     DbType type;
     ByteSlice bs = input.Eval(out type);
     if (DbTypeID.INT != type.ID)
     {
         throw new Exception("Expected INT, not " + type.Name.ToUpper());
     }
     return GetInt(bs);
 }
Пример #7
0
        public long GetLong(DbValue input)
        {
            DbType    type;
            ByteSlice bs = input.Eval(out type);

            if (DbTypeID.LONG != type.ID)
            {
                throw new Exception("Expected LONG, not " + type.Name.ToUpper());
            }
            return(GetLong(bs));
        }
Пример #8
0
        public int GetInt(DbValue input)
        {
            DbType    type;
            ByteSlice bs = input.Eval(out type);

            if (DbTypeID.INT != type.ID)
            {
                throw new Exception("Expected INT, not " + type.Name.ToUpper());
            }
            return(GetInt(bs));
        }
Пример #9
0
        public mstring GetString(DbValue input)
        {
            DbType    type;
            ByteSlice bs = input.Eval(out type);

            if (DbTypeID.CHARS != type.ID)
            {
                throw new Exception("Expected CHAR(n), not " + type.Name.ToUpper());
            }
            return(GetString(bs));
        }
Пример #10
0
        public double GetDouble(DbValue input)
        {
            DbType    type;
            ByteSlice bs = input.Eval(out type);

            if (DbTypeID.DOUBLE != type.ID)
            {
                throw new Exception("Expected DOUBLE, not " + type.Name.ToUpper());
            }
            return(GetDouble(bs));
        }
Пример #11
0
        public DateTime GetDateTime(DbValue input)
        {
            DbType    type;
            ByteSlice bs = input.Eval(out type);

            if (DbTypeID.DATETIME != type.ID)
            {
                throw new Exception("Expected DATETIME, not " + type.Name.ToUpper());
            }
            return(GetDateTime(bs));
        }
Пример #12
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));
        }
Пример #13
0
        // 0 is match, <0 is less than, >0 is greater than.
        public int Compare()
        {
            DbValue   result = Context.ExecDbFunction("COMPARE", args);
            DbType    typeresult;
            ByteSlice bsresult = result.Eval(out typeresult);

#if DEBUG
            if (DbTypeID.INT != typeresult.ID)
            {
                throw new Exception("DEBUG:  COMPARE should return INT, not " + typeresult.Name.ToUpper());
            }
#endif
            return(Context.Tools.GetInt(bsresult));
        }
Пример #14
0
 public mstring GetString(DbValue input)
 {
     DbType type;
     ByteSlice bs = input.Eval(out type);
     if (DbTypeID.CHARS != type.ID)
     {
         throw new Exception("Expected CHAR(n), not " + type.Name.ToUpper());
     }
     return GetString(bs);
 }
Пример #15
0
            public override void Map(ByteSlice row, MapOutput output)
            {
                if (JoinType.X == type)
                {
                    ftools = new DbFunctionTools();

                    string QlLeftTableName = DSpace_ExecArgs[0];
                    LeftTableName = Qa.QlArgsUnescape(QlLeftTableName);
                    string stype            = DSpace_ExecArgs[1];
                    string QlRightTableName = DSpace_ExecArgs[2];
                    RightTableName = Qa.QlArgsUnescape(QlRightTableName);
                    string QlOn = DSpace_ExecArgs[3];
                    On = Qa.QlArgsUnescape(QlOn);
                    {
                        string LeftColInfo = Qa.QlArgsUnescape(DSpace_ExecArgs[4]);
                        int    ileq        = LeftColInfo.LastIndexOf('=');
                        string sLeftType   = LeftColInfo.Substring(ileq + 1);
                        LeftType = DbType.Prepare(sLeftType);
                        LeftConv = NeedConv(LeftType, RightType);
                        string[] st = LeftColInfo.Substring(0, ileq).Split(',');
                        LeftOffset = int.Parse(st[0]);
                        LeftSize   = int.Parse(st[1]);
                    }
                    {
                        string RightColInfo = Qa.QlArgsUnescape(DSpace_ExecArgs[5]);
                        int    ileq         = RightColInfo.LastIndexOf('=');
                        string sRightType   = RightColInfo.Substring(ileq + 1);
                        RightType = DbType.Prepare(sRightType);
                        RightConv = NeedConv(RightType, LeftType);
                        string[] st = RightColInfo.Substring(0, ileq).Split(',');
                        RightOffset = int.Parse(st[0]);
                        RightSize   = int.Parse(st[1]);
                    }
                    if (0 == string.Compare("INNER", stype, true))
                    {
                        type = JoinType.INNER_JOIN;
                    }
                    else if (0 == string.Compare("LEFT_OUTER", stype, true))
                    {
                        type = JoinType.LEFT_OUTER_JOIN;
                    }
                    else if (0 == string.Compare("RIGHT_OUTER", stype, true))
                    {
                        type = JoinType.RIGHT_OUTER_JOIN;
                    }
                    else
                    {
                        throw new NotSupportedException("DEBUG:  JOIN type not supported: " + stype);
                    }

                    string DfsTableFilesInput = DSpace_ExecArgs[6];

                    /*
                     * TableFileNames = DfsTableFilesInput.Split(';');
                     * if (2 != TableFileNames.Length)
                     * {
                     *  throw new Exception("DEBUG:  Invalid number of tables");
                     * }
                     * for (int it = 0; it < TableFileNames.Length; it++)
                     * {
                     *  if (TableFileNames[it].StartsWith("dfs://", StringComparison.OrdinalIgnoreCase))
                     *  {
                     *      TableFileNames[it] = TableFileNames[it].Substring(6);
                     *  }
                     *  int iat = TableFileNames[it].IndexOf('@');
                     *  if (-1 != iat)
                     *  {
                     *      TableFileNames[it] = TableFileNames[it].Substring(0, iat);
                     *  }
                     * }
                     * */
                }

                int tableid = GetTableID(row);

                ByteSlice key;

                if (0 == tableid)
                {
                    key = ByteSlice.Prepare(row, LeftOffset, LeftSize);
                    if (null != LeftConv)
                    {
                        key = LeftConv(key, DSpace_KeyLength, ftools);
                    }
                }
                else if (1 == tableid)
                {
                    key = ByteSlice.Prepare(row, RightOffset, RightSize);
                    if (null != RightConv)
                    {
                        key = RightConv(key, DSpace_KeyLength, ftools);
                    }
                }
                else
                {
                    throw new Exception("Map: Unexpected TableID: " + tableid);
                }

                List <byte> valuebuf = ftools.AllocBuffer(1 + 4 + row.Length);

                {
                    DbValue tableiddbvalue = ftools.AllocValue(tableid);
                    tableiddbvalue.Eval().AppendTo(valuebuf);
                }
                row.AppendTo(valuebuf);

                output.Add(key, ByteSlice.Prepare(valuebuf));

                ftools.ResetBuffers();
            }
Пример #16
0
        // Converts the CallInfoArgs of a CallInfo into arguments for DbExec.
        // frowsIndex is not preserved to the caller!
        List <DbFunctionArguments> _CallArgsToExecArgs(CallInfo ci)
        {
            List <List <DbValue> > fvargs = _NextCallList();
            int frowsCount = frows.Count;

            for (int irow = 0; irow < frowsCount; irow++)
            {
                fvargs.Add(functools.AllocDbValueList());
            }
            int argsCount = ci.args.Count;

            for (int iarg = 0; iarg < argsCount; iarg++)
            {
                if (null != ci.args[iarg].value)
                {
                    DbValue favalue = ci.args[iarg].value;
                    for (this.frowsIndex = 0; this.frowsIndex < frowsCount; this.frowsIndex++)
                    {
                        // Evaluate favalue now that this.frowsIndex (IValueContext) is updated.
                        // Field evaluation...
                        DbType    etype;
                        ByteSlice ebs    = favalue.Eval(out etype);
                        DbValue   evalue = functools.AllocValue(ebs, etype);

                        fvargs[this.frowsIndex].Add(evalue);
                    }
                }
                else //if (null == args[iarg].value)
                {
                    CallInfo       nestci = ci.args[iarg].nest;
                    List <DbValue> nestresults;
                    {
                        int save_frowsIndex = this.frowsIndex;
                        nestresults     = _ProcessSelectCallInfo(nestci);
                        this.frowsIndex = save_frowsIndex;
                    }
                    int nestresultsCount = nestresults.Count;
                    if (1 == nestresultsCount)
                    {
                        DbValue favalue = nestresults[0];
                        for (int irow = 0; irow < frowsCount; irow++)
                        {
                            List <DbValue> dbvaluelist = fvargs[irow];
                            fvargs[irow].Add(favalue);
                            fvargs[irow] = dbvaluelist;
                        }
                    }
                    else if (frowsCount == nestresultsCount)
                    {
                        for (int irow = 0; irow < frowsCount; irow++)
                        {
                            List <DbValue> dbvaluelist = fvargs[irow];
                            fvargs[irow].Add(nestresults[irow]);
                            fvargs[irow] = dbvaluelist;
                        }
                    }
                    else
                    {
                        throw new Exception("Unexpected number of values returned; on a group of " + frowsCount.ToString() + " rows, function returned " + nestresultsCount.ToString() + " values");
                    }
                }
            }
            List <DbFunctionArguments> fargs = _NextCallArgs();

            for (int irow = 0; irow < frowsCount; irow++)
            {
                DbFunctionArguments fa = new DbFunctionArguments(fvargs[irow]);
                fargs.Add(fa);
            }
            return(fargs);
        }
Пример #17
0
        CallInfo _GetSelectPartCalls(ref string calls)
        {
            CallInfo ci = new CallInfo();
            {
                string colpart = Qa.NextPart(ref calls);
                if (0 == colpart.Length)
                {
                    throw new Exception("Expected value");
                }
                if ("-" == colpart || "+" == colpart)
                {
                    colpart += Qa.NextPart(ref calls);
                }
                string s;
                s = Qa.NextPart(ref calls);
                if ("(" == s)
                {
                    List <CallInfoArg> args = new List <CallInfoArg>(4);
                    {
                        string xcalls = calls;
                        if (")" == Qa.NextPart(ref xcalls))
                        {
                            calls   = xcalls;
                            ci.func = colpart;
                            ci.args = args;
                            return(ci);
                        }
                    }
                    for (; ;)
                    {
                        CallInfo    nestci = _GetSelectPartCalls(ref calls);
                        CallInfoArg arg    = new CallInfoArg();
                        if (nestci.func == null)
                        {
#if DEBUG
                            if (1 != nestci.args.Count)
                            {
                                throw new Exception("DEBUG:  (1 != nestci.args.Count)");
                            }
#endif
                            arg = nestci.args[0];
                        }
                        else
                        {
                            arg.nest = nestci;
                        }
                        args.Add(arg);
                        s = Qa.NextPart(ref calls);
                        if (0 == string.Compare("AS", s, true))
                        {
                            arg = new CallInfoArg();
                            {
                                StringBuilder sbas     = new StringBuilder();
                                int           asparens = 0;
                                for (; ;)
                                {
                                    s = Qa.NextPart(ref calls);
                                    if (0 == s.Length || "," == s)
                                    {
                                        //calls += s;
                                        break;
                                    }
                                    else if ("(" == s)
                                    {
                                        asparens++;
                                        sbas.Append(s);
                                    }
                                    else if (")" == s)
                                    {
                                        if (0 == asparens)
                                        {
                                            //calls += s;
                                            break;
                                        }
                                        asparens--;
                                        sbas.Append(s);
                                    }
                                    else
                                    {
                                        sbas.Append(s);
                                    }
                                }
                                if (0 == sbas.Length)
                                {
                                    throw new Exception("Expected type after AS");
                                }
                                {
                                    DbValue iterval = functools.AllocValue(mstring.Prepare("AS " + sbas.ToString()));
                                    // Need to copy the value out of the functools memory
                                    // so that it survives this map/reduce iteration...
                                    DbType      xtype;
                                    ByteSlice   iterbs = iterval.Eval(out xtype);
                                    List <byte> newbuf = new List <byte>(iterbs.Length);
                                    iterbs.AppendTo(newbuf);
                                    arg.value = new ImmediateValue(null, ByteSlice.Prepare(newbuf), xtype);
                                }
                                args.Add(arg);
                            }
                            //s = Qa.NextPart(ref calls);
                        }
                        if (s == ",")
                        {
                            continue;
                        }
                        if (s == ")")
                        {
                            string xnc = calls;
                            s = Qa.NextPart(ref xnc);
                            if (0 == s.Length || "," == s || ")" == s ||
                                0 == string.Compare(s, "AS", true))
                            {
                                ci.func = colpart;
                                ci.args = args;
                                return(ci);
                            }
                            else
                            {
                                throw new Exception("Unexpected: " + s);
                            }
                            break;
                        }
                        else
                        {
                            if (s.Length != 0)
                            {
                                throw new Exception("Unexpected: " + s);
                            }
                            throw new Exception("Unexpected end of select clause");
                        }
                    }
                    // Doesn't reach here.
                }
                else
                {
                    //if (0 == s.Length || "," == s || ")" == s)
                    {
                        calls = s + " " + calls; // Undo.
                        //ci.func = null;
                        CallInfoArg arg;
                        arg = new CallInfoArg();
                        //arg.s = colpart;
                        if (colpart.Length > 0 &&
                            (char.IsLetter(colpart[0]) || '_' == colpart[0]))
                        {
                            int icol = DbColumn.IndexOf(cols, colpart);
                            if (-1 == icol)
                            {
                                throw new Exception("No such column named " + colpart);
                            }
                            arg.value = new ColValue(this, cols[icol]);
                        }
                        else
                        {
                            DbTypeID  typeid;
                            ByteSlice bs = Types.LiteralToValue(colpart, out typeid);
                            arg.value = new ImmediateValue(null, bs, DbType.Prepare(bs.Length, typeid));
                        }
                        ci.args    = new CallInfoArg[1];
                        ci.args[0] = arg;
                        return(ci);
                    }

                    /*else
                     * {
                     *  throw new Exception("Unexpected: " + s);
                     * }*/
                }
            }
        }
Пример #18
0
        public override ByteSlice Eval(out DbType type)
        {
            DbValue ret = Context.ExecDbFunction(funcname, args);

            return(ret.Eval(out type));
        }
Пример #19
0
 public double GetDouble(DbValue input)
 {
     DbType type;
     ByteSlice bs = input.Eval(out type);
     if (DbTypeID.DOUBLE != type.ID)
     {
         throw new Exception("Expected DOUBLE, not " + type.Name.ToUpper());
     }
     return GetDouble(bs);
 }