App Engine Java SDK version 1.9.8
[gae.git] / java / src / main / com / google / appengine / api / rdbms / RdbmsApiProxyClient.java
blob19ffb2b43886bb0eb0753e245982ac4dfd166dc7
1 // Copyright 2010 Google Inc. All Rights Reserved.
3 package com.google.appengine.api.rdbms;
5 import com.google.apphosting.api.ApiProxy;
6 import com.google.cloud.sql.jdbc.internal.Exceptions;
7 import com.google.cloud.sql.jdbc.internal.SqlProtoClient;
8 import com.google.cloud.sql.jdbc.internal.SqlRpc;
9 import com.google.cloud.sql.jdbc.internal.SqlRpcOptions;
10 import com.google.cloud.sql.jdbc.internal.SqlState;
11 import com.google.protobuf.InvalidProtocolBufferException;
12 import com.google.protobuf.Message;
13 import com.google.protobuf.MessageLite;
14 import com.google.protos.cloud.sql.CloseConnectionRequest;
15 import com.google.protos.cloud.sql.CloseConnectionResponse;
16 import com.google.protos.cloud.sql.ExecOpRequest;
17 import com.google.protos.cloud.sql.ExecOpResponse;
18 import com.google.protos.cloud.sql.ExecRequest;
19 import com.google.protos.cloud.sql.ExecResponse;
20 import com.google.protos.cloud.sql.MetadataRequest;
21 import com.google.protos.cloud.sql.MetadataResponse;
22 import com.google.protos.cloud.sql.OpenConnectionRequest;
23 import com.google.protos.cloud.sql.OpenConnectionResponse;
25 import java.sql.SQLException;
26 import java.util.concurrent.TimeUnit;
28 /**
29 * A {@link SqlProtoClient} extension that uses the app engine
30 * {@link ApiProxy}.
33 class RdbmsApiProxyClient extends SqlProtoClient {
35 static final String PACKAGE = "rdbms";
37 RdbmsApiProxyClient(String rdbmsInstance) {
38 super(rdbmsInstance, new ApiProxyBlockingInterface());
41 static final class ApiProxyBlockingInterface implements SqlRpc {
43 ApiProxyBlockingInterface() {
46 @Override
47 public ExecResponse exec(SqlRpcOptions options, ExecRequest request)
48 throws SQLException {
49 return makeSyncCall("Exec", request, createApiConfig(options.getQueryTimeOutMillis()),
50 ExecResponse.newBuilder()).build();
53 @Override
54 public ExecOpResponse execOp(SqlRpcOptions options, ExecOpRequest request)
55 throws SQLException {
56 return makeSyncCall("ExecOp", request, createApiConfig(options.getQueryTimeOutMillis()),
57 ExecOpResponse.newBuilder()).build();
60 @Override
61 public MetadataResponse getMetadata(SqlRpcOptions options,
62 MetadataRequest request) throws SQLException {
63 return makeSyncCall(
64 "GetMetadata", request, createApiConfig(options.getQueryTimeOutMillis()),
65 MetadataResponse.newBuilder()).build();
68 @Override
69 public OpenConnectionResponse openConnection(SqlRpcOptions options,
70 OpenConnectionRequest request) throws SQLException {
71 return makeSyncCall(
72 "OpenConnection", request, createApiConfig(options.getConnectTimeOutMillis()),
73 OpenConnectionResponse.newBuilder()).build();
76 @Override
77 public CloseConnectionResponse closeConnection(SqlRpcOptions options,
78 CloseConnectionRequest request) throws SQLException {
79 return makeSyncCall(
80 "CloseConnection", request, createApiConfig(options.getQueryTimeOutMillis()),
81 CloseConnectionResponse.newBuilder()).build();
84 private static ApiProxy.ApiConfig createApiConfig(long deadlineMillis) {
85 ApiProxy.ApiConfig config = new ApiProxy.ApiConfig();
86 config.setDeadlineInSeconds((double) TimeUnit.MILLISECONDS.toSeconds(deadlineMillis));
87 return config;
90 <T extends Message.Builder> T makeSyncCall(String method, MessageLite request,
91 ApiProxy.ApiConfig apiConfig, T responseBuilder) throws SQLException {
92 try {
93 byte[] responseBytes =
94 ApiProxy.makeSyncCall(PACKAGE, method, request.toByteArray(), apiConfig);
95 if (responseBytes != null) {
96 try {
97 responseBuilder.mergeFrom(responseBytes);
98 } catch (InvalidProtocolBufferException e) {
99 throw new SQLException(e.getMessage(), e);
102 } catch (ApiProxy.ApiDeadlineExceededException exception) {
103 throw Exceptions.newTimeoutException();
104 } catch (ApiProxy.ApplicationException exception) {
105 String sqlstate = method.equals("OpenConnection")
106 ? SqlState.forOpenConnectionError(exception.getApplicationError())
107 : SqlState.forError(exception.getApplicationError());
108 throw new SQLException(
109 exception.getErrorDetail(), sqlstate, exception.getApplicationError());
111 return responseBuilder;