oDbf:Delete() error

User avatar
Silvio.Falconi
Posts: 7170
Joined: Thu Oct 18, 2012 7:17 pm
Been thanked: 4 times

oDbf:Delete() error

Post by Silvio.Falconi »

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
Image


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

Image


I not understood why


the function to delete is this

Code: Select all | Expand


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
Image



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: Select all | Expand


#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
User avatar
nageswaragunupudi
Posts: 10729
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Been thanked: 10 times
Contact:

Re: oDbf:Delete() error

Post by nageswaragunupudi »

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
User avatar
Silvio.Falconi
Posts: 7170
Joined: Thu Oct 18, 2012 7:17 pm
Been thanked: 4 times

Re: oDbf:Delete() error

Post by Silvio.Falconi »

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
User avatar
nageswaragunupudi
Posts: 10729
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Been thanked: 10 times
Contact:

Re: oDbf:Delete() error

Post by nageswaragunupudi »

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
User avatar
Silvio.Falconi
Posts: 7170
Joined: Thu Oct 18, 2012 7:17 pm
Been thanked: 4 times

Re: oDbf:Delete() error

Post by Silvio.Falconi »

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
User avatar
Marc Venken
Posts: 1485
Joined: Tue Jun 14, 2016 7:51 am
Location: Belgium

Re: oDbf:Delete() error

Post by Marc Venken »

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
User avatar
Marc Venken
Posts: 1485
Joined: Tue Jun 14, 2016 7:51 am
Location: Belgium

Re: oDbf:Delete() error

Post by Marc Venken »

Maybe this will do it almost...

Code: Select all | Expand


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
User avatar
Silvio.Falconi
Posts: 7170
Joined: Thu Oct 18, 2012 7:17 pm
Been thanked: 4 times

Re: oDbf:Delete() error

Post by Silvio.Falconi »

Marc Venken wrote:Maybe this will do it almost...

Code: Select all | Expand


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
User avatar
Silvio.Falconi
Posts: 7170
Joined: Thu Oct 18, 2012 7:17 pm
Been thanked: 4 times

Re: oDbf:Delete() error

Post by Silvio.Falconi »

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
User avatar
Silvio.Falconi
Posts: 7170
Joined: Thu Oct 18, 2012 7:17 pm
Been thanked: 4 times

Re: oDbf:Delete() error

Post by Silvio.Falconi »

Now run ok

Code: Select all | Expand

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
User avatar
Marc Venken
Posts: 1485
Joined: Tue Jun 14, 2016 7:51 am
Location: Belgium

Re: oDbf:Delete() error

Post by Marc Venken »

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: Select all | Expand


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
User avatar
nageswaragunupudi
Posts: 10729
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Been thanked: 10 times
Contact:

Re: oDbf:Delete() error

Post by nageswaragunupudi »

Code: Select all | Expand

        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
User avatar
James Bott
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA
Contact:

Re: oDbf:Delete() error

Post by James Bott »

Maybe try this:

Code: Select all | Expand


          oPermessi:Lock()
          oPermessi:Exec( { |oUtenti|
             DELETE FOR ALLTRIM(Usuario)==ALLTRIM(oUtenti:Clave)
          } )
FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
User avatar
nageswaragunupudi
Posts: 10729
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Been thanked: 10 times
Contact:

Re: oDbf:Delete() error

Post by nageswaragunupudi »

James Bott wrote:Maybe try this:

Code: Select all | Expand


          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: Select all | Expand

  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
User avatar
Silvio.Falconi
Posts: 7170
Joined: Thu Oct 18, 2012 7:17 pm
Been thanked: 4 times

Re: oDbf:Delete() error

Post by Silvio.Falconi »

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: Select all | Expand

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


Image


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
Post Reply