Skip to content

Commit

Permalink
Update Connection for Couchbase > 2.3.x
Browse files Browse the repository at this point in the history
Add Checking Primary Index exists or not when opening a bucket
Remove deprecated method enableN1ql
  • Loading branch information
cpdevops committed May 19, 2017
1 parent 0f27b8d commit 2fc46c5
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 16 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"illuminate/container": "^5.4",
"illuminate/database": "^5.4",
"illuminate/events": "^5.4",
"ext-couchbase": ">=2.2.0"
"ext-couchbase": ">=2.3.0"
},
"require-dev": {
"phpunit/phpunit": "^4.0|^5.0",
Expand Down
56 changes: 41 additions & 15 deletions src/Mpociot/Couchbase/Connection.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?php namespace Mpociot\Couchbase;

use CouchbaseBucket;
use CouchbaseCluster;
use CouchbaseN1qlQuery;
use \Couchbase\ClassicAuthenticator;
use \Couchbase\Cluster;
use \Couchbase\Bucket;
use \Couchbase\N1qlQuery;
use \Couchbase\N1qlIndex;

class Connection extends \Illuminate\Database\Connection
{
Expand Down Expand Up @@ -43,21 +45,24 @@ public function __construct(array $config)
// Build the connection string
$dsn = $this->getDsn($config);

// Set bucket / database
$this->bucketname = $config['bucket'];

// Create the connection
$this->connection = $this->createConnection($dsn, $config);

// Select database
$this->bucketname = $config['bucket'];
// $this->connection->manager(array_get($config, 'username'), array_get($config, 'password'));

// opens connection to bucket
$this->bucket = $this->connection->openBucket($this->bucketname);
// Enable N1QL for bucket
$this->bucket->enableN1ql($config['n1ql_hosts']);

// Check if Primary Index exists or not, If not then create one to prevent error
$this->checkOrCreatePrimaryIndex();

$this->useDefaultPostProcessor();

$this->useDefaultSchemaGrammar();
}

/**
* Get the default post processor instance.
*
Expand Down Expand Up @@ -98,7 +103,7 @@ public function builder($type)
*
* @return mixed
*/
protected function executeQuery(CouchbaseN1qlQuery $query)
protected function executeQuery(\Couchbase\N1qlQuery $query)
{
return $this->bucket->query($query);
}
Expand All @@ -113,7 +118,7 @@ public function select($query, $bindings = [], $useReadPdo = true)
return [];
}

$query = CouchbaseN1qlQuery::fromString($query);
$query = \Couchbase\N1qlQuery::fromString($query);
$query->consistency($this->consistency);
$query->positionalParams($bindings);

Expand Down Expand Up @@ -172,7 +177,7 @@ public function affectingStatement($query, $bindings = [])
if ($this->pretending()) {
return 0;
}
$query = \CouchbaseN1qlQuery::fromString($query);
$query = \Couchbase\N1qlQuery::fromString($query);
$query->consistency($this->consistency);
$query->namedParams(['parameters' => $bindings]);
$result = $this->executeQuery($query);
Expand All @@ -194,7 +199,7 @@ public function positionalStatement($query, array $bindings = [])
if ($this->pretending()) {
return 0;
}
$query = CouchbaseN1qlQuery::fromString($query);
$query = \Couchbase\N1qlQuery::fromString($query);
$query->consistency($this->consistency);
$query->positionalParams($bindings);
$result = $this->executeQuery($query);
Expand Down Expand Up @@ -265,7 +270,28 @@ public function getCouchbaseCluster()
*/
protected function createConnection($dsn, array $config)
{
return new CouchbaseCluster($dsn, array_get($config, 'username'), array_get($config, 'password'));
$authenticator = new \Couchbase\ClassicAuthenticator();
// if(array_get($config, 'username') != '' && array_get($config, 'password') != '') {
// $authenticator->cluster(array_get($config, 'username'), array_get($config, 'password'));
// }
$authenticator->bucket(array_get($config, 'bucket'), array_get($config, 'bucket_password'));

$cluster = new \Couchbase\Cluster($dsn);
$cluster->authenticate($authenticator);
return $cluster;
}

protected function checkOrCreatePrimaryIndex()
{
$query = 'SELECT * FROM system:indexes WHERE `name` = "#primary" AND `keyspace_id` = "' . $this->bucketname . '";';
$query = \Couchbase\N1qlQuery::fromString($query);
$result = $this->executeQuery($query);

if(count($result->rows) != 1) {
$query = 'CREATE PRIMARY INDEX ON `'. $this->bucketname .'`;';
$query = \Couchbase\N1qlQuery::fromString($query);
$result = $this->executeQuery($query);
}
}

/**
Expand Down

0 comments on commit 2fc46c5

Please sign in to comment.