Sybase Technical Library - Product Manuals Home
[Search Forms] [Previous Section with Hits] [Next Section with Hits] [Clear Search] Expand Search

Chapter 5: Accessing Data with Transact-SQL [Table of Contents] Glossary

(Workplace UNIX) Introducing SQL Server

[-] Chapter 6: Building Client/ServerApplications

Chapter 6

Building Client/ServerApplications

Introduction

SQL Server for Workplace UNIX includes products that make it easy for C programmers to develop customized client applications to access SQL Server. This chapter discusses these Sybase connectivity products, including:

SQL Server for Workplace UNIX also includes 16-bit and 32-bit ODBC Driver Kits for accessing SQL Server data from a variety of Windows or Windows NT front-end applications.

What Is Open Client?

Open Client is a programming application interface (API) used to develop customized client applications that access SQL Server. The Open Client libraries manage all communications between a client application or tool and SQL Server databases.

In addition, if you purchase the Sybase Open Server product, you can develop applications that allow any data source, information application, or system service to respond to Open Client requests as if it were SQL Server.

The Open Client connectivity products:

The SQL Server installation includes the following Open Client products:

These products offer a choice of interfaces to the Sybase architecture for use with third- or fourth-generation programming languages, including development tools from Sybase or other vendors.

Client-Library

Client-Library includes routines that you include in a C-language client application to:

A Client-Library program is compiled and run in the same way as any other C program.

Asynchronous Processing

Client-Library uses asynchronous processing, so that applications can make server requests while continuing to process other work. Asynchronous processing provides the following capabilities:

Developing Generic Applications

Client-Library is a generic interface. As such, Client-Library does not enforce or reflect any restrictions of SQL Server.

When writing a Client-Library application, keep in mind the application's ultimate target server (for example, SQL Server or an Open Server application). If you are unsure about what is legal for a particular server application, consult its documentation.

Client-Library Features

Client-Library provides the following features for developing client/server applications:

Implementing a Client-Library Application

The following steps describe the basic procedure required for implementing an Open Client/C application.

  1. Set up the Client-Library programming environment by:

    CS-Library is the Open Client common library; it contains utilities used by both Open Client and Open Server. At least two calls to CS-Library are required for all Client-Library applications.

  2. Define error handling.

    Most applications use callback routines to handle Client-Library and server error and informational messages.

  3. Connect to SQL Server by:

  4. Send a command to SQL Server by:

  5. Process the results of the command by:

    Result rows can then be accessed and processed. Processing for cursor results includes several steps specific to handling of cursors.

  6. Complete the application by:

Control Structures

To send commands to SQL Server, a Client-Library application allocates three types of control structure, as shown below.

Structure

Function

CS_CONTEXT

Defines a specific application "context" or operating environment

CS_CONNECTION

Defines a specific client/server connection

CS_COMMAND

Defines a "command space" in which commands are sent to a server

Through these structures, a client application sets up its environment, connects to servers, sends commands, and processes results.

Client/Server Connections

Clients connect to SQL Server or Open Server applications across a network. To connect with a server, the client application must:

  1. Determine the name of the server.

  2. Find out where on the network that server is located.

The Server Name

Use Client-Library routines to connect to the server. There are several ways to determine the name of the server with which to connect. You may:

Making the Connection

After Open Client learns which server to connect to, it must figure out how to make the connection. Open Client obtains this information from the network configuration files, which contain network address information for clients and servers. These files consist of entries for all servers to which a client might connect as well as Net-Library driver information. As such, they serve as the "address book" for the client.

The files used to specify client and server addresses and network drivers vary somewhat by platform. Refer to the platform-specific Open Client/Server Supplement for complete information.

Result Set Processing

After an Open Client application sends a command to a server, it processes any results generated by the command or by the server. Listed below are the results that Client-Library routine calls can return.

Result Type

Description

Regular row results

Generated when SQL Server executes a Transact-SQL select statement; contains zero or more rows of data.

Cursor row results

Generated when an application executes a Client-Library cursor open command; contains zero or more rows of data.

Parameter results

Contains a single row of parameters (for example, message parameters or stored procedure return parameters).

Stored procedure return status results

Generated by the execution of a stored procedure; consists of a single row containing a return status.

Compute row results

Generated by the execution of a Transact-SQL select statement that contains a compute clause; consists of a single row containing a number of columns equal to the number of row aggregates in the compute clause.

Message results

Contains an id, which an application can retrieve.

If parameters are associated with a message, they are returned as a separate parameter result set following the message result set.

Describe results

Indicates the existence of descriptive information returned as the result of a Dynamic SQL statement.

Format results

Makes advance format information available to an application that needs format information before the result set is processed (for example, a gateway application that repackages SQL Server results before sending them to a foreign client).

A single command can generate more than one type of result. For example, a Transact-SQL statement that executes a stored procedure can generate multiple regular row and compute row result sets, a parameter result set, and a return status result set. For this reason, it is important that applications be coded to handle all results that SQL Server can generate.

The simplest way for an application to process all possible results is by using a loop construct. Inside the loop, a set of switch statements implements processing of whatever type of result is currently available for processing.

Datatypes

Client-Library supports a wide range of datatypes. A header file provided with Open Client contains type definitions for all of the Open Client datatypes. Using type definitions hides variations in datatype implementations on different platforms, making Open Client programs more portable.

A client application declaring program variables uses Open Client type definitions in its declaration section. For example:

CS_CHAR      buffer[40];
CS_INT result_type, count;
CS_MONEY profit;

In most cases, Open Client datatypes correspond directly to SQL Server datatypes.

Callbacks

Callbacks are user-supplied routines that are automatically called by Client-Library whenever certain triggering events, known as callback events, occur. For example, a client message callback event occurs when Client-Library generates an error message. When Client-Library recognizes a callback event, it automatically calls the appropriate callback routine.

Listed below are the types of callbacks:

Type of Callback

When Called

Client Message

In response to a Client-Library error or informational message

Completion

When an asynchronous Client-Library routine completes

Encryption

During the connection process, in response to a SQL Server request for an encrypted password

Negotiation

During the connection process:

  • In response to a SQL Server request for login security labels
  • In response to a SQL Server challenge

Server Message

In response to a SQL Server error or informational message.

Signal

In response to an operating-system SIGIO or SIGPOLL signal

Error and Message Handling

Both SQL Server and Client-Library generate messages in response to a wide range of error and informational conditions. Each error message has a number, text, and severity level.

An application can handle Client-Library and SQL Server messages using one of two methods:

Most applications use callbacks to handle messages, but an application that is running on a platform and language combination that does not support callbacks must use the inline method.

Building a Client-Library Executable

To compile a Client-Library/C application, use a Sybase-certified C compiler for your platform. Compile and link your application as specified by your compiler and operating-system documentation.

All Client-Library applications require the header file provided with Open Client that contains type definitions and declarations required by Client-Library routines.

Localizing Client-Library Applications

You can localize an application to run in a specific national language environment. An application that is localized typically:

The localization information of an application is described by a locale name. A locale name is a character string that represents a language/character set/sort order combination. For example, the locale name "fr" might represent the combination french/ iso_1/binary. Sybase provides some pre-defined locale names, and you can define others, as well.

Character Set Conversion Between SQL Server and Client-Library

When a localized Client-Library application connects to SQL Server, the server checks to see if it supports the application's language, character set, and sort order. If it does, then SQL Server handles the translation and issues messages in the client's language and character set. If SQL Server does not support the application's language, character set, or sort order, it issues a warning and does not make the connection.

Client-Library Sample Programs

Open Client Client-Library/C includes sample programs that illustrate how to use the Client-Library routines. These programs access the pubs2 sample database, which is also provided as part of your SQL Server for Workplace UNIX installation.

Net-Library

Net-Library furnishes a standard interface to most of the available network protocol implementations and provides transport protocol and operating system independence.

Because data can reside on any hardware platform supported by Sybase and can be accessed with common communications protocols, Net-Library's capabilities enable application development and deployment independent of the network. By specifying a Net-Library at run time, you can choose network protocols as needed.

DB-Library

DB-Library is the programming interface that Sybase provided with earlier releases. SQL Server for Workplace UNIX includes
DB-Library for compatibility with existing applications.

Using DB-Library with Client-Library

Although you can make calls to both DB-Library and Client-Library in a single application, you must treat the libraries as separate within the application. That is, you cannot use functions in one library with the structures, datatypes, commands, or result sets from the other library.

You can convert DB-Library programs to their Client-Library equivalents.

Getting More Information

The table below lists sources of information for topics discussed in this chapter.

Topic

Source(s) of Information

Callbacks

  • Open Client Client-Library/C Reference Manual

Client-Library API

  • Open Client Client-Library/C Reference Manual
  • Open Client Client-Library/C Programmer's Guide

Connection routines

  • Open Client Client-Library/C Reference Manual

Control structures

  • Open Client Client-Library/C Reference Manual
  • Open Client Client-Library/C Programmer's Guide

Client-Library Datatypes

  • Open Client Client-Library/C Reference Manual
  • Open Client and Open Server Common Libraries Reference Manual

DB-Library API

  • Open Client DB-Library/C Reference Manual

Error handling

  • Open Client Client-Library/C Reference Manual

Localization

  • Open Client Client-Library/C Reference Manual

Message handling

  • Open Client Client-Library/C Reference Manual

Net-Library

  • Open Client/Server Supplement for the relevant platform

Results Processing

  • Open Client Client-Library/C Programmer's Guide


Chapter 5: Accessing Data with Transact-SQL [Table of Contents] Glossary