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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 17d26eaba8bff2c769aa318e98fbba4755d24e62
4
- data.tar.gz: 9894527302f8c15e82cb1549f54b0ad63a188eae
3
+ metadata.gz: 0039923a88aaae3c4dfe23fd755a4db44ce3a345
4
+ data.tar.gz: 44df49c9eb526fcec5a9dd7365f13355e1b6d819
5
5
  SHA512:
6
- metadata.gz: 7ae07feb06415c3866e2273752e1cde3e3202f1417d5910b2f8aeb8b73a034ebea4262ffad14ced609b04278dce660f2f00af5d6113eae35f0a8864e9a9f1906
7
- data.tar.gz: 25ffe2207b0c87c947a6872910ab5a87116223ace7550b9b8eee82a6bf4d1a425a3012c48123aeb9cf0fa71de45cd765779db55e01deb14f0f49cb05105e3ef0
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
- # Create request string
34
- def create_request_string(name, attributes)
35
- "{CALL #{name}(#{stringify_parameters(attributes)})}"
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
- # Register parameters for callable statement
39
- def register_parameters(statement, attributes)
40
- if attributes.is_a?(Hash)
41
- attributes.each { |k, v| register_parameter(statement, k, v) }
42
- elsif attributes.is_a?(Array)
43
- attributes.each_with_index { |v, i| register_parameter(statement, i, v) }
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
- # Choose the best way to register parameter (IN or OUT)
50
- def register_parameter(statement, key, value)
51
- case value
52
- when Symbol
53
- statement.registerOutParameter(key.to_s, SQL_TYPES[value])
54
- else
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
- # Store results in hash
60
- def parse_results(statement, attributes)
61
- result = Hash.new
62
-
63
- if attributes.is_a?(Hash)
64
- attributes.each { |k, v|
65
- result[k] = parse_result(statement, k.to_s, v) if v.is_a? Symbol
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
- # Choose the method to call with type
77
- def parse_result(statement, key, type)
78
- case type
79
- when :binary
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
- # Entry point to call procedure
107
- def call_procedure(name, attributes = nil)
108
- request_string = create_request_string(name, attributes)
109
- statement = connection.raw_connection.with_connection_retry_guard do |jdbc_connection|
110
- jdbc_connection.prepareCall(request_string)
111
- end
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
- register_parameters(statement, attributes)
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
- logger.info 'Call procedure: ' + request_string
116
- statement.execute
113
+ # Entry point to call procedure
114
+ def call_procedure(name, *binds)
115
+ parameters = parse_parameters(binds)
117
116
 
118
- parse_results(statement, attributes)
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::Base.send(:include, ActiveRecord::Jdbc::PLSql)
137
+ ActiveRecord::ConnectionAdapters::JdbcAdapter.send(:include, ActiveRecord::Jdbc::PLSql)
@@ -1,7 +1,7 @@
1
1
  module ActiveRecord
2
2
  module Jdbc
3
3
  module PLSql
4
- VERSION = "0.0.2"
4
+ VERSION = "0.0.3"
5
5
  end
6
6
  end
7
7
  end
@@ -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
@@ -1,7 +1,6 @@
1
1
  require 'rubygems'
2
2
  require 'bundler/setup'
3
3
 
4
- require 'active_support'
5
4
  require 'arjdbc'
6
5
  require 'activerecord/jdbc/plsql'
7
6
 
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.2
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-25 00:00:00.000000000 Z
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