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 2009-2014 Igor Polevoy
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 org.javalite.activejdbc;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.List;
import java.util.Map;
import java.util.Properties;
/**
* This class provides a number of convenience methods for opening/closing database connections, running various
* types of queries, and executing SQL statements. This class differs from {@link DB} such that
* in this class you a logical name for a connection is hard-coded to be "default". Use this class when you have
* only one database.
*
* This class is a convenience wrapper of {@link DB}
*
* @author Igor Polevoy
*/
public class Base {
private static final String DEFAULT_DB_NAME = "default";
/**
* Opens a new connection based on JDBC properties and attaches it to a current thread.
*
* @param driver class name of driver
* @param url URL connection to DB
* @param user user name.
* @param password password.
*/
public static void open(String driver, String url, String user, String password) {
new DB(DEFAULT_DB_NAME).open(driver, url, user, password);
}
/**
* Opens a new connection in case additional driver-specific parameters need to be passed in.
*
* @param driver driver class name
* @param url JDBC URL
* @param props connection properties
*/
public static void open(String driver, String url, Properties props) {
new DB(DEFAULT_DB_NAME).open(driver, url, props);
}
/**
* Opens a connection from JNDI based on a registered name. This assumes that there is a jndi.properties
* file with proper JNDI configuration in it.
*
* @param jndiName name of a configured data source.
*/
public static void open(String jndiName) {
new DB(DEFAULT_DB_NAME).open(jndiName);
}
/**
* Opens a new connection from JNDI data source by name using explicit JNDI properties. This method can be used in cases
* when file jndi.properties cannot be easily modified.
*
* @param jndiName name of JNDI data source.
* @param jndiProperties JNDI properties
*/
public static void open(String jndiName, Properties jndiProperties) {
new DB(DEFAULT_DB_NAME).open(jndiName, jndiProperties);
}
/**
* Opens a connection from a datasource. This methods gives a high level control while sourcing a DB connection.
*
* @param dataSource datasource will be used to acquire a connection.
*/
public static void open(DataSource dataSource) {
new DB(DEFAULT_DB_NAME).open(dataSource);
}
/**
* Returns connection attached to a current thread and named "default".
*
* @return connection attached to a current thread and named "default".
*/
public static Connection connection() {
return new DB(DEFAULT_DB_NAME).connection();
}
/**
* Use to check if there is a default connection present on current thread.
*
* @return true if finds default connection on current thread, false if not.
*/
public static boolean hasConnection(){
return new DB(DEFAULT_DB_NAME).hasConnection();
}
/**
* Closes connection and detaches it from current thread.
* @param suppressWarning true to not display a warning in case of a problem (connection not there)
*/
public static void close(boolean suppressWarning) {
new DB(DEFAULT_DB_NAME).close(suppressWarning);
}
/**
* Closes connection and detaches it from current thread.
*/
public static void close() {
new DB(DEFAULT_DB_NAME).close();
}
/**
* Returns count of rows in table.
*
* @param table name of table.
* @return count of rows in table.
*/
public static Long count(String table){
return new DB(DEFAULT_DB_NAME).count(table);
}
/**
* Runs a count query, returns a number of matching records.
*
* @param table table in which to count rows.
* @param query this is a filtering query for the count. If '*' provided, all records will be counted. Example:
* "age > 65 AND department = 'accounting'"
* @param params parameters for placeholder substitution.
* @return copunt number of records found in a table.
*/
public static Long count(String table, String query, Object... params) {
return new DB(DEFAULT_DB_NAME).count(table, query, params);
}
/**
* Returns a value of the first column of the first row.
* This query expects only one column selected in the select statement.
* If more than one column returned, it will throw {@link IllegalArgumentException}.
*
* @param query query
* @param params parameters
* @return fetched value, or null if query did not fetch anything.
*/
public static Object firstCell(String query, Object... params) {
return new DB(DEFAULT_DB_NAME).firstCell(query, params);
}
/**
* This method returns entire resultset as one list. Do not use it for large result sets.
* Example:
*
*
* List<Map<String, Object>> people = Base.findAll("select * from people where first_name = ?", "John");
* for(Map person: people)
* System.out.println(person.get("first_name"));
*
*
*
* @param query raw SQL query. This query is parametrized.
* @param params list of parameters for a parametrized query.
* @return entire result set corresponding to the query.
*/
public static List