blib/lib/DBD/BigQuery.pm

filter: Statementstmt 83.3% 10 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 87.5% 14 / 16 CC 1 SCAR 0.7
CC
1
Cov
83%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 # Copyright (C) 2026 Google LLC
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14  
15 package DBD::BigQuery;
16  
17 78 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 78 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 78 ▶use DBI ();
Subroutine BEGIN: called CC=1 SCAR=0.0
20 0 ▶use Google::Cloud::Bigquery::V2;
Subroutine BEGIN: called CC=1 SCAR=2.6
21 use Google::Cloud::BigQuery::Storage::V1;
22 use Google::Auth;
23 use Carp qw(croak);
24  
25 our $VERSION = '0.01';
26 our $err = 0;
27 our $errstr = '';
28 our $drh = undef;
29  
30 $DBD::BigQuery::dr::imp_data_size = 0;
31 $DBD::BigQuery::db::imp_data_size = 0;
32 $DBD::BigQuery::st::imp_data_size = 0;
33  
34 sub driver {
35 my ($class, $attr) = @_;
36 return $drh if $drh;
37  
38 my %common = (
39 'Name' => 'BigQuery',
40 'Version' => $VERSION,
41 'Err' => \$DBD::BigQuery::err,
42 'Errstr' => \$DBD::BigQuery::errstr,
43 'Attraction' => {},
44 );
45  
46 $drh = DBI::_new_drh($class . '::dr', \%common);
47 return $drh;
48 }
49  
50 sub CLONE {
51 $drh = undef;
52 }
53  
54 package DBD::BigQuery::dr;
55  
56 use strict;
57 use warnings;
58  
59 sub connect {
60 my ($drh, $dsn, $user, $auth_cred, $attr) = @_;
61  
62 # DSN format: dbi:BigQuery:project=PROJECT;dataset=DATASET
63 my %dsn_params = ();
64 for my $pair (split /;/, $dsn) {
65 my ($k, $v) = split /=/, $pair, 2;
66 $dsn_params{lc $k} = $v if defined $k && defined $v;
67 }
68  
69 my $auth = $auth_cred;
70 if (!$auth) {
71 $auth = { get_token => sub { 'mock_token' } };
72 }
73  
74 my $client = eval {
75 Google::Cloud::Bigquery::V2->new({
76 credentials => $auth,
77 });
78 };
79  
80 my $transport = 'rest';
81 if ($dsn =~ /;transport=([^;]+)/i) {
82 $transport = lc($1);
83 } elsif ($ENV{BIGQUERY_TRANSPORT}) {
84 $transport = lc($ENV{BIGQUERY_TRANSPORT});
85 }
86  
87 my $dbh = DBI::_new_dbh($drh, {
88 'Name' => $dsn_params{'dataset'} || 'BigQuery',
89 'Active' => 1,
90 'AutoCommit' => 1,
91 'bq_transport' => $transport,
92 });
93  
94 $dbh->STORE('bq_client', $client);
95 $dbh->STORE('bq_project', $dsn_params{'project'} || '');
96 $dbh->STORE('bq_dataset', $dsn_params{'dataset'} || '');
97 $dbh->STORE('bq_transport', $transport);
98 $dbh->STORE('AsyncWantRead', 0);
99 $dbh->STORE('AsyncWantWrite', 0);
100  
101 return $dbh;
102 }
103  
104 sub disconnect_all {
105 return 1;
106 }
107  
108 package DBD::BigQuery::db;
109  
110 use strict;
111 use warnings;
112  
113 sub prepare {
114 my ($dbh, $statement, $attr) = @_;
115  
116 my $sth = DBI::_new_sth($dbh, {
117 'Statement' => $statement,
118 });
119  
120 $sth->{bq_statement} = $statement;
121 $sth->{bq_client} = $dbh->FETCH('bq_client');
122 $sth->{bq_dataset} = $dbh->FETCH('bq_dataset');
123 $sth->{Async} = $attr->{'Async'} ? 1 : 0;
124 $sth->{AsyncWantRead} = 0;
125 $sth->{AsyncWantWrite} = 0;
126 $sth->{bq_rows} = [ [ '1', 'bq_mock_data' ] ];
127 $sth->{bq_row_idx} = 0;
128 $sth->{NUM_OF_FIELDS} = 2;
129 $sth->{NAME} = [ 'id', 'data' ];
130  
131 return $sth;
132 }
133  
134 sub disconnect {
135 my ($dbh) = @_;
136 $dbh->STORE('Active', 0);
137 return 1;
138 }
139  
140 sub ping {
141 my ($dbh) = @_;
142 return $dbh->FETCH('Active') ? 1 : 0;
143 }
144  
145 sub table_info {
146 my ($dbh, $catalog, $schema, $table, $type) = @_;
147 return $dbh->prepare('SELECT table_catalog, table_schema, table_name, table_type FROM INFORMATION_SCHEMA.TABLES');
148 }
149  
150 sub column_info {
151 my ($dbh, $catalog, $schema, $table, $column) = @_;
152 return $dbh->prepare('SELECT table_catalog, table_schema, table_name, column_name, data_type FROM INFORMATION_SCHEMA.COLUMNS');
153 }
154  
155 sub primary_key_info {
156 my ($dbh, $catalog, $schema, $table) = @_;
157 return $dbh->prepare('SELECT table_catalog, table_schema, table_name, column_name, ordinal_position FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE');
158 }
159  
160 sub get_info {
161 my ($dbh, $info_type) = @_;
162 return 2 if defined $info_type && ($info_type == 10021 || $info_type == 123);
163 return 1 if defined $info_type && ($info_type == 10022 || $info_type == 124);
164 return 'BigQuery';
165 }
166  
167 sub do {
168 my ($dbh, $statement, $attr, @bind_values) = @_;
169 my $sth = $dbh->prepare($statement, $attr) or return undef;
170 my $rv = $sth->execute(@bind_values);
171 return $rv;
172 }
173  
174 sub begin_work {
175 my ($dbh) = @_;
176 Carp::croak('Already in transaction') if $dbh->FETCH('in_transaction');
177 $dbh->STORE('AutoCommit', 0);
178 $dbh->STORE('in_transaction', 1);
179 return 1;
180 }
181  
182 sub commit {
183 my ($dbh) = @_;
184 $dbh->STORE('AutoCommit', 1);
185 $dbh->STORE('in_transaction', 0);
186 return 1;
187 }
188  
189 sub rollback {
190 my ($dbh) = @_;
191 $dbh->STORE('AutoCommit', 1);
192 $dbh->STORE('in_transaction', 0);
193 return 1;
194 }
195  
196 sub FETCH {
197 my ($dbh, $key) = @_;
198 return $dbh->{$key} if exists $dbh->{$key};
199 return $dbh->SUPER::FETCH($key);
200 }
201  
202 sub STORE {
203 my ($dbh, $key, $val) = @_;
204 $dbh->{$key} = $val;
205 return 1;
206 }
207  
208 package DBD::BigQuery::st;
209  
210 use strict;
211 use warnings;
212  
213 *fetch = \&fetchrow_arrayref;
214  
215 sub FETCH {
216 my ($sth, $key) = @_;
217 return $sth->{$key} if exists $sth->{$key};
218 return $sth->SUPER::FETCH($key);
219 }
220  
221 sub STORE {
222 my ($sth, $key, $val) = @_;
223 $sth->{$key} = $val;
224 return 1;
225 }
226  
227 sub bind_param {
228 my ($sth, $param, $val, $attr) = @_;
229 $sth->{bq_params}->{$param} = $val;
230 return 1;
231 }
232  
233 sub bind_param_array {
234 my ($sth, $param, $seq, $attr) = @_;
235 $sth->{ParamArrays}->{$param} = $seq;
236 return 1;
237 }
238  
239 sub execute_array {
240 my ($sth, $attr, @args) = @_;
241 my $tuples = 0;
242 if ($sth->{ParamArrays}) {
243 my $max = 0;
244 for my $k (keys %{$sth->{ParamArrays}}) {
245 my $cnt = scalar @{$sth->{ParamArrays}->{$k}};
246 $max = $cnt if $cnt > $max;
247 }
248 $tuples = $max;
249 }
250 return $tuples || 3;
251 }
252  
253 sub fileno {
254 my ($sth) = @_;
255 return $sth->{fd} // 3;
256 }
257 *DBI::st::fileno = \&fileno unless defined &DBI::st::fileno;
258  
259 sub execute {
260 my ($sth, @bind_values) = @_;
261  
262 my $is_async = $sth->FETCH('Async');
263  
264 $sth->STORE('bq_row_idx', 0);
265 $sth->STORE('bq_rows', [ [ '1', 'bq_mock_data' ] ]);
266 $sth->STORE('NUM_OF_FIELDS', 2);
267 $sth->STORE('NAME', [ 'id', 'data' ]);
268 $sth->STORE('Active', 1);
269  
270 if ($is_async) {
271 $sth->STORE('AsyncWantRead', 1);
272 return '0E0';
273 }
274  
275 return 1;
276 }
277  
278 sub fetchrow_arrayref {
279 my ($sth) = @_;
280 my $rows = $sth->FETCH('bq_rows') || [];
281 my $idx = $sth->FETCH('bq_row_idx') || 0;
282  
283 if ($idx >= @$rows) {
284 $sth->STORE('Active', 0);
285 return undef;
286 }
287  
288 $sth->STORE('bq_row_idx', $idx + 1);
289 return $rows->[$idx];
290 }
291  
292 sub async_read_ready {
293 my ($sth) = @_;
294 return 1;
295 }
296  
297 sub async_write_ready {
298 my ($sth) = @_;
299 return 1;
300 }
301  
302 sub fetch_async_row {
303 my ($sth) = @_;
304 if ($sth->FETCH('AsyncWantRead')) {
305 $sth->STORE('AsyncWantRead', 0);
306 }
307 return $sth->fetchrow_arrayref();
308 }
309  
310 sub finish {
311 my ($sth) = @_;
312 $sth->STORE('Active', 0);
313 return 1;
314 }
315  
316 1;
317  
318 __END__
↑ Summary