public virtual void WriteStmt(Symbol stmt) { Notation.Record[] recs = notation.Select(stmt, Descriptor.Root, 1); if (ShowHints) WriteOptimizerHint(stmt); WriteQueryExp(recs[0].Arg0); WriteOrderByClause(stmt); }
protected virtual void WriteOptimizerHint(Symbol sym) { Notation.Record[] recs = notation.Select(sym, Descriptor.OptimizerHint, 1); if (recs.Length > 0) { String[] arr = Lisp.ToArray<String>(recs[0].args[0]); for (int k = 0; k < arr.Length; k++) { WriteText("//!"); WriteText(arr[k]); SmartNewLine(); } } recs = notation.Select(sym, Descriptor.HintNamespace, 2); if (recs.Length > 0) { WriteText("/*+ "); for (int k = 0; k < recs.Length; k++) { if (k > 0) WriteText(", "); WriteTextFormat("namespace({0}:{1})", recs[k].args[0], recs[k].args[1]); } WriteText("*/"); SmartNewLine(); } recs = notation.Select(sym, Descriptor.HintColumn, 3); if (recs.Length > 0) { WriteText("/*+ "); for (int k = 0; k < recs.Length; k++) { if (k > 0) WriteText(", "); WriteTextFormat("column({0},{1},{2})", recs[k].args[0], recs[k].args[1], recs[k].args[2]); } WriteText("*/"); SmartNewLine(); } #if DEBUG recs = notation.Select(sym, Descriptor.HintServerSubquery, 1); if (recs.Length > 0) { WriteText("/*+ server("); if (recs[0].args[0] == null) WriteText("null"); else WriteText((string)recs[0].args[0]); WriteText(")*/"); } #endif }
private void ProcessQueryExp(Notation notation, Symbol qexpr, QueryContext context) { Notation.Record[] recs = notation.Select(qexpr, new Descriptor[] { Descriptor.Union, Descriptor.Except }, 2); if (recs.Length > 0) { ProcessQueryExp(notation, recs[0].Arg0, context); ProcessQueryTerm(notation, recs[0].Arg1, context); } else ProcessQueryTerm(notation, qexpr, context); ConfirmBindings(notation, qexpr, context); }
protected virtual void WriteOrderByClause(Symbol sym) { Notation.Record[] recs = notation.Select(sym, Descriptor.Order, 1); if (recs.Length > 0) { SmartNewLine(); WriteText("ORDER BY "); Symbol[] arr = Lisp.ToArray<Symbol>(recs[0].args[0]); for (int k = 0; k < arr.Length; k++) { if (k > 0) WriteText(", "); WriteSortKey((Value)arr[k]); if (notation.Flag(arr[k], Descriptor.Desc)) WriteText(" DESC"); } } }
protected virtual void WriteRowConstructor(Symbol sym) { Notation.Record[] recs = notation.Select(sym, Descriptor.RowValue, 1); if (recs.Length > 0) { WriteText("("); Symbol[] arr = Lisp.ToArray<Symbol>(recs[0].args[0]); for (int i = 0; i < arr.Length; i++) { if (i > 0) WriteText(", "); WriteRowConstructorElem(arr[i]); } WriteText(")"); } else WriteRowConstructorElem(sym); }
protected virtual void WriteSelectList(Symbol sym) { Notation.Record[] recs = notation.Select(sym, new Descriptor[] { Descriptor.Select }); if (recs.Length > 0) if (recs[0].args.Length == 0) WriteText("*"); else { Symbol[] arr = Lisp.ToArray<Symbol>(recs[0].args[0]); for (int k = 0; k < arr.Length; k++) { if (k > 0) WriteText(", "); WriteSelectSubList(arr[k]); } } SmartNewLine(); }
protected virtual void WriteSelectSubList(Symbol sym) { Notation.Record[] recs = notation.Select(sym, Descriptor.TableFields, 1); if (recs.Length > 0) { WriteQualifiedName((Qname)recs[0].Arg0); WriteText(".*"); } else { if (sym.Tag == Tag.SQuery && notation.Flag(sym, Descriptor.Dynatable)) { WriteText("TABLE "); WriteSubQuery(sym); } else WriteValueExp(sym); recs = notation.Select(sym, Descriptor.Alias, 1); if (recs.Length > 0) { WriteText(" AS "); WriteQualifiedName((Qname)recs[0].Arg0); } } }
protected virtual void WriteXmlValueFunc(Symbol sym) { Notation.Record[] recs = notation.Select(sym, Descriptor.XMLConcat, 1); if (recs.Length > 0) { WriteText("XMLCONCAT ("); Symbol[] arr = Lisp.ToArray<Symbol>(recs[0].args[0]); for (int k = 0; k < arr.Length; k++) { if (k > 0) WriteText(", "); WriteValueExp(arr[k]); } WriteText(")"); return; } recs = notation.Select(sym, Descriptor.XMLQuery, 3); if (recs.Length > 0) { WriteText("XMLQUERY ("); WriteText("'"); WriteText((String)recs[0].args[0]); WriteText("'"); if (recs[0].args[1] != null) { WriteText(" PASSING BY VALUE "); Symbol[] arr = Lisp.ToArray<Symbol>(recs[0].args[1]); for (int k = 0; k < arr.Length; k++) { if (k > 0) WriteText(", "); if (arr[k].Tag == Tag.SQuery && notation.Flag(arr[k], Descriptor.Dynatable)) WriteSubQuery(arr[k]); else WriteValueExp(arr[k]); Notation.Record[] recs1 = notation.Select(arr[k], Descriptor.Alias, 1); if (recs1.Length > 0) { WriteText(" AS "); WriteQualifiedName((Qname)recs1[0].Arg0); } } } WriteText(")"); return; } recs = notation.Select(sym, Descriptor.XMLForestAll, 0); if (recs.Length > 0) { WriteText("XMLFOREST (*)"); return; } recs = notation.Select(sym, Descriptor.XMLForest, 1); if (recs.Length > 0) { WriteText("XMLFOREST ("); Notation.Record[] recs1 = notation.Select(sym, Descriptor.XMLNamespaces, 1); if (recs1.Length > 0) { WriteXmlNamespaces(recs1); WriteText(", "); } WriteContentValueList(recs[0].args[0]); WriteText(")"); return; } recs = notation.Select(sym, Descriptor.XMLParse, 3); if (recs.Length > 0) { WriteText("XMLPARSE ("); WriteText(((Literal)recs[0].Arg0).Data); WriteText(" "); WriteValueExp(recs[0].Arg1); TokenWrapper w = (TokenWrapper)recs[0].Arg2; if (w != null) switch (w.Data) { case Token.PRESERVE_WHITESPACE: WriteText(" PRESERVE WHITESPACE"); break; case Token.STRIP_WHITESPACE: WriteText(" STRIP WHITESPACE"); break; } WriteText(")"); return; } recs = notation.Select(sym, Descriptor.XMLPI, 2); if (recs.Length > 0) { WriteText("XMLPI (NAME "); WriteQualifiedName((Qname)recs[0].Arg0); if (recs[0].Arg1 != null) { WriteText(", "); WriteValueExp(recs[0].Arg1); } WriteText(")"); return; } recs = notation.Select(sym, Descriptor.XMLComment, 1); if (recs.Length > 0) { WriteText("XMLCOMMENT ("); WriteValueExp(recs[0].Arg0); WriteText(")"); return; } recs = notation.Select(sym, Descriptor.XMLCDATA, 1); if (recs.Length > 0) { WriteText("XMLCDATA ("); WriteValueExp(recs[0].Arg0); WriteText(")"); return; } recs = notation.Select(sym, Descriptor.XMLRoot, 3); if (recs.Length > 0) { WriteText("XMLROOT ("); WriteValueExp(recs[0].Arg0); if (recs[0].Arg1 != null) { WriteText(" VERSION "); WriteUnsignedLit((Value)recs[0].Arg1); } if (recs[0].Arg2 != null) { WriteText(" STANDALONE "); WriteQualifiedName((Qname)recs[0].Arg2); } WriteText(")"); return; } recs = notation.Select(sym, Descriptor.XMLElement, 2); if (recs.Length > 0) { WriteText("XMLELEMENT ("); WriteQualifiedName((Qname)recs[0].Arg0); Notation.Record[] recs1 = notation.Select(sym, Descriptor.XMLNamespaces, 1); if (recs1.Length > 0) { WriteText(", "); WriteXmlNamespaces(recs1); } recs1 = notation.Select(sym, Descriptor.XMLAttributes, 1); if (recs1.Length > 0) { WriteText(", XMLATTRIBUTES ("); if (recs1[0].args[0] == null) WriteText("*"); else WriteContentValueList(recs1[0].args[0]); WriteText(")"); } if (recs[0].args[1] != null) { WriteText(", "); Symbol[] arr = Lisp.ToArray<Symbol>(recs[0].args[1]); for (int k = 0; k < arr.Length; k++) { if (k > 0) WriteText(", "); WriteValueExp(arr[k]); } } WriteText(")"); return; } }
protected virtual void WriteSimpleTable(Symbol sym) { switch (sym.Tag) { case Tag.SQuery: WriteQuerySpec(sym); break; case Tag.TableConstructor: WriteTableConstructor(sym); break; case Tag.ExplictTable: WriteExplictTable(sym); break; } }
protected virtual void WriteTableConstructor(Symbol sym) { Notation.Record[] recs = notation.Select(sym, Descriptor.TableValue, 1); if (recs.Length > 0) { WriteText("VALUES "); Symbol[] arr = Lisp.ToArray<Symbol>(recs[0].args[0]); for (int i = 0; i < arr.Length; i++) { if (i > 0) { WriteText(","); SmartNewLine(); } WriteRowConstructor((Symbol)arr[i]); } SmartNewLine(); } }
protected virtual void WriteFactor(Symbol sym) { Notation.Record[] recs = notation.Select(sym, Descriptor.UnaryMinus, 1); if (recs.Length > 0) { WriteText("-"); WriteNumPrimary(recs[0].Arg0); } else WriteNumPrimary(sym); }
protected virtual void WriteJoinSpec(Symbol sym) { #if DEBUG if (ShowHints) { Notation.Record[] recsd = notation.Select(sym, Descriptor.HintKeyPair, 2); if (recsd.Length > 0) { WriteText(" /*+ "); string[] k1 = (string[])recsd[0].args[0]; string[] k2 = (string[])recsd[0].args[1]; recsd = notation.Select(sym, Descriptor.HintJoin, 1); if (recsd.Length > 0) { JoinMethod method = (JoinMethod)recsd[0].args[0]; switch (method) { case JoinMethod.NestedLoops: WriteText("nested_loops"); break; case JoinMethod.MergeJoin: WriteText("merge"); break; case JoinMethod.HashJoin: WriteText("hash_join"); break; case JoinMethod.SemiJoin: WriteText("semi_join"); break; } } else WriteText("nested_loops"); WriteText("("); for (int p = 0; p < k1.Length; p++) { if (p > 0) WriteText(", "); WriteTextFormat("{0}={1}", k1[p], k2[p]); } WriteText(")*/ "); } } #endif Notation.Record[] recs = notation.Select(sym, Descriptor.JoinSpec, 1); if (recs.Length > 0) { Notation.Record[] recs1 = notation.Select(recs[0].Arg0, Descriptor.Using, 1); if (recs1.Length > 0) { WriteText(" USING ("); Symbol[] arr = Lisp.ToArray<Symbol>(recs1[0].args[0]); for (int k = 0; k < arr.Length; k++) { if (k > 0) WriteText(", "); WriteQualifiedName((Qname)arr[k]); } WriteText(")"); SmartNewLine(); } else { recs1 = notation.Select(recs[0].Arg0, Descriptor.Constraint, 1); if (recs1.Length > 0) { WriteText(" USING "); if (recs1[0].Arg0.Tag == Tag.Qname) { WriteText("CONSTRAINT "); WriteQualifiedName((Qname)recs1[0].Arg0); } else { TokenWrapper w = (TokenWrapper)recs1[0].Arg0; switch (w.Data) { case Token.PRIMARY: WriteText("PRIMARY KEY"); break; case Token.FOREIGN: WriteText("FOREIGN KEY"); break; } } SmartNewLine(); } else { WriteText(" ON "); WriteSearchCondition(recs[0].Arg0); } } } }
public virtual void WriteValueExp(Symbol sym) { Notation.Record[] recs = notation.Select(sym, Descriptor.Concat, 2); if (recs.Length > 0) { WriteValueExp(recs[0].Arg0); WriteText("||"); WriteCharFactor(recs[0].Arg1); } else WriteCharFactor(sym); }
protected virtual void WriteTableRefSimple(Symbol sym) { #if DEBUG if (ShowHints) { Notation.Record[] recsd = notation.Select(sym, Descriptor.HintSort, 1); if (recsd.Length > 0) { WriteText("/*+ sort("); string[] columns = Lisp.ToArray<String>(recsd[0].args[0]); for (int k = 0; k < recsd.Length; k++) { if (k > 0) WriteText(", "); WriteText(columns[k]); } WriteText(")*/"); } } #endif if (sym is Qname) { WriteQualifiedName((Qname)sym); #if DEBUG if (ShowHints) { Notation.Record[] recsd = notation.Select(sym, Descriptor.HintFilter, 1); if (recsd.Length > 0) { WriteText("/*+ filter("); WriteSearchCondition(recsd[0].Arg0); WriteText(")*/"); } } #endif } else { Notation.Record[] recs1 = notation.Select(sym, Descriptor.Dynatable, 1); if (recs1.Length > 0) { WriteText("TABLE "); WriteValueExp(recs1[0].Arg0); } else WriteSubQuery(sym); } Notation.Record[] recs = notation.Select(sym, Descriptor.Alias, 1); if (recs.Length > 0) { WriteText(" AS "); WriteQualifiedName((Qname)recs[0].Arg0); } }
protected virtual void WriteJoinedTable(Symbol sym) { Notation.Record[] recs = notation.Select(sym, new Descriptor[] { Descriptor.CrossJoin, Descriptor.UnionJoin, Descriptor.NaturalJoin, Descriptor.QualifiedJoin, Descriptor.Branch }); switch (recs[0].descriptor) { case Descriptor.CrossJoin: WriteTableRefSimple(recs[0].Arg0); WriteText(" CROSS JOIN "); WriteTableRef(recs[0].Arg1); break; case Descriptor.UnionJoin: WriteTableRefSimple(recs[0].Arg0); WriteText(" UNION JOIN "); WriteTableRef(recs[0].Arg1); break; case Descriptor.NaturalJoin: WriteTableRefSimple(recs[0].Arg0); WriteText(" NATURAL"); WriteJoinType(sym); WriteText(" JOIN "); WriteTableRef(recs[0].Arg1); break; case Descriptor.QualifiedJoin: WriteTableRefSimple(recs[0].Arg0); WriteJoinType(sym); WriteText(" JOIN "); WriteTableRef(recs[0].Arg1); WriteJoinSpec(sym); break; case Descriptor.Branch: WriteText("("); WriteTableRef(recs[0].Arg0); WriteText(")"); break; } }
protected virtual void WriteTableRef(Symbol sym) { if (sym.Tag == Tag.Join) WriteJoinedTable(sym); else WriteTableRefSimple(sym); }
protected virtual void WriteFromClause(Symbol sym) { Notation.Record[] recs = notation.Select(sym, Descriptor.From, 1); if (recs.Length > 0) { WriteText(" FROM "); Symbol[] arr = Lisp.ToArray<Symbol>(recs[0].args[0]); for (int k = 0; k < arr.Length; k++) { if (k > 0) WriteText(", "); WriteTableRef(arr[k]); #if DEBUG if (ShowHints) { Notation.Record[] recsd = notation.Select(arr[k], Descriptor.HintKeyPair, 2); if (recsd.Length > 0) { WriteText(" /*+ "); string[] k1 = (string[])recsd[0].args[0]; string[] k2 = (string[])recsd[0].args[1]; WriteText("nested_loops"); WriteText("("); for (int p = 0; p < k1.Length; p++) { if (p > 0) WriteText(", "); WriteTextFormat("{0}={1}", k1[p], k2[p]); } WriteText(")*/"); } } #endif } } SmartNewLine(); }
protected virtual void WriteQuerySpec(Symbol sym) { WriteText("SELECT "); if (ExtendedSyntax) { Notation.Record[] recs = notation.Select(sym, Descriptor.Top, 1); if (recs.Length > 0) { WriteText("TOP("); WriteText(recs[0].args[0].ToString()); WriteText(") "); } } if (notation.Flag(sym, Descriptor.Distinct)) WriteText("DISTINCT "); WriteSelectList(sym); WriteFromClause(sym); WriteWhereClause(sym); WriteGroupByClause(sym); WriteHavingClause(sym); }
protected virtual void WriteRowConstructorElem(Symbol sym) { if (sym is TokenWrapper) { TokenWrapper w = (TokenWrapper)sym; switch (w.Data) { case Token.NULL: WriteText("NULL"); break; case Token.DEFAULT: WriteText("DEFAULT"); break; } } else WriteValueExp(sym); }
protected virtual void WriteNumValueExp(Symbol sym) { Notation.Record[] recs = notation.Select(sym, new Descriptor[] { Descriptor.Add, Descriptor.Sub }, 2); if (recs.Length > 0) { WriteNumValueExp(recs[0].Arg0); switch (recs[0].descriptor) { case Descriptor.Add: WriteText("+"); break; case Descriptor.Sub: WriteText("-"); break; } WriteTerm(recs[0].Arg1); } else WriteTerm(sym); }
protected virtual void WriteJoinType(Symbol sym) { Notation.Record[] recs0 = notation.Select(sym, Descriptor.JoinType, 1); if (recs0.Length > 0) { TokenWrapper w = (TokenWrapper)recs0[0].args[0]; switch (w.Data) { case Token.INNER: WriteText(" INNER"); break; case Token.LEFT: WriteText(" LEFT"); break; case Token.RIGHT: WriteText(" RIGHT"); break; case Token.FULL: WriteText(" FULL"); break; } } if (notation.Flag(sym, Descriptor.Outer)) WriteText(" OUTER"); }
protected virtual void WriteExplictTable(Symbol sym) { Notation.Record[] recs = notation.Select(sym, Descriptor.Explicit, 1); if (recs.Length > 0) { WriteText("TABLE "); WriteQualifiedName((Qname)recs[0].Arg0); SmartNewLine(); } }
protected virtual void WriteCharFactor(Symbol sym) { Notation.Record[] recs = notation.Select(sym, new Descriptor[] { Descriptor.Substring, Descriptor.StringUpper, Descriptor.StringLower, Descriptor.StringConvert, Descriptor.StringTrim }); if (recs.Length > 0) { switch (recs[0].descriptor) { case Descriptor.Substring: WriteText("SUBSTRING ("); WriteValueExp(recs[0].Arg0); WriteText(" FROM "); WriteNumValueExp(recs[0].Arg1); if (recs[0].args.Length == 3) { WriteText(" FOR "); WriteNumValueExp(recs[0].Arg2); } WriteText(")"); break; case Descriptor.StringUpper: WriteText("UPPER ("); WriteValueExp(recs[0].Arg0); WriteText(")"); break; case Descriptor.StringLower: WriteText("LOWER ("); WriteValueExp(recs[0].Arg0); WriteText(")"); break; case Descriptor.StringConvert: WriteText("CONVERT ("); WriteValueExp(recs[0].Arg0); WriteText(" USING "); WriteQualifiedName((Qname)recs[0].Arg1); WriteText(")"); break; case Descriptor.StringTrim: TokenWrapper w = (TokenWrapper)recs[0].args[0]; Literal lit = null; if (recs[0].args[1] is Literal) lit = (Literal)recs[0].args[1]; WriteText("TRIM ("); if (w.Data == Token.BOTH && (lit != null && lit.Data.Equals(" "))) WriteValueExp(recs[0].Arg2); else { switch (w.Data) { case Token.TRAILING: WriteText("TRAILING "); break; case Token.LEADING: WriteText("LEADING "); break; } if (lit == null || !lit.Data.Equals(" ")) WriteValueExp(recs[0].Arg1); WriteText(" FROM "); WriteValueExp(recs[0].Arg2); } WriteText(")"); break; } } else WriteNumValueExp(sym); }
protected virtual void WriteWhereClause(Symbol sym) { Notation.Record[] recs = notation.Select(sym, Descriptor.Where, 1); if (recs.Length > 0) { SmartNewLine(); WriteText("WHERE "); WriteSearchCondition(recs[0].Arg0); SmartNewLine(); } }
protected virtual void WriteTerm(Symbol sym) { Notation.Record[] recs = notation.Select(sym, new Descriptor[] { Descriptor.Mul, Descriptor.Div }, 2); if (recs.Length > 0) { WriteTerm(recs[0].Arg0); switch (recs[0].descriptor) { case Descriptor.Mul: WriteText("*"); break; case Descriptor.Div: WriteText("/"); break; } WriteFactor(recs[0].Arg1); } else WriteFactor(sym); }
protected virtual void WriteGroupByClause(Symbol sym) { Notation.Record[] recs = notation.Select(sym, Descriptor.GroupBy, 1); if (recs.Length > 0) { WriteText("GROUP BY "); Symbol[] arr = Lisp.ToArray<Symbol>(recs[0].args[0]); for (int k = 0; k < arr.Length; k++) { if (k > 0) WriteText(", "); WriteQualifiedName((Qname)arr[k]); } SmartNewLine(); } }
protected virtual void WriteNumPrimary(Symbol sym) { Notation.Record[] recs = notation.Select(sym, Descriptor.PosString, 2); if (recs.Length > 0) { WriteText("POSITION ("); WriteValueExp(recs[0].Arg1); WriteText(" IN "); WriteValueExp(recs[0].Arg0); WriteText(")"); } else if (sym.Tag == Tag.SQLX) WriteXmlValueFunc(sym); else WriteNumExpPrimary(sym); }
protected virtual void WriteInPredicateValue(Symbol sym) { Notation.Record[] recs = notation.Select(sym, Descriptor.ValueList, 1); if (recs.Length > 0) { Symbol[] arr = Lisp.ToArray<Symbol>(recs[0].args[0]); WriteText("("); for (int k = 0; k < arr.Length; k++) { if (k > 0) WriteText(", "); WriteValueExp(arr[k]); } WriteText(")"); } else WriteSubQuery(sym); }
protected virtual void WriteHavingClause(Symbol sym) { Notation.Record[] recs = notation.Select(sym, Descriptor.Having, 1); if (recs.Length > 0) { WriteText("HAVING "); WriteSearchCondition(recs[0].Arg0); SmartNewLine(); } }
protected virtual void WriteQueryTerm(Symbol qterm) { Notation.Record[] recs = notation.Select(qterm, Descriptor.Intersect, 2); if (recs.Length > 0) { WriteQueryTerm(recs[0].Arg0); WriteText("INTERSECT"); if (notation.Flag(qterm, Descriptor.Distinct)) WriteText(" ALL"); SmartNewLine(); WriteQueryTerm(recs[0].Arg1); } else WriteSimpleTable(qterm); }