DataQuery.cpp 3.8 KB

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