Esempio n. 1
0
File: query.c Progetto: baitisj/pkg
struct pkg_repo_it *
pkg_repo_binary_query(struct pkg_repo *repo, const char *pattern, match_t match)
{
	sqlite3 *sqlite = PRIV_GET(repo);
	sqlite3_stmt	*stmt = NULL;
	struct sbuf	*sql = NULL;
	const char	*comp = NULL;
	int		 ret;
	char		 basesql[BUFSIZ] = ""
		"SELECT id, origin, name, name as uniqueid, version, comment, "
		"prefix, desc, arch, maintainer, www, "
		"licenselogic, flatsize, pkgsize, "
		"cksum, manifestdigest, path AS repopath, '%s' AS dbname "
		"FROM packages AS p";

	if (match != MATCH_ALL && (pattern == NULL || pattern[0] == '\0'))
		return (NULL);

	sql = sbuf_new_auto();
	comp = pkgdb_get_pattern_query(pattern, match);
	if (comp && comp[0])
		strlcat(basesql, comp, sizeof(basesql));

	sbuf_printf(sql, basesql, repo->name);

	sbuf_cat(sql, " ORDER BY name;");
	sbuf_finish(sql);

	pkg_debug(4, "Pkgdb: running '%s' query for %s", sbuf_data(sql),
	     pattern == NULL ? "all": pattern);
	ret = sqlite3_prepare_v2(sqlite, sbuf_data(sql), sbuf_len(sql), &stmt,
	    NULL);
	if (ret != SQLITE_OK) {
		ERROR_SQLITE(sqlite, sbuf_data(sql));
		sbuf_delete(sql);
		return (NULL);
	}

	sbuf_delete(sql);

	if (match != MATCH_ALL && match != MATCH_CONDITION)
		sqlite3_bind_text(stmt, 1, pattern, -1, SQLITE_TRANSIENT);

	return (pkg_repo_binary_it_new(repo, stmt, PKGDB_IT_FLAG_ONCE));
}
Esempio n. 2
0
struct pkgdb_it *
pkgdb_rquery(struct pkgdb *db, const char *pattern, match_t match,
             const char *repo)
{
    sqlite3_stmt	*stmt = NULL;
    struct sbuf	*sql = NULL;
    const char	*reponame = NULL;
    const char	*comp = NULL;
    int		 ret;
    char		 basesql[BUFSIZ] = ""
                                   "SELECT id, origin, name, version, comment, "
                                   "prefix, desc, arch, maintainer, www, "
                                   "licenselogic, flatsize, pkgsize, "
                                   "cksum, manifestdigest, path AS repopath, '%1$s' AS dbname "
                                   "FROM '%1$s'.packages p";

    assert(db != NULL);
    assert(match == MATCH_ALL || (pattern != NULL && pattern[0] != '\0'));

    /*
     * If we have no remote repos loaded, we just return nothing instead of failing
     * an assert deep inside pkgdb_get_reponame
     */
    if (db->type != PKGDB_REMOTE)
        return (NULL);

    reponame = pkgdb_get_reponame(db, repo);

    sql = sbuf_new_auto();
    comp = pkgdb_get_pattern_query(pattern, match);
    if (comp && comp[0])
        strlcat(basesql, comp, sizeof(basesql));

    /*
     * Working on multiple remote repositories
     */
    if (reponame == NULL) {
        /* duplicate the query via UNION for all the attached
         * databases */

        ret = pkgdb_sql_all_attached(db->sqlite, sql,
                                     basesql, " UNION ALL ");
        if (ret != EPKG_OK) {
            sbuf_delete(sql);
            return (NULL);
        }
    } else
        sbuf_printf(sql, basesql, reponame, reponame);

    sbuf_cat(sql, " ORDER BY name;");
    sbuf_finish(sql);

    pkg_debug(4, "Pkgdb: running '%s'", sbuf_get(sql));
    ret = sqlite3_prepare_v2(db->sqlite, sbuf_get(sql), sbuf_size(sql), &stmt, NULL);
    if (ret != SQLITE_OK) {
        ERROR_SQLITE(db->sqlite);
        sbuf_delete(sql);
        return (NULL);
    }

    sbuf_delete(sql);

    if (match != MATCH_ALL && match != MATCH_CONDITION)
        sqlite3_bind_text(stmt, 1, pattern, -1, SQLITE_TRANSIENT);

    return (pkgdb_it_new(db, stmt, PKG_REMOTE, PKGDB_IT_FLAG_ONCE));
}