Hi All.
Can some tell me about difference between read() and readAll(), and also the syntax or how we can use in while loop.
Thanks,
Akshay
in DBAction I think readAll is used for example to count's
DBAction EXTITR = database.table("EXTITR").index("00").selectAllFields().build() DBContainer container = EXTITR.createContainer() container.set("EXCONO", program.LDAZD.CONO) container.set("EXITNO", mi.inData.get("ITNO")) double average = 0 Closure<?> listRatings = { DBContainer record -> average += record.getInt("EXRATG") } int count = EXTITR.readAll(container, 2, listRatings)
Akshay,You need to use the 'readAll()' if you are returning Resultset using 'Partial Key' for instance, and it will then allow you to return all matching records in a Custom API for instance. The 'read()' requires you to use the FULL Key.I have some questions myself on the functionality of the 'readAll', since it would be nice to be able to capture FIRST record returned only as a DBContainer record instead of assuming that it should ONLY retain the LAST record once executed. (There are ways around this of course, but I have yet to find the optimal way).
This is explained on the XtendM3 official GitHub documentation page.
Essentially, if you're reading one and only one record with full key, read is the way to go. If you are reading with partial key, whether you want one record or multiple, readAll is the way to go.
@rene-bottern readAll was never designed to read the first or the last record specifically. What you're describing is I think is a misunderstanding in how the method should be used.
Consider this example. The idea, is not to perform readAll and retain the last record read, but rather go through each individual record and process them accordingly per the functional specification.
def handleReleasedItems() {int currentCompany = (Integer)program.getLDAZD().CONO DBAction query = database.table("MITMAS").index("20").selection("MMCONO", "MMITNO", "MMITDS", "MMSTAT").build() DBContainer container = query.getContainer() container.set("MMCONO", currentCompany) container.set("MMSTAT", "20") query.readAll(container, 2, releasedItemProcessor)}Closure<?> releasedItemProcessor = { DBContainer container ->String description = container.get("MMITDS")String status = container.get("MMSTAT")// Use this found record as intended}
However, there are certain times, where you need to perform a partial read and get the first matching record. For that there is a method that is available but not documented on the website and it is as following
def handleReleasedItems() {int currentCompany = (Integer)program.getLDAZD().CONO DBAction query = database.table("MITMAS").index("20").selection("MMCONO", "MMITNO", "MMITDS", "MMSTAT").build() DBContainer container = query.getContainer() container.set("MMCONO", currentCompany) container.set("MMSTAT", "20") query.readAll(container, 2, 1, releasedItemProcessor)}Closure<?> releasedItemProcessor = { DBContainer container ->String description = container.get("MMITDS")String status = container.get("MMSTAT")// Use this found record as intended}
Now in this example, if you take a look at readAll, it almost looks identical, with the only difference that there is an additional integer argument passed in with the value of 1. This, 1, means that the readAll will only read the first matching record and then stops i.e. the third argument in the readAll method, is the number of records that readAll will read and then stop afterwards, even if there are more matches available. This is the most efficient way of reading records with partial keys.
A couple of additions to Erfan's brilliant answer.
The third parameter (number of records to read) must also be used in List transactions with the max records as parameter.
int pageSize = mi.getMaxRecords() <= 0 ? 10000 : mi.getMaxRecords(); dbAction.readAll(EXTMLA, 1, pageSize, listRecords);
Also to get specific records please try to have a look at the ExpressionFactory.
This can really enrich your read from the database.
You can do greaterOrEqual, lessOrEqual, in, like and lots of other stuff. Just like SQL!
Thanks Erfan, this is exactly what I was looking for! Interesting that the version of SDK I have (which should be the latest) does not have this definition?I can only see the one returning ALL records!?When I compile it in XTendM3 it is accepted however, so does the SDK need an update to the Class Definitions?
Hi Erfan,
does this method allow to make a partial read ?
like you create an expressuion factory, and then you want to read the resultset from record n to record n+100 (for instance) ?
Jérôme