implemented simple delete from database

This commit is contained in:
Andreas Steffen
2011-11-28 14:39:53 +01:00
parent f4159ff816
commit 2b28a13182
2 changed files with 49 additions and 3 deletions
@@ -63,6 +63,7 @@ static void do_args(int argc, char *argv[])
{ "help", no_argument, NULL, 'h' }, { "help", no_argument, NULL, 'h' },
{ "files", no_argument, NULL, 'f' }, { "files", no_argument, NULL, 'f' },
{ "add", no_argument, NULL, 'a' }, { "add", no_argument, NULL, 'a' },
{ "delete", no_argument, NULL, 'd' },
{ "del", no_argument, NULL, 'd' }, { "del", no_argument, NULL, 'd' },
{ "products", no_argument, NULL, 'p' }, { "products", no_argument, NULL, 'p' },
{ "hashes", no_argument, NULL, 'H' }, { "hashes", no_argument, NULL, 'H' },
@@ -114,12 +114,13 @@ METHOD(attest_db_t, set_product, bool,
if (!create) if (!create)
{ {
printf("product '%s' not found in database\n", product); printf("product '%s' not found in database\n", product);
return FALSE;
} }
/* Add a new database entry */ /* Add a new database entry */
this->product_set = this->db->execute(this->db, &this->pid, this->product_set = this->db->execute(this->db, &this->pid,
"INSERT INTO products (name) VALUES (?)", "INSERT INTO products (name) VALUES (?)",
DB_TEXT, product); DB_TEXT, product) == 1;
printf("product '%s' %sinserted into database\n", product, printf("product '%s' %sinserted into database\n", product,
this->product_set ? "" : "could not be "); this->product_set ? "" : "could not be ");
@@ -188,12 +189,13 @@ METHOD(attest_db_t, set_file, bool,
if (!create) if (!create)
{ {
printf("file '%s' not found in database\n", file); printf("file '%s' not found in database\n", file);
return FALSE;
} }
/* Add a new database entry */ /* Add a new database entry */
this->file_set = this->db->execute(this->db, &this->fid, this->file_set = this->db->execute(this->db, &this->fid,
"INSERT INTO files (type, path) VALUES (0, ?)", "INSERT INTO files (type, path) VALUES (0, ?)",
DB_TEXT, file); DB_TEXT, file) == 1;
printf("file '%s' %sinserted into database\n", file, printf("file '%s' %sinserted into database\n", file,
this->file_set ? "" : "could not be "); this->file_set ? "" : "could not be ");
@@ -264,12 +266,13 @@ METHOD(attest_db_t, set_directory, bool,
if (!create) if (!create)
{ {
printf("directory '%s' not found in database\n", dir); printf("directory '%s' not found in database\n", dir);
return FALSE;
} }
/* Add a new database entry */ /* Add a new database entry */
this->dir_set = this->db->execute(this->db, &this->did, this->dir_set = this->db->execute(this->db, &this->did,
"INSERT INTO files (type, path) VALUES (1, ?)", "INSERT INTO files (type, path) VALUES (1, ?)",
DB_TEXT, dir); DB_TEXT, dir) == 1;
printf("directory '%s' %sinserted into database\n", dir, printf("directory '%s' %sinserted into database\n", dir,
this->dir_set ? "" : "could not be "); this->dir_set ? "" : "could not be ");
@@ -571,6 +574,48 @@ METHOD(attest_db_t, add, bool,
METHOD(attest_db_t, delete, bool, METHOD(attest_db_t, delete, bool,
private_attest_db_t *this) private_attest_db_t *this)
{ {
bool success;
if (this->pid && (this->fid || this->did))
{
printf("deletion of product/file entries not supported yet\n");
return FALSE;
}
if (this->pid)
{
success = this->db->execute(this->db, NULL,
"DELETE FROM products WHERE id = ?",
DB_UINT, this->pid) > 0;
printf("product '%s' %sdeleted from database\n", this->product,
success ? "" : "could not be ");
return success;
}
if (this->fid)
{
success = this->db->execute(this->db, NULL,
"DELETE FROM files WHERE id = ?",
DB_UINT, this->fid) > 0;
printf("file '%s' %sdeleted from database\n", this->file,
success ? "" : "could not be ");
return success;
}
if (this->did)
{
success = this->db->execute(this->db, NULL,
"DELETE FROM files WHERE type = 1 AND id = ?",
DB_UINT, this->did) > 0;
printf("directory '%s' %sdeleted from database\n", this->dir,
success ? "" : "could not be ");
return success;
}
printf("empty delete command\n");
return FALSE; return FALSE;
} }