TDatabase Class

Re: TDatabase Class

Postby nageswaragunupudi » Thu May 20, 2021 4:39 am

1) I want to create a Database as oDbf and pass oDbf much like we do with Ado .. oRs


Sample code:
Code: Select all  Expand view

function CreateTable()

   local aStruct  := { { "ID", "+", 4, 0 }, { "CODE", "C", 2, 0 }, { "NAME", "C", 25, 0 } }
   local aData    := { { "WA", "Washington" }, { "NY", "New York" }, { "NE", "Nebraska" } }
   local oDbf

   oDbf  := TDatabase():Create( "NAMES.DBF", aStruct, "DBFCDX", "*" ) // "*" means index all fields
   oDbf:ArrayToDBF( aData, "CODE,NAME" ) // Add initial data
   oDbf:GoTop()

   ? oDbf:ID, oDbf:Code, oDbf:Name

   XBROWSER oDbf TITLE "NAMES.DBF : CREATED"

   oDbf:Close()

return nil
 


Opening DBF with TDatabase

The following code works. Kept for backward compatibility. We do not recommend this approach
Code: Select all  Expand view

USE NAMES
// or SELECT nArea
DATABASE oDbf
// works but not recommended
 


Recommended:
Code: Select all  Expand view

function OpenTable()

   local oDbf, lShared := .t.

   oDbf  := TDatabase():Open( nil, "NAMES", "DBFCDX", lShared )

   ? oDbf:ID, oDbf:Code, oDbf:Name
   XBROWSER oDbf TITLE "NAMES.DBF : OPENED" AUTOSORT

   oDbf:Close()

return nil
 


When we want to create a new table and use:
Recommended:
Code: Select all  Expand view

oDbf := TDatabase():Create( ... ) // as in above sample
oDbf:Close()
oDbf := TDatabase():Open(...) // as in above sample
 



2) How do I set a filter ? .. are the oDbf:Filters as fast as with ADO ? oDbf:Filter := "[CustomerName] like '"+cLname1+"%'" ?


Please use DBF syntax for filter expressions, not SQL syntax.
Eg:
"NAME LIKE 'X%'" does not work
"NAME = 'X'" works

ADO:
oRs:Filter := "[CustomerName] like '"+cLname1+"%'"
TDatabase:
oDbf:SetFilter( "NAME = '" + cLName1 + "'")
oDbf:GoTop()
OR
oDbf:SetFilter( "NAME = ?", { cLName1 } )
oDbf:GoTop()

TDataBase Filter methods:
Code: Select all  Expand view

oDbf:SetFilter() --> present dbfilter
oDbf:SetFilter( "" ) // Clear filter
oDbf:clear filter    // Clear filter
oDbf:SetFilter( cFilter ) // new filter
oDbf:SetFilter( cFilter, aParams ) // parameters for ? place holders
oDbf:setFilter( cFilter, bParams ) // Eval( bParams ) -> aParams
// In case of parametrised filters we can later use
oDbf:ReFilter( aNewParams )
 


Setting Scopes:

Code: Select all  Expand view

oDbf:OrdScope( 0, uTopValue )
oDbf:OrdScope( 1, uBottomValue )
oDbf:GoTop()
 

if the value is NIL, clears the scope.

3) How to I extract a oDBf field Value ? oDBf:Fields("Whatever"):Value ??


Accessing:
Code: Select all  Expand view

? oDbf:<fieldName1>, oDbf:<fieldName2>, ...
 

Assign new values and saving:
Code: Select all  Expand view

oDbf:<fieldName1> := uNewValue1
oDbf:<fieldName2> := uNewValue2
oDbf:Save()  // similar to oRs:Update()
 


Modifying data and saving changes:
1)
Code: Select all  Expand view

oDbf:Code := "WA"
oDbf:Name := "Washington"
oDbf:Save()
 


2)
Code: Select all  Expand view

oDbf:Update( { "CODE", "NAME" }, { "WA", "Washington" } )
//or
oDbf:Update( "CODE,NAME", { "WA", "Washington" } )
 


Appending New Data:
1)
Code: Select all  Expand view

oDbf:Append()  // same as oRs:AddNew()
oDbf:<fieldName1> := uNewValue1
oDbf:<fieldName2> := uNewValue2
oDbf:Save()  // similar to oRs:Update()
 


2)
Code: Select all  Expand view

oDbf:Append( { "CODE", "NAME" }, { "NW", "Newark" } ) // like ADO AddNew( flds, values )
 



4) How to I create and use index's .. Like .cdx ( tags )


Approach-1:
oDbf:CreateIndex( [cFile], cTag, cKey, [lUnique], [lDescend], [lMemory] )

Approach-2 (Easiest):

oDbf:CdxCreate() /// creates index on all fields
oDbf:CdxCreaate( cFieldList, [lMemory] )
Eg:
oDbf:CdxCreate( "ID,CODE,NAME" )
Character field expressions are automatically enclosed in UPPER()

Setting order to index tag:
Code: Select all  Expand view

oDbf:SetOrder( cnIndexOrder, [cFile] )
 


5) Reference oDbf in xBrowse() ?


XBROWSER oDbf [COLUMNS "ID", "CODE", "NAME" ] ....

@ row,col XBROWSE oBrw SIZE w,h PIXEL OF oDlg ;
DATASOURCE oDbf ;
COLUMNS "ID", "CODE", "NAME" ;
..........

For quick editable browse:

Code: Select all  Expand view

oDbf:Browse( [cTitle], [acFields] )
 


For Quick Edit Dialog
Code: Select all  Expand view

oDbf:Edit( [cFieldList], [lNew] )
 
Regards

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

Re: TDatabase Class

Postby TimStone » Thu May 20, 2021 6:15 pm

Although I have been using database classes for several years now, I did want to comment this overview is excellent.

I'm sure it will be helpful to many who have not yet moved to full OOPS.
Tim Stone
http://www.MasterLinkSoftware.com
http://www.autoshopwriter.com
timstone@masterlinksoftware.com
Using: FWH 23.10 with Harbour 3.2.0 / Microsoft Visual Studio Community 2022-24 32/64 bit
User avatar
TimStone
 
Posts: 2944
Joined: Fri Oct 07, 2005 1:45 pm
Location: Trabuco Canyon, CA USA

Re: TDatabase Class

Postby nageswaragunupudi » Fri May 21, 2021 4:10 am

Mr. Rick

I guess this is what you are looking for.
viewtopic.php?f=3&t=40438
Regards

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

Re: TDatabase Class

Postby Silvio.Falconi » Sat May 22, 2021 10:27 am

Nages,
Dbftoarray function with Tdatabase how run ?
wich parameters ?
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: 7061
Joined: Thu Oct 18, 2012 7:17 pm

Previous

Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 93 guests