UpdateJSON.defineTable()

The defineTable() method drops (deletes) the personContacts table if it exists. Dropping a table deletes all of the table data from the store. The defineTable() method then creates the new table without data.

As always, we can write no data to the store until the table has been defined in the store using the appropriate DDL statement.

This method relies on the UpdateJSON.runDDL() method, which we show later in this appendix.

    // Drops the example table if it exists. This removes all table 
    // data and indexes. The table is then created in the store. 
    // The loadTable() method is used to populate the newly created
    // table with data.
    private void defineTable(KVStore kvstore) {
        System.out.println("Dropping table....");
        String statement = "DROP TABLE IF EXISTS personContacts";
        boolean success = runDDL(kvstore, statement);

        if (success) {
            statement =
                  "CREATE TABLE personContacts (" +
                  "account INTEGER," +
                  "person JSON," +
                  "PRIMARY KEY(account))";
            System.out.println("Creating table....");
            success = runDDL(kvstore, statement);
            if (!success) {
                System.out.println("Table creation failed.");
                System.exit(-1);
            }
        }

    }