QueryPredicate.h revision b6f76ebe
1//----------------------------------------------------------------------
2//  This software is part of the OpenBeOS distribution and is covered
3//  by the MIT License.
4//---------------------------------------------------------------------
5/*!
6	\file QueryPredicate.h
7	BQuery predicate helper classes interface declaration.
8*/
9#ifndef _QUERY_PREDICATE_H
10#define _QUERY_PREDICATE_H
11
12#include <stdio.h>
13
14#include <List.h>
15#include <Query.h>
16#include <String.h>
17
18namespace BPrivate {
19namespace Storage {
20
21// QueryNode
22class QueryNode {
23public:
24	QueryNode();
25	virtual ~QueryNode();
26
27	virtual uint32 Arity() const = 0;
28	virtual status_t SetChildAt(QueryNode *child, int32 index) = 0;
29	virtual QueryNode *ChildAt(int32 index) = 0;
30
31	virtual status_t GetString(BString &predicate) = 0;
32};
33
34// LeafNode
35class LeafNode : public QueryNode {
36public:
37	LeafNode();
38	virtual ~LeafNode();
39
40	virtual uint32 Arity() const;
41	virtual status_t SetChildAt(QueryNode *child, int32 index);
42	virtual QueryNode *ChildAt(int32 index);
43};
44
45// UnaryNode
46class UnaryNode : public QueryNode {
47public:
48	UnaryNode();
49	virtual ~UnaryNode();
50
51	virtual uint32 Arity() const;
52	virtual status_t SetChildAt(QueryNode *child, int32 index);
53	virtual QueryNode *ChildAt(int32 index);
54
55protected:
56	QueryNode	*fChild;
57};
58
59// BinaryNode
60class BinaryNode : public QueryNode {
61public:
62	BinaryNode();
63	virtual ~BinaryNode();
64
65	virtual uint32 Arity() const;
66	virtual status_t SetChildAt(QueryNode *child, int32 index);
67	virtual QueryNode *ChildAt(int32 index);
68
69protected:
70	QueryNode	*fChild1;
71	QueryNode	*fChild2;
72};
73
74// AttributeNode
75class AttributeNode : public LeafNode {
76public:
77	AttributeNode(const char *attribute);
78
79	virtual status_t GetString(BString &predicate);
80
81private:
82	BString	fAttribute;
83};
84
85// StringNode
86class StringNode : public LeafNode {
87public:
88	StringNode(const char *value, bool caseInsensitive = false);
89
90	virtual status_t GetString(BString &predicate);
91
92	inline const char *Value() const { return fValue.String(); }
93
94private:
95	BString	fValue;
96};
97
98// DateNode
99class DateNode : public LeafNode {
100public:
101	DateNode(const char *value);
102
103	virtual status_t GetString(BString &predicate);
104
105private:
106	BString	fValue;
107};
108
109// ValueNode
110template<typename ValueType>
111class ValueNode : public LeafNode {
112public:
113	ValueNode(const ValueType &value);
114
115	virtual status_t GetString(BString &predicate);
116
117private:
118	ValueType	fValue;
119};
120
121// constructor
122template<typename ValueType>
123ValueNode<ValueType>::ValueNode(const ValueType &value)
124					: LeafNode(),
125					  fValue(value)
126{
127}
128
129// GetString
130template<typename ValueType>
131status_t
132ValueNode<ValueType>::GetString(BString &predicate)
133{
134	predicate.SetTo("");
135	predicate << fValue;
136	return B_OK;
137}
138
139// specializations for float and double
140template<> status_t ValueNode<float>::GetString(BString &predicate);
141template<> status_t ValueNode<double>::GetString(BString &predicate);
142
143
144// short hands
145typedef ValueNode<int32>	Int32ValueNode;
146typedef ValueNode<uint32>	UInt32ValueNode;
147typedef ValueNode<int64>	Int64ValueNode;
148typedef ValueNode<uint64>	UInt64ValueNode;
149typedef ValueNode<float>	FloatValueNode;
150typedef ValueNode<double>	DoubleValueNode;
151
152
153// SpecialOpNode
154class SpecialOpNode : public LeafNode {
155public:
156	SpecialOpNode(query_op op);
157
158	virtual status_t GetString(BString &predicate);
159
160private:
161	query_op	fOp;
162};
163
164// UnaryOpNode
165class UnaryOpNode : public UnaryNode {
166public:
167	UnaryOpNode(query_op op);
168
169	virtual status_t GetString(BString &predicate);
170
171private:
172	query_op	fOp;
173};
174
175// BinaryOpNode
176class BinaryOpNode : public BinaryNode {
177public:
178	BinaryOpNode(query_op op);
179
180	virtual status_t GetString(BString &predicate);
181
182private:
183	query_op	fOp;
184};
185
186
187// QueryStack
188class QueryStack {
189public:
190	QueryStack();
191	virtual ~QueryStack();
192
193	status_t PushNode(QueryNode *node);
194	QueryNode *PopNode();
195
196	status_t ConvertToTree(QueryNode *&rootNode);
197
198private:
199	status_t _GetSubTree(QueryNode *&rootNode);
200
201private:
202	BList	fNodes;
203};
204
205};	// namespace Storage
206};	// namespace BPrivate
207
208#endif	// _QUERY_PREDICATE_H
209
210
211