activerecord-jdbc-plsql 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +16 -0
- data/activerecord-jdbc-plsql.gemspec +0 -1
- data/lib/activerecord/jdbc/plsql.rb +111 -100
- data/lib/activerecord/jdbc/plsql/version.rb +1 -1
- data/spec/plsql_spec.rb +43 -0
- data/spec/spec_helper.rb +0 -1
- metadata +4 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0039923a88aaae3c4dfe23fd755a4db44ce3a345
|
4
|
+
data.tar.gz: 44df49c9eb526fcec5a9dd7365f13355e1b6d819
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1314afbdc1d419798a2dd1171d1cedc93605607de3d9962e4a9e72de636fce394f237d3de509efa3c5f3e8f84bdf93a47b7d127bbf32d39566421146dfe3fe73
|
7
|
+
data.tar.gz: db0138c15966cbc325ef13c4794f8f621159f83bc6460280b1a2501975e6143ffb6988d7a83e7ac3afe98a28685004623e10ec8b340fc98b1d64d6b12c3e6c10
|
data/README.md
CHANGED
@@ -20,6 +20,22 @@ Or install it yourself as:
|
|
20
20
|
|
21
21
|
activerecord-jdbc-plsql provide a new method to call PL/SQL programs. Use **call_procedure** from your models. Example:
|
22
22
|
|
23
|
+
### Standard
|
24
|
+
```ruby
|
25
|
+
result = call_procedure('my_procedure', 'value1', 'value2', :integer)
|
26
|
+
```
|
27
|
+
|
28
|
+
This call will return this ruby hash:
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
{
|
32
|
+
3 => 'the expected value'
|
33
|
+
}
|
34
|
+
```
|
35
|
+
|
36
|
+
The key will be the parameter index starting at 1 ( accordance with JDBC API ).
|
37
|
+
|
38
|
+
### With named parameters
|
23
39
|
```ruby
|
24
40
|
result = call_procedure('my_procedure', an_input_parameter: 'value1', an_other_input_parameter: 'value2', an_output_parameter: :integer)
|
25
41
|
```
|
@@ -18,7 +18,6 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
|
-
spec.add_dependency "activesupport"
|
22
21
|
spec.add_dependency "activerecord"
|
23
22
|
spec.add_dependency "activerecord-jdbc-adapter"
|
24
23
|
spec.add_development_dependency "bundler", "~> 1.3"
|
@@ -3,124 +3,135 @@ require "activerecord/jdbc/plsql/version"
|
|
3
3
|
module ActiveRecord
|
4
4
|
module Jdbc
|
5
5
|
module PLSql
|
6
|
-
extend ActiveSupport::Concern
|
7
|
-
|
8
|
-
module ClassMethods
|
9
|
-
|
10
|
-
SQL_TYPES = {
|
11
|
-
binary: java.sql.Types::BINARY,
|
12
|
-
boolean: java.sql.Types::BOOLEAN,
|
13
|
-
date: java.sql.Types::DATE,
|
14
|
-
datetime: java.sql.Types::TIMESTAMP,
|
15
|
-
decimal: java.sql.Types::DECIMAL,
|
16
|
-
float: java.sql.Types::FLOAT,
|
17
|
-
integer: java.sql.Types::INTEGER,
|
18
|
-
string: java.sql.Types::VARCHAR,
|
19
|
-
text: java.sql.Types::VARCHAR,
|
20
|
-
time: java.sql.Types::TIME,
|
21
|
-
timestamp: java.sql.Types::TIMESTAMP
|
22
|
-
}
|
23
|
-
|
24
|
-
# Create parameter part of call string
|
25
|
-
def stringify_parameters(attributes)
|
26
|
-
if attributes.is_a?(Hash) || attributes.is_a?(Array)
|
27
|
-
Array.new(attributes.size, '?').join(', ')
|
28
|
-
elsif !attributes.nil?
|
29
|
-
'?'
|
30
|
-
end
|
31
|
-
end
|
32
6
|
|
33
|
-
|
34
|
-
|
35
|
-
|
7
|
+
SQL_TYPES = {
|
8
|
+
binary: java.sql.Types::BINARY,
|
9
|
+
boolean: java.sql.Types::BOOLEAN,
|
10
|
+
date: java.sql.Types::DATE,
|
11
|
+
datetime: java.sql.Types::TIMESTAMP,
|
12
|
+
decimal: java.sql.Types::DECIMAL,
|
13
|
+
float: java.sql.Types::FLOAT,
|
14
|
+
integer: java.sql.Types::INTEGER,
|
15
|
+
string: java.sql.Types::VARCHAR,
|
16
|
+
text: java.sql.Types::VARCHAR,
|
17
|
+
time: java.sql.Types::TIME,
|
18
|
+
timestamp: java.sql.Types::TIMESTAMP
|
19
|
+
}
|
20
|
+
|
21
|
+
def parse_parameters(parameters)
|
22
|
+
if parameters.size == 1 and parameters[0].is_a? Hash
|
23
|
+
parameters[0]
|
24
|
+
else
|
25
|
+
parameters
|
36
26
|
end
|
27
|
+
end
|
37
28
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
elsif !attributes.nil?
|
45
|
-
statement.setString(1, attributes.to_s)
|
46
|
-
end
|
29
|
+
def parse_input_parameters(parameters)
|
30
|
+
input_parameters = Hash.new
|
31
|
+
if parameters.is_a? Hash
|
32
|
+
parameters.each { |k, v| input_parameters[k] = v unless SQL_TYPES.include? v}
|
33
|
+
else
|
34
|
+
parameters.each_with_index { |v, i| input_parameters[i + 1] = v unless SQL_TYPES.include? v }
|
47
35
|
end
|
36
|
+
input_parameters
|
37
|
+
end
|
48
38
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
statement.setString(key.to_s, value.to_s)
|
56
|
-
end
|
39
|
+
def parse_output_parameters(parameters)
|
40
|
+
output_parameters = Hash.new
|
41
|
+
if parameters.is_a? Hash
|
42
|
+
parameters.each { |k, v| output_parameters[k] = v if SQL_TYPES.include? v}
|
43
|
+
else
|
44
|
+
parameters.each_with_index { |v, i| output_parameters[i + 1] = v if SQL_TYPES.include? v }
|
57
45
|
end
|
46
|
+
output_parameters
|
47
|
+
end
|
58
48
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
elsif attributes.is_a?(Array)
|
68
|
-
attributes.each_with_index { |v, i|
|
69
|
-
result[i] = parse_result(statement, i, :string) if v.nil?
|
70
|
-
}
|
71
|
-
end
|
72
|
-
|
73
|
-
result
|
49
|
+
def create_request_string(name, attributes)
|
50
|
+
"{CALL #{name}(#{Array.new(attributes.size, '?').join(', ')})}"
|
51
|
+
end
|
52
|
+
|
53
|
+
def register_input_parameters(statement, input_parameters)
|
54
|
+
input_parameters.each do |k, v|
|
55
|
+
key = k.is_a?(Symbol) ? k.to_s : k
|
56
|
+
statement.setObject(key, v)
|
74
57
|
end
|
58
|
+
end
|
75
59
|
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
statement.getString(key)
|
81
|
-
when :boolean
|
82
|
-
statement.getBoolean(key)
|
83
|
-
when :date
|
84
|
-
statement.getDate(key)
|
85
|
-
when :datetime
|
86
|
-
statement.getTimestamp(key)
|
87
|
-
when :decimal
|
88
|
-
statement.getDouble(key)
|
89
|
-
when :float
|
90
|
-
statement.getFloat(key)
|
91
|
-
when :integer
|
92
|
-
statement.getInt(key)
|
93
|
-
when :string
|
94
|
-
statement.getString(key)
|
95
|
-
when :text
|
96
|
-
statement.getString(key)
|
97
|
-
when :time
|
98
|
-
statement.getTime(key)
|
99
|
-
when :timestamp
|
100
|
-
statement.getTimestamp(key)
|
101
|
-
else
|
102
|
-
statement.getObject(key)
|
103
|
-
end
|
60
|
+
def register_output_parameters(statement, output_parameters)
|
61
|
+
output_parameters.each do |k, v|
|
62
|
+
key = k.is_a?(Symbol) ? k.to_s : k
|
63
|
+
statement.registerOutParameter(key, SQL_TYPES[v])
|
104
64
|
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def register_parameters(statement, output_parameters, input_parameters)
|
68
|
+
# Order is important for INOUT parameters
|
69
|
+
register_output_parameters(statement, output_parameters)
|
70
|
+
register_input_parameters(statement, input_parameters)
|
71
|
+
end
|
105
72
|
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
73
|
+
# Store results in hash
|
74
|
+
def get_results(statement, output_parameters)
|
75
|
+
results = Hash.new
|
76
|
+
output_parameters.each do |k, v|
|
77
|
+
key = k.is_a?(Symbol) ? k.to_s : k
|
78
|
+
results[k] = get_result(statement, key, v)
|
79
|
+
end
|
80
|
+
results
|
81
|
+
end
|
112
82
|
|
113
|
-
|
83
|
+
# Choose the method to call with type
|
84
|
+
def get_result(statement, key, type)
|
85
|
+
case type
|
86
|
+
when :binary
|
87
|
+
statement.getString(key)
|
88
|
+
when :boolean
|
89
|
+
statement.getBoolean(key)
|
90
|
+
when :date
|
91
|
+
statement.getDate(key)
|
92
|
+
when :datetime
|
93
|
+
statement.getTimestamp(key)
|
94
|
+
when :decimal
|
95
|
+
statement.getDouble(key)
|
96
|
+
when :float
|
97
|
+
statement.getFloat(key)
|
98
|
+
when :integer
|
99
|
+
statement.getInt(key)
|
100
|
+
when :string
|
101
|
+
statement.getString(key)
|
102
|
+
when :text
|
103
|
+
statement.getString(key)
|
104
|
+
when :time
|
105
|
+
statement.getTime(key)
|
106
|
+
when :timestamp
|
107
|
+
statement.getTimestamp(key)
|
108
|
+
else
|
109
|
+
statement.getObject(key)
|
110
|
+
end
|
111
|
+
end
|
114
112
|
|
115
|
-
|
116
|
-
|
113
|
+
# Entry point to call procedure
|
114
|
+
def call_procedure(name, *binds)
|
115
|
+
parameters = parse_parameters(binds)
|
117
116
|
|
118
|
-
|
117
|
+
request_string = create_request_string(name, parameters)
|
118
|
+
statement = raw_connection.with_connection_retry_guard do |jdbc_connection|
|
119
|
+
jdbc_connection.prepareCall(request_string)
|
119
120
|
end
|
120
121
|
|
122
|
+
output_parameters = parse_output_parameters(parameters)
|
123
|
+
input_parameters = parse_input_parameters(parameters)
|
124
|
+
|
125
|
+
register_parameters(statement, output_parameters, input_parameters)
|
126
|
+
|
127
|
+
logger.info 'Call procedure: ' + request_string
|
128
|
+
statement.execute
|
129
|
+
|
130
|
+
get_results(statement, output_parameters)
|
121
131
|
end
|
132
|
+
|
122
133
|
end
|
123
134
|
end
|
124
135
|
end
|
125
136
|
|
126
|
-
ActiveRecord::
|
137
|
+
ActiveRecord::ConnectionAdapters::JdbcAdapter.send(:include, ActiveRecord::Jdbc::PLSql)
|
data/spec/plsql_spec.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'PLSql' do
|
4
|
+
before do
|
5
|
+
@pl_sql = Object.new
|
6
|
+
@pl_sql.extend(ActiveRecord::Jdbc::PLSql)
|
7
|
+
|
8
|
+
@parameters = ['test', :integer]
|
9
|
+
@named_parameters = {test1: 'test', test2: :integer}
|
10
|
+
end
|
11
|
+
|
12
|
+
describe '#parse_input_parameters' do
|
13
|
+
it 'returns an input parameter array when parameters are named' do
|
14
|
+
input_parameters = @pl_sql.parse_input_parameters(@named_parameters)
|
15
|
+
expect(input_parameters.size).to be 1
|
16
|
+
expect(input_parameters.keys.first).to eq :test1
|
17
|
+
expect(input_parameters.values.first).to eq 'test'
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'returns an input parameter array when parameters are unnamed' do
|
21
|
+
input_parameters = @pl_sql.parse_input_parameters(@parameters)
|
22
|
+
expect(input_parameters.size).to be 1
|
23
|
+
expect(input_parameters.keys.first).to eq 1
|
24
|
+
expect(input_parameters.values.first).to eq 'test'
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe '#parse_output_parameters' do
|
29
|
+
it 'returns an input parameter array when parameters are named' do
|
30
|
+
output_parameters = @pl_sql.parse_output_parameters(@named_parameters)
|
31
|
+
expect(output_parameters.size).to be 1
|
32
|
+
expect(output_parameters.keys.first).to eq :test2
|
33
|
+
expect(output_parameters.values.first).to be :integer
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'returns an input parameter array when parameters are unnamed' do
|
37
|
+
output_parameters = @pl_sql.parse_output_parameters(@parameters)
|
38
|
+
expect(output_parameters.size).to be 1
|
39
|
+
expect(output_parameters.keys.first).to be 2
|
40
|
+
expect(output_parameters.values.first).to be :integer
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,29 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activerecord-jdbc-plsql
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pierrick Rouxel
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-07-
|
11
|
+
date: 2013-07-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
-
- !ruby/object:Gem::Dependency
|
14
|
-
name: activesupport
|
15
|
-
requirement: !ruby/object:Gem::Requirement
|
16
|
-
requirements:
|
17
|
-
- - '>='
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: '0'
|
20
|
-
type: :runtime
|
21
|
-
prerelease: false
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
23
|
-
requirements:
|
24
|
-
- - '>='
|
25
|
-
- !ruby/object:Gem::Version
|
26
|
-
version: '0'
|
27
13
|
- !ruby/object:Gem::Dependency
|
28
14
|
name: activerecord
|
29
15
|
requirement: !ruby/object:Gem::Requirement
|
@@ -110,6 +96,7 @@ files:
|
|
110
96
|
- activerecord-jdbc-plsql.gemspec
|
111
97
|
- lib/activerecord/jdbc/plsql.rb
|
112
98
|
- lib/activerecord/jdbc/plsql/version.rb
|
99
|
+
- spec/plsql_spec.rb
|
113
100
|
- spec/spec_helper.rb
|
114
101
|
homepage: https://github.com/pierrickrouxel/activerecord-jdbc-plsql
|
115
102
|
licenses:
|
@@ -136,4 +123,5 @@ signing_key:
|
|
136
123
|
specification_version: 4
|
137
124
|
summary: PL/SQL with Active Record
|
138
125
|
test_files:
|
126
|
+
- spec/plsql_spec.rb
|
139
127
|
- spec/spec_helper.rb
|