com.ibm.mobileservices.isync.midp
Class TableMetaData

java.lang.Object
  |
  +--com.ibm.mobileservices.isync.midp.TableMetaData

public class TableMetaData
extends java.lang.Object

This class holds table information within an ISyncSubscriptionSet, consisting of the name of the table, the number of columns in the table, the type of each column, if it is nullable, and if it is a primary key.

The application (MIDLET) designer is responsible for reading and writing the raw data in the byte arrays read from and written to FastRecordStore and Index .

This format is described below.

Row Data Format

Every row has an extra byte at its beginning to indicate if the application modified the row, this byte is intially zero. The application designer is required to update this byte if they DELETE, UPDATE or INSERT a row into RMS. This indicates that on a future SYNCHRONIZE request the rows should be sent to DB2 Everyplace to be included in the source database.

If the sync is successful, we clear the dirty bytes in those rows.

Failure to set this byte to ISync.ROW_ADDED, ISync.ROW_CHANGED, ISync.ROW_DELETED will cause your changes to never be replicated.

Within each row, for each column, if it is nullable, call DataInputStream.readBoolean to get the null indicator. If null, no data for the column follows.

If not null (indicator is false) or if the column is not nullable, the following table indicates which DataInputStream/DataOutputStream method to call.

Data Type Mapping From the Source Database to java.sql.Types to MIDP Row Data

SQL Data Type on Data Source java.sql.Types on MIDP Client Java Data Type on MIDP Client
datalink // unsupported N/A
time INTEGER int
date INTEGER int
timestamp LONG long
blob LONGVARCHAR String, or byte[]
char CHAR String
varchar VARCHAR String
clob LONGVARBINARY String, or byte[]
graphic // unsupported N/A
vargraphic // unsupported N/A
dbclob // unsupported N/A
real REAL String
double precision DOUBLE String
bit VARCHAR String
//unsupported TINYINT N/A
smallint SMALLINT short
int INTEGER int
bigint, int8 BIGINT long
numeric VARCHAR String

DataInputStream and DataOutputStream Methods To Use Based on MIDP Row Format

SQL Type JAVA Type DataOut/InputStream method
INT8, BIGINT long writeLong, readLong
CHAR String writeUTF, readUTF
VARCHAR String writeUTF, readUTF
DATE, TIME int (see below) writeInt, readInt
TIMESTAMP long (see below) writeLong, readLong
DECIMAL, NUMERIC String writeUTF, readUTF
INTEGER int writeInt, readInt
SMALLINT short writeShort, readShort
TINYINT byte writeByte, readByte
BLOB/CLOB String writeUTF, readUTF

Interpreting the DATE, TIME and TIMESTAMP Values

DATE stored in an integer is represented as follows:

        
int dt = din.readInt(); int year = dt >> 16; int mon = (dt >> 8) & 0xFF; int day = dt & 0xFF;
All values are 1 based, e.g. Jan == 1.

TIME is represented as:

int tm = din.readInt(); int hour = tm >> 16; int min = (tm >> 8) & 0xFF; sec = tm & 0xFF;
TIMESTAMP stores the DATE in the upper 4 bytes of the Long and TIME in the lower:
        
long ts = dinr.readLong(); int dt = (int) (ts >> 32);

int year = dt >> 16; int mon  = (dt >> 8) & 0xFF; int day  = dt & 0xFF;

int tm = (int) (ts & 0xFFFF);

int hour = tm >> 16; int min  = (tm >> 8) & 0xFF; int sec  = tm & 0xFF;

An Example:

For the following table:
create table isyncUSync (a int, b int not null, c varchar(20), id bigint not null primary key)
        insert into isyncUSync values (null, 10, 'some text', 99);
the rowdata would be interpreted as follows:
DataOutputStream dout = new DataOutputStream(byteArrayOutStrm);

dout.writeByte(ISync.ROW_ADDED); // dirty byte, flags byte at start of row

dout.writeBoolean(true); // 'a' is null, no data follows
dout.writeInt(10); // 'b', not nullable, no null  ind
dout.writeBoolean(false); // c is nullable, but not null
dout.writeUTF("some text");
dout.writeLong(99); // 'id' not nullable

Example Using TableMetaData

        
TableMetaData rs = ((MIDPISync)isync).getTableMetaDataByName(storeName); int numCols = rs.getNumCols(); FastRecordStore rms = FastRecordStore.openRecordStore(storeName, false); Index index = new Index(rms, rs); FastRecordEnumeration enum = rms.enumerateRecords(null, null, false); while (enum.hasNextElement()) {
int id = enum.nextRecordId(); int recSize = rms.getRecordSize(id); byte data = new byte[recSize]; recSize = rms.getRecord(id, data, 0); // found record to delete from source database, during next sync cycle if (id == rowToDelete) {
// mark row dirty data[0] = ISync.ROW_DELETED; index.updateRecord(id, data, recSize); continue; // to next row
} ByteArrayInputStream bin = new ByteArrayInputStream(data); DataInputStream din = new DataInputStream(bin);

din.readByte(); // dirty byte, flags byte at start of row

for (int c = 0; c < numCols; c++) {

int type = rs.getType(c); boolean isNullable = rs.isNullable(c); boolean ind = false; if (isNullable)
ind = din.readBoolean(); // null indicator
if (ind)
continue;
switch (type) {
case CHAR: case VARCHAR: strval = din.readUTF(); break;

case INTEGER: case DATE: case TIME: intval = din.readInt(); break; // etc

}
}
}


Field Summary
 java.lang.String operation
           
 
Constructor Summary
TableMetaData(java.lang.String name, byte[] schema, int schemaLen, boolean readOnlyTable)
          Called from JavaCommonISync after reading schema from SS
 
Method Summary
 java.lang.String getColumnName(int colnum)
          Return column name, if present
 int getNumCols()
          Get the number of columns in this table
 int getNumPrimaryKeys()
          Return the number of primary keys
 java.lang.String getTableName()
          Return the name of the table represented by this TableMetaData
 short getType(int colnum)
          Return the com.ibm.mobileservices.isync.sql.Types java type, colnum is 0 based.
 boolean isNullable(int colnum)
          Return if this column is nullable, colnum is 0 based.
 boolean isPrimaryKey(int colnum)
          Return if this column is the primary key, colnum is 0 based.
 boolean isReadOnlyTable()
           
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

operation

public java.lang.String operation
Constructor Detail

TableMetaData

public TableMetaData(java.lang.String name,
                     byte[] schema,
                     int schemaLen,
                     boolean readOnlyTable)
              throws java.io.IOException
Called from JavaCommonISync after reading schema from SS

Method Detail

getNumPrimaryKeys

public int getNumPrimaryKeys()
Return the number of primary keys


getType

public short getType(int colnum)
              throws java.lang.Exception
Return the com.ibm.mobileservices.isync.sql.Types java type, colnum is 0 based.

java.lang.Exception
See Also:
Types

isNullable

public boolean isNullable(int colnum)
                   throws java.lang.Exception
Return if this column is nullable, colnum is 0 based.

java.lang.Exception

isPrimaryKey

public boolean isPrimaryKey(int colnum)
                     throws java.lang.Exception
Return if this column is the primary key, colnum is 0 based.

java.lang.Exception

getColumnName

public java.lang.String getColumnName(int colnum)
                               throws java.lang.Exception
Return column name, if present

java.lang.Exception

getTableName

public java.lang.String getTableName()
Return the name of the table represented by this TableMetaData


getNumCols

public int getNumCols()
Get the number of columns in this table


isReadOnlyTable

public boolean isReadOnlyTable()

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object


(c) Copyright IBM Corp. 2001, 2002, 2003. All Rights Reserved.