mysql2-cs-bind 0.0.5 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/README.md +2 -2
- data/lib/mysql2-cs-bind.rb +30 -16
- data/mysql2-cs-bind.gemspec +4 -4
- data/spec/mysql2/client_spec.rb +13 -18
- data/spec/mysql2/error_spec.rb +0 -32
- metadata +16 -59
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: d3495f65b800c8cddbb748e628a64ed8fca1d9d33766cd1196ed124c87c2b998
|
4
|
+
data.tar.gz: 66d1445f6104b171ec1cb72661f33d75f010a79a557bad875d3d836303d54ed5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 210e1bdf190d73617d5e3df68ecae830acf14d7f78a459c8d1cbbbf514ec4245d87713a38dc7358b9e34a1608d819d243dccdf78c5ed2586d2ccbc991aa53731
|
7
|
+
data.tar.gz: '058ca605f2586f0e3e23a37797f571386792ea8e44145dc4e301ce2de1009d8a4439fb7d8f2f43624bc90faf1b1ff00470971b8e9cb52966ae907cb05a95d476'
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
#
|
1
|
+
# mysql2-cs-bind
|
2
2
|
|
3
|
-
'
|
3
|
+
'mysql2-cs-bind' is extension of 'mysql2', to add method of client-side variable binding (pseudo prepared statement).
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
data/lib/mysql2-cs-bind.rb
CHANGED
@@ -1,14 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'mysql2'
|
2
4
|
|
3
5
|
class Mysql2::Client
|
4
6
|
|
5
|
-
def xquery(sql, *args)
|
6
|
-
|
7
|
-
args.pop
|
8
|
-
else
|
9
|
-
{}
|
10
|
-
end
|
11
|
-
if args.size < 1
|
7
|
+
def xquery(sql, *args, **options)
|
8
|
+
if args.empty?
|
12
9
|
query(sql, options)
|
13
10
|
else
|
14
11
|
query(Mysql2::Client.pseudo_bind(sql, args), options)
|
@@ -24,22 +21,39 @@ class Mysql2::Client
|
|
24
21
|
placeholders.push(pos)
|
25
22
|
search_pos = pos + 1
|
26
23
|
end
|
27
|
-
|
28
|
-
|
24
|
+
|
25
|
+
if placeholders.length != values.length &&
|
26
|
+
placeholders.length != (values = values.flatten(1)).length
|
27
|
+
raise ArgumentError, "mismatch between placeholders number and values arguments"
|
28
|
+
end
|
29
29
|
|
30
30
|
while pos = placeholders.pop()
|
31
31
|
rawvalue = values.pop()
|
32
|
-
if rawvalue.
|
33
|
-
sql[pos] =
|
34
|
-
elsif rawvalue.respond_to?(:strftime)
|
35
|
-
sql[pos] = "'" + rawvalue.strftime('%Y-%m-%d %H:%M:%S') + "'"
|
36
|
-
elsif rawvalue.is_a?(Array)
|
37
|
-
sql[pos] = rawvalue.map{|v| "'" + Mysql2::Client.escape(v.to_s) + "'" }.join(",")
|
32
|
+
if rawvalue.is_a?(Array)
|
33
|
+
sql[pos] = rawvalue.map{|v| quote(v) }.join(",")
|
38
34
|
else
|
39
|
-
sql[pos] =
|
35
|
+
sql[pos] = quote(rawvalue)
|
40
36
|
end
|
41
37
|
end
|
42
38
|
sql
|
43
39
|
end
|
44
40
|
|
41
|
+
private
|
42
|
+
|
43
|
+
def self.quote(rawvalue)
|
44
|
+
case rawvalue
|
45
|
+
when nil
|
46
|
+
'NULL'
|
47
|
+
when true
|
48
|
+
'TRUE'
|
49
|
+
when false
|
50
|
+
'FALSE'
|
51
|
+
else
|
52
|
+
if rawvalue.respond_to?(:strftime)
|
53
|
+
"'#{rawvalue.strftime('%Y-%m-%d %H:%M:%S')}'"
|
54
|
+
else
|
55
|
+
"'#{Mysql2::Client.escape(rawvalue.to_s)}'"
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
45
59
|
end
|
data/mysql2-cs-bind.gemspec
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |gem|
|
4
4
|
gem.name = "mysql2-cs-bind"
|
5
|
-
gem.version = "0.
|
5
|
+
gem.version = "0.1.1"
|
6
6
|
gem.authors = ["TAGOMORI Satoshi"]
|
7
7
|
gem.email = ["tagomoris@gmail.com"]
|
8
8
|
gem.homepage = "https://github.com/tagomoris/mysql2-cs-bind"
|
@@ -14,11 +14,11 @@ Gem::Specification.new do |gem|
|
|
14
14
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
15
15
|
gem.require_paths = ["lib"]
|
16
16
|
|
17
|
-
gem.
|
17
|
+
gem.required_ruby_version = ">= 2.0.0"
|
18
|
+
|
19
|
+
gem.add_runtime_dependency "mysql2"
|
18
20
|
|
19
21
|
# tests
|
20
|
-
gem.add_development_dependency "mysql2"
|
21
|
-
gem.add_development_dependency 'eventmachine'
|
22
22
|
gem.add_development_dependency 'rake-compiler', "~> 0.7.7"
|
23
23
|
gem.add_development_dependency 'rake', '0.8.7' # NB: 0.8.7 required by rake-compiler 0.7.9
|
24
24
|
gem.add_development_dependency 'rspec', '2.10.0'
|
data/spec/mysql2/client_spec.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
# encoding: UTF-8
|
2
2
|
require 'spec_helper'
|
3
|
+
require 'time'
|
3
4
|
|
4
5
|
describe Mysql2::Client do
|
5
6
|
before(:each) do
|
@@ -20,7 +21,7 @@ describe Mysql2::Client do
|
|
20
21
|
@klass.pseudo_bind("SELECT x,y,z FROM x WHERE x=?", [1]).should eql("SELECT x,y,z FROM x WHERE x='1'")
|
21
22
|
@klass.pseudo_bind("SELECT x,y,z FROM x WHERE x=? AND y=?", [1, 'X']).should eql("SELECT x,y,z FROM x WHERE x='1' AND y='X'")
|
22
23
|
end
|
23
|
-
|
24
|
+
|
24
25
|
it "should raise ArgumentError if mismatch exists between placeholders and arguments" do
|
25
26
|
expect {
|
26
27
|
@klass.pseudo_bind("SELECT x,y,z FROM x", [1])
|
@@ -41,16 +42,23 @@ describe Mysql2::Client do
|
|
41
42
|
end
|
42
43
|
|
43
44
|
it "should replace placeholder with formatted timestamp string about Time object" do
|
44
|
-
require 'time'
|
45
45
|
t = Time.strptime('2012/04/20 16:50:45', '%Y/%m/%d %H:%M:%S')
|
46
46
|
@klass.pseudo_bind("UPDATE x SET y=? WHERE x=?", [t,1]).should eql("UPDATE x SET y='2012-04-20 16:50:45' WHERE x='1'")
|
47
47
|
end
|
48
48
|
|
49
|
+
it "should replace placeholder with TRUE/FALSE about true/false" do
|
50
|
+
@klass.pseudo_bind("UPDATE x SET y=? WHERE x=?", [true,1]).should eql("UPDATE x SET y=TRUE WHERE x='1'")
|
51
|
+
@klass.pseudo_bind("UPDATE x SET y=? WHERE x=?", [false,1]).should eql("UPDATE x SET y=FALSE WHERE x='1'")
|
52
|
+
end
|
53
|
+
|
49
54
|
it "should replace placeholder with value list about Array object" do
|
55
|
+
t = Time.strptime('2012/04/20 16:50:45', '%Y/%m/%d %H:%M:%S')
|
50
56
|
@klass.pseudo_bind("SELECT x,y,z FROM x WHERE x in (?)", [[1,2,3]]).should eql("SELECT x,y,z FROM x WHERE x in ('1','2','3')")
|
51
57
|
@klass.pseudo_bind("SELECT x,y,z FROM x WHERE x = ? and y in (?)", [1, [1, 2, 3]]).should eql("SELECT x,y,z FROM x WHERE x = '1' and y in ('1','2','3')")
|
52
58
|
@klass.pseudo_bind("SELECT id FROM (SELECT 1 AS id) AS tbl WHERE id = ? OR id in (?)", [1, [2,3]]).should eql("SELECT id FROM (SELECT 1 AS id) AS tbl WHERE id = '1' OR id in ('2','3')")
|
53
59
|
@klass.pseudo_bind("SELECT id FROM (SELECT 1 AS id) AS tbl WHERE id in (?) OR id = ?", [[1,2], 3]).should eql("SELECT id FROM (SELECT 1 AS id) AS tbl WHERE id in ('1','2') OR id = '3'")
|
60
|
+
@klass.pseudo_bind("SELECT x,y,z FROM x WHERE x = ? and y in (?)", [1, [true, nil]]).should eql("SELECT x,y,z FROM x WHERE x = '1' and y in (TRUE,NULL)")
|
61
|
+
@klass.pseudo_bind("SELECT x,y,z FROM x WHERE x = ? and y in (?)", [1, [t, nil]]).should eql("SELECT x,y,z FROM x WHERE x = '1' and y in ('2012-04-20 16:50:45',NULL)")
|
54
62
|
end
|
55
63
|
end
|
56
64
|
|
@@ -63,23 +71,16 @@ describe Mysql2::Client do
|
|
63
71
|
}.to_not raise_exception(Mysql2::Error)
|
64
72
|
end
|
65
73
|
|
66
|
-
it "should accept an options hash that inherits from Mysql2::Client.default_query_options" do
|
67
|
-
@client.xquery "SELECT ?", 1, :something => :else
|
68
|
-
@client.query_options.should eql(@client.query_options.merge(:something => :else))
|
69
|
-
end
|
70
|
-
|
71
74
|
it "should return results as a hash by default" do
|
72
75
|
@client.xquery("SELECT ?", 1).first.class.should eql(Hash)
|
73
76
|
end
|
74
77
|
|
75
78
|
it "should be able to return results as an array" do
|
76
79
|
@client.xquery("SELECT ?", 1, :as => :array).first.class.should eql(Array)
|
77
|
-
@client.xquery("SELECT ?", 1).each(:as => :array)
|
78
|
-
@client.query("SELECT 1").first.should eql([1])
|
79
|
-
@client.query("SELECT '1'").first.should eql(['1'])
|
80
|
+
@client.xquery("SELECT ?", 1).each(:as => :array).first.class.should eql(Array)
|
80
81
|
@client.xquery("SELECT 1", :as => :array).first.should eql([1])
|
81
|
-
@client.xquery("SELECT ?", 1).first.should eql(['1'])
|
82
|
-
@client.xquery("SELECT ?+1", 1).first.should eql([2.0])
|
82
|
+
@client.xquery("SELECT ?", 1, :as => :array).first.should eql(['1'])
|
83
|
+
@client.xquery("SELECT ?+1", 1, :as => :array).first.should eql([2.0])
|
83
84
|
end
|
84
85
|
|
85
86
|
it "should read multi style args" do
|
@@ -119,10 +120,4 @@ describe Mysql2::Client do
|
|
119
120
|
it "should respond to escape" do
|
120
121
|
Mysql2::Client.should respond_to(:escape)
|
121
122
|
end
|
122
|
-
|
123
|
-
if RUBY_VERSION =~ /1.9/
|
124
|
-
it "should respond to #encoding" do
|
125
|
-
@client.should respond_to(:encoding)
|
126
|
-
end
|
127
|
-
end
|
128
123
|
end
|
data/spec/mysql2/error_spec.rb
CHANGED
@@ -34,36 +34,4 @@ describe Mysql2::Error do
|
|
34
34
|
it "should alias #message to #error" do
|
35
35
|
@error.should respond_to(:error)
|
36
36
|
end
|
37
|
-
|
38
|
-
if RUBY_VERSION =~ /1.9/
|
39
|
-
it "#message encoding should match the connection's encoding, or Encoding.default_internal if set" do
|
40
|
-
if Encoding.default_internal.nil?
|
41
|
-
@error.message.encoding.should eql(@client.encoding)
|
42
|
-
@error2.message.encoding.should eql(@client2.encoding)
|
43
|
-
else
|
44
|
-
@error.message.encoding.should eql(Encoding.default_internal)
|
45
|
-
@error2.message.encoding.should eql(Encoding.default_internal)
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
it "#error encoding should match the connection's encoding, or Encoding.default_internal if set" do
|
50
|
-
if Encoding.default_internal.nil?
|
51
|
-
@error.error.encoding.should eql(@client.encoding)
|
52
|
-
@error2.error.encoding.should eql(@client2.encoding)
|
53
|
-
else
|
54
|
-
@error.error.encoding.should eql(Encoding.default_internal)
|
55
|
-
@error2.error.encoding.should eql(Encoding.default_internal)
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
it "#sql_state encoding should match the connection's encoding, or Encoding.default_internal if set" do
|
60
|
-
if Encoding.default_internal.nil?
|
61
|
-
@error.sql_state.encoding.should eql(@client.encoding)
|
62
|
-
@error2.sql_state.encoding.should eql(@client2.encoding)
|
63
|
-
else
|
64
|
-
@error.sql_state.encoding.should eql(Encoding.default_internal)
|
65
|
-
@error2.sql_state.encoding.should eql(Encoding.default_internal)
|
66
|
-
end
|
67
|
-
end
|
68
|
-
end
|
69
37
|
end
|
metadata
CHANGED
@@ -1,84 +1,46 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mysql2-cs-bind
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.1.1
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- TAGOMORI Satoshi
|
9
|
-
autorequire:
|
8
|
+
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2021-08-23 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: mysql2
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
|
-
- -
|
17
|
+
- - ">="
|
20
18
|
- !ruby/object:Gem::Version
|
21
19
|
version: '0'
|
22
20
|
type: :runtime
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
|
-
- -
|
28
|
-
- !ruby/object:Gem::Version
|
29
|
-
version: '0'
|
30
|
-
- !ruby/object:Gem::Dependency
|
31
|
-
name: mysql2
|
32
|
-
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
|
-
requirements:
|
35
|
-
- - ! '>='
|
36
|
-
- !ruby/object:Gem::Version
|
37
|
-
version: '0'
|
38
|
-
type: :development
|
39
|
-
prerelease: false
|
40
|
-
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
|
-
requirements:
|
43
|
-
- - ! '>='
|
44
|
-
- !ruby/object:Gem::Version
|
45
|
-
version: '0'
|
46
|
-
- !ruby/object:Gem::Dependency
|
47
|
-
name: eventmachine
|
48
|
-
requirement: !ruby/object:Gem::Requirement
|
49
|
-
none: false
|
50
|
-
requirements:
|
51
|
-
- - ! '>='
|
52
|
-
- !ruby/object:Gem::Version
|
53
|
-
version: '0'
|
54
|
-
type: :development
|
55
|
-
prerelease: false
|
56
|
-
version_requirements: !ruby/object:Gem::Requirement
|
57
|
-
none: false
|
58
|
-
requirements:
|
59
|
-
- - ! '>='
|
24
|
+
- - ">="
|
60
25
|
- !ruby/object:Gem::Version
|
61
26
|
version: '0'
|
62
27
|
- !ruby/object:Gem::Dependency
|
63
28
|
name: rake-compiler
|
64
29
|
requirement: !ruby/object:Gem::Requirement
|
65
|
-
none: false
|
66
30
|
requirements:
|
67
|
-
- - ~>
|
31
|
+
- - "~>"
|
68
32
|
- !ruby/object:Gem::Version
|
69
33
|
version: 0.7.7
|
70
34
|
type: :development
|
71
35
|
prerelease: false
|
72
36
|
version_requirements: !ruby/object:Gem::Requirement
|
73
|
-
none: false
|
74
37
|
requirements:
|
75
|
-
- - ~>
|
38
|
+
- - "~>"
|
76
39
|
- !ruby/object:Gem::Version
|
77
40
|
version: 0.7.7
|
78
41
|
- !ruby/object:Gem::Dependency
|
79
42
|
name: rake
|
80
43
|
requirement: !ruby/object:Gem::Requirement
|
81
|
-
none: false
|
82
44
|
requirements:
|
83
45
|
- - '='
|
84
46
|
- !ruby/object:Gem::Version
|
@@ -86,7 +48,6 @@ dependencies:
|
|
86
48
|
type: :development
|
87
49
|
prerelease: false
|
88
50
|
version_requirements: !ruby/object:Gem::Requirement
|
89
|
-
none: false
|
90
51
|
requirements:
|
91
52
|
- - '='
|
92
53
|
- !ruby/object:Gem::Version
|
@@ -94,7 +55,6 @@ dependencies:
|
|
94
55
|
- !ruby/object:Gem::Dependency
|
95
56
|
name: rspec
|
96
57
|
requirement: !ruby/object:Gem::Requirement
|
97
|
-
none: false
|
98
58
|
requirements:
|
99
59
|
- - '='
|
100
60
|
- !ruby/object:Gem::Version
|
@@ -102,7 +62,6 @@ dependencies:
|
|
102
62
|
type: :development
|
103
63
|
prerelease: false
|
104
64
|
version_requirements: !ruby/object:Gem::Requirement
|
105
|
-
none: false
|
106
65
|
requirements:
|
107
66
|
- - '='
|
108
67
|
- !ruby/object:Gem::Version
|
@@ -115,7 +74,7 @@ executables: []
|
|
115
74
|
extensions: []
|
116
75
|
extra_rdoc_files: []
|
117
76
|
files:
|
118
|
-
- .gitignore
|
77
|
+
- ".gitignore"
|
119
78
|
- Gemfile
|
120
79
|
- MIT-LICENSE
|
121
80
|
- README.md
|
@@ -128,27 +87,25 @@ files:
|
|
128
87
|
- spec/spec_helper.rb
|
129
88
|
homepage: https://github.com/tagomoris/mysql2-cs-bind
|
130
89
|
licenses: []
|
131
|
-
|
90
|
+
metadata: {}
|
91
|
+
post_install_message:
|
132
92
|
rdoc_options: []
|
133
93
|
require_paths:
|
134
94
|
- lib
|
135
95
|
required_ruby_version: !ruby/object:Gem::Requirement
|
136
|
-
none: false
|
137
96
|
requirements:
|
138
|
-
- -
|
97
|
+
- - ">="
|
139
98
|
- !ruby/object:Gem::Version
|
140
|
-
version:
|
99
|
+
version: 2.0.0
|
141
100
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
142
|
-
none: false
|
143
101
|
requirements:
|
144
|
-
- -
|
102
|
+
- - ">="
|
145
103
|
- !ruby/object:Gem::Version
|
146
104
|
version: '0'
|
147
105
|
requirements: []
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
specification_version: 3
|
106
|
+
rubygems_version: 3.3.0.dev
|
107
|
+
signing_key:
|
108
|
+
specification_version: 4
|
152
109
|
summary: extension for mysql2 to add client-side variable binding
|
153
110
|
test_files:
|
154
111
|
- spec/mysql2/client_spec.rb
|