備考
コレクションとは、すべて同じ型の要素が順序付けされたグループです。たとえば、クラス内の生徒またはクラス内の各生徒に対する評価などが考えられます。各要素には、索引と呼ばれる固有の添字があり、コレクション内の位置を判断します。
ネストした表のコレクション型は、データベース表の列に格納された表として参照できます。ネストした表の行は、1
で始まる連続した添字が割り当てられて取り出されます。配列にアクセスする要領で、個々の行にアクセスして取り出します。
VARRAY
のコレクション型は、データベース表の列に格納された配列として参照できます。VARRAY
データ型の要素を参照するには、標準的な添字構文を使用できます。たとえば、Grade(3)
は、Grades
という名前のVARRAY
データ型の3番目の要素を参照します。
OraCollection
は、Oracleコレクションにアクセスして操作するメソッドを提供します。OraCollection
オブジェクトには、Oracleコレクションの個々の要素にアクセスして操作(更新と挿入)するOLEオートメーション・コレクション・インタフェースが暗黙的に含まれます。添字を使用して、個々の要素にアクセスできます。OraCollection
要素の索引は1から開始します。
要素の値は、Variant
型として取り出されます。要素のVariant
型は、コレクションの要素の型によって決まります。要素の値にはNULL
を使用でき、NULL
を設定できます。オブジェクト型およびREF
の要素の場合は、要素の値が、その型に対応するOO4Oオブジェクトとして戻されます。VARRAY
およびネストした表は、LOB、VARRAY
およびネストした表の要素をサポートしません。
表9-1に、要素の型とその要素の戻り値を示します。
表9-1 要素の型と要素の戻り値
要素の型 | 要素の値 |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
要素の値は、SafeArray
プロパティを使用して、Variant
型のSAFEARRAY
形式に変換されます。サポートの対象は、基本的な型の要素のみです。Variant
型SAFEARRAY
の索引は0
から開始します。
OraDatabase
オブジェクトのCreateOraObject
メソッドは、OraCollection
オブジェクトを戻します。このOraCollection
オブジェクトに関連付けられたOracleコレクションは、クライアント側のオブジェクト・キャッシュに作成されます。
コレクションからダイナセットを作成する方法については、「OraCollectionオブジェクトからのダイナセットの作成」を参照してください。
例
サンプル・コードを実行する前に、必要なデータ型と表がデータベース内にあることを確認してください。OraCollection
の例で使用されているスキーマ・オブジェクトについては、「OraCollectionの例で使用されているスキーマ・オブジェクト」を参照してください。
例: コレクション要素へのアクセス
次の例では、コレクション要素へのアクセス方法を示します。
OraDynasetの例
Dim OraSession as OraSession Dim OraDatabase as OraDatabase Dim OraDynaset as OraDynaset Dim EnameList as OraCollection 'create the OraSession Object. Set OraSession = CreateObject("OracleInProcServer.XOraSession") 'create the OraDatabase Object by opening a connection to Oracle. Set OraDatabase = OraSession.OpenDatabase("ExampleDb","scott/tiger", 0&) 'create a dynaset object from department set OraDynaset = OraDatabase.CreateDynaset("select * from department", 0&) 'retrieve a Enames column from Department. 'Here Value property of OraField object returns EnameList OraCollection set EnameList = OraDynaset.Fields("Enames").Value 'access the first element of EnameList msgbox EnameList(1) 'move to next to row OraDynaset.MoveNext 'access all the elements of EnameList for the second row For index = 1 To EnameList.Size msgbox EnameList(index) Next Index
OraParameterの例
Dim OraSession as OraSession Dim OraDatabase as OraDatabase Dim EnameList as OraCollection 'Create the OraSession Object. Set OraSession = CreateObject("OracleInProcServer.XOraSession") 'Create the OraDatabase Object by opening a connection to Oracle. Set OraDatabase = OraSession.OpenDatabase("ExampleDb", "scott/tiger", 0&) 'create an OraParameter object representing EnameList collection bind Variable OraDatabase.Parameters.Add "ENAMES", Null, ORAPARM_OUTPUT, _ ORATYPE_VARRAY,"ENAMELIST" 'execute the sql statement which selects ENAMES VARRAY from the department table OraDatabase.ExecuteSQL ("BEGIN select enames into :ENAMES from department " & _ "where dept_id = 10; END;") 'get the EnameList collection from OraParameter set EnameList = OraDatabase.Parameters("ENAMES").Value 'access all the elements of EnameList For index = 1 To EnameList.Size msgbox EnameList(index) Next Index
例: コレクション要素の変更
次の例では、コレクション要素の変更方法を示します。
Dim OraSession as OraSession Dim OraDatabase as OraDatabase Dim OraDynaset as OraDynaset Dim EnameList as OraCollection 'create the OraSession Object. Set OraSession = CreateObject("OracleInProcServer.XOraSession") 'create the OraDatabase Object by opening a connection to Oracle. Set OraDatabase = OraSession.OpenDatabase("ExampleDb", "scott/tiger", 0&) 'create a dynaset object from department set OraDynaset = OraDatabase.CreateDynaset("select * from department", 0&) 'retrieve a Enames column from Department. Here Value property of OraField object 'returns EnameList OraCollection set EnameList = OraDynaset.Fields("Enames").Value 'lock the row for editing and set the 2nd element of the EnameList to new value OraDynaset.Edit EnameList(2) = "Eric" OraDynaset.Update
例: コレクションへの挿入
次の例では、要素をOracleコレクションに挿入する方法を示します。
OraDynasetの例
Dim OraSession as OraSession Dim OraDatabase as OraDatabase Dim OraDynaset as OraDynaset Dim EnameListNew as OraCollection 'create the OraSession Object. Set OraSession = CreateObject("OracleInProcServer.XOraSession") 'create the OraDatabase Object by opening a connection to Oracle. Set OraDatabase = OraSession.OpenDatabase("ExampleDb", "scott/tiger", 0&) 'create a new OraCollection object from the database set EnameListNew = OraDatabase.CreateOraObject("ENAMELIST") 'set EnameListNew's element values EnameListNew(1) = "Nasser" EnameListNew(2) = "Chris" EnameListNew(3) = "Gopal" 'create a dynaset object from department set OraDynaset = OraDatabase.CreateDynaset("select * from department", 0&) 'start the AddNew operation and insert the EnameListNew collection OraDynaset.AddNew OraDynaset.Fields("dept_id") = 40 OraDynaset.Fields("name") = "DEVELOPMENT" 'set the EnameListNew to enames column OraDynaset.Fields("enames") = EnameListNew OraDynaset.Update
OraParameterの例
Dim OraSession as OraSession Dim OraDatabase as OraDatabase Dim EnameListNew as OraCollection 'Create the OraSession Object. Set OraSession = CreateObject("OracleInProcServer.XOraSession") 'Create the OraDatabase Object by opening a connection to Oracle. Set OraDatabase = OraSession.OpenDatabase("ExampleDb", "scott/tiger", 0&) 'create a new OraCollection object from the database set EnameListNew = OraDatabase.CreateOraObject("ENAMELIST") 'set EnameListNew's element values EnameListNew(1) = "Nasser" EnameListNew(2) = "Chris" EnameListNew(3) = "Gopal" 'create an input OraParameter object representing EnameList collection bind 'Variable OraDatabase.Parameters.Add "ENAMES", Null, ORAPARM_INPUT, ORATYPE_VARRAY, _ "ENAMELIST" 'set the ENAMES parameter value to EnameListNew OraDatabase.Parameters("ENAMES").Value = EnameListNew 'execute the insert sql statement OraDatabase.ExecuteSQL ("insert into department values (40,'DEVELOPMENT', " & _ ":ENAMES)")
例: オブジェクト型の要素を含むコレクション
次の例では、オブジェクト型の要素を含むOracleコレクションの使用方法を示します。
Dim OraSession as OraSession Dim OraDatabase as OraDatabase Dim OraDynaset as OraDynaset Dim CourseList as OraCollection Dim Course as OraObject 'create the OraSession Object. Set OraSession = CreateObject("OracleInProcServer.XOraSession") 'create the OraDatabase Object by opening a connection to Oracle. Set OraDatabase = OraSession.OpenDatabase("ExampleDb", "scott/tiger", 0&) 'create a dynaset object from division set OraDynaset = OraDatabase.CreateDynaset("select * from division", 0&) 'retrieve a Courses column from Division. 'Here Value property of OraField object returns CourseList OraCollection set CourseList = OraDynaset.Fields("Courses").Value 'retrieve the element value of the CourseList at index 1. 'Here element value is returned as Course OraObject set Course = CourseList(1) 'retrieve course_no and title attribute of the Course msgbox Course.course_no msgbox Course.title 'move to next row OraDynaset.MoveNext 'now CourseList object represents collection value for the second row 'and course OraObject 'represents the element value at index 1. 'retrieve course_no and title attribute of the Course. msgbox Course.course_no msgbox Course.title
例: コレクションからのSAFEARRAY変数の作成
次の例では、Oracleコレクションを使用してSAFEARRAY Variant
型を取得および設定する方法を示します。
コレクションからのSAFEARRAY変数の作成
Dim OraSession as OraSession Dim OraDatabase as OraDatabase Dim OraDynaset as OraDynaset Dim EnameList as OraCollection Dim EnameArray as Variant 'create the OraSession Object. Set OraSession = CreateObject("OracleInProcServer.XOraSession") 'create the OraDatabase Object by opening a connection to Oracle. Set OraDatabase = OraSession.OpenDatabase("ExampleDb", "scott/tiger", 0&) 'create a dynaset object from department set OraDynaset = OraDatabase.CreateDynaset("select * from department", 0&) 'retrieve a Enames column from Department. 'Here Value property of OraField objectreturns EnameList OraCollection set EnameList = OraDynaset.Fields("Enames").Value 'get the Variant SAFEARRAY from the collection. EnameArray = EnameList.SafeArray 'display the individual elements of EnameArray msgbox EnameArray(0) msgbox EnameArray(1) msgbox EnameArray(2)
コレクションへのSAFEARRAY変数の設定
Dim OraSession as OraSession Dim OraDatabase as OraDatabase Dim EnameList as OraCollection Dim EnameArray() As String ReDim EnameArray(3) 'Create the OraSession Object. Set OraSession = CreateObject("OracleInProcServer.XOraSession") 'Create the OraDatabase Object by opening a connection to Oracle. Set OraDatabase = OraSession.OpenDatabase("ExampleDb", "scott/tiger", 0&) 'create an Empty uninitialized input OraParameter object 'represent EnameList collection bind Variable OraDatabase.Parameters.Add "ENAMES", Empty, ORAPARM_INPUT, _ ORATYPE_VARRAY,"ENAMELIST" 'get the Empty uninitialized ENAMES parameter value set EnameList = OraDatabase.Parameters("ENAMES").Value 'initialize the EnameArray EnameArray(0) = "Nasser" EnameArray(1) = "Chris" EnameArray(2) = "Gopal" 'set the EnameArray to EnameList's SafeArray EnameList.SafeArray = EnameArray 'execute the insert sql statement OraDatabase.ExecuteSQL ("insert into department " & _ "values (40,'DEVELOPMENT', :ENAMES)")
例: コレクションからのダイナセットの作成
次の例では、Oracleコレクションからダイナセットを作成する方法を示します。
Dim OraSession as OraSession Dim OraDatabase as OraDatabase Dim OraDynaset as OraDynaset Dim CourseList as OraCollection Dim Course as OraObject Dim CourseListDyn as OraDynaset 'create the OraSession Object. Set OraSession = CreateObject("OracleInProcServer.XOraSession") 'create the OraDatabase Object by opening a connection to Oracle. Set OraDatabase = OraSession.OpenDatabase("ExampleDb", "scott/tiger", 0&) 'create a dynaset object from division set OraDynaset = OraDatabase.CreateDynaset("select * from division", 0&) 'retrieve a Courses column from Division. Here Value 'property of OraField object returns CourseList OraCollection set CourseList = OraDynaset.Fields("Courses").Value 'create a input parameter for CourseList for nested table dynaset OraDatabase.Parameters.Add "COURSELIST", CourseList, ORAPARM_INPUT, _ ORATYPE_TABLE, "COURSELIST" 'create a read only dynaset based on the CourseList. Set CourseListDyn = OraDatabase.CreateDynaset("select * from THE" & _ "(select CAST(:COURSELIST AS COURSELIST) from dual)", ORADYN_READONLY) 'dynaset can also be created from Oracle8 collection 'using the following statement, which requires OO4O v8.1.x later Set CourseListDyn = OraDatabase.CreateDynaset("select * from " & _ "TABLE(CAST(:COURSELIST AS COURSELIST))", ORADYN_READONLY) 'get the field values of the collection dynaset msgbox CourseListDyn.Fields("title").Value msgbox CourseListDyn.Fields("course_no").Value 'move the original dynaset to second row Oradynaset.MoveNext 'set the new value of CourseList collection from the second row of main dynaset 'to the "COURSELIST" parameter OraDatabase.Parameters("COURSELIST").Value = CourseList 'refresh the collection dynaset. Now the collection dynaset values are refreshed ' with new collection value. CourseListDyn.Refresh 'get the field values of the collection dynaset msgbox CourseListDyn.Fields("title").Value msgbox CourseListDyn.Fields("course_no").Value
例: コレクション・イテレータ
「例: OraCollectionイテレータ」を参照してください。