public static string ReadNextBasicExpression(PartReader input, out ExpressionType etype) { string s; s = input.NextPart(); if (s.Length > 0) { if ('\'' == s[0]) { if (s[s.Length - 1] == '\'') { etype = ExpressionType.STRING; return(s); } else { throw new Exception("Unterminated string: " + s); } } else if (char.IsDigit(s[0])) { etype = ExpressionType.NUMBER; return(s); } else if ("+" == s || "-" == s) { bool positive = "+" == s; s = input.NextPart(); if (0 == s.Length) { throw new Exception("Expected number after sign"); } etype = ExpressionType.NUMBER; return(positive ? s : ("-" + s)); } else if (0 == string.Compare("NULL", s, true)) { etype = ExpressionType.NULL; return("NULL"); } else if (char.IsLetter(s[0])) { string name = s; s = input.PeekPart(); if ("(" == s) { input.NextPart(); // Eat the "(". StringBuilder call = new StringBuilder(); call.Append(name); call.Append('('); s = input.NextPart(); if (")" != s) { bool prevident = false; int nparens = 1; for (; ;) { if ("(" == s) { nparens++; } bool thisident = (s.Length > 0 && (char.IsLetterOrDigit(s[0]) || '_' == s[0])); if (prevident && thisident) { call.Append(' '); } prevident = thisident; call.Append(s); s = input.NextPart(); if (")" == s) { if (0 == --nparens) { break; } } if (0 == s.Length) { throw new Exception("Expected ) after function"); } } } call.Append(')'); etype = ExpressionType.FUNCTION; return(call.ToString()); } else if (0 == string.Compare("AS", name, true)) { { StringBuilder sbas = new StringBuilder(); int asparens = 0; for (; ;) { //string s = s = input.PeekPart(); if (0 == s.Length || "," == s) { break; } else if ("(" == s) { asparens++; sbas.Append(input.NextPart()); } else if (")" == s) { if (0 == asparens) { break; } asparens--; sbas.Append(input.NextPart()); } else { sbas.Append(input.NextPart()); } } if (0 == sbas.Length) { throw new Exception("Expected type after AS"); } etype = ExpressionType.AS; return("'AS " + sbas.Replace("'", "''") + "'"); } } else { etype = ExpressionType.NAME; return(name); } } else { #if DEBUG //System.Diagnostics.Debugger.Launch(); #endif throw new Exception("Misunderstood value: " + s); } } etype = ExpressionType.NONE; return(""); }
// _ReadNextLiteral kept for SET static ByteSlice _ReadNextLiteral(PartReader input, DbColumn TypeHint) { string op; op = input.NextPart(); if (op.Length > 0) { if ('\'' == op[0]) { if (op[op.Length - 1] == '\'') { if ("DateTime" == TypeHint.Type.Name) { DateTime dt = DateTime.Parse(op.Substring(1, op.Length - 2)); Int64 x = dt.Ticks; List<byte> buf = new List<byte>(1 + 8); buf.Add(0); Entry.ToBytesAppend64(x, buf); return ByteSlice.Prepare(buf); } else { string str = op.Substring(1, op.Length - 2).Replace("''", "'"); byte[] strbytes = System.Text.Encoding.Unicode.GetBytes(str); //List<byte> buf = new List<byte>(1 + strbytes.Length); List<byte> buf = new List<byte>(TypeHint.Type.Size); buf.Add(0); buf.AddRange(strbytes); // Pad up the end of the char to be the whole column size. while (buf.Count < TypeHint.Type.Size) { buf.Add(0); } return ByteSlice.Prepare(buf); } } else { throw new Exception("Unterminated string: " + op); } } else if (char.IsDigit(op[0])) { return ByteSlice.Prepare(_NumberLiteralToType(op, TypeHint)); } else if ("+" == op || "-" == op) { bool positive = "+" == op; op = input.NextPart(); if (0 == op.Length) { throw new Exception("Expected number after sign"); } return ByteSlice.Prepare(_NumberLiteralToType(positive ? op : ("-" + op), TypeHint)); } else { throw new Exception("Misunderstood value: " + op); } } return ByteSlice.Prepare(new byte[] { 0 }); }
public static string ReadNextBasicExpression(PartReader input, out ExpressionType etype) { string s; s = input.NextPart(); if (s.Length > 0) { if ('\'' == s[0]) { if (s[s.Length - 1] == '\'') { etype = ExpressionType.STRING; return s; } else { throw new Exception("Unterminated string: " + s); } } else if (char.IsDigit(s[0])) { etype = ExpressionType.NUMBER; return s; } else if ("+" == s || "-" == s) { bool positive = "+" == s; s = input.NextPart(); if (0 == s.Length) { throw new Exception("Expected number after sign"); } etype = ExpressionType.NUMBER; return positive ? s : ("-" + s); } else if (0 == string.Compare("NULL", s, true)) { etype = ExpressionType.NULL; return "NULL"; } else if (char.IsLetter(s[0])) { string name = s; s = input.PeekPart(); if ("(" == s) { input.NextPart(); // Eat the "(". StringBuilder call = new StringBuilder(); call.Append(name); call.Append('('); s = input.NextPart(); if (")" != s) { bool prevident = false; int nparens = 1; for (; ; ) { if ("(" == s) { nparens++; } bool thisident = (s.Length > 0 && (char.IsLetterOrDigit(s[0]) || '_' == s[0])); if (prevident && thisident) { call.Append(' '); } prevident = thisident; call.Append(s); s = input.NextPart(); if (")" == s) { if (0 == --nparens) { break; } } if (0 == s.Length) { throw new Exception("Expected ) after function"); } } } call.Append(')'); etype = ExpressionType.FUNCTION; return call.ToString(); } else if (0 == string.Compare("AS", name, true)) { { StringBuilder sbas = new StringBuilder(); int asparens = 0; for (; ; ) { //string s = s = input.PeekPart(); if (0 == s.Length || "," == s) { break; } else if ("(" == s) { asparens++; sbas.Append(input.NextPart()); } else if (")" == s) { if (0 == asparens) { break; } asparens--; sbas.Append(input.NextPart()); } else { sbas.Append(input.NextPart()); } } if (0 == sbas.Length) { throw new Exception("Expected type after AS"); } etype = ExpressionType.AS; return "'AS " + sbas.Replace("'", "''") + "'"; } } else { etype = ExpressionType.NAME; return name; } } else { #if DEBUG //System.Diagnostics.Debugger.Launch(); #endif throw new Exception("Misunderstood value: " + s); } } etype = ExpressionType.NONE; return ""; }
// _ReadNextLiteral kept for SET static ByteSlice _ReadNextLiteral(PartReader input, DbColumn TypeHint) { string op; op = input.NextPart(); if (op.Length > 0) { if ('\'' == op[0]) { if (op[op.Length - 1] == '\'') { if ("DateTime" == TypeHint.Type.Name) { DateTime dt = DateTime.Parse(op.Substring(1, op.Length - 2)); Int64 x = dt.Ticks; List <byte> buf = new List <byte>(1 + 8); buf.Add(0); Entry.ToBytesAppend64(x, buf); return(ByteSlice.Prepare(buf)); } else { string str = op.Substring(1, op.Length - 2).Replace("''", "'"); byte[] strbytes = System.Text.Encoding.Unicode.GetBytes(str); //List<byte> buf = new List<byte>(1 + strbytes.Length); List <byte> buf = new List <byte>(TypeHint.Type.Size); buf.Add(0); buf.AddRange(strbytes); // Pad up the end of the char to be the whole column size. while (buf.Count < TypeHint.Type.Size) { buf.Add(0); } return(ByteSlice.Prepare(buf)); } } else { throw new Exception("Unterminated string: " + op); } } else if (char.IsDigit(op[0])) { return(ByteSlice.Prepare(_NumberLiteralToType(op, TypeHint))); } else if ("+" == op || "-" == op) { bool positive = "+" == op; op = input.NextPart(); if (0 == op.Length) { throw new Exception("Expected number after sign"); } return(ByteSlice.Prepare(_NumberLiteralToType(positive ? op : ("-" + op), TypeHint))); } else { throw new Exception("Misunderstood value: " + op); } } return(ByteSlice.Prepare(new byte[] { 0 })); }