FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour oDbf:Delete() error
Posts: 7317
Joined: Thu Oct 18, 2012 07:17 PM
oDbf:Delete() error
Posted: Thu Jun 03, 2021 09:09 AM
I have two database

I must delete the User and all permission of this user


The user is deleted as you can see here

USERS



The permissions are not deleted ( sometimes two or three records are deleted)




I not understood why


the function to delete is this

Code (fw): Select all Collapse
Static Function UtBorra(oUtenti,oPermessi,oGrid2,oGrid3,oBtnCambia)
   local cClave:= alltrim(oUtenti:Clave)  
   IF lABC .AND. !oUtenti:EoF()
      IF MsgYesNo("Cancellare i permessi dell'utente "+Alltrim(oUtenti:Nombre)+"?","Confermare!")
         oPermessi:Seek(cClave)
                DO WHILE !oPermessi:EoF()
            IF alltrim(oPermessi:Usuario)==cClave
                  oPermessi:Delete()
            ENDIF
            oPermessi:Skip()
         ENDDO
            oUtenti:Delete()
            oGrid2:GoUp()
            oGrid2:Refresh()
            oGrid2:SetFocus()
          Rinfresca(nMod,oUtenti,oPermessi,oGrid2,oGrid3,oBtnCambia)
      ENDIF
   ELSE
   ENDIF
   oGrid2:SetFocus()
RETURN (NIL)


the key and the name of the user is always 8 characters but I put an alltrim () because it can be even less than 8 characters like for example "TEST"

on the oPermessi there is a SetScope
static cKey
cKey:=oUtenti:Clave
oPermessi:setscope(cKey)

I did not understand why sometimes some records are cacenalled and other times none and other times only one,

Another test made this morning




yet putting an xbrowser after the seek makes me see all the modules of that user but then does not delete them all


Any solution ?

a test sample to try the problem
Code (fw): Select all Collapse
#include "FiveWin.ch"

static cKey,lABC

request dbfcdx
request dbffpt

Function test()

local ctest:="Test"

RddSetDefault( "DBFCDX" )
     oUtenti:= TUtenti():New()
     oModuli:= TModuli():New()
     oPermessi:=TPermessi():New()

     lABC :=.t.

      xbrowser oUtenti
      xbrowser oPermessi

         oPermessi:SetOrder(2)
         cKey:=oUtenti:Clave
        oPermessi:setscope(cKey)
        xbrowser oPermessi

        UtBorra(oUtenti,oPermessi)

           xbrowser oPermessi
return nil


Static Function UtBorra(oUtenti,oPermessi)
   local cClave:= alltrim(oUtenti:Clave)  // può essere meno di 8 caratteri  data errore
   IF lABC .AND. !oUtenti:EoF()
      IF MsgYesNo("Cancellare i permessi dell'utente "+Alltrim(oUtenti:Nombre)+"?","Confermare!")
         oPermessi:Seek(cClave)
         xbrowser oPermessi
         DO WHILE !oPermessi:EoF()
            IF alltrim(oPermessi:Usuario)==cClave
                  oPermessi:Delete()
            ENDIF
            oPermessi:Skip()
         ENDDO
            oUtenti:Delete()
            *oGrid2:GoUp()
            *oGrid2:Refresh()
            *oGrid2:SetFocus()
          *Rinfresca(nMod,oUtenti,oPermessi,oGrid2,oGrid3,oBtnCambia)
      ENDIF
   ELSE
   ENDIF
  * oGrid2:SetFocus()
RETURN (NIL)






CLASS TXData from TDataBase
       DATA cExePath  init cFilePath(GetModuleFileName( GetInstance() ))
     ENDCLASS


CLASS TUtenti from TXData
   METHOD New()
ENDCLASS

METHOD New( lShared ) CLASS TUtenti
   Default lShared := .t.
   ::super:Open(,::cExePath + "Utenti" ,, lShared)
   if ::use()
      ::setOrder(1)
      ::gotop()
   endif
RETURN Self
//----------------------------------------------------------------------moduli
CLASS TModuli from TXData
   METHOD New()
ENDCLASS

METHOD New( lShared ) CLASS TModuli
   Default lShared := .t.
   ::super:Open(,::cExePath + "Moduli" ,, lShared)
   if ::use()
      ::setOrder(1)
      ::gotop()
   endif
RETURN Self
//----------------------------------------------------------------------Permessi
CLASS TPermessi from TXData
   METHOD New()
ENDCLASS

METHOD New( lShared ) CLASS TPermessi
   Default lShared := .t.
   ::super:Open(,::cExePath + "Permessi" ,, lShared)
   if ::use()
      ::setOrder(1)
      ::gotop()
   endif
RETURN Self


write to me at silvio[dot]falconi[at]gmail[dot]com if you need the dbfs to test it
Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)

I use : FiveWin for Harbour March-April 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: oDbf:Delete() error
Posted: Fri Jun 04, 2021 05:34 AM

When you want to delete multiple records for a given condition

oDbf:Lock()
oDbf:Exec( { || DELETE FOR <cond> } )
oDbf:Unlock()

Regards



G. N. Rao.

Hyderabad, India
Posts: 7317
Joined: Thu Oct 18, 2012 07:17 PM
Re: oDbf:Delete() error
Posted: Fri Jun 04, 2021 12:49 PM

Nages,
someone (an american boy) sad me not use lock and unlock (and commit) because
tdatabase make it auto.

Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)

I use : FiveWin for Harbour March-April 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: oDbf:Delete() error
Posted: Fri Jun 04, 2021 02:30 PM
Silvio.Falconi wrote:Nages,
someone (an american boy) sad me not use lock and unlock (and commit) because
tdatabase make it auto.


Not FLOCK
Regards



G. N. Rao.

Hyderabad, India
Posts: 7317
Joined: Thu Oct 18, 2012 07:17 PM
Re: oDbf:Delete() error
Posted: Fri Jun 04, 2021 08:24 PM

then

DO WHILE !oPermessi:EoF()
oPermessi:Lock()
oPermessi:Exec( { || DELETE FOR oPermessi:Usuario==oUtenti:Clave } )
oPermessi:Unlock()

        oPermessi:Skip()
 ENDDO

is it right ?

make me error
source\test.prg(688) Error E0030 Syntax error "syntax error at 'FOR'"
1 error

Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)

I use : FiveWin for Harbour March-April 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
Posts: 1487
Joined: Tue Jun 14, 2016 07:51 AM
Re: oDbf:Delete() error
Posted: Sat Jun 05, 2021 08:48 AM
Silvio.Falconi wrote:then

DO WHILE !oPermessi:EoF()
oPermessi:Lock()
oPermessi:Exec( { || DELETE FOR oPermessi:Usuario==oUtenti:Clave } )
oPermessi:Unlock()

oPermessi:Skip()
ENDDO

is it right ?

make me error
source\test.prg(688) Error E0030 Syntax error "syntax error at 'FOR'"
1 error


No, it's not

As i see it, the

oPermessi:Lock() // is actually a Flock and therefore not needed in a loop
oPermessi:Exec( { || DELETE FOR oPermessi:Usuario==oUtenti:Clave } ) // block that run all data inside the exec(), so not in a loop

Only the 3 lines from Mr. Rao seems to be needed.

the <cond> is the tricky part... what code is needed

// other post :

If you want to delete all records where the field INVNUM == nInvoice, i.e., "1234567890128", then:
[code]
Local nInvoice := "1234567890128"

oPartres:Exec( <||
DELETE ALL FOR ALLTRIM(FIIELD->INVNUM) == nInvoice
> )
[code]

viewtopic.php?f=3&t=37616&hilit=delete+for
Marc Venken

Using: FWH 23.08 with Harbour
Posts: 1487
Joined: Tue Jun 14, 2016 07:51 AM
Re: oDbf:Delete() error
Posted: Sat Jun 05, 2021 08:55 AM
Maybe this will do it almost...

Code (fw): Select all Collapse
nInvoice := "1234567890128"
oPermessi:Lock() 

oPermessi:Exec( <|| DELETE ALL FOR ALLTRIM(FIELD->INVNUM) == nInvoice  > )  // here your code

oPermessi:Unlock()
Marc Venken

Using: FWH 23.08 with Harbour
Posts: 7317
Joined: Thu Oct 18, 2012 07:17 PM
Re: oDbf:Delete() error
Posted: Sat Jun 05, 2021 10:28 AM
Marc Venken wrote:Maybe this will do it almost...

Code (fw): Select all Collapse
nInvoice := "1234567890128"
oPermessi:Lock() 

oPermessi:Exec( <|| DELETE ALL FOR ALLTRIM(FIELD->INVNUM) == nInvoice  > )  // here your code

oPermessi:Unlock()



usuario and clave are 8 cr

DO WHILE !oPermessi:EoF()
oPermessi:Lock()
oPermessi:Exec( { || DELETE FOR alltrim(oPermessi:Usuario)==oUtenti:Clave } )
oPermessi:Unlock()
oPermessi:Skip()
ENDDO

I tried also with oPermessi:Exec( { || DELETE FOR alltrim(FIELD->Usuario)==oUtenti:Clave } )
give me an error on compilation
Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)

I use : FiveWin for Harbour March-April 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
Posts: 7317
Joined: Thu Oct 18, 2012 07:17 PM
Re: oDbf:Delete() error
Posted: Sat Jun 05, 2021 10:53 AM

Now run ok

DO WHILE !oPermessi:EoF()
oPermessi:Lock()
oPermessi:Exec( { ||
DELETE FOR ALLTRIM(oPermessi:Usuario)==ALLTRIM(oUtenti:Clave)
} )
oPermessi:Unlock()
oPermessi:Skip()
ENDDO

and it delete all records but only some records because on Permessi dbf there is a filter, it deleted only that record have M = 5 :)

Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)

I use : FiveWin for Harbour March-April 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
Posts: 7317
Joined: Thu Oct 18, 2012 07:17 PM
Re: oDbf:Delete() error
Posted: Sat Jun 05, 2021 11:08 AM
Now run ok

Code (fw): Select all Collapse
Static Function UtBorra(oUtenti,oPermessi,oGrid2,oGrid3,oBtnCambia)
   local cClave:= oUtenti:Clave
      cKey:=(oUtenti:Clave)
   IF lABC .AND. !oUtenti:EoF()
      IF MsgYesNo("Cancellare i permessi dell'utente "+Alltrim(oUtenti:Nombre)+"?","Confermare!")
         oPermessi:Seek(cClave)
         oPermessi:SetScope(cKey)
          DO WHILE !oPermessi:EoF()
             oPermessi:Lock()
             oPermessi:Exec( { ||
             DELETE FOR ALLTRIM(oPermessi:Usuario)==ALLTRIM(oUtenti:Clave)
          } )
            oPermessi:Unlock()
            oPermessi:Skip()
         ENDDO
            oUtenti:Delete()
            oGrid2:GoUp()
            oGrid2:Refresh()
            oGrid2:SetFocus()
          Rinfresca(nMod,oUtenti,oPermessi,oGrid2,oGrid3,oBtnCambia)
      ENDIF
   ELSE
   ENDIF
   oGrid2:SetFocus()
RETURN (NIL)
Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)

I use : FiveWin for Harbour March-April 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
Posts: 1487
Joined: Tue Jun 14, 2016 07:51 AM
Re: oDbf:Delete() error
Posted: Sun Jun 06, 2021 08:30 AM
Hey Silvio,

Great that you have it working !

Could you please try this function (1 loop less that yours) for my personel curiossity ?
I would like to know if

oPermessi:Exec( { ||
DELETE FOR ALLTRIM(oPermessi:Usuario)==ALLTRIM(oUtenti:Clave)
} )

acts like a dbeval and processes all scopes records...

Code (fw): Select all Collapse
Static Function UtBorra(oUtenti,oPermessi,oGrid2,oGrid3,oBtnCambia)
   local cClave:= oUtenti:Clave
      cKey:=(oUtenti:Clave)
   IF lABC .AND. !oUtenti:EoF()
      IF MsgYesNo("Cancellare i permessi dell'utente "+Alltrim(oUtenti:Nombre)+"?","Confermare!")
         oPermessi:Seek(cClave)
         oPermessi:SetScope(cKey)
         oPermessi:Lock()
          oPermessi:Exec( { ||
             DELETE FOR ALLTRIM(oPermessi:Usuario)==ALLTRIM(oUtenti:Clave)
          } )
           oPermessi:Unlock()
            oUtenti:Delete()
           oGrid2:GoUp()
           oGrid2:Refresh()
           oGrid2:SetFocus()
          Rinfresca(nMod,oUtenti,oPermessi,oGrid2,oGrid3,oBtnCambia)
      ENDIF
   ELSE
   ENDIF
   oGrid2:SetFocus()
RETURN (NIL)
Marc Venken

Using: FWH 23.08 with Harbour
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: oDbf:Delete() error
Posted: Sun Jun 06, 2021 10:09 AM
Code (fw): Select all Collapse
         oPermessi:Lock()
          oPermessi:Exec( { ||
             DELETE FOR ALLTRIM(Usuario)==ALLTRIM(oUtenti:Clave)
          } )


The DELETE FOR statement should be PURE RDD statement,
Regards



G. N. Rao.

Hyderabad, India
Posts: 4840
Joined: Fri Nov 18, 2005 04:52 PM
Re: oDbf:Delete() error
Posted: Sun Jun 06, 2021 06:30 PM
Maybe try this:

Code (fw): Select all Collapse
          oPermessi:Lock()
          oPermessi:Exec( { |oUtenti|
             DELETE FOR ALLTRIM(Usuario)==ALLTRIM(oUtenti:Clave)
          } )
FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: oDbf:Delete() error
Posted: Sun Jun 06, 2021 07:16 PM
James Bott wrote:Maybe try this:

Code (fw): Select all Collapse
          oPermessi:Lock()
          oPermessi:Exec( { |oUtenti|
             DELETE FOR ALLTRIM(Usuario)==ALLTRIM(oUtenti:Clave)
          } )


This gives wrong results.
Exec method evaluates the codeblock with Self as parameter.
Code (fw): Select all Collapse
   METHOD Exec( bBlock )      INLINE ( ::cAlias )->( Eval( bBlock, Self ) )

So, the variable oUtenti gets the value of oPermiessi inside this codeblock.
Regards



G. N. Rao.

Hyderabad, India
Posts: 7317
Joined: Thu Oct 18, 2012 07:17 PM
Re: oDbf:Delete() error
Posted: Sun Jun 06, 2021 08:15 PM
Marc Venken wrote:Hey Silvio,


Mark,
on my procedure when I delete a User I must delete all permissions of that user , but when I call ut_borra() function to delete that use I have on Permission dbf a set scope
Now with the new fwh ( april 2021) I can erase that setscope with setscope(nil), then I can delete all the permissions of that user with

Code (fw): Select all Collapse
 DO WHILE !oPermessi:EoF()
             oPermessi:Lock()
             oPermessi:Exec( { ||
             DELETE FOR ALLTRIM(oPermessi:Usuario)==ALLTRIM(oUtenti:Clave)
          } )
            oPermessi:Unlock()
            oPermessi:Skip()
         ENDDO


then I can delete the user with oUtenti:Delete()

before it is not working because need a Flock (1) and I had the setscope on Usuario field ( user) (2)
with these two problems only some records were deleted, but now it seems to work fine





I didn't see the deleted records and I trusted them
I did not see them because the dbf was indexed with condition! Deleted ()
and then there was the scope on the user and on the number of menus (green box)
so if I selected a user in the xbrowse above I could not see the records in the xbrowse that it was below,
I didn't select it because the user had been deleted but his permissions were still in the archive
Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)

I use : FiveWin for Harbour March-April 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com