Exemplo n.º 1
0
def DateToTicks(value):
    """Converts a Date object to ticks."""
    timeStruct = Date(value.year, value.month, value.day).timetuple()
    try:
        return int(time.mktime(timeStruct))
    except:
        raise DataError("Year out of range")
Exemplo n.º 2
0
    def getClob(self):
        """Read the next Clob(Character Large OBject) value off the session."""
        typeCode = self._getTypeCode()

        if typeCode in range(protocol.CLOBLEN0, protocol.CLOBLEN4 + 1):
            strLength = fromByteString(self._takeBytes(typeCode - 194))
            return self._takeBytes(strLength)

        raise DataError('Not a clob')
Exemplo n.º 3
0
    def getBlob(self):
        """Read the next Blob(Binary Large OBject) value off the session."""
        typeCode = self._getTypeCode()

        if typeCode in range(protocol.BLOBLEN0, protocol.BLOBLEN4 + 1):
            strLength = fromByteString(self._takeBytes(typeCode - 189))
            return datatype.Binary(self._takeBytes(strLength))

        raise DataError('Not a blob')
Exemplo n.º 4
0
    def getBoolean(self):
        """Read the next Boolean value off the session."""
        typeCode = self._getTypeCode()

        if typeCode == protocol.TRUE:
            return True
        if typeCode == protocol.FALSE:
            return False

        raise DataError('Not a boolean')
Exemplo n.º 5
0
    def getScaledInt(self):
        """Read the next Scaled Integer value off the session."""
        typeCode = self._getTypeCode()

        if typeCode in range(protocol.SCALEDLEN0, protocol.SCALEDLEN8 + 1):
            scale = fromByteString(self._takeBytes(1))
            value = fromSignedByteString(self._takeBytes(typeCode - 60))
            return decimal.Decimal(value) / decimal.Decimal(10**scale)

        raise DataError('Not a scaled integer')
Exemplo n.º 6
0
    def getScaledDate(self):
        """Read the next Scaled Date value off the session."""
        typeCode = self._getTypeCode()

        if typeCode in range(protocol.SCALEDDATELEN1,
                             protocol.SCALEDDATELEN8 + 1):
            scale = fromByteString(self._takeBytes(1))
            date = fromSignedByteString(self._takeBytes(typeCode - 200))
            return datatype.DateFromTicks(round(date / 10.0**scale))

        raise DataError('Not a scaled date')
Exemplo n.º 7
0
    def getInt(self):
        """Read the next Integer value off the session."""
        typeCode = self._getTypeCode()

        if typeCode in range(protocol.INTMINUS10, protocol.INT31 + 1):
            return typeCode - 20

        elif typeCode in range(protocol.INTLEN1, protocol.INTLEN8 + 1):
            return fromSignedByteString(self._takeBytes(typeCode - 51))

        raise DataError('Not an integer')
Exemplo n.º 8
0
    def getUUID(self):
        """Read the next UUID value off the session."""
        if self._getTypeCode() == protocol.UUID:
            return uuid.UUID(bytes=self._takeBytes(16))
        if self._getTypeCode() == protocol.SCALEDCOUNT1:
            # before version 11
            pass
        if self._getTypeCode() == protocol.SCALEDCOUNT2:
            # version 11 and later
            pass

        raise DataError('Not a UUID')
Exemplo n.º 9
0
    def getOpaque(self):
        """Read the next Opaque value off the session."""
        typeCode = self._getTypeCode()

        if typeCode in range(protocol.OPAQUELEN0, protocol.OPAQUELEN39 + 1):
            return datatype.Binary(self._takeBytes(typeCode - 149))

        if typeCode in range(protocol.OPAQUECOUNT1, protocol.OPAQUECOUNT4 + 1):
            strLength = fromByteString(self._takeBytes(typeCode - 72))
            return datatype.Binary(self._takeBytes(strLength))

        raise DataError('Not an opaque value')
Exemplo n.º 10
0
    def getString(self):
        """Read the next String off the session."""
        typeCode = self._getTypeCode()

        if typeCode in range(protocol.UTF8LEN0, protocol.UTF8LEN39 + 1):
            return self._takeBytes(typeCode - 109)

        if typeCode in range(protocol.UTF8COUNT1, protocol.UTF8COUNT4 + 1):
            strLength = fromByteString(self._takeBytes(typeCode - 68))
            return self._takeBytes(strLength)

        raise DataError('Not a string')
Exemplo n.º 11
0
    def getScaledTime(self):
        """Read the next Scaled Time value off the session."""
        typeCode = self._getTypeCode()

        if typeCode in range(protocol.SCALEDTIMELEN1,
                             protocol.SCALEDTIMELEN8 + 1):
            scale = fromByteString(self._takeBytes(1))
            time = fromByteString(self._takeBytes(typeCode - 208))
            ticks = decimal.Decimal(str(time)) / decimal.Decimal(10**scale)
            return datatype.TimeFromTicks(
                round(int(ticks)), int((ticks % 1) * decimal.Decimal(1000000)))

        raise DataError('Not a scaled time')
Exemplo n.º 12
0
    def getTime(self):
        """Read the next Time value off the session."""
        typeCode = self._getTypeCode()

        if typeCode in range(protocol.MILLISECLEN0, protocol.MILLISECLEN8 + 1):
            return fromSignedByteString(self._takeBytes(typeCode - 86))

        if typeCode in range(protocol.NANOSECLEN0, protocol.NANOSECLEN8 + 1):
            return fromSignedByteString(self._takeBytes(typeCode - 95))

        if typeCode in range(protocol.TIMELEN0, protocol.TIMELEN4 + 1):
            return fromByteString(self._takeBytes(typeCode - 104))

        raise DataError('Not a time')
Exemplo n.º 13
0
    def getDouble(self):
        """Read the next Double off the session."""
        typeCode = self._getTypeCode()

        if typeCode == protocol.DOUBLELEN0:
            return 0.0

        if typeCode in range(protocol.DOUBLELEN0 + 1, protocol.DOUBLELEN8 + 1):
            test = self._takeBytes(typeCode - 77)
            if typeCode < protocol.DOUBLELEN8:
                for i in xrange(0, protocol.DOUBLELEN8 - typeCode):
                    test = test + chr(0)
            return struct.unpack('!d', test)[0]

        raise DataError('Not a double')
Exemplo n.º 14
0
def TimestampToTicks(value):
    """Converts a Timestamp object to ticks."""
    timeStruct = Timestamp(value.year, value.month, value.day, value.hour,
                           value.minute, value.second).timetuple()
    try:
        if value.microsecond:
            micro = decimal.Decimal(
                value.microsecond) / decimal.Decimal(1000000)
            return (int(
                (decimal.Decimal(int(time.mktime(timeStruct))) + micro) *
                decimal.Decimal(int(10**(len(str(micro)) - 2)))),
                    len(str(micro)) - 2)
        else:
            return (int(time.mktime(timeStruct)), 0)
    except:
        raise DataError("Year out of range")
Exemplo n.º 15
0
def TypeObjectFromNuodb(nuodb_type_name):
    """TODO:  this is very fragile.  The driver should map type numbers not typenames."""
    """Returns one of STRING, BINARY, NUMBER, DATETIME, ROWID based on the 
    supplied NuoDB column type name
    """

    if nuodb_type_name == "<null>":
        return None

    elif nuodb_type_name == "string":
        return STRING

    elif nuodb_type_name == "char":
        return STRING

    elif nuodb_type_name == "varchar":
        return STRING

    elif nuodb_type_name == "smallint":
        return NUMBER

    elif nuodb_type_name == "integer":
        return NUMBER

    elif nuodb_type_name == "bigint":
        return NUMBER

    elif nuodb_type_name == "float":
        return NUMBER

    elif nuodb_type_name == "double":
        return NUMBER

    elif nuodb_type_name == "date":
        return DATETIME

    elif nuodb_type_name == "timestamp":
        return DATETIME

    elif nuodb_type_name == "time":
        return DATETIME

    elif nuodb_type_name == "clob":
        return BINARY

    elif nuodb_type_name == "blob":
        return BINARY

    elif nuodb_type_name == "numeric":
        return NUMBER

    elif nuodb_type_name == "number":
        return NUMBER

    elif nuodb_type_name == "bytes":
        return BINARY

    elif nuodb_type_name == "binarystring":
        return BINARY

    elif nuodb_type_name == "binaryvaryingstring":
        return BINARY

    elif nuodb_type_name == "boolean":
        #TODO: Not sure about this?
        return NUMBER

    elif nuodb_type_name == "binary":
        return BINARY

    else:
        raise DataError('received unknown column type (%s) from the database' %
                        nuodb_type_name)
Exemplo n.º 16
0
 def getNull(self):
     """Read the next Null value off the session."""
     if self._getTypeCode() != protocol.NULL:
         raise DataError('Not null')