Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_rollback_after_create_error(self):
"""
test_rollback_after_create_error
For some reason, SQL server will issue a batch-abort
if the statement is a CREATE statement and it fails. This means
the transaction is implicitly rolled back and a subsequent call to
rollback() without special handling would result in an error.
"""
cur = self.conn.cursor()
cur.execute('insert into users values (%s)', 'foobar')
eq_(self.row_count(), 1)
try:
cur.execute("CREATE TABLE badschema.t1 ( test1 CHAR(5) NOT NULL)")
except pym.ProgrammingError as e:
if 'badschema' not in str(e):
raise
# encountered an error, so we want to rollback
self.conn.rollback()
# rollback should have resulted in user's insert getting rolled back
# too
eq_(self.row_count(), 0)
def execute(self, query):
retVal = False
try:
self.cursor.execute(getText(query))
retVal = True
except (pymssql.OperationalError, pymssql.ProgrammingError) as ex:
logger.log(logging.WARN if conf.dbmsHandler else logging.DEBUG, "(remote) '%s'" % getSafeExString(ex).replace("\n", " "))
except pymssql.InternalError as ex:
raise SqlmapConnectionException(getSafeExString(ex))
return retVal
def _run_sql_commands(host, user, password, database):
# Connect to sql and get cursor to run sql commands
_ensure_pymssql()
import pymssql
with pymssql.connect(host, user, password, database, tds_version='7.0') as connection:
logger.warning('Successfully Connected to PostgreSQL.')
with connection.cursor() as cursor:
try:
db_password = _create_db_password(database)
cursor.execute("CREATE USER root WITH PASSWORD = '{}'".format(db_password))
logger.warning("Ran Database Query: `CREATE USER root WITH PASSWORD = '%s'`", db_password)
except pymssql.ProgrammingError:
pass
cursor.execute("Use {};".format(database))
cursor.execute("GRANT ALL to root")
logger.warning("Ran Database Query: `GRANT ALL TO root`")
def query_messages_mssql(cursor, sql_str):
try:
logger.info('Starting execute SQL')
logger.info(sql_str)
# execute sql string
cursor.execute(sql_str)
parse_messages_mssql(cursor)
except ProgrammingError as e:
logger.error(e)
logger.error('SQL execute error: '.format(sql_str))
def execute(self, query):
retVal = False
try:
self.cursor.execute(getText(query))
retVal = True
except (pymssql.OperationalError, pymssql.ProgrammingError) as ex:
logger.log(logging.WARN if conf.dbmsHandler else logging.DEBUG, "(remote) '%s'" % getSafeExString(ex).replace("\n", " "))
except pymssql.InternalError as ex:
raise SqlmapConnectionException(getSafeExString(ex))
return retVal
def execute(self, query):
retVal = False
try:
self.cursor.execute(utf8encode(query))
retVal = True
except (pymssql.OperationalError, pymssql.ProgrammingError), msg:
logger.log(logging.WARN if conf.dbmsHandler else logging.DEBUG, "(remote) %s" % str(msg).replace("\n", " "))
except pymssql.InternalError, msg:
raise SqlmapConnectionException(msg)
return retVal
def execute(self, query):
retVal = False
try:
self.cursor.execute(utf8encode(query))
retVal = True
except (pymssql.OperationalError, pymssql.ProgrammingError), msg:
logger.log(logging.WARN if conf.dbmsHandler else logging.DEBUG, "(remote) %s" % str(msg).replace("\n", " "))
except pymssql.InternalError, msg:
raise SqlmapConnectionException(msg)
return retVal
def fetchall(self):
try:
return self.cursor.fetchall()
except (pymssql.ProgrammingError, pymssql.OperationalError, _mssql.MssqlDatabaseException), msg:
logger.log(logging.WARN if conf.dbmsHandler else logging.DEBUG, "(remote) %s" % str(msg).replace("\n", " "))
return None