<s> import struct <EOL> import socket <EOL> import select <EOL> import random <EOL> import binascii <EOL> import math <EOL> import datetime <EOL> import sys <EOL> import string <EOL> from impacket import ntlm , uuid , LOG <EOL> from impacket . structure import Structure <EOL> try : <EOL> import OpenSSL <EOL> from OpenSSL import SSL , crypto <EOL> except : <EOL> LOG . critical ( "<STR_LIT>" ) <EOL> raise <EOL> class DummyPrint : <EOL> def logMessage ( self , message ) : <EOL> print message <EOL> SQLR_PORT = <NUM_LIT> <EOL> SQLR_CLNT_BCAST_EX = <NUM_LIT> <EOL> SQLR_CLNT_UCAST_EX = <NUM_LIT> <EOL> SQLR_CLNT_UCAST_INST = <NUM_LIT> <EOL> SQLR_CLNT_UCAST_DAC = <NUM_LIT> <EOL> class SQLR ( Structure ) : <EOL> commonHdr = ( <EOL> ( '<STR_LIT>' , '<STR_LIT:B>' ) , <EOL> ) <EOL> class SQLR_UCAST_INST ( SQLR ) : <EOL> structure = ( <EOL> ( '<STR_LIT>' , '<STR_LIT::>' ) <EOL> ) <EOL> def __init__ ( self , data = None ) : <EOL> SQLR . __init__ ( self , data ) <EOL> if data is not None : <EOL> self [ '<STR_LIT>' ] = SQLR_CLNT_UCAST_INST <EOL> class SQLR_UCAST_DAC ( SQLR ) : <EOL> structure = ( <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT::>' ) , <EOL> ) <EOL> def __init__ ( self , data = None ) : <EOL> SQLR . __init__ ( self , data ) <EOL> if data is not None : <EOL> self [ '<STR_LIT>' ] = SQLR_CLNT_UCAST_DAC <EOL> class SQLR_Response ( SQLR ) : <EOL> structure = ( <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT::>' ) , <EOL> ) <EOL> class SQLErrorException ( Exception ) : <EOL> pass <EOL> TDS_SQL_BATCH = <NUM_LIT:1> <EOL> TDS_PRE_TDS_LOGIN = <NUM_LIT:2> <EOL> TDS_RPC = <NUM_LIT:3> <EOL> TDS_TABULAR = <NUM_LIT:4> <EOL> TDS_ATTENTION = <NUM_LIT:6> <EOL> TDS_BULK_LOAD_DATA = <NUM_LIT:7> <EOL> TDS_TRANSACTION = <NUM_LIT> <EOL> TDS_LOGIN7 = <NUM_LIT:16> <EOL> TDS_SSPI = <NUM_LIT> <EOL> TDS_PRE_LOGIN = <NUM_LIT> <EOL> TDS_STATUS_NORMAL = <NUM_LIT:0> <EOL> TDS_STATUS_EOM = <NUM_LIT:1> <EOL> TDS_STATUS_RESET_CONNECTION = <NUM_LIT:8> <EOL> TDS_STATUS_RESET_SKIPTRANS = <NUM_LIT:16> <EOL> TDS_ENCRYPT_OFF = <NUM_LIT:0> <EOL> TDS_ENCRYPT_ON = <NUM_LIT:1> <EOL> TDS_ENCRYPT_NOT_SUP = <NUM_LIT:2> <EOL> TDS_ENCRYPT_REQ = <NUM_LIT:3> <EOL> TDS_INTEGRATED_SECURITY_ON = <NUM_LIT> <EOL> TDS_INIT_LANG_FATAL = <NUM_LIT> <EOL> TDS_ODBC_ON = <NUM_LIT> <EOL> TDS_ALTMETADATA_TOKEN = <NUM_LIT> <EOL> TDS_ALTROW_TOKEN = <NUM_LIT> <EOL> TDS_COLMETADATA_TOKEN = <NUM_LIT> <EOL> TDS_COLINFO_TOKEN = <NUM_LIT> <EOL> TDS_DONE_TOKEN = <NUM_LIT> <EOL> TDS_DONEPROC_TOKEN = <NUM_LIT> <EOL> TDS_DONEINPROC_TOKEN = <NUM_LIT> <EOL> TDS_ENVCHANGE_TOKEN = <NUM_LIT> <EOL> TDS_ERROR_TOKEN = <NUM_LIT> <EOL> TDS_INFO_TOKEN = <NUM_LIT> <EOL> TDS_LOGINACK_TOKEN = <NUM_LIT> <EOL> TDS_NBCROW_TOKEN = <NUM_LIT> <EOL> TDS_OFFSET_TOKEN = <NUM_LIT> <EOL> TDS_ORDER_TOKEN = <NUM_LIT> <EOL> TDS_RETURNSTATUS_TOKEN = <NUM_LIT> <EOL> TDS_RETURNVALUE_TOKEN = <NUM_LIT> <EOL> TDS_ROW_TOKEN = <NUM_LIT> <EOL> TDS_SSPI_TOKEN = <NUM_LIT> <EOL> TDS_TABNAME_TOKEN = <NUM_LIT> <EOL> TDS_ENVCHANGE_DATABASE = <NUM_LIT:1> <EOL> TDS_ENVCHANGE_LANGUAGE = <NUM_LIT:2> <EOL> TDS_ENVCHANGE_CHARSET = <NUM_LIT:3> <EOL> TDS_ENVCHANGE_PACKETSIZE = <NUM_LIT:4> <EOL> TDS_ENVCHANGE_UNICODE = <NUM_LIT:5> <EOL> TDS_ENVCHANGE_UNICODE_DS = <NUM_LIT:6> <EOL> TDS_ENVCHANGE_COLLATION = <NUM_LIT:7> <EOL> TDS_ENVCHANGE_TRANS_START = <NUM_LIT:8> <EOL> TDS_ENVCHANGE_TRANS_COMMIT = <NUM_LIT:9> <EOL> TDS_ENVCHANGE_ROLLBACK = <NUM_LIT:10> <EOL> TDS_ENVCHANGE_DTC = <NUM_LIT:11> <EOL> TDS_NULL_TYPE = <NUM_LIT> <EOL> TDS_INT1TYPE = <NUM_LIT> <EOL> TDS_BITTYPE = <NUM_LIT> <EOL> TDS_INT2TYPE = <NUM_LIT> <EOL> TDS_INT4TYPE = <NUM_LIT> <EOL> TDS_DATETIM4TYPE = <NUM_LIT> <EOL> TDS_FLT4TYPE = <NUM_LIT> <EOL> TDS_MONEYTYPE = <NUM_LIT> <EOL> TDS_DATETIMETYPE = <NUM_LIT> <EOL> TDS_FLT8TYPE = <NUM_LIT> <EOL> TDS_MONEY4TYPE = <NUM_LIT> <EOL> TDS_INT8TYPE = <NUM_LIT> <EOL> TDS_GUIDTYPE = <NUM_LIT> <EOL> TDS_INTNTYPE = <NUM_LIT> <EOL> TDS_DECIMALTYPE = <NUM_LIT> <EOL> TDS_NUMERICTYPE = <NUM_LIT> <EOL> TDS_BITNTYPE = <NUM_LIT> <EOL> TDS_DECIMALNTYPE = <NUM_LIT> <EOL> TDS_NUMERICNTYPE = <NUM_LIT> <EOL> TDS_FLTNTYPE = <NUM_LIT> <EOL> TDS_MONEYNTYPE = <NUM_LIT> <EOL> TDS_DATETIMNTYPE = <NUM_LIT> <EOL> TDS_DATENTYPE = <NUM_LIT> <EOL> TDS_TIMENTYPE = <NUM_LIT> <EOL> TDS_DATETIME2NTYPE = <NUM_LIT> <EOL> TDS_DATETIMEOFFSETNTYPE = <NUM_LIT> <EOL> TDS_CHARTYPE = <NUM_LIT> <EOL> TDS_VARCHARTYPE = <NUM_LIT> <EOL> TDS_BINARYTYPE = <NUM_LIT> <EOL> TDS_VARBINARYTYPE = <NUM_LIT> <EOL> TDS_BIGVARBINTYPE = <NUM_LIT> <EOL> TDS_BIGVARCHRTYPE = <NUM_LIT> <EOL> TDS_BIGBINARYTYPE = <NUM_LIT> <EOL> TDS_BIGCHARTYPE = <NUM_LIT> <EOL> TDS_NVARCHARTYPE = <NUM_LIT> <EOL> TDS_NCHARTYPE = <NUM_LIT> <EOL> TDS_XMLTYPE = <NUM_LIT> <EOL> TDS_UDTTYPE = <NUM_LIT> <EOL> TDS_TEXTTYPE = <NUM_LIT> <EOL> TDS_IMAGETYPE = <NUM_LIT> <EOL> TDS_NTEXTTYPE = <NUM_LIT> <EOL> TDS_SSVARIANTTYPE = <NUM_LIT> <EOL> class TDSPacket ( Structure ) : <EOL> structure = ( <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT::>' ) , <EOL> ) <EOL> class TDS_PRELOGIN ( Structure ) : <EOL> structure = ( <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT::>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT:B>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT::>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT::>' ) , <EOL> ) <EOL> def __str__ ( self ) : <EOL> self [ '<STR_LIT>' ] = <NUM_LIT> <EOL> self [ '<STR_LIT>' ] = self [ '<STR_LIT>' ] + len ( self [ '<STR_LIT>' ] ) <EOL> self [ '<STR_LIT>' ] = self [ '<STR_LIT>' ] + <NUM_LIT:1> <EOL> self [ '<STR_LIT>' ] = self [ '<STR_LIT>' ] + len ( self [ '<STR_LIT>' ] ) <EOL> return Structure . __str__ ( self ) <EOL> class TDS_LOGIN ( Structure ) : <EOL> structure = ( <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT::>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT::>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT::>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT::>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT::>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT::>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT::>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT::>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT::>' ) , <EOL> ) <EOL> def __init__ ( self , data = None ) : <EOL> Structure . __init__ ( self , data ) <EOL> if data is None : <EOL> self [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> self [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> self [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> self [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> def __str__ ( self ) : <EOL> index = <NUM_LIT> + <NUM_LIT:50> <EOL> self [ '<STR_LIT>' ] = index <EOL> index += len ( self [ '<STR_LIT>' ] ) <EOL> if self [ '<STR_LIT>' ] != '<STR_LIT>' : <EOL> self [ '<STR_LIT>' ] = index <EOL> else : <EOL> self [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> index += len ( self [ '<STR_LIT>' ] ) <EOL> if self [ '<STR_LIT>' ] != '<STR_LIT>' : <EOL> self [ '<STR_LIT>' ] = index <EOL> else : <EOL> self [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> index += len ( self [ '<STR_LIT>' ] ) <EOL> self [ '<STR_LIT>' ] = index <EOL> self [ '<STR_LIT>' ] = self [ '<STR_LIT>' ] + len ( self [ '<STR_LIT>' ] ) <EOL> self [ '<STR_LIT>' ] = self [ '<STR_LIT>' ] + len ( self [ '<STR_LIT>' ] ) <EOL> self [ '<STR_LIT>' ] = self [ '<STR_LIT>' ] + len ( self [ '<STR_LIT>' ] ) <EOL> self [ '<STR_LIT>' ] = self [ '<STR_LIT>' ] <EOL> self [ '<STR_LIT>' ] = self [ '<STR_LIT>' ] + len ( self [ '<STR_LIT>' ] ) <EOL> self [ '<STR_LIT>' ] = self [ '<STR_LIT>' ] + len ( self [ '<STR_LIT>' ] ) <EOL> return Structure . __str__ ( self ) <EOL> class TDS_LOGIN_ACK ( Structure ) : <EOL> structure = ( <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT::>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ) <EOL> class TDS_RETURNSTATUS ( Structure ) : <EOL> structure = ( <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ) <EOL> class TDS_INFO_ERROR ( Structure ) : <EOL> structure = ( <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT::>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT::>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT::>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ) <EOL> class TDS_ENVCHANGE ( Structure ) : <EOL> structure = ( <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT::>' ) , <EOL> ) <EOL> class TDS_DONEINPROC ( Structure ) : <EOL> structure = ( <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ) <EOL> class TDS_ORDER ( Structure ) : <EOL> structure = ( <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT::>' ) , <EOL> ) <EOL> class TDS_ENVCHANGE_VARCHAR ( Structure ) : <EOL> structure = ( <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT::>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT::>' ) , <EOL> ) <EOL> class TDS_ROW ( Structure ) : <EOL> structure = ( <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT::>' ) , <EOL> ) <EOL> class TDS_DONE ( Structure ) : <EOL> structure = ( <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ) <EOL> class TDS_COLMETADATA ( Structure ) : <EOL> structure = ( <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT::>' ) , <EOL> ) <EOL> class MSSQL : <EOL> def __init__ ( self , address , port = <NUM_LIT> , rowsPrinter = DummyPrint ( ) ) : <EOL> self . packetSize = <NUM_LIT> <EOL> self . server = address <EOL> self . port = port <EOL> self . socket = <NUM_LIT:0> <EOL> self . replies = { } <EOL> self . colMeta = [ ] <EOL> self . rows = [ ] <EOL> self . currentDB = '<STR_LIT>' <EOL> self . COL_SEPARATOR = '<STR_LIT:U+0020>' <EOL> self . MAX_COL_LEN = <NUM_LIT:255> <EOL> self . lastError = False <EOL> self . tlsSocket = None <EOL> self . __rowsPrinter = rowsPrinter <EOL> def getInstances ( self , timeout = <NUM_LIT:5> ) : <EOL> packet = SQLR ( ) <EOL> packet [ '<STR_LIT>' ] = SQLR_CLNT_UCAST_EX <EOL> af , socktype , proto , canonname , sa = socket . getaddrinfo ( self . server , SQLR_PORT , <NUM_LIT:0> , socket . SOCK_DGRAM ) [ <NUM_LIT:0> ] <EOL> s = socket . socket ( af , socktype , proto ) <EOL> s . sendto ( str ( packet ) , <NUM_LIT:0> , ( self . server , SQLR_PORT ) ) <EOL> ready , _ , _ = select . select ( [ s . fileno ( ) ] , [ ] , [ ] , timeout ) <EOL> if not ready : <EOL> return [ ] <EOL> else : <EOL> data , _ = s . recvfrom ( <NUM_LIT> , <NUM_LIT:0> ) <EOL> s . close ( ) <EOL> resp = SQLR_Response ( data ) <EOL> entries = resp [ '<STR_LIT>' ] . split ( '<STR_LIT>' ) <EOL> entries . pop ( ) <EOL> resp = [ ] <EOL> for i , entry in enumerate ( entries ) : <EOL> fields = entry . split ( '<STR_LIT:;>' ) <EOL> ret = { } <EOL> for j , field in enumerate ( fields ) : <EOL> if ( j & <NUM_LIT> ) == <NUM_LIT:0> : <EOL> ret [ field ] = fields [ j + <NUM_LIT:1> ] <EOL> resp . append ( ret ) <EOL> return resp <EOL> def preLogin ( self ) : <EOL> prelogin = TDS_PRELOGIN ( ) <EOL> prelogin [ '<STR_LIT>' ] = "<STR_LIT>" <EOL> prelogin [ '<STR_LIT>' ] = TDS_ENCRYPT_OFF <EOL> prelogin [ '<STR_LIT>' ] = struct . pack ( '<STR_LIT>' , random . randint ( <NUM_LIT:0> , <NUM_LIT> ) ) <EOL> prelogin [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> self . sendTDS ( TDS_PRE_LOGIN , str ( prelogin ) , <NUM_LIT:0> ) <EOL> tds = self . recvTDS ( ) <EOL> return TDS_PRELOGIN ( tds [ '<STR_LIT>' ] ) <EOL> def encryptPassword ( self , password ) : <EOL> return '<STR_LIT>' . join ( map ( lambda x : chr ( ( ( ord ( x ) & <NUM_LIT> ) << <NUM_LIT:4> ) + ( ( ord ( x ) & <NUM_LIT> ) >> <NUM_LIT:4> ) ^ <NUM_LIT> ) , password ) ) <EOL> def connect ( self ) : <EOL> af , socktype , proto , canonname , sa = socket . getaddrinfo ( self . server , self . port , <NUM_LIT:0> , socket . SOCK_STREAM ) [ <NUM_LIT:0> ] <EOL> sock = socket . socket ( af , socktype , proto ) <EOL> try : <EOL> sock . connect ( sa ) <EOL> except Exception : <EOL> raise <EOL> self . socket = sock <EOL> return sock <EOL> def disconnect ( self ) : <EOL> if self . socket : <EOL> return self . socket . close ( ) <EOL> def setPacketSize ( self , packetSize ) : <EOL> self . packetSize = packetSize <EOL> def getPacketSize ( self ) : <EOL> return self . packetSize <EOL> def socketSendall ( self , data ) : <EOL> if self . tlsSocket is None : <EOL> return self . socket . sendall ( data ) <EOL> else : <EOL> self . tlsSocket . sendall ( data ) <EOL> dd = self . tlsSocket . bio_read ( self . packetSize ) <EOL> return self . socket . sendall ( dd ) <EOL> def sendTDS ( self , packetType , data , packetID = <NUM_LIT:1> ) : <EOL> if ( len ( data ) - <NUM_LIT:8> ) > self . packetSize : <EOL> remaining = data [ self . packetSize - <NUM_LIT:8> : ] <EOL> tds = TDSPacket ( ) <EOL> tds [ '<STR_LIT>' ] = packetType <EOL> tds [ '<STR_LIT>' ] = TDS_STATUS_NORMAL <EOL> tds [ '<STR_LIT>' ] = packetID <EOL> tds [ '<STR_LIT>' ] = data [ : self . packetSize - <NUM_LIT:8> ] <EOL> self . socketSendall ( str ( tds ) ) <EOL> while len ( remaining ) > ( self . packetSize - <NUM_LIT:8> ) : <EOL> packetID += <NUM_LIT:1> <EOL> tds [ '<STR_LIT>' ] = packetID <EOL> tds [ '<STR_LIT>' ] = remaining [ : self . packetSize - <NUM_LIT:8> ] <EOL> self . socketSendall ( str ( tds ) ) <EOL> remaining = remaining [ self . packetSize - <NUM_LIT:8> : ] <EOL> data = remaining <EOL> packetID += <NUM_LIT:1> <EOL> tds = TDSPacket ( ) <EOL> tds [ '<STR_LIT>' ] = packetType <EOL> tds [ '<STR_LIT>' ] = TDS_STATUS_EOM <EOL> tds [ '<STR_LIT>' ] = packetID <EOL> tds [ '<STR_LIT>' ] = data <EOL> self . socketSendall ( str ( tds ) ) <EOL> def socketRecv ( self , packetSize ) : <EOL> data = self . socket . recv ( packetSize ) <EOL> if self . tlsSocket is not None : <EOL> dd = '<STR_LIT>' <EOL> self . tlsSocket . bio_write ( data ) <EOL> while True : <EOL> try : <EOL> dd += self . tlsSocket . read ( packetSize ) <EOL> except SSL . WantReadError : <EOL> data2 = self . socket . recv ( packetSize - len ( data ) ) <EOL> self . tlsSocket . bio_write ( data2 ) <EOL> pass <EOL> else : <EOL> data = dd <EOL> break <EOL> return data <EOL> def recvTDS ( self , packetSize = None ) : <EOL> if packetSize is None : <EOL> packetSize = self . packetSize <EOL> packet = TDSPacket ( self . socketRecv ( packetSize ) ) <EOL> status = packet [ '<STR_LIT>' ] <EOL> packetLen = packet [ '<STR_LIT>' ] - <NUM_LIT:8> <EOL> while packetLen > len ( packet [ '<STR_LIT>' ] ) : <EOL> data = self . socketRecv ( packetSize ) <EOL> packet [ '<STR_LIT>' ] += data <EOL> remaining = None <EOL> if packetLen < len ( packet [ '<STR_LIT>' ] ) : <EOL> remaining = packet [ '<STR_LIT>' ] [ packetLen : ] <EOL> packet [ '<STR_LIT>' ] = packet [ '<STR_LIT>' ] [ : packetLen ] <EOL> while status != TDS_STATUS_EOM : <EOL> if remaining is not None : <EOL> tmpPacket = TDSPacket ( remaining ) <EOL> else : <EOL> tmpPacket = TDSPacket ( self . socketRecv ( packetSize ) ) <EOL> packetLen = tmpPacket [ '<STR_LIT>' ] - <NUM_LIT:8> <EOL> while packetLen > len ( tmpPacket [ '<STR_LIT>' ] ) : <EOL> data = self . socketRecv ( packetSize ) <EOL> tmpPacket [ '<STR_LIT>' ] += data <EOL> remaining = None <EOL> if packetLen < len ( tmpPacket [ '<STR_LIT>' ] ) : <EOL> remaining = tmpPacket [ '<STR_LIT>' ] [ packetLen : ] <EOL> tmpPacket [ '<STR_LIT>' ] = tmpPacket [ '<STR_LIT>' ] [ : packetLen ] <EOL> status = tmpPacket [ '<STR_LIT>' ] <EOL> packet [ '<STR_LIT>' ] += tmpPacket [ '<STR_LIT>' ] <EOL> packet [ '<STR_LIT>' ] += tmpPacket [ '<STR_LIT>' ] - <NUM_LIT:8> <EOL> return packet <EOL> def kerberosLogin ( self , database , username , password = '<STR_LIT>' , domain = '<STR_LIT>' , hashes = None , aesKey = '<STR_LIT>' , kdcHost = None , TGT = None , TGS = None , useCache = True ) : <EOL> if hashes is not None : <EOL> lmhash , nthash = hashes . split ( '<STR_LIT::>' ) <EOL> lmhash = binascii . a2b_hex ( lmhash ) <EOL> nthash = binascii . a2b_hex ( nthash ) <EOL> else : <EOL> lmhash = '<STR_LIT>' <EOL> nthash = '<STR_LIT>' <EOL> resp = self . preLogin ( ) <EOL> if resp [ '<STR_LIT>' ] == TDS_ENCRYPT_REQ or resp [ '<STR_LIT>' ] == TDS_ENCRYPT_OFF : <EOL> LOG . info ( "<STR_LIT>" ) <EOL> ctx = SSL . Context ( SSL . TLSv1_METHOD ) <EOL> ctx . set_cipher_list ( '<STR_LIT>' ) <EOL> tls = SSL . Connection ( ctx , None ) <EOL> tls . set_connect_state ( ) <EOL> while True : <EOL> try : <EOL> tls . do_handshake ( ) <EOL> except SSL . WantReadError : <EOL> data = tls . bio_read ( <NUM_LIT> ) <EOL> self . sendTDS ( TDS_PRE_LOGIN , data , <NUM_LIT:0> ) <EOL> tds = self . recvTDS ( ) <EOL> tls . bio_write ( tds [ '<STR_LIT>' ] ) <EOL> else : <EOL> break <EOL> self . packetSize = <NUM_LIT:16> * <NUM_LIT> - <NUM_LIT:1> <EOL> self . tlsSocket = tls <EOL> login = TDS_LOGIN ( ) <EOL> login [ '<STR_LIT>' ] = ( '<STR_LIT>' . join ( [ random . choice ( string . letters ) for _ in range ( <NUM_LIT:8> ) ] ) ) . encode ( '<STR_LIT>' ) <EOL> login [ '<STR_LIT>' ] = ( '<STR_LIT>' . join ( [ random . choice ( string . letters ) for _ in range ( <NUM_LIT:8> ) ] ) ) . encode ( '<STR_LIT>' ) <EOL> login [ '<STR_LIT>' ] = self . server . encode ( '<STR_LIT>' ) <EOL> login [ '<STR_LIT>' ] = login [ '<STR_LIT>' ] <EOL> login [ '<STR_LIT>' ] = random . randint ( <NUM_LIT:0> , <NUM_LIT> ) <EOL> login [ '<STR_LIT>' ] = self . packetSize <EOL> if database is not None : <EOL> login [ '<STR_LIT>' ] = database . encode ( '<STR_LIT>' ) <EOL> login [ '<STR_LIT>' ] = TDS_INIT_LANG_FATAL | TDS_ODBC_ON <EOL> from impacket . spnego import SPNEGO_NegTokenInit , TypesMech <EOL> from impacket . krb5 . asn1 import AP_REQ , Authenticator , TGS_REP , seq_set <EOL> from impacket . krb5 . kerberosv5 import getKerberosTGT , getKerberosTGS , KerberosError <EOL> from impacket . krb5 import constants <EOL> from impacket . krb5 . types import Principal , KerberosTime , Ticket <EOL> from pyasn1 . codec . der import decoder , encoder <EOL> from impacket . krb5 . ccache import CCache <EOL> import os <EOL> import datetime <EOL> if useCache is True : <EOL> try : <EOL> ccache = CCache . loadFile ( os . getenv ( '<STR_LIT>' ) ) <EOL> except : <EOL> pass <EOL> else : <EOL> if user == '<STR_LIT>' and len ( ccache . principal . components ) > <NUM_LIT:0> : <EOL> user = ccache . principal . components [ <NUM_LIT:0> ] [ '<STR_LIT:data>' ] <EOL> if domain == '<STR_LIT>' : <EOL> domain = ccache . principal . realm [ '<STR_LIT:data>' ] <EOL> LOG . debug ( "<STR_LIT>" % os . getenv ( '<STR_LIT>' ) ) <EOL> principal = '<STR_LIT>' % ( self . server , domain , self . port ) <EOL> creds = ccache . getCredential ( principal ) <EOL> if creds is None : <EOL> principal = '<STR_LIT>' % ( domain . upper ( ) , domain . upper ( ) ) <EOL> creds = ccache . getCredential ( principal ) <EOL> if creds is not None : <EOL> TGT = creds . toTGT ( ) <EOL> LOG . debug ( '<STR_LIT>' ) <EOL> else : <EOL> LOG . debug ( "<STR_LIT>" ) <EOL> else : <EOL> TGS = creds . toTGS ( ) <EOL> LOG . debug ( '<STR_LIT>' ) <EOL> userName = Principal ( username , type = constants . PrincipalNameType . NT_PRINCIPAL . value ) <EOL> while True : <EOL> if TGT is None : <EOL> if TGS is None : <EOL> try : <EOL> tgt , cipher , oldSessionKey , sessionKey = getKerberosTGT ( userName , password , domain , lmhash , nthash , aesKey , kdcHost ) <EOL> except KerberosError , e : <EOL> if e . getErrorCode ( ) == constants . ErrorCodes . KDC_ERR_ETYPE_NOSUPP . value : <EOL> if lmhash is '<STR_LIT>' and nthash is '<STR_LIT>' and ( aesKey is '<STR_LIT>' or aesKey is None ) and TGT is None and TGS is None : <EOL> from impacket . ntlm import compute_lmhash , compute_nthash <EOL> LOG . debug ( '<STR_LIT>' ) <EOL> lmhash = compute_lmhash ( password ) <EOL> nthash = compute_nthash ( password ) <EOL> continue <EOL> else : <EOL> raise <EOL> else : <EOL> raise <EOL> else : <EOL> tgt = TGT [ '<STR_LIT>' ] <EOL> cipher = TGT [ '<STR_LIT>' ] <EOL> sessionKey = TGT [ '<STR_LIT>' ] <EOL> if TGS is None : <EOL> serverName = Principal ( '<STR_LIT>' % ( self . server , domain , self . port ) , type = constants . PrincipalNameType . NT_SRV_INST . value ) <EOL> try : <EOL> tgs , cipher , oldSessionKey , sessionKey = getKerberosTGS ( serverName , domain , kdcHost , tgt , cipher , sessionKey ) <EOL> except KerberosError , e : <EOL> if e . getErrorCode ( ) == constants . ErrorCodes . KDC_ERR_ETYPE_NOSUPP . value : <EOL> if lmhash is '<STR_LIT>' and nthash is '<STR_LIT>' and ( aesKey is '<STR_LIT>' or aesKey is None ) and TGT is None and TGS is None : <EOL> from impacket . ntlm import compute_lmhash , compute_nthash <EOL> LOG . debug ( '<STR_LIT>' ) <EOL> lmhash = compute_lmhash ( password ) <EOL> nthash = compute_nthash ( password ) <EOL> else : <EOL> raise <EOL> else : <EOL> raise <EOL> else : <EOL> break <EOL> else : <EOL> tgs = TGS [ '<STR_LIT>' ] <EOL> cipher = TGS [ '<STR_LIT>' ] <EOL> sessionKey = TGS [ '<STR_LIT>' ] <EOL> break <EOL> blob = SPNEGO_NegTokenInit ( ) <EOL> blob [ '<STR_LIT>' ] = [ TypesMech [ '<STR_LIT>' ] ] <EOL> tgs = decoder . decode ( tgs , asn1Spec = TGS_REP ( ) ) [ <NUM_LIT:0> ] <EOL> ticket = Ticket ( ) <EOL> ticket . from_asn1 ( tgs [ '<STR_LIT>' ] ) <EOL> apReq = AP_REQ ( ) <EOL> apReq [ '<STR_LIT>' ] = <NUM_LIT:5> <EOL> apReq [ '<STR_LIT>' ] = int ( constants . ApplicationTagNumbers . AP_REQ . value ) <EOL> opts = list ( ) <EOL> apReq [ '<STR_LIT>' ] = constants . encodeFlags ( opts ) <EOL> seq_set ( apReq , '<STR_LIT>' , ticket . to_asn1 ) <EOL> authenticator = Authenticator ( ) <EOL> authenticator [ '<STR_LIT>' ] = <NUM_LIT:5> <EOL> authenticator [ '<STR_LIT>' ] = domain <EOL> seq_set ( authenticator , '<STR_LIT>' , userName . components_to_asn1 ) <EOL> now = datetime . datetime . utcnow ( ) <EOL> authenticator [ '<STR_LIT>' ] = now . microsecond <EOL> authenticator [ '<STR_LIT>' ] = KerberosTime . to_asn1 ( now ) <EOL> encodedAuthenticator = encoder . encode ( authenticator ) <EOL> encryptedEncodedAuthenticator = cipher . encrypt ( sessionKey , <NUM_LIT:11> , encodedAuthenticator , None ) <EOL> apReq [ '<STR_LIT>' ] = None <EOL> apReq [ '<STR_LIT>' ] [ '<STR_LIT>' ] = cipher . enctype <EOL> apReq [ '<STR_LIT>' ] [ '<STR_LIT>' ] = encryptedEncodedAuthenticator <EOL> blob [ '<STR_LIT>' ] = encoder . encode ( apReq ) <EOL> login [ '<STR_LIT>' ] |= TDS_INTEGRATED_SECURITY_ON <EOL> login [ '<STR_LIT>' ] = blob . getData ( ) <EOL> login [ '<STR_LIT>' ] = len ( str ( login ) ) <EOL> self . sendTDS ( TDS_LOGIN7 , str ( login ) ) <EOL> if resp [ '<STR_LIT>' ] == TDS_ENCRYPT_OFF : <EOL> self . tlsSocket = None <EOL> tds = self . recvTDS ( ) <EOL> self . replies = self . parseReply ( tds [ '<STR_LIT>' ] ) <EOL> if self . replies . has_key ( TDS_LOGINACK_TOKEN ) : <EOL> return True <EOL> else : <EOL> return False <EOL> def login ( self , database , username , password = '<STR_LIT>' , domain = '<STR_LIT>' , hashes = None , useWindowsAuth = False ) : <EOL> if hashes is not None : <EOL> lmhash , nthash = hashes . split ( '<STR_LIT::>' ) <EOL> lmhash = binascii . a2b_hex ( lmhash ) <EOL> nthash = binascii . a2b_hex ( nthash ) <EOL> else : <EOL> lmhash = '<STR_LIT>' <EOL> nthash = '<STR_LIT>' <EOL> resp = self . preLogin ( ) <EOL> if resp [ '<STR_LIT>' ] == TDS_ENCRYPT_REQ or resp [ '<STR_LIT>' ] == TDS_ENCRYPT_OFF : <EOL> LOG . info ( "<STR_LIT>" ) <EOL> ctx = SSL . Context ( SSL . TLSv1_METHOD ) <EOL> ctx . set_cipher_list ( '<STR_LIT>' ) <EOL> tls = SSL . Connection ( ctx , None ) <EOL> tls . set_connect_state ( ) <EOL> while True : <EOL> try : <EOL> tls . do_handshake ( ) <EOL> except SSL . WantReadError : <EOL> data = tls . bio_read ( <NUM_LIT> ) <EOL> self . sendTDS ( TDS_PRE_LOGIN , data , <NUM_LIT:0> ) <EOL> tds = self . recvTDS ( ) <EOL> tls . bio_write ( tds [ '<STR_LIT>' ] ) <EOL> else : <EOL> break <EOL> self . packetSize = <NUM_LIT:16> * <NUM_LIT> - <NUM_LIT:1> <EOL> self . tlsSocket = tls <EOL> login = TDS_LOGIN ( ) <EOL> login [ '<STR_LIT>' ] = ( '<STR_LIT>' . join ( [ random . choice ( string . letters ) for i in range ( <NUM_LIT:8> ) ] ) ) . encode ( '<STR_LIT>' ) <EOL> login [ '<STR_LIT>' ] = ( '<STR_LIT>' . join ( [ random . choice ( string . letters ) for i in range ( <NUM_LIT:8> ) ] ) ) . encode ( '<STR_LIT>' ) <EOL> login [ '<STR_LIT>' ] = self . server . encode ( '<STR_LIT>' ) <EOL> login [ '<STR_LIT>' ] = login [ '<STR_LIT>' ] <EOL> login [ '<STR_LIT>' ] = random . randint ( <NUM_LIT:0> , <NUM_LIT> ) <EOL> login [ '<STR_LIT>' ] = self . packetSize <EOL> if database is not None : <EOL> login [ '<STR_LIT>' ] = database . encode ( '<STR_LIT>' ) <EOL> login [ '<STR_LIT>' ] = TDS_INIT_LANG_FATAL | TDS_ODBC_ON <EOL> if useWindowsAuth is True : <EOL> login [ '<STR_LIT>' ] |= TDS_INTEGRATED_SECURITY_ON <EOL> auth = ntlm . getNTLMSSPType1 ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> login [ '<STR_LIT>' ] = str ( auth ) <EOL> else : <EOL> login [ '<STR_LIT>' ] = username . encode ( '<STR_LIT>' ) <EOL> login [ '<STR_LIT>' ] = self . encryptPassword ( password . encode ( '<STR_LIT>' ) ) <EOL> login [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> login [ '<STR_LIT>' ] = len ( str ( login ) ) <EOL> self . sendTDS ( TDS_LOGIN7 , str ( login ) ) <EOL> if resp [ '<STR_LIT>' ] == TDS_ENCRYPT_OFF : <EOL> self . tlsSocket = None <EOL> tds = self . recvTDS ( ) <EOL> if useWindowsAuth is True : <EOL> serverChallenge = tds [ '<STR_LIT>' ] [ <NUM_LIT:3> : ] <EOL> type3 , exportedSessionKey = ntlm . getNTLMSSPType3 ( auth , serverChallenge , username , password , domain , lmhash , nthash ) <EOL> self . sendTDS ( TDS_SSPI , str ( type3 ) ) <EOL> tds = self . recvTDS ( ) <EOL> self . replies = self . parseReply ( tds [ '<STR_LIT>' ] ) <EOL> if self . replies . has_key ( TDS_LOGINACK_TOKEN ) : <EOL> return True <EOL> else : <EOL> return False <EOL> def processColMeta ( self ) : <EOL> for col in self . colMeta : <EOL> if col [ '<STR_LIT>' ] in [ TDS_NVARCHARTYPE , TDS_NCHARTYPE , TDS_NTEXTTYPE ] : <EOL> col [ '<STR_LIT>' ] = col [ '<STR_LIT>' ] / <NUM_LIT:2> <EOL> fmt = '<STR_LIT>' <EOL> elif col [ '<STR_LIT>' ] in [ TDS_GUIDTYPE ] : <EOL> col [ '<STR_LIT>' ] = <NUM_LIT> <EOL> fmt = '<STR_LIT>' <EOL> elif col [ '<STR_LIT>' ] in [ TDS_DECIMALNTYPE , TDS_NUMERICNTYPE ] : <EOL> col [ '<STR_LIT>' ] = ord ( col [ '<STR_LIT>' ] [ <NUM_LIT:0> ] ) <EOL> fmt = '<STR_LIT>' <EOL> elif col [ '<STR_LIT>' ] in [ TDS_DATETIMNTYPE ] : <EOL> col [ '<STR_LIT>' ] = <NUM_LIT> <EOL> fmt = '<STR_LIT>' <EOL> elif col [ '<STR_LIT>' ] in [ TDS_INT4TYPE , TDS_INTNTYPE ] : <EOL> col [ '<STR_LIT>' ] = <NUM_LIT:11> <EOL> fmt = '<STR_LIT>' <EOL> elif col [ '<STR_LIT>' ] in [ TDS_FLTNTYPE , TDS_MONEYNTYPE ] : <EOL> col [ '<STR_LIT>' ] = <NUM_LIT> <EOL> fmt = '<STR_LIT>' <EOL> elif col [ '<STR_LIT>' ] in [ TDS_BITNTYPE , TDS_BIGCHARTYPE ] : <EOL> col [ '<STR_LIT>' ] = col [ '<STR_LIT>' ] <EOL> fmt = '<STR_LIT>' <EOL> elif col [ '<STR_LIT>' ] in [ TDS_BIGBINARYTYPE , TDS_BIGVARBINTYPE ] : <EOL> col [ '<STR_LIT>' ] = col [ '<STR_LIT>' ] * <NUM_LIT:2> <EOL> fmt = '<STR_LIT>' <EOL> elif col [ '<STR_LIT>' ] in [ TDS_TEXTTYPE , TDS_BIGVARCHRTYPE ] : <EOL> col [ '<STR_LIT>' ] = col [ '<STR_LIT>' ] <EOL> fmt = '<STR_LIT>' <EOL> else : <EOL> col [ '<STR_LIT>' ] = <NUM_LIT:10> <EOL> fmt = '<STR_LIT>' <EOL> if len ( col [ '<STR_LIT:Name>' ] ) > col [ '<STR_LIT>' ] : <EOL> col [ '<STR_LIT>' ] = len ( col [ '<STR_LIT:Name>' ] ) <EOL> elif col [ '<STR_LIT>' ] > self . MAX_COL_LEN : <EOL> col [ '<STR_LIT>' ] = self . MAX_COL_LEN <EOL> col [ '<STR_LIT>' ] = fmt % col [ '<STR_LIT>' ] <EOL> def printColumnsHeader ( self ) : <EOL> if len ( self . colMeta ) == <NUM_LIT:0> : <EOL> return <EOL> for col in self . colMeta : <EOL> self . __rowsPrinter . logMessage ( col [ '<STR_LIT>' ] % col [ '<STR_LIT:Name>' ] + self . COL_SEPARATOR + '<STR_LIT:\n>' ) <EOL> for col in self . colMeta : <EOL> self . __rowsPrinter . logMessage ( '<STR_LIT:->' * col [ '<STR_LIT>' ] + self . COL_SEPARATOR + '<STR_LIT:\n>' ) <EOL> def printRows ( self ) : <EOL> if self . lastError is True : <EOL> return <EOL> self . processColMeta ( ) <EOL> self . printColumnsHeader ( ) <EOL> for row in self . rows : <EOL> for col in self . colMeta : <EOL> self . __rowsPrinter . logMessage ( col [ '<STR_LIT>' ] % row [ col [ '<STR_LIT:Name>' ] ] + self . COL_SEPARATOR ) <EOL> def printReplies ( self ) : <EOL> for keys in self . replies . keys ( ) : <EOL> for i , key in enumerate ( self . replies [ keys ] ) : <EOL> if key [ '<STR_LIT>' ] == TDS_ERROR_TOKEN : <EOL> error = "<STR_LIT>" % ( key [ '<STR_LIT>' ] . decode ( '<STR_LIT>' ) , key [ '<STR_LIT>' ] , key [ '<STR_LIT>' ] . decode ( '<STR_LIT>' ) ) <EOL> self . lastError = SQLErrorException ( "<STR_LIT>" % ( key [ '<STR_LIT>' ] , key [ '<STR_LIT>' ] . decode ( '<STR_LIT>' ) ) ) <EOL> LOG . error ( error ) <EOL> elif key [ '<STR_LIT>' ] == TDS_INFO_TOKEN : <EOL> LOG . info ( "<STR_LIT>" % ( key [ '<STR_LIT>' ] . decode ( '<STR_LIT>' ) , key [ '<STR_LIT>' ] , key [ '<STR_LIT>' ] . decode ( '<STR_LIT>' ) ) ) <EOL> elif key [ '<STR_LIT>' ] == TDS_LOGINACK_TOKEN : <EOL> LOG . info ( "<STR_LIT>" % ( key [ '<STR_LIT>' ] , key [ '<STR_LIT>' ] . decode ( '<STR_LIT>' ) , key [ '<STR_LIT>' ] , key [ '<STR_LIT>' ] , key [ '<STR_LIT>' ] , key [ '<STR_LIT>' ] ) ) <EOL> elif key [ '<STR_LIT>' ] == TDS_ENVCHANGE_TOKEN : <EOL> if key [ '<STR_LIT>' ] in ( TDS_ENVCHANGE_DATABASE , TDS_ENVCHANGE_LANGUAGE , TDS_ENVCHANGE_CHARSET , TDS_ENVCHANGE_PACKETSIZE ) : <EOL> record = TDS_ENVCHANGE_VARCHAR ( key [ '<STR_LIT>' ] ) <EOL> if record [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> record [ '<STR_LIT>' ] = '<STR_LIT:None>' . encode ( '<STR_LIT>' ) <EOL> elif record [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> record [ '<STR_LIT>' ] = '<STR_LIT:None>' . encode ( '<STR_LIT>' ) <EOL> if key [ '<STR_LIT>' ] == TDS_ENVCHANGE_DATABASE : <EOL> _type = '<STR_LIT>' <EOL> elif key [ '<STR_LIT>' ] == TDS_ENVCHANGE_LANGUAGE : <EOL> _type = '<STR_LIT>' <EOL> elif key [ '<STR_LIT>' ] == TDS_ENVCHANGE_CHARSET : <EOL> _type = '<STR_LIT>' <EOL> elif key [ '<STR_LIT>' ] == TDS_ENVCHANGE_PACKETSIZE : <EOL> _type = '<STR_LIT>' <EOL> else : <EOL> _type = "<STR_LIT>" % key [ '<STR_LIT>' ] <EOL> LOG . info ( "<STR_LIT>" % ( _type , record [ '<STR_LIT>' ] . decode ( '<STR_LIT>' ) , record [ '<STR_LIT>' ] . decode ( '<STR_LIT>' ) ) ) <EOL> def parseRow ( self , token , tuplemode = False ) : <EOL> if len ( token ) == <NUM_LIT:1> : <EOL> return <NUM_LIT:0> <EOL> row = [ ] if tuplemode else { } <EOL> origDataLen = len ( token [ '<STR_LIT>' ] ) <EOL> data = token [ '<STR_LIT>' ] <EOL> for col in self . colMeta : <EOL> _type = col [ '<STR_LIT>' ] <EOL> if ( _type == TDS_NVARCHARTYPE ) | ( _type == TDS_NCHARTYPE ) : <EOL> charLen = struct . unpack ( '<STR_LIT>' , data [ : struct . calcsize ( '<STR_LIT>' ) ] ) [ <NUM_LIT:0> ] <EOL> data = data [ struct . calcsize ( '<STR_LIT>' ) : ] <EOL> if charLen != <NUM_LIT> : <EOL> value = data [ : charLen ] . decode ( '<STR_LIT>' ) <EOL> data = data [ charLen : ] <EOL> else : <EOL> value = '<STR_LIT>' <EOL> elif _type == TDS_BIGVARCHRTYPE : <EOL> charLen = struct . unpack ( '<STR_LIT>' , data [ : struct . calcsize ( '<STR_LIT>' ) ] ) [ <NUM_LIT:0> ] <EOL> data = data [ struct . calcsize ( '<STR_LIT>' ) : ] <EOL> if charLen != <NUM_LIT> : <EOL> value = data [ : charLen ] <EOL> data = data [ charLen : ] <EOL> else : <EOL> value = '<STR_LIT>' <EOL> elif _type == TDS_GUIDTYPE : <EOL> uuidLen = ord ( data [ <NUM_LIT:0> ] ) <EOL> data = data [ <NUM_LIT:1> : ] <EOL> if uuidLen > <NUM_LIT:0> : <EOL> uu = data [ : uuidLen ] <EOL> value = uuid . bin_to_string ( uu ) <EOL> data = data [ uuidLen : ] <EOL> else : <EOL> value = '<STR_LIT>' <EOL> elif ( _type == TDS_NTEXTTYPE ) | ( _type == TDS_IMAGETYPE ) : <EOL> charLen = ord ( data [ <NUM_LIT:0> ] ) <EOL> if charLen == <NUM_LIT:0> : <EOL> value = '<STR_LIT>' <EOL> data = data [ <NUM_LIT:1> : ] <EOL> else : <EOL> data = data [ <NUM_LIT:1> + charLen + <NUM_LIT:8> : ] <EOL> charLen = struct . unpack ( '<STR_LIT>' , data [ : struct . calcsize ( '<STR_LIT>' ) ] ) [ <NUM_LIT:0> ] <EOL> data = data [ struct . calcsize ( '<STR_LIT>' ) : ] <EOL> if charLen != <NUM_LIT> : <EOL> if _type == TDS_NTEXTTYPE : <EOL> value = data [ : charLen ] . decode ( '<STR_LIT>' ) <EOL> else : <EOL> value = binascii . b2a_hex ( data [ : charLen ] ) <EOL> data = data [ charLen : ] <EOL> else : <EOL> value = '<STR_LIT>' <EOL> elif _type == TDS_TEXTTYPE : <EOL> charLen = ord ( data [ <NUM_LIT:0> ] ) <EOL> if charLen == <NUM_LIT:0> : <EOL> value = '<STR_LIT>' <EOL> data = data [ <NUM_LIT:1> : ] <EOL> else : <EOL> data = data [ <NUM_LIT:1> + charLen + <NUM_LIT:8> : ] <EOL> charLen = struct . unpack ( '<STR_LIT>' , data [ : struct . calcsize ( '<STR_LIT>' ) ] ) [ <NUM_LIT:0> ] <EOL> data = data [ struct . calcsize ( '<STR_LIT>' ) : ] <EOL> if charLen != <NUM_LIT> : <EOL> value = data [ : charLen ] <EOL> data = data [ charLen : ] <EOL> else : <EOL> value = '<STR_LIT>' <EOL> elif ( _type == TDS_BIGVARBINTYPE ) | ( _type == TDS_BIGBINARYTYPE ) : <EOL> charLen = struct . unpack ( '<STR_LIT>' , data [ : struct . calcsize ( '<STR_LIT>' ) ] ) [ <NUM_LIT:0> ] <EOL> data = data [ struct . calcsize ( '<STR_LIT>' ) : ] <EOL> if charLen != <NUM_LIT> : <EOL> value = binascii . b2a_hex ( data [ : charLen ] ) <EOL> data = data [ charLen : ] <EOL> else : <EOL> value = '<STR_LIT>' <EOL> elif ( _type == TDS_DATETIM4TYPE ) | ( _type == TDS_DATETIMNTYPE ) | ( _type == TDS_DATETIMETYPE ) : <EOL> value = '<STR_LIT>' <EOL> if _type == TDS_DATETIMNTYPE : <EOL> if ord ( data [ <NUM_LIT:0> ] ) == <NUM_LIT:4> : <EOL> _type = TDS_DATETIM4TYPE <EOL> elif ord ( data [ <NUM_LIT:0> ] ) == <NUM_LIT:8> : <EOL> _type = TDS_DATETIMETYPE <EOL> else : <EOL> value = '<STR_LIT>' <EOL> data = data [ <NUM_LIT:1> : ] <EOL> if _type == TDS_DATETIMETYPE : <EOL> dateValue = struct . unpack ( '<STR_LIT>' , data [ : <NUM_LIT:4> ] ) [ <NUM_LIT:0> ] <EOL> data = data [ <NUM_LIT:4> : ] <EOL> if dateValue < <NUM_LIT:0> : <EOL> baseDate = datetime . date ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> else : <EOL> baseDate = datetime . date ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> timeValue = struct . unpack ( '<STR_LIT>' , data [ : <NUM_LIT:4> ] ) [ <NUM_LIT:0> ] <EOL> data = data [ <NUM_LIT:4> : ] <EOL> elif _type == TDS_DATETIM4TYPE : <EOL> dateValue = struct . unpack ( '<STR_LIT>' , data [ : struct . calcsize ( '<STR_LIT>' ) ] ) [ <NUM_LIT:0> ] <EOL> data = data [ struct . calcsize ( '<STR_LIT>' ) : ] <EOL> timeValue = struct . unpack ( '<STR_LIT>' , data [ : struct . calcsize ( '<STR_LIT>' ) ] ) [ <NUM_LIT:0> ] <EOL> data = data [ struct . calcsize ( '<STR_LIT>' ) : ] <EOL> baseDate = datetime . date ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> if value != '<STR_LIT>' : <EOL> dateValue = datetime . date . fromordinal ( baseDate . toordinal ( ) + dateValue ) <EOL> hours , mod = divmod ( timeValue / <NUM_LIT> , <NUM_LIT> * <NUM_LIT> ) <EOL> minutes , second = divmod ( mod , <NUM_LIT> ) <EOL> value = datetime . datetime ( dateValue . year , dateValue . month , dateValue . day , hours , minutes , second ) <EOL> elif ( _type == TDS_INT4TYPE ) | ( _type == TDS_MONEY4TYPE ) | ( _type == TDS_FLT4TYPE ) : <EOL> value = struct . unpack ( '<STR_LIT>' , data [ : struct . calcsize ( '<STR_LIT>' ) ] ) [ <NUM_LIT:0> ] <EOL> data = data [ struct . calcsize ( '<STR_LIT>' ) : ] <EOL> elif _type == TDS_FLTNTYPE : <EOL> valueSize = ord ( data [ : <NUM_LIT:1> ] ) <EOL> if valueSize == <NUM_LIT:4> : <EOL> fmt = '<STR_LIT>' <EOL> elif valueSize == <NUM_LIT:8> : <EOL> fmt = '<STR_LIT>' <EOL> data = data [ <NUM_LIT:1> : ] <EOL> if valueSize > <NUM_LIT:0> : <EOL> value = struct . unpack ( fmt , data [ : valueSize ] ) [ <NUM_LIT:0> ] <EOL> data = data [ valueSize : ] <EOL> else : <EOL> value = '<STR_LIT>' <EOL> elif _type == TDS_MONEYNTYPE : <EOL> valueSize = ord ( data [ : <NUM_LIT:1> ] ) <EOL> if valueSize == <NUM_LIT:4> : <EOL> fmt = '<STR_LIT>' <EOL> elif valueSize == <NUM_LIT:8> : <EOL> fmt = '<STR_LIT>' <EOL> data = data [ <NUM_LIT:1> : ] <EOL> if valueSize > <NUM_LIT:0> : <EOL> value = struct . unpack ( fmt , data [ : valueSize ] ) [ <NUM_LIT:0> ] <EOL> if valueSize == <NUM_LIT:4> : <EOL> value = float ( value ) / math . pow ( <NUM_LIT:10> , <NUM_LIT:4> ) <EOL> else : <EOL> value = float ( value >> <NUM_LIT:32> ) / math . pow ( <NUM_LIT:10> , <NUM_LIT:4> ) <EOL> data = data [ valueSize : ] <EOL> else : <EOL> value = '<STR_LIT>' <EOL> elif _type == TDS_BIGCHARTYPE : <EOL> charLen = struct . unpack ( '<STR_LIT>' , data [ : struct . calcsize ( '<STR_LIT>' ) ] ) [ <NUM_LIT:0> ] <EOL> data = data [ struct . calcsize ( '<STR_LIT>' ) : ] <EOL> value = data [ : charLen ] <EOL> data = data [ charLen : ] <EOL> elif ( _type == TDS_INT8TYPE ) | ( _type == TDS_FLT8TYPE ) | ( _type == TDS_MONEYTYPE ) : <EOL> value = struct . unpack ( '<STR_LIT>' , data [ : struct . calcsize ( '<STR_LIT>' ) ] ) [ <NUM_LIT:0> ] <EOL> data = data [ struct . calcsize ( '<STR_LIT>' ) : ] <EOL> elif _type == TDS_INT2TYPE : <EOL> value = struct . unpack ( '<STR_LIT>' , ( data [ : <NUM_LIT:2> ] ) ) [ <NUM_LIT:0> ] <EOL> data = data [ <NUM_LIT:2> : ] <EOL> elif _type == TDS_DATENTYPE : <EOL> valueSize = ord ( data [ : <NUM_LIT:1> ] ) <EOL> data = data [ <NUM_LIT:1> : ] <EOL> if valueSize > <NUM_LIT:0> : <EOL> dateBytes = data [ : valueSize ] <EOL> dateValue = struct . unpack ( '<STR_LIT>' , '<STR_LIT:\x00>' + dateBytes ) [ <NUM_LIT:0> ] <EOL> value = datetime . date . fromtimestamp ( dateValue ) <EOL> data = data [ valueSize : ] <EOL> else : <EOL> value = '<STR_LIT>' <EOL> elif ( _type == TDS_BITTYPE ) | ( _type == TDS_INT1TYPE ) : <EOL> value = ord ( data [ : <NUM_LIT:1> ] ) <EOL> data = data [ <NUM_LIT:1> : ] <EOL> elif ( _type == TDS_NUMERICNTYPE ) | ( _type == TDS_DECIMALNTYPE ) : <EOL> valueLen = ord ( data [ : <NUM_LIT:1> ] ) <EOL> data = data [ <NUM_LIT:1> : ] <EOL> value = data [ : valueLen ] <EOL> data = data [ valueLen : ] <EOL> precision = ord ( col [ '<STR_LIT>' ] [ <NUM_LIT:1> ] ) <EOL> scale = ord ( col [ '<STR_LIT>' ] [ <NUM_LIT:2> ] ) <EOL> if valueLen > <NUM_LIT:0> : <EOL> isPositiveSign = ord ( value [ <NUM_LIT:0> ] ) <EOL> if ( valueLen - <NUM_LIT:1> ) == <NUM_LIT:2> : <EOL> fmt = '<STR_LIT>' <EOL> elif ( valueLen - <NUM_LIT:1> ) == <NUM_LIT:4> : <EOL> fmt = '<STR_LIT>' <EOL> elif ( valueLen - <NUM_LIT:1> ) == <NUM_LIT:8> : <EOL> fmt = '<STR_LIT>' <EOL> else : <EOL> value = "<STR_LIT>" <EOL> number = struct . unpack ( fmt , value [ <NUM_LIT:1> : ] ) [ <NUM_LIT:0> ] <EOL> number /= math . pow ( precision , scale ) <EOL> if isPositiveSign == <NUM_LIT:0> : <EOL> number *= - <NUM_LIT:1> <EOL> value = number <EOL> else : <EOL> value = '<STR_LIT>' <EOL> elif _type == TDS_BITNTYPE : <EOL> valueSize = ord ( data [ : <NUM_LIT:1> ] ) <EOL> data = data [ <NUM_LIT:1> : ] <EOL> if valueSize > <NUM_LIT:0> : <EOL> if valueSize == <NUM_LIT:1> : <EOL> value = ord ( data [ : valueSize ] ) <EOL> else : <EOL> value = data [ : valueSize ] <EOL> else : <EOL> value = '<STR_LIT>' <EOL> data = data [ valueSize : ] <EOL> elif _type == TDS_INTNTYPE : <EOL> valueSize = ord ( data [ : <NUM_LIT:1> ] ) <EOL> if valueSize == <NUM_LIT:1> : <EOL> fmt = '<STR_LIT>' <EOL> elif valueSize == <NUM_LIT:2> : <EOL> fmt = '<STR_LIT>' <EOL> elif valueSize == <NUM_LIT:4> : <EOL> fmt = '<STR_LIT>' <EOL> elif valueSize == <NUM_LIT:8> : <EOL> fmt = '<STR_LIT>' <EOL> else : <EOL> fmt = '<STR_LIT>' <EOL> data = data [ <NUM_LIT:1> : ] <EOL> if valueSize > <NUM_LIT:0> : <EOL> value = struct . unpack ( fmt , data [ : valueSize ] ) [ <NUM_LIT:0> ] <EOL> data = data [ valueSize : ] <EOL> else : <EOL> value = '<STR_LIT>' <EOL> elif _type == TDS_SSVARIANTTYPE : <EOL> LOG . critical ( "<STR_LIT>" ) <EOL> raise <EOL> else : <EOL> LOG . critical ( "<STR_LIT>" % _type ) <EOL> raise <EOL> if tuplemode : <EOL> row . append ( value ) <EOL> else : <EOL> row [ col [ '<STR_LIT:Name>' ] ] = value <EOL> self . rows . append ( row ) <EOL> return origDataLen - len ( data ) <EOL> def parseColMetaData ( self , token ) : <EOL> count = token [ '<STR_LIT>' ] <EOL> if count == <NUM_LIT> : <EOL> return <NUM_LIT:0> <EOL> self . colMeta = [ ] <EOL> origDataLen = len ( token [ '<STR_LIT>' ] ) <EOL> data = token [ '<STR_LIT>' ] <EOL> for i in range ( count ) : <EOL> column = { } <EOL> userType = struct . unpack ( '<STR_LIT>' , data [ : struct . calcsize ( '<STR_LIT>' ) ] ) [ <NUM_LIT:0> ] <EOL> data = data [ struct . calcsize ( '<STR_LIT>' ) : ] <EOL> flags = struct . unpack ( '<STR_LIT>' , data [ : struct . calcsize ( '<STR_LIT>' ) ] ) [ <NUM_LIT:0> ] <EOL> data = data [ struct . calcsize ( '<STR_LIT>' ) : ] <EOL> colType = struct . unpack ( '<STR_LIT>' , data [ : struct . calcsize ( '<STR_LIT>' ) ] ) [ <NUM_LIT:0> ] <EOL> data = data [ struct . calcsize ( '<STR_LIT>' ) : ] <EOL> if ( colType == TDS_BITTYPE ) | ( colType == TDS_INT1TYPE ) | ( colType == TDS_INT2TYPE ) | ( colType == TDS_INT8TYPE ) | ( colType == TDS_DATETIMETYPE ) | ( colType == TDS_DATETIM4TYPE ) | ( colType == TDS_FLT4TYPE ) | ( colType == TDS_FLT8TYPE ) | ( colType == TDS_MONEYTYPE ) | ( colType == TDS_MONEY4TYPE ) | ( colType == TDS_DATENTYPE ) | ( colType == TDS_INT4TYPE ) : <EOL> typeData = '<STR_LIT>' <EOL> elif ( colType == TDS_INTNTYPE ) | ( colType == TDS_TIMENTYPE ) | ( colType == TDS_DATETIME2NTYPE ) | ( colType == TDS_DATETIMEOFFSETNTYPE ) | ( colType == TDS_FLTNTYPE ) | ( colType == TDS_MONEYNTYPE ) | ( colType == TDS_GUIDTYPE ) | ( colType == TDS_BITNTYPE ) : <EOL> typeData = ord ( data [ <NUM_LIT:0> ] ) <EOL> data = data [ <NUM_LIT:1> : ] <EOL> elif colType == TDS_DATETIMNTYPE : <EOL> typeData = ord ( data [ <NUM_LIT:0> ] ) <EOL> data = data [ <NUM_LIT:1> : ] <EOL> elif ( colType == TDS_BIGVARBINTYPE ) | ( colType == TDS_BIGBINARYTYPE ) | ( colType == TDS_NCHARTYPE ) | ( colType == TDS_NVARCHARTYPE ) | ( colType == TDS_BIGVARCHRTYPE ) | ( colType == TDS_BIGCHARTYPE ) : <EOL> typeData = struct . unpack ( '<STR_LIT>' , data [ : <NUM_LIT:2> ] ) [ <NUM_LIT:0> ] <EOL> data = data [ <NUM_LIT:2> : ] <EOL> elif ( colType == TDS_DECIMALNTYPE ) | ( colType == TDS_NUMERICNTYPE ) | ( colType == TDS_DECIMALTYPE ) : <EOL> typeData = data [ : <NUM_LIT:3> ] <EOL> data = data [ <NUM_LIT:3> : ] <EOL> elif ( colType == TDS_IMAGETYPE ) | ( colType == TDS_TEXTTYPE ) | ( colType == TDS_XMLTYPE ) | ( colType == TDS_SSVARIANTTYPE ) | ( colType == TDS_NTEXTTYPE ) : <EOL> typeData = struct . unpack ( '<STR_LIT>' , data [ : <NUM_LIT:4> ] ) [ <NUM_LIT:0> ] <EOL> data = data [ <NUM_LIT:4> : ] <EOL> else : <EOL> LOG . critical ( "<STR_LIT>" % colType ) <EOL> raise <EOL> if ( colType == TDS_NTEXTTYPE ) | ( colType == TDS_BIGCHARTYPE ) | ( colType == TDS_BIGVARCHRTYPE ) | ( colType == TDS_NCHARTYPE ) | ( colType == TDS_NVARCHARTYPE ) | ( colType == TDS_TEXTTYPE ) : <EOL> data = data [ <NUM_LIT:5> : ] <EOL> if ( colType == TDS_IMAGETYPE ) | ( colType == TDS_TEXTTYPE ) | ( colType == TDS_NTEXTTYPE ) : <EOL> dataLen = struct . unpack ( '<STR_LIT>' , data [ : <NUM_LIT:2> ] ) [ <NUM_LIT:0> ] <EOL> data = data [ <NUM_LIT:2> : ] <EOL> data = data [ dataLen * <NUM_LIT:2> : ] <EOL> colNameLength = struct . unpack ( '<STR_LIT>' , data [ : struct . calcsize ( '<STR_LIT>' ) ] ) [ <NUM_LIT:0> ] <EOL> data = data [ struct . calcsize ( '<STR_LIT>' ) : ] <EOL> colName = data [ : colNameLength * <NUM_LIT:2> ] . decode ( '<STR_LIT>' ) <EOL> data = data [ colNameLength * <NUM_LIT:2> : ] <EOL> column [ '<STR_LIT:Name>' ] = colName <EOL> column [ '<STR_LIT>' ] = colType <EOL> column [ '<STR_LIT>' ] = typeData <EOL> column [ '<STR_LIT>' ] = flags <EOL> self . colMeta . append ( column ) <EOL> return origDataLen - len ( data ) <EOL> def parseReply ( self , tokens , tuplemode = False ) : <EOL> if len ( tokens ) == <NUM_LIT:0> : <EOL> return False <EOL> replies = { } <EOL> while len ( tokens ) > <NUM_LIT:0> : <EOL> tokenID = struct . unpack ( '<STR_LIT:B>' , tokens [ <NUM_LIT:0> ] ) [ <NUM_LIT:0> ] <EOL> if tokenID == TDS_ERROR_TOKEN : <EOL> token = TDS_INFO_ERROR ( tokens ) <EOL> elif tokenID == TDS_RETURNSTATUS_TOKEN : <EOL> token = TDS_RETURNSTATUS ( tokens ) <EOL> elif tokenID == TDS_INFO_TOKEN : <EOL> token = TDS_INFO_ERROR ( tokens ) <EOL> elif tokenID == TDS_LOGINACK_TOKEN : <EOL> token = TDS_LOGIN_ACK ( tokens ) <EOL> elif tokenID == TDS_ENVCHANGE_TOKEN : <EOL> token = TDS_ENVCHANGE ( tokens ) <EOL> if token [ '<STR_LIT>' ] is TDS_ENVCHANGE_PACKETSIZE : <EOL> record = TDS_ENVCHANGE_VARCHAR ( token [ '<STR_LIT>' ] ) <EOL> self . packetSize = string . atoi ( record [ '<STR_LIT>' ] . decode ( '<STR_LIT>' ) ) <EOL> elif token [ '<STR_LIT>' ] is TDS_ENVCHANGE_DATABASE : <EOL> record = TDS_ENVCHANGE_VARCHAR ( token [ '<STR_LIT>' ] ) <EOL> self . currentDB = record [ '<STR_LIT>' ] . decode ( '<STR_LIT>' ) <EOL> elif ( tokenID == TDS_DONEINPROC_TOKEN ) | ( tokenID == TDS_DONEPROC_TOKEN ) : <EOL> token = TDS_DONEINPROC ( tokens ) <EOL> elif tokenID == TDS_ORDER_TOKEN : <EOL> token = TDS_ORDER ( tokens ) <EOL> elif tokenID == TDS_ROW_TOKEN : <EOL> token = TDS_ROW ( tokens ) <EOL> tokenLen = self . parseRow ( token , tuplemode ) <EOL> token [ '<STR_LIT>' ] = token [ '<STR_LIT>' ] [ : tokenLen ] <EOL> elif tokenID == TDS_COLMETADATA_TOKEN : <EOL> token = TDS_COLMETADATA ( tokens ) <EOL> tokenLen = self . parseColMetaData ( token ) <EOL> token [ '<STR_LIT>' ] = token [ '<STR_LIT>' ] [ : tokenLen ] <EOL> elif tokenID == TDS_DONE_TOKEN : <EOL> token = TDS_DONE ( tokens ) <EOL> else : <EOL> LOG . error ( "<STR_LIT>" % tokenID ) <EOL> return replies <EOL> if replies . has_key ( tokenID ) is not True : <EOL> replies [ tokenID ] = list ( ) <EOL> replies [ tokenID ] . append ( token ) <EOL> tokens = tokens [ len ( token ) : ] <EOL> return replies <EOL> def batch ( self , cmd , tuplemode = False , wait = True ) : <EOL> self . rows = [ ] <EOL> self . colMeta = [ ] <EOL> self . lastError = False <EOL> self . sendTDS ( TDS_SQL_BATCH , ( cmd + '<STR_LIT:\r\n>' ) . encode ( '<STR_LIT>' ) ) <EOL> if wait : <EOL> tds = self . recvTDS ( ) <EOL> self . replies = self . parseReply ( tds [ '<STR_LIT>' ] , tuplemode ) <EOL> return self . rows <EOL> else : <EOL> return True <EOL> def batchStatement ( self , cmd , tuplemode = False ) : <EOL> self . rows = [ ] <EOL> self . colMeta = [ ] <EOL> self . lastError = False <EOL> self . sendTDS ( TDS_SQL_BATCH , ( cmd + '<STR_LIT:\r\n>' ) . encode ( '<STR_LIT>' ) ) <EOL> sql_query = batch <EOL> def changeDB ( self , db ) : <EOL> if db != self . currentDB : <EOL> chdb = '<STR_LIT>' % db <EOL> self . batch ( chdb ) <EOL> self . printReplies ( ) <EOL> def RunSQLQuery ( self , db , sql_query , tuplemode = False , wait = True , ** kwArgs ) : <EOL> db = db or '<STR_LIT>' <EOL> self . changeDB ( db ) <EOL> self . printReplies ( ) <EOL> ret = self . batch ( sql_query , tuplemode , wait ) <EOL> if wait : <EOL> self . printReplies ( ) <EOL> if self . lastError : <EOL> raise self . lastError <EOL> if self . lastError : <EOL> raise self . lastError <EOL> return ret <EOL> def RunSQLStatement ( self , db , sql_query , wait = True , ** kwArgs ) : <EOL> self . RunSQLQuery ( db , sql_query , wait = wait ) <EOL> if self . lastError : <EOL> raise self . lastError <EOL> return True </s>
