Module Name:    othersrc
Committed By:   agc
Date:           Fri Nov 27 06:05:26 UTC 2015

Modified Files:
        othersrc/external/bsd/sqlite3db/dist: sqlite3db.c

Log Message:
When calling "put" method with R_NOOVERWRITE and we already have that key,
don't add it to the database, just return 1.


To generate a diff of this commit:
cvs rdiff -u -r1.2 -r1.3 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/dist/sqlite3db.c
diff -u othersrc/external/bsd/sqlite3db/dist/sqlite3db.c:1.2 othersrc/external/bsd/sqlite3db/dist/sqlite3db.c:1.3
--- othersrc/external/bsd/sqlite3db/dist/sqlite3db.c:1.2	Fri Nov 27 03:58:20 2015
+++ othersrc/external/bsd/sqlite3db/dist/sqlite3db.c	Fri Nov 27 06:05:26 2015
@@ -89,11 +89,13 @@ sqlite3db_get(const DB *db, const DBT *k
 static int
 sqlite3db_put(const DB *db, DBT *key,  const DBT *value, unsigned flags)
 {
+	sqlite3_stmt	*getstmt;
+	sqlite3_stmt	*upstmt;
+	sqlite3_stmt	*instmt;
 	sqldbshim_t	*shim;
+	const char	*get = "SELECT value FROM db WHERE key=?";
 	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) {
@@ -104,6 +106,20 @@ sqlite3db_put(const DB *db, DBT *key,  c
 		sqlite3_finalize(shim->getstmt);
 		shim->getstmt = NULL;
 	}
+	if (flags & R_NOOVERWRITE) {
+		rc = sqlite3_prepare_v2(shim->db, get, -1, &getstmt, 0);
+		if (rc != SQLITE_OK) {
+			warn("put: sqlite_prepare_v2: expected 0 got %d", rc);
+			return RC_ERROR;
+		}
+		sqlite3_bind_text(getstmt, 1, key->data, key->size, SQLITE_TRANSIENT);
+		rc = sqlite3_step(getstmt);
+		sqlite3_finalize(getstmt);
+		if (rc == SQLITE_ROW) {
+			/* got the key already, return 1 */
+			return RC_NOT_FOUND;
+		}
+	}
 	rc = sqlite3_prepare_v2(shim->db, update, -1, &upstmt, 0);
 	if (rc != SQLITE_OK) {
 		warn("put: sqlite_prepare_v2: expected 0 got %d", rc);
@@ -112,6 +128,7 @@ sqlite3db_put(const DB *db, DBT *key,  c
 	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);
+	rc = sqlite3_finalize(upstmt);
 	if (sqlite3_changes(shim->db) == 0) {
 		rc = sqlite3_prepare_v2(shim->db, insert, -1, &instmt, 0);
 		if (rc != SQLITE_OK) {
@@ -125,7 +142,6 @@ sqlite3db_put(const DB *db, DBT *key,  c
 		}
 		rc = sqlite3_finalize(instmt);
 	}
-	rc = sqlite3_finalize(upstmt);
 	return (rc == SQLITE_OK) ? RC_SUCCESS : RC_ERROR;
 }
 

Reply via email to