MCPcopy
hub / github.com/go-sql-driver/mysql / TestConnectionAttributes

Function TestConnectionAttributes

driver_test.go:3499–3567  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

3497}
3498
3499func TestConnectionAttributes(t *testing.T) {
3500 if !available {
3501 t.Skipf("MySQL server not running on %s", netAddr)
3502 }
3503
3504 defaultAttrs := []string{
3505 connAttrClientName,
3506 connAttrOS,
3507 connAttrPlatform,
3508 connAttrPid,
3509 connAttrServerHost,
3510 }
3511 host, _, _ := net.SplitHostPort(addr)
3512 defaultAttrValues := []string{
3513 connAttrClientNameValue,
3514 connAttrOSValue,
3515 connAttrPlatformValue,
3516 strconv.Itoa(os.Getpid()),
3517 host,
3518 }
3519
3520 customAttrs := []string{"attr1", "fo/o"}
3521 customAttrValues := []string{"value1", "bo/o"}
3522
3523 customAttrStrs := make([]string, len(customAttrs))
3524 for i := range customAttrs {
3525 customAttrStrs[i] = fmt.Sprintf("%s:%s", customAttrs[i], customAttrValues[i])
3526 }
3527 dsn += "&connectionAttributes=" + url.QueryEscape(strings.Join(customAttrStrs, ","))
3528
3529 var db *sql.DB
3530 if _, err := ParseDSN(dsn); err != errInvalidDSNUnsafeCollation {
3531 db, err = sql.Open(driverNameTest, dsn)
3532 if err != nil {
3533 t.Fatalf("error connecting: %s", err.Error())
3534 }
3535 defer db.Close()
3536 }
3537
3538 dbt := &DBTest{t, db}
3539
3540 var varName string
3541 var varValue string
3542 err := dbt.db.QueryRow("SHOW VARIABLES LIKE 'performance_schema'").Scan(&varName, &varValue)
3543 if err != nil {
3544 t.Fatalf("error: %s", err.Error())
3545 }
3546 if varValue != "ON" {
3547 t.Skipf("Performance schema is not enabled. skipping")
3548 }
3549 queryString := "SELECT ATTR_NAME, ATTR_VALUE FROM performance_schema.session_account_connect_attrs WHERE PROCESSLIST_ID = CONNECTION_ID()"
3550 rows := dbt.mustQuery(queryString)
3551 defer rows.Close()
3552
3553 rowsMap := make(map[string]string)
3554 for rows.Next() {
3555 var attrName, attrValue string
3556 rows.Scan(&attrName, &attrValue)

Callers

nothing calls this directly

Calls 7

mustQueryMethod · 0.95
ParseDSNFunction · 0.85
OpenMethod · 0.80
ScanMethod · 0.80
ErrorMethod · 0.45
CloseMethod · 0.45
NextMethod · 0.45

Tested by

no test coverage detected