Skip to content

Commit 10ea678

Browse files
committed
Support StringView in SQLite
1 parent 21aa34f commit 10ea678

File tree

3 files changed

+27
-13
lines changed

3 files changed

+27
-13
lines changed

src/Sqlite.cpp

+10-8
Original file line numberDiff line numberDiff line change
@@ -74,20 +74,20 @@ bool Sqlite::Statement::step() {
7474
}
7575

7676
Sqlite::Statement &Sqlite::Statement::bindBlob(
77-
const std::string &param, const void *data, size_t length) {
77+
StringView param, const void *data, size_t length) {
7878
R(sqlite3_bind_blob(statement_, P(param), data, length, SQLITE_TRANSIENT));
7979
return *this;
8080
}
8181

82-
Sqlite::Statement &Sqlite::Statement::bindInt64(const std::string &param,
82+
Sqlite::Statement &Sqlite::Statement::bindInt64(StringView param,
8383
int64_t data) {
8484
R(sqlite3_bind_int64(statement_, P(param), data));
8585
return *this;
8686
}
8787

88-
Sqlite::Statement &Sqlite::Statement::bindString(const std::string &param,
89-
const std::string &data) {
90-
R(sqlite3_bind_text(statement_, P(param), data.c_str(), data.size(),
88+
Sqlite::Statement &Sqlite::Statement::bindString(StringView param,
89+
StringView data) {
90+
R(sqlite3_bind_text(statement_, P(param), data.begin(), data.length(),
9191
SQLITE_TRANSIENT));
9292
return *this;
9393
}
@@ -108,11 +108,13 @@ std::string Sqlite::Statement::columnName(int index) const {
108108
return sqlite3_column_name(statement_, index);
109109
}
110110

111-
int Sqlite::Statement::P(const std::string &param) const {
112-
auto index = sqlite3_bind_parameter_index(statement_, param.c_str());
111+
int Sqlite::Statement::P(StringView param) const {
112+
std::string asStringStorage;
113+
auto index = sqlite3_bind_parameter_index(statement_,
114+
param.c_str(asStringStorage));
113115
if (index == 0)
114116
throw std::runtime_error(
115-
"Unable to find bound parameter '" + param + "'");
117+
"Unable to find bound parameter '" + param.str() + "'");
116118
return index;
117119
}
118120

src/Sqlite.h

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
#pragma once
22

3+
#include "StringView.h"
4+
35
#include <cstdint>
46
#include <string>
57
#include <sqlite3.h>
8+
#include <unordered_map>
69
#include <vector>
710

811
class Log;
@@ -56,11 +59,10 @@ class Sqlite {
5659
Statement &operator=(Statement &&other);
5760

5861
Statement &reset();
59-
Statement &bindInt64(const std::string &param, int64_t data);
60-
Statement &bindBlob(const std::string &param, const void *data,
62+
Statement &bindInt64(StringView param, int64_t data);
63+
Statement &bindBlob(StringView param, const void *data,
6164
size_t length);
62-
Statement &bindString(const std::string &param,
63-
const std::string &string);
65+
Statement &bindString(StringView param, StringView string);
6466

6567
bool step();
6668

@@ -72,7 +74,7 @@ class Sqlite {
7274
std::vector<uint8_t> columnBlob(int index) const;
7375

7476
private:
75-
int P(const std::string &param) const;
77+
int P(StringView param) const;
7678
};
7779

7880
Statement prepare(const std::string &sql) const;

src/StringView.h

+10
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,15 @@ class StringView {
3838
return length_ ? std::string(begin_, length_) : std::string();
3939
}
4040

41+
// Return a zero-terminated c-style string, using the StringView storage
42+
// itself if possible, else placing the zero-terminated string in the
43+
// supplied std::string. Only works for strings without embedded nuls.
44+
const char *c_str(std::string &storage) const {
45+
if (length_ == 0) return "";
46+
if (begin_[length_] == '\0') return begin_; // already zero-terminated
47+
storage.assign(begin_, length_);
48+
return storage.c_str();
49+
}
50+
4151
friend std::ostream &operator<<(std::ostream &o, const StringView &sv);
4252
};

0 commit comments

Comments
 (0)