DataQuery.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #ifndef ROCKETCONTROLSDATAQUERY_H
  28. #define ROCKETCONTROLSDATAQUERY_H
  29. #include <Rocket/Controls/Header.h>
  30. #include <Rocket/Core/TypeConverter.h>
  31. #include <Rocket/Core/Log.h>
  32. namespace Rocket {
  33. namespace Controls {
  34. class DataSource;
  35. /**
  36. DataQuery
  37. @author Robert Curry
  38. Represents a request for information from an DataSource, encapsulates the result and offers
  39. mechanisms to iterate through the returned rows.
  40. */
  41. class ROCKETCONTROLS_API DataQuery
  42. {
  43. public:
  44. DataQuery();
  45. DataQuery(DataSource* data_source, const Rocket::Core::String& table, const Rocket::Core::String& fields, int offset = 0, int limit = -1, const Rocket::Core::String& order = "");
  46. virtual ~DataQuery();
  47. void ExecuteQuery(DataSource* data_source, const Rocket::Core::String& table, const Rocket::Core::String& fields, int offset = 0, int limit = -1, const Rocket::Core::String& order = "");
  48. bool NextRow();
  49. bool IsFieldSet(const Rocket::Core::String& field) const;
  50. template< typename T >
  51. T Get(const Rocket::Core::String& field_name, const T& default_value) const
  52. {
  53. FieldIndices::const_iterator itr = field_indices.find(field_name);
  54. if (itr == field_indices.end())
  55. {
  56. Rocket::Core::Log::Message(Rocket::Core::Log::LT_ERROR, "Field %s not found in query", field_name.CString());
  57. return default_value;
  58. }
  59. T return_value = default_value;
  60. GetInto((*itr).second, return_value);
  61. return return_value;
  62. }
  63. template< typename T >
  64. bool GetInto(const Rocket::Core::String& field_name, T& value) const
  65. {
  66. FieldIndices::const_iterator itr = field_indices.find(field_name);
  67. if (itr == field_indices.end())
  68. {
  69. Rocket::Core::Log::Message(Rocket::Core::Log::LT_ERROR, "Field %s not found in query", field_name.CString());
  70. return false;
  71. }
  72. return GetInto((*itr).second, value);
  73. }
  74. template< typename T >
  75. T Get(const size_t field_index, const T& default_value) const
  76. {
  77. T return_value = default_value;
  78. GetInto(field_index, return_value);
  79. return return_value;
  80. }
  81. template< typename T >
  82. bool GetInto(const size_t field_index, T& value) const
  83. {
  84. if (field_index < rows[current_row].size())
  85. {
  86. return Rocket::Core::TypeConverter< Rocket::Core::String, T >::Convert(rows[current_row][field_index], value);
  87. }
  88. return false;
  89. }
  90. size_t GetNumFields()
  91. {
  92. return rows[current_row].size();
  93. }
  94. private:
  95. Rocket::Core::StringList fields;
  96. DataSource* data_source;
  97. Rocket::Core::String table;
  98. int current_row;
  99. int offset;
  100. int limit;
  101. typedef std::vector< Rocket::Core::StringList > Rows;
  102. Rows rows;
  103. typedef std::map< Rocket::Core::String, size_t > FieldIndices;
  104. FieldIndices field_indices;
  105. void LoadRow();
  106. };
  107. }
  108. }
  109. #endif // ROCKETCONTROLSDATAQUERY_H