oci8_simple 0.1.1 → 0.2.0
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.rdoc +4 -4
- data/Rakefile +1 -1
- data/VERSION +1 -1
- data/lib/oci8_simple/cli.rb +27 -0
- data/lib/oci8_simple/client.rb +89 -0
- data/lib/oci8_simple.rb +4 -137
- data/oci8_simple.gemspec +9 -5
- data/test/cli_test.rb +36 -0
- data/test/{test_oci8_simple.rb → client_test.rb} +11 -22
- metadata +10 -6
data/README.rdoc
CHANGED
@@ -5,7 +5,7 @@ command-line scripts to aid automation. This is *not* meant to replace an ORM s
|
|
5
5
|
The only prerequisite to running this code is that you have installed the ruby-oci8 gem on your machine.
|
6
6
|
|
7
7
|
== Installation
|
8
|
-
|
8
|
+
gem install oci8_simple
|
9
9
|
|
10
10
|
== Configuration
|
11
11
|
To configure environments and schema settings, edit the
|
@@ -24,11 +24,9 @@ All logging is done to ~/.oci8_simple/oci8_simple.log
|
|
24
24
|
|
25
25
|
== Examples
|
26
26
|
* Initialize a client against the development schema
|
27
|
-
client = Oci8Simple.new
|
27
|
+
client = Oci8Simple::Client.new
|
28
28
|
* Run a simple select query against development schema
|
29
29
|
client.run('select id, name from foos') => [[2, "lol"], [3, "hey"], ...]
|
30
|
-
* Run a simple select query against stage schema
|
31
|
-
Oci8Simple.new("stage").run('select id, name from foos') => [[2, "lol"], [3, "hey"], ...]
|
32
30
|
* Update something
|
33
31
|
client.run <<-SQL
|
34
32
|
UPDATE foos SET bar='baz' WHERE id=1233
|
@@ -49,6 +47,8 @@ All logging is done to ~/.oci8_simple/oci8_simple.log
|
|
49
47
|
INSERT INTO T1 VALUES(b,a);
|
50
48
|
END;
|
51
49
|
SQL
|
50
|
+
* Run a query against stage schema
|
51
|
+
Oci8Simple::Client.new("stage").run('select id, name from foos') => [[2, "lol"], [3, "hey"], ...]
|
52
52
|
|
53
53
|
== Contributing to oci8_simple
|
54
54
|
|
data/Rakefile
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Oci8Simple
|
2
|
+
# == Description
|
3
|
+
# A very thin wrapper around Oci8Simple::Client that formats the output
|
4
|
+
# as CSV (suitable for printing on the console)
|
5
|
+
# == Usage
|
6
|
+
# cli = Oci8Simple::Cli.new
|
7
|
+
# cli.run "select id, name from foos" # "3, Bacon\n5, Cheese Puffs\n..."
|
8
|
+
class Cli
|
9
|
+
attr_accessor :env, :client
|
10
|
+
|
11
|
+
def initialize(env=nil)
|
12
|
+
self.env = env
|
13
|
+
end
|
14
|
+
|
15
|
+
def run(sql)
|
16
|
+
format(client.run(sql))
|
17
|
+
end
|
18
|
+
|
19
|
+
def format(arr)
|
20
|
+
arr.map{|row| row.join(", ")}.join("\n")
|
21
|
+
end
|
22
|
+
|
23
|
+
def client
|
24
|
+
@client ||= Client.new(env)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
|
4
|
+
# Run single statements against an arbitrary Oracle schema. This client is intended to be used by simple
|
5
|
+
# command-line scripts to aid automation. This is *not* meant to replace an ORM such as ActiveRecord + OracleEnhancedAdapter.
|
6
|
+
# The only prerequisite to running this code is that you have installed the ruby-oci8 gem on your machine.
|
7
|
+
#
|
8
|
+
# == Logging
|
9
|
+
# All logging is done to ~/.oci8_simple/oci8_simple.log
|
10
|
+
#
|
11
|
+
# == Examples
|
12
|
+
# * Initialize a client against the development schema
|
13
|
+
# client = Oci8Simple::Client.new
|
14
|
+
# * Run a simple select query against development schema
|
15
|
+
# client.run('select id, name from foos') => [[2, "lol"], [3, "hey"], ...]
|
16
|
+
# * Run a simple select query against stage schema
|
17
|
+
# Oci8Simple:Client.new("stage").run('select id, name from foos') => [[2, "lol"], [3, "hey"], ...]
|
18
|
+
# * Update something
|
19
|
+
# client.run <<-SQL
|
20
|
+
# UPDATE foos SET bar='baz' WHERE id=1233
|
21
|
+
# SQL
|
22
|
+
# * Run some DDL
|
23
|
+
# client.run <<-SQL
|
24
|
+
# CREATE TABLE foos (
|
25
|
+
# ID NUMBER(38) NOT NULL
|
26
|
+
# )
|
27
|
+
# SQL
|
28
|
+
# * Run some PL/SQL
|
29
|
+
# client.run <<-SQL
|
30
|
+
# DECLARE
|
31
|
+
# a NUMBER;
|
32
|
+
# b NUMBER;
|
33
|
+
# BEGIN
|
34
|
+
# SELECT e,f INTO a,b FROM T1 WHERE e>1;
|
35
|
+
# INSERT INTO T1 VALUES(b,a);
|
36
|
+
# END;
|
37
|
+
# SQL
|
38
|
+
module Oci8Simple
|
39
|
+
class Client
|
40
|
+
USER_DIR = File.join(ENV["HOME"], ".oci8_simple")
|
41
|
+
CONFIG_FILE = File.join(USER_DIR, "database.yml")
|
42
|
+
LOG_FILE = File.join(USER_DIR, "oci8_simple.log")
|
43
|
+
|
44
|
+
attr_accessor :log_file, :env
|
45
|
+
|
46
|
+
# * env is the environment heading in your database.yml file
|
47
|
+
def initialize(env=nil)
|
48
|
+
self.env = env || "development"
|
49
|
+
conn.autocommit = true
|
50
|
+
end
|
51
|
+
|
52
|
+
def log_file
|
53
|
+
@log_file ||= File.open(LOG_FILE, 'a')
|
54
|
+
end
|
55
|
+
|
56
|
+
def run(sql)
|
57
|
+
log(sql)
|
58
|
+
result = []
|
59
|
+
conn.exec(sql) do |r|
|
60
|
+
row = []
|
61
|
+
r.map do |col|
|
62
|
+
if col.class == BigDecimal
|
63
|
+
row << col.to_i
|
64
|
+
elsif col.class == OCI8::CLOB
|
65
|
+
row << col.read
|
66
|
+
else
|
67
|
+
row << col.to_s
|
68
|
+
end
|
69
|
+
end
|
70
|
+
result << row
|
71
|
+
end
|
72
|
+
result
|
73
|
+
end
|
74
|
+
|
75
|
+
def config
|
76
|
+
@config ||= YAML.load_file(CONFIG_FILE)[env]
|
77
|
+
end
|
78
|
+
|
79
|
+
private
|
80
|
+
|
81
|
+
def conn
|
82
|
+
@conn ||= OCI8.new(config["username"], config["password"], config["database"])
|
83
|
+
end
|
84
|
+
|
85
|
+
def log(str)
|
86
|
+
log_file.puts "#{Time.now} - #{@env} - #{str}"
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
data/lib/oci8_simple.rb
CHANGED
@@ -1,143 +1,10 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
|
3
1
|
require 'rubygems'
|
4
|
-
gem 'ruby-oci8'
|
5
|
-
require 'oci8'
|
6
2
|
require 'pp'
|
7
3
|
require 'bigdecimal'
|
8
4
|
require 'yaml'
|
9
5
|
|
10
|
-
|
11
|
-
|
12
|
-
# The only prerequisite to running this code is that you have installed the ruby-oci8 gem on your machine.
|
13
|
-
#
|
14
|
-
# == Installation
|
15
|
-
# [sudo] gem install oci8-simple
|
16
|
-
#
|
17
|
-
# == Configuration
|
18
|
-
# To configure environments and schema settings, edit the
|
19
|
-
# database.yml file in ~/.oci8_simple/
|
20
|
-
# development:
|
21
|
-
# database: oracle.hostname:1521/sid
|
22
|
-
# username: foo_dev
|
23
|
-
# password: OMG333
|
24
|
-
# test:
|
25
|
-
# database: oracle.hostname:1521/sid
|
26
|
-
# username: foo_test
|
27
|
-
# password: OMG333
|
28
|
-
#
|
29
|
-
# == Logging
|
30
|
-
# All logging is done to ~/.oci8_simple/oci8_simple.log
|
31
|
-
#
|
32
|
-
# == Examples
|
33
|
-
# * Initialize a client against the development schema
|
34
|
-
# client = Oci8Simple.new
|
35
|
-
# * Run a simple select query against development schema
|
36
|
-
# client.run('select id, name from foos') => [[2, "lol"], [3, "hey"], ...]
|
37
|
-
# * Run a simple select query against stage schema
|
38
|
-
# Oci8Simple.new("stage").run('select id, name from foos') => [[2, "lol"], [3, "hey"], ...]
|
39
|
-
# * Update something
|
40
|
-
# client.run <<-SQL
|
41
|
-
# UPDATE foos SET bar='baz' WHERE id=1233
|
42
|
-
# SQL
|
43
|
-
# * Run some DDL
|
44
|
-
# client.run <<-SQL
|
45
|
-
# CREATE TABLE foos (
|
46
|
-
# ID NUMBER(38) NOT NULL
|
47
|
-
# )
|
48
|
-
# SQL
|
49
|
-
# * Run some PL/SQL
|
50
|
-
# client.run <<-SQL
|
51
|
-
# DECLARE
|
52
|
-
# a NUMBER;
|
53
|
-
# b NUMBER;
|
54
|
-
# BEGIN
|
55
|
-
# SELECT e,f INTO a,b FROM T1 WHERE e>1;
|
56
|
-
# INSERT INTO T1 VALUES(b,a);
|
57
|
-
# END;
|
58
|
-
# SQL
|
59
|
-
class Oci8Simple
|
60
|
-
USER_DIR = File.join(ENV["HOME"], ".oci8_simple")
|
61
|
-
CONFIG_FILE = File.join(USER_DIR, "database.yml")
|
62
|
-
LOG_FILE = File.join(USER_DIR, "oci8_simple.log")
|
63
|
-
|
64
|
-
attr_accessor :log_file, :puts_mode, :env
|
65
|
-
|
66
|
-
# * env is the environment heading in your database.yml file
|
67
|
-
# * uname is the username you want to use (defaults to config["username"])
|
68
|
-
# * puts_mode is whether you want to format the result for printing to a terminal (defaults to false)
|
69
|
-
def initialize(env="development", uname=nil, puts_mode=false)
|
70
|
-
@env = env
|
71
|
-
@uname = uname
|
72
|
-
@puts_mode = puts_mode
|
73
|
-
conn.autocommit = true
|
74
|
-
end
|
75
|
-
|
76
|
-
def log_file
|
77
|
-
@log_file ||= File.open(LOG_FILE, 'a')
|
78
|
-
end
|
79
|
-
|
80
|
-
def run(sql)
|
81
|
-
log(sql)
|
82
|
-
result = []
|
83
|
-
conn.exec(sql) do |r|
|
84
|
-
row = []
|
85
|
-
r.map do |col|
|
86
|
-
if col.class == BigDecimal
|
87
|
-
row << col.to_i
|
88
|
-
elsif col.class == OCI8::CLOB
|
89
|
-
row << col.read
|
90
|
-
else
|
91
|
-
row << col.to_s
|
92
|
-
end
|
93
|
-
end
|
94
|
-
result << row
|
95
|
-
end
|
96
|
-
|
97
|
-
if @puts_mode
|
98
|
-
result.map{|row| row.join(", ")}.join("\n")
|
99
|
-
else
|
100
|
-
if(result.length == 1 && result[0].length == 1)
|
101
|
-
result[0][0]
|
102
|
-
else
|
103
|
-
result
|
104
|
-
end
|
105
|
-
end
|
106
|
-
end
|
107
|
-
|
108
|
-
def config
|
109
|
-
@config ||= YAML.load_file(CONFIG_FILE)[@env]
|
110
|
-
end
|
111
|
-
|
112
|
-
private
|
113
|
-
def conn
|
114
|
-
@o ||= OCI8.new(@uname || config["username"], config["password"], config["database"])
|
115
|
-
end
|
116
|
-
|
117
|
-
def log(str)
|
118
|
-
log_file.puts "#{Time.now} - #{@env} - #{str}"
|
119
|
-
end
|
120
|
-
|
121
|
-
def self.help
|
122
|
-
<<-HELP
|
123
|
-
Run arbitrary SQL against an Oracle schema. The default schema is the one defined
|
124
|
-
in the development section of your ~/.oci8_simple/database.yml file.
|
125
|
-
|
126
|
-
You do not need to include a semicolon to end a statement. The statement should be enclosed in single
|
127
|
-
or double quotes.
|
128
|
-
|
129
|
-
Usage: #{$0} SQL [ENV]
|
130
|
-
Example: #{$0} 'select id from users' stage
|
131
|
-
HELP
|
132
|
-
end
|
133
|
-
end
|
6
|
+
gem 'ruby-oci8'
|
7
|
+
require 'oci8'
|
134
8
|
|
135
|
-
|
136
|
-
|
137
|
-
Oci8Simple.help
|
138
|
-
else
|
139
|
-
sql = ARGV[0] || ""
|
140
|
-
env = ARGV[1] || "development"
|
141
|
-
Oci8Simple.new(env).run(sql)
|
142
|
-
end
|
143
|
-
end
|
9
|
+
require 'oci8_simple/client'
|
10
|
+
require 'oci8_simple/cli'
|
data/oci8_simple.gemspec
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{oci8_simple}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.2.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"]
|
@@ -26,9 +26,12 @@ Gem::Specification.new do |s|
|
|
26
26
|
"Rakefile",
|
27
27
|
"VERSION",
|
28
28
|
"lib/oci8_simple.rb",
|
29
|
+
"lib/oci8_simple/cli.rb",
|
30
|
+
"lib/oci8_simple/client.rb",
|
29
31
|
"oci8_simple.gemspec",
|
30
|
-
"test/
|
31
|
-
"test/
|
32
|
+
"test/cli_test.rb",
|
33
|
+
"test/client_test.rb",
|
34
|
+
"test/helper.rb"
|
32
35
|
]
|
33
36
|
s.homepage = %q{http://github.com/unclebilly/oci8_simple}
|
34
37
|
s.licenses = ["MIT"]
|
@@ -36,8 +39,9 @@ Gem::Specification.new do |s|
|
|
36
39
|
s.rubygems_version = %q{1.5.2}
|
37
40
|
s.summary = %q{Run single statements against an arbitrary Oracle schema.}
|
38
41
|
s.test_files = [
|
39
|
-
"test/
|
40
|
-
"test/
|
42
|
+
"test/cli_test.rb",
|
43
|
+
"test/client_test.rb",
|
44
|
+
"test/helper.rb"
|
41
45
|
]
|
42
46
|
|
43
47
|
if s.respond_to? :specification_version then
|
data/test/cli_test.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'helper')
|
2
|
+
class CliTest < Test::Unit::TestCase
|
3
|
+
context "Given a table and some data" do
|
4
|
+
setup do
|
5
|
+
@client = Oci8Simple::Client.new("test")
|
6
|
+
@client.run "DROP TABLE OCI8_SIMPLE_TEST CASCADE CONSTRAINTS" rescue nil
|
7
|
+
@client.run <<-SQL
|
8
|
+
CREATE TABLE "OCI8_SIMPLE_TEST"
|
9
|
+
(
|
10
|
+
"ID" NUMBER(38,0) NOT NULL ENABLE,
|
11
|
+
"NAME" VARCHAR2(400 CHAR) NOT NULL ENABLE,
|
12
|
+
"TEXTS" CLOB
|
13
|
+
)
|
14
|
+
SQL
|
15
|
+
@client.run "INSERT INTO OCI8_SIMPLE_TEST (ID, NAME, TEXTS) VALUES (1, 'Johnny', 'OMG')"
|
16
|
+
@client.run "INSERT INTO OCI8_SIMPLE_TEST (ID, NAME, TEXTS) VALUES (2, 'Jenny', 'OMG')"
|
17
|
+
@cli = Oci8Simple::Cli.new("test")
|
18
|
+
end
|
19
|
+
context "with an env" do
|
20
|
+
setup do
|
21
|
+
@cli = Oci8Simple::Cli.new("test")
|
22
|
+
end
|
23
|
+
should "format results for the command line" do
|
24
|
+
assert_equal("1, Johnny, OMG\n2, Jenny, OMG", @cli.run("select * from oci8_simple_test"))
|
25
|
+
end
|
26
|
+
end
|
27
|
+
context "without an env" do
|
28
|
+
setup do
|
29
|
+
@cli = Oci8Simple::Cli.new
|
30
|
+
end
|
31
|
+
should "default to development" do
|
32
|
+
assert_equal("development", @cli.client.env)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -1,15 +1,15 @@
|
|
1
|
-
require 'helper'
|
1
|
+
require File.join(File.dirname(__FILE__), 'helper')
|
2
2
|
|
3
|
-
class
|
3
|
+
class ClientTest < Test::Unit::TestCase
|
4
4
|
context "A client" do
|
5
5
|
should "default to development environment" do
|
6
|
-
@client = Oci8Simple.new
|
6
|
+
@client = Oci8Simple::Client.new
|
7
7
|
assert_equal "development", @client.env
|
8
8
|
end
|
9
9
|
end
|
10
10
|
context "Given a table" do
|
11
11
|
setup do
|
12
|
-
@client = Oci8Simple.new("test"
|
12
|
+
@client = Oci8Simple::Client.new("test")
|
13
13
|
@client.run "DROP TABLE OCI8_SIMPLE_TEST CASCADE CONSTRAINTS" rescue nil
|
14
14
|
@client.run <<-SQL
|
15
15
|
CREATE TABLE "OCI8_SIMPLE_TEST"
|
@@ -26,33 +26,22 @@ class TestOci8Simple < Test::Unit::TestCase
|
|
26
26
|
INSERT INTO OCI8_SIMPLE_TEST (ID, NAME, TEXTS) VALUES (2, 'Jenny', 'OMG')
|
27
27
|
SQL
|
28
28
|
end
|
29
|
-
context "
|
30
|
-
setup do
|
31
|
-
end
|
29
|
+
context "the client" do
|
32
30
|
should "be able to run a simple query with a single result" do
|
33
|
-
assert_equal 2, @client.run("select count(*) from OCI8_SIMPLE_TEST")
|
31
|
+
assert_equal [[2]], @client.run("select count(*) from OCI8_SIMPLE_TEST")
|
34
32
|
end
|
35
33
|
should "be able to run a simple query with multiple results and multiple columns" do
|
36
34
|
assert_equal [["1", "Johnny", "OMG"], ["2", "Jenny", "OMG"]], @client.run("select * from OCI8_SIMPLE_TEST")
|
37
35
|
end
|
38
36
|
|
39
37
|
should "have logged something" do
|
40
|
-
File.unlink(Oci8Simple::LOG_FILE)
|
41
|
-
assert(!File.exists?(Oci8Simple::LOG_FILE))
|
42
|
-
@client = Oci8Simple.new("test"
|
38
|
+
File.unlink(Oci8Simple::Client::LOG_FILE)
|
39
|
+
assert(!File.exists?(Oci8Simple::Client::LOG_FILE))
|
40
|
+
@client = Oci8Simple::Client.new("test")
|
43
41
|
@client.run "select NULL from dual"
|
44
42
|
@client.log_file.close
|
45
|
-
assert(File.exists?(Oci8Simple::LOG_FILE))
|
46
|
-
assert(!(0 == File.size(Oci8Simple::LOG_FILE)))
|
47
|
-
end
|
48
|
-
end
|
49
|
-
context "when puts mode is true the client" do
|
50
|
-
setup do
|
51
|
-
@client.puts_mode = true
|
52
|
-
end
|
53
|
-
should "format result" do
|
54
|
-
result = @client.run("select * from OCI8_SIMPLE_TEST")
|
55
|
-
assert_equal "1, Johnny, OMG\n2, Jenny, OMG", result
|
43
|
+
assert(File.exists?(Oci8Simple::Client::LOG_FILE))
|
44
|
+
assert(!(0 == File.size(Oci8Simple::Client::LOG_FILE)))
|
56
45
|
end
|
57
46
|
end
|
58
47
|
end
|
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:
|
4
|
+
hash: 23
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 2
|
9
|
+
- 0
|
10
|
+
version: 0.2.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Billy Reisinger
|
@@ -116,9 +116,12 @@ files:
|
|
116
116
|
- Rakefile
|
117
117
|
- VERSION
|
118
118
|
- lib/oci8_simple.rb
|
119
|
+
- lib/oci8_simple/cli.rb
|
120
|
+
- lib/oci8_simple/client.rb
|
119
121
|
- oci8_simple.gemspec
|
122
|
+
- test/cli_test.rb
|
123
|
+
- test/client_test.rb
|
120
124
|
- test/helper.rb
|
121
|
-
- test/test_oci8_simple.rb
|
122
125
|
has_rdoc: true
|
123
126
|
homepage: http://github.com/unclebilly/oci8_simple
|
124
127
|
licenses:
|
@@ -154,5 +157,6 @@ signing_key:
|
|
154
157
|
specification_version: 3
|
155
158
|
summary: Run single statements against an arbitrary Oracle schema.
|
156
159
|
test_files:
|
160
|
+
- test/cli_test.rb
|
161
|
+
- test/client_test.rb
|
157
162
|
- test/helper.rb
|
158
|
-
- test/test_oci8_simple.rb
|