Two-tier
Client Server
Delphi:
- BDE without middleware
(ie. no MIDAS/ENTERA/etc):
- standard 2 tier approach:
- all levels of Delphi
- TDatabase to point to remote database
- using TClientDataSet:
- Delphi 3 or higher
- ref: DI 1998 April p30-35 DI9804DM.zip
- TClientDataset gets data from a DBDataset
on same machine via its IProvider
interface:
- assign at runtime:
- Uses: BdeProv (else
runtime message "No
provider available")
- Form.create:
- ClientDataset1.Provider
:= Table1.provider;
- ClientDataset1.Data
:= Table1.provider.data;
- assign at design time:
- ClientDataSet command Assign
Local Data
- NB. DFM size will
grow as it stores all the
physical table data
assoc. with DBDataset if
it is made active
- trim this by
executing the Clear
Data command
- optionally can assign
provider at design time
if Delphi 3.02 or higher:
- assign a component
which has IProvider
interface (eg. TTable,
Query) to
TClientDataset.DataProvider
property.
- NB. this allows you
to add fields from the
table & create
calculated fields - these
must be done on
TClientDataSet as the
IProvider interface does
not pass calculated
fields from DBDataSet to
ClientDataSet!
- think of the
DBdataset component as a
separate application that
can't be manipulated by
you with code
- NB. leave RemoteServer properties
blank as this is only used in
3-tier!
- Advantages:
- dramatically reduce network
traffic in several instances:
- reading an entire table
- static lookup tables
- sorting a table
- TClientDataSet:
- stores all of its data in
memory & thus is very
fast
- you can control the
content of the data
packets retrieved in 3
ways:
- number of records
retrieved at one time via
the PacketRecords
property
- using Fields editor
to create persistent
field objects determines
which fields will be
retrieved (but must
include the primary key
field(s)).
- TProvider.Options
lets you control whether
BLObs & detail
records will be fetched
automatically or must be
specifically requested in
code
- has the ability to read
& write its contents
to local files via LoadFromFile
& SaveToFile:
- store metadata, data
& change log for that
table
- => you can
retrieve data from server
database, edit it, save
edits to local CDS file
& later, when you are
disconnected from the
database, you can load
the data from the CDS
file & still have the
ability to undo changes
using standard
ClientDataset methods
- => can save lookup
tables locally to save
continual network traffic
of rarely changed lookups
(see lookup.dpr in
DI9804DM.zip)
- AddIndex & DeleteIndex
methods allow you to
change whether index is
ascending or descending
& this sorting is
done on the data in
memory without having to
re-run a query to sort on
a different field=>
very fast, no network
traffic, can sort on
calculated fields!
- NB. to sort on
calculated fields, the
field must be specified
as being fkInternalCalc,
however, doing so means
its data will be stored
in memory too just like a
normal field rather than
having its values derived
only when needed, thus if
there is no need to sort
on the calculated field
leave its FieldKind
property as calculated
field.
- Delphi 4: can define aggregates
(eg. sum, min, max, avg,
count) via a property
inspector at design time
with ability to change:
- displayed name,
groupingLevel (number of
index fields to group
on), indexname
- refreshRecords
method avoids clearing
the change log &
losing unapplied changes
as would occur with the refresh
method, thus retrieving a
new set of records from
the server & merges
them with the changes in
the change log as though
the changes were
originally made to the
new records. If any
conflicts occur, it
triggers an OnReconcileError
event.
- is the official way to
handle cached updates:
- no longer have to
apply updates when you
move from a master record
as was the case in Delphi
2.
- can delay applying
updates via
ClientDataset.Provider.ApplyUpdates
- setting parameters for a
query or stored procedure
is easier in D4 than D3,
as it now has the Params
property
- allows creation, reading
& writing of
single-user flat files
without need for the BDE!
- readies your application for
transitioning to 3-tier model
- Disadvantages:
- need to deploy 2 additional
COM-based files:
- DBClient.dll &
STDVCL32.dll which need
to be copied to the
Windows System directory
& registered as for
OCX's (eg. with
Regsvr32.exe)
- delaying ApplyUpdates may cause inconsistencies when using Paradox
files but works well with SQL
back-ends such as Sybase SQL
Anywhere
- to ensure TDataset's events are
activated, you need to set
TProvider.ResolveToDataSet to
true although doing so will be
slower as updates must 1st be
processed by the dataset
component rather than applied
directly to the database using
SQL.