Esempio n. 1
0
    def dttm_sql_literal(
        self,
        dttm: DateTime,
        time_range_endpoints: Optional[Tuple[utils.TimeRangeEndpoint,
                                             utils.TimeRangeEndpoint]],
    ) -> str:
        """Convert datetime object to a SQL expression string"""
        sql = (self.table.database.db_engine_spec.convert_dttm(
            self.type, dttm) if self.type else None)

        if sql:
            return sql

        tf = self.python_date_format

        # Fallback to the default format (if defined) only if the SIP-15 time range
        # endpoints, i.e., [start, end) are enabled.
        if not tf and time_range_endpoints == (
                utils.TimeRangeEndpoint.INCLUSIVE,
                utils.TimeRangeEndpoint.EXCLUSIVE,
        ):
            tf = (self.table.database.get_extra().get(
                "python_date_format_by_column_name", {}).get(self.column_name))

        if tf:
            if tf in ["epoch_ms", "epoch_s"]:
                seconds_since_epoch = int(dttm.timestamp())
                if tf == "epoch_s":
                    return str(seconds_since_epoch)
                return str(seconds_since_epoch * 1000)
            return f"'{dttm.strftime(tf)}'"

        # TODO(john-bodley): SIP-15 will explicitly require a type conversion.
        return f"""'{dttm.strftime("%Y-%m-%d %H:%M:%S.%f")}'"""
Esempio n. 2
0
 def dttm_sql_literal(self, dttm: DateTime) -> str:
     """Convert datetime object to a SQL expression string"""
     tf = self.python_date_format
     if tf:
         seconds_since_epoch = int(dttm.timestamp())
         if tf == "epoch_s":
             return str(seconds_since_epoch)
         elif tf == "epoch_ms":
             return str(seconds_since_epoch * 1000)
         return "'{}'".format(dttm.strftime(tf))
     else:
         s = self.table.database.db_engine_spec.convert_dttm(self.type or "", dttm)
         return s or "'{}'".format(dttm.strftime("%Y-%m-%d %H:%M:%S.%f"))
    def dttm_sql_literal(self, dttm: DateTime) -> str:
        """Convert datetime object to a SQL expression string"""
        sql = (self.table.database.db_engine_spec.convert_dttm(
            self.type, dttm) if self.type else None)

        if sql:
            return sql

        tf = self.python_date_format

        if tf:
            if tf in ["epoch_ms", "epoch_s"]:
                seconds_since_epoch = int(dttm.timestamp())
                if tf == "epoch_s":
                    return str(seconds_since_epoch)
                return str(seconds_since_epoch * 1000)
            return f"'{dttm.strftime(tf)}'"

        # TODO(john-bodley): SIP-15 will explicitly require a type conversion.
        return f"""'{dttm.strftime("%Y-%m-%d %H:%M:%S.%f")}'"""