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-2019 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 org.javalite.activejdbc.connection_config.DBConfiguration;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.function.Supplier;
/**
* 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
* @author Eric Nielsen
*/
public class Base {
private Base() {}
/**
*
* This method will open a connection defined in the file 'database.properties' set by an initial previous call to {@link DBConfiguration#loadConfiguration(String)}.
* The connection picked up from the file is defined by ACTIVE_ENV environment variable or active_env system property.
* If this variable is not defined, it defaults to 'development' environment.
*
*
* If there is JUnit on classpath, this method assumes it is running under test, and defaults to 'test'.
*
*/
public static DB open(){
return new DB(DB.DEFAULT_NAME).open();
}
/**
* 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 DB open(String driver, String url, String user, String password) {
return new DB(DB.DEFAULT_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 DB open(String driver, String url, Properties props) {
return new DB(DB.DEFAULT_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 DB open(String jndiName) {
return new DB(DB.DEFAULT_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 DB open(String jndiName, Properties jndiProperties) {
return new DB(DB.DEFAULT_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 DB open(DataSource dataSource) {
return new DB(DB.DEFAULT_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(DB.DEFAULT_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(DB.DEFAULT_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(DB.DEFAULT_NAME).close(suppressWarning);
}
/**
* Closes connection and detaches it from current thread.
*/
public static void close() {
new DB(DB.DEFAULT_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(DB.DEFAULT_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(DB.DEFAULT_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(DB.DEFAULT_NAME).firstCell(query, params);
}
/**
* This method returns entire resultset as one list. Do not use it for large result sets.
* Example:
*
* List<Map> 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