00001 // @(#)root/pgsql:$Id: TPgSQLResult.cxx 20882 2007-11-19 11:31:26Z rdm $ 00002 // Author: g.p.ciceri <gp.ciceri@acm.org> 01/06/2001 00003 00004 /************************************************************************* 00005 * Copyright (C) 1995-2001, Rene Brun and Fons Rademakers. * 00006 * All rights reserved. * 00007 * * 00008 * For the licensing terms see $ROOTSYS/LICENSE. * 00009 * For the list of contributors see $ROOTSYS/README/CREDITS. * 00010 *************************************************************************/ 00011 00012 #include "TPgSQLResult.h" 00013 #include "TPgSQLRow.h" 00014 00015 00016 ClassImp(TPgSQLResult) 00017 00018 //______________________________________________________________________________ 00019 TPgSQLResult::TPgSQLResult(void *result) 00020 { 00021 // PgSQL query result. 00022 00023 fResult = (PGresult *) result; 00024 fRowCount = fResult ? PQntuples(fResult) : 0; 00025 fCurrentRow = 0; 00026 } 00027 00028 //______________________________________________________________________________ 00029 TPgSQLResult::~TPgSQLResult() 00030 { 00031 // Cleanup PgSQL query result. 00032 00033 if (fResult) 00034 Close(); 00035 } 00036 00037 //______________________________________________________________________________ 00038 void TPgSQLResult::Close(Option_t *) 00039 { 00040 // Close query result. 00041 00042 if (!fResult) 00043 return; 00044 00045 PQclear(fResult); 00046 fResult = 0; 00047 fRowCount = 0; 00048 fCurrentRow = 0; 00049 } 00050 00051 //______________________________________________________________________________ 00052 Bool_t TPgSQLResult::IsValid(Int_t field) 00053 { 00054 // Check if result set is open and field index within range. 00055 00056 if (!fResult) { 00057 Error("IsValid", "result set closed"); 00058 return kFALSE; 00059 } 00060 if (field < 0 || field >= GetFieldCount()) { 00061 Error("IsValid", "field index out of bounds"); 00062 return kFALSE; 00063 } 00064 return kTRUE; 00065 } 00066 00067 //______________________________________________________________________________ 00068 Int_t TPgSQLResult::GetFieldCount() 00069 { 00070 // Get number of fields in result. 00071 00072 if (!fResult) { 00073 Error("GetFieldCount", "result set closed"); 00074 return 0; 00075 } 00076 return PQnfields(fResult); 00077 } 00078 00079 //______________________________________________________________________________ 00080 const char *TPgSQLResult::GetFieldName(Int_t field) 00081 { 00082 // Get name of specified field. 00083 00084 if (!fResult) { 00085 Error("GetFieldName", "result set closed"); 00086 return 0; 00087 } 00088 return PQfname(fResult, field); 00089 } 00090 00091 //______________________________________________________________________________ 00092 TSQLRow *TPgSQLResult::Next() 00093 { 00094 // Get next query result row. The returned object must be 00095 // deleted by the user. 00096 00097 Int_t row; 00098 00099 if (!fResult) { 00100 Error("Next", "result set closed"); 00101 return 0; 00102 } 00103 row = fCurrentRow++; 00104 if (row >= fRowCount) 00105 return 0; 00106 else 00107 return new TPgSQLRow((void *) fResult, (ULong_t) row); 00108 }