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.
package com.dieselpoint.norm;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.dieselpoint.norm.sqlmakers.PojoInfo;
import com.dieselpoint.norm.sqlmakers.SqlMaker;
/**
* Holds all of the information in a query. Create a query
* using Database.someQueryCreationMethod(), populate it using
* a builder pattern, and execute it using either .execute() (to
* update the database) or .results() (to get the results of a query.)
*/
public class Query {
private Object insertRow;
private String sql;
private String table;
private String where;
private String orderBy;
private Object[] args;
private int rowsAffected;
private Database db;
private SqlMaker sqlMaker;
private Transaction transaction;
public Query(Database db) {
this.db = db;
this.sqlMaker = db.getSqlMaker();
}
/**
* Add a where clause and some parameters to a query. Has no effect if
* the .sql() method is used.
* @param where Example: "name=?"
* @param args The parameter values to use in the where, example: "Bob"
*/
public Query where(String where, Object... args) {
this.where = where;
this.args = args;
return this;
}
/**
* Create a query using straight SQL. Overrides any other methods
* like .where(), .orderBy(), etc.
* @param sql The SQL string to use, may include ? parameters.
* @param args The parameter values to use in the query.
*/
public Query sql(String sql, Object... args) {
this.sql = sql;
this.args = args;
return this;
}
/**
* Create a query using straight SQL. Overrides any other methods
* like .where(), .orderBy(), etc.
* @param sql The SQL string to use, may include ? parameters.
* @param args The parameter values to use in the query.
*/
public Query sql(String sql, List> args) {
this.sql = sql;
this.args = args.toArray();
return this;
}
/**
* Add an "orderBy" clause to a query.
*/
public Query orderBy(String orderBy) {
this.orderBy = orderBy;
return this;
}
/**
* Returns the first row in a query in a pojo, or null if the query returns no results.
* Will return it in a Map if a class that implements Map is specified.
*/
public T first(Class clazz) {
List list = results(clazz);
if (list.size() > 0) {
return (T) list.get(0);
} else {
return null;
}
}
/**
* Provides the results as a list of Map objects instead of a list of pojos.
*/
private List