oracle.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. # Licensed to the Apache Software Foundation (ASF) under one
  2. # or more contributor license agreements. See the NOTICE file
  3. # distributed with this work for additional information
  4. # regarding copyright ownership. The ASF licenses this file
  5. # to you under the Apache License, Version 2.0 (the
  6. # "License"); you may not use this file except in compliance
  7. # with the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing,
  12. # software distributed under the License is distributed on an
  13. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. # KIND, either express or implied. See the License for the
  15. # specific language governing permissions and limitations
  16. # under the License.
  17. from datetime import datetime
  18. from typing import Optional
  19. from superset.db_engine_specs.base import LimitMethod
  20. from superset.db_engine_specs.postgres import PostgresBaseEngineSpec
  21. class OracleEngineSpec(PostgresBaseEngineSpec):
  22. engine = "oracle"
  23. limit_method = LimitMethod.WRAP_SQL
  24. force_column_alias_quotes = True
  25. max_column_name_length = 30
  26. _time_grain_functions = {
  27. None: "{col}",
  28. "PT1S": "CAST({col} as DATE)",
  29. "PT1M": "TRUNC(CAST({col} as DATE), 'MI')",
  30. "PT1H": "TRUNC(CAST({col} as DATE), 'HH')",
  31. "P1D": "TRUNC(CAST({col} as DATE), 'DDD')",
  32. "P1W": "TRUNC(CAST({col} as DATE), 'WW')",
  33. "P1M": "TRUNC(CAST({col} as DATE), 'MONTH')",
  34. "P0.25Y": "TRUNC(CAST({col} as DATE), 'Q')",
  35. "P1Y": "TRUNC(CAST({col} as DATE), 'YEAR')",
  36. }
  37. @classmethod
  38. def convert_dttm(cls, target_type: str, dttm: datetime) -> Optional[str]:
  39. tt = target_type.upper()
  40. if tt == "DATE":
  41. return f"TO_DATE('{dttm.date().isoformat()}', 'YYYY-MM-DD')"
  42. if tt == "TIMESTAMP":
  43. return f"""TO_TIMESTAMP('{dttm.isoformat(timespec="microseconds")}', 'YYYY-MM-DD"T"HH24:MI:SS.ff6')""" # pylint: disable=line-too-long
  44. return None