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_connect_disconnect_with_withstatement(self):
with exasol.connect(**self.odbc_kwargs) as ecn:
crs = ecn.cursor()
crs.execute('select * from dual')
rows = crs.fetchall()
self.assertEqual(1, len(rows))
self.assertIsNone(rows[0][0])
self.assertRaises(pyodbc.ProgrammingError, ecn.close)
def test_close_cnxn(self):
"""Make sure using a Cursor after closing its connection doesn't crash."""
self.cursor.execute("create table t1(id integer, s varchar(20))")
self.cursor.execute("insert into t1 values (?,?)", 1, 'test')
self.cursor.execute("select * from t1")
self.cnxn.close()
# Now that the connection is closed, we expect an exception. (If the code attempts to use
# the HSTMT, we'll get an access violation instead.)
self.sql = "select * from t1"
self.assertRaises(pyodbc.ProgrammingError, self._exec)
def _startOutputService(self):
"""Start service for EXASolution UDF scripts' output
After the service is running, the createScript function
produces additional code in scripts, which redirects the
stdout and stderr of a stript to this service.
The output of this service is the local stdout.
"""
if not self._connected:
raise pyodbc.ProgrammingError("Not connected")
self._stopOutputService()
self._outputService = ScriptOutputThread()
self._outputService.fileObject = self.outputFileObject
self._outputService.finished = False
self._outputService.serverAddress = self.clientAddress
self._outputService.init()
self.clientAddress = self._outputService.serverAddress
self._outputService.start()
conn.setencoding(str, encoding='utf-8')
conn.setencoding(str, encoding='utf-8', ctype=pyodbc.SQL_CHAR)
# https://github.com/mkleehammer/pyodbc/issues/194 for this encoding fix
conn.setdecoding(pyodbc.SQL_WMETADATA, encoding='utf-32le')
except pyodbc.Error as e:
print("ODBC Connection Error Message:\n")
print(e)
print("ODBC error, exiting...\n")
sys.exit()
cursor = conn.cursor()
try:
cursor.execute(query)
except pyodbc.ProgrammingError as e:
print("\nODBC Query Error Message:\n")
print(e)
print("\nODBC error, exiting...\n")
sys.exit()
return cursor
def fetchall(self):
try:
return self.cursor.fetchall()
except pyodbc.ProgrammingError, msg:
logger.log(logging.WARN if conf.dbmsHandler else logging.DEBUG, "(remote) %s" % msg[1])
return None
def readData(self, sqlCommand, readCallback=None, **kw):
"""Execute a DQL statement and returns the result
This is a optimized version of pyodbc.Connection.execute
function. ReadData returns per default a pandas data frame
or any other data, if a different readCallback was specified.
readCallback
A function, which is called with the file object contained
the query result as CSV and all keyword arguments given to
readData. The returned data will be returned from
readData function.
"""
if not self._connected:
raise pyodbc.ProgrammingError("Not connected")
if readCallback is None:
if self.csvIsDefault:
readCallback = csvReadCallback
else:
readCallback = pandasReadCallback
odbc = self.odbc
self.odbc = None # during command execution is odbc not usable
try:
srv = TunneledTCPServer(self.serverAddress, HTTPIOHandler)
srv.pipeInFd, srv.pipeOutFd = os.pipe()
srv.outputMode = True
srv.error, srv.pipeIn, srv.pipeOut = None, os.fdopen(srv.pipeInFd), os.fdopen(srv.pipeOutFd, 'w')
s = HTTPIOServerThread()
s.srv = srv
srv.serverThread = s
q = HTTPExportQueryThread()
oracleDatabaseError = cx_Oracle.DatabaseError
oracleInterfaceError = cx_Oracle.InterfaceError
oracleNCLOB = cx_Oracle.NCLOB
except ImportError:
# also requires "Oracle Instant Client"
hasOracle = False
oracleConnect = noop
oracleDatabaseError = oracleInterfaceError = NoopException
oracleCLOB = None
try:
import pyodbc
hasMSSql = True
mssqlConnect = pyodbc.connect
mssqlOperationalError = pyodbc.OperationalError
mssqlProgrammingError = pyodbc.ProgrammingError
mssqlInterfaceError = pyodbc.InterfaceError
mssqlInternalError = pyodbc.InternalError
mssqlDataError = pyodbc.DataError
mssqlIntegrityError = pyodbc.IntegrityError
except ImportError:
hasMSSql = False
mssqlConnect = noop
mssqlOperationalError = mssqlProgrammingError = mssqlInterfaceError = mssqlInternalError = \
mssqlDataError = mssqlIntegrityError = NoopException
try:
import sqlite3
hasSQLite = True
sqliteConnect = sqlite3.connect
sqliteParseDecltypes = sqlite3.PARSE_DECLTYPES
sqliteOperationalError = sqlite3.OperationalError
def writeData(self, data, table,
columnNames=None,
quotedIdentifiers=False,
writeCallback=None,
**kw):
"""Import data to a table in EXASolution DBMS
Per default it imports the given pandas data frame to the
given table. If a writeCallback is specified, then this
function is called with given data frame and a file object,
where the CSV file should be written. The format of CSV should
be csv.excel dialect.
"""
if not self._connected:
raise pyodbc.ProgrammingError("Not connected")
if writeCallback is None:
if self.csvIsDefault:
writeCallback = csvWriteCallback
else:
writeCallback = pandasWriteCallback
odbc = self.odbc
self.odbc = None
try:
srv = TunneledTCPServer(self.serverAddress, HTTPIOHandler)
srv.pipeInFd, srv.pipeOutFd = os.pipe()
srv.outputMode = False
srv.doneEvent = threading.Event()
srv.startedEvent = threading.Event()
srv.error = None
srv.pipeIn, srv.pipeOut = os.fdopen(srv.pipeInFd), os.fdopen(srv.pipeOutFd, 'w')
s = HTTPIOServerThread()