EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FairDbFieldType.cxx
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file FairDbFieldType.cxx
1 
2 #include <iostream>
3 #include <sstream>
4 
5 #include "TSQLServer.h"
6 #include "TString.h"
7 
8 #include "FairDbFieldType.h"
9 
11 
12 FairDbFieldType::FairDbFieldType(Int_t type /* = FairDb::kInt */)
13  : fConcept(0),
14  fSize(0),
15  fType(0)
16 {
17 
18  this->Init(type);
19 
20 }
21 
23  Int_t size,
24  const char* typeName)
25  : fConcept(0),
26  fSize(0),
27  fType(0)
28 {
29 
30 
31  TString name(typeName);
32  name.ToUpper();
33 
34  // Handle integer types.
35 
36  if ( type == TSQLServer::kSQL_INTEGER || type == TSQLServer::kSQL_NUMERIC ) {
37 
38  // TSQLServer reports e.g. int(32) as size 32, (even though maximum display is 11)
39  // so treat any type starting int or INT as size kMaxInt (i.e. standard 4 byte int)
40  if ( name.BeginsWith("INT") ) { size = kMaxInt; }
41  if ( size <= kMaxTinyInt ) { this->Init(FairDb::kTiny); return; }
42  else if ( size <= kMaxSmallInt ) { this->Init(FairDb::kShort); return; }
43  else if ( size <= kMaxInt ) { this->Init(FairDb::kInt); return; }
44  else { this->Init(FairDb::kLong); return; }
45 
46  }
47 
48  // Handle floating point types
49 
50  if ( type == TSQLServer::kSQL_FLOAT ) { this->Init(FairDb::kFloat); return; }
51  if ( type == TSQLServer::kSQL_DOUBLE ) { this->Init(FairDb::kDouble); return; }
52 
53  // Handle cases where type is determined uniquely by type name.
54 
55  if ( name == "BINARY_FLOAT" ) { this->Init(FairDb::kFloat); return; }
56  if ( name == "BINARY_DOUBLE" ) { this->Init(FairDb::kDouble); return; }
57  if ( name == "TINYTEXT" ) { this->Init(FairDb::kString,kMaxMySQLVarchar); return; }
58  if ( name == "TEXT" ) { this->Init(FairDb::kString,kMaxMySQLText); return; }
59  if ( name == "DATE" ) { this->Init(FairDb::kDate); return; }
60  if ( name == "DATETIME" ) { this->Init(FairDb::kDate); return; }
61 
62  // Handle character types
63 
64  if ( type == TSQLServer::kSQL_CHAR && size <= kMaxChar ) {
65  this->Init(FairDb::kChar,size);
66  return;
67  }
68  if ( type == TSQLServer::kSQL_CHAR || type == TSQLServer::kSQL_VARCHAR ) {
69  if ( size < kMaxMySQLVarchar ) { Init(FairDb::kString,size); }
71  return;
72  }
73 
74  // Anything else is bad news!
75 
76  cout
77  << "Unable to form SQL FairDbFieldType from: " << type << endl;
78  this->Init(FairDb::kUnknown);
79 
80 }
81 
82 //.....................................................................
83 
85  : fConcept(from.fConcept),
86  fSize(from.fSize),
87  fType(from.fType)
88 {
89  *this = from;
90 }
91 
92 //.....................................................................
93 
95  Int_t size )
96  : fConcept(0),
97  fSize(0),
98  fType(0)
99 {
100 
101  if ( sql == "TINYINT" ) { this->Init(FairDb::kTiny); }
102 
103  else if ( sql == "SMALLINT" ) { this->Init(FairDb::kShort); }
104 
105  else if ( sql == "INT"
106  || sql == "INTEGER"
107  || sql == "NUMERIC" ) { this->Init(FairDb::kInt); }
108 
109  else if ( sql == "BIGINT" ) { this->Init(FairDb::kLong); }
110 
111  else if ( sql == "FLOAT"
112  || sql == "REAL") { this->Init(FairDb::kFloat); }
113 
114  else if ( sql == "DOUBLE" ) { this->Init(FairDb::kDouble); }
115 
116  else if ( sql == "CHAR"
117  || sql == "VARCHAR"
118  || sql == "TEXT"
119  || sql == "TINYTEXT" ) {
120 
121  if ( sql == "TINYTEXT" ) { size = kMaxMySQLVarchar; }
122  else if ( sql == "TEXT" ) { size = kMaxMySQLText; }
123  else {
124  if ( size < 0 ) {
125  if ( sql == "CHAR" ) { size = 1; }
126  else { size = kMaxMySQLVarchar -1; }
127  }
128  }
129  if ( fSize <= kMaxChar ) { this->Init(FairDb::kChar,size); }
130  else { this->Init(FairDb::kString,size); }
131 
132  }
133 
134  else if ( sql == "DATETIME" ) { this->Init(FairDb::kDate); }
135 
136  else {
137 
138  cout << "Unable to type from SQL: " << sql << endl;
139  this->Init(FairDb::kUnknown);
140  }
141 
142 }
143 
144 
146 {
147 
148 
149 }
150 
151 
153 {
154 
155  switch ( fType ) {
156 
157  case FairDb::kBool :
158  return "Bool";
159  case FairDb::kChar :
160  return "Char";
161  case FairDb::kUChar :
162  return "UChar";
163  case FairDb::kTiny :
164  return "Tiny";
165  case FairDb::kUTiny :
166  return "UTiny";
167  case FairDb::kShort :
168  return "Short";
169  case FairDb::kUShort :
170  return "UShort";
171  case FairDb::kInt :
172  return "Int";
173  case FairDb::kUInt :
174  return "UInt";
175  case FairDb::kLong :
176  return "Long";
177  case FairDb::kULong :
178  return "ULong";
179  case FairDb::kFloat :
180  return "Float";
181  case FairDb::kDouble :
182  return "Double";
183  case FairDb::kString :
184  return "String";
185  case FairDb::kTString :
186  return "TString";
187  case FairDb::kDate :
188  return "Date";
189  }
190  return "Unknown";
191 }
192 
194 {
195 //
196 //
197 // Purpose: Return field type as a string suitable for MySQL/ORACLE column.
198 
199  ostringstream os;
200 
201 // Deal with MySQL ddl
202 
203  if ( dbType != FairDb::kOracle ) {
204  switch ( fType ) {
205 
206  case FairDb::kBool :
207  os << "CHAR";
208  break;
209 
210  case FairDb::kUTiny :
211  case FairDb::kTiny :
212  os << "TINYINT";
213  break;
214 
215  case FairDb::kShort :
216  case FairDb::kUShort :
217  os << "SMALLINT";
218  break;
219 
220  case FairDb::kInt :
221  case FairDb::kUInt :
222  os << "INT";
223  break;
224 
225  case FairDb::kLong :
226  case FairDb::kULong :
227  os << "BIGINT";
228  break;
229 
230  case FairDb::kFloat :
231  os << "FLOAT";
232  break;
233 
234  case FairDb::kDouble :
235  os << "DOUBLE";
236  break;
237 
238  case FairDb::kChar :
239  case FairDb::kUChar :
240  case FairDb::kString :
241  case FairDb::kTString :
242  if ( fSize == 1) { os << "CHAR"; }
243  else if ( fSize <= kMaxChar) { os << "CHAR(" << fSize << ')'; }
244  else if ( fSize < kMaxMySQLVarchar) { os << "VARCHAR(" << fSize << ')'; }
245  else if ( fSize == kMaxMySQLVarchar) { os << "TINYTEXT"; }
246  else { os << "TEXT"; }
247  break;
248 
249  case FairDb::kDate :
250  os << "DATETIME";
251  break;
252 
253  default :
254  os << "Unknown";
255 
256  }
257  }
258 
259  else {
260 
261 // Deal with ORACLE ddl
262 
263 
264  int size = fSize;
265  if ( fSize < kMaxOracleVarchar ) { size = kMaxOracleVarchar; }
266 
267  switch ( fType ) {
268 
269  case FairDb::kBool :
270  os << "CHAR";
271  break;
272 
273  case FairDb::kUTiny :
274  case FairDb::kTiny :
275  os << "NUMBER(4)";
276  break;
277 
278  case FairDb::kShort :
279  case FairDb::kUShort :
280  os << "NUMBER(6)";
281  break;
282 
283  case FairDb::kInt :
284  case FairDb::kUInt :
285  os << "NUMBER(11)";
286  break;
287 
288  case FairDb::kLong :
289  case FairDb::kULong :
290  os << "NUMBER(20)";
291  break;
292 
293  case FairDb::kFloat :
294  os << "BINARY_FLOAT";
295  break;
296 
297  case FairDb::kDouble :
298  os << "BINARY_DOUBLE";
299  break;
300 
301  case FairDb::kChar :
302  case FairDb::kUChar :
303  case FairDb::kString :
304  case FairDb::kTString :
305  if ( size == 1) { os << "CHAR"; }
306  else if ( size <= kMaxChar) { os << "CHAR(" << size << ')'; }
307  else { os << "VARCHAR(" << size << ')'; }
308  break;
309 
310  case FairDb::kDate :
311  os << "DATE";
312  break;
313 
314  default :
315  os << "Unknown";
316 
317  }
318  }
319 
320  return os.str();
321 
322 }
323 
324 
325 
326 void FairDbFieldType::Init(Int_t type,
327  Int_t size )
328 {
329 //
330 //
331 // Purpose: Initialise object.
332 //
333 
334  switch ( type ) {
335 
336  case FairDb::kBool :
339  fSize = 1;
340  break;
341 
342  case FairDb::kChar :
345  fSize = 1;
346  break;
347 
348  case FairDb::kUChar :
351  fSize = 1;
352  break;
353 
354  case FairDb::kTiny :
357  fSize = 1;
358  break;
359 
360  case FairDb::kUTiny :
363  fSize = 1;
364  break;
365 
366  case FairDb::kShort :
369  fSize = 2;
370  break;
371 
372  case FairDb::kUShort :
375  fSize = 2;
376  break;
377 
378  case FairDb::kInt :
381  fSize = 4;
382  break;
383 
384  case FairDb::kUInt :
387  fSize = 4;
388  break;
389 
390  case FairDb::kLong :
393  fSize = 8;
394  break;
395 
396  case FairDb::kULong :
399  fSize = 8;
400  break;
401 
402  case FairDb::kFloat :
405  fSize = 4;
406  break;
407 
408  case FairDb::kDouble :
411  fSize = 8;
412  break;
413 
414  case FairDb::kString :
417  fSize = 65535;
418  break;
419 
420  case FairDb::kTString :
423  fSize = 65535;
424  break;
425 
426  case FairDb::kDate :
429  fSize = 4;
430  break;
431 
432  case FairDb::kUnknown :
435  break;
436 
437  default :
438  cout << "Unable to form Root FairDbFieldType from: " << type << endl;
441  fSize = 0;
442  }
443 
444  if ( size >= 0 ) { fSize = size; }
445 
446 }
447 
448 
450 {
451 
452 #define MATCHES(x,y) \
453  (fConcept == x) && (concept == y) \
454  || (fConcept == y) && (concept == x)
455  UInt_t concept = other.GetConcept();
456  if ( fConcept == concept ) { return kTRUE; }
457  if ( MATCHES(FairDb::kBool, FairDb::kChar) ) { return kTRUE; }
458  if ( MATCHES(FairDb::kBool, FairDb::kUChar) ) { return kTRUE; }
459  if ( MATCHES(FairDb::kInt, FairDb::kChar) ) { return kTRUE; }
460  if ( MATCHES(FairDb::kUInt, FairDb::kUChar) ) { return kTRUE; }
461 // Allow unsigned to match signed because the TSQLResultSetL interface
462 // does not support unsigned types but its GetShort and GetInt
463 // methods will return unsigned data intact so we must trust
464 // that the user knows what they are doing!
465  if ( MATCHES(FairDb::kUInt, FairDb::kInt) ) { return kTRUE; }
466 // Allow char to be input to string.
467  if ( concept == FairDb::kChar && fConcept == FairDb::kString ) { return kTRUE; }
468 
469  return kFALSE;
470 
471 }
472 
473 //.....................................................................
474 
476 {
477 
478  switch ( fConcept ) {
479  case FairDb::kBool :
480  return "0";
481  case FairDb::kChar :
482  return "";
483  case FairDb::kUChar :
484  return "";
485  case FairDb::kInt :
486  return "-999";
487  case FairDb::kUInt :
488  return " 999";
489  case FairDb::kFloat :
490  return "-999.";
491  case FairDb::kString :
492  return "";
493  case FairDb::kDate :
494  return "1980-00-00 00:00:00";
495 
496  default :
497  cout << "Unable to define undefined type for: "
498  << fConcept << endl;
499  return "";
500  }
501 }