Used by direct row handling to process uploaded rows.
Parameter name for SQL scripts | Description | Order |
---|---|---|
UploadData | A .NET or Java class encapsulating table operations uploaded by a MobiLink client. This class is defined in the MobiLink server API for Java and .NET. | 1 |
None.
The handle_UploadData event allows you to process the upload for MobiLink direct row handling. This event fires once for each upload transaction in a synchronization, unless you are using transaction-level uploads, in which case it fires for each transaction.
See Direct row handling.
This event takes a single UploadData parameter. Your Java or .NET method can use the UploadData getUploadedTables or getUploadedTableByName methods to obtain UploadedTableData instances. UploadedTableData allows you to access insert, update, and delete operations uploaded by a MobiLink client in the current synchronization.
For more information about the UploadData and UploadedTableData classes, see Handling direct uploads.
If you want to read column name metadata, you must specify the SendColumnNames MobiLink client extended option or property. Otherwise you can refer to columns by index, as defined at the remote database.
See SendColumnNames (scn) extended option and Send Column Names synchronization parameter.
This event cannot be implemented as SQL.
The following call to a MobiLink system procedure registers a Java method called handleUpload for the handle_UploadData connection event when synchronizing the script version ver1. You run this system procedure against your MobiLink consolidated database.
CALL ml_add_java_connection_script( 'ver1', 'handle_UploadData', 'MyPackage.MyClass.handleUpload' ) |
For more information about ml_add_java_connection_script, see ml_add_java_connection_script system procedure.
The following Java method processes the upload for the remoteOrders table. The UploadData.getUploadedTableByName method returns an UploadedTableData instance for the remoteOrders table. The UploadedTableData getInserts method returns a java.sql.ResultSet instance representing new rows.
import ianywhere.ml.script.*; import java.sql.*; import java.io.*; // ... public void handleUpload( UploadData ut ) throws SQLException, IOException { // Get an UploadedTableData instance representing the // remoteOrders table. UploadedTableData remoteOrdersTable = ut.getUploadedTableByName("remoteOrders"); // Get inserts uploaded by the MobiLink client. java.sql.ResultSet results = remoteOrdersTable.getInserts(); while( results.next() ) { // You can reference column names here because SendColumnNames is on // Get the primary key. int pk = results.getInt("pk"); // Get the uploaded num_ordered value. int numOrdered = results.getInt("num_ordered"); // The current insert row is now ready to be uploaded to wherever // you want it to go (a file, a web service, and so on). } results.close(); } |
The following example outputs insert, update and delete operations uploaded by a MobiLink remote database. The UploadData getUploadedTables method returns UploadedTableData instances representing all tables uploaded by a remote. The order of the tables in this array is the order in which they where uploaded by the remote. The UploadedTableData getInserts, getUpdates, and getDeletes methods return standard JDBC result sets. You can use the println method or output data to a text file or another location.
import ianywhere.ml.script.*; import java.sql.*; import java.io.*; // ... public void handleUpload( UploadData ud ) throws SQLException, IOException { UploadedTableData tables[] = ud.getUploadedTables(); for( int i = 0; i < tables.length; i++ ) { UploadedTableData currentTable = tables[i]; println( "table " + java.lang.Integer.toString( i ) + " name: " + currentTable.getName() ); // Print out delete result set. println( "Deletes" ); printRSInfo( currentTable.getDeletes() ); // Print out insert result set. println( "Inserts" ); printRSInfo( currentTable.getInserts() ); // print out update result set println( "Updates" ); printUpdateRSInfo( currentTable.getUpdates() ); } } |
The printRSInfo method prints out an insert, update, or delete result set and accepts a single java.sql.ResultSet object. Detailed column information, including column labels, is provided by the ResultSetMetaData object returned by the ResultSet getMetaData method. Column labels are available only if the client has the SendColumnNames option turned on. The printRow method prints out each row in a result set.
public void printRSInfo( ResultSet results ) throws SQLException, IOException { // Obtain the result set metadata. ResultSetMetaData metaData = results.getMetaData(); String columnHeading = ""; // Print out column headings. for( int c = 1; c <= metaData.getColumnCount(); c++ ) { columnHeading += metaData.getColumnLabel(c); if( c < metaData.getColumnCount() ) { columnHeading += ", "; } } println( columnHeading ); while( results.next() ) { // Print out each row. printRow( results, metaData.getColumnCount() ); } // Close the java.sql.ResultSet. results.close(); } |
The printRow method, shown below, uses the ResultSet getString method to obtain each column value.
public void printRow( ResultSet results, int colCount ) throws SQLException, IOException { String row = "( "; for( int c = 1; c <= colCount; c++ ) { // Get a column value. String currentColumn = results.getString( c ); // Check for null values. if( currentColumn == null ) { currentColumn = "<NULL>"; } // Add the column value to the row string. row += cur_col; if( c < colCount ) { row += ", "; } } row += " )"; // Print out the row. println( row ); } |
The following call to a MobiLink system procedure registers a .NET method called HandleUpload for the handle_UploadData connection event when synchronizing the script version ver1. You run this system procedure against your MobiLink consolidated database.
CALL ml_add_dnet_connection_script( 'ver1', 'handle_UploadData', 'TestScripts.Test.HandleUpload' ) |
The following .NET method processes the upload for the remoteOrders table.
using System; using System.Data; using System.IO; using iAnywhere.MobiLink.Script; using iAnywhere.MobiLink; namespace MyScripts { public class MyUpload { public MyUpload( DBConnectionContext cc ) { } ~MyUpload() { } public void handleUpload( UploadData ut ) { int i; UploadedTableData[] tables = ut.GetUploadedTables(); for( i=0; i<tables.Length; i+=1 ) { UploadedTableData cur_table = tables[i]; Console.Write( "table " + i + " name: " + cur_table.GetName() ); // Print out delete result set. Console.Write( "Deletes" ); printRSInfo( cur_table.GetDeletes() ); // Print out insert result set. Console.Write( "Inserts" ); printRSInfo( cur_table.GetInserts() ); // print out update result set Console.Write( "Updates" ); printUpdateRSInfo( cur_table.GetUpdates() ); } } public void printRSInfo( IDataReader dr ) { // Obtain the result set metadata. DataTable dt = dr.GetSchemaTable(); DataColumnCollection cc = dt.Columns; DataColumn dc; String columnHeading = ""; // Print out column headings. for( int c=0; c < cc.Count; c = c + 1 ) { dc = cc[ c ]; columnHeading += dc.ColumnName; if( c < cc.Count - 1 ) { columnHeading += ", "; } } Console.Write( columnHeading ); while( dr.Read() ) { // Print out each row. printRow( dr, cc.Count ); } // Close the java.sql.ResultSet. dr.Close(); } public void printUpdateRSInfo( UpdateDataReader utr ) { // Obtain the result set metadata. DataTable dt = utr.GetSchemaTable(); DataColumnCollection cc = dt.Columns; DataColumn dc; String columnHeading = "TYPE, "; // Print out column headings. for( int c = 0; c < cc.Count; c = c + 1 ) { dc = cc[ c ]; columnHeading += dc.ColumnName; if( c < cc.Count - 1 ) { columnHeading += ", "; } } Console.Write( columnHeading ); while( utr.Read() ) { // Print out the new values for the row. utr.SetNewRowValues(); Console.Write( "NEW:" ); printRow( utr, cc.Count ); // Print out the old values for the row. utr.SetOldRowValues(); Console.Write( "OLD:" ); printRow( utr, cc.Count ); } // Close the java.sql.ResultSet. utr.Close(); } public void printRow( IDataReader dr, int col_count ) { String row = "( "; int c; for( c = 0; c < col_count; c = c + 1 ) { // Get a column value. String cur_col = dr.GetString( c ); // Check for null values. if( cur_col == null ) { cur_col = "<NULL>"; } // Add the column value to the row string. row += cur_col; if( c < col_count ) { row += ", "; } } row += " )"; // Print out the row. Console.Write( row ); } } } |
Discuss this page in DocCommentXchange. Send feedback about this page using email. |
Copyright © 2009, iAnywhere Solutions, Inc. - SQL Anywhere 11.0.1 |