/usr/include/Wt/Dbo/Query is in libwtdbo-dev 3.1.10-1ubuntu2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 | // This may look like C code, but it's really -*- C++ -*-
/*
* Copyright (C) 2008 Emweb bvba, Kessel-Lo, Belgium.
*
* See the LICENSE file for terms of use.
*/
#ifndef WT_DBO_QUERY_H_
#define WT_DBO_QUERY_H_
#include <vector>
#include <Wt/Dbo/SqlTraits>
#include <Wt/Dbo/ptr>
namespace Wt {
namespace Dbo {
template <class C> class collection;
namespace Impl {
struct SelectField
{
std::size_t begin, end;
};
typedef std::vector<SelectField> SelectFieldList;
typedef std::vector<SelectFieldList> SelectFieldLists;
template <class Result>
class QueryBase {
protected:
std::vector<FieldInfo> fields() const;
void fieldsForSelect(const SelectFieldList& list,
std::vector<FieldInfo>& result) const;
std::pair<SqlStatement *, SqlStatement *>
statements(const std::string& where, const std::string& groupBy,
const std::string& orderBy, int limit, int offset) const;
Session& session() const;
QueryBase();
QueryBase(Session& session, const std::string& sql);
QueryBase(Session& session, const std::string& table,
const std::string& where);
QueryBase& operator=(const QueryBase& other);
Result singleResult(const collection<Result>& results) const;
Session *session_;
std::string sql_;
SelectFieldLists selectFieldLists_;
bool simpleCount_;
};
}
/*! \class DirectBinding Wt/Dbo/Query Wt/Dbo/Query
*
* Bind strategy indicating that parameters are bound directly to an underlying
* prepared statement.
*/
struct DirectBinding { };
/*! \class DynamicBinding Wt/Dbo/Query Wt/Dbo/Query
*
* Bind strategy indicating that binding to the underlying prepared statement
* is deferred and parameter values are temporarily stored first in a dynamic
* value vector.
*/
struct DynamicBinding { };
class Session;
class SqlConnection;
/*! \class Query Wt/Dbo/Query Wt/DboQuery
* \brief A database query.
*
* The query fetches results of type \p Result from the database. This
* can be any type for which query_result_traits are properly
* implemented. The library provides these implementations for
* primitive values (see sql_value_traits), database objects (ptr) and
* <tt>boost::tuple</tt>.
*
* Simple queries can be done using Session::find(), while more elaborate
* queries (with arbitrary result types) using Session::query().
*
* You may insert positional holders for parameters (in the conditional where
* part) using '?', and bind these to actual values using bind().
*
* The query result may be fetched using resultValue() or resultList().
*
* Usage example:
* \code
* typedef Wt::Dbo::ptr<Account> AccountPtr;
* typedef Wt::Dbo::collection<AccountPtr> Accounts;
*
* Wt::Dbo::Query<AccountPtr> query = session.find<Account>().where("balance > ?").bind(100000);
* Accounts accounts = query.resultList();
*
* for (Accounts::const_iterator i = accounts.begin(); i != accounts.end(); ++i)
* std::cerr << "Name: " << (*i)->name << std::end;
* \endcode
*
* The \p BindStrategy specifies how you want to bind parameters to
* your query (if any).
*
* When using DynamicBinding (which is the default), parameter binding
* to an actual sql statement is deferred until the query is run. This
* has the advantage that you can compose the query definition using
* helper methods provided in the query object (where(), orderBy() and
* groupBy()), possibly intermixing this with parameter binding, and
* you can keep the query around and run the query multiple times,
* perhaps with different parameter values or to scroll through the
* query results.
*
* When using DirectBinding, parameters are directly bound to an
* underlying sql statement. Therefore, the query must be specified
* entirely when created. Because of this reliance on an sql
* statement, it can be run only once (one call to resultValue() or
* resultList()) after which it should be discarded. You should not
* try to keep a query object around when using this parameter binding
* strategy (that will amost always not do what you would hope it to
* do).
*
* \ingroup dbo
*/
template <class Result, typename BindStrategy = DynamicBinding>
class Query
{
#ifdef DOXYGEN_ONLY
public:
/*! \brief Default constructor.
*/
Query();
/*! \brief Destructor.
*/
~Query();
/*! \brief Copy constructor.
*/
Query(const Query& other);
/*! \brief Assignment operator.
*/
Query& operator= (const Query& other);
/*! \brief Returns the result fields.
*/
std::vector<FieldInfo> fields() const;
Session& session() const;
/*! \brief Binds a value to the next positional marker.
*
* This binds the \p value to the next positional marker in the
* query condition.
*/
template<typename T>
Query<Result, BindStrategy>& bind(const T& value);
/*! \brief Returns a unique result value.
*
* You can use this method if you are expecting the query to return
* at most one result. If the query returns more than one result a
* NoUniqueResultException is thrown.
*
* When using a DynamicBinding bind strategy, after a result has
* been fetched, the query can no longer be used.
*/
Result resultValue() const;
/*! \brief Returns a result list.
*
* This returns a collection which is backed by the underlying query.
* The query is not actually run until this collection is traversed
* or its size is asked.
*
* When using a DynamicBinding bind strategy, after a result has
* been fetched, the query can no longer be used.
*/
collection< Result > resultList() const;
/*! \brief Returns a unique result value.
*
* This is a convenience conversion operator that calls resultValue().
*/
operator Result () const;
/*! \brief Returns a result list.
*
* This is a convenience conversion operator that calls resultList().
*/
operator collection< Result > () const;
/** @name Methods for composing a query (DynamicBinding only)
*/
//@{
/*! \brief Adds a query condition.
*
* The condition must be a valid SQL condition expression.
*
* Multiple conditions may be provided, which must each be fulfilled,
* and are concatenated together using 'and').
*
* A condition may contain positional markers '?' to which values may
* be bound using bind().
*
* This provides the <i>where</i> part of an SQL query.
*
* \note This method is not available when using a DirectBinding binding
* strategy.
*/
Query<Result, BindStrategy>& where(const std::string& condition);
/*! \brief Sets the result order.
*
* Orders the results based on the given field name (or multiple
* names, comma-separated).
*
* This provides the <i>order by</i> part of an SQL query.
*
* \note This method is not available when using a DirectBinding binding
* strategy.
*/
Query<Result, BindStrategy>& orderBy(const std::string& fieldName);
/*! \brief Sets the grouping field(s).
*
* Groups results based on unique values of the indicated field(s),
* which is a comma separated list of fields. Only fields on which
* you group and aggregate functions can be selected by a query.
*
* A field that refers to a database object that is selected by the
* query is expanded to all the corresponding fields of that
* database object (as in the select statement).
*
* This provides the <i>group by</i> part of an SQL query.
*
* \note This method is not available when using a DirectBinding binding
* strategy.
*/
Query<Result, BindStrategy>& groupBy(const std::string& fields);
/*! \brief Sets a result offset.
*
* Sets a result offset. This has the effect that the next
* resultList() call will skip as many results as the offset
* indicates. Use -1 to indicate no offset.
*
* This provides the (non standard) <i>offset</i> part of an SQL query.
*
* \sa limit()
*
* \note This method is not available when using a DirectBinding binding
* strategy.
*/
Query<Result, BindStrategy>& offset(int count);
/*! \brief Returns an offset set for this query.
*
* \sa offset(int)
*/
int offset() const;
/*! \brief Sets a result limit.
*
* Sets a result limit. This has the effect that the next
* resultList() call will return up to \p count results. Use -1 to
* indicate no limit.
*
* This provides the (non standard) <i>limit</i> part of an SQL query.
*
* \sa offset()
*
* \note This method is not available when using a DirectBinding binding
* strategy.
*/
Query<Result, BindStrategy>& limit(int count);
/*! \brief Returns a limit set for this query.
*
* \sa limit(int)
*/
int limit() const;
//@}
#endif // DOXYGEN_ONLY
};
template <class Result>
class Query<Result, DirectBinding> : private Impl::QueryBase<Result>
{
public:
using Impl::QueryBase<Result>::fields;
using Impl::QueryBase<Result>::session;
Query();
~Query();
template<typename T> Query<Result, DirectBinding>& bind(const T& value);
Result resultValue() const;
collection< Result > resultList() const;
operator Result () const;
operator collection< Result > () const;
private:
Query(Session& session, const std::string& sql);
Query(Session& session, const std::string& table, const std::string& where);
mutable int column_;
mutable SqlStatement *statement_, *countStatement_;
void prepareStatements() const;
friend class Session;
};
template <class Result>
class Query<Result, DynamicBinding> : private Impl::QueryBase<Result>
{
public:
using Impl::QueryBase<Result>::fields;
using Impl::QueryBase<Result>::session;
Query();
~Query();
Query(const Query& other);
Query& operator= (const Query& other);
template<typename T> Query<Result, DynamicBinding>& bind(const T& value);
Query<Result, DynamicBinding>& where(const std::string& condition);
Query<Result, DynamicBinding>& orderBy(const std::string& fieldName);
Query<Result, DynamicBinding>& groupBy(const std::string& fields);
Query<Result, DynamicBinding>& offset(int count);
int offset() const;
Query<Result, DynamicBinding>& limit(int count);
int limit() const;
Result resultValue() const;
collection< Result > resultList() const;
operator Result () const;
operator collection< Result > () const;
private:
Query(Session& session, const std::string& sql);
Query(Session& session, const std::string& table, const std::string& where);
std::string where_, groupBy_, orderBy_;
int limit_, offset_;
std::vector<Impl::ParameterBase *> parameters_;
void reset();
void bindParameters(SqlStatement *statement) const;
friend class Session;
template <class C> friend class collection;
};
}
}
#endif // WT_DBO_QUERY
|