-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathStreamingTransaction.php
158 lines (136 loc) · 4.28 KB
/
StreamingTransaction.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
/**
* ArangoDB PHP client: streaming transaction
*
* @package ArangoDBClient
* @author Jan Steemann
* @copyright Copyright 2013, triagens GmbH, Cologne, Germany
*/
namespace ArangoDBClient;
/**
* Streaming transaction object
*
* @package ArangoDBClient
* @since 3.5
*/
class StreamingTransaction extends TransactionBase
{
/**
* class constant for id values
*/
const ENTRY_ID = 'id';
/**
* The transaction id - assigned by the server
*
* @var string - transaction id
*/
private $_id;
/**
* An array of collections used by this transaction
*
* @var array - collection objects used by the transaction
*/
private $_collections;
/**
* Constructs a streaming transaction object
*
* @param Connection $connection - client connection
* @param array $transactionArray - array with collections used by the transaction
*/
public function __construct(Connection $connection, array $transactionArray = null)
{
parent::__construct($connection);
if (is_array($transactionArray)) {
$this->buildTransactionAttributesFromArray($transactionArray);
}
// set up participating collections
$this->_collections = [];
foreach ($this->attributes[self::ENTRY_COLLECTIONS][self::ENTRY_EXCLUSIVE] as $name) {
$this->_collections[$name] = new StreamingTransactionCollection($this, $name, self::ENTRY_EXCLUSIVE);
}
foreach ($this->attributes[self::ENTRY_COLLECTIONS][self::ENTRY_WRITE] as $name) {
if (!isset($this->_collections[$name])) {
$this->_collections[$name] = new StreamingTransactionCollection($this, $name, self::ENTRY_WRITE);
}
}
foreach ($this->attributes[self::ENTRY_COLLECTIONS][self::ENTRY_READ] as $name) {
if (!isset($this->_collections[$name])) {
$this->_collections[$name] = new StreamingTransactionCollection($this, $name, self::ENTRY_READ);
}
}
}
/**
* Get a participating collection of the transaction by name
* Will throw an exception if the collection is not part of the transaction
*
* @throws ClientException
*
* @param string $name - name of the collection
*
* @return StreamingTransactionCollection - collection object
*/
public function getCollection($name)
{
if (!isset($this->_collections[$name])) {
throw new ClientException('Collection not registered in transaction');
}
return $this->_collections[$name];
}
/**
* Get the transaction's id
*
* @return string - transaction id
*/
public function getId()
{
return $this->_id;
}
/**
* Set the transaction's id - this is used internally and should not be called by end users
*
* @param mixed $id - transaction id as number or string
*/
public function setId($id)
{
assert(is_string($id) || is_numeric($id));
if ($this->_id !== null) {
throw new ClientException('Should not update the id of an existing transaction');
}
if (is_numeric($id)) {
$id = (string) $id;
}
$this->_id = $id;
}
/**
* Executes an AQL query inside the transaction
*
* This is a shortcut for creating a new Statement and executing it.
*
* @throws ClientException
*
* @param array $data - query data, as is required by Statement::__construct()
*
* @return Cursor - query cursor, as returned by Statement::execute()
*/
public function query(array $data)
{
$data['transaction'] = $this;
$statement = new Statement($this->getConnection(), $data);
return $statement->execute();
}
/**
* Build the object's attributes from a given array
*
* @param $options
*
* @throws \ArangoDBClient\ClientException
*/
protected function buildTransactionAttributesFromArray($options)
{
parent::buildTransactionAttributesFromArray($options);
if (isset($options[self::ENTRY_ID])) {
$this->setId($options[self::ENTRY_ID]);
}
}
}
class_alias(Transaction::class, '\triagens\ArangoDb\StreamingTransaction');