File: st.pm

package info (click to toggle)
libdbd-cassandra-perl 0.57-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 196 kB
  • sloc: perl: 286; makefile: 2
file content (153 lines) | stat: -rw-r--r-- 3,389 bytes parent folder | download | duplicates (2)
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
package DBD::Cassandra::st;
our $AUTHORITY = 'cpan:TVDW';
$DBD::Cassandra::st::VERSION = '0.57';
# ABSTRACT: DBD::Cassandra statement handle

use 5.010;
use strict;
use warnings;

# Documentation-driven cargocult
$DBD::Cassandra::st::imp_data_size = 0;

sub bind_param {
    my ($sth, $pNum, $val, $attr)= @_;
    my $params= ($sth->{cass_params} ||= []);
    $params->[$pNum-1] = $val;
    1;
}

sub execute {
    my ($sth, @bind_values)= @_;

    $sth->{cass_bind}= (@bind_values ? \@bind_values : $sth->{cass_params});
    &start_async;
    $sth->STORE('Active', 1);
    if (!$sth->{cass_async}) {
        return &x_finish_async;
    }

    return '0E0';
}

sub start_async {
    my ($sth)= @_;

    $sth->{cass_future}= $sth->{Database}{cass_client}->future_call_execute($sth->{Statement}, $sth->{cass_bind}, {
        consistency => $sth->{cass_consistency},
        page_size => $sth->{cass_page_size},
        page => $sth->{cass_next_page},
    });
}

sub x_finish_async {
    my ($sth)= @_;

    my $future= delete $sth->{cass_future};
    if (!$future) { return '0E0'; }

    my ($error, $result)= $future->();
    if ($error) {
        $sth->STORE('Active', 0);
        return $sth->set_err($DBI::stderr, $error);
    }

    my $rows= ($result && $result->rows) || [];
    my $names= ($result && $result->column_names) || [];
    my $page= ($result && $result->next_page);

    $sth->{rows}= $rows;
    $sth->{row_count}= 0+@$rows;

    if (!@$rows && !@$names) {
        # This is a weird Cassandra corner-case, triggered by doing 'list permissions' etc when there are none.
        # Our code is fine with it, but DBI wants to have at least one column. So let's give it one.
        @$names= ('no_column_names_returned_by_cassandra');
    }

    $sth->STORE('NUM_OF_FIELDS', 0+@$names);
    $sth->{NAME}= $names;
    $sth->{cass_next_page}= $page;

    return ((0+@$rows) || '0E0');
}

sub execute_for_fetch {
    die 'Not implemented'; #TODO
}

sub bind_param_array {
    die 'Not implemented'; #TODO
}

sub fetchrow_arrayref {
    my ($sth)= @_;
    if ($sth->{cass_future}) {
        return undef unless &x_finish_async;
    }

    my $row= shift @{$sth->{rows}};
    if (!$row && $sth->{cass_next_page}) {
        &start_async;
        if (!&x_finish_async) {
            return undef;
        }
        $row= shift @{$sth->{rows}};
    }
    if ($row) {
        if ($sth->FETCH('ChopBlanks')) {
            map { $_ =~ s/\s+$//; } @$row;
        }

        return $sth->_set_fbav($row);
    }

    $sth->STORE('Active', 0);
    return undef;
}

*fetch = \&fetchrow_arrayref;

sub rows {
    my $sth= shift;
    if ($sth->{cass_future}) {
        return undef unless &x_finish_async;
    }
    return $sth->{row_count};
}

sub FETCH {
    my ($sth, $attr)= @_;
    if ($attr =~ /\A(?:NAME|NUM_OF_FIELDS)\z/ && $sth->{cass_future}) {
        return undef unless &x_finish_async;
        return $sth->{$attr} if $attr eq 'NAME';
    }
    return $sth->SUPER::FETCH($attr);
}

1;

__END__

=pod

=head1 NAME

DBD::Cassandra::st - DBD::Cassandra statement handle

=head1 VERSION

version 0.57

=head1 AUTHOR

Tom van der Woerdt <tvdw@cpan.org>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2017 by Tom van der Woerdt.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut