From b0aee65db26a91457a3b2b862733240071dbcae0 Mon Sep 17 00:00:00 2001 From: eqinox76 <5861557+eqinox76@users.noreply.github.com> Date: Sat, 21 Nov 2020 08:12:41 +0100 Subject: [PATCH] Added parameters for compression (#24) --- connection.go | 1 + connector.go | 5 +++++ dsn.go | 7 ++++++- packets.go | 5 +++++ 4 files changed, 17 insertions(+), 1 deletion(-) diff --git a/connection.go b/connection.go index 90aec6439..1cfb5f13b 100644 --- a/connection.go +++ b/connection.go @@ -35,6 +35,7 @@ type mysqlConn struct { sequence uint8 parseTime bool reset bool // set when the Go SQL package calls ResetSession + compressed bool // for context support (Go 1.8+) watching bool diff --git a/connector.go b/connector.go index d567b4e4f..30c827859 100644 --- a/connector.go +++ b/connector.go @@ -136,6 +136,11 @@ func (c *connector) Connect(ctx context.Context) (driver.Conn, error) { return nil, err } + // #24 compression requested by client and supported by server + if mc.flags&clientCompress > 0 && mc.cfg.Compress { + mc.compressed = true + } + return mc, nil } diff --git a/dsn.go b/dsn.go index 93f3548cb..b86cff742 100644 --- a/dsn.go +++ b/dsn.go @@ -58,6 +58,7 @@ type Config struct { CheckConnLiveness bool // Check connections for liveness before using them ClientFoundRows bool // Return number of matching rows instead of rows changed ColumnsWithAlias bool // Prepend table alias to column names + Compress bool // #24 use compression protocol InterpolateParams bool // Interpolate placeholders into query string MultiStatements bool // Allow multiple statements in one query ParseTime bool // Parse time values to time.Time @@ -437,7 +438,11 @@ func parseDSNParams(cfg *Config, params string) (err error) { // Compression case "compress": - return errors.New("compression not implemented yet") + var isBool bool + cfg.Compress, isBool = readBool(value) + if !isBool { + return errors.New("invalid bool value: " + value) + } // Enable client side placeholder substitution case "interpolateParams": diff --git a/packets.go b/packets.go index 6664e5ae5..c7e5d25e8 100644 --- a/packets.go +++ b/packets.go @@ -301,6 +301,11 @@ func (mc *mysqlConn) writeHandshakeResponsePacket(authResp []byte, plugin string clientFlags |= clientMultiStatements } + // To enable compression + if mc.cfg.Compress { + clientFlags |= clientCompress + } + // encode length of the auth plugin data var authRespLEIBuf [9]byte authRespLen := len(authResp)