DbHelper.h 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // Copyright: All Rights Reserved
  2. // Author: egametang@gmail.com (tanghai)
  3. #ifndef ORM_SQLHELPER_H
  4. #define ORM_SQLHELPER_H
  5. #include <boost/scoped_ptr.hpp>
  6. #include <boost/make_shared.hpp>
  7. #include <cppconn/driver.h>
  8. #include <cppconn/exception.h>
  9. #include <cppconn/resultset.h>
  10. #include <cppconn/statement.h>
  11. #include <mysql_connection.h>
  12. #include <glog/logging.h>
  13. #include "Orm/DbResult.h"
  14. #include "Orm/Typedef.h"
  15. #include "Orm/Select.h"
  16. namespace Egametang {
  17. class DbHelper
  18. {
  19. private:
  20. boost::scoped_ptr<sql::Connection> connection;
  21. boost::scoped_ptr<sql::Statement> statement;
  22. public:
  23. DbHelper(std::string url, std::string username, std::string password);
  24. virtual ~DbHelper();
  25. template <typename Table>
  26. DbResultPtr Execute(Select<Table> select)
  27. {
  28. std::string sql = select.ToString();
  29. VLOG(2) << "execute sql: " << sql;
  30. ResultSetPtr resultSet(statement->executeQuery(sql));
  31. DbResultPtr dbResult = boost::make_shared<DbResult>(resultSet);
  32. return dbResult;
  33. }
  34. };
  35. } // namespace Egametang
  36. #endif // ORM_SQLHELPER_H