TMySQLResult.cxx

Go to the documentation of this file.
00001 // @(#)root/mysql:$Id: TMySQLResult.cxx 20882 2007-11-19 11:31:26Z rdm $
00002 // Author: Fons Rademakers   15/02/2000
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2000, 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 "TMySQLResult.h"
00013 #include "TMySQLRow.h"
00014 
00015 
00016 ClassImp(TMySQLResult)
00017 
00018 //______________________________________________________________________________
00019 TMySQLResult::TMySQLResult(void *result)
00020 {
00021    // MySQL query result.
00022    
00023    fResult    = (MYSQL_RES *) result;
00024    fRowCount  = fResult ? mysql_num_rows(fResult) : 0;
00025    fFieldInfo = 0;
00026 }
00027 
00028 //______________________________________________________________________________
00029 TMySQLResult::~TMySQLResult()
00030 {
00031    // Cleanup MySQL query result.
00032 
00033    if (fResult)
00034       Close();
00035 }
00036 
00037 //______________________________________________________________________________
00038 void TMySQLResult::Close(Option_t *)
00039 {
00040    // Close query result.
00041 
00042    if (!fResult)
00043       return;
00044 
00045    mysql_free_result(fResult);
00046    fResult    = 0;
00047    fFieldInfo = 0;
00048    fRowCount  = 0;
00049 }
00050 
00051 //______________________________________________________________________________
00052 Bool_t TMySQLResult::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 TMySQLResult::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 mysql_num_fields(fResult);
00077 }
00078 
00079 //______________________________________________________________________________
00080 const char *TMySQLResult::GetFieldName(Int_t field)
00081 {
00082    // Get name of specified field.
00083    
00084    if (!IsValid(field))
00085       return 0;
00086 
00087    if (!fFieldInfo)
00088       fFieldInfo = mysql_fetch_fields(fResult);
00089    
00090    if (!fFieldInfo) {
00091       Error("GetFieldName", "cannot get field info");
00092       return 0;
00093    }
00094 
00095    return fFieldInfo[field].name;
00096 }
00097 
00098 //______________________________________________________________________________
00099 TSQLRow *TMySQLResult::Next()
00100 {
00101    // Get next query result row. The returned object must be
00102    // deleted by the user.
00103 
00104    MYSQL_ROW row;
00105     
00106    if (!fResult) {
00107       Error("Next", "result set closed");
00108       return 0;
00109    }
00110    row = mysql_fetch_row(fResult);
00111    if (!row)
00112       return 0;
00113    else
00114       return new TMySQLRow((void *) fResult, (ULong_t) row);
00115 }

Generated on Tue Jul 5 15:14:38 2011 for ROOT_528-00b_version by  doxygen 1.5.1