Page 1 of 1

create a function with Tdatabase

Posted: Sat Apr 24, 2021 9:29 am
by Silvio.Falconi
I would like to have a function that opens an archive and sets a series of indexes
I tried to do this function but it doesn't turn me at all I don't understand where I'm wrong

sample of Usage

oCustomers:Open_Db( "customer", , {FIRST,LAST} )
oCustomers:SetOrder( "FIRST" )
oCustomers:GoTop()



Code: Select all | Expand


FUNCTION Open_Db( cArchivio, aIdx )
   LOCAL cAlias
   LOCAL oDbf
   LOCAL i
   STATIC _Select_
   DEFAULT _Select_ := 0

   cAlias := "TD" + PADL( ++_Select_, 3, "0" )

   DbUseArea( .T. ,, cArchivio, cAlias, .T. )
   IF VALTYPE( aIdx ) == "A"
      FOR i := 1 TO LEN( aIdx )
         DBSETINDEX( aIdx[ i ] )
      NEXT
   ENDIF

   oDbf := TDatabase():Open(,cAlias)

RETURN oDbf



return me a message "TD0001.dbf not avaible"


then I tried with

Code: Select all | Expand

FUNCTION Open_Db( cArchivio, aIdx )
   LOCAL cAlias
   LOCAL oDbf
   LOCAL i
   STATIC _Select_
   DEFAULT _Select_ := 0

   oDbf := TDatabase():Open(,cArchivio)

   IF VALTYPE( aIdx ) == "A"
      FOR i := 1 TO LEN( aIdx )
         DBSETINDEX( aIdx[ i ] )
      NEXT
   ENDIF
RETURN oDbf
 


but make error because

Error description: Error DBCMD/2001 Workarea not in use: ORDLISTADD

Stack Calls
===========
Called from: => ORDLISTADD( 0 )
Called from: ../../../rddord.prg => DBSETINDEX( 0 )

what do you recommend?

Re: create a function with Tdatabase

Posted: Sat Apr 24, 2021 11:19 am
by Antonio Linares
Silvio,

Class TDataBase has a destructor method, so when the object gets out of scope then it gets called

change local oDbf into static oDbf (or keep it in an array) so the object is not destroyed

Re: create a function with Tdatabase

Posted: Sat Apr 24, 2021 11:49 am
by nageswaragunupudi
oDbf := TDatabase():Open(,cAlias)


This syntax is wrong. The DBF is already open.

Correct:

Code: Select all | Expand

oDbf := TDatabase():New( SELECT( cAlias ) )

Re: create a function with Tdatabase

Posted: Sat Apr 24, 2021 11:54 am
by nageswaragunupudi

Code: Select all | Expand

  oDbf := TDatabase():Open(,cArchivio)

   IF VALTYPE( aIdx ) == "A"
      FOR i := 1 TO LEN( aIdx )
         DBSETINDEX( aIdx[ i ] )
      NEXT
   ENDIF
 


Change this as:

Code: Select all | Expand

  oDbf := TDatabase():Open(,cArchivio)

   IF VALTYPE( aIdx ) == "A"
      oDbf:Exec( <||
         local i
         FOR i := 1 TO LEN( aIdx )
            DBSETINDEX( aIdx[ i ] )
         NEXT
         return nil
         > )
   ENDIF
 

Re: create a function with Tdatabase

Posted: Sun Apr 25, 2021 4:35 pm
by Silvio.Falconi
thanks

Re: create a function with Tdatabase

Posted: Wed Apr 28, 2021 5:57 pm
by James Bott
Sivio,

Maybe you didn't know that CDXs have always had the option to open all the indexes automatically when the DBF is opened. And as I understand it, NTXs can now do that also.

REQUEST DBFCDX
rddsetdefault( "DBFCDX" )
SET EXCLUSIVE OFF
SET(_SET_AUTOPEN, .T. )

Re: create a function with Tdatabase

Posted: Thu Apr 29, 2021 8:01 am
by Silvio.Falconi
James Bott wrote:Sivio,

Maybe you didn't know that CDXs have always had the option to open all the indexes automatically when the DBF is opened. And as I understand it, NTXs can now do that also.

REQUEST DBFCDX
rddsetdefault( "DBFCDX" )
SET EXCLUSIVE OFF
SET(_SET_AUTOPEN, .T. )


yes I knew it but it happens that in my application there is a function that does not have to load the indexes or in one of the functions that I have seen that does not load them all
As you well know in my old applications made for my school I had problems because sometimes the indexes were missing,
so not checking them first when opening the application seems to me a stretch because I am always afraid of not finding them in that folder

Re: create a function with Tdatabase

Posted: Thu Apr 29, 2021 7:50 pm
by James Bott
yes I knew it but it happens that in my application there is a function that does not have to load the indexes or in one of the functions that I have seen that does not load them all


Hmm, I always open the indexes whether they are used or not. This simplifies coding doesn't have a downside that I can think of.

If you are using database objects, in order to not open the indexes sometimes, it would mean you would have to pass a parameter and when looking at the code you could never tell if the indexes were open or not.

Is there a reason you need them to NOT have the indexes open?