{"id":4404,"date":"2019-05-11T14:11:47","date_gmt":"2019-05-11T06:11:47","guid":{"rendered":"https:\/\/ixyzero.com\/blog\/?p=4404"},"modified":"2019-05-11T14:53:22","modified_gmt":"2019-05-11T06:53:22","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e4%bb%8esql%e4%b8%ad%e6%8f%90%e5%8f%96%e5%87%ba%e6%b6%89%e5%8f%8a%e5%88%b0%e7%9a%84%e8%a1%a8%e5%90%8d%e3%80%81%e5%88%97%e5%90%8d%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/4404.html","title":{"rendered":"\u5982\u4f55\u7528Python\u4eceSQL\u4e2d\u63d0\u53d6\u51fa\u6d89\u53ca\u5230\u7684\u8868\u540d\u3001\u5217\u540d\uff1f"},"content":{"rendered":"\n<p>=Start=<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"id-\u6a21\u677f-\u7f18\u7531\uff1a\">\u7f18\u7531\uff1a<\/h4>\n\n\n\n<p>\u5f53\u524d\u9700\u8981\u7528 Python \u6216 Java \u5bf9 SQL\u8bed\u53e5 \u8fdb\u884c\u89e3\u6790\uff0c\u4ee5\u63d0\u53d6\u51fa\u5176\u4e2d\u7684 \u8868\u540d\u3001\u5217\u540d \u7b49\u4fe1\u606f\uff0c\u65b9\u4fbf\u540e\u7eed\u8fdb\u884c\u7cbe\u51c6\u7684\u5b89\u5168\u95ee\u9898\u5206\u6790\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u4ec5\u4ec5\u53ea\u662f\u63d0\u53d6\u8868\u540d\u7684\u8bdd\u8fd8\u6bd4\u8f83\u7b80\u5355\uff08\u76f4\u63a5\u7528\u5b57\u7b26\u4e32\u5207\u5206\u3001\u5339\u914d\u7b49\u5904\u7406\u5c31\u53ef\u4ee5\u641e\u5b9a\uff09\uff0c\u4f46\u5982\u679c\u8fd8\u8981\u63d0\u53d6\u5217\u540d\uff08\u4e14SQL\u6bd4\u8f83\u590d\u6742\uff09\u7684\u8bdd\uff0c\u5c31\u4e0d\u90a3\u4e48\u5bb9\u6613\u641e\u5b9a\u4e86\uff0c\u8fd9\u65f6\u5c31\u9700\u8981\u5bf9SQL\u8fdb\u884c\u8bed\u6cd5\u5206\u6790\u4ee5\u8fdb\u884c\u51c6\u786e\u63d0\u53d6\u3002\u95ee\u4e86\u4e00\u4e0b\u522b\u4eba\uff0c\u8bf4 ANTLR4 \u53ef\u4ee5\u505a\uff0c\u4e8e\u662f\u5728\u7f51\u4e0a\u641c\u4e86\u4e00\u4e9b\u8d44\u6599\uff0c\u5728\u6b64\u5b9e\u8df5\u6574\u7406\u4e00\u4e0b\uff0c\u65b9\u4fbf\u4ee5\u540e\u53c2\u8003\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"id-\u6a21\u677f-\u6b63\u6587\uff1a\">\u6b63\u6587\uff1a<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\" id=\"id-\u6a21\u677f-\u53c2\u8003\u89e3\u7b54\uff1a\">\u53c2\u8003\u89e3\u7b54\uff1a<\/h5>\n\n\n\n<p>\u8fd9\u91cc\u5148\u4ecb\u7ecd\u4e00\u4e0b\u5982\u4f55\u901a\u8fc7\u5b57\u7b26\u4e32\u5207\u5206\u548c\u6b63\u5219\u7684\u65b9\u5f0f\u6765\u63d0\u53d6SQL\u4e2d\u7684\u8868\u540d\uff08\u5217\u540d\u63d0\u53d6\u6bd4\u8f83\u590d\u6742\uff0c\u4e0d\u592a\u597d\u5b9e\u73b0\uff0c\u540e\u7eed\u627e\u5230\u597d\u7684\u65b9\u6cd5\u4e86\u518d\u8865\u5145\u8fdb\u6765\uff09\u3002<\/p>\n\n\n\n<p>\u65b9\u6cd5\u4e00\uff08\u5b57\u7b26\u4e32\u5207\u5206\uff09\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>def tables_in_query(sql):\n     replace_list = ['\\n', '(', ')', '*', '=', '`']\n     for i in replace_list:\n         sql = sql.replace(i, ' ')\n     sql = sql.split()\n     res = set()\n     for i in range(1, len(sql)):\n         if sql[i-1].lower() in ['from', 'join'] and sql[i].lower() != 'select': \n             res.add(sql[i])\n     # print(res) # ['table1', 'table2', 'table3', 'table4', 'table5']\n     return list(res)<\/code><\/pre>\n\n\n\n<p>\u65b9\u6cd5\u4e8c\uff08\u6b63\u5219\u63d0\u53d6\uff09\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import re\n\ndef tables_in_query2(sql_str):\n    # remove the \/* *\/ comments (\u5148\u79fb\u9664\u5757\u6ce8\u91ca)\n    q = re.sub(r\"\/\\*[^*]*\\*+(?:[^*\/][^*]*\\*+)*\/\", \"\", sql_str)\n\n    # remove whole line -- and # comments (\u518d\u79fb\u9664\u6574\u884c\u6ce8\u91ca)\n    lines = [line for line in q.splitlines() if not re.match(\"^\\s*(--|#)\", line)]\n\n    # remove trailing -- and # comments (\u518d\u79fb\u9664\u884c\u5c3e\u6ce8\u91ca)\n    q = \" \".join([re.split(\"--|#\", line)[0] for line in lines])\n\n    # split on blanks, parens and semicolons (\u7528\u300c\u7a7a\u767d\u7b26\u3001\u62ec\u53f7\u3001\u5206\u53f7\u300d\u4f5c\u4e3a\u5206\u9694\u7b26\u8fdb\u884c\u5207\u5206)\n    tokens = re.split(r\"[\\s)(;`]+\", q)\n\n    # scan the tokens. if we see a FROM or JOIN, we set the get_next\n    # flag, and grab the next one (unless it's SELECT).\n    # \u626b\u63cf\u7ecf\u8fc7\u4e0a\u8ff0\u5206\u8bcd\u7684\u5217\u8868\uff0c\u5f53\u770b\u5230 FROM\/JOIN \u5173\u952e\u5b57\u65f6\uff0c\u83b7\u53d6\u4e0b\u4e00\u4e2a\u975e\u300c\u7a7a\/select\u300d\u7684\u4f5c\u4e3a\u8868\u540d\n\n    result = set()\n    get_next = False\n    for tok in tokens:\n        if get_next:\n            if tok.lower() not in [\"\", \"select\"]:\n                result.add(tok)\n            get_next = False\n        get_next = tok.lower() in [\"from\", \"join\"]\n\n    return result<\/code><\/pre>\n\n\n\n<p>\u5176\u5b9e\u6b63\u5219\u7684\u8fd9\u4e2a\u65b9\u6cd5\u8003\u8651\u7684\u633a\u5b8c\u6574\u7684\uff08\u6bd4\u5982\u5728\u79fb\u9664\u6ce8\u91ca\u90a3\u91cc\uff09\uff0c\u4f46\u662f\u4e0a\u9762\u7684\u5b57\u7b26\u4e32\u7248\u672c\u4f1a\u66f4\u7b80\u5355\u800c\u4e14\u6548\u7387\u66f4\u9ad8\uff08\u5728\u7ebf\u4e0aMySQL\u64cd\u4f5c\u4e2d\u4e00\u822c\u4e0d\u4f1a\u6709\u6ce8\u91ca\uff09\uff0c\u6240\u4ee5\u53ef\u4ee5\u6309\u9700\u9009\u62e9\u3002\u4e0b\u9762\u63d0\u4f9b\u4e00\u4e2a\u6d4b\u8bd5\u6548\u679c\u7528\u7684SQL\uff1a<\/p>\n\n\n\n<p><code>sql = \"\"\"SELECT a.time_updated_server\/1000,<br> content,<br> nick,<br> name<br> FROM table1 a<br> JOIN table2 b ON a.sender_id = b.user_id<br> JOIN table3 c ON a.channel_id = c.channel_id<br> JOIN table4 d ON c.store_id = d.store_id<br> WHERE sender_id NOT IN<br>   (SELECT user_id<br>    FROM table5<br>    WHERE store_id IN ('agent_store:1', 'ask:1'))<br>    AND to_timestamp(a.time_updated_server\/1000)::date >= '2014-05-01'<br>    GROUP BY 1,2,3,4<br>    HAVING sum(1) > 500<br>    ORDER BY 1 ASC<br> \"\"\"<\/code><\/p>\n\n\n\n<p>\u63d0\u53d6\u5217\u540d\u7684\u65b9\u5f0f\u5176\u5b9e\u7167\u7740\u8868\u540d\u7684\u65b9\u6cd5\u6539\u4e00\u4e0b\u5e94\u8be5\u4e5fOK\uff0c\u4f46\u8fd9\u91cc\u6682\u65f6\u5c31\u4e0d\u653e\u51fa\u6765\u4e86\uff0c\u6709\u9700\u8981\u7684\u53ef\u4ee5\u6309\u9700\u8fdb\u884c\u4fee\u6539\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h5 class=\"wp-block-heading\" id=\"id-\u6a21\u677f-\u53c2\u8003\u94fe\u63a5\uff1a\">\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n\n\n\n<ul class=\"wp-block-list\"><li>Parsing Table Names From SQL<br><a href=\"https:\/\/grisha.org\/blog\/2016\/11\/14\/table-names-from-sql\/\">https:\/\/grisha.org\/blog\/2016\/11\/14\/table-names-from-sql\/<\/a><\/li><\/ul>\n\n\n\n<ul class=\"wp-block-list\"><li>How to extract tables names in a SQL script?<a href=\"https:\/\/stackoverflow.com\/questions\/49773059\/how-to-extract-tables-names-in-a-sql-script\">https:\/\/stackoverflow.com\/questions\/49773059\/how-to-extract-tables-names-in-a-sql-script<\/a><\/li><\/ul>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/sqlparse.readthedocs.io\/en\/latest\/\">https:\/\/sqlparse.readthedocs.io\/en\/latest\/<\/a><\/li><\/ul>\n\n\n\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u5f53\u524d\u9700\u8981\u7528 Python \u6216 Java \u5bf9 SQL\u8bed\u53e5 \u8fdb\u884c\u89e3\u6790\uff0c\u4ee5\u63d0\u53d6\u51fa\u5176\u4e2d\u7684 \u8868\u540d [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14,23,7,12],"tags":[1377,8,1262,1376,1378],"class_list":["post-4404","post","type-post","status-publish","format-standard","hentry","category-database","category-knowledgebase-2","category-programing","category-tools","tag-lex","tag-python","tag-sql","tag-sqlparse","tag-yacc"],"views":9104,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4404","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=4404"}],"version-history":[{"count":3,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4404\/revisions"}],"predecessor-version":[{"id":4411,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4404\/revisions\/4411"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=4404"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=4404"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=4404"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}