Avatica.pm

filter: Statementstmt 44.9% 22 / 49 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 72.7% 8 / 11 Podpod n/a totaltotal 34.0% 30 / 88 CC 12 SCAR 42.0
CC
12
Cov
30%
_client
132.0
driver
6.0
CLONE
2.0
CRAP 67.0
↑ Summary Avatica/Adapter/Phoenix.pm »
1 package DBD::Avatica;
2  
3 # ABSTRACT: Driver for Apache Avatica compatible servers
4  
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use vars qw($VERSION $err $errstr $sqlstate $drh);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 $VERSION = '0.2.0';
11  
12 $drh = undef;
13  
14 sub driver {
15 0 ▶ return $drh if $drh;
Subroutine driver: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if $DBD::Avatica::drh 0 0 0
16 0 my ($class, $attr) = @_;
17 0 DBI->setup_driver('DBD::Avatica');
18 0 $drh = DBI::_new_drh("${class}::dr", {
19 'Name' => 'Avatica',
20 'Version' => $VERSION,
21 'Err' => \$err,
22 'Errstr' => \$errstr,
23 'State' => \$sqlstate,
24 'Attribution' => "DBD::Avatica $VERSION by skbkontur team"
25 });
26 0 return $drh;
27 }
28  
29 sub CLONE {
30 0 ▶ $drh = undef;
Subroutine CLONE: not called CC=1 SCAR=6.9
31 }
32  
33 # h - some handle, it may be drh, dbh, sth
34 sub _client {
35 0 ▶ my ($h, $method) = (shift, shift);
Subroutine _client: not called CC=11 SCAR=48.8
36  
37 0 my $client = $h->FETCH('avatica_client');
38 0 ▶ return unless $client;
Branch: 1 branch
BranchTrueFalseTotal
unless $client 0 0 0
39  
40 0 my $connection_id = $h->FETCH('avatica_connection_id');
41  
42 0 local $SIG{PIPE} = "IGNORE";
43  
44 0 my $is_trace = $h->trace();
45 0 ▶ $h->trace_msg(join('; ', '--> ', $method, ($connection_id // ()), map { defined ? $_ : 'undef' } @_, "\n")) if $is_trace;
Branch: 2 branches
BranchTrueFalseTotal
if $is_trace 0 0 0
defined $_ ? : 0 0 0
Condition0%0/3
$connection_id // ()
l!l&&r!l&&!r
000
MC/DC0%0/2
$connection_id // ()
$connection_id ()
Truth table0%0/3
A // B
A= $connection_idB= ()
ABresult
1X1
011
000
46  
47 0 ▶ my ($ret, $response) = $client->$method($connection_id // (), @_);
Condition0%0/3
$connection_id // ()
l!l&&r!l&&!r
000
MC/DC0%0/2
$connection_id // ()
$connection_id ()
Truth table0%0/3
A // B
A= $connection_idB= ()
ABresult
1X1
011
000
48  
49 0 ▶ $h->trace_msg(join('; ', '<-- ', $method, $ret ? ref($response)->encode_json($response) : $response, "\n")) if $is_trace;
Branch: 2 branches
BranchTrueFalseTotal
if $is_trace 0 0 0
$ret ? : 0 0 0
50  
51 0 ▶ unless ($ret) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ret) 0 0 0
52 0 ▶ if ($response->{protocol}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->{'protocol'}) { } 0 0 0
53 0 my ($err, $msg, $state) = @{$response->{protocol}}{qw/error_code message sql_state/};
54 0 my $status = $response->{http_status};
55 0 ▶ $msg = "http status $status, error code $err, sql state $state" unless $msg;
Branch: 1 branch
BranchTrueFalseTotal
unless $msg 0 0 0
56 0 $h->set_err($err, $msg, $state);
57 } else {
58 0 $h->set_err(1, $response->{message});
59 }
60 }
61  
62 0 return ($ret, $response);
63 }
64  
65 package DBD::Avatica::dr;
66  
67 our $imp_data_size = 0;
68  
69 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
70 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
71  
72 2 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
73 2 ▶use Avatica::Client;
Subroutine BEGIN: called CC=1 SCAR=2.6
74  
75 *_client = \&DBD::Avatica::_client;
76  
77 sub connect {
78 my ($drh, $dsn, $user, $pass, $attr) = @_;
79  
80 my %dsn = split /[;=]/, $dsn;
81  
82 my $adapter_name = ucfirst($dsn{adapter_name} // '');
83 return $drh->set_err(1, q{Parameter "adapter_name" is required in dsn}) unless $adapter_name;
84 my $adapter_class_path = "DBD/Avatica/Adapter/${adapter_name}.pm";
85 my $adapter_class = "DBD::Avatica::Adapter::${adapter_name}";
86 return $drh->set_err(1, qq{Adapter for adapter_name param $adapter_name not found}) unless eval { require $adapter_class_path; 1};
87  
88 my $url = $dsn{url};
89 $url = 'http://' . $dsn{'hostname'} . ':' . $dsn{'port'} if !$url && $dsn{'hostname'} && $dsn{'port'};
90 return $drh->set_err(1, q{Missing "url" parameter}) unless $url;
91  
92 my %client_params;
93 $client_params{ua} = delete $attr->{UserAgent} if $attr->{UserAgent};
94 $client_params{max_retries} = delete $attr->{MaxRetries} if $attr->{MaxRetries};
95  
96 my $client = Avatica::Client->new(url => $url, %client_params);
97 my $connection_id = _random_str();
98  
99 $drh->{avatica_client} = $client;
100 my ($ret, $response) = _client($drh, 'open_connection', $connection_id);
101 $drh->{avatica_client} = undef;
102  
103 return unless $ret;
104  
105 my ($outer, $dbh) = DBI::_new_dbh($drh, {
106 'Name' => $dsn
107 });
108  
109 my $adapter = $adapter_class->new(dbh => $dbh);
110 $dbh->{avatica_adapter} = $adapter;
111  
112 $dbh->{avatica_pid} = $$;
113  
114 $dbh->STORE(Active => 1);
115  
116 $dbh->{avatica_client} = $client;
117 $dbh->{avatica_connection_id} = $connection_id;
118 my $connections = $drh->{avatica_connections} || [];
119 push @$connections, $dbh;
120 $drh->{avatica_connections} = $connections;
121  
122 for (qw/AutoCommit ReadOnly TransactionIsolation Catalog Schema/) {
123 $dbh->{$_} = delete $attr->{$_} if exists $attr->{$_};
124 }
125 DBD::Avatica::db::_sync_connection_params($dbh);
126 DBD::Avatica::db::_load_database_properties($dbh);
127  
128 $outer;
129 }
130  
131 sub data_sources { }
132  
133 sub disconnect_all {
134 my $drh = shift;
135 my $connections = $drh->{avatica_connections};
136 return unless $connections && @$connections;
137  
138 my ($dbh, $name);
139 while ($dbh = shift @$connections) {
140 $name = $dbh->{Name};
141 $drh->trace_msg("Disconnecting $name\n", 3);
142 $dbh->disconnect();
143 }
144 }
145  
146 sub _random_str {
147 my @alpha = ('0' .. '9', 'a' .. 'z');
148 return join '', @alpha[ map { rand scalar(@alpha) } 1 .. 30 ];
149 }
150  
151 sub STORE {
152 my ($drh, $attr, $value) = @_;
153 if ($attr =~ m/^avatica_/) {
154 $drh->{$attr} = $value;
155 return 1;
156 }
157 return $drh->SUPER::STORE($attr, $value);
158 }
159  
160 sub FETCH {
161 my ($drh, $attr) = @_;
162 if ($attr =~ m/^avatica_/) {
163 return $drh->{$attr};
164 }
165 return $drh->SUPER::FETCH($attr);
166 }
167  
168 package DBD::Avatica::db;
169  
170 our $imp_data_size = 0;
171  
172 use strict;
173 use warnings;
174  
175 use DBI;
176  
177 *_client = \&DBD::Avatica::_client;
178  
179 sub prepare {
180 my ($dbh, $statement, $attr) = @_;
181  
182 my ($ret, $response) = _client($dbh, 'prepare', $statement);
183 return unless $ret;
184  
185 my $stmt = $response->get_statement;
186 my $statement_id = $stmt->get_id;
187 my $signature = $stmt->get_signature;
188  
189 my ($outer, $sth) = DBI::_new_sth($dbh, {'Statement' => $statement});
190  
191 $sth->STORE(NUM_OF_PARAMS => $signature->parameters_size);
192 $sth->STORE(NUM_OF_FIELDS => undef);
193  
194 $sth->{avatica_client} = $dbh->FETCH('avatica_client');
195 $sth->{avatica_connection_id} = $dbh->FETCH('avatica_connection_id');
196 $sth->{avatica_statement_id} = $statement_id;
197 $sth->{avatica_signature} = $signature;
198 $sth->{avatica_params} = $signature->get_parameters_list;
199 $sth->{avatica_rows} = -1;
200 $sth->{avatica_bind_params} = [];
201 $sth->{avatica_data_done} = 1;
202 $sth->{avatica_data} = [];
203  
204 $outer;
205 }
206  
207 sub ping {
208 my $dbh = shift;
209 my $sth = $dbh->prepare_cached('SELECT 1') or return 0;
210 $sth->execute or return 0;
211 $sth->finish;
212 return 1;
213 }
214  
215 sub begin_work {
216 my $dbh = shift;
217 $dbh->{avatica_autocommit_at_begin_work} = $dbh->{AutoCommit};
218 return 1 unless $dbh->{AutoCommit};
219 $dbh->{AutoCommit} = 0;
220 return _sync_connection_params($dbh);
221 }
222  
223 sub commit {
224 my $dbh = shift;
225 return 1 if $dbh->{AutoCommit};
226 my ($ret, $response) = _client($dbh, 'commit');
227 return $ret unless $dbh->{avatica_autocommit_at_begin_work};
228 $dbh->{AutoCommit} = 1;
229 unless (_sync_connection_params($dbh)) {
230 warn 'DBD::Avatica::db commit failed: ' . $dbh->errstr if $dbh->{PrintError};
231 # clear errors of setting autocomit = 1, because commit succeed
232 $dbh->set_err(undef, undef, '');
233 }
234 return $ret;
235 }
236  
237 sub rollback {
238 my $dbh = shift;
239 return 1 if $dbh->{AutoCommit};
240 my ($ret, $response) = _client($dbh, 'rollback');
241 return $ret unless $dbh->{avatica_autocommit_at_begin_work};
242 $dbh->{AutoCommit} = 1;
243 unless (_sync_connection_params($dbh)) {
244 warn 'DBD::Avatica::db rollback failed: ' . $dbh->errstr if $dbh->{PrintError};
245 # clear errors of setting autocomit = 1, because rollback succeed
246 $dbh->set_err(undef, undef, '');
247 }
248 return $ret;
249 }
250  
251 sub last_insert_id {
252 my $dbh = shift;
253 return $dbh->{avatica_adapter}->last_insert_id($dbh, @_);
254 }
255  
256 my %get_info_type = (
257 ## Driver information:
258 6 => ['SQL_DRIVER_NAME', 'DBD::Avatica' ],
259 7 => ['SQL_DRIVER_VER', 'DBD_VERSION' ], # magic word
260 14 => ['SQL_SEARCH_PATTERN_ESCAPE', '\\' ],
261 ## DBMS Information
262 17 => ['SQL_DBMS_NAME', 'DBMS_NAME' ], # magic word
263 18 => ['SQL_DBMS_VERSION', 'DBMS_VERSION' ], # magic word
264 ## Data source information
265 ## Supported SQL
266 114 => ['SQL_CATALOG_LOCATION', 0 ],
267 41 => ['SQL_CATALOG_NAME_SEPARATOR', '' ],
268 28 => ['SQL_IDENTIFIER_CASE', 1 ], # SQL_IC_UPPER
269 29 => ['SQL_IDENTIFIER_QUOTE_CHAR', q{"} ],
270 89 => ['SQL_KEYWORDS', 'SQL_KEYWORDS' ], # magic word
271 ## SQL limits
272 ## Scalar function information
273 ## Conversion information - all but BIT, LONGVARBINARY, and LONGVARCHAR
274 );
275 for (keys %get_info_type) {
276 $get_info_type{$get_info_type{$_}->[0]} = $get_info_type{$_};
277 }
278  
279 sub get_info {
280 my ($dbh, $type) = @_;
281 my $res = $get_info_type{$type}[1];
282  
283 if (grep { $res eq $_ } 'DBMS_NAME', 'DBMS_VERSION', 'SQL_KEYWORDS') {
284 _load_database_properties($dbh) unless $dbh->{avatica_info_type_cache};
285 return $dbh->{avatica_info_type_cache}{$res};
286 }
287  
288 if ($res eq 'DBD_VERSION') {
289 my $v = $DBD::Avatica::VERSION;
290 $v =~ s/_/./g; # 1.12.3_4 strip trial/dev symbols
291 $v =~ s/[^0-9.]//g; # strip trial/dev symbols, a-la "-TRIAL" at the end
292 return sprintf '%02d.%02d.%1d%1d%1d%1d', (split(/\./, "${v}.0.0.0.0.0.0"))[0..5];
293 }
294  
295 return $res;
296 }
297  
298 # returned columns:
299 # TABLE_CAT, TABLE_SCHEM, TABLE_NAME, TABLE_TYPE, REMARKS, TYPE_NAME, SELF_REFERENCING_COL_NAME,
300 # REF_GENERATION, INDEX_STATE, IMMUTABLE_ROWS, SALT_BUCKETS, MULTI_TENANT, VIEW_STATEMENT, VIEW_TYPE,
301 # INDEX_TYPE, TRANSACTIONAL, IS_NAMESPACE_MAPPED, GUIDE_POSTS_WIDTH, TRANSACTION_PROVIDER
302 sub table_info {
303 my $dbh = shift;
304 my ($catalog, $schema, $table, $type) = @_;
305  
306 # minimum number of columns
307 my $cols = ['TABLE_CAT', 'TABLE_SCHEM', 'TABLE_NAME', 'TABLE_TYPE', 'REMARKS'];
308  
309 if (
310 defined $catalog && $catalog eq '%' &&
311 defined $schema && $schema eq '' &&
312 defined $table && $table eq ''
313 ) {
314 # returned columns: TABLE_CAT
315 my ($ret, $response) = _client($dbh, 'catalog');
316 return unless $ret;
317 my $sth = _sth_from_result_set($dbh, 'table_info_catalog', $response);
318 my $rows = $sth->fetchall_arrayref;
319 push @$_, (undef) x 4 for @$rows; # fill to the minimum number of columns
320 return _sth_from_data('table_info_catalog', $rows, $cols);
321 }
322  
323 if (
324 defined $catalog && $catalog eq '' &&
325 defined $schema && $schema eq '%' &&
326 defined $table && $table eq ''
327 ) {
328 # returned columns: TABLE_SCHEM, TABLE_CATALOG
329 my ($ret, $response) = _client($dbh, 'schemas');
330 return unless $ret;
331 my $sth = _sth_from_result_set($dbh, 'table_info_schemas', $response);
332 my $rows = $sth->fetchall_arrayref;
333 $_ = [reverse(@$_), (undef) x 3] for @$rows; # fill to the minimum number of columns
334 return _sth_from_data('table_info_schemas', $rows, $cols);
335 }
336  
337 if (
338 defined $catalog && $catalog eq '' &&
339 defined $schema && $schema eq '' &&
340 defined $table && $table eq '' &&
341 defined $type && $type eq '%'
342 ) {
343 # returned columns: TABLE_TYPE
344 my ($ret, $response) = _client($dbh, 'table_types');
345 return unless $ret;
346 my $sth = _sth_from_result_set($dbh, 'table_info_table_types', $response);
347 my $rows = $sth->fetchall_arrayref;
348 $_ = [(undef) x 3, @$_, undef] for @$rows; # fill to the minimum number of columns
349 return _sth_from_data('table_info_table_types', $rows, $cols);
350 }
351  
352 my ($ret, $response) = _client($dbh, 'tables', $catalog, $schema, $table, $type);
353 return unless $ret;
354 return _sth_from_result_set($dbh, 'table_info', $response);
355 }
356  
357 # returned columns:
358 # TABLE_CAT, TABLE_SCHEM, TABLE_NAME, COLUMN_NAME, DATA_TYPE, TYPE_NAME, COLUMN_SIZE, BUFFER_LENGTH,
359 # DECIMAL_DIGITS, NUM_PREC_RADIX, NULLABLE, REMARKS, COLUMN_DEF, SQL_DATA_TYPE, SQL_DATETIME_SUB,
360 # CHAR_OCTET_LENGTH, ORDINAL_POSITION, IS_NULLABLE, SCOPE_CATALOG, SCOPE_SCHEMA, SCOPE_TABLE,
361 # SOURCE_DATA_TYPE, IS_AUTOINCREMENT, ARRAY_SIZE, COLUMN_FAMILY, TYPE_ID, VIEW_CONSTANT, MULTI_TENANT,
362 # KEY_SEQ
363 sub column_info {
364 my $dbh = shift;
365 my ($catalog, $schema, $table, $column) = @_;
366  
367 my ($ret, $response) = _client($dbh, 'columns', $catalog, $schema, $table, $column);
368 return unless $ret;
369  
370 return _sth_from_result_set($dbh, 'column_info', $response);
371 }
372  
373 # returned columns:
374 # TABLE_CAT, TABLE_SCHEM, TABLE_NAME, COLUMN_NAME, KEY_SEQ, PK_NAME,
375 # ASC_OR_DESC, DATA_TYPE, TYPE_NAME, COLUMN_SIZE, TYPE_ID, VIEW_CONSTANT
376 sub primary_key_info {
377 my ($dbh, $catalog, $schema, $table) = @_;
378  
379 my ($ret, $response) = _client($dbh, 'primary_keys', $catalog, $schema, $table);
380 return unless $ret;
381  
382 # extend signature with database specific columns
383 $dbh->{avatica_adapter}->extend_primary_key_info_signature($response->get_signature);
384  
385 return _sth_from_result_set($dbh, 'primary_keys', $response);
386 }
387  
388 sub foreign_key_info { }
389  
390 sub statistics_info { }
391  
392 sub type_info_all { [] }
393  
394 sub _sth_from_data {
395 my ($statement, $rows, $col_names, %attr) = @_;
396 my $sponge = DBI->connect('dbi:Sponge:', '', '', { RaiseError => 1 });
397 my $sth = $sponge->prepare($statement, { rows=>$rows, NAME=>$col_names, %attr });
398 return $sth;
399 }
400  
401 sub _sth_from_result_set {
402 my ($dbh, $operation, $result_set) = @_;
403  
404 my $statement_id = $result_set->get_statement_id;
405 my $signature = $result_set->get_signature;
406 my $num_columns = $signature->columns_size;
407  
408 my ($outer, $sth) = DBI::_new_sth($dbh, {'Statement' => $operation});
409  
410 my $frame = $result_set->get_first_frame;
411 $sth->{avatica_data_done} = $frame->get_done;
412 $sth->{avatica_data} = $frame->get_rows_list;
413 $sth->{avatica_rows} = 0;
414 $sth->{avatica_client} = $dbh->{avatica_client};
415 $sth->{avatica_connection_id} = $dbh->{avatica_connection_id};
416 $sth->{avatica_statement_id} = $statement_id;
417 $sth->{avatica_signature} = $signature;
418  
419 $sth->STORE(NUM_OF_FIELDS => $num_columns);
420 $sth->STORE(Active => 1);
421  
422 $outer;
423 }
424  
425 sub _sync_connection_params {
426 my $dbh = shift;
427 my %props = map { $_ => $dbh->{$_} }
428 grep { exists $dbh->{$_} }
429 qw/AutoCommit ReadOnly TransactionIsolation Catalog Schema/;
430  
431 my ($ret, $response) = _client($dbh, 'connection_sync', \%props);
432 return unless $ret;
433  
434 my $props = $response->get_conn_props;
435 $dbh->{AutoCommit} = $props->get_auto_commit if $props->has_auto_commit;
436 $dbh->{ReadOnly} = $props->get_read_only if $props->has_read_only;
437 $dbh->{TransactionIsolation} = $props->get_transaction_isolation;
438 $dbh->{Catalog} = $props->get_catalog if $props->get_catalog;
439 $dbh->{Schema} = $props->get_schema if $props->get_schema;
440 return 1;
441 }
442  
443 sub _load_database_properties {
444 my $dbh = shift;
445 my ($ret, $response) = _client($dbh, 'database_property');
446 return unless $ret;
447 my $props = $dbh->{avatica_adapter}->map_database_properties($response->get_props_list);
448 $dbh->{$_} = $props->{$_} for qw/AVATICA_DRIVER_NAME AVATICA_DRIVER_VERSION/;
449 $dbh->{avatica_info_type_cache}{$_} = $props->{$_} for qw/DBMS_NAME DBMS_VERSION SQL_KEYWORDS/;
450 }
451  
452 sub disconnect {
453 my $dbh = shift;
454 return 1 unless $dbh->FETCH('Active');
455 $dbh->STORE(Active => 0);
456  
457 if ($dbh->{avatica_pid} != $$) {
458 $dbh->{avatica_client} = undef;
459 return 1;
460 }
461  
462 my ($ret, $response) = _client($dbh, 'close_connection');
463 $dbh->{avatica_client} = undef;
464  
465 return $ret;
466 }
467  
468 sub STORE {
469 my ($dbh, $attr, $value) = @_;
470 if (grep { $attr eq $_ } ('AutoCommit', 'ReadOnly', 'TransactionIsolation', 'Catalog', 'Schema')) {
471 $dbh->{$attr} = $value;
472 _sync_connection_params($dbh);
473 return 1;
474 }
475 if ($attr =~ m/^avatica_/) {
476 $dbh->{$attr} = $value;
477 return 1;
478 }
479 # READ ONLY attributes
480 return 0 if grep { $attr eq $_ } qw/AVATICA_DRIVER_NAME AVATICA_DRIVER_VERSION/;
481 return $dbh->SUPER::STORE($attr, $value);
482 }
483  
484 sub FETCH {
485 my ($dbh, $attr) = @_;
486 if ($attr =~ m/^avatica_/) {
487 return $dbh->{$attr};
488 }
489 if (grep { $attr eq $_ }
490 qw/AutoCommit ReadOnly TransactionIsolation Catalog Schema AVATICA_DRIVER_NAME AVATICA_DRIVER_VERSION/) {
491 return $dbh->{$attr};
492 }
493 return $dbh->SUPER::FETCH($attr);
494 }
495  
496 sub DESTROY {
497 my $dbh = shift;
498 return unless $dbh->FETCH('Active');
499 return if $dbh->FETCH('InactiveDestroy');
500 eval { $dbh->disconnect() };
501 }
502  
503 package DBD::Avatica::st;
504  
505 our $imp_data_size = 0;
506  
507 use strict;
508 use warnings;
509  
510 use DBI;
511  
512 use constant FETCH_SIZE => 2000;
513  
514 *_client = \&DBD::Avatica::_client;
515  
516  
517 sub bind_param {
518 my ($sth, $param, $value, $attr) = @_;
519  
520 # at the moment the type is not processed because we know type from prepare request
521 # my ($type) = (ref $attr) ? $attr->{'TYPE'} : $attr;
522  
523 my $params = $sth->{avatica_bind_params};
524 $params->[$param - 1] = $value;
525 1;
526 }
527  
528 sub execute {
529 my ($sth, @bind_values) = @_;
530  
531 my $bind_params = $sth->{avatica_bind_params};
532 @bind_values = @$bind_params if !@bind_values && $bind_params && @$bind_params;
533  
534 my $num_params = $sth->FETCH('NUM_OF_PARAMS');
535 return $sth->set_err(1, 'Wrong number of parameters') if @bind_values != $num_params;
536  
537 my $statement_id = $sth->{avatica_statement_id};
538 my $signature = $sth->{avatica_signature};
539  
540 my $dbh = $sth->{Database};
541 my $mapped_params = $dbh->{avatica_adapter}->row_to_jdbc(\@bind_values, $sth->{avatica_params});
542  
543 my ($ret, $response) = _client($sth, 'execute', $statement_id, $signature, $mapped_params, DBD::Avatica::st->FETCH_SIZE);
544 unless ($ret) {
545 return if $num_params != 0 || index($response->{message}, 'NullPointerException') == -1;
546  
547 # https://issues.apache.org/jira/browse/CALCITE-4900
548 # so, workaround, if num_params == 0 then need to use create_statement && prepare_and_execute without params
549  
550 # clear errors
551 $sth->set_err(undef, undef, '');
552  
553 my $sql = $sth->FETCH('Statement');
554  
555 ($ret, $response) = _client($sth, 'create_statement');
556 return unless $ret;
557  
558 $statement_id = $sth->{avatica_statement_id} = $response->get_statement_id;
559  
560 ($ret, $response) = _client($sth, 'prepare_and_execute', $statement_id, $sql, undef, DBD::Avatica::st->FETCH_SIZE);
561 return unless $ret;
562 }
563  
564 my $result = $response->get_results(0);
565  
566 if ($result->get_own_statement) {
567 my $new_statement_id = $result->get_statement_id;
568 _avatica_close_statement($sth) if $statement_id && $statement_id != $new_statement_id;
569 $sth->{avatica_statement_id} = $new_statement_id;
570 }
571  
572 $signature = $result->get_signature;
573 $sth->{avatica_signature} = $signature if $signature;
574  
575 my $num_updates = $result->get_update_count;
576 $num_updates = -1 if $num_updates == '18446744073709551615'; # max_int
577  
578 if ($num_updates >= 0) {
579 # DML
580 $sth->STORE(Active => 0);
581 $sth->STORE(NUM_OF_FIELDS => 0);
582 $sth->{avatica_rows} = $num_updates;
583 $sth->{avatica_data_done} = 1;
584 $sth->{avatica_data} = [];
585 return $num_updates == 0 ? '0E0' : $num_updates;
586 }
587  
588 # SELECT
589 my $frame = $result->get_first_frame;
590 $sth->{avatica_data_done} = $frame->get_done;
591 $sth->{avatica_data} = $frame->get_rows_list;
592 $sth->{avatica_rows} = 0;
593  
594 my $num_columns = $signature->columns_size;
595 $sth->STORE(Active => 1);
596 $sth->STORE(NUM_OF_FIELDS => $num_columns);
597  
598 return 1;
599 }
600  
601 sub fetch {
602 my ($sth) = @_;
603  
604 my $signature = $sth->{avatica_signature};
605  
606 my $avatica_rows_list = $sth->{avatica_data};
607 my $avatica_rows_done = $sth->{avatica_data_done};
608  
609 if ((!$avatica_rows_list || !@$avatica_rows_list) && !$avatica_rows_done) {
610 my $statement_id = $sth->{avatica_statement_id};
611 my ($ret, $response) = _client($sth, 'fetch', $statement_id, undef, DBD::Avatica::st->FETCH_SIZE);
612 return unless $ret;
613  
614 my $frame = $response->get_frame;
615 $sth->{avatica_data_done} = $frame->get_done;
616 $sth->{avatica_data} = $frame->get_rows_list;
617  
618 $avatica_rows_done = $sth->{avatica_data_done};
619 $avatica_rows_list = $sth->{avatica_data};
620 }
621  
622 if ($avatica_rows_list && @$avatica_rows_list) {
623 $sth->{avatica_rows} += 1;
624 my $dbh = $sth->{Database};
625 my $avatica_row = shift @$avatica_rows_list;
626 my $values = $avatica_row->get_value_list;
627 my $columns = $signature->get_columns_list;
628 my $row = $dbh->{avatica_adapter}->row_from_jdbc($values, $columns);
629 return $sth->_set_fbav($row);
630 }
631  
632 $sth->finish;
633 return;
634 }
635 *fetchrow_arrayref = \&fetch;
636  
637 sub rows {
638 shift->{avatica_rows}
639 }
640  
641 # It seems that here need to call _avatica_close_statement method,
642 # but then such a scenario will not work
643 # when there are many "execute" commands for one "prepare" command.
644 # Therefore, we will not do this here.
645 sub finish {
646 my $sth = shift;
647 $sth->STORE(Active => 0);
648 1;
649 }
650  
651 sub STORE {
652 my ($sth, $attr, $value) = @_;
653 if ($attr =~ m/^avatica_/) {
654 $sth->{$attr} = $value;
655 return 1;
656 }
657 return $sth->SUPER::STORE($attr, $value);
658 }
659  
660 sub FETCH {
661 my ($sth, $attr) = @_;
662 if ($attr =~ m/^avatica_/) {
663 return $sth->{$attr};
664 }
665 if ($attr eq 'NAME') {
666 return $sth->{avatica_cache_name} ||=
667 [map { $_->get_column_name } @{$sth->{avatica_signature}->get_columns_list}];
668 }
669 if ($attr eq 'TYPE') {
670 my $dbh = $sth->{Database};
671 return $sth->{avatica_cache_type} ||=
672 [map { $dbh->{avatica_adapter}->to_dbi($_->get_type) } @{$sth->{avatica_signature}->get_columns_list}];
673 }
674 if ($attr eq 'PRECISION') {
675 return $sth->{avatica_cache_precision} ||=
676 [map { $_->get_display_size } @{$sth->{avatica_signature}->get_columns_list}];
677 }
678 if ($attr eq 'SCALE') {
679 return $sth->{avatica_cache_scale} ||=
680 [map { $_->get_scale || undef } @{$sth->{avatica_signature}->get_columns_list}];
681 }
682 if ($attr eq 'NULLABLE') {
683 return $sth->{avatica_cache_nullable} ||=
684 [map { $_->get_nullable} @{$sth->{avatica_signature}->get_columns_list}];
685 }
686 if ($attr eq 'ParamValues') {
687 return $sth->{avatica_cache_paramvalues} ||=
688 {map { $_ => ($sth->{avatica_bind_params}->[$_ - 1] // undef) } 1 .. @{$sth->{avatica_params} // []}};
689 }
690 return $sth->SUPER::FETCH($attr);
691 }
692  
693 sub _avatica_close_statement {
694 my $sth = shift;
695 my $statement_id = $sth->{avatica_statement_id};
696 _client($sth, 'close_statement', $statement_id) if $statement_id && $sth->FETCH('Database')->{avatica_pid} == $$;
697 $sth->{avatica_statement_id} = undef;
698 }
699  
700 sub DESTROY {
701 my $sth = shift;
702 return if $sth->FETCH('InactiveDestroy');
703 return unless $sth->FETCH('Database')->FETCH('Active');
704 eval { _avatica_close_statement($sth) };
705 $sth->finish;
706 }
707  
708 1;
709  
710 __END__
↑ Summary Avatica/Adapter/Phoenix.pm »