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 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 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 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 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_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."); } } }
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(); }
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)); }
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()); }
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 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 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 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 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 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 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 LEN(DbFunctionTools tools, DbFunctionArguments args) { string FunctionName = "LEN"; args.EnsureCount(FunctionName, 1); DbType arg0type; ByteSlice arg0 = args[0].Eval(out arg0type); if (Types.IsNullValue(arg0)) { return(tools.AllocNullValue()); // Give a null, take a null. } if (arg0type.ID != DbTypeID.CHARS) { args.Expected(FunctionName, 0, "input CHAR(n), got " + arg0type.Name.ToUpper()); } mstring x = tools.GetString(arg0); int len = x.Length; return(tools.AllocValue(len)); }
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 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 void DbFunctions_FORMAT() { DbFunctionTools tools = new DbFunctionTools(); { Console.WriteLine("Testing DbFunctions.FORMAT('MM')..."); List<DbValue> args = new List<DbValue>(); mstring formatstr = mstring.Prepare("MM"); args.Add(tools.AllocValue(formatstr)); DateTime dt = new DateTime(2000, 9, 14, 12, 0, 0); args.Add(tools.AllocValue(dt)); DbFunctionArguments fargs = new DbFunctionArguments(args); DbValue valOutput = DbFunctions.FORMAT(tools, fargs); ByteSlice bs = valOutput.Eval(); mstring output = tools.GetString(bs); mstring expected = mstring.Prepare(dt.ToString(formatstr.ToString())); if (expected != output) { throw new Exception("DbFunctions.FORMAT('MM') has failed. Expected result: " + expected.ToString() + ", but received: " + output.ToString()); } else { Console.WriteLine("Expected results received."); } } { Console.WriteLine("Testing DbFunctions.FORMAT('dd')..."); List<DbValue> args = new List<DbValue>(); mstring formatstr = mstring.Prepare("dd"); args.Add(tools.AllocValue(formatstr)); DateTime dt = new DateTime(2000, 9, 14, 12, 0, 0); args.Add(tools.AllocValue(dt)); DbFunctionArguments fargs = new DbFunctionArguments(args); DbValue valOutput = DbFunctions.FORMAT(tools, fargs); ByteSlice bs = valOutput.Eval(); mstring output = tools.GetString(bs); mstring expected = mstring.Prepare(dt.ToString(formatstr.ToString())); if (expected != output) { throw new Exception("DbFunctions.FORMAT('dd') has failed. Expected result: " + expected.ToString() + ", but received: " + output.ToString()); } else { Console.WriteLine("Expected results received."); } } { Console.WriteLine("Testing DbFunctions.FORMAT('t')..."); List<DbValue> args = new List<DbValue>(); mstring formatstr = mstring.Prepare("t"); args.Add(tools.AllocValue(formatstr)); DateTime dt = new DateTime(2000, 9, 14, 12, 0, 0); args.Add(tools.AllocValue(dt)); DbFunctionArguments fargs = new DbFunctionArguments(args); DbValue valOutput = DbFunctions.FORMAT(tools, fargs); ByteSlice bs = valOutput.Eval(); mstring output = tools.GetString(bs); mstring expected = mstring.Prepare(dt.ToString(formatstr.ToString())); if (expected != output) { throw new Exception("DbFunctions.FORMAT('t') has failed. Expected result: " + expected.ToString() + ", but received: " + output.ToString()); } else { Console.WriteLine("Expected results received."); } } }
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); } }
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)); }
public static void DbFunctions_IIF() { DbFunctionTools tools = new DbFunctionTools(); Random rnd = new Random(); // int { Console.WriteLine("Testing DbFunctions.IIF(1, 'First', 'Second')..."); List<DbValue> args = new List<DbValue>(); args.Add(tools.AllocValue(1)); args.Add(tools.AllocValue(mstring.Prepare("First"))); args.Add(tools.AllocValue(mstring.Prepare("Second"))); DbFunctionArguments fargs = new DbFunctionArguments(args); DbValue valOutput = DbFunctions.IIF(tools, fargs); ByteSlice bs = valOutput.Eval(); mstring output = tools.GetString(bs); mstring expected = mstring.Prepare("First"); if (expected != output) { throw new Exception("DbFunctions.IIF(1, 'First', 'Second') has failed. Expected result: " + expected.ToString() + ", but received: " + output.ToString()); } else { Console.WriteLine("Expected results received."); } } { Console.WriteLine("Testing DbFunctions.IIF(0, 'First', 'Second')..."); List<DbValue> args = new List<DbValue>(); args.Add(tools.AllocValue(0)); args.Add(tools.AllocValue(mstring.Prepare("First"))); args.Add(tools.AllocValue(mstring.Prepare("Second"))); DbFunctionArguments fargs = new DbFunctionArguments(args); DbValue valOutput = DbFunctions.IIF(tools, fargs); ByteSlice bs = valOutput.Eval(); mstring output = tools.GetString(bs); mstring expected = mstring.Prepare("Second"); if (expected != output) { throw new Exception("DbFunctions.IIF(0, 'First', 'Second') has failed. Expected result: " + expected.ToString() + ", but received: " + output.ToString()); } else { Console.WriteLine("Expected results received."); } } { Console.WriteLine("Testing DbFunctions.IIF(42, 'First', 'Second')..."); List<DbValue> args = new List<DbValue>(); args.Add(tools.AllocValue(42)); args.Add(tools.AllocValue(mstring.Prepare("First"))); args.Add(tools.AllocValue(mstring.Prepare("Second"))); DbFunctionArguments fargs = new DbFunctionArguments(args); DbValue valOutput = DbFunctions.IIF(tools, fargs); ByteSlice bs = valOutput.Eval(); mstring output = tools.GetString(bs); mstring expected = mstring.Prepare("First"); if (expected != output) { throw new Exception("DbFunctions.IIF(42, 'First', 'Second') has failed. Expected result: " + expected.ToString() + ", but received: " + output.ToString()); } else { Console.WriteLine("Expected results received."); } } // NULL { Console.WriteLine("Testing DbFunctions.IIF(NULL, 'First', 'Second')..."); List<DbValue> args = new List<DbValue>(); args.Add(tools.AllocNullValue()); args.Add(tools.AllocValue(mstring.Prepare("First"))); args.Add(tools.AllocValue(mstring.Prepare("Second"))); DbFunctionArguments fargs = new DbFunctionArguments(args); DbValue valOutput = DbFunctions.IIF(tools, fargs); ByteSlice bs = valOutput.Eval(); mstring output = tools.GetString(bs); mstring expected = mstring.Prepare("Second"); if (expected != output) { throw new Exception("DbFunctions.IIF(NULL, 'First', 'Second') has failed. Expected result: " + expected.ToString() + ", but received: " + output.ToString()); } else { Console.WriteLine("Expected results received."); } } }
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."); } } }
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)); }
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 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); }
public static DbValue DATEDIFF(DbFunctionTools tools, DbFunctionArguments args) { string FunctionName = "DATEDIFF"; 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.DATETIME) { args.Expected(FunctionName, 0, "input DATETIME, 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(); DateTime startdate = tools.GetDateTime(arg1); DateTime enddate = tools.GetDateTime(arg2); double partdiff = 0; switch (datepart) { case "YEAR": case "YY": case "YYYY": partdiff = enddate.Year - startdate.Year; break; case "QUARTER": case "QQ": case "Q": partdiff = GetMonthDiff(startdate.Year, startdate.Month, enddate.Year, enddate.Month) / 3; break; case "MONTH": case "MM": case "M": partdiff = GetMonthDiff(startdate.Year, startdate.Month, enddate.Year, enddate.Month); break; case "DAY": case "DD": case "D": { DateTime sdate = new DateTime(startdate.Year, startdate.Month, startdate.Day); DateTime edate = new DateTime(enddate.Year, enddate.Month, enddate.Day); TimeSpan sp = edate - sdate; partdiff = sp.TotalDays; } break; case "WEEK": case "WK": case "WW": { DateTime sdate = new DateTime(startdate.Year, startdate.Month, startdate.Day); DateTime edate = new DateTime(enddate.Year, enddate.Month, enddate.Day); TimeSpan sp = edate - sdate; partdiff = (int)sp.TotalDays / 7; } break; case "HOUR": case "HH": { DateTime sdate = new DateTime(startdate.Year, startdate.Month, startdate.Day, startdate.Hour, 0, 0); DateTime edate = new DateTime(enddate.Year, enddate.Month, enddate.Day, enddate.Hour, 0, 0); TimeSpan sp = edate - sdate; partdiff = sp.TotalHours; } break; case "MINUTE": case "MI": case "N": { DateTime sdate = new DateTime(startdate.Year, startdate.Month, startdate.Day, startdate.Hour, startdate.Minute, 0); DateTime edate = new DateTime(enddate.Year, enddate.Month, enddate.Day, enddate.Hour, enddate.Minute, 0); TimeSpan sp = edate - sdate; partdiff = sp.TotalMinutes; } break; case "SECOND": case "SS": case "S": { DateTime sdate = new DateTime(startdate.Year, startdate.Month, startdate.Day, startdate.Hour, startdate.Minute, startdate.Second); DateTime edate = new DateTime(enddate.Year, enddate.Month, enddate.Day, enddate.Hour, enddate.Minute, enddate.Second); TimeSpan sp = edate - sdate; partdiff = sp.TotalSeconds; } break; case "MILLISECOND": case "MS": { TimeSpan sp = enddate - startdate; partdiff = sp.TotalMilliseconds; } break; default: args.Expected(FunctionName, 0, "input datepart invalid"); return(null); } return(tools.AllocValue(partdiff)); }
public static DbValue DATEDIFF(DbFunctionTools tools, DbFunctionArguments args) { string FunctionName = "DATEDIFF"; 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.DATETIME) { args.Expected(FunctionName, 0, "input DATETIME, 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(); DateTime startdate = tools.GetDateTime(arg1); DateTime enddate = tools.GetDateTime(arg2); double partdiff = 0; switch (datepart) { case "YEAR": case "YY": case "YYYY": partdiff = enddate.Year - startdate.Year; break; case "QUARTER": case "QQ": case "Q": partdiff = GetMonthDiff(startdate.Year, startdate.Month, enddate.Year, enddate.Month) / 3; break; case "MONTH": case "MM": case "M": partdiff = GetMonthDiff(startdate.Year, startdate.Month, enddate.Year, enddate.Month); break; case "DAY": case "DD": case "D": { DateTime sdate = new DateTime(startdate.Year, startdate.Month, startdate.Day); DateTime edate = new DateTime(enddate.Year, enddate.Month, enddate.Day); TimeSpan sp = edate - sdate; partdiff = sp.TotalDays; } break; case "WEEK": case "WK": case "WW": { DateTime sdate = new DateTime(startdate.Year, startdate.Month, startdate.Day); DateTime edate = new DateTime(enddate.Year, enddate.Month, enddate.Day); TimeSpan sp = edate - sdate; partdiff = (int)sp.TotalDays / 7; } break; case "HOUR": case "HH": { DateTime sdate = new DateTime(startdate.Year, startdate.Month, startdate.Day, startdate.Hour, 0, 0); DateTime edate = new DateTime(enddate.Year, enddate.Month, enddate.Day, enddate.Hour, 0, 0); TimeSpan sp = edate - sdate; partdiff = sp.TotalHours; } break; case "MINUTE": case "MI": case "N": { DateTime sdate = new DateTime(startdate.Year, startdate.Month, startdate.Day, startdate.Hour, startdate.Minute, 0); DateTime edate = new DateTime(enddate.Year, enddate.Month, enddate.Day, enddate.Hour, enddate.Minute, 0); TimeSpan sp = edate - sdate; partdiff = sp.TotalMinutes; } break; case "SECOND": case "SS": case "S": { DateTime sdate = new DateTime(startdate.Year, startdate.Month, startdate.Day, startdate.Hour, startdate.Minute, startdate.Second); DateTime edate = new DateTime(enddate.Year, enddate.Month, enddate.Day, enddate.Hour, enddate.Minute, enddate.Second); TimeSpan sp = edate - sdate; partdiff = sp.TotalSeconds; } break; case "MILLISECOND": case "MS": { TimeSpan sp = enddate - startdate; partdiff = sp.TotalMilliseconds; } break; default: args.Expected(FunctionName, 0, "input datepart invalid"); return null; } return tools.AllocValue(partdiff); }
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)); }
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); }
public static void DbFunctions_RPAD() { DbFunctionTools tools = new DbFunctionTools(); { Console.WriteLine("Testing DbFunctions.RPAD(String,Int32, String)..."); List<DbValue> args = new List<DbValue>(); args.Add(tools.AllocValue(mstring.Prepare("hello"))); int len = 11; args.Add(tools.AllocValue(len)); args.Add(tools.AllocValue(mstring.Prepare("xy"))); DbFunctionArguments fargs = new DbFunctionArguments(args); DbValue valOutput = DbFunctions.RPAD(tools, fargs); ByteSlice bs = valOutput.Eval(); mstring output = tools.GetString(bs); mstring expected = mstring.Prepare("helloxyxyxy"); if (expected != output) { throw new Exception("DbFunctions.RPAD(String,Int32, String) has failed. Expected result: " + expected.ToString() + ", but received: " + output.ToString()); } else { Console.WriteLine("Expected results received."); } } { Console.WriteLine("Testing DbFunctions.RPAD(String,Int32, String)..."); List<DbValue> args = new List<DbValue>(); args.Add(tools.AllocValue(mstring.Prepare("hello"))); int len = 10; args.Add(tools.AllocValue(len)); args.Add(tools.AllocValue(mstring.Prepare("xy"))); DbFunctionArguments fargs = new DbFunctionArguments(args); DbValue valOutput = DbFunctions.RPAD(tools, fargs); ByteSlice bs = valOutput.Eval(); mstring output = tools.GetString(bs); mstring expected = mstring.Prepare("helloxyxyx"); if (expected != output) { throw new Exception("DbFunctions.RPAD(String,Int32, String) has failed. Expected result: " + expected.ToString() + ", but received: " + output.ToString()); } else { Console.WriteLine("Expected results received."); } } { Console.WriteLine("Testing DbFunctions.RPAD(String,Int32, String)..."); List<DbValue> args = new List<DbValue>(); args.Add(tools.AllocValue(mstring.Prepare("hello"))); int len = 5; args.Add(tools.AllocValue(len)); args.Add(tools.AllocValue(mstring.Prepare("xy"))); DbFunctionArguments fargs = new DbFunctionArguments(args); DbValue valOutput = DbFunctions.RPAD(tools, fargs); ByteSlice bs = valOutput.Eval(); mstring output = tools.GetString(bs); mstring expected = mstring.Prepare("hello"); if (expected != output) { throw new Exception("DbFunctions.RPAD(String,Int32, String) has failed. Expected result: " + expected.ToString() + ", but received: " + output.ToString()); } else { Console.WriteLine("Expected results received."); } } { Console.WriteLine("Testing DbFunctions.RPAD(String,Int32, String)..."); List<DbValue> args = new List<DbValue>(); args.Add(tools.AllocValue(mstring.Prepare("hello"))); int len = 3; args.Add(tools.AllocValue(len)); args.Add(tools.AllocValue(mstring.Prepare("xy"))); DbFunctionArguments fargs = new DbFunctionArguments(args); DbValue valOutput = DbFunctions.RPAD(tools, fargs); ByteSlice bs = valOutput.Eval(); mstring output = tools.GetString(bs); mstring expected = mstring.Prepare("hel"); if (expected != output) { throw new Exception("DbFunctions.RPAD(String,Int32, String) 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 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); }
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); }
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); } }
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."); } } }
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)); }