DataQuery.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "../../../Include/RmlUi/Core/Elements/DataQuery.h"
  29. #include "../../../Include/RmlUi/Core/Elements/DataSource.h"
  30. #include <algorithm>
  31. namespace Rml {
  32. class DataQuerySort
  33. {
  34. public:
  35. DataQuerySort(const StringList& _order_parameters)
  36. {
  37. order_parameters = _order_parameters;
  38. }
  39. bool operator()(const StringList& RMLUI_UNUSED_PARAMETER(left), const StringList& RMLUI_UNUSED_PARAMETER(right))
  40. {
  41. RMLUI_UNUSED(left);
  42. RMLUI_UNUSED(right);
  43. return false;
  44. }
  45. private:
  46. StringList order_parameters;
  47. };
  48. DataQuery::DataQuery(DataSource* data_source, const String& table, const String& _fields, int offset, int limit, const String& order)
  49. {
  50. ExecuteQuery(data_source, table, _fields, offset, limit, order);
  51. }
  52. DataQuery::DataQuery()
  53. {
  54. data_source = nullptr;
  55. table = "";
  56. offset = -1;
  57. limit = -1;
  58. current_row = -1;
  59. }
  60. DataQuery::~DataQuery()
  61. {
  62. }
  63. void DataQuery::ExecuteQuery(DataSource* _data_source, const String& _table, const String& _fields, int _offset, int _limit, const String& order)
  64. {
  65. data_source = _data_source;
  66. table = _table;
  67. offset = _offset;
  68. limit = _limit;
  69. // Set up the field list and field index cache.
  70. StringUtilities::ExpandString(fields, _fields);
  71. for (size_t i = 0; i < fields.size(); i++)
  72. {
  73. field_indices[fields[i]] = i;
  74. }
  75. // Initialise the row pointer.
  76. current_row = -1;
  77. // If limit is -1, then we fetch to the end of the data source.
  78. if (limit == -1)
  79. {
  80. limit = data_source->GetNumRows(table) - offset;
  81. }
  82. if (!order.empty())
  83. {
  84. // Fetch the rows from offset to limit.
  85. rows.resize(limit);
  86. for (int i = 0; i < limit; i++)
  87. {
  88. data_source->GetRow(rows[i], table, offset + i, fields);
  89. }
  90. // Now sort the rows, based on the ordering requirements.
  91. StringList order_parameters;
  92. StringUtilities::ExpandString(order_parameters, order);
  93. std::sort(rows.begin(), rows.end(), DataQuerySort(order_parameters));
  94. }
  95. }
  96. bool DataQuery::NextRow()
  97. {
  98. current_row++;
  99. if (current_row >= limit)
  100. {
  101. return false;
  102. }
  103. LoadRow();
  104. return true;
  105. }
  106. bool DataQuery::IsFieldSet(const String& field) const
  107. {
  108. FieldIndices::const_iterator itr = field_indices.find(field);
  109. if (itr == field_indices.end() || (*itr).second >= rows[current_row].size())
  110. {
  111. return false;
  112. }
  113. return true;
  114. }
  115. void DataQuery::LoadRow()
  116. {
  117. RMLUI_ASSERT(current_row <= (int)rows.size());
  118. if (current_row >= (int)rows.size())
  119. {
  120. rows.push_back(StringList());
  121. data_source->GetRow(rows[current_row], table, offset + current_row, fields);
  122. }
  123. }
  124. } // namespace Rml