LArSoft  v07_13_02
Liquid Argon Software toolkit - http://larsoft.org/
Column.cpp
Go to the documentation of this file.
1 #include <iostream>
2 #include <fstream>
3 #include <sys/stat.h>
4 #include <cstdlib>
5 #include <cctype>
6 #include <ctime>
7 #include <algorithm>
8 
12 
13 //************************************************************
14 namespace nutools {
15  namespace dbi {
16 
18  fModified(false)
19  {
20  fValue = 0;
21  fType = kIntLike;
22 
23  std::string t = c.Type();
24  if (t == "timestamp") fType = kTimeStamp;
25  else if (t == "date") fType = kDateStamp;
26  else if (t == "bool") fType = kBool;
27  else if (t == "float" || t == "double") fType = kFloatLike;
28  else if (t == "string" || t == "text") fType = kString;
29  else if (t == "autoincr") fType = kAutoIncr;
30 
31  }
32 
33  //************************************************************
34 
36  {
37  if (!c.fValue)
38  fValue=0;
39  else {
40  fValue = new char[strlen(c.fValue)+1];
41  strcpy(fValue,c.fValue);
42  }
43  fType = c.fType;
44  fModified = c.fModified;
45 
46  }
47 
48  //************************************************************
49 
51  {
52  if (fValue) delete[] fValue;
53  }
54 
55  //************************************************************
56  void Column::Clear()
57  {
58  if (fValue) {
59  delete fValue;
60  fValue = 0;
61  }
62  // fIsNull = true;
63  fModified = false;
64  }
65 
66  //************************************************************
67  bool Column::operator >= (const Column& c) const
68  {
69  if (c.Type() != fType) return false;
70 
71  std::string val1 = fValue;
72  std::string val2 = c.fValue;
73 
74  if (fType == kBool) {
75  bool a = (val1 == "1");
76  bool b = (val2 == "1");
77  return ((a == b) || (a&!b));
78  }
79 
80  if (fType == kIntLike) {
81  long a = boost::lexical_cast<long>(val1);
82  long b = boost::lexical_cast<long>(val2);
83  return (a >= b);
84  }
85 
86  if (fType == kFloatLike) {
87  double a = boost::lexical_cast<double>(val1);
88  double b = boost::lexical_cast<double>(val2);
89  return (a >= b);
90  }
91 
92  if (fType == kString)
93  return (val1 >= val2);
94 
95  if (fType == kTimeStamp) {
96  std::string a, b;
97  if (this->Get(a) && c.Get(b)) {
98  time_t tta, ttb;
101  return (tta >= ttb);
102  }
103  else
104  return false;
105  }
106 
107  if (fType == kDateStamp) {
108  std::string a, b;
109  if (this->Get(a) && c.Get(b)) {
110  time_t tta, ttb;
113  return (tta >= ttb);
114  }
115  else
116  return false;
117  }
118  return false;
119 
120  }
121 
122  //************************************************************
123  bool Column::operator > (const Column& c) const
124  {
125  if (c.Type() != fType) return false;
126 
127  std::string val1 = fValue;
128  std::string val2 = c.fValue;
129 
130  if (fType == kBool) {
131  bool a = (val1 == "1");
132  bool b = (val2 == "1");
133  return (a&!b);
134  }
135 
136  if (fType == kIntLike) {
137  long a = boost::lexical_cast<long>(val1);
138  long b = boost::lexical_cast<long>(val2);
139  return (a > b);
140  }
141 
142  if (fType == kFloatLike) {
143  double a = boost::lexical_cast<double>(val1);
144  double b = boost::lexical_cast<double>(val2);
145  return (a > b);
146  }
147 
148  if (fType == kString)
149  return (val1 > val2);
150 
151  if (fType == kTimeStamp) {
152  std::string a, b;
153  if (this->Get(a) && c.Get(b)) {
154  time_t tta, ttb;
157  return (tta > ttb);
158  }
159  else
160  return false;
161  }
162 
163  if (fType == kDateStamp) {
164  std::string a, b;
165  if (this->Get(a) && c.Get(b)) {
166  time_t tta, ttb;
169  return (tta > ttb);
170  }
171  else
172  return false;
173  }
174  return false;
175 
176  }
177 
178  //************************************************************
179  bool Column::operator <= (const Column& c) const
180  {
181  if (c.fType != fType) return false;
182 
183  std::string val1 = fValue;
184  std::string val2 = c.fValue;
185 
186  if (fType == kBool) {
187  bool a = (val1 == "1");
188  bool b = (val2 == "1");
189  return ((a == b) || (!a&b));
190  }
191 
192  if (fType == kIntLike) {
193  long a = boost::lexical_cast<long>(val1);
194  long b = boost::lexical_cast<long>(val2);
195  return (a <= b);
196  }
197 
198  if (fType == kFloatLike) {
199  double a = boost::lexical_cast<double>(val1);
200  double b = boost::lexical_cast<double>(val2);
201  return (a <= b);
202  }
203 
204  if (fType == kString)
205  return (val1 <= val2);
206 
207  if (fType == kTimeStamp) {
208  std::string a, b;
209  if (this->Get(a) && c.Get(b)) {
210  time_t tta, ttb;
213  return (tta <= ttb);
214  }
215  else
216  return false;
217  }
218 
219  if (fType == kDateStamp) {
220  std::string a, b;
221  if (this->Get(a) && c.Get(b)) {
222  time_t tta, ttb;
225  return (tta <= ttb);
226  }
227  else
228  return false;
229  }
230 
231  return false;
232 
233  }
234 
235  //************************************************************
236  bool Column::operator < (const Column& c) const
237  {
238  if (c.fType != fType) return false;
239 
240  std::string val1 = fValue;
241  std::string val2 = c.fValue;
242 
243  if (fType == kBool) {
244  bool a = (val1 == "1");
245  bool b = (val2 == "1");
246  return (!a&b);
247  }
248 
249  if (fType == kIntLike) {
250  long a = boost::lexical_cast<long>(val1);
251  long b = boost::lexical_cast<long>(val2);
252  return (a < b);
253  }
254 
255  if (fType == kFloatLike) {
256  double a = boost::lexical_cast<double>(val1);
257  double b = boost::lexical_cast<double>(val2);
258  return (a < b);
259  }
260 
261  if (fType == kString)
262  return (val1 < val2);
263 
264  if (fType == kTimeStamp) {
265  std::string a, b;
266  if (this->Get(a) && c.Get(b)) {
267  time_t tta, ttb;
270  return (tta < ttb);
271  }
272  else
273  return false;
274  }
275 
276  if (fType == kDateStamp) {
277  std::string a, b;
278  if (this->Get(a) && c.Get(b)) {
279  time_t tta, ttb;
282  return (tta < ttb);
283  }
284  else
285  return false;
286  }
287 
288  return false;
289 
290  }
291  //************************************************************
292  bool Column::operator == (const Column& c) const
293  {
294  if (c.fType != fType) return false;
295 
296  if (fValue == 0 && (fValue==c.fValue)) return true;
297 
298  return (strcmp(fValue,c.fValue)==0);
299 
300  }
301  }
302 }
uint16_t fType
Definition: Column.h:145
bool operator==(const Column &c) const
Definition: Column.cpp:292
bool operator>(const Column &c) const
Definition: Column.cpp:123
Simple service to provide a RunHistory configured to the right run.
Definition: Column.cpp:14
bool Get(T &val) const
Definition: Column.h:72
bool operator>=(const Column &c) const
Definition: Column.cpp:67
bool operator<=(const Column &c) const
Definition: Column.cpp:179
std::string Type() const
Definition: ColumnDef.h:22
bool operator<(const Column &c) const
Definition: Column.cpp:236
static bool TimeAsStringToTime_t(std::string ts, time_t &t)
Definition: Util.cpp:81
uint8_t Type() const
Definition: Column.h:40
static bool DateAsStringToTime_t(std::string date, time_t &t)
Definition: Util.cpp:106