Guidelines to Use Zim Server Connectivity

There are two different moments when dealing with Zim Server Connectivity:

1) The preparation of client database: this step is done only once and creates data definitions to allow the client database to “see” what is defined in the other Zim database and it is obligatory;

2) The preparation of remote database: the other database must know the existence of some objects referenced by the client. This step is optional;

3) The interaction with another Zim database: this the real activity that connects, interacts and disconnects with the other database.

The Preparation of the Client Database

The client database needs to know what is the information that is going to be “seen” from the foreign database, that is, which entity-sets, relationships and documents are going to be visible by the client application. In other words, the client database must define these objects in its data dictionary (as it would define any other object) as they were defined in the foreign database with a certain degree of variation. For instance, a field originally defined in the foreign database does not need to be defined in the client database (for safety reasons or because this particular field is not needed) or even the format of the fields can be a little bit different like define a CHAR in the foreign database and define a VARCHAR in the client database, provided that the differences are not conflicting.

It is a good practice, though, to define all objects in the client database as they are defined elsewhere. Normally, definitions at the client database are obtained by exporting the definitions from the original database. In this way, all definitions in both sides will always be the same.

To export and import definitions from one database to another, use the Export Wizard and Import Wizard from DC.

There is another information needed to be provided to the client database. If objects are all defined the same way (that is, local objects or remote objects), a distinction has to be made so that the client session knows where the information has to be handled. This is told by the EntType, RelType and DocType fields in the definition of entity sets, relationships and documents respectively. Normally, these fields contain the value “ZIM” which tells the client session that the corresponding object is local. If these fields contain “zimserv”, then the client session will deal with these objects as being remote objects located in the other database.

There are two ways of changing the fields EntType, RelType or DocType:

1) Using DC:

when editing the corresponding object, there is a field called “Table Type” for Entity Sets and Relationships or a field called “Document Type” for Documents. Select “zimserv” in these fiedls. In this way, the Data Dictionary definition of object is changed and the object itself is recreated. Programs referencing this object must be recompiled;

2) Using the command SET TABLETYPE:

This command changes the internal state of the fields but does not change the Data Dictionary definition nor recreates the object. Existent compiled programs must be recompiled. Since this command is very practical but does not properly document the actions taken, it is only recommended when testing or briefly executing particular procedures. For additional information, refer to SET TABLETYPE command.

The Preparation of the Remote Database

This step is only required if the client requests for interaction specify objects only known by the client database. For example:

FIND Customers WHERE Code = 12345

The remote database can normally execute this command because the objects Customers, Code and 12345 are all known by the remote database (we are assuming that the client database defined Customers with the EntType equals to “zimserv”) because Customers is an entity set, Code is a field of this entity set and 12345 is a constant.

However, the command:

FIND Customers WHERE Code = MyForm

cannot be executed by the remote database because MyForm is not known there (we are assuming that MyForm is a form defined only in the client database). In this way, the object MyForm must be imported from the client database in order to be known in the remote database.

To export and import definitions from one database to another, use the Export Wizard and Import Wizard from DC.

The Interaction Between Databases

Once the client and remote databases have been prepared, the client application can connect to the remote database, retrieve and/or update data located there and then disconnect from the remote database.

In essence:

1) Establish the connection between the client and remote databases via the command CONNECT, passing parameters that identify which database is the remote database and where this database is located (some other parameters can be provided like the user and password);

2) Issue normal ZIM commands to process the information needed. Internally, the client session proceeds this way:

a) the command is parsed in the client which determines that some objects belong to a remote database (EntType, for instance, provides this information);

b) in this case, the command syntax is sent to the remote database where is parsed and executed. Along with the command, are also sent values needed for form fields, variables, parameter to procedures, etc.;

c) all variables, forms, form fields and sets that were sent to the server in the previous step are returned to the Zim application with any updated values. Commands that generate output, such as LIST or REPORT, return their output to the Zim application that writes it to the current output document as if the command had been executed locally. System variables such as $setcount are updated in the Zim application to indicate the result of executing the command on the server.

3) Destroy the connection whenever is no longer needed via the command DISCONNECT. This step is optional because the connection is automatically destroyed whenever the client session is terminated.

When the command is parsed (as in item 2a), a special situation might happen when a Zim command references a mixture of local and remote objects as in the following example:

FIND Customers Issue Orders

where Customers is a remote entity set and Orders is a local entity set. In this case, the client session cannot send the statement as it is because Orders does not have data in the remote database but rather the client sends a sequence of SQL statements to retrieve values from Customers and then relate them to Orders. Efficiency, in this case, can be very compromised.

The Zim Database Agent Functionality

In many respects, the Zim Database Agent operates like any other client session as it reads a database dictionary, reads the same configuration options file, parses and executes commands, etc. There are some differences though:

. It is automatically spawned by Zim Server when a CONNECT statement is executed by the client session and cannot be started manually by a user;

. It is automatically killed by the DISCONNECT statement executed by the client session or upon the termination of the client session;

. Because it always runs in background, it does not process any user interface commands. Commands like FORM DISPLAY are ignored;

. Upon its initialization, it executes the SERVPROF file instead of the ZIMPROF file;

. Upon its termination, it executes the SERVBYE file instead of the ZIMBYE file;

Database Object Definitions

Certain rules apply to the definition of objects that are common to the client and the remote database:

  • If an entity set or relationship in an application belongs to a Zim directory other than ZIM, then it must belong to a directory with the same name on the remote database or make use of the Remote Naming Feature (below). Also, the corresponding Zim directory must be accessed when the Zim Database Agent starts (see SERVPROF);
  • A document, an entity set or relationships must have the same name on the remote database or make use of the Remote Naming Feature (below);
  • At the remote database, you can define entity sets, documents and relationships that have no corresponding definition at the client. For example, such objects might be defined for use by local Zim applications only.
  • Fields defined in the application must also exist in the corresponding object on the remote database; the names of entity set or relationship fields must be the same on the server or make use of the Remote Naming Feature (below). However, the order of fields and their types do not need to match (although, if different types are used, conversion errors might occur depending on the actual data values).

Remote Naming Feature

Normally, data dictionary definitions at the client side are the same as those at the remote database.

There are circumstances where names must be different in which case the definition at the client side can use the RemoteName or RemoteOwnerName. These two fields are defined in DC when the object is also defined and tell which name and Zim directory are the real names at the remote database.

For example, at the client side, an entity set is defined with the name MyCustomers belonging to Zim directory MyZimDir in order to access, at a remote database, an entity set called Customers defined in Zim directory called ZimDir. In this case, the definition of RemoteName or RemoteOwnerName at the client side must be:

EntName: “MyCustomers”

DirName: “MyZimDir”

RemoteName: “Customers”

RemoteOwnerName: “ZimDir”

Connecting and Recompiling the Zim Application

The application is compiled using the “compilemode” option “sqlmode” and conversion to a Zim Client-Server application is complete.

If the application is to be developed for execution against a pre-existing SQL server database, then the Zim application should be compiled using the “compilemode” option “zimmode” to test the application for functionality and performance. Then the application can be compiled using the “compilemode” option “sqlmode”. If the application is being compiled in the default “sqlmode” with the sqlcompile setting “on”, then you must be connected to the SQL server.

Tracing SQL Commands

In order to access relational databases, Zim client-server generates SQL statements. You can see the statements generated by setting SQLTRACE on. Statements are generated when Zim statements containing references to SQL tables are parsed, compiled, or executed interpretively. Previewing the SQL statements generated provides an insight into the type or amount of work the database is going to be doing. This option is set using the command:

set sqltrace [on | off]

The default setting for SQLTRACE is OFF.

Program Execution

Zim is capable of executing database commands in either of these three modes:

SQLMODESQL mode
SERVERMODEZim Integrated Server mode
ZIMMODEStandard Zim mode

The default mode of operation is SERVERMODE. For all database servers except Zim Integrated Server, SERVERMODE and SQLMODE behave identically. In SERVERMODE, a Zim client accessing tables on Zim Integrated Server sends Zim commands to the server instead of SQL commands which greatly improves performance. In SQLMODE, the ZIM client determines the optimal SQL syntax to send to the server by inspecting the values for “EntType” or “RelType” in the Object Dictionary for the tables specified in the command.

The mode in which Zim operates may be set by the command:

SET EXECUTEMODE [ SERVERMODE | SQLMODE | ZIMMODE ]

where SERVERMODE is the default setting. For example, if the command:

set executemode zimmode

were executed, then Zim would revert to executing standard Zim code. If the program had been compiled in SQLMODE, it would revert to interpreting the program source file but issue error and warning messages.

Zim generates SQL statements to access entity sets and relationships that are stored in relational databases. To execute these statements (i.e. your application), you must be connected to the relational database. In order to support “off-line” development Zim enables you to turn off SQL statement generation, in effect generating standard Zim code. Zim ignores “EntType” and “RelType” and considers everything to be under Zim’s control. This enables you to develop and maintain applications without connecting to the database. In fact, development can proceed independent of the particular database vendor.

Compiling Client-Server Application Programs

The “SET SQLCOMPILE” Command

Zim is designed to take advantage of many characteristics of a given SQL server. Many SQL servers provide compile time facilities to increase performance at runtime. A compile time option enables you to control when the database server does certain compilation functions such as statement parsing. If SQLCOMPILE is OFF, then these functions are performed at Zim’s runtime. If SQLCOMPILE is ON, these function are performed as Zim compiles the application, if possible. This option is set using the command:

SET SQLCOMPILE [ ON | OFF ]

The default setting for SQLCOMPILE is OFF.

In order to take advantage of these facilities, you must be connected to the SQL server at compile time. If this option is turned off, all work is done at run time and you do not have to be connected at compile time.

Different applications being compiled against the same SQL server database can have programs with the same name (i.e. “MAIN”). To avoid conflicts that can arise when compiling several applications against the same SQL server database, each application should be flagged with a unique identifier. The “config.zim” entry is

SQL DATABASE NAME xxx

where “xxx” is a three-character ID. Zim makes use of this ID when compiling. It should be noted that SQLCOMPILE can be turned on or off for a single statement if desired.

Importing SQL Table Definitions

If you are developing a Zim application to run against an existing SQL database, you must create an equivalent representation of that data in Zim. Zim’s definition interface translates SQL definitions into equivalent definitions in Zim.

The mapping of SQL tables to Zim EntitySets or data relationships is straight-forward. A Zim developer must first establish a clear understanding of the characteristics of each table in the SQL database. Do this by examining the SQL statements (in particular, the join conditions of “select” statements) in existing application code to successfully identify the relationships relevant to the database.

SQL tables that exhibit the same properties as Zim EntitySets (i.e. contain data about “things” only) can be mapped directly to Zim EntitySets. Tables that appear to contain information representing interactions between other tables are candidates for mapping to either data relationships or EntitySets. In Zim, a binary data relationship “r”, defined between the EntitySets “e1” and “e2”, can be decomposed into an EntitySet called “e3” and two non-data binary relationships “r1” between “e1” and “e3” and “r2” between “e3” and “e2”. SQL statements in existing application code (in particular the join conditions of “select” statements) should be examined to identify potential non-data relationships and the relationship conditions for all relationships.

Accessing Multiple Databases at Same Time

Zim enables you to connect to more than one remote database at the same time. This enables you to do join operations between different databases on different machines managed by different Zim database servers. No support for distributed concurrency control is available; updates which span multiple servers, though permitted, should be performed with care.

As seen in The Preparation of the Client Database, the object being accessed must have its definition of the fields EntType, RelType or DocType changed to reflect the remote database being accessed via “zimserv”. In other words, all objects with the definition “zimserv” will be accessed in this particular remote Zim database once connected to it because the CONNECT statement links a particular remote database located in the network to the server type “zimserv”. Therefore, a Zim Server connection can only access one remote database at a time. If it is needed to access objects from different remote databases, a set of CONNECT and DISCONNECT commands must be issued for each object located in different remote databases.

There are situations, however, where it is needed to access all objects at the same time from different databases. This can be done by creating more than one instance or aliases of a “zimserv” via the utility modstab. For example, “zimserv1”, “zimserv2” or “Abroad” can be aliases of “zimserv”.

Consequently, if objects have their corresponding fields EntType, RelType or DocType created with any of the above aliases and after the CONNECT, these objects will be available from the correct remote databases.

The following example illustrates this idea. If Customers is locally located (that is, Entype is equal to “zim”), Cities has an EntType equals to “zimserv1” and Orders has an EntType equals to “Abroad”, then the FIND statement will access all customers in the local database that live in cities from the first remote database and that purchased something as defined in the second database:

CONNECT TO “zimserv1” USING (… parameters to access remote database named “zimserv1″…)

CONNECT TO “abroad” USING (… parameters to access remote database named “abroad”…)

FIND Customers LiveIn Cities Issue Orders

DISCONNECT FROM “zimserv1”

DISCONNECT FROM “abroad”

Connecting to SQL Database Servers

All SQL servers require that some type of connection be established between a client program and the server before any database operations can be attempted. In Zim, a connection between a Zim application and an SQL server is established by the “CONNECT” command and removed by the “DISCONNECT” command.

During the execution of any Zim application, a CONNECT command must have been executed prior to the execution of any Zim command requiring database operations to be performed by the SQL server. Connections can be multiple or single as explained below.

Single Connections

When SQL database servers are accessed one at a time, a connection is always followed by a disconnection. Example:

connect to “JDBCSAM” using (“SalesDB”, “sqlserver”, “admin”, “”, “1422”, “172.16.20.33”)

disconnect from “JDBCSAM”

connect to “JDBCSAM” using (“Marketing”, “oracle”, “scott”, “tiger”, “1501”, “www.mycompany.com”)

disconnect from “JDBCSAM”

Notice that each CONNECT statement is followed by a DISCONNECT statement. In the above example, the first connection is different from the second one but it can be the same as well.

Multiple or Concurrent Connections

In this scenario, more than one connection is established and maintained at the same time without an intervening disconnection:

connect to “JDBCSAM” using (“SalesDB”, “sqlserver”, “admin”, “”, “1422”, “172.16.20.33”)

connect to “MYJDBC” using (“Marketing”, “oracle”, “scott”, “tiger”, “1501”, “www.mycompany.com”)

disconnect from “MYJDBC”

disconnect from “JDBCSAM”

Now, the second connection is performed not to JDBCSAM anymore but an alias to it called MYJDBC. The reasoning behind this difference is that all ZIM objects need to reference a specific type of object (for Entity Sets, it is the EntType; for Relationships, it is the RelType and so on) and Zim needs to know which object belongs to each type.

The aliases are defined in the “zimalias.zim” configuration file as described in the Servers Alias Configuration File.

Obviously, an alias could have been used in the example for single connections as well.

Zim Database Connection Concept

Distinguishing Between Zim 9 Server and Zim Server Connectivity

Zim 9 Server

  • Client/Server Mode: Zim 9 Server operates in Client/Server mode where Zim Server acts as the server, and Zim or Zim Thin Client are the clients.
  • Automatic Connection: Clients run a Zim application that accesses data provided by the server. Parameters, including the database name, are provided at the start of a client session to establish an automatic connection to Zim 9 Server.
  • Database Interaction: All data references are directed to the specified database until the client ends its operation.

Zim Server Connectivity

  • Accessing Multiple Databases: Clients can access data from another database serviced by the current Zim Server or another Zim Server on a different machine.
  • Connection Process: The client connects to a second database, performs data processing, and disconnects, all while still connected to the original database.
  • Concurrent Connections: Multiple Zim Server Connectivities can be performed with third or fourth databases, either simultaneously or sequentially.

Example Workflow

  1. Start Execution: ZIM executable connects to the “Original” database.
  2. Connect to Second Database: Explicitly connect to “Second” database, process data, and disconnect.
  3. Concurrent Connections: Connect to “Second” and “Third” databases concurrently, process data, and disconnect.
  4. End Execution: Automatically disconnect from “Original” database.

Comments

  • Explicit Connection: Use the CONNECT command with parameters to identify and connect to the desired database.
  • Database Agent: Zim Server starts a ZIM Database Agent to handle client operations. One agent per connection.
  • Disconnect Command: Use the DISCONNECT command to end the interaction and terminate the agent.

Concurrent Connections

  • Alias Configuration: Define aliases in the “zimalias.zim” configuration file to manage concurrent connections and ensure Zim knows which objects are related to each connection.
pt_BRPortuguese