Completed
Push — master ( 579af5...b29473 )
by Oleg
07:53
created

MongoConnection::switchDatabase()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php /** MongoDbConnectionMicro */
2
3
namespace Micro\Db;
4
5
use Micro\Base\Exception;
6
7
/**
8
 * MongoDB Connection class file.
9
 *
10
 * @author Oleg Lunegov <[email protected]>
11
 * @link https://github.com/lugnsk/micro
12
 * @copyright Copyright &copy; 2013 Oleg Lunegov
13
 * @license /LICENSE
14
 * @package Micro
15
 * @subpackage Db
16
 * @version 1.0
17
 * @since 1.0
18
 */
19
class MongoConnection extends Connection
20
{
21
    /** @var \MongoClient $conn Connection to MongoDB */
22
    public $conn;
23
    /** @var array $collection lazy load collections */
24
    protected $collections = [];
25
    /** @var string $dbName Database name */
26
    private $dbName;
27
28
29
    /**
30
     * Construct MongoDB
31
     *
32
     * @access public
33
     *
34
     * @param array $config configuration array
35
     *
36
     * @result void
37
     * @throws \MongoConnectionException
38
     * @throws \Micro\Base\Exception
39
     */
40
    public function __construct(array $config = [])
41
    {
42
        parent::__construct($config);
43
44
        if (!empty($config['dbname'])) {
45
            $this->dbName = $config['dbname'];
46
        } else {
47
            throw new Exception('MongoDB database name not defined!');
48
        }
49
50
        try {
51
            if (!empty($config['connectionString'])) {
52
                $this->conn = new \MongoClient($config['connectionString'], $config['options']);
53
            } else {
54
                $this->conn = new \MongoClient;
55
            }
56
        } catch (Exception $e) {
57
            if (!$config['ignoreFail']) {
58
                throw new Exception('MongoDB error connect to database');
59
            }
60
        }
61
    }
62
63
    /**
64
     * Destruct MongoDB client
65
     *
66
     * @access public
67
     * @return void
68
     */
69
    public function __destruct()
70
    {
71
        $this->conn = null;
72
    }
73
74
    /**
75
     * @inheritdoc
76
     */
77
    public function rawQuery($query = '', array $params = [], $fetchType = \PDO::FETCH_ASSOC, $fetchClass = 'Model')
78
    {
79
        // TODO: Implement rawQuery() method.
80
    }
81
82
    /**
83
     * @inheritdoc
84
     */
85
    public function listDatabases()
86
    {
87
        return $this->conn->listDBs();
88
    }
89
90
    /**
91
     * @inheritdoc
92
     */
93
    public function infoDatabase($dbName)
94
    {
95
        // TODO: Implement infoDatabase() method.
96
    }
97
98
    /**
99
     * @inheritdoc
100
     */
101
    public function tableExists($table)
102
    {
103
        return (bool)array_search($table, $this->listTables(), true);
104
    }
105
106
    /**
107
     * @inheritdoc
108
     */
109
    public function listTables()
110
    {
111
        $this->conn->{$this->dbName}->listCollections();
112
    }
113
114
    /**
115
     * @inheritdoc
116
     */
117
    public function createTable($name, array $elements = [], $params = '')
118
    {
119
        $this->conn->{$this->dbName}->createCollection($name, $params);
120
    }
121
122
    /**
123
     * @inheritdoc
124
     */
125
    public function clearTable($name)
126
    {
127
        $this->removeTable($name);
128
    }
129
130
    /**
131
     * @inheritdoc
132
     */
133
    public function removeTable($name)
134
    {
135
        $this->conn->$name->drop();
136
    }
137
138
    /**
139
     * @inheritdoc
140
     */
141
    public function fieldExists($field, $table)
142
    {
143
        // TODO: Implement fieldExists() method.
144
    }
145
146
    /**
147
     * @inheritdoc
148
     */
149
    public function listFields($table)
150
    {
151
        // TODO: Implement listFields() method.
152
    }
153
154
    /**
155
     * @inheritdoc
156
     */
157
    public function fieldInfo($field, $table)
158
    {
159
        // TODO: Implement fieldInfo() method.
160
    }
161
162
    /**
163
     * @inheritdoc
164
     */
165
    public function switchDatabase($dbName)
166
    {
167
        $this->conn->selectDB($dbName);
168
        $this->dbName = $dbName;
169
    }
170
171
    /**
172
     * @inheritdoc
173
     */
174
    public function insert($table, array $line = [], $multi = false)
175
    {
176
        // TODO: Implement insert() method.
177
    }
178
179
    /**
180
     * @inheritdoc
181
     */
182
    public function update($table, array $elements = [], $conditions = '')
183
    {
184
        // TODO: Implement update() method.
185
    }
186
187
    /**
188
     * @inheritdoc
189
     */
190
    public function delete($table, $conditions, array $ph = [])
191
    {
192
        // TODO: Implement delete() method.
193
    }
194
195
    /**
196
     * @inheritdoc
197
     */
198
    public function exists($table, array $params = [])
199
    {
200
        // TODO: Implement exists() method.
201
    }
202
203
    /**
204
     * @inheritdoc
205
     */
206
    public function count($subQuery = '', $table = '')
207
    {
208
        // TODO: Implement count() method.
209
    }
210
}
211