Skip to content

Commit

Permalink
Fix #146: support client_label connection option (#150)
Browse files Browse the repository at this point in the history
  • Loading branch information
sitingren authored Oct 11, 2022
1 parent 120d657 commit 8132295
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ Currently supported query arguments are:
| | | 'server-strict' = server must support SSL/TLS |
| | | {customName} = use custom registered `tls.Config` (see "Using custom TLS config" section below) |
| backup_server_node | a list of backup hosts for the client to try to connect if the primary host is unreachable | a comma-seperated list of backup host-port pairs. E.g.<br> 'host1:port1,host2:port2,host3:port3' |
| client_label | Sets a label for the connection on the server. This value appears in the `client_label` column of the SESSIONS system table. | (default) vertica-sql-go-{version}-{pid}-{timestamp} |

To ping the server and validate a connection (as the connection isn't necessarily created at that moment), simply call the *PingContext()* method.

Expand Down
6 changes: 5 additions & 1 deletion connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,11 @@ func newConnection(connString string) (*connection, error) {
}

result.clientPID = os.Getpid()
result.sessionID = fmt.Sprintf("%s-%s-%d-%d", driverName, driverVersion, result.clientPID, time.Now().Unix())
if client_label := result.connURL.Query().Get("client_label"); client_label != "" {
result.sessionID = client_label
} else {
result.sessionID = fmt.Sprintf("%s-%s-%d-%d", driverName, driverVersion, result.clientPID, time.Now().Unix())
}

// Read the interpolate flag.
if iFlag := result.connURL.Query().Get("use_prepared_statements"); iFlag != "" {
Expand Down
18 changes: 16 additions & 2 deletions driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func closeConnection(t *testing.T, connDB *sql.DB, teardownScript ...interface{}
func TestTLSConfiguration(t *testing.T) {
connDB := openConnection(t)
defer closeConnection(t, connDB)
rows, err := connDB.QueryContext(ctx, "SELECT ssl_state FROM sessions")
rows, err := connDB.QueryContext(ctx, "SELECT ssl_state FROM sessions WHERE session_id=(SELECT current_session())")
assertNoErr(t, err)
defer rows.Close()

Expand All @@ -187,6 +187,20 @@ func TestTLSConfiguration(t *testing.T) {
}
}

func TestClientLabel(t *testing.T) {
connDB := openConnection(t)
defer closeConnection(t, connDB)
rows, err := connDB.QueryContext(ctx, "SELECT client_label FROM sessions WHERE session_id=(SELECT current_session())")
assertNoErr(t, err)
defer rows.Close()

var client_label string
for rows.Next() {
assertNoErr(t, rows.Scan(&client_label))
assertEqual(t, client_label, "tests-for-golang")
}
}

func TestBasicQuery(t *testing.T) {
connDB := openConnection(t)
defer closeConnection(t, connDB)
Expand Down Expand Up @@ -1228,7 +1242,7 @@ func init() {
testLogger.Fatal("could not register tls config: %v", err)
}
}
myDBConnectString = "vertica://" + *verticaUserName + ":" + *verticaPassword + "@" + *verticaHostPort + "?" + usePreparedStmtsString + "&tlsmode=" + *tlsMode
myDBConnectString = "vertica://" + *verticaUserName + ":" + *verticaPassword + "@" + *verticaHostPort + "?" + usePreparedStmtsString + "&tlsmode=" + *tlsMode + "&client_label=tests-for-golang"
otherConnectString = "vertica://TestGuy:TestGuyPass@" + *verticaHostPort + "/?tlsmode=" + *tlsMode
badConnectString = "vertica://TestGuy:TestGuyBadPass@" + *verticaHostPort + "/?tlsmode=" + *tlsMode
failoverConnectString = "vertica://" + *verticaUserName + ":" + *verticaPassword + "@badHost" + "?backup_server_node=abc.com:100000," + *verticaHostPort + ",localhost:port"
Expand Down

0 comments on commit 8132295

Please sign in to comment.