epoxy 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +30 -0
- data/Rakefile +56 -0
- data/VERSION +1 -0
- data/lib/epoxy.rb +90 -0
- data/test/helper.rb +10 -0
- data/test/test_epoxy.rb +56 -0
- metadata +72 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Erik Hollensbe
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
= Epoxy - bind data to queries for any query language.
|
2
|
+
|
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'"
|
9
|
+
|
10
|
+
Epoxy handles:
|
11
|
+
|
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
|
+
|
18
|
+
== Note on Patches/Pull Requests
|
19
|
+
|
20
|
+
* Fork the project.
|
21
|
+
* Make your feature addition or bug fix.
|
22
|
+
* Add tests for it. This is important so I don't break it in a
|
23
|
+
future version unintentionally.
|
24
|
+
* Commit, do not mess with rakefile, version, or history.
|
25
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
26
|
+
* Send me a pull request. Bonus points for topic branches.
|
27
|
+
|
28
|
+
== Copyright
|
29
|
+
|
30
|
+
Copyright (c) 2010 Erik Hollensbe. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "epoxy"
|
8
|
+
gem.summary = %Q{A binding API for query languages that does not depend on any specific database.}
|
9
|
+
gem.description = %Q{Parse binds in SQL or any other data query language, quote, even configure for client-side binding. It all works!}
|
10
|
+
gem.email = "erik@hollensbe.org"
|
11
|
+
gem.homepage = "http://github.com/erikh/epoxy"
|
12
|
+
gem.authors = ["Erik Hollensbe"]
|
13
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
14
|
+
end
|
15
|
+
Jeweler::GemcutterTasks.new
|
16
|
+
rescue LoadError
|
17
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
18
|
+
end
|
19
|
+
|
20
|
+
require 'rake/testtask'
|
21
|
+
Rake::TestTask.new(:test) do |test|
|
22
|
+
test.libs << 'lib' << 'test'
|
23
|
+
test.pattern = 'test/**/test_*.rb'
|
24
|
+
test.verbose = true
|
25
|
+
end
|
26
|
+
|
27
|
+
begin
|
28
|
+
require 'rcov/rcovtask'
|
29
|
+
Rcov::RcovTask.new do |test|
|
30
|
+
test.libs << 'test'
|
31
|
+
test.pattern = 'test/**/test_*.rb'
|
32
|
+
test.verbose = true
|
33
|
+
end
|
34
|
+
rescue LoadError
|
35
|
+
task :rcov do
|
36
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
task :test => :check_dependencies
|
41
|
+
|
42
|
+
task :default => :test
|
43
|
+
|
44
|
+
task :to_blog => [:clobber_rdoc, :rdoc] do
|
45
|
+
sh "rm -fr $git/blog/content/docs/epoxy && mv rdoc $git/blog/content/docs/epoxy"
|
46
|
+
end
|
47
|
+
|
48
|
+
require 'rake/rdoctask'
|
49
|
+
Rake::RDocTask.new do |rdoc|
|
50
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
51
|
+
|
52
|
+
rdoc.rdoc_dir = 'rdoc'
|
53
|
+
rdoc.title = "epoxy #{version}"
|
54
|
+
rdoc.rdoc_files.include('README*')
|
55
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
56
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/lib/epoxy.rb
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
# = Epoxy - bind data to queries for any query language.
|
2
|
+
#
|
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'"
|
9
|
+
#
|
10
|
+
# Epoxy handles:
|
11
|
+
#
|
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
|
+
#
|
18
|
+
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)
|
26
|
+
query.scan(%r{
|
27
|
+
(
|
28
|
+
-- .* (?# matches "--" style comments to the end of line or string )
|
29
|
+
| - (?# matches single "-" )
|
30
|
+
|
|
31
|
+
/[*] .*? [*]/ (?# matches C-style comments )
|
32
|
+
| / (?# matches single slash )
|
33
|
+
|
|
34
|
+
' ( [^'\\] | '' | \\. )* ' (?# match strings surrounded by apostophes )
|
35
|
+
|
|
36
|
+
" ( [^"\\] | "" | \\. )* " (?# match strings surrounded by " )
|
37
|
+
|
|
38
|
+
\?\?? (?# match one or two question marks )
|
39
|
+
|
|
40
|
+
[^-/'"?]+ (?# match all characters except ' " ? - and / )
|
41
|
+
|
42
|
+
)
|
43
|
+
}x).collect(&:first)
|
44
|
+
end
|
45
|
+
|
46
|
+
# tokens generated by Epoxy.parse_tokens. Just use Epoxy#quote for now.
|
47
|
+
attr_reader :tokens
|
48
|
+
# the original query, before quoting.
|
49
|
+
attr_reader :query
|
50
|
+
|
51
|
+
#
|
52
|
+
# Takes a query as a string. The binding rules are as follows:
|
53
|
+
#
|
54
|
+
# * ? for numbered binds (named binds coming soon!)
|
55
|
+
# * ?? for a *real* question mark
|
56
|
+
# * '?' for a *real* question mark
|
57
|
+
# * comments, weird quoting styles are unaffected.
|
58
|
+
#
|
59
|
+
def initialize(query)
|
60
|
+
@query = query
|
61
|
+
@tokens = self.class.parse_tokens(query)
|
62
|
+
end
|
63
|
+
|
64
|
+
#
|
65
|
+
# Processes your query for quoting. Provide a block that emulates how your
|
66
|
+
# data should be quoted, and it will yield on each successive bound element
|
67
|
+
# with the index of that element passed.
|
68
|
+
#
|
69
|
+
# *You* are responsible for quoting your data properly. Epoxy just makes it
|
70
|
+
# easier to get the places you need to quote out of the query.
|
71
|
+
#
|
72
|
+
def quote(&block)
|
73
|
+
result = ""
|
74
|
+
bind_pos = 0
|
75
|
+
|
76
|
+
tokens.each do |part|
|
77
|
+
case part
|
78
|
+
when '?'
|
79
|
+
result << block.call(bind_pos)
|
80
|
+
bind_pos += 1
|
81
|
+
when '??'
|
82
|
+
result << "?"
|
83
|
+
else
|
84
|
+
result << part
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
return result
|
89
|
+
end
|
90
|
+
end
|
data/test/helper.rb
ADDED
data/test/test_epoxy.rb
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
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)
|
8
|
+
|
9
|
+
assert_equal("select * from foo where bar='foo'", ep.quote { |x| "'foo'" })
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_02_double_question
|
13
|
+
ep = Epoxy.new("select ?? from foo where bar=?")
|
14
|
+
assert_equal("select ? from foo where bar='foo'", ep.quote { |x| "'foo'" })
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_03_quotes_already
|
18
|
+
ep = Epoxy.new("select * from \"foo\" where bar=?")
|
19
|
+
assert_equal("select * from \"foo\" where bar='foo'", ep.quote { |x| "'foo'" })
|
20
|
+
|
21
|
+
ep = Epoxy.new("select * from 'foo' where bar=?")
|
22
|
+
assert_equal("select * from 'foo' where bar='foo'", ep.quote { |x| "'foo'" })
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_04_holy_shit
|
26
|
+
ep = Epoxy.new("select * from \"'foo'\" where bar=?")
|
27
|
+
assert_equal("select * from \"'foo'\" where bar='foo'", ep.quote { |x| "'foo'" })
|
28
|
+
|
29
|
+
ep = Epoxy.new("select * from E\"'foo'\" where bar=?")
|
30
|
+
assert_equal("select * from E\"'foo'\" where bar='foo'", ep.quote { |x| "'foo'" })
|
31
|
+
|
32
|
+
ep = Epoxy.new("select * from E\"''foo''\" where bar=?")
|
33
|
+
assert_equal("select * from E\"''foo''\" where bar='foo'", ep.quote { |x| "'foo'" })
|
34
|
+
|
35
|
+
ep = Epoxy.new("select * from E\"\\''foo''\" where bar=?")
|
36
|
+
assert_equal("select * from E\"\\''foo''\" where bar='foo'", ep.quote { |x| "'foo'" })
|
37
|
+
|
38
|
+
ep = Epoxy.new("select * from E\"\\''foo\\''\" where bar=?")
|
39
|
+
assert_equal("select * from E\"\\''foo\\''\" where bar='foo'", ep.quote { |x| "'foo'" })
|
40
|
+
|
41
|
+
ep = Epoxy.new("select * from foo where bar='?'")
|
42
|
+
assert_equal("select * from foo where bar='?'", ep.quote { |x| "'foo'" })
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_05_comments
|
46
|
+
ep = Epoxy.new(%Q{
|
47
|
+
-- a comment!
|
48
|
+
select * from foo where bar=?
|
49
|
+
}.strip)
|
50
|
+
|
51
|
+
assert_equal(%Q{
|
52
|
+
-- a comment!
|
53
|
+
select * from foo where bar='foo'
|
54
|
+
}.strip, ep.quote { |x| "'foo'" })
|
55
|
+
end
|
56
|
+
end
|
metadata
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: epoxy
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Erik Hollensbe
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-05-27 00:00:00 -04:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: Parse binds in SQL or any other data query language, quote, even configure for client-side binding. It all works!
|
22
|
+
email: erik@hollensbe.org
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files:
|
28
|
+
- LICENSE
|
29
|
+
- README.rdoc
|
30
|
+
files:
|
31
|
+
- .document
|
32
|
+
- .gitignore
|
33
|
+
- LICENSE
|
34
|
+
- README.rdoc
|
35
|
+
- Rakefile
|
36
|
+
- VERSION
|
37
|
+
- lib/epoxy.rb
|
38
|
+
- test/helper.rb
|
39
|
+
- test/test_epoxy.rb
|
40
|
+
has_rdoc: true
|
41
|
+
homepage: http://github.com/erikh/epoxy
|
42
|
+
licenses: []
|
43
|
+
|
44
|
+
post_install_message:
|
45
|
+
rdoc_options:
|
46
|
+
- --charset=UTF-8
|
47
|
+
require_paths:
|
48
|
+
- lib
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
segments:
|
54
|
+
- 0
|
55
|
+
version: "0"
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
segments:
|
61
|
+
- 0
|
62
|
+
version: "0"
|
63
|
+
requirements: []
|
64
|
+
|
65
|
+
rubyforge_project:
|
66
|
+
rubygems_version: 1.3.6
|
67
|
+
signing_key:
|
68
|
+
specification_version: 3
|
69
|
+
summary: A binding API for query languages that does not depend on any specific database.
|
70
|
+
test_files:
|
71
|
+
- test/helper.rb
|
72
|
+
- test/test_epoxy.rb
|