Main Page
Related Pages
Modules
Namespaces
Classes
Files
Examples
File List
File Members
nutools
old
IFDatabase
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,
19
kFloatLike
=0x8,
20
kString
=0x10,
21
kTimeStamp
=0x20,
22
kDateStamp
=0x40
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
:
35
Column
() {
fValue
=0;
fType
=
kIntLike
;};
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
;
145
uint16_t
fType
;
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
nutools::dbi::Column::operator<<
friend std::ostream & operator<<(std::ostream &stream, const Column &col)
Definition:
Column.h:152
nutools::dbi::Column::fType
uint16_t fType
Definition:
Column.h:145
nutools::dbi::kAutoIncr
Definition:
Column.h:16
nutools::dbi::Column::operator==
bool operator==(const Column &c) const
Definition:
Column.cpp:292
nutools::dbi::Column::operator>
bool operator>(const Column &c) const
Definition:
Column.cpp:123
nutools::dbi::Column::Clear
void Clear()
Definition:
Column.cpp:56
nutools::dbi::Column::Modified
bool Modified() const
Definition:
Column.h:45
val
Definition:
registry_via_id_test_2.cc:15
nutools::dbi::kBool
Definition:
Column.h:17
generate_CCQE_events.t
t
Definition:
generate_CCQE_events.py:68
nutools::dbi::Column::~Column
~Column()
Definition:
Column.cpp:50
string
std::string string
Definition:
nybbler.cc:12
nutools::dbi::kDateStamp
Definition:
Column.h:22
nutools::dbi::Column::Set
bool Set(const T &val, bool ignoreAutoIncr=false)
Definition:
Column.h:89
ValidateOpDetSimulation.T
T
Definition:
ValidateOpDetSimulation.py:48
nutools::dbi::kIntLike
Definition:
Column.h:18
uint16_t
unsigned short uint16_t
Definition:
stdint.h:125
nutools::dbi::kFloatLike
Definition:
Column.h:19
nutools::dbi::Column::Update
bool Update(const T &val)
Definition:
Column.h:128
nutools
Simple service to provide a RunHistory configured to the right run.
Definition:
Column.cpp:14
uint8_t
unsigned char uint8_t
Definition:
stdint.h:124
nutools::dbi::Column::Get
bool Get(T &val) const
Definition:
Column.h:72
nutools::dbi::Column::FastSet
void FastSet(const char *v)
Definition:
Column.h:62
nutools::dbi::Column::operator>=
bool operator>=(const Column &c) const
Definition:
Column.cpp:67
nutools::dbi::Column::operator<=
bool operator<=(const Column &c) const
Definition:
Column.cpp:179
nutools::dbi::ColType
ColType
Definition:
Column.h:15
nutools::dbi::Column::fModified
bool fModified
Definition:
Column.h:144
ValidateOpDetSimulation.c
dictionary c
Definition:
ValidateOpDetSimulation.py:53
nutools::dbi::Column::fValue
char * fValue
Definition:
Column.h:146
nutools::dbi::Column::IsNull
bool IsNull() const
Definition:
Column.h:44
generate_datataset.stream
stream
Definition:
generate_datataset.py:30
testsqlite3.val
list val
Definition:
testsqlite3.py:13
nutools::dbi::Column::operator<
bool operator<(const Column &c) const
Definition:
Column.cpp:236
nutools::dbi::Column::Type
uint8_t Type() const
Definition:
Column.h:40
stdint.h
nutools::dbi::kTimeStamp
Definition:
Column.h:21
nutools::dbi::Column::Column
Column()
Definition:
Column.h:35
nutools::dbi::Column::SetType
void SetType(uint8_t t)
Definition:
Column.h:49
nutools::dbi::Column
Definition:
Column.h:32
endl
QTextStream & endl(QTextStream &s)
Definition:
qtextstream.cpp:2030
nutools::dbi::kString
Definition:
Column.h:20
nutools::dbi::Column::Value
std::string Value() const
Definition:
Column.h:41
nutools::dbi::Column::FastSet
void FastSet(std::string v)
Definition:
Column.h:53
nutools::dbi::ColumnDef
Definition:
ColumnDef.h:15
Generated by
1.8.11