pysybase.py :  » Database » SQLAlchemy » SQLAlchemy-0.6.0 » lib » sqlalchemy » dialects » sybase » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Database » SQLAlchemy 
SQLAlchemy » SQLAlchemy 0.6.0 » lib » sqlalchemy » dialects » sybase » pysybase.py
# pysybase.py
# Copyright (C) 2010 Michael Bayer mike_mp@zzzcomputing.com
#
# This module is part of SQLAlchemy and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php

"""
Support for Sybase via the python-sybase driver.

http://python-sybase.sourceforge.net/

Connect strings are of the form::

    sybase+pysybase://<username>:<password>@<dsn>/[database name]

Unicode Support
---------------

The python-sybase driver does not appear to support non-ASCII strings of any
kind at this time.

"""

from sqlalchemy import types
from sqlalchemy.dialects.sybase.base import SybaseDialect,\
                                        SybaseExecutionContext, SybaseSQLCompiler


class _SybNumeric(sqltypes.Numeric):
    def result_processor(self, dialect, type_):
        if not self.asdecimal:
            return processors.to_float
        else:
            return sqltypes.Numeric.result_processor(self, dialect, type_)

class SybaseExecutionContext_pysybase(SybaseExecutionContext):

    def set_ddl_autocommit(self, dbapi_connection, value):
        if value:
            # call commit() on the Sybase connection directly,
            # to avoid any side effects of calling a Connection 
            # transactional method inside of pre_exec()
            dbapi_connection.commit()

    def pre_exec(self):
        SybaseExecutionContext.pre_exec(self)

        for param in self.parameters:
            for key in list(param):
                param["@" + key] = param[key]
                del param[key]


class SybaseSQLCompiler_pysybase(SybaseSQLCompiler):
    def bindparam_string(self, name):
        return "@" + name
   
class SybaseDialect_pysybase(SybaseDialect):
    driver = 'pysybase'
    execution_ctx_cls = SybaseExecutionContext_pysybase
    statement_compiler = SybaseSQLCompiler_pysybase

    colspecs={
       sqltypes.Numeric:_SybNumeric,
       sqltypes.Float:sqltypes.Float
    }

    @classmethod
    def dbapi(cls):
        import Sybase
        return Sybase

    def create_connect_args(self, url):
        opts = url.translate_connect_args(username='user', password='passwd')

        return ([opts.pop('host')], opts)

    def do_executemany(self, cursor, statement, parameters, context=None):
        # calling python-sybase executemany yields:
        # TypeError: string too long for buffer
        for param in parameters:
            cursor.execute(statement, param)

    def _get_server_version_info(self, connection):
       vers = connection.scalar("select @@version_number")
       # i.e. 15500, 15000, 12500 == (15, 5, 0, 0), (15, 0, 0, 0), (12, 5, 0, 0)
       return (vers / 1000, vers % 1000 / 100, vers % 100 / 10, vers % 10)

    def is_disconnect(self, e):
        if isinstance(e, (self.dbapi.OperationalError, self.dbapi.ProgrammingError)):
            msg = str(e)
            return ('Unable to complete network request to host' in msg or
                    'Invalid connection state' in msg or
                    'Invalid cursor state' in msg)
        else:
            return False

dialect = SybaseDialect_pysybase
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.