package minidb.je;

import com.sleepycat.je.*;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

public class ExecuteHelpers {
    public static final boolean READ_ONLY = false;        //Temporarily made it false, insert was failing.
    public static final boolean READ_WRITE = false;

    public static File myDbEnvPath = new File("JEDB");

    public static boolean isTablePresent(Database relationDB, String relationName) {
        return isTablePresent(relationDB, relationName, new DatabaseEntry());
    }

    public static boolean isTablePresent(Database relationDB, String relationName, DatabaseEntry tempData) {

        DatabaseEntry theRelKey = null;
        try {
            theRelKey = new DatabaseEntry((relationName).getBytes("UTF-8"));
            relationDB.get(null, theRelKey, tempData, LockMode.DEFAULT);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        return tempData.getSize() != 0;
    }

    public static String getSelectData(String relationData) {
        String[] columnTypes = new String[relationData.split(",").length];
        StringBuffer displayString = new StringBuffer();
        String[] columnData = relationData.split(",");
        displayString.append(columnData[0]+ " (");
        for(int j = 1; j < columnData.length; j++) {
            columnTypes[j-1] = columnData[j].split(":")[1];
            displayString.append(columnData[j].split(":")[0]+",");
        }
        displayString.append(")");
        displayString.append("\n");

        String dbName = columnData[0]+"DB";
        try{
            ArrayList<String> tuples = ExecuteHelpers.getAllRowsOfTable(dbName, columnTypes);
            for(String s : tuples)
                displayString.append(s+"\n");
        } catch(DatabaseNotFoundException e){}
        return displayString.toString();
    }

    public static ArrayList<String> getAllRowsOfTable(String relation)
            throws DatabaseException {
        //Second param of getAllRowsOfTable is not yet implemented
        return getAllRowsOfTable(relation, new String[0]);
    }

    public static ArrayList<String> getAllRowsOfTable(String relation, String[] columnTypes)
            throws DatabaseException {
        ArrayList<String> tuples = new ArrayList<String>();
        MyDbEnv myDbEnv = new MyDbEnv();
        myDbEnv.setup(myDbEnvPath, READ_ONLY);

        // Get a cursor
        Database database = myDbEnv.getDB(relation, READ_ONLY);
        Cursor cursor = database.openCursor(null, null);

        // DatabaseEntry objects used for reading records
        DatabaseEntry foundKey = new DatabaseEntry();
        DatabaseEntry foundData = new DatabaseEntry();

        try { // always want to make sure the cursor gets closed
            while (cursor.getNext(foundKey, foundData,
                    LockMode.DEFAULT) == OperationStatus.SUCCESS) {
                String key =  new String(foundKey.getData(), "UTF-8");
                String data =  new String(foundData.getData(), "UTF-8").replaceAll("&&",",");

                tuples.add(data);
            }
        } catch (Exception e) {
            System.err.println("Error on relation cursor:");
            System.err.println(e.toString());
            e.printStackTrace();
        } finally {
            cursor.close();
            database.close();
            myDbEnv.close();
        }
        return tuples;
    }

}

@@ -134,16 +134,39 @@
 rayList;
+%0Aimport java.util.List;
 %0A%0Apublic
@@ -1025,24 +1025,148 @@
  != 0;%0A    %7D
+%0A    public static List%3CString%3E getSelectData(String relationData) %7B%0A        return getSelectData(relationData, null);%0A    %7D
 %0A%0A    public
@@ -1173,22 +1173,28 @@
  static 
+List%3C
 String
+%3E
  getSele
@@ -1211,32 +1211,54 @@
 ing relationData
+, List%3CString%3E columns
 ) %7B%0A        Stri
@@ -1320,16 +1320,78 @@
 ength%5D;%0A
+        List%3CString%3E displayList = new ArrayList%3CString%3E();%0A//
         
@@ -1513,44 +1513,259 @@
 play
-String.append(columnData%5B0%5D+ %22 (%22);%0A
+List.add(relationData);%0A//        displayString.append(columnData%5B0%5D+ %22 (%22);%0A//        String SEPARATOR = %22%22;%0A//        int column_size = columns == null ? columnData.length-1 : columns.size();%0A//        int column_indices%5B%5D = new int%5Bcolumn_size%5D;%0A//
     
@@ -1809,24 +1809,26 @@
 gth; j++) %7B%0A
+//
             
@@ -1875,16 +1875,18 @@
 :%22)%5B1%5D;%0A
+//
         
@@ -1902,32 +1902,44 @@
 ayString.append(
+SEPARATOR + 
 columnData%5Bj%5D.sp
@@ -1953,15 +1953,81 @@
 )%5B0%5D
-+
+);%0A//            if(columns == null) %7B%7D%0A//            SEPARATOR = 
 %22,%22
-)
 ;%0A
+//
     
@@ -2024,32 +2024,34 @@
 ,%22;%0A//        %7D%0A
+//
         displayS
@@ -2065,24 +2065,26 @@
 ppend(%22)%22);%0A
+//
         disp
@@ -2290,24 +2290,62 @@
 s : tuples)%0A
+                displayList.add(s);%0A//
             
@@ -2451,25 +2451,12 @@
 play
-String.toString()
+List
 ;%0A  
@@ -3632,29 +3632,8 @@
 -8%22)
-.replaceAll(%22&&%22,%22,%22)
 ;%0A%0A 

Clean the print part