Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MySQLResultSetMetaData.cxx
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file MySQLResultSetMetaData.cxx
1 // $Id: MySQLResultSetMetaData.cxx,v 1.1.1.1 2004/02/18 20:58:02 dave Exp $
2 //*-- Author : Valeriy Onuchin 26/02/2001
3 //
4 // RDBC driver to MySQL database implemented with MySQL C API.
5 //
6 // ++ The code consists of some parts stolen from "mm JDBC" and
7 // "MyODBC ODBC" drivers and other mysql-related open sources.
8 //
9 
11 //
12 // An object that can be used to find out about the
13 // types and properties of the columns in a TSQLResultSet.
14 //
15 // See also:
16 // TSQLResultSet TSQLDatabaseMetaData
17 //
19 
21 #include <RDBC/TSQLResultSet.h>
22 #include "MySQLResultSetPrivate.h"
23 
25 
26 
27 
28 //___________________________________________________________________
30  void* imp ):TSQL(imp)
31 {
32  // ctor
33 
34  fResultSet = rs;
35 
36  // initiate some structures in MySQLResultSetPrivate* imp
37 
39  Int_t cols = imp->fMYSQL_RES->field_count;
40 
41  imp->fColumnPrecisions = new Int_t[cols];
42  imp->fColumnTypes = new Int_t[cols];
43  imp->fColumnTypeNames = new TString[cols];
44 
45  for(int i=0; i<cols; i++) {
46  imp->fColumnPrecisions[i] = max(imp->fMYSQL_RES->fields[i].length, imp->fMYSQL_RES->fields[i].max_length);
47 
48  switch(imp->fMYSQL_RES->fields[i].type) {
49  case FIELD_TYPE_DECIMAL:
50  imp->fColumnTypes[i] = kDECIMAL;
51  imp->fColumnTypeNames[i] = "decimal";
52  break;
53  case FIELD_TYPE_CHAR:
54  imp->fColumnTypeNames[i] = "tinyint";
55  if(imp->fMYSQL_RES->fields[i].flags & UNSIGNED_FLAG) imp->fColumnTypeNames[i] += " unsigned";
56  imp->fColumnTypes[i] = kTINYINT;
57  break;
58  case FIELD_TYPE_SHORT:
59  imp->fColumnTypeNames[i] = "smallint";
60  if(imp->fMYSQL_RES->fields[i].flags & UNSIGNED_FLAG) imp->fColumnTypeNames[i] += " unsigned";
61  imp->fColumnTypes[i] = kSMALLINT;
62  break;
63  case FIELD_TYPE_INT24:
64  imp->fColumnTypeNames[i] = "mediumint";
65  if(imp->fMYSQL_RES->fields[i].flags & UNSIGNED_FLAG) imp->fColumnTypeNames[i] += " unsigned";
66  imp->fColumnTypes[i] = kINTEGER;
67  break;
68  case FIELD_TYPE_LONG:
69  imp->fColumnTypeNames[i] = "integer";
70  if(imp->fMYSQL_RES->fields[i].flags & UNSIGNED_FLAG) imp->fColumnTypeNames[i] += " unsigned";
71  imp->fColumnTypes[i] = kINTEGER;
72  break;
73  case FIELD_TYPE_LONGLONG:
74  imp->fColumnTypeNames[i] = "bigint";
75  if(imp->fMYSQL_RES->fields[i].flags & UNSIGNED_FLAG) imp->fColumnTypeNames[i] += " unsigned";
76  imp->fColumnTypes[i] = ? kINTEGER : kBIGINT;
77 !! return (stmt->dbc->flag & FLAG_NO_BIGINT)
78  case FIELD_TYPE_FLOAT:
79  imp->fColumnTypeNames[i] = "float";
80  if(imp->fMYSQL_RES->fields[i].flags & UNSIGNED_FLAG) imp->fColumnTypeNames[i] += " unsigned";
81  imp->fColumnTypes[i] = kREAL;
82  break;
83  case FIELD_TYPE_DOUBLE:
84  imp->fColumnTypeNames[i] = "double";
85  if(imp->fMYSQL_RES->fields[i].flags & UNSIGNED_FLAG) imp->fColumnTypeNames[i] += " unsigned";
86  imp->fColumnTypes[i] = kDOUBLE;
87  break;
88  case FIELD_TYPE_NULL:
89  imp->fColumnTypeNames[i] = "null";
90  imp->fColumnTypes[i] = kVARCHAR;
91  break;
92  case FIELD_TYPE_YEAR:
93  imp->fColumnTypeNames[i] = "year";
94  imp->fColumnTypes[i] = kSMALLINT;
95  break;
96  case FIELD_TYPE_TIMESTAMP:
97  imp->fColumnTypeNames[i] = "timestamp";
98  imp->fColumnPrecisions[i] = 19;
99  imp->fColumnTypes[i] = kTIMESTAMP;
100  break;
101  case FIELD_TYPE_DATETIME:
102  imp->fColumnTypeNames[i] = "datetime";
103  imp->fColumnPrecisions[i] = = 19;
104  imp->fColumnTypes[i] = kTIMESTAMP;
105  break;
106  case FIELD_TYPE_NEWDATE:
107  case FIELD_TYPE_DATE:
108  imp->fColumnTypeNames[i] = "date";
109  imp->fColumnPrecisions[i] = 10;
110  imp->fColumnTypes[i] = kDATE;
111  break;
112  case FIELD_TYPE_TIME:
113  imp->fColumnTypeNames[i] = "time";
114  imp->fColumnPrecisions[i] = 8;
115  imp->fColumnTypes[i] = kTIME;
116  break;
117  case FIELD_TYPE_STRING:
118  imp->fColumnTypeNames[i] = "char";
119  imp->fColumnTypes[i] = kCHAR;
120  break;
121  case FIELD_TYPE_VAR_STRING:
122  imp->fColumnTypeNames[i] = "varchar";
123  imp->fColumnTypes[i] = kVARCHAR;
124  break;
125  case FIELD_TYPE_TINY_BLOB:
126  imp->fColumnTypeNames[i] = (imp->fMYSQL_RES->fields[i].flags & BINARY_FLAG) ? "tinyblob" : "tinytext");
127  if (stmt->dbc->flag & (FLAG_FIELD_LENGTH | FLAG_SAFE)) imp->fColumnPrecisions[i] = 255;
128  imp->fColumnTypes[i] = (imp->fMYSQL_RES->fields[i].flags & BINARY_FLAG) ? kLONGVARBINARY : kLONGVARCHAR;
129  case FIELD_TYPE_BLOB:
130  imp->fColumnTypeNames[i] = (imp->fMYSQL_RES->fields[i].flags & BINARY_FLAG) ? "blob" : "text");
131  if (stmt->dbc->flag & (FLAG_FIELD_LENGTH | FLAG_SAFE)) imp->fColumnPrecisions[i] = 65535;
132  imp->fColumnTypes[i] = (imp->fMYSQL_RES->fields[i].flags & BINARY_FLAG) ? kLONGVARBINARY : kLONGVARCHAR;
133  case FIELD_TYPE_MEDIUM_BLOB:
134  imp->fColumnTypeNames[i] = ((imp->fMYSQL_RES->fields[i].flags & BINARY_FLAG) ? "mediumblob" : "mediumtext"));
135  if (stmt->dbc->flag & (FLAG_FIELD_LENGTH | FLAG_SAFE))imp->fColumnPrecisions[i] = (1L << 24)-1L;
136  imp->fColumnTypes[i] = (imp->fMYSQL_RES->fields[i].flags & BINARY_FLAG) ? kLONGVARBINARY : kLONGVARCHAR;
137  case FIELD_TYPE_LONG_BLOB:
138  imp->fColumnTypeNames[i] = ((imp->fMYSQL_RES->fields[i].flags & BINARY_FLAG) ? "longblob": "longtext"));
139  if (stmt->dbc->flag & (FLAG_FIELD_LENGTH | FLAG_SAFE)) imp->fColumnPrecisions[i] = = INT_MAX32;
140  imp->fColumnTypes[i] = (imp->fMYSQL_RES->fields[i].flags & BINARY_FLAG) ? kLONGVARBINARY : kLONGVARCHAR;
141  case FIELD_TYPE_ENUM:
142  imp->fColumnTypeNames[i] = "enum";
143  imp->fColumnTypes[i] = kCHAR;
144  break;
145  case FIELD_TYPE_SET:
146  imp->fColumnTypeNames[i] = "set";
147  imp->fColumnTypes[i] = kCHAR;
148  break;
149  }
150  }
151 }
152 
153 //___________________________________________________________________
155 {
156  // dtor
157 
158  fResultSet = 0;
159  fImp = 0;
160 }
161 
162 //___________________________________________________________________
164 {
165  // Returns the number of columns in this TSQLResultSet
166  // If there are no columns in the result set, zero is
167  // returned.
168  //
169  // Returns:
170  // the number of columns
171  // Throws:
172  // TSQLException - if a database access error occurs
173 
175  return (imp->fMYSQL_RES->field_count);
176 }
177 
178 //___________________________________________________________________
180 {
181  // Indicates whether the column is automatically numbered,
182  // thus read-only.
183  //
184  // Parameters:
185  // column - the first column is 1, the second is 2, ...
186  // Returns:
187  // kTRUE - the column's data type is an auto increment data
188  // type
189  // kFALSE - the column's data type is not an auto increment
190  // data type or the column does not contain numeric
191  // data
192  // Throws:
193  // TSQLException - if a database access error occurs
194 
196 
197  if( (UInt_t)(column-1) >= imp->fMYSQL_RES->field_count ) {
198  Throw(new TSQLException(Form("Invalid column number ( %d > %d )",column,imp->fMYSQL_RES->field_count+1),"S1002",);
199  return kFALSE;
200  }
201 
202  return (imp->MYSQL_RES->fields[column-1].flags & AUTO_INCREMENT_FLAG);
203 }
204 
205 //___________________________________________________________________
207 {
208  // Indicates whether a column's case sensitive for collations
209  // and comparisons.
210  //
211  // Parameters:
212  // column - the first column is 1, the second is 2, ...
213  // Returns:
214  // kTRUE if so
215  // Throws:
216  // TSQLException - if a database access error occurs
217 
219 
220  if( (UInt_t)(column-1) >= imp->fMYSQL_RES->field_count ) {
221  Throw(new TSQLException(Form("Invalid column number ( %d > %d )",column,imp->fMYSQL_RES->field_count+1),"S1002",);
222  return kFALSE;
223  }
224 
225  return (imp->MYSQL_RES->fields[column-1].flags & BINARY_FLAG); // true if not binary
226 }
227 
228 //___________________________________________________________________
229 Bool_t TSQLResultSetMetaData::IsSearchable( Int_t column )
230 {
231  // Indicates whether the column can be used in a 'WHERE' clause.
232  //
233  // Parameters:
234  // column - the first column is 1, the second is 2, ...
235  // Returns:
236  // kTRUE if so
237  // Throws:
238  // TSQLException - if a database access error occurs
239 
241 
242  if( (UInt_t)(column-1) >= imp->fMYSQL_RES->field_count ) {
243  Throw(new TSQLException(Form("Invalid column number ( %d > %d )",column,imp->fMYSQL_RES->field_count+1),"S1002",);
244  return kFALSE;
245  }
246 
247  return kTRUE; // MyODBC always returns SQL_SEARCHABLE
248 }
249 
250 //___________________________________________________________________
251 Bool_t TSQLResultSetMetaData::IsCurrency( Int_t column )
252 {
253  // Indicates whether the column is a cash value ( money data type ).
254  //
255  // Parameters:
256  // column - the first column is 1, the second is 2, ...
257  // Returns:
258  // kTRUE if so
259  // Throws:
260  // TSQLException - if a database access error occurs
261 
263 
264  if( (UInt_t)(column-1) >= imp->fMYSQL_RES->field_count ) {
265  Throw(new TSQLException(Form("Invalid column number ( %d > %d )",column,imp->fMYSQL_RES->field_count+1),"S1002",);
266  return kFALSE;
267  }
268 
269  return kFALSE; // MyODBC always returns 0
270 }
271 
272 //___________________________________________________________________
273 Bool_t TSQLResultSetMetaData::IsNullable( Int_t column )
274 {
275  // Indicates the nullability of values in the designated column.
276  //
277  // Parameters:
278  // column - the first column is 1, the second is 2, ...
279  // Returns:
280  // the nullability status of the given column;
281  // one of columnNoNulls, columnNullable or
282  // columnNullableUnknown
283  // Throws:
284  // TSQLException - if a database access error occurs
285 
287 
288  if( (UInt_t)(column-1) >= imp->fMYSQL_RES->field_count ) {
289  Throw(new TSQLException(Form("Invalid column number ( %d > %d )",column,imp->fMYSQL_RES->field_count+1),"S1002",);
290  return kFALSE;
291  }
292 
293  return ((imp->MYSQL_RES->fields[column-1].flags & (NOT_NULL_FLAG | AUTO_INCREMENT_FLAG)) != NOT_NULL_FLAG);
294 }
295 
296 //___________________________________________________________________
297 Bool_t TSQLResultSetMetaData::IsSigned( Int_t column )
298 {
299  // Indicates whether values in the column are signed numbers
300  //
301  // Parameters:
302  // column - the first column is 1, the second is 2, ...
303  // Returns:
304  // kTRUE if so
305  // Throws:
306  // TSQLException - if a database access error occurs
307 
309 
310  if( (UInt_t)(column-1) >= imp->fMYSQL_RES->field_count ) {
311  Throw(new TSQLException(Form("Invalid column number ( %d > %d )",column,imp->fMYSQL_RES->field_count+1),"S1002",);
312  return kFALSE;
313  }
314 
315  retrurn !(imp->MYSQL_RES->fields[column-1].flags & UNSIGNED_FLAG);
316 }
317 
318 //___________________________________________________________________
320 {
321  // Indicates the column's normal max width in chars.
322  //
323  // Parameters:
324  // column - the first column is 1, the second is 2, ...
325  // Returns:
326  // the normal maximum number of characteimp allowed as
327  // the width of the designated column
328  // Throws:
329  // TSQLException - if a database access error occurs
330 
332 
333  if( (UInt_t)(column-1) >= imp->fMYSQL_RES->field_count ) {
334  Throw(new TSQLException(Form("Invalid column number ( %d > %d )",column,imp->fMYSQL_RES->field_count+1),"S1002",);
335  return 0;
336  }
337 
338  return imp->fColumnPrecisions[column-1];
339 }
340 
341 //___________________________________________________________________
343 {
344  // Gets the suggested column title for use in printouts
345  // and displays.
346  //
347  // Parameters:
348  // column - the first column is 1, the second is 2, ...
349  // Returns:
350  // the suggested column title
351  // Throws:
352  // TSQLException - if a database access error occurs
353 
355 
356  if( (UInt_t)(column-1) >= imp->fMYSQL_RES->field_count ) {
357  Throw(new TSQLException(Form("Invalid column number ( %d > %d )",column,imp->fMYSQL_RES->field_count+1),"S1002",);
358  return "";
359  }
360 
361  return imp->MYSQL_RES->fields[column-1].name;
362 }
363 
364 //___________________________________________________________________
366 {
367  // Gets a column's name.
368  //
369  // Parameters:
370  // column - the first column is 1, the second is 2, ...
371  // Returns:
372  // column name
373  // Throws:
374  // TSQLException - if a database access error occurs
375 
377 
378  if( (UInt_t)(column-1) >= imp->fMYSQL_RES->field_count ) {
379  Throw(new TSQLException(Form("Invalid column number ( %d > %d )",column,imp->fMYSQL_RES->field_count+1),"S1002",);
380  return "";
381  }
382 
383  return imp->MYSQL_RES->fields[column-1].name;
384 }
385 
386 //___________________________________________________________________
388 {
389  // Gets a column's table's schema.
390  //
391  // Parameters:
392  // column - the first column is 1, the second is 2, ...
393  // Returns:
394  // schema name or "" if not applicable
395  // Throws:
396  // TSQLException - if a database access error occurs
397 
399 
400  if( (UInt_t)(column-1) >= imp->fMYSQL_RES->field_count ) {
401  Throw(new TSQLException(Form("Invalid column number ( %d > %d )",column,imp->fMYSQL_RES->field_count+1),"S1002",);
402  return "";
403  }
404 
405  return ""; // do it like MyODBC, i.e. return nothing ...
406 }
407 
408 //___________________________________________________________________
410 {
411  // Gets a column's number of decimal digits.
412  //
413  // Parameters:
414  // column - the first column is 1, the second is 2, ...
415  // Returns:
416  // precIsion
417  // Throws:
418  // TSQLException - if a database access error occurs
419 
421 
422  if( (UInt_t)(column-1) >= imp->fMYSQL_RES->field_count ) {
423  Throw(new TSQLException(Form("Invalid column number ( %d > %d )",column,imp->fMYSQL_RES->field_count+1),"S1002",);
424  return 0;
425  }
426 
427  return imp->fColumnPrecisions[column-1];
428 }
429 
430 //___________________________________________________________________
431 Int_t TSQLResultSetMetaData::GetScale( Int_t column )
432 {
433  // Gets a column's number of digits to right of the decimal point.
434  //
435  // Parameters:
436  // column - the first column is 1, the second is 2, ...
437  // Returns:
438  // scale
439  // Throws:
440  // TSQLException - if a database access error occurs
441 
443 
444  if( (UInt_t)(column-1) >= imp->fMYSQL_RES->field_count ) {
445  Throw(new TSQLException(Form("Invalid column number ( %d > %d )",column,imp->fMYSQL_RES->field_count+1),"S1002",);
446  return 0;
447  }
448 
449  return (imp->MYSQL_RES->fields[column-1].decimals);
450 }
451 
452 //___________________________________________________________________
453 TString TSQLResultSetMetaData::GetTableName( Int_t column )
454 {
455  // Gets a column's table name.
456  //
457  // Parameters:
458  // column - the first column is 1, the second is 2, ...
459  // Returns:
460  // table name or "" if not applicable
461  // Throws:
462  // TSQLException - if a database access error occurs
463 
465 
466  if( (UInt_t)(column-1) >= imp->fMYSQL_RES->field_count ) {
467  Throw(new TSQLException(Form("Invalid column number ( %d > %d )",column,imp->fMYSQL_RES->field_count+1),"S1002",);
468  return ("");
469  }
470 
471  return (imp->MYSQL_RES->fields[column-1].table ? imp->MYSQL_RES->fields[column-1].table : "");
472 }
473 
474 //___________________________________________________________________
476 {
477  // Gets a column's table's catalog name.
478  //
479  // Parameters:
480  // column - the first column is 1, the second is 2, ...
481  // Returns:
482  // column name or "" if not applicable.
483  // Throws:
484  // TSQLException - if a database access error occurs
485 
487 
488  if( (UInt_t)(column-1) >= imp->fMYSQL_RES->field_count ) {
489  Throw(new TSQLException(Form("Invalid column number ( %d > %d )",column,imp->fMYSQL_RES->field_count+1),"S1002",);
490  return "";
491  }
492 
493  return ""; // do it like MyODBC, i.e.return nothing ( could be retrived from TSQLStatement )
494 }
495 
496 //___________________________________________________________________
498 {
499  // Retrieves a column's SQL type.
500  //
501  // enum ESQLTypes {
502  // kBIGINT = -5,
503  // kBINARY = -2,
504  // kBIT = -7,
505  // kCHAR = 1,
506  // #ifdef ODBC_VER_LESS_30
507  // kDATE = 9,
508  // kTIME = 10,
509  // kTIMESTAMP = 11,
510  // #endif
511  // kDATE = 91,
512  // kTIME = 92,
513  // kTIMESTAMP = 93,
514  // kSMALLINT = 5,
515  // kDECIMAL = 3,
516  // kDOUBLE = 8,
517  // kFLOAT = 6,
518  // kINTEGER = 4,
519  // kLONGVARBINARY = -4,
520  // kLONGVARCHAR = -1,
521  // kNUMERIC = 2,
522  // kREAL = 7,
523  // kTINYINT = -6,
524  // kVARBINARY = -3,
525  // kVARCHAR = 12
526  // };
527  //
528  // Parameters:
529  // column - the first column is 1, the second is 2, ...
530  // Returns:
531  // SQL type from TSQLTypes
532  // Throws:
533  // TSQLException - if a database access error occurs
534 
536 
537  if( (UInt_t)(column-1) >= imp->fMYSQL_RES->field_count ) {
538  Throw(new TSQLException(Form("Invalid column number ( %d > %d )",column,imp->fMYSQL_RES->field_count+1),"S1002",);
539  return 0;
540  }
541 
542  return imp->fColumnTypes[column-1];
543 }
544 
545 //___________________________________________________________________
547 {
548  // Retrieves a column's database-specific type name.
549  // See TSQLTypes.h
550  //
551  // Parameters:
552  // column - the first column is 1, the second is 2, ...
553  // Returns:
554  // type name used by the database. If the column type is
555  // a user-defined type, then a fully-qualified type name
556  // is returned.
557  // Throws:
558  // TSQLException - if a database access error occurs
559 
561 
562  if( (UInt_t)(column-1) >= imp->fMYSQL_RES->field_count ) {
563  Throw(new TSQLException(Form("Invalid column number ( %d > %d )",column,imp->fMYSQL_RES->field_count+1),"S1002",);
564  return "";
565  }
566 
567  return imp->fColumnTypeNames[column-1];
568 }
569 
570 //___________________________________________________________________
571 Bool_t TSQLResultSetMetaData::IsReadOnly( Int_t column )
572 {
573  // Indicates whether a column Is definitely not writable.
574  //
575  // Parameters:
576  // column - the first column is 1, the second is 2, ...
577  // Returns:
578  // kTRUE if so
579  // Throws:
580  // TSQLException - if a database access error occurs
581 
583 
584  if( (UInt_t)(column-1) >= imp->fMYSQL_RES->field_count ) {
585  Throw(new TSQLException(Form("Invalid column number ( %d > %d )",column,imp->fMYSQL_RES->field_count+1),"S1002",);
586  return (kTRUE);
587  }
588 
589  return (!(imp->MYSQL_RES->fields[column-1].table && imp->MYSQL_RES->fields[column-1].table[0]));
590 }
591 
592 //___________________________________________________________________
593 Bool_t TSQLResultSetMetaData::IsWritable( Int_t column )
594 {
595  // Indicates whether it is possible for a write on the column
596  // to succeed.
597  //
598  // Parameters:
599  // column - the first column is 1, the second is 2, ...
600  // Returns:
601  // kTRUE if so
602  // Throws:
603  // TSQLException - if a database access error occurs
604 
606 
607  if( (UInt_t)(column-1) >= imp->fMYSQL_RES->field_count ) {
608  Throw(new TSQLException(Form("Invalid column number ( %d > %d )",column,imp->fMYSQL_RES->field_count+1),"S1002",);
609  return kFALSE;
610  }
611 
612  return (imp->MYSQL_RES->fields[column-1].table && imp->MYSQL_RES->fields[column-1].table[0]);
613 }
614 
615 //___________________________________________________________________
617 {
618  // Indicates whether a write on the column will definitely succeed.
619  //
620  // Parameters:
621  // column - the first column is 1, the second is 2, ...
622  // Returns:
623  // kTRUE if so
624  // Throws:
625  // TSQLException - if a database access error occurs
626  //
627  // Comment: the same as TSQLResultSetMetaData::IsWritable()
628 
630 
631  if( (UInt_t)(column-1) >= imp->fMYSQL_RES->field_count ) {
632  Throw(new TSQLException(Form("Invalid column number ( %d > %d )",column,imp->fMYSQL_RES->field_count+1),"S1002",);
633  return kFALSE;
634  }
635 
636  return (imp->MYSQL_RES->fields[column-1].table && imp->MYSQL_RES->fields[column-1].table[0]);
637 }