quickdb


Namequickdb JSON
Version 0.0.39 PyPI version JSON
download
home_pagehttps://github.com/Leviathangk/gftp
Summary利用 sqlalchemy 封装一个易用的用来处理数据库的工具,以及其余的便捷连接操作
upload_time2023-03-27 06:23:58
maintainer
docs_urlNone
author郭一会儿
requires_python
licenseMIT Licence
keywords quickdb
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 一、模块介绍

quickdb 是一个操作合集

* mysql
* postgresql
* kafka
* mongo
* redis

## 二 mysql

使用 sqlalchemy 为其定义了一个类

* MysqlSQLAlchemyEngine

### 2.1、MysqlSQLAlchemyEngine

该类继承自 SQLAlchemyEngineBase 用来定义连接 \
可接收 sqlalchemy create_engine 的参数

    engine = MysqlSQLAlchemyEngine(host='localhost', port=3306, user='root', pwd='1234', db='test')
    
    with engine.session() as session, session.begin:
        pass

    with engine.connection() as conn, conn.begin:
        pass

### 2.2、MysqlSQLAlchemyEngine 方法

其含有以下方法:

* reverse_table_model:逆向表模型
* insert:一条或多条
* upsert:一条或多条
* delete
* execute
* merge

主要说一下 reverse_table_model
该方法含有三个参数:

* path:生成的 model 路径,需含文件名
* tables:需要的表,可不指定
* commands:额外的命令

      method = MysqlSQLAlchemyMethods(engine=engine)
      method.reverse_table_model(path='./modules.py')

## 三、postgresql

同 mysql

## 四、kafka

主要是使用了 with 和方便的 send,会帮助你将 msg 转化为 bytes,也可以同时 flush

    p = KafkaMsgProducer(server=xxx)
    p.send(topic, msg)

    with KafkaMsgProducer(server=xxx) as p:
        p.send()

## 五、mongo

通过 get_collection 返回的是修改过的 Collection 对象,其有两个新方法

- iter: 快速迭代数据库
- upsert_one:插入或更新的便捷写法

```
conn = MongoConn(host, port)
col = conn.get_collection(db, col)

for i in col.iter():
    print(i)
```

其使用了 with,可以自动回收连接

    conn = MongoConn(host, port)
    col = conn.get_collection(db, col)
    conn.close()

    with MongoConn(host, port) as conn:
        col = conn.get_collection(db, col)

## 六、redis

### 1、redisConn

    with RedisConn() as conn:
        pass

### 2、RedisLock

这是一个阻塞的 redis 事务锁

    with RedisLock(lock_name=""):
        pass

### 3、RedisLockNoWait

这是一个非阻塞的 redis 事务锁,只有获取到锁的人才执行,获取不到就不会继续等待锁,但是需要使用 lock_success 判断

    with RedisLockNoWait(lock_name=") as lock:
        if lock.lock_success:
            ...

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/Leviathangk/gftp",
    "name": "quickdb",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "quickdb",
    "author": "\u90ed\u4e00\u4f1a\u513f",
    "author_email": "1015295213@qq.com",
    "download_url": "https://files.pythonhosted.org/packages/7b/3e/500baef92b3306e2ada5c7ac2d1e63c01913597caf5693df515cfa06c6b7/quickdb-0.0.39.tar.gz",
    "platform": "any",
    "description": "# \u4e00\u3001\u6a21\u5757\u4ecb\u7ecd\r\n\r\nquickdb \u662f\u4e00\u4e2a\u64cd\u4f5c\u5408\u96c6\r\n\r\n* mysql\r\n* postgresql\r\n* kafka\r\n* mongo\r\n* redis\r\n\r\n## \u4e8c mysql\r\n\r\n\u4f7f\u7528 sqlalchemy \u4e3a\u5176\u5b9a\u4e49\u4e86\u4e00\u4e2a\u7c7b\r\n\r\n* MysqlSQLAlchemyEngine\r\n\r\n### 2.1\u3001MysqlSQLAlchemyEngine\r\n\r\n\u8be5\u7c7b\u7ee7\u627f\u81ea SQLAlchemyEngineBase \u7528\u6765\u5b9a\u4e49\u8fde\u63a5 \\\r\n\u53ef\u63a5\u6536 sqlalchemy create_engine \u7684\u53c2\u6570\r\n\r\n    engine = MysqlSQLAlchemyEngine(host='localhost', port=3306, user='root', pwd='1234', db='test')\r\n    \r\n    with engine.session() as session, session.begin:\r\n        pass\r\n\r\n    with engine.connection() as conn, conn.begin:\r\n        pass\r\n\r\n### 2.2\u3001MysqlSQLAlchemyEngine \u65b9\u6cd5\r\n\r\n\u5176\u542b\u6709\u4ee5\u4e0b\u65b9\u6cd5\uff1a\r\n\r\n* reverse_table_model\uff1a\u9006\u5411\u8868\u6a21\u578b\r\n* insert\uff1a\u4e00\u6761\u6216\u591a\u6761\r\n* upsert\uff1a\u4e00\u6761\u6216\u591a\u6761\r\n* delete\r\n* execute\r\n* merge\r\n\r\n\u4e3b\u8981\u8bf4\u4e00\u4e0b reverse_table_model\r\n\u8be5\u65b9\u6cd5\u542b\u6709\u4e09\u4e2a\u53c2\u6570\uff1a\r\n\r\n* path\uff1a\u751f\u6210\u7684 model \u8def\u5f84\uff0c\u9700\u542b\u6587\u4ef6\u540d\r\n* tables\uff1a\u9700\u8981\u7684\u8868\uff0c\u53ef\u4e0d\u6307\u5b9a\r\n* commands\uff1a\u989d\u5916\u7684\u547d\u4ee4\r\n\r\n      method = MysqlSQLAlchemyMethods(engine=engine)\r\n      method.reverse_table_model(path='./modules.py')\r\n\r\n## \u4e09\u3001postgresql\r\n\r\n\u540c mysql\r\n\r\n## \u56db\u3001kafka\r\n\r\n\u4e3b\u8981\u662f\u4f7f\u7528\u4e86 with \u548c\u65b9\u4fbf\u7684 send\uff0c\u4f1a\u5e2e\u52a9\u4f60\u5c06 msg \u8f6c\u5316\u4e3a bytes\uff0c\u4e5f\u53ef\u4ee5\u540c\u65f6 flush\r\n\r\n    p = KafkaMsgProducer(server=xxx)\r\n    p.send(topic, msg)\r\n\r\n    with KafkaMsgProducer(server=xxx) as p:\r\n        p.send()\r\n\r\n## \u4e94\u3001mongo\r\n\r\n\u901a\u8fc7 get_collection \u8fd4\u56de\u7684\u662f\u4fee\u6539\u8fc7\u7684 Collection \u5bf9\u8c61\uff0c\u5176\u6709\u4e24\u4e2a\u65b0\u65b9\u6cd5\r\n\r\n- iter: \u5feb\u901f\u8fed\u4ee3\u6570\u636e\u5e93\r\n- upsert_one\uff1a\u63d2\u5165\u6216\u66f4\u65b0\u7684\u4fbf\u6377\u5199\u6cd5\r\n\r\n```\r\nconn = MongoConn(host, port)\r\ncol = conn.get_collection(db, col)\r\n\r\nfor i in col.iter():\r\n    print(i)\r\n```\r\n\r\n\u5176\u4f7f\u7528\u4e86 with\uff0c\u53ef\u4ee5\u81ea\u52a8\u56de\u6536\u8fde\u63a5\r\n\r\n    conn = MongoConn(host, port)\r\n    col = conn.get_collection(db, col)\r\n    conn.close()\r\n\r\n    with MongoConn(host, port) as conn:\r\n        col = conn.get_collection(db, col)\r\n\r\n## \u516d\u3001redis\r\n\r\n### 1\u3001redisConn\r\n\r\n    with RedisConn() as conn:\r\n        pass\r\n\r\n### 2\u3001RedisLock\r\n\r\n\u8fd9\u662f\u4e00\u4e2a\u963b\u585e\u7684 redis \u4e8b\u52a1\u9501\r\n\r\n    with RedisLock(lock_name=\"\"):\r\n        pass\r\n\r\n### 3\u3001RedisLockNoWait\r\n\r\n\u8fd9\u662f\u4e00\u4e2a\u975e\u963b\u585e\u7684 redis \u4e8b\u52a1\u9501\uff0c\u53ea\u6709\u83b7\u53d6\u5230\u9501\u7684\u4eba\u624d\u6267\u884c\uff0c\u83b7\u53d6\u4e0d\u5230\u5c31\u4e0d\u4f1a\u7ee7\u7eed\u7b49\u5f85\u9501\uff0c\u4f46\u662f\u9700\u8981\u4f7f\u7528 lock_success \u5224\u65ad\r\n\r\n    with RedisLockNoWait(lock_name=\") as lock:\r\n        if lock.lock_success:\r\n            ...\r\n",
    "bugtrack_url": null,
    "license": "MIT Licence",
    "summary": "\u5229\u7528 sqlalchemy \u5c01\u88c5\u4e00\u4e2a\u6613\u7528\u7684\u7528\u6765\u5904\u7406\u6570\u636e\u5e93\u7684\u5de5\u5177\uff0c\u4ee5\u53ca\u5176\u4f59\u7684\u4fbf\u6377\u8fde\u63a5\u64cd\u4f5c",
    "version": "0.0.39",
    "split_keywords": [
        "quickdb"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "7b3e500baef92b3306e2ada5c7ac2d1e63c01913597caf5693df515cfa06c6b7",
                "md5": "699363585d035809f44825939e6c7a81",
                "sha256": "a8d2420cebc45bd3feb708bcde9bbfb8ceeb0dbd117a860b2787de1aa39c08e6"
            },
            "downloads": -1,
            "filename": "quickdb-0.0.39.tar.gz",
            "has_sig": false,
            "md5_digest": "699363585d035809f44825939e6c7a81",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 14158,
            "upload_time": "2023-03-27T06:23:58",
            "upload_time_iso_8601": "2023-03-27T06:23:58.316099Z",
            "url": "https://files.pythonhosted.org/packages/7b/3e/500baef92b3306e2ada5c7ac2d1e63c01913597caf5693df515cfa06c6b7/quickdb-0.0.39.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-03-27 06:23:58",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "Leviathangk",
    "github_project": "gftp",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "quickdb"
}
        
Elapsed time: 0.07346s