libRocket
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
DataQuery.h
Go to the documentation of this file.
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 
28 #ifndef ROCKETCONTROLSDATAQUERY_H
29 #define ROCKETCONTROLSDATAQUERY_H
30 
31 #include "Header.h"
32 #include "../Core/TypeConverter.h"
33 #include "../Core/Log.h"
34 
35 namespace Rocket {
36 namespace Controls {
37 
38 class DataSource;
39 
49 {
50 public:
51  DataQuery();
52  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 = "");
53  virtual ~DataQuery();
54 
55  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 = "");
56  bool NextRow();
57 
58  bool IsFieldSet(const Rocket::Core::String& field) const;
59 
60  template< typename T >
61  T Get(const Rocket::Core::String& field_name, const T& default_value) const
62  {
63  FieldIndices::const_iterator itr = field_indices.find(field_name);
64  if (itr == field_indices.end())
65  {
66  Rocket::Core::Log::Message(Rocket::Core::Log::LT_ERROR, "Field %s not found in query", field_name.CString());
67  return default_value;
68  }
69 
70  T return_value = default_value;
71 
72  GetInto((*itr).second, return_value);
73 
74  return return_value;
75  }
76 
77  template< typename T >
78  bool GetInto(const Rocket::Core::String& field_name, T& value) const
79  {
80  FieldIndices::const_iterator itr = field_indices.find(field_name);
81  if (itr == field_indices.end())
82  {
83  Rocket::Core::Log::Message(Rocket::Core::Log::LT_ERROR, "Field %s not found in query", field_name.CString());
84  return false;
85  }
86 
87  return GetInto((*itr).second, value);
88  }
89 
90  template< typename T >
91  T Get(const size_t field_index, const T& default_value) const
92  {
93  T return_value = default_value;
94 
95  GetInto(field_index, return_value);
96 
97  return return_value;
98  }
99 
100  template< typename T >
101  bool GetInto(const size_t field_index, T& value) const
102  {
103  if (field_index < rows[current_row].size())
104  {
105  return Rocket::Core::TypeConverter< Rocket::Core::String, T >::Convert(rows[current_row][field_index], value);
106  }
107 
108  return false;
109  }
110 
111  size_t GetNumFields()
112  {
113  return rows[current_row].size();
114  }
115 
116 private:
118 
119  DataSource* data_source;
120  Rocket::Core::String table;
121  int current_row;
122  int offset;
123  int limit;
124 
125  typedef std::vector< Rocket::Core::StringList > Rows;
126  Rows rows;
127  typedef std::map< Rocket::Core::String, size_t > FieldIndices;
128  FieldIndices field_indices;
129 
130  void LoadRow();
131 };
132 
133 }
134 }
135 
136 #endif // ROCKETCONTROLSDATAQUERY_H
Definition: DataQuery.h:48
const T * CString() const
Access the string as a standard C string.
Definition: StringBase.h:147
Definition: Clipboard.h:33
T Get(const size_t field_index, const T &default_value) const
Definition: DataQuery.h:91
bool GetInto(const size_t field_index, T &value) const
Definition: DataQuery.h:101
std::vector< String > StringList
Definition: String.h:41
T Get(const Rocket::Core::String &field_name, const T &default_value) const
Definition: DataQuery.h:61
size_t GetNumFields()
Definition: DataQuery.h:111
Definition: DataSource.h:46
bool GetInto(const Rocket::Core::String &field_name, T &value) const
Definition: DataQuery.h:78
#define ROCKETCONTROLS_API
Definition: Header.h:41
static void Message(Type type, const char *format,...)
Definition: Log.h:49
static bool Convert(const SourceType &src, DestType &dest)
Definition: TypeConverter.h:30