public static DbValue AVG(DbFunctionTools tools, DbAggregatorArguments args) { string AggregatorName = "AVG"; double sum = 0; int count = 0; for (int iarg = 0; iarg < args.Length; iarg++) { args[iarg].EnsureCount(AggregatorName, 1); DbType arg0type; ByteSlice arg0 = args[iarg][0].Eval(out arg0type); if (!Types.IsNullValue(arg0)) //ignore null { count++; switch (arg0type.ID) { case DbTypeID.INT: sum += (double)tools.GetInt(arg0); break; case DbTypeID.LONG: sum += (double)tools.GetLong(arg0); break; case DbTypeID.DOUBLE: sum += tools.GetDouble(arg0); break; default: args[iarg].Expected(AggregatorName, 0, "input INT, LONG or DOUBLE, got " + arg0type.Name.ToUpper()); return(null); // Doesn't reach here. } } } double avg = 0; if (count > 0) { avg = sum / (double)count; } return(tools.AllocValue(avg)); }
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)); }
private static byte[] bitop(mstring[] values, int whatop) { byte[] buf = null; byte[] result = null; for (int i = 0; i < values.Length; i++) { if (i == 0) { ByteSlice bs = values[i].ToByteSliceUTF16(); result = bs.ToBytes(); continue; } { ByteSlice bs = values[i].ToByteSliceUTF16(); buf = bs.ToBytes(); } bitop(result, buf, whatop); } return(result); }
void _SysIndexes(System.IO.Stream output) { const int ColLineChars = 1000; int ColLineLength = 1 + (ColLineChars * 2); int ip = TableName.IndexOf('('); if (-1 == ip || TableName[TableName.Length - 1] != ')') { return; } string indexdir = QlArgsUnescape(TableName.Substring(ip + 1, TableName.Length - ip - 1 - 1)); System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(indexdir); System.IO.FileInfo[] files = dir.GetFiles("ind.Index.*.ind"); foreach (System.IO.FileInfo file in files) { string[] parts = file.Name.Split('.'); string indexname = parts[2]; recordset rs = recordset.Prepare(); rs.PutByte(0); // Nullable byte. rs.PutString(indexname); { ByteSlice bs = rs.ToByteSlice(); if (bs.Length > ColLineLength) { throw new Exception("Column Line: length is too long: " + indexname); } for (int ibs = 0; ibs < bs.Length; ibs++) { output.WriteByte(bs[ibs]); } for (int ibs = bs.Length; ibs < DSpace_OutputRecordLength; ibs++) { output.WriteByte(0); } } } }
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 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 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 NEXT_DAY(DbFunctionTools tools, DbFunctionArguments args) { string FunctionName = "NEXT_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.AddDays(1); return(tools.AllocValue(dt)); }
public static DbValue COUNTDISTINCT(DbFunctionTools tools, DbAggregatorArguments args) { string AggregatorName = "COUNTDISTINCT"; Dictionary <ByteSlice, short> dict = new Dictionary <ByteSlice, short>(new ByteSliceEqualityComparer()); for (int iarg = 0; iarg < args.Length; iarg++) { args[iarg].EnsureCount(AggregatorName, 1); DbType arg0type; ByteSlice arg0 = args[iarg][0].Eval(out arg0type); if (Types.IsNullValue(arg0)) //ignore null { continue; } if (!dict.ContainsKey(arg0)) { dict.Add(arg0, 0); } } return(tools.AllocValue((long)dict.Count)); }
public static DbValue CEILING(DbFunctionTools tools, DbFunctionArguments args) { string FunctionName = "CEILING"; 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.Ceiling(x); return(tools.AllocValue(x)); }
public static void DbFunctions_ROUND() { DbFunctionTools tools = new DbFunctionTools(); Random rnd = new Random(); //Double. { Console.WriteLine("Testing DbFunctions.ROUND(Double, Int32)..."); double input = rnd.NextDouble(); if (input < 0.5) { input = input * -1d; } List <DbValue> args = new List <DbValue>(); args.Add(tools.AllocValue(input)); args.Add(tools.AllocValue(3)); DbFunctionArguments fargs = new DbFunctionArguments(args); DbValue valOutput = DbFunctions.ROUND(tools, fargs); ByteSlice bs = valOutput.Eval(); double output = tools.GetDouble(bs); double expected = Math.Round(input, 3); if (expected != output) { throw new Exception("DbFunctions.ROUND(Double, Int32) has failed. Expected result: " + expected.ToString() + ", but received: " + output.ToString()); } else { Console.WriteLine("Expected results received."); } } }
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 RAND(DbFunctionTools tools, DbFunctionArguments args) { string FunctionName = "RAND"; if (args.Length == 1) { DbType arg0type; ByteSlice arg0 = args[0].Eval(out arg0type); if (Types.IsNullValue(arg0)) { if (null == _rnd) { _rnd = new Random(); } } else { if (arg0type.ID != DbTypeID.INT) { args.Expected(FunctionName, 0, "input INT, got " + arg0type.Name.ToUpper()); } int seed = tools.GetInt(arg0); _rnd = new Random(seed); } } else { if (null == _rnd) { _rnd = new Random(); } } return(tools.AllocValue(_rnd.NextDouble())); }
public static DbValue DATEPART_HOUR(DbFunctionTools tools, DbFunctionArguments args) { string FunctionName = "DATEPART_HOUR"; 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()); return(null); // Doesn't reach here. } DateTime dt = tools.GetDateTime(arg0); return(tools.AllocValue(dt.Hour)); }
public static void DbFunctions_PI() { DbFunctionTools tools = new DbFunctionTools(); { Console.WriteLine("Testing DbFunctions.PI()..."); DbFunctionArguments fargs = new DbFunctionArguments(); DbValue valOutput = DbFunctions.PI(tools, fargs); ByteSlice bs = valOutput.Eval(); double output = tools.GetDouble(bs); double expected = Math.PI; if (expected != output) { throw new Exception("DbFunctions.PI() has failed. Expected result: " + expected.ToString() + ", but received: " + output.ToString()); } else { Console.WriteLine("Expected results received."); } } }
public static DbValue NVL(DbFunctionTools tools, DbFunctionArguments args) { string FunctionName = "NVL"; args.EnsureCount(FunctionName, 2); DbType arg0type; ByteSlice arg0 = args[0].Eval(out arg0type); DbType arg1type; ByteSlice arg1 = args[1].Eval(out arg1type); if (arg0type.ID != arg1type.ID) { args.Expected(FunctionName, 0, "input " + arg0type.Name.ToString() + ", got " + arg1type.Name.ToUpper()); } if (Types.IsNullValue(arg0)) { return(args[1]); } else { return(args[0]); } }
public static ByteSlice LiteralToValueBuffer(string literal, out DbTypeID type, List <byte> buffer) { if (literal.Length > 0) { if ('\'' == literal[0]) { if ('\'' != literal[literal.Length - 1]) { throw new Exception("Expected closing quote in string literal"); } if (null == buffer) { buffer = new List <byte>(1 + (literal.Length - 2) * 2); } buffer.Clear(); buffer.Add(0); for (int i = 1; i <= literal.Length - 2; i++) { int ich = literal[i]; //ConvertCodeValueToBytesUTF16 buffer.Add((byte)ich); buffer.Add((byte)(ich >> 8)); if ('\'' == ich) { i++; // Skip escape quote. #if DEBUG if ('\'' != literal[i]) { throw new Exception("DEBUG: ('\'' != literal[i])"); } #endif } } type = DbTypeID.CHARS; #if DEBUG { DbTypeID _xtype; int ltvi = LiteralToValueInfo(literal, out _xtype); if (buffer.Count != ltvi) { #if DEBUG System.Diagnostics.Debugger.Launch(); #endif throw new Exception("DEBUG: LiteralToValue: (buffer.Count{" + buffer.Count + "} != LiteralToValueInfo(literal, out _xtype)){" + ltvi + "}"); } } #endif return(ByteSlice.Prepare(buffer)); } else if (0 == string.Compare("NULL", literal, true)) { type = DbTypeID.NULL; if (null == buffer) { buffer = new List <byte>(1); } buffer.Clear(); buffer.Add(1); // Nullable, IsNull=true. return(ByteSlice.Prepare(buffer)); } else // Assumed numeric. { try { return(ByteSlice.Prepare(NumberLiteralToBestTypeBuffer(literal, out type, buffer))); } catch { // Continue on to null on exception. } } } type = DbTypeID.NULL; if (buffer == null) { buffer = new List <byte>(0); } buffer.Clear(); return(ByteSlice.Prepare(buffer)); }
public static void DbFunctions_DATEPART() { DbFunctionTools tools = new DbFunctionTools(); DateTime input = new DateTime(1999, 3, 14, 13, 1, 33); DbValue valInput = tools.AllocValue(input); List <DbValue> args = new List <DbValue>(); args.Add(valInput); DbFunctionArguments fargs = new DbFunctionArguments(args); { Console.WriteLine("Testing DbFunctions.DATEPART_YEAR..."); DbValue valOutput = DbFunctions.DATEPART_YEAR(tools, fargs); ByteSlice bs = valOutput.Eval(); int output = tools.GetInt(bs); int expected = input.Year; if (expected != output) { throw new Exception("DbFunctions.DATEPART_YEAR has failed. Expected result: " + expected.ToString() + ", but received: " + output.ToString()); } else { Console.WriteLine("Expected results received."); } } { Console.WriteLine("Testing DbFunctions.DATEPART_MONTH..."); DbValue valOutput = DbFunctions.DATEPART_MONTH(tools, fargs); ByteSlice bs = valOutput.Eval(); int output = tools.GetInt(bs); int expected = input.Month; if (expected != output) { throw new Exception("DbFunctions.DATEPART_MONTH has failed. Expected result: " + expected.ToString() + ", but received: " + output.ToString()); } else { Console.WriteLine("Expected results received."); } } { Console.WriteLine("Testing DbFunctions.DATEPART_DAY..."); DbValue valOutput = DbFunctions.DATEPART_DAY(tools, fargs); ByteSlice bs = valOutput.Eval(); int output = tools.GetInt(bs); int expected = input.Day; if (expected != output) { throw new Exception("DbFunctions.DATEPART_DAY has failed. Expected result: " + expected.ToString() + ", but received: " + output.ToString()); } else { Console.WriteLine("Expected results received."); } } { Console.WriteLine("Testing DbFunctions.DATEPART_HOUR..."); DbValue valOutput = DbFunctions.DATEPART_HOUR(tools, fargs); ByteSlice bs = valOutput.Eval(); int output = tools.GetInt(bs); int expected = input.Hour; if (expected != output) { throw new Exception("DbFunctions.DATEPART_HOUR has failed. Expected result: " + expected.ToString() + ", but received: " + output.ToString()); } else { Console.WriteLine("Expected results received."); } } { Console.WriteLine("Testing DbFunctions.DATEPART_MINUTE..."); DbValue valOutput = DbFunctions.DATEPART_MINUTE(tools, fargs); ByteSlice bs = valOutput.Eval(); int output = tools.GetInt(bs); int expected = input.Minute; if (expected != output) { throw new Exception("DbFunctions.DATEPART_MINUTE has failed. Expected result: " + expected.ToString() + ", but received: " + output.ToString()); } else { Console.WriteLine("Expected results received."); } } { Console.WriteLine("Testing DbFunctions.DATEPART_SECOND..."); DbValue valOutput = DbFunctions.DATEPART_SECOND(tools, fargs); ByteSlice bs = valOutput.Eval(); int output = tools.GetInt(bs); int expected = input.Second; if (expected != output) { throw new Exception("DbFunctions.DATEPART_SECOND has failed. Expected result: " + expected.ToString() + ", but received: " + output.ToString()); } else { Console.WriteLine("Expected results received."); } } }
public static void DbAggregators_LAST() { DbFunctionTools tools = new DbFunctionTools(); Random rnd = new Random(); const int rowcount = 20; //Double. { Console.WriteLine("Testing DbAggregators_LAST(Double)..."); DbFunctionArguments[] fargs = new DbFunctionArguments[rowcount]; double expected = 0; for (int i = 0; i < fargs.Length; i++) { double input = rnd.NextDouble(); if (input < 0.5) { input = input * -1d; } if (i == fargs.Length - 1) { expected = input; } List <DbValue> args = new List <DbValue>(); args.Add(tools.AllocValue(input)); fargs[i] = new DbFunctionArguments(args); } DbValue valOutput = DbAggregators.LAST(tools, new DbAggregatorArguments(fargs)); ByteSlice bs = valOutput.Eval(); double output = tools.GetDouble(bs); if (expected != output) { throw new Exception("DbAggregators_LAST(Double) has failed. Expected result: " + expected.ToString() + ", but received: " + output.ToString()); } else { Console.WriteLine("Expected results received."); } } { Console.WriteLine("Testing DbAggregators_LAST(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 (i == fargs.Length - 1) { expected = input; } List <DbValue> args = new List <DbValue>(); args.Add(tools.AllocValue(input)); fargs[i] = new DbFunctionArguments(args); } DbValue valOutput = DbAggregators.LAST(tools, new DbAggregatorArguments(fargs)); ByteSlice bs = valOutput.Eval(); int output = tools.GetInt(bs); if (expected != output) { throw new Exception("DbAggregators_LAST(Int32) has failed. Expected result: " + expected.ToString() + ", but received: " + output.ToString()); } else { Console.WriteLine("Expected results received."); } } { Console.WriteLine("Testing DbAggregators_LAST(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 (i == fargs.Length - 1) { expected = input; } List <DbValue> args = new List <DbValue>(); args.Add(tools.AllocValue(input)); fargs[i] = new DbFunctionArguments(args); } DbValue valOutput = DbAggregators.LAST(tools, new DbAggregatorArguments(fargs)); ByteSlice bs = valOutput.Eval(); long output = tools.GetLong(bs); if (expected != output) { throw new Exception("DbAggregators_LAST(Int64) has failed. Expected result: " + expected.ToString() + ", but received: " + output.ToString()); } else { Console.WriteLine("Expected results received."); } } { Console.WriteLine("Testing DbAggregators_LAST(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 (i == fargs.Length - 1) { expected = input.ToString(); } List <DbValue> args = new List <DbValue>(); args.Add(tools.AllocValue(input)); fargs[i] = new DbFunctionArguments(args); } DbValue valOutput = DbAggregators.LAST(tools, new DbAggregatorArguments(fargs)); ByteSlice bs = valOutput.Eval(); mstring output = tools.GetString(bs); if (expected != output.ToString()) { throw new Exception("DbAggregators_LAST(char(n)) has failed. Expected result: " + expected.ToString() + ", but received: " + output.ToString()); } else { Console.WriteLine("Expected results received."); } } }
public static void DbFunctions_DATEADD() { DbFunctionTools tools = new DbFunctionTools(); { Console.WriteLine("Testing DbFunctions.DATEADD(year)..."); List <DbValue> args = new List <DbValue>(); mstring datepart = mstring.Prepare("year"); args.Add(tools.AllocValue(datepart)); int number = -10; args.Add(tools.AllocValue(number)); DateTime dt = new DateTime(2000, 9, 14, 12, 0, 0); args.Add(tools.AllocValue(dt)); DbFunctionArguments fargs = new DbFunctionArguments(args); DbValue valOutput = DbFunctions.DATEADD(tools, fargs); ByteSlice bs = valOutput.Eval(); DateTime output = tools.GetDateTime(bs); DateTime expected = dt.AddYears(number); if (expected != output) { throw new Exception("DbFunctions.DATEADD(year) has failed. Expected result: " + expected.ToString() + ", but received: " + output.ToString()); } else { Console.WriteLine("Expected results received."); } } { Console.WriteLine("Testing DbFunctions.DATEADD(quarter)..."); List <DbValue> args = new List <DbValue>(); mstring datepart = mstring.Prepare("qq"); args.Add(tools.AllocValue(datepart)); int number = 5; args.Add(tools.AllocValue(number)); DateTime dt = new DateTime(2000, 9, 14, 12, 0, 0); args.Add(tools.AllocValue(dt)); DbFunctionArguments fargs = new DbFunctionArguments(args); DbValue valOutput = DbFunctions.DATEADD(tools, fargs); ByteSlice bs = valOutput.Eval(); DateTime output = tools.GetDateTime(bs); DateTime expected = dt.AddMonths(number * 3); if (expected != output) { throw new Exception("DbFunctions.DATEADD(quarter) has failed. Expected result: " + expected.ToString() + ", but received: " + output.ToString()); } else { Console.WriteLine("Expected results received."); } } { Console.WriteLine("Testing DbFunctions.DATEADD(month)..."); List <DbValue> args = new List <DbValue>(); mstring datepart = mstring.Prepare("m"); args.Add(tools.AllocValue(datepart)); int number = 10; args.Add(tools.AllocValue(number)); DateTime dt = new DateTime(2000, 9, 14, 12, 0, 0); args.Add(tools.AllocValue(dt)); DbFunctionArguments fargs = new DbFunctionArguments(args); DbValue valOutput = DbFunctions.DATEADD(tools, fargs); ByteSlice bs = valOutput.Eval(); DateTime output = tools.GetDateTime(bs); DateTime expected = dt.AddMonths(number); if (expected != output) { throw new Exception("DbFunctions.DATEADD(month) has failed. Expected result: " + expected.ToString() + ", but received: " + output.ToString()); } else { Console.WriteLine("Expected results received."); } } { Console.WriteLine("Testing DbFunctions.DATEADD(day)..."); List <DbValue> args = new List <DbValue>(); mstring datepart = mstring.Prepare("day"); args.Add(tools.AllocValue(datepart)); int number = -9; args.Add(tools.AllocValue(number)); DateTime dt = new DateTime(2000, 9, 14, 12, 0, 0); args.Add(tools.AllocValue(dt)); DbFunctionArguments fargs = new DbFunctionArguments(args); DbValue valOutput = DbFunctions.DATEADD(tools, fargs); ByteSlice bs = valOutput.Eval(); DateTime output = tools.GetDateTime(bs); DateTime expected = dt.AddDays(number); if (expected != output) { throw new Exception("DbFunctions.DATEADD(day) has failed. Expected result: " + expected.ToString() + ", but received: " + output.ToString()); } else { Console.WriteLine("Expected results received."); } } { Console.WriteLine("Testing DbFunctions.DATEADD(week)..."); List <DbValue> args = new List <DbValue>(); mstring datepart = mstring.Prepare("wk"); args.Add(tools.AllocValue(datepart)); int number = 22; args.Add(tools.AllocValue(number)); DateTime dt = new DateTime(2000, 9, 14, 12, 0, 0); args.Add(tools.AllocValue(dt)); DbFunctionArguments fargs = new DbFunctionArguments(args); DbValue valOutput = DbFunctions.DATEADD(tools, fargs); ByteSlice bs = valOutput.Eval(); DateTime output = tools.GetDateTime(bs); DateTime expected = dt.AddDays(number * 7); if (expected != output) { throw new Exception("DbFunctions.DATEADD(week) has failed. Expected result: " + expected.ToString() + ", but received: " + output.ToString()); } else { Console.WriteLine("Expected results received."); } } { Console.WriteLine("Testing DbFunctions.DATEADD(hour)..."); List <DbValue> args = new List <DbValue>(); mstring datepart = mstring.Prepare("hour"); args.Add(tools.AllocValue(datepart)); int number = -99; args.Add(tools.AllocValue(number)); DateTime dt = new DateTime(2000, 9, 14, 12, 0, 0); args.Add(tools.AllocValue(dt)); DbFunctionArguments fargs = new DbFunctionArguments(args); DbValue valOutput = DbFunctions.DATEADD(tools, fargs); ByteSlice bs = valOutput.Eval(); DateTime output = tools.GetDateTime(bs); DateTime expected = dt.AddHours(number); if (expected != output) { throw new Exception("DbFunctions.DATEADD(hour) has failed. Expected result: " + expected.ToString() + ", but received: " + output.ToString()); } else { Console.WriteLine("Expected results received."); } } { Console.WriteLine("Testing DbFunctions.DATEADD(minute)..."); List <DbValue> args = new List <DbValue>(); mstring datepart = mstring.Prepare("mi"); args.Add(tools.AllocValue(datepart)); int number = 80; args.Add(tools.AllocValue(number)); DateTime dt = new DateTime(2000, 9, 14, 12, 0, 0); args.Add(tools.AllocValue(dt)); DbFunctionArguments fargs = new DbFunctionArguments(args); DbValue valOutput = DbFunctions.DATEADD(tools, fargs); ByteSlice bs = valOutput.Eval(); DateTime output = tools.GetDateTime(bs); DateTime expected = dt.AddMinutes(number); if (expected != output) { throw new Exception("DbFunctions.DATEADD(minute) has failed. Expected result: " + expected.ToString() + ", but received: " + output.ToString()); } else { Console.WriteLine("Expected results received."); } } { Console.WriteLine("Testing DbFunctions.DATEADD(second)..."); List <DbValue> args = new List <DbValue>(); mstring datepart = mstring.Prepare("s"); args.Add(tools.AllocValue(datepart)); int number = 900; args.Add(tools.AllocValue(number)); DateTime dt = new DateTime(2000, 9, 14, 12, 0, 0); args.Add(tools.AllocValue(dt)); DbFunctionArguments fargs = new DbFunctionArguments(args); DbValue valOutput = DbFunctions.DATEADD(tools, fargs); ByteSlice bs = valOutput.Eval(); DateTime output = tools.GetDateTime(bs); DateTime expected = dt.AddSeconds(number); if (expected != output) { throw new Exception("DbFunctions.DATEADD(second) has failed. Expected result: " + expected.ToString() + ", but received: " + output.ToString()); } else { Console.WriteLine("Expected results received."); } } { Console.WriteLine("Testing DbFunctions.DATEADD(millisecond)..."); List <DbValue> args = new List <DbValue>(); mstring datepart = mstring.Prepare("millisecond"); args.Add(tools.AllocValue(datepart)); int number = 900; args.Add(tools.AllocValue(number)); DateTime dt = new DateTime(2000, 9, 14, 12, 0, 0); args.Add(tools.AllocValue(dt)); DbFunctionArguments fargs = new DbFunctionArguments(args); DbValue valOutput = DbFunctions.DATEADD(tools, fargs); ByteSlice bs = valOutput.Eval(); DateTime output = tools.GetDateTime(bs); DateTime expected = dt.AddMilliseconds(number); if (expected != output) { throw new Exception("DbFunctions.DATEADD(millisecond) has failed. Expected result: " + expected.ToString() + ", but received: " + output.ToString()); } else { Console.WriteLine("Expected results received."); } } }
public static void DbAggregators_VAR_SAMP() { DbFunctionTools tools = new DbFunctionTools(); Random rnd = new Random(); const int rowcount = 20; double[] values = new double[rowcount]; //Double. { Console.WriteLine("Testing DbAggregators_VAR_SAMP(Double)..."); DbFunctionArguments[] fargs = new DbFunctionArguments[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.VAR_SAMP(tools, new DbAggregatorArguments(fargs)); ByteSlice bs = valOutput.Eval(); double output = tools.GetDouble(bs); double expected = var(values, true); if (expected != output) { throw new Exception("DbAggregators_VAR_SAMP(Double) has failed. Expected result: " + expected.ToString() + ", but received: " + output.ToString()); } else { Console.WriteLine("Expected results received."); } } { Console.WriteLine("Testing DbAggregators_VAR_SAMP(Int32)..."); DbFunctionArguments[] fargs = new DbFunctionArguments[rowcount]; for (int i = 0; i < fargs.Length; i++) { int input = rnd.Next(Int32.MinValue, Int32.MaxValue); values[i] = (double)input; List <DbValue> args = new List <DbValue>(); args.Add(tools.AllocValue(input)); fargs[i] = new DbFunctionArguments(args); } DbValue valOutput = DbAggregators.VAR_SAMP(tools, new DbAggregatorArguments(fargs)); ByteSlice bs = valOutput.Eval(); double output = tools.GetDouble(bs); double expected = var(values, true); if (expected != output) { throw new Exception("DbAggregators_VAR_SAMP(Int32) has failed. Expected result: " + expected.ToString() + ", but received: " + output.ToString()); } else { Console.WriteLine("Expected results received."); } } { Console.WriteLine("Testing DbAggregators_VAR_SAMP(Int64)..."); DbFunctionArguments[] fargs = new DbFunctionArguments[rowcount]; for (int i = 0; i < fargs.Length; i++) { long input = DateTime.Now.Ticks; if (input % 2 == 0) { input = input * -1; } values[i] = (double)input; List <DbValue> args = new List <DbValue>(); args.Add(tools.AllocValue(input)); fargs[i] = new DbFunctionArguments(args); } DbValue valOutput = DbAggregators.VAR_SAMP(tools, new DbAggregatorArguments(fargs)); ByteSlice bs = valOutput.Eval(); double output = tools.GetDouble(bs); double expected = var(values, true); if (expected != output) { throw new Exception("DbAggregators_VAR_SAMP(Int64) has failed. Expected result: " + expected.ToString() + ", but received: " + output.ToString()); } else { Console.WriteLine("Expected results received."); } } }
public static void DbFunctions_ABS() { DbFunctionTools tools = new DbFunctionTools(); Random rnd = new Random(); //Int32. { Console.WriteLine("Testing DbFunctions.ABS(Int32)..."); int input = rnd.Next(Int32.MinValue, Int32.MaxValue); DbValue valInput = tools.AllocValue(input); List <DbValue> args = new List <DbValue>(); args.Add(valInput); DbFunctionArguments fargs = new DbFunctionArguments(args); DbValue valOutput = DbFunctions.ABS(tools, fargs); ByteSlice bs = valOutput.Eval(); int output = tools.GetInt(bs); int expected = Math.Abs(input); if (expected != output) { throw new Exception("DbFunctions.ABS(Int32) has failed. Expected result: " + expected.ToString() + ", but received: " + output.ToString()); } else { Console.WriteLine("Expected results received."); } } //Int64. { Console.WriteLine("Testing DbFunctions.ABS(Int64)..."); long input = DateTime.Now.Ticks - 1; if (input % 2 == 0) { input = input * -1L; } DbValue valInput = tools.AllocValue(input); List <DbValue> args = new List <DbValue>(); args.Add(valInput); DbFunctionArguments fargs = new DbFunctionArguments(args); DbValue valOutput = DbFunctions.ABS(tools, fargs); ByteSlice bs = valOutput.Eval(); byte[] buf = bs.ToBytes(); long output = Entry.ToInt64(Entry.BytesToULong(buf, 1)); long expected = Math.Abs(input); if (expected != output) { throw new Exception("DbFunctions.ABS(Int64) has failed. Expected result: " + expected.ToString() + ", but received: " + output.ToString()); } else { Console.WriteLine("Expected results received."); } } //Double. { Console.WriteLine("Testing DbFunctions.ABS(Double)..."); double input = rnd.NextDouble(); if (input < 0.5) { input = input * -1d; } List <DbValue> args = new List <DbValue>(); args.Add(tools.AllocValue(input)); DbFunctionArguments fargs = new DbFunctionArguments(args); DbValue valOutput = DbFunctions.ABS(tools, fargs); ByteSlice bs = valOutput.Eval(); byte[] buf = bs.ToBytes(); double output = Entry.BytesToDouble(buf, 1); double expected = Math.Abs(input); if (expected != output) { throw new Exception("DbFunctions.ABS(Double) has failed. Expected result: " + expected.ToString() + ", but received: " + output.ToString()); } else { Console.WriteLine("Expected results received."); } } }
public static void DbFunctions_ISNOTNULL() { DbFunctionTools tools = new DbFunctionTools(); Random rnd = new Random(); { Console.WriteLine("Testing DbFunctions.ISNOTNULL(char(n))..."); { mstring s1 = Utils.GenString(rnd.Next(1, 200)); List <DbValue> args = new List <DbValue>(); args.Add(tools.AllocValue(s1)); DbFunctionArguments fargs = new DbFunctionArguments(args); DbValue valOutput = DbFunctions.ISNOTNULL(tools, fargs); ByteSlice bs = valOutput.Eval(); bool output = tools.GetInt(bs) != 0; bool expected = !false; if (expected != output) { throw new Exception("DbFunctions.ISNOTNULL(char(n)='" + s1.ToString() + "') has failed. Expected result: " + expected.ToString() + ", but received: " + output.ToString()); } else { Console.WriteLine("Expected results received."); } } { List <DbValue> args = new List <DbValue>(); args.Add(tools.AllocValue(ByteSlice.Prepare(new byte[] { 1 }), DbTypeID.CHARS)); DbFunctionArguments fargs = new DbFunctionArguments(args); DbValue valOutput = DbFunctions.ISNOTNULL(tools, fargs); ByteSlice bs = valOutput.Eval(); bool output = tools.GetInt(bs) != 0; bool expected = !true; if (expected != output) { throw new Exception("DbFunctions.ISNOTNULL(char(n)=NULL) has failed. Expected result: " + expected.ToString() + ", but received: " + output.ToString()); } else { Console.WriteLine("Expected results received."); } } } { Console.WriteLine("Testing DbFunctions.ISNOTNULL(Int32)..."); { int x = rnd.Next(Int32.MinValue, Int32.MaxValue); List <DbValue> args = new List <DbValue>(); args.Add(tools.AllocValue(x)); DbFunctionArguments fargs = new DbFunctionArguments(args); DbValue valOutput = DbFunctions.ISNOTNULL(tools, fargs); ByteSlice bs = valOutput.Eval(); bool output = tools.GetInt(bs) != 0; bool expected = !false; if (expected != output) { throw new Exception("DbFunctions.ISNOTNULL(Int32=" + x.ToString() + ") has failed. Expected result: " + expected.ToString() + ", but received: " + output.ToString()); } else { Console.WriteLine("Expected results received."); } } { List <DbValue> args = new List <DbValue>(); args.Add(tools.AllocValue(ByteSlice.Prepare(new byte[] { 1 }), DbTypeID.INT)); DbFunctionArguments fargs = new DbFunctionArguments(args); DbValue valOutput = DbFunctions.ISNOTNULL(tools, fargs); ByteSlice bs = valOutput.Eval(); bool output = tools.GetInt(bs) != 0; bool expected = !true; if (expected != output) { throw new Exception("DbFunctions.ISNOTNULL(Int32=NULL) has failed. Expected result: " + expected.ToString() + ", but received: " + output.ToString()); } else { Console.WriteLine("Expected results received."); } } } { Console.WriteLine("Testing DbFunctions.ISNOTNULL(Double)..."); { double x = rnd.NextDouble(); List <DbValue> args = new List <DbValue>(); args.Add(tools.AllocValue(x)); DbFunctionArguments fargs = new DbFunctionArguments(args); DbValue valOutput = DbFunctions.ISNOTNULL(tools, fargs); ByteSlice bs = valOutput.Eval(); bool output = tools.GetInt(bs) != 0; bool expected = !false; if (expected != output) { throw new Exception("DbFunctions.ISNOTNULL(Double=" + x.ToString() + ") has failed. Expected result: " + expected.ToString() + ", but received: " + output.ToString()); } else { Console.WriteLine("Expected results received."); } } { List <DbValue> args = new List <DbValue>(); args.Add(tools.AllocValue(ByteSlice.Prepare(new byte[] { 1 }), DbTypeID.DOUBLE)); DbFunctionArguments fargs = new DbFunctionArguments(args); DbValue valOutput = DbFunctions.ISNOTNULL(tools, fargs); ByteSlice bs = valOutput.Eval(); bool output = tools.GetInt(bs) != 0; bool expected = !true; if (expected != output) { throw new Exception("DbFunctions.ISNOTNULL(Double=NULL) has failed. Expected result: " + expected.ToString() + ", but received: " + output.ToString()); } else { Console.WriteLine("Expected results received."); } } } { Console.WriteLine("Testing DbFunctions.ISNOTNULL(Long)..."); { long x = DateTime.Now.Ticks; List <DbValue> args = new List <DbValue>(); args.Add(tools.AllocValue(x)); DbFunctionArguments fargs = new DbFunctionArguments(args); DbValue valOutput = DbFunctions.ISNOTNULL(tools, fargs); ByteSlice bs = valOutput.Eval(); bool output = tools.GetInt(bs) != 0; bool expected = !false; if (expected != output) { throw new Exception("DbFunctions.ISNOTNULL(Long=" + x.ToString() + ") has failed. Expected result: " + expected.ToString() + ", but received: " + output.ToString()); } else { Console.WriteLine("Expected results received."); } } { List <DbValue> args = new List <DbValue>(); args.Add(tools.AllocValue(ByteSlice.Prepare(new byte[] { 1 }), DbTypeID.LONG)); DbFunctionArguments fargs = new DbFunctionArguments(args); DbValue valOutput = DbFunctions.ISNOTNULL(tools, fargs); ByteSlice bs = valOutput.Eval(); bool output = tools.GetInt(bs) != 0; bool expected = !true; if (expected != output) { throw new Exception("DbFunctions.ISNOTNULL(Long=NULL) has failed. Expected result: " + expected.ToString() + ", but received: " + output.ToString()); } else { Console.WriteLine("Expected results received."); } } } }
public override ByteSlice Eval(out DbType type) { type = col.Type; return(ByteSlice.Prepare(Context.CurrentRow, col.RowOffset, col.Type.Size)); }
public void SetValue(ByteSlice value) { this._value = value; this._type = DbType.Prepare(value.Length, _type.ID); }
public ImmediateValue(IValueContext Context, ByteSlice value, DbType type) : base(Context) { this._value = value; this._type = type; }
public static DbValue POWER(DbFunctionTools tools, DbFunctionArguments args) { string FunctionName = "POWER"; args.EnsureCount(FunctionName, 2); DbType arg0type; ByteSlice arg0 = args[0].Eval(out arg0type); if (Types.IsNullValue(arg0)) { return(tools.AllocNullValue()); // Give a null, take a null. } DbType arg1type; ByteSlice arg1 = args[1].Eval(out arg1type); double b = 0; double p = 0; switch (arg0type.ID) { case DbTypeID.INT: { int x = tools.GetInt(arg0); b = (double)x; } break; case DbTypeID.LONG: { long x = tools.GetLong(arg0); b = (double)x; } break; case DbTypeID.DOUBLE: { b = tools.GetDouble(arg0); } break; default: args.Expected(FunctionName, 0, "input INT, LONG or DOUBLE, got " + arg0type.Name.ToUpper()); return(null); // Doesn't reach here. } switch (arg1type.ID) { case DbTypeID.INT: { int x = tools.GetInt(arg1); p = (double)x; } break; case DbTypeID.LONG: { long x = tools.GetLong(arg1); p = (double)x; } break; case DbTypeID.DOUBLE: { p = tools.GetDouble(arg1); } break; default: args.Expected(FunctionName, 0, "input INT, LONG or DOUBLE, got " + arg1type.Name.ToUpper()); return(null); // Doesn't reach here. } double r = Math.Pow(b, p); return(tools.AllocValue(r)); }
public static void DbFunctions_SQUARE() { DbFunctionTools tools = new DbFunctionTools(); Random rnd = new Random(); //Double. { Console.WriteLine("Testing DbFunctions.SQUARE(Double)..."); double input = rnd.NextDouble(); if (input < 0.5) { input = input * -1d; } List <DbValue> args = new List <DbValue>(); args.Add(tools.AllocValue(input)); DbFunctionArguments fargs = new DbFunctionArguments(args); DbValue valOutput = DbFunctions.SQUARE(tools, fargs); ByteSlice bs = valOutput.Eval(); double output = tools.GetDouble(bs); double expected = Math.Pow((double)input, 2); if (expected != output) { throw new Exception("DbFunctions.SQUARE(Double) has failed. Expected result: " + expected.ToString() + ", but received: " + output.ToString()); } else { Console.WriteLine("Expected results received."); } } { Console.WriteLine("Testing DbFunctions.SQUARE(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.SQUARE(tools, fargs); ByteSlice bs = valOutput.Eval(); double output = tools.GetDouble(bs); double expected = Math.Pow((double)input, 2); if (expected != output) { throw new Exception("DbFunctions.SQUARE(Int32) has failed. Expected result: " + expected.ToString() + ", but received: " + output.ToString()); } else { Console.WriteLine("Expected results received."); } } { Console.WriteLine("Testing DbFunctions.SQUARE(Long)..."); long input = DateTime.Now.Ticks; List <DbValue> args = new List <DbValue>(); args.Add(tools.AllocValue(input)); DbFunctionArguments fargs = new DbFunctionArguments(args); DbValue valOutput = DbFunctions.SQUARE(tools, fargs); ByteSlice bs = valOutput.Eval(); double output = tools.GetDouble(bs); double expected = Math.Pow((double)input, 2); if (expected != output) { throw new Exception("DbFunctions.SQUARE(Long) has failed. Expected result: " + expected.ToString() + ", but received: " + output.ToString()); } else { Console.WriteLine("Expected results received."); } } }
public static void DbFunctions_NULLIF() { DbFunctionTools tools = new DbFunctionTools(); { Console.WriteLine("Testing DbFunctions.NULLIF(DateTime, DateTime)..."); List <DbValue> args = new List <DbValue>(); args.Add(tools.AllocValue(DateTime.Parse("12/1/2000 10:00:00 AM"))); args.Add(tools.AllocValue(DateTime.Parse("12/1/2000 10:00:00 AM"))); DbFunctionArguments fargs = new DbFunctionArguments(args); DbValue valOutput = DbFunctions.NULLIF(tools, fargs); ByteSlice bs = valOutput.Eval(); bool output = Types.IsNullValue(bs); bool expected = true; if (expected != output) { throw new Exception("DbFunctions.NULLIF(DateTime, DateTime) has failed. Expected result: " + expected.ToString() + ", but received: " + output.ToString()); } else { Console.WriteLine("Expected results received."); } } { Console.WriteLine("Testing DbFunctions.NULLIF(DateTime, DateTime)..."); List <DbValue> args = new List <DbValue>(); DateTime dt = DateTime.Parse("12/1/2000 10:00:00 AM"); args.Add(tools.AllocValue(dt)); args.Add(tools.AllocValue(DateTime.Parse("12/2/2000 10:00:00 AM"))); DbFunctionArguments fargs = new DbFunctionArguments(args); DbValue valOutput = DbFunctions.NULLIF(tools, fargs); ByteSlice bs = valOutput.Eval(); DateTime output = tools.GetDateTime(bs); DateTime expected = dt; if (expected != output) { throw new Exception("DbFunctions.NULLIF(DateTime, DateTime) has failed. Expected result: " + expected.ToString() + ", but received: " + output.ToString()); } else { Console.WriteLine("Expected results received."); } } { Console.WriteLine("Testing DbFunctions.NULLIF(Int32, Int32)..."); List <DbValue> args = new List <DbValue>(); args.Add(tools.AllocValue(10)); args.Add(tools.AllocValue(10)); DbFunctionArguments fargs = new DbFunctionArguments(args); DbValue valOutput = DbFunctions.NULLIF(tools, fargs); ByteSlice bs = valOutput.Eval(); bool output = Types.IsNullValue(bs); bool expected = true; if (expected != output) { throw new Exception("DbFunctions.NULLIF(Int32, Int32) has failed. Expected result: " + expected.ToString() + ", but received: " + output.ToString()); } else { Console.WriteLine("Expected results received."); } } { Console.WriteLine("Testing DbFunctions.NULLIF(Int32, Int32)..."); List <DbValue> args = new List <DbValue>(); int x = 10; args.Add(tools.AllocValue(x)); args.Add(tools.AllocValue(11)); DbFunctionArguments fargs = new DbFunctionArguments(args); DbValue valOutput = DbFunctions.NULLIF(tools, fargs); ByteSlice bs = valOutput.Eval(); int output = tools.GetInt(bs); int expected = x; if (expected != output) { throw new Exception("DbFunctions.NULLIF(Int32, Int32) has failed. Expected result: " + expected.ToString() + ", but received: " + output.ToString()); } else { Console.WriteLine("Expected results received."); } } { Console.WriteLine("Testing DbFunctions.NULLIF(Int32, long)..."); List <DbValue> args = new List <DbValue>(); int x = 10; long y = 10; args.Add(tools.AllocValue(x)); args.Add(tools.AllocValue(y)); DbFunctionArguments fargs = new DbFunctionArguments(args); DbValue valOutput = DbFunctions.NULLIF(tools, fargs); ByteSlice bs = valOutput.Eval(); bool output = Types.IsNullValue(bs); bool expected = true; if (expected != output) { throw new Exception("DbFunctions.NULLIF(Int32, long) has failed. Expected result: " + expected.ToString() + ", but received: " + output.ToString()); } else { Console.WriteLine("Expected results received."); } } { Console.WriteLine("Testing DbFunctions.NULLIF(Int32, long)..."); List <DbValue> args = new List <DbValue>(); int x = 10; long y = 11; args.Add(tools.AllocValue(x)); args.Add(tools.AllocValue(y)); DbFunctionArguments fargs = new DbFunctionArguments(args); DbValue valOutput = DbFunctions.NULLIF(tools, fargs); ByteSlice bs = valOutput.Eval(); int output = tools.GetInt(bs); int expected = x; if (expected != output) { throw new Exception("DbFunctions.NULLIF(Int32, long) has failed. Expected result: " + expected.ToString() + ", but received: " + output.ToString()); } else { Console.WriteLine("Expected results received."); } } }