xref: /haiku/headers/private/storage/QueryPredicate.h (revision 4c8e85b316c35a9161f5a1c50ad70bc91c83a76f)
1 //----------------------------------------------------------------------
2 //  This software is part of the Haiku 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 
18 namespace BPrivate {
19 namespace Storage {
20 
21 // QueryNode
22 class QueryNode {
23 public:
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
35 class LeafNode : public QueryNode {
36 public:
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
46 class UnaryNode : public QueryNode {
47 public:
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 
55 protected:
56 	QueryNode	*fChild;
57 };
58 
59 // BinaryNode
60 class BinaryNode : public QueryNode {
61 public:
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 
69 protected:
70 	QueryNode	*fChild1;
71 	QueryNode	*fChild2;
72 };
73 
74 // AttributeNode
75 class AttributeNode : public LeafNode {
76 public:
77 	AttributeNode(const char *attribute);
78 
79 	virtual status_t GetString(BString &predicate);
80 
81 private:
82 	BString	fAttribute;
83 };
84 
85 // StringNode
86 class StringNode : public LeafNode {
87 public:
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 
94 private:
95 	BString	fValue;
96 };
97 
98 // DateNode
99 class DateNode : public LeafNode {
100 public:
101 	DateNode(const char *value);
102 
103 	virtual status_t GetString(BString &predicate);
104 
105 private:
106 	BString	fValue;
107 };
108 
109 // ValueNode
110 template<typename ValueType>
111 class ValueNode : public LeafNode {
112 public:
113 	ValueNode(const ValueType &value);
114 
115 	virtual status_t GetString(BString &predicate);
116 
117 private:
118 	ValueType	fValue;
119 };
120 
121 // constructor
122 template<typename ValueType>
123 ValueNode<ValueType>::ValueNode(const ValueType &value)
124 					: LeafNode(),
125 					  fValue(value)
126 {
127 }
128 
129 // GetString
130 template<typename ValueType>
131 status_t
132 ValueNode<ValueType>::GetString(BString &predicate)
133 {
134 	predicate.SetTo("");
135 	predicate << fValue;
136 	return B_OK;
137 }
138 
139 // specializations for float and double
140 template<> status_t ValueNode<float>::GetString(BString &predicate);
141 template<> status_t ValueNode<double>::GetString(BString &predicate);
142 
143 
144 // short hands
145 typedef ValueNode<int32>	Int32ValueNode;
146 typedef ValueNode<uint32>	UInt32ValueNode;
147 typedef ValueNode<int64>	Int64ValueNode;
148 typedef ValueNode<uint64>	UInt64ValueNode;
149 typedef ValueNode<float>	FloatValueNode;
150 typedef ValueNode<double>	DoubleValueNode;
151 
152 
153 // SpecialOpNode
154 class SpecialOpNode : public LeafNode {
155 public:
156 	SpecialOpNode(query_op op);
157 
158 	virtual status_t GetString(BString &predicate);
159 
160 private:
161 	query_op	fOp;
162 };
163 
164 // UnaryOpNode
165 class UnaryOpNode : public UnaryNode {
166 public:
167 	UnaryOpNode(query_op op);
168 
169 	virtual status_t GetString(BString &predicate);
170 
171 private:
172 	query_op	fOp;
173 };
174 
175 // BinaryOpNode
176 class BinaryOpNode : public BinaryNode {
177 public:
178 	BinaryOpNode(query_op op);
179 
180 	virtual status_t GetString(BString &predicate);
181 
182 private:
183 	query_op	fOp;
184 };
185 
186 
187 // QueryStack
188 class QueryStack {
189 public:
190 	QueryStack();
191 	virtual ~QueryStack();
192 
193 	status_t PushNode(QueryNode *node);
194 	QueryNode *PopNode();
195 
196 	status_t ConvertToTree(QueryNode *&rootNode);
197 
198 private:
199 	status_t _GetSubTree(QueryNode *&rootNode);
200 
201 private:
202 	BList	fNodes;
203 };
204 
205 };	// namespace Storage
206 };	// namespace BPrivate
207 
208 #endif	// _QUERY_PREDICATE_H
209 
210 
211