< 前へ | 次へ >

結果セット

クエリーから戻された ResultSet 内を移動して、そこに含まれている各レコードのデータを 取り出すことができます。

ResultSet オブジェクトを使用する際の手順は次のとおりです。

  1. ResultSet オブジェクトを作成します。 ResultSet オブジェクトを作成するには、Session オブジェクトの BuildResultSet メソッドまたは BuildSQLQuery メソッドを使用します。これらのメソッドはいずれも ResultSet オブジェクトを戻します。このオブジェクトは、クエリーを実行する準備は整っていますがデータは入っていません。
  2. クエリーを実行して、ResultSet にデータを表示します。 クエリーを実行するには、ResultSet オブジェクトの Execute または ExecuteAndCountRecords メソッドを呼び出します。これらのメソッドは、ResultSet にデータベースからのデータを表示します。
  3. 必要なレコードが見つかるまで結果データ内をナビゲートします。 結果セット内の最初のレコードに移動するには、MoveNext メソッドを呼び出します。このメソッドは、カーソルを初期化して最初のレコードに移動します。これで、ResultSet のメソッドを使用して最初のレコードのフィールド情報を取得できます。後続のレコードに移動するには、再度にわたって MoveNext メソッドを使用します。これで、ResultSet のメソッドを使用して現在のレコードのフィールド情報を取得できます。
  4. レコードのフィールドから値を取得します。 ResultSet オブジェクトのメソッドを使用して、レコードのフィールドに関する 情報を取得できます。

以下のコード例は、ResultSet オブジェクトのメソッドを 使用して、クエリーから戻されたデータ間を移動する方法を 示しています。

use CQPerlExt; 
#Start a Rational ClearQuest session 
$SessionObj = CQSession::Build(); 
$dbsetname = "CQMS.SAMPL.HOME"; 

#Refresh list of accessible databases 
$databases = $SessionObj->GetAccessibleDatabases("MASTR", "", $dbsetname); 

#Log into a database 
$SessionObj->UserLogon("admin","","SAMPL",$dbsetname); 

#Create a Query by calling the BuildQuery(record_type) method, You specify a record type as the argument
$querydef = $SessionObj->BuildQuery("defect"); 
#  You use the BuildField (field_name) method for each field value that you want included for each record 
#  returned in the result set for the query:
$querydef->BuildField("id"); 
$querydef->BuildField("headline"); 
$querydef->BuildField("owner.login_name"); 
$querydef->BuildField("submit_date"); 

# The BuildResultSet(query_def_name) you build will contain the records returned by the query: 
$resultset = $SessionObj->BuildResultSet($querydef); 

# The following command runs the query and counts the number of records in the result set.
$ct = $resultset->ExecuteAndCountRecords(); 

for ($i = 0; $i < $ct; $i++) { 
#  You use the MoveNext method to iterate through the records in the ResultSet:
$resultset->MoveNext(); 
# You can also loop through ResultSet without using the count, like this:
# while ( $resultset->MoveNext == $CQPerlExt::CQ_SUCCESS ) {

#  You use the GetColumnValue(column_number) method to get the field values that you specified to be returned 
# for each record (by calling the BuildField method for each field to include):
   print $resultset->GetColumnValue(1); 
   print " "; 
   print $resultset->GetColumnValue(2); 
   print " "; 
   print $resultset->GetColumnValue(3); 
   print " "; 
   print $resultset->GetColumnValue(4); 
   print "¥n";
   } 

# There are also methods to discover column properties such as these methods: 
# ResultSet:GetNumberOfColumns 
# ResultSet:GetColumnType (all values are returned as strings, but you may wish to format them) 
#ResultSet:GetColumnLabel

CQSession::Unbuild($SessionObj); 
最も有用なクエリーには、特定の基準に一致する特定のレコードを 取り出すよう定義したフィルタが組み込まれています。次のレッスンでは、検索基準を 定義する方法について説明します。

フィードバック
< 前へ | 次へ >