-
Notifications
You must be signed in to change notification settings - Fork 11
/
MongoDataConnection.php
155 lines (128 loc) · 3.54 KB
/
MongoDataConnection.php
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
namespace MABI;
include_once __DIR__ . '/DataConnection.php';
/**
* todo: docs
*/
class MongoDataConnection implements DataConnection {
/**
* @var \MongoDB
*/
protected $db = NULL;
/**
* @return \MongoDB
*/
public function getDb() {
return $this->db;
}
/**
* todo: docs
*
* @param $connectionURI string
*
* @return MongoDataConnection
*/
public static function create($connectionURI) {
$connection = new MongoDataConnection();
$database = parse_url($connectionURI, PHP_URL_PATH);
$database = ltrim($database, '/');
$mongo = new \MongoClient($connectionURI);
$connection->db = $mongo->selectDB($database);
return $connection;
}
public function getDefaultIdColumn() {
return '_id';
}
function getNewId() {
$newId = new \MongoId();
return $newId->__toString();
}
function convertToNativeId($stringId) {
return new \MongoId($stringId);
}
function convertFromNativeId($nativeId) {
if (is_object($nativeId) && get_class($nativeId) == 'MongoId') {
return $nativeId->__toString();
}
return $nativeId;
}
public function findAll($table) {
$return = $this->db->selectCollection($table)->find();
$mongodata = array();
while ($return->hasNext()) {
$return->getNext();
$mongodata[] = $return->current();
}
return $mongodata;
}
public function insert($table, $data) {
$this->db->selectCollection($table)->insert($data);
return $data;
}
function save($table, $data, $field, $value) {
$this->db->selectCollection($table)->update(array($field => $value), $data);
}
public function clearAll($table) {
$this->db->selectCollection($table)->drop();
}
public function findOneByField($field, $value, $table, array $fields = array()) {
$result = $this->db->selectCollection($table)->findOne(array($field => $value), $fields);
if (empty($result)) {
return NULL;
}
return $result;
}
function findAllByField($field, $value, $table, array $fields = array()) {
$return = $this->db->selectCollection($table)->find(array($field => $value), $fields);
$mongodata = array();
while ($return->hasNext()) {
$return->getNext();
$mongodata[] = $return->current();
}
return $mongodata;
}
function query($table, $query) {
if (isset($query['group'])) {
if (empty($query['group']['condition'])) {
$return = $this->db->selectCollection($table)->group(
$query['group']['_id'],
$query['group']['initial'],
$query['group']['reduce']);
} else {
$return = $this->db->selectCollection($table)->group(
$query['group']['_id'],
$query['group']['initial'],
$query['group']['reduce'],
$query['group']['condition']);
}
return $return;
}
$mquery = $query['query'];
$return = $this->db->selectCollection($table)->find($mquery);
if (!empty($query['skip'])) {
$return = $return->skip($query['skip']);
}
if (!empty($query['limit'])) {
$return = $return->limit($query['limit']);
}
if (!empty($query['sort'])) {
$return = $return->sort($query['sort']);
}
$mongodata = array();
while ($return->hasNext()) {
$return->getNext();
$mongodata[] = $return->current();
}
return $mongodata;
}
/**
* todo: docs
*
* @param $field
* @param $value
* @param $table
*/
function deleteByField($field, $value, $table) {
$this->db->selectCollection($table)->remove(array($field => $value));
}
}