Data Storage and Management


Table of Contents

Introduction

What database should be used?

After the server is initialized, you may want to store the data collected from clients to a database for future reference, so a MySQL database will be compatible with our framework.

What kind of data can be stored?

Our framework provides 12 different types of data that can be stored in the database by using our database API within the lib-server library corresponding to the 12 types of data containers in the core library.

What should you have before using database API?

In order to use the database API successfully, you should have one your own MySQL database before using our API. Also, you need to need to know the URL, username, password and database name of your database.

 

 

How to use database API

Our database API follows a factory design pattern and you can store all 12 types of data by the same steps.

1. Initialize your database information

Before connecting to your database, you should create DatabaseInfo object, which contains the URL, username, password and database name of your database.

2. Generate a table modifier

There could be multiple tables inside of one database, so you should first decide the table that you want to create if it does not exist or you want to use if it already existed. Also, you should decide which data type you want to select within the 12 types. Then you can get a GetDatabaseTableModifier instance by using the getInstance() method and get a specific table modifier by using the corresponding getTableModifier() method with the table name.

3. Select specific records

When you want to retrieve or modify the records inside of a table, you need to setup your query condition. By creating a RecordSelectionInfo object with the table name, you can have 5 basic relational operations methods: notequal(), equals(), lessthan(), lessthanEquals(), greaterthan(), greaterthanEquals(). Before using these methods, you should create a SelectionValueObject object first, which first contains the field name you want to project to and second contains the real value used to filter the table. You can concatenate multiple query condition together within one RecordSelectionInfo object by passing different SelectionValueObject objects to these 5 methods and the method returnDataID() will return a set of records (rows) corresponding to the query you made.

4. Specify record update information

If you want to update some column values of a table, you first need to create RecordUpdateInfo object by using the field column name and the updated values.

5. Modify a table

To modify a table corresponding to one of the 12 data types, you should get a DatabaseTableModifier object. There are several methods for this object:

  1. createTable() – create a table if there is not one with the same name existed in the database
  2. insertRecord() – insert a record into the table (A record or a row is a group of fields within a table that are relevant to a specific entity )
  3. updateRecord() – update a record or a group of records by the specific values defined by you
  4. deleteRecord() – delete a record or a group of records by the specific query conditions defined by you
  5. selectRecord() – select a record or a group of records by the specific query conditions defined by you and return a set of StandardBTMDatabaseRecord object

(A StandardBTMDatabaseRecord object contains the TimeZonedTimeStampObject object, the DATAID object, the SID object and submissionTime object)

Tutorial Example Code:

// Initialize your database information

String URL = "database URL";

// for example, the MySQL database: URL = “jdbc:mysql://localhost:3306/”

String USERNAME = "database username";

String PASSWORD = "database password";

String DATABASENAME = "database name";

DatabaseInfo databaseInfo = new DatabaseInfo(URL,USERNAME,PASSWORD,DATABASENAME);



//Create a table modifier

String tableName = "table name";

GetDatabaseTableModifier getDatabaseTableModifier = GetDatabaseTableModifier.getInstance();

BatteryUsageTableModifier batteryUsageTableModifier = getDatabaseTableModifier.getBatteryUsageTableModifier(tableName);

CadenceTableModifier cadenceTableModifier = getDatabaseTableModifier.getCadenceTableModifier(tableName);

DistanceTableModifier distanceTableModifier = getDatabaseTableModifier.getDistanceTableModifier(tableName);

EETableModifier eeTableModifier = getDatabaseTableModifier.getEETableModifier(tableName);




//Select specific records

RecordSelectionInfo recordSelectionInfo = new RecordSelectionInfo(tableName);

SelectionValueObject selectionValueObject = new SelectionValueObject(“fieldName”, fieldValue);

recordSelectionInfo.euqals(selectionValueObject);

recordSelectionInfo.lessthan(selectionValueObject);

recordSelectionInfo.lessthanEquals(selectionValueObject);

recordSelectionInfo.greaterthan(selectionValueObject);

recordSelectionInfo.greaterthanEquals(selectionValueObject);



//Specify record update information

Map<String, String> map = new HashMap<>();

map.put("field name","update value");

map.put("field name","update value");

RecordUpdateInfo recordUpdateInfo = new RecordUpdateInfo(map);



//Modify a table

batteryUsageTableModifier.createTable();

TimeZonedTimestampedObject tztsObject;

BatteryUsageContainer batteryUsageContainer = (BatteryUsageContainer)tztsObject.getObject();

TimeZonedTimestamp tzts = tztsObject.getTimeZonedTimestamp();

UserIdentifierInterface UID;

batteryUsageTableModifier.insertRecord(batteryUsageContainer,tzts,UID);

batteryUsageTableModifier.updateRecord(recordSelectionInfo,recordUpdateInfo);

batteryUsageTableModifier.deleteRecord(recordSelectionInfo);

Set<StandardBTMDatabaseRecord> resSet = batteryUsageTableModifier.selectRecord(recordSelectionInfo);