/
metadata.rb
2148 lines (1869 loc) · 65.9 KB
/
metadata.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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
207
208
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
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
410
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# oci8.rb -- implements OCI8::Metadata.
#
# Copyright (C) 2006-2013 Kubo Takehiro <kubo@jiubao.org>
#
# See {'Describing Schema Metadata' in Oracle Call Interface Programmer's Guide}
# [http://download-west.oracle.com/docs/cd/B19306_01/appdev.102/b14250/oci06des.htm]
#
class OCI8
# OCI8 has methods to obtain information about database objects such
# as tables, views, procedures, functions ans so on. The obtained
# data are called metadata and retrived as an instance of
# OCI8::Metadata::Base's subclass.
#
# List of methods which return OCI8::Metadata::Base.
# * {OCI8#describe_any OCI8#describe_any(object_name)}
# * {OCI8#describe_table OCI8#describe_table(table_name, table_only = false)}
# * {OCI8#describe_view OCI8#describe_view(view_name)}
# * {OCI8#describe_procedure OCI8#describe_procedure(procedure_name)}
# * {OCI8#describe_function OCI8#describe_function(function_name)}
# * {OCI8#describe_package OCI8#describe_package(package_name)}
# * {OCI8#describe_type OCI8#describe_type(type_name)}
# * {OCI8#describe_synonym OCI8#describe_synonym(synonym_name, check_public_also = true)}
# * {OCI8#describe_sequence OCI8#describe_sequence(sequence_name)}
# * {OCI8#describe_schema OCI8#describe_schema(schema_name)}
# * {OCI8#describe_database OCI8#describe_database(database_name)}
# * {OCI8::Metadata::Type#map_method OCI8::Metadata::Type#map_method}
# * {OCI8::Metadata::Type#order_method OCI8::Metadata::Type#order_method}
# * {OCI8::Metadata::Type#collection_element OCI8::Metadata::Type#collection_element}
#
# List of methods which return an array of OCI8::Metadata::Base.
# * {OCI8::Cursor#column_metadata OCI8::Cursor#column_metadata}
# * {OCI8::Metadata::Database#schemas OCI8::Metadata::Database#schemas}
# * {OCI8::Metadata::Schema#all_objects OCI8::Metadata::Schema#all_objects}
# * {OCI8::Metadata::Schema#objects OCI8::Metadata::Schema#objects}
# * {OCI8::Metadata::Table#columns OCI8::Metadata::Table#columns}
# * {OCI8::Metadata::Package#subprograms OCI8::Metadata::Package#subprograms}
# * {OCI8::Metadata::Subprogram#arguments OCI8::Metadata::Procedure#arguments}
# * {OCI8::Metadata::Subprogram#arguments OCI8::Metadata::Function#arguments}
# * {OCI8::Metadata::Type#type_attrs OCI8::Metadata::Type#type_attrs}
# * {OCI8::Metadata::Type#type_methods OCI8::Metadata::Type#type_methods}
# * {OCI8::Metadata::TypeMethod#arguments OCI8::Metadata::TypeMethod#arguments}
#
# Example:
# conn = OCI8.new('username/passord')
# table = conn.describe_table('scott.emp')
# table.columns.each do |col|
# puts "#{col.name} #{col.data_type_string}"
# end
module Metadata
# Abstract super class of Metadata classes.
class Base
# Table 6-1 Attributes Belonging to All Parameters
# Returns the number of parameters.
def num_params
attr_get_ub2(OCI_ATTR_NUM_PARAMS)
end
private :num_params
# Returns the object ID which corresponds to the data dictionary
# view column ALL_OBJECTS.OBJECT_ID.
#
# @return [Integer or nil]
def obj_id
attr_get_ub4(OCI_ATTR_OBJ_ID, false)
end
# Returns the object name which corresponds to the data dictionary view column ALL_OBJECTS.OBJECT_NAME.
#
# @return [String or nil] object name
def obj_name
attr_get_string(OCI_ATTR_OBJ_NAME, false)
end
# Returns the schema name which corresponds to the data dictionary view column ALL_OBJECTS.OWNER.
#
# @return [String or nil] schema name
def obj_schema
attr_get_string(OCI_ATTR_OBJ_SCHEMA, false)
end
# The timestamp of the object
#-
# As far as I checked, it is current timestamp, not the object's timestamp. Why?
#+
#def timestamp
# attr_get_oradate(OCI_ATTR_TIMESTAMP)
#end
# Returns the database link name if the object is at the remote host.
# Note that this is available only when the object is returned by OCI8#describe_* methods.
#
# @return [String or nil] database link name
# @since 2.1.7
def obj_link
@obj_link
end
# @private
def inspect
"#<#{self.class.name}:(#{obj_id}) #{obj_schema}.#{obj_name}>"
end
private
def __boolean(idx)
attr_get_ub1(idx) == 0 ? false : true
end
alias __word attr_get_sb4
def __anydata(idx); raise NotImplementedError; end
# Mapping from SQLT values to names
# @private
DATA_TYPE_MAP = {}
# SQLT_CHR
DATA_TYPE_MAP[1] = [:varchar2,
Proc.new do |p|
if p.charset_form == :nchar
"NVARCHAR2(#{p.char_size})"
elsif p.respond_to?(:char_used?) && p.char_used?
"VARCHAR2(#{p.char_size} CHAR)"
else
"VARCHAR2(#{p.data_size})"
end
end]
# SQLT_NUM
DATA_TYPE_MAP[2] = [:number,
Proc.new do |p|
begin
case p.scale
when -127
case p.precision
when 0
"NUMBER"
when 126
"FLOAT"
else
"FLOAT(#{p.precision})"
end
when 0
case p.precision
when 0
"NUMBER"
else
"NUMBER(#{p.precision})"
end
else
"NUMBER(#{p.precision},#{p.scale})"
end
rescue OCIError
"NUMBER"
end
end]
# SQLT_LNG
DATA_TYPE_MAP[8] = [:long, "LONG"]
# SQLT_DAT
DATA_TYPE_MAP[12] = [:date, "DATE"]
# SQLT_BIN
DATA_TYPE_MAP[23] = [:raw,
Proc.new do |p|
"RAW(#{p.data_size})"
end]
# SQLT_LBI
DATA_TYPE_MAP[24] = [:long_raw, "LONG RAW"]
# SQLT_AFC
DATA_TYPE_MAP[96] = [:char,
Proc.new do |p|
if p.charset_form == :nchar
"NCHAR(#{p.char_size})"
elsif p.char_used?
"CHAR(#{p.char_size} CHAR)"
else
"CHAR(#{p.data_size})"
end
end]
# SQLT_IBFLOAT
DATA_TYPE_MAP[100] = [:binary_float, "BINARY_FLOAT"]
# SQLT_IBDOUBLE
DATA_TYPE_MAP[101] = [:binary_double, "BINARY_DOUBLE"]
# SQLT_RDD
DATA_TYPE_MAP[104] = [:rowid, "ROWID"]
# SQLT_NTY
DATA_TYPE_MAP[108] = [:named_type,
Proc.new do |p|
"#{p.schema_name}.#{p.type_name}"
end]
# SQLT_REF
DATA_TYPE_MAP[110] = [:ref,
Proc.new do |p|
"REF #{p.schema_name}.#{p.type_name}"
end]
# SQLT_CLOB
DATA_TYPE_MAP[112] = [:clob,
Proc.new do |p|
if p.charset_form == :nchar
"NCLOB"
else
"CLOB"
end
end]
# SQLT_BLOB
DATA_TYPE_MAP[113] = [:blob, "BLOB"]
# SQLT_BFILE
DATA_TYPE_MAP[114] = [:bfile, "BFILE"]
# SQLT_RSET
DATA_TYPE_MAP[116] = [:cursor, "CURSOR"]
# SQLT_TIMESTAMP
DATA_TYPE_MAP[187] = [:timestamp,
Proc.new do |p|
fsprecision = p.fsprecision
if fsprecision == 6
"TIMESTAMP"
else
"TIMESTAMP(#{fsprecision})"
end
end]
# SQLT_TIMESTAMP_TZ
DATA_TYPE_MAP[188] = [:timestamp_tz,
Proc.new do |p|
fsprecision = p.fsprecision
if fsprecision == 6
"TIMESTAMP WITH TIME ZONE"
else
"TIMESTAMP(#{fsprecision}) WITH TIME ZONE"
end
end]
# SQLT_INTERVAL_YM
DATA_TYPE_MAP[189] = [:interval_ym,
Proc.new do |p|
lfprecision = p.lfprecision
if lfprecision == 2
"INTERVAL YEAR TO MONTH"
else
"INTERVAL YEAR(#{lfprecision}) TO MONTH"
end
end]
# SQLT_INTERVAL_DS
DATA_TYPE_MAP[190] = [:interval_ds,
Proc.new do |p|
lfprecision = p.lfprecision
fsprecision = p.fsprecision
if lfprecision == 2 && fsprecision == 6
"INTERVAL DAY TO SECOND"
else
"INTERVAL DAY(#{lfprecision}) TO SECOND(#{fsprecision})"
end
end]
# SQLT_TIMESTAMP_LTZ
DATA_TYPE_MAP[232] = [:timestamp_ltz,
Proc.new do |p|
fsprecision = p.fsprecision
if fsprecision == 6
"TIMESTAMP WITH LOCAL TIME ZONE"
else
"TIMESTAMP(#{fsprecision}) WITH LOCAL TIME ZONE"
end
end]
DATA_TYPE_MAP[245] = [:record,
Proc.new do |p|
"#{p.schema_name}.#{p.type_name}"
end]
DATA_TYPE_MAP[252] = [:boolean, "BOOLEAN"]
DATA_TYPE_MAP[266] = [:pls_integer, "PLS_INTEGER"]
def __data_type # :nodoc:
return @data_type if defined? @data_type
entry = DATA_TYPE_MAP[attr_get_ub2(OCI_ATTR_DATA_TYPE)]
type = entry.nil? ? attr_get_ub2(OCI_ATTR_DATA_TYPE) : entry[0]
type = type.call(self) if type.is_a? Proc
@data_type = type
end
def __duration # :nodoc:
case attr_get_ub2(OCI_ATTR_DURATION)
when OCI_DURATION_SESSION
:session
when OCI_DURATION_TRANS
:transaction
when OCI_DURATION_NULL
nil
end
end
def __charset_form # :nodoc:
case attr_get_ub1(OCI_ATTR_CHARSET_FORM)
when 1; :implicit # for CHAR, VARCHAR2, CLOB w/o a specified set
when 2; :nchar # for NCHAR, NCHAR VARYING, NCLOB
when 3; :explicit # for CHAR, etc, with "CHARACTER SET ..." syntax
when 4; :flexible # for PL/SQL "flexible" parameters
when 5; :lit_null # for typecheck of NULL and empty_clob() lits
end
end
def __data_type_string # :nodoc:
entry = DATA_TYPE_MAP[attr_get_ub2(OCI_ATTR_DATA_TYPE)]
type = entry.nil? ? "unknown(#{attr_get_ub2(OCI_ATTR_DATA_TYPE)})" : entry[1]
type = type.call(self) if type.is_a? Proc
if respond_to?(:nullable?) && !nullable?
type + " NOT NULL"
else
type
end
end
def __typecode(idx) # :nodoc:
case attr_get_ub2(idx)
when 110; :ref # OCI_TYPECODE_REF
when 12; :date # OCI_TYPECODE_DATE
when 27; :signed8 # OCI_TYPECODE_SIGNED8
when 28; :signed16 # OCI_TYPECODE_SIGNED16
when 29; :signed32 # OCI_TYPECODE_SIGNED32
when 21; :real # OCI_TYPECODE_REAL
when 22; :double # OCI_TYPECODE_DOUBLE
when 100; :binary_float # OCI_TYPECODE_BFLOAT
when 101; :binary_double # OCI_TYPECODE_BDOUBLE
when 4; :float # OCI_TYPECODE_FLOAT
when 2; :number # OCI_TYPECODE_NUMBER
when 7; :decimal # OCI_TYPECODE_DECIMAL
when 23; :unsigned8 # OCI_TYPECODE_UNSIGNED8
when 25; :unsigned16 # OCI_TYPECODE_UNSIGNED16
when 26; :unsigned32 # OCI_TYPECODE_UNSIGNED32
when 245; :octet # OCI_TYPECODE_OCTET
when 246; :smallint # OCI_TYPECODE_SMALLINT
when 3; :integer # OCI_TYPECODE_INTEGER
when 95; :raw # OCI_TYPECODE_RAW
when 32; :ptr # OCI_TYPECODE_PTR
when 9; :varchar2 # OCI_TYPECODE_VARCHAR2
when 96; :char # OCI_TYPECODE_CHAR
when 1; :varchar # OCI_TYPECODE_VARCHAR
when 105; :mlslabel # OCI_TYPECODE_MLSLABEL
when 247; :varray # OCI_TYPECODE_VARRAY
when 248; :table # OCI_TYPECODE_TABLE
when 108; :named_type # OCI_TYPECODE_OBJECT
when 58; :opaque # OCI_TYPECODE_OPAQUE
when 122; :named_collection # OCI_TYPECODE_NAMEDCOLLECTION
when 113; :blob # OCI_TYPECODE_BLOB
when 114; :bfile # OCI_TYPECODE_BFILE
when 112; :clob # OCI_TYPECODE_CLOB
when 115; :cfile # OCI_TYPECODE_CFILE
when 185; :time # OCI_TYPECODE_TIME
when 186; :time_tz # OCI_TYPECODE_TIME_TZ
when 187; :timestamp # OCI_TYPECODE_TIMESTAMP
when 188; :timestamp_tz # OCI_TYPECODE_TIMESTAMP_TZ
when 232; :timestamp_ltz # OCI_TYPECODE_TIMESTAMP_LTZ
when 189; :interval_ym # OCI_TYPECODE_INTERVAL_YM
when 190; :interval_ds # OCI_TYPECODE_INTERVAL_DS
when 104; :urowid # OCI_TYPECODE_UROWID
#when 228; :otmfirst # OCI_TYPECODE_OTMFIRST
#when 320; :otmlast # OCI_TYPECODE_OTMLAST
#when 228; :sysfirst # OCI_TYPECODE_SYSFIRST
#when 235; :syslast # OCI_TYPECODE_SYSLAST
when 266; :pls_integer # OCI_TYPECODE_PLS_INTEGER
when 250; :record # OCI_TYPECODE_RECORD
when 251; :itable # OCI_TYPECODE_ITABLE
when 252; :boolean # OCI_TYPECODE_BOOLEAN
end
end
end
# Information about database objects which cannot be classified
# into other metadata classes.
#
# An instance of this class is returned by:
# * {OCI8::Metadata::Schema#all_objects OCI8::Metadata::Schema#all_objects}
class Unknown < Base
register_ptype OCI_PTYPE_UNK
end
# Information about tables
#
# An instance of this class is returned by:
# * {OCI8#describe_any OCI8#describe_any(name)}
# * {OCI8#describe_table OCI8#describe_table(name)}
# * {OCI8::Metadata::Schema#all_objects OCI8::Metadata::Schema#all_objects}
# * {OCI8::Metadata::Schema#objects OCI8::Metadata::Schema#objects}
class Table < Base
register_ptype OCI_PTYPE_TABLE
## Table 6-2 Attributes Belonging to Tables or Views
# Returns number of columns
#
# @return [Integer]
def num_cols
attr_get_ub2(OCI_ATTR_NUM_COLS)
end
# column list
def list_columns # :nodoc:
__param(OCI_ATTR_LIST_COLUMNS)
end
private :list_columns
# Retruns an instance of OCI8::Metadata::Type if the table is an
# {object table}[http://download.oracle.com/docs/cd/B28359_01/appdev.111/b28371/adobjint.htm#sthref61].
# Otherwise, +nil+.
#
# @return [OCI8::Metadata::Type or nil]
def type_metadata
__type_metadata(OCI8::Metadata::Type) if is_typed?
end
# Returns +true+ if the table is a
# {temporary table}[http://download.oracle.com/docs/cd/B28359_01/server.111/b28318/schema.htm#i16096].
# Otherwise, +false+.
def is_temporary?
attr_get_ub1(OCI_ATTR_IS_TEMPORARY) != 0
end
# Returns +true+ if the table is a object table. Otherwise, +false+.
def is_typed?
attr_get_ub1(OCI_ATTR_IS_TYPED) != 0
end
# Retruns +:transaction+ if the table is a
# {transaction-specific temporary table}[http://download.oracle.com/docs/cd/B28359_01/server.111/b28286/statements_7002.htm#i2189569].
# +:session+ if it is a
# {session-specific temporary table}[http://download.oracle.com/docs/cd/B28359_01/server.111/b28286/statements_7002.htm#i2189569].
# Otherwise, +nil+.
#
# @return [:transaction, :session or nil]
def duration
__duration
end
## Table 6-3 Attributes Specific to Tables
# Returns a Data Block Address(DBA) of the segment header.
#
# The dba is converted to the file number and the block number
# by DBMS_UTILITY.DATA_BLOCK_ADDRESS_FILE and
# DBMS_UTILITY.DATA_BLOCK_ADDRESS_BLOCK respectively.
#
# @return [Integer]
def dba
attr_get_ub4(OCI_ATTR_RDBA)
end
# Returns a tablespace number the table resides in.
#
# @return [Integer]
def tablespace
__word(OCI_ATTR_TABLESPACE)
end
# Returns +true+ if the table is part of a
# cluster[http://download.oracle.com/docs/cd/B28359_01/server.111/b28318/schema.htm#CNCPT608].
# Otherwise, +false+.
def clustered?
attr_get_ub1(OCI_ATTR_CLUSTERED) != 0
end
# Returns +true+ if the table is a
# {partitioned table}[http://download.oracle.com/docs/cd/B28359_01/server.111/b28318/partconc.htm#i449863].
# Otherwise, +false+.
def partitioned?
attr_get_ub1(OCI_ATTR_PARTITIONED) != 0
end
# Returns +true+ if the table is an
# {index-organized table}[http://download.oracle.com/docs/cd/B28359_01/server.111/b28318/schema.htm#i23877]
# Otherwise, +false+.
def index_only?
attr_get_ub1(OCI_ATTR_INDEX_ONLY) != 0
end
# Returns column information of the table.
#
# @return [array of OCI8::Metadata::Column]
def columns
@columns ||= list_columns.to_a
end
end
# Information about views
#
# An instance of this class is returned by:
# * {OCI8#describe_any OCI8#describe_any(name)}
# * {OCI8#describe_table OCI8#describe_table(name, false)}
# * {OCI8#describe_view OCI8#describe_view(name)}
# * {OCI8::Metadata::Schema#all_objects OCI8::Metadata::Schema#all_objects}
# * {OCI8::Metadata::Schema#objects OCI8::Metadata::Schema#objects}
class View < Base
register_ptype OCI_PTYPE_VIEW
## Table 6-2 Attributes Belonging to Tables or Views
# Returns number of columns
#
# @return [Integer]
def num_cols
attr_get_ub2(OCI_ATTR_NUM_COLS)
end
# column list
def list_columns # :nodoc:
__param(OCI_ATTR_LIST_COLUMNS)
end
private :list_columns
# This does't work for view.
#def type_metadata
# __type_metadata(OCI8::Metadata::Type)
#end
# This does't work for view.
#def is_temporary?
# attr_get_ub1(OCI_ATTR_IS_TEMPORARY) != 0
#end
# This does't work for view.
#def is_typed?
# attr_get_ub1(OCI_ATTR_IS_TYPED) != 0
#end
# This does't work for view.
#def duration
# __duration
#end
# Returns column information of the view.
#
# @return [array of OCI8::Metadata::Column]
def columns
@columns ||= list_columns.to_a
end
end
# Information about PL/SQL subprograms
#
# A PL/SQL subprogram is a named PL/SQL block that can be invoked
# with a set of parameters. A subprogram can be either a procedure
# or a function.
#
# This is the abstract base class of {OCI8::Metadata::Procedure OCI8::Metadata::Procedure} and
# {OCI8::Metadata::Function OCI8::Metadata::Function}.
class Subprogram < Base
## Table 6-4 Attribute Belonging to Procedures or Functions
# Argument list
def list_arguments # :nodoc:
__param(OCI_ATTR_LIST_ARGUMENTS)
end
private :list_arguments
def obj_id # :nodoc:
super if is_standalone?
end
def obj_name # :nodoc:
is_standalone? ? super : attr_get_string(OCI_ATTR_NAME)
end
def obj_schema # :nodoc:
super if is_standalone?
end
# Returns +true+ if the subprogram has
# {invoker's rights}[http://download.oracle.com/docs/cd/B28359_01/appdev.111/b28370/subprograms.htm#i18574].
# Otherwise, +false+.
def is_invoker_rights?
attr_get_ub1(OCI_ATTR_IS_INVOKER_RIGHTS) != 0
end
## Table 6-5 Attributes Specific to Package Subprograms
# name of the subprogram
#
#def name
# attr_get_string(OCI_ATTR_NAME)
#end
alias name obj_name # :nodoc: for backward compatibility
# Returns +nil+ for a standalone stored subprogram,
# positive integer for an
# {overloaded packaged subprogram}[http://download.oracle.com/docs/cd/B28359_01/appdev.111/b28370/subprograms.htm#i12352],
# otherwise zero.
#
# @return [Integer or nil]
def overload_id
attr_get_ub2(OCI_ATTR_OVERLOAD_ID) unless is_standalone?
end
# Returns argument information of the subprogram.
# If it is a function, the first array element is information of its return type.
#
# @return [array of OCI8::Metadata::Argument]
def arguments
@arguments ||= list_arguments.to_a
end
# Returns +true+ if the subprogram is standalone, +false+
# if packaged.
def is_standalone?
@is_standalone = true unless defined? @is_standalone
@is_standalone
end
# @private
def inspect
"#<#{self.class.name}: #{name}>"
end
end
# Information about procedures
#
# An instance of this class is returned by:
# * OCI8#describe_any(name)
# * OCI8#describe_procedure(name)
# * OCI8::Metadata::Schema#all_objects
# * OCI8::Metadata::Schema#objects
# * OCI8::Metadata::Package#subprograms
#
# See OCI8::Metadata::Subprogram.
class Procedure < Subprogram
register_ptype OCI_PTYPE_PROC
end
# Information about functions
#
# An instance of this class is returned by:
# * OCI8#describe_any(name)
# * OCI8#describe_function(name)
# * OCI8::Metadata::Schema#all_objects
# * OCI8::Metadata::Schema#objects
# * OCI8::Metadata::Package#subprograms
#
# See OCI8::Metadata::Subprogram.
class Function < Subprogram
register_ptype OCI_PTYPE_FUNC
end
# Information about packages.
#
# An instance of this class is returned by:
# * OCI8#describe_any(name)
# * OCI8#describe_package(name)
# * OCI8::Metadata::Schema#all_objects
# * OCI8::Metadata::Schema#objects
class Package < Base
register_ptype OCI_PTYPE_PKG
## Table 6-6 Attributes Belonging to Packages
# subprogram list
def list_subprograms # :nodoc:
__param(OCI_ATTR_LIST_SUBPROGRAMS)
end
private :list_subprograms
# package type list
def list_types # :nodoc:
__param(137) # OCI_ATTR_LIST_PKG_TYPES
end
private :list_types
# Returns +true+ if the package subprograms have
# {invoker's rights}[http://download.oracle.com/docs/cd/B28359_01/appdev.111/b28370/subprograms.htm#i18574].
# Otherwise, +false+.
def is_invoker_rights?
attr_get_ub1(OCI_ATTR_IS_INVOKER_RIGHTS) != 0
end
# Returns an array of Function and Procedure defined within the Package.
#
# @return [array of OCI8::Metadata::Function and OCI8::Metadata::Procedure]
def subprograms
@subprograms ||= list_subprograms.to_a.each do |prog|
prog.instance_variable_set(:@is_standalone, false)
end
end
if OCI8.oracle_client_version < ORAVER_12_1
def types
raise "This version of the Oracle client does not support PL/SQL package type descriptions."
end
else
# Returns an array of types defined within the Package.
#
# @return [array of OCI8::Metadata::Type]
# @since 2.1.6
def types
@types ||= list_types.to_a.each do |type|
type.instance_variable_set(:@is_standalone, false)
end
end
end
end
# Information about types
#
# An instance of this class is returned by:
# * OCI8#describe_any(name)
# * OCI8#describe_type(name)
# * OCI8::Metadata::Schema#all_objects
# * OCI8::Metadata::Schema#objects
class Type < Base
register_ptype OCI_PTYPE_TYPE
## Table 6-7 Attributes Belonging to Types
# to type metadata if possible
def type_metadata
self
end
# Returns +:named_type+ if the type is an object type,
# +:named_collection+ if it is a nested table or a varray.
#
# @return [:named_type or :named_collection]
def typecode
__typecode(OCI_ATTR_TYPECODE)
end
# Returns +:table+ if the type is a nested table,
# +:varray+ if it is a varray. Otherwise, +nil+.
#
# @return [:table, :varray or nil]
def collection_typecode
__typecode(OCI_ATTR_COLLECTION_TYPECODE) if typecode == :named_collection
end
# Returns +true+ if the type is an
# {incomplete type}[http://download.oracle.com/docs/cd/B28359_01/appdev.111/b28371/adobjmng.htm#i1003083],
# which is used for {forward declaration}[http://en.wikipedia.org/wiki/Forward_declaration].
# Otherwise, +false+.
def is_incomplete_type?
attr_get_ub1(OCI_ATTR_IS_INCOMPLETE_TYPE) != 0
end
# Always returns +false+ because there is no way to create
# a type metadata object for a system type such as +NUMBER+,
# +CHAR+ and +VARCHAR+.
def is_system_type? # :nodoc:
attr_get_ub1(OCI_ATTR_IS_SYSTEM_TYPE) != 0
end
# Always returns +false+.
#--
# I don't know the definition of predefined type...
def is_predefined_type? # :nodoc:
attr_get_ub1(OCI_ATTR_IS_PREDEFINED_TYPE) != 0
end
# Always returns +false+ because there is no way to create
# a type metadata object for a transient type, which is a type
# dynamically created by C API.
def is_transient_type? # :nodoc:
attr_get_ub1(OCI_ATTR_IS_TRANSIENT_TYPE) != 0
end
# Always returns +false+.
#--
# I don't know the definition of system generated type.
# What is different with system type and predefined type.
def is_system_generated_type? # :nodoc:
attr_get_ub1(OCI_ATTR_IS_SYSTEM_GENERATED_TYPE) != 0
end
# Returns +true+ if the type is a nested table or
# has a nested table attribute.
# Otherwise, +false+.
def has_nested_table?
attr_get_ub1(OCI_ATTR_HAS_NESTED_TABLE) != 0
end
# Returns +true+ if the type has a CLOB, NCLOB or BLOB
# attribute.
# Otherwise, +false+.
def has_lob?
attr_get_ub1(OCI_ATTR_HAS_LOB) != 0
end
# Returns +true+ if the type has a BFILE attribute.
# Otherwise, +false+.
def has_file?
attr_get_ub1(OCI_ATTR_HAS_FILE) != 0
end
# Returns element information of the collection type if the type is a nested
# table or a varray.
# Otherwise, +nil+.
#
# @return [OCI8::Metadata::Collection or nil]
def collection_element
__param(OCI_ATTR_COLLECTION_ELEMENT) if typecode == :named_collection
end
# Returns number of type attributes.
#
# @return [Integer]
def num_type_attrs
attr_get_ub2(OCI_ATTR_NUM_TYPE_ATTRS)
end
# list of type attributes
def list_type_attrs # :nodoc:
__param(OCI_ATTR_LIST_TYPE_ATTRS)
end
private :list_type_attrs
# Returns number of type methods.
#
# @return [Integer]
def num_type_methods
attr_get_ub2(OCI_ATTR_NUM_TYPE_METHODS)
end
# list of type methods
def list_type_methods # :nodoc:
__param(OCI_ATTR_LIST_TYPE_METHODS)
end
private :list_type_methods
# Returns information of the
# {map method}[http://download.oracle.com/docs/cd/B28359_01/appdev.111/b28371/adobjbas.htm#sthref180]
# if it is defined in the type. Otherwise, +nil+.
#
# @return [OCI8::Metadata::TypeMethod or nil]
def map_method
__param(OCI_ATTR_MAP_METHOD)
end
# Returns information of the
# {order method}[http://download.oracle.com/docs/cd/B28359_01/appdev.111/b28371/adobjbas.htm#sthref185]
# if it is defined in the type. Otherwise, +nil+.
#
# @return [OCI8::Metadata::TypeMethod or nil]
def order_method
__param(OCI_ATTR_ORDER_METHOD)
end
# Returns +true+ if the type has
# {invoker's rights}[http://download.oracle.com/docs/cd/B28359_01/appdev.111/appdev.111/b28371/adobjdes.htm#ADOBJ00810].
# Otherwise, +false+.
def is_invoker_rights?
attr_get_ub1(OCI_ATTR_IS_INVOKER_RIGHTS) != 0
end
# Returns the type name.
#
# @return [String]
def name
attr_get_string(OCI_ATTR_NAME)
end
# Returns the schema name where the type has been created.
#
# @return [String]
def schema_name
attr_get_string(OCI_ATTR_SCHEMA_NAME)
end
# Returns +true+ if the type is a
# {final type}[http://download.oracle.com/docs/cd/B28359_01/appdev.111/b28371/adobjbas.htm#CIHFBHFC];
# in other words, subtypes cannot be derived from the type.
# Otherwise, +false+.
def is_final_type?
attr_get_ub1(OCI_ATTR_IS_FINAL_TYPE) != 0
end
# Returns +true+ if the type is not declared without
# {<tt>NOT INSTANTIABLE</tt>}[http://download.oracle.com/docs/cd/B28359_01/appdev.111/b28371/adobjbas.htm#i456586].
# Otherwise, +false+.
def is_instantiable_type?
attr_get_ub1(OCI_ATTR_IS_INSTANTIABLE_TYPE) != 0
end
# Returns +true+ if the type is a
# {subtype}[http://download.oracle.com/docs/cd/B28359_01/appdev.111/b28371/adobjbas.htm#BCFJJADG].
# Otherwise, +false+.
def is_subtype?
attr_get_ub1(OCI_ATTR_IS_SUBTYPE) != 0
end
# Returns the supertype's schema name if the type is a subtype.
# Otherwise, +nil+.
#
# @return [String or nil]
def supertype_schema_name
attr_get_string(OCI_ATTR_SUPERTYPE_SCHEMA_NAME) if is_subtype?
end
# Returns the supertype's name if the type is a subtype.
# Otherwise, +nil+.
#
# @return [String or nil]
def supertype_name
attr_get_string(OCI_ATTR_SUPERTYPE_NAME) if is_subtype?
end
# Returns the package name if the type is a package type.
# Otherwise, +nil+.
#
# @return [String or nil]
# @since 2.1.6
def package_name
attr_get_string(12, false) # OCI_ATTR_PACKAGE_NAME
end
# Returns attribute information of the type.
#
# @return [array of OCI8::Metadata::TypeAttr]
def type_attrs
@type_attrs ||= list_type_attrs.to_a
end
# Returns method information of the type.
#
# @return [array of OCI8::Metadata::TypeMethod]
def type_methods
@type_methods ||= list_type_methods.to_a
end
def inspect # :nodoc:
"#<#{self.class.name}:(#{obj_id}) #{schema_name}.#{name}>"
end
end
# Metadata for a type attribute.
#
# This is returned by:
# * OCI8::Metadata::Type#type_attrs
class TypeAttr < Base
register_ptype OCI_PTYPE_TYPE_ATTR
## Table 6-8 Attributes Belonging to Type Attributes
# The maximum size of the type attribute. This length is
# returned in bytes and not characters for strings and raws. It
# returns 22 for NUMBERs.
def data_size
attr_get_ub2(OCI_ATTR_DATA_SIZE)
end
# typecode
def typecode
__typecode(OCI_ATTR_TYPECODE)
end
# the datatype of the type
def data_type
__data_type
end
# the type attribute name
def name
attr_get_string(OCI_ATTR_NAME)
end
# The precision of numeric type attributes. If the precision is
# nonzero and scale is -127, then it is a FLOAT, else it is a
# NUMBER(precision, scale). For the case when precision is 0,
# NUMBER(precision, scale) can be represented simply as NUMBER.
def precision
__is_implicit? ? attr_get_sb2(OCI_ATTR_PRECISION) : attr_get_ub1(OCI_ATTR_PRECISION)
end
# The scale of numeric type attributes. If the precision is
# nonzero and scale is -127, then it is a FLOAT, else it is a
# NUMBER(precision, scale). For the case when precision is 0,
# NUMBER(precision, scale) can be represented simply as NUMBER.
def scale
attr_get_sb1(OCI_ATTR_SCALE)
end
# A string which is the type name. The returned value will
# contain the type name if the datatype is <tt>:named_type</tt>
# or <tt>:ref</tt>. If the datatype is <tt>:named_type</tt>, the
# name of the named datatype's type is returned. If the datatype
# is <tt>:ref</tt>, the type name of the named datatype pointed
# to by the REF is returned.
def type_name
attr_get_string(OCI_ATTR_TYPE_NAME)
end
# schema name where the type has been created.
def schema_name
attr_get_string(OCI_ATTR_SCHEMA_NAME)
end
# to type metadata if possible
def type_metadata
__type_metadata(OCI8::Metadata::Type)
end
# character set id if the type attribute is of a string/character type.
def charset_id
attr_get_ub2(OCI_ATTR_CHARSET_ID)
end
# character set form, if the type attribute is of a string/character type.
def charset_form
__charset_form
end
# The fractional seconds precision of a datetime or interval.
def fsprecision