Skip to content

Added simple checker methods to test if Collection/Document/Vertex/Edge exists. #170

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,11 @@ In order to use ArangoDB, you need to specify the connection options. We do so b
// authorization type to use (currently supported: 'Basic')
ArangoConnectionOptions::OPTION_AUTH_TYPE => 'Basic',
// user for basic authorization
ArangoConnectionOptions::OPTION_AUTH_USER => 'root',
ArangoConnectionOptions::OPTION_AUTH_USER => 'root',
// password for basic authorization
ArangoConnectionOptions::OPTION_AUTH_PASSWD => '',
ArangoConnectionOptions::OPTION_AUTH_PASSWD => '',
// connection persistence on server. can use either 'Close' (one-time connections) or 'Keep-Alive' (re-used connections)
ArangoConnectionOptions::OPTION_CONNECTION => 'Close',
ArangoConnectionOptions::OPTION_CONNECTION => 'Close',
// connect timeout in seconds
ArangoConnectionOptions::OPTION_TIMEOUT => 3,
// whether or not to reconnect when a keep-alive connection has timed out on server
Expand Down Expand Up @@ -263,14 +263,18 @@ The below code will first set up the collection locally in a variable name $user

$collectionHandler = new CollectionHandler($connection);

// create a new document
// create a new collection
$userCollection = new ArangoCollection();
$userCollection->setName('user');
$id = $collectionHandler->add($userCollection);

// print the collection id created by the server
var_dump($id);

// check if the collection exists
$result = $collectionHandler->has('user');
var_dump($result);


<a name="creating_document"/a>
## Creating a document
Expand Down Expand Up @@ -298,6 +302,10 @@ The below code will first set up the document locally in a variable name $user,
// print the document id created by the server
var_dump($id);

// check if a document exists
$result = $handler->has("users", $id);
var_dump($result);



Document properties can be set by using the set() method, or by directly manipulating the document properties.
Expand Down Expand Up @@ -484,11 +492,11 @@ Here's the full code that combines all the pieces outlined above:
// authorization type to use (currently supported: 'Basic')
ArangoConnectionOptions::OPTION_AUTH_TYPE => 'Basic',
// user for basic authorization
ArangoConnectionOptions::OPTION_AUTH_USER => 'root',
ArangoConnectionOptions::OPTION_AUTH_USER => 'root',
// password for basic authorization
ArangoConnectionOptions::OPTION_AUTH_PASSWD => '',
ArangoConnectionOptions::OPTION_AUTH_PASSWD => '',
// connection persistence on server. can use either 'Close' (one-time connections) or 'Keep-Alive' (re-used connections)
ArangoConnectionOptions::OPTION_CONNECTION => 'Close',
ArangoConnectionOptions::OPTION_CONNECTION => 'Close',
// connect timeout in seconds
ArangoConnectionOptions::OPTION_TIMEOUT => 3,
// optionally create new collections when inserting documents
Expand Down
4 changes: 4 additions & 0 deletions examples/collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
$result = $handler->add($col);
var_dump($result);

// check if a collection exists
$result = $handler->has("foobar");
var_dump($result);

// get an existing collection
$result = $handler->get("hihi");
var_dump($result);
Expand Down
4 changes: 4 additions & 0 deletions examples/document.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@
// delete the document
$result = $handler->deleteById("users", $id);
var_dump($result);

// check if a document exists
$result = $handler->has("users", "foobar123");
var_dump($result);
} catch (ConnectException $e) {
print $e . PHP_EOL;
} catch (ServerException $e) {
Expand Down
6 changes: 6 additions & 0 deletions examples/graph.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,15 @@
$getResult1 = $graphHandler->getVertex('Graph', 'vertex1');
$getResult2 = $graphHandler->getVertex('Graph', 'vertex2');

// check if vertex exists
var_dump($graphHandler->hasVertex('Graph', 'vertex1'));

// Save the connecting edge
$saveEdgeResult1 = $graphHandler->saveEdge('Graph', 'vertex1', 'vertex2', 'somelabelValue', $edge1);

// check if edge exists
var_dump($graphHandler->hasEdge('Graph', 'edge1'));

// Get the connecting edge
$getEdgeResult1 = $graphHandler->getEdge('Graph', 'edge1');

Expand Down
33 changes: 32 additions & 1 deletion lib/triagens/ArangoDb/CollectionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,37 @@ public function get($collectionId)
}


/**
* Check if a collection exists
*
* This will call self::get() internally and checks if there
* was an exception thrown which represents an 404 request.
*
* @throws Exception When any other error than a 404 occurs
*
* @param mixed $collectionId - collection id as a string or number
* @return boolean
*/
public function has($collectionId)
{
try {
// will throw ServerException if entry could not be retrieved
$result = $this->get($collectionId);
return true;
} catch (ServerException $e) {
// we are expecting a 404 to return boolean false
if (strpos($e->getMessage(), '404') !== false) {
return false;
}

// just rethrow
throw $e;
}

return false;
}


/**
* Get properties of a collection
*
Expand Down Expand Up @@ -438,7 +469,7 @@ public function create($collection, $options = array())
if ($collection->getNumberOfShards() !== null) {
$params[Collection::ENTRY_NUMBER_OF_SHARDS] = $collection->getNumberOfShards();
}

if (is_array($collection->getShardKeys())) {
$params[Collection::ENTRY_SHARD_KEYS] = $collection->getShardKeys();
}
Expand Down
38 changes: 35 additions & 3 deletions lib/triagens/ArangoDb/DocumentHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,38 @@ public function get($collectionId, $documentId, array $options = array())
}


/**
* Check if a document exists
*
* This will call self::get() internally and checks if there
* was an exception thrown which represents an 404 request.
*
* @throws Exception When any other error than a 404 occurs
*
* @param string $collectionId - collection id as a string or number
* @param mixed $documentId - document identifier
* @return boolean
*/
public function has($collectionId, $documentId)
{
try {
// will throw ServerException if entry could not be retrieved
$result = $this->get($collectionId, $documentId);
return true;
} catch (ServerException $e) {
// we are expecting a 404 to return boolean false
if (strpos($e->getMessage(), '404') !== false) {
return false;
}

// just rethrow
throw $e;
}

return false;
}


/**
* Get a single document from a collection
*
Expand Down Expand Up @@ -590,8 +622,8 @@ public function replaceById($collectionId, $documentId, Document $document, $opt
{
return $this->put(Urls::URL_DOCUMENT, $collectionId, $documentId, $document, $options);
}


/**
* Replace an existing document in a collection (internal method)
*
Expand Down Expand Up @@ -734,7 +766,7 @@ public function removeById($collectionId, $documentId, $revision = null, $option
return $this->erase(Urls::URL_DOCUMENT, $collectionId, $documentId, $revision, $options);
}


/**
* Remove a document from a collection (internal method)
*
Expand Down
64 changes: 64 additions & 0 deletions lib/triagens/ArangoDb/GraphHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,38 @@ public function getVertex($graphName, $vertexId, array $options = array())
}


/**
* Check if a vertex exists
*
* This will call self::getVertex() internally and checks if there
* was an exception thrown which represents an 404 request.
*
* @throws Exception When any other error than a 404 occurs
*
* @param mixed $graph - graph name as a string or instance of Graph
* @param mixed $vertexId - the vertex identifier
* @return boolean
*/
public function hasVertex($graph, $vertexId)
{
try {
// will throw ServerException if entry could not be retrieved
$result = $this->getVertex($graph, $vertexId);
return true;
} catch (ServerException $e) {
// we are expecting a 404 to return boolean false
if (strpos($e->getMessage(), '404') !== false) {
return false;
}

// just rethrow
throw $e;
}

return false;
}


/**
* Replace an existing vertex in a graph, identified graph name and vertex id
*
Expand Down Expand Up @@ -522,6 +554,38 @@ public function getEdge($graphName, $edgeId, array $options = array())
}


/**
* Check if an edge exists
*
* This will call self::getEdge() internally and checks if there
* was an exception thrown which represents an 404 request.
*
* @throws Exception When any other error than a 404 occurs
*
* @param mixed $graph - graph name as a string or instance of Graph
* @param mixed $edgeId - the vertex identifier
* @return boolean
*/
public function hasEdge($graph, $edgeId)
{
try {
// will throw ServerException if entry could not be retrieved
$result = $this->getEdge($graph, $edgeId);
return true;
} catch (ServerException $e) {
// we are expecting a 404 to return boolean false
if (strpos($e->getMessage(), '404') !== false) {
return false;
}

// just rethrow
throw $e;
}

return false;
}


/**
* Replace an existing edge in a graph, identified graph name and edge id
*
Expand Down
Loading