コード例 #1
0
 /**
  * @dataProvider exceptionProvider
  */
 public function testConvertException($e, $expectedClass)
 {
     $exception = ExceptionConverter::toLegacy($e);
     $this->assertInstanceOf($expectedClass, $exception);
     $this->assertSame($e->getMessage(), $exception->getMessage());
     $this->assertSame($e->getCode(), $exception->getCode());
     $this->assertSame($e, $exception->getPrevious());
 }
コード例 #2
0
 /**
  * Saves an object to this collection
  *
  * @link http://www.php.net/manual/en/mongocollection.save.php
  * @param array|object $a Array to save. If an object is used, it may not have protected or private properties.
  * @param array $options Options for the save.
  * @throws MongoException if the inserted document is empty or if it contains zero-length keys. Attempting to insert an object with protected and private properties will cause a zero-length key error.
  * @throws MongoCursorException if the "w" option is set and the write fails.
  * @throws MongoCursorTimeoutException if the "w" option is set to a value greater than one and the operation takes longer than MongoCursor::$timeout milliseconds to complete. This does not kill the operation on the server, it is a client-side timeout. The operation in MongoCollection::$wtimeout is milliseconds.
  * @return array|boolean If w was set, returns an array containing the status of the save.
  * Otherwise, returns a boolean representing if the array was not empty (an empty array will not be inserted).
  */
 public function save(&$a, array $options = [])
 {
     $id = $this->ensureDocumentHasMongoId($a);
     $document = (array) $a;
     $options['upsert'] = true;
     try {
         /** @var \MongoDB\UpdateResult $result */
         $result = $this->collection->replaceOne(TypeConverter::fromLegacy(['_id' => $id]), TypeConverter::fromLegacy($document), $this->convertWriteConcernOptions($options));
     } catch (\MongoDB\Driver\Exception\Exception $e) {
         ExceptionConverter::toLegacy($e);
     }
     if (!$result->isAcknowledged()) {
         return true;
     }
     return ['ok' => 1.0, 'nModified' => $result->getModifiedCount(), 'n' => $result->getMatchedCount(), 'err' => null, 'errmsg' => null, 'updatedExisting' => $result->getUpsertedCount() == 0];
 }
コード例 #3
0
ファイル: MongoDB.php プロジェクト: alcaeus/mongo-php-adapter
 /**
  * Execute a database command
  *
  * @link http://www.php.net/manual/en/mongodb.command.php
  * @param array $data The query to send.
  * @param array $options
  * @return array Returns database response.
  */
 public function command(array $data, $options = [], &$hash = null)
 {
     try {
         $cursor = new \MongoCommandCursor($this->connection, $this->name, $data);
         $cursor->setReadPreference($this->getReadPreference());
         return iterator_to_array($cursor)[0];
     } catch (\MongoDB\Driver\Exception\Exception $e) {
         return ExceptionConverter::toResultArray($e);
     }
 }
コード例 #4
0
 /**
  * Execute a database command
  *
  * @link http://www.php.net/manual/en/mongodb.command.php
  * @param array $data The query to send.
  * @param array $options
  * @return array Returns database response.
  */
 public function command(array $data, $options = [], &$hash = null)
 {
     try {
         $cursor = new \MongoCommandCursor($this->connection, $this->name, $data);
         $cursor->setReadPreference($this->getReadPreference());
         return iterator_to_array($cursor)[0];
     } catch (\MongoDB\Driver\Exception\ExecutionTimeoutException $e) {
         throw new MongoCursorTimeoutException($e->getMessage(), $e->getCode(), $e);
     } catch (\MongoDB\Driver\Exception\RuntimeException $e) {
         return ['ok' => 0, 'errmsg' => $e->getMessage(), 'code' => $e->getCode()];
     } catch (\MongoDB\Driver\Exception\Excepiton $e) {
         ExceptionConverter::toLegacy($e);
     }
 }
コード例 #5
0
 /**
  * Execute the query
  * @link http://www.php.net/manual/en/mongocursor.doquery.php
  * @throws MongoConnectionException if it cannot reach the database.
  * @return void
  */
 protected function doQuery()
 {
     $options = $this->getOptions() + $this->options;
     try {
         $this->cursor = $this->collection->find(TypeConverter::fromLegacy($this->query), $options);
     } catch (\MongoDB\Driver\Exception\ExecutionTimeoutException $e) {
         throw new MongoCursorTimeoutException($e->getMessage(), $e->getCode(), $e);
     } catch (\MongoDB\Driver\Exception\Exception $e) {
         throw ExceptionConverter::toLegacy($e);
     }
 }
コード例 #6
0
 /**
  * Lists all of the databases available
  *
  * @link http://php.net/manual/en/mongoclient.listdbs.php
  * @return array Returns an associative array containing three fields. The first field is databases, which in turn contains an array. Each element of the array is an associative array corresponding to a database, giving the database's name, size, and if it's empty. The other two fields are totalSize (in bytes) and ok, which is 1 if this method ran successfully.
  */
 public function listDBs()
 {
     try {
         $databaseInfoIterator = $this->client->listDatabases();
     } catch (\MongoDB\Driver\Exception\Exception $e) {
         throw ExceptionConverter::toLegacy($e);
     }
     $databases = ['databases' => [], 'totalSize' => 0, 'ok' => 1.0];
     foreach ($databaseInfoIterator as $databaseInfo) {
         $databases['databases'][] = ['name' => $databaseInfo->getName(), 'empty' => $databaseInfo->isEmpty(), 'sizeOnDisk' => $databaseInfo->getSizeOnDisk()];
         $databases['totalSize'] += $databaseInfo->getSizeOnDisk();
     }
     return $databases;
 }