Logo ROOT  
Reference Guide
TSQLiteResult.cxx
Go to the documentation of this file.
1 // @(#)root/sqlite:$Id$
2 // Author: o.freyermuth <o.f@cern.ch>, 01/06/2013
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2013, 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 "TSQLiteResult.h"
13 #include "TSQLiteRow.h"
14 
15 #include <sqlite3.h>
16 
18 
19 ////////////////////////////////////////////////////////////////////////////////
20 /// SQLite query result.
21 
23 {
24  fResult = (sqlite3_stmt *) result;
25 
26  // RowCount is -1, as sqlite cannot determine RowCount beforehand:
27  fRowCount = -1;
28 }
29 
30 ////////////////////////////////////////////////////////////////////////////////
31 /// Cleanup SQLite query result.
32 
34 {
35  if (fResult)
36  Close();
37 }
38 
39 ////////////////////////////////////////////////////////////////////////////////
40 /// Close query result.
41 
43 {
44  if (!fResult)
45  return;
46 
47  sqlite3_finalize(fResult);
48  fResult = nullptr;
49 }
50 
51 ////////////////////////////////////////////////////////////////////////////////
52 /// Check if result set is open and field index within range.
53 
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 
71 {
72  if (!fResult) {
73  Error("GetFieldCount", "result set closed");
74  return 0;
75  }
76  return sqlite3_column_count(fResult);
77 }
78 
79 ////////////////////////////////////////////////////////////////////////////////
80 /// Get name of specified field.
81 
83 {
84  if (!fResult) {
85  Error("GetFieldName", "result set closed");
86  return nullptr;
87  }
88  return sqlite3_column_name(fResult, field);
89 }
90 
91 ////////////////////////////////////////////////////////////////////////////////
92 /// SQLite can not determine the row count for a Query, return -1 instead.
93 /// For similar functionality, call Next() until it retruns nullptr.
94 
96 {
97  return -1;
98 }
99 
100 ////////////////////////////////////////////////////////////////////////////////
101 /// Get next query result row. The returned object must be
102 /// deleted by the user.
103 
105 {
106  if (!fResult) {
107  Error("Next", "result set closed");
108  return nullptr;
109  }
110 
111  int ret = sqlite3_step(fResult);
112  if ((ret != SQLITE_DONE) && (ret != SQLITE_ROW)) {
113  Error("Statement", "SQL Error: %d %s", ret, sqlite3_errmsg(sqlite3_db_handle(fResult)));
114  return nullptr;
115  }
116  if (ret == SQLITE_DONE) {
117  // Finished executing, no other row!
118  return nullptr;
119  }
120  return new TSQLiteRow((void *) fResult, -1);
121 }
122 
kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:91
TSQLiteRow.h
TSQLiteRow
Definition: TSQLiteRow.h:19
Option_t
const char Option_t
Definition: RtypesCore.h:66
TSQLiteResult::~TSQLiteResult
~TSQLiteResult()
Cleanup SQLite query result.
Definition: TSQLiteResult.cxx:33
ClassImp
#define ClassImp(name)
Definition: Rtypes.h:364
TObject::Error
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:890
TSQLiteResult.h
bool
TSQLiteResult::IsValid
Bool_t IsValid(Int_t field)
Check if result set is open and field index within range.
Definition: TSQLiteResult.cxx:54
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:92
TSQLRow
Definition: TSQLRow.h:30
TSQLiteResult::fResult
sqlite3_stmt * fResult
Definition: TSQLiteResult.h:22
TSQLiteResult::Next
TSQLRow * Next() final
Get next query result row.
Definition: TSQLiteResult.cxx:104
TSQLiteResult::GetRowCount
Int_t GetRowCount() const final
SQLite can not determine the row count for a Query, return -1 instead.
Definition: TSQLiteResult.cxx:95
TSQLiteResult::GetFieldCount
Int_t GetFieldCount() final
Get number of fields in result.
Definition: TSQLiteResult.cxx:70
TSQLiteResult::TSQLiteResult
TSQLiteResult(void *result)
SQLite query result.
Definition: TSQLiteResult.cxx:22
TSQLiteResult::GetFieldName
const char * GetFieldName(Int_t field) final
Get name of specified field.
Definition: TSQLiteResult.cxx:82
TSQLiteResult
Definition: TSQLiteResult.h:19
TSQLResult::fRowCount
Int_t fRowCount
Definition: TSQLResult.h:35
int
TSQLiteResult::Close
void Close(Option_t *opt="") final
Close query result.
Definition: TSQLiteResult.cxx:42