DbHelper.h 899 B

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