/usr/include/Wt/Dbo/Session is in libwtdbo-dev 3.3.0-1build1.
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 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 | // 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_SESSION_H_
#define WT_DBO_SESSION_H_
#include <map>
#include <set>
#include <string>
#include <typeinfo>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/sequenced_index.hpp>
#include <boost/multi_index/member.hpp>
#include <Wt/Dbo/ptr>
#include <Wt/Dbo/Field>
#include <Wt/Dbo/Query>
#include <Wt/Dbo/Transaction>
namespace Wt {
namespace Dbo {
namespace Impl {
extern WTDBO_API std::string quoteSchemaDot(const std::string& table);
template <class C, typename T> struct LoadHelper;
}
struct NullType {
static NullType null_;
};
class Call;
class SqlConnection;
class SqlConnectionPool;
class SqlStatement;
template <typename Result, typename BindStrategy> class Query;
struct DirectBinding;
struct DynamicBinding;
/*! \class Session Wt/Dbo/Session Wt/Dbo/Session
* \brief A database session.
*
* A database session manages meta data about the mapping of C++
* classes to database tables, and keeps track of a working set of
* in-memory objects (objects which are referenced from your code or
* from within a transaction).
*
* It also manages an active transaction, which you need to access
* database objects.
*
* You can provide the session with a dedicated database connection
* using setConnection(), or with a connection pool (from which it
* will take a connection while processing a transaction) using
* setConnectionPool(). In either case, the session does not take
* ownership of the connection or connection pool.
*
* A session will typically be a long-lived object in your
* application.
*
* \ingroup dbo
*/
class WTDBO_API Session
{
public:
/*! \brief Creates a database session.
*/
Session();
/*! \brief Destructor.
*
* A session must survive all database objects that have been loaded
* through it, and will warning during this destructor if there are
* still database objects that are being referenced from a ptr.
*/
~Session();
/*! \brief Sets a dedicated connection.
*
* The connection will be used exclusively by this session.
*
* \sa setConnectionPool()
*/
void setConnection(SqlConnection& connection);
/*! \brief Sets a connection pool.
*
* The connection pool is typically shared with other sessions.
*
* \sa setConnection()
*/
void setConnectionPool(SqlConnectionPool& pool);
/*! \brief Maps a class to a database table.
*
* The class \p C is mapped to table with name \p tableName. You
* need to map classes to tables.
*
* You may provide a schema-qualified table name, if the underlying
* database supports this, eg. <tt>"myschema.users"</tt>.
*/
template <class C> void mapClass(const char *tableName);
/*! \brief Returns the mapped table name for a class.
*
* \sa mapClass()
*/
template <class C> const char *tableName() const;
/*! \brief Persists a transient object.
*
* The transient object pointed to by \p ptr is added to the
* session, and will be persisted when the session is flushed.
*
* A transient object is usually a newly created object which want
* to add to the database.
*
* The method returns \p ptr.
*/
template <class C> ptr<C> add(ptr<C>& ptr);
/*! \brief Persists a transient object.
*
* This is an overloaded method for convenience, and is implemented as:
* \code
* return add(ptr<C>(obj));
* \endcode
*
* The method returns a database pointer to the object.
*/
template <class C> ptr<C> add(C *obj);
/*! \brief Loads a persisted object.
*
* This method returns a database object with the given object
* id. If the object was already loaded in the session, the loaded
* object is returned, otherwise the object is loaded from the
* database.
*
* If \p forceReread is set to \c true, then a fresh copy is loaded
* from the database. This is almost equivalent to calling \link
* ptr<C>::reread() reread()\endlink on the returned object, except
* that it will not result in two database reads in case the object was
* in fact not yet loaded in the session.
*
* Throws an ObjectNotFoundException when the object was not found.
*
* \sa ptr::id()
*/
template <class C> ptr<C> load(const typename dbo_traits<C>::IdType& id,
bool forceReread = false);
#ifndef DOXYGEN_ONLY
template <class C>
Query< ptr<C> > find(const std::string& condition = std::string()) {
// implemented in-line because otherwise it crashes gcc 4.0.1
return find<C, DynamicBinding>(condition);
}
#endif // DOXYGEN_ONLY
/*! \brief Finds database objects.
*
* This method creates a query for finding objects of type \p C.
*
* When passing an empty \p condition parameter, it will return all
* objects of type \p C. Otherwise, it will add the condition, by
* generating an SQL <i>where</i> clause.
*
* The \p BindStrategy specifies how you want to bind parameters to
* your query (if any).
*
* When using \p DynamicBinding (which is the default), you will
* defer the binding until the query is run. This has the advantage
* that you can compose the query definition using helper methods
* provided in the query object, 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 \p DirectBinding, the query must be specified entirely
* using the \p condition, and can be run only once. This method
* does have the benefit of binding parameters directly to the
* underlying prepared statement.
*
* This method is convenient when you are querying only results from a
* single table. For more generic query support, see query().
*
* Usage example:
* \code
* // Bart is missing, let's find him.
* Wt::Dbo::ptr<User> bart = session.find<User>().where("name = ?").bind("Bart");
*
* // Find all users, order by name
* typedef Wt::Dbo::collection< Wt::Dbo::ptr<User> > Users;
* Users users = session.find<User>().orderBy("name");
* \endcode
*
* In the \p condition, parameters can be bound using '?' as a
* positional placeholder: each occurence of '?' (as a lexical
* token) is replaced by a bound parameter. This is actually done by
* most of the backends themselves using prepared statements and
* parameter binding. Parameter binding is possible for all types
* for which sql_value_traits is specialized.
*
* \sa query()
*/
#ifdef DOXYGEN_ONLY
template <class C, typename BindStrategy = DynamicBinding>
#else
template <class C, typename BindStrategy>
#endif
Query< ptr<C>, BindStrategy>
find(const std::string& condition = std::string());
#ifndef DOXYGEN_ONLY
template <class Result> Query<Result> query(const std::string& sql);
#endif // DOXYGEN_ONLY
/*! \brief Creates a query.
*
* The sql statement should be a complete SQL statement, starting
* with a "select ". The items listed in the "select" must match the
* \p Result type. An item that corresponds to a database object
* (ptr) is substituted with the selection of all the fields in the
* dbo.
*
* For example, the following query (class User is mapped onto table 'user'):
* \code
* session.query< ptr<User> >("select u from user u").where("u.name = ?").bind("Bart");
* \endcode
* is the more general version of:
* \code
* session.find<User>().where("name = ?").bind("Bart");
* \endcode
*
* Note that "u" in this query will be expanded to select the fields of the
* user table (u.id, u.version, u.name, ...). The same expansion happens when
* using an alias in Query::groupBy().
*
* The additional flexibility offered by %query() over find() is
* however that it may support other result types.
*
* Thus, it may return plain values:
* \code
* session.query<int>("select count(1) from ...");
* \endcode
*
* Or Boost.Tuple for an arbitrary combination of result values:
*
* \code
* session.query< boost::tuple<int, int> >("select A.id, B.id from table_a A, table_b B").where("...");
* \endcode
*
* A tuple may combine any kind of object that is supported as a result,
* including database objects (see also ptr_tuple):
* \code
* session.query< boost::tuple<ptr<A>, ptr<B> > >("select A, B from table_a A, table_b B").where("...");
* \endcode
*
* The \p BindStrategy specifies how you want to bind parameters to
* your query (if any).
*
* When using \p DynamicBinding (which is the default), you will
* defer the binding until the query is run. This has the advantage
* that you can compose the query using helper methods provided in
* the Query object, 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 \p DirectBinding, the query must be specified entirely
* using the \p sql, and can be run only once. This method does have
* the benefit of binding parameters directly to the underlying
* prepared statement.
*
* This method uses query_result_traits to unmarshal the query result
* into the \p Result type.
*
* In the \p sql query, parameters can be bound using '?' as the
* positional placeholder: each occurence of '?' (as a lexical
* token) is replaced by a bound parameter. This is actually done by
* most of the backends themselves using prepared statements and
* parameter binding. Parameter binding is possible for all types
* for which sql_value_traits is specialized.
*
* \note The query must be a ASCII-7 string: UTF-8 is not supported by
* the underlying query parser. To add a non-English string to the query
* use parameter binding instead (which prevents against SQL injection
* attacks at the same time) instead of string concatenation.
*/
#ifdef DOXYGEN_ONLY
template <class Result, typename BindStrategy = DynamicBinding>
#else
template <class Result, typename BindStrategy>
#endif
Query<Result, BindStrategy> query(const std::string& sql);
/*! \brief Executs an Sql command.
*
* This executs an Sql command. It differs from query() in that no
* result is expected from the call.
*
* Usage example:
* \code
* session.execute("update user set name = ? where name = ?").bind("Bart").bind("Sarah");
* \endcode
*/
Call execute(const std::string& sql);
/*! \brief Creates the database schema.
*
* This will create the database schema of the mapped tables. Schema
* creation will fail if one or more tables already existed.
*
* \sa mapClass(), dropTables()
*/
void createTables();
/*! \brief Returns database creation SQL.
*/
std::string tableCreationSql();
/*! \brief Drops the database schema.
*
* This will drop the database schema. Dropping the schema will fail
* if one or more tables did not exist.
*
* \sa createTables()
*/
void dropTables();
/*! \brief Flushes the session.
*
* This flushes all modified objects to the database. This does not
* commit the transaction.
*
* Normally, you need not to call this method as the session is
* flushed automatically before committing a transaction, or before
* running a query (to be sure to take into account pending
* modifications).
*/
void flush();
/*! \brief Rereads all objects.
*
* This rereads all objects from the database, possibly discarding
* unflushed modifications. This is a catch-all solution for a
* StaleObjectException.
*
* If a \p tableName is given, then only objects of that table are
* reread.
*
* \sa ptr::reread()
*/
void rereadAll(const char *tableName = 0);
void getFields(const char *tableName, std::vector<FieldInfo>& result);
private:
Session(const Session& s);
typedef boost::multi_index::multi_index_container<
MetaDboBase *,
boost::multi_index::indexed_by<
boost::multi_index::sequenced<>,
boost::multi_index::hashed_unique
<boost::multi_index::identity<MetaDboBase *> >
>
> MetaDboBaseSet;
enum { SqlInsert = 0,
SqlUpdate = 1,
SqlDelete = 2,
SqlDeleteVersioned = 3,
SqlSelectById = 4,
FirstSqlSelectSet = 5 };
struct JoinId {
std::string joinIdName;
std::string tableIdName;
std::string sqlType;
JoinId(const std::string& aJoinIdName,
const std::string& aTableIdName,
const std::string& aSqlType);
};
struct WTDBO_API SetInfo {
const char *tableName;
std::string joinName;
std::string joinSelfId, joinOtherId;
RelationType type;
int fkConstraints, otherFkConstraints;
SetInfo(const char *aTableName, RelationType type,
const std::string& aJoinName,
const std::string& aJoinSelfId,
int someFkConstraints);
};
struct WTDBO_API MappingInfo {
bool initialized_;
const char *tableName;
const char *versionFieldName;
const char *surrogateIdFieldName;
std::string naturalIdFieldName; // for non-auto generated id
int naturalIdFieldSize; // for non-auto generated id
std::string idCondition;
std::vector<FieldInfo> fields;
std::vector<SetInfo> sets;
std::vector<std::string> statements;
MappingInfo();
virtual ~MappingInfo();
virtual void init(Session& session);
virtual void dropTable(Session& session,
std::set<std::string>& tablesDropped);
virtual void rereadAll();
std::string primaryKeys() const;
};
template <class C>
struct Mapping : public MappingInfo
{
typedef std::map<typename dbo_traits<C>::IdType, MetaDbo<C> *> Registry;
Registry registry_;
virtual ~Mapping();
virtual void init(Session& session);
virtual void dropTable(Session& session,
std::set<std::string>& tablesDropped);
virtual void rereadAll();
};
typedef const std::type_info * const_typeinfo_ptr;
struct typecomp {
bool operator() (const const_typeinfo_ptr& lhs, const const_typeinfo_ptr& rhs) const
{
return lhs->before(*rhs) != 0;
}
};
typedef std::map<const_typeinfo_ptr, MappingInfo *, typecomp> ClassRegistry;
typedef std::map<std::string, MappingInfo *> TableRegistry;
ClassRegistry classRegistry_;
TableRegistry tableRegistry_;
bool schemaInitialized_;
bool useRowsFromTo_;
MetaDboBaseSet dirtyObjects_;
SqlConnection *connection_;
SqlConnectionPool *connectionPool_;
Transaction::Impl *transaction_;
void initSchema() const;
void resolveJoinIds(MappingInfo *mapping);
void prepareStatements(MappingInfo *mapping);
void executeSql(std::vector<std::string> &sql,std::ostream *sout);
void executeSql(std::stringstream &sql,std::ostream *sout);
std::string constraintName(const char *tableName,
std::string foreignKeyName);
std::vector<JoinId> getJoinIds(MappingInfo *mapping,
const std::string& joinId);
void createTable(MappingInfo *mapping,
std::set<std::string>& tablesCreated,
std::ostream *sout);
void createRelations(MappingInfo *mapping,
std::set<std::string>& tablesCreated,
std::ostream *sout);
std::string constraintString(MappingInfo *mapping,
const FieldInfo& field,
unsigned fromIndex,
unsigned toIndex);
unsigned findLastForeignKeyField(MappingInfo *mapping,
const FieldInfo& field,
unsigned index);
void createJoinTable(const std::string& joinName,
MappingInfo *mapping1, MappingInfo *mapping2,
const std::string& joinId1,
const std::string& joinId2,
int fkConstraints1, int fkConstraints2,
std::set<std::string>& tablesCreated,
std::ostream *sout);
void addJoinTableFields(MappingInfo& joinTableMapping,
MappingInfo *mapping, const std::string& joinId,
const std::string& foreignKeyName, int fkConstraints);
void createJoinIndex(MappingInfo& joinTableMapping,
MappingInfo *mapping,
const std::string& joinId,
const std::string& foreignKeyName,
std::ostream *sout);
void needsFlush(MetaDboBase *dbo);
template <class C> Mapping<C> *getMapping() const;
MappingInfo *getMapping(const char *tableName) const;
template <class C> ptr<C> loadLazy(const typename dbo_traits<C>::IdType& id);
template <class C> ptr<C> load(SqlStatement *statement, int& column);
template <class C>
ptr<C> loadWithNaturalId(SqlStatement *statement, int& column);
template <class C>
ptr<C> loadWithLongLongId(SqlStatement *statement, int& column);
void discardChanges(MetaDboBase *obj);
template <class C> void prune(MetaDbo<C> *obj);
template<class C> void implSave(MetaDbo<C>& dbo);
template<class C> void implDelete(MetaDbo<C>& dbo);
template<class C> void implTransactionDone(MetaDbo<C>& dbo, bool success);
template<class C> void implLoad(MetaDbo<C>& dbo, SqlStatement *statement,
int& column);
static std::string statementId(const char *table, int statementIdx);
template <class C> SqlStatement *getStatement(int statementIdx);
SqlStatement *getStatement(const std::string& id);
SqlStatement *getStatement(const char *tableName, int statementIdx);
const std::string& getStatementSql(const char *tableName, int statementIdx);
SqlStatement *prepareStatement(const std::string& id,
const std::string& sql);
SqlStatement *getOrPrepareStatement(const std::string& sql);
template <class C> void prepareStatements();
template <class C> std::string manyToManyJoinId(const std::string& joinName,
const std::string& notId);
SqlConnection *useConnection();
void returnConnection(SqlConnection *connection);
SqlConnection *connection(bool openTransaction);
template <class C> friend class MetaDbo;
template <class C> friend class collection;
template <class C> friend class weak_ptr;
template <class C, typename S> friend class Query;
template <class C> friend class Impl::QueryBase;
template <class C, typename T> friend struct Impl::LoadHelper;
template <typename V> friend class FieldRef;
template <class C> friend struct query_result_traits;
template <class C> friend class SaveDbAction;
template <class C> friend class LoadDbAction;
template <class C> friend class PtrRef;
friend class Call;
friend class CollectionHelper;
friend class DboAction;
friend class DropSchema;
friend class FromAnyAction;
friend class InitSchema;
friend class LoadBaseAction;
friend class MetaDboBase;
friend class SaveBaseAction;
friend class SessionAddAction;
friend class ToAnysAction;
friend class Transaction;
friend class TransactionDoneAction;
friend struct Transaction::Impl;
};
}
}
#endif // WT_SESSION_H_
|