Next / Previous / TCC home / NMT home

Source listing for sy_db.py

logo
"""sy_db.py:  Sybase version of `dbhelpers' database interface

    Exports all functions from `db_helpers.py', which see
"""

VERSION = "$Revision: 1.1 $  $Date: 2000/07/11 19:01:41 $"

#================================================================
# Imports
#----------------------------------------------------------------

from db_helpers import *    # Basic database interface functions

import ctsybase             # Sybase interface

SQL_DEBUG = 1       # Display SQL commands if set



# - - -   T h e D a t a b a s e   - - -

def TheDatabase ( options ):
    if  not Database.instance:
        Database.instance  =  Database(options)

    return Database.instance


# - - - - -   c l a s s   D a t a b a s e   - - - - -

class Database(BasicDatabase):
    """Sybase implementation of db_helpers.Database

      Exports everything from BasicDatabase, except:
        Database ( options )
          [ if options is a dictionary that defines keys 'user',
            'password', and 'server' with appropriate values to
            access a Sybase database ->
              return a new Database object representing
              that database
            else -> raise DBHelpersError ]
    """


# - - -   D a t a b a s e . _ _ i n i t _ _   - - -

    def __init__ ( self, options ):
        """ Constructor for Database
        """
        #-- 1 --
        # [ call the parent constructor ]
        BasicDatabase.__init__ ( self, options )

        #-- 2 --
        # [ if options is a valid option dictionary for a Sybase connect ->
        #     self.connect  :=  a ctsybase object for that connection
        #   else ->
        #     raise DBHelpersError ]
        try:
            self.connect  =  ctsybase.ctsybase(options)
        except ctsybase.error, detail:
            raise DBHelpersError, ( "Sybase error: %s" % `detail` )


# - - -   D a t a b a s e . e x e c u t e   - - -

    def execute ( self, sql ):
        """ Executes a line of SQL against self.
        """
        if  SQL_DEBUG:
            sys.stderr.write("$$$ %s\n" % sql)

        result  =  self.connect.execute ( sql )
        return result


# - - - - -   c l a s s   T a b l e   - - - -

class Table(BasicTable):
    """Sybase implementation of BasicTable

      Exports: all functions of BasicTable.
    """


# - - -   T a b l e . e x i s t s   - - -

    def exists ( self ):
        """Does the table exist in the db?
        """
        foo = self.db.execute ( "sp_help %s" % self.name )[0][0]
        if  len(foo) > 1:
            return 1
        else:
            return 0

See also: Python database helper functions
TCC home: TCC home
NMT home: NMT home

Last updated: 2014-11-24 02:33 MST