epoxy 0.2.1 → 0.3.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.
Files changed (7) hide show
  1. data/README.rdoc +23 -9
  2. data/Rakefile +4 -1
  3. data/VERSION +1 -1
  4. data/lib/epoxy.rb +104 -72
  5. data/test/helper.rb +2 -0
  6. data/test/test_epoxy.rb +156 -90
  7. metadata +26 -5
@@ -1,19 +1,33 @@
1
- = Epoxy - bind data to queries for any query language.
2
-
3
- Let me hit ya with some science!
1
+ = Epoxy - bind data to queries for any query language.
2
+
3
+ Let me hit ya with some science!
4
4
 
5
+ # numbered binds
5
6
  ep = Epoxy.new("select * from foo where bar=?")
6
7
  binds = %W[foo]
7
8
  bound_query = ep.quote { |x| "'" + binds[x] + "'" }
8
9
  "select * from foo where bar='foo'"
9
10
 
10
- Epoxy handles:
11
+ # named binds
12
+ binds = { :name => 'Lee', :age => 132 }
13
+ ep = Epoxy.new("select * from people where name=?name and age=?age")
14
+ bound_query = ep.quote(binds) { |x| "'#{binds[x]}'" }
15
+ "select * from people where name='Lee' and age='132'"
11
16
 
12
- * ? for numbered binds (named binds coming soon!)
13
- * ?? for a *real* question mark
14
- * '?' for a *real* question mark
15
- * comments, weird quoting styles (look at the "holy shit" test for examples)
16
- * not telling you how to quote your data. This solution works for any query language and any database.
17
+ # mix them!
18
+ binds = { 0 => "Age", :name => 'Lee' }
19
+ ep = Epoxy.new("select * from people where name=?name and age=?")
20
+ bound_query = ep.quote(binds) { |x| "'#{binds[x]}'" }
21
+ "select * from people where name='Lee' and age='Age'"
22
+
23
+ Epoxy handles:
24
+
25
+ * ?<name> for named binds
26
+ * ? for numbered binds
27
+ * ?? for a *real* question mark
28
+ * '?' for a *real* question mark
29
+ * comments, weird quoting styles (look at the "holy shit" test for examples)
30
+ * not telling you how to quote your data. This solution works for any query language and any database.
17
31
 
18
32
  == Note on Patches/Pull Requests
19
33
 
data/Rakefile CHANGED
@@ -11,6 +11,7 @@ begin
11
11
  gem.homepage = "http://github.com/erikh/epoxy"
12
12
  gem.authors = ["Erik Hollensbe"]
13
13
  gem.add_development_dependency 'rdoc'
14
+ gem.add_development_dependency 'test-unit'
14
15
  # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
16
  end
16
17
  Jeweler::GemcutterTasks.new
@@ -43,7 +44,7 @@ task :test => :check_dependencies
43
44
  task :default => :test
44
45
 
45
46
  task :to_blog => [:clobber_rdoc, :rdoc] do
46
- sh "rm -fr $git/blog/content/docs/epoxy && mv rdoc $git/blog/content/docs/epoxy"
47
+ sh "rm -fr $git/blog/content/docs/epoxy && mv rdoc $git/blog/content/docs/epoxy"
47
48
  end
48
49
 
49
50
  require 'rdoc/task'
@@ -56,3 +57,5 @@ RDoc::Task.new do |rdoc|
56
57
  rdoc.rdoc_files.include('README*')
57
58
  rdoc.rdoc_files.include('lib/**/*.rb')
58
59
  end
60
+
61
+ # vim: syntax=ruby ts=2 et sw=2 sts=2
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.1
1
+ 0.3.0
@@ -1,92 +1,124 @@
1
1
  # = Epoxy - bind data to queries for any query language.
2
2
  #
3
3
  # Let me hit ya with some science!
4
- #
5
- # ep = Epoxy.new("select * from foo where bar=?")
6
- # binds = %W[foo]
7
- # bound_query = ep.quote { |x| "'" + binds[x] + "'" }
8
- # "select * from foo where bar='foo'"
4
+ #
5
+ # # numbered binds
6
+ # ep = Epoxy.new("select * from foo where bar=?")
7
+ # binds = %W[foo]
8
+ # bound_query = ep.quote { |x| "'" + binds[x] + "'" }
9
+ # "select * from foo where bar='foo'"
10
+ #
11
+ # # named binds
12
+ # binds = { :name => 'Lee', :age => 132 }
13
+ # ep = Epoxy.new("select * from people where name=?name and age=?age")
14
+ # bound_query = ep.quote(binds) { |x| "'#{binds[x]}'" }
15
+ # "select * from people where name='Lee' and age='132'"
16
+ #
17
+ # # mix them!
18
+ # binds = { 0 => "Age", :name => 'Lee' }
19
+ # ep = Epoxy.new("select * from people where name=?name and age=?")
20
+ # bound_query = ep.quote(binds) { |x| "'#{binds[x]}'" }
21
+ # "select * from people where name='Lee' and age='Age'"
9
22
  #
10
23
  # Epoxy handles:
11
24
  #
12
- # * ? for numbered binds (named binds coming soon!)
25
+ # * ?<name> for named binds
26
+ # * ? for numbered binds
13
27
  # * ?? for a *real* question mark
14
28
  # * '?' for a *real* question mark
15
29
  # * comments, weird quoting styles (look at the "holy shit" test for examples)
16
30
  # * not telling you how to quote your data. This solution works for any query language and any database.
17
31
  #
18
32
  class Epoxy
19
- #
20
- # Token parser, isolates components of the query into parts to where they
21
- # can be managed indepdently.
22
- #
23
- # Probably not the easiest thing to deal with by itself. Use the standard
24
- # methods plox.
25
- def self.parse_tokens(query, comment_chars)
26
- query.scan(%r{
27
- (
28
- #{comment_chars}.* (?# matches "--" style comments to the end of line or string )
29
- |
30
- ' ( [^'\\] | '' | \\. )* ' (?# match strings surrounded by apostophes )
31
- |
32
- " ( [^"\\] | "" | \\. )* " (?# match strings surrounded by " )
33
- |
34
- ['"] (?# match a loose quote )
35
- |
36
- \?\?? (?# match one or two question marks )
37
- |
38
- [^-/'"?]+ (?# match all characters except ' " ? - and / )
39
-
40
- )
41
- }x).collect(&:first)
42
- end
33
+ #
34
+ # Token parser, isolates components of the query into parts to where they
35
+ # can be managed indepdently.
36
+ #
37
+ # Probably not the easiest thing to deal with by itself. Use the standard
38
+ # methods plox.
39
+ def self.parse_tokens(query, comment_chars)
40
+ a = query.scan(%r{
41
+ (
42
+ #{comment_chars}.* (?# matches "--" style comments to the end of line or string )
43
+ |
44
+ ' ( [^'\\] | '' | \\. )* ' (?# match strings surrounded by apostophes )
45
+ |
46
+ " ( [^"\\] | "" | \\. )* " (?# match strings surrounded by " )
47
+ |
48
+ ['"] (?# match a loose quote )
49
+ |
50
+ \?[a-zA-Z]+ (?# match a named bind )
51
+ |
52
+ \?\?? (?# match one or two question marks )
53
+ |
54
+ [^-/'"?:]+ (?# match all characters except ' " ? - : and / )
55
+ )
56
+ }x).collect(&:first)
57
+ end
43
58
 
44
- # tokens generated by Epoxy.parse_tokens. Just use Epoxy#quote for now.
45
- attr_reader :tokens
46
- # the original query, before quoting.
47
- attr_reader :query
48
- # leader comment characters - defaults to SQL "--"
49
- attr_reader :comment_chars
59
+ # tokens generated by Epoxy.parse_tokens. Just use Epoxy#quote for now.
60
+ attr_reader :tokens
61
+ # the original query, before quoting.
62
+ attr_reader :query
63
+ # leader comment characters - defaults to SQL "--"
64
+ attr_reader :comment_chars
50
65
 
51
- #
52
- # Takes a query as a string and an optional regexp defining
53
- # beginning-of-line comments. The binding rules are as follows:
54
- #
55
- # * ? for numbered binds (named binds coming soon!)
56
- # * ?? for a *real* question mark
57
- # * '?' for a *real* question mark
58
- # * comments, weird quoting styles are unaffected.
59
- #
60
- def initialize(query, comment_chars=%r{--|//})
61
- @comment_chars = comment_chars
62
- @query = query
63
- @tokens = self.class.parse_tokens(query, @comment_chars)
64
- end
66
+ #
67
+ # Takes a query as a string and an optional regexp defining
68
+ # beginning-of-line comments. The binding rules are as follows:
69
+ #
70
+ # * ?<name> for named binds
71
+ # * ? for numbered binds
72
+ # * ?? for a *real* question mark
73
+ # * '?' for a *real* question mark
74
+ # * comments, weird quoting styles are unaffected.
75
+ #
76
+ def initialize(query, comment_chars=%r{--|//})
77
+ @comment_chars = comment_chars
78
+ @query = query
79
+ @tokens = self.class.parse_tokens(query, @comment_chars)
80
+ end
65
81
 
66
- #
67
- # Processes your query for quoting. Provide a block that emulates how your
68
- # data should be quoted, and it will yield on each successive bound element
69
- # with the index of that element passed.
70
- #
71
- # *You* are responsible for quoting your data properly. Epoxy just makes it
72
- # easier to get the places you need to quote out of the query.
73
- #
74
- def quote(&block)
75
- result = ""
76
- bind_pos = 0
82
+ #
83
+ # Processes your query for quoting. Provide a block that emulates how your
84
+ # data should be quoted. This method accepts a Hash to process named bindings,
85
+ # which when provided will yield each successive Hash key which has a match
86
+ # in the named binds. <b>Keys are coerced to symbols before being yielded.</b>
87
+ #
88
+ # Without a Hash it will yield on each successive bound element
89
+ # with the index of that element passed.
90
+ #
91
+ # *You* are responsible for quoting your data properly. Epoxy just makes it
92
+ # easier to get the places you need to quote out of the query.
93
+ #
94
+ def quote(binds = {}, &block)
95
+ result = ""
96
+ bind_pos = 0
77
97
 
78
- tokens.each do |part|
79
- case part
80
- when '?'
81
- result << block.call(bind_pos)
82
- bind_pos += 1
83
- when '??'
84
- result << "?"
85
- else
86
- result << part
87
- end
98
+ unless binds.empty?
99
+ tokens.each do |token|
100
+ binds.each do |key, rep|
101
+ if token == "?#{key}"
102
+ token.replace block.call(key.to_sym)
103
+ end
88
104
  end
105
+ end
106
+ end
89
107
 
90
- return result
108
+ tokens.each do |part|
109
+ case part
110
+ when '?'
111
+ result << block.call(bind_pos)
112
+ bind_pos += 1
113
+ when '??'
114
+ result << "?"
115
+ else
116
+ result << part
117
+ end
91
118
  end
119
+
120
+ return result
121
+ end
92
122
  end
123
+
124
+ # vim: syntax=ruby ts=2 et sw=2 sts=2
@@ -8,3 +8,5 @@ require 'epoxy'
8
8
 
9
9
  class Test::Unit::TestCase
10
10
  end
11
+
12
+ # vim: syntax=ruby ts=2 et sw=2 sts=2
@@ -1,142 +1,208 @@
1
1
  require 'helper'
2
2
 
3
3
  class TestEpoxy < Test::Unit::TestCase
4
- def test_01_basic
5
- ep = Epoxy.new("select * from foo where bar=?")
6
- assert(ep)
7
- assert_kind_of(Epoxy, ep)
4
+ def test_01_basic
5
+ ep = Epoxy.new("select * from foo where bar=?")
6
+ assert(ep)
7
+ assert_kind_of(Epoxy, ep)
8
8
 
9
- assert_equal("select * from foo where bar='foo'", ep.quote { |x| "'foo'" })
10
- end
9
+ assert_equal("select * from foo where bar='foo'", ep.quote { |x| "'foo'" })
10
+ end
11
11
 
12
- def test_02_literal_question_mark
13
- ep = Epoxy.new("select ?? from foo where bar=?")
14
- assert_equal("select ? from foo where bar='foo'", ep.quote { |x| "'foo'" })
12
+ def test_02_literal_question_mark
13
+ ep = Epoxy.new("select ?? from foo where bar=?")
14
+ assert_equal("select ? from foo where bar='foo'", ep.quote { |x| "'foo'" })
15
15
 
16
- ep = Epoxy.new("select ??? from foo where bar=?")
17
- assert_equal("select ?'foo' from foo where bar='foo'", ep.quote { |x| "'foo'" })
16
+ ep = Epoxy.new("select ??? from foo where bar=?")
17
+ assert_equal("select ?'foo' from foo where bar='foo'", ep.quote { |x| "'foo'" })
18
18
 
19
- ep = Epoxy.new("select ???? from foo where bar=?")
20
- assert_equal("select ?? from foo where bar='foo'", ep.quote { |x| "'foo'" })
19
+ ep = Epoxy.new("select ???? from foo where bar=?")
20
+ assert_equal("select ?? from foo where bar='foo'", ep.quote { |x| "'foo'" })
21
21
 
22
- ep = Epoxy.new("select ????? from foo where bar=?")
23
- assert_equal("select ??'foo' from foo where bar='foo'", ep.quote { |x| "'foo'" })
22
+ ep = Epoxy.new("select ????? from foo where bar=?")
23
+ assert_equal("select ??'foo' from foo where bar='foo'", ep.quote { |x| "'foo'" })
24
24
 
25
- ep = Epoxy.new("select '?' from foo where bar=?")
26
- assert_equal("select '?' from foo where bar='foo'", ep.quote { |x| "'foo'" })
25
+ ep = Epoxy.new("select '?' from foo where bar=?")
26
+ assert_equal("select '?' from foo where bar='foo'", ep.quote { |x| "'foo'" })
27
27
 
28
- ep = Epoxy.new("select '?'? from foo where bar=?")
29
- assert_equal("select '?''foo' from foo where bar='foo'", ep.quote { |x| "'foo'" })
28
+ ep = Epoxy.new("select '?'? from foo where bar=?")
29
+ assert_equal("select '?''foo' from foo where bar='foo'", ep.quote { |x| "'foo'" })
30
30
 
31
- ep = Epoxy.new("select '?''?' from foo where bar=?")
32
- assert_equal("select '?''?' from foo where bar='foo'", ep.quote { |x| "'foo'" })
31
+ ep = Epoxy.new("select '?''?' from foo where bar=?")
32
+ assert_equal("select '?''?' from foo where bar='foo'", ep.quote { |x| "'foo'" })
33
33
 
34
- ep = Epoxy.new("select ?'?' from foo where bar=?")
35
- assert_equal("select 'foo''?' from foo where bar='foo'", ep.quote { |x| "'foo'" })
34
+ ep = Epoxy.new("select ?'?' from foo where bar=?")
35
+ assert_equal("select 'foo''?' from foo where bar='foo'", ep.quote { |x| "'foo'" })
36
36
 
37
- ep = Epoxy.new("select ?'?'? from foo where bar=?")
38
- assert_equal("select 'foo''?''foo' from foo where bar='foo'", ep.quote { |x| "'foo'" })
37
+ ep = Epoxy.new("select ?'?'? from foo where bar=?")
38
+ assert_equal("select 'foo''?''foo' from foo where bar='foo'", ep.quote { |x| "'foo'" })
39
39
 
40
- ep = Epoxy.new("select '?'?'? from foo where bar=?")
41
- assert_equal("select '?''foo'''foo' from foo where bar='foo'", ep.quote { |x| "'foo'" })
40
+ ep = Epoxy.new("select '?'?'? from foo where bar=?")
41
+ assert_equal("select '?''foo'''foo' from foo where bar='foo'", ep.quote { |x| "'foo'" })
42
42
 
43
- ep = Epoxy.new("select '?'?'?' from foo where bar=?")
44
- assert_equal("select '?''foo''?' from foo where bar='foo'", ep.quote { |x| "'foo'" })
43
+ ep = Epoxy.new("select '?'?'?' from foo where bar=?")
44
+ assert_equal("select '?''foo''?' from foo where bar='foo'", ep.quote { |x| "'foo'" })
45
45
 
46
- ep = Epoxy.new("select '?'?? from foo where bar=?")
47
- assert_equal("select '?'? from foo where bar='foo'", ep.quote { |x| "'foo'" })
46
+ ep = Epoxy.new("select '?'?? from foo where bar=?")
47
+ assert_equal("select '?'? from foo where bar='foo'", ep.quote { |x| "'foo'" })
48
48
 
49
- ep = Epoxy.new("select '?'??? from foo where bar=?")
50
- assert_equal("select '?'?'foo' from foo where bar='foo'", ep.quote { |x| "'foo'" })
49
+ ep = Epoxy.new("select '?'??? from foo where bar=?")
50
+ assert_equal("select '?'?'foo' from foo where bar='foo'", ep.quote { |x| "'foo'" })
51
51
 
52
- ep = Epoxy.new("select ??'?'??? from foo where bar=?")
53
- assert_equal("select ?'?'?'foo' from foo where bar='foo'", ep.quote { |x| "'foo'" })
52
+ ep = Epoxy.new("select ??'?'??? from foo where bar=?")
53
+ assert_equal("select ?'?'?'foo' from foo where bar='foo'", ep.quote { |x| "'foo'" })
54
54
 
55
- ep = Epoxy.new("select ???'?'??? from foo where bar=?")
56
- assert_equal("select ?'foo''?'?'foo' from foo where bar='foo'", ep.quote { |x| "'foo'" })
55
+ ep = Epoxy.new("select ???'?'??? from foo where bar=?")
56
+ assert_equal("select ?'foo''?'?'foo' from foo where bar='foo'", ep.quote { |x| "'foo'" })
57
57
 
58
- ep = Epoxy.new("select ''? from foo where bar=?")
59
- assert_equal("select '''foo' from foo where bar='foo'", ep.quote { |x| "'foo'" })
58
+ ep = Epoxy.new("select ''? from foo where bar=?")
59
+ assert_equal("select '''foo' from foo where bar='foo'", ep.quote { |x| "'foo'" })
60
60
 
61
- ep = Epoxy.new("select '''? from foo where bar=?")
62
- assert_equal("select ''''foo' from foo where bar='foo'", ep.quote { |x| "'foo'" })
61
+ ep = Epoxy.new("select '''? from foo where bar=?")
62
+ assert_equal("select ''''foo' from foo where bar='foo'", ep.quote { |x| "'foo'" })
63
63
 
64
- ep = Epoxy.new("select ''?' from foo where bar=?")
65
- assert_equal("select '''foo'' from foo where bar='foo'", ep.quote { |x| "'foo'" })
64
+ ep = Epoxy.new("select ''?' from foo where bar=?")
65
+ assert_equal("select '''foo'' from foo where bar='foo'", ep.quote { |x| "'foo'" })
66
66
 
67
- ep = Epoxy.new("select ''?'' from foo where bar=?")
68
- assert_equal("select '''foo''' from foo where bar='foo'", ep.quote { |x| "'foo'" })
67
+ ep = Epoxy.new("select ''?'' from foo where bar=?")
68
+ assert_equal("select '''foo''' from foo where bar='foo'", ep.quote { |x| "'foo'" })
69
69
 
70
- ep = Epoxy.new("select ''??' from foo where bar=?")
71
- assert_equal("select ''?' from foo where bar='foo'", ep.quote { |x| "'foo'" })
70
+ ep = Epoxy.new("select ''??' from foo where bar=?")
71
+ assert_equal("select ''?' from foo where bar='foo'", ep.quote { |x| "'foo'" })
72
72
 
73
- ep = Epoxy.new("select '?''?' from foo where bar=?")
74
- assert_equal("select '?''?' from foo where bar='foo'", ep.quote { |x| "'foo'" })
73
+ ep = Epoxy.new("select '?''?' from foo where bar=?")
74
+ assert_equal("select '?''?' from foo where bar='foo'", ep.quote { |x| "'foo'" })
75
75
 
76
- ep = Epoxy.new("select '?'''?' from foo where bar=?")
77
- assert_equal("select '?''''foo'' from foo where bar='foo'", ep.quote { |x| "'foo'" })
76
+ ep = Epoxy.new("select '?'''?' from foo where bar=?")
77
+ assert_equal("select '?''''foo'' from foo where bar='foo'", ep.quote { |x| "'foo'" })
78
78
 
79
- ep = Epoxy.new("select '?''''?' from foo where bar=?")
80
- assert_equal("select '?''''?' from foo where bar='foo'", ep.quote { |x| "'foo'" })
79
+ ep = Epoxy.new("select '?''''?' from foo where bar=?")
80
+ assert_equal("select '?''''?' from foo where bar='foo'", ep.quote { |x| "'foo'" })
81
81
 
82
- ep = Epoxy.new("select '??' from foo where bar=?")
83
- assert_equal("select '??' from foo where bar='foo'", ep.quote { |x| "'foo'" })
82
+ ep = Epoxy.new("select '??' from foo where bar=?")
83
+ assert_equal("select '??' from foo where bar='foo'", ep.quote { |x| "'foo'" })
84
84
 
85
- ep = Epoxy.new("select '???' from foo where bar=?")
86
- assert_equal("select '???' from foo where bar='foo'", ep.quote { |x| "'foo'" })
85
+ ep = Epoxy.new("select '???' from foo where bar=?")
86
+ assert_equal("select '???' from foo where bar='foo'", ep.quote { |x| "'foo'" })
87
87
 
88
- ep = Epoxy.new("select ''???' from foo where bar=?")
89
- assert_equal("select ''?'foo'' from foo where bar='foo'", ep.quote { |x| "'foo'" })
90
- end
88
+ ep = Epoxy.new("select ''???' from foo where bar=?")
89
+ assert_equal("select ''?'foo'' from foo where bar='foo'", ep.quote { |x| "'foo'" })
90
+ end
91
91
 
92
- def test_03_quotes_already
93
- ep = Epoxy.new("select * from \"foo\" where bar=?")
94
- assert_equal("select * from \"foo\" where bar='foo'", ep.quote { |x| "'foo'" })
92
+ def test_03_quotes_already
93
+ ep = Epoxy.new("select * from \"foo\" where bar=?")
94
+ assert_equal("select * from \"foo\" where bar='foo'", ep.quote { |x| "'foo'" })
95
95
 
96
- ep = Epoxy.new("select * from 'foo' where bar=?")
97
- assert_equal("select * from 'foo' where bar='foo'", ep.quote { |x| "'foo'" })
98
- end
96
+ ep = Epoxy.new("select * from 'foo' where bar=?")
97
+ assert_equal("select * from 'foo' where bar='foo'", ep.quote { |x| "'foo'" })
98
+ end
99
99
 
100
- def test_04_holy_shit
101
- ep = Epoxy.new("select * from \"'foo'\" where bar=?")
102
- assert_equal("select * from \"'foo'\" where bar='foo'", ep.quote { |x| "'foo'" })
100
+ def test_04_holy_shit
101
+ ep = Epoxy.new("select * from \"'foo'\" where bar=?")
102
+ assert_equal("select * from \"'foo'\" where bar='foo'", ep.quote { |x| "'foo'" })
103
103
 
104
- ep = Epoxy.new("select * from E\"'foo'\" where bar=?")
105
- assert_equal("select * from E\"'foo'\" where bar='foo'", ep.quote { |x| "'foo'" })
104
+ ep = Epoxy.new("select * from E\"'foo'\" where bar=?")
105
+ assert_equal("select * from E\"'foo'\" where bar='foo'", ep.quote { |x| "'foo'" })
106
106
 
107
- ep = Epoxy.new("select * from E\"''foo''\" where bar=?")
108
- assert_equal("select * from E\"''foo''\" where bar='foo'", ep.quote { |x| "'foo'" })
107
+ ep = Epoxy.new("select * from E\"''foo''\" where bar=?")
108
+ assert_equal("select * from E\"''foo''\" where bar='foo'", ep.quote { |x| "'foo'" })
109
109
 
110
- ep = Epoxy.new("select * from E\"\\''foo''\" where bar=?")
111
- assert_equal("select * from E\"\\''foo''\" where bar='foo'", ep.quote { |x| "'foo'" })
110
+ ep = Epoxy.new("select * from E\"\\''foo''\" where bar=?")
111
+ assert_equal("select * from E\"\\''foo''\" where bar='foo'", ep.quote { |x| "'foo'" })
112
112
 
113
- ep = Epoxy.new("select * from E\"\\''foo\\''\" where bar=?")
114
- assert_equal("select * from E\"\\''foo\\''\" where bar='foo'", ep.quote { |x| "'foo'" })
113
+ ep = Epoxy.new("select * from E\"\\''foo\\''\" where bar=?")
114
+ assert_equal("select * from E\"\\''foo\\''\" where bar='foo'", ep.quote { |x| "'foo'" })
115
115
 
116
- ep = Epoxy.new("select * from foo where bar='?'")
117
- assert_equal("select * from foo where bar='?'", ep.quote { |x| "'foo'" })
118
- end
116
+ ep = Epoxy.new("select * from foo where bar='?'")
117
+ assert_equal("select * from foo where bar='?'", ep.quote { |x| "'foo'" })
118
+ end
119
119
 
120
- def test_05_comments
121
- ep = Epoxy.new(%Q{
122
- -- a comment!
120
+ def test_05_comments
121
+ ep = Epoxy.new(%Q{
122
+ -- a comment?!
123
123
  select * from foo where bar=?
124
124
  }.strip)
125
125
 
126
- assert_equal(%Q{
127
- -- a comment!
126
+ assert_equal(%Q{
127
+ -- a comment?!
128
128
  select * from foo where bar='foo'
129
129
  }.strip, ep.quote { |x| "'foo'" })
130
-
131
- ep = Epoxy.new(%Q{
130
+
131
+ ep = Epoxy.new(%Q{
132
+ // a comment?!
133
+ select * from foo where bar=?
134
+ }.strip)
135
+
136
+ assert_equal(%Q{
137
+ // a comment?!
138
+ select * from foo where bar='foo'
139
+ }.strip, ep.quote { |x| "'foo'" })
140
+
141
+ ep = Epoxy.new(%Q{
132
142
  # a comment!
133
143
  select * from foo where bar=?
134
144
  }.strip, %r{#})
135
-
136
- assert_equal(%Q{
145
+
146
+ assert_equal(%Q{
137
147
  # a comment!
138
148
  select * from foo where bar='foo'
139
149
  }.strip, ep.quote { |x| "'foo'" })
140
150
 
141
- end
151
+ end
152
+
153
+ def test_06_named_binds
154
+ binds = { 0 => "test", :foo => 'bar', "bar" => 'baz', "void" => 'unused' }
155
+ yarrr = proc { |x| "'#{binds[x] || binds[x.to_s]}'" }
156
+
157
+ ep = Epoxy.new("select * from 'foo' where bar=?foo and baz=?bar")
158
+ assert_equal(
159
+ "select * from 'foo' where bar='bar' and baz='baz'",
160
+ ep.quote(binds, &yarrr)
161
+ )
162
+
163
+ ep = Epoxy.new("select * from 'foo' where bar='foo ?bar' and baz=?bar")
164
+ assert_equal(
165
+ "select * from 'foo' where bar='foo ?bar' and baz='baz'",
166
+ ep.quote(binds, &yarrr)
167
+ )
168
+
169
+ ep = Epoxy.new("select * from 'foo' where bar=?foo and baz=?")
170
+ assert_equal(
171
+ "select * from 'foo' where bar='bar' and baz='test'",
172
+ ep.quote(binds, &yarrr)
173
+ )
174
+
175
+ ep = Epoxy.new("select * from 'foo' where bar='?foo' and baz='?baz'")
176
+ assert_equal(
177
+ "select * from 'foo' where bar='?foo' and baz='?baz'",
178
+ ep.quote(binds, &yarrr)
179
+ )
180
+
181
+ ep = Epoxy.new("select * from 'foo' where bar=?foo and baz=?foo")
182
+ assert_equal(
183
+ "select * from 'foo' where bar='bar' and baz='bar'",
184
+ ep.quote(binds, &yarrr)
185
+ )
186
+
187
+ ep = Epoxy.new("select * from 'foo' where bar=??")
188
+ assert_equal(
189
+ "select * from 'foo' where bar=?",
190
+ ep.quote(binds, &yarrr)
191
+ )
192
+
193
+ ep = Epoxy.new("select * from 'foo' where bar=??bar")
194
+ assert_equal(
195
+ "select * from 'foo' where bar=?bar",
196
+ ep.quote(binds, &yarrr)
197
+ )
198
+
199
+ ep = Epoxy.new("select * from 'foo' where bar=?notfound")
200
+ assert_equal(
201
+ "select * from 'foo' where bar=?notfound",
202
+ ep.quote(binds, &yarrr)
203
+ )
204
+ end
205
+
142
206
  end
207
+
208
+ # vim: syntax=ruby ts=2 et sw=2 sts=2
metadata CHANGED
@@ -1,12 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: epoxy
3
3
  version: !ruby/object:Gem::Version
4
+ hash: 19
4
5
  prerelease: false
5
6
  segments:
6
7
  - 0
7
- - 2
8
- - 1
9
- version: 0.2.1
8
+ - 3
9
+ - 0
10
+ version: 0.3.0
10
11
  platform: ruby
11
12
  authors:
12
13
  - Erik Hollensbe
@@ -14,21 +15,37 @@ autorequire:
14
15
  bindir: bin
15
16
  cert_chain: []
16
17
 
17
- date: 2010-05-27 00:00:00 -04:00
18
+ date: 2010-07-12 00:00:00 -04:00
18
19
  default_executable:
19
20
  dependencies:
20
21
  - !ruby/object:Gem::Dependency
21
22
  name: rdoc
22
23
  prerelease: false
23
24
  requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
24
26
  requirements:
25
27
  - - ">="
26
28
  - !ruby/object:Gem::Version
29
+ hash: 3
27
30
  segments:
28
31
  - 0
29
32
  version: "0"
30
33
  type: :development
31
34
  version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: test-unit
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ type: :development
48
+ version_requirements: *id002
32
49
  description: Parse binds in SQL or any other data query language, quote, even configure for client-side binding. It all works!
33
50
  email: erik@hollensbe.org
34
51
  executables: []
@@ -58,23 +75,27 @@ rdoc_options:
58
75
  require_paths:
59
76
  - lib
60
77
  required_ruby_version: !ruby/object:Gem::Requirement
78
+ none: false
61
79
  requirements:
62
80
  - - ">="
63
81
  - !ruby/object:Gem::Version
82
+ hash: 3
64
83
  segments:
65
84
  - 0
66
85
  version: "0"
67
86
  required_rubygems_version: !ruby/object:Gem::Requirement
87
+ none: false
68
88
  requirements:
69
89
  - - ">="
70
90
  - !ruby/object:Gem::Version
91
+ hash: 3
71
92
  segments:
72
93
  - 0
73
94
  version: "0"
74
95
  requirements: []
75
96
 
76
97
  rubyforge_project:
77
- rubygems_version: 1.3.6
98
+ rubygems_version: 1.3.7
78
99
  signing_key:
79
100
  specification_version: 3
80
101
  summary: A binding API for query languages that does not depend on any specific database.