/usr/lib/Wt/test/dbo/DboTest2.C is in witty-examples 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 | /*
* Copyright (C) 2009 Emweb bvba, Kessel-Lo, Belgium.
*
* See the LICENSE file for terms of use.
*/
#ifdef WTDBO
#include <boost/test/unit_test.hpp>
#include <Wt/Dbo/Dbo>
#include <Wt/Dbo/backend/Postgres>
#include <Wt/Dbo/backend/Sqlite3>
#include <Wt/WDate>
#include <Wt/WDateTime>
#include <Wt/WTime>
#include <Wt/Dbo/WtSqlTraits>
#include <Wt/Dbo/ptr_tuple>
namespace dbo = Wt::Dbo;
class Post;
typedef dbo::collection< dbo::ptr<Post> > Posts;
class User {
public:
enum Role {
Visitor = 0,
Admin = 1,
Alien = 42
};
std::string name;
std::string password;
Role role;
int karma;
Posts posts;
template<class Action>
void persist(Action& a)
{
dbo::field(a, name, "name");
dbo::field(a, password, "password");
dbo::field(a, role, "role");
dbo::field(a, karma, "karma");
dbo::hasMany(a, posts, dbo::ManyToOne, "user");
}
};
class Tag;
class Post {
public:
std::string title;
std::string contents;
dbo::ptr<User> user;
dbo::collection< dbo::ptr<Tag> > tags;
template<class Action>
void persist(Action& a)
{
dbo::field(a, title, "title");
dbo::field(a, contents, "contents");
dbo::belongsTo(a, user, "user");
dbo::hasMany(a, tags, dbo::ManyToMany, "post_tags");
}
};
class Tag {
public:
std::string name;
dbo::collection< dbo::ptr<Post> > posts;
template<class Action>
void persist(Action& a)
{
dbo::field(a, name, "name");
dbo::hasMany(a, posts, dbo::ManyToMany, "post_tags");
}
};
struct Dbo2Fixture
{
Dbo2Fixture()
{
#ifdef SQLITE3
connection_ = new dbo::backend::Sqlite3(":memory:");
#endif // SQLITE3
#ifdef POSTGRES
connection_ = new dbo::backend::Postgres
("host=127.0.0.1 user=test password=test port=5432 dbname=test");
#endif // POSTGRES
connection_->setProperty("show-queries", "true");
session_ = new dbo::Session();
session_->setConnection(*connection_);
session_->mapClass<User>("user");
session_->mapClass<Post>("post");
session_->mapClass<Tag>("tag");
session_->createTables();
}
~Dbo2Fixture()
{
session_->dropTables();
delete session_;
delete connection_;
}
dbo::SqlConnection *connection_;
dbo::Session *session_;
};
BOOST_AUTO_TEST_CASE( dbo2_test1 )
{
Dbo2Fixture f;
dbo::Session& session = *f.session_;
dbo::Transaction transaction(session);
User *user = new User();
user->name = "Joe";
user->password = "Secret";
user->role = User::Visitor;
user->karma = 13;
dbo::ptr<User> userPtr = session.add(user);
User *user2 = new User();
user2->name = "Daisy";
user2->password = "Secret2";
user2->role = User::Visitor;
user2->karma = 12;
session.add(user2);
// simple queries: session.find()
dbo::ptr<User> joe = session.find<User>("where name = ?").bind("Joe");
std::cerr << "Joe has karma: " << joe->karma << std::endl;
// any queries: session.query()
dbo::ptr<User> joe2 = session.query< dbo::ptr<User> >
("select u from \"user\" u where name = ?").bind("Joe");
std::cerr << "Indeed, Joe has karma: " << joe2->karma << std::endl;
std::cerr << "Joe == Joe: " << (joe == joe2) << std::endl;
// session.query() can return anything
int count = session.query<int>
("select count(*) from \"user\" where name = ?").bind("Joe");
std::cerr << "There is only " << count << " Joe." << std::endl;
BOOST_REQUIRE(count == 1);
// session.query() and session.find() can return a collection
typedef dbo::collection< dbo::ptr<User> > Users;
Users users = session.find<User>();
std::cerr << "We have " << users.size() << " users:" << std::endl;
for (Users::const_iterator i = users.begin(); i != users.end(); ++i)
std::cerr << " user " << (*i)->name
<< " with karma of " << (*i)->karma << std::endl;
joe = session.find<User>("where name = ?").bind("Joe");
joe.modify()->karma++;
joe.modify()->password = "public";
dbo::ptr<User> silly = session.add(new User());
silly.remove();
dbo::ptr<Post> post = session.add(new Post());
post.modify()->user = joe;
#if 0
{
dbo::ptr<Post> p;
std::string msg;
boost::tie(p, msg)
= session.query<boost::tuple<dbo::ptr<Post>, std::string> >
("SELECT (doc), msg FROM db_function('Howdy!')").resultValue();
}
#endif
std::cerr << "Joe has " << joe->posts.size() << " posts." << std::endl;
dbo::ptr<Tag> cooking = session.add(new Tag());
cooking.modify()->name = "Cooking";
post.modify()->tags.insert(cooking);
std::cerr << cooking->posts.size() << " post(s) tagged with Cooking."
<< std::endl;
transaction.commit();
}
#endif
|