Django

Code

Ticket #5062: pymssql.diff

File pymssql.diff, 3.6 kB (added by mamcx, 1 year ago)

Fixed pymssql. Install from site and then apply this

  • pymssql.py

    old new  
    133133 
    134134                # first try to execute all queries 
    135135                totrows = 0 
    136                 sql = "" 
    137                 try: 
    138                         for params in param_seq: 
    139                                 if params != None: 
    140                                         sql = _quoteparams(operation, params) 
     136                #import pdb 
     137                #pdb.set_trace() 
     138                #Respect GO terminator 
     139                for sql in operation.split('\nGO'): 
     140                        if sql=='': 
     141                                continue 
     142                        try: 
     143                                for params in param_seq: 
     144                                        if params != None: 
     145                                                sql = _quoteparams(sql, params) 
     146 
     147                                        #print sql 
     148                                        ret = self.__source.query(sql) 
     149                                        if ret == 1: 
     150                                                self._result = self.__source.fetch_array() 
     151                                                totrows = totrows + self._result[self.__resultpos][1] 
     152                                        else: 
     153                                            self._result = None 
     154                                            raise DatabaseError, "error: %s" % self.__source.errmsg() 
     155                        except Exception,e: 
     156                                if self.__source.errmsg() == None: 
     157                                        raise e 
    141158                                else: 
    142                                         sql = operation 
    143                                 #print sql 
    144                                 ret = self.__source.query(sql) 
    145                                 if ret == 1: 
    146                                         self._result = self.__source.fetch_array() 
    147                                         totrows = totrows + self._result[self.__resultpos][1] 
    148                                 else: 
    149                                     self._result = None 
    150                                     raise DatabaseError, "error: %s" % self.__source.errmsg() 
    151                 except: 
    152                         raise DatabaseError, "internal error: %s" % self.__source.errmsg() 
     159                                        raise DatabaseError, "internal error: %s" % self.__source.errmsg() 
    153160 
    154161                # then initialize result raw count and description 
    155162                if len(self._result[self.__resultpos][0]) > 0: 
     
    220227        # alternative quoting by Luciano Pacheco <lucmult@gmail.com> 
    221228        #elif hasattr(x, 'timetuple'): 
    222229        #       x = time.strftime('\'%Y%m%d %H:%M:%S\'', x.timetuple()) 
     230        elif type(x) == types.BooleanType:  
     231                x = x and 1 or 0  
    223232        else: 
    224233                #print "didn't like " + x + " " + str(type(x)) 
    225234                raise InterfaceError, 'do not know how to handle type %s' % type(x) 
     
    244253 
    245254        def __init__(self, cnx): 
    246255                self.__cnx = cnx 
     256                self.__autocommit = False 
    247257                try: 
    248                         self.__cnx.query("begin tran") 
     258                        self.__cnx.query("IF @@TRANCOUNT>0 begin tran") 
    249259                        self.__cnx.fetch_array() 
    250260                except: 
    251261                        raise OperationalError, "invalid connection." 
     
    259269        def commit(self): 
    260270                if self.__cnx == None: 
    261271                        raise OperationalError, "invalid connection." 
     272 
     273                if self.__autocommit == True: 
     274                        return 
     275 
    262276                try: 
    263                         self.__cnx.query("commit tran") 
     277                        self.__cnx.query("IF @@TRANCOUNT>0 commit tran") 
    264278                        self.__cnx.fetch_array() 
    265                         self.__cnx.query("begin tran") 
     279                        self.__cnx.query("IF @@TRANCOUNT>0 begin tran") 
    266280                        self.__cnx.fetch_array() 
    267281                except: 
    268282                        raise OperationalError, "can't commit." 
     
    270284        def rollback(self): 
    271285                if self.__cnx == None: 
    272286                        raise OperationalError, "invalid connection." 
     287 
     288                if self.__autocommit == True: 
     289                        return 
     290 
    273291                try: 
    274                         self.__cnx.query("rollback tran") 
     292                        self.__cnx.query("IF @@TRANCOUNT>0 rollback tran") 
    275293                        self.__cnx.fetch_array() 
    276                         self.__cnx.query("begin tran") 
     294                        self.__cnx.query("IF @@TRANCOUNT>0 begin tran") 
    277295                        self.__cnx.fetch_array() 
    278296                except: 
    279297                        raise OperationalError, "can't rollback." 
    280298 
     299        def autocommit(self,status): 
     300                if status: 
     301                        if self.__autocommit == False: 
     302                                self.__cnx.query("IF @@TRANCOUNT>0 rollback tran") 
     303                                self.__cnx.fetch_array() 
     304                                self.__autocommit = True 
     305                        else: 
     306                                if self.__autocommit == True: 
     307                                        self.__cnx.query("IF @@TRANCOUNT>0 begin tran") 
     308                                        self.__cnx.fetch_array() 
     309                                        self.__autocommit = False 
     310 
    281311        def cursor(self): 
    282312                if self.__cnx == None: 
    283313                        raise OperationalError, "invalid connection."