Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create Objects from records via SQL

Status
Not open for further replies.

Olaf Doschke

Programmer
Oct 13, 2004
14,847
DE
Code:
oCollection = CreateObject("Collection")
Select oCollection.add(recordobject(),Transform(Rand()*100000)) from foxcode into Array garbage

* just to show one sample
loRecord2=oCollection.Item(3)
? loRecord2.UniqueID

Function recordobject()
   Local loRecord
   Scatter memo name loRecord
   Return loRecord
EndFunc

There's just one downside, that VFPs SQL engine always processes the first record twice and so makes one first additional call to oCollection.add(), because it needs to find out the structure of the result cursor.

Therefore you can't use for example foxcode.uniqueid as the key of each collections item (second parameter of Add()), although it's really unique.

The sql result (in this case an array) is just a single boolean column, but you also have a collection of record objects created alongside, starting at oCollection.Item(2).

Bye, Olaf.
 
True, but unfortunately you can't add the recordobjects with the records primary key as the collection items key, that's the real problem.

Of course you could also just add the items without a key or create an array with
Code:
Select recordobject() from foxcode into Array aRecords
if you only need/want the array of recordobjects.

Anyway it has it's limits, if you begin to join tables.

Bye, Olaf.
 
You can subclass the collection and fixed it.
Try this:

<code>
release oCollection
public oCollection
use (_foxcode) again

*- my collection class
oCollection = CreateObject("RecordColl")

s=seconds()
select oCollection.add(recordobject(), foxcode.UniqueID+iif(empty(Abbrev), Expanded, Abbrev)) ;
from foxcode ;
where !empty(UniqueID);
into array garbage
? seconds()-s

loRecord2=oCollection.Item(1) && first record
? loRecord2.UniqueID
loRecord2=oCollection.Item(2) && SECOND record
? loRecord2.UniqueID

Function recordobject()
Local loRecord
Scatter memo name loRecord
Return loRecord
EndFunc

define class RecordColl as collection
lFirst=.T. && first record will be ignored
procedure Add (puData, puKey)
if this.lFirst
this.lFirst=.F.
nodefault
return
endif
endproc
enddefine
</code>

Tomas
 
Nice idea, thanks.

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top