CernVM-FS  2.12.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
sql.cc
Go to the documentation of this file.
1 
5 #include "cvmfs_config.h"
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 {
21  const bool success = Init(sqlite_db, statement);
22  assert(success);
23 }
24 
25 
27  last_error_code_ = sqlite3_finalize(statement_);
28 
29  if (!Successful()) {
31  "failed to finalize statement - error code: %d", last_error_code_);
32  }
33  LogCvmfs(kLogSql, kLogDebug, "successfully finalized statement");
34 }
35 
36 
42 bool Sql::Execute() {
43  LazyInit();
44  last_error_code_ = sqlite3_step(statement_);
45 #ifdef DEBUGMSG
46  if (!Successful()) {
47  LogCvmfs(kLogSql, kLogDebug, "SQL query failed - SQLite: %d - %s",
48  GetLastError(), GetLastErrorMsg().c_str());
49  }
50 #endif
51  return Successful();
52 }
53 
54 
62 bool Sql::FetchRow() {
63  LazyInit();
64  last_error_code_ = sqlite3_step(statement_);
65  return SQLITE_ROW == last_error_code_;
66 }
67 
68 
69 std::string Sql::DebugResultTable() {
70  std::string line;
71  std::string result;
72  unsigned int rows = 0;
73 
74  // go through all data rows
75  while (FetchRow()) {
76  // retrieve the table header (once)
77  const unsigned int cols = sqlite3_column_count(statement_);
78  if (rows == 0) {
79  for (unsigned int col = 0; col < cols; ++col) {
80  const char *name = sqlite3_column_name(statement_, col);
81  line += name;
82  if (col + 1 < cols) 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) line += " | ";
109  }
110 
111  result += line + "\n";
112  line.clear();
113  ++rows;
114  }
115 
116  // print the result
117  result += "Retrieved Rows: " + StringifyInt(rows);
118  return result;
119 }
120 
121 
126 bool Sql::Reset() {
127  last_error_code_ = sqlite3_reset(statement_);
128  return Successful();
129 }
130 
131 
132 bool Sql::Init(const sqlite3 *database, const std::string &statement) {
133  database_ = const_cast<sqlite3 *>(database);
134  return Init(statement.c_str());
135 }
136 
137 bool Sql::Init(const char *statement) {
138  assert(NULL == statement_);
139  assert(NULL != database_);
140 
141  last_error_code_ = sqlite3_prepare_v2(database_,
142  statement,
143  -1, // parse until null termination
144  &statement_,
145  NULL);
146 
147  if (!Successful()) {
148  LogCvmfs(kLogSql, kLogDebug, "failed to prepare statement '%s' (%d: %s)",
149  statement, GetLastError(), sqlite3_errmsg(database_));
150  return false;
151  }
152 
153  LogCvmfs(kLogSql, kLogDebug, "successfully prepared statement '%s'",
154  statement);
155  return true;
156 }
157 
158 void Sql::DeferredInit(const sqlite3 *database, const char *statement) {
159  assert(NULL == database_);
160  database_ = const_cast<sqlite3 *>(database);
161  query_string_ = statement;
162 }
163 
164 std::string Sql::GetLastErrorMsg() const {
165  std::string msg = sqlite3_errmsg(database_);
166  return msg;
167 }
168 
169 } // namespace sqlite
const char * query_string_
Definition: sql.h:508
void LazyInit()
Definition: sql.h:497
std::string GetLastErrorMsg() const
Definition: sql.cc:164
bool Execute()
Definition: sql.cc:42
bool Reset()
Definition: sql.cc:126
bool FetchRow()
Definition: sql.cc:62
bool Init(const sqlite3 *database, const std::string &statement)
Definition: sql.cc:132
double RetrieveDouble(const int idx_column) const
Definition: sql.h:439
int last_error_code_
Definition: sql.h:509
assert((mem||(size==0))&&"Out Of Memory")
std::string DebugResultTable()
Definition: sql.cc:69
string StringifyDouble(const double value)
Definition: string.cc:96
void DeferredInit(const sqlite3 *database, const char *statement)
Definition: sql.cc:158
int GetLastError() const
Definition: sql.h:343
sqlite3 * database_
Definition: sql.h:506
sqlite3_int64 RetrieveInt64(const int idx_column) const
Definition: sql.h:445
string StringifyInt(const int64_t value)
Definition: string.cc:78
sqlite3_stmt * statement_
Definition: sql.h:507
virtual ~Sql()
Definition: sql.cc:26
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:528