Package adams.db.generic
Class SQL
- java.lang.Object
-
- adams.core.logging.LoggingObject
-
- adams.db.generic.SQL
-
- All Implemented Interfaces:
LoggingSupporter,SizeOfHandler,DatabaseConnectionProvider,DatabaseConnectionUser,SQLIntf,TableInterface,Serializable
- Direct Known Subclasses:
AbstractTable
public class SQL extends LoggingObject implements SQLIntf
Basic SQL support.- Author:
- dale, FracPete (fracpete at waikato dot ac dot nz)
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description protected AbstractDatabaseConnectionm_DatabaseConnectionconnection to database.protected booleanm_Debugwhether debugging is turned on.protected AbstractDatabaseQueriesm_Queriesfor helping with queries.protected AbstractDatabaseQuirksm_Quirksthe quirks for the connection.protected static TableManager<SQL>m_TableManagerthe table manager.-
Fields inherited from class adams.core.logging.LoggingObject
m_Logger, m_LoggingIsEnabled, m_LoggingLevel
-
Fields inherited from interface adams.db.SQLIntf
FAKE_TABLE_NAME
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedSQL(AbstractDatabaseConnection dbcon)Constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleancolumnExists(String table, String column)Checks that a given column exists.protected ResultSetdoSelect(boolean distinct, String columns, String tables, String where)Do a select on given columns for all data in joined tables, with condition.booleandrop(String table)Drops the table.Booleanexecute(String query)Executes a SQL query.ResultSetexecuteGeneratedKeys(String query)Executes a SQL query.AbstractDatabaseConnectiongetDatabaseConnection()Returns the database connection this table is for.booleangetDebug()Returns whether debugging is enabled.intgetMaxColumnNameLength()Returns the maximum length for column names.ResultSetgetResultSet(String query)Return resultset of given query.SimpleResultSetgetSimpleResultSet(String query)Execute the given SQL statement and return ResultSet.StringgetTableName()Returns fake table name.PreparedStatementprepareStatement(String query)Create a Prepared statement with given query.PreparedStatementprepareStatement(String query, boolean returnKeys)Create a Prepared statement with given query.PreparedStatementprepareStatement(Connection conn, String query, boolean returnKeys)Create a Prepared statement with given query.ResultSetselect(String columns, String tables, String where)Do a select on given columns for all data in joined tables, with condition.ResultSetselectDistinct(String columns, String tables, String where)Do a select distinct on given columns for all data in joined tables, with condition.gnu.trove.list.TDoubleListselectDouble(boolean distinct, String column, String tables, String where)Selects all doubles from the specified column.gnu.trove.list.TIntListselectInt(boolean distinct, String column, String tables, String where)Selects all integers from the specified column.gnu.trove.list.TLongListselectLong(boolean distinct, String column, String tables, String where)Selects all longs from the specified column.List<String>selectString(boolean distinct, String column, String tables, String where)Selects all strings from the specified column.List<String[]>selectStrings(boolean distinct, String[] columns, String tables, String where)Selects all strings from the specified columns.voidsetDebug(boolean value)Sets whether debugging is enabled, outputs more on the console.static SQLsingleton(AbstractDatabaseConnection dbcon)Returns the singleton instance.booleantableExists(String table)Checks that a given table exists.StringtoString()Returns a short string representation.booleantruncate(String table)Empty table.intupdate(String updateString, String table, String where)Update table.protected voidupdatePrefix()Updates the prefix of the console object output streams.protected StringupdateTableName(DatabaseMetaData dbmd, String table)Updates the case of the table name if necessary.-
Methods inherited from class adams.core.logging.LoggingObject
configureLogger, getLogger, getLoggingLevel, initializeLogging, isLoggingEnabled, sizeOf
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface adams.core.logging.LoggingSupporter
getLogger, isLoggingEnabled
-
-
-
-
Field Detail
-
m_Debug
protected boolean m_Debug
whether debugging is turned on.
-
m_DatabaseConnection
protected AbstractDatabaseConnection m_DatabaseConnection
connection to database.
-
m_Quirks
protected AbstractDatabaseQuirks m_Quirks
the quirks for the connection.
-
m_Queries
protected AbstractDatabaseQueries m_Queries
for helping with queries.
-
m_TableManager
protected static TableManager<SQL> m_TableManager
the table manager.
-
-
Constructor Detail
-
SQL
protected SQL(AbstractDatabaseConnection dbcon)
Constructor.- Parameters:
dbcon- the database context to use
-
-
Method Detail
-
getTableName
public String getTableName()
Returns fake table name.- Specified by:
getTableNamein interfaceTableInterface- Returns:
- the name
-
updatePrefix
protected void updatePrefix()
Updates the prefix of the console object output streams.
-
getDatabaseConnection
public AbstractDatabaseConnection getDatabaseConnection()
Returns the database connection this table is for.- Specified by:
getDatabaseConnectionin interfaceDatabaseConnectionProvider- Returns:
- the database connection
-
setDebug
public void setDebug(boolean value)
Sets whether debugging is enabled, outputs more on the console.
-
getDebug
public boolean getDebug()
Returns whether debugging is enabled.
-
updateTableName
protected String updateTableName(DatabaseMetaData dbmd, String table) throws Exception
Updates the case of the table name if necessary.- Parameters:
dbmd- the database meta-data to querytable- the table to process- Returns:
- the (potentially) updated table name
- Throws:
Exception- if accessing the meta-data fails
-
tableExists
public boolean tableExists(String table)
Checks that a given table exists.- Specified by:
tableExistsin interfaceSQLIntf- Parameters:
table- the table to look for- Returns:
- true if the table exists.
-
columnExists
public boolean columnExists(String table, String column)
Checks that a given column exists.- Specified by:
columnExistsin interfaceSQLIntf- Parameters:
table- the table to searchcolumn- the column to look for- Returns:
- true if the column exists.
-
getSimpleResultSet
public SimpleResultSet getSimpleResultSet(String query) throws Exception
Execute the given SQL statement and return ResultSet.- Specified by:
getSimpleResultSetin interfaceSQLIntf- Parameters:
query- SQL query String- Returns:
- resulting ResultSet, or null if Error
- Throws:
Exception- if something goes wrong
-
prepareStatement
public PreparedStatement prepareStatement(String query) throws Exception
Create a Prepared statement with given query.- Specified by:
prepareStatementin interfaceSQLIntf- Parameters:
query- the query to execute- Returns:
- PreparedStatement
- Throws:
Exception- if something goes wrong
-
prepareStatement
public PreparedStatement prepareStatement(String query, boolean returnKeys) throws Exception
Create a Prepared statement with given query.- Specified by:
prepareStatementin interfaceSQLIntf- Parameters:
query- the query to executereturnKeys- whether to initialize the statement that it returns the generated keys- Returns:
- PreparedStatement
- Throws:
Exception- if something goes wrong
-
prepareStatement
public PreparedStatement prepareStatement(Connection conn, String query, boolean returnKeys) throws Exception
Create a Prepared statement with given query.- Specified by:
prepareStatementin interfaceSQLIntf- Parameters:
conn- the database connection to usequery- the query to executereturnKeys- whether to initialize the statement that it returns the generated keys- Returns:
- PreparedStatement
- Throws:
Exception- if something goes wrong
-
update
public int update(String updateString, String table, String where) throws Exception
Update table.
-
executeGeneratedKeys
public ResultSet executeGeneratedKeys(String query) throws Exception
Executes a SQL query. Return any Generated Keys Caller is responsible for closing the statement.- Specified by:
executeGeneratedKeysin interfaceSQLIntf- Parameters:
query- the SQL query- Returns:
- Generated keys as a resultset, or null if failure
- Throws:
Exception- if something goes wrong
-
truncate
public boolean truncate(String table)
Empty table.
-
drop
public boolean drop(String table)
Drops the table.
-
select
public ResultSet select(String columns, String tables, String where) throws Exception
Do a select on given columns for all data in joined tables, with condition.
-
selectDistinct
public ResultSet selectDistinct(String columns, String tables, String where) throws Exception
Do a select distinct on given columns for all data in joined tables, with condition.- Specified by:
selectDistinctin interfaceSQLIntf- Parameters:
columns- columns to selecttables- the tables to select fromwhere- condition- Returns:
- resultset of data
- Throws:
Exception- if something goes wrong
-
doSelect
protected ResultSet doSelect(boolean distinct, String columns, String tables, String where) throws Exception
Do a select on given columns for all data in joined tables, with condition. Can be distinct.- Parameters:
distinct- whether values in columns has to be distinctcolumns- columns to selecttables- the tables to select from, ignored if nullwhere- condition, can be null- Returns:
- resultset of data
- Throws:
Exception- if something goes wrong
-
selectString
public List<String> selectString(boolean distinct, String column, String tables, String where) throws Exception
Selects all strings from the specified column. Can be distinct.- Specified by:
selectStringin interfaceSQLIntf- Parameters:
distinct- whether values in column have to be distinctcolumn- the string column to selecttables- the tables to select from, ignored if nullwhere- condition, can be null- Returns:
- resultset of data
- Throws:
Exception- if something goes wrong
-
selectStrings
public List<String[]> selectStrings(boolean distinct, String[] columns, String tables, String where) throws Exception
Selects all strings from the specified columns. Can be distinct (uses a CONCAT internally for that).- Specified by:
selectStringsin interfaceSQLIntf- Parameters:
distinct- whether values in column have to be distinctcolumns- the string columns to selecttables- the tables to select from, ignored if nullwhere- condition, can be null- Returns:
- resultset of data
- Throws:
Exception- if something goes wrong
-
selectInt
public gnu.trove.list.TIntList selectInt(boolean distinct, String column, String tables, String where) throws ExceptionSelects all integers from the specified column. Can be distinct.- Specified by:
selectIntin interfaceSQLIntf- Parameters:
distinct- whether values in column have to be distinctcolumn- the int column to selecttables- the tables to select from, ignored if nullwhere- condition, can be null- Returns:
- resultset of data
- Throws:
Exception- if something goes wrong
-
selectLong
public gnu.trove.list.TLongList selectLong(boolean distinct, String column, String tables, String where) throws ExceptionSelects all longs from the specified column. Can be distinct.- Specified by:
selectLongin interfaceSQLIntf- Parameters:
distinct- whether values in column have to be distinctcolumn- the long column to selecttables- the tables to select from, ignored if nullwhere- condition, can be null- Returns:
- resultset of data
- Throws:
Exception- if something goes wrong
-
selectDouble
public gnu.trove.list.TDoubleList selectDouble(boolean distinct, String column, String tables, String where) throws ExceptionSelects all doubles from the specified column. Can be distinct.- Specified by:
selectDoublein interfaceSQLIntf- Parameters:
distinct- whether values in column have to be distinctcolumn- the long column to selecttables- the tables to select from, ignored if nullwhere- condition, can be null- Returns:
- resultset of data
- Throws:
Exception- if something goes wrong
-
getResultSet
public ResultSet getResultSet(String query) throws Exception
Return resultset of given query.- Specified by:
getResultSetin interfaceSQLIntf- Parameters:
query- sql query- Returns:
- resulset
- Throws:
Exception- if something goes wrong
-
toString
public String toString()
Returns a short string representation.
-
getMaxColumnNameLength
public int getMaxColumnNameLength() throws SQLExceptionReturns the maximum length for column names. In case the meta-data returns 0,Integer.MAX_VALUEis used instead.- Specified by:
getMaxColumnNameLengthin interfaceSQLIntf- Returns:
- the maximum length
- Throws:
SQLException- if the query fails
-
singleton
public static SQL singleton(AbstractDatabaseConnection dbcon)
Returns the singleton instance.- Returns:
- the singleton
-
-