Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
TPgSQLResult.cxx
Go to the documentation of this file.
1 // @(#)root/pgsql:$Id$
2 // Author: g.p.ciceri <gp.ciceri@acm.org> 01/06/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 "TPgSQLResult.h"
13 #include "TPgSQLRow.h"
14 
15 
16 ClassImp(TPgSQLResult);
17 
18 ////////////////////////////////////////////////////////////////////////////////
19 /// PgSQL query result.
20 
21 TPgSQLResult::TPgSQLResult(void *result)
22 {
23  fResult = (PGresult *) result;
24  fRowCount = fResult ? PQntuples(fResult) : 0;
25  fCurrentRow = 0;
26 }
27 
28 ////////////////////////////////////////////////////////////////////////////////
29 /// Cleanup PgSQL query result.
30 
31 TPgSQLResult::~TPgSQLResult()
32 {
33  if (fResult)
34  Close();
35 }
36 
37 ////////////////////////////////////////////////////////////////////////////////
38 /// Close query result.
39 
40 void TPgSQLResult::Close(Option_t *)
41 {
42  if (!fResult)
43  return;
44 
45  PQclear(fResult);
46  fResult = 0;
47  fRowCount = 0;
48  fCurrentRow = 0;
49 }
50 
51 ////////////////////////////////////////////////////////////////////////////////
52 /// Check if result set is open and field index within range.
53 
54 Bool_t TPgSQLResult::IsValid(Int_t field)
55 {
56  if (!fResult) {
57  Error("IsValid", "result set closed");
58  return kFALSE;
59  }
60  if (field < 0 || field >= GetFieldCount()) {
61  Error("IsValid", "field index out of bounds");
62  return kFALSE;
63  }
64  return kTRUE;
65 }
66 
67 ////////////////////////////////////////////////////////////////////////////////
68 /// Get number of fields in result.
69 
70 Int_t TPgSQLResult::GetFieldCount()
71 {
72  if (!fResult) {
73  Error("GetFieldCount", "result set closed");
74  return 0;
75  }
76  return PQnfields(fResult);
77 }
78 
79 ////////////////////////////////////////////////////////////////////////////////
80 /// Get name of specified field.
81 
82 const char *TPgSQLResult::GetFieldName(Int_t field)
83 {
84  if (!fResult) {
85  Error("GetFieldName", "result set closed");
86  return 0;
87  }
88  return PQfname(fResult, field);
89 }
90 
91 ////////////////////////////////////////////////////////////////////////////////
92 /// Get next query result row. The returned object must be
93 /// deleted by the user.
94 
95 TSQLRow *TPgSQLResult::Next()
96 {
97  Int_t row;
98 
99  if (!fResult) {
100  Error("Next", "result set closed");
101  return 0;
102  }
103  row = fCurrentRow++;
104  if (row >= fRowCount)
105  return 0;
106  else
107  return new TPgSQLRow((void *) fResult, (ULong_t) row);
108 }