mg-sql


Namemg-sql JSON
Version 0.0.1 PyPI version JSON
download
home_pagehttps://github.com/denisxab/mg_sql.git
SummaryСоздание файлов конфигурация
upload_time2023-01-05 14:40:32
maintainer
docs_urlNone
authorDenis Kustov
requires_python>=3.10,<4.0
license
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Подготовка

Для работы с БД я создал библиотеку на основе `SqlAlchemy`.

Главно, что нужно сделать для работы с этой библиотекой, это заранее вызвать конструктор класса `SQL`, который создаст
подключение к СУБД

```python
SQL(SqlUrlConnect.СУБД(user='', password='', host='', name_db=''))
```

- `SqlUrlConnect` - Класс с шаблонами формирования `url` для подключения к СУБД

# Использование

## Использование в асинхронной Функции/Методе

```python
import asyncio
from mg_sql.sql_async import SqlUrlConnect
from mg_sql.sql_async.base import SQL
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession, AsyncConnection, AsyncEngine

@SQL.get_session_decor
async def ReadDB(_session: AsyncSession):
    res = await SQL.read_execute_raw_sql(_session, raw_sql=""" SQL ЗАПРОС = :ключ """, params={'ключ':Значение})

asyncio.run(ReadDB())
```

- `raw_sql=''` - SQL запрос
- `params:dict[str,Any]` - Параметры в шаблонные

## Создание таблиц

```python
import asyncio
from mg_sql.sql_async.base import SQL
from mg_sql.sql_async.model_logic import RawSqlModel

class ИмяТаблицы(RawSqlModel):
    """Пользователи"""
    table_name = 'ИмяТаблицы'

    @classmethod
    def create_table(cls) -> str:
        return """
CREATE TABLE ИмяТаблицы (
 id INTEGER,
 user_id INTEGER,
);
CREATE UNIQUE INDEX ix_users_vk_user_id ON users_vk (user_id);
    """

asyncio.run(SQL.create_models(
    [ИмяТаблицы]
))
```

<!-- ## Использование в FastApi

```python
@router.api_route("/Путь", methods=["POST"])
async def ИмяМаршрутизатора(request: Request, session: AsyncSession = Depends(SQL.get_session)):
    res = await SQL.read_execute_raw_sql(session, raw_sql='', params={})
```

- `raw_sql=''` - SQL запрос
- `params:dict[str,Any]` - Параметры в шаблонные
 -->

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/denisxab/mg_sql.git",
    "name": "mg-sql",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.10,<4.0",
    "maintainer_email": "",
    "keywords": "",
    "author": "Denis Kustov",
    "author_email": "denis-kustov@rambler.ru",
    "download_url": "https://files.pythonhosted.org/packages/1e/3d/746944431df21595ff7d08f5d7947cec7eb28a6b532438eea6342a071e99/mg_sql-0.0.1.tar.gz",
    "platform": null,
    "description": "# \u041f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043a\u0430\n\n\u0414\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0411\u0414 \u044f \u0441\u043e\u0437\u0434\u0430\u043b \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 `SqlAlchemy`.\n\n\u0413\u043b\u0430\u0432\u043d\u043e, \u0447\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u044d\u0442\u043e\u0439 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u043e\u0439, \u044d\u0442\u043e \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0432\u044b\u0437\u0432\u0430\u0442\u044c \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 \u043a\u043b\u0430\u0441\u0441\u0430 `SQL`, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043e\u0437\u0434\u0430\u0441\u0442\n\u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u043a \u0421\u0423\u0411\u0414\n\n```python\nSQL(SqlUrlConnect.\u0421\u0423\u0411\u0414(user='', password='', host='', name_db=''))\n```\n\n- `SqlUrlConnect` - \u041a\u043b\u0430\u0441\u0441 \u0441 \u0448\u0430\u0431\u043b\u043e\u043d\u0430\u043c\u0438 \u0444\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f `url` \u0434\u043b\u044f \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u043a \u0421\u0423\u0411\u0414\n\n# \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\n\n## \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0432 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0439 \u0424\u0443\u043d\u043a\u0446\u0438\u0438/\u041c\u0435\u0442\u043e\u0434\u0435\n\n```python\nimport asyncio\nfrom mg_sql.sql_async import SqlUrlConnect\nfrom mg_sql.sql_async.base import SQL\nfrom sqlalchemy.ext.asyncio import create_async_engine, AsyncSession, AsyncConnection, AsyncEngine\n\n@SQL.get_session_decor\nasync def ReadDB(_session: AsyncSession):\n    res = await SQL.read_execute_raw_sql(_session, raw_sql=\"\"\" SQL \u0417\u0410\u041f\u0420\u041e\u0421 = :\u043a\u043b\u044e\u0447 \"\"\", params={'\u043a\u043b\u044e\u0447':\u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435})\n\nasyncio.run(ReadDB())\n```\n\n- `raw_sql=''` - SQL \u0437\u0430\u043f\u0440\u043e\u0441\n- `params:dict[str,Any]` - \u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u0432 \u0448\u0430\u0431\u043b\u043e\u043d\u043d\u044b\u0435\n\n## \u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\n\n```python\nimport asyncio\nfrom mg_sql.sql_async.base import SQL\nfrom mg_sql.sql_async.model_logic import RawSqlModel\n\nclass \u0418\u043c\u044f\u0422\u0430\u0431\u043b\u0438\u0446\u044b(RawSqlModel):\n    \"\"\"\u041f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0438\"\"\"\n    table_name = '\u0418\u043c\u044f\u0422\u0430\u0431\u043b\u0438\u0446\u044b'\n\n    @classmethod\n    def create_table(cls) -> str:\n        return \"\"\"\nCREATE TABLE \u0418\u043c\u044f\u0422\u0430\u0431\u043b\u0438\u0446\u044b (\n id INTEGER,\n user_id INTEGER,\n);\nCREATE UNIQUE INDEX ix_users_vk_user_id ON users_vk (user_id);\n    \"\"\"\n\nasyncio.run(SQL.create_models(\n    [\u0418\u043c\u044f\u0422\u0430\u0431\u043b\u0438\u0446\u044b]\n))\n```\n\n<!-- ## \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0432 FastApi\n\n```python\n@router.api_route(\"/\u041f\u0443\u0442\u044c\", methods=[\"POST\"])\nasync def \u0418\u043c\u044f\u041c\u0430\u0440\u0448\u0440\u0443\u0442\u0438\u0437\u0430\u0442\u043e\u0440\u0430(request: Request, session: AsyncSession = Depends(SQL.get_session)):\n    res = await SQL.read_execute_raw_sql(session, raw_sql='', params={})\n```\n\n- `raw_sql=''` - SQL \u0437\u0430\u043f\u0440\u043e\u0441\n- `params:dict[str,Any]` - \u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u0432 \u0448\u0430\u0431\u043b\u043e\u043d\u043d\u044b\u0435\n -->\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0444\u0430\u0439\u043b\u043e\u0432 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u044f",
    "version": "0.0.1",
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "fdc593976d30242d900b953a106af56e3ddd13d488efd361cf3d7f76ffc6d653",
                "md5": "ff64aadd9a3bb3819bf3ce6158bb745f",
                "sha256": "bdf6a823d3517e43f160eafa7fa22cd0e0b6cc816611526435a14f6efea66f32"
            },
            "downloads": -1,
            "filename": "mg_sql-0.0.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "ff64aadd9a3bb3819bf3ce6158bb745f",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.10,<4.0",
            "size": 37378,
            "upload_time": "2023-01-05T14:40:30",
            "upload_time_iso_8601": "2023-01-05T14:40:30.342509Z",
            "url": "https://files.pythonhosted.org/packages/fd/c5/93976d30242d900b953a106af56e3ddd13d488efd361cf3d7f76ffc6d653/mg_sql-0.0.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "1e3d746944431df21595ff7d08f5d7947cec7eb28a6b532438eea6342a071e99",
                "md5": "e97643d2865272156d02f3421996a01d",
                "sha256": "38faae5a644c044b06047203de08622ede39555f2b863bac82a27bc227a6552d"
            },
            "downloads": -1,
            "filename": "mg_sql-0.0.1.tar.gz",
            "has_sig": false,
            "md5_digest": "e97643d2865272156d02f3421996a01d",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.10,<4.0",
            "size": 25666,
            "upload_time": "2023-01-05T14:40:32",
            "upload_time_iso_8601": "2023-01-05T14:40:32.267188Z",
            "url": "https://files.pythonhosted.org/packages/1e/3d/746944431df21595ff7d08f5d7947cec7eb28a6b532438eea6342a071e99/mg_sql-0.0.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-01-05 14:40:32",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "denisxab",
    "github_project": "mg_sql.git",
    "lcname": "mg-sql"
}
        
Elapsed time: 0.05453s