Class SQLStore<V extends Identifiable>
- java.lang.Object
-
- edu.uiuc.ncsa.security.storage.sql.SQLDatabase
-
- edu.uiuc.ncsa.security.storage.sql.SQLStore<V>
-
- All Implemented Interfaces:
Store<V>,Map<Identifier,V>
- Direct Known Subclasses:
MonitoredSQLStore,SASSQLStore
public abstract class SQLStore<V extends Identifiable> extends SQLDatabase implements Store<V>
Top-level SQL store object. A store is simply a logical analog of a hash table, where the key is the primary key. This in practice may front multiple tables. This implements several of the basic operations. You need to implement a couple of methods and supply aTablethat models the storage and aMapConverterthat allows you to turn a java object's properties into a map -- then you should be in business for using an SQL backend. All of these statements are SQL 2003 compliant and should work without change for all major vendors. This class also maintains aConnectionPoolto a database.Created by Jeff Gaynor
on Mar 12, 2010 at 12:58:14 PM
-
-
Field Summary
Fields Modifier and Type Field Description protected MapConverter<V>converterprotected javax.inject.Provider<V>identifiableProvider-
Fields inherited from interface edu.uiuc.ncsa.security.core.Store
VERSION_TAG
-
-
Constructor Summary
Constructors Modifier Constructor Description SQLStore()protectedSQLStore(ConnectionPool connectionPool, Table table, javax.inject.Provider<V> identifiableProvider, MapConverter<V> converter)
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidcheckColumns()When invoked this will loop through the columns of the table and add columns as needed with the correct type.voidcheckTable()voidclear()booleancontainsKey(Object key)booleancontainsValue(Object o)static List<String>crappySQLParser(String fileName)This is to take an SQL script for e.g.static List<String>crappySQLParser(List<String> script)Vcreate()Create a new object of the given type.ColumnMapdepopulate(V t)Takes the object, V and returns a map of column name, value.voiddoRegisterStatement(PreparedStatement stmt, V value)Sets the values in thePreparedStatementto those in the value.Set<Map.Entry<Identifier,V>>entrySet()Vget(Object o)Retrieve a single row from a table then populate an instance.List<V>getAll()This will get every entry in the database.abstract StringgetCreationTSField()protected StringgetDerbyMostRecent(String attrbutes, boolean desc)MapConvertergetMapConverter()List<V>getMostRecent(int n, List<String> attr)protected StringgetMostRecentStatement(String attributes, boolean desc)TablegetTable()XMLConverter<V>getXMLConverter()booleanisEmpty()Set<Identifier>keySet()voidpopulate(ColumnMap map, V t)Take a *new* value and populate it from the given mapping of column names and values.Vput(Identifier k, V v)voidputAll(Map<? extends Identifier,? extends V> m)A terrifically inefficient way to add these since it loops.voidregister(V value)Almost Identical to put(K,V) but since the object should have an identifier, passing along the key is redundant.Vremove(Object key)booleanremove(List<V> objects)Removes a list of identifiable objects.booleanremoveByID(List<Identifier> ids)remove a collection of items by primary key.voidsave(V value)Saves an object.List<V>search(String key, boolean isNull)Searches if a single column and returns all of the null or non-null elementsList<V>search(String key, String condition, boolean isRegEx)Allows for searching via a reg ex.List<V>search(String key, String condition, boolean isRegEx, List<String> attr)Return a subset of all the attributes.List<V>search(String key, String condition, boolean isRegEx, List<String> attr, String dateField, Date before, Date after)voidsetUpdateValues(V value, PreparedStatement stmt)intsize()intsize(boolean includeVersions)protected intsize(String tablename, boolean includeVersions)Utility that gets the total number of rows in a given table, given the name.StringtoString()voidupdate(List<Identifier> ids, Map<String,Object> map)Mass update.voidupdate(V value)For an existing entry in the store.voidupdate(V value, boolean existenceChecked)Collection<V>values()Again, this is basic functionality for the map interface.-
Methods inherited from class edu.uiuc.ncsa.security.storage.sql.SQLDatabase
destroyConnection, getConnection, getConnectionPool, releaseConnection, rsToMap, setConnectionPool
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, equals, forEach, getOrDefault, hashCode, merge, putIfAbsent, remove, replace, replace, replaceAll
-
-
-
-
Field Detail
-
identifiableProvider
protected javax.inject.Provider<V extends Identifiable> identifiableProvider
-
converter
protected MapConverter<V extends Identifiable> converter
-
-
Constructor Detail
-
SQLStore
protected SQLStore(ConnectionPool connectionPool, Table table, javax.inject.Provider<V> identifiableProvider, MapConverter<V> converter)
-
SQLStore
public SQLStore()
-
-
Method Detail
-
create
public V create()
Description copied from interface:StoreCreate a new object of the given type. This is not in the store until it is registered. Attempts to update the object should throw an exception. Note that this allows for a separation of creation semantics. Some objects require specific initialization before saving- Specified by:
createin interfaceStore<V extends Identifiable>- Returns:
-
getAll
public List<V> getAll()
This will get every entry in the database. For sparing use since this may be a huge load.- Specified by:
getAllin interfaceStore<V extends Identifiable>- Returns:
-
update
public void update(V value)
For an existing entry in the store. This will select it based on the primary key and change all other values.- Specified by:
updatein interfaceStore<V extends Identifiable>- Parameters:
value-
-
update
public void update(V value, boolean existenceChecked)
-
update
public void update(List<Identifier> ids, Map<String,Object> map) throws UnregisteredObjectException
Description copied from interface:StoreMass update. For each id, update the keys with the new values. At the end of this operation, every entry in the id list has the same (key, value) pairs. This is used in processing StoreCommands RSRecords.- Specified by:
updatein interfaceStore<V extends Identifiable>- Throws:
UnregisteredObjectException
-
setUpdateValues
public void setUpdateValues(V value, PreparedStatement stmt) throws SQLException
- Throws:
SQLException
-
populate
public void populate(ColumnMap map, V t)
Take a *new* value and populate it from the given mapping of column names and values. All values must be accounted for since the result set generally will come from aSELECT * FROM...- Parameters:
map-t-
-
depopulate
public ColumnMap depopulate(V t) throws SQLException
Takes the object, V and returns a map of column name, value. This is used to construct various statements This is where the columns and object properties are put in correspondence. We could try to do this with some sort of introspection, but that is very, very slow and not always clear on how it should be done.- Parameters:
t-- Returns:
- Throws:
SQLException
-
save
public void save(V value)
Description copied from interface:StoreSaves an object. This bridges the gap between SQL stores update and insert commands. Implementations should check if the object already exists in the store and issue an appropriate call.- Specified by:
savein interfaceStore<V extends Identifiable>
-
doRegisterStatement
public void doRegisterStatement(PreparedStatement stmt, V value) throws SQLException
Sets the values in thePreparedStatementto those in the value. This allows other utilities to create batch statements for this store.- Parameters:
stmt-value-- Throws:
SQLException
-
register
public void register(V value)
Description copied from interface:StoreAlmost Identical to put(K,V) but since the object should have an identifier, passing along the key is redundant. This persists the object in the store. Note that this returns void since the contract assumes that this is not registered. If the object is registered an exception should be thrown. Generally use save(V).- Specified by:
registerin interfaceStore<V extends Identifiable>
-
get
public V get(Object o)
Retrieve a single row from a table then populate an instance.
Note: If you need to jazz this up, it is probably better to override theSQLDatabase.rsToMap(java.sql.ResultSet)method in this class. For instance, if the select statement is a join and there are multiple rows to process. The basic version of this class presupposed one row per object, but there is no reason this cannot be extended.- Specified by:
getin interfaceMap<Identifier,V extends Identifiable>- Parameters:
o-- Returns:
-
search
public List<V> search(String key, String condition, boolean isRegEx, List<String> attr, String dateField, Date before, Date after)
- Specified by:
searchin interfaceStore<V extends Identifiable>
-
search
public List<V> search(String key, String condition, boolean isRegEx, List<String> attr)
Description copied from interface:StoreReturn a subset of all the attributes. For non-SQL stores performance may be slow.- Specified by:
searchin interfaceStore<V extends Identifiable>- Returns:
-
search
public List<V> search(String key, boolean isNull)
Description copied from interface:StoreSearches if a single column and returns all of the null or non-null elements- Specified by:
searchin interfaceStore<V extends Identifiable>- Returns:
-
search
public List<V> search(String key, String condition, boolean isRegEx)
Description copied from interface:StoreAllows for searching via a reg ex. Note that this may be very expensive for certain stores!- Specified by:
searchin interfaceStore<V extends Identifiable>- Returns:
-
getTable
public Table getTable()
-
size
public int size()
- Specified by:
sizein interfaceMap<Identifier,V extends Identifiable>
-
size
public int size(boolean includeVersions)
- Specified by:
sizein interfaceStore<V extends Identifiable>
-
size
protected int size(String tablename, boolean includeVersions)
Utility that gets the total number of rows in a given table, given the name.- Parameters:
tablename-- Returns:
-
isEmpty
public boolean isEmpty()
- Specified by:
isEmptyin interfaceMap<Identifier,V extends Identifiable>
-
containsKey
public boolean containsKey(Object key)
- Specified by:
containsKeyin interfaceMap<Identifier,V extends Identifiable>
-
containsValue
public boolean containsValue(Object o)
- Specified by:
containsValuein interfaceMap<Identifier,V extends Identifiable>
-
put
public V put(Identifier k, V v)
- Specified by:
putin interfaceMap<Identifier,V extends Identifiable>
-
removeByID
public boolean removeByID(List<Identifier> ids)
remove a collection of items by primary key. This uses SQL batching and should be used for large sets.remove(List)takes a list of objects and makes a very long list of things to remove.- Specified by:
removeByIDin interfaceStore<V extends Identifiable>- Parameters:
ids-- Returns:
-
remove
public boolean remove(List<V> objects)
Description copied from interface:StoreRemoves a list of identifiable objects. I.e., it will grab theIdentifiers from the objects and pass this toStore.removeByID(List).- Specified by:
removein interfaceStore<V extends Identifiable>- Returns:
-
remove
public V remove(Object key)
- Specified by:
removein interfaceMap<Identifier,V extends Identifiable>
-
putAll
public void putAll(Map<? extends Identifier,? extends V> m)
A terrifically inefficient way to add these since it loops. If you need this to work better, override and optimize.- Specified by:
putAllin interfaceMap<Identifier,V extends Identifiable>- Parameters:
m-
-
clear
public void clear()
- Specified by:
clearin interfaceMap<Identifier,V extends Identifiable>
-
keySet
public Set<Identifier> keySet()
- Specified by:
keySetin interfaceMap<Identifier,V extends Identifiable>
-
values
public Collection<V> values()
Again, this is basic functionality for the map interface. Do you really need to get everything in the database? If the database is large, this might fail for various unrelated reasons. If you really need to use a call like this, then you should probably over-ride it and optimize, say with partial retrievals or some such.- Specified by:
valuesin interfaceMap<Identifier,V extends Identifiable>- Returns:
-
entrySet
public Set<Map.Entry<Identifier,V>> entrySet()
- Specified by:
entrySetin interfaceMap<Identifier,V extends Identifiable>
-
checkColumns
public void checkColumns() throws SQLExceptionWhen invoked this will loop through the columns of the table and add columns as needed with the correct type. NOTE that this should only be run once as a utility at, say, servlet loading time before any data access can occur. Also, all the added columns are allowed to be null. You will have to change that (along with setting a defaul) if you do not want that.- Throws:
SQLException
-
checkTable
public void checkTable()
-
getXMLConverter
public XMLConverter<V> getXMLConverter()
- Specified by:
getXMLConverterin interfaceStore<V extends Identifiable>
-
getMapConverter
public MapConverter getMapConverter()
-
getCreationTSField
public abstract String getCreationTSField()
-
getMostRecent
public List<V> getMostRecent(int n, List<String> attr)
- Specified by:
getMostRecentin interfaceStore<V extends Identifiable>
-
crappySQLParser
public static List<String> crappySQLParser(String fileName)
This is to take an SQL script for e.g. creating a database and return a list of statements. It is not a clever parser at all. It strips out comments and anything that ends with a ; is a statement. Mostly the assumption is that this is a set of table create statements and ceate index statements. Anything else is at your own risk.- Parameters:
fileName- either the name of a resource or the fully qualified path to the file.- Returns:
- Throws:
Throwable
-
-