X-Sharp / XSharpPublic

Public repository for the source code for the XSharp Compiler, Runtime, Project System and Tools.
Apache License 2.0
89 stars 36 forks source link

DBSetFilter() incompatibilities to VO #1489

Closed cpyrgas closed 2 weeks ago

cpyrgas commented 2 weeks ago

Main problem is that using DBSetFilter() with an empty or whitespace string results to a runtime exception. VO seems to clear the filter in this case:

FUNCTION Start() AS VOID
    LOCAL cFileName AS STRING
    cFileName := "c:\dbf\testfilter"

    RddSetDefault("DBFCDX")
//  RddSetDefault("DBFNTX")

    DbCreate(cFileName, {{"FLD","C",10,0}})
    DbUseArea(,,cFileName)
    DbAppend()
    FieldPut(1, "bbb")
    DbAppend()
    FieldPut(1, "aaa")

    DbGoTop()

    ? "setting filter to false:"
    ? DbSetFilter(,"1==2")
    DbGoTop()
    ? Eof() // TRUE in both for DBFCDX, VO returns FALSE for DBFNTX!!!
    ?
    ? "setting filter to empty string:"
    ? DbSetFilter(,"")
    DbGoTop() // runtime exception in X#
    ? Eof() // FALSE in VO
    ?
    ? "setting filter to whitespace:"
    ? DbSetFilter(,"    ")
    DbGoTop() // runtime exception in X#
    ? Eof() // FALSE in VO

    DbCloseArea()
System.NullReferenceException

XSharp.RDD.DBFCDX.[Method XSharp.RDD.DBF.EvalFilter(oBlock:XSharp.ICodeblock):System.Boolean]
XSharp.RDD.DBFCDX.[Method XSharp.RDD.Workarea.SkipFilter(nToSkip:System.Int32):System.Boolean]
XSharp.RDD.DBFCDX.[Method XSharp.RDD.DBF.SkipFilter(nToSkip:System.Int32):System.Boolean]
XSharp.RDD.DBFCDX.[Method XSharp.RDD.DBF.GoTop():System.Boolean]
XSharp.RDD.DBFCDX.[Method XSharp.RDD.DBFCDX.GoTop():System.Boolean]
XSharp.CoreDb.<>c.[Method XSharp.CoreDb+<>c.<GoTop>b__58_0():System.Boolean]
[SpecializedMethod XSharp.CoreDb.Do[System.Boolean](action:System.Func`1[[System.Boolean]]):System.Boolean]
[Method XSharp.CoreDb.GoTop():System.Boolean]
[Method XSharp.RT.Functions.DbGoTop():System.Boolean]
[Method xRuntime.Exe.Functions.Start():System.Void]  :  C:\VIDE\Projects\Project3\Applications\xRuntime\Prg\xRntime.prg  :  24
cpyrgas commented 2 weeks ago

There are also some more incompatibilities, but VO's behavior looks crazy for most, so not sure if we should deal with those:

  1. Passing the string as filter to the first param (which should accept a codeblock) of DBSetFilter() with for example DBSetFilter("alltrim(fld)=='bbb'") results to a runtime "Syntax Error" in VO, with message "Error setting filter". In X#, this works without errors, even though the type of the argument to the function was wrong. Not sure if we should change that.

  2. For some bizarre reason, VO for this code:

    DbSetFilter(,".T.")
    DbGotop()
    ? EOF()
    DbSetFilter(,".F.")
    DbGotop()
    ? EOF()
    DbSetFilter(,"1==2")
    DbGotop()
    ? EOF()
    DbSetFilter(,"1==1")
    DbGotop()
    ? EOF()
    DbSetFilter(,"SOMEINVALIDFILTER")
    DbGotop()
    ? EOF()

returns T,T,T,F,F for DBFCDX and F,F,F,F,F for DBFNTX!!! None of the two make any sense, so I guess we should ignore this.

RobertvanderHulst commented 2 weeks ago
  1. When you pass a string as first parameter to DbSetFilter() then we convert that to a codeblock and assume you meant to pass it as second parameter. This should not harm I think
  2. These should not produce an exception. Will fix that.
cpyrgas commented 2 weeks ago

Robert, sorry, the "runtime exception" comments in the code in https://github.com/X-Sharp/XSharpPublic/issues/1489#issuecomment-2175768911 were a leftover from previous tests, will fix that. None of those produce an exception in X#, it's just different behavior to (the very bizarre behavior of) VO.

X# produces a runtime exception only for the original report https://github.com/X-Sharp/XSharpPublic/issues/1489#issue-2359521275, DBSetFilter() with whitespace for the filter string, which is the main problem.

RobertvanderHulst commented 2 weeks ago

The problem with the white space should be fairly easy to fix. There is already the following code:

FUNCTION DbSetFilter(cbCondition, cCondition) AS LOGIC CLIPPER
    LOCAL sCondition AS STRING
    LOCAL cbCond AS CODEBLOCK
    if PCount() == 0 .or. (cbCondition:IsNil .and. cCondition:IsNil)
        return DbClearFilter()
    endif

We can add a condition there to test for an empty string.

cpyrgas commented 2 weeks ago

OK, done, but this exposed a deeper serious issue with DBSetFilter(), I opened https://github.com/X-Sharp/XSharpPublic/issues/1493 for it