simple_query_tool 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/MIT-LICENSE +19 -0
- data/README.textile +8 -0
- data/Rakefile +17 -0
- data/VERSION +1 -0
- data/lib/simple_query_tool.rb +54 -0
- data/simple_query_tool.gemspec +50 -0
- data/test/simple_query_tool_test.rb +93 -0
- metadata +71 -0
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
pkg
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2009 Capital Thought.
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
THE SOFTWARE.
|
data/README.textile
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
h1. Simple Query Tool
|
2
|
+
|
3
|
+
This is starter code for building Presenters and other classes that generate custom SQL. It gives you a light wrapper for ActiveRecord and some useful behavior for instantiating objects. It's perfect for search interfaces.
|
4
|
+
|
5
|
+
Inspired by Jay Fields' Presenter post:
|
6
|
+
http://blog.jayfields.com/2007/03/rails-presenter-pattern.html
|
7
|
+
|
8
|
+
Copyright (c) 2009 Capital Thought, released under the MIT license.
|
data/Rakefile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
begin
|
2
|
+
require 'jeweler'
|
3
|
+
Jeweler::Tasks.new do |gemspec|
|
4
|
+
gemspec.name = "simple_query_tool"
|
5
|
+
gemspec.summary = "Starter code for building Presenters and other classes that generate custom SQL"
|
6
|
+
gemspec.description = "Including this module gives you a light wrapper for ActiveRecord and some useful behavior for instantiating objects that generate custom SQL queries, perfect for search interfaces."
|
7
|
+
gemspec.email = "mike@otherinbox.com"
|
8
|
+
gemspec.homepage = "http://github.com/otherinbox/simple_logger"
|
9
|
+
gemspec.authors = ["Mike Subelsky"]
|
10
|
+
gemspec.add_development_dependency "mocha"
|
11
|
+
gemspec.test_files = %w(test/simple_query_tool_test.rb)
|
12
|
+
end
|
13
|
+
|
14
|
+
Jeweler::GemcutterTasks.new
|
15
|
+
rescue LoadError
|
16
|
+
puts "Jeweler not available. Install it with: sudo gem install jeweler"
|
17
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
|
3
|
+
module SimpleQueryTool
|
4
|
+
|
5
|
+
# will try to default to ActiveRecord::Base components if you don't specify an adapter
|
6
|
+
attr_accessor :db_adapter
|
7
|
+
|
8
|
+
def initialize(params={})
|
9
|
+
params.each_pair { |attrib, value| instance_variable_set :"@#{attrib}", value }
|
10
|
+
@db_adapter ||= ActiveRecord::Base
|
11
|
+
super()
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def connection
|
17
|
+
db_adapter.connection
|
18
|
+
end
|
19
|
+
|
20
|
+
def select_all_with_query_cache(sql)
|
21
|
+
connection.select_all_with_query_cache(prepare(sql))
|
22
|
+
end
|
23
|
+
|
24
|
+
def select_all(sql)
|
25
|
+
connection.select_all(prepare(sql))
|
26
|
+
end
|
27
|
+
|
28
|
+
def select_one(sql)
|
29
|
+
connection.select_one(prepare(sql))
|
30
|
+
end
|
31
|
+
|
32
|
+
def select_rows(sql)
|
33
|
+
connection.select_rows(prepare(sql))
|
34
|
+
end
|
35
|
+
|
36
|
+
def select_value(sql)
|
37
|
+
connection.select_value(prepare(sql))
|
38
|
+
end
|
39
|
+
|
40
|
+
def select_values(sql)
|
41
|
+
connection.select_values(prepare(sql))
|
42
|
+
end
|
43
|
+
|
44
|
+
def quote(p)
|
45
|
+
db_adapter.quote_value(p)
|
46
|
+
end
|
47
|
+
|
48
|
+
def prepare(sql)
|
49
|
+
sql = sql.to_a
|
50
|
+
sql.unshift("SET statement_timeout = #{@statement_timeout};") if @statement_timeout
|
51
|
+
sql.join(' ')
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{simple_query_tool}
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Mike Subelsky"]
|
12
|
+
s.date = %q{2010-01-04}
|
13
|
+
s.description = %q{Including this module gives you a light wrapper for ActiveRecord and some useful behavior for instantiating objects that generate custom SQL queries, perfect for search interfaces.}
|
14
|
+
s.email = %q{mike@otherinbox.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"README.textile"
|
17
|
+
]
|
18
|
+
s.files = [
|
19
|
+
".gitignore",
|
20
|
+
"MIT-LICENSE",
|
21
|
+
"README.textile",
|
22
|
+
"Rakefile",
|
23
|
+
"VERSION",
|
24
|
+
"lib/simple_query_tool.rb",
|
25
|
+
"simple_query_tool.gemspec",
|
26
|
+
"test/simple_query_tool_test.rb"
|
27
|
+
]
|
28
|
+
s.homepage = %q{http://github.com/otherinbox/simple_logger}
|
29
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
30
|
+
s.require_paths = ["lib"]
|
31
|
+
s.rubygems_version = %q{1.3.5}
|
32
|
+
s.summary = %q{Starter code for building Presenters and other classes that generate custom SQL}
|
33
|
+
s.test_files = [
|
34
|
+
"test/simple_query_tool_test.rb"
|
35
|
+
]
|
36
|
+
|
37
|
+
if s.respond_to? :specification_version then
|
38
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
39
|
+
s.specification_version = 3
|
40
|
+
|
41
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
42
|
+
s.add_development_dependency(%q<mocha>, [">= 0"])
|
43
|
+
else
|
44
|
+
s.add_dependency(%q<mocha>, [">= 0"])
|
45
|
+
end
|
46
|
+
else
|
47
|
+
s.add_dependency(%q<mocha>, [">= 0"])
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
@@ -0,0 +1,93 @@
|
|
1
|
+
#!/usr/local/bin/ruby
|
2
|
+
require 'rubygems'
|
3
|
+
require 'test/unit'
|
4
|
+
require 'mocha'
|
5
|
+
require File.join(File.dirname(__FILE__),'..','lib','simple_query_tool')
|
6
|
+
|
7
|
+
class TestHash < Test::Unit::TestCase
|
8
|
+
|
9
|
+
class Foo
|
10
|
+
include SimpleQueryTool
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_should_set_instance_variables_on_the_fly
|
14
|
+
f = Foo.new(:a => 1, :b => 2, :c => 3,:db_connection => mock,:db_quoter => mock)
|
15
|
+
assert_equal [1,2,3], f.instance_eval { [@a,@b,@c] }
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_should_set_db_adapter_to_given_variable
|
19
|
+
adapter = mock()
|
20
|
+
f = Foo.new(:db_adapter => adapter)
|
21
|
+
assert_equal adapter,f.db_adapter
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_should_default_to_using_active_record
|
25
|
+
a = Class.new { include SimpleQueryTool }
|
26
|
+
assert_equal ActiveRecord::Base,a.new.db_adapter
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_should_pass_on_string_sql_queries
|
30
|
+
conn = mock("connection")
|
31
|
+
adapter = mock("adapter")
|
32
|
+
adapter.stubs(:connection).returns(conn)
|
33
|
+
f = Foo.new(:db_adapter => adapter)
|
34
|
+
|
35
|
+
methods = [:select_all_with_query_cache,:select_all,:select_one,:select_rows,:select_value,:select_values]
|
36
|
+
|
37
|
+
methods.each do |method|
|
38
|
+
conn.expects(method).with("SELECT * FROM messages")
|
39
|
+
end
|
40
|
+
|
41
|
+
f.instance_eval do
|
42
|
+
methods.each do |method|
|
43
|
+
send(method,"SELECT * FROM messages")
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_should_join_and_pass_on_array_sql_queries
|
49
|
+
conn = mock("connection")
|
50
|
+
adapter = mock("adapter")
|
51
|
+
adapter.stubs(:connection).returns(conn)
|
52
|
+
f = Foo.new(:db_adapter => adapter)
|
53
|
+
|
54
|
+
methods = [:select_all_with_query_cache,:select_all,:select_one,:select_rows,:select_value,:select_values]
|
55
|
+
|
56
|
+
methods.each do |method|
|
57
|
+
conn.expects(method).with("SELECT * FROM labels WHERE tag = 'Color'")
|
58
|
+
end
|
59
|
+
|
60
|
+
f.instance_eval do
|
61
|
+
methods.each do |method|
|
62
|
+
send(method,["SELECT * FROM labels","WHERE tag = 'Color'"])
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_should_quote_variables
|
68
|
+
adapter = mock("adapter")
|
69
|
+
adapter.expects(:quote_value).returns("'quote_me'")
|
70
|
+
f = Foo.new(:db_adapter => adapter)
|
71
|
+
|
72
|
+
assert_equal "'quote_me'", f.instance_eval { quote("quote_me") }
|
73
|
+
end
|
74
|
+
|
75
|
+
def test_should_prepend_statement_timeout_if_specified
|
76
|
+
conn = mock("connection")
|
77
|
+
adapter = mock("adapter")
|
78
|
+
adapter.stubs(:connection).returns(conn)
|
79
|
+
|
80
|
+
c = Class.new do
|
81
|
+
include SimpleQueryTool
|
82
|
+
end
|
83
|
+
|
84
|
+
i = c.new(:db_adapter => adapter,:statement_timeout => 1000)
|
85
|
+
|
86
|
+
conn.expects(:select_all).with("SET statement_timeout = 1000; SELECT * FROM labels WHERE tag = 'Green'")
|
87
|
+
|
88
|
+
i.instance_eval do
|
89
|
+
select_all("SELECT * FROM labels WHERE tag = 'Green'")
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
end
|
metadata
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simple_query_tool
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Mike Subelsky
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-01-04 00:00:00 -05:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: mocha
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
description: Including this module gives you a light wrapper for ActiveRecord and some useful behavior for instantiating objects that generate custom SQL queries, perfect for search interfaces.
|
26
|
+
email: mike@otherinbox.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- README.textile
|
33
|
+
files:
|
34
|
+
- .gitignore
|
35
|
+
- MIT-LICENSE
|
36
|
+
- README.textile
|
37
|
+
- Rakefile
|
38
|
+
- VERSION
|
39
|
+
- lib/simple_query_tool.rb
|
40
|
+
- simple_query_tool.gemspec
|
41
|
+
- test/simple_query_tool_test.rb
|
42
|
+
has_rdoc: true
|
43
|
+
homepage: http://github.com/otherinbox/simple_logger
|
44
|
+
licenses: []
|
45
|
+
|
46
|
+
post_install_message:
|
47
|
+
rdoc_options:
|
48
|
+
- --charset=UTF-8
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: "0"
|
56
|
+
version:
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: "0"
|
62
|
+
version:
|
63
|
+
requirements: []
|
64
|
+
|
65
|
+
rubyforge_project:
|
66
|
+
rubygems_version: 1.3.5
|
67
|
+
signing_key:
|
68
|
+
specification_version: 3
|
69
|
+
summary: Starter code for building Presenters and other classes that generate custom SQL
|
70
|
+
test_files:
|
71
|
+
- test/simple_query_tool_test.rb
|