00001
#ifndef SQLUTIL_H
00002
#define SQLUTIL_H
00003
00004
#include "Expression.h"
00005
00006
using namespace Borealis;
00007
00008 struct SQLContext
00009 {
00010 SQLContext(
const ExprContext &ec, string tname) :
00011
ctxt(ec),
table_name(tname) {}
00012 bool isTableField(string field)
const
00013
{
00014 TupleDescription td;
00015
unsigned int index;
00016
ctxt.
getNamedTupleInfo(
table_name, td, index);
00017
return (td.indexOfField(field) != -1);
00018 }
00019
00020 const ExprContext &
ctxt;
00021 string
table_name;
00022 };
00023
00024 class SQLWhereClause
00025 {
00026
public:
00027 enum Type
00028 {
00029
EXPRESSION,
00030
FIELD_EQ_EXPRESSION,
00031
CONJUNCTION
00032 };
00033
00034 class SubClause
00035 {
00036
public:
00037
virtual Type
getType()
const = 0;
00038
virtual ptr<Expression>
asExpression(
const ExprContext &ctxt)
const = 0;
00039 virtual ~SubClause() {}
00040 virtual string
as_string()
const {
return "SubClause(UNKNOWN)"; }
00041 };
00042
00043 class ExprSubClause :
public SubClause
00044 {
00045
public:
00046 ExprSubClause(ptr<Expression> expr) :
00047 _type(EXPRESSION), _expr(expr)
00048 {
00049
00050
if (_expr && _expr->getType() != DataType::BOOL)
00051
Throw(AuroraTypingException,
00052
"Non-boolean where sub clause: " +
to_string(_expr));
00053 }
00054
00055 virtual ~ExprSubClause() {}
00056 virtual Type
getType()
const {
return _type; }
00057 virtual ptr<Expression>
asExpression(
const ExprContext &ctxt)
const
00058
{
return _expr; }
00059 virtual string
as_string()
const {
return (
"ExprSubClause(" +
00060
to_string(_expr) +
00061
")"); }
00062
private:
00063 Type _type;
00064 ptr<Expression> _expr;
00065 };
00066
00067
00068 class FieldEqSubClause :
public SubClause
00069 {
00070
public:
00071 FieldEqSubClause(string table_name, string field_name,
00072 ptr<Expression> value) :
00073 _type(FIELD_EQ_EXPRESSION), _table_name(table_name),
00074 _field_name(field_name), _value(value) {}
00075 virtual ~FieldEqSubClause() {}
00076 virtual Type
getType()
const {
return _type; }
00077
virtual ptr<Expression>
asExpression(
const ExprContext &ctxt)
const;
00078 string
getFieldName()
const {
return _field_name; }
00079 ptr<Expression>
getFieldValue()
const {
return _value; }
00080 string
as_string()
const {
return (
"FieldEqSubClause(" + _field_name +
00081
"==" +
to_string(_value) +
")");}
00082
00083
private:
00084 Type _type;
00085 string _table_name, _field_name;
00086 ptr<Expression> _value;
00087 };
00088
00089 class ConjunctionSubClause :
public SubClause
00090 {
00091
public:
00092 ConjunctionSubClause(ptr<SubClause> l, ptr<SubClause> r) : _type(CONJUNCTION)
00093 {
00094 addClause(l);
00095 addClause(r);
00096 }
00097
00098 virtual ~ConjunctionSubClause() {}
00099 virtual Type
getType()
const {
return _type; }
00100
virtual ptr<Expression>
asExpression(
const ExprContext &ctxt)
const;
00101 const vector<ptr<SubClause> > &
getSubClauses()
const {
return _clauses; }
00102 virtual string
as_string()
const {
return "ConjunctionSubClause(" +
00103
to_string(_clauses) +
")"; }
00104
private:
00105
void addClause(ptr<SubClause> c);
00106 Type _type;
00107 vector<ptr<SubClause> > _clauses;
00108 };
00109
00110 SQLWhereClause(
const ExprContext &ctxt, ptr<SubClause> sub_clause) :
00111 _ctxt(ctxt), _sub_clause(sub_clause) {}
00112
00113 ptr<Expression>
asExpression()
const
00114
{
00115
return _sub_clause->asExpression(_ctxt);
00116 }
00117
00118 ptr<SubClause>
getSubClause()
const {
return _sub_clause; }
00119
00120
00121 string
as_string()
const {
return (
"WhereClause(" +
00122
to_string(_sub_clause) +
00123
")"); }
00124
private:
00125
const ExprContext _ctxt;
00126 ptr<SubClause> _sub_clause;
00127 };
00128
00129
#endif // SQLUTIL_H