MobiLink Synchronization User's Guide
File-Based Downloads
Examples
This example implements file-based download for snapshot synchronization.
The following commands create the three databases used in the example: a consolidated database, a remote database, and a file-definition database.
dbinit scons.db dbinit sremote.db dbinit sfdef.db
The following commands start the three databases, create a data source name for MobiLink to use to connect to the consolidated database, and start the MobiLink synchronization server.
dbeng9 -n sfdef_eng sfdef.db dbeng9 -n scons_eng scons.db dbeng9 -n sremote_eng sremote.db dbdsn -y -w fbd_demo -c "eng=scons_eng;dbf=scons.db;uid=dba; pwd=sql;astart=off;astop=off" start dbmlsrv9 -v+ -c "dsn=fbd_demo" -zu+ -ot scons.txt
In this example, the consolidated database has one table, called T1. After connecting to the consolidated database, you can run the following SQL to create table T1:
CREATE TABLE T1 ( pk INTEGER PRIMARY KEY, c1 INTEGER );
The following code creates a download script for the "filebased" script version:
CALL ml_add_table_script( 'filebased', 'T1', 'download_cursor', 'SELECT pk, c1 FROM T1' );
The following code creates upload and download scripts for the "normal" script version:
CALL ml_add_table_script ( 'normal', 'T1', 'upload_insert', 'INSERT INTO T1 VALUES (?,?)');
CALL ml_add_table_script( 'normal', 'T1', 'upload_update', 'UPDATE T1 SET c1 = ? WHERE pk = ? ' ); CALL ml_add_table_script( 'normal', 'T1', 'upload_delete', 'DELETE FROM T1 WHERE pk = ?' ); CALL ml_add_table_script( 'normal', 'T1', 'download_cursor', 'SELECT pk, c1 FROM T1' ); COMMIT;
The following command creates the stored procedure begin_pub and specifies that begin_pub is the begin_publication script for both the "normal" and "filebased" script versions:
CREATE PROCEDURE begin_pub ( INOUT generation_num integer, IN username varchar(128), IN pubname varchar(128) ) BEGIN SET gnum=1; END; CALL ml_add_connection_script( 'filebased', 'begin_publication', '{ call begin_pub( ?, ?, ? ) }' ); CALL ml_add_connection_script( 'normal', 'begin_publication', '{ call begin_pub( ?, ?, ? ) }' );
In this example, the remote database also contains one table, called T1. Connect to the remote database and run the following SQL to create the table T1, a publication called P1, and a user called U1. The SQL also creates a subscription for U1 to P1.
CREATE TABLE T1 ( pk INTEGER PRIMARY KEY, c1 INTEGER ); CREATE PUBLICATION P1 ( TABLE T1 );
CREATE SYNCHRONIZATION USER U1; CREATE SYNCHRONIZATION SUBSCRIPTION TO P1 FOR U1;
The following code creates an sp_hook_dbmlsync_validate_download_file hook to implement user-defined validation logic in the remote database:
CREATE PROCEDURE sp_hook_dbmlsync_validate_download_file() BEGIN DECLARE udata varchar(256); SELECT value INTO udata FROM #hook_dict WHERE name = 'user data';
IF udata <> 'ok' THEN UPDATE #hook_dict SET value = 'FALSE' WHERE name = 'apply file'; END IF; END
A file-definition database is required in MobiLink systems that use file-based downloads. This database has the same schema as the remote databases being updated by file-based download, and it contains no data or state information. The file-definition database is used solely to define the structure of the data that is to be included in the download file. One file-definition database can be used for many groups of remote databases, each defined by its own MobiLink group user name.
The following code defines the file-definition database for this sample. It creates a schema that is identical to the remote database, and also creates:
a publication called P1 that publishes all rows of the T1 table. The same publication name must be used in the file-definition database and the remote databases.
a MobiLink user called G1. This user represents all the remotes that are to be updated in the file-based download.
a subscription to the publication
You must connect to sfdef.db before running this code.
CREATE TABLE T1 ( pk INTEGER PRIMARY KEY, c1 INTEGER );
CREATE PUBLICATION P1 ( TABLE T1 ); CREATE SYNCHRONIZATION USER G1;
CREATE SYNCHRONIZATION SUBSCRIPTION TO P1 FOR G1;
To prepare your new remote database so that you can apply a download file, you need to either perform a normal synchronization or create the download file with the dbmlsync -bg option. This example shows you how to initialize your new remote database by performing a normal synchronization.
You can perform an initial synchronization of the remote database with the script version called normal that was created earlier:
dbmlsync -c "uid=dba;pwd=sql;eng=sremote_eng; dbf=sremote.db" -v+ -e "sv=normal"
Connect to the consolidated database and insert some data that will be synchronized by file-based download, such as the following:
INSERT INTO T1 VALUES( 1, 1 ); INSERT INTO T1 VALUES( 2, 4 ); INSERT INTO T1 VALUES( 3, 9 ); COMMIT;
The following command must be run on the computer that holds the file-definition database. It does the following:
the dbmlsync -bc option creates the download file, and names it file1.df.
the -be option includes the string "OK" in the download file that will be accessible to the sp_dbmlsync_validate_download_file hook.
dbmlsync -c "uid=dba;pwd=sql;eng=sfdef_eng;dbf=sfdef.db" -v+ -e "sv=filebased" -bc file1.df -be ok -ot fdef.txt
To apply the download file, run dbmlsync with the -ba option on the remote database, supplying the name of the download file you want to apply:
dbmlsync -c "uid=dba;pwd=sql;eng=sremote_eng; dbf=sremote.db" -v+ -ba file1.df -ot remote.txt
The changes are now applied to the remote database. Open Interactive SQL, connect to the remote database, and run the following SQL command to verify that the remote has the data:
SELECT * FROM T1
The following commands stop all three database engines and erase the files.
del file1.df dbmlstop -h -w dbstop -y -c eng=sfdef_eng dbstop -y -c eng=scons_eng dbstop -y -c eng=sremote_eng dberase -y sfdef.db dberase -y scons.db dberase -y sremote.db