|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace kalanis\kw_mapper\Storage\Database\Dialects; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use kalanis\kw_mapper\Interfaces\IQueryBuilder; |
|
7
|
|
|
use kalanis\kw_mapper\MapperException; |
|
8
|
|
|
use kalanis\kw_mapper\Storage\Shared\QueryBuilder; |
|
9
|
|
|
use MongoDB\Driver; |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class MongoDb |
|
14
|
|
|
* @package kalanis\kw_mapper\Storage\Database\Dialects |
|
15
|
|
|
* Create queries for MongoDB servers |
|
16
|
|
|
* @codeCoverageIgnore how to test objects instead of strings? |
|
17
|
|
|
*/ |
|
18
|
|
|
class MongoDb extends ADialect |
|
19
|
|
|
{ |
|
20
|
|
|
use TTranslate; |
|
21
|
|
|
|
|
22
|
|
|
public function insert(QueryBuilder $builder) |
|
23
|
|
|
{ |
|
24
|
|
|
$write = new Driver\BulkWrite(); |
|
25
|
|
|
$write->insert($this->propertyArray($builder)); |
|
26
|
|
|
return $write; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function select(QueryBuilder $builder) |
|
30
|
|
|
{ |
|
31
|
|
|
$options = []; |
|
32
|
|
|
if (!empty($builder->getOrdering())) { |
|
33
|
|
|
$options['sort'] = $this->order($builder->getOrdering()); |
|
34
|
|
|
} |
|
35
|
|
|
if (!is_null($builder->getOffset())) { |
|
36
|
|
|
$options['skip'] = $builder->getOffset(); |
|
37
|
|
|
} |
|
38
|
|
|
if (!is_null($builder->getLimit())) { |
|
39
|
|
|
$options['limit'] = $builder->getLimit(); |
|
40
|
|
|
} |
|
41
|
|
|
return new Driver\Query($this->filterArray($builder), $options); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function update(QueryBuilder $builder) |
|
45
|
|
|
{ |
|
46
|
|
|
$write = new Driver\BulkWrite(); |
|
47
|
|
|
$write->update($this->filterArray($builder), $this->propertyArray($builder)); |
|
48
|
|
|
return $write; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function delete(QueryBuilder $builder) |
|
52
|
|
|
{ |
|
53
|
|
|
$write = new Driver\BulkWrite(); |
|
54
|
|
|
$write->delete($this->filterArray($builder)); |
|
55
|
|
|
return $write; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function describe(QueryBuilder $builder) |
|
59
|
|
|
{ |
|
60
|
|
|
return ''; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param QueryBuilder $builder |
|
65
|
|
|
* @return string[]|int[]|float[] |
|
66
|
|
|
*/ |
|
67
|
|
|
public function propertyArray(QueryBuilder $builder): array |
|
68
|
|
|
{ |
|
69
|
|
|
$result = []; |
|
70
|
|
|
$values = $builder->getParams(); |
|
71
|
|
|
foreach ($builder->getProperties() as $column) { |
|
72
|
|
|
$result[$column->getColumnName()] = $values[$column->getColumnKey()]; |
|
73
|
|
|
} |
|
74
|
|
|
return $result; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param QueryBuilder $builder |
|
79
|
|
|
* @throws MapperException |
|
80
|
|
|
* @return array<string, array<array<string, int|string|float|array<int, array<int|string|float>>>>> |
|
81
|
|
|
*/ |
|
82
|
|
|
public function filterArray(QueryBuilder $builder): array |
|
83
|
|
|
{ |
|
84
|
|
|
$result = []; |
|
85
|
|
|
$values = $builder->getParams(); |
|
86
|
|
|
foreach ($builder->getConditions() as $condition) { |
|
87
|
|
|
if ($condition->isRaw()) { |
|
88
|
|
|
$result = array_merge($result, (array) $condition->getRaw()); |
|
89
|
|
|
} else { |
|
90
|
|
|
$result[$condition->getColumnName()] = $this->operation($condition, $values); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
$relation = (IQueryBuilder::RELATION_AND == $builder->getRelation()) ? '$and' : '$or'; |
|
94
|
|
|
return [$relation => $result]; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param QueryBuilder\Order[] $ordering |
|
99
|
|
|
* @return array<string|int, int> |
|
100
|
|
|
*/ |
|
101
|
|
|
public function order(array $ordering): array |
|
102
|
|
|
{ |
|
103
|
|
|
$columns = []; |
|
104
|
|
|
foreach ($ordering as $column) { |
|
105
|
|
|
$columns[$column->getColumnName()] = (IQueryBuilder::ORDER_ASC == $column->getDirection() ? 1 : -1 ); |
|
106
|
|
|
} |
|
107
|
|
|
return $columns; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public function availableJoins(): array |
|
111
|
|
|
{ |
|
112
|
|
|
return []; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @param QueryBuilder\Condition $condition |
|
117
|
|
|
* @param array<int|string> $values |
|
118
|
|
|
* @throws MapperException |
|
119
|
|
|
* @return array<string, int|string|float|array<int, array<int|string|float>>> |
|
120
|
|
|
*/ |
|
121
|
|
|
protected function operation(QueryBuilder\Condition $condition, array &$values): array |
|
122
|
|
|
{ |
|
123
|
|
|
$columnKey = strval($condition->getColumnKey()); |
|
124
|
|
|
switch ($condition->getOperation()) { |
|
125
|
|
|
// case IQueryBuilder::OPERATION_NULL: |
|
126
|
|
|
// return 'IS NULL'; |
|
127
|
|
|
// case IQueryBuilder::OPERATION_NNULL: |
|
128
|
|
|
// return 'IS NOT NULL'; |
|
129
|
|
|
case IQueryBuilder::OPERATION_EQ: |
|
130
|
|
|
return ['$eq' => $values[$columnKey]]; |
|
131
|
|
|
case IQueryBuilder::OPERATION_NEQ: |
|
132
|
|
|
return ['$ne' => $values[$columnKey]]; |
|
133
|
|
|
case IQueryBuilder::OPERATION_GT: |
|
134
|
|
|
return ['$gt' => $values[$columnKey]]; |
|
135
|
|
|
case IQueryBuilder::OPERATION_GTE: |
|
136
|
|
|
return ['$gte' => $values[$columnKey]]; |
|
137
|
|
|
case IQueryBuilder::OPERATION_LT: |
|
138
|
|
|
return ['$le' => $values[$columnKey]]; |
|
139
|
|
|
case IQueryBuilder::OPERATION_LTE: |
|
140
|
|
|
return ['$lte' => $values[$columnKey]]; |
|
141
|
|
|
// case IQueryBuilder::OPERATION_LIKE: |
|
142
|
|
|
// return 'LIKE'; |
|
143
|
|
|
// case IQueryBuilder::OPERATION_NLIKE: |
|
144
|
|
|
// return 'NOT LIKE'; |
|
145
|
|
|
case IQueryBuilder::OPERATION_REXP: |
|
146
|
|
|
return ['$regex' => $values[$columnKey]]; |
|
147
|
|
|
case IQueryBuilder::OPERATION_IN: |
|
148
|
|
|
return ['$in' => [$this->notEmptyArray($values[$columnKey])]]; |
|
149
|
|
|
case IQueryBuilder::OPERATION_NIN: |
|
150
|
|
|
return ['$nin' => [$this->notEmptyArray($values[$columnKey])]]; |
|
151
|
|
|
default: |
|
152
|
|
|
throw new MapperException(sprintf('Unknown operation *%s*', $condition->getOperation())); |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|