Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
TMySQLRow.cxx
Go to the documentation of this file.
1 // @(#)root/mysql:$Id$
2 // Author: Fons Rademakers 15/02/2000
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 #include "TMySQLRow.h"
13 
14 
15 ClassImp(TMySQLRow);
16 
17 ////////////////////////////////////////////////////////////////////////////////
18 /// Single row of query result.
19 
20 TMySQLRow::TMySQLRow(void *res, ULong_t rowHandle)
21 {
22  fResult = (MYSQL_RES *) res;
23  fFields = (MYSQL_ROW) rowHandle;
24  fFieldLength = 0;
25 }
26 
27 ////////////////////////////////////////////////////////////////////////////////
28 /// Destroy row object.
29 
30 TMySQLRow::~TMySQLRow()
31 {
32  if (fFields)
33  Close();
34 }
35 
36 ////////////////////////////////////////////////////////////////////////////////
37 /// Close row.
38 
39 void TMySQLRow::Close(Option_t *)
40 {
41  if (!fFields)
42  return;
43 
44  fFields = 0;
45  fResult = 0;
46  fFieldLength = 0;
47 }
48 
49 ////////////////////////////////////////////////////////////////////////////////
50 /// Check if row is open and field index within range.
51 
52 Bool_t TMySQLRow::IsValid(Int_t field)
53 {
54  if (!fFields) {
55  Error("IsValid", "row closed");
56  return kFALSE;
57  }
58  if (field < 0 || field >= (Int_t)mysql_num_fields(fResult)) {
59  Error("IsValid", "field index out of bounds");
60  return kFALSE;
61  }
62  return kTRUE;
63 }
64 
65 ////////////////////////////////////////////////////////////////////////////////
66 /// Get length in bytes of specified field.
67 
68 ULong_t TMySQLRow::GetFieldLength(Int_t field)
69 {
70  if (!IsValid(field))
71  return 0;
72 
73  if (!fFieldLength)
74  fFieldLength = mysql_fetch_lengths(fResult);
75 
76  if (!fFieldLength) {
77  Error("GetFieldLength", "cannot get field length");
78  return 0;
79  }
80 
81  return fFieldLength[field];
82 }
83 
84 ////////////////////////////////////////////////////////////////////////////////
85 /// Get specified field from row (0 <= field < GetFieldCount()).
86 
87 const char *TMySQLRow::GetField(Int_t field)
88 {
89  if (!IsValid(field))
90  return 0;
91 
92  return fFields[field];
93 }