nyansqlite 1.0.0.dev1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
nyansqlite/__init__.py ADDED
@@ -0,0 +1,15 @@
1
+ from ._markers import CompositeIndex, Indexed, Searchable, UniqueIndexed
2
+ from .core import NyanSQLite
3
+ from .exceptions import FieldNotFoundError, ModelNotRegisteredError, SearchNotEnabledError
4
+
5
+ __all__ = [
6
+ "NyanSQLite",
7
+ "Indexed",
8
+ "UniqueIndexed",
9
+ "Searchable",
10
+ "CompositeIndex",
11
+ "FieldNotFoundError",
12
+ "ModelNotRegisteredError",
13
+ "SearchNotEnabledError",
14
+ ]
15
+ __version__ = "1.0.0dev1"
@@ -0,0 +1,113 @@
1
+ from __future__ import annotations
2
+
3
+ from collections.abc import Generator
4
+ from contextlib import contextmanager
5
+ from typing import Any
6
+
7
+ try:
8
+ import apsw
9
+ except ImportError:
10
+ apsw = None
11
+
12
+ try:
13
+ import sqlite3
14
+ except ImportError:
15
+ sqlite3 = None
16
+
17
+
18
+ class NyanConnection:
19
+ """Uniform dict-row interface over apsw (preferred) or sqlite3 (fallback).
20
+
21
+ WAL mode, 64 MB page cache, and foreign keys are enabled by default.
22
+ """
23
+
24
+ _backend: str
25
+ _conn: Any
26
+
27
+ def __init__(self, path: str, wal: bool = True):
28
+ try:
29
+ import apsw
30
+
31
+ self._conn = apsw.Connection(path)
32
+ self._backend = "apsw"
33
+ except ImportError:
34
+ import sqlite3
35
+
36
+ self._conn = sqlite3.connect(path, check_same_thread=False)
37
+ self._conn.isolation_level = None # manual transaction control
38
+ self._backend = "sqlite3"
39
+
40
+ pragmas = [
41
+ ("journal_mode", "WAL" if wal else "DELETE"),
42
+ ("foreign_keys", "ON"),
43
+ ("synchronous", "NORMAL"),
44
+ ("cache_size", "-65536"), # 64 MB
45
+ ]
46
+ for pragma, value in pragmas:
47
+ self._raw(f"PRAGMA {pragma} = {value}")
48
+
49
+ # ── low-level execute ────────────────────────────────────────────── #
50
+
51
+ def _raw(self, sql: str, params: tuple = ()) -> list[dict[str, Any]]:
52
+ if self._backend == "apsw":
53
+ cur = self._conn.cursor()
54
+ result = cur.execute(sql, params)
55
+
56
+ # APSW用の修正: description取得を安全に行う
57
+ description = None
58
+ try:
59
+ description = cur.description
60
+ except (apsw.ExecutionCompleteError, AttributeError):
61
+ # 実行が完了している、または結果がない場合はここに来る
62
+ pass
63
+
64
+ if description:
65
+ cols = [d[0] for d in description]
66
+ return [dict(zip(cols, row)) for row in result]
67
+ return []
68
+
69
+ else:
70
+ # sqlite3 backend
71
+ cur = self._conn.execute(sql, params)
72
+ if cur.description:
73
+ cols = [d[0] for d in cur.description]
74
+ # fetchall() でリスト化して返す
75
+ return [dict(zip(cols, row)) for row in cur.fetchall()]
76
+ return []
77
+
78
+ def execute(self, sql: str, params: tuple = ()) -> list[dict[str, Any]]:
79
+ return self._raw(sql, params)
80
+
81
+ def executemany(self, sql: str, rows: list[tuple]) -> None:
82
+ if self._backend == "apsw":
83
+ self._conn.cursor().executemany(sql, rows)
84
+ else:
85
+ self._conn.executemany(sql, rows)
86
+
87
+ # ── transaction ──────────────────────────────────────────────────── #
88
+
89
+ @contextmanager
90
+ def transaction(self) -> Generator[None, None, None]:
91
+ self._raw("BEGIN")
92
+ try:
93
+ yield
94
+ self._raw("COMMIT")
95
+ except Exception:
96
+ self._raw("ROLLBACK")
97
+ raise
98
+
99
+ # ── util ─────────────────────────────────────────────────────────── #
100
+
101
+ def changes(self) -> int:
102
+ """Number of rows affected by the last DML statement."""
103
+ if self._backend == "apsw":
104
+ return self._conn.changes()
105
+ rows = self._raw("SELECT changes() AS c")
106
+ return rows[0]["c"] if rows else 0
107
+
108
+ def close(self) -> None:
109
+ self._conn.close()
110
+
111
+ @property
112
+ def backend(self) -> str:
113
+ return self._backend
nyansqlite/_markers.py ADDED
@@ -0,0 +1,84 @@
1
+ from __future__ import annotations
2
+
3
+ from typing import Any
4
+
5
+
6
+ class _NyanIndexedMarker:
7
+ def __init__(self, unique: bool = False):
8
+ self.unique = unique
9
+
10
+ def __repr__(self) -> str:
11
+ return f"Indexed(unique={self.unique})"
12
+
13
+
14
+ class _NyanSearchableMarker:
15
+ def __repr__(self) -> str:
16
+ return "Searchable"
17
+
18
+
19
+ class Indexed:
20
+ """Mark a field for an automatic B-tree index.
21
+
22
+ Usage::
23
+
24
+ name: Indexed[str]
25
+ age: Indexed[int]
26
+ """
27
+
28
+ def __class_getitem__(cls, item: Any):
29
+ from typing import Annotated
30
+ return Annotated[item, _NyanIndexedMarker(unique=False)]
31
+
32
+
33
+ class UniqueIndexed:
34
+ """Mark a field for a UNIQUE B-tree index.
35
+
36
+ Usage::
37
+
38
+ email: UniqueIndexed[str]
39
+ """
40
+
41
+ def __class_getitem__(cls, item: Any):
42
+ from typing import Annotated
43
+ return Annotated[item, _NyanIndexedMarker(unique=True)]
44
+
45
+
46
+ class Searchable:
47
+ """Mark a str field for FTS5 full-text search.
48
+
49
+ Usage::
50
+
51
+ title: Searchable[str]
52
+ body: Searchable[str]
53
+ """
54
+
55
+ def __class_getitem__(cls, item: Any):
56
+ from typing import Annotated
57
+ return Annotated[item, _NyanSearchableMarker()]
58
+
59
+
60
+ class CompositeIndex:
61
+ """Declare a composite (multi-column) index via ``__nyan_indexes__``.
62
+
63
+ Usage::
64
+
65
+ class Order(BaseModel):
66
+ __nyan_indexes__ = [CompositeIndex("user_id", "created_at")]
67
+ id: int
68
+ user_id: int
69
+ created_at: datetime
70
+ amount: float
71
+
72
+ # Unique composite:
73
+ __nyan_indexes__ = [CompositeIndex("tenant_id", "email", unique=True)]
74
+ """
75
+
76
+ def __init__(self, *fields: str, unique: bool = False):
77
+ if not fields:
78
+ raise ValueError("CompositeIndex requires at least one field name.")
79
+ self.fields: tuple[str, ...] = fields
80
+ self.unique: bool = unique
81
+
82
+ def __repr__(self) -> str:
83
+ u = ", unique=True" if self.unique else ""
84
+ return f"CompositeIndex({', '.join(self.fields)!r}{u})"
nyansqlite/_schema.py ADDED
@@ -0,0 +1,151 @@
1
+ from __future__ import annotations
2
+
3
+ import re
4
+ from typing import Any, Optional
5
+
6
+ from pydantic import BaseModel
7
+
8
+ try:
9
+ from typing import get_type_hints
10
+ except ImportError: # pragma: no cover
11
+ from typing_extensions import get_type_hints
12
+
13
+ from ._markers import CompositeIndex
14
+ from ._types import is_indexed, is_searchable, python_type_to_sqlite, resolve_type
15
+
16
+ # ── helpers ────────────────────────────────────────────────────────────── #
17
+
18
+ def model_to_table_name(model: type[BaseModel]) -> str:
19
+ """``CamelCase`` → ``snake_case``."""
20
+ return re.sub(r"(?<!^)(?=[A-Z])", "_", model.__name__).lower()
21
+
22
+
23
+ def get_primary_key(model: type[BaseModel]) -> Optional[str]:
24
+ """Return the primary-key field name, or ``None`` (rowid implicit)."""
25
+ # Explicit override
26
+ pk = getattr(model, "__nyan_primary_key__", None)
27
+ if pk:
28
+ return pk
29
+ hints = get_type_hints(model, include_extras=True)
30
+ return "id" if "id" in hints else None
31
+
32
+
33
+ def model_hints(model: type[BaseModel]) -> dict[str, Any]:
34
+ return get_type_hints(model, include_extras=True)
35
+
36
+
37
+ # ── DDL ────────────────────────────────────────────────────────────────── #
38
+
39
+ def model_to_ddl(model: type[BaseModel]) -> str:
40
+ """Generate ``CREATE TABLE IF NOT EXISTS …`` DDL."""
41
+ table = model_to_table_name(model)
42
+ pk = get_primary_key(model)
43
+ hints = model_hints(model)
44
+
45
+ columns: list[str] = []
46
+ for field_name, annotation in hints.items():
47
+ base_type, is_optional = resolve_type(annotation)
48
+ sql_type = python_type_to_sqlite(annotation)
49
+
50
+ col = f'"{field_name}" {sql_type}'
51
+ if field_name == pk:
52
+ col += " PRIMARY KEY"
53
+ elif not is_optional:
54
+ col += " NOT NULL"
55
+ columns.append(col)
56
+
57
+ col_defs = ",\n ".join(columns)
58
+ return f'CREATE TABLE IF NOT EXISTS "{table}" (\n {col_defs}\n)'
59
+
60
+
61
+ # ── indexes ─────────────────────────────────────────────────────────────── #
62
+
63
+ def model_to_indexes(model: type[BaseModel]) -> list[str]:
64
+ """Generate ``CREATE [UNIQUE] INDEX IF NOT EXISTS …`` statements."""
65
+ table = model_to_table_name(model)
66
+ pk = get_primary_key(model)
67
+ hints = model_hints(model)
68
+ stmts: list[str] = []
69
+
70
+ # Per-field indexes from Indexed[T] / UniqueIndexed[T]
71
+ for field_name, annotation in hints.items():
72
+ if field_name == pk:
73
+ continue
74
+ indexed, unique = is_indexed(annotation)
75
+ if not indexed:
76
+ continue
77
+ u = "UNIQUE " if unique else ""
78
+ idx = f"idx_{table}_{field_name}"
79
+ stmts.append(
80
+ f'CREATE {u}INDEX IF NOT EXISTS "{idx}" ON "{table}"("{field_name}")'
81
+ )
82
+
83
+ # Composite indexes from __nyan_indexes__
84
+ for ci in getattr(model, "__nyan_indexes__", []):
85
+ if not isinstance(ci, CompositeIndex):
86
+ continue
87
+ u = "UNIQUE " if ci.unique else ""
88
+ idx = f'idx_{table}_{"_".join(ci.fields)}'
89
+ col_sql = ", ".join(f'"{f}"' for f in ci.fields)
90
+ stmts.append(
91
+ f'CREATE {u}INDEX IF NOT EXISTS "{idx}" ON "{table}"({col_sql})'
92
+ )
93
+
94
+ return stmts
95
+
96
+
97
+ # ── FTS5 virtual table + sync triggers ────────────────────────────────── #
98
+
99
+ def model_to_fts5(model: type[BaseModel]) -> tuple[Optional[str], list[str]]:
100
+ """Return ``(CREATE VIRTUAL TABLE stmt | None, [trigger stmts])``."""
101
+ table = model_to_table_name(model)
102
+ hints = model_hints(model)
103
+
104
+ s_fields = [f for f, ann in hints.items() if is_searchable(ann)]
105
+ if not s_fields:
106
+ return None, []
107
+
108
+ fts = f"{table}_fts"
109
+ cols = ", ".join(f'"{f}"' for f in s_fields)
110
+
111
+ create_fts = (
112
+ f'CREATE VIRTUAL TABLE IF NOT EXISTS "{fts}" USING fts5(\n'
113
+ f" {cols},\n"
114
+ f' content="{table}",\n'
115
+ f' content_rowid="rowid"\n'
116
+ f")"
117
+ )
118
+
119
+ new_vals = ", ".join(f'new."{f}"' for f in s_fields)
120
+ old_vals = ", ".join(f'old."{f}"' for f in s_fields)
121
+
122
+ triggers = [
123
+ # INSERT
124
+ (
125
+ f'CREATE TRIGGER IF NOT EXISTS "{table}_fts_ai"\n'
126
+ f' AFTER INSERT ON "{table}" BEGIN\n'
127
+ f' INSERT INTO "{fts}"(rowid, {cols})\n'
128
+ f" VALUES(new.rowid, {new_vals});\n"
129
+ f"END"
130
+ ),
131
+ # DELETE
132
+ (
133
+ f'CREATE TRIGGER IF NOT EXISTS "{table}_fts_ad"\n'
134
+ f' AFTER DELETE ON "{table}" BEGIN\n'
135
+ f' INSERT INTO "{fts}"("{fts}", rowid, {cols})\n'
136
+ f" VALUES('delete', old.rowid, {old_vals});\n"
137
+ f"END"
138
+ ),
139
+ # UPDATE
140
+ (
141
+ f'CREATE TRIGGER IF NOT EXISTS "{table}_fts_au"\n'
142
+ f' AFTER UPDATE ON "{table}" BEGIN\n'
143
+ f' INSERT INTO "{fts}"("{fts}", rowid, {cols})\n'
144
+ f" VALUES('delete', old.rowid, {old_vals});\n"
145
+ f' INSERT INTO "{fts}"(rowid, {cols})\n'
146
+ f" VALUES(new.rowid, {new_vals});\n"
147
+ f"END"
148
+ ),
149
+ ]
150
+
151
+ return create_fts, triggers
nyansqlite/_types.py ADDED
@@ -0,0 +1,120 @@
1
+ from __future__ import annotations
2
+
3
+ import json
4
+ import sys
5
+ from datetime import date, datetime
6
+ from typing import Any, Union, get_args, get_origin
7
+
8
+ try:
9
+ from typing import Annotated # 3.9+
10
+ except ImportError: # pragma: no cover
11
+ from typing import Annotated
12
+
13
+ from ._markers import _NyanIndexedMarker, _NyanSearchableMarker
14
+
15
+ # ── annotation inspection ──────────────────────────────────────────────── #
16
+
17
+ def is_indexed(annotation: Any) -> tuple[bool, bool]:
18
+ """Return ``(is_indexed, is_unique)``."""
19
+ if get_origin(annotation) is Annotated:
20
+ for meta in get_args(annotation)[1:]:
21
+ if isinstance(meta, _NyanIndexedMarker):
22
+ return True, meta.unique
23
+ return False, False
24
+
25
+
26
+ def is_searchable(annotation: Any) -> bool:
27
+ if get_origin(annotation) is Annotated:
28
+ return any(isinstance(m, _NyanSearchableMarker) for m in get_args(annotation)[1:])
29
+ return False
30
+
31
+
32
+ def unwrap_annotated(annotation: Any) -> Any:
33
+ """Strip the ``Annotated[T, ...]`` wrapper and return ``T``."""
34
+ if get_origin(annotation) is Annotated:
35
+ return get_args(annotation)[0]
36
+ return annotation
37
+
38
+
39
+ def resolve_type(annotation: Any) -> tuple[Any, bool]:
40
+ """Return ``(base_type, is_optional)`` after stripping Annotated and Optional."""
41
+ inner = unwrap_annotated(annotation)
42
+
43
+ origin = get_origin(inner)
44
+
45
+ # typing.Union / Optional[T] / Python 3.10+ T | None
46
+ if origin is Union or (sys.version_info >= (3, 10) and _is_union_type(inner)):
47
+ args = get_args(inner)
48
+ non_none = [a for a in args if a is not type(None)]
49
+ is_opt = type(None) in args
50
+ return (non_none[0] if len(non_none) == 1 else inner), is_opt
51
+
52
+ return inner, False
53
+
54
+
55
+ def _is_union_type(tp: Any) -> bool:
56
+ import types
57
+ return isinstance(tp, getattr(types, "UnionType", type(None)))
58
+
59
+
60
+ # ── Python → SQLite type map ───────────────────────────────────────────── #
61
+
62
+ _PY_TO_SQL: dict[Any, str] = {
63
+ int: "INTEGER",
64
+ float: "REAL",
65
+ str: "TEXT",
66
+ bytes: "BLOB",
67
+ bool: "INTEGER",
68
+ datetime: "TEXT",
69
+ date: "TEXT",
70
+ dict: "TEXT", # JSON
71
+ list: "TEXT", # JSON
72
+ }
73
+
74
+
75
+ def python_type_to_sqlite(annotation: Any) -> str:
76
+ base, _ = resolve_type(annotation)
77
+ origin = get_origin(base)
78
+ if origin in (list, dict):
79
+ return "TEXT"
80
+ return _PY_TO_SQL.get(base, "TEXT")
81
+
82
+
83
+ # ── value serialization ────────────────────────────────────────────────── #
84
+
85
+ def serialize_value(value: Any, annotation: Any) -> Any:
86
+ """Convert a Python value to a SQLite-storable scalar."""
87
+ if value is None:
88
+ return None
89
+ base, _ = resolve_type(annotation)
90
+ origin = get_origin(base)
91
+
92
+ if isinstance(value, bool):
93
+ return int(value)
94
+ if isinstance(value, (dict, list)) or origin in (dict, list):
95
+ return json.dumps(value, ensure_ascii=False, default=str)
96
+ if isinstance(value, datetime):
97
+ return value.isoformat()
98
+ if isinstance(value, date):
99
+ return value.isoformat()
100
+ return value
101
+
102
+
103
+ def deserialize_value(value: Any, annotation: Any) -> Any:
104
+ """Convert a SQLite scalar back to the correct Python type."""
105
+ if value is None:
106
+ return None
107
+ base, _ = resolve_type(annotation)
108
+ origin = get_origin(base)
109
+
110
+ if base is bool:
111
+ return bool(value)
112
+ if origin in (dict, list) or base in (dict, list):
113
+ if isinstance(value, str):
114
+ return json.loads(value)
115
+ return value
116
+ if base is datetime:
117
+ return datetime.fromisoformat(value)
118
+ if base is date:
119
+ return date.fromisoformat(value)
120
+ return value