Skip to content

Commit 4d98d15

Browse files
committed
add nullptr in binder, 'binder() << nullptr'
1 parent 2534d35 commit 4d98d15

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

headeronly_src/sqlite3pp.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,14 @@ namespace sqlite3pp
226226
++idx_;
227227
return *this;
228228
}
229+
bindstream& operator << (std::nullptr_t value) {
230+
auto rc = cmd_.bind(idx_);
231+
if (rc != SQLITE_OK) {
232+
throw database_error(cmd_.db_);
233+
}
234+
++idx_;
235+
return *this;
236+
}
229237

230238
private:
231239
command& cmd_;

test/testdb.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,40 @@ void test_insert_bind3() {
112112
expect_eq("555-1234", phone);
113113
}
114114

115+
void test_insert_bind_null() {
116+
auto db = contacts_db();
117+
sqlite3pp::command cmd(db, "INSERT INTO contacts (name, phone, address) VALUES (:user, :phone, :address)");
118+
cmd.bind(":user", "Mike", sqlite3pp::nocopy);
119+
cmd.bind(":phone", "555-1234", sqlite3pp::nocopy);
120+
cmd.bind(":address");
121+
expect_eq(0, cmd.execute());
122+
123+
sqlite3pp::query qry(db, "SELECT name, phone, address FROM contacts");
124+
auto iter = qry.begin();
125+
string name, phone;
126+
const char* address = nullptr;
127+
(*iter).getter() >> name >> phone >> address;
128+
expect_eq("Mike", name);
129+
expect_eq("555-1234", phone);
130+
expect_eq(nullptr, address);
131+
}
132+
133+
void test_insert_binder_null() {
134+
auto db = contacts_db();
135+
sqlite3pp::command cmd(db, "INSERT INTO contacts (name, phone, address) VALUES (?, ?, ?)");
136+
cmd.binder() << "Mike" << "555-1234" << nullptr;
137+
expect_eq(0, cmd.execute());
138+
139+
sqlite3pp::query qry(db, "SELECT name, phone, address FROM contacts");
140+
auto iter = qry.begin();
141+
string name, phone;
142+
const char* address = nullptr;
143+
(*iter).getter() >> name >> phone >> address;
144+
expect_eq("Mike", name);
145+
expect_eq("555-1234", phone);
146+
expect_eq(nullptr, address);
147+
}
148+
115149
void test_query_columns() {
116150
auto db = contacts_db();
117151
expect_eq(0, db.execute("INSERT INTO contacts (name, phone) VALUES ('Mike', '555-1234')"));
@@ -260,6 +294,8 @@ int main()
260294
test_insert_bind1();
261295
test_insert_bind2();
262296
test_insert_bind3();
297+
test_insert_bind_null();
298+
test_insert_binder_null();
263299
test_query_columns();
264300
test_query_get();
265301
test_query_tie();
@@ -279,6 +315,7 @@ sqlite3pp::database contacts_db() {
279315
id INTEGER PRIMARY KEY,
280316
name TEXT NOT NULL,
281317
phone TEXT NOT NULL,
318+
address TEXT,
282319
UNIQUE(name, phone)
283320
);
284321
)""");

0 commit comments

Comments
 (0)