Skip to main content

Sql Tool (PostgreSQL version)

Project description

PostgreSQL 版本的 psql_sqltool。本项目由 007gzs/psql_sqltool (基于 pymysql) 改写而来, 全部适配为 PostgreSQL 语法, 底层使用 psycopg2

作者: 0716gzs (https://github.com/0716gzs) 项目地址: https://github.com/0716gzs/psql_sqltool

特性

  • PostgreSQL 连接池 (fork 安全, 自动归还连接)

  • 链式 SQL 生成器: INSERT / SELECT / UPDATE / DELETE

  • 批量 SQL 生成, 适合把数据落盘成导入脚本

  • ON CONFLICT ... DO UPDATE (对应 MySQL 的 ON DUPLICATE KEY UPDATE)

  • 自增主键 / 唯一冲突的内存去重

安装

pip install psycopg2-binary
python setup.py install

快速上手

from psql_sqltool.postgres_client import PostgresClient

client = PostgresClient(
    host='127.0.0.1',
    port=5432,
    user='postgres',
    password='postgres',
    dbname='demo',
)

# 查询
rows = client.select(table_name='user', wheres={'id': 1})

# 插入 (参数化)
client.insert(
    [{'id': 1, 'name': 'alice'}, {'id': 2, 'name': 'bob'}],
    table_name='user',
    field_list=('id', 'name'),
)

# 插入并冲突更新 (upsert)
client.insert(
    [{'id': 1, 'name': 'alice2'}],
    table_name='user',
    field_list=('id', 'name'),
    on_conflict_fields=('id',),
    on_duplicate_key_update_fields=('name',),
)

# 更新 / 删除
client.update(table_name='user', update_columns={'name': 'alice'}, wheres={'id': 1})
client.delete(table_name='user', wheres={'id': 1})

批量生成 SQL (导出脚本)

from psql_sqltool.sql_gen import GenSqlAutoId

class GenUser(GenSqlAutoId):
    TABLE_NAME = 'user'
    FIELD_LIST = ('id', 'name')

gen = GenUser()
gen.add_item(name='alice')
gen.add_item(name='bob')
for sql in gen.gen_sql():
    print(sql)

与原 MySQL 版的主要差异

  • 标识符引用: 反引号 ` -> 双引号 "

  • ON DUPLICATE KEY UPDATE ... AS __new_data__ -> ON CONFLICT (...) DO UPDATE SET ... EXCLUDED..., 且 PostgreSQL 必须显式指定冲突目标 on_conflict_fields

  • 分页: LIMIT start, size -> LIMIT size OFFSET start

  • INFORMATION_SCHEMA ... AUTO_INCREMENT -> 通过 pg_get_serial_sequence 读取序列

  • 底层驱动: pymysql -> psycopg2

License

BSD-3-Clause

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

psql_sqltool-0.1.1.tar.gz (12.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

psql_sqltool-0.1.1-py3-none-any.whl (11.3 kB view details)

Uploaded Python 3

File details

Details for the file psql_sqltool-0.1.1.tar.gz.

File metadata

  • Download URL: psql_sqltool-0.1.1.tar.gz
  • Upload date:
  • Size: 12.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.11

File hashes

Hashes for psql_sqltool-0.1.1.tar.gz
Algorithm Hash digest
SHA256 d75255341655357eec8631fca5cd28a4c49a2bcc5eaee99537a1a51a97bc236d
MD5 fc69088dfb454d8bce4073d29270c269
BLAKE2b-256 f016b9524aeeaed39aa7dce0a66b10c75174d25ae6d447fa238a82c735caa975

See more details on using hashes here.

File details

Details for the file psql_sqltool-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: psql_sqltool-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 11.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.11

File hashes

Hashes for psql_sqltool-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6bbf41ab007f1d06d55904789bcfbd0d5c9745a5332113b49dcd373067427cae
MD5 ff07b23969a935c3515a06f3339ad021
BLAKE2b-256 f9cb944799f326dadc4a4f00f0df53924b8512fa42e5e042f13c4a8c0e900a65

See more details on using hashes here.

Supported by

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