- if (NULL != sec) {
- sz += strlen(andsec) + 1;
- sql = mandoc_realloc(sql, sz);
- strlcat(sql, andsec, sz);
- }
-
- sz += 2;
- sql = mandoc_realloc(sql, sz);
- strlcat(sql, "(", sz);
-
- for ( ; NULL != e; e = e->next) {
- sz += (NULL == e->substr ? regexpsz : substrsz) +
- (NULL == e->next ? 3 : 5);
- sql = mandoc_realloc(sql, sz);
- strlcat(sql, NULL == e->substr ? regexp : substr, sz);
- strlcat(sql, NULL == e->next ? ");" : " OR ", sz);
+ if (NULL != arch)
+ sql_append(&sql, &sz, "arch = ? AND ", 1);
+ if (NULL != sec)
+ sql_append(&sql, &sz, "sec = ? AND ", 1);
+ sql_append(&sql, &sz, "(", 1);
+
+ for (needop = 0; NULL != e; e = e->next) {
+ if (e->and)
+ sql_append(&sql, &sz, " AND ", 1);
+ else if (needop)
+ sql_append(&sql, &sz, " OR ", 1);
+ if (e->open)
+ sql_append(&sql, &sz, "(", e->open);
+ sql_append(&sql, &sz, NULL == e->substr ?
+ "id IN (SELECT pageid FROM keys "
+ "WHERE key REGEXP ? AND bits & ?)" :
+ "id IN (SELECT pageid FROM keys "
+ "WHERE key MATCH ? AND bits & ?)", 1);
+ if (e->close)
+ sql_append(&sql, &sz, ")", e->close);
+ needop = 1;