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

source_changes at macosforge.org source_changes at macosforge.org
Mon Feb 8 15:24:45 PST 2010


Revision: 698
          http://trac.macosforge.org/projects/darwinbuild/changeset/698
Author:   wsiegrist at apple.com
Date:     2010-02-08 15:24:42 -0800 (Mon, 08 Feb 2010)
Log Message:
-----------
Partial port of Depot to DarwinupDatabase. Test suite passes.

Modified Paths:
--------------
    branches/PR-7489777/darwinup/DB.cpp
    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/main.cpp

Modified: branches/PR-7489777/darwinup/DB.cpp
===================================================================
--- branches/PR-7489777/darwinup/DB.cpp	2010-02-08 19:05:07 UTC (rev 697)
+++ branches/PR-7489777/darwinup/DB.cpp	2010-02-08 23:24:42 UTC (rev 698)
@@ -75,10 +75,10 @@
 
 
 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),
+							(uint32_t)sizeof(uuid_t),
 							name,
 							(uint64_t)date_added,
 							(uint64_t)0,

Modified: branches/PR-7489777/darwinup/Database.cpp
===================================================================
--- branches/PR-7489777/darwinup/Database.cpp	2010-02-08 19:05:07 UTC (rev 697)
+++ branches/PR-7489777/darwinup/Database.cpp	2010-02-08 23:24:42 UTC (rev 698)
@@ -39,6 +39,16 @@
 	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]);
+	}
+	fprintf(stderr, "\n");
+}
+
+
 Database::Database() {
 	// XXX: make the initial allocation for 2 to tailor to darwinup usage
 	m_table_max = 1;
@@ -174,15 +184,18 @@
 }
 
 
+/**
+ * attempt to get a row count of the first table to detect if the schema
+ * needs to be initialized
+ */
 bool Database::empty() {
 	if (!m_tables[0]) {
 		fprintf(stderr, "Error: Database has not had a schema initialized.\n");
 		return false;
 	}
-	sqlite3_stmt* stmt = m_tables[0]->count(m_db);
-	int res = sqlite3_step(stmt);
-	sqlite3_finalize(stmt);
-	return res!=SQLITE_ROW;
+	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() {
@@ -219,34 +232,52 @@
 	__SQL(NULL, NULL, fmt);
 	if (error) {
 		strlcpy(m_error, error, m_error_size);
+		fprintf(stderr, "Error: sql(): %s \n", m_error);
+		fprintf(stderr, "Error: fmt: %s \n", fmt);
 		sqlite3_free(error);
 	}
-	IF_DEBUG("[DATABASE] __SQL set res = %d \n", res);
 	return res;
 }
 
+int Database::execute(sqlite3_stmt* stmt) {
+	int res = sqlite3_step(stmt);
+	if (res == SQLITE_DONE) {
+		res = SQLITE_OK;
+	} else {
+		strlcpy(m_error, sqlite3_errmsg(m_db), m_error_size);
+		fprintf(stderr, "Error: execute() error: %s \n", m_error);
+	}
+	sqlite3_reset(stmt);
+	return res;
+}
+
 #undef __SQL
 
-
 #define __bind_all_columns(_lastarg) \
     va_list args; \
     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; \
+        uint64_t val = 1234; \
 		switch(col->type()) { \
 			case TYPE_INTEGER: \
-				res = sqlite3_bind_int64(stmt, param++, va_arg(args, uint64_t)); \
+                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++, \
+                res = sqlite3_bind_blob(stmt, param++, \
 										bdata, \
 										bsize, \
 										SQLITE_STATIC); \
@@ -297,13 +328,6 @@
     } \
     va_end(args);
 
-int Database::execute(sqlite3_stmt* stmt) {
-	int res = sqlite3_step(stmt);
-	if (res == SQLITE_DONE) res = SQLITE_OK;
-	sqlite3_reset(stmt);
-	return res;
-}
-
 #define __get_stmt(expr) \
 	sqlite3_stmt* stmt; \
 	char* key = strdup(name); \
@@ -325,7 +349,7 @@
 #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: 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) { \
@@ -402,6 +426,7 @@
 
 /**
  * Given a table and an arg list in the same order as Table::add_column() calls,
+ * minus any primary key columns, 
  * binds and executes a sql insertion. The Table is responsible for preparing the
  * statement in Table::insert()
  *
@@ -419,10 +444,11 @@
 		fprintf(stderr, "Error: %s table gave a NULL statement when trying to insert.\n", table->name());
 		return false;
 	}
-	
+
 	uint32_t param = 1; // counter to track placeholders in sql statement
 	__bind_all_columns(table);
 	res = this->execute(stmt);
+	
 	return res == SQLITE_OK;
 }
 

Modified: branches/PR-7489777/darwinup/Database.h
===================================================================
--- branches/PR-7489777/darwinup/Database.h	2010-02-08 19:05:07 UTC (rev 697)
+++ branches/PR-7489777/darwinup/Database.h	2010-02-08 23:24:42 UTC (rev 698)
@@ -110,12 +110,13 @@
 	bool add_table(Table*);
 	uint64_t last_insert_id();
 	
-
+	int sql(const char* fmt, ...);
+	
 protected:
 	
 	bool empty();
 	bool create_tables();
-	int sql(const char* fmt, ...);
+
 	
 	int execute(sqlite3_stmt* stmt);
 	

Modified: branches/PR-7489777/darwinup/Depot.cpp
===================================================================
--- branches/PR-7489777/darwinup/Depot.cpp	2010-02-08 19:05:07 UTC (rev 697)
+++ branches/PR-7489777/darwinup/Depot.cpp	2010-02-08 23:24:42 UTC (rev 698)
@@ -118,22 +118,13 @@
 	res = this->lock(LOCK_SH);
 	if (res) return res;
 	m_is_locked = 1;
-	
-	int exists = is_regular_file(m_database_path);
-	
+		
 	res = sqlite3_open(m_database_path, &m_db);
 	if (res) {
 		sqlite3_close(m_db);
 		m_db = NULL;
 	}
 	
-	if (m_db && !exists) {
-		this->SQL("CREATE TABLE archives (serial INTEGER PRIMARY KEY AUTOINCREMENT, uuid BLOB UNIQUE, name TEXT, date_added INTEGER, active INTEGER, info INTEGER)");
-		this->SQL("CREATE TABLE files (serial INTEGER PRIMARY KEY AUTOINCREMENT, archive INTEGER, info INTEGER, mode INTEGER, uid INTEGER, gid INTEGER, size INTEGER, digest BLOB, path TEXT)");
-		this->SQL("CREATE INDEX archives_uuid ON archives (uuid)");
-		this->SQL("CREATE INDEX files_path ON files (path)");
-	}
-	
 	m_db2 = new DarwinupDatabase(m_database_path);
 	
 	return res;
@@ -771,8 +762,8 @@
 
 	// Installation is complete.  Activate the archive in the database.
 	if (res == 0) res = this->begin_transaction();
-	if (res == 0) res = SQL("UPDATE archives SET active=1 WHERE serial=%lld;", rollback->serial());
-	if (res == 0) res = SQL("UPDATE archives SET active=1 WHERE serial=%lld;", archive->serial());
+	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->commit_transaction();
 
 	// Remove the stage and rollback directories (save disk space)
@@ -925,7 +916,7 @@
 
 	// We do this here to get an exclusive lock on the database.
 	if (res == 0) res = this->begin_transaction();
-	if (res == 0) res = SQL("UPDATE archives SET active=0 WHERE serial=%lld;", serial);
+	if (res == 0) res = m_db2->sql("UPDATE archives SET active=0 WHERE serial=%lld;", serial);
 	if (res == 0) res = this->commit_transaction();
 
 	InstallContext context(this, archive);
@@ -936,7 +927,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);
+		if (res == 0) res = m_db2->delete_file(serial) != true;
 	}
 	if (res == 0) res = this->commit_transaction();
 
@@ -1182,15 +1173,15 @@
 
 
 int Depot::begin_transaction() {
-	return this->SQL("BEGIN TRANSACTION");
+	return this->m_db2->sql("BEGIN TRANSACTION");
 }
 
 int Depot::rollback_transaction() {
-	return this->SQL("ROLLBACK TRANSACTION");
+	return this->m_db2->sql("ROLLBACK TRANSACTION");
 }
 
 int Depot::commit_transaction() {
-	return this->SQL("COMMIT TRANSACTION");
+	return this->m_db2->sql("COMMIT TRANSACTION");
 }
 
 int Depot::is_locked() { return m_is_locked; }
@@ -1291,7 +1282,7 @@
 		fprintf(stderr, "Error: unable to delete archive %llu \n", archive->serial());
 		return false;
 	}
-	return res == true;
+	return res != true;
 }
 
 int Depot::remove(File* file) {
@@ -1370,6 +1361,8 @@
 
 int Depot::SQL(const char* fmt, ...) {
 	int res;
+	fprintf(stderr, "Depot::SQL() called!\n");
+	assert(false);
 	__SQL(NULL, NULL, fmt);
 	return res;
 }

Modified: branches/PR-7489777/darwinup/Table.cpp
===================================================================
--- branches/PR-7489777/darwinup/Table.cpp	2010-02-08 19:05:07 UTC (rev 697)
+++ branches/PR-7489777/darwinup/Table.cpp	2010-02-08 23:24:42 UTC (rev 698)
@@ -53,6 +53,13 @@
 	m_columns       = (Column**)malloc(sizeof(Column*) * m_column_max);
 	m_name          = strdup("unnamed_table");
 	m_create_sql    = NULL;
+	m_insert_sql    = NULL;
+	m_update_sql    = NULL;
+	m_delete_sql    = NULL;
+	m_prepared_insert = NULL;
+	m_prepared_update = NULL;
+	m_prepared_delete = NULL;
+
 }
 
 Table::Table(const char* name) {
@@ -61,6 +68,12 @@
 	m_columns       = (Column**)malloc(sizeof(Column*) * m_column_max);
 	m_name          = strdup(name);
 	m_create_sql    = NULL;
+	m_insert_sql    = NULL;
+	m_update_sql    = NULL;
+	m_delete_sql    = NULL;
+	m_prepared_insert = NULL;
+	m_prepared_update = NULL;
+	m_prepared_delete = NULL;	
 }
 
 Table::~Table() {
@@ -194,7 +207,7 @@
 	int res = sqlite3_prepare_v2(db, query, size, &stmt, NULL); \
 	free(query); \
 	if (res != SQLITE_OK) { \
-		fprintf(stderr, "Error: unable to prepare statement.\n"); \
+		fprintf(stderr, "Error: unable to prepare statement: %s\n", sqlite3_errmsg(db)); \
 		return NULL; \
 	}
 
@@ -242,8 +255,14 @@
  */
 sqlite3_stmt* Table::update(sqlite3* db) {
 	// we only need to prepare once, return if we already have it
-	if (m_prepared_update) return m_prepared_update;
+	if (m_prepared_update) {
+		fprintf(stderr, "[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);
+	
 	uint32_t i = 0;
 	bool comma = false;  // flag we set to start adding commas
 	
@@ -294,8 +313,14 @@
 
 sqlite3_stmt* Table::insert(sqlite3* db) {
 	// we only need to prepare once, return if we already have it
-	if (m_prepared_insert) return m_prepared_insert;
+	if (m_prepared_insert) {
+		fprintf(stderr, "[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);
+	
 	uint32_t i = 0;
 	bool comma = false;  // flag we set to start adding commas
 	

Modified: branches/PR-7489777/darwinup/main.cpp
===================================================================
--- branches/PR-7489777/darwinup/main.cpp	2010-02-08 19:05:07 UTC (rev 697)
+++ branches/PR-7489777/darwinup/main.cpp	2010-02-08 23:24:42 UTC (rev 698)
@@ -145,6 +145,7 @@
 		exit(2);
 	}
 	
+	/*
 	// XXX: test area for new database... remove me
 	DarwinupDatabase* testdb = depot->get_db2();
 
@@ -169,8 +170,8 @@
 	
 	exit(0);
 	// XXX
+	*/
 	
-	
 	if (argc == 2 && strcmp(argv[0], "install") == 0) {
 		char uuid[37];
 		Archive* archive = ArchiveFactory(argv[1], depot->downloads_path());
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/darwinbuild-changes/attachments/20100208/4c29ee27/attachment-0001.html>


More information about the darwinbuild-changes mailing list