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

            args.EnsureCount(FunctionName, 2);

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

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

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

            mstring s0 = tools.GetString(arg0);
            mstring s1 = tools.GetString(arg1);

            s0 = s0.AppendM(s1);
            return(tools.AllocValue(s0));
        }
        public static DbValue REVERSE(DbFunctionTools tools, DbFunctionArguments args)
        {
            string FunctionName = "REVERSE";

            args.EnsureCount(FunctionName, 1);

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

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

            mstring x = tools.GetString(arg0);

            mstring r = mstring.Prepare();

            for (int i = x.Length - 1; i >= 0; i--)
            {
                r = r.AppendM(x.SubstringM(i, 1));
            }

            return(tools.AllocValue(r));
        }
        public static DbValue 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));
            }
        }
Exemple #4
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));
        }