mysql2-cs-bind 0.0.3 → 0.1.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4407a9571990644923605f3e94e689cfa2f58a2b0ae65751df47fe504c9f87cc
4
+ data.tar.gz: 9e7f64a7e17c636b9da92d3a8849468b35519b7bf301bcda722e7664b0f39346
5
+ SHA512:
6
+ metadata.gz: 1635039201253b1d8b48251fef0e5acf02ea4ac8e379af39bf417b0f1ab83f1b9f2cfe4d5587006e3c8ca5b8dd817e19978698182778e45865ae620d02f8d7f4
7
+ data.tar.gz: 1dfec3139736b9de9d6bb61a76a53fcf72f2909ea16f79c6620d99ad55de045288cc6557cf04c70ecd6e48a662a4cc5154e74dd31e6cd0ab86992ba921619707
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
- # mysql-cs-bind
1
+ # mysql2-cs-bind
2
2
 
3
- 'mysql-cs-bind' is extension of 'mysql2', to add method of client-side variable binding (pseudo prepared statement).
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
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'mysql2'
2
4
 
3
5
  class Mysql2::Client
@@ -11,7 +13,7 @@ class Mysql2::Client
11
13
  if args.size < 1
12
14
  query(sql, options)
13
15
  else
14
- query(Mysql2::Client.pseudo_bind(sql, args.flatten), options)
16
+ query(Mysql2::Client.pseudo_bind(sql, args), options)
15
17
  end
16
18
  end
17
19
 
@@ -24,21 +26,33 @@ class Mysql2::Client
24
26
  placeholders.push(pos)
25
27
  search_pos = pos + 1
26
28
  end
29
+ values = values.flatten(1) if placeholders.length == values.flatten(1).length
27
30
  raise ArgumentError, "mismatch between placeholders number and values arguments" if placeholders.length != values.length
28
31
 
29
32
  while pos = placeholders.pop()
30
33
  rawvalue = values.pop()
31
- if rawvalue.nil?
32
- sql[pos] = 'NULL'
33
- elsif rawvalue.is_a?(Time)
34
- sql[pos] = "'" + rawvalue.strftime('%Y-%m-%d %H:%M:%S') + "'"
35
- elsif rawvalue.is_a?(Array)
36
- sql[pos] = rawvalue.map{|v| "'" + Mysql2::Client.escape(v.to_s) + "'" }.join(",")
34
+ if rawvalue.is_a?(Array)
35
+ sql[pos] = rawvalue.map{|v| quote(v) }.join(",")
37
36
  else
38
- sql[pos] = "'" + Mysql2::Client.escape(rawvalue.to_s) + "'"
37
+ sql[pos] = quote(rawvalue)
39
38
  end
40
39
  end
41
40
  sql
42
41
  end
43
42
 
43
+ private
44
+
45
+ def self.quote(rawvalue)
46
+ if rawvalue.nil?
47
+ 'NULL'
48
+ elsif rawvalue == true
49
+ 'TRUE'
50
+ elsif rawvalue == false
51
+ 'FALSE'
52
+ elsif 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
44
58
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |gem|
4
4
  gem.name = "mysql2-cs-bind"
5
- gem.version = "0.0.3"
5
+ gem.version = "0.1.0"
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,10 +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.add_runtime_dependency "mysql2"
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
22
  gem.add_development_dependency 'eventmachine'
22
23
  gem.add_development_dependency 'rake-compiler', "~> 0.7.7"
23
24
  gem.add_development_dependency 'rake', '0.8.7' # NB: 0.8.7 required by rake-compiler 0.7.9
@@ -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
@@ -41,14 +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')")
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')")
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)")
52
62
  end
53
63
  end
54
64
 
@@ -80,6 +90,28 @@ describe Mysql2::Client do
80
90
  @client.xquery("SELECT ?+1", 1).first.should eql([2.0])
81
91
  end
82
92
 
93
+ it "should read multi style args" do
94
+ @client.xquery("SELECT id FROM (SELECT 1 AS id) AS tbl WHERE id IN (1)").first["id"].should eql(1)
95
+
96
+ @client.xquery("SELECT id FROM (SELECT 1 AS id) AS tbl WHERE id = ?", 1).first["id"].should eql(1)
97
+ @client.xquery("SELECT id FROM (SELECT 1 AS id) AS tbl WHERE id = ? OR id = ?", 1, 2).first["id"].should eql(1)
98
+ @client.xquery("SELECT id FROM (SELECT 1 AS id) AS tbl WHERE id = ? OR id = ?", [1,2]).first["id"].should eql(1)
99
+
100
+ @client.xquery("SELECT id FROM (SELECT 1 AS id) AS tbl WHERE id in (?)", [1,2,3]).first["id"].should eql(1)
101
+ @client.xquery("SELECT id FROM (SELECT 1 AS id) AS tbl WHERE id = ? OR id in (?)", 1, [2,3]).first["id"].should eql(1)
102
+ @client.xquery("SELECT id FROM (SELECT 1 AS id) AS tbl WHERE id = ? OR id in (?)", [1, [2,3]]).first["id"].should eql(1)
103
+ @client.xquery("SELECT id FROM (SELECT 1 AS id) AS tbl WHERE id in (?) OR id = ?", [1,2], 3).first["id"].should eql(1)
104
+ @client.xquery("SELECT id FROM (SELECT 1 AS id) AS tbl WHERE id in (?) OR id = ?", [[1,2], 3]).first["id"].should eql(1)
105
+
106
+ @client.xquery("SELECT id FROM (SELECT 1 AS id) AS tbl WHERE id IN (1)",:something => :else).first["id"].should eql(1)
107
+ @client.xquery("SELECT id FROM (SELECT 1 AS id) AS tbl WHERE id = ? OR id in (?)", 1, [2,3],:something => :else).first["id"].should eql(1)
108
+ @client.xquery("SELECT id FROM (SELECT 1 AS id) AS tbl WHERE id in (?) OR id = ?", [[1,2], 3],:something => :else).first["id"].should eql(1)
109
+
110
+ expect {
111
+ @client.xquery("SELECT id FROM (SELECT 1 AS id) AS tbl WHERE id in (?) OR id = ?", [[1,2], 3, 4],:something => :else)
112
+ }.should raise_exception(ArgumentError)
113
+ end
114
+
83
115
  it "should be able to return results with symbolized keys" do
84
116
  @client.xquery("SELECT 1", :symbolize_keys => true).first.keys[0].class.should eql(Symbol)
85
117
  end
metadata CHANGED
@@ -1,84 +1,60 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mysql2-cs-bind
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
5
- prerelease:
4
+ version: 0.1.0
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: 2012-07-17 00:00:00.000000000 Z
11
+ date: 2020-09-15 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
- - - ! '>='
24
+ - - ">="
44
25
  - !ruby/object:Gem::Version
45
26
  version: '0'
46
27
  - !ruby/object:Gem::Dependency
47
28
  name: eventmachine
48
29
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
30
  requirements:
51
- - - ! '>='
31
+ - - ">="
52
32
  - !ruby/object:Gem::Version
53
33
  version: '0'
54
34
  type: :development
55
35
  prerelease: false
56
36
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
37
  requirements:
59
- - - ! '>='
38
+ - - ">="
60
39
  - !ruby/object:Gem::Version
61
40
  version: '0'
62
41
  - !ruby/object:Gem::Dependency
63
42
  name: rake-compiler
64
43
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
44
  requirements:
67
- - - ~>
45
+ - - "~>"
68
46
  - !ruby/object:Gem::Version
69
47
  version: 0.7.7
70
48
  type: :development
71
49
  prerelease: false
72
50
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
51
  requirements:
75
- - - ~>
52
+ - - "~>"
76
53
  - !ruby/object:Gem::Version
77
54
  version: 0.7.7
78
55
  - !ruby/object:Gem::Dependency
79
56
  name: rake
80
57
  requirement: !ruby/object:Gem::Requirement
81
- none: false
82
58
  requirements:
83
59
  - - '='
84
60
  - !ruby/object:Gem::Version
@@ -86,7 +62,6 @@ dependencies:
86
62
  type: :development
87
63
  prerelease: false
88
64
  version_requirements: !ruby/object:Gem::Requirement
89
- none: false
90
65
  requirements:
91
66
  - - '='
92
67
  - !ruby/object:Gem::Version
@@ -94,7 +69,6 @@ dependencies:
94
69
  - !ruby/object:Gem::Dependency
95
70
  name: rspec
96
71
  requirement: !ruby/object:Gem::Requirement
97
- none: false
98
72
  requirements:
99
73
  - - '='
100
74
  - !ruby/object:Gem::Version
@@ -102,7 +76,6 @@ dependencies:
102
76
  type: :development
103
77
  prerelease: false
104
78
  version_requirements: !ruby/object:Gem::Requirement
105
- none: false
106
79
  requirements:
107
80
  - - '='
108
81
  - !ruby/object:Gem::Version
@@ -115,7 +88,7 @@ executables: []
115
88
  extensions: []
116
89
  extra_rdoc_files: []
117
90
  files:
118
- - .gitignore
91
+ - ".gitignore"
119
92
  - Gemfile
120
93
  - MIT-LICENSE
121
94
  - README.md
@@ -128,27 +101,25 @@ files:
128
101
  - spec/spec_helper.rb
129
102
  homepage: https://github.com/tagomoris/mysql2-cs-bind
130
103
  licenses: []
131
- post_install_message:
104
+ metadata: {}
105
+ post_install_message:
132
106
  rdoc_options: []
133
107
  require_paths:
134
108
  - lib
135
109
  required_ruby_version: !ruby/object:Gem::Requirement
136
- none: false
137
110
  requirements:
138
- - - ! '>='
111
+ - - ">="
139
112
  - !ruby/object:Gem::Version
140
- version: '0'
113
+ version: 2.0.0
141
114
  required_rubygems_version: !ruby/object:Gem::Requirement
142
- none: false
143
115
  requirements:
144
- - - ! '>='
116
+ - - ">="
145
117
  - !ruby/object:Gem::Version
146
118
  version: '0'
147
119
  requirements: []
148
- rubyforge_project:
149
- rubygems_version: 1.8.21
150
- signing_key:
151
- specification_version: 3
120
+ rubygems_version: 3.1.2
121
+ signing_key:
122
+ specification_version: 4
152
123
  summary: extension for mysql2 to add client-side variable binding
153
124
  test_files:
154
125
  - spec/mysql2/client_spec.rb