Class: PLSQL::Schema
- Inherits:
-
Object
- Object
- PLSQL::Schema
- Includes:
- SQLStatements
- Defined in:
- lib/plsql/schema.rb
Constant Summary collapse
- DBMS_OUTPUT_MAX_LINES =
Maximum line numbers for DBMS_OUTPUT in one PL/SQL call (from DBMSOUTPUT_LINESARRAY type)
2147483647
- @@schemas =
{}
Instance Attribute Summary collapse
-
#connection ⇒ Object
Returns connection wrapper object (this is not raw OCI8 or JDBC connection!).
Class Method Summary collapse
Instance Method Summary collapse
-
#activerecord_class=(ar_class) ⇒ Object
Set connection to current ActiveRecord connection (use in initializer file):.
-
#connect!(*args) ⇒ Object
Create new OCI8 or JDBC connection using one of the following ways:.
-
#dbms_output_buffer_size ⇒ Object
DBMS_OUTPUT buffer size (default is 20_000).
-
#dbms_output_buffer_size=(value) ⇒ Object
Seet DBMS_OUTPUT buffer size (default is 20_000).
-
#dbms_output_stream ⇒ Object
IO stream where to log DBMS_OUTPUT from PL/SQL procedures.
-
#dbms_output_stream=(stream) ⇒ Object
Specify IO stream where to log DBMS_OUTPUT from PL/SQL procedures.
-
#default_timezone ⇒ Object
Default timezone to which database values will be converted - :utc or :local.
-
#default_timezone=(value) ⇒ Object
Set default timezone to which database values will be converted - :utc or :local.
-
#initialize(raw_conn = nil, schema = nil, original_schema = nil) ⇒ Schema
constructor
:nodoc:.
-
#local_timezone_offset ⇒ Object
Same implementation as for ActiveRecord DateTimes aren’t aware of DST rules, so use a consistent non-DST offset when creating a DateTime with an offset in the local zone.
-
#logoff ⇒ Object
Disconnect from Oracle.
-
#raw_connection=(raw_conn) ⇒ Object
:nodoc:.
-
#root_schema ⇒ Object
:nodoc:.
-
#schema_name ⇒ Object
Current Oracle schema name.
Methods included from SQLStatements
#commit, #execute, #rollback, #rollback_to, #savepoint, #select, #select_all, #select_first, #select_one
Constructor Details
#initialize(raw_conn = nil, schema = nil, original_schema = nil) ⇒ Schema
:nodoc:
19 20 21 22 23 |
# File 'lib/plsql/schema.rb', line 19 def initialize(raw_conn = nil, schema = nil, original_schema = nil) #:nodoc: self.connection = raw_conn @schema_name = schema ? schema.to_s.upcase : nil @original_schema = original_schema end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object (private)
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/plsql/schema.rb', line 176 def method_missing(method, *args, &block) raise ArgumentError, "No database connection" unless connection # search in database if not in cache at first object = (@schema_objects[method] ||= find_database_object(method) || find_other_schema(method) || find_public_synonym(method) || find_standard_procedure(method)) raise ArgumentError, "No database object '#{method.to_s.upcase}' found" unless object if object.is_a?(Procedure) object.exec(*args, &block) elsif object.is_a?(Type) && !args.empty? object.new(*args, &block) else object end end |
Instance Attribute Details
#connection ⇒ Object
Returns connection wrapper object (this is not raw OCI8 or JDBC connection!)
26 27 28 |
# File 'lib/plsql/schema.rb', line 26 def connection @connection end |
Class Method Details
.find_or_new(connection_alias) ⇒ Object
:nodoc:
8 9 10 11 12 13 14 15 |
# File 'lib/plsql/schema.rb', line 8 def find_or_new(connection_alias) #:nodoc: connection_alias ||= :default if @@schemas[connection_alias] @@schemas[connection_alias] else @@schemas[connection_alias] = self.new end end |
Instance Method Details
#activerecord_class=(ar_class) ⇒ Object
Set connection to current ActiveRecord connection (use in initializer file):
plsql.activerecord_class = ActiveRecord::Base
78 79 80 81 82 |
# File 'lib/plsql/schema.rb', line 78 def activerecord_class=(ar_class) @connection = ar_class ? Connection.create(nil, ar_class) : nil reset_instance_variables ar_class end |
#connect!(*args) ⇒ Object
Create new OCI8 or JDBC connection using one of the following ways:
plsql.connect! username, password, database_tns_alias
plsql.connect! username, password, :host => host, :port => port, :database => database
plsql.connect! :username => username, :password => password, :database => database_tns_alias
plsql.connect! :username => username, :password => password, :host => host, :port => port, :database => database
64 65 66 67 68 69 70 71 72 |
# File 'lib/plsql/schema.rb', line 64 def connect!(*args) params = {} params[:username] = args.shift if args[0].is_a?(String) params[:password] = args.shift if args[0].is_a?(String) params[:database] = args.shift if args[0].is_a?(String) params.merge!(args.shift) if args[0].is_a?(Hash) raise ArgumentError, "Wrong number of arguments" unless args.empty? self.connection = Connection.create_new(params) end |
#dbms_output_buffer_size ⇒ Object
DBMS_OUTPUT buffer size (default is 20_000)
125 126 127 128 129 130 131 |
# File 'lib/plsql/schema.rb', line 125 def dbms_output_buffer_size if @original_schema @original_schema.dbms_output_buffer_size else @dbms_output_buffer_size || 20_000 end end |
#dbms_output_buffer_size=(value) ⇒ Object
Seet DBMS_OUTPUT buffer size (default is 20_000). Example:
plsql.dbms_output_buffer_size = 100_000
137 138 139 |
# File 'lib/plsql/schema.rb', line 137 def dbms_output_buffer_size=(value) @dbms_output_buffer_size = value end |
#dbms_output_stream ⇒ Object
IO stream where to log DBMS_OUTPUT from PL/SQL procedures.
156 157 158 159 160 161 162 |
# File 'lib/plsql/schema.rb', line 156 def dbms_output_stream if @original_schema @original_schema.dbms_output_stream else @dbms_output_stream end end |
#dbms_output_stream=(stream) ⇒ Object
Specify IO stream where to log DBMS_OUTPUT from PL/SQL procedures. Example:
plsql.dbms_output_stream = STDOUT
148 149 150 151 152 153 |
# File 'lib/plsql/schema.rb', line 148 def dbms_output_stream=(stream) @dbms_output_stream = stream if @dbms_output_stream.nil? && @connection sys.dbms_output.disable end end |
#default_timezone ⇒ Object
Default timezone to which database values will be converted - :utc or :local
97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/plsql/schema.rb', line 97 def default_timezone if @original_schema @original_schema.default_timezone else @default_timezone || # Use ActiveRecord class default_timezone when ActiveRecord connection is used (@connection && (ar_class = @connection.activerecord_class) && ar_class.default_timezone) || # default to local timezone :local end end |
#default_timezone=(value) ⇒ Object
Set default timezone to which database values will be converted - :utc or :local
110 111 112 113 114 115 116 |
# File 'lib/plsql/schema.rb', line 110 def default_timezone=(value) if [:local, :utc].include?(value) @default_timezone = value else raise ArgumentError, "default timezone should be :local or :utc" end end |
#local_timezone_offset ⇒ Object
Same implementation as for ActiveRecord DateTimes aren’t aware of DST rules, so use a consistent non-DST offset when creating a DateTime with an offset in the local zone
120 121 122 |
# File 'lib/plsql/schema.rb', line 120 def local_timezone_offset #:nodoc: ::Time.local(2007).utc_offset.to_r / 86400 end |
#logoff ⇒ Object
Disconnect from Oracle
85 86 87 88 |
# File 'lib/plsql/schema.rb', line 85 def logoff @connection.logoff self.connection = nil end |
#raw_connection=(raw_conn) ⇒ Object
:nodoc:
32 33 34 35 |
# File 'lib/plsql/schema.rb', line 32 def raw_connection=(raw_conn) #:nodoc: @connection = raw_conn ? Connection.create(raw_conn) : nil reset_instance_variables end |
#root_schema ⇒ Object
:nodoc:
28 29 30 |
# File 'lib/plsql/schema.rb', line 28 def root_schema #:nodoc: @original_schema || self end |
#schema_name ⇒ Object
Current Oracle schema name
91 92 93 94 |
# File 'lib/plsql/schema.rb', line 91 def schema_name return nil unless connection @schema_name ||= select_first("SELECT SYS_CONTEXT('userenv','session_user') FROM dual")[0] end |