CernVM-FS  2.13.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
sql.cc
Go to the documentation of this file.
1 
6 #include "sql.h"
7 
8 #include "util/logging.h"
9 #include "util/string.h"
10 
11 using namespace std; // NOLINT
12 
13 namespace sqlite {
14 
15 Sql::Sql(sqlite3 *sqlite_db, const std::string &statement)
16  : database_(NULL)
17  , statement_(NULL)
18  , query_string_(NULL)
19  , last_error_code_(0) {
20  const bool success = Init(sqlite_db, statement);
21  assert(success);
22 }
23 
24 
26  last_error_code_ = sqlite3_finalize(statement_);
27 
28  if (!Successful()) {
30  "failed to finalize statement - error code: %d", last_error_code_);
31  }
32  LogCvmfs(kLogSql, kLogDebug, "successfully finalized statement");
33 }
34 
35 
41 bool Sql::Execute() {
42  LazyInit();
43  last_error_code_ = sqlite3_step(statement_);
44 #ifdef DEBUGMSG
45  if (!Successful()) {
46  LogCvmfs(kLogSql, kLogDebug, "SQL query failed - SQLite: %d - %s",
47  GetLastError(), GetLastErrorMsg().c_str());
48  }
49 #endif
50  return Successful();
51 }
52 
53 
61 bool Sql::FetchRow() {
62  LazyInit();
63  last_error_code_ = sqlite3_step(statement_);
64  return SQLITE_ROW == last_error_code_;
65 }
66 
67 
68 std::string Sql::DebugResultTable() {
69  std::string line;
70  std::string result;
71  unsigned int rows = 0;
72 
73  // go through all data rows
74  while (FetchRow()) {
75  // retrieve the table header (once)
76  const unsigned int cols = sqlite3_column_count(statement_);
77  if (rows == 0) {
78  for (unsigned int col = 0; col < cols; ++col) {
79  const char *name = sqlite3_column_name(statement_, col);
80  line += name;
81  if (col + 1 < cols)
82  line += " | ";
83  }
84  result += line + "\n";
85  line.clear();
86  }
87 
88  // retrieve the data fields for each row
89  for (unsigned int col = 0; col < cols; ++col) {
90  const int type = sqlite3_column_type(statement_, col);
91  switch (type) {
92  case SQLITE_INTEGER:
93  line += StringifyInt(RetrieveInt64(col));
94  break;
95  case SQLITE_FLOAT:
96  line += StringifyDouble(RetrieveDouble(col));
97  break;
98  case SQLITE_TEXT:
99  line += reinterpret_cast<const char *>(RetrieveText(col));
100  break;
101  case SQLITE_BLOB:
102  line += "[BLOB data]";
103  break;
104  case SQLITE_NULL:
105  line += "[NULL]";
106  break;
107  }
108  if (col + 1 < cols)
109  line += " | ";
110  }
111 
112  result += line + "\n";
113  line.clear();
114  ++rows;
115  }
116 
117  // print the result
118  result += "Retrieved Rows: " + StringifyInt(rows);
119  return result;
120 }
121 
122 
127 bool Sql::Reset() {
128  last_error_code_ = sqlite3_reset(statement_);
129  return Successful();
130 }
131 
132 
133 bool Sql::Init(const sqlite3 *database, const std::string &statement) {
134  database_ = const_cast<sqlite3 *>(database);
135  return Init(statement.c_str());
136 }
137 
138 bool Sql::Init(const char *statement) {
139  assert(NULL == statement_);
140  assert(NULL != database_);
141 
142  last_error_code_ = sqlite3_prepare_v2(database_,
143  statement,
144  -1, // parse until null termination
145  &statement_,
146  NULL);
147 
148  if (!Successful()) {
149  LogCvmfs(kLogSql, kLogDebug, "failed to prepare statement '%s' (%d: %s)",
150  statement, GetLastError(), sqlite3_errmsg(database_));
151  return false;
152  }
153 
154  LogCvmfs(kLogSql, kLogDebug, "successfully prepared statement '%s'",
155  statement);
156  return true;
157 }
158 
159 void Sql::DeferredInit(const sqlite3 *database, const char *statement) {
160  assert(NULL == database_);
161  database_ = const_cast<sqlite3 *>(database);
162  query_string_ = statement;
163 }
164 
165 std::string Sql::GetLastErrorMsg() const {
166  std::string msg = sqlite3_errmsg(database_);
167  return msg;
168 }
169 
170 } // namespace sqlite
const char * query_string_
Definition: sql.h:507
void LazyInit()
Definition: sql.h:496
std::string GetLastErrorMsg() const
Definition: sql.cc:165
bool Execute()
Definition: sql.cc:41
bool Reset()
Definition: sql.cc:127
bool FetchRow()
Definition: sql.cc:61
bool Init(const sqlite3 *database, const std::string &statement)
Definition: sql.cc:133
double RetrieveDouble(const int idx_column) const
Definition: sql.h:434
int last_error_code_
Definition: sql.h:508
assert((mem||(size==0))&&"Out Of Memory")
std::string DebugResultTable()
Definition: sql.cc:68
string StringifyDouble(const double value)
Definition: string.cc:95
void DeferredInit(const sqlite3 *database, const char *statement)
Definition: sql.cc:159
int GetLastError() const
Definition: sql.h:340
sqlite3 * database_
Definition: sql.h:505
sqlite3_int64 RetrieveInt64(const int idx_column) const
Definition: sql.h:445
string StringifyInt(const int64_t value)
Definition: string.cc:77
sqlite3_stmt * statement_
Definition: sql.h:506
virtual ~Sql()
Definition: sql.cc:25
const unsigned char * RetrieveText(const int idx_column) const
Definition: sql.h:448
bool Successful() const
Definition: sql.h:489
CVMFS_EXPORT void LogCvmfs(const LogSource source, const int mask, const char *format,...)
Definition: logging.cc:545