/* * Copyright 2014-2015 the original author or authors * * 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.wplatform.ddal.command; import java.util.ArrayList; import com.wplatform.ddal.command.expression.Parameter; import com.wplatform.ddal.command.expression.ParameterInterface; import com.wplatform.ddal.result.ResultInterface; import com.wplatform.ddal.value.Value; import com.wplatform.ddal.value.ValueNull; /** * Represents a single SQL statements. * It wraps a prepared statement. */ class CommandContainer extends Command { private Prepared prepared; private boolean readOnlyKnown; private boolean readOnly; CommandContainer(Parser parser, String sql, Prepared prepared) { super(parser, sql); prepared.setCommand(this); this.prepared = prepared; } @Override public ArrayList<? extends ParameterInterface> getParameters() { return prepared.getParameters(); } @Override public boolean isTransactional() { return prepared.isTransactional(); } @Override public boolean isQuery() { return prepared.isQuery(); } private void recompileIfRequired() { if (prepared.needRecompile()) { // TODO test with 'always recompile' String sql = prepared.getSQL(); ArrayList<Parameter> oldParams = prepared.getParameters(); Parser parser = new Parser(session); prepared = parser.parse(sql); ArrayList<Parameter> newParams = prepared.getParameters(); for (int i = 0, size = newParams.size(); i < size; i++) { Parameter old = oldParams.get(i); if (old.isValueSet()) { Value v = old.getValue(session); Parameter p = newParams.get(i); p.setValue(v); } } prepared.prepare(); } } @Override public int update() { recompileIfRequired(); start(); session.setLastScopeIdentity(ValueNull.INSTANCE); prepared.checkParameters(); int updateCount = prepared.update(); prepared.trace(startTime, updateCount); return updateCount; } @Override public ResultInterface query(int maxrows) { recompileIfRequired(); start(); prepared.checkParameters(); ResultInterface result = prepared.query(maxrows); prepared.trace(startTime, result.getRowCount()); return result; } @Override public boolean isReadOnly() { if (!readOnlyKnown) { readOnly = prepared.isReadOnly(); readOnlyKnown = true; } return readOnly; } @Override public ResultInterface queryMeta() { return prepared.queryMeta(); } @Override public boolean isCacheable() { return prepared.isCacheable(); } @Override public int getCommandType() { return prepared.getType(); } }