CNBDBer: Universal DB runner for SQL-like commands across SQLite/MySQL/PostgreSQL/MongoDB
Project description
CNBDBer
Universal DB runner for SQL-like commands across SQLite, MySQL, PostgreSQL, and MongoDB.
Install
- From PyPI (Python 3.9+):
pip install cnbdber
# optional extras
pip install 'cnbdber[mysql]'
pip install 'cnbdber[postgres]'
pip install 'cnbdber[mongo]'
pip install 'cnbdber[ssh]' # for SSH tunnel support
- From source (development):
pip install -e .
Config
- App config:
./.configs/cnbdber.config(auto-created on first run) - Logger config:
./.configs/cnblogger.config(auto-created if missing) - Logs:
./.logs/
You can override the app config via CNBDBER_CONFIG=/abs/path/to/cnbdber.config.
The logger path is read from cnbdber.config and passed directly to CNBLogger.
See .config-examples/ in this repo for ready-to-copy examples:
sqlite-cnbdber.configmysql-cnbdber.configpostgres-cnbdber.configmongo-cnbdber.configmysql-direct.configmysql-ssh-same-creds.configmysql-ssh-different-creds.configpostgres-direct.configpostgres-ssh-same-creds.configpostgres-ssh-different-creds.config
Usage
Run a one-off command:
cnbdber -c "SELECT * FROM users LIMIT 5;"
Run from a file:
cnbdber --file ./query.sql
Specify a config explicitly:
cnbdber --config ./my-config.json -c "DELETE FROM logs WHERE created_at < NOW() - INTERVAL 30 DAY;"
- Library usage (import in Python)
Quick one-liner function:
from cnbdber import cnbdber
# Use default/auto-created config
result = cnbdber("SELECT 1;")
print(result or "")
# Or pass a custom config path
result = cnbdber("SELECT * FROM users LIMIT 5;", "./my-config.json")
print(result or "")
Or use the config loader and backend helpers for finer control:
from typing import Optional
from cnbdber import load_config, get_logger
from cnbdber.core import create_backend, run_command, create_backend_context
cfg = load_config() # or load_config("./my-config.json")
logger = get_logger(cfg.logger_config_path, inline_config=cfg.logger)
with create_backend_context(cfg.target, logger) as backend:
# DDL/DML: returns None on success
ddl_result: Optional[str] = run_command(backend, "CREATE TABLE IF NOT EXISTS items(id INTEGER PRIMARY KEY, name TEXT);")
insert_result: Optional[str] = run_command(backend, "INSERT INTO items(name) VALUES ('alpha');")
# SELECT: returns tab-separated text (with header when available)
select_result: Optional[str] = run_command(backend, "SELECT id, name FROM items ORDER BY id;")
print(select_result or "")
To target MySQL/PostgreSQL/MongoDB, set cfg.target via cnbdber.config (see examples below) or construct a dict at runtime and pass it to create_backend.
- SQL backends (SQLite/MySQL/PostgreSQL): raw SQL is executed as-is.
- MongoDB: a minimal SQL-to-Mongo translation is supported for simple
SELECT/INSERT/UPDATE/DELETEwith equality-onlyWHEREclauses.
Example cnbdber.config
{
"logger_config_path": "./.configs/cnblogger.config",
"target": {
"type": "sqlite",
"sqlite_path": "./example.db"
}
}
Switch to MySQL:
{
"logger_config_path": "./.configs/cnblogger.config",
"target": {
"type": "mysql",
"host": "127.0.0.1",
"port": 3306,
"user": "root",
"password": "",
"password_file": "",
"database": "test"
}
}
SSH tunnel (optional) for MySQL/PostgreSQL:
{
"logger_config_path": "./.configs/cnblogger.config",
"target": {
"type": "postgres",
"host": "db.internal",
"port": 5432,
"user": "postgres",
"password": "",
"password_file": "~/.secrets/db.pass",
"database": "app",
"sslmode": "require",
"ssh": {
"enabled": true,
"host": "bastion.example.com",
"port": 22,
"user": "ec2-user",
"pkey_path": "~/.ssh/id_rsa",
"pkey_password": "",
"local_bind_host": "127.0.0.1",
"local_bind_port": 0,
"remote_host": "db.internal",
"remote_port": 5432
}
}
}
Notes:
- When
password_fileis provided, it will be read and used instead ofpassword. - Using SSH requires extra
cnbdber[ssh]. - Always prefer the context manager (
create_backend_context) to ensure SSH tunnels (if any) are cleaned up automatically.
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file cnbdber-0.3.0.tar.gz.
File metadata
- Download URL: cnbdber-0.3.0.tar.gz
- Upload date:
- Size: 11.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f2740ba326553f5b69bfeb8012eeca7453fcef20b8310db52d40ff6f37b65c51
|
|
| MD5 |
7ec64451fe7e722c604389d61baf0415
|
|
| BLAKE2b-256 |
a2685da82ec7cd4082c6476a0218e7b1baebe51dcbe724a6caf5b25da955226f
|
File details
Details for the file cnbdber-0.3.0-py3-none-any.whl.
File metadata
- Download URL: cnbdber-0.3.0-py3-none-any.whl
- Upload date:
- Size: 11.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
000ad8283e201726515ff4b3070159e443ec232b55543abdc5014298f741a560
|
|
| MD5 |
753fceb2d07cdb298148f5375e0939e3
|
|
| BLAKE2b-256 |
5899c1666d287d087d1973ca4db56c025234bd252934b8a09f848fe0318ac046
|