コード例 #1
0
 protected function tearDown()
 {
     if (!self::$supported) {
         return;
     }
     parent::tearDown();
     $this->client->dropDatabase(self::DATABASE_NAME);
 }
コード例 #2
0
 /**
  * Get the collection associated to a model class
  *
  * @param string $class
  *
  * @return Collection
  */
 public function getCollection($class)
 {
     if (!isset($this->collections[$class])) {
         $info = $this->resolver->resolve($class);
         $this->collections[$class] = $this->client->selectCollection($info['database'], $info['collection']);
     }
     return $this->collections[$class];
 }
コード例 #3
0
ファイル: MongoTest.php プロジェクト: imbo/imbo
 /**
  * Drop the test database after each test
  */
 public function tearDown()
 {
     if (class_exists('MongoDB\\Client')) {
         $client = new MongoClient();
         $client->dropDatabase($this->databaseName);
     }
     parent::tearDown();
 }
コード例 #4
0
 /**
  * @return MongoDBDatabase
  *
  * @throws InvalidArgumentException
  */
 protected function getDatabaseConnection() : MongoDBDatabase
 {
     if (!$this->databaseConnection) {
         $client = new MongoDBClient($this->getConnectionString());
         $this->databaseConnection = $client->selectDatabase($this->configuration->getDatabaseName());
     }
     return $this->databaseConnection;
 }
コード例 #5
0
 /**
  * Open session
  *
  * @param string $savePath
  * @param string $name
  * @return bool
  */
 public function open($savePath, $name)
 {
     // Note: session save path is not used
     $this->sessionName = $name;
     $this->lifetime = (int) ini_get('session.gc_maxlifetime');
     $this->mongoCollection = $this->mongoClient->selectCollection($this->options->getDatabase(), $this->options->getCollection());
     $this->mongoCollection->createIndex([$this->options->getModifiedField() => 1], $this->options->useExpireAfterSecondsIndex() ? ['expireAfterSeconds' => $this->lifetime] : []);
     return true;
 }
コード例 #6
0
 /**
  * {@inheritdoc}
  */
 protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output)
 {
     $client = new Client($input->getOption('server'));
     $db = $client->selectDatabase($input->getArgument('database'));
     $output->writeln("<info>✓ Successfully established database connection</info>", $output::VERBOSITY_VERBOSE);
     $databaseMigrationsLockCollection = $db->selectCollection('DATABASE_MIGRATIONS_LOCK');
     $databaseMigrationsLockCollection->updateOne(['locked' => ['$exists' => true]], ['$set' => ['locked' => false]], ['upsert' => true]);
     $output->writeln("<info>✓ Successfully released migration lock</info>");
 }
コード例 #7
0
ファイル: FunctionalTest.php プロジェクト: makasim/yadm
 /**
  * @before
  */
 protected function setUpMongoClient()
 {
     $client = new Client();
     $this->database = $client->selectDatabase('yadm_test');
     foreach ($this->database->listCollections() as $collectionInfo) {
         if ('system.indexes' == $collectionInfo->getName()) {
             continue;
         }
         $this->database->dropCollection($collectionInfo->getName());
     }
 }
コード例 #8
0
 public function testSelectDatabasePassesReadPreferenceAndWriteConcern()
 {
     $databaseOptions = ['readPreference' => new ReadPreference(ReadPreference::RP_SECONDARY_PREFERRED), 'writeConcern' => new WriteConcern(WriteConcern::MAJORITY)];
     $client = new Client($this->getUri());
     $database = $client->selectDatabase($this->getDatabaseName(), $databaseOptions);
     $debug = $database->__debugInfo();
     $this->assertInstanceOf('MongoDB\\Driver\\ReadPreference', $debug['readPreference']);
     $this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
     $this->assertInstanceOf('MongoDB\\Driver\\WriteConcern', $debug['writeConcern']);
     $this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
 }
コード例 #9
0
 /**
  * Create a new database connection instance.
  *
  * @param  array   $config
  */
 public function __construct(array $config)
 {
     $this->config = $config;
     // Build the connection string
     $dsn = $this->getDsn($config);
     // You can pass options directly to the MongoDB constructor
     $options = array_get($config, 'options', []);
     // Create the connection
     $this->connection = $this->createConnection($dsn, $config, $options);
     // Select database
     $this->db = $this->connection->selectDatabase($config['database']);
     $this->useDefaultPostProcessor();
 }
コード例 #10
0
ファイル: MongoDBHandler.php プロジェクト: acrobat/monolog
 /**
  * Constructor.
  *
  * @param Client|Manager $mongodb    MongoDB library or driver client
  * @param string         $database   Database name
  * @param string         $collection Collection name
  * @param int            $level      The minimum logging level at which this handler will be triggered
  * @param Boolean        $bubble     Whether the messages that are handled can bubble up the stack or not
  */
 public function __construct($mongodb, $database, $collection, $level = Logger::DEBUG, $bubble = true)
 {
     if (!($mongodb instanceof Client || $mongodb instanceof Manager)) {
         throw new \InvalidArgumentException('MongoDB\\Client or MongoDB\\Driver\\Manager instance required');
     }
     if ($mongodb instanceof Client) {
         $this->collection = $mongodb->selectCollection($database, $collection);
     } else {
         $this->manager = $mongodb;
         $this->namespace = $database . '.' . $collection;
     }
     parent::__construct($level, $bubble);
 }
コード例 #11
0
 public function testSelectDatabasePassesOptions()
 {
     $databaseOptions = ['readConcern' => new ReadConcern(ReadConcern::LOCAL), 'readPreference' => new ReadPreference(ReadPreference::RP_SECONDARY_PREFERRED), 'typeMap' => ['root' => 'array'], 'writeConcern' => new WriteConcern(WriteConcern::MAJORITY)];
     $client = new Client($this->getUri());
     $database = $client->selectDatabase($this->getDatabaseName(), $databaseOptions);
     $debug = $database->__debugInfo();
     $this->assertInstanceOf('MongoDB\\Driver\\ReadConcern', $debug['readConcern']);
     $this->assertSame(ReadConcern::LOCAL, $debug['readConcern']->getLevel());
     $this->assertInstanceOf('MongoDB\\Driver\\ReadPreference', $debug['readPreference']);
     $this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
     $this->assertInternalType('array', $debug['typeMap']);
     $this->assertSame(['root' => 'array'], $debug['typeMap']);
     $this->assertInstanceOf('MongoDB\\Driver\\WriteConcern', $debug['writeConcern']);
     $this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
 }
コード例 #12
0
 /**
  * Return a "MongoCollection" instance.
  *
  * @return \MongoCollection
  */
 private function getCollection()
 {
     if (null === $this->collection) {
         $this->collection = $this->mongo->selectCollection($this->options['database'], $this->options['collection']);
     }
     return $this->collection;
 }
コード例 #13
0
 public function testUpsertedIds()
 {
     $client = new \MongoDB\Client(Config::get('mongolab.uri') . '/' . Config::get('mongolab.database_name'));
     $preaggregated_collection = $client->selectCollection(Config::get('mongolab.database_name'), "update_many_test");
     $preaggregated_collection->drop();
     $preaggregated_collection->insertOne(['_id' => 'i1', "views" => 1]);
     $preaggregated_collection->insertOne(['_id' => 'i2', "views" => 1]);
     $preaggregated_collection->insertOne(['_id' => 'i3', "views" => 1]);
     $preaggregated_collection->insertOne(['_id' => 'i4', "views" => 1]);
     //        $update_result = $preaggregated_collection->updateMany(
     //            ['_id' => ['$in' => ['i1', 'i2', 'i10']]],
     //            ['$inc' => ["views" => 1]],
     //            ['upsert' => true]
     //        );
     $update_result = $preaggregated_collection->updateOne(['_id' => 'i10'], ['$inc' => ["views" => 1]], ['upsert' => true]);
 }
コード例 #14
0
ファイル: Mongo.php プロジェクト: trashf/jormakalevi2
 /**
  * @param array $options
  */
 public function __construct(array $options = array())
 {
     if (isset($options['db']) && $options['db'] instanceof Database) {
         $this->_db = $options['db'];
     } else {
         if (!empty($options['client']) && $options['client'] instanceof Client) {
             $connection = $options['client'];
         } elseif (!empty($options['serverString'])) {
             $connection = new Client($options['serverString']);
         } else {
             $connection = new Client();
         }
         $databaseName = !empty($options['dbName']) ? $options['dbName'] : 'jormakalevi';
         $this->_db = $connection->selectDatabase($databaseName);
     }
 }
コード例 #15
0
 public function getDatabases()
 {
     $result = [];
     /** @var DatabaseInfo $item */
     foreach ($this->client->listDatabases() as $item) {
         $result[] = new SchemaObject($item->getName(), $item->getSizeOnDisk(), $item->isEmpty());
     }
     return $result;
 }
コード例 #16
0
 /**
  * Delete a reserved job from the queue.
  *
  * @param  string $queue
  * @param  string $id
  * @return void
  */
 public function deleteReserved($queue, $id)
 {
     // original query: $this->database->table($this->table)->where('_id', $id)->delete();
     try {
         $result = $this->client->selectCollection($this->databaseName, $this->table)->deleteOne(['_id' => $id], ['$isolated' => 1]);
     } catch (Exception $e) {
         printf("Other error: %s\n", $e->getMessage());
         exit;
     }
 }
コード例 #17
0
ファイル: Connection.php プロジェクト: pliashkou/mongodb
 /**
  * Return a new Database instance.
  *
  * If a logger callable was defined, a LoggableDatabase will be returned.
  *
  * @see Connection::selectDatabase()
  * @param string $name
  * @return Database
  */
 protected function doSelectDatabase($name)
 {
     $mongoDB = $this->mongoClient->selectDatabase($name);
     $numRetries = $this->config->getRetryQuery();
     $loggerCallable = $this->config->getLoggerCallable();
     return new Database($this, $mongoDB, $this->eventManager, $numRetries);
     //        return $loggerCallable !== null
     //            ? new LoggableDatabase($this, $mongoDB, $this->eventManager, $numRetries, $loggerCallable)
     //            : new Database($this, $mongoDB, $this->eventManager, $numRetries);
 }
コード例 #18
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()
 {
     $databaseInfoIterator = $this->client->listDatabases();
     $databases = ['databases' => [], 'totalSize' => 0, 'ok' => 1.0];
     foreach ($databaseInfoIterator as $databaseInfo) {
         $databases['databases'][] = $databaseInfo->getName();
         $databases['totalSize'] += $databaseInfo->getSizeOnDisk();
     }
     return $databases;
 }
コード例 #19
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;
 }
コード例 #20
0
 /**
  * Add $triples about a given $subject to Mongo. Only $triples with subject matching $subject will be added, others will be ignored.
  * Make them quads with a $context
  * @param $subject
  * @param array $triples
  * @param string $storeName
  * @param string $podName
  * @param null $context
  * @param null $allowableTypes
  */
 public function loadTriplesAbout($subject, array $triples, $storeName, $podName, $context = null, $allowableTypes = null)
 {
     $context = $context == null ? Config::getInstance()->getDefaultContextAlias() : $this->labeller->uri_to_alias($context);
     if (array_key_exists($podName, $this->collections)) {
         $collection = $this->collections[$podName];
     } else {
         $m = new Client(Config::getInstance()->getConnStr($storeName), [], ['typeMap' => ['root' => 'array', 'document' => 'array', 'array' => 'array']]);
         $collection = $m->selectDatabase($storeName)->selectCollection($podName);
     }
     $graph = new MongoGraph();
     foreach ($triples as $triple) {
         $triple = rtrim($triple);
         $parts = preg_split("/\\s/", $triple);
         $subject = trim($parts[0], '><');
         $predicate = trim($parts[1], '><');
         $object = $this->extract_object($parts);
         if ($this->isUri($object)) {
             $graph->add_resource_triple($subject, $predicate, $object);
         } else {
             $graph->add_literal_triple($subject, $predicate, $object);
         }
     }
     if ($allowableTypes != null && is_array($allowableTypes)) {
         $types = $graph->get_resource_triple_values($subject, "http://www.w3.org/1999/02/22-rdf-syntax-ns#type");
         if ($types == null || empty($types)) {
             return;
         } else {
             foreach ($types as $type) {
                 if (in_array($type, $allowableTypes)) {
                     $this->saveCBD($subject, $graph, $collection, $context);
                     return;
                 }
             }
             return;
         }
     }
     $this->saveCBD($subject, $graph, $collection, $context);
 }
コード例 #21
0
ファイル: MongoDbAdapter.php プロジェクト: hellofresh/engine
 /**
  * Get mongo db stream collection
  *
  * @param StreamName $streamName
  * @return Collection
  */
 private function getCollection(StreamName $streamName)
 {
     return $this->client->selectCollection($this->dbName, (string) $streamName);
 }
コード例 #22
0
ファイル: MongoDb.php プロジェクト: webiny/mongo
 /**
  * Select database
  *
  * @param string $database
  */
 public function selectDatabase($database)
 {
     $this->db = $this->connection->selectDatabase($database);
 }
コード例 #23
0
 public function __construct($server = "mongodb://localhost:27017", array $options = array("connect" => true))
 {
     parent::__construct($server, $options);
 }
コード例 #24
0
ファイル: Chirp.php プロジェクト: batopa/chirp
 /**
  * Setup MongoDB connection
  *
  * $mongoDbConf must contain
  *  - db: the name of mongo database to use
  *
  *  and can contain
  * - uri
  * - uriOptions
  * - driverOptions
  *
  * @see \MongoDB\Client for $mongoConf
  * @param array $mongoDbConf
  * @return $this
  */
 public function setupMongoDb(array $mongoDbConf)
 {
     $mongoDbConf += ['uri' => 'mongodb://localhost:27017', 'uriOptions' => [], 'driverOptions' => [], 'db' => ''];
     $client = new Client($mongoDbConf['uri'], $mongoDbConf['uriOptions'], $mongoDbConf['driverOptions']);
     $this->db = $client->selectDatabase($mongoDbConf['db']);
     return $this;
 }
コード例 #25
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()
 {
     return $this->client->listDatabases();
 }
コード例 #26
0
 /**
  * Test including an rdf sequence in a view
  */
 public function testGenerateViewContainingRdfSequence()
 {
     // get the view - this should trigger generation
     $this->tripodViews->getViewForResource("http://talisaspire.com/resources/filter1", "v_resource_rdfsequence");
     $expectedView = array("_id" => array(_ID_RESOURCE => "http://talisaspire.com/resources/filter1", _ID_CONTEXT => 'http://talisaspire.com/', "type" => "v_resource_rdfsequence"), "value" => array(_GRAPHS => array(array("_id" => array("r" => "http://talisaspire.com/works/filter1", "c" => 'http://talisaspire.com/'), "dct:subject" => array("u" => "http://talisaspire.com/disciplines/physics"), "rdf:type" => array(array(VALUE_URI => "bibo:Book"), array(VALUE_URI => "acorn:Work"))), array("_id" => array("r" => "http://talisaspire.com/works/filter2", "c" => 'http://talisaspire.com/'), "dct:subject" => array("u" => "http://talisaspire.com/disciplines/physics"), "rdf:type" => array(array(VALUE_URI => "bibo:Book"), array(VALUE_URI => "acorn:Work"))), array("_id" => array("r" => "http://talisaspire.com/works/filter3", "c" => 'http://talisaspire.com/'), "dct:subject" => array("u" => "http://talisaspire.com/disciplines/physics"), "rdf:type" => array(array(VALUE_URI => "bibo:Chapter"), array(VALUE_URI => "acorn:Work"))), array("_id" => array("r" => "http://talisaspire.com/resources/filter1", "c" => 'http://talisaspire.com/'), "rdf:type" => array(array(VALUE_URI => "bibo:Book"), array(VALUE_URI => "acorn:Resource"), array(VALUE_URI => "rdf:Seq")), "dct:isVersionOf" => array(array(VALUE_URI => "http://talisaspire.com/works/filter1"), array(VALUE_URI => "http://talisaspire.com/works/filter2"), array(VALUE_URI => "http://talisaspire.com/works/filter3")), "rdf:_1" => array("u" => "http://talisaspire.com/1"), "rdf:_2" => array("u" => "http://talisaspire.com/2"), "rdf:_3" => array("u" => "http://talisaspire.com/3"))), _IMPACT_INDEX => array(array(_ID_RESOURCE => "http://talisaspire.com/resources/filter1", _ID_CONTEXT => 'http://talisaspire.com/'), array(_ID_RESOURCE => "http://talisaspire.com/works/filter1", _ID_CONTEXT => 'http://talisaspire.com/'), array(_ID_RESOURCE => "http://talisaspire.com/works/filter2", _ID_CONTEXT => 'http://talisaspire.com/'), array(_ID_RESOURCE => "http://talisaspire.com/works/filter3", _ID_CONTEXT => 'http://talisaspire.com/'))));
     // get the view direct from mongo
     $mongo = new Client(\Tripod\Mongo\Config::getInstance()->getConnStr('tripod_php_testing'), [], ['typeMap' => ['root' => 'array', 'document' => 'array', 'array' => 'array']]);
     $actualView = $mongo->selectCollection('tripod_php_testing', 'views')->findOne(array('_id' => array("r" => 'http://talisaspire.com/resources/filter1', "c" => 'http://talisaspire.com/', "type" => 'v_resource_rdfsequence')));
     $this->assertEquals($expectedView, $actualView);
 }
コード例 #27
0
ファイル: MongoDB.php プロジェクト: phpdatabase/phpdatabase
 public function __construct(Client $client, $databaseName, $collectionName)
 {
     $this->collection = $client->selectCollection($databaseName, $collectionName);
 }
コード例 #28
0
ファイル: Client.php プロジェクト: algatux/mongo-bundle
 /**
  * Client constructor.
  *
  * @param string                       $uri
  * @param array                        $uriOptions
  * @param array                        $driverOptions
  * @param DataCollectorLoggerInterface $logger
  */
 public function __construct($uri = 'mongodb://localhost:27017', array $uriOptions = [], array $driverOptions = [], DataCollectorLoggerInterface $logger)
 {
     parent::__construct($uri, $uriOptions, $driverOptions);
     $this->logger = $logger;
 }
コード例 #29
0
 /**
  * @param string $databaseName
  * @return array - command result
  */
 public static function dropDB($databaseName)
 {
     return static::$_mongoClient->dropDatabase($databaseName);
 }
コード例 #30
0
 /**
  * Disconnect from the underlying MongoClient connection.
  */
 public function disconnect()
 {
     $this->connection->close();
 }