From 77770112bd250504abab1dd9c0c2327bab8350c8 Mon Sep 17 00:00:00 2001 From: Dan Sosedoff Date: Tue, 5 Jun 2018 15:35:19 -0500 Subject: [PATCH] Detect server type and version with regular expression --- pkg/client/client.go | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/pkg/client/client.go b/pkg/client/client.go index 64b387d..275cab4 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -4,6 +4,7 @@ import ( "fmt" neturl "net/url" "reflect" + "regexp" "strings" "time" @@ -17,10 +18,19 @@ import ( "github.com/sosedoff/pgweb/pkg/statements" ) +var ( + postgresSignature = regexp.MustCompile(`(?i)postgresql ([\d\.]+)\s`) + postgresType = "postgres" + + cockroachSignature = regexp.MustCompile(`(?i)cockroachdb ccl v([\d\.]+)\s`) + cockroachType = "cockroach" +) + type Client struct { db *sqlx.DB tunnel *Tunnel serverVersion string + serverType string lastQueryTime time.Time External bool History []history.Record `json:"history"` @@ -132,7 +142,22 @@ func (client *Client) setServerVersion() { } version := res.Rows[0][0].(string) - client.serverVersion = strings.Split(version, " ")[1] + + // Detect postgresql + matches := postgresSignature.FindAllStringSubmatch(version, 1) + if len(matches) > 0 { + client.serverType = postgresType + client.serverVersion = matches[0][1] + return + } + + // Detect cockroachdb + matches = cockroachSignature.FindAllStringSubmatch(version, 1) + if len(matches) > 0 { + client.serverType = postgresType + client.serverVersion = matches[0][1] + return + } } func (client *Client) Test() error {