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
17namespace litesql {
18
20class In;
21class Like;
22class SelectQuery;
23
24class FieldType {
25protected:
26 typedef std::vector< std::pair<std::string, std::string> > Values;
27public:
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 }
51private:
52 std::string _name, _type, _table;
53 Values _values;
54};
55
57template <class From, class To>
58To convert(From value);
59
61template <class T>
62std::string store(const T& value) {
63 return litesql::toString(value);
64// return convert<T,std::string>(value);
65}
66
67template <class T>
68T load(const std::string& value) {
69 return convert<const std::string&, T>(value);
70}
72template <class T>
73class Field {
74 const FieldType * field;
75 bool _modified;
76 T _value;
77public:
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) {
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
115template <>
116class Field<std::string> {
117 const FieldType * field;
118 bool _modified;
119 std::string _value;
120public:
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;
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
156typedef unsigned char u8_t;
157typedef long long bigint;
158
159class Blob {
160public:
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);
190private:
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
198std::ostream& operator << (std::ostream& os, const Blob& blob);
199template <>
200Blob convert<const std::string&, Blob>(const std::string& value);
201template <>
202std::string convert<const Blob&, std::string>(const Blob& value);
203
204template <>
205class Field<Blob> {
206 const FieldType * field;
207 bool _modified;
208 Blob _value;
209public:
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/*
226const 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
248template <class T>
249std::string operator+(std::string a, litesql::Field<T> f) {
250 return a + std::string(f);
251}
252template <class T>
253std::string operator+(litesql::Field<T> f, std::string a) {
254 return std::string(f) + a;
255}
256template <class T>
257std::ostream & operator << (std::ostream & os, const litesql::Field<T> & f) {
258 return os << f.value();
259}
260
261}
262#endif
Definition field.hpp:159
Definition field.hpp:24
In in(const std::string &set) const
syntactic sugar to Expr-API, Object::field_.in(set)
Like like(const std::string &s) const
syntactic sugar to Expr-API, Object::field_.like(s)
Definition field.cpp:20
holds field value
Definition field.hpp:73
in operator
Definition expr.hpp:184
like operator
Definition expr.hpp:177
a class that helps creating SELECT-SQL statements.
Definition selectquery.hpp:18
std::string store(const T &value)
store function
Definition field.hpp:62
To convert(From value)
convert function
helpful string utils
std::string toString(T a)
returns string representation of passed parameter if it can be written to ostringstream
Definition string.hpp:18

SourceForge.net Logo