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.
/*
* Tentackle - https://tentackle.org
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.tentackle.dbms;
import org.tentackle.common.BMoney;
import org.tentackle.common.Binary;
import org.tentackle.common.DMoney;
import org.tentackle.log.Logger;
import org.tentackle.misc.Convertible;
import org.tentackle.session.PersistenceException;
import org.tentackle.sql.Backend;
import org.tentackle.sql.DataType;
import org.tentackle.sql.DataTypeFactory;
import java.math.BigDecimal;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.Time;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
/**
* A database query.
*
* Combines the generation of an SQL-string and parameter set for
* the ResultSetWrapper. Useful for one-time queries entered by the user.
* Notice that by default the underlying prepared statement is closed
* when the result-set (or cursor) is closed by the application.
* You can change that behaviour, however.
*
* Notice further, that the columns returned by the select
* must be provided by the application via add() _before_ execution.
* The SELECT, however, and LIMIT/OFFSET clauses are provided by the query automatically,
* because they are backend-specific.
*
* @author harald
*/
public class Query {
private static final Logger LOGGER = Logger.get(Query.class);
private final List items; // query parameters
private int fetchSize; // fetchsize, 0 = default
private int maxRows; // max. number of rows fetched at all, 0 = no limit
private int offset; // != 0 if add an offset clause
private int limit; // != 0 if add a limit clause
private boolean statementCached; // true if cache prepared statement
/**
* Creates a query.
*/
public Query() {
items = new ArrayList<>();
}
/**
* Enables caching of the prepared statement.
* By default, queries are one-time prepared statements that will be closed
* when the resultset is closed. However, if a query uses a fixed SQL string
* and is executed a significant number of times, the prepared statement
* can be prepared only once and re-used afterward. Please notice, that once
* prepared with caching, the statement will remain open until it is closed
* explicitly.
*
* @param statementCached true if prepare only once and reuse cached statement, false if one-shot
*/
public void setStatementCached(boolean statementCached) {
this.statementCached = statementCached;
}
/**
* Returns whether statement caching is enabled.
*
* @return true if prepare only once and reuse cached statement, false if one-shot (default)
*/
public boolean isStatementCached() {
return statementCached;
}
/**
* Sets an offset, i.e. number of rows to skip in a query.
* By default, the offset is 0.
* @param offset the offset, 0 to disable
*/
public void setOffset(int offset) {
this.offset = offset;
}
/**
* Gets the offset of this query.
*
* @return the number of rows to skip, 0 if no offset
*/
public int getOffset() {
return offset;
}
/**
* Sets the maximum number of rows to retrieve for this query.
*e
* @param limit the maximum number of rows, 0 if unlimited (default)
*/
public void setLimit(int limit) {
this.limit = limit;
}
/**
* Gets the maximum number of rows for this query.
*
* @return the maximum number of rows, 0 if unlimited (default)
*/
public int getLimit() {
return limit;
}
/**
* Appends an SQL-part and corresponding parameters to this query.
*
* Example: add(" AND CN_ID=?", object.getId());
* add(" AND CN_MONEY=?", object.getAmount());
*
*
* @param sql is the SQL-string, null or empty if just data
* @param data is an array of parameters
*/
public void add(CharSequence sql, Object... data) {
items.add(new QueryItem(sql, data));
}
/**
* Appends an SQL-part and corresponding parameters to this query.
* Same as {@link #add(CharSequence, Object...)} but with a {@link List} of parameters
* instead of an array or varargs.
*
* @param sql is the SQL-string, null or empty if just data
* @param data the {@link List} of parameters
*/
public void add(CharSequence sql, List