ducpy.builders.sql_builder

Thin wrapper to open .duc files as SQLite databases and run raw SQL.

A .duc file is a standard SQLite database. This builder just handles opening/creating/exporting and exposes the raw sqlite3.Connection so you can write whatever SQL you want.

Usage:

import ducpy as duc

# Open existing .duc
with duc.DucSQL("drawing.duc") as db:
    rows = db.sql("SELECT id, label FROM elements WHERE element_type = ?", "rectangle")
    db.sql("UPDATE elements SET label = ? WHERE id = ?", "new-label", rows[0]["id"])

# Create new .duc from scratch
with duc.DucSQL.new() as db:
    db.sql("INSERT INTO elements (id, element_type, x, y, width, height) VALUES (?,?,?,?,?,?)",
           "r1", "rectangle", 0, 0, 100, 50)
    db.save("output.duc")

# From bytes
with duc.DucSQL.from_bytes(raw) as db:
    print(db.sql("SELECT COUNT(*) AS n FROM elements")[0]["n"])
    modified = db.to_bytes()

Classes

DucSQL

Raw SQL access to a .duc SQLite database.

Module Contents

class ducpy.builders.sql_builder.DucSQL(path: str | pathlib.Path)

Raw SQL access to a .duc SQLite database.

Attributes:
conn: The underlying sqlite3.Connection.

Use it directly for cursor-level ops, conn.executemany, etc.

Open an existing .duc file.

conn: sqlite3.Connection
_path: str | None = ''
_temp: str | None = None
_closed = False
classmethod new(path: str | pathlib.Path | None = None) DucSQL

Create a new .duc database with the full schema bootstrapped.

Pass a path to write to disk, or omit for in-memory.

classmethod from_bytes(data: bytes) DucSQL

Open a .duc from raw bytes (temp file, cleaned up on close).

sql(query: str, *args: Any) List[sqlite3.Row]

Run a SQL statement with positional ? params. Returns rows.

sql_dict(query: str, params: dict) List[sqlite3.Row]

Run a SQL statement with named :key params. Returns rows.

commit() None
rollback() None
save(path: str | pathlib.Path | None = None) None

Write the database to a file. Omit path to save in-place.

to_bytes() bytes

Export the database as raw bytes.

close() None
__enter__() DucSQL
__exit__(*exc: Any) None
__del__() None
__repr__() str