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

source_changes at macosforge.org source_changes at macosforge.org
Tue Feb 9 14:25:57 PST 2010


Revision: 699
          http://trac.macosforge.org/projects/darwinbuild/changeset/699
Author:   wsiegrist at apple.com
Date:     2010-02-09 14:25:57 -0800 (Tue, 09 Feb 2010)
Log Message:
-----------
Further porting to new DB. Cleanup debug printf. Standardize on int returns instead of mixed bool/int.

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/Table.cpp
    branches/PR-7489777/darwinup/Table.h

Modified: branches/PR-7489777/darwinup/DB.cpp
===================================================================
--- branches/PR-7489777/darwinup/DB.cpp	2010-02-08 23:24:42 UTC (rev 698)
+++ branches/PR-7489777/darwinup/DB.cpp	2010-02-09 22:25:57 UTC (rev 699)
@@ -50,40 +50,70 @@
 	
 	this->m_archives_table = new Table("archives");
 	//                                                                           index  pk     unique
-	assert(m_archives_table->add_column(new Column("serial",     TYPE_INTEGER,   false, true,  false)));
-	assert(m_archives_table->add_column(new Column("uuid",       TYPE_BLOB,      true,  false, true)));
-	assert(m_archives_table->add_column(new Column("name",       TYPE_TEXT)));
-	assert(m_archives_table->add_column(new Column("date_added", TYPE_INTEGER)));
-	assert(m_archives_table->add_column(new Column("active",     TYPE_INTEGER)));
-	assert(m_archives_table->add_column(new Column("info",       TYPE_INTEGER)));
-	assert(this->add_table(this->m_archives_table));
+	assert(m_archives_table->add_column(new Column("serial",     TYPE_INTEGER,   false, true,  false))==0);
+	assert(m_archives_table->add_column(new Column("uuid",       TYPE_BLOB,      true,  false, true))==0);
+	assert(m_archives_table->add_column(new Column("name",       TYPE_TEXT))==0);
+	assert(m_archives_table->add_column(new Column("date_added", TYPE_INTEGER))==0);
+	assert(m_archives_table->add_column(new Column("active",     TYPE_INTEGER))==0);
+	assert(m_archives_table->add_column(new Column("info",       TYPE_INTEGER))==0);
+	assert(this->add_table(this->m_archives_table)==0);
 	
 	this->m_files_table = new Table("files");
 	//                                                                           index  pk     unique
-	assert(m_files_table->add_column(new Column("serial",  TYPE_INTEGER,         false, true,  false)));
-	assert(m_files_table->add_column(new Column("archive", TYPE_INTEGER)));
-	assert(m_files_table->add_column(new Column("info",    TYPE_INTEGER)));
-	assert(m_files_table->add_column(new Column("mode",    TYPE_INTEGER)));
-	assert(m_files_table->add_column(new Column("uid",     TYPE_INTEGER)));
-	assert(m_files_table->add_column(new Column("gid",     TYPE_INTEGER)));
-	assert(m_files_table->add_column(new Column("size",    TYPE_INTEGER)));
-	assert(m_files_table->add_column(new Column("digest",  TYPE_BLOB)));
-	assert(m_files_table->add_column(new Column("path",    TYPE_TEXT,            true,  false, false)));
-	assert(this->add_table(this->m_files_table));	
+	assert(m_files_table->add_column(new Column("serial",  TYPE_INTEGER,         false, true,  false))==0);
+	assert(m_files_table->add_column(new Column("archive", TYPE_INTEGER))==0);
+	assert(m_files_table->add_column(new Column("info",    TYPE_INTEGER))==0);
+	assert(m_files_table->add_column(new Column("mode",    TYPE_INTEGER))==0);
+	assert(m_files_table->add_column(new Column("uid",     TYPE_INTEGER))==0);
+	assert(m_files_table->add_column(new Column("gid",     TYPE_INTEGER))==0);
+	assert(m_files_table->add_column(new Column("size",    TYPE_INTEGER))==0);
+	assert(m_files_table->add_column(new Column("digest",  TYPE_BLOB))==0);
+	assert(m_files_table->add_column(new Column("path",    TYPE_TEXT,            true,  false, false))==0);
+	assert(this->add_table(this->m_files_table)==0);	
 }
 
+int DarwinupDatabase::activate_archive(uint64_t serial) {
+	uint64_t active = 1;
+	return this->set_archive_active(serial, &active);
+}
 
+int DarwinupDatabase::deactivate_archive(uint64_t serial) {
+	uint64_t active = 0;
+	return this->set_archive_active(serial, &active);
+}
 
-uint64_t DarwinupDatabase::insert_archive(uuid_t uuid, uint32_t info, const char* name, time_t date_added) {
+int DarwinupDatabase::set_archive_active(uint64_t serial, uint64_t* active) {
+	return this->update_value("activate_archive", 
+							  this->m_archives_table,
+							  this->m_archives_table->column(4), // active
+							  (void**)active,
+							  1,
+							  this->m_archives_table->column(0), // serial
+							  serial); // convert from bool to int	
+}
+
+int DarwinupDatabase::update_archive(uint64_t serial, uuid_t uuid, const char* name,
+									  time_t date_added, uint32_t active, uint32_t info) {
+	return this->update(this->m_archives_table, serial,
+						(uint8_t*)uuid,
+						(uint32_t)sizeof(uuid_t),
+						name,
+						(uint64_t)date_added,
+						(uint64_t)active,
+						(uint64_t)info);
+}
+
+uint64_t DarwinupDatabase::insert_archive(uuid_t uuid, uint32_t info, const char* name, 
+										  time_t date_added) {
 	
-	bool res = this->insert(this->m_archives_table,
-							(uint8_t*)uuid,
-							(uint32_t)sizeof(uuid_t),
-							name,
-							(uint64_t)date_added,
-							(uint64_t)0,
-							(uint64_t)info);
-	if (!res) {
+	int res = this->insert(this->m_archives_table,
+						   (uint8_t*)uuid,
+						   (uint32_t)sizeof(uuid_t),
+						   name,
+						   (uint64_t)date_added,
+						   (uint64_t)0,
+						   (uint64_t)info);
+	if (res != SQLITE_OK) {
 		fprintf(stderr, "Error: unable to insert archive %s: %s \n",
 				name, this->error());
 		return 0;
@@ -91,23 +121,25 @@
 	
 	return this->last_insert_id();
 }
-										  
-bool DarwinupDatabase::update_file(Archive* archive, const char* path, uint32_t info, mode_t mode, 
-								   uid_t uid, gid_t gid, Digest* digest) {
 
-	bool res = false;
-	// get the serial for the file where archive and path match
-	uint64_t serial;
-	
-	res = this->get_value("file_serial__archive_path",
-						  (void**)&serial,
-						  this->m_files_table,
-						  this->m_files_table->column(0), // serial
-						  2,                              // number of where conditions
-						  this->m_files_table->column(1), // archive
-						  (uint64_t)archive->serial(),
-						  this->m_files_table->column(8), // path
-						  path);
+uint64_t DarwinupDatabase::get_file_serial_from_archive(Archive* archive, const char* path) {
+	uint64_t serial = 0;
+	this->get_value("file_serial__archive_path",
+					(void**)&serial,
+					this->m_files_table,
+					this->m_files_table->column(0), // serial
+					2,                              // number of where conditions
+					this->m_files_table->column(1), // archive
+					(uint64_t)archive->serial(),
+					this->m_files_table->column(8), // path
+					path);
+	return serial;
+}
+
+int DarwinupDatabase::update_file(uint64_t serial, Archive* archive, uint32_t info, mode_t mode, 
+								   uid_t uid, gid_t gid, Digest* digest, const char* path) {
+
+	int res = SQLITE_OK;
 								  
 	// update the information
 	res = this->update(this->m_files_table, serial,
@@ -121,7 +153,7 @@
 					   (uint32_t)(digest ? digest->size() : 0), 
 					   path);
 
-	if (!res) {
+	if (res != SQLITE_OK) {
 		fprintf(stderr, "Error: unable to update file with serial %llu and path %s: %s \n",
 				serial, path, this->error());
 	}
@@ -132,7 +164,7 @@
 uint64_t DarwinupDatabase::insert_file(uint32_t info, mode_t mode, uid_t uid, gid_t gid, 
 									   Digest* digest, Archive* archive, const char* path) {
 	
-	bool res = this->insert(this->m_files_table,
+	int res = this->insert(this->m_files_table,
 							(uint64_t)archive->serial(),
 							(uint64_t)info,
 							(uint64_t)mode,
@@ -142,7 +174,7 @@
 							(uint8_t*)(digest ? digest->data() : NULL), 
 							(uint32_t)(digest ? digest->size() : 0), 
 							path);
-	if (!res) {
+	if (res != SQLITE_OK) {
 		fprintf(stderr, "Error: unable to insert file at %s: %s \n",
 				path, this->error());
 		return 0;
@@ -153,8 +185,9 @@
 
 uint64_t DarwinupDatabase::count_files(Archive* archive, const char* path) {
 
-	bool res = false;
+	int res = SQLITE_OK;
 	uint64_t* c = (uint64_t*)malloc(sizeof(uint64_t));
+	if (!c) fprintf(stderr, "Error: ran out of memory in DarwinupDatabase::count_files().\n");
 	res = this->count("count_files",
 					  (void**)c,
 					  this->m_files_table,
@@ -167,23 +200,23 @@
 }
 
 
-bool DarwinupDatabase::delete_archive(Archive* archive) {
+int DarwinupDatabase::delete_archive(Archive* archive) {
 	return this->del(this->m_archives_table, archive->serial());
 }
 
-bool DarwinupDatabase::delete_archive(uint64_t serial) {
-	return this->del(this->m_archives_table, serial);
+int DarwinupDatabase::delete_archive(uint64_t serial) {
+	return this->del(this->m_archives_table, serial) ;
 }
 
-bool DarwinupDatabase::delete_file(File* file) {
+int DarwinupDatabase::delete_file(File* file) {
 	return this->del(this->m_files_table, file->serial());
 }
 
-bool DarwinupDatabase::delete_file(uint64_t serial) {
+int DarwinupDatabase::delete_file(uint64_t serial) {
 	return this->del(this->m_files_table, serial);
 }
 
-bool DarwinupDatabase::delete_files(Archive* archive) {
+int DarwinupDatabase::delete_files(Archive* archive) {
 	return this->del("delete_files__archive",
 					 this->m_files_table,
 					 1,

Modified: branches/PR-7489777/darwinup/DB.h
===================================================================
--- branches/PR-7489777/darwinup/DB.h	2010-02-08 23:24:42 UTC (rev 698)
+++ branches/PR-7489777/darwinup/DB.h	2010-02-09 22:25:57 UTC (rev 699)
@@ -61,22 +61,29 @@
 	uint64_t count_files(Archive* archive, const char* path);
 	
 	// Archives table modifications
+	int      activate_archive(uint64_t serial);
+	int      deactivate_archive(uint64_t serial);
+	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);
-	bool     delete_archive(Archive* archive);
-	bool     delete_archive(uint64_t serial);
+	int      delete_archive(Archive* archive);
+	int      delete_archive(uint64_t serial);
 
 	// Files table modifications
-	bool     update_file(Archive* archive, const char* path, uint32_t info, mode_t mode, 
-						 uid_t uid, gid_t gid, Digest* digest);
+	uint64_t get_file_serial_from_archive(Archive* archive, const char* path);
+	int      update_file(uint64_t serial, Archive* archive, uint32_t info, mode_t mode, 
+						 uid_t uid, gid_t gid, Digest* digest, const char* path);
 	uint64_t insert_file(uint32_t info, mode_t mode, uid_t uid, gid_t gid, 
 						 Digest* digest, Archive* archive, const char* path);
-	bool     delete_file(uint64_t serial);
-	bool     delete_file(File* file);
-	bool     delete_files(Archive* archive);
+	int      delete_file(uint64_t serial);
+	int      delete_file(File* file);
+	int      delete_files(Archive* archive);
 	
 
 protected:
 	
+	int      set_archive_active(uint64_t serial, uint64_t* active);
+	
 	Table*        m_archives_table;
 	Table*        m_files_table;
 	

Modified: branches/PR-7489777/darwinup/Database.cpp
===================================================================
--- branches/PR-7489777/darwinup/Database.cpp	2010-02-08 23:24:42 UTC (rev 698)
+++ branches/PR-7489777/darwinup/Database.cpp	2010-02-09 22:25:57 UTC (rev 699)
@@ -39,13 +39,12 @@
 	IF_DEBUG("[TRACE] %s \n", sql);
 }
 
-// XXX
 void __blob_hex(uint8_t* data, uint32_t size) {
 	if (!size) return;
 	for (uint32_t i=0; i < size; i++) {
-		fprintf(stderr, "%02x", data[i]);
+		IF_DEBUG("%02x", data[i]);
 	}
-	fprintf(stderr, "\n");
+	IF_DEBUG("\n");
 }
 
 
@@ -92,7 +91,7 @@
 
 
 void Database::init_cache() {
-	fprintf(stderr, "CACHE: init_cache \n");
+	IF_DEBUG("CACHE: init_cache \n");
 	cache_attributes_t attrs;
 	attrs.version = CACHE_ATTRIBUTES_VERSION_2;
 	attrs.key_hash_cb = cache_key_hash_cb_cstring;
@@ -105,34 +104,33 @@
 }
 
 void Database::destroy_cache() {
-	fprintf(stderr, "CACHE: destroy_cache \n");
+	IF_DEBUG("CACHE: destroy_cache \n");
 	cache_destroy(m_statement_cache);
 }
 
 bool cache_key_is_equal(void* key1, void* key2, void* user) {
-	fprintf(stderr, "CACHE: key1: %s key2: %s \n", (char*)key1, (char*)key2);
 	bool res = (strcmp((char*)key1, (char*)key2) == 0);
-	fprintf(stderr, "CACHE: key_is_equal returning %d \n", res);
+	IF_DEBUG("CACHE: key1: %s key2: %s res: %d\n", (char*)key1, (char*)key2, res);
 	return res;
 }
 
 void cache_key_retain(void* key_in, void** key_out, void* user_data) {
-	fprintf(stderr, "CACHE: key_retain %s\n", (char*)key_in);
+	IF_DEBUG("CACHE: key_retain %s\n", (char*)key_in);
 	*key_out = strdup((char*)key_in);
 }
 
 void cache_key_release(void* key, void* user_data) {
-	fprintf(stderr, "CACHE: key_release %s\n", (char*)key);
+	IF_DEBUG("CACHE: key_release %s\n", (char*)key);
 	free(key);
 }
 
 void cache_value_retain(void* value, void* user_data) {
-	fprintf(stderr, "CACHE: value_retain %p\n", value);
+	IF_DEBUG("CACHE: value_retain %p\n", value);
 	// do nothing
 }
 
 void cache_value_release(void* value, void* user_data) {
-	fprintf(stderr, "CACHE: value_release %p\n", value);
+	IF_DEBUG("CACHE: value_release %p\n", value);
 	sqlite3_finalize((sqlite3_stmt*)value);
 }
 
@@ -145,7 +143,7 @@
 	return m_error;
 }
 
-bool Database::connect() {
+int Database::connect() {
 	int res = 0;
 	this->init_schema();
 	res = sqlite3_open(m_path, &m_db);
@@ -153,65 +151,71 @@
 		sqlite3_close(m_db);
 		m_db = NULL;
 		fprintf(stderr, "Error: unable to connect to database at: %s \n", m_path);
-		return false;
+		return res;
 	}	
 	sqlite3_trace(m_db, dbtrace, NULL);
-	if (this->empty()) {
-		assert(this->create_tables());
+	if (this->is_empty()) {
+		assert(this->create_tables() == 0);
 	}	
-	return true;	
+	return res;	
 }
 
-bool Database::connect(const char* path) {
+int Database::connect(const char* path) {
 	this->m_path = strdup(path);
-	if (!m_path) fprintf(stderr, "Error: ran out of memory when trying to connect to database.\n");
-	return m_path && this->connect();
+	if (!m_path) {
+		fprintf(stderr, "Error: ran out of memory when trying to connect to database.\n");
+		return 1;
+	}
+	return this->connect();
 }
 
 
-bool Database::add_table(Table* t) {
+int Database::add_table(Table* t) {
 	if (m_table_count >= m_table_max) {
 		m_tables = (Table**)realloc(m_tables, m_table_max * sizeof(Table*) * 4);
 		if (!m_tables) {
 			fprintf(stderr, "Error: unable to reallocate memory to add a table\n");
-			return false;
+			return 1;
 		}
 		m_table_max *= 4;
 	}
 	m_tables[m_table_count++] = t;
 	
-	return true;
+	return 0;
 }
 
+int Database::create_tables() {
+	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());
+		if (res!=SQLITE_OK) {
+			fprintf(stderr, "Error: sql error trying to create table: %s: %s\n", 
+					m_tables[i]->name(), m_error);
+			return res;
+		}
+	}
+	return res;
+}
 
 /**
  * attempt to get a row count of the first table to detect if the schema
  * needs to be initialized
  */
-bool Database::empty() {
+bool Database::is_empty() {
 	if (!m_tables[0]) {
 		fprintf(stderr, "Error: Database has not had a schema initialized.\n");
 		return false;
 	}
-	int res = this->sql("SELECT count(*) FROM %s;", m_tables[0]->name());
-	fprintf(stderr, "Debug: empty() res = %d \n", res);
-	return res != 0;
-}
-
-bool Database::create_tables() {
 	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());
-		if (res!=SQLITE_OK) {
-			fprintf(stderr, "Error: sql error trying to create table: %s: %s\n", 
-					m_tables[i]->name(), m_error);
-			return false;
-		}
-	}
-	return res==SQLITE_OK;
+	char* query;
+	asprintf(&query, "SELECT count(*) FROM %s;", m_tables[0]->name());
+	res = sqlite3_exec(this->m_db, query, NULL, NULL, NULL);
+	free(query);
+	return res != SQLITE_OK;
 }
 
+
 #define __SQL(callback, context, fmt) \
     va_list args; \
     va_start(args, fmt); \
@@ -247,7 +251,7 @@
 		strlcpy(m_error, sqlite3_errmsg(m_db), m_error_size);
 		fprintf(stderr, "Error: execute() error: %s \n", m_error);
 	}
-	sqlite3_reset(stmt);
+	res = sqlite3_reset(stmt);
 	return res;
 }
 
@@ -258,7 +262,6 @@
     va_start(args, _lastarg); \
 	for (uint32_t i=0; i<table->column_count(); i++) { \
 		Column* col = table->column(i); \
-        fprintf(stderr, "DEBUG: got a column from va_arg: %p \n", col); \
 		if (col->is_pk()) continue; \
 		uint8_t* bdata = NULL; \
 		uint32_t bsize = 0; \
@@ -266,15 +269,12 @@
 		switch(col->type()) { \
 			case TYPE_INTEGER: \
                 val = va_arg(args, uint64_t); \
-                fprintf(stderr, "DEBUG: param %d is integer: %llu \n", param, val); \
 				res = sqlite3_bind_int64(stmt, param++, val); \
 				break; \
 			case TYPE_TEXT: \
-                fprintf(stderr, "DEBUG: param %d is text\n", param); \
 				res = sqlite3_bind_text(stmt, param++, va_arg(args, char*), -1, SQLITE_STATIC); \
 				break; \
 			case TYPE_BLOB: \
-                fprintf(stderr, "DEBUG: param %d is blob\n", param); \
 				bdata = va_arg(args, uint8_t*); \
 				bsize = va_arg(args, uint32_t); \
                 res = sqlite3_bind_blob(stmt, param++, \
@@ -287,7 +287,7 @@
 			fprintf(stderr, "Error: failed to bind parameter #%d with column #%d of type %d " \
 					"table %s \n", \
 					param, i, col->type(), table->name()); \
-			return false; \
+			return res; \
 		} \
 	} \
     va_end(args);
@@ -297,20 +297,16 @@
     va_start(args, _lastarg); \
     for (uint32_t i=0; i<count; i++) { \
         Column* col = va_arg(args, Column*); \
-        fprintf(stderr, "DEBUG: got a column from va_arg: %p \n", col); \
         uint8_t* bdata = NULL; \
         uint32_t bsize = 0; \
         switch(col->type()) { \
             case TYPE_INTEGER: \
-                fprintf(stderr, "DEBUG: param %d is integer\n", param); \
                 res = sqlite3_bind_int64(stmt, param++, va_arg(args, uint64_t)); \
                 break; \
             case TYPE_TEXT: \
-                fprintf(stderr, "DEBUG: param %d is text\n", param); \
                 res = sqlite3_bind_text(stmt, param++, va_arg(args, char*), -1, SQLITE_STATIC); \
                 break; \
             case TYPE_BLOB: \
-                fprintf(stderr, "DEBUG: param %d is blob\n", param); \
                 bdata = va_arg(args, uint8_t*); \
                 bsize = va_arg(args, uint32_t); \
                 res = sqlite3_bind_blob(stmt, param++, \
@@ -323,7 +319,7 @@
             fprintf(stderr, "Error: failed to bind parameter #%d with column #%d of type %d " \
                             "table %s \n", \
 							param, i, col->type(), table->name()); \
-            return false; \
+            return res; \
         } \
     } \
     va_end(args);
@@ -331,67 +327,54 @@
 #define __get_stmt(expr) \
 	sqlite3_stmt* stmt; \
 	char* key = strdup(name); \
-    fprintf(stderr, "CACHE: statement cache at %p \n", m_statement_cache); \
 	cache_get_and_retain(m_statement_cache, key, (void**)&stmt); \
 	if (!stmt) { \
-		fprintf(stderr, "DEBUG: generating query for %s \n", key); \
 		va_list args; \
 		va_start(args, count); \
 		stmt = expr; \
 		va_end(args); \
 		cache_set_and_retain(m_statement_cache, key, stmt, sizeof(stmt)); \
-	} else { \
-		fprintf(stderr, "DEBUG: found query for %s in cache: %p \n", key, stmt); \
-		fprintf(stderr, "DEBUG: query is: %s \n", sqlite3_sql(stmt)); \
 	} \
 	free(key);
 
 #define __step_and_store(_stmt, _type, _output) \
-    fprintf(stderr, "DEBUG: _stmt = %p \n", _stmt); \
-    fprintf(stderr, "DEBUG: _output = %p \n", _output); \
-    fprintf(stderr, "DEBUG: query = %s \n", sqlite3_sql(_stmt)); \
-    fprintf(stderr, "DEBUG: col count: %d \n", sqlite3_column_count(_stmt)); \
     res = sqlite3_step(_stmt); \
     if (res == SQLITE_ROW) { \
 	    switch(_type) { \
 		    case TYPE_INTEGER: \
-				fprintf(stderr, "DEBUG: step and store : integer\n"); \
 			    *(uint64_t*)_output = (uint64_t)sqlite3_column_int64(_stmt, 0); \
-				fprintf(stderr, "DEBUG: step and store : %p %llu\n", (uint64_t*)_output, *(uint64_t*)_output); \
 			    break; \
 		    case TYPE_TEXT: \
-				fprintf(stderr, "DEBUG: step and store : text\n"); \
 			    *(const unsigned char**)_output = sqlite3_column_text(_stmt, 0); \
-				fprintf(stderr, "DEBUG: step and store : %s\n", *(char**)_output); \
 			    break; \
     		case TYPE_BLOB: \
-				fprintf(stderr, "DEBUG: step and store : blob\n"); \
 	    		*(const void**)_output = sqlite3_column_blob(_stmt, 0); \
 		    	break; \
     	} \
     } else { \
-        fprintf(stderr, "ERROR: %d \n", res); \
+        fprintf(stderr, "ERROR: __step_and_store(_stmt, _type_output) = %d \n", res); \
+		return res; \
     } \
     sqlite3_reset(_stmt); \
     cache_release_value(m_statement_cache, &_stmt);
 
-bool Database::count(const char* name, void** output, Table* table, uint32_t count, ...) {
+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;
 	uint32_t param = 1;
 	__bind_va_columns(count);
 	__step_and_store(stmt, TYPE_INTEGER, output);
-	return output != NULL;
+	return output == NULL;
 }
 
-bool Database::get_value(const char* name, void** output, Table* table, Column* value_column, 
+int Database::get_value(const char* name, void** output, Table* table, Column* value_column, 
 						 uint32_t count, ...) {
 	__get_stmt(table->get_value(m_db, value_column, count, args));
 	int res = SQLITE_OK;
 	uint32_t param = 1;
 	__bind_va_columns(count);
 	__step_and_store(stmt, value_column->type(), output);
-	return output != NULL;
+	return output == NULL;
 }
 
 /**
@@ -404,25 +387,58 @@
  * and then the uint32_t value for size of the data. 
  *
  */
-bool Database::update(Table* table, uint64_t pkvalue, ...) {
+int Database::update(Table* table, uint64_t pkvalue, ...) {
 	int res = SQLITE_OK;
 	
 	// get the prepared statement
 	sqlite3_stmt* stmt = table->update(m_db);
 	if (!stmt) {
 		fprintf(stderr, "Error: %s table gave a NULL statement when trying to update.\n", table->name());
-		return false;
+		return 1;
 	}
 	
 	uint32_t param = 1; // counter to track placeholders in sql statement
 	__bind_all_columns(pkvalue);
 	
 	// bind the primary key in the WHERE clause
-	res = sqlite3_bind_int64(stmt, param++, pkvalue);
-	res = this->execute(stmt);
-	return res == SQLITE_OK;
+	if (res==SQLITE_OK) res = sqlite3_bind_int64(stmt, param++, pkvalue);
+	if (res==SQLITE_OK) res = this->execute(stmt);
+	return res;
 }
 
+/**
+ * Given a table, value_column, and value to set, plus a va_list of Column,value for WHERE clause
+ *  set the value_column to value when WHERE is true
+ */
+int Database::update_value(const char* name, Table* table, Column* value_column, void** value, 
+							uint32_t count, ...) {
+	__get_stmt(table->update_value(m_db, value_column, count, args));
+	int res = SQLITE_OK;
+	uint32_t param = 1;
+	switch(value_column->type()) {
+		case TYPE_INTEGER:
+			res = sqlite3_bind_int64(stmt, param++, (uint64_t)*value);
+			break;
+		case TYPE_TEXT:
+			res = sqlite3_bind_text(stmt, param++, (char*)*value, -1, SQLITE_STATIC);
+			break;
+		// XXX: support blob columns here
+		case TYPE_BLOB:
+			fprintf(stderr, "Error: Database::update_value() not implemented for BLOB columns.\n");
+			assert(false);
+	}
+	if (res != SQLITE_OK) {
+		fprintf(stderr, "Error: update_value failed to bind value with value_column type %d in "
+				"table %s. \n",
+				value_column->type(), table->name());
+		return res;
+	}
+	__bind_va_columns(count);
+	res = sqlite3_step(stmt);
+	sqlite3_reset(stmt);
+    cache_release_value(m_statement_cache, &stmt);
+	return (res == SQLITE_DONE ? SQLITE_OK : res);
+}
 
 /**
  * Given a table and an arg list in the same order as Table::add_column() calls,
@@ -435,43 +451,40 @@
  * and then the uint32_t value for size of the data. 
  *
  */
-bool Database::insert(Table* table, ...) {
+int Database::insert(Table* table, ...) {
 	int res = SQLITE_OK;
-
 	// get the prepared statement
 	sqlite3_stmt* stmt = table->insert(m_db);
 	if (!stmt) {
 		fprintf(stderr, "Error: %s table gave a NULL statement when trying to insert.\n", table->name());
-		return false;
+		return 1;
 	}
-
 	uint32_t param = 1; // counter to track placeholders in sql statement
 	__bind_all_columns(table);
-	res = this->execute(stmt);
-	
-	return res == SQLITE_OK;
+	if (res == SQLITE_OK) res = this->execute(stmt);
+	return res;
 }
 
 
-bool Database::del(Table* table, uint64_t serial) {
+int Database::del(Table* table, uint64_t serial) {
 	int res = SQLITE_OK;
 	sqlite3_stmt* stmt = table->del(m_db);
 	if (!stmt) {
 		fprintf(stderr, "Error: %s table gave a NULL statement when trying to delete.\n", table->name());
-		return false;
+		return res;
 	}
-	res = sqlite3_bind_int64(stmt, 1, serial);
-	res = this->execute(stmt);
-	return res == SQLITE_OK;
+	if (res == SQLITE_OK) res = sqlite3_bind_int64(stmt, 1, serial);
+	if (res == SQLITE_OK) res = this->execute(stmt);
+	return res;
 }
 
-bool Database::del(const char* name, Table* table, uint32_t count, ...) {
+int Database::del(const char* name, Table* table, uint32_t count, ...) {
 	__get_stmt(table->del(m_db, count, args));
 	int res = SQLITE_OK;
 	uint32_t param = 1;
 	__bind_va_columns(count);
-	res = this->execute(stmt);
-	return res == SQLITE_OK;
+	if (res == SQLITE_OK) res = this->execute(stmt);
+	return res;
 	
 }
 
@@ -484,3 +497,15 @@
 }
 
 
+int Database::begin_transaction() {
+	return this->sql("BEGIN TRANSACTION");
+}
+
+int Database::rollback_transaction() {
+	return this->sql("ROLLBACK TRANSACTION");
+}
+
+int Database::commit_transaction() {
+	return this->sql("COMMIT TRANSACTION");
+}
+

Modified: branches/PR-7489777/darwinup/Database.h
===================================================================
--- branches/PR-7489777/darwinup/Database.h	2010-02-08 23:24:42 UTC (rev 698)
+++ branches/PR-7489777/darwinup/Database.h	2010-02-09 22:25:57 UTC (rev 699)
@@ -70,23 +70,16 @@
 	static const int TYPE_BLOB    = SQLITE_BLOB;
 	
 	virtual void init_schema();
-	const char* path();
-	const char* error();
-	bool connect();
-	bool connect(const char* path);
+	const char*  path();
+	const char*  error();
+	int          connect();
+	int          connect(const char* path);
 	
 	
-	bool begin_transaction();
-	bool rollback_transaction();
-	bool commit_transaction();
+	int          begin_transaction();
+	int          rollback_transaction();
+	int          commit_transaction();
 	
-
-	const char* get_row(Table* table, const char* where);
-	const char* get_column(Table* table, Column* column, const char* where);
-	const char* get_all(Table* table, const char* where);
-		
-	
-	
 	/**
 	 * SELECT statement caching and execution
 	 *
@@ -97,25 +90,26 @@
 	 *  everything else are Column*,value pairs for making a WHERE clause
 	 *
 	 */
-	bool count(const char* name, void** output, Table* table, uint32_t count, ...);
-	bool get_value(const char* name, void** output, Table* table, Column* value_column, uint32_t count, ...);
+	int  count(const char* name, void** output, Table* table, uint32_t count, ...);
+	int  get_value(const char* name, void** output, Table* table, Column* value_column, 
+				   uint32_t count, ...);
+	int  update_value(const char* name, Table* table, Column* value_column, void** value, 
+					  uint32_t count, ...);
+	int  update(Table* table, uint64_t pkvalue, ...);
+	int  insert(Table* table, ...);
 
-	
-	bool update(Table* table, uint64_t pkvalue, ...);
-	bool insert(Table* table, ...);
-
-	bool del(Table* table, uint64_t serial);
-	bool del(const char* name, Table* table, uint32_t count, ...);
-	
-	bool add_table(Table*);
+	int  del(Table* table, uint64_t serial);
+	int  del(const char* name, Table* table, uint32_t count, ...);
+		
+	int  add_table(Table*);
 	uint64_t last_insert_id();
 	
 	int sql(const char* fmt, ...);
 	
 protected:
 	
-	bool empty();
-	bool create_tables();
+	bool  is_empty();
+	int   create_tables();
 
 	
 	int execute(sqlite3_stmt* stmt);

Modified: branches/PR-7489777/darwinup/Depot.cpp
===================================================================
--- branches/PR-7489777/darwinup/Depot.cpp	2010-02-08 23:24:42 UTC (rev 698)
+++ branches/PR-7489777/darwinup/Depot.cpp	2010-02-09 22:25:57 UTC (rev 699)
@@ -762,15 +762,21 @@
 
 	// Installation is complete.  Activate the archive in the database.
 	if (res == 0) res = this->begin_transaction();
-	if (res == 0) res = this->m_db2->sql("UPDATE archives SET active=1 WHERE serial=%lld;", rollback->serial());
-	if (res == 0) res = this->m_db2->sql("UPDATE archives SET active=1 WHERE serial=%lld;", archive->serial());
+	if (res == 0) {
+		res = this->m_db2->activate_archive(rollback->serial());
+		if (res) this->rollback_transaction();
+	}
+	if (res == 0) {
+		res = this->m_db2->activate_archive(archive->serial());
+		if (res) this->rollback_transaction();
+	}
 	if (res == 0) res = this->commit_transaction();
 
 	// Remove the stage and rollback directories (save disk space)
 	remove_directory(archive_path);
 	remove_directory(rollback_path);
-	if (rollback_path) free(rollback_path);
-	if (archive_path) free(archive_path);
+	free(rollback_path);
+	free(archive_path);
 	
 	(void)this->lock(LOCK_SH);
 
@@ -916,7 +922,7 @@
 
 	// We do this here to get an exclusive lock on the database.
 	if (res == 0) res = this->begin_transaction();
-	if (res == 0) res = m_db2->sql("UPDATE archives SET active=0 WHERE serial=%lld;", serial);
+	if (res == 0) res = m_db2->deactivate_archive(serial);
 	if (res == 0) res = this->commit_transaction();
 
 	InstallContext context(this, archive);
@@ -927,7 +933,7 @@
 	for (i = 0; i < context.files_to_remove->count; ++i) {
 		uint64_t serial = context.files_to_remove->values[i];
 		IF_DEBUG("deleting file %lld\n", serial);
-		if (res == 0) res = m_db2->delete_file(serial) != true;
+		if (res == 0) res = m_db2->delete_file(serial);
 	}
 	if (res == 0) res = this->commit_transaction();
 
@@ -1173,15 +1179,15 @@
 
 
 int Depot::begin_transaction() {
-	return this->m_db2->sql("BEGIN TRANSACTION");
+	return this->m_db2->begin_transaction();
 }
 
 int Depot::rollback_transaction() {
-	return this->m_db2->sql("ROLLBACK TRANSACTION");
+	return this->m_db2->rollback_transaction();
 }
 
 int Depot::commit_transaction() {
-	return this->m_db2->sql("COMMIT TRANSACTION");
+	return this->m_db2->commit_transaction();
 }
 
 int Depot::is_locked() { return m_is_locked; }
@@ -1225,7 +1231,7 @@
 }
 
 int Depot::insert(Archive* archive, File* file) {
-	bool res = true;
+	int res = 0;
 	int do_update = 0;
 
 	// check for the destination prefix in file's path, remove if found
@@ -1239,19 +1245,26 @@
 
 	if (this->has_file(archive, file)) {
 		do_update = 1;
-		res = m_db2->update_file(archive, relpath, file->info(), file->mode(), file->uid(), file->gid(),
-								 file->digest());		
+		uint64_t serial = this->m_db2->get_file_serial_from_archive(archive, relpath);
+		if (!serial) {
+			fprintf(stderr, "Error: unable to find file from archive %llu at path %s \n", 
+					archive->serial(), relpath);
+			return 1;
+		}
+		res = m_db2->update_file(serial, archive, file->info(), file->mode(), file->uid(), file->gid(),
+								 file->digest(), relpath);		
 	} else {
 		file->m_serial = m_db2->insert_file(file->info(), file->mode(), file->uid(), file->gid(), 
-								 file->digest(), archive, relpath);
+											file->digest(), archive, relpath);
 		if (!file->m_serial) {
 			fprintf(stderr, "%s:%d: Could not add file to database: %s (%d)\n", 
 					__FILE__, __LINE__, sqlite3_errmsg(m_db), res);
+			return 2;
 		}
 	}
 
 	free(path);
-	return res != true;
+	return res;
 }
 
 int Depot::has_file(Archive* archive, File* file) {
@@ -1271,22 +1284,22 @@
 }
 
 int Depot::remove(Archive* archive) {
-	bool res = true;
+	int res = 0;
 	res = m_db2->delete_files(archive);
-	if (!res) {
+	if (res) {
 		fprintf(stderr, "Error: unable to delete files for archive %llu \n", archive->serial());
-		return false;
+		return res;
 	}
 	res = m_db2->delete_archive(archive);
-	if (!res) {
+	if (res) {
 		fprintf(stderr, "Error: unable to delete archive %llu \n", archive->serial());
-		return false;
+		return res;
 	}
-	return res != true;
+	return res;
 }
 
 int Depot::remove(File* file) {
-	return m_db2->delete_file(file) != true;
+	return m_db2->delete_file(file);
 }
 
 // helper to dispatch the actual command for process_archive()

Modified: branches/PR-7489777/darwinup/Table.cpp
===================================================================
--- branches/PR-7489777/darwinup/Table.cpp	2010-02-08 23:24:42 UTC (rev 698)
+++ branches/PR-7489777/darwinup/Table.cpp	2010-02-09 22:25:57 UTC (rev 699)
@@ -39,11 +39,11 @@
 // XXX
 void __hex_str(const char* s) {
 	int len = strlen(s);
-	fprintf(stderr, "HEXSTR: %d \n", len);
+	IF_DEBUG("HEXSTR: %d \n", len);
 	for (int i=0; i <= len; i++) {
-		fprintf(stderr, "%02x", (unsigned int)s[i]);
+		IF_DEBUG("%02x", (unsigned int)s[i]);
 	}
-	fprintf(stderr, "\n");
+	IF_DEBUG("\n");
 }
 
 
@@ -105,18 +105,18 @@
 }
 
 
-bool Table::add_column(Column* c) {
+int Table::add_column(Column* c) {
 	if (m_column_count >= m_column_max) {
 		m_columns = (Column**)realloc(m_columns, m_column_max * sizeof(Column*) * 4);
 		if (!m_columns) {
 			fprintf(stderr, "Error: unable to reallocate memory to add a column\n");
-			return false;
+			return 1;
 		}
 		m_column_max *= 4;
 	}
 	m_columns[m_column_count++] = c;
 	
-	return true;
+	return 0;
 }
 
 
@@ -249,6 +249,21 @@
 	return stmt;
 }
 
+sqlite3_stmt* Table::update_value(sqlite3* db, Column* value_column, uint32_t count, va_list args) {
+	__alloc_stmt_query;
+	strlcpy(query, "UPDATE ", size);
+	__check_and_cat(m_name);
+	__check_and_cat(" SET ");
+	__check_and_cat(value_column->name());
+	__check_and_cat("=? WHERE 1");
+	__where_va_columns;
+	strlcat(query, ";", size);
+	IF_DEBUG("[TABLE] update_value query: %s \n", query);
+	__prepare_stmt;
+	
+	return stmt;
+}
+
 /**
  * Prepare and cache the update statement. 
  * Assumes table only has 1 primary key
@@ -256,12 +271,11 @@
 sqlite3_stmt* Table::update(sqlite3* db) {
 	// we only need to prepare once, return if we already have it
 	if (m_prepared_update) {
-		fprintf(stderr, "[TABLE] %s table found cached update statement at %p \n",
-				m_name, m_prepared_update);
+		IF_DEBUG("[TABLE] %s table found cached update statement at %p \n", m_name, m_prepared_update);
 		return m_prepared_update;
 	}
 	
-	fprintf(stderr, "[TABLE] %s is generating an update statement \n", m_name);
+	IF_DEBUG("[TABLE] %s is generating an update statement \n", m_name);
 	
 	uint32_t i = 0;
 	bool comma = false;  // flag we set to start adding commas
@@ -314,12 +328,12 @@
 sqlite3_stmt* Table::insert(sqlite3* db) {
 	// we only need to prepare once, return if we already have it
 	if (m_prepared_insert) {
-		fprintf(stderr, "[TABLE] %s table found cached insert statement at %p \n",
-				m_name, m_prepared_insert);
+		IF_DEBUG("[TABLE] %s table found cached insert statement at %p \n",
+				 m_name, m_prepared_insert);
 		return m_prepared_insert;
 	}
 	
-	fprintf(stderr, "[TABLE] %s is generating an insert statement \n", m_name);
+	IF_DEBUG("[TABLE] %s is generating an insert statement \n", m_name);
 	
 	uint32_t i = 0;
 	bool comma = false;  // flag we set to start adding commas

Modified: branches/PR-7489777/darwinup/Table.h
===================================================================
--- branches/PR-7489777/darwinup/Table.h	2010-02-08 23:24:42 UTC (rev 698)
+++ branches/PR-7489777/darwinup/Table.h	2010-02-09 22:25:57 UTC (rev 699)
@@ -47,7 +47,7 @@
 	const Column** columns();
 	Column*        column(uint32_t index);
 	uint32_t       column_count();
-	bool           add_column(Column*);
+	int            add_column(Column*);
 		
 	// return SQL statements for this table
 	char*    create();  
@@ -58,7 +58,10 @@
 
 	sqlite3_stmt*    count(sqlite3* db);
 	sqlite3_stmt*    count(sqlite3* db, uint32_t count, va_list args);
-	sqlite3_stmt*    get_value(sqlite3* db, Column* value_column, uint32_t count, va_list args);
+	sqlite3_stmt*    get_value(sqlite3* db, Column* value_column, uint32_t count, 
+							   va_list args);
+	sqlite3_stmt*    update_value(sqlite3* db, Column* value_column, uint32_t count,
+								  va_list args);
 	sqlite3_stmt*    insert(sqlite3* db);
 	sqlite3_stmt*    update(sqlite3* db);
 	sqlite3_stmt*    del(sqlite3* db);
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/darwinbuild-changes/attachments/20100209/5d0d3550/attachment-0001.html>


More information about the darwinbuild-changes mailing list