如何用动态 "queries" 替换静态 "query" 的工作方式?

How to replace static "query" way of working by dynamic "queries"?

在此中,我询问了如何处理可能定义或未定义table的情况。我试图遵循的建议解决方案是使用动态“查询”而不是静态查询。不幸的是我不知道该怎么做,有人可以帮我吗?让我举个例子:

静态工作方式:

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.

动态工作方式:

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.

BUFFER-CREATE还是其他什么方法,参数应该怎么填(比如ASSIGN Table1.Field1 = 123),...?

使用:

def var hb as handle no-undo.

create buffer hb for table "mytable".
hb:buffer-create().
assign
   hb::myfield = 123
   hb::another = "ok"
   .
hb:buffer-release()

finally:
   delete object hb no-error.
end finally.

请注意 hb::myfield 对于

是 shorthand
hb:buffer-field('myfield'):buffer-value

还要注意,在使用动态对象时,您要负责垃圾回收。如果创建了,需要删除。

动态查找独特部分:

def var hb     as handle  no-undo.
def var lfound as logical no-undo.

create buffer hb for table 'Table1'.

lfound = hb:find-uniqe(
            'where field1 = 123'
            +  ' and field2 = ' + quoter( current-table.field2 ) )
            +  ' and upper( field3 ) = "constant-string"'
            +  ' and field4 = ' + quoter( temp-table.field4 ) 
         ) no-error.

或使用替代品:

lfound = hb:find-unique(
            substitute(
               'where field1 = &1':u
               +  ' and field2 = &2':u
               +  ' and upper( field3 ) = &3':u
               +  ' and field4 = &4':u,
               123,
               quoter( current-table.field2 ),
               quoter( 'constant-string' ),
               quoter( temp-table.field4 )
            )
         ) no-error.

您很快就会想要创建一个查询生成器 class / 函数来处理创建查询并可能还处理 运行-time 验证,您现在也负责这些。