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 SummaryFields Modifier and Type Field Description protected MapConverter<V>converterprotected javax.inject.Provider<V>identifiableProvider- 
Fields inherited from interface edu.uiuc.ncsa.security.core.StoreVERSION_TAG
 
- 
 - 
Constructor SummaryConstructors Modifier Constructor Description SQLStore()protectedSQLStore(ConnectionPool connectionPool, Table table, javax.inject.Provider<V> identifiableProvider, MapConverter<V> converter)
 - 
Method SummaryAll 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.SQLDatabasedestroyConnection, getConnection, getConnectionPool, releaseConnection, rsToMap, setConnectionPool
 - 
Methods inherited from class java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 - 
Methods inherited from interface java.util.Mapcompute, computeIfAbsent, computeIfPresent, equals, forEach, getOrDefault, hashCode, merge, putIfAbsent, remove, replace, replace, replaceAll
 
- 
 
- 
- 
- 
Field Detail- 
identifiableProviderprotected javax.inject.Provider<V extends Identifiable> identifiableProvider 
 - 
converterprotected MapConverter<V extends Identifiable> converter 
 
- 
 - 
Constructor Detail- 
SQLStoreprotected SQLStore(ConnectionPool connectionPool, Table table, javax.inject.Provider<V> identifiableProvider, MapConverter<V> converter) 
 - 
SQLStorepublic SQLStore() 
 
- 
 - 
Method Detail- 
createpublic 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 interface- Store<V extends Identifiable>
- Returns:
 
 - 
getAllpublic List<V> getAll() This will get every entry in the database. For sparing use since this may be a huge load.- Specified by:
- getAllin interface- Store<V extends Identifiable>
- Returns:
 
 - 
updatepublic 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 interface- Store<V extends Identifiable>
- Parameters:
- value-
 
 - 
updatepublic void update(V value, boolean existenceChecked) 
 - 
updatepublic 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 interface- Store<V extends Identifiable>
- Throws:
- UnregisteredObjectException
 
 - 
setUpdateValuespublic void setUpdateValues(V value, PreparedStatement stmt) throws SQLException - Throws:
- SQLException
 
 - 
populatepublic 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-
 
 - 
depopulatepublic 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
 
 - 
savepublic 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 interface- Store<V extends Identifiable>
 
 - 
doRegisterStatementpublic 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
 
 - 
registerpublic 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 interface- Store<V extends Identifiable>
 
 - 
getpublic 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 interface- Map<Identifier,V extends Identifiable>
- Parameters:
- o-
- Returns:
 
 - 
searchpublic List<V> search(String key, String condition, boolean isRegEx, List<String> attr, String dateField, Date before, Date after) - Specified by:
- searchin interface- Store<V extends Identifiable>
 
 - 
searchpublic 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 interface- Store<V extends Identifiable>
- Returns:
 
 - 
searchpublic 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 interface- Store<V extends Identifiable>
- Returns:
 
 - 
searchpublic 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 interface- Store<V extends Identifiable>
- Returns:
 
 - 
getTablepublic Table getTable() 
 - 
sizepublic int size() - Specified by:
- sizein interface- Map<Identifier,V extends Identifiable>
 
 - 
sizepublic int size(boolean includeVersions) - Specified by:
- sizein interface- Store<V extends Identifiable>
 
 - 
sizeprotected int size(String tablename, boolean includeVersions) Utility that gets the total number of rows in a given table, given the name.- Parameters:
- tablename-
- Returns:
 
 - 
isEmptypublic boolean isEmpty() - Specified by:
- isEmptyin interface- Map<Identifier,V extends Identifiable>
 
 - 
containsKeypublic boolean containsKey(Object key) - Specified by:
- containsKeyin interface- Map<Identifier,V extends Identifiable>
 
 - 
containsValuepublic boolean containsValue(Object o) - Specified by:
- containsValuein interface- Map<Identifier,V extends Identifiable>
 
 - 
putpublic V put(Identifier k, V v) - Specified by:
- putin interface- Map<Identifier,V extends Identifiable>
 
 - 
removeByIDpublic 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 interface- Store<V extends Identifiable>
- Parameters:
- ids-
- Returns:
 
 - 
removepublic 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 interface- Store<V extends Identifiable>
- Returns:
 
 - 
removepublic V remove(Object key) - Specified by:
- removein interface- Map<Identifier,V extends Identifiable>
 
 - 
putAllpublic 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 interface- Map<Identifier,V extends Identifiable>
- Parameters:
- m-
 
 - 
clearpublic void clear() - Specified by:
- clearin interface- Map<Identifier,V extends Identifiable>
 
 - 
keySetpublic Set<Identifier> keySet() - Specified by:
- keySetin interface- Map<Identifier,V extends Identifiable>
 
 - 
valuespublic 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 interface- Map<Identifier,V extends Identifiable>
- Returns:
 
 - 
entrySetpublic Set<Map.Entry<Identifier,V>> entrySet() - Specified by:
- entrySetin interface- Map<Identifier,V extends Identifiable>
 
 - 
checkColumnspublic 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
 
 - 
checkTablepublic void checkTable() 
 - 
getXMLConverterpublic XMLConverter<V> getXMLConverter() - Specified by:
- getXMLConverterin interface- Store<V extends Identifiable>
 
 - 
getMapConverterpublic MapConverter getMapConverter() 
 - 
getCreationTSFieldpublic abstract String getCreationTSField() 
 - 
getMostRecentpublic List<V> getMostRecent(int n, List<String> attr) - Specified by:
- getMostRecentin interface- Store<V extends Identifiable>
 
 - 
crappySQLParserpublic 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
 
 
- 
 
-