package com.nigorojr.typebest;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedHashMap;

public class Records extends Database {

    public static final String tableName = "RECORDS";

    private static LinkedHashMap<String, String> columnNamesAndTypes = new LinkedHashMap<String, String>() {
        {
            put("RECORD_ID",
                    "INT NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1)");
            put("USER_ID", "BIGINT NOT NULL");
            put("USERNAME", "VARCHAR(100) NOT NULL");
            put("KEYBOARD_LAYOUT", "VARCHAR(100)");
            put("TIME", "BIGINT");
            put("MISS_TYPES", "INT");
            put("DATE", "TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP");
        }
    };

    public Records() throws SQLException {
        super(tableName, columnNamesAndTypes, "RECORD_ID");
    }

    /**
     * Adds a new record to the database.
     * 
     * @param id
     *            The user ID determined by the PREFERENCES table. This is not
     *            used as a unique key for this RECORDS table.
     * @param username
     *            The username used when the record was achieved.
     * @param keyboardLayout
     *            The keyboard layout that was used when the record was
     *            achieved.
     * @param time
     *            The time it took to finish in nanoseconds.
     * @param miss
     *            The number of incorrect keystrokes.
     */
    public void addNewRecord(long id, String username, String keyboardLayout,
            long time, int miss) {
        LinkedHashMap<String, String> columnNamesAndValues = new LinkedHashMap<String, String>();
        columnNamesAndValues.put("USER_ID", Long.toString(id));
        columnNamesAndValues.put("USERNAME", String.format("'%s'", username));
        columnNamesAndValues.put("KEYBOARD_LAYOUT",
                String.format("'%s'", keyboardLayout));
        columnNamesAndValues.put("TIME", Long.toString(time));
        columnNamesAndValues.put("MISS_TYPES", Integer.toString(miss));
        super.insert(columnNamesAndValues);
    }

    /**
     * Retrieves all the records that are in the table.
     * 
     * @return All the records in the table. Each record is stored using Record
     *         class.
     */
    public Record[] getAllRecords() {
        ArrayList<Record> records = new ArrayList<Record>();
        ResultSet queryResult = super.select(new String[] { "*" });
        try {
            while (queryResult.next()) {
                Record r = new Record();
                r.user_id = queryResult.getLong("USER_ID");
                r.username = queryResult.getString("USERNAME");
                r.keyboardLayout = queryResult.getString("KEYBOARD_LAYOUT");
                r.time = queryResult.getLong("TIME");
                r.miss = queryResult.getInt("MISS_TYPES");

                records.add(r);
            }
        }
        catch (SQLException e) {
            e.printStackTrace();
        }
        Record[] ret = new Record[records.size()];
        return records.toArray(ret);
    }
}
@@ -2387,126 +2387,589 @@
     
-ArrayList%3CRecord%3E records = new ArrayList%3CRecord%3E(
+ResultSet queryResult = super.select(new String%5B%5D %7B %22*%22 %7D);%0A        return resultSetToRecordArray(queryResult
 );%0A    
+%7D%0A%0A
     
-ResultSet queryResult = super.select(new String%5B%5D %7B %22*%22 %7D
+/**%0A     * Converts the given ResultSet object to an array of Record, which is a%0A     * collection of information such as the time, miss-types, username etc.%0A     * %0A     * @param resultSet%0A     *            A ResultSet object containing the result of a query.%0A     * @return An array of Record converted from the ResultSet.%0A     */%0A    private Record%5B%5D resultSetToRecordArray(ResultSet resultSet) %7B%0A        ArrayList%3CRecord%3E records = new ArrayList%3CRecord%3E(
 );%0A 
@@ -3000,27 +3000,25 @@
  while (
-queryR
+r
 esult
+Set
 .next())
@@ -3089,27 +3089,25 @@
 er_id = 
-queryR
+r
 esult
+Set
 .getLong
@@ -3144,35 +3144,33 @@
 .username = 
-queryR
+r
 esult
+Set
 .getString(%22
@@ -3216,27 +3216,25 @@
 ayout = 
-queryR
+r
 esult
+Set
 .getStri
@@ -3281,27 +3281,25 @@
 .time = 
-queryR
+r
 esult
+Set
 .getLong
@@ -3333,27 +3333,25 @@
 .miss = 
-queryR
+r
 esult
+Set
 .getInt(

Add new helper method that converts ResultSet to array of Record