sqlite.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 List, Optional, TYPE_CHECKING
  19. from sqlalchemy.engine.reflection import Inspector
  20. from superset.db_engine_specs.base import BaseEngineSpec
  21. from superset.utils import core as utils
  22. if TYPE_CHECKING:
  23. # prevent circular imports
  24. from superset.models.core import Database # pylint: disable=unused-import
  25. class SqliteEngineSpec(BaseEngineSpec):
  26. engine = "sqlite"
  27. _time_grain_functions = {
  28. None: "{col}",
  29. "PT1S": "DATETIME(STRFTIME('%Y-%m-%dT%H:%M:%S', {col}))",
  30. "PT1M": "DATETIME(STRFTIME('%Y-%m-%dT%H:%M:00', {col}))",
  31. "PT1H": "DATETIME(STRFTIME('%Y-%m-%dT%H:00:00', {col}))",
  32. "P1D": "DATE({col})",
  33. "P1W": "DATE({col}, -strftime('%W', {col}) || ' days')",
  34. "P1M": "DATE({col}, -strftime('%d', {col}) || ' days', '+1 day')",
  35. "P1Y": "DATETIME(STRFTIME('%Y-01-01T00:00:00', {col}))",
  36. "P1W/1970-01-03T00:00:00Z": "DATE({col}, 'weekday 6')",
  37. "1969-12-28T00:00:00Z/P1W": "DATE({col}, 'weekday 0', '-7 days')",
  38. }
  39. @classmethod
  40. def epoch_to_dttm(cls) -> str:
  41. return "datetime({col}, 'unixepoch')"
  42. @classmethod
  43. def get_all_datasource_names(
  44. cls, database: "Database", datasource_type: str
  45. ) -> List[utils.DatasourceName]:
  46. schemas = database.get_all_schema_names(
  47. cache=database.schema_cache_enabled,
  48. cache_timeout=database.schema_cache_timeout,
  49. force=True,
  50. )
  51. schema = schemas[0]
  52. if datasource_type == "table":
  53. return database.get_all_table_names_in_schema(
  54. schema=schema,
  55. force=True,
  56. cache=database.table_cache_enabled,
  57. cache_timeout=database.table_cache_timeout,
  58. )
  59. elif datasource_type == "view":
  60. return database.get_all_view_names_in_schema(
  61. schema=schema,
  62. force=True,
  63. cache=database.table_cache_enabled,
  64. cache_timeout=database.table_cache_timeout,
  65. )
  66. else:
  67. raise Exception(f"Unsupported datasource_type: {datasource_type}")
  68. @classmethod
  69. def convert_dttm(cls, target_type: str, dttm: datetime) -> Optional[str]:
  70. if target_type.upper() == "TEXT":
  71. return f"""'{dttm.isoformat(sep=" ", timespec="microseconds")}'"""
  72. return None
  73. @classmethod
  74. def get_table_names(
  75. cls, database: "Database", inspector: Inspector, schema: Optional[str]
  76. ) -> List[str]:
  77. """Need to disregard the schema for Sqlite"""
  78. return sorted(inspector.get_table_names())