LiteSQL  0.3.10
field.hpp
Go to the documentation of this file.
1 /* LiteSQL
2  *
3  * The list of contributors at http://litesql.sf.net/
4  *
5  * See LICENSE for copyright information. */
6 
7 #ifndef litesql_field_hpp
8 #define litesql_field_hpp
9 #include <iostream>
10 #include <vector>
11 #include <utility>
12 #include <string>
13 #include "litesql/string.hpp"
14 
17 namespace litesql {
18 
20 class In;
21 class Like;
22 class SelectQuery;
23 
24 class FieldType {
25 protected:
26  typedef std::vector< std::pair<std::string, std::string> > Values;
27 public:
28  FieldType() {};
29  FieldType(const std::string& n,
30  const std::string& t,
31  const std::string& tbl,
32  const Values& vals = Values())
33  : _name(n), _type(t), _table(tbl), _values(vals) {}
34  std::string fullName() const { return table() + "." + name(); }
35  std::string name() const { return _name; }
36  std::string type() const { return _type; }
37  std::string table() const { return _table; }
38  std::vector< std::pair<std::string, std::string> > values() { return _values; }
40  In in(const std::string& set) const;
42  In in(const SelectQuery& sel) const;
44  Like like(const std::string& s) const;
45  bool operator==(const FieldType & fd) const {
46  return fd.fullName() == fullName();
47  }
48  bool operator!=(const FieldType & fd) const {
49  return ! (*this == fd);
50  }
51 private:
52  std::string _name, _type, _table;
53  Values _values;
54 };
55 
57 template <class From, class To>
58 To convert(From value);
59 
61 template <class T>
62 std::string store(const T& value) {
63  return litesql::toString(value);
64 // return convert<T,std::string>(value);
65 }
66 
67 template <class T>
68 T load(const std::string& value) {
69  return convert<const std::string&, T>(value);
70 }
72 template <class T>
73 class Field {
74  const FieldType * field;
75  bool _modified;
76  T _value;
77 public:
78  Field(const FieldType & f) : field(&f), _modified(true) {}
79  std::string fullName() const { return field->fullName(); }
80  std::string name() const { return field->name(); }
81  std::string type() const { return field->type(); }
82  std::string table() const { return field->table(); }
83  T value() const { return _value; }
84  const FieldType & fieldType() const { return *field; }
85  bool modified() const { return _modified; }
86  void setModified(bool state) { _modified = state; }
87  const Field & operator=(const std::string& v) {
88  _value = convert<const std::string&, T>(v);
89  _modified = true;
90  return *this;
91  }
92  const Field & operator=(const T& v) {
93  _value = v;
94  _modified = true;
95  return *this;
96  }
97  template <class T2>
98  const Field & operator=(T2 v) {
99  _modified = true;
100  _value = litesql::convert<T2, T>(v);
101  return *this;
102  }
103  template <class T2>
104  bool operator==(const T2& v) const {
105  return litesql::toString(_value) == litesql::toString(v);
106  }
107  template <class T2>
108  bool operator!=(const T2& v) const { return !(*this == v); }
109 
110  operator std::string() const { return toString(value()); }
111 
112  operator T() const { return value(); }
113 };
114 
115 template <>
116 class Field<std::string> {
117  const FieldType * field;
118  bool _modified;
119  std::string _value;
120 public:
121  Field(const FieldType & f) : field(&f), _modified(true) {}
122  std::string fullName() const { return field->fullName(); }
123  std::string name() const { return field->name(); }
124  std::string type() const { return field->type(); }
125  std::string table() const { return field->table(); }
126  std::string value() const { return _value; }
127  const FieldType & fieldType() const { return *field; }
128  bool modified() const { return _modified; }
129  void setModified(bool state) { _modified = state; }
130  const Field & operator=(std::string v) {
131  _value = v;
132  _modified = true;
133  return *this;
134  }
135  const Field& operator=(const char * v) {
136  _value = v;
137  _modified = true;
138  return *this;
139  }
140  template <class T2>
141  const Field & operator=(T2 v) {
142  _modified = true;
143  _value = litesql::convert<T2, std::string>(v);
144  return *this;
145  }
146  template <class T2>
147  bool operator==(const T2& v) const {
148  return litesql::toString(_value) == litesql::toString(v);
149  }
150  template <class T2>
151  bool operator!=(const T2& v) const { return !(*this == v); }
152 
153  operator std::string() const { return value(); }
154 };
155 
156 typedef unsigned char u8_t;
157 typedef long long bigint;
158 
159 class Blob {
160 public:
161  static const Blob nil;
162  Blob() : m_data(NULL),m_length(0) {};
163  Blob(const std::string & value) : m_data(NULL),m_length(0)
164  {
165  initWithHexString(value);
166  };
167 
168 
169  Blob(const Blob & b) : m_data(NULL)
170  {
171  initWithData(b.m_data,b.m_length);
172  };
173 
174  Blob(const void* data, size_t length) : m_data(NULL), m_length(0)
175  {
176  initWithData((u8_t*)data,length);
177  };
178 
179  virtual ~Blob();
180  const Blob& operator=(const Blob& v) {
181  initWithData(v.m_data,v.m_length);
182  return *this;
183  }
184 
185  std::string toHex() const ;
186  size_t length() const { return m_length; };
187  bool isNull() const { return m_data==NULL; };
188  u8_t data(size_t index) const { return m_data[index]; };
189  void data(const char* pszData);
190 private:
191  u8_t* m_data;
192  size_t m_length;
193 
194  void initWithData(const u8_t* data, size_t length);
195  void initWithHexString(const std::string& hexString);
196 };
197 
198 std::ostream& operator << (std::ostream& os, const Blob& blob);
199 template <>
200 Blob convert<const std::string&, Blob>(const std::string& value);
201 template <>
202 std::string convert<const Blob&, std::string>(const Blob& value);
203 
204 template <>
205 class Field<Blob> {
206  const FieldType * field;
207  bool _modified;
208  Blob _value;
209 public:
210  Field(const FieldType & f) : field(&f), _modified(true) {}
211  std::string fullName() const { return field->fullName(); }
212  std::string name() const { return field->name(); }
213  std::string type() const { return field->type(); }
214  std::string table() const { return field->table(); }
215  Blob value() const { return _value; }
216  const FieldType & fieldType() const { return *field; }
217  bool modified() const { return _modified; }
218  void setModified(bool state) { _modified = state; }
219  const Field & operator=(const Blob& v) {
220  _value = v;
221  _modified = true;
222  return *this;
223  }
224 
225 /*
226 const Field& operator=(const char * v) {
227  _value = v;
228  _modified = true;
229  return *this;
230  }
231  template <class T2>
232  const Field & operator=(T2 v) {
233  _modified = true;
234  _value = litesql::convert<T2, Blob>(v);
235  return *this;
236  }
237  template <class T2>
238  bool operator==(const T2& v) const {
239  return litesql::toString(_value) == litesql::toString(v);
240  }
241  template <class T2>
242  bool operator!=(const T2& v) const { return !(*this == v); }
243 */
244 
245  operator std::string() const { return _value.toHex(); }
246 };
247 
248 template <class T>
249 std::string operator+(std::string a, litesql::Field<T> f) {
250  return a + std::string(f);
251 }
252 template <class T>
253 std::string operator+(litesql::Field<T> f, std::string a) {
254  return std::string(f) + a;
255 }
256 template <class T>
257 std::ostream & operator << (std::ostream & os, const litesql::Field<T> & f) {
258  return os << f.value();
259 }
260 
261 }
262 #endif
Definition: backend.hpp:14
holds field value
Definition: field.hpp:73
in operator
Definition: expr.hpp:184
std::string toString(T a)
returns string representation of passed parameter if it can be written to ostringstream ...
Definition: string.hpp:18
like operator
Definition: expr.hpp:177
Definition: field.hpp:159
a class that helps creating SELECT-SQL statements.
Definition: selectquery.hpp:18
Like like(const std::string &s) const
syntactic sugar to Expr-API, Object::field_.like(s)
Definition: field.cpp:20
helpful string utils
Definition: field.hpp:24
In in(const std::string &set) const
syntactic sugar to Expr-API, Object::field_.in(set)
std::string store(const T &value)
store function
Definition: field.hpp:62
To convert(From value)
convert function

SourceForge.net Logo