DataQuery.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. #include "../../Include/Rocket/Controls/DataQuery.h"
  28. #include "../../Include/Rocket/Controls/DataSource.h"
  29. #include <algorithm>
  30. namespace Rocket {
  31. namespace Controls {
  32. class DataQuerySort
  33. {
  34. public:
  35. DataQuerySort(const Rocket::Core::StringList& _order_parameters)
  36. {
  37. order_parameters = _order_parameters;
  38. }
  39. bool operator()(const Rocket::Core::StringList& ROCKET_UNUSED_PARAMETER(left), const Rocket::Core::StringList& ROCKET_UNUSED_PARAMETER(right))
  40. {
  41. ROCKET_UNUSED(left);
  42. ROCKET_UNUSED(right);
  43. return false;
  44. }
  45. private:
  46. Rocket::Core::StringList order_parameters;
  47. };
  48. DataQuery::DataQuery(DataSource* data_source, const Rocket::Core::String& table, const Rocket::Core::String& _fields, int offset, int limit, const Rocket::Core::String& order)
  49. {
  50. ExecuteQuery(data_source, table, _fields, offset, limit, order);
  51. }
  52. DataQuery::DataQuery()
  53. {
  54. data_source = NULL;
  55. table = "";
  56. offset = -1;
  57. limit = -1;
  58. }
  59. DataQuery::~DataQuery()
  60. {
  61. }
  62. void DataQuery::ExecuteQuery(DataSource* _data_source, const Rocket::Core::String& _table, const Rocket::Core::String& _fields, int _offset, int _limit, const Rocket::Core::String& order)
  63. {
  64. data_source = _data_source;
  65. table = _table;
  66. offset = _offset;
  67. limit = _limit;
  68. // Set up the field list and field index cache.
  69. Rocket::Core::StringUtilities::ExpandString(fields, _fields);
  70. for (size_t i = 0; i < fields.size(); i++)
  71. {
  72. field_indices[fields[i]] = i;
  73. }
  74. // Initialise the row pointer.
  75. current_row = -1;
  76. // If limit is -1, then we fetch to the end of the data source.
  77. if (limit == -1)
  78. {
  79. limit = data_source->GetNumRows(table) - offset;
  80. }
  81. if (!order.Empty())
  82. {
  83. // Fetch the rows from offset to limit.
  84. rows.resize(limit);
  85. for (int i = 0; i < limit; i++)
  86. {
  87. data_source->GetRow(rows[i], table, offset + i, fields);
  88. }
  89. // Now sort the rows, based on the ordering requirements.
  90. Rocket::Core::StringList order_parameters;
  91. Rocket::Core::StringUtilities::ExpandString(order_parameters, order);
  92. sort(rows.begin(), rows.end(), DataQuerySort(order_parameters));
  93. }
  94. }
  95. bool DataQuery::NextRow()
  96. {
  97. current_row++;
  98. if (current_row >= limit)
  99. {
  100. return false;
  101. }
  102. LoadRow();
  103. return true;
  104. }
  105. bool DataQuery::IsFieldSet(const Rocket::Core::String& field) const
  106. {
  107. FieldIndices::const_iterator itr = field_indices.find(field);
  108. if (itr == field_indices.end() || (*itr).second >= (int)rows[current_row].size())
  109. {
  110. return false;
  111. }
  112. return true;
  113. }
  114. void DataQuery::LoadRow()
  115. {
  116. ROCKET_ASSERT(current_row <= (int)rows.size());
  117. if (current_row >= (int)rows.size())
  118. {
  119. rows.push_back(Rocket::Core::StringList());
  120. data_source->GetRow(rows[current_row], table, offset + current_row, fields);
  121. }
  122. }
  123. }
  124. }