Skip to content

Commit 97ec884

Browse files
committed
Update MongoDB getIndex and Database::Type
1 parent b252c9b commit 97ec884

File tree

3 files changed

+145
-127
lines changed

3 files changed

+145
-127
lines changed

src/Database/Drivers/MongoDB/Index.php

Lines changed: 3 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22

33
namespace CodexShaper\DBM\Database\Drivers\MongoDB;
44

5+
use CodexShaper\DBM\Database\Drivers\MongoDB\Traits\IndexTrait;
56
use MongoDB\Collection;
67
use MongoDB\Model\IndexInfo;
78

89
class Index
910
{
11+
use IndexTrait;
12+
1013
public static function getIndexes(Collection $collection)
1114
{
1215
$listIndexes = $collection->listIndexes();
@@ -44,104 +47,6 @@ public static function getType(IndexInfo $index)
4447
return $type;
4548
}
4649

47-
protected static function getCommonType(IndexInfo $index)
48-
{
49-
$type = "";
50-
51-
if ($index->isText()) {
52-
$type = "TEXT";
53-
} else if ($index->is2dSphere()) {
54-
$type = "2DSPARSE";
55-
} else if ($index->isTtl()) {
56-
$type = "TTL";
57-
} else if ($index->isGeoHaystack()) {
58-
$type = "GEOHAYSTACK";
59-
}
60-
61-
return $type;
62-
}
63-
64-
protected static function getSpecialType(IndexInfo $index)
65-
{
66-
$type = "";
67-
68-
if (static::checkUnique($index)) {
69-
$type = "UNIQUE";
70-
} else if (static::checkUniqueDesc($index)) {
71-
$type = "UNIQUE_DESC";
72-
} else if (static::checkSparse($index)) {
73-
$type = "SPARSE";
74-
} else if (static::checkSparseUnique($index)) {
75-
$type = "SPARSE_UNIQUE";
76-
} else if (static::checkSparseUniqueDesc($index)) {
77-
$type = "SPARSE_UNIQUE_DESC";
78-
} else if (static::checkSparseDesc($index)) {
79-
$type = "SPARSE_DESC";
80-
}
81-
82-
return $type;
83-
}
84-
85-
protected static function getDefaultType(IndexInfo $index)
86-
{
87-
$name = $index->getName();
88-
$partials = explode("_", $name);
89-
$type = end($partials);
90-
91-
if ($type == 'asc') {
92-
return "ASC";
93-
} else if ($type == 'index') {
94-
return "INDEX";
95-
} else if ($type == 'desc') {
96-
return "DESC";
97-
}
98-
99-
return "";
100-
}
101-
102-
protected static function checkUnique($index)
103-
{
104-
return $index->isUnique() && !$index->isSparse() && !static::checkDescending($index) ? true : false;
105-
}
106-
107-
protected static function checkUniqueDesc($index)
108-
{
109-
return $index->isUnique() && !$index->isSparse() && static::checkDescending($index) ? true : false;
110-
}
111-
112-
protected static function checkSparse($index)
113-
{
114-
return $index->isSparse() && !static::checkDescending($index) ? true : false;
115-
}
116-
117-
protected static function checkSparseUnique($index)
118-
{
119-
return $index->isSparse() && $index->isUnique() && !static::checkDescending($index) ? true : false;
120-
}
121-
122-
protected static function checkSparseUniqueDesc($index)
123-
{
124-
return $index->isSparse() && $index->isUnique() && static::checkDescending($index) ? true : false;
125-
}
126-
127-
protected static function checkSparseDesc($index)
128-
{
129-
return $index->isSparse() && static::checkDescending($index) ? true : false;
130-
}
131-
132-
protected static function checkDescending($index)
133-
{
134-
$keys = $index->getKey();
135-
136-
foreach ($keys as $key => $value) {
137-
if ($value == -1) {
138-
return true;
139-
}
140-
}
141-
142-
return false;
143-
}
144-
14550
public static function setIndexes(Collection $collection, $indexes)
14651
{
14752

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
3+
namespace CodexShaper\DBM\Database\Drivers\MongoDB\Traits;
4+
5+
use MongoDB\Model\IndexInfo;
6+
7+
trait IndexTrait
8+
{
9+
10+
protected static function getCommonType(IndexInfo $index)
11+
{
12+
$type = "";
13+
14+
if ($index->isText()) {
15+
$type = "TEXT";
16+
} else if ($index->is2dSphere()) {
17+
$type = "2DSPARSE";
18+
} else if ($index->isTtl()) {
19+
$type = "TTL";
20+
} else if ($index->isGeoHaystack()) {
21+
$type = "GEOHAYSTACK";
22+
}
23+
24+
return $type;
25+
}
26+
27+
protected static function getSpecialType(IndexInfo $index)
28+
{
29+
if (static::checkUnique($index)) {
30+
return "UNIQUE";
31+
}
32+
33+
if (static::checkUniqueDesc($index)) {
34+
return "UNIQUE_DESC";
35+
}
36+
37+
if (static::checkSparse($index)) {
38+
return "SPARSE";
39+
}
40+
if (static::checkSparseUnique($index)) {
41+
return "SPARSE_UNIQUE";
42+
}
43+
if (static::checkSparseUniqueDesc($index)) {
44+
return "SPARSE_UNIQUE_DESC";
45+
}
46+
47+
if (static::checkSparseDesc($index)) {
48+
return "SPARSE_DESC";
49+
}
50+
51+
return "";
52+
}
53+
54+
protected static function getDefaultType(IndexInfo $index)
55+
{
56+
$name = $index->getName();
57+
$partials = explode("_", $name);
58+
$type = end($partials);
59+
60+
if ($type == 'asc') {
61+
return "ASC";
62+
} else if ($type == 'index') {
63+
return "INDEX";
64+
} else if ($type == 'desc') {
65+
return "DESC";
66+
}
67+
68+
return "";
69+
}
70+
71+
protected static function checkUnique($index)
72+
{
73+
return $index->isUnique() && !$index->isSparse() && !static::checkDescending($index) ? true : false;
74+
}
75+
76+
protected static function checkUniqueDesc($index)
77+
{
78+
return $index->isUnique() && !$index->isSparse() && static::checkDescending($index) ? true : false;
79+
}
80+
81+
protected static function checkSparse($index)
82+
{
83+
return $index->isSparse() && !static::checkDescending($index) ? true : false;
84+
}
85+
86+
protected static function checkSparseUnique($index)
87+
{
88+
return $index->isSparse() && $index->isUnique() && !static::checkDescending($index) ? true : false;
89+
}
90+
91+
protected static function checkSparseUniqueDesc($index)
92+
{
93+
return $index->isSparse() && $index->isUnique() && static::checkDescending($index) ? true : false;
94+
}
95+
96+
protected static function checkSparseDesc($index)
97+
{
98+
return $index->isSparse() && static::checkDescending($index) ? true : false;
99+
}
100+
101+
protected static function checkDescending($index)
102+
{
103+
$keys = $index->getKey();
104+
105+
foreach ($keys as $key => $value) {
106+
if ($value == -1) {
107+
return true;
108+
}
109+
}
110+
111+
return false;
112+
}
113+
}

src/Database/Types/Type.php

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -140,34 +140,34 @@ public static function getTypeCategories()
140140
];
141141
}
142142

143-
public static function getTypeCategory($type)
144-
{
145-
$categories = static::getTypeCategories();
146-
147-
foreach ($categories as $key => $category) {
148-
foreach ($category as $value) {
149-
if ($value == $type) {
150-
switch ($key) {
151-
case 'numbers':
152-
return 'Numbers';
153-
case 'strings':
154-
return 'Strings';
155-
case 'datetime':
156-
return 'Date and Time';
157-
case 'lists':
158-
return 'Lists';
159-
case 'binary':
160-
return 'Binary';
161-
case 'network':
162-
return 'Networks';
163-
case 'geometry':
164-
return 'Geometry';
165-
case 'objects':
166-
return 'Objects';
167-
}
168-
}
169-
}
170-
}
171-
}
143+
// public static function getTypeCategory($type)
144+
// {
145+
// $categories = static::getTypeCategories();
146+
147+
// foreach ($categories as $key => $category) {
148+
// foreach ($category as $value) {
149+
// if ($value == $type) {
150+
// switch ($key) {
151+
// case 'numbers':
152+
// return 'Numbers';
153+
// case 'strings':
154+
// return 'Strings';
155+
// case 'datetime':
156+
// return 'Date and Time';
157+
// case 'lists':
158+
// return 'Lists';
159+
// case 'binary':
160+
// return 'Binary';
161+
// case 'network':
162+
// return 'Networks';
163+
// case 'geometry':
164+
// return 'Geometry';
165+
// case 'objects':
166+
// return 'Objects';
167+
// }
168+
// }
169+
// }
170+
// }
171+
// }
172172

173173
}

0 commit comments

Comments
 (0)