oci8_simple 0.6.1 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,10 +1,10 @@
1
1
  source :rubygems
2
2
 
3
- gem "ruby-oci8", "~>2.0.4"
3
+ gem "ruby-oci8"
4
4
 
5
5
  group :development do
6
6
  gem "shoulda", ">= 0"
7
- gem "bundler", "~> 1.0.0"
8
- gem "jeweler", "~> 1.5.2"
7
+ gem "jeweler"
9
8
  gem "mocha", ">= 0"
9
+ gem "pry"
10
10
  end
data/Gemfile.lock CHANGED
@@ -1,22 +1,39 @@
1
1
  GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
+ coderay (1.0.6)
4
5
  git (1.2.5)
5
- jeweler (1.5.2)
6
- bundler (~> 1.0.0)
6
+ jeweler (1.8.3)
7
+ bundler (~> 1.0)
7
8
  git (>= 1.2.5)
8
9
  rake
9
- mocha (0.9.12)
10
- rake (0.8.7)
11
- ruby-oci8 (2.0.4)
12
- shoulda (2.10.1)
10
+ rdoc
11
+ json (1.6.6)
12
+ metaclass (0.0.1)
13
+ method_source (0.7.1)
14
+ mocha (0.10.5)
15
+ metaclass (~> 0.0.1)
16
+ pry (0.9.8.4)
17
+ coderay (~> 1.0.5)
18
+ method_source (~> 0.7.1)
19
+ slop (>= 2.4.4, < 3)
20
+ rake (0.9.2.2)
21
+ rdoc (3.12)
22
+ json (~> 1.4)
23
+ ruby-oci8 (2.1.0)
24
+ shoulda (3.0.1)
25
+ shoulda-context (~> 1.0.0)
26
+ shoulda-matchers (~> 1.0.0)
27
+ shoulda-context (1.0.0)
28
+ shoulda-matchers (1.0.0)
29
+ slop (2.4.4)
13
30
 
14
31
  PLATFORMS
15
32
  ruby
16
33
 
17
34
  DEPENDENCIES
18
- bundler (~> 1.0.0)
19
- jeweler (~> 1.5.2)
35
+ jeweler
20
36
  mocha
21
- ruby-oci8 (~> 2.0.4)
37
+ pry
38
+ ruby-oci8
22
39
  shoulda
data/README.rdoc CHANGED
@@ -81,7 +81,8 @@ functions, packages, procedures, sequences, synonyms, tables, types, and views.
81
81
  require 'oci8_simple'
82
82
  client = Oci8Simple::Client.new
83
83
  * Run a simple select query against development schema
84
- client.run('select id, name from foos') => [[2, "lol"], [3, "hey"], ...]
84
+ client.run('select id, name from foos') => [[2, "lol"], [3, "hey"], ...]
85
+ client.run('select id, name from foos', :hash => true) => [{:id => 2, :name => "lol"}, {:id => 3, :name=>"hey"}, ...])
85
86
  * Update something
86
87
  client.run <<-SQL
87
88
  UPDATE foos SET bar='baz' WHERE id=1233
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.6.1
1
+ 0.7.0
@@ -14,12 +14,16 @@ module Oci8Simple
14
14
  self.env = env
15
15
  end
16
16
 
17
- def run(sql)
18
- format(client.run(sql))
17
+ def run(sql, options={})
18
+ format(client.run(sql, options), options)
19
19
  end
20
20
 
21
- def format(arr)
22
- arr.map{|row| row.join(", ")}.join("\n")
21
+ def format(arr, options)
22
+ if(options[:hash])
23
+ arr.map{|row| row.map{|k,v| "#{k}: #{v}"}.join("\n")}.join("\n\n")
24
+ else
25
+ arr.map{|row| row.join(", ")}.join("\n")
26
+ end
23
27
  end
24
28
 
25
29
  def client
@@ -35,7 +39,7 @@ module Oci8Simple
35
39
  if(ARGV[0].nil?)
36
40
  puts o
37
41
  else
38
- puts self.new(ARGV[1]).run(ARGV[0])
42
+ puts self.new(ARGV[1]).run(ARGV[0], @options)
39
43
  end
40
44
  end
41
45
 
@@ -52,25 +52,49 @@ module Oci8Simple
52
52
  raise LogError.new("Cannot write to #{LOG_FILE}... be sure you have write permissions to #{USER_DIR}")
53
53
  end
54
54
 
55
- def run(sql)
55
+ # sql - a query
56
+ # options:
57
+ # :hash => true|false - default is false - return an array of hashes (with column names)
58
+ # instead of an array or arrays
59
+ def run(sql, options={})
56
60
  log(sql)
57
61
  result = []
58
- conn.exec(sql) do |r|
59
- row = []
60
- r.map do |col|
61
- if col.class == BigDecimal
62
- row << col.to_f
63
- elsif col.class == OCI8::CLOB
64
- row << col.read
65
- else
66
- row << col.to_s
67
- end
62
+ if(options[:hash])
63
+ fetch_hashes(sql) do |r|
64
+ result << r
65
+ end
66
+ else
67
+ fetch_arrays(sql) do |r|
68
+ result << r
68
69
  end
69
- result << row
70
70
  end
71
71
  result
72
72
  end
73
73
 
74
+ def fetch_hashes(sql, &block)
75
+ cursor = conn.exec(sql)
76
+ col_names = cursor.get_col_names.map{|s| s.downcase.to_sym }
77
+ while(r = cursor.fetch) do
78
+ yield Hash[*col_names.zip(r.map {|col| record_to_string(col)}).flatten]
79
+ end
80
+ end
81
+
82
+ def fetch_arrays(sql, &block)
83
+ conn.exec(sql) do |r|
84
+ yield r.map{|col| record_to_string(col)}
85
+ end
86
+ end
87
+
88
+ def record_to_string(record)
89
+ if record.class == BigDecimal
90
+ record.to_f
91
+ elsif record.class == OCI8::CLOB
92
+ record.read
93
+ else
94
+ record.to_s
95
+ end
96
+ end
97
+
74
98
  def config
75
99
  @config ||= YAML.load_file(CONFIG_FILE)[env]
76
100
  rescue Errno::ENOENT => e
@@ -8,8 +8,12 @@ module Oci8Simple
8
8
 
9
9
  # Returns an OptionParser object.
10
10
  def parse_options(banner)
11
+ @options= {}
11
12
  o = OptionParser.new do |opt|
12
13
  opt.banner = banner
14
+ opt.on("-c", "--show_column_names", "Show column names for each result") do
15
+ @options[:hash] = true
16
+ end
13
17
  opt.on("-v", "--version", "Show version") do
14
18
  puts "#{self.to_s} #{Oci8Simple::VERSION}"
15
19
  exit
data/oci8_simple.gemspec CHANGED
@@ -4,17 +4,15 @@
4
4
  # -*- encoding: utf-8 -*-
5
5
 
6
6
  Gem::Specification.new do |s|
7
- s.name = %q{oci8_simple}
8
- s.version = "0.6.1"
7
+ s.name = "oci8_simple"
8
+ s.version = "0.7.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Billy Reisinger"]
12
- s.date = %q{2011-07-22}
13
- s.description = %q{Command-line tools for interacting with an Oracle database. This client is intended to be used
14
- to aid development and automation. This is *not* meant to replace an ORM such as ActiveRecord + OracleEnhancedAdapter.
15
- The only prerequisite to running this code is that you have installed the ruby-oci8 gem on your machine.}
16
- s.email = %q{billy.reisinger@gmail.com}
17
- s.executables = ["show", "oci8_simple", "describe"]
12
+ s.date = "2012-04-17"
13
+ s.description = "Command-line tools for interacting with an Oracle database. This client is intended to be used \n to aid development and automation. This is *not* meant to replace an ORM such as ActiveRecord + OracleEnhancedAdapter.\n The only prerequisite to running this code is that you have installed the ruby-oci8 gem on your machine."
14
+ s.email = "billy.reisinger@gmail.com"
15
+ s.executables = ["oci8_simple", "describe", "show"]
18
16
  s.extra_rdoc_files = [
19
17
  "README.rdoc"
20
18
  ]
@@ -42,43 +40,36 @@ Gem::Specification.new do |s|
42
40
  "test/helper.rb",
43
41
  "test/show_test.rb"
44
42
  ]
45
- s.homepage = %q{http://github.com/unclebilly/oci8_simple}
43
+ s.homepage = "http://github.com/unclebilly/oci8_simple"
46
44
  s.licenses = ["MIT"]
47
45
  s.require_paths = ["lib"]
48
- s.rubygems_version = %q{1.5.2}
49
- s.summary = %q{Command-line tools for interacting with an Oracle database.}
50
- s.test_files = [
51
- "test/cli_test.rb",
52
- "test/client_test.rb",
53
- "test/describe_test.rb",
54
- "test/helper.rb",
55
- "test/show_test.rb"
56
- ]
46
+ s.rubygems_version = "1.8.19"
47
+ s.summary = "Command-line tools for interacting with an Oracle database."
57
48
 
58
49
  if s.respond_to? :specification_version then
59
50
  s.specification_version = 3
60
51
 
61
52
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
62
- s.add_runtime_dependency(%q<ruby-oci8>, ["~> 2.0.4"])
53
+ s.add_runtime_dependency(%q<ruby-oci8>, [">= 0"])
63
54
  s.add_development_dependency(%q<shoulda>, [">= 0"])
64
- s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
65
- s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
55
+ s.add_development_dependency(%q<jeweler>, [">= 0"])
66
56
  s.add_development_dependency(%q<mocha>, [">= 0"])
57
+ s.add_development_dependency(%q<pry>, [">= 0"])
67
58
  s.add_runtime_dependency(%q<ruby-oci8>, ["~> 2.0.4"])
68
59
  else
69
- s.add_dependency(%q<ruby-oci8>, ["~> 2.0.4"])
60
+ s.add_dependency(%q<ruby-oci8>, [">= 0"])
70
61
  s.add_dependency(%q<shoulda>, [">= 0"])
71
- s.add_dependency(%q<bundler>, ["~> 1.0.0"])
72
- s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
62
+ s.add_dependency(%q<jeweler>, [">= 0"])
73
63
  s.add_dependency(%q<mocha>, [">= 0"])
64
+ s.add_dependency(%q<pry>, [">= 0"])
74
65
  s.add_dependency(%q<ruby-oci8>, ["~> 2.0.4"])
75
66
  end
76
67
  else
77
- s.add_dependency(%q<ruby-oci8>, ["~> 2.0.4"])
68
+ s.add_dependency(%q<ruby-oci8>, [">= 0"])
78
69
  s.add_dependency(%q<shoulda>, [">= 0"])
79
- s.add_dependency(%q<bundler>, ["~> 1.0.0"])
80
- s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
70
+ s.add_dependency(%q<jeweler>, [">= 0"])
81
71
  s.add_dependency(%q<mocha>, [">= 0"])
72
+ s.add_dependency(%q<pry>, [">= 0"])
82
73
  s.add_dependency(%q<ruby-oci8>, ["~> 2.0.4"])
83
74
  end
84
75
  end
data/test/cli_test.rb CHANGED
@@ -23,6 +23,9 @@ class CliTest < Test::Unit::TestCase
23
23
  should "format results for the command line" do
24
24
  assert_equal("1, Johnny, OMG\n2, Jenny, OMG", @cli.run("select * from oci8_simple_test"))
25
25
  end
26
+ should "format results for the command line using column names" do
27
+ assert_equal("name: Johnny\n\nname: Jenny", @cli.run("select name from oci8_simple_test", :hash => true))
28
+ end
26
29
  end
27
30
  context "without an env" do
28
31
  setup do
data/test/client_test.rb CHANGED
@@ -55,6 +55,9 @@ class ClientTest < Test::Unit::TestCase
55
55
  should "be able to run a simple query with multiple results and multiple columns" do
56
56
  assert_equal [["1", "Johnny", "OMG"], ["2", "Jenny", "OMG"]], @client.run("select * from OCI8_SIMPLE_TEST")
57
57
  end
58
+ should "be able to run a simple query with a single result and return a hash" do
59
+ assert_equal [{:count => 2}], @client.run("select count(*) as count from OCI8_SIMPLE_TEST", :hash => true)
60
+ end
58
61
 
59
62
  should "have logged something" do
60
63
  File.unlink(Oci8Simple::Client::LOG_FILE)
@@ -20,16 +20,14 @@ class DescribeTest < Test::Unit::TestCase
20
20
  end
21
21
  should "format results for the command line" do
22
22
  expected=<<-STR
23
- Required Name Type Size Char? Char_size Precision Scale Default
24
- ------------------------------------------------------------------------------------
25
- * id number 22 38 0 7
26
- * long_thing varchar2 4000 * 2000 a a a a a ...
27
- * name varchar2 1600 * 400 FOO
28
- texts clob
23
+ Required Name Type Size Char? Char_size Precision Scale Default
24
+ --------------------------------------------------------------------------------------
25
+ * id number 22.0 38.0 0.0 7
26
+ * long_thing varchar2 4000.0 * 2000.0 a a a a a ...
27
+ * name varchar2 1600.0 * 400.0 FOO
28
+ texts clob
29
29
  STR
30
- # puts
31
- # puts expected.chop
32
- # puts @describe.run("oci8_simple_test")
30
+
33
31
  assert_equal(expected.chop, @describe.run("oci8_simple_test"))
34
32
  end
35
33
  end
data/test/helper.rb CHANGED
@@ -10,6 +10,7 @@ end
10
10
  require 'test/unit'
11
11
  require 'shoulda'
12
12
  require 'mocha'
13
+ require 'pry'
13
14
 
14
15
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
15
16
  $LOAD_PATH.unshift(File.dirname(__FILE__))
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: oci8_simple
3
3
  version: !ruby/object:Gem::Version
4
- hash: 5
4
+ hash: 3
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 6
9
- - 1
10
- version: 0.6.1
8
+ - 7
9
+ - 0
10
+ version: 0.7.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Billy Reisinger
@@ -15,28 +15,25 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-07-22 00:00:00 -05:00
19
- default_executable:
18
+ date: 2012-04-17 00:00:00 Z
20
19
  dependencies:
21
20
  - !ruby/object:Gem::Dependency
22
- name: ruby-oci8
23
- version_requirements: &id001 !ruby/object:Gem::Requirement
21
+ type: :runtime
22
+ requirement: &id001 !ruby/object:Gem::Requirement
24
23
  none: false
25
24
  requirements:
26
- - - ~>
25
+ - - ">="
27
26
  - !ruby/object:Gem::Version
28
- hash: 7
27
+ hash: 3
29
28
  segments:
30
- - 2
31
29
  - 0
32
- - 4
33
- version: 2.0.4
30
+ version: "0"
31
+ version_requirements: *id001
32
+ name: ruby-oci8
34
33
  prerelease: false
35
- type: :runtime
36
- requirement: *id001
37
34
  - !ruby/object:Gem::Dependency
38
- name: shoulda
39
- version_requirements: &id002 !ruby/object:Gem::Requirement
35
+ type: :development
36
+ requirement: &id002 !ruby/object:Gem::Requirement
40
37
  none: false
41
38
  requirements:
42
39
  - - ">="
@@ -45,44 +42,40 @@ dependencies:
45
42
  segments:
46
43
  - 0
47
44
  version: "0"
45
+ version_requirements: *id002
46
+ name: shoulda
48
47
  prerelease: false
49
- type: :development
50
- requirement: *id002
51
48
  - !ruby/object:Gem::Dependency
52
- name: bundler
53
- version_requirements: &id003 !ruby/object:Gem::Requirement
49
+ type: :development
50
+ requirement: &id003 !ruby/object:Gem::Requirement
54
51
  none: false
55
52
  requirements:
56
- - - ~>
53
+ - - ">="
57
54
  - !ruby/object:Gem::Version
58
- hash: 23
55
+ hash: 3
59
56
  segments:
60
- - 1
61
- - 0
62
57
  - 0
63
- version: 1.0.0
58
+ version: "0"
59
+ version_requirements: *id003
60
+ name: jeweler
64
61
  prerelease: false
65
- type: :development
66
- requirement: *id003
67
62
  - !ruby/object:Gem::Dependency
68
- name: jeweler
69
- version_requirements: &id004 !ruby/object:Gem::Requirement
63
+ type: :development
64
+ requirement: &id004 !ruby/object:Gem::Requirement
70
65
  none: false
71
66
  requirements:
72
- - - ~>
67
+ - - ">="
73
68
  - !ruby/object:Gem::Version
74
- hash: 7
69
+ hash: 3
75
70
  segments:
76
- - 1
77
- - 5
78
- - 2
79
- version: 1.5.2
71
+ - 0
72
+ version: "0"
73
+ version_requirements: *id004
74
+ name: mocha
80
75
  prerelease: false
81
- type: :development
82
- requirement: *id004
83
76
  - !ruby/object:Gem::Dependency
84
- name: mocha
85
- version_requirements: &id005 !ruby/object:Gem::Requirement
77
+ type: :development
78
+ requirement: &id005 !ruby/object:Gem::Requirement
86
79
  none: false
87
80
  requirements:
88
81
  - - ">="
@@ -91,12 +84,12 @@ dependencies:
91
84
  segments:
92
85
  - 0
93
86
  version: "0"
87
+ version_requirements: *id005
88
+ name: pry
94
89
  prerelease: false
95
- type: :development
96
- requirement: *id005
97
90
  - !ruby/object:Gem::Dependency
98
- name: ruby-oci8
99
- version_requirements: &id006 !ruby/object:Gem::Requirement
91
+ type: :runtime
92
+ requirement: &id006 !ruby/object:Gem::Requirement
100
93
  none: false
101
94
  requirements:
102
95
  - - ~>
@@ -107,18 +100,18 @@ dependencies:
107
100
  - 0
108
101
  - 4
109
102
  version: 2.0.4
103
+ version_requirements: *id006
104
+ name: ruby-oci8
110
105
  prerelease: false
111
- type: :runtime
112
- requirement: *id006
113
106
  description: |-
114
107
  Command-line tools for interacting with an Oracle database. This client is intended to be used
115
108
  to aid development and automation. This is *not* meant to replace an ORM such as ActiveRecord + OracleEnhancedAdapter.
116
109
  The only prerequisite to running this code is that you have installed the ruby-oci8 gem on your machine.
117
110
  email: billy.reisinger@gmail.com
118
111
  executables:
119
- - show
120
112
  - oci8_simple
121
113
  - describe
114
+ - show
122
115
  extensions: []
123
116
 
124
117
  extra_rdoc_files:
@@ -146,7 +139,6 @@ files:
146
139
  - test/describe_test.rb
147
140
  - test/helper.rb
148
141
  - test/show_test.rb
149
- has_rdoc: true
150
142
  homepage: http://github.com/unclebilly/oci8_simple
151
143
  licenses:
152
144
  - MIT
@@ -176,13 +168,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
176
168
  requirements: []
177
169
 
178
170
  rubyforge_project:
179
- rubygems_version: 1.5.2
171
+ rubygems_version: 1.8.19
180
172
  signing_key:
181
173
  specification_version: 3
182
174
  summary: Command-line tools for interacting with an Oracle database.
183
- test_files:
184
- - test/cli_test.rb
185
- - test/client_test.rb
186
- - test/describe_test.rb
187
- - test/helper.rb
188
- - test/show_test.rb
175
+ test_files: []
176
+