sqliter-py 0.9.0__py3-none-any.whl → 0.16.0__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.
Files changed (44) hide show
  1. sqliter/constants.py +4 -3
  2. sqliter/exceptions.py +43 -0
  3. sqliter/model/__init__.py +38 -3
  4. sqliter/model/foreign_key.py +153 -0
  5. sqliter/model/model.py +42 -3
  6. sqliter/model/unique.py +20 -11
  7. sqliter/orm/__init__.py +16 -0
  8. sqliter/orm/fields.py +412 -0
  9. sqliter/orm/foreign_key.py +8 -0
  10. sqliter/orm/model.py +243 -0
  11. sqliter/orm/query.py +221 -0
  12. sqliter/orm/registry.py +169 -0
  13. sqliter/query/query.py +720 -69
  14. sqliter/sqliter.py +533 -76
  15. sqliter/tui/__init__.py +62 -0
  16. sqliter/tui/__main__.py +6 -0
  17. sqliter/tui/app.py +179 -0
  18. sqliter/tui/demos/__init__.py +96 -0
  19. sqliter/tui/demos/base.py +114 -0
  20. sqliter/tui/demos/caching.py +283 -0
  21. sqliter/tui/demos/connection.py +150 -0
  22. sqliter/tui/demos/constraints.py +211 -0
  23. sqliter/tui/demos/crud.py +154 -0
  24. sqliter/tui/demos/errors.py +231 -0
  25. sqliter/tui/demos/field_selection.py +150 -0
  26. sqliter/tui/demos/filters.py +389 -0
  27. sqliter/tui/demos/models.py +248 -0
  28. sqliter/tui/demos/ordering.py +156 -0
  29. sqliter/tui/demos/orm.py +460 -0
  30. sqliter/tui/demos/results.py +241 -0
  31. sqliter/tui/demos/string_filters.py +210 -0
  32. sqliter/tui/demos/timestamps.py +126 -0
  33. sqliter/tui/demos/transactions.py +177 -0
  34. sqliter/tui/runner.py +116 -0
  35. sqliter/tui/styles/app.tcss +130 -0
  36. sqliter/tui/widgets/__init__.py +7 -0
  37. sqliter/tui/widgets/code_display.py +81 -0
  38. sqliter/tui/widgets/demo_list.py +65 -0
  39. sqliter/tui/widgets/output_display.py +92 -0
  40. {sqliter_py-0.9.0.dist-info → sqliter_py-0.16.0.dist-info}/METADATA +27 -11
  41. sqliter_py-0.16.0.dist-info/RECORD +47 -0
  42. {sqliter_py-0.9.0.dist-info → sqliter_py-0.16.0.dist-info}/WHEEL +2 -2
  43. sqliter_py-0.16.0.dist-info/entry_points.txt +3 -0
  44. sqliter_py-0.9.0.dist-info/RECORD +0 -14
@@ -0,0 +1,156 @@
1
+ """Ordering & Pagination demos."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import io
6
+
7
+ from sqliter import SqliterDB
8
+ from sqliter.model import BaseDBModel
9
+ from sqliter.tui.demos.base import Demo, DemoCategory, extract_demo_code
10
+
11
+
12
+ def _run_order_asc() -> str:
13
+ """Sort query results in ascending order.
14
+
15
+ Use order(field_name) to sort results from lowest to highest.
16
+ """
17
+ output = io.StringIO()
18
+
19
+ class User(BaseDBModel):
20
+ name: str
21
+ age: int
22
+
23
+ db = SqliterDB(memory=True)
24
+ db.create_table(User)
25
+
26
+ db.insert(User(name="Charlie", age=35))
27
+ db.insert(User(name="Alice", age=25))
28
+ db.insert(User(name="Bob", age=30))
29
+
30
+ results = db.select(User).order("age").fetch_all()
31
+ output.write("Users ordered by age (ascending):\n")
32
+ for user in results:
33
+ output.write(f" - {user.name}: {user.age}\n")
34
+
35
+ db.close()
36
+ return output.getvalue()
37
+
38
+
39
+ def _run_order_desc() -> str:
40
+ """Sort query results in descending order.
41
+
42
+ Use order(field_name, reverse=True) to sort from highest to lowest.
43
+ """
44
+ output = io.StringIO()
45
+
46
+ class Product(BaseDBModel):
47
+ name: str
48
+ price: float
49
+
50
+ db = SqliterDB(memory=True)
51
+ db.create_table(Product)
52
+
53
+ db.insert(Product(name="Item A", price=10.0))
54
+ db.insert(Product(name="Item B", price=30.0))
55
+ db.insert(Product(name="Item C", price=20.0))
56
+
57
+ results = db.select(Product).order("price", reverse=True).fetch_all()
58
+ output.write("Products ordered by price (descending):\n")
59
+ for product in results:
60
+ output.write(f" - {product.name}: ${product.price}\n")
61
+
62
+ db.close()
63
+ return output.getvalue()
64
+
65
+
66
+ def _run_limit() -> str:
67
+ """Limit the number of results returned.
68
+
69
+ Use limit(count) to fetch only the first N records.
70
+ """
71
+ output = io.StringIO()
72
+
73
+ class Article(BaseDBModel):
74
+ title: str
75
+
76
+ db = SqliterDB(memory=True)
77
+ db.create_table(Article)
78
+
79
+ for i in range(1, 11):
80
+ db.insert(Article(title=f"Article {i}"))
81
+
82
+ results = db.select(Article).limit(3).fetch_all()
83
+ output.write("Top 3 articles:\n")
84
+ for article in results:
85
+ output.write(f" - {article.title}\n")
86
+
87
+ db.close()
88
+ return output.getvalue()
89
+
90
+
91
+ def _run_offset() -> str:
92
+ """Skip a specified number of results.
93
+
94
+ Use offset(count) with limit() for pagination, skipping first N records.
95
+ """
96
+ output = io.StringIO()
97
+
98
+ class Item(BaseDBModel):
99
+ name: str
100
+
101
+ db = SqliterDB(memory=True)
102
+ db.create_table(Item)
103
+
104
+ for i in range(1, 11):
105
+ db.insert(Item(name=f"Item {i}"))
106
+
107
+ results = db.select(Item).limit(5).offset(5).fetch_all()
108
+ output.write("Items 6-10:\n")
109
+ for item in results:
110
+ output.write(f" - {item.name}\n")
111
+
112
+ db.close()
113
+ return output.getvalue()
114
+
115
+
116
+ def get_category() -> DemoCategory:
117
+ """Get the Ordering & Pagination demo category."""
118
+ return DemoCategory(
119
+ id="ordering",
120
+ title="Ordering & Pagination",
121
+ icon="",
122
+ demos=[
123
+ Demo(
124
+ id="order_asc",
125
+ title="Order Ascending",
126
+ description="Sort results in ascending order",
127
+ category="ordering",
128
+ code=extract_demo_code(_run_order_asc),
129
+ execute=_run_order_asc,
130
+ ),
131
+ Demo(
132
+ id="order_desc",
133
+ title="Order Descending",
134
+ description="Sort results in descending order",
135
+ category="ordering",
136
+ code=extract_demo_code(_run_order_desc),
137
+ execute=_run_order_desc,
138
+ ),
139
+ Demo(
140
+ id="paginate_limit",
141
+ title="Limit Results",
142
+ description="Limit number of results",
143
+ category="ordering",
144
+ code=extract_demo_code(_run_limit),
145
+ execute=_run_limit,
146
+ ),
147
+ Demo(
148
+ id="paginate_offset",
149
+ title="Offset Results",
150
+ description="Skip records for pagination",
151
+ category="ordering",
152
+ code=extract_demo_code(_run_offset),
153
+ execute=_run_offset,
154
+ ),
155
+ ],
156
+ )
@@ -0,0 +1,460 @@
1
+ """ORM Features demos."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import io
6
+ from typing import Optional
7
+
8
+ from sqliter import SqliterDB
9
+ from sqliter.orm import BaseDBModel, ForeignKey
10
+ from sqliter.tui.demos.base import Demo, DemoCategory, extract_demo_code
11
+
12
+
13
+ def _run_lazy_loading() -> str:
14
+ """Load related objects on-demand using foreign keys.
15
+
16
+ Accessing a ForeignKey field triggers a database query to fetch the
17
+ related object only when you need it.
18
+ """
19
+ output = io.StringIO()
20
+
21
+ class Author(BaseDBModel):
22
+ name: str
23
+
24
+ class Book(BaseDBModel):
25
+ title: str
26
+ author: ForeignKey[Author] = ForeignKey(Author)
27
+
28
+ db = SqliterDB(memory=True)
29
+ db.create_table(Author)
30
+ db.create_table(Book)
31
+
32
+ author = db.insert(Author(name="J.K. Rowling"))
33
+ book1 = db.insert(Book(title="Harry Potter 1", author=author))
34
+ book2 = db.insert(Book(title="Harry Potter 2", author=author))
35
+
36
+ output.write(f"Author: {author.name}\n")
37
+ output.write(f"Author ID: {author.pk}\n")
38
+
39
+ # Access related author through foreign key - triggers lazy load
40
+ output.write("\nAccessing book.author triggers lazy load:\n")
41
+ output.write(f" '{book1.title}' was written by {book1.author.name}\n")
42
+
43
+ output.write(f"\n'{book2.title}' was written by {book2.author.name}\n")
44
+ output.write("Related objects loaded on-demand from database\n")
45
+
46
+ db.close()
47
+ return output.getvalue()
48
+
49
+
50
+ def _run_orm_style_access() -> str:
51
+ """Insert records with foreign key relationships.
52
+
53
+ BaseDBModel provides attribute-style access to fields, with automatic
54
+ primary key generation via the pk field. Foreign keys store related
55
+ object primary keys.
56
+ """
57
+ output = io.StringIO()
58
+
59
+ class Author(BaseDBModel):
60
+ name: str
61
+
62
+ class Book(BaseDBModel):
63
+ title: str
64
+ author: ForeignKey[Author] = ForeignKey(Author)
65
+
66
+ db = SqliterDB(memory=True)
67
+ db.create_table(Author)
68
+ db.create_table(Book)
69
+
70
+ author = db.insert(Author(name="Jane Austen"))
71
+ book = db.insert(Book(title="Pride and Prejudice", author=author))
72
+
73
+ output.write("Created book:\n")
74
+ output.write(f" title: {book.title}\n")
75
+ output.write(f" author: {book.author.name}\n")
76
+ output.write(
77
+ "\nForeign key stores the primary key internally,\n"
78
+ "but access returns the object\n"
79
+ )
80
+
81
+ db.close()
82
+ return output.getvalue()
83
+
84
+
85
+ def _run_nullable_foreign_key() -> str:
86
+ """Declare nullable FKs using Optional[T] in the type annotation.
87
+
88
+ SQLiter auto-detects nullability from the annotation so you don't
89
+ need to pass null=True explicitly.
90
+
91
+ Note: this demo already uses ForeignKey[Optional[Author]], but
92
+ annotation-based nullability is most reliable when models are defined at
93
+ module level (especially if you use type aliases). We include null=True
94
+ here for compatibility.
95
+ """
96
+ output = io.StringIO()
97
+
98
+ class Author(BaseDBModel):
99
+ name: str
100
+
101
+ class Book(BaseDBModel):
102
+ title: str
103
+ author: ForeignKey[Optional[Author]] = ForeignKey(
104
+ Author, on_delete="SET NULL", null=True
105
+ )
106
+
107
+ db = SqliterDB(memory=True)
108
+ db.create_table(Author)
109
+ db.create_table(Book)
110
+
111
+ author = db.insert(Author(name="Jane Austen"))
112
+ book_with = db.insert(Book(title="Pride and Prejudice", author=author))
113
+ book_without = db.insert(Book(title="Anonymous Work", author=None))
114
+
115
+ book1 = db.get(Book, book_with.pk)
116
+ book2 = db.get(Book, book_without.pk)
117
+
118
+ if book1 is not None:
119
+ author_name = book1.author.name if book1.author else "None"
120
+ output.write(f"'{book1.title}' author: {author_name}\n")
121
+ if book2 is not None:
122
+ output.write(f"'{book2.title}' author: {book2.author}\n")
123
+
124
+ output.write("\nOptional[Author] auto-sets null=True on the FK column\n")
125
+
126
+ db.close()
127
+ return output.getvalue()
128
+
129
+
130
+ def _run_relationship_navigation() -> str:
131
+ """Navigate from one object to another using foreign keys.
132
+
133
+ ForeignKey fields let you traverse relationships by accessing
134
+ related objects as attributes.
135
+ """
136
+ output = io.StringIO()
137
+
138
+ class Team(BaseDBModel):
139
+ name: str
140
+
141
+ class Player(BaseDBModel):
142
+ name: str
143
+ team: ForeignKey[Team] = ForeignKey(Team)
144
+
145
+ db = SqliterDB(memory=True)
146
+ db.create_table(Team)
147
+ db.create_table(Player)
148
+
149
+ team = db.insert(Team(name="Lakers"))
150
+ player1 = db.insert(Player(name="LeBron", team=team))
151
+ player2 = db.insert(Player(name="Davis", team=team))
152
+
153
+ output.write(f"Team: {team.name}\n")
154
+
155
+ # Navigate from player to team via FK
156
+ output.write(f"\n{player1.name} plays for: {player1.team.name}\n")
157
+ output.write(f"{player2.name} plays for: {player2.team.name}\n")
158
+ output.write("Foreign keys enable relationship navigation\n")
159
+
160
+ db.close()
161
+ return output.getvalue()
162
+
163
+
164
+ def _run_reverse_relationships() -> str:
165
+ """Access related objects in reverse using related_name.
166
+
167
+ When you define a ForeignKey, SQLiter automatically creates a reverse
168
+ relationship to access all objects that reference a given object.
169
+ """
170
+ output = io.StringIO()
171
+
172
+ class Author(BaseDBModel):
173
+ name: str
174
+
175
+ class Book(BaseDBModel):
176
+ title: str
177
+ author: ForeignKey[Author] = ForeignKey(Author, related_name="books")
178
+
179
+ db = SqliterDB(memory=True)
180
+ db.create_table(Author)
181
+ db.create_table(Book)
182
+
183
+ author = db.insert(Author(name="Jane Austen"))
184
+ db.insert(Book(title="Pride and Prejudice", author=author))
185
+ db.insert(Book(title="Emma", author=author))
186
+ db.insert(Book(title="Sense and Sensibility", author=author))
187
+
188
+ output.write(f"Author: {author.name}\n")
189
+
190
+ # Access reverse relationship - get all books by this author
191
+ # Note: 'books' attribute added dynamically by ForeignKey descriptor
192
+ output.write("\nAccessing author.books (reverse relationship):\n")
193
+ reverse_attr = "books" # Dynamic attribute added by FK descriptor
194
+ books_query = getattr(author, reverse_attr)
195
+ books = books_query.fetch_all()
196
+ for book in books:
197
+ output.write(f" - {book.title}\n")
198
+
199
+ output.write(f"\nTotal books: {len(books)}\n")
200
+ output.write("Reverse relationships auto-generated from FKs\n")
201
+
202
+ db.close()
203
+ return output.getvalue()
204
+
205
+
206
+ def _run_select_related_basic() -> str:
207
+ """Demonstrate eager loading with select_related().
208
+
209
+ Shows how select_related() fetches related objects in a single JOIN query
210
+ instead of lazy loading (which causes N+1 queries).
211
+ """
212
+ output = io.StringIO()
213
+
214
+ class Author(BaseDBModel):
215
+ name: str
216
+
217
+ class Book(BaseDBModel):
218
+ title: str
219
+ author: ForeignKey[Author] = ForeignKey(Author)
220
+
221
+ db = SqliterDB(memory=True)
222
+ db.create_table(Author)
223
+ db.create_table(Book)
224
+
225
+ # Insert test data
226
+ author1 = db.insert(Author(name="Jane Austen"))
227
+ author2 = db.insert(Author(name="Charles Dickens"))
228
+
229
+ db.insert(Book(title="Pride and Prejudice", author=author1))
230
+ db.insert(Book(title="Emma", author=author1))
231
+ db.insert(Book(title="Oliver Twist", author=author2))
232
+
233
+ # Eager load - single JOIN query
234
+ output.write("Fetching books with eager loading:\n")
235
+ books = db.select(Book).select_related("author").fetch_all()
236
+
237
+ for book in books:
238
+ output.write(f" '{book.title}' by {book.author.name}\n")
239
+
240
+ output.write("\nAll authors loaded in single query (no N+1 problem)\n")
241
+
242
+ db.close()
243
+ return output.getvalue()
244
+
245
+
246
+ def _run_select_related_nested() -> str:
247
+ """Demonstrate nested relationship eager loading.
248
+
249
+ Shows how to load nested relationships using double underscore syntax:
250
+ select_related("book__author") loads both Book and Author in one query.
251
+ """
252
+ output = io.StringIO()
253
+
254
+ class Author(BaseDBModel):
255
+ name: str
256
+
257
+ class Book(BaseDBModel):
258
+ title: str
259
+ author: ForeignKey[Author] = ForeignKey(Author)
260
+
261
+ class Comment(BaseDBModel):
262
+ text: str
263
+ book: ForeignKey[Book] = ForeignKey(Book)
264
+
265
+ db = SqliterDB(memory=True)
266
+ db.create_table(Author)
267
+ db.create_table(Book)
268
+ db.create_table(Comment)
269
+
270
+ # Insert nested test data
271
+ author = db.insert(Author(name="Jane Austen"))
272
+ book = db.insert(Book(title="Pride and Prejudice", author=author))
273
+ db.insert(Comment(text="Amazing book!", book=book))
274
+
275
+ # Load nested relationship - single query joins Comment -> Book -> Author
276
+ output.write("Loading nested relationships:\n")
277
+ comment = db.select(Comment).select_related("book__author").fetch_one()
278
+
279
+ if comment is not None:
280
+ output.write(f"Comment: {comment.text}\n")
281
+ output.write(f"Book: {comment.book.title}\n")
282
+ # Access author through book's foreign key relationship
283
+ # Both book and author were loaded in a single JOIN query
284
+ output.write(f"Author: {comment.book.author.name}\n")
285
+
286
+ output.write("\nNested relationships loaded in single query\n")
287
+
288
+ db.close()
289
+ return output.getvalue()
290
+
291
+
292
+ def _run_relationship_filter_traversal() -> str:
293
+ """Demonstrate relationship filter traversal.
294
+
295
+ Shows how to filter by fields on related models using double underscore
296
+ syntax: filter(author__name="Jane Austen")
297
+ """
298
+ output = io.StringIO()
299
+
300
+ class Author(BaseDBModel):
301
+ name: str
302
+
303
+ class Book(BaseDBModel):
304
+ title: str
305
+ author: ForeignKey[Author] = ForeignKey(Author)
306
+
307
+ db = SqliterDB(memory=True)
308
+ db.create_table(Author)
309
+ db.create_table(Book)
310
+
311
+ # Insert test data
312
+ author1 = db.insert(Author(name="Jane Austen"))
313
+ author2 = db.insert(Author(name="Charles Dickens"))
314
+
315
+ db.insert(Book(title="Pride and Prejudice", author=author1))
316
+ db.insert(Book(title="Emma", author=author1))
317
+ db.insert(Book(title="Oliver Twist", author=author2))
318
+ db.insert(Book(title="Great Expectations", author=author2))
319
+
320
+ # Filter by related field
321
+ output.write("Filtering by author name:\n")
322
+ books = db.select(Book).filter(author__name="Jane Austen").fetch_all()
323
+
324
+ for book in books:
325
+ output.write(f" {book.title}\n")
326
+
327
+ output.write(f"\nFound {len(books)} book(s) by Jane Austen\n")
328
+ output.write("(Automatic JOIN added behind the scenes)\n")
329
+
330
+ db.close()
331
+ return output.getvalue()
332
+
333
+
334
+ def _run_select_related_combined() -> str:
335
+ """Demonstrate combining select_related() with relationship filters.
336
+
337
+ Shows how to use select_related() with filter() for optimal performance:
338
+ load related objects AND filter by them in a single query.
339
+ """
340
+ output = io.StringIO()
341
+
342
+ class Author(BaseDBModel):
343
+ name: str
344
+
345
+ class Book(BaseDBModel):
346
+ title: str
347
+ year: int
348
+ author: ForeignKey[Author] = ForeignKey(Author)
349
+
350
+ db = SqliterDB(memory=True)
351
+ db.create_table(Author)
352
+ db.create_table(Book)
353
+
354
+ # Insert test data
355
+ author1 = db.insert(Author(name="Jane Austen"))
356
+ author2 = db.insert(Author(name="Charles Dickens"))
357
+
358
+ db.insert(Book(title="Pride and Prejudice", year=1813, author=author1))
359
+ db.insert(Book(title="Emma", year=1815, author=author1))
360
+ db.insert(Book(title="Oliver Twist", year=1838, author=author2))
361
+
362
+ # Combine filter + eager load
363
+ output.write("Filter and eager load in single query:\n")
364
+ books = (
365
+ db.select(Book)
366
+ .select_related("author")
367
+ .filter(author__name__startswith="Jane")
368
+ .fetch_all()
369
+ )
370
+
371
+ for book in books:
372
+ output.write(f" {book.title} ({book.year}) by {book.author.name}\n")
373
+
374
+ output.write(f"\n{len(books)} result(s) with authors preloaded\n")
375
+
376
+ db.close()
377
+ return output.getvalue()
378
+
379
+
380
+ def get_category() -> DemoCategory:
381
+ """Get the ORM Features demo category."""
382
+ return DemoCategory(
383
+ id="orm",
384
+ title="ORM Features",
385
+ icon="",
386
+ demos=[
387
+ Demo(
388
+ id="orm_lazy",
389
+ title="Lazy Loading",
390
+ description="Load related data on demand",
391
+ category="orm",
392
+ code=extract_demo_code(_run_lazy_loading),
393
+ execute=_run_lazy_loading,
394
+ ),
395
+ Demo(
396
+ id="orm_fk_insert",
397
+ title="Inserting with Foreign Keys",
398
+ description="Create records linked to other records",
399
+ category="orm",
400
+ code=extract_demo_code(_run_orm_style_access),
401
+ execute=_run_orm_style_access,
402
+ ),
403
+ Demo(
404
+ id="orm_nullable_fk",
405
+ title="Nullable Foreign Keys",
406
+ description="Auto-detect nullable FKs from annotations",
407
+ category="orm",
408
+ code=extract_demo_code(_run_nullable_foreign_key),
409
+ execute=_run_nullable_foreign_key,
410
+ ),
411
+ Demo(
412
+ id="orm_relationships",
413
+ title="Relationship Navigation",
414
+ description="Navigate using foreign keys",
415
+ category="orm",
416
+ code=extract_demo_code(_run_relationship_navigation),
417
+ execute=_run_relationship_navigation,
418
+ ),
419
+ Demo(
420
+ id="orm_reverse",
421
+ title="Reverse Relationships",
422
+ description="Access related objects via related_name",
423
+ category="orm",
424
+ code=extract_demo_code(_run_reverse_relationships),
425
+ execute=_run_reverse_relationships,
426
+ ),
427
+ Demo(
428
+ id="orm_select_related",
429
+ title="Eager Loading with select_related()",
430
+ description="Fetch related objects in a single JOIN query",
431
+ category="orm",
432
+ code=extract_demo_code(_run_select_related_basic),
433
+ execute=_run_select_related_basic,
434
+ ),
435
+ Demo(
436
+ id="orm_select_related_nested",
437
+ title="Nested Relationship Loading",
438
+ description="Load nested relationships with double underscore",
439
+ category="orm",
440
+ code=extract_demo_code(_run_select_related_nested),
441
+ execute=_run_select_related_nested,
442
+ ),
443
+ Demo(
444
+ id="orm_filter_traversal",
445
+ title="Relationship Filter Traversal",
446
+ description="Filter by related object fields",
447
+ category="orm",
448
+ code=extract_demo_code(_run_relationship_filter_traversal),
449
+ execute=_run_relationship_filter_traversal,
450
+ ),
451
+ Demo(
452
+ id="orm_select_related_combined",
453
+ title="Combining select_related with Filters",
454
+ description="Eager load and filter by relationships",
455
+ category="orm",
456
+ code=extract_demo_code(_run_select_related_combined),
457
+ execute=_run_select_related_combined,
458
+ ),
459
+ ],
460
+ )