oracle_raw 0.1.6 → 0.1.8

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/README.md ADDED
@@ -0,0 +1,93 @@
1
+ oracle_raw
2
+ ==========
3
+
4
+ This is a library for interfacing with an Oracle Database. It uses ActiveRecord Oracle Enhanced adapter (https://github.com/rsim/oracle-enhanced) for connection pooling, but otherwise a raw Ruby-OCI8 connection (http://ruby-oci8.rubyforge.org/en/) is used.
5
+
6
+ Installation
7
+ ============
8
+
9
+ ```bash
10
+ gem install oracle_raw
11
+ ```
12
+
13
+ Usage
14
+ =====
15
+
16
+ At the moment the following methods are implemented:
17
+
18
+ * `query`: a general querying method
19
+ * `with_connection`: a method taking a block as an argument; inside the block you can use a raw connection object to execute queries, updates etc.
20
+
21
+ Connect to a database and create a pool of five connections. Global options (see below) and pool size are not mandatory. Default pool size is 1.
22
+
23
+ ```ruby
24
+ tnsnames = '(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SID = TEST)))'
25
+ schema = 'scott'
26
+ password = 'tiger'
27
+ connection_pool_size = 5
28
+ global_options = {}
29
+
30
+ db = OracleRaw.new(tnsnames, schema, password, connection_pool_size, global_options)
31
+ ```
32
+
33
+ Get a connection from the pool and do something with it:
34
+
35
+ ```ruby
36
+ db.with_connection { |c|
37
+ c.exec('create table names (id number, name varchar2(50))')
38
+ c.exec("insert into names (id, name) values (1, 'Paul')")
39
+ c.exec("insert into names (id, name) values (2, 'Maria')")
40
+ c.commit
41
+ }
42
+ ```
43
+
44
+ Use query method (handles connection internally). Without parameters:
45
+
46
+ ```ruby
47
+ db.query('select name from names')
48
+ => {:data=>[["Paul"], ["Maria"]]}
49
+ ```
50
+
51
+ Query with parameters:
52
+
53
+ ```ruby
54
+ db.query('select name from names where id = :id', [[:id, 1, Integer]])
55
+ => {:data=>[["Paul"]]}
56
+ ```
57
+
58
+ Query with options: the behaviour of the query method can be controlled with the following options given to initializer, #query or both:
59
+
60
+ * `:metadata`: if `:all`, returns the number of items in the result set, column names in lower case, and the time and duration of the query. If `:none`, returns only the result set.
61
+ * `:item_format`: if `:hash`, query returns the result items as hashes. The default is `:array`, i.e. the items are arrays.
62
+ * `:amount`: if `:all_rows`, returns all rows. If `:first_row`, returns only the first row. If `:single_value`, returns only the first value of the first row. `:single_value` cannot be used if `:item_format` is `:hash`. Default is to return all rows.
63
+
64
+ Global options can be changed after initialization.
65
+ Options given to `query` override global options given to initializer (but not when the option value is nil).
66
+
67
+ ```ruby
68
+ db.query('select name from names', nil, {:item_format => :hash})
69
+ => {:data=>[{"NAME"=>"Paul"}, {"NAME"=>"Maria"}]}
70
+
71
+ db.query('select name from names', nil, {:item_format => :array})
72
+ => {:data=>[["Paul"], ["Maria"]]}
73
+
74
+ db.query('select name from names', nil, {:metadata=>:plain})
75
+ => [["Paul"], ["Maria"]]
76
+
77
+ db.query('select name from names', nil, {:metadata => :all})
78
+ => {:count=>2, :columns=>["name"], :data=>[["Paul"], ["Maria"]], :date=>2012-09-17 15:53:46 +0300, :duration=>0.0016196}
79
+
80
+ db.query('select count(*) from names', nil, {:amount=>:single_value, :metadata=>:plain}).to_i
81
+ => 2
82
+ ```
83
+
84
+ Close the connection pool:
85
+
86
+ ```ruby
87
+ db.close
88
+ ```
89
+
90
+ Copyright
91
+ =========
92
+
93
+ Copyright (c) 2011 opiskelijarekisteri-devel. License: LGPLv3. See LICENSE.txt for further details.
data/Rakefile CHANGED
@@ -16,7 +16,7 @@ Jeweler::Tasks.new do |gem|
16
16
  # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
17
  gem.name = "oracle_raw"
18
18
  gem.homepage = "http://github.com/opiskelijarekisteri-devel/oracle_raw"
19
- gem.license = "GPLv3"
19
+ gem.license = "LGPLv3"
20
20
  gem.summary = %Q{Ruby library for interfacing with an Oracle Database using pooled OCI8 raw connections.}
21
21
  gem.description = %Q{This is a Ruby library for interfacing with an Oracle Database using pooled OCI8 raw connections (http://ruby-oci8.rubyforge.org/en/). Connection pooling is achieved by utilizing ActiveRecord Oracle Enhanced adapter (https://github.com/rsim/oracle-enhanced).}
22
22
  gem.email = "opiskelijarekisteri-devel@helsinki.fi"
@@ -32,17 +32,9 @@ Rake::TestTask.new(:test) do |test|
32
32
  test.verbose = true
33
33
  end
34
34
 
35
- require 'rcov/rcovtask'
36
- Rcov::RcovTask.new do |test|
37
- test.libs << 'test'
38
- test.pattern = 'test/**/test_*.rb'
39
- test.verbose = true
40
- test.rcov_opts << '--exclude "gems/*"'
41
- end
42
-
43
35
  task :default => :test
44
36
 
45
- require 'rake/rdoctask'
37
+ require 'rdoc/task'
46
38
  Rake::RDocTask.new do |rdoc|
47
39
  version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
40
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.6
1
+ 0.1.8
data/lib/oracle_raw.rb CHANGED
@@ -1,8 +1,4 @@
1
1
  # encoding: UTF-8
2
- #
3
- # Author:: opiskelijarekisteri-devel (mailto:opiskelijarekisteri-devel@helsinki.fi)
4
- # Copyright:: Copyright (c) 2011 opiskelijarekisteri-devel
5
- # License:: GNU General Public License, Version 3 (http://www.gnu.org/copyleft/gpl.txt)
6
2
 
7
3
  require 'oci8'
8
4
 
@@ -35,60 +31,18 @@ require 'active_record'
35
31
  # This is a Ruby library for interfacing with an Oracle Database using pooled OCI8 raw connections (http://ruby-oci8.rubyforge.org/en/).
36
32
  # It uses ActiveRecord Oracle Enhanced adapter (https://github.com/rsim/oracle-enhanced) for connection pooling.
37
33
  #
38
- # At the moment the following methods are implemented:
39
- # * #query: a general querying method
40
- # * #with_connection: a method taking a block as an argument; inside the block you can use a raw connection object to execute queries, updates etc.
41
- #
42
- # The behaviour of the query method can be controlled with the following options given to initializer, #query or both:
43
- # * +:metadata+: if +:all+, returns the number of items in the result set, column names in lower case, and the time and duration of the query. If +:none+, returns only the result set.
44
- # * +:item_format+: if +:hash+, query returns the result items as hashes. The default is +:array+, i.e. the items are arrays.
45
- # * +:amount+: if +:all_rows+, returns all rows. If +:first_row+, returns only the first row. If +:single_value+, returns only the first value of the first row. +:single_value+ cannot be used if +:item_format+ is +:hash+. Default is to return all rows.
46
- #
47
- # Global options can be changed after initialization.
48
- # Options given to #query override global options given to initializer (but not when the option value is nil).
49
- #
50
34
  # == Installation
51
35
  #
52
36
  # <tt>gem install oracle_raw</tt>
53
37
  #
54
38
  # == Usage
55
39
  #
56
- # <tt>tnsnames = '(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SID = TEST)))'</tt>
57
- #
58
- # <tt>schema = 'scott'</tt>
59
- #
60
- # <tt>password = 'tiger'</tt>
61
- #
62
- # <tt>connection_pool_size = 5</tt>
63
- #
64
- # <tt>global_options = { :metadata => :all, :item_format => :hash, :amount => :first_row }</tt>
65
- #
66
- # <tt>db = OracleRaw.new(tnsnames, schema, password, connection_pool_size, global_options)</tt>
67
- #
68
- # <tt>students_sql = 'select * from students where last_name = :last_name' and first_name = :first_name</tt>
69
- #
70
- # <tt>last_name = 'Kruskal-Wallis'</tt>
71
- #
72
- # <tt>first_name = 'Lucy'</tt>
73
- #
74
- # <tt>result = db.query(students_sql, [[:last_name, last_name, String], [:first_name, first_name, String]])</tt>
75
- #
76
- # <tt>puts result[:rowcount]</tt>
77
- #
78
- # <tt>puts result[:duration]</tt>
79
- #
80
- # <tt>result = db.query(students_sql, [[:last_name, last_name, String, 50], [:first_name, first_name, String, 50]], { :metadata => :none, :item_format => :array })</tt>
81
- #
82
- # <tt>puts result[:data]</tt>
40
+ # Consult the README.
83
41
  #
84
- # <tt>sysdate_sql = 'select sysdate from dual'</tt>
42
+ # == Additional information
85
43
  #
86
- # <tt>puts db.query(sysdate_sql, nil, { :metadata => :none, :amount => :single_value})[:data]</tt>
87
- #
88
- # <tt>puts db.with_connection { |c| (c.exec(sysdate_sql).fetch)[0] }</tt>
89
- #
90
- #--
91
44
  # ActiveRecord and ConnectionPool documentation:
45
+ #
92
46
  # * http://ar.rubyonrails.org/
93
47
  # * http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/ConnectionPool.html
94
48
 
@@ -123,58 +77,54 @@ class OracleRaw
123
77
  ActiveRecord::Base.connection_pool.with_connection { |conn| yield conn.raw_connection }
124
78
  end
125
79
 
126
- # Depending whether the +:metadata+ option is +:all+ or +:none+, returns either of the following:
80
+ # Depending whether the +:metadata+ option is +:none+ or +:all+, returns either a plain result set or a map of the following kind:
127
81
  #
128
82
  # <tt>{ :count => rowcount, :columns => colnames, :data => data, :date => date, :duration => duration }</tt>
129
- #
130
- # <tt>{ :data => data }</tt>
131
83
  #
132
- # If an exception occurs, returns <tt>{ :exception => e }</tt>.
84
+ # Exception are propagated to the caller.
133
85
 
134
86
  def query(sqlquery, parameters = [], options = {})
135
87
 
136
- begin
137
- with_connection { |conn|
88
+ with_connection { |conn|
138
89
 
139
- starttime = Time.new; data = []
90
+ starttime = Time.new; data = []
140
91
 
141
- cursor = conn.parse(sqlquery)
142
- cursor.bind_parameters(parameters) if parameters
143
- cursor.exec_with_prefetch(5000)
92
+ cursor = conn.parse(sqlquery)
93
+ cursor.bind_parameters(parameters) if parameters
94
+ cursor.exec_with_prefetch(5000)
144
95
 
145
- case options[:item_format] || @global_options[:item_format]
96
+ case options[:item_format] || @global_options[:item_format]
146
97
 
147
- when :hash then
98
+ when :hash then
148
99
 
149
- case options[:amount] || @global_options[:amount]
100
+ case options[:amount] || @global_options[:amount]
150
101
 
151
- when :first_row then data = cursor.fetch_hash()
152
- else while r = cursor.fetch_hash(); data << r; end
153
- end
154
- else
155
- case options[:amount] || @global_options[:amount]
102
+ when :first_row then data = cursor.fetch_hash()
103
+ else while r = cursor.fetch_hash(); data << r; end
104
+ end
105
+ else
106
+ case options[:amount] || @global_options[:amount]
156
107
 
157
- when :single_value then temp = cursor.fetch(); data = (temp ? temp[0] : nil)
158
- when :first_row then data = cursor.fetch()
159
- else while r = cursor.fetch(); data << r; end
160
- end
161
- end
108
+ when :single_value then temp = cursor.fetch(); data = (temp ? temp[0] : nil)
109
+ when :first_row then data = cursor.fetch()
110
+ else while r = cursor.fetch(); data << r; end
111
+ end
112
+ end
162
113
 
163
- case options[:metadata] || @global_options[:metadata]
114
+ case options[:metadata] || @global_options[:metadata]
164
115
 
165
- when :all then
166
- colnames = cursor.get_col_names.each do |n| n.downcase! end
167
- rowcount = cursor.row_count
168
- cursor.close
169
- {:count => rowcount, :columns => colnames, :data => data, :date => starttime, :duration => Time.new - starttime}
170
- else
171
- cursor.close
172
- {:data => data}
173
- end
174
- }
175
- rescue => e
176
- {:exception => e}
177
- end
116
+ when :all then
117
+ colnames = cursor.get_col_names.each do |n| n.downcase! end
118
+ rowcount = cursor.row_count
119
+ cursor.close
120
+ {:count => rowcount, :columns => colnames, :data => data, :date => starttime, :duration => Time.new - starttime}
121
+ when :plain then
122
+ cursor.close
123
+ data
124
+ else
125
+ cursor.close
126
+ {:data => data}
127
+ end
128
+ }
178
129
  end
179
130
  end
180
-
@@ -36,7 +36,8 @@ class TestOracleRaw < Test::Unit::TestCase
36
36
  c.exec("insert into oracle_raw_test (name, age) values ('Lucia', 25)")
37
37
 
38
38
  cursor = c.parse('insert into oracle_raw_test (name, age) values (:name, :age)')
39
- cursor.bind_parameters([[:name, 'Kinnie', String], [:age, 30, Integer]])
39
+ cursor.bind_param(:name, 'Kinnie', String)
40
+ cursor.bind_param(:age, 30, Integer)
40
41
  cursor.exec
41
42
  cursor.close
42
43
 
@@ -67,7 +68,7 @@ class TestOracleRaw < Test::Unit::TestCase
67
68
  start = Time.new; num_calls = 1000
68
69
  num_calls.times do
69
70
  result = db.query('select 1 from dual')
70
- puts result[:exception].backtrace if result[:exception]
71
+ #puts result[:exception].backtrace if result[:exception]
71
72
  assert_equal(result[:exception], nil)
72
73
  end
73
74
  puts "\nSpeed test: pooled: #{num_calls/(Time.new - start)} calls/second.\n"
@@ -106,5 +107,29 @@ puts result[:exception].backtrace if result[:exception]
106
107
  assert(/ORA-00942/ =~ result[:exception].message)
107
108
  db.close
108
109
  end
109
- end
110
110
 
111
+ def test_bind
112
+ db = connect
113
+ id = 10
114
+ lastname = 'Kruskal-Wallis'
115
+ q = "select * from students where id = :id"
116
+
117
+ db.with_connection { |c|
118
+
119
+ cursor = c.parse(q)
120
+
121
+ # these should work and do work
122
+ cursor.bind_param(':id', id, Fixnum)
123
+
124
+ # these should not work and do not work
125
+ cursor.bind_param(':id', lastname, Fixnum) # TypeError; ok
126
+ assert(/ORA-00942/ =~ result[:exception].message)
127
+ cursor.bind_param(':lastname', lastname, String) # OCIError; ok
128
+ assert(/ORA-00942/ =~ result[:exception].message)
129
+
130
+ cursor.close
131
+ }
132
+ db.close # clear connection pool
133
+ true
134
+ end
135
+ end
metadata CHANGED
@@ -1,131 +1,142 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: oracle_raw
3
- version: !ruby/object:Gem::Version
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.8
4
5
  prerelease:
5
- version: 0.1.6
6
6
  platform: ruby
7
- authors:
7
+ authors:
8
8
  - opiskelijarekisteri-devel
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
-
13
- date: 2011-11-27 00:00:00 Z
14
- dependencies:
15
- - !ruby/object:Gem::Dependency
12
+ date: 2012-09-17 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
16
15
  name: ruby-oci8
17
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
18
17
  none: false
19
- requirements:
20
- - - ">="
21
- - !ruby/object:Gem::Version
22
- version: 2.0.6
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 2.1.2
23
22
  type: :runtime
24
23
  prerelease: false
25
- version_requirements: *id001
26
- - !ruby/object:Gem::Dependency
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 2.1.2
30
+ - !ruby/object:Gem::Dependency
27
31
  name: activerecord
28
- requirement: &id002 !ruby/object:Gem::Requirement
32
+ requirement: !ruby/object:Gem::Requirement
29
33
  none: false
30
- requirements:
31
- - - ">="
32
- - !ruby/object:Gem::Version
33
- version: 3.1.1
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: 3.2.8
34
38
  type: :runtime
35
39
  prerelease: false
36
- version_requirements: *id002
37
- - !ruby/object:Gem::Dependency
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: 3.2.8
46
+ - !ruby/object:Gem::Dependency
38
47
  name: activerecord-oracle_enhanced-adapter
39
- requirement: &id003 !ruby/object:Gem::Requirement
48
+ requirement: !ruby/object:Gem::Requirement
40
49
  none: false
41
- requirements:
42
- - - ">="
43
- - !ruby/object:Gem::Version
44
- version: 1.4.0
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: 1.4.1
45
54
  type: :runtime
46
55
  prerelease: false
47
- version_requirements: *id003
48
- - !ruby/object:Gem::Dependency
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: 1.4.1
62
+ - !ruby/object:Gem::Dependency
49
63
  name: bundler
50
- requirement: &id004 !ruby/object:Gem::Requirement
64
+ requirement: !ruby/object:Gem::Requirement
51
65
  none: false
52
- requirements:
53
- - - ~>
54
- - !ruby/object:Gem::Version
55
- version: 1.0.0
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: 1.1.5
56
70
  type: :development
57
71
  prerelease: false
58
- version_requirements: *id004
59
- - !ruby/object:Gem::Dependency
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: 1.1.5
78
+ - !ruby/object:Gem::Dependency
60
79
  name: jeweler
61
- requirement: &id005 !ruby/object:Gem::Requirement
80
+ requirement: !ruby/object:Gem::Requirement
62
81
  none: false
63
- requirements:
64
- - - ~>
65
- - !ruby/object:Gem::Version
66
- version: 1.6.4
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: 1.8.4
67
86
  type: :development
68
87
  prerelease: false
69
- version_requirements: *id005
70
- - !ruby/object:Gem::Dependency
71
- name: rcov
72
- requirement: &id006 !ruby/object:Gem::Requirement
88
+ version_requirements: !ruby/object:Gem::Requirement
73
89
  none: false
74
- requirements:
75
- - - ">="
76
- - !ruby/object:Gem::Version
77
- version: "0"
78
- type: :development
79
- prerelease: false
80
- version_requirements: *id006
81
- description: This is a Ruby library for interfacing with an Oracle Database using pooled OCI8 raw connections (http://ruby-oci8.rubyforge.org/en/). Connection pooling is achieved by utilizing ActiveRecord Oracle Enhanced adapter (https://github.com/rsim/oracle-enhanced).
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: 1.8.4
94
+ description: This is a Ruby library for interfacing with an Oracle Database using
95
+ pooled OCI8 raw connections (http://ruby-oci8.rubyforge.org/en/). Connection pooling
96
+ is achieved by utilizing ActiveRecord Oracle Enhanced adapter (https://github.com/rsim/oracle-enhanced).
82
97
  email: opiskelijarekisteri-devel@helsinki.fi
83
98
  executables: []
84
-
85
99
  extensions: []
86
-
87
- extra_rdoc_files:
100
+ extra_rdoc_files:
88
101
  - LICENSE.txt
89
- - README.rdoc
90
- files:
102
+ - README.md
103
+ files:
91
104
  - .document
92
105
  - Gemfile
93
106
  - LICENSE.txt
94
- - README.rdoc
107
+ - README.md
95
108
  - Rakefile
96
109
  - VERSION
97
110
  - lib/oracle_raw.rb
98
111
  - test/helper.rb
99
112
  - test/test_oracle_raw.rb
100
113
  homepage: http://github.com/opiskelijarekisteri-devel/oracle_raw
101
- licenses:
102
- - GPLv3
114
+ licenses:
115
+ - LGPLv3
103
116
  post_install_message:
104
117
  rdoc_options: []
105
-
106
- require_paths:
118
+ require_paths:
107
119
  - lib
108
- required_ruby_version: !ruby/object:Gem::Requirement
120
+ required_ruby_version: !ruby/object:Gem::Requirement
109
121
  none: false
110
- requirements:
111
- - - ">="
112
- - !ruby/object:Gem::Version
113
- hash: 4539384040906102851
114
- segments:
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ segments:
115
127
  - 0
116
- version: "0"
117
- required_rubygems_version: !ruby/object:Gem::Requirement
128
+ hash: 2071649133811204594
129
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
130
  none: false
119
- requirements:
120
- - - ">="
121
- - !ruby/object:Gem::Version
122
- version: "0"
131
+ requirements:
132
+ - - ! '>='
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
123
135
  requirements: []
124
-
125
136
  rubyforge_project:
126
- rubygems_version: 1.8.11
137
+ rubygems_version: 1.8.23
127
138
  signing_key:
128
139
  specification_version: 3
129
- summary: Ruby library for interfacing with an Oracle Database using pooled OCI8 raw connections.
140
+ summary: Ruby library for interfacing with an Oracle Database using pooled OCI8 raw
141
+ connections.
130
142
  test_files: []
131
-