cql_connection.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Copyright (C) 2015 - 2016 Deutsche Telekom AG.
  2. //
  3. // Author: Razvan Becheriu <razvan.becheriu@qualitance.com>
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. #ifndef CQL_CONNECTION_H
  17. #define CQL_CONNECTION_H
  18. #include <dhcpsrv/database_connection.h>
  19. #include <dhcpsrv/dhcpsrv_log.h>
  20. #include <inttypes.h>
  21. #include <cassandra.h>
  22. #include <vector>
  23. namespace isc {
  24. namespace dhcp {
  25. /// @brief Defines a single query
  26. ///
  27. /// @param params_ Bind parameter names
  28. /// @param name_ Short name of the query.
  29. /// @param text_ Text representation of the actual query.
  30. struct CqlTaggedStatement {
  31. const char** params_;
  32. const char* name_;
  33. const char* text_;
  34. };
  35. // Defines CQL backend version: 2.3
  36. const uint32_t CQL_DRIVER_VERSION_MAJOR = CASS_VERSION_MAJOR;
  37. const uint32_t CQL_DRIVER_VERSION_MINOR = CASS_VERSION_MINOR;
  38. /// Defines CQL schema version: 1.0
  39. const uint32_t CQL_SCHEMA_VERSION_MAJOR = 1;
  40. const uint32_t CQL_SCHEMA_VERSION_MINOR = 0;
  41. class CqlConnection : public DatabaseConnection {
  42. public:
  43. /// @brief Constructor
  44. ///
  45. /// Initialize CqlConnection object with parameters needed for connection.
  46. CqlConnection(const ParameterMap& parameters);
  47. /// @brief Destructor
  48. virtual ~CqlConnection();
  49. /// @brief Prepare statements
  50. ///
  51. /// Creates the prepared statements for all of the CQL statements used
  52. /// by the CQL backend.
  53. ///
  54. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  55. /// failed.
  56. /// @throw isc::InvalidParameter 'index' is not valid for the vector. This
  57. /// represents an internal error within the code.
  58. void prepareStatements(CqlTaggedStatement *statements);
  59. /// @brief Open Database
  60. ///
  61. /// Opens the database using the information supplied in the parameters
  62. /// passed to the constructor. If no parameters are supplied, the default
  63. /// values will be used (keyspace keatest).
  64. ///
  65. /// @throw DbOpenError Error opening the database
  66. void openDatabase();
  67. /// @brief Commit Transactions
  68. ///
  69. /// This is a no-op for Cassandra.
  70. virtual void commit();
  71. /// @brief Rollback Transactions
  72. ///
  73. /// This is a no-op for Cassandra.
  74. virtual void rollback();
  75. /// @brief Check Error
  76. ///
  77. /// Chech error for current database operation.
  78. void checkStatementError(std::string& error, CassFuture* future,
  79. uint32_t stindex, const char* what) const;
  80. /// @brief Check Error
  81. ///
  82. /// Chech error for current database operation.
  83. void checkStatementError(std::string& error, CassFuture* future,
  84. const char* what) const;
  85. /// @brief CQL connection handle
  86. CassCluster* cluster_;
  87. /// @brief CQL session handle
  88. CassSession* session_;
  89. /// @brief CQL prepared statements - used for faster statement execution using
  90. /// bind functionality
  91. std::vector<const CassPrepared*> statements_;
  92. /// @brief Pointer to external array of tagged statements containing statement
  93. /// name, array of names of bind parameters and text query
  94. CqlTaggedStatement* tagged_statements_;
  95. };
  96. }; // end of isc::dhcp namespace
  97. }; // end of isc namespace
  98. #endif // CQL_CONNECTION_H