/**
  * Execute the operation.
  *
  * @see Executable::execute()
  * @param Server $server
  * @return array|object Command result document
  */
 public function execute(Server $server)
 {
     $cursor = $server->executeCommand($this->databaseName, new Command(['dropDatabase' => 1]));
     if (isset($this->options['typeMap'])) {
         $cursor->setTypeMap($this->options['typeMap']);
     }
     return current($cursor->toArray());
 }
 /**
  * Execute the operation.
  *
  * @see Executable::execute()
  * @param Server $server
  * @return integer
  */
 public function execute(Server $server)
 {
     $readPreference = isset($this->options['readPreference']) ? $this->options['readPreference'] : null;
     $cursor = $server->executeCommand($this->databaseName, $this->command, $readPreference);
     if (isset($this->options['typeMap'])) {
         $cursor->setTypeMap($this->options['typeMap']);
     }
     return $cursor;
 }
Example #3
0
 /**
  * Execute the operation.
  *
  * @see Executable::execute()
  * @param Server $server
  * @return array|object Command result document
  */
 public function execute(Server $server)
 {
     $cmd = ['dropIndexes' => $this->collectionName, 'index' => $this->indexName];
     $cursor = $server->executeCommand($this->databaseName, new Command($cmd));
     if (isset($this->options['typeMap'])) {
         $cursor->setTypeMap($this->options['typeMap']);
     }
     return current($cursor->toArray());
 }
Example #4
0
 /**
  * Create one or more indexes for the collection using the createIndexes
  * command.
  *
  * @param Server $server
  */
 private function executeCommand(Server $server)
 {
     $command = new Command(['createIndexes' => $this->collectionName, 'indexes' => $this->indexes]);
     $cursor = $server->executeCommand($this->databaseName, $command);
     $result = current($cursor->toArray());
     if (empty($result->ok)) {
         throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error');
     }
 }
 /**
  * Execute the operation.
  *
  * @see Executable::execute()
  * @param Server $server
  * @return object Command result document
  */
 public function execute(Server $server)
 {
     $cursor = $server->executeCommand($this->databaseName, new Command(array('drop' => $this->collectionName)));
     $result = current($cursor->toArray());
     if (empty($result->ok)) {
         throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error');
     }
     return $result;
 }
Example #6
0
 /**
  * Execute the operation.
  *
  * @see Executable::execute()
  *
  * @param Server $server
  *
  * @return mixed[]
  * @throws UnexpectedValueException if the command response was malformed
  */
 public function execute(Server $server)
 {
     $readPreference = isset($this->options['readPreference']) ? $this->options['readPreference'] : null;
     $cursor = $server->executeCommand($this->databaseName, $this->createCommand($server), $readPreference);
     $result = current($cursor->toArray());
     if (!isset($result->values) || !is_array($result->values)) {
         throw new UnexpectedValueException('distinct command did not return a "values" array');
     }
     return $result->values;
 }
 /**
  * Returns information for all indexes for this collection using the
  * listIndexes command.
  *
  * @param Server $server
  * @return IndexInfoIteratorIterator
  */
 private function executeCommand(Server $server)
 {
     $cmd = array('listIndexes' => $this->collectionName);
     if (isset($this->options['maxTimeMS'])) {
         $cmd['maxTimeMS'] = $this->options['maxTimeMS'];
     }
     $cursor = $server->executeCommand($this->databaseName, new Command($cmd));
     $cursor->setTypeMap(array('root' => 'array', 'document' => 'array'));
     return new IndexInfoIteratorIterator($cursor);
 }
Example #8
0
 /**
  * Execute the operation.
  *
  * @see Executable::execute()
  * @param Server $server
  * @return object
  */
 public function execute(Server $server)
 {
     $command = new Command(['group' => ['ns' => $this->collectionName, 'key' => $this->keys, 'initial' => $this->initial, '$reduce' => $this->reduce]]);
     $cursor = $server->executeCommand($this->databaseName, $command);
     // Get first element of iterator
     foreach ($cursor as $result) {
         break;
     }
     return isset($result) ? $result : null;
 }
 /**
  * Execute the operation.
  *
  * @see Executable::execute()
  * @param Server $server
  * @return mixed[]
  */
 public function execute(Server $server)
 {
     $cursor = $server->executeCommand($this->databaseName, $this->createCommand());
     $result = current($cursor->toArray());
     if (empty($result->ok)) {
         throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error');
     }
     if (!isset($result->values) || !is_array($result->values)) {
         throw new UnexpectedValueException('distinct command did not return a "values" array');
     }
     return $result->values;
 }
Example #10
0
 /**
  * Returns information for all collections in this database using the
  * listCollections command.
  *
  * @param Server $server
  *
  * @return CollectionInfoCommandIterator
  */
 private function executeCommand(Server $server)
 {
     $cmd = ['listCollections' => 1];
     if (!empty($this->options['filter'])) {
         $cmd['filter'] = (object) $this->options['filter'];
     }
     if (isset($this->options['maxTimeMS'])) {
         $cmd['maxTimeMS'] = $this->options['maxTimeMS'];
     }
     $cursor = $server->executeCommand($this->databaseName, new Command($cmd));
     $cursor->setTypeMap(['root' => 'array', 'document' => 'array']);
     return new CollectionInfoCommandIterator($cursor);
 }
 /**
  * Execute the operation.
  *
  * @see Executable::execute()
  * @param Server $server
  * @return integer
  */
 public function execute(Server $server)
 {
     $cursor = $server->executeCommand($this->databaseName, $this->createCommand());
     $result = current($cursor->toArray());
     if (empty($result->ok)) {
         throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error');
     }
     // Older server versions may return a float
     if (!isset($result->n) || !(is_integer($result->n) || is_float($result->n))) {
         throw new UnexpectedValueException('count command did not return a numeric "n" value');
     }
     return (int) $result->n;
 }
 /**
  * Execute the operation.
  *
  * @see Executable::execute()
  * @param Server $server
  * @return object Command result document
  */
 public function execute(Server $server)
 {
     try {
         $cursor = $server->executeCommand($this->databaseName, new Command(['drop' => $this->collectionName]));
     } catch (RuntimeException $e) {
         /* The server may return an error if the collection does not exist.
          * Check for an error message (unfortunately, there isn't a code)
          * and NOP instead of throwing.
          */
         if ($e->getMessage() === self::$errorMessageNamespaceNotFound) {
             return (object) ['ok' => 0, 'errmsg' => self::$errorMessageNamespaceNotFound];
         }
         throw $e;
     }
     return current($cursor->toArray());
 }
 /**
  * Execute the operation.
  *
  * @see Executable::execute()
  * @param Server $server
  * @return object Command result document
  */
 public function execute(Server $server)
 {
     try {
         $cursor = $server->executeCommand($this->databaseName, new Command(array('drop' => $this->collectionName)));
     } catch (DriverRuntimeException $e) {
         /* The server may return an error if the collection does not exist.
          * Check for an error message (unfortunately, there isn't a code)
          * and NOP instead of throwing.
          */
         if ($e->getMessage() === self::$errorMessageNamespaceNotFound) {
             return (object) ['ok' => 0, 'errmsg' => 'ns not found'];
         }
         throw $e;
     }
     $result = current($cursor->toArray());
     if (empty($result->ok)) {
         throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error');
     }
     return $result;
 }
Example #14
0
 /**
  * Execute the operation.
  *
  * @see Executable::execute()
  *
  * @param Server $server
  *
  * @return DatabaseInfoIterator
  * @throws UnexpectedValueException if the command response was malformed
  */
 public function execute(Server $server)
 {
     $cmd = ['listDatabases' => 1];
     if (isset($this->options['maxTimeMS'])) {
         $cmd['maxTimeMS'] = $this->options['maxTimeMS'];
     }
     $cursor = $server->executeCommand('admin', new Command($cmd));
     $cursor->setTypeMap(['root' => 'array', 'document' => 'array']);
     $result = current($cursor->toArray());
     if (!isset($result['databases']) || !is_array($result['databases'])) {
         throw new UnexpectedValueException('listDatabases command did not return a "databases" array');
     }
     /* Return an Iterator instead of an array in case listDatabases is
      * eventually changed to return a command cursor, like the collection
      * and index enumeration commands. This makes the "totalSize" command
      * field inaccessible, but users can manually invoke the command if they
      * need that value.
      */
     return new DatabaseInfoLegacyIterator($result['databases']);
 }
 /**
  * Returns information for all indexes for this collection using the
  * listIndexes command.
  *
  * @param Server $server
  * @return IndexInfoIteratorIterator
  */
 private function executeCommand(Server $server)
 {
     $cmd = ['listIndexes' => $this->collectionName];
     if (isset($this->options['maxTimeMS'])) {
         $cmd['maxTimeMS'] = $this->options['maxTimeMS'];
     }
     try {
         $cursor = $server->executeCommand($this->databaseName, new Command($cmd));
     } catch (RuntimeException $e) {
         /* The server may return an error if the collection does not exist.
          * Check for possible error codes (see: SERVER-20463) and return an
          * empty iterator instead of throwing.
          */
         if ($e->getCode() === self::$errorCodeNamespaceNotFound || $e->getCode() === self::$errorCodeDatabaseNotFound) {
             return new IndexInfoIteratorIterator(new EmptyIterator());
         }
         throw $e;
     }
     $cursor->setTypeMap(['root' => 'array', 'document' => 'array']);
     return new IndexInfoIteratorIterator($cursor);
 }
Example #16
0
 /**
  * Create one or more indexes for the collection using the createIndexes
  * command.
  *
  * @param Server $server
  */
 private function executeCommand(Server $server)
 {
     $command = new Command(['createIndexes' => $this->collectionName, 'indexes' => $this->indexes]);
     $server->executeCommand($this->databaseName, $command);
 }
 /**
  * Execute the operation.
  *
  * @see Executable::execute()
  * @param Server $server
  * @return object Command result document
  */
 public function execute(Server $server)
 {
     $cursor = $server->executeCommand($this->databaseName, $this->createCommand());
     return current($cursor->toArray());
 }
Example #18
0
 /**
  * Create one or more indexes for the collection using the createIndexes
  * command.
  *
  * @param Server $server
  */
 private function executeCommand(Server $server)
 {
     $command = new Command(['createIndexes' => $this->collectionName, 'indexes' => $this->indexes]);
     $cursor = $server->executeCommand($this->databaseName, $command);
     return current($cursor->toArray());
 }
 /**
  * Execute the operation.
  *
  * @see Executable::execute()
  * @param Server $server
  * @return object Command result document
  */
 public function execute(Server $server)
 {
     $cmd = ['dropIndexes' => $this->collectionName, 'index' => $this->indexName];
     $cursor = $server->executeCommand($this->databaseName, new Command($cmd));
     return current($cursor->toArray());
 }
 /**
  * Execute the operation.
  *
  * @see Executable::execute()
  * @param Server $server
  * @return object|null
  */
 public function execute(Server $server)
 {
     $cursor = $server->executeCommand($this->databaseName, $this->createCommand());
     $result = current($cursor->toArray());
     if (empty($result->ok)) {
         throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error');
     }
     if (!isset($result->value)) {
         return null;
     }
     /* Prior to 3.0, findAndModify returns an empty document instead of null
      * when an upsert is performed and the pre-modified document was
      * requested.
      */
     if ($this->options['upsert'] && !$this->options['new'] && isset($result->lastErrorObject->updatedExisting) && !$result->lastErrorObject->updatedExisting) {
         return null;
     }
     if (!is_object($result->value)) {
         throw new UnexpectedValueException('findAndModify command did not return a "value" document');
     }
     return $result->value;
 }
Example #21
0
 /**
  * Execute the operation.
  *
  * @see Executable::execute()
  *
  * @param Server $server
  *
  * @return integer
  * @throws UnexpectedValueException if the command response was malformed
  */
 public function execute(Server $server)
 {
     $readPreference = isset($this->options['readPreference']) ? $this->options['readPreference'] : null;
     $cursor = $server->executeCommand($this->databaseName, $this->createCommand($server), $readPreference);
     $result = current($cursor->toArray());
     // Older server versions may return a float
     if (!isset($result->n) || !(is_integer($result->n) || is_float($result->n))) {
         throw new UnexpectedValueException('count command did not return a numeric "n" value');
     }
     return (int) $result->n;
 }
Example #22
0
 /**
  * Execute the operation.
  *
  * @see Executable::execute()
  * @param Server $server
  * @return object Command result document
  */
 public function execute(Server $server)
 {
     $cursor = $server->executeCommand($this->databaseName, new Command(['dropDatabase' => 1]));
     return current($cursor->toArray());
 }