1
|
|
|
<?php |
2
|
|
|
namespace Cassandra\Request; |
3
|
|
|
use Cassandra\Protocol\Frame; |
4
|
|
|
use Cassandra\Protocol; |
5
|
|
|
use Cassandra\Connection; |
6
|
|
|
use Cassandra\Type; |
7
|
|
|
|
8
|
|
|
class Batch extends Request{ |
9
|
|
|
const TYPE_LOGGED = 0; |
10
|
|
|
const TYPE_UNLOGGED = 1; |
11
|
|
|
const TYPE_COUNTER = 2; |
12
|
|
|
|
13
|
|
|
protected $opcode = Frame::OPCODE_BATCH; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
protected $_queryArray = []; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var int |
22
|
|
|
*/ |
23
|
|
|
protected $_batchType; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* |
27
|
|
|
* @var int |
28
|
|
|
*/ |
29
|
|
|
protected $_consistency; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
protected $_options; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* |
39
|
|
|
* @param string $type |
40
|
|
|
* @param string $consistency |
41
|
|
|
* @param array $options |
42
|
|
|
*/ |
43
|
|
|
public function __construct($type = null, $consistency = null, $options = []) { |
44
|
|
|
$this->_batchType = $type === null ? Batch::TYPE_LOGGED : $type; |
45
|
|
|
$this->_consistency = $consistency === null ? Request::CONSISTENCY_ONE : $consistency; |
46
|
|
|
$this->_options = $options; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Exec transaction |
51
|
|
|
*/ |
52
|
|
|
public function getBody() { |
53
|
|
|
return pack('C', $this->_batchType) |
54
|
|
|
. pack('n', count($this->_queryArray)) . implode('', $this->_queryArray) |
55
|
|
|
. self::queryParameters($this->_consistency, $this->_options); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string $cql |
60
|
|
|
* @param array $values |
61
|
|
|
* @return self |
62
|
|
|
*/ |
63
|
|
View Code Duplication |
public function appendQuery($cql, array $values = []) { |
|
|
|
|
64
|
|
|
$binary = pack('C', 0); |
65
|
|
|
|
66
|
|
|
$binary .= pack('N', strlen($cql)) . $cql; |
67
|
|
|
$binary .= Request::valuesBinary($values, !empty($this->_options['names_for_values'])); |
68
|
|
|
|
69
|
|
|
$this->_queryArray[] = $binary; |
70
|
|
|
|
71
|
|
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* |
76
|
|
|
* @param string $queryId |
77
|
|
|
* @param array $values |
78
|
|
|
* @return self |
79
|
|
|
*/ |
80
|
|
View Code Duplication |
public function appendQueryId($queryId, array $values = []) { |
|
|
|
|
81
|
|
|
$binary = pack('C', 1); |
82
|
|
|
|
83
|
|
|
$binary .= pack('n', strlen($queryId)) . $queryId; |
84
|
|
|
$binary .= Request::valuesBinary($values, !empty($this->_options['names_for_values'])); |
85
|
|
|
|
86
|
|
|
$this->_queryArray[] = $binary; |
87
|
|
|
|
88
|
|
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* |
93
|
|
|
* @param int $consistency |
94
|
|
|
* @param array $options |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
|
|
public static function queryParameters($consistency, array $options = []){ |
98
|
|
|
$flags = 0; |
99
|
|
|
$optional = ''; |
100
|
|
|
|
101
|
|
View Code Duplication |
if (isset($options['serial_consistency'])) { |
|
|
|
|
102
|
|
|
$flags |= Query::FLAG_WITH_SERIAL_CONSISTENCY; |
103
|
|
|
$optional .= pack('n', $options['serial_consistency']); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
View Code Duplication |
if (isset($options['default_timestamp'])) { |
|
|
|
|
107
|
|
|
$flags |= Query::FLAG_WITH_DEFAULT_TIMESTAMP; |
108
|
|
|
$optional .= Type\Bigint::binary($options['default_timestamp']); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
if (!empty($options['names_for_values'])){ |
112
|
|
|
/** |
113
|
|
|
* @link https://github.com/duoshuo/php-cassandra/issues/40 |
114
|
|
|
*/ |
115
|
|
|
throw new \Cassandra\Exception('NAMES_FOR_VALUES in batch request seems never work in Cassandra 2.1.x. Keep NAMES_FOR_VALUES flag false to avoid this bug.'); |
116
|
|
|
|
117
|
|
|
$flags |= Query::FLAG_WITH_NAMES_FOR_VALUES; |
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return pack('n', $consistency) . pack('C', $flags) . $optional; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.