2 * Copyright 2002-2014 Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
7 * Axel Dörfler, axeld@pinc-software.de
8 * John Scipione, jscipione@gmail.com
9 * Ingo Weinhold, bonefish@users.sf.net
12 * headers/os/storage/Query.h hrev47402
13 * src/kits/storage/Query.cpp hrev47402
21 \brief Provides the BQuery class.
29 \brief Provides an interface for creating file system queries and
30 implements BEntryList methods for iterating through the results.
38 \brief Creates an uninitialized BQuery object.
48 \brief Destroys the BQuery object and frees any associated resources.
55 \fn status_t BQuery::Clear()
56 \brief Resets the object to a uninitialized state.
65 \fn status_t BQuery::Fetch()
66 \brief Start fetching entries satisfying the predicate.
68 After Fetch() has been called GetNextEntry(), GetNextRef() and
69 GetNextDirents() can be used to retrieve the entities. Live query updates
70 may be sent immediately after this method has been called.
72 Fetch() fails if it has already been called. To reuse the BQuery object it
73 must first be reset with the Clear() method.
75 \return A status code.
76 \retval B_OK Everything went fine.
77 \retval B_NO_INIT The object predicate or the volume wasn't set.
78 \retval B_BAD_VALUE The object predicate was invalid.
79 \retval B_NOT_ALLOWED Fetch() already called.
88 Methods to push data onto the predicate stack.
90 \warning In BeOS R5 these methods returned \c void. That is checking the
91 return value will render your code source and binary
92 incompatible! Calling PushXYZ() after a Fetch() does change the
93 predicate on R5, but it doesn't affect the active query and the
94 newly created predicate can not even be used for the next query,
95 since in order to be able to reuse the BQuery object for another
96 query, Clear() has to be called and Clear() also deletes the
105 \fn status_t BQuery::PushAttr(const char* attrName)
106 \brief Pushes an attribute name onto the predicate stack.
108 \param attrName The name of the attribute to push on the stack.
110 \return A status code.
111 \retval B_OK Everything went fine.
112 \retval B_NO_MEMORY Not enough memory.
113 \retval B_NOT_ALLOWED PushAttribute() was called after Fetch().
120 \fn status_t BQuery::PushOp(query_op op)
121 \brief Pushes an operator onto the predicate stack.
123 \param op The operator code to push onto the stack.
125 \return A status code.
126 \retval B_OK Everything went fine.
127 \retval B_NO_MEMORY Not enough memory.
128 \retval B_NOT_ALLOWED PushOp() was called after Fetch().
135 \fn status_t BQuery::PushUInt32(uint32 value)
136 \brief Pushes a \c uint32 onto the predicate stack.
138 \param value The \c uint32 to push onto the stack.
140 \return A status code.
141 \retval B_OK Everything went fine.
142 \retval B_NO_MEMORY Not enough memory.
143 \retval B_NOT_ALLOWED PushUInt32() was called after Fetch().
150 \fn status_t BQuery::PushInt32(int32 value)
151 \brief Pushes an \c int32 onto the predicate stack.
153 \param value The \c int32 to push onto the stack.
155 \return A status code.
156 \retval B_OK Everything went fine.
157 \retval B_NO_MEMORY Not enough memory.
158 \retval B_NOT_ALLOWED PushInt32() was called after Fetch().
165 \fn status_t BQuery::PushUInt64(uint64 value)
166 \brief Pushes a \c uint64 onto the predicate stack.
168 \param value The \c uint64 to push onto the stack.
170 \return A status code.
171 \retval B_OK Everything went fine.
172 \retval B_NO_MEMORY Not enough memory.
173 \retval B_NOT_ALLOWED PushUInt64() was called after Fetch().
180 \fn status_t BQuery::PushInt64(int64 value)
181 \brief Pushes an int64 onto the predicate stack.
183 \param value The \c int64 to push onto the stack.
185 \return A status code.
186 \retval B_OK Everything went fine.
187 \retval B_NO_MEMORY Not enough memory.
188 \retval B_NOT_ALLOWED PushInt64() was called after Fetch().
195 \fn status_t BQuery::PushFloat(float value)
196 \brief Pushes a \c float onto the predicate stack.
198 \param value The \c float to push onto the stack.
200 \return A status code.
201 \retval B_OK Everything went fine.
202 \retval B_NO_MEMORY Not enough memory.
203 \retval B_NOT_ALLOWED PushFloat() was called after Fetch().
210 \fn status_t BQuery::PushDouble(double value)
211 \brief Pushes a \c double onto the predicate stack.
213 \param value The \c double to push onto the stack.
215 \return A status code.
216 \retval B_OK Everything went fine.
217 \retval B_NO_MEMORY Not enough memory.
218 \retval B_NOT_ALLOWED PushDouble() was called after Fetch().
225 \fn status_t BQuery::PushString(const char* value, bool caseInsensitive)
226 \brief Pushes a string onto the predicate stack.
228 \param value The string to push onto the stack.
229 \param caseInsensitive Whether or not the case of \a value should be
230 ignored in the resulting query.
232 \return A status code.
233 \retval B_OK Everything went fine.
234 \retval B_NO_MEMORY Not enough memory.
235 \retval B_NOT_ALLOWED PushString() was called after Fetch().
242 \fn status_t BQuery::PushDate(const char* date)
243 \brief Pushes a date string onto the predicate stack.
245 The supplied date can be any string understood by parsedate().
247 \param date The date string to push onto the stack.
249 \return A status code.
250 \retval B_OK Everything went fine.
251 \retval B_NO_MEMORY Not enough memory.
252 \retval B_NOT_ALLOWED PushDate() was called after Fetch().
272 \fn status_t BQuery::SetVolume(const BVolume* volume)
273 \brief Assigns \a volume to the BQuery object.
275 A query may only be assigned to one volume.
277 The method fails if called after Fetch(). To reuse the BQuery object it
278 must first be reset using the Clear() method.
280 \param volume The \a volume to set.
282 \return A status code.
283 \retval B_OK Everything went fine.
284 \retval B_NOT_ALLOWED SetVolume() was called after Fetch().
291 \fn status_t BQuery::SetPredicate(const char* expression)
292 \brief Assigns the passed-in predicate \a expression.
294 A predicate can be set either using this method or by constructing one on
295 the predicate stack, however, the two methods can not be mixed. The
296 predicate stack takes precedence over this method.
298 The method fails if called after Fetch(). To reuse the BQuery object it
299 must first be reset using the Clear() method.
301 \param expression The predicate \a expression to set.
303 \return A status code.
304 \retval B_OK Everything went fine.
305 \retval B_NO_MEMORY Not enough memory.
306 \retval B_NOT_ALLOWED SetPredicate() was called after Fetch().
313 \fn status_t BQuery::SetTarget(BMessenger messenger)
314 \brief Assigns the target \a messenger and makes the query live.
316 The query update messages are sent to the specified target. They might
317 roll in immediately after calling Fetch().
319 This methods fails if called after Fetch(). To reuse the BQuery object it
320 must first be reset via Clear().
322 \param messenger The target \a messenger to set.
324 \return A status code.
325 \retval B_OK Everything went fine.
326 \retval B_NO_MEMORY Not enough memory.
327 \retval B_NOT_ALLOWED SetTarget() was called after Fetch().
337 \name Query Information
345 \fn bool BQuery::IsLive() const
346 \brief Gets whether the query associated with this object is live.
348 \return \c true, if the query is live, \c false otherwise.
357 \fn dev_t BQuery::TargetDevice() const
358 \brief Gets the device ID identifying the volume of the BQuery object.
360 \return The device ID of the volume or \c B_NO_INIT if the volume wasn't
370 \fn size_t BQuery::PredicateLength()
371 \brief Gets the length of the predicate string.
373 This method returns the length of the string representation of the
374 predicate (including the terminating \c NUL) regardless of whether the
375 predicate has been constructed using the predicate stack or set via
378 \return The length of the predicate string or 0 if an error occurred.
392 These methods fetch a string representation regardless of whether the
393 predicate has been constructed using the predicate stack or via
396 \note These methods cause the predicate stack to be evaluated and cleared.
397 You can't interleave calls to push data and GetPredicate() methods.
405 \fn status_t BQuery::GetPredicate(char* buffer, size_t length)
406 \brief Fills out \a buffer with the predicate string assigned to the
409 \param buffer A pointer to a buffer which the predicate is written to.
410 \param length the size of \a buffer.
412 \return A status code.
413 \retval B_OK Everything went fine.
414 \retval B_NO_INIT The predicate of the BQuery object wasn't set.
415 \retval B_BAD_VALUE \a buffer was \c NULL or too short.
422 \fn status_t BQuery::GetPredicate(BString* predicate)
423 \brief Fills out the passed-in BString object with the predicate string
424 assigned to the BQuery object.
426 \param predicate A pointer to a BString object that gets filled out with
427 the predicate string.
429 \return A status code.
430 \retval B_OK Everything went fine.
431 \retval B_NO_INIT The predicate of the BQuery object wasn't set.
432 \retval B_BAD_VALUE \a predicate was \c NULL.
442 \name BEntryList Interface
444 These methods are used to traverse the results of a query as a BEntryList.
446 \note The iterator used by these methods is the same one used by
447 GetNextRef() and GetNextDirents().
455 \fn status_t BQuery::GetNextEntry(BEntry* entry, bool traverse)
456 \brief Fills out \a entry with the next entry traversing symlinks if
457 \a traverse is \c true.
459 \param entry A pointer to a BEntry object initialized with the entry.
460 \param traverse Whether or not to follow symbolic links.
462 \return A status code.
463 \retval B_OK Everything went fine.
464 \retval B_ENTRY_NOT_FOUND At end of list.
465 \retval B_BAD_VALUE The predicate included unindexed attributes.
466 \retval B_NOT_ALLOWED Fetch() was not previously called on the object.
473 \fn status_t BQuery::GetNextRef(entry_ref* ref)
474 \brief Fills out \a ref with the next entry as an entry_ref.
476 \param ref A pointer to an entry_ref object filled out with the
479 \return A status code.
480 \retval B_OK Everything went fine.
481 \retval B_ENTRY_NOT_FOUND At end of list.
482 \retval B_BAD_VALUE The predicate included unindexed attributes.
483 \retval B_NOT_ALLOWED Fetch() was not previously called on the object.
490 \fn int32 BQuery::GetNextDirents(struct dirent* buffer, size_t length,
492 \brief Fill out up to \a count entries into the array of dirent structs
493 pointed to by \a buffer.
495 Reads as many but no more than \a count entries, as many entries as
496 remain, or as many entries as will fit into the array at \a buffer with
497 the given \a length (in bytes), whichever is smallest.
499 \param buffer A pointer to a buffer filled out with dirent structures of
501 \param length The length of \a buffer.
502 \param count The maximum number of entries to be read.
504 \return The number of dirent structures stored in the buffer, 0 when there
505 are no more entries to be read, or an error code.
506 \retval B_BAD_VALUE The predicate included unindexed attributes.
507 \retval B_FILE_ERROR Fetch() was not previously called on the object.
514 \fn status_t BQuery::Rewind()
515 \brief Rewinds the entry list back to the first entry.
517 \note BeOS R5 does not implement this method for BQuery.
519 \return A status code.
520 \retval B_OK Everything went fine.
521 \retval B_FILE_ERROR Fetch() was not previously called on the object.
528 \fn int32 BQuery::CountEntries()
529 \brief Unimplemented.