Column.h
Go to the documentation of this file.
1 #ifndef __DBICOLUMN_HPP_
2 #define __DBICOLUMN_HPP_
3 
4 #include <string>
5 #include <vector>
6 #include <stdint.h>
7 #include <iostream>
8 #include <boost/lexical_cast.hpp>
9 
10 namespace nutools {
11  namespace dbi {
12 
13  class ColumnDef;
14 
15  enum ColType {
16  kAutoIncr=0x1,
17  kBool=0x2,
18  kIntLike=0x4,
20  kString=0x10,
21  kTimeStamp=0x20,
23  };
24 
25  /**
26  * Generalized Database Column Interface
27  *
28  * @author Jonathan Paley
29  * @version $Id: Column.h,v 1.24 2013/03/12 19:53:02 jpaley Exp $
30  */
31 
32  class Column
33  {
34  public:
36  Column(const ColumnDef& c);
37  Column(const Column& c);
38  ~Column();
39 
40  uint8_t Type() const { return fType;}
41  std::string Value() const {
42  if (!fValue) return std::string("");
43  else return std::string(fValue); }
44  bool IsNull() const { return (!fValue); }
45  bool Modified() const { return fModified; }
46 
47  void Clear();
48 
49  void SetType(uint8_t t) { fType = t; }
50 
51  // WARNING: the casual user should NOT use this method. Only use it
52  // if you _really_ know what you're doing!
53  void FastSet(std::string v) {
54  if (fValue) {
55  delete fValue;
56  fValue=0;
57  }
58  fValue = new char[v.length()+1];
59  strcpy(fValue,v.c_str());
60  }
61 
62  void FastSet(const char* v) {
63  if (fValue) {
64  delete fValue;
65  fValue=0;
66  }
67  fValue = new char[strlen(v)+1];
68  strcpy(fValue,v);
69  }
70 
71  template <class T>
72  bool Get(T& val) const {
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  }
87 
88  template <class T>
89  bool Set(const T& val,bool ignoreAutoIncr=false) {
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  }
126 
127  template <class T>
128  bool Update(const T& val) {
129  if (Set(val)) {
130  fModified = true; return true; }
131  else
132  return false;
133  }
134 
135  friend std::ostream& operator<< (std::ostream& stream, const Column& col);
136 
137  bool operator >= (const Column& c) const;
138  bool operator <= (const Column& c) const;
139  bool operator > (const Column& c) const;
140  bool operator < (const Column& c) const;
141  bool operator == (const Column& c) const;
142 
143  private:
144  bool fModified;
146  char* fValue;
147 
148  }; // class end
149 
150  //************************************************************
151 
152  inline std::ostream& operator<< (std::ostream& stream, const Column& col) {
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  }
174 
175  } // namespace dbi close
176 } // namespace nutools close
177 
178 #endif
friend std::ostream & operator<<(std::ostream &stream, const Column &col)
Definition: Column.h:152
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
bool Modified() const
Definition: Column.h:45
std::string string
Definition: nybbler.cc:12
bool Set(const T &val, bool ignoreAutoIncr=false)
Definition: Column.h:89
unsigned short uint16_t
Definition: stdint.h:125
bool Update(const T &val)
Definition: Column.h:128
Simple service to provide a RunHistory configured to the right run.
Definition: Column.cpp:14
unsigned char uint8_t
Definition: stdint.h:124
bool Get(T &val) const
Definition: Column.h:72
void FastSet(const char *v)
Definition: Column.h:62
bool operator>=(const Column &c) const
Definition: Column.cpp:67
bool operator<=(const Column &c) const
Definition: Column.cpp:179
bool IsNull() const
Definition: Column.h:44
bool operator<(const Column &c) const
Definition: Column.cpp:236
uint8_t Type() const
Definition: Column.h:40
void SetType(uint8_t t)
Definition: Column.h:49
QTextStream & endl(QTextStream &s)
std::string Value() const
Definition: Column.h:41
void FastSet(std::string v)
Definition: Column.h:53