Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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 ClassImp(TSQLiteResult);
16 
17 ////////////////////////////////////////////////////////////////////////////////
18 /// SQLite query result.
19 
20 TSQLiteResult::TSQLiteResult(void *result)
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 
31 TSQLiteResult::~TSQLiteResult()
32 {
33  if (fResult)
34  Close();
35 }
36 
37 ////////////////////////////////////////////////////////////////////////////////
38 /// Close query result.
39 
40 void TSQLiteResult::Close(Option_t *)
41 {
42  if (!fResult)
43  return;
44 
45  sqlite3_finalize(fResult);
46  fResult = 0;
47 }
48 
49 ////////////////////////////////////////////////////////////////////////////////
50 /// Check if result set is open and field index within range.
51 
52 Bool_t TSQLiteResult::IsValid(Int_t field)
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 
68 Int_t TSQLiteResult::GetFieldCount()
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 
80 const char *TSQLiteResult::GetFieldName(Int_t field)
81 {
82  if (!fResult) {
83  Error("GetFieldName", "result set closed");
84  return 0;
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 
93 Int_t TSQLiteResult::GetRowCount() const
94 {
95  return -1;
96 }
97 
98 ////////////////////////////////////////////////////////////////////////////////
99 /// Get next query result row. The returned object must be
100 /// deleted by the user.
101 
102 TSQLRow *TSQLiteResult::Next()
103 {
104  if (!fResult) {
105  Error("Next", "result set closed");
106  return 0;
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 NULL;
113  }
114  if (ret == SQLITE_DONE) {
115  // Finished executing, no other row!
116  return NULL;
117  }
118  return new TSQLiteRow((void *) fResult, -1);
119 }
120