Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2011, Mysema Ltd
*
* 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.mysema.query.sql;
import javax.annotation.Nullable;
import java.lang.reflect.InvocationTargetException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import com.google.common.collect.ImmutableList;
import com.mysema.commons.lang.CloseableIterator;
import com.mysema.query.*;
import com.mysema.query.support.QueryMixin;
import com.mysema.query.types.*;
import com.mysema.query.types.expr.Wildcard;
import com.mysema.util.ResultSetAdapter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
/**
* AbstractSQLQuery is the base type for SQL query implementations
*
* @author tiwe
*
* @param concrete subtype
*/
public abstract class AbstractSQLQuery> extends ProjectableSQLQuery {
private static final Logger logger = LoggerFactory.getLogger(AbstractSQLQuery.class);
private static final QueryFlag rowCountFlag = new QueryFlag(QueryFlag.Position.AFTER_PROJECTION, ", count(*) over() ");
@Nullable
private final Connection conn;
protected SQLListeners listeners;
protected boolean useLiterals;
private boolean getLastCell;
private Object lastCell;
public AbstractSQLQuery(@Nullable Connection conn, Configuration configuration) {
this(conn, configuration, new DefaultQueryMetadata().noValidate());
}
public AbstractSQLQuery(@Nullable Connection conn, Configuration configuration, QueryMetadata metadata) {
super(new QueryMixin(metadata, false), configuration);
this.conn = conn;
this.listeners = new SQLListeners(configuration.getListeners());
this.useLiterals = configuration.getUseLiterals();
}
/**
* @param listener
*/
public void addListener(SQLListener listener) {
listeners.add(listener);
}
@Override
public long count() {
try {
return unsafeCount();
} catch (SQLException e) {
String error = "Caught " + e.getClass().getName();
logger.error(error, e);
throw configuration.translate(e);
}
}
/**
* If you use forUpdate() with a backend that uses page or row locks, rows examined by the
* query are write-locked until the end of the current transaction.
*
* Not supported for SQLite and CUBRID
*
* @return
*/
public Q forUpdate() {
return addFlag(SQLOps.FOR_UPDATE_FLAG);
}
protected SQLSerializer createSerializer() {
SQLSerializer serializer = new SQLSerializer(configuration);
serializer.setUseLiterals(useLiterals);
return serializer;
}
@Nullable
private T get(ResultSet rs, Expression> expr, int i, Class type) throws SQLException {
return configuration.get(rs, expr instanceof Path ? (Path>)expr : null, i, type);
}
private void set(PreparedStatement stmt, Path> path, int i, Object value) throws SQLException{
configuration.set(stmt, path, i, value);
}
/**
* Called to create and start a new SQL Listener context
*
* @param connection the database connection
* @param metadata the meta data for that context
* @return the newly started context
*/
protected SQLListenerContextImpl startContext(Connection connection, QueryMetadata metadata) {
SQLListenerContextImpl context = new SQLListenerContextImpl(metadata, connection);
listeners.start(context);
return context;
}
/**
* Called to make the call back to listeners when an exception happens
*
* @param context the current context in play
* @param e the exception
*/
protected void onException(SQLListenerContextImpl context, Exception e) {
context.setException(e);
listeners.exception(context);
}
/**
* Called to end a SQL listener context
*
* @param context the listener context to end
*/
protected void endContext(SQLListenerContext context) {
listeners.end(context);
}
/**
* Get the results as an JDBC result set
*
* @param exprs the expression arguments to retrieve
* @return
*/
public ResultSet getResults(Expression>... exprs) {
queryMixin.addProjection(exprs);
SQLListenerContextImpl context = startContext(conn, queryMixin.getMetadata());
String queryString = null;
List