mirror of
https://github.com/silverstripe/silverstripe-postgresql
synced 2024-10-22 17:05:45 +02:00
Merge pull request #53 from open-sausages/pulls/4.0/namespace-cms
Update for SilverStripe\CMS namespace
This commit is contained in:
commit
7d23f2c97d
@ -2,12 +2,12 @@
|
|||||||
|
|
||||||
namespace SilverStripe\PostgreSQL;
|
namespace SilverStripe\PostgreSQL;
|
||||||
|
|
||||||
|
use Injector;
|
||||||
use SilverStripe\Framework\Core\Configurable;
|
use SilverStripe\Framework\Core\Configurable;
|
||||||
use SilverStripe\ORM\DB;
|
use SilverStripe\ORM\DB;
|
||||||
use SilverStripe\ORM\DataObject;
|
use SilverStripe\ORM\DataObject;
|
||||||
use SilverStripe\ORM\ArrayList;
|
use SilverStripe\ORM\ArrayList;
|
||||||
use SilverStripe\ORM\Connect\SS_Database;
|
use SilverStripe\ORM\Connect\SS_Database;
|
||||||
use Config;
|
|
||||||
use ErrorException;
|
use ErrorException;
|
||||||
use Exception;
|
use Exception;
|
||||||
use PaginatedList;
|
use PaginatedList;
|
||||||
@ -369,18 +369,25 @@ class PostgreSQLDatabase extends SS_Database
|
|||||||
$keywords= str_replace(' ', ' | ', $keywords);
|
$keywords= str_replace(' ', ' | ', $keywords);
|
||||||
$keywords= str_replace('"', "'", $keywords);
|
$keywords= str_replace('"', "'", $keywords);
|
||||||
|
|
||||||
|
|
||||||
$keywords = $this->quoteString(trim($keywords));
|
$keywords = $this->quoteString(trim($keywords));
|
||||||
|
|
||||||
|
// Get tables
|
||||||
|
$tablesToSearch = [];
|
||||||
|
foreach($classesToSearch as $class) {
|
||||||
|
$tablesToSearch[$class] = DataObject::getSchema()->baseDataTable($class);
|
||||||
|
}
|
||||||
|
|
||||||
//We can get a list of all the tsvector columns though this query:
|
//We can get a list of all the tsvector columns though this query:
|
||||||
//We know what tables to search in based on the $classesToSearch variable:
|
//We know what tables to search in based on the $classesToSearch variable:
|
||||||
$classesPlaceholders = DB::placeholders($classesToSearch);
|
$classesPlaceholders = DB::placeholders($classesToSearch);
|
||||||
$result = $this->preparedQuery("
|
$searchableColumns = $this->preparedQuery("
|
||||||
SELECT table_name, column_name, data_type
|
SELECT table_name, column_name, data_type
|
||||||
FROM information_schema.columns
|
FROM information_schema.columns
|
||||||
WHERE data_type='tsvector' AND table_name in ($classesPlaceholders);",
|
WHERE data_type='tsvector' AND table_name in ($classesPlaceholders);",
|
||||||
$classesToSearch
|
array_values($tablesToSearch)
|
||||||
);
|
);
|
||||||
if (!$result->numRecords()) {
|
if (!$searchableColumns->numRecords()) {
|
||||||
throw new Exception('there are no full text columns to search');
|
throw new Exception('there are no full text columns to search');
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -388,10 +395,12 @@ class PostgreSQLDatabase extends SS_Database
|
|||||||
$tableParameters = array();
|
$tableParameters = array();
|
||||||
|
|
||||||
// Make column selection lists
|
// Make column selection lists
|
||||||
|
$pageClass = 'SilverStripe\\CMS\\Model\\SiteTree';
|
||||||
|
$fileClass = 'File';
|
||||||
$select = array(
|
$select = array(
|
||||||
'SiteTree' => array(
|
$pageClass => array(
|
||||||
'"ClassName"',
|
'"ClassName"',
|
||||||
'"SiteTree"."ID"',
|
'"' . $tablesToSearch[$pageClass] . '"."ID"',
|
||||||
'"ParentID"',
|
'"ParentID"',
|
||||||
'"Title"',
|
'"Title"',
|
||||||
'"URLSegment"',
|
'"URLSegment"',
|
||||||
@ -401,9 +410,9 @@ class PostgreSQLDatabase extends SS_Database
|
|||||||
'NULL AS "Name"',
|
'NULL AS "Name"',
|
||||||
'"CanViewType"'
|
'"CanViewType"'
|
||||||
),
|
),
|
||||||
'File' => array(
|
$fileClass => array(
|
||||||
'"ClassName"',
|
'"ClassName"',
|
||||||
'"File"."ID"',
|
'"' . $tablesToSearch[$fileClass] . '"."ID"',
|
||||||
'0 AS "ParentID"',
|
'0 AS "ParentID"',
|
||||||
'"Title"',
|
'"Title"',
|
||||||
'NULL AS "URLSegment"',
|
'NULL AS "URLSegment"',
|
||||||
@ -415,15 +424,18 @@ class PostgreSQLDatabase extends SS_Database
|
|||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
foreach ($result as $row) {
|
foreach ($searchableColumns as $searchableColumn) {
|
||||||
$conditions = array();
|
$conditions = array();
|
||||||
if ($row['table_name'] === 'SiteTree' || $row['table_name'] === 'File') {
|
$tableName = $searchableColumn['table_name'];
|
||||||
|
$columnName = $searchableColumn['column_name'];
|
||||||
|
$className = DataObject::getSchema()->tableClass($tableName);
|
||||||
|
if (DataObject::singleton($className)->db('ShowInSearch')) {
|
||||||
$conditions[] = array('"ShowInSearch"' => 1);
|
$conditions[] = array('"ShowInSearch"' => 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
$method = self::default_fts_search_method();
|
$method = self::default_fts_search_method();
|
||||||
$conditions[] = "\"{$row['table_name']}\".\"{$row['column_name']}\" $method q ";
|
$conditions[] = "\"{$tableName}\".\"{$columnName}\" $method q ";
|
||||||
$query = DataObject::get($row['table_name'], $conditions)->dataQuery()->query();
|
$query = DataObject::get($className, $conditions)->dataQuery()->query();
|
||||||
|
|
||||||
// Could parameterise this, but convention is only to to so for where conditions
|
// Could parameterise this, but convention is only to to so for where conditions
|
||||||
$query->addFrom(array(
|
$query->addFrom(array(
|
||||||
@ -431,7 +443,7 @@ class PostgreSQLDatabase extends SS_Database
|
|||||||
));
|
));
|
||||||
$query->setSelect(array());
|
$query->setSelect(array());
|
||||||
|
|
||||||
foreach ($select[$row['table_name']] as $clause) {
|
foreach ($select[$className] as $clause) {
|
||||||
if (preg_match('/^(.*) +AS +"?([^"]*)"?/i', $clause, $matches)) {
|
if (preg_match('/^(.*) +AS +"?([^"]*)"?/i', $clause, $matches)) {
|
||||||
$query->selectField($matches[1], $matches[2]);
|
$query->selectField($matches[1], $matches[2]);
|
||||||
} else {
|
} else {
|
||||||
@ -439,7 +451,7 @@ class PostgreSQLDatabase extends SS_Database
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$query->selectField("ts_rank(\"{$row['table_name']}\".\"{$row['column_name']}\", q)", 'Relevance');
|
$query->selectField("ts_rank(\"{$tableName}\".\"{$columnName}\", q)", 'Relevance');
|
||||||
$query->setOrderBy(array());
|
$query->setOrderBy(array());
|
||||||
|
|
||||||
//Add this query to the collection
|
//Add this query to the collection
|
||||||
@ -456,17 +468,18 @@ class PostgreSQLDatabase extends SS_Database
|
|||||||
$orderBy='';
|
$orderBy='';
|
||||||
}
|
}
|
||||||
|
|
||||||
$fullQuery = "SELECT * FROM (" . implode(" UNION ", $tables) . ") AS q1 $orderBy LIMIT $limit OFFSET $offset";
|
$fullQuery = "SELECT *, count(*) OVER() as _fullcount FROM (" . implode(" UNION ", $tables) . ") AS q1 $orderBy LIMIT $limit OFFSET $offset";
|
||||||
|
|
||||||
// Get records
|
// Get records
|
||||||
$records = $this->preparedQuery($fullQuery, $tableParameters);
|
$records = $this->preparedQuery($fullQuery, $tableParameters);
|
||||||
$totalCount=0;
|
$totalCount = 0;
|
||||||
|
$objects = [];
|
||||||
foreach ($records as $record) {
|
foreach ($records as $record) {
|
||||||
$objects[] = new $record['ClassName']($record);
|
$objects[] = Injector::inst()->createWithArgs($record['ClassName'], [$record]);
|
||||||
$totalCount++;
|
$totalCount = $record['_fullcount'];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset($objects)) {
|
if ($objects) {
|
||||||
$results = new ArrayList($objects);
|
$results = new ArrayList($objects);
|
||||||
} else {
|
} else {
|
||||||
$results = new ArrayList();
|
$results = new ArrayList();
|
||||||
|
Loading…
Reference in New Issue
Block a user