このレッスンでは、ダイレクト・ロー・ハンドリングを使用して、クライアント・データベース内の OrderComments テーブルのローを処理します。ダイレクト・ロー・ハンドリング用に次のメソッドを追加します。
GetUpload このメソッドは handle_UploadData イベントに使用します。GetUpload では、アップロードされたコメントを orderComments.txt というファイルに書き込みます。
SetDownload このメソッドは handle_DownloadData イベントに使用します。SetDownload では、orderResponses.txt ファイルを使用してリモート・クライアントに応答をダウンロードします。
また、end_download イベントを処理する EndDownload メソッドも追加します。
次の手順では、処理用メソッドを含む Java または .NET のクラスを作成する方法を示します。完全なリストについては、MobiLinkOrders の全リスト (Java)またはMobiLinkOrders の全リスト (.NET)を参照してください。
Java または .NET を使用して、MobiLinkOrders というクラスを作成します。
Java の場合は、テキスト・エディタまたは開発環境で次のコードを入力します。
import ianywhere.ml.script.*; import java.io.*; import java.sql.*; public class MobiLinkOrders{ // to do... |
.NET の場合は、次のコードを入力します。
using iAnywhere.MobiLink.Script; using System.IO; using System.Data; using System.Text; public class MobiLinkOrders // to do... |
クラスレベルの DBConnectionContext インスタンスを宣言します。
Java の場合:
// class level DBConnectionContext DBConnectionContext _cc; |
.NET の場合:
// class level DBConnectionContext private DBConnectionContext _cc = null; |
Mobile Link サーバによって DBConnectionContext のインスタンスがクラス・コンストラクタに渡されます。DBConnectionContext には、Mobile Link 統合データベースとの現在の接続に関する情報がカプセル化されます。
クラス・コンストラクタを作成します。
クラス・コンストラクタが、クラスレベルの DBConnectionContext インスタンスを設定します。
Java の場合:
public MobiLinkOrders( DBConnectionContext cc ) { // set your class-level DBConnectionContext _cc = cc; } |
.NET の場合:
public MobiLinkOrders( DBConnectionContext cc ) { _cc = cc; } |
ファイル入出力に使用するオブジェクトを宣言します。
Java の場合、java.io.FileWriter と java.io.BufferedReader を宣言します。
// java objects for file i/o FileWriter my_writer; BufferedReader my_reader; |
.NET の場合、Stream Writer と Stream Reader を宣言します。
// instances for file I/O private static StreamWriter my_writer = null; private static StreamReader my_reader = null; |
EndDownload メソッドを作成します。
このメソッドでは、end_download 接続イベントを処理し、またリソースを解放できます。
Java の場合:
public void EndDownload() throws IOException { // free i/o resources if (my_reader!=null) my_reader.close(); if (my_writer!=null) my_writer.close(); } |
.NET の場合:
public void EndDownload() { if( my_writer != null ) { my_writer.Close(); my_writer = null; } |
GetUpload メソッドを作成します。
GetUpload メソッドでは、OrderComments テーブルを表す UploadedTableData クラス・インスタンスを取得します。OrderComments テーブルには、遠隔地の営業部員による特別なコメントが含まれます。このテーブルはレッスン 5:Mobile Link クライアントの設定で作成します。UploadedTableData の getInserts メソッドでは、注文に対する新しいコメントの結果セットを返します。writeOrderComment メソッドでは、結果セット内の各ローをテキスト・ファイルに書き出します。
Java の場合:
//method for the handle_UploadData synchronization event public void GetUpload( UploadData ut ) throws SQLException, IOException { // get an UploadedTableData for OrderComments UploadedTableData orderCommentsTbl = ut.getUploadedTableByName("OrderComments"); // get inserts uploaded by the MobiLink client ResultSet insertResultSet = orderCommentsTbl.getInserts(); while( insertResultSet.next() ) { // get order comments int _commentID = insertResultSet.getInt("comment_id"); int _orderID = insertResultSet.getInt("order_id"); String _specialComments = insertResultSet.getString("order_comment"); if (_specialComments != null) { writeOrderComment(_commentID,_orderID,_specialComments); } } insertResultSet.close(); } // writes out comment details to file public void writeOrderComment( int _commentID, int _orderID, String _comments ) throws IOException { // a FileWriter for writing order comments if(my_writer == null) { my_writer = new FileWriter( "C:\\MLdirect\\orderComments.txt",true); } // write out the order comments to remoteOrderComments.txt my_writer.write(_commentID + "\t" + _orderID + "\t" + _comments); my_writer.write( "\n" ); my_writer.flush(); } |
.NET の場合:
// method for the handle_UploadData synchronization event. public void GetUpload( UploadData ut ) { // get UploadedTableData for remote table called OrderComments UploadedTableData order_comments_table_data = ut.GetUploadedTableByName( "OrderComments" ); // get inserts upload by the MobiLink client IDataReader new_comment_reader = order_comments_table_data.GetInserts(); while( new_comment_reader.Read() ) { // columns are // 0 - "order_comment" // 1 - "comment_id" // 2 - "order_id" // you can look up these values using the DataTable returned by: // order_comments_table_data.GetSchemaTable() if the send column names // option is turned on the remote. // in this example you just use the known column order to determine the column // indexes // only process this insert if the order_comment is not null if( !new_comment_reader.IsDBNull( 2 ) ) { int comment_id = new_comment_reader.GetInt32( 0 ); int order_id = new_comment_reader.GetInt32( 1 ); string comments= new_comment_reader.GetString( 2 ); WriteOrderComment( comment_id, order_id, comments ); } } // always close the reader when you are done with it! new_comment_reader.Close(); } |
SetDownload メソッドを作成します。
OrderComments テーブルを表すクラス・インスタンスを取得します。
DBConnectionContext の getDownloadData メソッドを使用して DownloadData のインスタンスを取得します。DownloadData の getDownloadTableByName メソッドを使用して、OrderComments テーブルの DownloadTableData インスタンスを返します。
Java の場合:
DownloadData download_d = _cc.getDownloadData(); DownloadTableData download_td = download_d.getDownloadTableByName( "OrderComments" ); |
.NET の場合:
DownloadTableData comments_for_download = _cc.GetDownloadData().GetDownloadTableByName( "OrderComments" ); |
このテーブルは、レッスン 5:Mobile Link クライアントの設定でリモート・データベースに作成します。
準備文または IDBCommand を取得します。これを使用すると、ダウンロードに挿入操作や更新操作を追加できます。
Java の場合は、DownloadTableData の getUpsertPreparedStatement メソッドを使用して java.sql.PreparedStatement のインスタンスを返します。
PreparedStatement update_ps = download_td.getUpsertPreparedStatement(); |
.NET の場合、DownloadTableData の GetUpsertCommand メソッドを使用します。
// add upserts to the set of operation that are going to be applied at the // remote database IDbCommand comments_upsert = comments_for_download.GetUpsertCommand(); |
応答をリモート・クライアントに返します。
orderResponses.txt というテキスト・ファイルを c:\MLdirect に作成します。このファイルには、コメントに対する応答が含まれています。orderResponses.txt には、たとえば次のエントリが含まれる場合があります。これには、タブで区切った comment_id、order_id、order_comment の値が含まれます。
... 786 34 OK, we will ship promotional material. 787 35 Yes, the product is going out of production. 788 36 No, we can't increase your commission... ... |
各ローのダウンロード・データを設定します。
Java の場合、次の例では、orderResponses.txt を参照して、Mobile Link ダウンロードにデータを追加しています。
Java の場合:
// a BufferedReader for reading in responses if (my_reader==null) my_reader = new BufferedReader(new FileReader( "c:\\MLdirect\\orderResponses.txt")); // send updated comments down to clients String commentLine; // read the first line from orderResponses.txt commentLine = my_reader.readLine(); while(commentLine != null) { // get the next line from orderResponses.txt String[] response_details = commentLine.split("\t"); if (response_details.length != 3) { System.err.println("Error reading from orderResponses.txt"); System.err.println("Error setting direct row handling download"); return; } int comment_id = Integer.parseInt(response_details[0]); int order_id = Integer.parseInt(response_details[0]); String updated_comment = response_details[2]; // set an order comment response in the MobiLink download update_ps.setInt(1, comment_id); update_ps.setInt(2, order_id); update_ps.setString(3, updated_comment); update_ps.executeUpdate(); // get next line from orderResponses.txt commentLine = my_reader.readLine(); } |
.NET の場合:
string comment_line; while( (comment_line = my_reader.ReadLine()) != null) { // three values are on each line separated by '\t' string[] response_details = comment_line.Split( '\t' ); if( response_details.Length != 3 ) { throw( new SynchronizationException( "Error reading from orderResponses.txt" ) ); } int comment_id = System.Int32.Parse( response_details[0] ); int order_id = System.Int32.Parse( response_details[1] ); string comments= response_details[2]; // Parameters of the correct number and type have already been added // so you just need to set the values of the IDataParameters ((IDataParameter)(comments_upsert.Parameters[0])).Value = comment_id; ((IDataParameter)(comments_upsert.Parameters[1])).Value = order_id; ((IDataParameter)(comments_upsert.Parameters[2])).Value = comments; // add the upsert operation comments_upsert.ExecuteNonQuery(); } |
ダウンロードに挿入操作または更新操作を追加する準備文を終了します。
Java の場合:
update_ps.close(); |
.NET の場合、IDBCommand を閉じる必要はありません。これは、ダウンロードの終わりに Mobile Link によって破棄されます。
クラス・ファイルをコンパイルします
Java または .NET のソース・ファイルが含まれるディレクトリに移動します。
Java または .NET 用の Mobile Link サーバ API ライブラリを参照して MobiLinkOrders をコンパイルします。
Java の場合は、install-dir\java にある mlscript.jar を参照する必要があります。次のコマンドを実行して Java クラスをコンパイルします。
javac -classpath "%SQLANY11%\java\mlscript.jar" MobiLinkOrders.java |
.NET の場合は、次のコマンドを実行します。
csc /out:MobiLinkServerCode.dll /target:library /reference:"%SQLANY11%\Assembly\v2\iAnywhere.MobiLink.Script.dll" MobiLinkOrders.cs |
ここに示した例では、プライマリ・キーの値がユニークとはかぎりません。ユニークなプライマリ・キーの管理を参照してください。
クラス・コンストラクタと DBConnectionContext の詳細については、以下を参照してください。
Java 同期論理の詳細については、Java による同期スクリプトの作成を参照してください。
.NET 同期論理の詳細については、.NET での同期スクリプトの作成を参照してください。
ダイレクト・ロー・ハンドリングの詳細については、ダイレクト・ロー・ハンドリングを参照してください。
MobiLinkOrders の全リスト (Java)
MobiLinkOrders の全リスト (.NET)
Copyright © 2009, iAnywhere Solutions, Inc. - SQL Anywhere 11.0.1 |