Skip to content
nmc9 edited this page Apr 30, 2018 · 4 revisions

Table Class

The table class should contain a list of all the columns of the table. It should also extend the TableHelper class. This class has the following methods and uses them to determine what columns and records that should sync when syncing occurs. The CreateTable method below will be described in more detail in the QueryBuilder section

Example

public class UsersTable extends TableHelper {

    //Name of the table It should match the server table's name
    public static final String TABLE_NAME = "users";
    
    //List of columns. Column names should match server names
    public static final String COL_ID = "userid";
    public static final String COL_USERNAME = "username";
    public static final String COL_PASSWORD = "password";
    
    // List of fields
    public static final String[] FIELDS = {
            COL_ID,
            COL_USERNAME,
            COL_PASSWORD
    };

    //List of fields that should be sent up on a server sync
    //If all should be synced return FIELDS;
    @Override
    public String[] sync_up_filter_fields() {
        return new String[]{COL_ID};
    }

    //Constructor
    public UsersTable(DatabaseHelper databaseHelper) {
        super(databaseHelper);
    }
  
    //If true this table is synced up
    @Override
    public boolean sync_up() {
        return false;
    }
    
    //If true this table is synced down
    @Override
    public boolean sync_down() {
        return false;
    }

    //What conditions need to be met for a record to be added to sync_up
    @Override
    public Where sync_up_condition() {
        return SYNC_NONE;
    }

    //Returns the name of the table
    @Override
    public String table_name() {
        return TABLE_NAME;
    }

    //Returns the id column of the table
    @Override
    public String id_column() {
        return COL_ID;
    }

    //Returns list of columns that make up the table
    @Override
    public String[] fields() {
        return FIELDS;
    }
    
    /*
    * Using the Create Table class build the query for Creating this table.
    * CreateTable has a lot of shortcut methods that can be used to remove hardcoded strings
    *
    * Building the CreateTable is similar to writing a create table statement
    * Create a new instance using the TABLE_NAME;
    * add columns with CreateTable.put();
    * put accepts a varaible number of strings so
    *   COLUMN_NAME TYPE(#) ATTRUBUTES looks like
    *   COLUMN_NAME, TYPE(#), PRIMARY_KEY
    *When the table is built use .get() to return the String used to create the table
    */
    public String CREATE_TABLE() {
        CreateTable user = new CreateTable(TABLE_NAME);
        user.put(COL_ID, INT(11), PRIMARY_KEY);
        user.put(COL_USERNAME, VARCHAR(20));
        user.put(COL_PASSWORD, VARCHAR(20));
        return user.get();
    }
}
}

Variables and Methods

TABLE_NAME:

Name of table. Should match the table name on server

COL_*

FIELD_*

These static Strings are used to name the columns in the table. The COL_ prefix is recommended for important fields like COL_ID. These should also match the column names on the server

FIELDS

Create a list and add all the fields you created as static Strings to this String[] variable;

sync_up_filter_fields:

If you want to sync every column to the server return FIELDS. Not every table is going to want to sync every column with the server. In this case set only the ones you want int a new String[] list.

sync_up

boolean to determine if this table should be included in server sync ups.

sync_down

boolean to determine if this table should be included in server sync down.

sync_up_condition:

Sets which conditions to apply when running the sync. Any records that match the condition will be synced up

Options (SYNC_ALL, SYNC_NONE)

SYNC_ALL will add every record in table to sync up list

SYNC_NONE will not include any

table_name:

Returns the name of the table

id_column

Returns the id column of the table

fields

Returns list of columns that make up the table

CREATE_TABLE

Use the CreateTable class and the column names to generate a Create Table String that will run when the database is first being created.