skip to main content
Developing ODBC Applications for Internationalization : The Driver Manager on Linux/UNIX
 

The Driver Manager on Linux/UNIX

The Driver Manager on Linux/UNIX shipped with DataDirect OpenAccess SDK Client for ODBC determines the type of Unicode encoding of both the application and the driver, and performs conversions when the application and driver each use different types of encoding.
Unicode ODBC drivers on Linux/UNIX can be written with either UTF-8 or UTF-16 encoding. This would normally mean that a UTF-8 application could not work with a UTF-16 driver, and, conversely, that a UTF-16 application could not work with a UTF-8 driver. To accomplish the goal of being able to use a single UTF-8 or UTF-16 application with either a UTF-8 or UTF-16 driver, the Driver Manager must be able to determine with which type of encoding the application and driver are written and, if necessary, convert them accordingly.
To make this determination, the Driver Manager supports two ODBC Environment Attributes: SQL_ATTR_APP_UNICODE_TYPE and SQL_ATTR_DRIVER_UNICODE_TYPE, each with possible values of SQL_DD_CP_UTF8 and SQL_DD_CP_UTF16. The default value is SQL_DD_CP_UTF8.
There are several steps the Driver Manager must undertake before actually connecting to the driver to achieve this goal.
1. Determine the application Unicode type: Applications that want to use UTF-16 encoding for their string types need to set SQL_ATTR_APP_UNICODE_TYPE accordingly before connecting to any driver. When the Driver Manager reads this attribute, it expects all string arguments to the ODBC W functions to be in the specified Unicode. This attribute also indicates how the SQL_C_WCHAR buffers must be encoded.
2. Determine the driver Unicode type: The Driver Manager must determine through which Unicode encoding the driver supports its "W" functions. This is done as follows:
SQLGetEnvAttr(SQL_ATTR_DRIVER_UNICODE_TYPE) is called in the driver by the Driver Manager. The driver, if capable, returns either SQL_DD_CP_UTF16 or SQL_DD_CP_UTF8 to indicate to the Driver Manager which encoding it expects.
If the preceding call to SQLGetEnvAttr fails, the Driver Manager looks either in the Data Source section of the odbc.ini specified by the connection string or in the connection string itself for a connection option called DriverUnicodeType. The valid values for this option are 1 (UTF-16) or 2 (UTF-8). The Driver Manager assumes that the Unicode encoding of the driver corresponds to the value specified.
If neither of the preceding attempts are successful, the Driver Manager assumes that the Unicode encoding of the driver is UTF-8.
3. Determine if the driver supports SQL_ATTR_WCHAR_TYPE: SQLSetConnectAttr (SQL_ATTR_WCHAR_TYPE, X) is called in the driver by the Driver Manager, where X is either SQL_DD_CP_UTF8 or SQL_DD_CP_UTF16, depending on the value of the SQL_ATTR_APP_UNICODE_TYPE environment setting. If the driver returns any error on this call to SQLSetConnectAttr, the Driver Manager assumes that the driver does not support this connection attribute.
In the case of an error, the Driver Manager converts all data bound as SQL_C_WCHAR to the application Unicode type as specified by SQL_ATTR_APP_UNICODE_TYPE. The Driver Manager also converts all bound parameter data from the application Unicode type to the driver Unicode type specified by SQL_ATTR_DRIVER_UNICODE_TYPE.
Based on the information it has gathered prior to connection, the Driver Manager either does not have to convert function calls, or it converts to either UTF-8 or UTF-16 all string arguments to calls to the ODBC W functions before calling the driver.