Question

I want to implement a solution for frequent situation when I need to check if the record exists and create it if its not.

PROCE CreateIfNotFound:
  DEF INPUT PARAM bBuf AS HANDLE NO-UNDO.
  DEF INPUT PARAM chPred AS CHAR NO-UNDO.
  DEF INPUT PARAM iLockMode AS INT NO-UNDO:
  DEF VAR hQry AS HANDLE NO-UNDO.
  CREATE QUERY hQry.
  hQry:SET-BUFFERS(bBuf).
  hQry:QUERY-PREPARE("FOR EACH " + bBuf:NAME + " WHERE " + chPred).
  hQry:QUERY-OPEN.
  IF NOT hQry:GET-FIRST(LockMode) THEN DO:
    bBuf:BUFFER-CREATE.
    ???
  END.
END PROCE.

chPred supposed to be:

field1 = value1 AND field2 = value2 AND [...]

where field1, field2 and [...] are unique index fields. How should I initialize given fields of created record with given values without chPred parsing? Also delimiters can't help much because values may contain AND, = and generally any delimiter I could choose. Off course unlikely if it is _!^@_MY_DELIM!@^__, but I don't really like this solution.

Was it helpful?

Solution

The values that you want to use to initialize are in chPred correct?

Why make it more complicated?

It seems to me that if you know that chPred consists of equality matches (always uses "=") and always uses AND conditions then that string is exactly what you would want to use and it would be awfully easy to parse:

define variable x as character no-undo format "x(30)".
define variable s as character no-undo format "x(30)".
define variable a as character no-undo.
define variable b as character no-undo.

define variable i as integer no-undo.

x = "field1 = 3 and field2 = 5".

do while true:

  i = index( x, " and " ).
  if i < 0 then
    x = s.
   else
    assign
      s = substring( x, 1, i - 1 )
      x = substring( x, i + 4 )
    .

  assign
    a = entry( 1, s, "=" )
    b = entry( 2, s, "=" )
  .

  display
    s
    x
    a
    b
  .

  pause.

  if i <= 0 then leave.

end.

If the chPred string does not consist of equality matches and AND then you are obviously going to have to pass the values via some other parametrized scheme.

OTHER TIPS

An easy one:

define buffer btable for table.

Find first btable where whatever no-lock no-error.

if not available btable then do:

 create btable.

 assign btable.fields and so on.

end.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top