Index: pymssql.py
===================================================================
--- pymssql.py	(revision 190)
+++ pymssql.py	(working copy)
@@ -133,23 +133,30 @@
 
 		# first try to execute all queries
 		totrows = 0
-		sql = ""
-		try:
-			for params in param_seq:
-				if params != None:
-					sql = _quoteparams(operation, params)
+		#import pdb
+		#pdb.set_trace()
+		#Respect GO terminator
+		for sql in operation.split('\nGO'):
+			if sql=='':
+				continue
+			try:
+				for params in param_seq:
+					if params != None:
+						sql = _quoteparams(sql, params)
+
+					#print sql
+					ret = self.__source.query(sql)
+					if ret == 1:
+						self._result = self.__source.fetch_array()
+						totrows = totrows + self._result[self.__resultpos][1]
+					else:
+					    self._result = None
+					    raise DatabaseError, "error: %s" % self.__source.errmsg()
+			except Exception,e:
+				if self.__source.errmsg() == None:
+					raise e
 				else:
-					sql = operation
-				#print sql
-				ret = self.__source.query(sql)
-				if ret == 1:
-					self._result = self.__source.fetch_array()
-					totrows = totrows + self._result[self.__resultpos][1]
-				else:
-				    self._result = None
-				    raise DatabaseError, "error: %s" % self.__source.errmsg()
-		except:
-			raise DatabaseError, "internal error: %s" % self.__source.errmsg()
+					raise DatabaseError, "internal error: %s" % self.__source.errmsg()
 
 		# then initialize result raw count and description
 		if len(self._result[self.__resultpos][0]) > 0:
@@ -220,6 +227,8 @@
 	# alternative quoting by Luciano Pacheco <lucmult@gmail.com>
 	#elif hasattr(x, 'timetuple'):
 	#	x = time.strftime('\'%Y%m%d %H:%M:%S\'', x.timetuple())
+	elif type(x) == types.BooleanType: 
+		x = x and 1 or 0 
 	else:
 		#print "didn't like " + x + " " + str(type(x))
 		raise InterfaceError, 'do not know how to handle type %s' % type(x)
@@ -244,8 +253,9 @@
 
 	def __init__(self, cnx):
 		self.__cnx = cnx
+		self.__autocommit = False
 		try:
-			self.__cnx.query("begin tran")
+			self.__cnx.query("IF @@TRANCOUNT>0 begin tran")
 			self.__cnx.fetch_array()
 		except:
 			raise OperationalError, "invalid connection."
@@ -259,10 +269,14 @@
 	def commit(self):
 		if self.__cnx == None:
 			raise OperationalError, "invalid connection."
+
+		if self.__autocommit == True:
+			return
+
 		try:
-			self.__cnx.query("commit tran")
+			self.__cnx.query("IF @@TRANCOUNT>0 commit tran")
 			self.__cnx.fetch_array()
-			self.__cnx.query("begin tran")
+			self.__cnx.query("IF @@TRANCOUNT>0 begin tran")
 			self.__cnx.fetch_array()
 		except:
 			raise OperationalError, "can't commit."
@@ -270,14 +284,30 @@
 	def rollback(self):
 		if self.__cnx == None:
 			raise OperationalError, "invalid connection."
+
+		if self.__autocommit == True:
+			return
+
 		try:
-			self.__cnx.query("rollback tran")
+			self.__cnx.query("IF @@TRANCOUNT>0 rollback tran")
 			self.__cnx.fetch_array()
-			self.__cnx.query("begin tran")
+			self.__cnx.query("IF @@TRANCOUNT>0 begin tran")
 			self.__cnx.fetch_array()
 		except:
 			raise OperationalError, "can't rollback."
 
+	def autocommit(self,status):
+		if status:
+			if self.__autocommit == False:
+				self.__cnx.query("IF @@TRANCOUNT>0 rollback tran")
+				self.__cnx.fetch_array()
+				self.__autocommit = True
+			else:
+				if self.__autocommit == True:
+					self.__cnx.query("IF @@TRANCOUNT>0 begin tran")
+					self.__cnx.fetch_array()
+					self.__autocommit = False
+
 	def cursor(self):
 		if self.__cnx == None:
 			raise OperationalError, "invalid connection."
