Package org.javalite.activejdbc
Class LazyList<T extends Model>
java.lang.Object
org.javalite.activejdbc.AbstractLazyList<T>
org.javalite.activejdbc.LazyList<T>
- All Implemented Interfaces:
Externalizable
,Serializable
,Iterable<T>
,Collection<T>
,List<T>
,RandomAccess
- Direct Known Subclasses:
SuperLazyList
While this class is public, it is never instantiated directly. This class provides
a number of APIs for augmenting the query.
- Author:
- Igor Polevoy, Eric Nielsen
- See Also:
- Serialized Form
-
Field Summary
Fields inherited from class org.javalite.activejdbc.AbstractLazyList
delegate
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionCollects values from a result set that correspond to a attribute name.collectDistinct(String attributeName)
collectDistinct(String attributeName, String filterAttribute, Object filterValue)
void
dump()
Dumps contents of this list toSystem.out
.void
dump(OutputStream out)
Dumps content of list to a stream.protected void
hydrate()
This method includes associated objects.limit(long limit)
This method limits the number of results in the resultset.load()
This method exists to force immediate load from DB.offset(long offset)
This method sets an offset of a resultset.Use this method to order results by a column.void
readExternal(ObjectInput in)
Generates JSON from content of this listtoMaps()
Converts the resultset to list of maps, where each map represents a row in the resultset keyed off column names.toSql()
Same astoSql(true)
, seetoSql(boolean)
;toSql(boolean showParameters)
Use to see what SQL will be sent to the database.Generates a XML document from content of this list.void
writeExternal(ObjectOutput out)
Methods inherited from class org.javalite.activejdbc.AbstractLazyList
add, add, addAll, addAll, clear, contains, containsAll, equals, get, hashCode, indexOf, isEmpty, iterator, lastIndexOf, listIterator, listIterator, remove, remove, removeAll, retainAll, set, size, subList, toArray, toArray, toString
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArray
Methods inherited from interface java.util.List
replaceAll, sort, spliterator
-
Constructor Details
-
LazyList
-
LazyList
- Parameters:
metaModel
-fullQuery
-forPaginator
- true is this list should not check usage of limit() and offset() methods.params
-
-
LazyList
protected LazyList()This is only used by SuperLazyList
-
-
Method Details
-
limit
This method limits the number of results in the resultset. It can be used in combination with the offset like this:List<Event> events = Event.find("mnemonic = ?", "GLUC").offset(101).limit(20).orderBy("history_event_id");
This will produce 20 records, starting from record 101. This is an efficient method, it will only retrieve records that are necessary.- Parameters:
limit
- how many records to retrieve.- Returns:
- instance of this
LazyList
-
offset
This method sets an offset of a resultset. For instance, if the offset is 101, then the resultset will skip the first 100 records. It can be used in combination wit the limit like this:List
This will produce 20 records, starting from record 101. This is an efficient method, it will only retrieve records that are necessary.events = Event.find("mnemonic = ?", "GLUC").offset(101).limit(20).orderBy("history_event_id"); - Parameters:
offset
-- Returns:
- instance of this
LazyList
-
orderBy
Use this method to order results by a column. These methods can be chained:Person.find(...).orderBy("department").orderBy("age")
- Parameters:
orderBy
- order by clause. Examples: "department", "age desc", etc.- Returns:
- instance of this
LazyList
-
include
This method includes associated objects. It will eagerly load associated models of models selected by the query. For instance, if there are modelsAuthor
,Post
andComment
, whereAuthor
has manyPost
s andPost
has manyComment
s, then this query:List
or:todayPosts = Post.where("post_date = ?", today).include(Author.class, Comment.class); List
will generate only three queries to database - one per model. All the dependencies (includes) will be eagerly loaded, and iteration via thetodayPosts = Post.where("post_date = ?", today).include(Author.class).include(Comment.class); todayPosts
list will not generate any more queries, even when a post author and comments are requested. Use this with caution as this method can allocate a lot of memory (obviously). This method will not follow relationships of related models, but rather only relationships of the current one.- Parameters:
classes
- list of dependent classes. These classes represent models with which a current model has a relationship.- Returns:
- instance of this
LazyList
-
toMaps
Converts the resultset to list of maps, where each map represents a row in the resultset keyed off column names.- Returns:
- list of maps, where each map represents a row in the resultset keyed off column names.
-
toXml
Generates a XML document from content of this list.- Parameters:
pretty
- pretty format (human readable), or one line text.declaration
- true to include XML declaration at the topattrs
- list of attributes to include. No arguments == include all attributes.- Returns:
- generated XML.
-
toJson
Generates JSON from content of this list- Parameters:
pretty
- true if you want pretty format, false if notattrs
- attributes to include, not providing any will include all.- Returns:
- generated JSON
-
load
This method exists to force immediate load from DB. Example;Person.find("name = ?", "Smith").load();
. It is not possible to call other methods after load(). The load() method should be the last to be called in the chain:Person.find("name = ?", "Smith").limit(10).load();
. This: will generate exception:Person.find("name = ?", "Smith").load().limit();
.- Returns:
- fully loaded list.
-
toSql
Same astoSql(true)
, seetoSql(boolean)
;- Returns:
- SQL in a dialect for current connection which will be used if you start querying this list.
-
toSql
Use to see what SQL will be sent to the database.- Parameters:
showParameters
- true to see parameter values, false not to.- Returns:
- SQL in a dialect for current connection which will be used if you start querying this list.
-
hydrate
protected void hydrate()- Specified by:
hydrate
in classAbstractLazyList<T extends Model>
-
collect
Collects values from a result set that correspond to a attribute name. For example, if a list contains collection ofPerson
models, then you can collect first names like this:List firstNames = Person.findAll().collect("first_name");
provided that the corresponding table has a columnfirst_name
. Keep in mind, that if all you need is a one column data, this method of getting it is not the most efficient (because since you are using a model, you will query all columns from a table, but will use only one). In these cases, you might want to considerBase.firstColumn(String, Object...)
andDB.firstColumn(String, Object...)
.- Parameters:
attributeName
- name of attribute to collect.- Returns:
- list of collected values for a column.
-
collectDistinct
-
collect
-
collectDistinct
-
dump
public void dump()Dumps contents of this list toSystem.out
. -
dump
Dumps content of list to a stream. Use for debugging.- Parameters:
out
-
-
writeExternal
- Specified by:
writeExternal
in interfaceExternalizable
- Throws:
IOException
-
readExternal
- Specified by:
readExternal
in interfaceExternalizable
- Throws:
IOException
ClassNotFoundException
-