Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add 'describe' bin script to describe a table

  • Loading branch information...
commit 83d0429464f3c49efaaed5d83abb8b716e1cae07 1 parent 7635380
@unclebilly authored
View
3  .gitignore
@@ -18,4 +18,5 @@ pkg
*.tmproj
tmtags
-sqlnet.log
+sqlnet.log
+.rvmrc
View
2  Gemfile
@@ -1,3 +1,5 @@
+source :rubygems
+
gem "ruby-oci8", "~>2.0.4"
group :development do
View
1  Gemfile.lock
@@ -1,4 +1,5 @@
GEM
+ remote: http://rubygems.org/
specs:
git (1.2.5)
jeweler (1.5.2)
View
5 bin/describe
@@ -0,0 +1,5 @@
+#!/usr/bin/env ruby
+$:.unshift File.join(File.dirname(__FILE__), "..", "lib")
+require 'oci8_simple'
+
+Oci8Simple::Reflector.run_from_argv
View
3  lib/oci8_simple.rb
@@ -6,5 +6,6 @@
gem 'ruby-oci8'
require 'oci8'
+require 'oci8_simple/cli'
require 'oci8_simple/client'
-require 'oci8_simple/cli'
+require 'oci8_simple/describe'
View
7 lib/oci8_simple/client.rb
@@ -90,12 +90,15 @@ def config
ERR
end
- private
-
+ # Create and return raw Oci8 connection
def conn
@conn ||= new_connection
end
+ private
+
+
+
def new_connection
c = OCI8.new(config["username"], config["password"], config["database"])
c.autocommit = true
View
36 lib/oci8_simple/describe.rb
@@ -0,0 +1,36 @@
+class Oci8Simple::Describe
+ def initialize(env=nil)
+ @env = env || "development"
+ end
+
+ def run(table)
+ description = client.conn.describe_table(table)
+ description.columns.map(&:to_s).join("\n")
+ end
+
+ def self.usage
+ "Usage: #{0} TABLE_NAME [ENVIRONMENT]"
+ end
+
+ def self.run_from_argv
+ o = OptionParser.new do |opt|
+ opt.banner = usage
+ opt.on("-v", "--version", "Show version") do
+ puts "version #{File.read(File.join(File.dirname(__FILE__), '..', '..', 'VERSION'))}"
+ exit
+ end
+ end
+ o.parse!
+ if(ARGV[0].nil?)
+ puts o
+ else
+ puts self.new(ARGV[1]).run(ARGV[0])
+ end
+ end
+
+ private
+
+ def client
+ @client ||= Oci8Simple::Client.new(@env)
+ end
+end
View
2  oci8_simple.gemspec
@@ -9,7 +9,7 @@ Gem::Specification.new do |s|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Billy Reisinger"]
- s.date = %q{2011-05-16}
+ s.date = %q{2011-06-10}
s.default_executable = %q{oci8_simple}
s.description = %q{Run single statements against an arbitrary Oracle schema. This client is intended to be used by simple
command-line scripts to aid automation. This is *not* meant to replace an ORM such as ActiveRecord + OracleEnhancedAdapter.
View
27 test/describe_test.rb
@@ -0,0 +1,27 @@
+require File.join(File.dirname(__FILE__), 'helper')
+class DescribeTest < Test::Unit::TestCase
+ context "Given a table and some data" do
+ setup do
+ @client = Oci8Simple::Client.new("test")
+ @client.run "DROP TABLE OCI8_SIMPLE_TEST CASCADE CONSTRAINTS" rescue nil
+ @client.run <<-SQL
+ CREATE TABLE "OCI8_SIMPLE_TEST"
+ (
+ "ID" NUMBER(38,0) NOT NULL ENABLE,
+ "NAME" VARCHAR2(400 CHAR) NOT NULL ENABLE,
+ "TEXTS" CLOB
+ )
+ SQL
+ @describe = Oci8Simple::Describe.new("test")
+ end
+ context "describing a table" do
+ setup do
+ end
+ should "format results for the command line" do
+ assert_equal(
+ "\"ID\" NUMBER(38) NOT NULL\n\"NAME\" VARCHAR2(400 CHAR) NOT NULL\n\"TEXTS\" CLOB",
+ @describe.run("oci8_simple_test"))
+ end
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.