incubator-empire-db-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Rainer Döbele <doeb...@esteam.de>
Subject re: Detecting failure to retrieve data
Date Wed, 07 Oct 2009 20:51:09 GMT
Hi Andrew,

when you call DBReader.open() the cursor is positioned before the first record not on the
first record.
The open method assumes that you want to retrieve a list of records hence you should always
iterate through the reader like this:
	reader.open(cmd, conn);
	while (reader.moveNext())
	{	// Init updateable record
		reader.initRecord(EMP, record);
	}
This will work for 0 to n records.

However, if you know for sure that the result is a single record or none, you can instead
use the method getRecordData(DBCommandExpr cmd, Connection conn) on DBReader. This method
opens the reader and moves to the first record. If no record was found this function returns
false.

About your question whether to subclass DBRecord or use beans:
Basically DBRecord objects are something like dynamic beans in a way that the properties don't
have to be declared at compile-time and that instead of an individual getter / setter for
each property you have several generic ones that require a field reference. 
Personally I prefer subclassing a DBRecord and use it to create other type safe methods that
work on these derived types. This gives you also the opportunity to override methods like
getValue(), setValue(), onFieldChanged(), isFieldReadOnly() etc.
Using beans IMO only makes sense if you either need to work with other frameworks that require
beans or if you need to serialize your beans.

Regards
Rainer


acooke.org@gmail.com wrote:
> re: Detecting failure to retrieve data
> 
> Hi,
> 
> Another question - hopefully simpler.  How do I detect when a query
> has failed to retrieve data?  In particular I have code like:
> 
>     cmd = schema.createCommand()
>     cmd.select(schema.CANONICALS.getColumns())
>     cmd.where(schema.CANONICALS.VALUE.is(value))
>     row = new DBRecord(schema.CANONICALS)
>     reader = new DBReader()
>     reader.open(cmd, cnxn)
>     ok = Schema.CANONICALS.initRecord(row, reader)
> 
> where ok is true even when there are no data (value does not exist in
> the VALUE column of the CANONICALS table and "row" contains nulls)
> (and similarly if I use the initRecord method on the reader).
> 
> Should ok be false above?  It appears to be true, but I must confess
> that I am also using Scala (I have modified it to look like Java
> above, I hope) and it is possible I am doing something stupid with
> Scala...
> 
> Finally, I was originally thinking that I should subclass DBRecord and
> so map rows to objects, but now I am thinking that it would be better
> to use beans.  Is there any reason to prefer one approach rather than
> another, or are they for different cases somehow?
> 
> Thanks,
> Andrew

Mime
View raw message