Last active
January 1, 2016 01:19
-
-
Save maliubiao/8071874 to your computer and use it in GitHub Desktop.
mysql manager demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| body { | |
| background-color: whiteSmoke; | |
| } | |
| #navPanel { | |
| margin-top: 3%; | |
| } | |
| #queryResult { | |
| top: 10px; left: 10px; | |
| display: block; | |
| width: 500px; | |
| } | |
| #query { | |
| width: 512px; | |
| font-size: 24px; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html> | |
| <link rel="stylesheet" type="text/css" href="./main.css"> | |
| <link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.css"> | |
| <link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap-responsive.css"> | |
| <head> | |
| <title>MySQL Manager</title> | |
| </head> | |
| <body> | |
| <div class="navbar navbar-fixed-top"> | |
| <div class="navbar-inner"> | |
| <div class="container"> | |
| <ul class="nav"> | |
| <li class="active"> | |
| <a href="#">SQL Manager</a> | |
| </li> | |
| </ul> | |
| </div> | |
| </div> | |
| </div> | |
| <div id="navPanel"> | |
| <ul class="nav nav-pills"> | |
| <li id="openSqlTitle" class="active"> | |
| <a href="#" id="openSqlConsole">SQL Console</a> | |
| </li> | |
| <li id="guiEditorTitle"> | |
| <a href="#" id="openGuiEditor">Gui Editor</a> | |
| </li> | |
| <li id="adminToolsTitle"> | |
| <a href="#" id="openAdminTools">Admin tools</a> | |
| </li> | |
| </ul> | |
| </div> | |
| <div id="sqlConsolePanel"> | |
| <form id="queryForm" action="http://localhost:8800/query" method="post"> | |
| <textarea id="query" placeholder="write SQL here." name="sql"></textarea> | |
| <input type="button" class="btn btn-primary" id="doQuery" value="Submit" onclick="sqlQuery()"> | |
| <label>Raw Query Result</label> | |
| <textarea rows="2" cols="40" id="queryResult"></textarea> | |
| </form> | |
| <div id="queryArea"> | |
| <table class="table" id="resultTable"> | |
| </table> | |
| </div> | |
| </div> | |
| </script> | |
| <div id="sqlGuiPanel"> | |
| <p>GuiPanel</p> | |
| </div> | |
| <div id="sqlAdminPanel"> | |
| <p>AdminPanel</p> | |
| </div> | |
| <script> | |
| var currentPanel = { | |
| panel: "#sqlConsolePanel", | |
| title: "#openSqlTitle" | |
| }; | |
| var updateQueryArea = function(data, status, jq) { | |
| $("#queryResult").val(jq.responseText); | |
| /*reset table*/ | |
| $("#resultTable").replaceWith('<table class="table" id="resultTable"></table>'); | |
| var table = $("#resultTable"); | |
| var result = jq.responseJSON; | |
| var query = $("#query"); | |
| if (result.length == 2) { | |
| query.css("border-color", "red"); | |
| var restore = function() { | |
| query.css("border-color", "#555555"); | |
| } | |
| setTimeout(restore, 1000); | |
| return; | |
| } else if (result.length > 2) { | |
| $("#query").css("border-color", "blue"); | |
| } | |
| /*query fields*/ | |
| table.append('<tr id="sqlheader"></tr>'); | |
| var header = $("#sqlheader"); | |
| var fields = JSON.parse(result.field); | |
| for (var f in fields) { | |
| header.append("<th>"+fields[f][0]+"</th>"); | |
| } | |
| /*query rows*/ | |
| var rows = JSON.parse(result.row); | |
| for (var r in rows) { | |
| table.append('<tr id="row'+r+'"></tr>'); | |
| var thisrow = $("#row"+r); | |
| var thisir = rows[r]; | |
| for (var ir in thisir) { | |
| thisrow.append("<td>"+thisir[ir]+"</td>"); | |
| } | |
| } | |
| }; | |
| var sqlQuery = function() { | |
| $.ajax({ | |
| type: "POST", | |
| url: "http://localhost:8800/query", | |
| data: { | |
| sql: $("#query").val() | |
| }, | |
| dataType: "json", | |
| success: updateQueryArea | |
| }); | |
| }; | |
| </script> | |
| <script src="jquery.js"></script> | |
| <script src="underscore.js"></script> | |
| <script src="backbone.js"></script> | |
| <script src="bootstrap/js/bootstrap.js"></script> | |
| <script> | |
| $("#sqlGuiPanel").hide(); | |
| $("#sqlAdminPanel").hide(); | |
| $("form").bind("keyup keypress", function(e) { | |
| var code = e.keyCode || e.which; | |
| if (code == 13) { | |
| $("#doQuery").click(); | |
| e.preventDefault(); | |
| return false; | |
| } | |
| }); | |
| $("#query").bind("keypress", function(e) { | |
| var code = e.keyCode || e.which; | |
| if (code == 13) { | |
| $("#doQuery").click(); | |
| e.preventDefault(); | |
| return false; | |
| } | |
| }); | |
| $("#openSqlConsole").bind("click", function(e) { | |
| $(currentPanel.panel).hide(); | |
| $(currentPanel.title).toggleClass("active"); | |
| currentPanel = { | |
| panel: "#sqlConsolePanel", | |
| title: "#openSqlTitle" | |
| }; | |
| $(currentPanel.panel).show(); | |
| $(currentPanel.title).toggleClass("active"); | |
| }); | |
| $("#openGuiEditor").bind("click", function(e) { | |
| $(currentPanel.panel).hide(); | |
| $(currentPanel.title).toggleClass("active"); | |
| currentPanel = { | |
| panel: "#sqlGuiPanel", | |
| title: "#guiEditorTitle" | |
| }; | |
| $(currentPanel.panel).show(); | |
| $(currentPanel.title).toggleClass("active"); | |
| }); | |
| $("#openAdminTools").bind("click", function(e) { | |
| $(currentPanel.panel).hide(); | |
| $(currentPanel.title).toggleClass("active"); | |
| currentPanel = { | |
| panel: "#sqlAdminPanel", | |
| title: "#adminToolsTitle" | |
| }; | |
| $(currentPanel.panel).show(); | |
| $(currentPanel.title).toggleClass("active"); | |
| }); | |
| </script> | |
| </body> | |
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import nonblocking | |
| import os | |
| import pdb | |
| import json | |
| import signal | |
| import _proc | |
| import umysql | |
| myport = 8800 | |
| mycpu = 0 | |
| mycon = umysql.Connection() | |
| def sigusr1_handler(signum, frame): | |
| print "cons: ", len(nonblocking.cons) | |
| print "worker at %d on cpu %d" % (myport, mycpu) | |
| def home_get(request, response): | |
| response.update({ | |
| "header": { | |
| "status": 403, | |
| "Content-Encoding": "gzip", | |
| "Content-Type": "text/html", | |
| }, | |
| "stream": "" | |
| }) | |
| def home_post(request, response): | |
| request_stream = request["stream"] | |
| if "host" in request_stream: | |
| try: | |
| host, port, user, passwd, db = request_stream.values() | |
| except: | |
| raise Exception(400) | |
| if mycon.is_connected(): | |
| mycon.close() | |
| try: | |
| mycon.connect(host, port, user, passwd, db) | |
| result = "" | |
| except Exception, err: | |
| result = json.dumps(err.args) | |
| response.update({ | |
| "header": { | |
| "status": 200, | |
| "Content-Encoding": "gzip", | |
| "Content-Type": "application/json" | |
| }, | |
| "stream": result | |
| }) | |
| return | |
| if "sql" in request_stream: | |
| sql = request_stream["sql"] | |
| try: | |
| query = mycon.query(sql) | |
| if isinstance(query, tuple): | |
| result = json.dumps(query) | |
| else: | |
| fields = json.dumps(query.fields) | |
| rows = json.dumps(query.rows) | |
| result = json.dumps({"field": fields, "row": rows}) | |
| except Exception, err: | |
| result = json.dumps(err.args) | |
| response.update({ | |
| "header": { | |
| "status": 200, | |
| "Content-Encoding": "gzip", | |
| "Content-Type": "application/json" | |
| }, | |
| "stream": result | |
| }) | |
| else: | |
| raise Exception(400) | |
| home_application = { | |
| "url": r"/query$", | |
| "get": home_get, | |
| "post": home_post | |
| } | |
| nonblocking.install(home_application) | |
| nonblocking.install_statics("ui", "/data/project/py/github/os/node/hello") | |
| try: | |
| _proc.setrlimit(_proc.RLIMIT_NOFILE, (10240, 20480)) | |
| except OSError, err: | |
| print "setrlimit failed, quit: %s" % str(err) | |
| exit(0) | |
| nonblocking.run_as_user("richard") | |
| signal.signal(signal.SIGUSR1, sigusr1_handler) | |
| nonblocking.poll_open(("localhost", 8800)) | |
| print "worker at %d on cpu %d" % (8800, 0) | |
| nonblocking.poll_wait() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment