2.1.3 CS_CONTEXT Objects

Calling the cs_ctx_alloc() or cs_ctx_global() function will create a CS_CONTEXT object. When the CS_CONTEXT object is deallocated the Sybase cs_ctx_drop() function will be called for the context.

CS_CONTEXT objects have the following interface:

debug
This is an integer which is used to control the output of debug messages. If set non-zero the object will write debug messages to stderr. The default value is zero.

ct_callback (action, type [, cb_func = None])
Installs, removes, or queries current Sybase callback function. This is only available when the sybasect module has been compiled without the WANT_THREADS macro defined in sybasect.h.

When the action argument is CS_SET, a callback function is installed. If cb_func is None the callback identified by type will be removed. The return value is the Sybase result code.

When the action argument is CS_GET, the current callback function will be returned. The cb_func must not be supplied in this case. The return value is a two element tuple containing the Sybase result code and the current callback function. When the Sybase result code is not CS_SUCCEED or there is no current callback, the returned function will be None.

The type argument identifies the callback function type. Currently only the following callback functions are supported.

type callback function arguments
CS_CLIENTMSG_CB ctx, conn, msg
CS_SERVERMSG_CB ctx, conn, msg

from sybasect import *

def ctlib_server_msg_handler(conn, cmd, msg):
    return CS_SUCCEED

status, ctx = cs_ctx_alloc()
if status != CS_SUCCEED:
    raise CSError(ctx, 'cs_ctx_alloc')
if ctx.ct_init(CS_VERSION_100):
    raise CSError(ctx, 'ct_init')
if ctx.ct_callback(CS_SET, CS_SERVERMSG_CB,
                   ctlib_server_msg_handler) != CS_SUCCEED:
    raise CSError(ctx, 'ct_callback')

ct_con_alloc ()
Allocates a new CS_CONNECTION object which is used to connect to a Sybase server.

The result is a tuple containing the Sybase result code and a new instance of the CS_CONNECTION class. None is returned as the CS_CONNECTION object when the result code is not CS_SUCCEED.

from sybasect import *

def init_db():
    status, ctx = cs_ctx_alloc()
    if status != CS_SUCCEED:
        raise CSError(ctx, 'cs_ctx_alloc')
    if ctx.ct_init() != CS_SUCCEED:
        raise CSError(ctx, 'ct_init')
    if ctx.cs_diag(CS_INIT) != CS_SUCCEED:
        raise CSError(ctx, 'cs_diag')
    if ctx.ct_config(CS_SET, CS_NETIO, CS_SYNC_IO) != CS_SUCCEED:
        raise CSError(ctx, 'ct_config')
    return ctx

status, conn = ctx.ct_con_alloc()
if status != CS_SUCCEED:
    raise CSError(ctx, 'ct_con_alloc')

ct_config (action, property [, value])
Sets and retrieves properties of the context object

When action is CS_SET a compatible value argument must be supplied and the method returns the Sybase result code.

When action is CS_GET the method returns a tuple containing the Sybase result code and the property value.

When action is CS_CLEAR the method clears the property and returns the Sybase result code.

The recognised properties are:

property type
CS_LOGIN_TIMEOUT int
CS_MAX_CONNECT int
CS_NETIO int
CS_NO_TRUNCATE int
CS_TEXTLIMIT int
CS_TIMEOUT int
CS_VER_STRING string
CS_VERSION string

For an explanation of the property values and get/set/clear semantics please refer to the Sybase documentation.

from sybasect import *
status, ctx = cs_ctx_alloc()
if status != CS_SUCCEED:
    raise CSError(ctx, 'cs_ctx_alloc')
if ctx.ct_init() != CS_SUCCEED:
    raise CSError(ctx, 'ct_init')
if ctx.cs_diag(CS_INIT) != CS_SUCCEED:
    raise CSError(ctx, 'cs_diag')
if ctx.ct_config(CS_SET, CS_NETIO, CS_SYNC_IO) != CS_SUCCEED:
    raise CSError(ctx, 'ct_config')

ct_exit ([option])
Calls the Sybase ct_exit() function. Returns the Sybase result code.

ct_init ([version = CS_VERSION_100])
Initialises the context object and tells the CT library which version of behaviour is expected. Returns the Sybase result code. This method must be called immediately after creating the context.

from sybasect import *
status, ctx = cs_ctx_alloc()
if status != CS_SUCCEED:
    raise CSError(ctx, 'cs_ctx_alloc')
if ctx.ct_init() != CS_SUCCEED:
    raise CSError(ctx, 'ct_init')

cs_ctx_drop ()
Calls the Sybase cs_ctx_drop() function. Returns the Sybase result code.

This method will be automatically called when the CS_CONTEXT object is deleted. Applications do not need to call the method.

cs_diag (operation [, ...])
Manage Open Client/Server error messages for the context.

When operation is CS_INIT a single argument is accepted and the Sybase result code is returned.

When operation is CS_MSGLIMIT two additional arguments are expected; type and num. Currently Sybase expects type to be CS_CLIENTMSG_TYPE. The Sybase result code is returned.

When operation is CS_CLEAR an additional type argument is accepted and the Sybase result code is returned.

When operation is CS_GET two additional arguments are expected; type which currently must be CS_SERVERMSG_TYPE, and index. A tuple is returned which contains the Sybase result code and the requested CS_SERVERMSG message. None is returned as the message object when the result code is not CS_SUCCEED.

When operation is CS_STATUS an additional type argument is accepted. A tuple is returned which contains the Sybase result code and the number of messages available for retrieval.

def print_msgs(ctx):
    status, num_msgs = ctx.cs_diag(CS_STATUS, CS_SERVERMSG_TYPE)
    if status == CS_SUCCEED:
        for i in range(num_msgs):
            status, msg = ctx.cs_diag(CS_GET, CS_SERVERMSG_TYPE, i + 1)
            if status != CS_SUCCEED:
                continue
            for attr in dir(msg):
                print '%s: %s' % (attr, getattr(msg, attr))
    ctx.cs_diag(CS_CLEAR, CS_SERVERMSG_TYPE)