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
16
17////////////////////////////////////////////////////////////////////////////////
18/// SQLite query result.
19
21{
22 fResult = (sqlite3_stmt *) result;
23
24 // RowCount is -1, as sqlite cannot determine RowCount beforehand:
25 fRowCount = -1;
26}
27
28////////////////////////////////////////////////////////////////////////////////
29/// Cleanup SQLite query result.
30
32{
33 if (fResult)
34 Close();
35}
36
37////////////////////////////////////////////////////////////////////////////////
38/// Close query result.
39
41{
42 if (!fResult)
43 return;
44
45 sqlite3_finalize(fResult);
46 fResult = nullptr;
47}
48
49////////////////////////////////////////////////////////////////////////////////
50/// Check if result set is open and field index within range.
51
53{
54 if (!fResult) {
55 Error("IsValid", "result set closed");
56 return kFALSE;
57 }
58 if (field < 0 || field >= GetFieldCount()) {
59 Error("IsValid", "field index out of bounds");
60 return kFALSE;
61 }
62 return kTRUE;
63}
64
65////////////////////////////////////////////////////////////////////////////////
66/// Get number of fields in result.
67
69{
70 if (!fResult) {
71 Error("GetFieldCount", "result set closed");
72 return 0;
73 }
74 return sqlite3_column_count(fResult);
75}
76
77////////////////////////////////////////////////////////////////////////////////
78/// Get name of specified field.
79
81{
82 if (!fResult) {
83 Error("GetFieldName", "result set closed");
84 return nullptr;
85 }
86 return sqlite3_column_name(fResult, field);
87}
88
89////////////////////////////////////////////////////////////////////////////////
90/// SQLite can not determine the row count for a Query, return -1 instead.
91/// For similar functionality, call Next() until it retruns nullptr.
92
94{
95 return -1;
96}
97
98////////////////////////////////////////////////////////////////////////////////
99/// Get next query result row. The returned object must be
100/// deleted by the user.
101
103{
104 if (!fResult) {
105 Error("Next", "result set closed");
106 return nullptr;
107 }
108
109 int ret = sqlite3_step(fResult);
110 if ((ret != SQLITE_DONE) && (ret != SQLITE_ROW)) {
111 Error("Statement", "SQL Error: %d %s", ret, sqlite3_errmsg(sqlite3_db_handle(fResult)));
112 return nullptr;
113 }
114 if (ret == SQLITE_DONE) {
115 // Finished executing, no other row!
116 return nullptr;
117 }
118 return new TSQLiteRow((void *) fResult, -1);
119}
120
const Bool_t kFALSE
Definition: RtypesCore.h:90
const Bool_t kTRUE
Definition: RtypesCore.h:89
const char Option_t
Definition: RtypesCore.h:64
#define ClassImp(name)
Definition: Rtypes.h:361
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:891
Int_t fRowCount
Definition: TSQLResult.h:35
void Close(Option_t *opt="") final
Close query result.
sqlite3_stmt * fResult
Definition: TSQLiteResult.h:22
const char * GetFieldName(Int_t field) final
Get name of specified field.
Int_t GetRowCount() const final
SQLite can not determine the row count for a Query, return -1 instead.
~TSQLiteResult()
Cleanup SQLite query result.
TSQLRow * Next() final
Get next query result row.
Int_t GetFieldCount() final
Get number of fields in result.
Bool_t IsValid(Int_t field)
Check if result set is open and field index within range.
TSQLiteResult(void *result)
SQLite query result.