In this other question, I asked how to handle the situation where a table might be defined or not. The proposed solution I'm trying to follow, is working with dynamic "queries" instead of static ones. Unfortunately I have no idea how to do this, can anybody help me with this? Let me give you an example:
Static way of working:
FIND Table1 WHERE Table1.field1 = 123
AND Table1.field2 = Current-Table.field2
AND UPPER(Table1.field3) = "Constant-string"
AND Table1.field4 = temp-table.field4 NO-ERROR.
IF NOT AVAILABLE Table1
THEN DO:
CREATE Table1.
ASSIGN Table1.field1 = 123
Table1.field2 = Current-Table.field2
Table1.field3 = "Constant-string"
Table1.field4 = temp-table.field4.
RELEASE Table1.
END.
Dynamic way of working:
CREATE BUFFER h-Table1 FOR TABLE "Table1" NO-ERROR.
IF VALID-HANDLE(h-Table1)
THEN DO:
L-Found = h-Table1:FIND-FIRST("WHERE Table1.field1 = " + STRING(123) +
"AND Table1.field2 = " + STRING(Current-Table.field2) +
"AND UPPER(Table1.field3) = 'Constant-string'" +
"AND Table1.field4 = " + temp-table.field4) NO-ERROR.
IF NOT L-Found
THEN DO:
h-Table1:BUFFER-CREATE("").
END.
ELSE MESSAGE "FOUND".
END.
Is it BUFFER-CREATE or some other method, how should I fill in the parameters (like ASSIGN Table1.Field1 = 123), ...?
Use:
Note that hb::myfield is shorthand for
And also beware that when using dynamic objects, you are responsible for garbage collection. If you create it, you need to delete it.
Dynamic find-unique part:
or using a substitute:
You will soon want to create a query builder class / function to handle creating queries and possibly also handling run-time validation which you are now also responsible for.