Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
TSQLiteRow.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 "TSQLiteRow.h"
13 
14 #include <sqlite3.h>
15 
16 
17 ClassImp(TSQLiteRow);
18 
19 ////////////////////////////////////////////////////////////////////////////////
20 /// Single row of query result.
21 
22 TSQLiteRow::TSQLiteRow(void *res, ULong_t /*rowHandle*/)
23 {
24  fResult = (sqlite3_stmt *) res;
25 }
26 
27 ////////////////////////////////////////////////////////////////////////////////
28 /// Destroy row object.
29 
30 TSQLiteRow::~TSQLiteRow()
31 {
32  if (fResult)
33  Close();
34 }
35 
36 ////////////////////////////////////////////////////////////////////////////////
37 /// Close row.
38 
39 void TSQLiteRow::Close(Option_t *)
40 {
41  fResult = 0;
42 }
43 
44 ////////////////////////////////////////////////////////////////////////////////
45 /// Check if row is open and field index within range.
46 
47 Bool_t TSQLiteRow::IsValid(Int_t field)
48 {
49  if (field < 0 || field >= (Int_t)sqlite3_column_count(fResult)) {
50  Error("IsValid", "field index out of bounds");
51  return kFALSE;
52  }
53  return kTRUE;
54 }
55 
56 ////////////////////////////////////////////////////////////////////////////////
57 /// Get length in bytes of specified field.
58 
59 ULong_t TSQLiteRow::GetFieldLength(Int_t field)
60 {
61  if (!IsValid(field))
62  return 0;
63 
64  // Should call the access-method first, so sqlite3 can check whether a NULL-terminator
65  // needs to be added to the byte-count, e.g. for BLOB!
66  sqlite3_column_text(fResult, field);
67 
68  ULong_t fieldLength = (ULong_t) sqlite3_column_bytes(fResult, field);
69 
70  if (!fieldLength) {
71  Error("GetFieldLength", "cannot get field length");
72  return 0;
73  }
74 
75  return fieldLength;
76 }
77 
78 ////////////////////////////////////////////////////////////////////////////////
79 /// Get specified field from row (0 <= field < GetFieldCount()).
80 
81 const char *TSQLiteRow::GetField(Int_t field)
82 {
83  if (!IsValid(field))
84  return 0;
85 
86  return reinterpret_cast<const char*>(sqlite3_column_text(fResult, field));
87 }
88