Modify the MobiLink user name.
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. This is an INOUT parameter. |
1 |
None.
This script is invoked at the end of the authentication transaction.
The MobiLink server provides the user name as a parameter when it calls scripts; the user name is sent by the MobiLink client. Sometimes you may want to have an alternate user name. This script allows you to modify the user name used in calling MobiLink scripts.
The username parameter must be long enough to hold the user name.
SQL scripts for the modify_user event must be implemented as stored procedures.
A more flexible approach to mapping the MobiLink user name is to use user-defined named parameters. See User-defined named parameters.
The following example maps a remote database user name to the ID of the user using the device, by using a mapping table called user_device. This technique can be used when the same person has multiple remotes (such as a PDA and a laptop) requiring the same synchronization logic (based on the user's name or id).
The following call to a MobiLink system procedure assigns the ModifyUser stored procedure to the modify_user event. This syntax is for a SQL Anywhere consolidated database.
CALL ml_add_connection_script( 'ver1', 'modify_user', 'call ModifyUser( {ml s.username} )' ) |
The following SQL statement creates the ModifyUser stored procedure.
CREATE PROCEDURE ModifyUser( INOUT u_name varchar(128) ) BEGIN SELECT user_name INTO u_name FROM user_device WHERE device_name = u_name; END |
The following call to a MobiLink system procedure registers a Java method called modifyUser as the script for the modify_user connection event when synchronizing the script version ver1.
CALL ml_add_java_connection_script( 'ver1', 'modify_user', 'ExamplePackage.ExampleClass.modifyUser' ) |
The following is the sample Java method modifyUser. It gets the user ID from the database and then uses it to set the user name.
package ExamplePackage; import java.lang.Integer; import java.sql.*; import ianywhere.ml.script.*; public class ExampleClass { DBConnectionContext curConn; public ExampleClass( DBConnectionContext cc ) { curConn = cc; } public void modifyUser( InOutString ioUserName ) throws SQLException { Connection conn = curConn.getConnection(); PreparedStatement uidSelect = conn.prepareStatement( "SELECT rep_id FROM SalesRep WHERE name = ?" ); try { uidSelect.setString( 1, ioUserName.getValue() ); ResultSet uidResult = uidSelect.executeQuery(); try { if( uidResult.next() ) { ioUserName.setValue( Integer.toString(uidResult.getInt( 1 ))); } } finally { uidResult.close(); } } finally { uidSelect.close(); } } } |
The following call to a MobiLink system procedure registers a .NET method called ModUser as the script for the modify_user connection event when synchronizing the script version ver1.
CALL ml_add_dnet_connection_script( 'ver1', 'modify_user', 'TestScripts.Test.ModUser' ) |
The following is the sample .NET method ModUser.
using iAnywhere.MobiLink.Script; namespace TestScripts { public class Test { DBConnectionContext curConn; public Test( DBConnectionContext cc ) { curConn = cc; } public void ModifyUser( ref string ioUserName ) { DBCommand cmd = curConn.GetConnection().CreateCommand(); cmd.CommandText = "SELECT rep_id FROM SalesRep WHERE name = ?"; cmd.Parameters[0] = ioUserName; DBRowReader r = cmd.ExecuteReader(); object[] row; if( (row = r.NextRow()) != null ) { ioUserName = (string) row[0]; } } } } |
Discuss this page in DocCommentXchange.
|
Copyright © 2012, iAnywhere Solutions, Inc. - SQL Anywhere 12.0.1 |