public static void DbFunctions_RIGHT() { DbFunctionTools tools = new DbFunctionTools(); //String,Int32. { Console.WriteLine("Testing DbFunctions.RIGHT(String,Int32)..."); mstring input = Utils.GenString(100); List<DbValue> args = new List<DbValue>(); args.Add(tools.AllocValue(input)); args.Add(tools.AllocValue(5)); DbFunctionArguments fargs = new DbFunctionArguments(args); DbValue valOutput = DbFunctions.RIGHT(tools, fargs); ByteSlice bs = valOutput.Eval(); mstring output = tools.GetString(bs); string str = input.ToString(); string expected = str.Substring(str.Length - 5, 5); if (expected != output.ToString()) { throw new Exception("DbFunctions.RIGHT(String,Int32) has failed. Expected result: " + expected.ToString() + ", but received: " + output.ToString()); } else { Console.WriteLine("Expected results received."); } } }
public static DbValue NULLIF(DbFunctionTools tools, DbFunctionArguments args) { string FunctionName = "NULLIF"; args.EnsureCount(FunctionName, 2); DbType arg0type; ByteSlice arg0 = args[0].Eval(out arg0type); ImmediateValue argval = null; argval = tools.AllocValue(arg0type.ID); argval.SetValue(arg0); DbFunctionArguments compareargs = new DbFunctionArguments(new DbValue[2]); compareargs[0] = argval; compareargs[1] = args[1]; DbValue result = COMPARE(tools, compareargs); int iresult = tools.GetInt(result); if (iresult == 0) { List<byte> buf = tools.AllocBuffer(arg0type.Size); buf.Add(1); for (int i = 0; i < arg0type.Size - 1; i++) { buf.Add(0); } return tools.AllocValue(ByteSlice.Prepare(buf), arg0type); } else { return args[0]; } }
public static DbValue INSTR(DbFunctionTools tools, DbFunctionArguments args) { string FunctionName = "INSTR"; args.EnsureMinCount(FunctionName, 2); DbType arg0type; ByteSlice arg0 = args[0].Eval(out arg0type); if (Types.IsNullValue(arg0)) { return tools.AllocNullValue(); // Give a null, take a null. } if (arg0type.ID != DbTypeID.CHARS) { args.Expected(FunctionName, 0, "input CHAR(n), got " + arg0type.Name.ToUpper()); } DbType arg1type; ByteSlice arg1 = args[1].Eval(out arg1type); if (Types.IsNullValue(arg1)) { return tools.AllocNullValue(); // Give a null, take a null. } if (arg1type.ID != DbTypeID.CHARS) { args.Expected(FunctionName, 0, "input CHAR(n), got " + arg1type.Name.ToUpper()); } int startIndex = 0; if (args.Length > 2) { DbType arg2type; ByteSlice arg2 = args[2].Eval(out arg2type); if (arg2type.ID != DbTypeID.INT) { args.Expected(FunctionName, 0, "input INT, got " + arg2type.Name.ToUpper()); } if (!Types.IsNullValue(arg2)) { startIndex = tools.GetInt(arg2); } } mstring sentence = tools.GetString(arg0); mstring word = tools.GetString(arg1); int index = -1; if (startIndex < sentence.Length) { if (startIndex > 0) { sentence = sentence.SubstringM(startIndex); } index = sentence.IndexOf(word); if (index > -1) { index += startIndex; } } return tools.AllocValue(index); }
public static void DbFunctions_PATINDEX() { DbFunctionTools tools = new DbFunctionTools(); //String,Int32. { Console.WriteLine("Testing DbFunctions.PATINDEX(String, string)..."); List<DbValue> args = new List<DbValue>(); args.Add(tools.AllocValue(mstring.Prepare("%ap_pl[e]%"))); args.Add(tools.AllocValue(mstring.Prepare("red is ap5ple, my favourite."))); DbFunctionArguments fargs = new DbFunctionArguments(args); DbValue valOutput = DbFunctions.PATINDEX(tools, fargs); ByteSlice bs = valOutput.Eval(); int output = tools.GetInt(bs); int expected = 7; if (expected != output) { throw new Exception("DbFunctions.PATINDEX(String) has failed. Expected result: " + expected.ToString() + ", but received: " + output.ToString()); } else { Console.WriteLine("Expected results received."); } } }
public static void DbFunctions_UPPER() { DbFunctionTools tools = new DbFunctionTools(); //String,Int32. { Console.WriteLine("Testing DbFunctions.UPPER(String)..."); List<DbValue> args = new List<DbValue>(); args.Add(tools.AllocValue(mstring.Prepare("hello world"))); DbFunctionArguments fargs = new DbFunctionArguments(args); DbValue valOutput = DbFunctions.UPPER(tools, fargs); ByteSlice bs = valOutput.Eval(); mstring output = tools.GetString(bs); mstring expected = mstring.Prepare("HELLO WORLD"); if (expected != output) { throw new Exception("DbFunctions.UPPER(String) has failed. Expected result: " + expected.ToString() + ", but received: " + output.ToString()); } else { Console.WriteLine("Expected results received."); } } }
public static void DbFunctions_RAND() { DbFunctionTools tools = new DbFunctionTools(); Random rnd = new Random(); { Console.WriteLine("Testing DbFunctions.RAND(Int32)..."); int input = rnd.Next(Int32.MinValue, Int32.MaxValue); List<DbValue> args = new List<DbValue>(); args.Add(tools.AllocValue(input)); DbFunctionArguments fargs = new DbFunctionArguments(args); DbValue valOutput = DbFunctions.RAND(tools, fargs); ByteSlice bs = valOutput.Eval(); double output = tools.GetDouble(bs); Console.WriteLine("Expected results received: {0}", output); } { Console.WriteLine("Testing DbFunctions.RAND()..."); List<DbValue> args = new List<DbValue>(); DbFunctionArguments fargs = new DbFunctionArguments(args); DbValue valOutput = DbFunctions.RAND(tools, fargs); ByteSlice bs = valOutput.Eval(); double output = tools.GetDouble(bs); Console.WriteLine("Expected results received: {0}", output); } }
public static DbValue ADD_MONTHS(DbFunctionTools tools, DbFunctionArguments args) { string FunctionName = "ADD_MONTHS"; args.EnsureCount(FunctionName, 2); DbType arg0type; ByteSlice arg0 = args[0].Eval(out arg0type); if (Types.IsNullValue(arg0)) { return tools.AllocNullValue(); // Give a null, take a null. } if (arg0type.ID != DbTypeID.DATETIME) { args.Expected(FunctionName, 0, "input DATETIME, got " + arg0type.Name.ToUpper()); } DbType arg1type; ByteSlice arg1 = args[1].Eval(out arg1type); if (Types.IsNullValue(arg1)) { return tools.AllocNullValue(); // Give a null, take a null. } if (arg1type.ID != DbTypeID.INT) { args.Expected(FunctionName, 0, "input INT, got " + arg1type.Name.ToUpper()); } DateTime dt = tools.GetDateTime(arg0); int months = tools.GetInt(arg1); dt = dt.AddMonths(months); return tools.AllocValue(dt); }
public static DbValue SPACE(DbFunctionTools tools, DbFunctionArguments args) { string FunctionName = "SPACE"; args.EnsureCount(FunctionName, 1); DbType arg0type; ByteSlice arg0 = args[0].Eval(out arg0type); if (Types.IsNullValue(arg0)) { return tools.AllocNullValue(); // Give a null, take a null. } if (arg0type.ID != DbTypeID.INT) { args.Expected(FunctionName, 0, "input INT, got " + arg0type.Name.ToUpper()); return null; } int len = tools.GetInt(arg0); if (len < 1) { return tools.AllocValue(mstring.Prepare("")); } else { mstring s = mstring.Prepare(); for (int i = 0; i < len; i++) { s = s.AppendM(" "); } return tools.AllocValue(s); } }
public static DbValue 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 void DbFunctions_CHARINDEX() { DbFunctionTools tools = new DbFunctionTools(); //String,Int32. { Console.WriteLine("Testing DbFunctions.CHARINDEX(char(n), char(n)..."); mstring word = mstring.Prepare("apple"); mstring sentence = mstring.Prepare("Red is apple"); List<DbValue> args = new List<DbValue>(); args.Add(tools.AllocValue(word)); args.Add(tools.AllocValue(sentence)); DbFunctionArguments fargs = new DbFunctionArguments(args); DbValue valOutput = DbFunctions.CHARINDEX(tools, fargs); ByteSlice bs = valOutput.Eval(); int output = tools.GetInt(bs); int expected = 7; if (expected != output) { throw new Exception("DbFunctions.CHARINDEX(char(n), char(n) has failed. Expected result: " + expected.ToString() + ", but received: " + output.ToString()); } else { Console.WriteLine("Expected results received."); } } { Console.WriteLine("Testing DbFunctions.CHARINDEX(char(n), char(n), Int32..."); mstring word = mstring.Prepare("apple"); mstring sentence = mstring.Prepare("Red is apple, or more apples."); int startIndex = 8; List<DbValue> args = new List<DbValue>(); args.Add(tools.AllocValue(word)); args.Add(tools.AllocValue(sentence)); args.Add(tools.AllocValue(startIndex)); DbFunctionArguments fargs = new DbFunctionArguments(args); DbValue valOutput = DbFunctions.CHARINDEX(tools, fargs); ByteSlice bs = valOutput.Eval(); int output = tools.GetInt(bs); int expected = 22; if (expected != output) { throw new Exception("DbFunctions.CHARINDEX(char(n), char(n), Int32 has failed. Expected result: " + expected.ToString() + ", but received: " + output.ToString()); } else { Console.WriteLine("Expected results received."); } } }
public static DbValue ROUND(DbFunctionTools tools, DbFunctionArguments args) { string FunctionName = "ROUND"; args.EnsureCount(FunctionName, 2); DbType arg0type; ByteSlice arg0 = args[0].Eval(out arg0type); if (Types.IsNullValue(arg0)) { return tools.AllocNullValue(); // Give a null, take a null. } if (arg0type.ID != DbTypeID.DOUBLE) { args.Expected(FunctionName, 0, "input DOUBLE, got " + arg0type.Name.ToUpper()); } DbType arg1type; ByteSlice arg1 = args[1].Eval(out arg1type); if (arg1type.ID != DbTypeID.INT) { args.Expected(FunctionName, 1, "digits INT, got " + arg1type.Name.ToUpper()); } int digits = tools.GetInt(arg1); double x = tools.GetDouble(arg0); x = Math.Round(x, digits); return tools.AllocValue(x); }
public static void DbFunctions_ADD_MONTHS() { DbFunctionTools tools = new DbFunctionTools(); //String,Int32. { Console.WriteLine("Testing DbFunctions.ADD_MONTHS..."); List<DbValue> args = new List<DbValue>(); DateTime dt = DateTime.Now; int months = 9; args.Add(tools.AllocValue(dt)); args.Add(tools.AllocValue(months)); DbFunctionArguments fargs = new DbFunctionArguments(args); DbValue valOutput = DbFunctions.ADD_MONTHS(tools, fargs); ByteSlice bs = valOutput.Eval(); DateTime output = tools.GetDateTime(bs); DateTime expected = dt.AddMonths(months); if (expected != output) { throw new Exception("DbFunctions.ADD_MONTHS has failed. Expected result: " + expected.ToString() + ", but received: " + output.ToString()); } else { Console.WriteLine("Expected results received."); } } }
public static DbValue NVL2(DbFunctionTools tools, DbFunctionArguments args) { string FunctionName = "NVL2"; args.EnsureCount(FunctionName, 3); DbType arg0type; ByteSlice arg0 = args[0].Eval(out arg0type); DbType arg1type; ByteSlice arg1 = args[1].Eval(out arg1type); DbType arg2type; ByteSlice arg2 = args[2].Eval(out arg2type); if (arg0type.ID != arg1type.ID) { args.Expected(FunctionName, 0, "input " + arg0type.Name.ToString() + ", got " + arg1type.Name.ToUpper()); } if (arg0type.ID != arg2type.ID) { args.Expected(FunctionName, 0, "input " + arg0type.Name.ToString() + ", got " + arg2type.Name.ToUpper()); } if (Types.IsNullValue(arg0)) { return args[2]; } else { return args[1]; } }
public static void DbFunctions_NVL2() { DbFunctionTools tools = new DbFunctionTools(); { Console.WriteLine("Testing DbFunctions.NVL2(DateTime)..."); List<DbValue> args = new List<DbValue>(); DateTime dt = DateTime.Parse("12/1/2000 10:00:00 AM"); DateTime ifnull = DateTime.Parse("12/11/2000 10:00:00 AM"); DateTime notnull = DateTime.Parse("12/14/2000 10:00:00 AM"); args.Add(tools.AllocValue(dt)); args.Add(tools.AllocValue(notnull)); args.Add(tools.AllocValue(ifnull)); DbFunctionArguments fargs = new DbFunctionArguments(args); DbValue valOutput = DbFunctions.NVL2(tools, fargs); ByteSlice bs = valOutput.Eval(); DateTime output = tools.GetDateTime(bs); DateTime expected = notnull; if (expected != output) { throw new Exception("DbFunctions.NVL2(DateTime) has failed. Expected result: " + expected.ToString() + ", but received: " + output.ToString()); } else { Console.WriteLine("Expected results received."); } } { Console.WriteLine("Testing DbFunctions.NVL2(DateTime)..."); List<DbValue> args = new List<DbValue>(); byte[] buf = new byte[9]; buf[0] = 1; //is null DateTime ifnull = DateTime.Parse("12/11/2000 10:00:00 AM"); DateTime notnull = DateTime.Parse("12/14/2000 10:00:00 AM"); args.Add(tools.AllocValue(ByteSlice.Prepare(buf), DbType.Prepare("DateTime", 9))); args.Add(tools.AllocValue(notnull)); args.Add(tools.AllocValue(ifnull)); DbFunctionArguments fargs = new DbFunctionArguments(args); DbValue valOutput = DbFunctions.NVL2(tools, fargs); ByteSlice bs = valOutput.Eval(); DateTime output = tools.GetDateTime(bs); DateTime expected = ifnull; if (expected != output) { throw new Exception("DbFunctions.NVL2(DateTime) has failed. Expected result: " + expected.ToString() + ", but received: " + output.ToString()); } else { Console.WriteLine("Expected results received."); } } }
public static 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); } }
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."); } } }
public static DbValue ISNULL(DbFunctionTools tools, DbFunctionArguments args) { string FunctionName = "ISNULL"; args.EnsureCount(FunctionName, 1); DbType arg0type; ByteSlice arg0 = args[0].Eval(out arg0type); bool bresult = Types.IsNullValue(arg0); return tools.AllocValue(bresult ? 1 : 0); }
public static DbValue LESSEREQUAL(DbFunctionTools tools, DbFunctionArguments args) { DbValue dbvcompare = COMPARE(tools, args); DbType typecompare; ByteSlice bsresult = dbvcompare.Eval(out typecompare); if (DbTypeID.INT != typecompare.ID) { return tools.AllocValue(bsresult, typecompare); } int compare = tools.GetInt(bsresult); return tools.AllocValue(compare <= 0 ? 1 : 0); }
public static DbValue MONTHS_BETWEEN(DbFunctionTools tools, DbFunctionArguments args) { string FunctionName = "MONTHS_BETWEEN"; args.EnsureCount(FunctionName, 2); DbType arg0type; ByteSlice arg0 = args[0].Eval(out arg0type); if (Types.IsNullValue(arg0)) { return tools.AllocNullValue(); // Give a null, take a null. } if (arg0type.ID != DbTypeID.DATETIME) { args.Expected(FunctionName, 0, "input DATETIME, got " + arg0type.Name.ToUpper()); } DbType arg1type; ByteSlice arg1 = args[1].Eval(out arg1type); if (Types.IsNullValue(arg1)) { return tools.AllocNullValue(); // Give a null, take a null. } if (arg1type.ID != DbTypeID.DATETIME) { args.Expected(FunctionName, 0, "input DATETIME, got " + arg1type.Name.ToUpper()); } DateTime dt0 = tools.GetDateTime(arg0); DateTime dt1 = tools.GetDateTime(arg1); int daysInMonth0 = DateTime.DaysInMonth(dt0.Year, dt0.Month); int daysInMonth1 = DateTime.DaysInMonth(dt1.Year, dt1.Month); double btw = 0; if (dt0.Year == dt1.Year && dt0.Month == dt1.Month) //same month and same year { btw = (double)(dt0.Day - dt1.Day) / (double)daysInMonth0; } else if (dt0.Day == daysInMonth0 && dt1.Day == daysInMonth1) //both fall on the last day of their months { btw = 12 * (dt0.Year - dt1.Year) + dt0.Month - dt1.Month; } else { TimeSpan sp = dt0 - dt1; btw = sp.TotalDays / 31d; } return tools.AllocValue(btw); }
public static DbValue MAX(DbFunctionTools tools, DbAggregatorArguments args) { string AggregatorName = "MAX"; DbValue highest = null; DbValue nullest = null; DbFunctionArguments compareargs = new DbFunctionArguments(new DbValue[2]); ImmediateValue argval = null; for (int iarg = 0; iarg < args.Length; iarg++) { args[iarg].EnsureCount(AggregatorName, 1); DbType arg0type; ByteSlice arg0 = args[iarg][0].Eval(out arg0type); if (Types.IsNullValue(arg0)) { if (null == nullest) { nullest = tools.AllocValue(arg0, arg0type); } } else { if (null == argval) { argval = tools.AllocValue(arg0type.ID); } argval.SetValue(arg0); compareargs[0] = argval; compareargs[1] = highest; if (null == highest || tools.GetInt(DbFunctions.COMPARE(tools, compareargs)) > 0) { highest = argval; // Keep this one. argval = null; // New one next time. } } } if (null == highest) { if (null == nullest) { return tools.AllocNullValue(); } return nullest; } return highest; }
public static DbValue 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. } }
public static DbValue REPLACE(DbFunctionTools tools, DbFunctionArguments args) { string FunctionName = "REPLACE"; args.EnsureCount(FunctionName, 3); DbType arg0type; ByteSlice arg0 = args[0].Eval(out arg0type); if (Types.IsNullValue(arg0)) { return tools.AllocNullValue(); // Give a null, take a null. } if (arg0type.ID != DbTypeID.CHARS) { args.Expected(FunctionName, 0, "input CHAR(n), got " + arg0type.Name.ToUpper()); } DbType arg1type; ByteSlice arg1 = args[1].Eval(out arg1type); if (Types.IsNullValue(arg1)) { return tools.AllocNullValue(); // Give a null, take a null. } if (arg1type.ID != DbTypeID.CHARS) { args.Expected(FunctionName, 0, "input CHAR(n), got " + arg1type.Name.ToUpper()); } DbType arg2type; ByteSlice arg2 = args[2].Eval(out arg2type); if (Types.IsNullValue(arg2)) { return tools.AllocNullValue(); // Give a null, take a null. } if (arg2type.ID != DbTypeID.CHARS) { args.Expected(FunctionName, 0, "input CHAR(n), got " + arg2type.Name.ToUpper()); } mstring sentence = tools.GetString(arg0); mstring word = tools.GetString(arg1); mstring replacement = tools.GetString(arg2); sentence = sentence.ReplaceM(ref word, ref replacement); return tools.AllocValue(sentence); }
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); } }
public static DbValue LTRIM(DbFunctionTools tools, DbFunctionArguments args) { string FunctionName = "LTRIM"; args.EnsureCount(FunctionName, 1); DbType arg0type; ByteSlice arg0 = args[0].Eval(out arg0type); if (Types.IsNullValue(arg0)) { return tools.AllocNullValue(); // Give a null, take a null. } if (arg0type.ID != DbTypeID.CHARS) { args.Expected(FunctionName, 0, "input CHAR(n), got " + arg0type.Name.ToUpper()); } string x = tools.GetString(arg0).ToString(); string y = x.TrimStart(); return tools.AllocValue(mstring.Prepare(y)); }
public static DbValue LOWER(DbFunctionTools tools, DbFunctionArguments args) { string FunctionName = "LOWER"; 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 lower = x.ToLowerM(); return tools.AllocValue(lower); }
public static DbValue FLOOR(DbFunctionTools tools, DbFunctionArguments args) { string FunctionName = "FLOOR"; args.EnsureCount(FunctionName, 1); DbType arg0type; ByteSlice arg0 = args[0].Eval(out arg0type); if (Types.IsNullValue(arg0)) { return tools.AllocNullValue(); // Give a null, take a null. } if (arg0type.ID != DbTypeID.DOUBLE) { args.Expected(FunctionName, 0, "input DOUBLE, got " + arg0type.Name.ToUpper()); } double x = tools.GetDouble(arg0); x = Math.Floor(x); return tools.AllocValue(x); }
public static DbValue ISLIKE(DbFunctionTools tools, DbFunctionArguments args) { string FunctionName = "ISLIKE"; args.EnsureCount(FunctionName, 2); int ismatch = 0; DbType arg0type; ByteSlice arg0 = args[0].Eval(out arg0type); if (Types.IsNullValue(arg0)) { return tools.AllocValue(ismatch); } if (arg0type.ID != DbTypeID.CHARS) { args.Expected(FunctionName, 0, "input CHAR(n), got " + arg0type.Name.ToUpper()); } DbType arg1type; ByteSlice arg1 = args[1].Eval(out arg1type); if (arg1type.ID != DbTypeID.CHARS) { args.Expected(FunctionName, 1, "pattern CHAR(n), got " + arg1type.Name.ToUpper()); } string x = tools.GetString(arg0).ToString(); string y = tools.GetString(arg1).ToString(); string pattern = GetRegexString(y); if (pattern != null) { System.Text.RegularExpressions.Regex regx = new System.Text.RegularExpressions.Regex(pattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase); if(regx.IsMatch(x)) { ismatch = 1; } } return tools.AllocValue(ismatch); }
public static DbValue PATINDEX(DbFunctionTools tools, DbFunctionArguments args) { string FunctionName = "PATINDEX"; args.EnsureCount(FunctionName, 2); DbType arg0type; ByteSlice arg0 = args[0].Eval(out arg0type); DbType arg1type; ByteSlice arg1 = args[1].Eval(out arg1type); if (Types.IsNullValue(arg0)) { return tools.AllocNullValue(); // Give a null, take a null. } if (arg0type.ID != DbTypeID.CHARS) { args.Expected(FunctionName, 0, "input CHAR(n), got " + arg0type.Name.ToUpper()); } if (arg1type.ID != DbTypeID.CHARS) { args.Expected(FunctionName, 0, "input CHAR(n), got " + arg1type.Name.ToUpper()); } string pat = tools.GetString(arg0).ToString(); string str = tools.GetString(arg1).ToString(); pat = pat.Trim('%'); string rpat = GetRegexString(pat); System.Text.RegularExpressions.Regex regx = new System.Text.RegularExpressions.Regex(rpat); System.Text.RegularExpressions.Match match = regx.Match(str); int indx = -1; if (match != null) { indx = match.Index; } return tools.AllocValue(indx); }
public static DbValue LAST_DAY(DbFunctionTools tools, DbFunctionArguments args) { string FunctionName = "LAST_DAY"; args.EnsureCount(FunctionName, 1); DbType arg0type; ByteSlice arg0 = args[0].Eval(out arg0type); if (Types.IsNullValue(arg0)) { return tools.AllocNullValue(); // Give a null, take a null. } if (arg0type.ID != DbTypeID.DATETIME) { args.Expected(FunctionName, 0, "input DATETIME, got " + arg0type.Name.ToUpper()); } DateTime dt = tools.GetDateTime(arg0); dt = dt.AddMonths(1); dt = dt.AddDays(-1); return tools.AllocValue(dt); }
public static DbValue FORMAT(DbFunctionTools tools, DbFunctionArguments args) { string FunctionName = "FORMAT"; args.EnsureCount(FunctionName, 2); DbType arg0type; ByteSlice arg0 = args[0].Eval(out arg0type); DbType arg1type; ByteSlice arg1 = args[1].Eval(out arg1type); if (Types.IsNullValue(arg0)) { return tools.AllocNullValue(); // Give a null, take a null. } if (arg0type.ID != DbTypeID.CHARS) { args.Expected(FunctionName, 0, "input CHAR(n), got " + arg0type.Name.ToUpper()); } if (Types.IsNullValue(arg1)) { return tools.AllocNullValue(); // Give a null, take a null. } if (arg1type.ID != DbTypeID.DATETIME) { args.Expected(FunctionName, 0, "input DATETIME, got " + arg1type.Name.ToUpper()); } string formatstr = tools.GetString(arg0).ToString(); DateTime dt = tools.GetDateTime(arg1); mstring result = mstring.Prepare(dt.ToString(formatstr)); while (result.Length < 80) { result.MAppend('\0'); } return tools.AllocValue(result); }
public static DbValue RPAD(DbFunctionTools tools, DbFunctionArguments args) { return(_PAD("RPAD", tools, args, false)); }
public static DbValue SYSDATE(DbFunctionTools tools, DbFunctionArguments args) { return(tools.AllocValue(DateTime.Now)); }
public static DbValue DIV(DbFunctionTools tools, DbFunctionArguments args) { string FunctionName = "DIV"; 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. } }
public static DbValue PI(DbFunctionTools tools, DbFunctionArguments args) { double x = Math.PI; return(tools.AllocValue(x)); }
// 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); }
DbValue IValueContext.ExecDbFunction(string name, DbFunctionArguments args) { return(DbExec.ExecDbScalarFunction(name, functools, args)); }