Skip to content

Commit

Permalink
Implement select
Browse files Browse the repository at this point in the history
* Does not support formatted print.
* Need to check aliasing and case ignoring.
  • Loading branch information
yeonwooKim committed Nov 23, 2016
1 parent 365f822 commit f255086
Show file tree
Hide file tree
Showing 10 changed files with 577 additions and 128 deletions.
8 changes: 7 additions & 1 deletion src/Attribute.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ public class Attribute implements Serializable {
private int foreignKey;
private boolean isNotNull;
private int index;
private String tablename;
private String tablename = null;
private String tableAlias = null;
private String alias = null;

public Attribute(Type attrType, String attrName) {
this.attrType = attrType;
Expand Down Expand Up @@ -44,6 +46,8 @@ public int getForeignKey() {
}

public String getTablename() { return tablename; }
public String getTableAlias() { return tableAlias; }
public String getAlias() { return alias; }

public void setForeignKey(int f) {
foreignKey = f;
Expand All @@ -60,6 +64,8 @@ public void setNotNull() {
public void setIndex(int index) { this.index = index; }

public void setTablename(String tablename) { this.tablename = tablename; }
public void setTableAlias(String tableAlias) { this.tableAlias = tableAlias; }
public void setAlias(String alias) { this.alias = alias; }

public int getIndex() { return index; }
}
95 changes: 85 additions & 10 deletions src/Berkeley.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,10 @@ public void insertRecord(String tablename, Record rec) {
private static boolean recordHasValue(DatabaseEntry data, ArrayList<Integer> index, ArrayList<Value> values) {
try {
Record rec = (Record) deserialize(data.getData());
Iterator<Integer> it = index.iterator();
int i = 0;
while (it.hasNext()) {
if (!values.get(i).equals(rec.getIndex(it.next())))
return false;
i ++;
}
return true;
ArrayList<Value> values1 = rec.getIndices(index);
if (values1.equals(values))
return true;
return false;
} catch (Exception e) {
return false;
}
Expand Down Expand Up @@ -196,13 +192,21 @@ private void cascadeToNull(String tablename, ArrayList<Integer> index, ArrayList
return;
}
Record rec = (Record) deserialize(data.getData());
if (rec.getIndices(index).equals(values));
if (rec.getIndices(index).equals(values)) {
recordToNull(rec, index);
cursor.delete();
data = new DatabaseEntry(serialize(rec));
db.put(null, key, data);
}

while (cursor.get(key, data, Get.NEXT_DUP, null) != null) {
rec = (Record) deserialize(data.getData());
if (rec.getIndices(index).equals(values))
if (rec.getIndices(index).equals(values)) {
recordToNull(rec, index);
cursor.delete();
data = new DatabaseEntry(serialize(rec));
db.put(null, key, data);
}
}
cursor.close();
} catch (Exception e) {
Expand Down Expand Up @@ -332,4 +336,75 @@ public Message removeRecord(String tablename, BooleanValueExpression bve) {
return null;
}
}

private boolean incrCursor(String[] tables, ArrayList<Cursor> cursors, ArrayList<DatabaseEntry> datas, int size) {
DatabaseEntry key = new DatabaseEntry(), data = new DatabaseEntry();
if (cursors.get(size - 1).get(key, data, Get.NEXT_DUP, null) != null) {
datas.set(size - 1, data);
return true;
}
else {
if (size == 1) {
return false;
}
else {
try {
key = new DatabaseEntry(tables[size - 1].getBytes("UTF-8"));
cursors.get(size - 1).getSearchKey(key, data, LockMode.DEFAULT);
datas.set(size - 1, data);
return incrCursor(tables, cursors, datas, size - 1);
} catch (Exception e) {
return false;
}
}
}
}

public void select(String tablename, BooleanValueExpression bve, ArrayList<Integer> projection) {
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;
key = new DatabaseEntry(tables[i].getBytes("UTF-8"));
data = new DatabaseEntry();
OperationStatus os = cursor.getSearchKey(key, data, LockMode.DEFAULT);
if (os != OperationStatus.SUCCESS) {
throw new Exception();
}
cursors.add(cursor);
datas.add(data);
}
do {
ArrayList<Value> arr = new ArrayList<>();
Iterator<DatabaseEntry> it = datas.iterator();
while (it.hasNext()) {
Record rec = (Record) deserialize(it.next().getData());
arr.addAll(rec.getValues());
}
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();
}
} while(incrCursor(tables, cursors, datas, tables.length));
Iterator<Cursor> it = cursors.iterator();
while (it.hasNext()) {
it.next().close();
}
} catch (Exception e) {
Iterator<Cursor> it = cursors.iterator();
while (it.hasNext()) {
it.next().close();
}
}
}
}
10 changes: 10 additions & 0 deletions src/DBManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ 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().equalsIgnoreCase(alias))
return n;
}
return null;
}

public void addTable(Table t) {
tables.add(t);
} // Called when CREATE TABLE requested
Expand Down
7 changes: 5 additions & 2 deletions src/MessageName.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@ public enum MessageName {
INSERT_COLUMN_EXISTENCE, // column name
INSERT_DUPLICATE_PRIMARY_KEY,
INSERT_REFERENTIAL_INTEGRITY,
SELECT, INSERT_SUCCESS,
SELECT_SUCCESS,
INSERT_SUCCESS,
DELETE_SUCCESS,
WHERE_INCOMPARABLE,
WHERE_TABLE_NOT_SPECIFIED,
WHERE_COLUMN_NOT_EXIST,
WHERE_AMBIGUOUS_REFERENCE
WHERE_AMBIGUOUS_REFERENCE,
SELECT_TABLE_EXISTENCE_ERROR,
SELECT_COLUMN_RESOLVE_ERROR
}
15 changes: 13 additions & 2 deletions src/MessagePrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ private static void dropTableFailed() {
System.out.print("Drop table has failed: ");
}
private static void insertionFailed() { System.out.print("Insertion has failed: "); }
private static void selectionFailed() {
System.out.print("Selection has failed: ");
}
public static void printMessage(Message m)
{
MessageName q = m.getMessagename();
Expand Down Expand Up @@ -91,9 +94,17 @@ public static void printMessage(Message m)
System.out.println("Char length should be over 0");
break;

case SELECT:
System.out.println("\'SELECT\' requested");
case SELECT_SUCCESS:
break;
case SELECT_COLUMN_RESOLVE_ERROR:
selectionFailed();
System.out.println("fail to resolve \'[" + n + "]\'");
break;
case SELECT_TABLE_EXISTENCE_ERROR:
selectionFailed();
System.out.println("\'[" + n + "]\' does not exist");
break;

case INSERT_SUCCESS:
System.out.println("The row is inserted");
break;
Expand Down
8 changes: 8 additions & 0 deletions src/Record.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ public Record() {
values = new ArrayList<>();
}

public Record(ArrayList<Value> values) {
this.values = values;
}

public Value getIndex(int i) {
return values.get(i);
}
Expand All @@ -26,6 +30,10 @@ public ArrayList<Value> getIndices(ArrayList<Integer> arr) {
return ret;
}

public ArrayList<Value> getValues() {
return values;
}

public void setIndex(int i, Value v) { values.set(i, v); }

private static void addForeignKeyList(ArrayList<ArrayList<Value>> foreignKey, int index, Value v,
Expand Down
Loading

0 comments on commit f255086

Please sign in to comment.