Module Name:    othersrc
Committed By:   agc
Date:           Fri Nov 27 03:58:20 UTC 2015

Modified Files:
        othersrc/external/bsd/sqlite3db/bin: Makefile
        othersrc/external/bsd/sqlite3db/dist: sqlite3db.c
Added Files:
        othersrc/external/bsd/sqlite3db/bin: 6.expected 6.in

Log Message:
In the "put" function, don't just do a straight INSERT, as the key may
already exist.  Instead, do an sqlite3 UPSERT (ha!) - well, the
nearest thing it has to one anyway. Add a test for this


To generate a diff of this commit:
cvs rdiff -u -r0 -r1.1 othersrc/external/bsd/sqlite3db/bin/6.expected \
    othersrc/external/bsd/sqlite3db/bin/6.in
cvs rdiff -u -r1.1.1.1 -r1.2 othersrc/external/bsd/sqlite3db/bin/Makefile
cvs rdiff -u -r1.1.1.1 -r1.2 othersrc/external/bsd/sqlite3db/dist/sqlite3db.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: othersrc/external/bsd/sqlite3db/bin/Makefile
diff -u othersrc/external/bsd/sqlite3db/bin/Makefile:1.1.1.1 othersrc/external/bsd/sqlite3db/bin/Makefile:1.2
--- othersrc/external/bsd/sqlite3db/bin/Makefile:1.1.1.1	Thu Nov 26 19:22:46 2015
+++ othersrc/external/bsd/sqlite3db/bin/Makefile	Fri Nov 27 03:58:20 2015
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile,v 1.1.1.1 2015/11/26 19:22:46 agc Exp $
+# $NetBSD: Makefile,v 1.2 2015/11/27 03:58:20 agc Exp $
 
 .include <bsd.own.mk>
 
@@ -54,4 +54,10 @@ t: ${PROG}
 	env LD_LIBRARY_PATH=${LIB_SQLITE3DB_DIR} ./${PROG} del sql1.db equals > 5.out
 	diff 5.expected 5.out
 	rm -f 5.out
+	@echo "6. upsert"
+	rm -f sql6.db
+	env LD_LIBRARY_PATH=${LIB_SQLITE3DB_DIR} ./${PROG} create sql6.db < 6.in
+	env LD_LIBRARY_PATH=${LIB_SQLITE3DB_DIR} ./${PROG} dump sql6.db > 6.out
+	diff 6.expected 6.out
+	rm -f 6.out sql6.db
 	rm -f sql1.db

Index: othersrc/external/bsd/sqlite3db/dist/sqlite3db.c
diff -u othersrc/external/bsd/sqlite3db/dist/sqlite3db.c:1.1.1.1 othersrc/external/bsd/sqlite3db/dist/sqlite3db.c:1.2
--- othersrc/external/bsd/sqlite3db/dist/sqlite3db.c:1.1.1.1	Thu Nov 26 19:22:46 2015
+++ othersrc/external/bsd/sqlite3db/dist/sqlite3db.c	Fri Nov 27 03:58:20 2015
@@ -90,8 +90,10 @@ static int
 sqlite3db_put(const DB *db, DBT *key,  const DBT *value, unsigned flags)
 {
 	sqldbshim_t	*shim;
-	const char	*put_sql_cmd = "INSERT INTO db(key, value) VALUES(?, ?)";
-	sqlite3_stmt	*stmt;
+	const char	*update = "UPDATE db SET value=? WHERE key=?";
+	const char	*insert = "INSERT INTO db(key, value) VALUES(?, ?)";
+	sqlite3_stmt	*upstmt;
+	sqlite3_stmt	*instmt;
 	int		 rc;
 
 	if (db == NULL || key == NULL || value == NULL) {
@@ -102,17 +104,28 @@ sqlite3db_put(const DB *db, DBT *key,  c
 		sqlite3_finalize(shim->getstmt);
 		shim->getstmt = NULL;
 	}
-	rc = sqlite3_prepare_v2(shim->db, put_sql_cmd, -1, &stmt, 0);
+	rc = sqlite3_prepare_v2(shim->db, update, -1, &upstmt, 0);
 	if (rc != SQLITE_OK) {
 		warn("put: sqlite_prepare_v2: expected 0 got %d", rc);
 		return RC_ERROR;
 	}
-	sqlite3_bind_text(stmt, 1, key->data, key->size, SQLITE_STATIC);
-	sqlite3_bind_text(stmt, 2, value->data, value->size, SQLITE_STATIC);
-	if ((rc = sqlite3_step(stmt)) != SQLITE_DONE) {
-		return RC_NOT_FOUND;
+	sqlite3_bind_text(upstmt, 1, value->data, value->size, SQLITE_STATIC);
+	sqlite3_bind_text(upstmt, 2, key->data, key->size, SQLITE_STATIC);
+	rc = sqlite3_step(upstmt);
+	if (sqlite3_changes(shim->db) == 0) {
+		rc = sqlite3_prepare_v2(shim->db, insert, -1, &instmt, 0);
+		if (rc != SQLITE_OK) {
+			warn("put: sqlite_prepare_v2: expected 0 got %d", rc);
+			return RC_ERROR;
+		}
+		sqlite3_bind_text(instmt, 1, key->data, key->size, SQLITE_STATIC);
+		sqlite3_bind_text(instmt, 2, value->data, value->size, SQLITE_STATIC);
+		if ((rc = sqlite3_step(instmt)) != SQLITE_DONE) {
+			return RC_NOT_FOUND;
+		}
+		rc = sqlite3_finalize(instmt);
 	}
-	rc = sqlite3_finalize(stmt);
+	rc = sqlite3_finalize(upstmt);
 	return (rc == SQLITE_OK) ? RC_SUCCESS : RC_ERROR;
 }
 

Added files:

Index: othersrc/external/bsd/sqlite3db/bin/6.expected
diff -u /dev/null othersrc/external/bsd/sqlite3db/bin/6.expected:1.1
--- /dev/null	Fri Nov 27 03:58:21 2015
+++ othersrc/external/bsd/sqlite3db/bin/6.expected	Fri Nov 27 03:58:20 2015
@@ -0,0 +1,4 @@
+hey	ho
+key	value
+black	kinda red
+equals	equals
Index: othersrc/external/bsd/sqlite3db/bin/6.in
diff -u /dev/null othersrc/external/bsd/sqlite3db/bin/6.in:1.1
--- /dev/null	Fri Nov 27 03:58:21 2015
+++ othersrc/external/bsd/sqlite3db/bin/6.in	Fri Nov 27 03:58:20 2015
@@ -0,0 +1,6 @@
+hey=ho
+key=value
+black=white
+equals=equals
+black=black
+black=kinda red

Reply via email to