forked from ProcessMaker/processmaker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIndexable.php
More file actions
82 lines (67 loc) · 2.22 KB
/
Indexable.php
File metadata and controls
82 lines (67 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
namespace ProcessMaker\Traits;
use DomainException;
use Facades\ProcessMaker\JsonColumnIndex;
trait Indexable
{
public function getFieldsFromPmql($pmql)
{
return ExtendedPMQL::getFields($pmql);
}
public function addJsonColumnsIndex($fields)
{
foreach ($fields as $field) {
$table = $this->getTableToLook($this->table, $this->type, $this->meta);
[$column, $path] = $this->getColumnAndPath($field);
if ($column && $path) {
\Log::info('Updating JSON path: ' . $path . ' indexes for table: ' . $table);
JsonColumnIndex::add($table, $column, $path);
}
}
}
public function addJsonColumnsIndexBatch($indexes)
{
foreach ($indexes as $index) {
[$column, $path] = $this->getColumnAndPath($index['field']);
JsonColumnIndex::add($index['table'], $column, $path);
}
}
public function deleteJsonColumnsIndexBatch($indexes)
{
foreach ($indexes as $index) {
[$column, $path] = $this->getColumnAndPath($index['field']);
JsonColumnIndex::remove($index['table'], $column, $path);
}
}
private function getColumnAndPath($field)
{
$exploded = explode('.', $field);
// Only columns in format data.field
if (count($exploded) > 1) {
$column = $exploded[0];
array_shift($exploded);
$path = '$.' . implode('.', $exploded);
return [$column, $path];
}
return null;
}
public function getTableToLook($table, $type, $meta)
{
if ($table !== 'saved_searches') {
return $table;
}
// If table is saved search, return the corresponding table
switch ($type) {
case 'request':
case 'task':
return 'process_requests';
case 'collection':
if ($meta && isset($meta->collection_id)) {
$collectionId = $meta->collection_id;
return 'collection_' . $collectionId;
}
default:
throw new DomainException('Unknown saved search type: ' . $type);
}
}
}