//'DROP TABLE' NAME; private Query ParseDrop(RQ q) { if (!q.IsNext(tokens["drop"])) { return(null); } q.Pop(); if (!q.IsNext(tokens["table"])) { return(null); } q.Pop(); if (!q.IsNext(tokens["name"])) { return(null); } string table = q.Pop().value; DropTableQuery dt = new DropTableQuery(); dt.name = table; return(dt); }
//(expression ( 'AND' | 'OR' ) expression) | ('NOT' expression) | (term); private Expression ParseExpression(RQ q) { Expression left = null; if (q.IsNext(tokens["not"])) { q.Pop(); NotExpression n = new NotExpression(); n.exp = ParseExpression(q); if (n.exp == null) { return(null); } left = n; } else { left = ParseTerm(q); } BooleanExpression b = new BooleanExpression(); if (q.IsNext(tokens["and"]) || q.IsNext(tokens["or"])) { b.or = q.Pop().value.ToLower() == "or"; Expression right = null; if (q.IsNext(tokens["not"])) { q.Pop(); NotExpression n = new NotExpression(); n.exp = ParseExpression(q); if (n.exp == null) { return(null); } right = n; } else { right = ParseTerm(q); } if (right == null || left == null) { return(null); } b.left = left; b.right = right; return(b); } else { return(left); } return(null); }
//literal | ( '(' exp ')' ); private Expression ParseValue(RQ q) { if (q.IsNext(tokens["open"])) { q.Pop(); Expression exp = ParseExpression(q); if (exp == null) { throw new ParseException("No expression in brackets"); } if (q.IsNext(tokens["close"])) { q.Pop(); return(exp); } return(null); } return(ParseLiteral(q)); }
//INTEGER | FLOAT | BOOLEAN | STRING | NAME | PARAM; private Expression ParseLiteral(RQ q) { if (q.IsNext(tokens["name"])) { RowValueExpression rv = new RowValueExpression(); rv.row = q.Pop().value; return(rv); } if (q.IsNext(tokens["int"]) || q.IsNext(tokens["float"]) || q.IsNext(tokens["bool"])) { ValueExpression v = new ValueExpression(); v.value = q.Pop().value; return(v); } if (q.IsNext(tokens["param"])) { ValueExpression v = new ValueExpression(); v.value = q.Peek().value; v.parameterId = int.Parse(v.value.Remove(0, 1)); return(v); } if (q.IsNext(tokens["string"])) { ValueExpression v = new ValueExpression(); v.value = q.Peek().value.Remove(0, 1); v.value = v.value.Remove(v.value.Length - 1); q.Pop(); return(v); } return(null); }
//'CREATE TABLE' NAME '(' (NAME NAME (',' NAME NAME)*)? ')'; private Query ParseCreate(RQ q) { if (!q.IsNext(tokens["create"])) { return(null); } q.Pop(); if (!q.IsNext(tokens["table"])) { return(null); } q.Pop(); if (!q.IsNext(tokens["name"])) { return(null); } string table = q.Pop().value; if (!q.IsNext(tokens["open"])) { return(null); } q.Pop(); List <string> elems = new List <string>(); //Column list while (q.IsNext(tokens["name"])) { string name = q.Pop().value; elems.Add(name); if (!q.IsNext(tokens["comma"])) { break; } q.Pop(); } if (!q.IsNext(tokens["close"])) { return(null); } q.Pop(); CreateTableQuery ct = new CreateTableQuery(); ct.name = table; ct.columns = elems.ToArray(); return(ct); }
//'DELETE FROM' NAME ('WHERE' expression)? private Query ParseDelete(RQ q) { if (!q.IsNext(tokens["delete"])) { return(null); } q.Pop(); if (!q.IsNext(tokens["from"])) { return(null); } q.Pop(); TableSource source = ParseSource(q); DeleteFromQuery df = new DeleteFromQuery(); df.table = source; df.eval = new TrueExpression(); if (q.IsNext(tokens["where"])) { q.Pop(); Expression e = ParseExpression(q); if (e == null) { e = new TrueExpression(); } else { df.eval = e; } } return(df); }
//NAME | NAME JOIN SOURCE private TableSource ParseSource(RQ q) { if (!q.IsNext(tokens["name"])) { return(null); } string left = q.Pop().value; if (!q.IsNext(tokens["join"])) { RawSource r = new RawSource(); r.table = left; return(r); } q.Pop(); TableSource right = ParseSource(q); JoinSource j = new JoinSource(); j.left = left; j.right = right; return(j); }
//value | (value ('>' | <' | '=') value); private Expression ParseTerm(RQ q) { Expression left = ParseValue(q); if (!q.IsNext(tokens["compare"])) { return(left); } string comp = q.Pop().value; Expression right = ParseValue(q); if (left == null || right == null) { return(null); } CompareExpression c = new CompareExpression(); c.left = left; c.right = right; c.op = comp; return(c); }
//'UPDATE' NAME 'SET' (NAME '=' expression) (',' NAME '=' expression)* ('WHERE' expression)? private Query ParseUpdate(RQ q) { if (!q.IsNext(tokens["update"])) { return(null); } q.Pop(); TableSource s = ParseSource(q); if (!q.IsNext(tokens["set"])) { return(null); } q.Pop(); List <string> fields = new List <string>(); List <Expression> values = new List <Expression>(); while (true) { if (!q.IsNext(tokens["name"])) { break; } string field = q.Pop().value; if (!q.IsNext(tokens["compare"])) { break; } string op = q.Peek().value; if (op != "=") { break; } q.Pop(); Expression e2 = ParseExpression(q); fields.Add(field); values.Add(e2); if (!q.IsNext(tokens["comma"])) { break; } } if (!q.IsNext(tokens["where"])) { return(null); } q.Pop(); Expression e = ParseExpression(q); if (e == null) { e = new TrueExpression(); } UpdateQuery u = new UpdateQuery(); u.table = s; u.fields = fields.ToArray(); u.values = values.ToArray(); u.exp = e; return(u); }
//'INSERT INTO' NAME ('(' NAME (',' NAME)* ')') 'VALUES' expression (',' expression)*; private Query ParseInsert(RQ q) { if (!q.IsNext(tokens["insert"])) { return(null); } q.Pop(); if (!q.IsNext(tokens["into"])) { return(null); } q.Pop(); TableSource source = ParseSource(q); if (!q.IsNext(tokens["open"])) { return(null); } q.Pop(); List <string> names = new List <string>(); while (q.IsNext(tokens["name"])) { string name = q.Pop().value; names.Add(name); if (!q.IsNext(tokens["comma"])) { break; } q.Pop(); } if (!q.IsNext(tokens["close"])) { return(null); } q.Pop(); if (!q.IsNext(tokens["values"])) { return(null); } q.Pop(); List <Expression> ex = new List <Expression>(); while (true) { Expression e = ParseExpression(q); if (e == null) { break; } ex.Add(e); if (!q.IsNext(tokens["comma"])) { break; } q.Pop(); } InsertIntoQuery ii = new InsertIntoQuery(); ii.exp = ex.ToArray(); ii.fields = names.ToArray(); ii.source = source; return(ii); }
//'SELECT' ('*' | (NAME (',' NAME)*)) 'FROM' table (WHERE expression)?; private Query ParseSelect(RQ q) { if (!q.IsNext(tokens["select"])) { return(null); } q.Pop(); SelectFromQuery sf = new SelectFromQuery(); if (q.IsNext(tokens["*"])) { sf.all = true; q.Pop(); } else { List <string> names = new List <string>(); while (true) { if (!q.IsNext(tokens["name"])) { break; } string name = q.Pop().value; names.Add(name); if (!q.IsNext(tokens["comma"])) { break; } q.Pop(); } sf.fields = names.ToArray(); } if (!q.IsNext(tokens["from"])) { return(null); } q.Pop(); TableSource source = ParseSource(q); if (!q.IsNext(tokens["where"])) { return(null); } q.Pop(); Expression e = ParseExpression(q); if (e == null) { e = new TrueExpression(); } sf.source = source; sf.expr = e; return(sf); }