com.alarexgroup.m2mplf.db
Class Database

java.lang.Object
  extended by com.alarexgroup.m2mplf.db.Database
public final class Database
extends java.lang.Object

Database object collects tables and executes their initialisation, opening and closing.

import com.alarexgroup.m2mplf.db.Database;
import com.alarexgroup.m2mplf.db.Table;
import com.alarexgroup.m2mplf.db.TableRecord;

public class DatabaseDemo {
    
    Database datab;
    Table lide;
    
    //definition of table structure
    //table will have columns - name, surname, number of inputs
     final static byte [] lide_struct = {TableRecord.TYPE_STRING, TableRecord.TYPE_STRING, TableRecord.TYPE_INT};
    //For easier work with columns we will create constancy, which defines position of column.
     final static int PEOPLE_NAME0;
     final static int PEOPLE_SURNAME1;
     final static int PEOPLE_INPUT;2;
    //will be two indexes
    //first index will have structure - surname and name
    //second index will have sturcture - number of inputs, surname, name
    //numbers mark columns in table
    byte [][] people_indexes_struct = {{PEOPLE_SURNAME, PEOPLE_NAME},{PEOPLE_INPUT,PEOPLE_SURNAME,PEOPLE_NAME}};
    
    public DatabaseDemo() {
        datab = new Database("pristupy");
        //Is created table of people according to structure noted above.
        peopledatab.createTable("people",people_struct,people_indexes_struct);
        //database initilisation, into table dates loding
        datab.initTables();
    }
    
    public void addPerson(String name, String surname){
        //we create record from table people
        TableRecord r = lide.createRecord();
        //we fill record
        r.setColumnAsString(PEOPLE_NAME, name);
        r.setColumnAsString(PEOPLE_SURNAME, surname);
        //we set number of inputs on 0
        r.setColumnAsInt(PEOPLE_INPUT,0);
        //we insert full row into table, duplicate records are not allowed
        lide.addRecord(r,false);
        //We secure saving into FLASH memory. This command is independent, that is why we could do more changes in table, but without saving into FLASH memory(slow access).
        people.flush();
    }
    
    public void newInput(String name, String surname){
        
        //we create incomplete record, what we are going to look for in the table.
        TableRecord r = people.createRecord();
        //we fill the record
        r.setColumnAsString(PEOPLE_NAME, name);
        r.setColumnAsString(PEOPLE_SURNAME, surname);
        // we search according to first index surname, name
        TableRecord s = people.findRecord(r,0);
        if (s!=null) {
            //record found
            //we erease row
            people.deleteRecord(s);
            //Becase we erased someting, inexes need optimalisation. We do not need process repackIndexes after every single erasing. 
            people.repackIndexes();
            //we adjust values in TaleRecord
            s.setColumnAsInt(PEOPLE_INPUT,s.getColumnAsInt(PEOPLE_INPUT)+1);
            //we save it as a new row
            people.addRecord(s,false);
            //we esure saving from memory to FLASH
            people.flush();
        }         
               
    }
    
    
    public int getInputs(String name, String surname){
        int inputs = -1;
        // we create incomplete record, what we are going to look for in the table.
        TableRecord r = people.createRecord();
        //we fill the record
        r.setColumnAsString(PEOPLE_NAME, name);
        r.setColumnAsString(PEOPLE_SURNAME, surname);
        //we search according to first index surname, name
        TableRecord s = people.findRecord(r,0);
        if (s!=null) {
            //record found
            inputs = s.getColumnAsInt(PEOPLE_INPUT);
        }         
        return inputs;       
    }
    
    
    
    public String[][] printByAcces(){
        String[][] list = null;
        TableRecord r;
        //we create selection from table according to index1 (inputs, surname, name)
        //protože chceme všechny, nedáváme omezující podmínky
        int num = lide.setQuery(1,null,null);
        if (num>0) {
            //vytvoříme seznam
            seznam = new String[num][3];
            for (int i=0;i<num;i++) {
                r = lide.getNextRecord();
                seznam[i][0= r.getColumnAsString(LIDE_JMENO);
                seznam[i][1= r.getColumnAsString(LIDE_PRIJMENI);
                seznam[i][2""+r.getColumnAsString(LIDE_PRISTUPY);
            }
        }
        return seznam;        
    }
    
    
    
}

Constructor Summary
Database(java.lang.String name)
           
 
Method Summary
 void closeDatabase()
          Finishes work with database - closes tables.
 Table createTable(java.lang.String name, byte[] struct, byte[][] indexStruct)
          Creates table in database
 long getSize()
          Full FLASH memory - in bytes
 long getSizeAvailable()
          Free FLASH memory - for database
 Table getTable(java.lang.String name)
          Returns reference to table in database according to name
 void initTables()
          Database initialisation - carries out structure initialisation in memory and FLASH memory.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Database

public Database(java.lang.String name)
Method Detail

getSize

public long getSize()
Full FLASH memory - in bytes

Returns:
actual size of database

getSizeAvailable

public long getSizeAvailable()
Free FLASH memory - for database

Returns:
size of free memory - in bytes

createTable

public Table createTable(java.lang.String name,
                         byte[] struct,
                         byte[][] indexStruct)
Creates table in database

Parameters:
name - jméno tabulky
struct - definice struktury tabulky
indexStruct - definice struktury indexu
Returns:
objekt tabulky

initTables

public void initTables()
Database initialisation - carries out structure initialisation in memory and FLASH memory. Reads saved data from existing tables.

getTable

public Table getTable(java.lang.String name)
Returns reference to table in database according to name

Parameters:
name - of table
Returns:
reference to table

closeDatabase

public void closeDatabase()
Finishes work with database - closes tables.