FreeBSD/contrib/mdocml/mansearch.c
/*
 * Prepare the search SQL statement.
 */
Prepare the search SQL statement.
Prepare the search SQL statement .
static char *
sql_statement(const struct expr *e)
{
	char		*sql;
	size_t		 sz;
	int		 needop;
	sql = mandoc_strdup(e->equal ?
	    "SELECT desc, form, pageid, bits "
		"FROM mpages NATURAL JOIN names WHERE " :
	    "SELECT desc, form, pageid, 0 FROM mpages WHERE ");
	sz = strlen(sql);
	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,
		    TYPE_Nd & e->bits
		    ? (NULL == e->substr
			? "desc REGEXP ?"
			: "desc MATCH ?")
		    : TYPE_Nm == e->bits
		    ? (NULL == e->substr
			? "pageid IN (SELECT pageid FROM names "
			  "WHERE name REGEXP ?)"
			: e->equal
			? "name = ? "
			: "pageid IN (SELECT pageid FROM names "
			  "WHERE name MATCH ?)")
		    : (NULL == e->substr
			? "pageid IN (SELECT pageid FROM keys "
			  "WHERE key REGEXP ? AND bits & ?)"
			: "pageid IN (SELECT pageid FROM keys "
			  "WHERE key MATCH ? AND bits & ?)"), 1);
		if (e->close)
			sql_append(&sql, &sz, ")", e->close);
		needop = 1;
	}
	return sql;
}
static char * sql_statement ( const struct expr * e ) { char * sql ; size_t sz ; int needop ; sql = mandoc_strdup ( e -> equal ? " " " " : " " ) ; sz = strlen ( sql ) ; for ( needop = 0 ; NULL != e ; e = e -> next ) { if ( e -> and ) sql_append ( & sql , & sz , " " , 1 ) ; else if ( needop ) sql_append ( & sql , & sz , " " , 1 ) ; if ( e -> open ) sql_append ( & sql , & sz , " " , e -> open ) ; sql_append ( & sql , & sz , TYPE_Nd & e -> bits ? ( NULL == e -> substr ? " " : " " ) : TYPE_Nm == e -> bits ? ( NULL == e -> substr ? " " " " : e -> equal ? " " : " " " " ) : ( NULL == e -> substr ? " " " " : " " " " ) , 1 ) ; if ( e -> close ) sql_append ( & sql , & sz , " " , e -> close ) ; needop = 1 ; } return sql ; }
Prepare the search SQL statement.
Prepare the search SQL statement .