Esempio n. 1
0
class MSSQLDumpDateToText(DumpToText):

    adapt(DateDomain, TextDomain)

    def __call__(self):
        self.format("SUBSTRING(CONVERT(VARCHAR, {base}, 21), 1, 10)",
                    base=self.base)
Esempio n. 2
0
class EmitSPSS(Emit):
    adapt(SPSSFormat)

    def __call__(self):
        product = to_spss(self.meta.domain, [self.meta])
        output = StringIO()
        self.render(output, product)
        yield output.getvalue()

    def render(self, stream, product):
        output_file, output_path = tempfile.mkstemp(suffix='.sav')
        try:
            sav_config = product.sav_config(self.data)
            writer_kwargs = {
                'savFileName': output_path,
                'varNames': sav_config['var_names'],
                'varTypes': sav_config['var_types'],
                'formats': sav_config['formats'],
                'columnWidths': sav_config['column_widths'],
                'ioUtf8': True
            }

            with CustomSavWriter(**writer_kwargs) as writer:
                for record in product.cells(self.data):
                    writer.writerow(record)

            with open(output_path, 'rb') as output_file:
                while True:
                    chunk = output_file.read(1024 * 1024)
                    if chunk:
                        stream.write(chunk)
                    else:
                        break
        finally:
            os.remove(output_path)
Esempio n. 3
0
class MSSQLDumpTextToTime(DumpToTime):

    adapt(TextDomain, TimeDomain)

    def __call__(self):
        self.format("CAST(CAST('1900-01-01 ' + {base} AS DATETIME) AS FLOAT)",
                    base=self.base)
Esempio n. 4
0
class MSSQLDumpDateTimeToDate(DumpToDate):

    adapt(DateTimeDomain, DateDomain)

    def __call__(self):
        self.format("CAST(FLOOR(CAST({base} AS FLOAT)) AS DATETIME)",
                    base=self.base)
Esempio n. 5
0
class UnscrambleSQLiteTime(Unscramble):

    adapt(TimeDomain)

    @staticmethod
    def convert(value):
        if value is None:
            return None
        if isinstance(value, datetime.time):
            return value
        if not isinstance(value, (str, unicode)):
            raise Error("Expected a time value, got", repr(value))
        # FIXME: verify that the value is in valid format.
        hour, minute, second = value.split(':')
        hour = int(hour)
        minute = int(minute)
        if '.' in second:
            second, microsecond = second.split('.')
            second = int(second)
            microsecond = int(microsecond)
        else:
            second = int(second)
            microsecond = 0
        value = datetime.time(hour, minute, second, microsecond)
        return value
Esempio n. 6
0
class MSSQLDumpDateTimeToTime(DumpToTime):

    adapt(DateTimeDomain, TimeDomain)

    def __call__(self):
        self.format("(CAST({base} AS FLOAT) - FLOOR(CAST({base} AS FLOAT)))",
                    base=self.base)
Esempio n. 7
0
class OracleDumpDateTimeToText(DumpToText):

    adapt(DateTimeDomain, TextDomain)

    def __call__(self):
        self.format("TO_CHAR({base}, 'YYYY-MM-DD HH24:MI:SS.FF')",
                    base=self.base)
Esempio n. 8
0
class OracleDumpTextToDateTime(DumpToDateTime):

    adapt(TextDomain, DateTimeDomain)

    def __call__(self):
        self.format("TO_TIMESTAMP({base}, 'YYYY-MM-DD HH24:MI:SS')",
                    base=self.base)
Esempio n. 9
0
class OracleRewriteLiteral(Rewrite):

    adapt(LiteralCode)

    def __call__(self):
        if isinstance(self.code.domain, TextDomain) and self.code.value == "":
            return self.code.clone(value=None)
        return self.code
Esempio n. 10
0
class UnscrambleMySQLEnum(Unscramble):

    adapt(EnumDomain)

    def convert(self, value):
        if value not in self.domain.labels:
            value = None
        return value
Esempio n. 11
0
class DecimalToExcel(ToExcel):
    adapt(DecimalDomain)

    def cells(self, value):
        if value is None or not value.is_finite():
            yield [None]
        else:
            yield [value]
Esempio n. 12
0
class FloatToExcel(ToExcel):
    adapt(FloatDomain)

    def cells(self, value):
        if value is None or math.isinf(value) or math.isnan(value):
            yield [None]
        else:
            yield [value]
Esempio n. 13
0
File: dump.py Progetto: sirex/htsql
class SQLiteDumpBooleanToText(SQLiteDumpToText):

    adapt(BooleanDomain, TextDomain)

    def __call__(self):
        self.format(
            "(CASE WHEN {base} THEN 'true'"
            " WHEN NOT {base} THEN 'false' END)",
            base=self.base)
Esempio n. 14
0
class UnscrambleMSSQLText(Unscramble):

    adapt(TextDomain)

    @staticmethod
    def convert(value):
        if isinstance(value, str):
            value = value.decode('utf-8')
        return value
Esempio n. 15
0
class MSSQLDumpTimeToText(DumpToText):

    adapt(TimeDomain, TextDomain)

    def __call__(self):
        self.format(
            "SUBSTRING(CONVERT(VARCHAR, CAST({base} AS DATETIME), 21),"
            " 12, 12)",
            base=self.base)
Esempio n. 16
0
class UnscrambleOracleDecimal(Unscramble):

    adapt(DecimalDomain)

    @staticmethod
    def convert(value):
        if value is None:
            return None
        return decimal.Decimal(value)
Esempio n. 17
0
class UnscramblePGSQLEnum(Unscramble):

    adapt(EnumDomain)

    @staticmethod
    def convert(value):
        if isinstance(value, str):
            value = value.decode('utf-8')
        return value
Esempio n. 18
0
class UnscrambleMSSQLBoolean(Unscramble):

    adapt(BooleanDomain)

    @staticmethod
    def convert(value):
        if value is None:
            return None
        return (value != 0)
Esempio n. 19
0
class OracleDumpTimeToText(DumpToText):

    adapt(TimeDomain, TextDomain)

    def __call__(self):
        self.format(
            "TO_CHAR(TIMESTAMP '2001-01-01 00:00:00' + {base},"
            " 'HH24:MI:SS.FF')",
            base=self.base)
Esempio n. 20
0
class UnscrambleOracleTime(Unscramble):

    adapt(TimeDomain)

    @staticmethod
    def convert(value):
        if isinstance(value, datetime.timedelta):
            assert not value.days
            value = (datetime.datetime(2001, 1, 1) + value).time()
        return value
Esempio n. 21
0
class UnscrambleMSSQLDate(Unscramble):

    adapt(DateDomain)

    @staticmethod
    def convert(value):
        if isinstance(value, datetime.datetime):
            assert not value.time()
            value = value.date()
        return value
Esempio n. 22
0
class UnscrambleMySQLEnum(Unscramble):

    adapt(EnumDomain)

    def convert(self, value):
        if isinstance(value, str):
            value = value.decode('utf-8')
        if value not in self.domain.labels:
            value = None
        return value
Esempio n. 23
0
class UnscrambleSQLiteText(Unscramble):

    adapt(TextDomain)

    @staticmethod
    def convert(value):
        if value is None:
            return None
        if not isinstance(value, str):
            raise Error("Expected a text value, got", repr(value))
        return value
Esempio n. 24
0
class UnscrambleSQLiteFloat(Unscramble):

    adapt(FloatDomain)

    @staticmethod
    def convert(value):
        if value is None:
            return None
        if isinstance(value, float):
            return value
        raise Error("Expected a float value, got", repr(value))
Esempio n. 25
0
class UnscrambleSQLiteInteger(Unscramble):

    adapt(IntegerDomain)

    @staticmethod
    def convert(value):
        if value is None:
            return None
        if isinstance(value, (int, long)):
            return value
        raise Error("Expected an integer value, got", repr(value))
Esempio n. 26
0
class UnscrambleSQLiteDecimal(Unscramble):

    adapt(DecimalDomain)

    @staticmethod
    def convert(value):
        if value is None:
            return None
        if isinstance(value, float):
            return decimal.Decimal(str(value))
        raise Error("Expected a decimal value, got", repr(value))
Esempio n. 27
0
class VoidToExcel(ToExcel):
    adapt(VoidDomain)

    def __init__(self, domain, profiles):
        super(VoidToExcel, self).__init__(domain, profiles)
        self.width = 0

    def headers(self):
        return []

    def cells(self, value):
        yield []
Esempio n. 28
0
class UnscrambleMSSQLTime(Unscramble):

    adapt(TimeDomain)

    @staticmethod
    def convert(value):
        if isinstance(value, float):
            assert 0.0 <= value < 1.0
            value = int(86400000000 * value) * datetime.timedelta(0, 0, 1)
            assert not value.days
            value = (datetime.datetime(2001, 1, 1) + value).time()
        return value
Esempio n. 29
0
class UnscrambleMySQLTime(Unscramble):

    adapt(TimeDomain)

    @staticmethod
    def convert(value):
        if isinstance(value, datetime.timedelta):
            if value.days != 0:
                value = None
            else:
                value = (datetime.datetime(2001, 1, 1) + value).time()
        return value
Esempio n. 30
0
class OracleDumpBooleanToText(DumpToText):

    adapt(BooleanDomain, TextDomain)

    def __call__(self):
        if self.base.is_nullable:
            self.format(
                "(CASE WHEN {base} <> 0 THEN 'true'"
                " WHEN NOT {base} = 0 THEN 'false' END)",
                base=self.base)
        else:
            self.format("(CASE WHEN {base} <> 0 THEN 'true' ELSE 'false' END)",
                        base=self.base)