call_sp 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in call_sp.gemspec
4
+ gemspec
5
+
6
+ gem 'pg'
7
+ gem 'activerecord'
8
+
9
+ group :development do
10
+ gem 'rspec'
11
+ end
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Igor Gonchar
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.
@@ -0,0 +1,67 @@
1
+ # CallSp
2
+
3
+ A stored procedures wrapper.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'call_sp', '~> 0.0.3'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install call_sp
18
+
19
+ ## Usage
20
+
21
+ Suppose your stored procedures are
22
+
23
+ CREATE OR REPLACE FUNCTION say_my_name(_name text) RETURNS TEXT AS $$
24
+ BEGIN
25
+ RETURN _name;
26
+ END;
27
+ $$ LANGUAGE plpgsql;
28
+
29
+ CREATE OR REPLACE FUNCTION sp.show_names() RETURNS TABLE (id integer, name varchar, age numeric) AS $$
30
+ BEGIN
31
+ RETURN QUERY SELECT * FROM "public"."names";
32
+ END;
33
+ $$ LANGUAGE plpgsql;
34
+
35
+ CREATE OR REPLACE FUNCTION sp.drop_teenagers() RETURNS VOID AS $$
36
+ BEGIN
37
+ DELETE FROM "public"."names" WHERE age < 20;
38
+ END;
39
+ $$ LANGUAGE plpgsql;
40
+
41
+
42
+ Just include CallSp module into your own class and will describe the stored procedures
43
+
44
+ class SpClass
45
+ include ::CallSp
46
+
47
+ procedure :my_name, { as: :say_my_name, mode: :fetch_sp_val }
48
+ procedure :show_names, { schema: :sp, mode: :fetch_sp }
49
+ procedure :drop_teenagers, { schema: :sp, mode: :execute_sp }
50
+ end
51
+
52
+ After that module will create a methods my_name, show_names, drop_teenagers and you can call them with parameters of stored procedure
53
+
54
+ SpClass.my_name(['John'])
55
+ SpClass.show_names([], { conditions: ["age > ?", 20], order: "age DESC" }).each do |row|
56
+ p row
57
+ end
58
+ SpClass.drop_teenagers()
59
+
60
+
61
+ ## Contributing
62
+
63
+ 1. Fork it ( http://github.com/gigorok/call_sp/fork )
64
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
65
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
66
+ 4. Push to the branch (`git push origin my-new-feature`)
67
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,22 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'call_sp/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "call_sp"
8
+ spec.version = CallSp::VERSION
9
+ spec.authors = ["Igor Gonchar"]
10
+ spec.email = ["gigorok@gmail.com"]
11
+ spec.summary = %q{"ActiveRecord extensions for PostgreSQL."}
12
+ spec.description = %q{"ActiveRecord extensions for PostgreSQL. Provides useful tools for working with stored procedures."}
13
+ spec.homepage = "http://github.com/gigorok/call_sp"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency(%q<rspec-rails>, [">= 0"])
22
+ end
@@ -1,9 +1,5 @@
1
1
  class StoredProcedure
2
2
 
3
- #def self.method_missing(method, params = [], options = {})
4
- # self.call_proc(method, params, options)
5
- #end
6
-
7
3
  def self.call_proc(proc_name, params = [], options = {})
8
4
  params.delete_if { |p| p.nil? }
9
5
 
@@ -1,3 +1,3 @@
1
1
  module CallSp
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -0,0 +1,97 @@
1
+ require 'spec_helper'
2
+
3
+ describe CallSp do
4
+
5
+ before(:all) {
6
+ conn = YAML.load(File.read("#{File.expand_path('../', __FILE__)}/database.yml"))
7
+ ActiveRecord::Base.establish_connection conn[ENV["RAILS_ENV"]]
8
+
9
+ ActiveRecord::Base.connection.execute File.read("#{File.expand_path('../', __FILE__)}/sql/up.sql")
10
+ }
11
+
12
+ after(:all) {
13
+ conn = YAML.load(File.read("#{File.expand_path('../', __FILE__)}/database.yml"))
14
+ ActiveRecord::Base.establish_connection conn[ENV["RAILS_ENV"]]
15
+
16
+ ActiveRecord::Base.connection.execute File.read("#{File.expand_path('../', __FILE__)}/sql/down.sql")
17
+ }
18
+
19
+ it 'fetch one' do
20
+ SpClass.my_name(['John']).should == 'John'
21
+ SpClass.my_name(['John Doe']).should == 'John Doe'
22
+ end
23
+
24
+ it 'fetch many' do
25
+
26
+ collection = [
27
+ {"id"=>"1", "name"=>"John", "age"=>"20"},
28
+ {"id"=>"2", "name"=>"Nick", "age"=>"22"},
29
+ {"id"=>"3", "name"=>"Kate", "age"=>"17"},
30
+ {"id"=>"4", "name"=>"Bill", "age"=>"30"}
31
+ ]
32
+
33
+ i = 0
34
+ SpClass.show_names().each do |row|
35
+ row["id"].should == collection.at(i)["id"]
36
+ row["name"].should == collection.at(i)["name"]
37
+ row["age"].should == collection.at(i)["age"]
38
+
39
+ i += 1
40
+ end
41
+
42
+
43
+ end
44
+
45
+ it 'fetch many with conditions' do
46
+ collection2 = [
47
+ {"id"=>"2", "name"=>"Nick", "age"=>"22"},
48
+ {"id"=>"4", "name"=>"Bill", "age"=>"30"}
49
+ ]
50
+
51
+ i = 0
52
+ SpClass.show_names([], { conditions: ["age > ?", 20] }).each do |row|
53
+ row["id"].should == collection2.at(i)["id"]
54
+ row["name"].should == collection2.at(i)["name"]
55
+ row["age"].should == collection2.at(i)["age"]
56
+
57
+ i += 1
58
+ end
59
+ end
60
+
61
+ it 'fetch many with conditions and order' do
62
+ collection2 = [
63
+ {"id"=>"4", "name"=>"Bill", "age"=>"30"},
64
+ {"id"=>"2", "name"=>"Nick", "age"=>"22"}
65
+ ]
66
+
67
+ i = 0
68
+ SpClass.show_names([], { conditions: ["age > ?", 20], order: "age DESC" }).each do |row|
69
+ row["id"].should == collection2.at(i)["id"]
70
+ row["name"].should == collection2.at(i)["name"]
71
+ row["age"].should == collection2.at(i)["age"]
72
+
73
+ i += 1
74
+ end
75
+ end
76
+
77
+ it 'execute' do
78
+
79
+ collection = [
80
+ {"id"=>"1", "name"=>"John", "age"=>"20"},
81
+ {"id"=>"2", "name"=>"Nick", "age"=>"22"},
82
+ {"id"=>"4", "name"=>"Bill", "age"=>"30"}
83
+ ]
84
+
85
+ SpClass.drop_teenagers()
86
+
87
+ i = 0
88
+ SpClass.show_names().each do |row|
89
+ row["id"].should == collection.at(i)["id"]
90
+ row["name"].should == collection.at(i)["name"]
91
+ row["age"].should == collection.at(i)["age"]
92
+
93
+ i += 1
94
+ end
95
+ end
96
+
97
+ end
@@ -0,0 +1,12 @@
1
+ development: &default
2
+ adapter: postgresql
3
+ encoding: unicode
4
+ database: call_cp_development
5
+ pool: 5
6
+ username: postgres
7
+ password: 1111
8
+ host: 127.0.0.1
9
+
10
+ test:
11
+ <<: *default
12
+ database: call_cp_test
@@ -0,0 +1,8 @@
1
+ ENV["RAILS_ENV"] ||= 'test'
2
+
3
+ require 'rspec'
4
+ require 'rspec/autorun'
5
+ require 'call_sp'
6
+ require 'yaml'
7
+ require 'active_record'
8
+ require 'support/sp_class'
@@ -0,0 +1,5 @@
1
+ DROP FUNCTION say_my_name(_name text);
2
+ DROP FUNCTION sp.show_names();
3
+ DROP FUNCTION sp.drop_teenagers();
4
+ DROP SCHEMA "sp";
5
+ DROP TABLE "public"."names";
@@ -0,0 +1,31 @@
1
+ CREATE OR REPLACE FUNCTION say_my_name(_name text) RETURNS TEXT AS $$
2
+ BEGIN
3
+ RETURN _name;
4
+ END;
5
+ $$ LANGUAGE plpgsql;
6
+
7
+ CREATE TABLE "public"."names" (
8
+ "id" int4 NOT NULL,
9
+ "name" varchar NOT NULL,
10
+ "age" numeric NOT NULL
11
+ );
12
+
13
+ INSERT INTO "public"."names"(id, name, age) values
14
+ (1, 'John', 20),
15
+ (2, 'Nick', 22),
16
+ (3, 'Kate', 17),
17
+ (4, 'Bill', 30);
18
+
19
+ CREATE SCHEMA "sp";
20
+
21
+ CREATE OR REPLACE FUNCTION sp.show_names() RETURNS TABLE (id integer, name varchar, age numeric) AS $$
22
+ BEGIN
23
+ RETURN QUERY SELECT * FROM "public"."names";
24
+ END;
25
+ $$ LANGUAGE plpgsql;
26
+
27
+ CREATE OR REPLACE FUNCTION sp.drop_teenagers() RETURNS VOID AS $$
28
+ BEGIN
29
+ DELETE FROM "public"."names" WHERE age < 20;
30
+ END;
31
+ $$ LANGUAGE plpgsql;
@@ -0,0 +1,7 @@
1
+ class SpClass
2
+ include ::CallSp
3
+
4
+ procedure :my_name, { as: :say_my_name, mode: :fetch_sp_val }
5
+ procedure :show_names, { schema: :sp, mode: :fetch_sp }
6
+ procedure :drop_teenagers, { schema: :sp, mode: :execute_sp }
7
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: call_sp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,26 +9,10 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-04-03 00:00:00.000000000 Z
12
+ date: 2014-04-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: bundler
16
- requirement: !ruby/object:Gem::Requirement
17
- none: false
18
- requirements:
19
- - - ~>
20
- - !ruby/object:Gem::Version
21
- version: '1.5'
22
- type: :development
23
- prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
- requirements:
27
- - - ~>
28
- - !ruby/object:Gem::Version
29
- version: '1.5'
30
- - !ruby/object:Gem::Dependency
31
- name: rake
15
+ name: rspec-rails
32
16
  requirement: !ruby/object:Gem::Requirement
33
17
  none: false
34
18
  requirements:
@@ -43,17 +27,29 @@ dependencies:
43
27
  - - ! '>='
44
28
  - !ruby/object:Gem::Version
45
29
  version: '0'
46
- description: Use this gem to add functionality to run stored procedures as functions
30
+ description: ! '"ActiveRecord extensions for PostgreSQL. Provides useful tools for
31
+ working with stored procedures."'
47
32
  email:
48
33
  - gigorok@gmail.com
49
34
  executables: []
50
35
  extensions: []
51
36
  extra_rdoc_files: []
52
37
  files:
38
+ - Gemfile
39
+ - LICENSE.txt
40
+ - README.md
41
+ - Rakefile
42
+ - call_sp.gemspec
53
43
  - lib/call_sp.rb
54
- - lib/call_sp/version.rb
55
44
  - lib/call_sp/stored_procedure.rb
56
- homepage: http://rubygems.org/gems/call_sp
45
+ - lib/call_sp/version.rb
46
+ - spec/call_sp_spec.rb
47
+ - spec/database.yml
48
+ - spec/spec_helper.rb
49
+ - spec/sql/down.sql
50
+ - spec/sql/up.sql
51
+ - spec/support/sp_class.rb
52
+ homepage: http://github.com/gigorok/call_sp
57
53
  licenses:
58
54
  - MIT
59
55
  post_install_message:
@@ -77,5 +73,11 @@ rubyforge_project:
77
73
  rubygems_version: 1.8.25
78
74
  signing_key:
79
75
  specification_version: 3
80
- summary: A stored procedures wrapper
81
- test_files: []
76
+ summary: ! '"ActiveRecord extensions for PostgreSQL."'
77
+ test_files:
78
+ - spec/call_sp_spec.rb
79
+ - spec/database.yml
80
+ - spec/spec_helper.rb
81
+ - spec/sql/down.sql
82
+ - spec/sql/up.sql
83
+ - spec/support/sp_class.rb