Scala: Exposing a JDBC ResultSet through a generator (iterable) -
i've got set of rows in database, , i'd provide interface spin through them this:
def findall: iterable[myobject]
where don't require having instances in memory @ once. in c# can create generators using yield, compiler takes care of converting code loops through recordset iterator (sort of inverting it).
my current code looks this:
def findall: list[myobject] = { val rs = getrs val values = new listbuffer[myobject] while ( rs.next() ) values += new valuefromresultset(rs) values.tolist }
is there way convert not store entire set in memory? perhaps use comprehension?
try extending iterator instead. haven't tested it, this:
def findall: iterator[myobject] = new iterator[myobject] { val rs = getrs override def hasnext = rs.hasnext override def next = new valuefromresultset(rs.next) }
this should store rs when it's called, , otherwise light wrapper calls rs.
if want save values traverse, check out stream.
Comments
Post a Comment