AnyDAC
ContentsIndexHome
PreviousUpNext
Browsing Table

AnyDAC offers TADTable component to browse database table data, filter and sort records, and edit the table data.

Group
Links
Using TADTable

To browse a single database table, sort and filter records, and edit the data the AnyDAC offers TADTable component.The TADTable transparently generates a SELECT statement basing on the TADTable property values and called methods and send it to a DBMS. 

The TADTable has two main operation modes:

  • Live Data Window mode. Allows bi-directional navigation through large data volumes with a minimal memory usage.
  • Standard mode. This mode is similar to TADQuery. TADTable generates a single SELECT command and uses the result set to walk through table records.

To open a table, the TableName property must be specified. Additionally may be set IndexFieldNames or IndexName properties. Note, to use IndexName property, the fiMeta must be included into FetchOptions.Items. For example: 

 

ADTable1.TableName := 'CUSTOMERS';
ADTable1.IndexFieldNames := 'CustNo';
ADTable1.Open;

 

Live Data Window mode

In Live Data Window (LDV) mode AnyDAC queries and keeps in memory only 2 * FetchOptions.RowsetSize of records - a window into the table data. When application navigates through the table data, AnyDAC automatically scrolls or positions the LDW to the required position. That gives benefits:

  • minimizes memory usage and allows to work with large data volumes, similar to an unidirectional dataset;
  • enables bi-directional navigation, in contrast to an unidirectional dataset;
  • gives always fresh data, reducing the need to refresh dataset;
  • does not give a delay to fetch all result set data, required to perform sorting, record location, jumping to last record, etc.

The Filter property, range filtering, IndexFieldNames and IndexName properties, Locate and Lookup methods, key locating, setting RecNo, setting a bookmark, etc are performed by additional SELECT commands or by setting additional phrases for the main SELECT command. 

In LDV mode the Filter property value is substituted as is into WHERE phrase. To make expression compatible with the DBMS and the local expression engine, application may use AnyDAC escape sequences. For example: 

 

ADTable1.Filter := 'DateField = {d ' + FormatDateTime('yyy-mm-dd', Trunc(MonthCalendar1.Date)) + '}';
ADTable1.Filtered := True;

 

LDV by design always applies ORDER BY phrase to the SELECT commands. The key requirement for correct LDV work are:

  • a table must have unique or primary key. See Unique Identifying Fields for more details.
  • the server side sort collation and client side sort collation must be the same. Otherwise TADTable may produce duplicated rows and raise "unique key violation" error.

Although AnyDAC minimizes the number of generated and executed SQL commands in LDV mode, still it produces more heavy DB load than TADQuery. So, application developers should carefully choose when to use TADTable and LDV. 

 

Setting LDV

The LDV is used when all the following conditions are met, otherwise the standard mode is used:

  • CachedUpdates = False (default value);
  • FetchOptions.Unidirectional = False (default value);
  • FetchOptions.CursorKind in [ckAutomatic, ckDynamic] (ckAutomatic is default value);
  • table has primary or unique key.

The mode may be changed only when TADTable is inactive. Consequently, for example, the CachedUpdates may be changed only when table is inactive. Additionally, the performance may be improved by setting FetchOptions.LiveWindowMode to wmApproximate (default value). 

 

Avoiding "unique key violation" error in LDV

To make the database sort order the same as the client side sort order, the FormatOptions.SortLocale and SortOptions may be used to adjust the client side sort order. For example, German speaking developers may setup TADTable to query the Firebird database with ISO8859_1 character set containing German language string data: 

 

uses
  Windows;
...
// Set locale ID to German phone book collation
ADTable1.FormatOptions.SortLocale := MAKELCID(MAKELANGID (LANG_GERMAN, SUBLANG_GERMAN), SORT_DEFAULT);
// Use the the punctuation and other symbols insensitive sorting
ADTable1.FormatOptions.SortOptions := [soNoSymbols];
ADTable1.IndexFieldNames := 'NAME';
ADTable1.TableName := 'CUSTOMERS';
ADTable1.Open;

 

Depending on a DBMS the following additional settings may be made:

DBMS 
Settings 
MySQL 
May be required to include soNoSymbols into SortOptions. 
Oracle 
May be required to execute ALTER SESSION SET NLS_COMP=ANSI. 
SQLite 
Set SortLocale to 0. 

 

For example, Oracle developers may execute the command: 

 

ADConnection1.Connected := True;
ADConnection1.ExecSQL('ALTER SESSION SET NLS_COMP=ANSI');

 

Also FormatOptions.StrsEmpty2Null and StrsTrim we recommend to set to False. 

Note, when a developer fails to adjust the client side sort collation, then the LDV may be turned off by setting FetchOptions.CursorKind to ckDefault, ckStatic or ckForwardOnly.

What do you think about this topic? Send feedback!