source: trunk/FACT++/src/FileEntry.h@ 20084

Last change on this file since 20084 was 20054, checked in by tbretz, 4 years ago
Added TIMESTAMP as SQL type.
File size: 8.4 KB
Line 
1#ifndef FACT_FileEntry
2#define FACT_FileEntry
3
4#include <vector>
5#include <string>
6#include <iomanip>
7
8namespace FileEntry
9{
10 enum BasicType_t
11 {
12 kNone = 0,
13 kConst,
14 kChar,
15 kVarchar,
16 kBool,
17 kInt8,
18 kUInt8,
19 kFloat,
20 kDouble,
21 kInt16,
22 kUInt16,
23 kInt32,
24 kUInt32,
25 kInt64,
26 kUInt64,
27 kDecimal,
28 kNumeric,
29 kTime,
30 kDate,
31 kDateTime,
32 };
33
34 struct Type
35 {
36 BasicType_t type;
37 std::string root; // root basic type
38 char branch; // root branch
39 char fits; // fits type
40 std::string sql; // sql type
41 };
42
43 struct Types : std::vector<Type>
44 {
45 Types(std::initializer_list<Type> il, const allocator_type& alloc = allocator_type())
46 : std::vector<Type>(il, alloc)
47 {
48 }
49
50 // Note that the result is not unique!
51 const std::vector<Type>::const_iterator root(const std::string &str) const
52 {
53 for (auto it=cbegin(); it!=cend(); it++)
54 if (str==it->root)
55 return it;
56 return cend();
57 }
58
59 // Note that the result is not unique!
60 const std::vector<Type>::const_iterator type(const BasicType_t &t) const
61 {
62 for (auto it=cbegin(); it!=cend(); it++)
63 if (t==it->type)
64 return it;
65 return cend();
66 }
67
68 const std::vector<Type>::const_iterator fits(const char &f) const
69 {
70 for (auto it=cbegin(); it!=cend(); it++)
71 if (f==it->fits)
72 return it;
73 return cend();
74 }
75
76 const std::vector<Type>::const_iterator sql(const std::string &str) const
77 {
78 for (auto it=cbegin(); it!=cend(); it++)
79 if (str==it->sql)
80 return it;
81 return cend();
82 }
83 };
84
85 static const Types LUT =
86 {
87 // type root branch fits sql
88 { kChar, "Char_t", ' ', 'A', "CHAR" },
89 { kChar, "Char_t", ' ', 'a', "CHAR" },
90 { kVarchar, "Char_t", ' ', 'A', "VARCHAR" },
91 { kVarchar, "Char_t", ' ', 'a', "VARCHAR" },
92 { kBool, "Bool_t", 'O', 'L', "BOOLEAN" },
93 { kBool, "Bool_t", 'O', 'l', "BOOLEAN" },
94 { kInt8, "Char_t", 'B', 'B', "TINYINT" },
95 { kUInt8, "UInt8_t", 'b', 'b', "TINYINT UNSIGNED" },
96 { kInt16, "Short_t", 'S', 'I', "SMALLINT" },
97 { kUInt16, "UShort_t", 's', 'i', "SMALLINT UNSIGNED" },
98 { kInt32, "Int_t", 'I', 'J', "INT" },
99 { kUInt32, "UInt_t", 'i', 'j', "INT UNSIGNED" },
100 { kInt32, "Int_t", 'I', 'J', "MEDIUMINT" },
101 { kUInt32, "UInt_t", 'i', 'j', "MEDIUMINT UNSIGNED" },
102 { kInt64, "Long64_t", 'L', 'K', "BIGINT" },
103 { kUInt64, "ULong64_t", 'l', 'k', "BIGINT UNSIGNED" },
104 { kFloat, "Float_t", 'F', 'E', "FLOAT" },
105 { kFloat, "Float_t", 'F', 'e', "FLOAT" },
106 { kDouble, "Double_t", 'D', 'D', "DOUBLE" },
107 { kDouble, "Double_t", 'D', 'd', "DOUBLE" },
108 { kTime, "UInt32_t", 'i', 'j', "TIME" },
109 { kDate, "UInt64_t", 'l', 'k', "DATE" },
110 { kDateTime, "UInt64_t", 'l', 'k', "DATETIME" }, // MySQL++ omits fractions
111 { kDateTime, "UInt64_t", 'l', 'k', "TIMESTAMP" }, // MySQL++ omits fractions
112 { kDecimal, "Double_t", 'D', 'D', "DECIMAL" },
113 { kDecimal, "Double_t", 'D', 'd', "DECIMAL" },
114 { kNumeric, "Double_t", 'D', 'D', "NUMERIC" },
115 { kNumeric, "Double_t", 'D', 'd', "NUMERIC" },
116 };
117
118 struct Container
119 {
120 static std::map<void*, size_t> counter;
121
122 std::string branch; // branch name
123 std::string column; // column name
124
125 BasicType_t type;
126
127 size_t num;
128 void *ptr;
129
130 Container() : type(kNone), num(0), ptr(0)
131 {
132 }
133
134 // Root File (do not mix with FitsFile!)
135 Container(const std::string &b, const std::string &c,
136 const BasicType_t &t, const size_t &n=1)
137 : branch(b), column(c), type(t), num(n), ptr(0)
138 {
139 switch (t)
140 {
141 case kInt8: ptr = new int8_t[n]; break;
142 case kUInt8: ptr = new uint8_t[n]; break;
143 case kBool: ptr = new uint8_t[n]; break;
144 case kFloat: ptr = new float[n]; break;
145 case kDecimal:
146 case kNumeric:
147 case kDouble: ptr = new double[n]; break;
148 case kInt16: ptr = new int16_t[n]; break;
149 case kUInt16: ptr = new uint16_t[n]; break;
150 case kInt32: ptr = new int32_t[n]; break;
151 case kTime:
152 case kUInt32: ptr = new uint32_t[n]; break;
153 case kInt64: ptr = new int64_t[n]; break;
154 case kDate:
155 case kDateTime:
156 case kUInt64: ptr = new uint64_t[n]; break;
157 case kChar:
158 case kVarchar:
159 case kConst:
160 case kNone:
161 break;
162 }
163 // Indicate that this was allocated and how often
164 counter[ptr]++;
165 }
166
167 // This is for rootifysql!
168 Container(const std::string &b, const BasicType_t &t)
169 : branch(b), type(t), num(1)
170 {
171 ptr = new double[1];
172 // Indicate that this was allocated and how often
173 counter[ptr]++;
174 }
175
176 Container(const std::string &c, const std::string &value)
177 : branch(value), column(c), type(kConst), num(1), ptr(0)
178 {
179 }
180
181 Container(const Container &c) : branch(c.branch), column(c.column),
182 type(c.type), num(c.num), ptr(c.ptr)
183 {
184 // If this was not allocated in the constructor do not increate the counter
185 if (counter[ptr])
186 counter[ptr]++;
187 }
188
189 // FitsFile (do not mix with RootFile!)
190 Container(const std::string &b, const std::string &c,
191 const BasicType_t &t, const size_t &n, void *_ptr)
192 : branch(b), column(c), type(t), num(n), ptr(_ptr)
193 {
194 }
195
196 ~Container()
197 {
198 // Count how often it gets deleted.
199 counter[ptr]--;
200 // Now it is time to delete it. Note that pointers which were
201 // never allocated (counter==0) now are != 0
202 if (counter[ptr]==0)
203 ::operator delete[](ptr); // It seems root is deleting it already
204 }
205
206 std::string fmt(const size_t &index) const
207 {
208 std::ostringstream str;
209
210 switch (type)
211 {
212 case kVarchar: str << std::string(reinterpret_cast<char*>(ptr), num).c_str(); break;
213 case kFloat: str << std::setprecision(8) << reinterpret_cast<float*>(ptr)[index]; break;
214 case kDecimal:
215 case kNumeric:
216 case kDouble: str << std::setprecision(16) << reinterpret_cast<double*>(ptr)[index]; break;
217 case kBool:
218 case kInt8: str << int32_t(reinterpret_cast<int8_t*>(ptr)[index]); break;
219 case kUInt8: str << uint32_t(reinterpret_cast<uint8_t*>(ptr)[index]); break;
220 case kInt16: str << reinterpret_cast<int16_t*>(ptr)[index]; break;
221 case kUInt16: str << reinterpret_cast<uint16_t*>(ptr)[index]; break;
222 case kInt32: str << reinterpret_cast<int32_t*>(ptr)[index]; break;
223 case kTime:
224 case kUInt32: str << reinterpret_cast<uint32_t*>(ptr)[index]; break;
225 case kInt64: str << reinterpret_cast<int64_t*>(ptr)[index]; break;
226 case kDate:
227 case kDateTime:
228 case kUInt64: str << reinterpret_cast<uint64_t*>(ptr)[index]; break;
229 case kConst: str << branch; break;
230 case kChar:
231 case kNone:
232 break;
233 }
234
235 //if (str.str()=="nan" || str.str()=="-nan" || str.str()=="inf" || str.str()=="-inf")
236 // return "NULL";
237
238 return str.str();
239 }
240 };
241
242 std::map<void*, size_t> Container::counter;
243}
244
245#endif
Note: See TracBrowser for help on using the repository browser.