MongoDB: Handle exceptions
This commit is contained in:
parent
a023e98c4d
commit
513e1f425f
|
@ -13,9 +13,28 @@ if (isset($_GET["mongo"])) {
|
||||||
|
|
||||||
function connect($uri, $options) {
|
function connect($uri, $options) {
|
||||||
$class = 'MongoDB\Driver\Manager';
|
$class = 'MongoDB\Driver\Manager';
|
||||||
$return = new $class($uri, $options);
|
$this->_link = new $class($uri, $options);
|
||||||
execute_command($return, 'admin', array('ping' => 1));
|
$this->executeCommand('admin', array('ping' => 1));
|
||||||
return $return;
|
}
|
||||||
|
|
||||||
|
function executeCommand($db, $command) {
|
||||||
|
$class = 'MongoDB\Driver\Command';
|
||||||
|
try {
|
||||||
|
return $this->_link->executeCommand($db, new $class($command));
|
||||||
|
} catch (Exception $e) {
|
||||||
|
$this->error = $e->getMessage();
|
||||||
|
return array();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function executeQuery($db, $command) {
|
||||||
|
$class = 'MongoDB\Driver\Query';
|
||||||
|
try {
|
||||||
|
return $this->_link->executeQuery($db, new $class($command));
|
||||||
|
} catch (Exception $e) {
|
||||||
|
$this->error = $e->getMessage();
|
||||||
|
return array();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function query($query) {
|
function query($query) {
|
||||||
|
@ -117,9 +136,7 @@ if (isset($_GET["mongo"])) {
|
||||||
}
|
}
|
||||||
$limit = min(200, max(1, (int) $limit));
|
$limit = min(200, max(1, (int) $limit));
|
||||||
$skip = $page * $limit;
|
$skip = $page * $limit;
|
||||||
$class = 'MongoDB\Driver\Query';
|
$results = $connection->executeQuery("$connection->_db_name.$table", array('projection' => $select, 'limit' => $limit, 'skip' => $skip, 'sort' => $sort));
|
||||||
$query = new $class($where, array('projection' => $select, 'limit' => $limit, 'skip' => $skip, 'sort' => $sort));
|
|
||||||
$results = $connection->_link->executeQuery("$connection->_db_name.$table", $query);
|
|
||||||
return new Min_Result($results);
|
return new Min_Result($results);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -176,16 +193,11 @@ if (isset($_GET["mongo"])) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function execute_command($link, $db, $command) {
|
|
||||||
$class = 'MongoDB\Driver\Command';
|
|
||||||
return $link->executeCommand($db, new $class($command));
|
|
||||||
}
|
|
||||||
|
|
||||||
function get_databases($flush) {
|
function get_databases($flush) {
|
||||||
/** @var Min_DB */
|
/** @var Min_DB */
|
||||||
global $connection;
|
global $connection;
|
||||||
$return = array();
|
$return = array();
|
||||||
foreach (execute_command($connection->_link, 'admin', array('listDatabases' => 1)) as $dbs) {
|
foreach ($connection->executeCommand('admin', array('listDatabases' => 1)) as $dbs) {
|
||||||
foreach ($dbs->databases as $db) {
|
foreach ($dbs->databases as $db) {
|
||||||
$return[] = $db->name;
|
$return[] = $db->name;
|
||||||
}
|
}
|
||||||
|
@ -201,7 +213,7 @@ if (isset($_GET["mongo"])) {
|
||||||
function tables_list() {
|
function tables_list() {
|
||||||
global $connection;
|
global $connection;
|
||||||
$collections = array();
|
$collections = array();
|
||||||
foreach (execute_command($connection->_link, $connection->_db_name, array('listCollections' => 1)) as $result) {
|
foreach ($connection->executeCommand($connection->_db_name, array('listCollections' => 1)) as $result) {
|
||||||
$collections[$result->name] = 'table';
|
$collections[$result->name] = 'table';
|
||||||
}
|
}
|
||||||
return $collections;
|
return $collections;
|
||||||
|
@ -214,7 +226,7 @@ if (isset($_GET["mongo"])) {
|
||||||
function indexes($table, $connection2 = null) {
|
function indexes($table, $connection2 = null) {
|
||||||
global $connection;
|
global $connection;
|
||||||
$return = array();
|
$return = array();
|
||||||
foreach (execute_command($connection->_link, $connection->_db_name, array('listIndexes' => $table)) as $index) {
|
foreach ($connection->executeCommand($connection->_db_name, array('listIndexes' => $table)) as $index) {
|
||||||
$descs = array();
|
$descs = array();
|
||||||
$columns = array();
|
$columns = array();
|
||||||
foreach (get_object_vars($index->key) as $column => $type) {
|
foreach (get_object_vars($index->key) as $column => $type) {
|
||||||
|
@ -259,7 +271,7 @@ if (isset($_GET["mongo"])) {
|
||||||
function found_rows($table_status, $where) {
|
function found_rows($table_status, $where) {
|
||||||
global $connection;
|
global $connection;
|
||||||
$where = where_to_query($where);
|
$where = where_to_query($where);
|
||||||
$toArray = execute_command($connection->_link, $connection->_db_name, array('count' => $table_status['Name'], 'query' => $where))->toArray();
|
$toArray = $connection->executeCommand($connection->_db_name, array('count' => $table_status['Name'], 'query' => $where))->toArray();
|
||||||
return $toArray[0]->n;
|
return $toArray[0]->n;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -362,12 +374,17 @@ if (isset($_GET["mongo"])) {
|
||||||
"(date)>=",
|
"(date)>=",
|
||||||
"(date)<=",
|
"(date)<=",
|
||||||
);
|
);
|
||||||
|
|
||||||
} elseif (class_exists('MongoDB')) {
|
} elseif (class_exists('MongoDB')) {
|
||||||
class Min_DB {
|
class Min_DB {
|
||||||
var $extension = "Mongo", $server_info = MongoClient::VERSION, $error, $last_id, $_link, $_db;
|
var $extension = "Mongo", $server_info = MongoClient::VERSION, $error, $last_id, $_link, $_db;
|
||||||
|
|
||||||
function connect($uri, $options) {
|
function connect($uri, $options) {
|
||||||
return @new MongoClient($uri, $options);
|
try {
|
||||||
|
return new MongoClient($uri, $options);
|
||||||
|
} catch (Exception $e) {
|
||||||
|
$this->error = $e->getMessage();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function query($query) {
|
function query($query) {
|
||||||
|
@ -613,21 +630,19 @@ if (isset($_GET["mongo"])) {
|
||||||
if (($auth_source = getenv("MONGO_AUTH_SOURCE"))) {
|
if (($auth_source = getenv("MONGO_AUTH_SOURCE"))) {
|
||||||
$options["authSource"] = $auth_source;
|
$options["authSource"] = $auth_source;
|
||||||
}
|
}
|
||||||
try {
|
$connection->connect("mongodb://$server", $options);
|
||||||
$connection->_link = $connection->connect("mongodb://$server", $options);
|
if ($connection->error) {
|
||||||
if ($password != "") {
|
return $connection->error;
|
||||||
$options["password"] = "";
|
|
||||||
try {
|
|
||||||
$connection->connect("mongodb://$server", $options);
|
|
||||||
return lang('Database does not support password.');
|
|
||||||
} catch (Exception $ex) {
|
|
||||||
// this is what we want
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return $connection;
|
|
||||||
} catch (Exception $ex) {
|
|
||||||
return $ex->getMessage();
|
|
||||||
}
|
}
|
||||||
|
if ($password != "") {
|
||||||
|
$options["password"] = "";
|
||||||
|
$connection2 = new Min_DB;
|
||||||
|
$connection2->connect("mongodb://$server", $options);
|
||||||
|
if (!$connection2->error) {
|
||||||
|
return lang('Database does not support password.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $connection;
|
||||||
}
|
}
|
||||||
|
|
||||||
function alter_indexes($table, $alter) {
|
function alter_indexes($table, $alter) {
|
||||||
|
|
Loading…
Reference in a new issue