Detect server type and version with regular expression
This commit is contained in:
parent
b18af0b907
commit
77770112bd
@ -4,6 +4,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
neturl "net/url"
|
neturl "net/url"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -17,10 +18,19 @@ import (
|
|||||||
"github.com/sosedoff/pgweb/pkg/statements"
|
"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 {
|
type Client struct {
|
||||||
db *sqlx.DB
|
db *sqlx.DB
|
||||||
tunnel *Tunnel
|
tunnel *Tunnel
|
||||||
serverVersion string
|
serverVersion string
|
||||||
|
serverType string
|
||||||
lastQueryTime time.Time
|
lastQueryTime time.Time
|
||||||
External bool
|
External bool
|
||||||
History []history.Record `json:"history"`
|
History []history.Record `json:"history"`
|
||||||
@ -132,7 +142,22 @@ func (client *Client) setServerVersion() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
version := res.Rows[0][0].(string)
|
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 {
|
func (client *Client) Test() error {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user