Metadata-Version: 2.4
Name: pendb
Version: 0.1.4
Summary: A tiny shortcut wrapper around Python's database drivers (SQLite now, more later)
Author-email: Antu <your-email@example.com>
License: MIT
Project-URL: Homepage, https://github.com/your-username/pendb
Classifier: Programming Language :: Python :: 3
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Requires-Python: >=3.7
Description-Content-Type: text/markdown
License-File: LICENSE
Dynamic: license-file

# pendb

A tiny shortcut wrapper around Python's database drivers — no more manual
connection/cursor/commit boilerplate.

Backends live under their own namespace, so more than one database engine
can be added later without their APIs colliding: `sq` for SQLite today,
`pg` for PostgreSQL down the line.

## Install

```bash
pip install pendb
```

## Usage

```python
from pendb import sq

db = sq.DB("shop.db")

db.ct("products", sq.it("id").lock(), sq.tx("name").lock())

db.inst("products", id=1, name="Rice")
db.inst("products", id=2, name="Oil")

print(db.select("products"))
print(db.select("products", where="name = 'Rice'"))

db.updt("products", "id = 1", name="Basmati Rice")
db.dlt("products", "id = 2")

db.close()
```

Or, as a context manager (auto-closes even if an error happens):

```python
with sq.DB("shop.db") as db:
    db.ct("products", sq.it("id").lock(), sq.tx("name").lock())
    db.inst("products", id=1, name="Rice")
```

### Filtering, sorting, limiting

`where` takes a condition string, or a list of condition strings (ANDed together).
Operators: `=`, `>`, `<`, `>=`, `<=`, `!=`, `~` (LIKE / contains).

```python
db.select("products", where="price > 50")
db.select("products", where=["price >= 10", "price <= 100"])  # AND
db.select("products", order="-price")    # descending
db.select("products", limit=5)
db.select("products", as_dict=True)      # rows as {"col": value, ...} instead of tuples
```

### Blocking duplicates on a column

Mark a column with `.block_duplicate()` when defining the table. Any insert
that would duplicate an existing value in that column raises
`BlockedDuplicateError` instead of silently failing or crashing with a raw
sqlite3 error.

```python
db.ct("api_keys", sq.tx("name"), sq.it("id").lock().block_duplicate())

db.inst("api_keys", id=1, name="antu")     # inserted normally
db.inst("api_keys", id=1, name="chondn")   # raises BlockedDuplicateError
```

### Bulk insert

```python
db.bulk_insert("products", [
    {"id": 3, "name": "Sugar"},
    {"id": 4, "name": "Salt"},
])
```

### Upsert

Updates matching rows; inserts instead if nothing matched.

```python
db.upsert("products", "id = 1", id=1, name="Rice")
```

### exists() / count()

```python
db.exists("products", "id = 1")   # -> True / False
db.count("products")              # -> total row count
db.count("products", "price > 50")
```

### Transactions (`atomic`)

Run several writes as one all-or-nothing block — if any operation fails,
everything in the block is rolled back.

```python
db.atomic([
    ("inst", "products", {"id": 5, "name": "Tea"}),
    ("updt", "products", "id = 5", {"name": "Green Tea"}),
    ("dlt",  "products", "id = 2"),
])
```

### Thread-safety & retry-on-lock

Off by default (fastest, matches plain `sqlite3` behaviour). Turn on when
sharing one `DB` across threads (e.g. a bot handling several updates at once):

```python
db = sq.DB("shop.db", thread_safe=True, retries=5)
```

`retries` controls how many times a query retries (with backoff) if it hits
a `"database is locked"` error before giving up.

## API

| Method | SQL equivalent |
|---|---|
| `db.ct(table, *columns)` | `CREATE TABLE` |
| `db.inst(table, **values)` | `INSERT` |
| `db.bulk_insert(table, rows)` | many `INSERT`s, one commit |
| `db.select(table, where=None, order=None, limit=None, as_dict=False)` | `SELECT` |
| `db.updt(table, where, **values)` | `UPDATE` |
| `db.upsert(table, where, **values)` | `UPDATE`, or `INSERT` if nothing matched |
| `db.dlt(table, where)` | `DELETE` |
| `db.exists(table, where)` | `SELECT 1 ... LIMIT 1` |
| `db.count(table, where=None)` | `SELECT COUNT(*)` |
| `db.atomic(operations)` | multiple statements, one transaction |
| `db.drp(table)` | `DROP TABLE` |

Columns: `sq.it("name")` for INTEGER, `sq.tx("name")` for TEXT.
Chain `.lock()` for NOT NULL, `.unq()` for UNIQUE, `.block_duplicate()` for
UNIQUE + a friendly `BlockedDuplicateError` on duplicate inserts.

Full method-by-method reference with more examples: [API_REFERENCE.md](API_REFERENCE.md)
