【问题标题】:How to use `export` with buffer handle?如何使用带有缓冲区句柄的“导出”?
【发布时间】:2019-08-15 20:52:15
【问题描述】:

在删除之前导出表的数据就像一个魅力。
示例:

OUTPUT TO VALUE("C:\mytable.d") APPEND.
EXPORT mybd.mytable.
OUTPUT CLOSE. 
DELETE mybd.mytable.

但是,当使用缓冲区句柄时,我还没有让它工作。 下面导出一个整数而不是删除的数据。

DEF INPUT PARAM hlTableToDelete AS HANDLE NO-UNDO.
...
OUTPUT TO VALUE("C:\" + hiTableToDelete:NAME + ".d") APPEND.
EXPORT hlTableToDelete:HANDLE.
OUTPUT CLOSE. 
hlTableToDelete:BUFFER-DELETE().

命令export 需要哪种语法才能工作并实际导出缓冲区句柄的数据?

【问题讨论】:

  • 问题应该是如何使用exportbuffer-handle - 你的第一个例子是导出一个/static/ 缓冲区。默认情况下,每个(临时)表都有一个与表同名的静态缓冲区。

标签: openedge progress-4gl


【解决方案1】:

EXPORT 仅适用于静态缓冲区。缓冲区句柄上没有 EXPORT 方法。

要获得等效功能,您需要编写一些循环遍历字段列表的代码。

这些方面的东西应该让你开始:

/* export data like EXPORT does
 *
 * makes no attempt to handle LOB data types
 *
 */

function exportData returns logical ( input bh as handle ):

  define variable bf as handle    no-undo.                                      /* handle to the field                          */
  define variable f  as integer   no-undo.                                      /* field number                                 */
  define variable i  as integer   no-undo.                                      /* array index                                  */

  do f = 1 to bh:num-fields:                                                    /* for each field...                            */

    bf = bh:buffer-field( f ).                                                  /* get a pointer to the field                   */

    if f > 1 then put stream expFile unformatted field_sep.                     /* output field separator                       */

    if bf:extent = 0 then                                                       /* is it an array?                              */
      put stream expFile unformatted
        ( if bf:data-type = "character" then                                    /* character data needs to be quoted to */
          quoter( string( bf:buffer-value ))                                    /* handle (potential) embedded delimiters       */
         else                                                                   /* and quotes within quotes!                    */
          string( bf:buffer-value )                                             /* other data types should not be quoted        */
        )
      .
     else                                                                       /* array fields need special handling           */
      do i = 1 to bf:extent:                                                    /* each extent is exported individually         */
        if i > 1 then put stream expFile unformatted field_sep.                 /* and a field separator                        */
        put stream expFile unformatted
          ( if bf:data-type = "character" then                                  /* see above...                                 */
            quoter( string( bf:buffer-value( i )))
           else  
            string( bf:buffer-value( i ))
          )
          field_sep
        .
      end.

  end.

  put stream expFile skip.                                                      /* don't forget the newline! ;-)                */

  return true.

end.

【讨论】:

  • 仅供参考,添加到汤姆的答案中,您看到的数字是缓冲区的句柄,类似于指针。
【解决方案2】:

Tom 例程的较短版本,它使用相同的代码路径处理范围字段和普通字段。

function exportBuffer returns logical ( input bh as handle ):

  define variable bf as handle    no-undo.
  define variable f  as integer   no-undo.
  define variable i  as integer   no-undo.

  do f = 1 to bh:num-fields:

    bf = bh:buffer-field( f ).

    do i = if bf:extent = 0 then 0 else 1 to bf:extent:        
      put stream expFile unformatted
        ( 
          if bf:data-type = "character" then
            quoter( string( bf:buffer-value( i ) ) )
          else  
            string( bf:buffer-value( i ) )
        )
        (
          if f = bh:num-fields and i = bf:extent then 
            ""
          else
            field_sep
        )
      .
    end.

  end.

  put stream expFile skip.

  return true.

end.

https://abldojo.services.progress.com:443/#/?shareId=5d5643554b1a0f40c34b8bed

如果真的、真的、真的、真的需要,我只会使用专有的导出语句格式,在任何其他情况下,我都会使用内置的序列化方法 (write-xml / write-json)。除了正确转义特殊字符外,它们还以全世界都能理解的格式导出所有数据。

【讨论】:

  • 带有逗号分隔符的 EXPORT 格式是 CSV 文件。很多东西仍然使用它。话虽如此 - 我同意 JSON 或 XML 更好。
【解决方案3】:

这很有帮助! 在我的测试中,我发现此代码不适用于带有 DATETIME 字段的 IMPORT 命令;这是我用来按照 IMPORT 预期的方式对其进行格式化的代码:

CASE FieldHandle:data-type:
    WHEN "datetime" THEN
    DO:
        OutStr = STRING(FieldHandle:BUFFER-VALUE,"99-99-9999THH:MM:SS.SSS").
        IF OutStr <> ? THEN
            OutStr = SUBSTRING(OutStr,7,4) + "-" + SUBSTRING(OutStr,1,5) + SUBSTRING(OutStr,11).
        PUT STREAM snapshot UNFORMATTED OutStr.
    END.

【讨论】:

  • 欢迎您,感谢您的贡献。您只需要应用代码块格式来确保符合换行符。我已经编辑了您的答案以包含此内容。看看以备将来参考。
猜你喜欢
  • 1970-01-01
  • 2017-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-30
  • 2013-09-26
相关资源
最近更新 更多