make it easier to adjust/extend the search queries

Syntax components can overwrite getSearchConfig() and implement their
own SearchConfig that then can overwrite runSQLBuilder() to access the
underlying QueryBuilder
This commit is contained in:
Andreas Gohr
2023-07-19 11:14:07 +02:00
parent 569c44d5e2
commit 299ca8cc64
3 changed files with 39 additions and 3 deletions

View File

@ -507,20 +507,32 @@ class Search
/**
* Transform the set search parameters into a statement
*
* Calls runSQLBuilder()
*
* @return array ($sql, $opts) The SQL and parameters to execute
*/
public function getSQL()
{
if (!$this->columns) throw new StructException('nocolname');
return $this->runSQLBuilder()->getSQL();
}
/**
* Initialize and execute the SQLBuilder
*
* Called from getSQL(). Can be overwritten to extend the query using the query builder
*
* @return SearchSQLBuilder
*/
protected function runSQLBuilder()
{
$sqlBuilder = new SearchSQLBuilder();
$sqlBuilder->addSchemas($this->schemas);
$sqlBuilder->addColumns($this->columns);
$sqlBuilder->addFilters($this->filter);
$sqlBuilder->addFilters($this->dynamicFilter);
$sqlBuilder->addSorts($this->sortby);
return $sqlBuilder->getSQL();
return $sqlBuilder;
}
/**

View File

@ -167,9 +167,21 @@ class SearchSQLBuilder
}
}
/**
* Access to the underlying QueryBuilder
*
* @return QueryBuilder
*/
public function getQueryBuilder()
{
return $this->qb;
}
/**
* Get the SQL query and parameters
*
* Shortcut for $this->getQueryBuilder()->getSQL()
*
* @return array ($sql, $params)
*/
public function getSQL()

View File

@ -108,7 +108,7 @@ class syntax_plugin_struct_table extends DokuWiki_Syntax_Plugin
}
try {
$search = new SearchConfig($config);
$search = $this->getSearchConfig($config);
if ($format === 'struct_csv') {
// no pagination in export
$search->setLimit(0);
@ -133,6 +133,18 @@ class syntax_plugin_struct_table extends DokuWiki_Syntax_Plugin
return true;
}
/**
* Initialize a SearchConfig with the given parsed config
*
* @param array $config
* @return SearchConfig
*/
protected function getSearchConfig($config)
{
return new SearchConfig($config);
}
/**
* Filter based on primary key columns, applicable in child classes
*