std::vector<User> Session::topUsers(int limit) { dbo::Transaction transaction(session_); Users top = session_.find<User>().orderBy("score desc").limit(limit); std::vector<User> result; for (Users::const_iterator i = top.begin(); i != top.end(); ++i) { dbo::ptr<User> user = *i; result.push_back(*user); dbo::ptr<AuthInfo> auth = *user->authInfos.begin(); std::string name = auth->identity(Auth::Identity::LoginName).toUTF8(); result.back().name = name; } transaction.commit(); return result; }
SayWindow::SayWindow(Wt::WContainerWidget* parent) : Wt::WContainerWidget(parent) { // Get the user's name to say Hi with HelloApp* app = dynamic_cast<HelloApp*>(Wt::WApplication::instance()); new Wt::WText(Wt::WString("I have the loggedin user as: ") + app->session().user()->getName() + app->userName(), this); addWidget(new Wt::WBreak()); new Wt::WAnchor(Wt::WLink(Wt::WLink::InternalPath, "/ask"), "Go back to ask page", this); // Show a list of users addWidget(new Wt::WBreak()); new Wt::WText(Wt::WString("in database i have to following users:"), this); addWidget(new Wt::WBreak()); addWidget(new Wt::WBreak()); Wt::WTable* table = new Wt::WTable(this); Wt::Dbo::Transaction t(app->session()); Users users = app->userList(); int i=0; for (Users::const_iterator user=users.begin(); user != users.end(); ++user) { table->elementAt(i++, 0)->addWidget( new Wt::WText(Wt::WString("user [number] is:") +(*user)->getName()) ); } }
void run() { /***** * Dbo tutorial section 3. A first session *****/ /* * Setup a session, would typically be done once at application startup. * * For testing, we'll be using Sqlite3's special :memory: database. You * can replace this with an actual filename for actual persistence. */ dbo::backend::Sqlite3 sqlite3(":memory:"); sqlite3.setProperty("show-queries", "true"); dbo::Session session; session.setConnection(sqlite3); session.mapClass<User>("user"); /* * Try to create the schema (will fail if already exists). */ session.createTables(); { dbo::Transaction transaction(session); User *user = new User(); user->name = "Joe"; user->password = "******"; user->role = User::Visitor; user->karma = 13; dbo::ptr<User> userPtr = session.add(user); } /***** * Dbo tutorial section 4. Querying objects *****/ { dbo::Transaction transaction(session); dbo::ptr<User> joe = session.find<User>().where("name = ?").bind("Joe"); std::cerr << "Joe has karma: " << joe->karma << std::endl; dbo::ptr<User> joe2 = session.query< dbo::ptr<User> > ("select u from user u").where("name = ?").bind("Joe"); } { dbo::Transaction transaction(session); 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; } /***** * Dbo tutorial section 5. Updating objects *****/ { dbo::Transaction transaction(session); dbo::ptr<User> joe = session.find<User>().where("name = ?").bind("Joe"); joe.modify()->karma++; joe.modify()->password = "******"; } { dbo::Transaction transaction(session); dbo::ptr<User> joe = session.find<User>().where("name = ?").bind("Joe"); if (joe) joe.remove(); } { dbo::Transaction transaction(session); dbo::ptr<User> silly = session.add(new User()); silly.modify()->name = "Silly"; silly.remove(); } }