handle_DownloadData connection event

Used by direct row handling to create a set of rows to download.

Parameters

None.

Default action

None.

Remarks

The handle_DownloadData event allows you to determine what operations to download to MobiLink clients using direct row handling.

Direct row handling is used to synchronize to data sources other than MobiLink supported consolidated databases. See Direct row handling.

To create the direct download, you can use the DownloadData and DownloadTableData classes in the MobiLink server API for Java or .NET.

For Java, the DBConnectionContext getDownloadData method returns a DownloadData instance for the current synchronization. DownloadData encapsulates all download operations to send to a remote client. You can use the DownloadData getDownloadTables and getDownloadTableByName methods to obtain a DownloadTableData instance. DownloadTableData encapsulates download operations for a particular table. You can use the getUpsertPreparedStatement method to obtain prepared statements for insert and update operations. You can use the DownloadTableData getDeletePreparedStatement method to obtain prepared statements for delete operations.

For .NET, the DBConnectionContext GetDownloadData method returns a DownloadData instance for the current synchronization. DownloadData encapsulates all download operations to send to a remote client. You can use the DownloadData GetDownloadTables and GetDownloadTableByName methods to obtain a DownloadTableData instance. DownloadTableData encapsulates download operations for a particular table. You can use the GetUpsertCommand method to obtain commands for insert and update operations. You can use the DownloadTableData getDeleteCommand method to obtain commands for delete operations.

For Java, see DBConnectionContext interface. For .NET, see DBConnectionContext interface.

You can create the download in handle_DownloadData or another synchronization event. MobiLink provides this flexibility so that you can set the download when data is uploaded or when particular events occur. If you want to create the direct download in an event other then handle_DownloadData, you must create a handle_DownloadData script whose method does nothing. MobiLink requires this script to be defined to enable direct row handling. Except in upload-only synchronization, the MobiLink server requires that at a minimum, a handle_DownloadData script be defined.

If you create the direct download in an event other than handle_DownloadData, the event must not be before the begin_synchronization event and cannot be after the end_download event.

Note

This event cannot be implemented as SQL.

See also
Java example

The following call to a MobiLink system procedure registers a Java method called handleDownload for the handle_DownloadData 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_DownloadData',
 'MyPackage.MyClass.handleDownload' )

See ml_add_java_connection_script system procedure.

The following example shows you how to use the handleDownload method to create a download.

The following code sets up a class level DBConnectionContext instance in the constructor for a class called MobiLinkOrders.

import ianywhere.ml.script.*;
import java.io.*;
import java.sql.*;
import java.lang.System;

public class  MobiLinkOrders{

  DBConnectionContext _cc;

  public MobiLinkOrders( DBConnectionContext cc ) {
    _cc = cc;
  }

In your HandleDownload method, you use the DBConnectionContext getDownloadData method to return a DownloadData instance for the current synchronization. The DownloadData getDownloadTableByName method returns a DownloadTableData instance for the remoteOrders table. The DownloadTableData getUpsertPreparedStatement method returns a java.sql.PreparedStatement. To add an operation to the download, you set all column values and call the executeUpdate method.

The following is the handleDownload method of the MobiLinkOrders class. It adds two rows to the download for a table called remoteOrders.

// Method used for the handle_DownloadData event.
public void handleDownload() throws SQLException {
  // Get DownloadData instance for current synchronization.
  DownloadData downloadData = _cc.getDownloadData();
 
  // Get a DownloadTableData instance for the remoteOrders table.
  DownloadTableData td = downloadData.getDownloadTableByName("remoteOrders");

  // Get a java.sql.PreparedStatement for upsert (update/insert) operations.
  PreparedStatement upsertPS = td.getUpsertPreparedStatement();

  // Set values for one row.
  upsertPS.setInt( 1, 2300 ); 
  upsertPS.setInt( 2, 100 );

  // Add the values to the download.
  int updateResult = upsertPS.executeUpdate();

  // Set values for another row.
  upsertPS.setInt( 1, 2301 );
  upsertPS.setInt( 2, 50 );
  updateResult = upsertPS.executeUpdate();

  upsertPS.close();

  // ... 
}
.NET example

The following call to a MobiLink system procedure registers a .NET method called HandleDownload as the script for the handle_DownloadData connection event when synchronizing the script version ver1. This syntax is for SQL Anywhere consolidated databases.

CALL ml_add_dnet_connection_script(
   'ver1', 'handle_DownloadData',
   'TestScripts.Test.HandleDownload'
)

The following is the sample .NET method HandleDownload:

using System;
using System.Data;
using System.IO;
using iAnywhere.MobiLink.Script;
using iAnywhere.MobiLink;

namespace MyScripts
{
    /// <summary>
    /// Tests that scripts are called correctly for most sync events.
    /// </summary>
    public class MobiLinkOrders
    {
 private DBConnectionContext _cc;

 public MobiLinkOrders( DBConnectionContext cc )
 {
     _cc = cc;
 }

 ~MobiLinkOrders()
 {
 }

 public void handleDownload()
 {
     // Get DownloadData instance for current synchronization.
     DownloadData my_dd = _cc.GetDownloadData();
     
     // Get a DownloadTableData instance for the remoteOrders table.
     DownloadTableData td = my_dd.GetDownloadTableByName("remoteOrders");

     // Get an IdbCommand for upsert (update/insert) operations.
     IDbCommand upsert_stmt = td.GetUpsertCommand();

     IDataParameterCollection parameters = upsert_stmt.Parameters; 

     // Set values for one row.
     parameters[ 0 ] = 2300;
     parameters[ 1 ] = 100;

     // Add the values to the download.
     int update_result = upsert_stmt.ExecuteNonQuery();

     // Set values for another row.
     parameters[ 0 ] = 2301;
     parameters[ 1 ] = 50;
     update_result = upsert_stmt.ExecuteNonQuery();

     // ...
 }

    }
}