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

SourceForge.net Logo