Skip to content

# Db base

The tool to check the availability or syntax of domain, IP or URL.

::

    ██████╗ ██╗   ██╗███████╗██╗   ██╗███╗   ██╗ ██████╗███████╗██████╗ ██╗     ███████╗
    ██╔══██╗╚██╗ ██╔╝██╔════╝██║   ██║████╗  ██║██╔════╝██╔════╝██╔══██╗██║     ██╔════╝
    ██████╔╝ ╚████╔╝ █████╗  ██║   ██║██╔██╗ ██║██║     █████╗  ██████╔╝██║     █████╗
    ██╔═══╝   ╚██╔╝  ██╔══╝  ██║   ██║██║╚██╗██║██║     ██╔══╝  ██╔══██╗██║     ██╔══╝
    ██║        ██║   ██║     ╚██████╔╝██║ ╚████║╚██████╗███████╗██████╔╝███████╗███████╗
    ╚═╝        ╚═╝   ╚═╝      ╚═════╝ ╚═╝  ╚═══╝ ╚═════╝╚══════╝╚═════╝ ╚══════╝╚══════╝

Provides the base of all datasets which acts as database interface.

Author: Nissar Chababy, @funilrys, contactTATAfunilrysTODTODcom

Special thanks: https://pyfunceble.github.io/#/special-thanks

Contributors: https://pyfunceble.github.io/#/contributors

Project link: https://github.com/funilrys/PyFunceble

Project documentation: https://docs.pyfunceble.com

Project homepage: https://pyfunceble.github.io/

License: ::

    Copyright 2017, 2018, 2019, 2020, 2022, 2023, 2024, 2025 Nissar Chababy

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        https://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

## `DBDatasetBase`

Bases: `DatasetBase`

Provides the base of all datasets which acts as database interface.

Source code in `PyFunceble/dataset/db_base.py`

     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356

    class DBDatasetBase(DatasetBase):
        """
        Provides the base of all datasets which acts as database interface.
        """

        STD_REMOVE_UNNEEDED_FIELDS: bool = True
        STD_AUTHORIZED: bool = False

        FIELDS: List[str] = []
        COMPARISON_FIELDS: List[str] = []

        source_file: Optional[str] = None

        _remove_unneeded_fields: Optional[bool] = True
        _authorized: Optional[bool] = False

        def __init__(
            self,
            *,
            authorized: Optional[bool] = None,
            remove_unneeded_fields: Optional[bool] = None,
            config_dir: Optional[str] = None,
        ) -> None:
            if config_dir is not None:
                self.config_dir = config_dir
            else:
                self.config_dir = PyFunceble.storage.CONFIG_DIRECTORY

            if authorized is not None:
                self.set_authorized(authorized)

            if remove_unneeded_fields is not None:
                self.set_remove_unneeded_fields(remove_unneeded_fields)

            self.__post_init__()

        def __post_init__(self) -> None:
            """
            A method to be called (automatically) after __init__.
            """

        def execute_if_authorized(default: Any = None):  # pylint: disable=no-self-argument
            """
            Executes the decorated method only if we are authorized to process.
            Otherwise, apply the given :code:`default`.
            """

            def inner_metdhod(func):
                @functools.wraps(func)
                def wrapper(self, *args, **kwargs):
                    if self.authorized:
                        return func(self, *args, **kwargs)  # pylint: disable=not-callable
                    return default

                return wrapper

            return inner_metdhod

        @property
        def config_dir(self) -> Optional[str]:
            """
            Provides the current state of the :code:`_config_dir` attribute.
            """

            return self._config_dir

        @config_dir.setter
        def config_dir(self, value: str) -> None:
            """
            Sets the configuration directory.

            :param value:
                The value to set.

            :raise TypeError:
                When value is not a :py:class:`str`.
            """

            if not isinstance(value, str):
                raise TypeError(f"<value> should be {str}, {type(value)} given.")

            self._config_dir = value

        def set_config_dir(self, value: str) -> "DBDatasetBase":
            """
            Sets the configuration directory.

            :param value:
                The value to set.
            """

            self.config_dir = value

            return self

        @property
        def authorized(self) -> Optional[bool]:
            """
            Provides the current state of the :code:`_authorized` attribute.
            """

            return self._authorized

        @authorized.setter
        def authorized(self, value: bool) -> None:
            """
            Sets the value of the :code:`_authorized` attribute.

            :param value:
                The value to set.

            :raise TypeError:
                When the given :code:`value` is not a :py:class:`bool`
            """

            if not isinstance(value, bool):
                raise TypeError(f"<value> should be {bool}, {type(value)} given.")

            self._authorized = value

        def set_authorized(self, value: bool) -> "DBDatasetBase":
            """
            Sets the value of the :code:`_authorized` attribute.

            :param value:
                The value to set.
            """

            self.authorized = value

            return self

        @property
        def remove_unneeded_fields(self) -> Optional[bool]:
            """
            Provides the current state of the :code:`_remove_unneeded_fields`.
            """

            return self._remove_unneeded_fields

        @remove_unneeded_fields.setter
        def remove_unneeded_fields(self, value: bool) -> None:
            """
            Sets the value of the :code:`_remove_unneeded_fields` attribute.

            :param value:
                The value to set.

            :raise TypeError:
                When the given :code:`value` is not a :py:class:`bool`
            """

            if not isinstance(value, bool):
                raise TypeError(f"<value> should be {bool}, {type(value)} given.")

            self._remove_unneeded_fields = value

        def set_remove_unneeded_fields(self, value: bool) -> "DBDatasetBase":
            """
            Sets the value of the :code:`_remove_unneeded_fields` attribute.

            :param value:
                The value to set.
            """

            self.remove_unneeded_fields = value

            return self

        @execute_if_authorized(dict())  # pylint: disable=use-dict-literal
        def get_filtered_row(self, row: dict) -> dict:
            """
            Removes all unkowns fields (not declared) from the given row.

            :param row:
                The row to work with.
            """

            result = {}

            for key, value in row.items():
                if value is None:
                    value = ""

                if key in self.FIELDS:
                    result[key] = value

            for field in self.COMPARISON_FIELDS:
                if field not in result:
                    result[field] = ""

            return result

        def add(self, row: dict) -> "DBDatasetBase":
            """
            Adds the given dataset into the database.

            :param row:
                The row or dataset to add.

            :raise TypeError:
                When the given :code:`row` is not a :py:class`dict`.
            """

            raise NotImplementedError()

        def remove(self, row: dict) -> "DBDatasetBase":
            """
            Removes the given dataset from the database.

            :param row:
                The row or dataset to remove.

            :raise TypeError:
                When the given :code:`row` is not a :py:class`dict`.
            """

            raise NotImplementedError()

        def update(self, row: dict, *, ignore_if_exist: bool = False) -> "DBDatasetBase":
            """
            Adds the given dataset into the database if it does not exists.
            Update otherwise.

            :param row:
                The row or dataset to manipulate.

            :param ignore_if_exist:
                Ignores the insertion/update if the row already exists.

            :raise TypeError:
                When the given :code:`row` is not a :py:class`dict`.
            """

            raise NotImplementedError()

        def get_content(self) -> Generator[Optional[dict], None, None]:
            """
            Provides a generator which provides the next line to read.
            """

            raise NotImplementedError()

        def cleanup(self) -> "DBDatasetBase":
            """
            Cleanups the dataset.
            """

            raise NotImplementedError()

        @execute_if_authorized(None)
        def get_filtered_content(
            self, filter_map: dict
        ) -> Generator[Optional[dict], None, None]:
            """
            Provides a generator which provides the next dataset. to read.

            :param filter_map:
                A dictionary representing what we need to filter.

            :raise TypeError:
                When the given :code:`filter_map` is not a :py:class:`dict`.
            """

            if not isinstance(filter_map, dict):
                raise TypeError(f"<filter_map> should be {dict}, {type(filter_map)} given.")

            for row in self.get_content():
                for key, value in filter_map.items():
                    if key not in row:
                        continue

                    if row[key] == value:
                        yield row

        def exists(self, row: dict) -> bool:
            """
            Checks if the given dataset exists in our dataset.

            :param row:
                The row or dataset to add.
            """

            raise NotImplementedError()

        def are_equal(self, read_row: dict, row: dict) -> bool:
            """
            Compares the given :code:`read_row` to the `row`.

            :param read_row:
                The row read from the dataset infrastructure.
            :param row:
                The row given by the testing infrastructure.
            """

            raise NotImplementedError()

### `authorized` `property` `writable`

Provides the current state of the :code:`_authorized` attribute.

### `config_dir` `property` `writable`

Provides the current state of the :code:`_config_dir` attribute.

### `remove_unneeded_fields` `property` `writable`

Provides the current state of the :code:`_remove_unneeded_fields`.

### `__post_init__()`

A method to be called (automatically) after init.

Source code in `PyFunceble/dataset/db_base.py`

    96
    97
    98
    99

    def __post_init__(self) -> None:
        """
        A method to be called (automatically) after __init__.
        """

### `add(row)`

Adds the given dataset into the database.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`row` | `dict` | The row or dataset to add. | required

Raises:

Type | Description
--- | ---
`TypeError` | When the given :code:`row` is not a class`dict`.

Source code in `PyFunceble/dataset/db_base.py`

    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264

    def add(self, row: dict) -> "DBDatasetBase":
        """
        Adds the given dataset into the database.

        :param row:
            The row or dataset to add.

        :raise TypeError:
            When the given :code:`row` is not a :py:class`dict`.
        """

        raise NotImplementedError()

### `are_equal(read_row, row)`

Compares the given :code:`read_row` to the `row`.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`read_row` | `dict` | The row read from the dataset infrastructure. | required
`row` | `dict` | The row given by the testing infrastructure. | required

Source code in `PyFunceble/dataset/db_base.py`

    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356

    def are_equal(self, read_row: dict, row: dict) -> bool:
        """
        Compares the given :code:`read_row` to the `row`.

        :param read_row:
            The row read from the dataset infrastructure.
        :param row:
            The row given by the testing infrastructure.
        """

        raise NotImplementedError()

### `cleanup()`

Cleanups the dataset.

Source code in `PyFunceble/dataset/db_base.py`

    304
    305
    306
    307
    308
    309

    def cleanup(self) -> "DBDatasetBase":
        """
        Cleanups the dataset.
        """

        raise NotImplementedError()

### `execute_if_authorized(default=None)`

Executes the decorated method only if we are authorized to process. Otherwise, apply the given :code:`default`.

Source code in `PyFunceble/dataset/db_base.py`

    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116

    def execute_if_authorized(default: Any = None):  # pylint: disable=no-self-argument
        """
        Executes the decorated method only if we are authorized to process.
        Otherwise, apply the given :code:`default`.
        """

        def inner_metdhod(func):
            @functools.wraps(func)
            def wrapper(self, *args, **kwargs):
                if self.authorized:
                    return func(self, *args, **kwargs)  # pylint: disable=not-callable
                return default

            return wrapper

        return inner_metdhod

### `exists(row)`

Checks if the given dataset exists in our dataset.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`row` | `dict` | The row or dataset to add. | required

Source code in `PyFunceble/dataset/db_base.py`

    336
    337
    338
    339
    340
    341
    342
    343
    344

    def exists(self, row: dict) -> bool:
        """
        Checks if the given dataset exists in our dataset.

        :param row:
            The row or dataset to add.
        """

        raise NotImplementedError()

### `get_content()`

Provides a generator which provides the next line to read.

Source code in `PyFunceble/dataset/db_base.py`

    297
    298
    299
    300
    301
    302

    def get_content(self) -> Generator[Optional[dict], None, None]:
        """
        Provides a generator which provides the next line to read.
        """

        raise NotImplementedError()

### `get_filtered_content(filter_map)`

Provides a generator which provides the next dataset. to read.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`filter_map` | `dict` | A dictionary representing what we need to filter. | required

Raises:

Type | Description
--- | ---
`TypeError` | When the given :code:`filter_map` is not a class:`dict`.

Source code in `PyFunceble/dataset/db_base.py`

    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334

    @execute_if_authorized(None)
    def get_filtered_content(
        self, filter_map: dict
    ) -> Generator[Optional[dict], None, None]:
        """
        Provides a generator which provides the next dataset. to read.

        :param filter_map:
            A dictionary representing what we need to filter.

        :raise TypeError:
            When the given :code:`filter_map` is not a :py:class:`dict`.
        """

        if not isinstance(filter_map, dict):
            raise TypeError(f"<filter_map> should be {dict}, {type(filter_map)} given.")

        for row in self.get_content():
            for key, value in filter_map.items():
                if key not in row:
                    continue

                if row[key] == value:
                    yield row

### `get_filtered_row(row)`

Removes all unkowns fields (not declared) from the given row.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`row` | `dict` | The row to work with. | required

Source code in `PyFunceble/dataset/db_base.py`

    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251

    @execute_if_authorized(dict())  # pylint: disable=use-dict-literal
    def get_filtered_row(self, row: dict) -> dict:
        """
        Removes all unkowns fields (not declared) from the given row.

        :param row:
            The row to work with.
        """

        result = {}

        for key, value in row.items():
            if value is None:
                value = ""

            if key in self.FIELDS:
                result[key] = value

        for field in self.COMPARISON_FIELDS:
            if field not in result:
                result[field] = ""

        return result

### `remove(row)`

Removes the given dataset from the database.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`row` | `dict` | The row or dataset to remove. | required

Raises:

Type | Description
--- | ---
`TypeError` | When the given :code:`row` is not a class`dict`.

Source code in `PyFunceble/dataset/db_base.py`

    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277

    def remove(self, row: dict) -> "DBDatasetBase":
        """
        Removes the given dataset from the database.

        :param row:
            The row or dataset to remove.

        :raise TypeError:
            When the given :code:`row` is not a :py:class`dict`.
        """

        raise NotImplementedError()

### `set_authorized(value)`

Sets the value of the :code:`_authorized` attribute.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`value` | `bool` | The value to set. | required

Source code in `PyFunceble/dataset/db_base.py`

    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190

    def set_authorized(self, value: bool) -> "DBDatasetBase":
        """
        Sets the value of the :code:`_authorized` attribute.

        :param value:
            The value to set.
        """

        self.authorized = value

        return self

### `set_config_dir(value)`

Sets the configuration directory.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`value` | `str` | The value to set. | required

Source code in `PyFunceble/dataset/db_base.py`

    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153

    def set_config_dir(self, value: str) -> "DBDatasetBase":
        """
        Sets the configuration directory.

        :param value:
            The value to set.
        """

        self.config_dir = value

        return self

### `set_remove_unneeded_fields(value)`

Sets the value of the :code:`_remove_unneeded_fields` attribute.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`value` | `bool` | The value to set. | required

Source code in `PyFunceble/dataset/db_base.py`

    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227

    def set_remove_unneeded_fields(self, value: bool) -> "DBDatasetBase":
        """
        Sets the value of the :code:`_remove_unneeded_fields` attribute.

        :param value:
            The value to set.
        """

        self.remove_unneeded_fields = value

        return self

### `update(row, *, ignore_if_exist=False)`

Adds the given dataset into the database if it does not exists. Update otherwise.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`row` | `dict` | The row or dataset to manipulate. | required
`ignore_if_exist` | `bool` | Ignores the insertion/update if the row already exists. | `False`

Raises:

Type | Description
--- | ---
`TypeError` | When the given :code:`row` is not a class`dict`.

Source code in `PyFunceble/dataset/db_base.py`

    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295

    def update(self, row: dict, *, ignore_if_exist: bool = False) -> "DBDatasetBase":
        """
        Adds the given dataset into the database if it does not exists.
        Update otherwise.

        :param row:
            The row or dataset to manipulate.

        :param ignore_if_exist:
            Ignores the insertion/update if the row already exists.

        :raise TypeError:
            When the given :code:`row` is not a :py:class`dict`.
        """

        raise NotImplementedError()