mysql2-client-general_log 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +70 -0
- data/Rakefile +7 -0
- data/lib/mysql2/client/general_log.rb +35 -0
- data/lib/mysql2/client/general_log/version.rb +7 -0
- data/lib/mysql2/client/general_log_test.rb +86 -0
- data/mysql2-client-general_logs.gemspec +25 -0
- metadata +111 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 7907d24a201269f7dfb4ae92af5ca21aad26ea88
|
4
|
+
data.tar.gz: 0ba19040ab8c4af8072e99f9335c6d4ae0a0a9a6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d4e06a1aea6fb7c9a2cef757775fdaa789833d9bbfce8a3cfe774de0a655d78425faf75830df556f39ba1af695694537f31f38ee00a20bb1f526a8bbcf118323
|
7
|
+
data.tar.gz: 0e0a1934181b98297d9f51ce6d82afebd112b2a4b796201f4daae81014778ded6c5adc42114ed9b50dfa43d506e70517e2051edd393ece287c9968e26348f301
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 ksss
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
Mysql2::Client::GeneralLog
|
2
|
+
===
|
3
|
+
|
4
|
+
[![Build Status](https://travis-ci.org/ksss/mysql2-client-general_log.svg?branch=v0.1.0)](https://travis-ci.org/ksss/mysql2-client-general_log)
|
5
|
+
|
6
|
+
A monkey patch for Mysql2.
|
7
|
+
Stock all general logs.
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
#! /usr/bin/env ruby
|
11
|
+
|
12
|
+
require "mysql2/client/general_log"
|
13
|
+
|
14
|
+
client = Mysql2::Client.new(config)
|
15
|
+
client.query("SELECT * FROM users LIMIT 1")
|
16
|
+
|
17
|
+
p client.general_log #=>
|
18
|
+
# [
|
19
|
+
# #<struct Mysql2::Client::GeneralLog::Log
|
20
|
+
# sql="SELECT * FROM users LIMIT 1",
|
21
|
+
# backtrace=["script.rb:6:in `<main>'"],
|
22
|
+
# time=0.0909838349907659>
|
23
|
+
# ]
|
24
|
+
```
|
25
|
+
|
26
|
+
## Examples
|
27
|
+
|
28
|
+
### sinatra
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
helpers do
|
32
|
+
def db
|
33
|
+
Thread.current[:db] ||= Mysql2::Client.new(config)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
get '/' do
|
38
|
+
# ...
|
39
|
+
end
|
40
|
+
|
41
|
+
after do
|
42
|
+
puts db.general_log.map(&:sql)
|
43
|
+
puts "path:#{request.path}\tsql:#{db.general_log.length}"
|
44
|
+
db.general_log.clear
|
45
|
+
end
|
46
|
+
```
|
47
|
+
|
48
|
+
## Installation
|
49
|
+
|
50
|
+
Add this line to your application's Gemfile:
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
gem 'mysql2-client-general_log'
|
54
|
+
```
|
55
|
+
|
56
|
+
And then execute:
|
57
|
+
|
58
|
+
$ bundle
|
59
|
+
|
60
|
+
Or install it yourself as:
|
61
|
+
|
62
|
+
$ gem install mysql2-client-general_log
|
63
|
+
|
64
|
+
## Contributing
|
65
|
+
|
66
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/mysql2-client-general_log. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
|
67
|
+
|
68
|
+
## License
|
69
|
+
|
70
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
require "mysql2"
|
2
|
+
require 'benchmark'
|
3
|
+
|
4
|
+
module Mysql2
|
5
|
+
class Client
|
6
|
+
module GeneralLog
|
7
|
+
require "mysql2/client/general_log/version"
|
8
|
+
|
9
|
+
class Log < Struct.new(
|
10
|
+
:sql,
|
11
|
+
:backtrace,
|
12
|
+
:time,
|
13
|
+
); end
|
14
|
+
|
15
|
+
attr_accessor :general_log
|
16
|
+
|
17
|
+
def initialize(opts = {})
|
18
|
+
@general_log = []
|
19
|
+
super
|
20
|
+
end
|
21
|
+
|
22
|
+
# dependent on Mysql2::Client#query
|
23
|
+
def query(sql, options={})
|
24
|
+
ret = nil
|
25
|
+
time = Benchmark.realtime do
|
26
|
+
ret = super
|
27
|
+
end
|
28
|
+
@general_log << Log.new(sql, caller_locations, time)
|
29
|
+
ret
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
prepend GeneralLog
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
require "mysql2/client/general_log"
|
2
|
+
|
3
|
+
module Mysql2ClientGeneralLogTest
|
4
|
+
def test_main(m)
|
5
|
+
@client = Mysql2::Client.new(
|
6
|
+
host: "127.0.0.1",
|
7
|
+
username: "root",
|
8
|
+
)
|
9
|
+
exit m.run
|
10
|
+
@client.query("DROP DATABASE IF EXISTS `mysql2_client_general_log_test`")
|
11
|
+
end
|
12
|
+
|
13
|
+
def db_init
|
14
|
+
@client.query("DROP DATABASE IF EXISTS `mysql2_client_general_log_test`")
|
15
|
+
@client.query("CREATE DATABASE `mysql2_client_general_log_test`")
|
16
|
+
@client.query("USE `mysql2_client_general_log_test`")
|
17
|
+
@client.query(<<-SQL)
|
18
|
+
CREATE TABLE users (
|
19
|
+
`id` int NOT NULL AUTO_INCREMENT PRIMARY KEY,
|
20
|
+
`name` varchar(255) NOT NULL UNIQUE,
|
21
|
+
`password` varchar(255) NOT NULL
|
22
|
+
);
|
23
|
+
SQL
|
24
|
+
@client.query(<<-SQL)
|
25
|
+
INSERT INTO `users` (`name`, `password`)
|
26
|
+
VALUES ('ksss', 'cheap-pass'),
|
27
|
+
('foo', 'fooo'),
|
28
|
+
('bar', 'barr')
|
29
|
+
;
|
30
|
+
SQL
|
31
|
+
@client.general_log.clear
|
32
|
+
end
|
33
|
+
|
34
|
+
def e(s)
|
35
|
+
Mysql2::Client.escape(s)
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_init(t)
|
39
|
+
if !@client.general_log.kind_of?(Array)
|
40
|
+
t.error("initial value expect Array class got #{@client.general_log.class}")
|
41
|
+
end
|
42
|
+
if !@client.general_log.empty?
|
43
|
+
t.error("initial value expect [] got #{@client.general_log}")
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_values(t)
|
48
|
+
db_init
|
49
|
+
ret = @client.query("SELECT * FROM users WHERE name = '#{e("ksss")}'").first
|
50
|
+
@client.query("SELECT * FROM users WHERE name = '#{e("barr")}'")
|
51
|
+
@client.query("SELECT * FROM users WHERE name = '#{e("foo")}'")
|
52
|
+
|
53
|
+
if @client.general_log.length != 3
|
54
|
+
t.error("expect log length 3 got #{@client.general_log.length}")
|
55
|
+
end
|
56
|
+
if @client.general_log.any?{|log| !log.kind_of?(Mysql2::Client::GeneralLog::Log)}
|
57
|
+
t.error("expect all collection item is instance of Mysql2::Client::GeneralLog::Log got #{@client.general_log.map(&:class).uniq}")
|
58
|
+
end
|
59
|
+
expect = {"id"=>1, "name"=>"ksss", "password"=>"cheap-pass"}
|
60
|
+
if ret != expect
|
61
|
+
t.error("expect query output not change from #{expect} got #{ret}")
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_log_class(t)
|
66
|
+
if Mysql2::Client::GeneralLog::Log.members != [:sql, :backtrace, :time]
|
67
|
+
t.error("expect Mysql2::Client::GeneralLog::Log.members is [:sql, :backtrace, :time] got #{Mysql2::Client::GeneralLog::Log.members}")
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def example_general_log
|
72
|
+
db_init
|
73
|
+
@client.query("SELECT * FROM users WHERE name = '#{e("ksss")}'")
|
74
|
+
@client.query("SELECT * FROM users WHERE name = '#{e("barr")}'")
|
75
|
+
@client.query("SELECT * FROM users WHERE name = '#{e("foo")}'")
|
76
|
+
puts @client.general_log.map{|log| log.sql}
|
77
|
+
puts @client.general_log.map{|log| log.backtrace.find{|c| %r{/gems/} !~ c.to_s}.to_s.gsub(/.*?:/, '')}
|
78
|
+
# Output:
|
79
|
+
# SELECT * FROM users WHERE name = 'ksss'
|
80
|
+
# SELECT * FROM users WHERE name = 'barr'
|
81
|
+
# SELECT * FROM users WHERE name = 'foo'
|
82
|
+
# in `example_general_log'
|
83
|
+
# in `example_general_log'
|
84
|
+
# in `example_general_log'
|
85
|
+
end
|
86
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
File.read('lib/mysql2/client/general_log/version.rb') =~ /.*VERSION\s*=\s*['"](.*?)['"]\s.*/
|
5
|
+
version = $1
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = "mysql2-client-general_log"
|
9
|
+
spec.version = version
|
10
|
+
spec.authors = ["ksss"]
|
11
|
+
spec.email = ["co000ri@gmail.com"]
|
12
|
+
|
13
|
+
spec.summary = %q{Simple stocker general log for mysql2 gem.}
|
14
|
+
spec.description = %q{Simple stocker general log for mysql2 gem.}
|
15
|
+
spec.homepage = "https://github.com/ksss/mysql2-client-general_log"
|
16
|
+
spec.license = "MIT"
|
17
|
+
|
18
|
+
spec.files = `git ls-files -z`.split("\x0")
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_runtime_dependency "mysql2"
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "rgot"
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mysql2-client-general_log
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- ksss
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-09-28 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: mysql2
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.10'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.10'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rgot
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: Simple stocker general log for mysql2 gem.
|
70
|
+
email:
|
71
|
+
- co000ri@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".travis.yml"
|
78
|
+
- CODE_OF_CONDUCT.md
|
79
|
+
- Gemfile
|
80
|
+
- LICENSE.txt
|
81
|
+
- README.md
|
82
|
+
- Rakefile
|
83
|
+
- lib/mysql2/client/general_log.rb
|
84
|
+
- lib/mysql2/client/general_log/version.rb
|
85
|
+
- lib/mysql2/client/general_log_test.rb
|
86
|
+
- mysql2-client-general_logs.gemspec
|
87
|
+
homepage: https://github.com/ksss/mysql2-client-general_log
|
88
|
+
licenses:
|
89
|
+
- MIT
|
90
|
+
metadata: {}
|
91
|
+
post_install_message:
|
92
|
+
rdoc_options: []
|
93
|
+
require_paths:
|
94
|
+
- lib
|
95
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
96
|
+
requirements:
|
97
|
+
- - ">="
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
100
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
requirements: []
|
106
|
+
rubyforge_project:
|
107
|
+
rubygems_version: 2.4.5.1
|
108
|
+
signing_key:
|
109
|
+
specification_version: 4
|
110
|
+
summary: Simple stocker general log for mysql2 gem.
|
111
|
+
test_files: []
|