DbHelper.h 960 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  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 <glog/logging.h>
  11. #include "Orm/DbResult.h"
  12. #include "Orm/Typedef.h"
  13. #include "Orm/Select.h"
  14. namespace Egametang {
  15. class DbHelper
  16. {
  17. private:
  18. std::unique_ptr<sql::Connection> connection;
  19. std::unique_ptr<sql::Statement> statement;
  20. public:
  21. DbHelper(std::string url, std::string username, std::string password);
  22. virtual ~DbHelper();
  23. template <typename Table>
  24. DbResultPtr Execute(Select<Table> select)
  25. {
  26. std::string sql = select.ToString();
  27. VLOG(2) << "execute sql: " << sql;
  28. ResultSetPtr resultSet(statement->executeQuery(sql));
  29. auto dbResult = std::make_shared<DbResult>(resultSet);
  30. return dbResult;
  31. }
  32. };
  33. } // namespace Egametang
  34. #endif // ORM_SQLHELPER_H