Defines a process for resolving a conflict in a specific table.
In the following table, the description provides the SQL data type. If you are writing your script in Java or .NET, you should use the appropriate corresponding data type. See SQL-Java data types and SQL-.NET data types.
In SQL scripts, you can specify event parameters by name or with a question mark. Using question marks has been deprecated and it is recommended that you use named parameters. You cannot mix names and question marks within a script. If you use question marks, the parameters must be in the order shown below and are optional only if no subsequent parameters are specified (for example, you must use parameter 1 if you want to use parameter 2). If you use named parameters, you can specify any subset of the parameters in any order.
Parameter name for SQL scripts | Description | Order (deprecated for SQL) |
---|---|---|
s.remote_id | VARCHAR(128). The MobiLink remote ID. You can only reference the remote ID if you are using named parameters. | Not applicable |
s.username |
VARCHAR(128). The MobiLink user name. |
1 |
s.table |
VARCHAR(128). The table name. |
2 |
None.
When a row is updated on a remote database, the MobiLink client saves a copy of the original values. The client sends both old and new values to the MobiLink server.
When the MobiLink server receives an updated row, it compares the original values with the present values in the consolidated database. The comparison is done using the upload_fetch script.
If the old uploaded values do not match the current values in the consolidated database, the row conflicts. Instead of updating the row, the MobiLink server inserts both old and new values into the consolidated database. The old and new rows are handled using the upload_old_row_insert and upload_new_row_insert scripts, respectively.
Once the values have been inserted, the MobiLink server executes the resolve_conflict script. It provides the opportunity to resolve the conflict. You can implement any scheme of your choosing.
This script is executed once per conflict.
Alternatively, instead of defining the resolve_conflict script, you can resolve conflicts in a set-oriented fashion by putting conflict-resolution logic either in your end_upload_rows script or in your end_upload table script.
You can have one resolve_conflict script for each table in the remote database.
The following statement defines a resolve_conflict script suited to the CustDB sample application for an Oracle installation. It calls a stored procedure ULResolveOrderConflict.
exec ml_add_table_script( 'custdb', 'ULOrder', 'resolve_conflict', 'begin ULResolveOrderConflict(); end; ') CREATE OR REPLACE PROCEDURE ULResolveOrderConflict() AS new_order_id integer; new_status varchar(20); new_notes varchar(50); BEGIN -- approval overrides denial SELECT order_id, status, notes INTO new_order_id, new_status, new_notes FROM ULNewOrder WHERE syncuser_id = SyncUserID; IF new_status = 'Approved' THEN UPDATE ULOrder o SET o.status = new_status, o.notes = new_notes WHERE o.order_id = new_order_id; END IF; DELETE FROM ULOldOrder; DELETE FROM ULNewOrder; END; |
The following call to a MobiLink system procedure registers a Java method called resolveConflict as the script for the resolve_conflict table event when synchronizing the script version ver1.
CALL ml_add_java_table_script( 'ver1', 'table1', 'resolve_conflict', 'ExamplePackage.ExampleClass.resolveConflict' ) |
The following is the sample Java method resolveConflict. It calls a Java method that uses the JDBC connection provided by MobiLink to resolve the conflict.
package ExamplePackage; import java.sql.*; import ianywhere.ml.script.*; public class ExampleClass { DBConnectionContext _cc; public ExampleClass( DBConnectionContext cc ) { _cc = cc; } public void resolveConflict( String user, String table ) throws java.sql.SQLException { if( table == "Order" ) { // Insert error information in a table, Connection conn = _cc.getConnection(); String conflictTable = "New" + table; PreparedStatement stmt = conn.prepareStatement( "SELECT order_id, new_status, new_notes " + "FROM " + conflictTable + "WHERE rid = " + _cc.getRemoteID() ); ResultSet rs = stmt.executeQuery(); PreparedStatement updt = conn.prepareStatement( "UPDATE ULOrder SET status = ?, notes = ? WHERE order_id = ?" ); while( rs.next() ) { if( rs.getString( 2 ) == "Approved" ) { updt.setString( 1, rs.getString( 2 ) ); updt.setString( 2, rs.getString( 3 ) ); updt.setInt( 3, rs.getInt( 1 ) ); } } updt.close(); stmt.close(); } } } |
The following call to a MobiLink system procedure registers a .NET method called ResolveConflict as the script for the resolve_conflict table event when synchronizing the script version ver1.
CALL ml_add_dnet_table_script( 'ver1', 'table1', 'resolve_conflict', 'TestScripts.Test.ResolveConflict' ) |
The following is the sample .NET method ResolveConflict. It calls a .NET method that resolves the conflict.
using System; using iAnywhere.MobiLink.Script; namespace TestScripts { public class Test { DBConnectionContext _cc; public Test( DBConnectionContext cc ) { _cc = cc; } public void ResolveConflict( string user, string table ) { if( table == "Order" ) { // Insert error information in a table, DBConnection conn = _cc.GetConnection(); String conflictTable = "New" + table; DBCommand cmd = conn.CreateCommand(); cmd.CommandText = "SELECT order_id, new_status, new_notes " + "FROM " + conflictTable + "WHERE rid = " + _cc.GetRemoteID(); DBRowReader dr = cmd.ExecuteReader(); DBCommand updt = conn.CreateCommand(); updt.CommandText = "UPDATE ULOrder SET status = ?, notes = ? WHERE order_id = ?"; object[] row; while( (row = dr.NextRow() ) != null ) { if( row.[1].Equals( "Approved" ) ) { updt.Parameters[0] = row[1]; updt.Parameters[1] = row[2]; updt.Parameters[2] = row[0]; } } updt.Close(); cmd.Close(); conn.Close(); } } } } |
Discuss this page in DocCommentXchange.
|
Copyright © 2012, iAnywhere Solutions, Inc. - SQL Anywhere 12.0.1 |