Merge pull request #196 from sosedoff/switch-db

Allow switching between databases
This commit is contained in:
Dan Sosedoff 2016-11-06 21:52:58 -06:00 committed by GitHub
commit 00c2b8262b
6 changed files with 94 additions and 6 deletions

View File

@ -4,6 +4,8 @@ import (
"encoding/base64"
"errors"
"fmt"
neturl "net/url"
"strings"
"time"
"github.com/gin-gonic/gin"
@ -118,6 +120,62 @@ func Connect(c *gin.Context) {
c.JSON(200, info.Format()[0])
}
func SwitchDb(c *gin.Context) {
if command.Opts.LockSession {
c.JSON(400, Error{"Session is locked"})
return
}
name := c.Request.URL.Query().Get("db")
if name == "" {
name = c.Request.FormValue("db")
}
if name == "" {
c.JSON(400, Error{"Database name is not provided"})
return
}
conn := DB(c)
if conn == nil {
c.JSON(400, Error{"Not connected"})
return
}
currentUrl, err := neturl.Parse(conn.ConnectionString)
if err != nil {
c.JSON(400, Error{"Unable to parse current connection string"})
return
}
newStr := strings.Replace(conn.ConnectionString, currentUrl.Path, "/"+name, 1)
cl, err := client.NewFromUrl(newStr, nil)
if err != nil {
c.JSON(400, Error{err.Error()})
return
}
err = cl.Test()
if err != nil {
c.JSON(400, Error{err.Error()})
return
}
info, err := cl.Info()
if err == nil {
err = setClient(c, cl)
if err != nil {
cl.Close()
c.JSON(400, Error{err.Error()})
return
}
}
conn.Close()
c.JSON(200, info.Format()[0])
}
func Disconnect(c *gin.Context) {
if command.Opts.LockSession {
c.JSON(400, Error{"Session is locked"})

View File

@ -30,6 +30,7 @@ func SetupRoutes(router *gin.Engine) {
api.GET("/info", GetInfo)
api.POST("/connect", Connect)
api.POST("/disconnect", Disconnect)
api.POST("/switchdb", SwitchDb)
api.GET("/databases", GetDatabases)
api.GET("/connection", GetConnectionInfo)
api.GET("/activity", GetActivity)

File diff suppressed because one or more lines are too long

View File

@ -111,6 +111,7 @@
#sidebar div.tables-list .title span.current-database {
margin-left: 4px;
cursor: pointer;
}
#sidebar div.tables-list .title:hover span.refresh {

View File

@ -241,5 +241,8 @@
<li><a href="#" data-action="delete">Delete table</a></li>
</ul>
</div>
<div id="databases_context_menu">
<ul class="dropdown-menu" role="menu"></ul>
</div>
</body>
</html>

View File

@ -744,6 +744,20 @@ function bindContextMenus() {
}
});
});
$("#current_database").contextmenu({
target: "#databases_context_menu",
onItem: function(context, e) {
var name = $(e.target).text();
apiCall("post", "/switchdb", { db: name }, function(resp) {
if (resp.error) {
alert(resp.error);
return;
}
window.location.reload();
});
}
});
}
$(document).ready(function() {
@ -929,6 +943,16 @@ $(document).ready(function() {
}
});
$("#current_database").on("click", function(e) {
apiCall("get", "/databases", {}, function(resp) {
$("#databases_context_menu > ul > li").remove();
resp.forEach(function(name) {
$("<li><a href='#'>" + name + "</a></li>").appendTo("#databases_context_menu > ul");
});
$("#current_database").triggerHandler("contextmenu");
});
});
$("#edit_connection").on("click", function() {
if (connected) {
$("#close_connection_window").show();
@ -1073,6 +1097,7 @@ $(document).ready(function() {
if (resp.error) {
connected = false;
showConnectionSettings();
$(".connection-actions").show();
}
else {
connected = true;