create a function with Tdatabase

create a function with Tdatabase

Postby Silvio.Falconi » Sat Apr 24, 2021 9:29 am

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 view

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 view
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?
Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)
I use : FiveWin for Harbour November 2023 - January 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: 6834
Joined: Thu Oct 18, 2012 7:17 pm

Re: create a function with Tdatabase

Postby Antonio Linares » Sat Apr 24, 2021 11:19 am

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
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 41366
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: create a function with Tdatabase

Postby nageswaragunupudi » Sat Apr 24, 2021 11:49 am

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


This syntax is wrong. The DBF is already open.

Correct:
Code: Select all  Expand view
oDbf := TDatabase():New( SELECT( cAlias ) )
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
 
Posts: 10295
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: create a function with Tdatabase

Postby nageswaragunupudi » Sat Apr 24, 2021 11:54 am

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

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
 
Posts: 10295
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: create a function with Tdatabase

Postby Silvio.Falconi » Sun Apr 25, 2021 4:35 pm

thanks
Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)
I use : FiveWin for Harbour November 2023 - January 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: 6834
Joined: Thu Oct 18, 2012 7:17 pm

Re: create a function with Tdatabase

Postby James Bott » Wed Apr 28, 2021 5:57 pm

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. )
FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Re: create a function with Tdatabase

Postby Silvio.Falconi » Thu Apr 29, 2021 8:01 am

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
Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)
I use : FiveWin for Harbour November 2023 - January 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: 6834
Joined: Thu Oct 18, 2012 7:17 pm

Re: create a function with Tdatabase

Postby James Bott » Thu Apr 29, 2021 7:50 pm

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?
FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: Google [Bot] and 44 guests