query

package module
v0.0.0-...-5a2b11d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 13, 2018 License: GPL-3.0 Imports: 4 Imported by: 0

README

Transform MySQL Queries

Go Report Card Build Status GoDoc

This package provides functions for transforming MySQL queries. Most important is query.Fingerprint:

package main

import (
    "fmt"
    "github.com/go-mysql/query"
)

func main() {
    f := query.Fingerprint(
        "SELECT c FROM t WHERE id IN (1,2,3) AND ts < '2017-01-01 00:00:00'",
    )
    fmt.Println(f)
}

Output: select c from t where id in(?+) and ts < ?

That fingerprint can be transformed into a unique ID:

id := query.Id(f) // return "EA2376FD2AFF00BA"

Fingerprints and IDs are used to parse and aggregate queries from the MySQL slow log.

Acknowledgement

This code was originally copied from percona/go-mysql @ 2a6037d7d809b18ebd6d735b397f2321879af611. See that project for original contributors and copyright.

This project is a fork to continue development of percona/go-mysql as separate packages. GitHub only allows forking a project once, which is why the code has been copied.

Documentation

Overview

Package query provides functions to transform queries.

Index

Constants

This section is empty.

Variables

View Source
var Debug bool = false

Debug prints very verbose tracing information to STDOUT.

View Source
var ReplaceNumbersInWords = false

ReplaceNumbersInWords enables replacing numbers in words. For example: `SELECT c FROM org235.t` -> `SELECT c FROM org?.t`. For more examples look at test query_test.go/TestFingerprintWithNumberInDbName.

Functions

func Fingerprint

func Fingerprint(q string) string

Fingerprint returns the canonical form of q. The primary transformations are:

  • Replace values with ?
  • Collapse whitespace
  • Remove comments
  • Lowercase everything

Additional trasnformations are performed which change the syntax of the original query without affecting its performance characteristics. For example, "ORDER BY col ASC" is the same as "ORDER BY col", so "ASC" in the fingerprint is removed.

func Id

func Id(fingerprint string) string

Id returns the right-most 16 characters of the MD5 checksum of fingerprint. Query IDs are the shortest way to uniquely identify queries.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL