Sha256: 5e4da061065436ea009a4179938b6943e80c49c1fb078960c184363efded7e34

Contents?: true

Size: 1.9 KB

Versions: 2

Compression:

Stored size: 1.9 KB

Contents

# nt-fingerprint

![test](https://github.com/nayuta-yanagisawa/nt-fingerprint/workflows/test/badge.svg)

## Synopsis

nt-fingerprint is a Ruby gem for converting queries into fingerprints.

```ruby
require 'nt/fingerprint'

class Sample
  include Nt::Fingerprint
end

puts Sample.new.fingerprint("SELECT a, b, 'c' FROM t WEHERE a = 1 AND b IN (1, 2, 3)")
# => select a, b, ? from t wehere a = ? and b in(?+)
```

nt-fingerprint exposes a single method `fingerprint`. What the method does is just calling a function `query.Fingerprint` of [percona/go-mysql](https://github.com/percona/go-mysql) via FFI. Thus, the behavior of `fingerprint` completely follows that of `query.Fingerprint`. We quote the description of `query.Fingerprint` from the [documentation](https://pkg.go.dev/github.com/percona/go-mysql@v0.0.0-20200630114833-b77f37c0bfa2/query#Fingerprint) of go-mysql.

> ```
> 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.

## Examples

A typical use case of nt-fingerprint is query logging. Using nt-fingerprint, one can exclude sensitive information from queries. This makes it possible to log production queries while maintaining security.

For example, one can capture arbitrary ActiveRecord queries by combining [Arproxy](https://github.com/cookpad/arproxy) and nt-fingerprint.

```ruby
class QueryLogger < Arproxy::Base
  include Nt::Fingerprint

  def execute(sql, name=nil)
    Rails.logger.info(fingerprint(sql))
    super(sql, name)
  end
end

Arproxy.configure do |config|
  config.use QueryLogger
end
```

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
nt-fingerprint-0.1.1 README.md
nt-fingerprint-0.1.0 README.md