2 * Copyright (c) 2022 Omar Polo <op@omarpolo.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 #include "dictionary.h"
30 #define SQLPORTS "/usr/local/share/sqlports"
33 #define QNUM "select count(distinct pkgstem) from portsq;"
34 #define QALL "select distinct pkgstem, comment, descr_contents from portsq;"
37 countports(sqlite3 *db)
42 r = sqlite3_prepare_v2(db, QNUM, -1, &stmt, NULL);
44 warnx("failed to prepare statement: %s",
49 r = sqlite3_step(stmt);
51 n = sqlite3_column_int(stmt, 0);
53 sqlite3_finalize(stmt);
58 idx_ports(struct dictionary *dict, struct db_entry **entries, size_t *len,
59 int argc, char **argv)
74 if ((r = sqlite3_open(dbpath, &db)) != SQLITE_OK)
75 errx(1, "can't open %s: %s", dbpath, sqlite3_errstr(r));
77 if ((r = countports(db)) == -1 || r == 0) {
78 warnx("error querying the db or empty portsq table!");
83 if ((*entries = calloc(*len, sizeof(**entries))) == NULL)
86 r = sqlite3_prepare_v2(db, QALL, -1, &stmt, NULL);
88 errx(1, "failed to prepare statement: %s", sqlite3_errstr(r));
90 for (i = 0; i < *len; ++i) {
91 const char *pkgstem, *comment, *descr;
94 r = sqlite3_step(stmt);
98 errx(1, "sqlite3_step: %s", sqlite3_errstr(r));
100 pkgstem = sqlite3_column_text(stmt, 0);
101 comment = sqlite3_column_text(stmt, 1);
102 descr = sqlite3_column_text(stmt, 2);
104 (*entries)[i].name = xstrdup(pkgstem);
105 (*entries)[i].descr = xstrdup(comment);
107 r = asprintf(&doc, "%s %s %s", pkgstem,
108 comment != NULL ? comment : "",
109 descr != NULL ? descr : "");
113 if ((toks = tokenize(doc)) == NULL)
115 if (!dictionary_add_words(dict, toks, i))
116 err(1, "dictionary_add_words");