{"id":373,"date":"2010-07-21T22:49:37","date_gmt":"2010-07-22T03:49:37","guid":{"rendered":"http:\/\/www.dpriver.com\/blog\/?page_id=373"},"modified":"2010-11-02T21:48:52","modified_gmt":"2010-11-03T02:48:52","slug":"sql-expression-parse-tree-traversal-in-preorderinorderpostorder","status":"publish","type":"page","link":"https:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/sql-expression-parse-tree-traversal-in-preorderinorderpostorder\/","title":{"rendered":"SQL expression parse tree traversal in preorder\/inorder\/postorder"},"content":{"rendered":"

SQL expression(condition) was often used in where clause to filter which records are returned in the query’s result set. Manipulate expression is a very common task when you process SQL query in your program. This demo illustrate how to iterate expression to list atomic elements in pre-order, in-order and post-order. Here is another example if you want to modify expression.<\/a><\/p>\n

Expression in where clause of this SQL will be iterated.<\/p>\n

    \r\nSELECT f1 FROM   t1\r\nWHERE  t1.a = 10.2\r\n       AND name = 'k'\r\n       OR id IN ( 2, 4, 5 )\r\n<\/pre>\nSQL expression parse tree traversal in pre-order<\/p>\n\r\nOR\r\nAND\r\n=\r\nt1.a\r\n10.2\r\n=\r\nname\r\n'k'\r\nIN\r\nid\r\n()\r\n,\r\n2\r\n,\r\n4\r\n5\r\n<\/pre>\nSQL expression parse tree traversal in in-order<\/p>\n\r\nt1.a\r\n=\r\n10.2\r\nAND\r\nname\r\n=\r\n'k'\r\nOR\r\nid\r\nIN\r\n2\r\n,\r\n4\r\n,\r\n5\r\n()\r\n<\/pre>\nSQL expression parse tree traversal in post-order<\/p>\n\r\nt1.a\r\n10.2\r\n=\r\nname\r\n'k'\r\n=\r\nAND\r\nid\r\n2\r\n4\r\n5\r\n,\r\n,\r\n()\r\nIN\r\nOR\r\n<\/pre>\n\nDemo code in C#\n<\/p>\n\r\nusing System;\r\nusing System.Collections.Generic;\r\nusing System.Text;\r\n\r\nusing gudusoft.gsqlparser;\r\nusing gudusoft.gsqlparser.Units;\r\n\r\n\r\nnamespace visitexpression\r\n{\r\n    class visitexpression\r\n    {\r\n        static void Main(string[] args)\r\n        {\r\n            TGSqlParser sqlparser = new TGSqlParser(TDbVendor.DbVOracle);\r\n            sqlparser.SqlText.Text = @\"SELECT f1 FROM   t1 WHERE  t1.a = 10.2        AND name = 'k'        OR id IN ( 2, 4, 5 ) HAVING MAX(x) > 6 \";\r\n\r\n            Console.WriteLine(\"Processing..\");\r\n\r\n            int i = sqlparser.Parse();\r\n            if (i == 0)\r\n            {\r\n               visitbinaryexprtree(sqlparser.SqlStatements[0].WhereClause);\r\n            }\r\n            else\r\n                Console.WriteLine(sqlparser.ErrorMessages);\r\n        }\r\n\r\n        public static void visitbinaryexprtree(TLzCustomExpression pExpr)\r\n        {\r\n            Console.WriteLine(\"pre-order:\");\r\n            pExpr.PreOrderTraverse(treenodevisitor);\r\n            Console.WriteLine(\"in-order:\");\r\n            pExpr.InOrderTraverse(treenodevisitor);\r\n            Console.WriteLine(\"post-order:\");\r\n            pExpr.PostOrderTraverse(treenodevisitor);\r\n\r\n        }\r\n\r\n        public static Boolean treenodevisitor(TLz_Node pnode, Boolean pIsLeafNode)\r\n        {\r\n            TLzCustomExpression lcexpr;\r\n            if (pIsLeafNode)\r\n            {\r\n                Console.WriteLine(pnode.AsText);\r\n            }\r\n            else\r\n            {\r\n                lcexpr = (TLzCustomExpression)pnode;\r\n                if (lcexpr.oper == TLzOpType.Expr_Parenthesis)\r\n                {\r\n                    Console.WriteLine(\"()\");\r\n                }\r\n                else if (lcexpr.opname != null)\r\n                {\r\n                    Console.WriteLine(lcexpr.opname.AsText);\r\n                }\r\n            }\r\n            return true;\r\n        }\r\n\r\n    }\r\n}\r\n<\/pre>\nDownload this demo: C# version<\/a>, Java version<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"SQL expression(condition) was often used in where clause to filter which records are returned in the query’s result set. Manipulate expression is a very common task when you process SQL query in your program. This demo illustrate how to iterate expression to list atomic elements in pre-order, in-order and post-order. Here is another example if you want to modify expression. Expression in where clause of this SQL will be iterated. SELECT f1 FROM t1 WHERE t1.a = 10.2 AND name = ‘k’ OR id IN ( 2, 4, 5 ) SQL expression parse tree traversal in pre-order OR AND =\u2026<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":235,"menu_order":100,"comment_status":"closed","ping_status":"open","template":"gsp_feature_page_tt.php","meta":[],"blocksy_meta":{"styles_descriptor":{"styles":{"desktop":"","tablet":"","mobile":""},"google_fonts":[],"version":5}},"yoast_head":"\nHelp you to make better use of General SQL Parser<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n