line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# You may distribute under the terms of either the GNU General Public License |
2
|
|
|
|
|
|
|
# or the Artistic License (the same terms as Perl itself) |
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
# (C) Paul Evans, 2013-2014 -- leonerd@leonerd.org.uk |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
package Protocol::CassandraCQL::ColumnMeta; |
7
|
|
|
|
|
|
|
|
8
|
5
|
|
|
5
|
|
750
|
use strict; |
|
5
|
|
|
|
|
17
|
|
|
5
|
|
|
|
|
172
|
|
9
|
5
|
|
|
5
|
|
24
|
use warnings; |
|
5
|
|
|
|
|
9
|
|
|
5
|
|
|
|
|
245
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our $VERSION = '0.11'; |
12
|
|
|
|
|
|
|
|
13
|
5
|
|
|
5
|
|
24
|
use Carp; |
|
5
|
|
|
|
|
8
|
|
|
5
|
|
|
|
|
305
|
|
14
|
|
|
|
|
|
|
|
15
|
5
|
|
|
5
|
|
1060
|
use Protocol::CassandraCQL qw( :rowflags ); |
|
5
|
|
|
|
|
10
|
|
|
5
|
|
|
|
|
814
|
|
16
|
5
|
|
|
5
|
|
3128
|
use Protocol::CassandraCQL::Type; |
|
5
|
|
|
|
|
17
|
|
|
5
|
|
|
|
|
6384
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
=head1 NAME |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
C - stores the column metadata of a Cassandra CQL query |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
=head1 DESCRIPTION |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
Objects in this class interpret the column metadata from a message frame |
25
|
|
|
|
|
|
|
containing a C response to a query giving C or |
26
|
|
|
|
|
|
|
C. It provides lookup of column names and type information, |
27
|
|
|
|
|
|
|
and provides a convenient accessor to the encoding and decoding support |
28
|
|
|
|
|
|
|
functions, allowing encoding of bytestrings from perl data when executing a |
29
|
|
|
|
|
|
|
prepared statement, and decoding of bytestrings to perl data when obtaining |
30
|
|
|
|
|
|
|
query results. |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
It is also subclassed as L. |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
=cut |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
=head1 CONSTRUCTORS |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=cut |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
=head2 $meta = Protocol::CassandraCQL::ColumnMeta->from_frame( $frame, $version ) |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
Returns a new column metadata object initialised from the given message frame |
43
|
|
|
|
|
|
|
at the given CQL version number. (Version will default to 1 if not supplied, |
44
|
|
|
|
|
|
|
but this may become a required parameter in a future version). |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
=cut |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub from_frame |
49
|
|
|
|
|
|
|
{ |
50
|
14
|
|
|
14
|
1
|
27
|
my $class = shift; |
51
|
14
|
|
|
|
|
28
|
my ( $frame, $version ) = @_; |
52
|
|
|
|
|
|
|
|
53
|
14
|
100
|
|
|
|
43
|
defined $version or $version = 1; |
54
|
|
|
|
|
|
|
|
55
|
14
|
|
|
|
|
85
|
my $self = bless {}, $class; |
56
|
|
|
|
|
|
|
|
57
|
14
|
|
|
|
|
60
|
$self->{columns} = \my @columns; |
58
|
|
|
|
|
|
|
|
59
|
14
|
|
|
|
|
54
|
my $flags = $frame->unpack_int; |
60
|
14
|
|
|
|
|
45
|
my $n_columns = $frame->unpack_int; |
61
|
|
|
|
|
|
|
|
62
|
14
|
|
|
|
|
31
|
my $has_gts = $flags & ROWS_HAS_GLOBALTABLESPEC; |
63
|
|
|
|
|
|
|
|
64
|
14
|
|
100
|
|
|
65
|
my $has_paging = ( $version > 1 ) && ( $flags & ROWS_HAS_MORE_PAGES ); |
65
|
14
|
|
100
|
|
|
54
|
my $no_metadata = ( $version > 1 ) && ( $flags & ROWS_NO_METADATA ); |
66
|
|
|
|
|
|
|
|
67
|
14
|
100
|
|
|
|
37
|
if( $has_paging ) { |
68
|
2
|
|
|
|
|
13
|
$self->{paging_state} = $frame->unpack_bytes; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
14
|
100
|
|
|
|
34
|
if( $no_metadata ) { |
72
|
2
|
|
|
|
|
12
|
push @columns, undef for 1 .. $n_columns; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
else { |
75
|
12
|
50
|
|
|
|
60
|
my @gts = $has_gts ? ( $frame->unpack_string, $frame->unpack_string ) |
76
|
|
|
|
|
|
|
: (); |
77
|
|
|
|
|
|
|
|
78
|
12
|
|
|
|
|
213
|
foreach ( 1 .. $n_columns ) { |
79
|
22
|
50
|
|
|
|
73
|
my @keyspace_table = $has_gts ? @gts : ( $frame->unpack_string, $frame->unpack_string ); |
80
|
22
|
|
|
|
|
65
|
my $colname = $frame->unpack_string; |
81
|
22
|
|
|
|
|
408
|
my $type = Protocol::CassandraCQL::Type->from_frame( $frame ); |
82
|
|
|
|
|
|
|
|
83
|
22
|
|
|
|
|
74
|
my @col = ( @keyspace_table, $colname, undef, $type ); |
84
|
|
|
|
|
|
|
|
85
|
22
|
|
|
|
|
75
|
push @columns, \@col; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
12
|
|
|
|
|
47
|
$self->_set_shortnames; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
14
|
|
|
|
|
56
|
return $self; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=head2 $meta = Protocol::CassandraCQL::ColumnMeta->new( %args ) |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
Returns a new column metadata object initialised directly from the given |
97
|
|
|
|
|
|
|
column data. This constructor is intended for use by unit test scripts, to |
98
|
|
|
|
|
|
|
create metadata directly from mocked connection objects or similar. |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
It takes the following named arguments: |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=over 8 |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=item columns => ARRAY[ARRAY[STR, STR, STR, STR]] |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
An ARRAY reference containing the data about individual columns. Each row is |
107
|
|
|
|
|
|
|
represented by an ARRAY reference containing four strings; giving the three |
108
|
|
|
|
|
|
|
components of its name, and the name of its type: |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
[ $keyspace, $table, $column, $typename ] |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=back |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=cut |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
sub new |
117
|
|
|
|
|
|
|
{ |
118
|
3
|
|
|
3
|
1
|
722
|
my $class = shift; |
119
|
3
|
|
|
|
|
10
|
my %args = @_; |
120
|
|
|
|
|
|
|
|
121
|
3
|
|
|
|
|
11
|
my $self = bless {}, $class; |
122
|
|
|
|
|
|
|
|
123
|
3
|
|
|
|
|
13
|
$self->{columns} = \my @columns; |
124
|
|
|
|
|
|
|
|
125
|
3
|
|
|
|
|
6
|
foreach my $c ( @{ $args{columns} } ) { |
|
3
|
|
|
|
|
8
|
|
126
|
6
|
|
|
|
|
33
|
push @columns, [ |
127
|
6
|
|
|
|
|
13
|
@{$c}[0,1,2], # name |
128
|
|
|
|
|
|
|
undef, # shortname |
129
|
|
|
|
|
|
|
Protocol::CassandraCQL::Type->from_name( $c->[3] ), |
130
|
|
|
|
|
|
|
]; |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
|
133
|
3
|
|
|
|
|
12
|
$self->_set_shortnames; |
134
|
|
|
|
|
|
|
|
135
|
3
|
|
|
|
|
17
|
return $self; |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
sub _set_shortnames |
139
|
|
|
|
|
|
|
{ |
140
|
15
|
|
|
15
|
|
27
|
my $self = shift; |
141
|
|
|
|
|
|
|
|
142
|
15
|
|
|
|
|
39
|
my $columns = $self->{columns}; |
143
|
|
|
|
|
|
|
|
144
|
15
|
|
|
|
|
62
|
foreach my $idx ( 0 .. $#$columns ) { |
145
|
28
|
|
|
|
|
50
|
my $c = $columns->[$idx]; |
146
|
28
|
|
|
|
|
32
|
my @names; |
147
|
|
|
|
|
|
|
|
148
|
28
|
|
|
|
|
89
|
my $name = "$c->[0].$c->[1].$c->[2]"; |
149
|
28
|
|
|
|
|
164
|
push @names, $name; |
150
|
|
|
|
|
|
|
|
151
|
28
|
|
|
|
|
64
|
$name = "$c->[1].$c->[2]"; |
152
|
28
|
50
|
|
|
|
47
|
push @names, $name if 1 == grep { "$_->[1].$_->[2]" eq $name } @$columns; |
|
58
|
|
|
|
|
232
|
|
153
|
|
|
|
|
|
|
|
154
|
28
|
|
|
|
|
74
|
$name = $c->[2]; |
155
|
28
|
50
|
|
|
|
45
|
push @names, $name if 1 == grep { $_->[2] eq $name } @$columns; |
|
58
|
|
|
|
|
185
|
|
156
|
|
|
|
|
|
|
|
157
|
28
|
|
|
|
|
56
|
$c->[3] = $names[-1]; |
158
|
28
|
|
|
|
|
350
|
$self->{name_to_col}{$_} = $idx for @names; |
159
|
|
|
|
|
|
|
} |
160
|
|
|
|
|
|
|
} |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=head1 METHODS |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=cut |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=head2 $n = $meta->columns |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
Returns the number of columns |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
=cut |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
sub columns |
173
|
|
|
|
|
|
|
{ |
174
|
38
|
|
|
38
|
1
|
6349
|
my $self = shift; |
175
|
38
|
|
|
|
|
50
|
return scalar @{ $self->{columns} }; |
|
38
|
|
|
|
|
159
|
|
176
|
|
|
|
|
|
|
} |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
=head2 $name = $meta->column_name( $idx ) |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
=head2 ( $keyspace, $table, $column ) = $meta->column_name( $idx ) |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
Returns the name of the column at the given (0-based) index; either as three |
183
|
|
|
|
|
|
|
separate strings, or all joined by ".". |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
=cut |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
sub column_name |
188
|
|
|
|
|
|
|
{ |
189
|
7
|
|
|
7
|
1
|
14
|
my $self = shift; |
190
|
7
|
|
|
|
|
9
|
my ( $idx ) = @_; |
191
|
|
|
|
|
|
|
|
192
|
7
|
50
|
33
|
|
|
26
|
croak "No such column $idx" unless $idx >= 0 and $idx < @{ $self->{columns} }; |
|
7
|
|
|
|
|
36
|
|
193
|
7
|
|
|
|
|
14
|
my @n = @{ $self->{columns}[$idx] }[0..2]; |
|
7
|
|
|
|
|
29
|
|
194
|
|
|
|
|
|
|
|
195
|
7
|
100
|
|
|
|
51
|
return @n if wantarray; |
196
|
2
|
|
|
|
|
13
|
return join ".", @n; |
197
|
|
|
|
|
|
|
} |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
=head2 $name = $meta->column_shortname( $idx ) |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
Returns the short name of the column; which will be just the column name |
202
|
|
|
|
|
|
|
unless it requires the table or keyspace name as well to make it unique within |
203
|
|
|
|
|
|
|
the set. |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
=cut |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
sub column_shortname |
208
|
|
|
|
|
|
|
{ |
209
|
28
|
|
|
28
|
1
|
39
|
my $self = shift; |
210
|
28
|
|
|
|
|
34
|
my ( $idx ) = @_; |
211
|
|
|
|
|
|
|
|
212
|
28
|
50
|
33
|
|
|
78
|
croak "No such column $idx" unless $idx >= 0 and $idx < @{ $self->{columns} }; |
|
28
|
|
|
|
|
106
|
|
213
|
28
|
|
|
|
|
598
|
return $self->{columns}[$idx][3]; |
214
|
|
|
|
|
|
|
} |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
=head2 $type = $meta->column_type( $idx ) |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
Returns the type of the column at the given index as an instance of |
219
|
|
|
|
|
|
|
L. |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
=cut |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
sub column_type |
224
|
|
|
|
|
|
|
{ |
225
|
52
|
|
|
52
|
1
|
77
|
my $self = shift; |
226
|
52
|
|
|
|
|
66
|
my ( $idx ) = @_; |
227
|
|
|
|
|
|
|
|
228
|
52
|
50
|
33
|
|
|
132
|
croak "No such column $idx" unless $idx >= 0 and $idx < @{ $self->{columns} }; |
|
52
|
|
|
|
|
202
|
|
229
|
52
|
|
|
|
|
335
|
return $self->{columns}[$idx][4]; |
230
|
|
|
|
|
|
|
} |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
=head2 $idx = $meta->find_column( $name ) |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
Returns the index of the given named column. The name may be given as |
235
|
|
|
|
|
|
|
C, or C or C if they are unique |
236
|
|
|
|
|
|
|
within the set. Returns C if no such column exists. |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
=cut |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
sub find_column |
241
|
|
|
|
|
|
|
{ |
242
|
6
|
|
|
6
|
1
|
10
|
my $self = shift; |
243
|
6
|
|
|
|
|
11
|
my ( $name ) = @_; |
244
|
|
|
|
|
|
|
|
245
|
6
|
|
|
|
|
27
|
return $self->{name_to_col}{$name}; |
246
|
|
|
|
|
|
|
} |
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
=head2 @bytes = $meta->encode_data( @data ) |
249
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
Returns a list of encoded bytestrings from the given data according to the |
251
|
|
|
|
|
|
|
type of each column. Checks each value is valid; if not throws an exception |
252
|
|
|
|
|
|
|
explaining which column failed and why. |
253
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
An exception is thrown if the wrong number of values is passed. |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
=cut |
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
sub encode_data |
259
|
|
|
|
|
|
|
{ |
260
|
4
|
|
|
4
|
1
|
1329
|
my $self = shift; |
261
|
4
|
|
|
|
|
19
|
my @data = @_; |
262
|
|
|
|
|
|
|
|
263
|
4
|
|
|
|
|
4
|
my $n = @{ $self->{columns} }; |
|
4
|
|
|
|
|
9
|
|
264
|
4
|
50
|
|
|
|
16
|
croak "Too many values" if @data > $n; |
265
|
4
|
50
|
|
|
|
9
|
croak "Not enough values" if @data < $n; |
266
|
|
|
|
|
|
|
|
267
|
4
|
|
|
|
|
12
|
foreach my $i ( 0 .. $#data ) { |
268
|
9
|
100
|
|
|
|
21
|
my $e = $self->column_type( $i )->validate( $data[$i] ) or next; |
269
|
|
|
|
|
|
|
|
270
|
2
|
|
|
|
|
10
|
croak "Cannot encode ".$self->column_shortname( $i ).": $e"; |
271
|
|
|
|
|
|
|
} |
272
|
|
|
|
|
|
|
|
273
|
2
|
50
|
|
|
|
7
|
return map { defined $data[$_] ? $self->column_type( $_ )->encode( $data[$_] ) : undef } |
|
6
|
|
|
|
|
29
|
|
274
|
|
|
|
|
|
|
0 .. $n-1; |
275
|
|
|
|
|
|
|
} |
276
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
=head2 @data = $meta->decode_data( @bytes ) |
278
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
Returns a list of decoded data from the given encoded bytestrings according to |
280
|
|
|
|
|
|
|
the type of each column. |
281
|
|
|
|
|
|
|
|
282
|
|
|
|
|
|
|
=cut |
283
|
|
|
|
|
|
|
|
284
|
|
|
|
|
|
|
sub decode_data |
285
|
|
|
|
|
|
|
{ |
286
|
9
|
|
|
9
|
1
|
1391
|
my $self = shift; |
287
|
9
|
|
|
|
|
25
|
my @bytes = @_; |
288
|
|
|
|
|
|
|
|
289
|
9
|
50
|
|
|
|
21
|
return map { defined $bytes[$_] ? $self->column_type( $_ )->decode( $bytes[$_] ) : undef } |
|
17
|
|
|
|
|
201
|
|
290
|
|
|
|
|
|
|
0 .. $#bytes; |
291
|
|
|
|
|
|
|
} |
292
|
|
|
|
|
|
|
|
293
|
|
|
|
|
|
|
=head2 $bytes = $meta->paging_state |
294
|
|
|
|
|
|
|
|
295
|
|
|
|
|
|
|
Returns the CQLv2+ paging state, if it was contained in the given frame. This |
296
|
|
|
|
|
|
|
would be returned in an C message to a query or execute request |
297
|
|
|
|
|
|
|
that requested paging. |
298
|
|
|
|
|
|
|
|
299
|
|
|
|
|
|
|
=cut |
300
|
|
|
|
|
|
|
|
301
|
|
|
|
|
|
|
sub paging_state |
302
|
|
|
|
|
|
|
{ |
303
|
2
|
|
|
2
|
1
|
6
|
my $self = shift; |
304
|
2
|
|
|
|
|
12
|
return $self->{paging_state}; |
305
|
|
|
|
|
|
|
} |
306
|
|
|
|
|
|
|
|
307
|
|
|
|
|
|
|
=head2 $bool = $meta->has_metadata |
308
|
|
|
|
|
|
|
|
309
|
|
|
|
|
|
|
Returns a boolean indicating whether the column metadata (field names and |
310
|
|
|
|
|
|
|
types) is actually defined. Normally this would be true, except if the object |
311
|
|
|
|
|
|
|
is an instance of L returned by executing a |
312
|
|
|
|
|
|
|
prepared statement with metadata specifically disabled. |
313
|
|
|
|
|
|
|
|
314
|
|
|
|
|
|
|
=cut |
315
|
|
|
|
|
|
|
|
316
|
|
|
|
|
|
|
sub has_metadata |
317
|
|
|
|
|
|
|
{ |
318
|
15
|
|
|
15
|
1
|
28
|
my $self = shift; |
319
|
15
|
|
|
|
|
70
|
return defined $self->{columns}[0]; |
320
|
|
|
|
|
|
|
} |
321
|
|
|
|
|
|
|
|
322
|
|
|
|
|
|
|
=head1 SPONSORS |
323
|
|
|
|
|
|
|
|
324
|
|
|
|
|
|
|
This code was paid for by |
325
|
|
|
|
|
|
|
|
326
|
|
|
|
|
|
|
=over 2 |
327
|
|
|
|
|
|
|
|
328
|
|
|
|
|
|
|
=item * |
329
|
|
|
|
|
|
|
|
330
|
|
|
|
|
|
|
Perceptyx L |
331
|
|
|
|
|
|
|
|
332
|
|
|
|
|
|
|
=item * |
333
|
|
|
|
|
|
|
|
334
|
|
|
|
|
|
|
Shadowcat Systems L |
335
|
|
|
|
|
|
|
|
336
|
|
|
|
|
|
|
=back |
337
|
|
|
|
|
|
|
|
338
|
|
|
|
|
|
|
=head1 AUTHOR |
339
|
|
|
|
|
|
|
|
340
|
|
|
|
|
|
|
Paul Evans |
341
|
|
|
|
|
|
|
|
342
|
|
|
|
|
|
|
=cut |
343
|
|
|
|
|
|
|
|
344
|
|
|
|
|
|
|
0x55AA; |