Logo ROOT   6.14/05
Reference Guide
TSapDBResult.cxx
Go to the documentation of this file.
1 // @(#)root/sapdb:$Id$
2 // Author: Mark Hemberger & Fons Rademakers 03/08/2001
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2001, 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 "TString.h"
13 #include "TSapDBResult.h"
14 #include "TSapDBRow.h"
15 
16 
18 
19 ////////////////////////////////////////////////////////////////////////////////
20 /// SapDB query result.
21 
22 TSapDBResult::TSapDBResult(SQLHSTMT result, SDWORD rowCount)
23 {
24  fResult = result;
25  fFieldNames = 0;
26  fFieldCount = -1;
27  fRowCount = 0;
28 
29  if (fResult) {
30  SQLLEN rowcount = 0;
31  if (SQLRowCount(fResult, &rowcount) != SQL_SUCCESS) {
32  Error("TSapDBResult", "no rows counted");
33  }
34  // -1 means: result has been found but the number of columns is
35  // undetermined (only for SYSTEM tables, a valid number is available)
36  fRowCount = rowcount < 0 ? rowCount : rowcount;
37  }
38 }
39 
40 ////////////////////////////////////////////////////////////////////////////////
41 /// Cleanup SapDB query result.
42 
44 {
45  if (fResult)
46  Close();
47 }
48 
49 ////////////////////////////////////////////////////////////////////////////////
50 /// Close query result.
51 
53 {
54  if (!fResult)
55  return;
56 
57  delete [] fFieldNames;
58  fFieldNames = 0;
59  fFieldCount = 0;
60  fResult = 0;
61  fRowCount = 0;
62 }
63 
64 ////////////////////////////////////////////////////////////////////////////////
65 /// Check if result set is open and field index within range.
66 
68 {
69  if (!fResult) {
70  Error("IsValid", "result set closed");
71  return kFALSE;
72  }
73  if (field < 0 || field >= GetFieldCount()) {
74  Error("IsValid", "field index out of bounds");
75  return kFALSE;
76  }
77  return kTRUE;
78 }
79 
80 ////////////////////////////////////////////////////////////////////////////////
81 /// Get number of fields in result.
82 
84 {
85  if (!fResult) {
86  Error("GetFieldCount", "result set closed");
87  return 0;
88  }
89 
90  if (fFieldCount >= 0)
91  return fFieldCount;
92 
93  SQLSMALLINT columnCount;
94  if (SQLNumResultCols(fResult, &columnCount) == SQL_SUCCESS)
95  fFieldCount = columnCount;
96  else
97  fFieldCount = 0;
98 
99  return fFieldCount;
100 }
101 
102 ////////////////////////////////////////////////////////////////////////////////
103 /// Get name of specified field.
104 
106 {
107  if (!IsValid(field))
108  return 0;
109 
110  if (!fFieldNames)
112 
113  if (!fFieldNames[field].IsNull())
114  return fFieldNames[field];
115 
116  // Get name of specified field.
117  SQLUSMALLINT columnNumber;
118  SQLCHAR columnName[256];
119  SQLSMALLINT bufferLength = 256;
120  SQLSMALLINT nameLength;
121  SQLSMALLINT dataType;
122  SQLULEN columnSize;
123  SQLSMALLINT decimalDigits;
124  SQLSMALLINT nullable;
125 
126  columnNumber = field + 1;
127  if (SQLDescribeCol(fResult, columnNumber, columnName, bufferLength,
128  &nameLength, &dataType, &columnSize, &decimalDigits,
129  &nullable) == SQL_SUCCESS) {
130  //printf ("ColumnNumber: %d\n", columnNumber);
131  //printf ("ColumnName: %s\n", columnName);
132  //printf ("DataType: %d\n", dataType);
133  //printf ("ColumnSize: %ld\n", columnSize);
134  fFieldNames[field] = (const char *)columnName;
135  } else {
136  Error("GetFieldName", "cannot get field info");
137  return 0;
138  }
139 
140  return fFieldNames[field];
141 }
142 
143 ////////////////////////////////////////////////////////////////////////////////
144 /// Get next query result row. The returned object must be
145 /// deleted by the user.
146 
148 {
149  if (!fResult) {
150  Error("Next", "result set closed");
151  return 0;
152  }
153 
154  RETCODE rc = SQLFetchScroll(fResult, SQL_FETCH_NEXT, 0);
155  if (rc == SQL_SUCCESS)
156  return new TSapDBRow(fResult, GetFieldCount());
157  else if (rc == SQL_NO_DATA)
158  return 0;
159  else {
160  Error("Next", "error during fetchscroll");
161  return 0;
162  }
163 
164  return 0;
165 }
const char * GetFieldName(Int_t field)
Get name of specified field.
Int_t fFieldCount
Definition: TSapDBResult.h:41
const char Option_t
Definition: RtypesCore.h:62
Basic string class.
Definition: TString.h:131
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
TSapDBResult(SQLHSTMT fStmt, SDWORD rowCount=0)
SapDB query result.
TString * fFieldNames
Definition: TSapDBResult.h:40
SQLHSTMT fResult
Definition: TSapDBResult.h:39
TSQLRow * Next()
Get next query result row.
Int_t GetFieldCount()
Get number of fields in result.
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:880
const Bool_t kFALSE
Definition: RtypesCore.h:88
#define ClassImp(name)
Definition: Rtypes.h:359
~TSapDBResult()
Cleanup SapDB query result.
void Close(Option_t *opt="")
Close query result.
Int_t fRowCount
Definition: TSQLResult.h:35
const Bool_t kTRUE
Definition: RtypesCore.h:87
Bool_t IsValid(Int_t field)
Check if result set is open and field index within range.