[go: up one dir, main page]

Menu

[r9]: / sql / sql.h  Maximize  Restore  History

Download this file

193 lines (146 with data), 3.9 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#ifndef __QUERY_H__
#define __QUERY_H__
#include <vector>
#include <string>
#include <iostream>
using namespace std;
enum OP {ATOM, EQ, LT, LE, GT, GE, NE, AND, OR, NOT, ADD, SUB, MUL, DIV, NEG, EXP};
enum OT {UNKNOWN, ATTR, STRING, INT, FLOAT};
class CCond
{
public:
CCond()
{
}
~CCond()
{
}
public:
bool evaluate()
{
switch (m_OP)
{
case EQ:
//return m_pLeft->evaluate() == m_pRight->evaluate();
case LT:
//return m_pLeft->evaluate() < m_pRight->evaluate();
case LE:
//return m_pLeft->evaluate() <= m_pRight->evaluate();
case GT:
//return m_pLeft->evaluate() > m_pRight->evaluate();
case GE:
//return m_pLeft->evaluate() >= m_pRight->evaluate();
case NE:
//return m_pLeft->evaluate() != m_pRight->evaluate();
default:
// error, throw exception
return true;
}
return true;
}
public:
OP m_OP;
CCond* m_pLeft;
CCond* m_pRight;
OT m_OT;
string m_strVal;
int m_intVal;
float m_floatVal;
string m_strAttr;
};
struct CQueryAttr
{
vector<string> m_vTableList;
vector<string> m_vAttrList;
CCond m_Cond;
};
struct CQType
{
string m_strVal;
vector<string> m_vstrVal;
CQueryAttr m_queryVal;
CCond m_Cond;
};
class CParser
{
public:
static CQType addTable(const CQType& t)
{
cout << "add table " << t.m_strVal << endl;
CQType al;
al.m_vstrVal.insert(al.m_vstrVal.end(), t.m_strVal);
return al;
}
static CQType addAttr(const CQType& a)
{
cout << "add attr " << " " << a.m_strVal << endl;
CQType al;
al.m_vstrVal.insert(al.m_vstrVal.end(), a.m_strVal);
return al;
}
static CQType addAttr(const CQType& a, CQType& al)
{
cout << "add attr " << " " << a.m_strVal << " " << al.m_vstrVal.size() << endl;
al.m_vstrVal.insert(al.m_vstrVal.end(), a.m_strVal);
return al;
}
static CQType finalizeQuery(const CQType& attrlist, const CQType& table)
{
cout << "finalize " << table.m_vstrVal.size() << " " << attrlist.m_vstrVal.size() << endl;
CQType q;
q.m_queryVal.m_vTableList = table.m_vstrVal;
q.m_queryVal.m_vAttrList = attrlist.m_vstrVal;
return q;
}
static CQType finalizeQuery(const CQType& attrlist, const CQType& table, const CQType& cond)
{
cout << "finalize " << table.m_vstrVal.size() << " " << attrlist.m_vstrVal.size() << endl;
CQType q;
q.m_queryVal.m_vTableList = table.m_vstrVal;
q.m_queryVal.m_vAttrList = attrlist.m_vstrVal;
q.m_queryVal.m_Cond = cond.m_Cond;
return q;
}
static CQType addCond(const OP& op, const OT& ot, const CQType& expr)
{
CQType q;
q.m_Cond.m_OP = op;
q.m_Cond.m_OT = ot;
q.m_Cond.m_pLeft = new CCond;
q.m_Cond.m_pLeft->m_OP = ATOM;
if (op == ATOM)
{
switch (ot)
{
case ATTR:
q.m_Cond.m_strAttr = expr.m_strVal;
break;
case STRING:
q.m_Cond.m_strVal = expr.m_strVal;
break;
case INT:
q.m_Cond.m_intVal = atoi(expr.m_strVal.c_str());
break;
case FLOAT:
q.m_Cond.m_floatVal = atof(expr.m_strVal.c_str());
break;
default:
// error, throw exception
break;
}
}
q.m_Cond.m_pRight = new CCond (expr.m_Cond);
return q;
}
static CQType addCond(const OP& op, const OT& ot, const CQType& left, const CQType& right)
{
CQType q;
q.m_Cond.m_OP = op;
q.m_Cond.m_OT = ot;
q.m_Cond.m_pLeft = new CCond(left.m_Cond);
q.m_Cond.m_pRight = new CCond(right.m_Cond);
return q;
}
static CQueryAttr* parse(const char* qt, const int& len);
};
#endif