Skip to main content

a quick tool base dict for mysql and sqlite

Project description

Cator

PyPI PyPI - Downloads PyPI - Python Version PyPI - License

简介

支持 mysql和sqlite数据库, 在现有连接对象Connection 基础上进行增强

返回数据统一为dict 字典

无论使用什么数据库驱动都支持4种占位符:

paramstyle support Meaning example
qmark OK Question mark style ...WHERE name=?
numeric - Numeric, positional style ...WHERE name=:1
named OK Named style ...WHERE name=:name
format OK ANSI C printf format codes ...WHERE name=%s
pyformat OK Python extended format codes ...WHERE name=%(name)s

安装

pip install cator

使用示例

指定 autocommit 模式

import cator

# mysql
db_url = "mysql://root:123456@127.0.0.1:3306/data?charset=utf8&autocommit=true"

# sqlite
db_url = 'sqlite:///data.db?isolation_level=null'

# open Database
db = cator.connect(db_url)

# close
db.close()

接口

Database类

class DatabaseProxy:
    def table(self, table_name):
        pass

    def select(self, operation, params=()):
        pass

    def select_one(self, operation, params=()):
        pass

    def update(self, operation, params=()):
        pass

    def delete(self, operation, params=()):
        pass

    def insert(self, operation, params: Union[list, dict]):
        pass

    def insert_one(self, operation, params: Union[tuple, dict] = ()):
        pass

    def before_execute(self, operation, params=None):
        pass

    def after_execute(self, cursor):
        pass

    def execute(self, operation, params=None):
        pass

    def cursor(self, *args, **kwargs):
        """return cursor object"""

    def connect(self):
        """connect database"""

    def close(self):
        """close connection"""

    def commit(self):
        pass

    def rollback(self):
        pass

Table 类

class Table:

    @property
    def total(self):
        pass

    def insert(self, data: Union[dict, list]):
        pass

    def insert_one(self, data: dict):
        pass

    def delete_by_id(self, uid):
        pass

    def update_by_id(self, uid, data):
        pass

    def select_by_id(self, uid):
        pass

扩展 peewee

通过DatabaseProxy类,使得peewee原生sql查询进行增强

from peewee import MySQLDatabase
from cator import DatabaseProxy


config = {
    'host': 'localhost',
    'port': 3306,
    'user': 'root',
    'password': '123456',
    'database': 'data',
    'charset': 'utf8mb4',
}

db = MySQLDatabase(**config)

# use cator database proxy
db_proxy = DatabaseProxy(db)

DatabaseProxy类接收一个Connection对象,只需要实现以下4个方法即可

class Connection(ABC):
    def close(self):
        pass

    def commit(self):
        pass

    def rollback(self):
        pass

    def cursor(self):
        pass

注意问题

  1. 使用时需注意链接超时问题
  2. cator支持了autocommit自动提交,默认关闭,如有需要可以打开,
  3. 如果需要执行事务就需要关闭自动提交

cator基于以下模块进行了改进

  1. myquery
  2. aquery
  3. puremysql
  4. pythink

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cator-0.0.8.tar.gz (10.2 kB view details)

Uploaded Source

Built Distribution

cator-0.0.8-py3-none-any.whl (14.9 kB view details)

Uploaded Python 3

File details

Details for the file cator-0.0.8.tar.gz.

File metadata

  • Download URL: cator-0.0.8.tar.gz
  • Upload date:
  • Size: 10.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.40.0 CPython/3.7.0

File hashes

Hashes for cator-0.0.8.tar.gz
Algorithm Hash digest
SHA256 8e015872af888a6c8369bc129ebfbb0f58e9f5f3182da34aeddb74a82ca6d981
MD5 3814e252c15a3ef045c0f3f2f0b702ca
BLAKE2b-256 624d2ce3b0c57cd963b646e5ddc81454498424087ce406e1cfc94e9ea65123fc

See more details on using hashes here.

File details

Details for the file cator-0.0.8-py3-none-any.whl.

File metadata

  • Download URL: cator-0.0.8-py3-none-any.whl
  • Upload date:
  • Size: 14.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.40.0 CPython/3.7.0

File hashes

Hashes for cator-0.0.8-py3-none-any.whl
Algorithm Hash digest
SHA256 f4162fd002c5266d390f345b7ec9007f7beb9c4b7cd86cdf9063b47484f2f32b
MD5 d001178ba80797ce6216f2ecee276bdd
BLAKE2b-256 941649cbba2aae98b2f517f4b26142fb921505ca568a36d8a67680c039fde48f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page