Skip to content

Commit

Permalink
refactor: sql lab command: separate concerns into different modules (a…
Browse files Browse the repository at this point in the history
…pache#16917)

* chore move sql_execution_context to sqllab package

* add new helper methods into base Dao

* refactor separate get existing query concern from command

* refactor separate query access validation concern

* refactor separate get query's database concern from command

* refactor separate get query rendering concern from command

* refactor sqllab_execution_context

* refactor separate creating payload for view

* chore decouple command from superset app

* fix pylint issues

* fix failed tests

* fix pylint issues

* fix failed test

* fix failed black

* fix failed black

* fix failed test
  • Loading branch information
ofekisr committed Oct 3, 2021
1 parent f0060a6 commit 0d0c759
Show file tree
Hide file tree
Showing 15 changed files with 660 additions and 285 deletions.
1 change: 1 addition & 0 deletions superset/charts/dao.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
# pylint: disable=arguments-renamed
import logging
from typing import List, Optional, TYPE_CHECKING

Expand Down
35 changes: 35 additions & 0 deletions superset/dao/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
# pylint: disable=isinstance-second-argument-not-valid-type
from typing import Any, Dict, List, Optional, Type

from flask_appbuilder.models.filters import BaseFilter
Expand Down Expand Up @@ -89,6 +90,19 @@ def find_all(cls) -> List[Model]:
).apply(query, None)
return query.all()

@classmethod
def find_one_or_none(cls, **filter_by: Any) -> Optional[Model]:
"""
Get the first that fit the `base_filter`
"""
query = db.session.query(cls.model_cls)
if cls.base_filter:
data_model = SQLAInterface(cls.model_cls, db.session)
query = cls.base_filter( # pylint: disable=not-callable
"id", data_model
).apply(query, None)
return query.filter_by(**filter_by).one_or_none()

@classmethod
def create(cls, properties: Dict[str, Any], commit: bool = True) -> Model:
"""
Expand All @@ -109,6 +123,27 @@ def create(cls, properties: Dict[str, Any], commit: bool = True) -> Model:
raise DAOCreateFailedError(exception=ex) from ex
return model

@classmethod
def save(cls, instance_model: Model, commit: bool = True) -> Model:
"""
Generic for saving models
:raises: DAOCreateFailedError
"""
if cls.model_cls is None:
raise DAOConfigError()
if not isinstance(instance_model, cls.model_cls):
raise DAOCreateFailedError(
"the instance model is not a type of the model class"
)
try:
db.session.add(instance_model)
if commit:
db.session.commit()
except SQLAlchemyError as ex: # pragma: no cover
db.session.rollback()
raise DAOCreateFailedError(exception=ex) from ex
return instance_model

@classmethod
def update(
cls, model: Model, properties: Dict[str, Any], commit: bool = True
Expand Down
2 changes: 1 addition & 1 deletion superset/datasets/dao.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def validate_metrics_uniqueness(dataset_id: int, metrics_names: List[str]) -> bo
return len(dataset_query) == 0

@classmethod
def update( # pylint: disable=arguments-differ
def update(
cls, model: SqlaTable, properties: Dict[str, Any], commit: bool = True
) -> Optional[SqlaTable]:
"""
Expand Down
3 changes: 2 additions & 1 deletion superset/models/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
# pylint: disable=line-too-long
"""A collection of ORM sqlalchemy models for Superset"""
import enum
import json
Expand Down Expand Up @@ -253,7 +254,7 @@ def parameters(self) -> Dict[str, Any]:
@property
def parameters_schema(self) -> Dict[str, Any]:
try:
parameters_schema = self.db_engine_spec.parameters_json_schema() # type: ignore # pylint: disable=line-too-long
parameters_schema = self.db_engine_spec.parameters_json_schema() # type: ignore
except Exception: # pylint: disable=broad-except
parameters_schema = {}
return parameters_schema
Expand Down
Loading

0 comments on commit 0d0c759

Please sign in to comment.