このトピックでは、テキスト・レポート・プログラムおよびハンドラーのサンプルを紹介します。
このプログラムは、SQL データベース内の全顧客のリストと、顧客の現在の残高を印刷します。
record CustomerRecord type SQLRecord
{tableNames = [["ADMINISTRATOR.CUSTOMER", "L1"]],
keyItems = [customerNumber]}
customerNumber STRING {column="C_NUMBER", maxLen=6};
customerName STRING {column="C_NAME", isSQLNullable=yes, maxLen=25};
customerAddr1 STRING {column="C_ADDR1", isSQLNullable=yes, maxLen=25};
customerAddr2 STRING {column="C_ADDR2", isSQLNullable=yes, maxLen=25};
customerAddr3 STRING {column="C_ADDR3", isSQLNullable=yes, maxLen=25};
customerBalance MONEY {column="C_BALANCE", isSQLNullable=yes};
end
package com.companyb.customer;
program reportGenerator type BasicProgram
myCustomer CustomerRecord;
myHandler textReportHandler{};
function main()
myHandler.start(); // 制御をハンドラーに渡す
// 顧客リストをデータベースから取得する
open myResults scroll for myCustomer;
forEach (from myResults) // 結果をループ
myHandler.output(myCustomer.customerName, myCustomer.customerBalance);
end // forEach
myHandler.finish(); // レポートを終了する
end
end
package com.companyb.customer;
record reportRecord type BasicRecord
customerName STRING;
customerBalance MONEY;
end
handler textReportHandler type BasicHandler
myReport TextReport{}; // インスタンスを作成する
currentReportRecord reportRecord;
runningTotal MONEY = 0;
function start()
myReport.onEveryRowListener = onEveryRow;
myReport.onLastRowListener = onLastRow;
// 宛先ファイル以外はデフォルトを受け入れる
myReport.startReport("D:/temp/customerReport.txt",
null,null,null,null,null,null);
end
function output(cName STRING in, cBal MONEY in)
// この情報はメインプログラムの forEach ループから取得する
currentReportRecord.customerName = cName;
currentReportRecord.customerBalance = cBal;
runningTotal = runningTotal + cBal;
// レポート・エンジンに制御を渡す
// onEveryRow が呼び出される
myReport.outputToReport();
end
function finish()
// レポート・エンジンに再び制御を渡す
// onLastRow が呼び出される
myReport.finishReport();
end
function onEveryRow(myEvent TextReportEvent in)
myReport.printText(currentReportRecord.customerName);
myReport.column(40);
myReport.printText(currentReportRecord.customerBalance);
myReport.println();
end
function onLastRow(myEvent TextReportEvent in)
myReport.println(); // 1 行スキップする
myReport.printText("All customers:");
myReport.column(40);
myReport.printText(runningTotal);
end
end