diff --git a/README.md b/README.md index a33eb2b..9d07ad7 100644 --- a/README.md +++ b/README.md @@ -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.
'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. diff --git a/connection.go b/connection.go index 6e7a40f..8cabd21 100644 --- a/connection.go +++ b/connection.go @@ -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 != "" { diff --git a/driver_test.go b/driver_test.go index b1230cd..17ab2c2 100644 --- a/driver_test.go +++ b/driver_test.go @@ -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() @@ -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) @@ -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"