package simpledb.test;

import simpledb.*;
import java.util.*;
import java.io.*;

// this is the test from lab1.html
public class DemoTest extends Test {
    public boolean runTest(String args[]) {
	
	// construct a 3-column table schema
	Type typeAr[] = new Type[3];
	typeAr[0] = Type.INT_TYPE;
	typeAr[1] = Type.INT_TYPE;
	typeAr[2] = Type.INT_TYPE;
	TupleDesc t = new TupleDesc(typeAr);

	// create the table, associate it with some_data_file.dat
	// and tell the catalog about the schema of this table.
	HeapFile table1 = new HeapFile(new File("some_data_file.dat"));
	Database.getCatalog().addTable(table1, t);
    
	// construct the query: we use a simple SeqScan, which spoonfeeds
	// tuples via its iterator.
	TransactionId tid = new TransactionId();
	SeqScan f = new SeqScan(tid, table1.id());

	Query q = new Query(f, tid);

	// and run it
	try {
	    
	    q.start();

	    try {
		while (true) {
		    Tuple tup = q.getNext();
		    tup.print();
		}
	    } catch(NoSuchElementException e) {
		f.close();
	    }
	} catch (TransactionAbortedException e) {
	    e.printStackTrace();

	} catch (DbException e) {
	    e.printStackTrace();
	    return false;
	} catch (IOException e) {
	    e.printStackTrace();
	    return false;
	}
	
	return true;
    }
}
