/********************************************************************************
*
* File: main.cc
* Title: The main program for the OQL optimizer
* Programmer: Leonidas Fegaras, UTA
* Date: 4/10/97
*
********************************************************************************/

#include <stream.h>
#include <string.h>
#include "external.h"
#include "evaluation.h"
#include "typecheck.h"

const evaluate_plans = true;

Expr optimize_query (Expr query);

parse_sql();

extern database* DATABASE;


/* Compile, optimize, and execute a query */
query ( Expr e ) {
  cout << "\n================================================================================\n\nQuery: ";
  e->pretty_print(7);
  Expr ne = convert(e);
  cout << endl;
  cout << "\nIn comprehension form: ";
  ne->pretty_print(23);
  cout << endl;
  cout << "\nIt has type: ";
  Schema lvars = new binding<Expr>;
  eliminate_type_vars(typecheck(ne,DATABASE->vartypes,lvars))->pretty_print(13);
  cout << endl;
  Expr q = optimize_query(eliminate_type_vars(ne));
  cout << "\nA physical plan is: ";
  q->pretty_print(20);
  cout << endl;
  if (evaluate_plans)
  { if (trace_evaluation)
        cout << "\n--------------------------------------------------------------------------------\n";
    Object o = evaluate_plan(q,DATABASE->varspace);
    cout << "\nThe result is: ";
    o->reify(0)->pretty_print(15);
  };
  cout << endl;
};



int main (int argc,char* arg[]) {
try{
  ifstream from("database");
  if (!from) DATABASE = new database();
  else { from.close(); DATABASE = new database(new string("database")); };
  parse_sql();
  DATABASE->close(new string("database"));
} catch (object::evaluation_exception) {}
  catch (type_exception) {};
};
