DataQuery.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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/Controls/DataQuery.h"
  29. #include "../../Include/RmlUi/Controls/DataSource.h"
  30. #include <algorithm>
  31. namespace Rml {
  32. namespace Controls {
  33. class DataQuerySort
  34. {
  35. public:
  36. DataQuerySort(const Rml::Core::StringList& _order_parameters)
  37. {
  38. order_parameters = _order_parameters;
  39. }
  40. bool operator()(const Rml::Core::StringList& RMLUI_UNUSED_PARAMETER(left), const Rml::Core::StringList& RMLUI_UNUSED_PARAMETER(right))
  41. {
  42. RMLUI_UNUSED(left);
  43. RMLUI_UNUSED(right);
  44. return false;
  45. }
  46. private:
  47. Rml::Core::StringList order_parameters;
  48. };
  49. DataQuery::DataQuery(DataSource* data_source, const Rml::Core::String& table, const Rml::Core::String& _fields, int offset, int limit, const Rml::Core::String& order)
  50. {
  51. ExecuteQuery(data_source, table, _fields, offset, limit, order);
  52. }
  53. DataQuery::DataQuery()
  54. {
  55. data_source = nullptr;
  56. table = "";
  57. offset = -1;
  58. limit = -1;
  59. }
  60. DataQuery::~DataQuery()
  61. {
  62. }
  63. void DataQuery::ExecuteQuery(DataSource* _data_source, const Rml::Core::String& _table, const Rml::Core::String& _fields, int _offset, int _limit, const Rml::Core::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. Rml::Core::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. Rml::Core::StringList order_parameters;
  92. Rml::Core::StringUtilities::ExpandString(order_parameters, order);
  93. 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 Rml::Core::String& field) const
  107. {
  108. FieldIndices::const_iterator itr = field_indices.find(field);
  109. if (itr == field_indices.end() || (*itr).second >= (int)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(Rml::Core::StringList());
  121. data_source->GetRow(rows[current_row], table, offset + current_row, fields);
  122. }
  123. }
  124. }
  125. }