[darwinbuild-changes] [700] branches/PR-7489777/darwinup

source_changes at macosforge.org source_changes at macosforge.org
Tue Feb 9 15:43:14 PST 2010


Revision: 700
          http://trac.macosforge.org/projects/darwinbuild/changeset/700
Author:   wsiegrist at apple.com
Date:     2010-02-09 15:43:11 -0800 (Tue, 09 Feb 2010)
Log Message:
-----------
Remove sql() from Depot. Add sql_once() to Database to execute queries without caching or returning data. Database::sql() now caches statements in the statement cache. Implement delete_empty_archive().

Modified Paths:
--------------
    branches/PR-7489777/darwinup/DB.cpp
    branches/PR-7489777/darwinup/DB.h
    branches/PR-7489777/darwinup/Database.cpp
    branches/PR-7489777/darwinup/Database.h
    branches/PR-7489777/darwinup/Depot.cpp
    branches/PR-7489777/darwinup/Depot.h

Modified: branches/PR-7489777/darwinup/DB.cpp
===================================================================
--- branches/PR-7489777/darwinup/DB.cpp	2010-02-09 22:25:57 UTC (rev 699)
+++ branches/PR-7489777/darwinup/DB.cpp	2010-02-09 23:43:11 UTC (rev 700)
@@ -208,6 +208,15 @@
 	return this->del(this->m_archives_table, serial) ;
 }
 
+int DarwinupDatabase::delete_empty_archives() {
+	return this->sql("delete_empty_archives", 
+					 "DELETE FROM archives "
+					 "WHERE serial IN "
+					 " (SELECT serial FROM archives "
+					 "  WHERE serial NOT IN "
+					 "   (SELECT DISTINCT archive FROM files));");	
+}
+
 int DarwinupDatabase::delete_file(File* file) {
 	return this->del(this->m_files_table, file->serial());
 }

Modified: branches/PR-7489777/darwinup/DB.h
===================================================================
--- branches/PR-7489777/darwinup/DB.h	2010-02-09 22:25:57 UTC (rev 699)
+++ branches/PR-7489777/darwinup/DB.h	2010-02-09 23:43:11 UTC (rev 700)
@@ -66,6 +66,7 @@
 	int      update_archive(uint64_t serial, uuid_t uuid, const char* name,
 							time_t date_added, uint32_t active, uint32_t info);
 	uint64_t insert_archive(uuid_t uuid, uint32_t info, const char* name, time_t date);
+	int      delete_empty_archives();
 	int      delete_archive(Archive* archive);
 	int      delete_archive(uint64_t serial);
 

Modified: branches/PR-7489777/darwinup/Database.cpp
===================================================================
--- branches/PR-7489777/darwinup/Database.cpp	2010-02-09 22:25:57 UTC (rev 699)
+++ branches/PR-7489777/darwinup/Database.cpp	2010-02-09 23:43:11 UTC (rev 700)
@@ -188,7 +188,7 @@
 	int res = SQLITE_OK;
 	for (uint32_t i=0; i<m_table_count; i++) {
 		IF_DEBUG("[DATABASE] creating table #%u \n", i);
-		res = this->sql(m_tables[i]->create());
+		res = this->sql_once(m_tables[i]->create());
 		if (res!=SQLITE_OK) {
 			fprintf(stderr, "Error: sql error trying to create table: %s: %s\n", 
 					m_tables[i]->name(), m_error);
@@ -215,25 +215,21 @@
 	return res != SQLITE_OK;
 }
 
-
-#define __SQL(callback, context, fmt) \
-    va_list args; \
-    va_start(args, fmt); \
-    char* error; \
-    if (this->m_db) { \
-        char *query = sqlite3_vmprintf(fmt, args); \
-        IF_DEBUG("[DATABASE] SQL: %s \n", query); \
-        res = sqlite3_exec(this->m_db, query, callback, context, &error); \
-        sqlite3_free(query); \
-    } else { \
-        fprintf(stderr, "Error: database not open.\n"); \
-        res = SQLITE_ERROR; \
-    } \
+int Database::sql_once(const char* fmt, ...) {
+	int res = 0;
+    va_list args;
+    va_start(args, fmt);
+    char* error;
+    if (this->m_db) {
+        char *query = sqlite3_vmprintf(fmt, args);
+        IF_DEBUG("[DATABASE] SQL(): %s \n", query);
+        res = sqlite3_exec(this->m_db, query, NULL, NULL, &error);
+        sqlite3_free(query);
+    } else {
+        fprintf(stderr, "Error: database not open.\n");
+        res = SQLITE_ERROR;
+    }
     va_end(args);
-
-int Database::sql(const char* fmt, ...) {
-	int res = 0;
-	__SQL(NULL, NULL, fmt);
 	if (error) {
 		strlcpy(m_error, error, m_error_size);
 		fprintf(stderr, "Error: sql(): %s \n", m_error);
@@ -255,7 +251,6 @@
 	return res;
 }
 
-#undef __SQL
 
 #define __bind_all_columns(_lastarg) \
     va_list args; \
@@ -358,6 +353,28 @@
     sqlite3_reset(_stmt); \
     cache_release_value(m_statement_cache, &_stmt);
 
+int Database::sql(const char* name, const char* fmt, ...) {
+	sqlite3_stmt* stmt;
+	char* key = strdup(name);
+	cache_get_and_retain(m_statement_cache, key, (void**)&stmt);
+	if (!stmt) {
+		va_list args;
+		va_start(args, fmt);
+		char* query = sqlite3_vmprintf(fmt, args);
+		int res = sqlite3_prepare_v2(m_db, query, strlen(query), &stmt, NULL);
+		va_end(args);
+		if (res != SQLITE_OK) {
+			fprintf(stderr, "Error: unable to prepare statement for query: %s\nError: %s\n",
+					query, sqlite3_errmsg(m_db));
+			free(key);
+			return res;
+		}
+		cache_set_and_retain(m_statement_cache, key, stmt, sizeof(stmt)); \
+		free(key);
+	}
+	return this->execute(stmt);
+}
+
 int Database::count(const char* name, void** output, Table* table, uint32_t count, ...) {
 	__get_stmt(table->count(m_db, count, args));
 	int res = SQLITE_OK;
@@ -498,14 +515,14 @@
 
 
 int Database::begin_transaction() {
-	return this->sql("BEGIN TRANSACTION");
+	return this->sql_once("BEGIN TRANSACTION");
 }
 
 int Database::rollback_transaction() {
-	return this->sql("ROLLBACK TRANSACTION");
+	return this->sql_once("ROLLBACK TRANSACTION");
 }
 
 int Database::commit_transaction() {
-	return this->sql("COMMIT TRANSACTION");
+	return this->sql_once("COMMIT TRANSACTION");
 }
 

Modified: branches/PR-7489777/darwinup/Database.h
===================================================================
--- branches/PR-7489777/darwinup/Database.h	2010-02-09 22:25:57 UTC (rev 699)
+++ branches/PR-7489777/darwinup/Database.h	2010-02-09 23:43:11 UTC (rev 700)
@@ -104,7 +104,8 @@
 	int  add_table(Table*);
 	uint64_t last_insert_id();
 	
-	int sql(const char* fmt, ...);
+	int sql_once(const char* fmt, ...);
+	int sql(const char* name, const char* fmt, ...);
 	
 protected:
 	

Modified: branches/PR-7489777/darwinup/Depot.cpp
===================================================================
--- branches/PR-7489777/darwinup/Depot.cpp	2010-02-09 22:25:57 UTC (rev 699)
+++ branches/PR-7489777/darwinup/Depot.cpp	2010-02-09 23:43:11 UTC (rev 700)
@@ -807,21 +807,7 @@
 
 int Depot::prune_archives() {
 	int res = 0;
-	static sqlite3_stmt* stmt = NULL;
-	if (stmt == NULL && m_db) {
-		const char* query = "DELETE FROM archives WHERE serial IN (SELECT serial FROM archives WHERE serial NOT IN (SELECT DISTINCT archive FROM files));";
-		res = sqlite3_prepare(m_db, query, -1, &stmt, NULL);
-		if (res != 0) fprintf(stderr, "%s:%d: sqlite3_prepare: %s: %s (%d)\n", __FILE__, __LINE__, query, sqlite3_errmsg(m_db), res);
-	}
-	if (stmt && res == 0) {
-		if (res == 0) res = sqlite3_step(stmt);
-		if (res == SQLITE_DONE) {
-			res = 0;
-		} else {
-			fprintf(stderr, "%s:%d: Could not prune archives in database: %s (%d)\n", __FILE__, __LINE__, sqlite3_errmsg(m_db), res);
-		}
-		sqlite3_reset(stmt);
-	}
+	res = this->m_db2->delete_empty_archives();
 	return res;
 }
 
@@ -1353,31 +1339,3 @@
 	free(list);
 	return res;
 }
-
-
-#define __SQL(callback, context, fmt) \
-	va_list args; \
-	char* errmsg; \
-	va_start(args, fmt); \
-	if (this->m_db) { \
-		char *query = sqlite3_vmprintf(fmt, args); \
-		res = sqlite3_exec(this->m_db, query, callback, context, &errmsg); \
-		if (res != SQLITE_OK) { \
-			fprintf(stderr, "Error: %s (%d)\n  SQL: %s\n", errmsg, res, query); \
-		} \
-		sqlite3_free(query); \
-	} else { \
-		fprintf(stderr, "Error: database not open.\n"); \
-		res = SQLITE_ERROR; \
-	} \
-	va_end(args);
-
-int Depot::SQL(const char* fmt, ...) {
-	int res;
-	fprintf(stderr, "Depot::SQL() called!\n");
-	assert(false);
-	__SQL(NULL, NULL, fmt);
-	return res;
-}
-
-#undef __SQL

Modified: branches/PR-7489777/darwinup/Depot.h
===================================================================
--- branches/PR-7489777/darwinup/Depot.h	2010-02-09 22:25:57 UTC (rev 699)
+++ branches/PR-7489777/darwinup/Depot.h	2010-02-09 23:43:11 UTC (rev 700)
@@ -152,9 +152,6 @@
 
 	int		check_consistency();
 
-
-	virtual int	SQL(const char* fmt, ...);
-
 	sqlite3*	      m_db;
 	DarwinupDatabase* m_db2;
 	
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/darwinbuild-changes/attachments/20100209/6daf3f6d/attachment.html>


More information about the darwinbuild-changes mailing list