1/*
2 * Copyright 2002-2009, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
4 */
5#ifndef _QUERY_H
6#define _QUERY_H
7
8
9#include <EntryList.h>
10#include <Messenger.h>
11#include <OS.h>
12
13class BVolume;
14struct entry_ref;
15
16
17namespace BPrivate {
18	namespace Storage {
19		class QueryNode;
20		class QueryStack;
21		class QueryTree;
22	};
23};
24
25typedef enum {
26	B_INVALID_OP	= 0,
27	B_EQ,
28	B_GT,
29	B_GE,
30	B_LT,
31	B_LE,
32	B_NE,
33	B_CONTAINS,
34	B_BEGINS_WITH,
35	B_ENDS_WITH,
36	B_AND			= 0x101,
37	B_OR,
38	B_NOT,
39	_B_RESERVED_OP_	= 0x100000
40} query_op;
41
42
43/*!
44	\class BQuery
45	\brief Represents a live or non-live file system query
46
47	Provides an interface for creating file system queries. Implements
48	the BEntryList for iterating through the found entries.
49
50	\author <a href='mailto:bonefish@users.sf.net'>Ingo Weinhold</a>
51
52	\version 0.0.0
53*/
54class BQuery : public BEntryList {
55public:
56							BQuery();
57	virtual					~BQuery();
58
59			status_t		Clear();
60
61			status_t		PushAttr(const char* attrName);
62			status_t		PushOp(query_op op);
63
64			status_t		PushUInt32(uint32 value);
65			status_t		PushInt32(int32 value);
66			status_t		PushUInt64(uint64 value);
67			status_t		PushInt64(int64 value);
68			status_t		PushFloat(float value);
69			status_t		PushDouble(double value);
70			status_t		PushString(const char* value,
71								bool caseInsensitive = false);
72			status_t		PushDate(const char* date);
73
74			status_t		SetVolume(const BVolume* volume);
75			status_t		SetPredicate(const char* expression);
76			status_t		SetTarget(BMessenger messenger);
77
78			bool			IsLive() const;
79
80			status_t		GetPredicate(char* buffer, size_t length);
81			status_t		GetPredicate(BString* predicate);
82			size_t			PredicateLength();
83
84			dev_t			TargetDevice() const;
85
86			status_t		Fetch();
87
88	// BEntryList interface
89	virtual	status_t		GetNextEntry(BEntry* entry, bool traverse = false);
90	virtual	status_t		GetNextRef(entry_ref* ref);
91	virtual	int32			GetNextDirents(struct dirent* buffer, size_t length,
92								 int32 count = INT_MAX);
93	virtual	status_t		Rewind();
94	virtual	int32			CountEntries();
95
96private:
97			bool			_HasFetched() const;
98			status_t		_PushNode(BPrivate::Storage::QueryNode* node,
99								bool deleteOnError);
100			status_t		_SetPredicate(const char* expression);
101			status_t		_EvaluateStack();
102			void			_ParseDates(BString& parsedPredicate);
103
104	// FBC
105	virtual	void			_QwertyQuery1();
106	virtual	void			_QwertyQuery2();
107	virtual	void			_QwertyQuery3();
108	virtual	void			_QwertyQuery4();
109	virtual	void			_QwertyQuery5();
110	virtual	void			_QwertyQuery6();
111
112private:
113			BPrivate::Storage::QueryStack* fStack;
114			char*			fPredicate;
115			dev_t			fDevice;
116			bool			fLive;
117			port_id			fPort;
118			long			fToken;
119			int				fQueryFd;
120			int32			_reservedData[4];
121};
122
123#endif	// _QUERY_H
124