| 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 Google::Cloud::Spanner::V1; | ||
| 16 | |||
| 17 | use strict; | ||
| 18 | use warnings; | ||
| 19 | use Moo; | ||
| 20 | use Google::Spanner::V1::Spanner; | ||
| 21 | use Google::gRPC::Client; | ||
| 22 | use Google::Auth; | ||
| 23 | use Carp qw(croak); | ||
| 24 | |||
| 25 | our $VERSION = '0.03'; | ||
| 26 | |||
| 27 | has credentials => ( is => 'ro', required => 0 ); | ||
| 28 | has transport => ( is => 'rw' ); | ||
| 29 | |||
| 30 | sub BUILD { | ||
| 31 | my ($self) = @_; | ||
| 32 | my $auth = $self->credentials; | ||
| 33 | if (!$auth || !eval { $auth->can('get_token') }) { | ||
| 34 | $auth = Google::Auth->default(); | ||
| 35 | } | ||
| 36 | if (!$self->transport) { | ||
| 37 | my $token = $auth ? eval { $auth->get_token() } : undef; | ||
| 38 | $self->transport(Google::gRPC::Client->new({ | ||
| 39 | target => 'spanner.googleapis.com', | ||
| 40 | auth_token => $token || '', | ||
| 41 | })); | ||
| 42 | } | ||
| 43 | } | ||
| 44 | |||
| 45 | sub new_execute_sql_request { | ||
| 46 | my ($self, $params) = @_; | ||
| 47 | my $msg = Google::Spanner::V1::Spanner::ExecuteSqlRequest->new($params); | ||
| 48 | return $msg->serialize(); | ||
| 49 | } | ||
| 50 | |||
| 51 | sub parse_partial_result_set { | ||
| 52 | my ($self, $bytes) = @_; | ||
| 53 | return Google::Spanner::V1::ResultSet::PartialResultSet->parse($bytes); | ||
| 54 | } | ||
| 55 | |||
| 56 | sub create_session { | ||
| 57 | my ($self, %params) = @_; | ||
| 58 | my $request = Google::Spanner::V1::Spanner::CreateSessionRequest->new(\%params); | ||
| 59 | return $self->transport->call({ | ||
| 60 | service => 'google.spanner.v1.Spanner', | ||
| 61 | method => 'CreateSession', | ||
| 62 | request => $request, | ||
| 63 | response_class => 'Google::Spanner::V1::Spanner::Session', | ||
| 64 | }); | ||
| 65 | } | ||
| 66 | |||
| 67 | sub execute_sql { | ||
| 68 | my ($self, %params) = @_; | ||
| 69 | my $request = Google::Spanner::V1::Spanner::ExecuteSqlRequest->new(\%params); | ||
| 70 | return $self->transport->call({ | ||
| 71 | service => 'google.spanner.v1.Spanner', | ||
| 72 | method => 'ExecuteSql', | ||
| 73 | request => $request, | ||
| 74 | response_class => 'Google::Spanner::V1::ResultSet::ResultSet', | ||
| 75 | }); | ||
| 76 | } | ||
| 77 | |||
| 78 | 1; |