flash-gordons-ruby-plsql 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,8 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Version" do
4
+ it "should return ruby-plsql version" do
5
+ PLSQL::VERSION.should == File.read(File.dirname(__FILE__)+'/../../VERSION').chomp
6
+ end
7
+
8
+ end
@@ -0,0 +1,264 @@
1
+ require 'spec_helper'
2
+
3
+ describe "View" do
4
+ before(:all) do
5
+ plsql.connect! CONNECTION_PARAMS
6
+ plsql.connection.autocommit = false
7
+ plsql.execute <<-SQL
8
+ CREATE TABLE test_employees (
9
+ employee_id NUMBER(15) NOT NULL,
10
+ first_name VARCHAR2(50),
11
+ last_name VARCHAR2(50),
12
+ hire_date DATE,
13
+ status VARCHAR2(1) DEFAULT 'N'
14
+ )
15
+ SQL
16
+ plsql.execute "CREATE OR REPLACE VIEW test_employees_v AS SELECT * FROM test_employees"
17
+
18
+ @employees = (1..10).map do |i|
19
+ {
20
+ :employee_id => i,
21
+ :first_name => "First #{i}",
22
+ :last_name => "Last #{i}",
23
+ :hire_date => Time.local(2000,01,i),
24
+ :status => 'A'
25
+ }
26
+ end
27
+ @employees_all_fields = [:employee_id, :first_name, :last_name, :hire_date, :status]
28
+ @employees_all_values = @employees.map{|e| @employees_all_fields.map{|f| e[f]}}
29
+ @employees_some_fields = [:employee_id, :first_name, :last_name]
30
+ @employees_some_values = @employees.map{|e| @employees_some_fields.map{|f| e[f]}}
31
+ @employee_default_values = {:hire_date => nil, :status => 'N'}
32
+ end
33
+
34
+ after(:all) do
35
+ plsql.execute "DROP VIEW test_employees_v"
36
+ plsql.execute "DROP TABLE test_employees"
37
+ plsql.logoff
38
+ end
39
+
40
+ after(:each) do
41
+ plsql.rollback
42
+ end
43
+
44
+ describe "find" do
45
+
46
+ it "should find existing view" do
47
+ PLSQL::View.find(plsql, :test_employees_v).should_not be_nil
48
+ end
49
+
50
+ it "should not find nonexisting view" do
51
+ PLSQL::View.find(plsql, :qwerty123456).should be_nil
52
+ end
53
+
54
+ it "should find existing view in schema" do
55
+ plsql.test_employees_v.should be_instance_of(PLSQL::View)
56
+ end
57
+
58
+ end
59
+
60
+ describe "synonym" do
61
+
62
+ before(:all) do
63
+ plsql.execute "CREATE SYNONYM test_employees_v_synonym FOR hr.test_employees_v"
64
+ end
65
+
66
+ after(:all) do
67
+ plsql.execute "DROP SYNONYM test_employees_v_synonym" rescue nil
68
+ end
69
+
70
+ it "should find synonym to view" do
71
+ PLSQL::View.find(plsql, :test_employees_v_synonym).should_not be_nil
72
+ end
73
+
74
+ it "should find view using synonym in schema" do
75
+ plsql.test_employees_v_synonym.should be_instance_of(PLSQL::View)
76
+ end
77
+
78
+ end
79
+
80
+ describe "public synonym" do
81
+
82
+ it "should find public synonym to view" do
83
+ PLSQL::View.find(plsql, :user_tables).should_not be_nil
84
+ end
85
+
86
+ it "should find view using public synonym in schema" do
87
+ plsql.user_tables.should be_instance_of(PLSQL::View)
88
+ end
89
+
90
+ end
91
+
92
+ describe "columns" do
93
+
94
+ it "should get column names for view" do
95
+ plsql.test_employees_v.column_names.should == [:employee_id, :first_name, :last_name, :hire_date, :status]
96
+ end
97
+
98
+ it "should get columns metadata for view" do
99
+ plsql.test_employees_v.columns.should == {
100
+ :employee_id => {
101
+ :position=>1, :data_type=>"NUMBER", :data_length=>22, :data_precision=>15, :data_scale=>0, :char_used=>nil,
102
+ :type_owner=>nil, :type_name=>nil, :sql_type_name=>nil, :nullable => false, :data_default => nil},
103
+ :first_name => {
104
+ :position=>2, :data_type=>"VARCHAR2", :data_length=>50, :data_precision=>nil, :data_scale=>nil, :char_used=>"B",
105
+ :type_owner=>nil, :type_name=>nil, :sql_type_name=>nil, :nullable => true, :data_default => nil},
106
+ :last_name => {
107
+ :position=>3, :data_type=>"VARCHAR2", :data_length=>50, :data_precision=>nil, :data_scale=>nil, :char_used=>"B",
108
+ :type_owner=>nil, :type_name=>nil, :sql_type_name=>nil, :nullable => true, :data_default => nil},
109
+ :hire_date => {
110
+ :position=>4, :data_type=>"DATE", :data_length=>7, :data_precision=>nil, :data_scale=>nil, :char_used=>nil,
111
+ :type_owner=>nil, :type_name=>nil, :sql_type_name=>nil, :nullable => true, :data_default => nil},
112
+ :status => {
113
+ :position=>5, :data_type=>"VARCHAR2", :data_length=>1, :data_precision=>nil, :data_scale=>nil, :char_used=>"B",
114
+ :type_owner=>nil, :type_name=>nil, :sql_type_name=>nil, :nullable => true, :data_default => nil}
115
+ }
116
+ end
117
+
118
+ end
119
+
120
+ describe "insert" do
121
+ it "should insert a record in view" do
122
+ plsql.test_employees_v.insert @employees.first
123
+ plsql.test_employees_v.all.should == [@employees.first]
124
+ end
125
+
126
+ it "should insert a record in view using partial list of columns" do
127
+ plsql.test_employees_v.insert @employees.first.except(:hire_date)
128
+ plsql.test_employees_v.all.should == [@employees.first.merge(:hire_date => nil)]
129
+ end
130
+
131
+ it "should insert default value from table definition if value not provided" do
132
+ plsql.test_employees_v.insert @employees.first.except(:status)
133
+ plsql.test_employees_v.all.should == [@employees.first.merge(:status => 'N')]
134
+ end
135
+
136
+ it "should insert array of records in view" do
137
+ plsql.test_employees_v.insert @employees
138
+ plsql.test_employees_v.all("ORDER BY employee_id").should == @employees
139
+ end
140
+
141
+ end
142
+
143
+ describe "insert values" do
144
+ it "should insert a record with array of values" do
145
+ plsql.test_employees_v.insert_values @employees_all_values.first
146
+ plsql.test_employees_v.all.should == [@employees.first]
147
+ end
148
+
149
+ it "should insert a record with list of all fields and array of values" do
150
+ plsql.test_employees_v.insert_values @employees_all_fields, @employees_all_values.first
151
+ plsql.test_employees_v.all.should == [@employees.first]
152
+ end
153
+
154
+ it "should insert a record with list of some fields and array of values" do
155
+ plsql.test_employees_v.insert_values @employees_some_fields, @employees_some_values.first
156
+ plsql.test_employees_v.all.should == [@employees.first.merge(@employee_default_values)]
157
+ end
158
+
159
+ it "should insert many records with array of values" do
160
+ plsql.test_employees_v.insert_values *@employees_all_values
161
+ plsql.test_employees_v.all.should == @employees
162
+ end
163
+
164
+ it "should insert many records with list of all fields and array of values" do
165
+ plsql.test_employees_v.insert_values @employees_all_fields, *@employees_all_values
166
+ plsql.test_employees_v.all.should == @employees
167
+ end
168
+
169
+ it "should insert many records with list of some fields and array of values" do
170
+ plsql.test_employees_v.insert_values @employees_some_fields, *@employees_some_values
171
+ plsql.test_employees_v.all.should == @employees.map{|e| e.merge(@employee_default_values)}
172
+ end
173
+
174
+ end
175
+
176
+ describe "select" do
177
+ before(:each) do
178
+ plsql.test_employees_v.insert @employees
179
+ end
180
+
181
+ it "should select first record in view" do
182
+ plsql.test_employees_v.select(:first, "ORDER BY employee_id").should == @employees.first
183
+ plsql.test_employees_v.first("ORDER BY employee_id").should == @employees.first
184
+ end
185
+
186
+ it "should select all records in view" do
187
+ plsql.test_employees_v.select(:all, "ORDER BY employee_id").should == @employees
188
+ plsql.test_employees_v.all("ORDER BY employee_id").should == @employees
189
+ plsql.test_employees_v.all(:order_by => :employee_id).should == @employees
190
+ end
191
+
192
+ it "should select record in view using WHERE condition" do
193
+ plsql.test_employees_v.select(:first, "WHERE employee_id = :1", @employees.first[:employee_id]).should == @employees.first
194
+ plsql.test_employees_v.first("WHERE employee_id = :1", @employees.first[:employee_id]).should == @employees.first
195
+ plsql.test_employees_v.first(:employee_id => @employees.first[:employee_id]).should == @employees.first
196
+ end
197
+
198
+ it "should select record in view using :column => nil condition" do
199
+ employee = @employees.last
200
+ employee[:employee_id] = employee[:employee_id] + 1
201
+ employee[:hire_date] = nil
202
+ plsql.test_employees_v.insert employee
203
+ plsql.test_employees_v.first("WHERE hire_date IS NULL").should == employee
204
+ plsql.test_employees_v.first(:hire_date => nil).should == employee
205
+ end
206
+
207
+ it "should count records in view" do
208
+ plsql.test_employees_v.select(:count).should == @employees.size
209
+ plsql.test_employees_v.count.should == @employees.size
210
+ end
211
+
212
+ it "should count records in view using condition" do
213
+ plsql.test_employees_v.select(:count, "WHERE employee_id <= :1", @employees[2][:employee_id]).should == 3
214
+ plsql.test_employees_v.count("WHERE employee_id <= :1", @employees[2][:employee_id]).should == 3
215
+ end
216
+
217
+ end
218
+
219
+ describe "update" do
220
+ it "should update a record in view" do
221
+ employee_id = @employees.first[:employee_id]
222
+ plsql.test_employees_v.insert @employees.first
223
+ plsql.test_employees_v.update :first_name => 'Test', :where => {:employee_id => employee_id}
224
+ plsql.test_employees_v.first(:employee_id => employee_id)[:first_name].should == 'Test'
225
+ end
226
+
227
+ it "should update a record in view using String WHERE condition" do
228
+ employee_id = @employees.first[:employee_id]
229
+ plsql.test_employees_v.insert @employees
230
+ plsql.test_employees_v.update :first_name => 'Test', :where => "employee_id = #{employee_id}"
231
+ plsql.test_employees_v.first(:employee_id => employee_id)[:first_name].should == 'Test'
232
+ # all other records should not be changed
233
+ plsql.test_employees_v.all("WHERE employee_id > :1", employee_id) do |employee|
234
+ employee[:first_name].should_not == 'Test'
235
+ end
236
+ end
237
+
238
+ it "should update all records in view" do
239
+ plsql.test_employees_v.insert @employees
240
+ plsql.test_employees_v.update :first_name => 'Test'
241
+ plsql.test_employees_v.all do |employee|
242
+ employee[:first_name].should == 'Test'
243
+ end
244
+ end
245
+
246
+ end
247
+
248
+ describe "delete" do
249
+ it "should delete record from view" do
250
+ employee_id = @employees.first[:employee_id]
251
+ plsql.test_employees_v.insert @employees
252
+ plsql.test_employees_v.delete :employee_id => employee_id
253
+ plsql.test_employees_v.first(:employee_id => employee_id).should be_nil
254
+ plsql.test_employees_v.all(:order_by => :employee_id).should == @employees[1, @employees.size-1]
255
+ end
256
+
257
+ it "should delete all records from view" do
258
+ plsql.test_employees_v.insert @employees
259
+ plsql.test_employees_v.delete
260
+ plsql.test_employees_v.all.should be_empty
261
+ end
262
+ end
263
+
264
+ end
@@ -0,0 +1,6 @@
1
+ --colour
2
+ --format
3
+ progress
4
+ --loadby
5
+ mtime
6
+ --reverse
@@ -0,0 +1,79 @@
1
+ require "rubygems"
2
+ require "bundler"
3
+ Bundler.setup(:default, :development)
4
+
5
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
6
+
7
+ require 'rspec'
8
+
9
+ unless ENV['NO_ACTIVERECORD']
10
+ require 'active_record'
11
+ else
12
+ puts 'Without ActiveRecord'
13
+ end
14
+
15
+ require 'ruby-plsql'
16
+
17
+ DATABASE_NAME = ENV['DATABASE_NAME'] || 'orcl'
18
+
19
+ if ENV['DATABASE_USE_TNS_NAMES']
20
+ DATABASE_HOST = nil
21
+ DATABASE_PORT = nil
22
+ else
23
+ DATABASE_HOST = ENV['DATABASE_HOST'] || 'localhost'
24
+ DATABASE_PORT = ENV['DATABASE_PORT'] || 1521
25
+ end
26
+
27
+ DATABASE_USERS_AND_PASSWORDS = [
28
+ [ENV['DATABASE_USER'] || 'hr', ENV['DATABASE_PASSWORD'] || 'hr'],
29
+ [ENV['DATABASE_USER2'] || 'arunit', ENV['DATABASE_PASSWORD2'] || 'arunit']
30
+ ]
31
+ # specify which database version is used (will be verified in one test)
32
+ DATABASE_VERSION = ENV['DATABASE_VERSION'] || '10.2.0.4'
33
+
34
+ def get_connection(user_number = 0)
35
+ database_user, database_password = DATABASE_USERS_AND_PASSWORDS[user_number]
36
+ if defined?(JRUBY_VERSION)
37
+ begin
38
+ java.sql.DriverManager.getConnection("jdbc:oracle:thin:@#{DATABASE_HOST}:#{DATABASE_PORT}:#{DATABASE_NAME}",
39
+ database_user, database_password)
40
+ # if connection fails then sleep 5 seconds and retry
41
+ rescue NativeException
42
+ sleep 5
43
+ java.sql.DriverManager.getConnection("jdbc:oracle:thin:@#{DATABASE_HOST}:#{DATABASE_PORT}:#{DATABASE_NAME}",
44
+ database_user, database_password)
45
+ end
46
+ else
47
+ begin
48
+ OCI8.new(database_user, database_password, DATABASE_NAME)
49
+ # if connection fails then sleep 5 seconds and retry
50
+ rescue OCIError
51
+ sleep 5
52
+ OCI8.new(database_user, database_password, DATABASE_NAME)
53
+ end
54
+ end
55
+ end
56
+
57
+ CONNECTION_PARAMS = {
58
+ :adapter => "oracle_enhanced",
59
+ :database => DATABASE_NAME,
60
+ :host => DATABASE_HOST,
61
+ :port => DATABASE_PORT,
62
+ :username => DATABASE_USERS_AND_PASSWORDS[0][0],
63
+ :password => DATABASE_USERS_AND_PASSWORDS[0][1]
64
+ }
65
+
66
+ class Hash
67
+ def except(*blacklist)
68
+ self.reject {|key, value| blacklist.include?(key) }
69
+ end unless method_defined?(:except)
70
+
71
+ def only(*whitelist)
72
+ self.reject {|key, value| !whitelist.include?(key) }
73
+ end unless method_defined?(:only)
74
+ end
75
+
76
+ # set default time zone in TZ environment variable
77
+ # which will be used to set session time zone
78
+ ENV['TZ'] ||= 'Europe/Riga'
79
+ # ENV['TZ'] ||= 'UTC'
metadata ADDED
@@ -0,0 +1,159 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: flash-gordons-ruby-plsql
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.0
5
+ platform: ruby
6
+ authors:
7
+ - Raimonds Simanovskis
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2012-04-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: jeweler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 1.8.3
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 1.8.3
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '='
32
+ - !ruby/object:Gem::Version
33
+ version: '2.9'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '='
39
+ - !ruby/object:Gem::Version
40
+ version: '2.9'
41
+ - !ruby/object:Gem::Dependency
42
+ name: activerecord
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '='
46
+ - !ruby/object:Gem::Version
47
+ version: 3.2.3
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '='
53
+ - !ruby/object:Gem::Version
54
+ version: 3.2.3
55
+ - !ruby/object:Gem::Dependency
56
+ name: activerecord-oracle_enhanced-adapter
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '='
60
+ - !ruby/object:Gem::Version
61
+ version: 1.4.1
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '='
67
+ - !ruby/object:Gem::Version
68
+ version: 1.4.1
69
+ - !ruby/object:Gem::Dependency
70
+ name: ruby-oci8
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '='
74
+ - !ruby/object:Gem::Version
75
+ version: 2.1.5
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '='
81
+ - !ruby/object:Gem::Version
82
+ version: 2.1.5
83
+ description: ! 'ruby-plsql gem provides simple Ruby API for calling Oracle PL/SQL
84
+ procedures.
85
+
86
+ It could be used both for accessing Oracle PL/SQL API procedures in legacy applications
87
+
88
+ as well as it could be used to create PL/SQL unit tests using Ruby testing libraries.
89
+
90
+ '
91
+ email: raimonds.simanovskis@gmail.com
92
+ executables: []
93
+ extensions: []
94
+ extra_rdoc_files:
95
+ - README.md
96
+ files:
97
+ - Gemfile
98
+ - History.txt
99
+ - License.txt
100
+ - README.md
101
+ - Rakefile
102
+ - VERSION
103
+ - lib/plsql/connection.rb
104
+ - lib/plsql/helpers.rb
105
+ - lib/plsql/jdbc_connection.rb
106
+ - lib/plsql/oci8_patches.rb
107
+ - lib/plsql/oci_connection.rb
108
+ - lib/plsql/package.rb
109
+ - lib/plsql/procedure.rb
110
+ - lib/plsql/procedure_call.rb
111
+ - lib/plsql/schema.rb
112
+ - lib/plsql/sequence.rb
113
+ - lib/plsql/sql_statements.rb
114
+ - lib/plsql/table.rb
115
+ - lib/plsql/type.rb
116
+ - lib/plsql/variable.rb
117
+ - lib/plsql/version.rb
118
+ - lib/plsql/view.rb
119
+ - lib/ruby-plsql.rb
120
+ - lib/ruby_plsql.rb
121
+ - ruby-plsql.gemspec
122
+ - spec/plsql/connection_spec.rb
123
+ - spec/plsql/package_spec.rb
124
+ - spec/plsql/procedure_spec.rb
125
+ - spec/plsql/schema_spec.rb
126
+ - spec/plsql/sequence_spec.rb
127
+ - spec/plsql/sql_statements_spec.rb
128
+ - spec/plsql/table_spec.rb
129
+ - spec/plsql/type_spec.rb
130
+ - spec/plsql/variable_spec.rb
131
+ - spec/plsql/version_spec.rb
132
+ - spec/plsql/view_spec.rb
133
+ - spec/spec.opts
134
+ - spec/spec_helper.rb
135
+ homepage: http://github.com/rsim/ruby-plsql
136
+ licenses:
137
+ - MIT
138
+ metadata: {}
139
+ post_install_message:
140
+ rdoc_options: []
141
+ require_paths:
142
+ - lib
143
+ required_ruby_version: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - ! '>='
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ required_rubygems_version: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ! '>='
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ requirements: []
154
+ rubyforge_project:
155
+ rubygems_version: 2.2.2
156
+ signing_key:
157
+ specification_version: 3
158
+ summary: Ruby API for calling Oracle PL/SQL procedures.
159
+ test_files: []