int explain_sql (char *sql) { sql_compile(sql); if (sql_ok) { sql_dump(); } else printf("PARSE ERROR.\n"); sql_cleanup_query(); return 0; }
/* Dumps search results that match the provided query term */ void print_search_results(char *term) { int count = 0; char *query = NULL, *q = NULL, *table = NULL; /* Format strings for the respective columns retrieved in sql_dump() */ char *col_fmt[NUM_COLS] = {"%-25s", "%-50s", "%-25s", "%-25s", "%-15s", "%-50s"}; if(sql_init(DB_NAME, DB_LOCAL) != SQLITE_OK) { sql_log_error(); goto end; } /* Queries should be in the format: <table.column>=<search term> */ table = strdup(term); q = strstr(table, QUERY_DELIMITER); if(!q) { fprintf(stderr, "ERROR: Improperly formatted query!\n"); goto end; } memset(q, 0, 1); q++; query = sqlite3_mprintf("SELECT firmware.vendor,firmware.description,hardware.vendor,model,revision,hardware.description FROM firmware JOIN hardware ON firmware.device_id=hardware.id WHERE %s LIKE '%%%q%%'", table, q); /* Print out a table of all relevant database info related to this certificate */ printf("\n%s\n%s\n", COL_HEADERS, HEADER_DELIM); count = sql_dump(query, col_fmt, NUM_COLS, stdout); printf("\nFound %d matches for '%s'.\n\n", count, q); end: if(table) free(table); sqlite3_free(query); sql_cleanup(); return; }
/* Prints out all information related to the selected certificate to stdout */ void print_all_cert_info(struct keymaster *certinfo) { int count = 0; char *query = NULL; /* Format strings for the respective columns retrieved in sql_dump() */ char *col_fmt[NUM_COLS] = {"%-25s", "%-50s", "%-25s", "%-25s", "%-15s", "%-50s"}; if(sql_init(DB_NAME, DB_LOCAL) != SQLITE_OK) { sql_log_error(); return; } query = sqlite3_mprintf("SELECT firmware.vendor,firmware.description,hardware.vendor,model,revision,hardware.description FROM firmware JOIN hardware ON firmware.device_id=hardware.id WHERE certificate_id = (SELECT id FROM certificates WHERE fingerprint = %Q)", certinfo->fingerprint); /* Print out a table of all relevant database info related to this certificate */ printf("\n%s\n%s\n", COL_HEADERS, HEADER_DELIM); count = sql_dump(query, col_fmt, NUM_COLS, stdout); printf("\nFound %d firmware(s) using this certificate.\n\n", count); sqlite3_free(query); sql_cleanup(); return; }