def params_to_blr(self, params): "Convert parameter array to BLR and values format." ln = len(params) * 2 blr = bytes([5, 2, 4, 0, ln & 255, ln >> 8]) values = bytes([]) for p in params: t = type(p) if (PYTHON_MAJOR_VER == 2 and t == str) or (PYTHON_MAJOR_VER == 3 and t == bytes): v = p nbytes = len(v) pad_length = (4 - nbytes) & 3 v += bytes([0]) * pad_length blr += bytes([14, nbytes & 255, nbytes >> 8]) elif t == int: v = bint_to_bytes(p, 4) blr += bytes([7, 0]) elif t == decimal.Decimal or t == float: if t == float: p = decimal.Decimal(str(p)) (sign, digits, exponent) = p.as_tuple() v = 0 ln = len(digits) for i in range(ln): v += digits[i] * (10 ** (ln - i - 1)) if sign: v *= -1 v = bint_to_bytes(v, 8) if exponent < 0: exponent += 256 blr += bytes([16, exponent]) elif t == datetime.date: v = convert_date(p) blr += bytes([12]) elif t == datetime.time: v = convert_time(p) blr += bytes([13]) elif t == datetime.datetime: v = convert_timestamp(p) blr += bytes([35]) elif p == None: v = bytes([0]) * 8 blr += bytes([9, 0]) values += v blr += bytes([7, 0]) values += bytes([0]) * 4 if p != None else bytes([0xFF, 0xFF, 0x34, 0x8C]) blr += bytes([255, 76]) # [blr_end, blr_eoc] return blr, values
def convert_date(v): # Convert datetime.date to BLR format data i = v.month + 9 jy = v.year + (i // 12) -1 jm = i % 12 c = jy // 100 jy -= 100 * c j = (146097*c) // 4 + (1461*jy) // 4 + (153*jm+2) // 5 + v.day - 678882 return bint_to_bytes(j, 4)
def convert_time(v): # Convert datetime.time to BLR format time t = (v.hour*3600 + v.minute*60 + v.second) *10000 + v.microsecond // 100 return bint_to_bytes(t, 4)