com.julienviet.pgclient.impl.PrepareStatementCommand Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of reactive-pg-client Show documentation
Show all versions of reactive-pg-client Show documentation
The reactive Postgres client
/*
* Copyright (C) 2017 Julien Viet
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.julienviet.pgclient.impl;
import com.julienviet.pgclient.PgException;
import com.julienviet.pgclient.impl.codec.decoder.DecodeContext;
import com.julienviet.pgclient.impl.codec.decoder.InboundMessage;
import com.julienviet.pgclient.impl.codec.decoder.message.*;
import com.julienviet.pgclient.impl.codec.encoder.message.Describe;
import com.julienviet.pgclient.impl.codec.encoder.message.Parse;
import com.julienviet.pgclient.impl.codec.encoder.message.Sync;
import io.vertx.core.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import java.util.Map;
public class PrepareStatementCommand extends CommandBase {
final String sql;
private long statement; // 0 means unamed statement otherwise CString
private ParameterDescription parameterDesc;
private RowDescription rowDesc;
private SocketConnection.CachedPreparedStatement cached;
private Handler> handler;
PrepareStatementCommand(String sql, Handler> handler) {
super(null); // Not pretty but well, that's fine for now
this.sql = sql;
this.handler = handler;
super.handler = ar -> {
handler.handle(ar);
if (cached != null) {
cached.fut.handle(ar);
}
};
}
@Override
void foo(SocketConnection conn) {
Map psCache = conn.psCache;
if (psCache != null) {
cached = psCache.get(sql);
if (cached != null) {
cached.get(handler);
} else {
statement = conn.psSeq.next();
cached = new SocketConnection.CachedPreparedStatement();
psCache.put(sql, this.cached);
super.foo(conn);
}
} else {
super.foo(conn);
}
}
@Override
void exec(SocketConnection conn) {
conn.decodeQueue.add(new DecodeContext(false, null, null, null));
conn.writeMessage(new Parse(sql).setStatement(statement));
conn.writeMessage(new Describe().setStatement(statement));
conn.writeMessage(Sync.INSTANCE);
}
@Override
public void handleMessage(InboundMessage msg) {
if (msg.getClass() == ParseComplete.class) {
// Response to Parse
} else if (msg.getClass() == ParameterDescription.class) {
// Response to Describe
parameterDesc = (ParameterDescription) msg;
} else if (msg.getClass() == RowDescription.class) {
// Response to Describe
rowDesc = (RowDescription) msg;
} else if (msg.getClass() == NoData.class) {
// Response to Describe
} else if (msg.getClass() == ErrorResponse.class) {
ErrorResponse error = (ErrorResponse) msg;
failure = new PgException(error);
} else {
if (msg.getClass() == ReadyForQuery.class) {
result = new PreparedStatement(sql, statement, parameterDesc, rowDesc);
}
super.handleMessage(msg);
}
}
@Override
void fail(Throwable err) {
Future failure = Future.failedFuture(err);
handler.handle(failure);
if (cached != null) {
cached.fut.handle(failure);
}
}
}