foxpro游标大小

时间:2010-01-22 14:43:33

标签: cursor foxpro

这似乎是一个简单的问题,但我似乎无法在任何地方找到解决方案。我的同事和我正在开发一个使用Foxpro xml转储工具的应用程序。它工作得很好,但我们希望根据一些大小限制将表拆分成多个文件。

这似乎应该是容易的部分:如何在Foxpro中找到游标的大小?

3 个答案:

答案 0 :(得分:2)

RECSIZE()将返回单个行的长度(以字节为单位) - RECCOUNT()将为您提供大小。已经讨论过的所有元素都是准确的。

关于备注字段,如果您需要知道THEY的大小,您可能需要在表结构中为“MemoLength”添加一个新的整数列。然后

用len替换所有memoLength(alltrim(YourMemoField))

然后,您可以使用MemoLength来帮助确定您的细分组,方法是将此列大小与您要提取的其余RECSIZE()*行一起考虑。

此外,您可能希望根据表的主键列运行查询,您可以将其用作链接并执行类似的操作...

选择YourPrimaryKey,len(alltrim(YourMemoField))作为MemoLength   来自YourTable   进入游标SomeHoldingCursor readwrite

..或者,选择

进入表MemSizeTable

在MemSizeTable上构建索引,您可以使用该联接来获取更多信息。这样,它不会扭曲原始记录大小,也不会破坏原始表结构,但是通过关系,您仍然可以提取所需的元素。

答案 1 :(得分:1)

如果您的意思是文件大小,您可以通过调用DBF()函数并将光标作为别名来查找游标所涉及的文件,检查返回值扩展名是.dbf,然后使用文件函数来读取文件大小。光标可能在内存中(报告的'文件名'将具有.tmp扩展名,如果我没记错的话),所以另一种方法是使用RECCOUNT()(获取行数)和AFIELDS()(获取每行的大小)来近似文件大小。 (有时可以通过在生成查询中包含NOFILTER子句将内存游标强制转换为磁盘)

答案 2 :(得分:1)

这是一个基于示例游标和虚假记录的完整功能......关键功能是DumpXML()例程,需要转储文件的别名,每个文件的大小要限制它(在“k”大小),以及您希望将XML转储为的文件名前缀。它将自动生成一个排序ex:MyXMLOutput1.xml,MyXMLOutput2.xml,MyXMLOutput3.xml等,无论它需要多少实例。花了我大约15分钟。

CREATE CURSOR SomeTest ;
    (   SomeField1      c(10),;
        AnotherField    i,;
        SomeNumber      N(8,2),;
        MemoFld         m,;
        SomeDateTime    t;
    )

INSERT INTO SomeTest VALUES ( "testchar10", 9403, 12345.78, "some memo value string", DATETIME() )


DumpXML( ALIAS(), 300, "MyXML" )


FUNCTION dumpXML
LPARAMETERS cAliasName, nSizeLimit, cNameOfXMLOutput
    IF NOT USED( cAliasName )
        RETURN ""
    ENDIF 

    */ Assume size limit in "k"
    nSizeLimit  = nSizeLimit * 1024

    SELECT ( cAliasName )

    */ Get a copy of the structure without disrupting original
    USE IN SELECT( "MySample" )  && pre-close in case left open from prior cycle
    SELECT * ;
        FROM ( cAliasName ) ;
        WHERE RECNO() = 1;
        INTO CURSOR MySample READWRITE

    SELECT MySample
    */ Populate each field with maximum capacities... typically
    */ critical for your char based fields
    AFIELDS( aActualStru )
    cMemoFields = ""
    lHasMemoFields = .f.
    FOR I = 1 TO FCOUNT()
        cFieldName = FIELD(I)
        DO CASE 
        CASE aActualStru[i,2] = "C"
            replace &cFieldName WITH REPLICATE( "X", aActualStru[i,3] )

        CASE aActualStru[i,2] = "L"
            replace &cFieldName WITH .T.

        CASE aActualStru[i,2] = "D"
            replace &cFieldName WITH DATE()

        CASE aActualStru[i,2] = "T"
            replace &cFieldName WITH DATETIME()

        CASE aActualStru[i,2] = "M"
            */ Default memo as a single character to ensure
            */ closing field name </endoffield> included in XML
            replace &cFieldName WITH "X"

            */ if a MEMO field, add this element to a string 
            */ to be macro'd to detect its size... Each record
            */ can contain MORE than one memo field...
            */ Ex: + LEN( ALLTRIM( MemoFld ))
            lHasMemoFields = .T.
            cMemoFields = cMemoFields + " + len( ALLTRIM( " + cFieldName + " ))"

        CASE aActualStru[i,2] = "I"
            */ Integer, force to just 10 1's
            replace &cFieldName WITH 1111111111


        CASE aActualStru[i,2] = "N"
            */ Actual numeric and not an integer, double or float
            */ Allow for full length plus decimal positions
            NumValue = VAL( REPLICATE( "9", aActualStru[i,3] - aActualStru[i,4] - 1 );
                            + "." + REPLICATE( "9", aActualStru[i,4] ))
            replace &cFieldName WITH NumValue

        ENDCASE     
    ENDFOR 

    */ Strip leading " + " from the string in case multiple fields
    IF lHasMemoFields
        cMemoFields = SUBSTR( cMemoFields, 3 )
    ENDIF 

    cXML = ""
    LOCAL oXML as XMLAdapter

    oXML = CREATEOBJECT( "XMLAdapter" )
    oXML.AddTableSchema( "MySample" )
    oXML.ToXML( "cXML", "", .f. )

    */ Now, determine the size of the per record at its full length -- less memo        
    nSizeOfPerRecord = LEN( STREXTRACT( cXML, "<MySample>", "</MySample>", 1, 4 ))

    */ and the rest of the header per XML dump
    nSizeOfSchema = LEN( cXML ) - nSizeOfPerRecord


    */ Now, back to the production alias to be split
    SELECT( cAliasName )
    nNewSize = 0
    nXMLCycle = 0

    */ if we just started, or finished writing another block
    */ and need to generate a new group of XML dump reset size
    nNewSize = nSizeOfSchema

    */ Always blank out the temp cursor for each batch...
    SELECT MySample
    ZAP 

    SELECT ( cAliasName )
    SCAN 
        IF lHasMemoFields
            nAllMemoSizes = &cMemoFields
        ELSE 
            nAllMemoSizes = 0
        ENDIF 

        IF nNewSize + nSizeOfPerRecord + nAllMemoSizes > nSizeLimit
            */ The upcoming record will have exceeded capacity, finish XML
            */ with all records up to this point
            nXMLCycle = nXMLCycle + 1
            cNewFile = FULLPATH( cNameOfXMLOutput + ALLTRIM( STR( nXMLCycle )) + ".XML" )

            oXML = CREATEOBJECT( "XMLAdapter" )
            oXML.AddTableSchema( "MySample" )
            */ Generate the XML cycle of these qualified records...
            oXML.ToXML( cNewFile, "", .t. )

            */ restart for next pass of data
            nNewSize = nSizeOfSchema

            */ Always blank out the temp cursor for each batch...
            SELECT MySample
            ZAP 
        ENDIF 

        */ Add record to total size...
        nNewSize = nNewSize + nSizeOfPerRecord + nAllMemoSizes

        */ we have a record to be included in segment dump...
        */ scatter from the original table and gather into the temp
        SCATTER MEMO NAME oFromOriginal

        SELECT MySample
        APPEND BLANK
        GATHER MEMO NAME oFromOriginal

        */ back to original table driving the XML Dump process
        SELECT ( cAliasName )

    ENDSCAN 


    */ if the "MyTable" has records not yet flushed from limit, write that too
    IF RECCOUNT( "MySample" ) > 0
        */ The upcoming record will have exceeded capacity, finish XML
        */ with all records up to this point
        nXMLCycle = nXMLCycle + 1
        cNewFile = FULLPATH( cNameOfXMLOutput + ALLTRIM( STR( nXMLCycle )) + ".XML" )

        oXML = CREATEOBJECT( "XMLAdapter" )
        oXML.AddTableSchema( "MySample" )
        */ Generate the XML cycle of these qualified records...
        oXML.ToXML( cNewFile, "", .t. )
    ENDIF 

    */ Done with the "MySample" for cursor to XML analysis...
    USE IN SELECT( "MySample" )

ENDFUNC