Query Manual
Query Manual
Documentation
Table of Contents
Chapter I Introduction 1
1 Overview
................................................................................................................. 2
2 Copyright
.................................................................................................................
Notice 2
3 What's .................................................................................................................
New 3
4 Rebuilding
.................................................................................................................
Packages 5
5 Installing
.................................................................................................................
Database Adapters 5
6 Getting.................................................................................................................
Support 6
7 Breaking
.................................................................................................................
Changes 6
Version
..........................................................................................................................................................
1.9 - Package Restructuration 6
I
Introduction
TMS Query Studio Documentation
1 Introduction
1.1 Overview
Query Studio provides an easy way to give users access to powerful queries
without requiring any knowledge about SQL. Users can setup complex queries in
an almost natural language way with Query Studio. Dropping the component
VisualQuery on the form and connect to the database opens the visual query
power of Query Studio.
Feature details:
For use in commercial applications, you must purchase a single license or a site
license of TMS Query Studio. A site license allows an unlimited number of
developers within the company holding the license to use the components for
commercial application development and to obtain free updates for a full
version cycle and priority email support. A single developer license allows ONE
developer within a company to use the components for commercial application
development, to obtain free updates and priority email support. A single
developer license is NOT transferable to another developer within the company
or to a developer from another company. Both licenses allow royalty free use of
the components when used in binary compiled applications.
The component cannot be distributed in any other way except through free
accessible Internet Web pages or ftp servers. The component can only be
distributed on CD-ROM or other media with written autorization of the author.
version 1.3
New : ssFirebird syntax
New : [Link] and [Link] properties
to allow syntax like "Order by 3, 5"
New : SyntaxConf property which can be used to configure the syntax of
SQL statement to be generated when SQLSyntax is set to ssCustom
New : SQL statement can also be retrieve from a TClientDataset when it is
set to the TargetDataset of TatVisualQuery component
New : OnRetrieveTablenameListEvent and OnRetrieveFieldnameListEvent
events available for all TatDatabase descendents
New : Fields in popup menu are now being displayed in alphabetical order
New : Events OnItemDeleting, OnItemDeleted, OnTreeViewParamChanged
New : Delphi 2006,2007 & C++Builder 2006,2007 support
New : CoInsertCustom option in TClauseOptions. Use this to turn allow/
prohibit end-user to use the custom dialogs for building the query
version 1.2
New : SQLDirect components support
New : SQL Parser now supports field and table names with spaces (e.g.,
Select "Customer Name" from "Customer Table")
New : Property [Link]
New : Methods LoadQueriesFromStream and SaveQueriesToStream
New : IBO components support
Improved : Query Studio package does not longer require TMS Pack package
when TMS Pack is installed
Improved : Generated SQL statement does not duplicate order by fields, even
if end-user duplicated it in visual query
Fixed : Disappearing check boxes after dragging fields
Fixed : Bug with "#sm#" string in table names
Fixed : "Could not parse SQL" with more than one space after "AND" and "OR"
operators
version 1.0
First release
AnyDAC
For installing TatAnyDACDatabase in your environment, add {$QS}\source
\drivers\anydac directory to the Delphi library path, where {$QS} is the root
directory of Query Studio files, and include [Link] unit in Query
Studio package.
DBISAM
For installing TatDBISAMDatabase in your environment, add {$QS}\source
\drivers\dbisam directory to the Delphi library path, where {$QS} is the root
directory of Query Studio files, and include [Link] unit in Query
Studio package.
IBO
For installing TatIBODatabase in your environment, add {$QS}\source\drivers
\ibo directory to the Delphi library path, where {$QS} is the root directory of
Query Studio files, and include [Link] unit in Query Studio package.
IBX
For installing TatIBXDatabase in your environment, add {$QS}\source\drivers
\ibx directory to the Delphi library path, where {$QS} is the root directory of
Query Studio files, and include [Link] unit in Query Studio package.
NexusDB
For installing TatNexusDatabase in your environment, add {$QS}\source\drivers
\nexus directory to the Delphi library path, where {$QS} is the root directory
of Query Studio files, and include [Link] unit in Query Studio package.
SQLDirect
For installing TatSQLDirDatabase in your environment, add {$QS}\source
\drivers\sqldir directory to the Delphi library path, where {$QS} is the root
directory of Query Studio files, and include [Link] unit in Query Studio
package.
Getting support
For general information: info@[Link]
Fax: +32-56-359696
For all questions, comments, problems and feature request for VCL
components: help@[Link].
To improve efficiency and speed of help, refer to the version of Delphi, C+
+Builder, Visual Studio .NET you are using as well as the version of the
component. In case of problems, always try to use the latest version available
first.
Version 1.9
There was a big package restructuration in version 1.9. More info in the
dedicated topic.
Previous versions:
Version Package File Name Bpl File Name Dcp File Name
Delphi 7 [Link] [Link] [Link]
Delphi 2007 [Link] [Link] [Link]
Delphi 2009 [Link] [Link] [Link]
Delphi 2010 [Link] [Link] [Link]
Delphi XE [Link] [Link] [Link]
Delphi XE2 [Link] [Link] [Link]
Delphi XE3 [Link] [Link] [Link]
Delphi XE4 [Link] [Link] [Link]
Delphi XE5 [Link] [Link] [Link]
Delphi XE6 [Link] [Link] [Link]
Delphi XE7 [Link] [Link] [Link]
Dcp files are generated with same name, and only bpl files are generated with
the suffix indicating the Delphi version. The suffix, however, is the same used
by the IDE packages (numeric one indicating IDE version: 160, 170, etc.). The
new package structure is as following (note that when 6.5 was released, latest
Delphi version was XE7. Packages for newer versions will follow the same
structure):
Version Package File Name Bpl File Name Dcp File Name
Delphi 7 [Link] [Link] [Link]
dclTMSQueryStudio. dclTMSQueryStudio70.b dclTMSQueryStudio.d
dpk pl cp
II
Getting Started
TMS Query Studio Documentation
2 Getting Started
TatVisualQuery interface is tree-view like, where the first top node contains
the name of the query being built. Each node below this one is related to an
SQL part:
- Source tables: allows user to choose which tables records will come from,
and how the tables will be linked
- Data fields: allows choosing of fields that will be included in Select clause of
SQL
- Filter conditions: allows filtering of records (Where clause)
- Grouping fields: specificies grouping for records (which fields will be used for
grouping)
- Ordering fields: specifieds fields for ordering records
- Parameter editors: provides a higher-level interface where programmer or
end-users can define parameters to be linked to SQL (similar to %parameter
syntax in Tquery component). This way end-user can just change parameters
in order to change SQL.
For more information about TatVisualQuery properties and methods, see the
component reference at online help from IDE.
The fours steps above will make TatVisualQuery to work. You will be able to
construct queries, but will be better if you could USE the built queries. The
built SQL is available in MetaSQLDef property:
BuiltMetaSQL := [Link];
5. Drop the dataset component you want to use (for example, TQuery for BDE
engine, or TADOQuery for ADO engine)
6. Set [Link] component to point to the dataset
component
7. Set [Link] to True.
That's all you need to get it running. TatVisualQuery will automatically update
and reopen your TQuery as end-user changes its query definitions. If you
attach a DBGrid to the dataset, you will see query results.
Component events:
Use this event to provide the names of tables which can be selected from. Fill
the AList parameter with the table names.
Example:
[Link](Customer);
[Link](Orders);
Use this event to provide the names and types of fields which can be selected
in the table specified by ATableName parameter. Fill the AList parameter with
the field names. The field type must be typecasted to a TObject and inserted
in the Objects property of the list.
Example:
For more information about TatMetaSQLDef properties and methods, see the
component reference at online help from IDE.
2.2 Localization
TMS Query Studio provides an easy way to localize the strings. All strings used
in user interface (messages, button captions, dialog texts, menu captions,
etc.) are in a single file named [Link].
In the languages folder, included in Query Studio distribution, there are several
[Link] files available for different languages. Just pick the one you
want and copy it to the official directory of your query studio source code.
If the language you want does not exist, you can translate it yourself. Just
open [Link] file and translate the strings to the language you want.
As a final alternative, you can translate the [Link] file, also included
in qs_languages.zip file, and send the new file to us. The advantage of this
approach is that this file is easier to translate (you don't have to deal with
pascal language) and can be included in the official Query Studio distribution.
This way we keep track of changes in translable strings and all new strings are
marked in the upcoming releases. This way, you will always know what is
missing to translate, and do not need to do some kind of file comparison in
every release of Query Studio.
Option 1
- Pick the correct [Link] file from the qs_languages.zip file, according
to the language you want.
- Replace the official [Link] (in source code directory) by the one
you picked.
Option 2
- Translate the official [Link] directly
Option 3
- Translate the [Link] file and send it to us (support@[Link]).
- We will send you back a translated [Link] file and this translation
will be included in official release.
III
Using parameters
TMS Query Studio Documentation
3 Using parameters
One interesting feature in TatVisualQuery component is the usage of
parameters. While building filtering conditions in TatVisualQuery, end-user
follows the known procedures: add a condition, choose a field which will be
compared to a value, choose operator (equal to, less than, etc.), and choose
the value.
Whenever a new parameter name is typed in filter condition, user can then
create the editor that will be used to edit the parameter value. This is done in
the "Parameter editors" section of TatVisualQuery component. Definitions made
in parameter editors are saved in the ParamDefs property of a TatMetaSQLDef
class. The ParamDefs property is a TatParamDefs class, which holds a
collection of TatParamDef classes. Each TatParamDef class contains
specifications of a single parameter editor. Below is the reference of
TatParamDef class.
ptFreeEdit: A simple edit component will be used to edit the parameter. User
must type the parameter value.
ptDistinctList: A combo box will be used to edit the parameter. User can type
the parameter value or choose one option from combo items. The items in
combo will be automatically filled with content of
database. For example, if the parameter is "Customer city", and the field
"CustomerCity" will be compared to this parameter, the combo items will be
filled with all possible city names. A distinct query is made to the field in
database to get all distinct values for the field.
ptQueryList: A combo box will be used to edit the parameter. The items of
the combo will be the query result of the SQL statement specified in
MetaSQLStr property. The different between ptDistinctList and ptQueryList is
that the query in distinct list is automatically built by TatVisualQuery, while
with ptQueryList it's the user that specified the SQL statement.
ptChooseList: A combo box will be displayed for editing the parameter. Each
item in combo box is related to an item in ListItems property. Once the user
chooses the combo box item, the related ListItem is applied. See ListItems
property for more details.
For more information about TatParamDef properties and methods, see the
component reference at online help from IDE.
Filter conditions are set to filter orders given by its ItemsTotal field, which
value must be bettwen LowValue parameter and HighValue parameter. In
Parameter editors section, a single parameter editor is created, of type
dropdown choose-list (which is ptChooseList type). Three items were created
for this parameter editor: Low-valued orders, Medium-valued orders and
High-valued orders. Each of item correspond to a TatParamListItem object.
For each list item, two parameter values were defined. For example, for the
item name Mediu-valued orders, the parameter LowValue receives 20000,
and the parameter HighValue receives 60000.
After all is set, end-user will see a combo with a caption Filter orders by
value..., and the combo has three options: Low-valued orders, Medium-
valued orders and High-valued orders. User just choose an item, and the
correct orders are displayed in grid. This is the mechanism: lets say end-user
chooses Medium-valued orders item. When this happen, the parameter values
defined in Medium-valued orders item is set: LowValue parameter receives
20000, and HighValue parameter receives 60000. Since these two parameters
are being used in Filter conditions section to filter records by ItemsTotal field,
the query will bring all orders which ItemsTotal field value is between 20000
and 60000.
Finally, the TatParamListItem object reflects the parameter editor item above.
Below is reference for TatParamListItem class, with examples related to the
screenshot above.
IV
TatMetaSQL
component
TMS Query Studio Documentation
4 TatMetaSQL component
TatMetaSQL is a component that encapsulates an SQL statement, keeping in
its properties all info needed to build the SQL statement, like fields to be
selected, tables involved, order fields, and so on. The primary use for
TatMetaSQL component is to allow building of SQL statements in an easy way.
Here are some advantages of using it:
For complete information about TatMetaSQL methods and properties, see the
Query Studio component reference at online help from IDE.
with [Link] do
begin
TableName:='Customer';
TableAlias:='C';
end;
with [Link] do
begin
FieldName:='CustNo';
TableAlias:='C';
end;
[Link]:=[Link];
Looks like it is more complicated, but suppose that now you want to change
SQL in order to select both CustNo and Company fields. In the classic way, you
would just change SQL text string. With MetaSQL, you do this:
with [Link] do
begin
FieldName:='Company';
TableAlias:='C';
end;
Now imagine that you want to allow your end-user to do it. And not only add
fields to select, but changing order, filtering, and so on. It will be very
complicated to change SQL string, specially if the SQL is complex. TatMetaSQL
makes it simpler.
[Link]:=ssBDELocal;
Now, first step to build an SQL statement is to define tables to query. This is
done using [Link] property:
with [Link] do
begin
TableName:='Customer';
TableAlias:='C';
end;
SELECT
*
FROM
Customer C
All references to a table in TatMetaSQL is done using the table alias (specified
in TableAlias property). So, you must not add two tables with same TableAlias.
Now if we add one more table:
with [Link] do
begin
TableName:='Orders';
TableAlias:='O';
end;
TatMetaSQL will be aware that you will query two tables. But you must then
indicate how these two tables are linked (see Defining table links).
The code below add four fields in TatMetaSQL (CustNo and Company, from
customer table, and CustNo and OrderNo, from orders table):
with [Link] do
begin
DataType:=ftFloat;
FieldName:='CustNo';
TableAlias:='C';
FieldAlias:='Customer_CustNo';
end;
with [Link] do
begin
DataType:=ftString;
FieldName:='Company';
TableAlias:='C';
FieldAlias:='Company';
end;
with [Link] do
begin
DataType:=ftFloat;
FieldName:='CustNo';
TableAlias:='O';
FieldAlias:='Orders_CustNo';
end;
with [Link] do
begin
DataType:=ftFloat;
FieldName:='OrderNo';
TableAlias:='O';
FieldAlias:='OrderNo';
end;
Here, TableAlias and FieldName property indicates the origin of field (from which
table it comes, and the name of the field in database). And, FieldAlias property
is the "ID" of the field in TatMetaSQL. Just like TableAlias for tables, FieldAlias
is used by other parts of TatMetaSQL to make a reference to a specified field
in SQLField property. Thus, you must not add to fields with same FieldAlias.
with [Link] do
begin
PrimaryTableAlias:='C';
ForeignTableAlias:='O';
LinkType:=altInnerJoin;
with [Link] do
begin
ConditionType:=ctFieldCompare;
FieldAlias:='Customer_CustNo';
Operator:='=';
Value:='Orders_CustNo';
end;
end;
SELECT
[Link] Customer_CustNo,
[Link] Company,
[Link] Orders_CustNo,
[Link] OrderNo
FROM
Customer C,
Orders O
WHERE
((
[Link] = [Link]
)
)
Now let's show again the use of TatMetaSQL to easily change SQL. Suppose
that we want to change the join for inner to outer (left join), in order to return
all customers, even those that don't have a related record in Orders table:
[Link]('C','O').LinkType:=altLeftJoin;
SELECT
[Link] Customer_CustNo,
[Link] Company,
[Link] Orders_CustNo,
[Link] OrderNo
FROM
(Customer C LEFT JOIN Orders O
ON (((
[Link] = [Link]
)
)))
with [Link] do
begin
FieldAlias:='Customer_CustNo';
SortType:=ortDescending;
end;
SELECT
[Link] Customer_CustNo,
[Link] Company,
[Link] Orders_CustNo,
[Link] OrderNo
FROM
(Customer C LEFT JOIN Orders O
ON (((
[Link] = [Link]
)
)))
ORDER BY
[Link] DESC
Note that, one more time, field alias is used to tell meta sql which field the SQL
should be ordered by. SortType property is used to indicate a descending
order.
with [Link] do
begin
ConditionType:=ctValueCompare;
FieldAlias:='Customer_CustNo';
Operator:='<';
Value:=1250;
end;
SELECT
[Link] Customer_CustNo,
[Link] Company,
[Link] Orders_CustNo,
[Link] OrderNo
FROM
(Customer C LEFT JOIN Orders O
ON (((
[Link] = [Link]
)
)))
WHERE
(
[Link] < 1250
)
ORDER BY
[Link] DESC
[Link]:=ssOracle;
SELECT
[Link] Customer_CustNo,
[Link] Company,
[Link] Orders_CustNo,
[Link] OrderNo
FROM
Customer C,
Orders O
WHERE
((
[Link] = [Link](*)
)
)
AND
(
[Link] < 1250
)
ORDER BY
[Link] DESC
an easy way. The following steps show how to use visual editor to build the
same SQL built in previous chapter by code.
Checking option Save last used database will keep the selected DB alias next
time you open visual editor.
Note that here you can check/uncheck fields. This is because fields have an
Active property. This checking/unchecking task will set Active property to true
or false. To more info about Active property and the other field properties you
can set here in visual editor, see the TatMetaSQL reference later in this
document.
The Fields tab has also two extra buttons: Append from dataset and Append
from table.
Append from dataset button: Clicking this button will bring a list of TDataset
descendant components (TTable, TQuery, etc.) that exists in the same form
that own TatMetaSQL component. After choosing desired dataset
component, the visual editor will create one new field in metasql for each
persistent field in dataset, copying properties like FieldName, DisplayLabel and
DataType.
Append from table button: Only works if there is a database alias selected.
Brings a list of existing tables in database. After choosing table, visual editor
will create one new field in metasql for each existing field in database.
In main windows of visual editor, you only add the join and set tables to join
and link type. You must then add join conditions, use Edit subconditions button
or double clicking the join. This will bring another window to edit join
coniditions. The figure above shows both windows.
Each join must have a least one condition of type ctFieldCompare. In that
condition you must define a comparison between fields of both tables being
linked. Then you can add more conditions for the join, if you need to.
Here the visual editor shows a different layout: there is a tree view at the left
of condition list. This is because conditions are recursive: each condition has a
property called SubConditions, which holds a collection of conditions. So, when
a condition has subconditions, and its ConditionType property is set to
ctSubConditions, than the condition is represented by its SubConditions, inside
parentesis. This is useful to build nested conditions and group then with OR or
AND logical operators.
In visual editor, the tree view at the left is used to see conditions structure.
The root node Main conditions is not a condition itself, but represents the
conditions that are directly create inside the [Link] property.
The (AND) after the name indicate how conditions inside that node will be
grouped (in this case, with and operator).
When you select a node in treeview, the condition list at the right will display
the conditions of that node. In the example of Figure 6, the Main conditions
node is selected, so all its conditions is displayed at the right (in this case, only
condition Condition0). To add a new condition, you must select node first, and
then click New button. This will create a new subcondition of selected
condition in tree view.
Conditions also have Active property, and because of that there is a check box
at the left of each condition. You can the activate or deactivate condition by
checking/unchecking it. Non active conditions will not be included in SQL.
Order fields also have Active property. In visual editor, you use check box at
the left of each order field to activate/deactivate orders. Only active order
fields will be included in SQL.
In addition to SQL statement preview, you can test the SQL in database, if you
have defined a database alias. Tab Data result will execute the query in
database and display a result dataset in a grid form. The figure below shows
the data result for our SQL example.
TatMetaSQL class
SQLTables Select * from Customer C, Order O, Parts P where [Link]=2
SQLFields Select [Link] CustomerNumber, [Link] Company,
TatSQLField class
FieldAlias Select [Link] CustomerNumber from Customer C
FieldName Select [Link] CustomerNumber from Customer C
TableAlias Select [Link] CustomerNumber from Customer C
GroupFunction Select Count([Link]) from Customer C
FieldExpression Select [Link] * [Link] from Orders O
TatSQLOrderField class
FieldAlias Select * from Orders O Order By [Link] Desc
SortType Select * from Orders O Order By [Link] Desc
TatSQLGroupField class
FieldAlias Select [Link], SUM([Link]) from Orders O Group By
[Link]
TatSQLTable class
TableAlias Select * from Customer C
TableName Select * from Customer C
TatSQLCondition class
FieldAlias Select * from Customer C where [Link] Like A*
Operator Select * from Customer C where [Link] Like A*
Value Select * from Customer C where [Link] Like A*
Expression Select * from Orders O where [Link] * [Link] > 2000
SubConditionsLogicalOper Select * from Customer C where [Link]=10 OR
([Link]
Like A* AND [Link] IS NULL) OR [Link]=3
SubConditions Select * from Customer C where [Link]=10 OR ([Link]
Like A* AND [Link] IS NULL) OR [Link]=3
TatSQLJoin class
JoinConditionsLogicalOper Select * from Customer C Inner Join Orders O ON
([Link]=[Link] AND ([Link]=2 OR [Link]=3))
JoinConditions Select * from Customer C Inner Join Orders O ON
([Link]=[Link] AND ([Link]=2 OR [Link]=3))
PrimaryTableAlias Select * from Customer C Inner Join Orders O ON
([Link]=[Link] AND ([Link]=2 OR [Link]=3))
ForeignTableAlias Select * from Customer C Inner Join Orders O ON
([Link]=[Link] AND ([Link]=2 OR [Link]=3))
LinkType Select * from Customer C Inner Join Orders O ON
TMS Query Studio handles localization through a single file named qsLanguage.pas, which contains all interface strings. Users can localize these strings by replacing this file with versions from different languages available in the qs_languages.zip file. Alternatively, users can directly translate the file or the qsLanguage.txt file and send it to TMS Software for inclusion in official releases, ensuring that all translatable strings are updated consistently .
TatMetaSQL's adaptable SQL Syntax is key for ensuring compatibility across various database systems, such as Oracle, by allowing a single line configuration change, e.g., MetaSQL.SQLSyntax:=ssOracle. This adaptability is crucial because different databases can have variations in SQL syntax, and altering the syntax at runtime ensures the generated SQL is appropriate for the target database environment .
To change the join type from inner join to left join in TatMetaSQL, you would use the FindLink method and set the LinkType to altLeftJoin. For example: MetaSQL.TableJoins.FindLink('C','O').LinkType:=altLeftJoin. The link type altLeftJoin specifies that the join should include all records from the left table (e.g., 'Customer') even if there are no matching records in the right table (e.g., 'Orders').
TatMetaSQL improves SQL query customization by providing a structured environment for SQL statement configuration through object properties and methods, rather than writing raw SQL. This allows for dynamic query modifications, such as changing joins, order fields, and conditions, with minimal effort and high accuracy. It abstracts the complexity of SQL syntax variations and provides tools to manage complex queries systematically, resulting in higher productivity and fewer errors .
Parameter definitions in TatVisualQuery allow for dynamic query construction by using parameters instead of static values. This capability enhances query flexibility as users can change parameter values without altering the entire query. Available parameter types include ptFreeEdit, where users manually enter parameter values, and ptDistinctList, which presents users with options pulled from the database to select from, enhancing ease of use and reducing errors .
The qsLanguage.txt file serves as an alternative way to translate interface strings without interacting directly with Pascal code. This file simplifies the translation process and allows users to submit new translations to TMS Software. The translated texts are then used to create a corresponding qsLanguage.pas file, which becomes part of the official release, ensuring broader support for different languages and keeping translations up-to-date .
MetaSQL conditions can be combined to form complex logical expressions using the SubConditionsLogicalOper property. This property allows the specification of logical operators (such as AND, OR) between subconditions, facilitating the construction of sophisticated query filters without needing intricate SQL knowledge. By using SubConditionsLogicalOper, developers can easily build nested conditions, enhancing the depth and functionality of query filtering .
Conditions in TatMetaSQL are defined using the Conditions property, which allows the specification of filtering criteria for query results. This involves setting properties like ConditionType, FieldAlias, Operator, and Value. For instance, if you want to filter records to only include customers with a number less than 1250, you would add a condition such as: ConditionType:=ctValueCompare; FieldAlias:='Customer_CustNo'; Operator:='<'; Value:=1250. This creates a WHERE clause in the SQL statement to filter the results accordingly .
At design-time, TMS Query Studio suggests using the standard Delphi object inspector or the TatMetaSQL visual editor for SQL query construction. Users can manipulate properties such as SQLFields, OrderFields, and SQLTables using these tools to efficiently define the structure of the SQL statement. The visual editor provides an interface for adding and editing fields, tables, and joins, streamlining the design process .
TatCustomDatabase provides a way to handle SQL query construction in environments where there is no direct database connection. It allows the simulation of database connectivity, enabling the end-user to build SQL statements by providing available tables and fields through events such as OnRetrieveTableNameListEvent and OnRetrieveFieldNameListEvent .