Module: PLSQL::ProcedureCommon
- Included in:
- Procedure, Type::TypeProcedure
- Defined in:
- lib/plsql/procedure.rb
Overview
:nodoc:
Constant Summary collapse
- PLSQL_COMPOSITE_TYPES =
["PL/SQL RECORD", "PL/SQL TABLE", "TABLE", "VARRAY", "REF CURSOR"].freeze
- PLSQL_COLLECTION_TYPES =
["PL/SQL TABLE", "TABLE", "VARRAY"].freeze
Instance Attribute Summary collapse
-
#argument_list ⇒ Object
readonly
Returns the value of attribute argument_list.
-
#arguments ⇒ Object
readonly
Returns the value of attribute arguments.
-
#out_list ⇒ Object
readonly
Returns the value of attribute out_list.
-
#package ⇒ Object
readonly
Returns the value of attribute package.
-
#procedure ⇒ Object
readonly
Returns the value of attribute procedure.
-
#return ⇒ Object
readonly
Returns the value of attribute return.
-
#schema ⇒ Object
readonly
Returns the value of attribute schema.
-
#schema_name ⇒ Object
readonly
Returns the value of attribute schema_name.
Class Method Summary collapse
-
.type_to_sql(metadata) ⇒ Object
return type string from metadata that can be used in DECLARE block or table definition.
Instance Method Summary collapse
-
#build_sql_type_name(type_owner, type_package, type_name) ⇒ Object
:nodoc:.
-
#collection_type?(data_type) ⇒ Boolean
:nodoc:.
-
#composite_type?(data_type) ⇒ Boolean
:nodoc:.
-
#construct_argument_list_for_overloads ⇒ Object
:nodoc:.
-
#ensure_tmp_tables_created(overload) ⇒ Object
:nodoc:.
-
#get_argument_metadata ⇒ Object
get procedure argument metadata from data dictionary.
-
#get_argument_metadata_below_18c ⇒ Object
:nodoc:.
-
#get_argument_metadata_from_18c ⇒ Object
get procedure argument metadata from data dictionary.
- #get_composite_type(type_owner, type_name, type_package) ⇒ Object
-
#get_element_definition(argument_metadata) ⇒ Object
:nodoc:.
-
#get_field_definitions(argument_metadata) ⇒ Object
:nodoc:.
-
#overloaded? ⇒ Boolean
:nodoc:.
Instance Attribute Details
#argument_list ⇒ Object (readonly)
Returns the value of attribute argument_list.
57 58 59 |
# File 'lib/plsql/procedure.rb', line 57 def argument_list @argument_list end |
#arguments ⇒ Object (readonly)
Returns the value of attribute arguments.
57 58 59 |
# File 'lib/plsql/procedure.rb', line 57 def arguments @arguments end |
#out_list ⇒ Object (readonly)
Returns the value of attribute out_list.
57 58 59 |
# File 'lib/plsql/procedure.rb', line 57 def out_list @out_list end |
#package ⇒ Object (readonly)
Returns the value of attribute package.
58 59 60 |
# File 'lib/plsql/procedure.rb', line 58 def package @package end |
#procedure ⇒ Object (readonly)
Returns the value of attribute procedure.
58 59 60 |
# File 'lib/plsql/procedure.rb', line 58 def procedure @procedure end |
#return ⇒ Object (readonly)
Returns the value of attribute return.
57 58 59 |
# File 'lib/plsql/procedure.rb', line 57 def return @return end |
#schema ⇒ Object (readonly)
Returns the value of attribute schema.
58 59 60 |
# File 'lib/plsql/procedure.rb', line 58 def schema @schema end |
#schema_name ⇒ Object (readonly)
Returns the value of attribute schema_name.
58 59 60 |
# File 'lib/plsql/procedure.rb', line 58 def schema_name @schema_name end |
Class Method Details
.type_to_sql(metadata) ⇒ Object
return type string from metadata that can be used in DECLARE block or table definition
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/plsql/procedure.rb', line 61 def self.type_to_sql() #:nodoc: case [:data_type] when "NUMBER" precision, scale = [:data_precision], [:data_scale] "NUMBER#{precision ? "(#{precision}#{scale ? ",#{scale}" : ""})" : ""}" when "VARCHAR", "VARCHAR2", "CHAR" length = case [:char_used] when "C" then "#{[:char_length]} CHAR" when "B" then "#{[:data_length]} BYTE" else [:data_length] end "#{[:data_type]}#{length && "(#{length})"}" when "NVARCHAR2", "NCHAR" length = [:char_length] "#{[:data_type]}#{length && "(#{length})"}" when "PL/SQL TABLE", "TABLE", "VARRAY", "OBJECT", "XMLTYPE" [:sql_type_name] else [:data_type] end end |
Instance Method Details
#build_sql_type_name(type_owner, type_package, type_name) ⇒ Object
:nodoc:
333 334 335 336 337 338 339 340 341 342 343 344 345 346 |
# File 'lib/plsql/procedure.rb', line 333 def build_sql_type_name(type_owner, type_package, type_name) #:nodoc: if type_owner == nil || type_owner == "PUBLIC" type_owner_res = "" else type_owner_res = "#{type_owner}." end if type_package == nil type_name_res = type_name else type_name_res = "#{type_package}.#{type_name}" end type_name_res && "#{type_owner_res}#{type_name_res}" end |
#collection_type?(data_type) ⇒ Boolean
:nodoc:
518 519 520 |
# File 'lib/plsql/procedure.rb', line 518 def collection_type?(data_type) #:nodoc: PLSQL_COLLECTION_TYPES.include? data_type end |
#composite_type?(data_type) ⇒ Boolean
:nodoc:
513 514 515 |
# File 'lib/plsql/procedure.rb', line 513 def composite_type?(data_type) #:nodoc: PLSQL_COMPOSITE_TYPES.include? data_type end |
#construct_argument_list_for_overloads ⇒ Object
:nodoc:
301 302 303 304 305 306 307 |
# File 'lib/plsql/procedure.rb', line 301 def construct_argument_list_for_overloads #:nodoc: @overloads = @arguments.keys.sort @overloads.each do |overload| @argument_list[overload] = @arguments[overload].keys.sort { |k1, k2| @arguments[overload][k1][:position] <=> @arguments[overload][k2][:position] } @out_list[overload] = @argument_list[overload].select { |k| @arguments[overload][k][:in_out] =~ /OUT/ } end end |
#ensure_tmp_tables_created(overload) ⇒ Object
:nodoc:
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 |
# File 'lib/plsql/procedure.rb', line 309 def ensure_tmp_tables_created(overload) #:nodoc: return if @tmp_tables_created.nil? || @tmp_tables_created[overload] @tmp_table_names[overload] && @tmp_table_names[overload].each do |table_name, | sql = "CREATE GLOBAL TEMPORARY TABLE #{table_name} (\n" = [:element] case [:data_type] when "PL/SQL RECORD" = [:fields] fields_sorted_by_position = .keys.sort_by { |k| [k][:position] } sql << fields_sorted_by_position.map do |field| = [field] "#{field} #{ProcedureCommon.type_to_sql()}" end.join(",\n") else sql << "element #{ProcedureCommon.type_to_sql()}" end sql << ",\ni__ NUMBER(38)\n" sql << ") ON COMMIT PRESERVE ROWS\n" sql_block = "DECLARE\nPRAGMA AUTONOMOUS_TRANSACTION;\nBEGIN\nEXECUTE IMMEDIATE :sql;\nEND;\n" @schema.execute sql_block, sql end @tmp_tables_created[overload] = true end |
#get_argument_metadata ⇒ Object
get procedure argument metadata from data dictionary
85 86 87 88 89 90 91 |
# File 'lib/plsql/procedure.rb', line 85 def #:nodoc: if (@schema.connection.database_version <=> [18, 0, 0, 0]) >= 0 else end end |
#get_argument_metadata_below_18c ⇒ Object
:nodoc:
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/plsql/procedure.rb', line 93 def #:nodoc: @arguments = {} @argument_list = {} @out_list = {} @return = {} @overloaded = false # store reference to previous level record or collection metadata = {} # store tmp tables for each overload for table parameters with types defined inside packages @tmp_table_names = {} # store if tmp tables are created for specific overload @tmp_tables_created = {} # subprogram_id column is available just from version 10g subprogram_id_column = (@schema.connection.database_version <=> [10, 2, 0, 2]) >= 0 ? "subprogram_id" : "NULL" # defaulted is available just from version 11g defaulted_column = (@schema.connection.database_version <=> [11, 0, 0, 0]) >= 0 ? "defaulted" : "NULL" @schema.select_all( "SELECT #{subprogram_id_column}, object_name, TO_NUMBER(overload), argument_name, position, data_level, data_type, in_out, data_length, data_precision, data_scale, char_used, char_length, type_owner, type_name, type_subname, #{defaulted_column} FROM all_arguments WHERE object_id = :object_id AND owner = :owner AND object_name = :procedure_name ORDER BY overload, sequence", @object_id, @schema_name, @procedure ) do |r| subprogram_id, object_name, overload, argument_name, position, data_level, data_type, in_out, data_length, data_precision, data_scale, char_used, char_length, type_owner, type_name, type_subname, defaulted = r @overloaded ||= !overload.nil? # if not overloaded then store arguments at key 0 overload ||= 0 @arguments[overload] ||= {} @return[overload] ||= nil @tmp_table_names[overload] ||= [] sql_type_name = type_owner && "#{type_owner == 'PUBLIC' ? nil : "#{type_owner}."}#{type_name}#{type_subname ? ".#{type_subname}" : nil}" tmp_table_name = nil # type defined inside package if type_subname if collection_type?(data_type) raise ArgumentError, "#{data_type} type #{sql_type_name} definition inside package is not supported as part of other type definition," << " use CREATE TYPE outside package" if data_level > 0 # if subprogram_id was not supported by all_arguments view # then generate unique ID from object_name and overload subprogram_id ||= "#{object_name.hash % 10000}#{overload}" tmp_table_name = "#{Connection::RUBY_TEMP_TABLE_PREFIX}#{@schema.connection.session_id}_#{@object_id}_#{subprogram_id}_#{position}" elsif data_type != "PL/SQL RECORD" # raise exception only when there are no overloaded procedure definitions # (as probably this overload will not be used at all) raise ArgumentError, "Parameter type #{sql_type_name} definition inside package is not supported, use CREATE TYPE outside package" if overload == 0 end end = { position: position && position.to_i, data_type: data_type, in_out: in_out, data_length: data_length && data_length.to_i, data_precision: data_precision && data_precision.to_i, data_scale: data_scale && data_scale.to_i, char_used: char_used, char_length: char_length && char_length.to_i, type_owner: type_owner, type_name: type_name, type_subname: type_subname, sql_type_name: sql_type_name, defaulted: defaulted } if tmp_table_name @tmp_table_names[overload] << [([:tmp_table_name] = tmp_table_name), ] end if composite_type?(data_type) case data_type when "PL/SQL RECORD" [:fields] = {} end [data_level] = end # if function has return value if argument_name.nil? && data_level == 0 && in_out == "OUT" @return[overload] = # if parameter else # top level parameter if data_level == 0 # sometime there are empty IN arguments in all_arguments view for procedures without arguments (e.g. for DBMS_OUTPUT.DISABLE) @arguments[overload][argument_name.downcase.to_sym] = if argument_name # or lower level part of composite type else case [data_level - 1][:data_type] when "PL/SQL RECORD" [data_level - 1][:fields][argument_name.downcase.to_sym] = when "PL/SQL TABLE", "TABLE", "VARRAY", "REF CURSOR" [data_level - 1][:element] = end end end end # if procedure is without arguments then create default empty argument list for default overload @arguments[0] = {} if @arguments.keys.empty? construct_argument_list_for_overloads end |
#get_argument_metadata_from_18c ⇒ Object
get procedure argument metadata from data dictionary
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
# File 'lib/plsql/procedure.rb', line 209 def #:nodoc: @arguments = {} @argument_list = {} @out_list = {} @return = {} @overloaded = false # store tmp tables for each overload for table parameters with types defined inside packages @tmp_table_names = {} # store if tmp tables are created for specific overload @tmp_tables_created = {} @schema.select_all( "SELECT subprogram_id, object_name, TO_NUMBER(overload), argument_name, position, data_type, in_out, data_length, data_precision, data_scale, char_used, char_length, type_owner, nvl(type_subname, type_name), decode(type_object_type, 'PACKAGE', type_name, null), type_object_type, defaulted FROM all_arguments WHERE object_id = :object_id AND owner = :owner AND object_name = :procedure_name ORDER BY overload, sequence", @object_id, @schema_name, @procedure ) do |r| subprogram_id, object_name, overload, argument_name, position, data_type, in_out, data_length, data_precision, data_scale, char_used, char_length, type_owner, type_name, type_package, type_object_type, defaulted = r @overloaded ||= !overload.nil? # if not overloaded then store arguments at key 0 overload ||= 0 @arguments[overload] ||= {} @return[overload] ||= nil @tmp_table_names[overload] ||= [] sql_type_name = build_sql_type_name(type_owner, type_package, type_name) tmp_table_name = nil # type defined inside package if type_package if collection_type?(data_type) tmp_table_name = "#{Connection::RUBY_TEMP_TABLE_PREFIX}#{@schema.connection.session_id}_#{@object_id}_#{subprogram_id}_#{position}" end end = { position: position && position.to_i, data_type: data_type, in_out: in_out, data_length: data_length && data_length.to_i, data_precision: data_precision && data_precision.to_i, data_scale: data_scale && data_scale.to_i, char_used: char_used, char_length: char_length && char_length.to_i, type_owner: type_owner, type_name: type_name, # TODO: should be renamed to type_package, when support for legacy database versions is dropped # due to the explicit change declaration of types in oracle plsql_type-catalogs (type_package + type_name), # the assignment of type + subtype was switched here for 18c and beyond type_subname: type_package, sql_type_name: sql_type_name, defaulted: defaulted, type_object_type: type_object_type } if tmp_table_name @tmp_table_names[overload] << [([:tmp_table_name] = tmp_table_name), ] end if composite_type?(data_type) case data_type when "PL/SQL RECORD", "REF CURSOR" [:fields] = get_field_definitions() when "PL/SQL TABLE", "TABLE", "VARRAY" [:element] = get_element_definition() end end # if function has return value if argument_name.nil? && in_out == "OUT" @return[overload] = else # sometime there are empty IN arguments in all_arguments view for procedures without arguments (e.g. for DBMS_OUTPUT.DISABLE) @arguments[overload][argument_name.downcase.to_sym] = if argument_name end end # if procedure is without arguments then create default empty argument list for default overload @arguments[0] = {} if @arguments.keys.empty? construct_argument_list_for_overloads end |
#get_composite_type(type_owner, type_name, type_package) ⇒ Object
501 502 503 504 505 506 507 508 509 510 |
# File 'lib/plsql/procedure.rb', line 501 def get_composite_type(type_owner, type_name, type_package) r = @schema.select_first("SELECT typecode FROM all_plsql_types WHERE owner = :owner AND type_name = :type_name AND package_name = :type_package UNION ALL SELECT typecode FROM all_types WHERE owner = :owner AND type_name = :type_name", type_owner, type_name, type_package, type_owner, type_name) typecode = r[0] raise ArgumentError, "#{type_name} type #{build_sql_type_name(type_owner, type_package, type_name)} definition inside package is not supported as part of other type definition," << " use CREATE TYPE outside package" if typecode == "COLLECTION" typecode end |
#get_element_definition(argument_metadata) ⇒ Object
:nodoc:
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 |
# File 'lib/plsql/procedure.rb', line 411 def get_element_definition() #:nodoc: = {} if collection_type?([:data_type]) case [:type_object_type] when "PACKAGE" r = @schema.select_first( "SELECT elem_type_owner, elem_type_name, elem_type_package, length, precision, scale, char_used, index_by FROM ALL_PLSQL_COLL_TYPES t WHERE t.OWNER = :owner AND t.TYPE_NAME = :type_name AND t.PACKAGE_NAME = :package_name", @schema_name, [:type_name], [:type_subname]) elem_type_owner, elem_type_name, elem_type_package, elem_length, elem_precision, elem_scale, elem_char_used, index_by = r if index_by == "VARCHAR2" raise ArgumentError, "Index-by Varchar-Table (associative array) #{[:type_name]} is not supported" end = { position: 1, data_type: if elem_type_owner == nil elem_type_name else elem_type_package != nil ? "PL/SQL RECORD" : "OBJECT" end, in_out: [:in_out], data_length: elem_length && elem_length.to_i, data_precision: elem_precision && elem_precision.to_i, data_scale: elem_scale && elem_scale.to_i, char_used: elem_char_used, char_length: elem_char_used && elem_length && elem_length.to_i, type_owner: elem_type_owner, type_name: elem_type_name, type_subname: elem_type_package, sql_type_name: elem_type_owner && build_sql_type_name(elem_type_owner, elem_type_package, elem_type_name), type_object_type: elem_type_package != nil ? "PACKAGE" : nil, defaulted: [:defaulted] } if elem_type_package != nil [:fields] = get_field_definitions() end when "TYPE" r = @schema.select_first( "SELECT elem_type_owner, elem_type_name, length, precision, scale, char_used FROM ALL_COLL_TYPES t WHERE t.owner = :owner AND t.TYPE_NAME = :type_name", @schema_name, [:type_name] ) elem_type_owner, elem_type_name, elem_length, elem_precision, elem_scale, elem_char_used = r = { position: 1, data_type: elem_type_owner == nil ? elem_type_name : "OBJECT", in_out: [:in_out], data_length: elem_length && elem_length.to_i, data_precision: elem_precision && elem_precision.to_i, data_scale: elem_scale && elem_scale.to_i, char_used: elem_char_used, char_length: elem_char_used && elem_length && elem_length.to_i, type_owner: elem_type_owner, type_name: elem_type_name, type_subname: nil, sql_type_name: elem_type_owner && build_sql_type_name(elem_type_owner, nil, elem_type_name), defaulted: [:defaulted] } end else = { position: 1, data_type: "PL/SQL RECORD", in_out: [:in_out], data_length: nil, data_precision: nil, data_scale: nil, char_used: "B", char_length: 0, type_owner: [:type_owner], type_name: [:type_name], type_subname: [:type_subname], sql_type_name: build_sql_type_name([:type_owner], [:type_subname], [:type_name]), defaulted: [:defaulted] } if [:type_subname] != nil [:fields] = get_field_definitions() end end end |
#get_field_definitions(argument_metadata) ⇒ Object
:nodoc:
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 |
# File 'lib/plsql/procedure.rb', line 348 def get_field_definitions() #:nodoc: fields = {} case [:type_object_type] when "PACKAGE" @schema.select_all( "SELECT attr_no, attr_name, attr_type_owner, attr_type_name, attr_type_package, length, precision, scale, char_used FROM ALL_PLSQL_TYPES t, ALL_PLSQL_TYPE_ATTRS ta WHERE t.OWNER = :owner AND t.type_name = :type_name AND t.package_name = :package_name AND ta.OWNER = t.owner AND ta.TYPE_NAME = t.TYPE_NAME AND ta.PACKAGE_NAME = t.PACKAGE_NAME ORDER BY attr_no", @schema_name, [:type_name], [:type_subname]) do |r| attr_no, attr_name, attr_type_owner, attr_type_name, attr_type_package, attr_length, attr_precision, attr_scale, attr_char_used = r fields[attr_name.downcase.to_sym] = { position: attr_no.to_i, data_type: attr_type_owner == nil ? attr_type_name : get_composite_type(attr_type_owner, attr_type_name, attr_type_package), in_out: [:in_out], data_length: attr_length && attr_length.to_i, data_precision: attr_precision && attr_precision.to_i, data_scale: attr_scale && attr_scale.to_i, char_used: attr_char_used == nil ? "0" : attr_char_used, char_length: attr_char_used && attr_length && attr_length.to_i, type_owner: attr_type_owner, type_name: attr_type_owner && attr_type_name, type_subname: attr_type_package, sql_type_name: attr_type_owner && build_sql_type_name(attr_type_owner, attr_type_package, attr_type_name), defaulted: [:defaulted] } if fields[attr_name.downcase.to_sym][:data_type] == "TABLE" && fields[attr_name.downcase.to_sym][:type_subname] != nil fields[attr_name.downcase.to_sym][:fields] = get_field_definitions(fields[attr_name.downcase.to_sym]) end end when "TABLE", "VIEW" @schema.select_all( "SELECT column_id, column_name, data_type, data_length, data_precision, data_scale, char_length, char_used FROM ALL_TAB_COLS WHERE OWNER = :owner AND TABLE_NAME = :type_name ORDER BY column_id", @schema_name, [:type_name]) do |r| col_no, col_name, col_type_name, col_length, col_precision, col_scale, col_char_length, col_char_used = r fields[col_name.downcase.to_sym] = { position: col_no.to_i, data_type: col_type_name, in_out: [:in_out], data_length: col_length && col_length.to_i, data_precision: col_precision && col_precision.to_i, data_scale: col_scale && col_scale.to_i, char_used: col_char_used == nil ? "0" : col_char_used, char_length: col_char_length && col_char_length.to_i, type_owner: nil, type_name: nil, type_subname: nil, sql_type_name: nil, defaulted: [:defaulted] } end end fields end |
#overloaded? ⇒ Boolean
:nodoc:
522 523 524 |
# File 'lib/plsql/procedure.rb', line 522 def overloaded? #:nodoc: @overloaded end |