Skip to content

Commit 713349a

Browse files
committed
Update MongoDB Index
1 parent 1ff4bb3 commit 713349a

File tree

1 file changed

+51
-24
lines changed

1 file changed

+51
-24
lines changed

src/Database/Drivers/MongoDB/Index.php

Lines changed: 51 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,46 @@ public static function getIndexes(Collection $collection)
3030
}
3131

3232
public static function getType(IndexInfo $index)
33+
{
34+
$type = static::getCommonType($index);
35+
36+
if (empty($type)) {
37+
$type = static::getSpecialType($index);
38+
}
39+
40+
return $type;
41+
}
42+
43+
protected static function getCommonType(IndexInfo $index)
3344
{
3445
$type = "";
3546

36-
$type = static::getCommonType($index);
47+
if ($index->isText()) {
48+
$type = "TEXT";
49+
}
50+
51+
if ($index->is2dSphere()) {
52+
$type = "2DSPARSE";
53+
}
54+
55+
if ($index->isTtl()) {
56+
$type = "TTL";
57+
}
58+
59+
if ($index->isGeoHaystack()) {
60+
$type = "GEOHAYSTACK";
61+
}
62+
63+
if ($type === "") {
64+
$type = static::getDefaultType($index);
65+
}
66+
67+
return $type;
68+
}
69+
70+
protected static function getSpecialType(IndexInfo $index)
71+
{
72+
$type = "";
3773

3874
if ($index->isUnique() && !$index->isSparse() && !static::checkDescending($index)) {
3975
$type = "UNIQUE";
@@ -62,34 +98,25 @@ public static function getType(IndexInfo $index)
6298
return $type;
6399
}
64100

65-
protected static function getCommonType(IndexInfo $index)
101+
protected static function getDefaultType(IndexInfo $index)
66102
{
67-
if ($index->isText()) {
68-
return "TEXT";
69-
} else if ($index->is2dSphere()) {
70-
return "2DSPARSE";
71-
} else if ($index->isTtl()) {
72-
return "TTL";
73-
} else if ($index->isGeoHaystack()) {
74-
return "GEOHAYSTACK";
75-
} else {
76-
$name = $index->getName();
77-
$partials = explode("_", $name);
78-
$type = end($partials);
79-
if ($type == 'asc') {
80-
return "ASC";
81-
}
103+
$name = $index->getName();
104+
$partials = explode("_", $name);
105+
$type = end($partials);
82106

83-
if ($type == 'index') {
84-
return "INDEX";
85-
}
107+
if ($type == 'asc') {
108+
return "ASC";
109+
}
86110

87-
if ($type == 'desc') {
88-
return "DESC";
89-
}
111+
if ($type == 'index') {
112+
return "INDEX";
113+
}
90114

91-
return "";
115+
if ($type == 'desc') {
116+
return "DESC";
92117
}
118+
119+
return "";
93120
}
94121

95122
protected static function checkDescending($index)

0 commit comments

Comments
 (0)