call_sp 0.0.2
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/lib/call_sp/stored_procedure.rb +57 -0
- data/lib/call_sp/version.rb +3 -0
- data/lib/call_sp.rb +26 -0
- metadata +81 -0
@@ -0,0 +1,57 @@
|
|
1
|
+
class StoredProcedure
|
2
|
+
|
3
|
+
#def self.method_missing(method, params = [], options = {})
|
4
|
+
# self.call_proc(method, params, options)
|
5
|
+
#end
|
6
|
+
|
7
|
+
def self.call_proc(proc_name, params = [], options = {})
|
8
|
+
params.delete_if { |p| p.nil? }
|
9
|
+
|
10
|
+
select = options[:select].present? ? options[:select].map {|k, v| "#{k} AS #{v}" }.join(', ') : '*'
|
11
|
+
schema = options[:schema] || 'public'
|
12
|
+
conditions = options[:conditions] || []
|
13
|
+
where = options[:conditions].present? ? " WHERE #{options[:conditions].first} " : ''
|
14
|
+
order = options[:order].present? ? " ORDER BY #{options[:order]} " : ''
|
15
|
+
|
16
|
+
sql = "SELECT #{select} FROM #{schema}.#{proc_name}(#{Array.new(params.count) { '?' }.join(', ')})#{where}#{order}"
|
17
|
+
|
18
|
+
conditions.shift
|
19
|
+
params.push(*conditions)
|
20
|
+
|
21
|
+
mode = options[:mode] || :fetch_sp
|
22
|
+
case mode
|
23
|
+
when :fetch_sp_val
|
24
|
+
self.fetch_sp_val(sql, *params)
|
25
|
+
when :execute_sp
|
26
|
+
self.execute_sp(sql, *params)
|
27
|
+
when :fetch_sp
|
28
|
+
self.fetch_sp(sql, *params)
|
29
|
+
else
|
30
|
+
raise 'Undefined mode'
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.execute_sp(sql, *bindings)
|
36
|
+
perform_sp(:execute, sql, *bindings)
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.fetch_sp(sql, *bindings)
|
40
|
+
perform_sp(:select_all, sql, *bindings)
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.fetch_sp_val(sql, *bindings)
|
44
|
+
perform_sp(:select_value, sql, *bindings)
|
45
|
+
end
|
46
|
+
|
47
|
+
protected
|
48
|
+
def self.perform_sp(method, sql, *bindings)
|
49
|
+
if bindings.any?
|
50
|
+
sql = ActiveRecord::Base.send(:sanitize_sql_array, bindings.unshift(sql))
|
51
|
+
end
|
52
|
+
ActiveRecord::Base.connection.send(method, sql)
|
53
|
+
end
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
end
|
data/lib/call_sp.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require "call_sp/version"
|
2
|
+
require "call_sp/stored_procedure"
|
3
|
+
|
4
|
+
module CallSp
|
5
|
+
|
6
|
+
def self.included(base)
|
7
|
+
base.extend ClassMethods
|
8
|
+
end
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
|
12
|
+
def procedure(name, options = {})
|
13
|
+
|
14
|
+
proc_name = options[:as] || name
|
15
|
+
|
16
|
+
self.class_eval(<<-EOM, __FILE__, __LINE__ + 1)
|
17
|
+
def self.#{name}(params = [], options = {})
|
18
|
+
options = #{options}.merge!(options)
|
19
|
+
StoredProcedure.call_proc("#{proc_name}", params, options)
|
20
|
+
end
|
21
|
+
EOM
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: call_sp
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Igor Gonchar
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-04-03 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
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
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
description: Use this gem to add functionality to run stored procedures as functions
|
47
|
+
email:
|
48
|
+
- gigorok@gmail.com
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- lib/call_sp.rb
|
54
|
+
- lib/call_sp/version.rb
|
55
|
+
- lib/call_sp/stored_procedure.rb
|
56
|
+
homepage: http://rubygems.org/gems/call_sp
|
57
|
+
licenses:
|
58
|
+
- MIT
|
59
|
+
post_install_message:
|
60
|
+
rdoc_options: []
|
61
|
+
require_paths:
|
62
|
+
- lib
|
63
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
64
|
+
none: false
|
65
|
+
requirements:
|
66
|
+
- - ! '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
|
+
none: false
|
71
|
+
requirements:
|
72
|
+
- - ! '>='
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
requirements: []
|
76
|
+
rubyforge_project:
|
77
|
+
rubygems_version: 1.8.25
|
78
|
+
signing_key:
|
79
|
+
specification_version: 3
|
80
|
+
summary: A stored procedures wrapper
|
81
|
+
test_files: []
|