Skip to content

Commit

Permalink
Implement formatted select
Browse files Browse the repository at this point in the history
  • Loading branch information
yeonwooKim committed Nov 24, 2016
1 parent 1380bd0 commit b92ed1a
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 26 deletions.
17 changes: 10 additions & 7 deletions src/Berkeley.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.sleepycat.je.*;

import java.io.*;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Iterator;

Expand Down Expand Up @@ -361,14 +362,18 @@ private boolean incrCursor(String[] tables, ArrayList<Cursor> cursors, ArrayList
}

public void select(String tablename, BooleanValueExpression bve, ArrayList<Integer> projection) {
Table t = DBManager.getDBManager().findTable(tablename);
ArrayList<String> names = t.getNames(projection);
MessagePrinter.selectionSucceeded(names);

String[] tables = tablename.split("@");
ArrayList<Cursor> cursors = new ArrayList<>();
ArrayList<DatabaseEntry> datas = new ArrayList<>();
try {
for (int i = 0; i < tables.length; i++) {
Cursor cursor = db.openCursor(null, null);
DatabaseEntry key;
DatabaseEntry data = null;
DatabaseEntry data;
key = new DatabaseEntry(tables[i].getBytes("UTF-8"));
data = new DatabaseEntry();
OperationStatus os = cursor.getSearchKey(key, data, LockMode.DEFAULT);
Expand All @@ -387,20 +392,18 @@ public void select(String tablename, BooleanValueExpression bve, ArrayList<Integ
}
Record newRecord = new Record(arr);
if (bve == null || bve.eval(DBManager.getDBManager().findTable(tablename), newRecord)) {
//TODO: print these values
ArrayList<Value> res = (projection == null) ? newRecord.getValues() : newRecord.getIndices(projection);
Iterator<Value> itPrint = res.iterator();
while (itPrint.hasNext()) {
itPrint.next().print();
}
System.out.println();
MessagePrinter.selectionRecordPrint(res);
}
} while(incrCursor(tables, cursors, datas, tables.length));

MessagePrinter.selectionEnded(names);
Iterator<Cursor> it = cursors.iterator();
while (it.hasNext()) {
it.next().close();
}
} catch (Exception e) {
MessagePrinter.selectionEnded(names);
Iterator<Cursor> it = cursors.iterator();
while (it.hasNext()) {
it.next().close();
Expand Down
10 changes: 0 additions & 10 deletions src/DBManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,6 @@ public Table findTable(String tablename) {
return null;
}

public Table findTableAlias(String alias) {
Iterator<Table> it = tables.iterator();
while (it.hasNext()) {
Table n = it.next();
if (n.getAlias().equals(alias))
return n;
}
return null;
}

public void addTable(Table t) {
tables.add(t);
} // Called when CREATE TABLE requested
Expand Down
30 changes: 30 additions & 0 deletions src/MessagePrinter.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import java.util.ArrayList;
import java.util.Iterator;

public class MessagePrinter
{
public static void printPrompt()
Expand All @@ -14,6 +17,33 @@ private static void dropTableFailed() {
private static void selectionFailed() {
System.out.print("Selection has failed: ");
}
public static void selectionRecordPrint(ArrayList<Value> arr) {
Iterator<Value> it = arr.iterator();
while (it.hasNext()) {
System.out.printf("| %-19s", it.next().toString());
}
System.out.println("|");
}
public static void selectionSucceeded(ArrayList<String> projection) {
int size = projection.size();
for (int i = 0 ; i < size ; i ++)
System.out.print("+--------------------");
System.out.println("+");
Iterator<String> it = projection.iterator();
while (it.hasNext()) {
System.out.printf("| %-19s", it.next().toUpperCase());
}
System.out.println("|");
for (int i = 0 ; i < size ; i ++)
System.out.print("+--------------------");
System.out.println("+");
}
public static void selectionEnded(ArrayList<String> projection) {
int size = projection.size();
for (int i = 0 ; i < size ; i ++)
System.out.print("+--------------------");
System.out.println("+");
}
public static void printMessage(Message m)
{
MessageName q = m.getMessagename();
Expand Down
24 changes: 24 additions & 0 deletions src/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -229,4 +229,28 @@ public void setAlias(String alias) {
public String getAlias() {
return alias;
}

private String getName(int index) {
Attribute attr = attrList.get(index);
String name = attr.getAlias();
if (name != null)
return name;
return attr.getAttributeName();
}
public ArrayList<String> getNames(ArrayList<Integer> projection) {
ArrayList<String> names = new ArrayList<>();
if (projection == null) {
int size = attrList.size();
for (int i = 0 ; i < size ; i ++) {
names.add(getName(i));
}
}
else {
Iterator<Integer> it = projection.iterator();
while (it.hasNext()) {
names.add(getName(it.next()));
}
}
return names;
}
}
15 changes: 6 additions & 9 deletions src/Value.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,22 +90,19 @@ public boolean equals(Object o) {
return false;
}

public void print() {
public String toString() {
if (typename == null) {
System.out.print("NULL ");
return;
return "NULL";
}
switch(typename) {
case INT:
System.out.print(intVal + " ");
break;
return Integer.toString(intVal);
case CHAR:
System.out.print(stringVal + " ");
break;
return stringVal;
case DATE:
System.out.print(dateVal + " ");
break;
return dateVal;
}
return null;
}

@Override
Expand Down

0 comments on commit b92ed1a

Please sign in to comment.