dm-twowaysql 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.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +58 -0
- data/Rakefile +14 -0
- data/dm-twowaysql.gemspec +23 -0
- data/lib/data_mapper/twowaysql/version.rb +5 -0
- data/lib/dm-twowaysql.rb +39 -0
- data/spec/integration/twowaysql_spec.rb +80 -0
- data/spec/spec.opts +4 -0
- data/spec/spec_helper.rb +4 -0
- metadata +123 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Takumi IINO
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
# DataMapper::TwoWaySQL
|
2
|
+
|
3
|
+
DataMapper plugin providing support for [TwoWaySQL](https://github.com/twada/twowaysql).
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'dm-twowaysql'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install dm-twowaysql
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
class PersonGateway
|
22
|
+
include DataMapper::TwoWaySQL
|
23
|
+
|
24
|
+
# define twowaysql select
|
25
|
+
twowaysql :select, :person_index_page, <<-EOS
|
26
|
+
SELECT * FROM people
|
27
|
+
/*BEGIN*/WHERE
|
28
|
+
/*IF ctx[:name]*/ name = /*ctx[:name]*/'Bob' /*END*/
|
29
|
+
/*IF ctx[:job]*/ job = /*ctx[:job]*/'CLERK' /*END*/
|
30
|
+
/*IF ctx[:age]*/ AND age > /*ctx[:age]*/30 /*END*/
|
31
|
+
/*END*/
|
32
|
+
/*IF ctx[:order_by] */ ORDER BY /*$ctx[:order_by]*/id /*$ctx[:order]*/ASC /*END*/
|
33
|
+
EOS
|
34
|
+
|
35
|
+
# define twowaysql execute
|
36
|
+
twowaysql :execute, :update_all_name, <<-EOS
|
37
|
+
UPDATE people SET name = /*ctx[:name]*/'name'
|
38
|
+
EOS
|
39
|
+
end
|
40
|
+
|
41
|
+
PersonGateway.template_select_person_index_page # => TwoWaySQL::Template instance
|
42
|
+
PersonGateway.template_execute_update_all_name # => TwoWaySQL::Template instance
|
43
|
+
|
44
|
+
# merged = PersonGateway.template_select_person_index_page.merge({})
|
45
|
+
# ::DataMapper.repository(:default).adapter.select(merged.sql, *merged.bound_variables)
|
46
|
+
PersonGateway.select_person_index_page(:default, {})
|
47
|
+
|
48
|
+
# merged = PersonGateway.template_execute_update_all_name.merge({:name => 'jobs'})
|
49
|
+
# ::DataMapper.repository(:default).adapter.execute(merged.sql, *merged.bound_variables)
|
50
|
+
PersonGateway.execute_update_all_name(:default, {:name => 'jobs'})
|
51
|
+
|
52
|
+
## Contributing
|
53
|
+
|
54
|
+
1. Fork it
|
55
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
56
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
57
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
58
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
require "bundler/gem_tasks"
|
3
|
+
|
4
|
+
require 'spec/rake/spectask'
|
5
|
+
|
6
|
+
spec_defaults = lambda do |spec|
|
7
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
8
|
+
spec.libs << 'lib' << 'spec'
|
9
|
+
spec.spec_opts << '--options' << 'spec/spec.opts'
|
10
|
+
end
|
11
|
+
|
12
|
+
Spec::Rake::SpecTask.new(:spec, &spec_defaults)
|
13
|
+
|
14
|
+
task :default => :spec
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/data_mapper/twowaysql/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Takumi IINO"]
|
6
|
+
gem.email = ["trot.thunder@gmail.com"]
|
7
|
+
gem.summary = "DataMapper plugin providing support for TwoWaySQL."
|
8
|
+
gem.description = gem.summary
|
9
|
+
gem.homepage = "http://github.com/troter/dm-twowaysql"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "dm-twowaysql"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = DataMapper::TwoWaySQL::VERSION
|
17
|
+
|
18
|
+
gem.add_runtime_dependency('dm-do-adapter', '~> 1.2.0')
|
19
|
+
gem.add_runtime_dependency('twowaysql', '~> 0.5.0')
|
20
|
+
|
21
|
+
gem.add_development_dependency('rake', '~> 0.9.2')
|
22
|
+
gem.add_development_dependency('rspec', '~> 1.3.2')
|
23
|
+
end
|
data/lib/dm-twowaysql.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'dm-do-adapter'
|
2
|
+
require 'twowaysql'
|
3
|
+
|
4
|
+
module DataMapper
|
5
|
+
module TwoWaySQL
|
6
|
+
|
7
|
+
def self.included(base)
|
8
|
+
base.extend ClassMethods
|
9
|
+
super
|
10
|
+
end
|
11
|
+
|
12
|
+
module ClassMethods
|
13
|
+
|
14
|
+
# @api public
|
15
|
+
def twowaysql(execute_type, name, sql_io, opts = {})
|
16
|
+
variable_name = "@twowaysql_template_#{execute_type}_#{name}"
|
17
|
+
raise "#{variable_name} is already defined." if instance_variable_defined? variable_name
|
18
|
+
raise "#{execute_type} is not :select or :execute" unless [:select, :execute].include?(execute_type)
|
19
|
+
instance_variable_set(variable_name, ::TwoWaySQL::Template.parse(sql_io, opts))
|
20
|
+
|
21
|
+
template_getter = "self.template_#{execute_type}_#{name}"
|
22
|
+
self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
23
|
+
def #{template_getter}
|
24
|
+
instance_variable_get "#{variable_name}"
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.#{execute_type}_#{name}(repository_name, data)
|
28
|
+
merged = #{template_getter}.merge(data)
|
29
|
+
::DataMapper.repository(repository_name).adapter.#{execute_type}(merged.sql, *merged.bound_variables)
|
30
|
+
end
|
31
|
+
RUBY
|
32
|
+
|
33
|
+
instance_variable_get variable_name
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'DataMapper::TwoWaySQL' do
|
4
|
+
|
5
|
+
class ::Employee
|
6
|
+
include DataMapper::TwoWaySQL
|
7
|
+
twowaysql :select, :emp_index_page, <<-EOS
|
8
|
+
SELECT * FROM emp
|
9
|
+
/*BEGIN*/WHERE
|
10
|
+
/*IF ctx[:job]*/ job = /*ctx[:job]*/'CLERK' /*END*/
|
11
|
+
/*IF ctx[:deptno_list]*/ AND deptno IN /*ctx[:deptno_list]*/(20, 30) /*END*/
|
12
|
+
/*IF ctx[:age]*/ AND age > /*ctx[:age]*/30 /*END*/
|
13
|
+
/*END*/
|
14
|
+
/*IF ctx[:order_by] */ ORDER BY /*$ctx[:order_by]*/id /*$ctx[:order]*/ASC /*END*/
|
15
|
+
EOS
|
16
|
+
end
|
17
|
+
|
18
|
+
class ::Mushroom
|
19
|
+
include DataMapper::TwoWaySQL
|
20
|
+
twowaysql :select, :by_name, <<-EOS
|
21
|
+
SELECT * FROM mushrooms
|
22
|
+
/*BEGIN*/WHERE
|
23
|
+
/*IF ctx[:name]*/ name = /*ctx[:name]*/'CLERK' /*END*/
|
24
|
+
/*END*/
|
25
|
+
EOS
|
26
|
+
twowaysql :execute, :update_all_name, <<-EOS
|
27
|
+
UPDATE mushrooms SET
|
28
|
+
name = /*ctx[:name]*/''
|
29
|
+
EOS
|
30
|
+
end
|
31
|
+
|
32
|
+
describe "#twowaysql" do
|
33
|
+
it "should define tempalte_\#{execute_type}_\#{name} method" do
|
34
|
+
Employee.respond_to?(:template_select_emp_index_page).should be_true
|
35
|
+
Mushroom.respond_to?(:template_select_by_name).should be_true
|
36
|
+
Mushroom.respond_to?(:template_execute_update_all_name).should be_true
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should define select_\#{name} method" do
|
40
|
+
Employee.respond_to?(:select_emp_index_page).should be_true
|
41
|
+
Mushroom.respond_to?(:select_by_name).should be_true
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should define execute_\#{name} method" do
|
45
|
+
Mushroom.respond_to?(:execute_update_all_name).should be_true
|
46
|
+
end
|
47
|
+
|
48
|
+
it "raise error when passed duplicate name" do
|
49
|
+
lambda do
|
50
|
+
class ::Mushroom
|
51
|
+
include DataMapper::TwoWaySQL
|
52
|
+
twowaysql :select, :by_name, "select * from mashrooms"
|
53
|
+
end
|
54
|
+
end.should raise_error
|
55
|
+
end
|
56
|
+
|
57
|
+
it "raise error when passed invalid execute_type" do
|
58
|
+
lambda do
|
59
|
+
class ::Mushroom
|
60
|
+
include DataMapper::TwoWaySQL
|
61
|
+
twowaysql :selecta, :all, "select * from mashrooms"
|
62
|
+
end
|
63
|
+
end.should raise_error
|
64
|
+
end
|
65
|
+
|
66
|
+
describe "#template_\#{execute_type}_\#{name}" do
|
67
|
+
it "should return object instance_of ::TwoWaySQL::Template" do
|
68
|
+
Employee.template_select_emp_index_page.should be_instance_of(::TwoWaySQL::Template)
|
69
|
+
Mushroom.template_select_by_name.should be_instance_of(::TwoWaySQL::Template)
|
70
|
+
Mushroom.template_execute_update_all_name.should be_instance_of(::TwoWaySQL::Template)
|
71
|
+
end
|
72
|
+
|
73
|
+
it "should return " do
|
74
|
+
Employee.template_select_emp_index_page.merge({}).sql.should eql(" SELECT * FROM emp\n \n \n")
|
75
|
+
Mushroom.template_select_by_name.merge({}).sql.should eql(" SELECT * FROM mushrooms\n \n")
|
76
|
+
Mushroom.template_execute_update_all_name.merge({:name => 'shiitake'}).sql.should eql(" UPDATE mushrooms SET\n name = ?\n")
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
data/spec/spec.opts
ADDED
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,123 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dm-twowaysql
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Takumi IINO
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-09-22 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: dm-do-adapter
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 1.2.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 1.2.0
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: twowaysql
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 0.5.0
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 0.5.0
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rake
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 0.9.2
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.9.2
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rspec
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ~>
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: 1.3.2
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ~>
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: 1.3.2
|
78
|
+
description: DataMapper plugin providing support for TwoWaySQL.
|
79
|
+
email:
|
80
|
+
- trot.thunder@gmail.com
|
81
|
+
executables: []
|
82
|
+
extensions: []
|
83
|
+
extra_rdoc_files: []
|
84
|
+
files:
|
85
|
+
- .gitignore
|
86
|
+
- Gemfile
|
87
|
+
- LICENSE
|
88
|
+
- README.md
|
89
|
+
- Rakefile
|
90
|
+
- dm-twowaysql.gemspec
|
91
|
+
- lib/data_mapper/twowaysql/version.rb
|
92
|
+
- lib/dm-twowaysql.rb
|
93
|
+
- spec/integration/twowaysql_spec.rb
|
94
|
+
- spec/spec.opts
|
95
|
+
- spec/spec_helper.rb
|
96
|
+
homepage: http://github.com/troter/dm-twowaysql
|
97
|
+
licenses: []
|
98
|
+
post_install_message:
|
99
|
+
rdoc_options: []
|
100
|
+
require_paths:
|
101
|
+
- lib
|
102
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
103
|
+
none: false
|
104
|
+
requirements:
|
105
|
+
- - ! '>='
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
109
|
+
none: false
|
110
|
+
requirements:
|
111
|
+
- - ! '>='
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
version: '0'
|
114
|
+
requirements: []
|
115
|
+
rubyforge_project:
|
116
|
+
rubygems_version: 1.8.24
|
117
|
+
signing_key:
|
118
|
+
specification_version: 3
|
119
|
+
summary: DataMapper plugin providing support for TwoWaySQL.
|
120
|
+
test_files:
|
121
|
+
- spec/integration/twowaysql_spec.rb
|
122
|
+
- spec/spec.opts
|
123
|
+
- spec/spec_helper.rb
|