LArSoft  v07_13_02
Liquid Argon Software toolkit - http://larsoft.org/
nutools::dbi::Column Class Reference

#include "Column.h"

Public Member Functions

 Column ()
 
 Column (const ColumnDef &c)
 
 Column (const Column &c)
 
 ~Column ()
 
uint8_t Type () const
 
std::string Value () const
 
bool IsNull () const
 
bool Modified () const
 
void Clear ()
 
void SetType (uint8_t t)
 
void FastSet (std::string v)
 
void FastSet (const char *v)
 
template<class T >
bool Get (T &val) const
 
template<class T >
bool Set (const T &val, bool ignoreAutoIncr=false)
 
template<class T >
bool Update (const T &val)
 
bool operator>= (const Column &c) const
 
bool operator<= (const Column &c) const
 
bool operator> (const Column &c) const
 
bool operator< (const Column &c) const
 
bool operator== (const Column &c) const
 

Private Attributes

bool fModified
 
uint16_t fType
 
char * fValue
 

Friends

std::ostream & operator<< (std::ostream &stream, const Column &col)
 

Detailed Description

Generalized Database Column Interface

Author
Jonathan Paley
Version
Id
Column.h,v 1.24 2013/03/12 19:53:02 jpaley Exp

Definition at line 32 of file Column.h.

Constructor & Destructor Documentation

nutools::dbi::Column::Column ( )
inline

Definition at line 35 of file Column.h.

References fType, fValue, nutools::dbi::kIntLike, and ~Column().

nutools::dbi::Column::Column ( const ColumnDef c)

Definition at line 17 of file Column.cpp.

References fType, fValue, nutools::dbi::kAutoIncr, nutools::dbi::kBool, nutools::dbi::kDateStamp, nutools::dbi::kFloatLike, nutools::dbi::kIntLike, nutools::dbi::kString, nutools::dbi::kTimeStamp, and nutools::dbi::ColumnDef::Type().

17  :
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  }
uint16_t fType
Definition: Column.h:145
nutools::dbi::Column::Column ( const Column c)

Definition at line 35 of file Column.cpp.

References fModified, fType, and fValue.

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  }
uint16_t fType
Definition: Column.h:145
nutools::dbi::Column::~Column ( )

Definition at line 50 of file Column.cpp.

References fValue.

Referenced by Column().

51  {
52  if (fValue) delete[] fValue;
53  }

Member Function Documentation

void nutools::dbi::Column::Clear ( )

Definition at line 56 of file Column.cpp.

References fModified, and fValue.

Referenced by Modified().

57  {
58  if (fValue) {
59  delete fValue;
60  fValue = 0;
61  }
62  // fIsNull = true;
63  fModified = false;
64  }
void nutools::dbi::Column::FastSet ( std::string  v)
inline

Definition at line 53 of file Column.h.

References fValue.

53  {
54  if (fValue) {
55  delete fValue;
56  fValue=0;
57  }
58  fValue = new char[v.length()+1];
59  strcpy(fValue,v.c_str());
60  }
void nutools::dbi::Column::FastSet ( const char *  v)
inline

Definition at line 62 of file Column.h.

References fValue.

62  {
63  if (fValue) {
64  delete fValue;
65  fValue=0;
66  }
67  fValue = new char[strlen(v)+1];
68  strcpy(fValue,v);
69  }
template<class T >
bool nutools::dbi::Column::Get ( T &  val) const
inline

Definition at line 72 of file Column.h.

References fValue.

Referenced by operator<(), operator<=(), operator>(), and operator>=().

72  {
73  if (fValue) {
74  try {
75  val = boost::lexical_cast<T>(std::string(fValue));
76  }
77  catch (boost::bad_lexical_cast &) {
78  std::cerr << "Column::Get(): Bad_lexical_cast! Value = "
79  << fValue << std::endl;
80  return false;
81  }
82  return true;
83  }
84 
85  return false;
86  }
bool nutools::dbi::Column::IsNull ( ) const
inline

Definition at line 44 of file Column.h.

References fValue.

Referenced by nutools::dbi::Table::AddRow().

44 { return (!fValue); }
bool nutools::dbi::Column::Modified ( ) const
inline

Definition at line 45 of file Column.h.

References Clear(), and fModified.

Referenced by nutools::dbi::Table::WriteToDB().

45 { return fModified; }
bool nutools::dbi::Column::operator< ( const Column c) const

Definition at line 236 of file Column.cpp.

References nutools::dbi::Util::DateAsStringToTime_t(), fType, fValue, Get(), nutools::dbi::kBool, nutools::dbi::kDateStamp, nutools::dbi::kFloatLike, nutools::dbi::kIntLike, nutools::dbi::kString, nutools::dbi::kTimeStamp, and nutools::dbi::Util::TimeAsStringToTime_t().

Referenced by Update().

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  }
uint16_t fType
Definition: Column.h:145
bool Get(T &val) const
Definition: Column.h:72
static bool TimeAsStringToTime_t(std::string ts, time_t &t)
Definition: Util.cpp:81
static bool DateAsStringToTime_t(std::string date, time_t &t)
Definition: Util.cpp:106
bool nutools::dbi::Column::operator<= ( const Column c) const

Definition at line 179 of file Column.cpp.

References nutools::dbi::Util::DateAsStringToTime_t(), fType, fValue, Get(), nutools::dbi::kBool, nutools::dbi::kDateStamp, nutools::dbi::kFloatLike, nutools::dbi::kIntLike, nutools::dbi::kString, nutools::dbi::kTimeStamp, and nutools::dbi::Util::TimeAsStringToTime_t().

Referenced by Update().

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  }
uint16_t fType
Definition: Column.h:145
bool Get(T &val) const
Definition: Column.h:72
static bool TimeAsStringToTime_t(std::string ts, time_t &t)
Definition: Util.cpp:81
static bool DateAsStringToTime_t(std::string date, time_t &t)
Definition: Util.cpp:106
bool nutools::dbi::Column::operator== ( const Column c) const

Definition at line 292 of file Column.cpp.

References fType, and fValue.

Referenced by Update().

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  }
uint16_t fType
Definition: Column.h:145
bool nutools::dbi::Column::operator> ( const Column c) const

Definition at line 123 of file Column.cpp.

References nutools::dbi::Util::DateAsStringToTime_t(), fType, fValue, Get(), nutools::dbi::kBool, nutools::dbi::kDateStamp, nutools::dbi::kFloatLike, nutools::dbi::kIntLike, nutools::dbi::kString, nutools::dbi::kTimeStamp, nutools::dbi::Util::TimeAsStringToTime_t(), and Type().

Referenced by Update().

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  }
uint16_t fType
Definition: Column.h:145
bool Get(T &val) const
Definition: Column.h:72
static bool TimeAsStringToTime_t(std::string ts, time_t &t)
Definition: Util.cpp:81
static bool DateAsStringToTime_t(std::string date, time_t &t)
Definition: Util.cpp:106
bool nutools::dbi::Column::operator>= ( const Column c) const

Definition at line 67 of file Column.cpp.

References nutools::dbi::Util::DateAsStringToTime_t(), fType, fValue, Get(), nutools::dbi::kBool, nutools::dbi::kDateStamp, nutools::dbi::kFloatLike, nutools::dbi::kIntLike, nutools::dbi::kString, nutools::dbi::kTimeStamp, nutools::dbi::Util::TimeAsStringToTime_t(), and Type().

Referenced by Update().

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  }
uint16_t fType
Definition: Column.h:145
bool Get(T &val) const
Definition: Column.h:72
static bool TimeAsStringToTime_t(std::string ts, time_t &t)
Definition: Util.cpp:81
static bool DateAsStringToTime_t(std::string date, time_t &t)
Definition: Util.cpp:106
template<class T >
bool nutools::dbi::Column::Set ( const T &  val,
bool  ignoreAutoIncr = false 
)
inline

Definition at line 89 of file Column.h.

References fType, fValue, nutools::dbi::kAutoIncr, and nutools::dbi::kBool.

Referenced by Update().

89  {
90  if (!ignoreAutoIncr && fType==kAutoIncr) {
91  std::cerr << "Cannot set a column of type \"autoincr\"!"
92  << std::endl;
93  return false;
94  }
95  try {
96  if (fValue) {
97  delete fValue;
98  fValue=0;
99  }
100  std::string tstr = boost::lexical_cast<std::string>(val);
101  if (tstr == "" || tstr=="NULL") {
102  return true;
103  }
104  if (fType == kBool) {
105  fValue = new char[1];
106  if (tstr == "TRUE" || tstr == "t" || tstr == "true" ||
107  tstr == "y" || tstr == "yes" || tstr == "1" || tstr == "on")
108  fValue[0] = '1';
109  else
110  fValue[1] = '0';
111  return true;
112  }
113  else {
114  fValue = new char[tstr.length()];
115  strcpy(fValue,tstr.c_str());
116  return true;
117  }
118  }
119  catch (boost::bad_lexical_cast &) {
120  std::cerr << "Column::Set(): Bad lexical cast! Value = "
121  << val << std::endl;
122  return false;
123  }
124  return false;
125  }
uint16_t fType
Definition: Column.h:145
void nutools::dbi::Column::SetType ( uint8_t  t)
inline

Definition at line 49 of file Column.h.

References fType.

49 { fType = t; }
uint16_t fType
Definition: Column.h:145
uint8_t nutools::dbi::Column::Type ( ) const
inline

Definition at line 40 of file Column.h.

References fType.

Referenced by operator>(), and operator>=().

40 { return fType;}
uint16_t fType
Definition: Column.h:145
template<class T >
bool nutools::dbi::Column::Update ( const T &  val)
inline

Definition at line 128 of file Column.h.

References col, fModified, operator<(), operator<<, operator<=(), operator==(), operator>(), operator>=(), and Set().

128  {
129  if (Set(val)) {
130  fModified = true; return true; }
131  else
132  return false;
133  }
bool Set(const T &val, bool ignoreAutoIncr=false)
Definition: Column.h:89
std::string nutools::dbi::Column::Value ( ) const
inline

Definition at line 41 of file Column.h.

References fValue.

41  {
42  if (!fValue) return std::string("");
43  else return std::string(fValue); }

Friends And Related Function Documentation

std::ostream& operator<< ( std::ostream &  stream,
const Column col 
)
friend

Definition at line 152 of file Column.h.

Referenced by Update().

152  {
153  if (!col.fValue) {
154  stream << "NULL";
155  }
156  else {
157  if (col.fType == kBool) {
158  if (col.fValue[0] == '1')
159  stream << "true";
160  else
161  stream << "false";
162  }
163  else {
164  bool needsQuotes = (col.fType == kString ||
165  col.fType == kTimeStamp ||
166  col.fType == kDateStamp );
167  if (needsQuotes) stream << "\'";
168  stream << col.fValue;
169  if (needsQuotes) stream << "\'";
170  }
171  }
172  return stream;
173  }
Int_t col[ntarg]
Definition: Style.C:29

Member Data Documentation

bool nutools::dbi::Column::fModified
private

Definition at line 144 of file Column.h.

Referenced by Clear(), Column(), Modified(), and Update().

uint16_t nutools::dbi::Column::fType
private
char* nutools::dbi::Column::fValue
private

The documentation for this class was generated from the following files: