Class: PLSQL::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/plsql/schema.rb

Constant Summary collapse

@@schemas =
{}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_conn = nil, schema = nil, first = true) ⇒ Schema

Returns a new instance of Schema.



17
18
19
20
21
# File 'lib/plsql/schema.rb', line 17

def initialize(raw_conn = nil, schema = nil, first = true)
  self.connection = raw_conn
  @schema_name = schema ? schema.to_s.upcase : nil
  @first = first
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object (private)

Raises:

  • (ArgumentError)


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/plsql/schema.rb', line 68

def method_missing(method, *args)
  raise ArgumentError, "No PL/SQL connection" unless connection
  if procedure = @procedures[method]
    procedure.exec(*args)
  elsif procedure = Procedure.find(self, method)
    @procedures[method] = procedure
    procedure.exec(*args)
  elsif package = @packages[method]
    package
  elsif package = Package.find(self, method)
    @packages[method] = package
  elsif schema = @schemas[method]
    schema
  elsif schema = find_other_schema(method)
    @schemas[method] = schema
  else
    raise ArgumentError, "No PL/SQL procedure found"
  end
end

Class Method Details

.find_or_new(connection_alias) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/plsql/schema.rb', line 6

def find_or_new(connection_alias)
  connection_alias ||= :default
  if @@schemas[connection_alias]
    @@schemas[connection_alias]
  else
    @@schemas[connection_alias] = self.new
  end
end

Instance Method Details

#commitObject



58
59
60
# File 'lib/plsql/schema.rb', line 58

def commit
  connection.commit
end

#connectionObject



23
24
25
# File 'lib/plsql/schema.rb', line 23

def connection
  @connection
end

#connection=(raw_conn) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/plsql/schema.rb', line 27

def connection=(raw_conn)
  @connection = raw_conn ? Connection.create(raw_conn) : nil
  if @connection
    @procedures = {}
    @packages = {}
    @schemas = {}
  else
    @procedures = nil
    @packages = nil
    @schemas = nil
  end
end

#logoffObject



40
41
42
43
# File 'lib/plsql/schema.rb', line 40

def logoff
  connection.logoff
  self.connection = nil
end

#rollbackObject



62
63
64
# File 'lib/plsql/schema.rb', line 62

def rollback
  connection.rollback
end

#schema_nameObject



45
46
47
48
# File 'lib/plsql/schema.rb', line 45

def schema_name
  return nil unless connection
  @schema_name ||= select_first("SELECT SYS_CONTEXT('userenv','session_user') FROM dual")[0]
end

#select_first(sql, *bindvars) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/plsql/schema.rb', line 50

def select_first(sql, *bindvars)
  # cursor = connection.exec(sql, *bindvars)
  # result = cursor.fetch
  # cursor.close
  # result
  connection.select_first(sql, *bindvars)
end