sqliter-py 0.1.0__tar.gz → 0.2.0__tar.gz

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.

Potentially problematic release.


This version of sqliter-py might be problematic. Click here for more details.

@@ -215,3 +215,5 @@ pyrightconfig.json
215
215
  .python-version
216
216
  *.db
217
217
  .vscode
218
+ .changelog_generator.toml
219
+ repopack-output.xml
@@ -0,0 +1,351 @@
1
+ Metadata-Version: 2.3
2
+ Name: sqliter-py
3
+ Version: 0.2.0
4
+ Summary: Interact with SQLite databases using Python and Pydantic
5
+ Project-URL: Pull Requests, https://github.com/seapagan/sqliter-py/pulls
6
+ Project-URL: Bug Tracker, https://github.com/seapagan/sqliter-py/issues
7
+ Project-URL: Changelog, https://github.com/seapagan/sqliter-py/blob/main/CHANGELOG.md
8
+ Project-URL: Repository, https://github.com/seapagan/sqliter-py
9
+ Author-email: Grant Ramsay <grant@gnramsay.com>
10
+ License-Expression: MIT
11
+ Classifier: Development Status :: 4 - Beta
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: License :: OSI Approved :: MIT License
14
+ Classifier: Operating System :: OS Independent
15
+ Classifier: Programming Language :: Python :: 3
16
+ Classifier: Programming Language :: Python :: 3.9
17
+ Classifier: Programming Language :: Python :: 3.10
18
+ Classifier: Programming Language :: Python :: 3.11
19
+ Classifier: Programming Language :: Python :: 3.12
20
+ Classifier: Topic :: Software Development
21
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
22
+ Requires-Python: >=3.9
23
+ Requires-Dist: pydantic>=2.9.0
24
+ Description-Content-Type: text/markdown
25
+
26
+ # SQLiter <!-- omit in toc -->
27
+
28
+ ![PyPI - Version](https://img.shields.io/pypi/v/sqliter-py)
29
+ [![Test Suite](https://github.com/seapagan/sqliter-py/actions/workflows/testing.yml/badge.svg)](https://github.com/seapagan/sqliter-py/actions/workflows/testing.yml)
30
+ [![Linting](https://github.com/seapagan/sqliter-py/actions/workflows/linting.yml/badge.svg)](https://github.com/seapagan/sqliter-py/actions/workflows/linting.yml)
31
+ [![Type Checking](https://github.com/seapagan/sqliter-py/actions/workflows/mypy.yml/badge.svg)](https://github.com/seapagan/sqliter-py/actions/workflows/mypy.yml)
32
+ ![PyPI - Python Version](https://img.shields.io/pypi/pyversions/sqliter-py)
33
+
34
+ SQLiter is a lightweight Object-Relational Mapping (ORM) library for SQLite
35
+ databases in Python. It provides a simplified interface for interacting with
36
+ SQLite databases using Pydantic models. The only external run-time dependency
37
+ is Pydantic itself.
38
+
39
+ It does not aim to be a full-fledged ORM like SQLAlchemy, but rather a simple
40
+ and easy-to-use library for basic database operations, especially for small
41
+ projects. It is NOT asynchronous and does not support complex queries (at this
42
+ time).
43
+
44
+ The ideal use case is more for Python CLI tools that need to store data in a
45
+ database-like format without needing to learn SQL or use a full ORM.
46
+
47
+ > [!NOTE]
48
+ > This project is still in the early stages of development and is lacking some
49
+ > planned functionality. Please use with caution.
50
+ >
51
+ > See the [TODO](TODO.md) for planned features and improvements.
52
+
53
+ - [Features](#features)
54
+ - [Installation](#installation)
55
+ - [Quick Start](#quick-start)
56
+ - [Detailed Usage](#detailed-usage)
57
+ - [Defining Models](#defining-models)
58
+ - [Database Operations](#database-operations)
59
+ - [Creating a Connection](#creating-a-connection)
60
+ - [Creating Tables](#creating-tables)
61
+ - [Inserting Records](#inserting-records)
62
+ - [Querying Records](#querying-records)
63
+ - [Updating Records](#updating-records)
64
+ - [Deleting Records](#deleting-records)
65
+ - [Commit your changes](#commit-your-changes)
66
+ - [Close the Connection](#close-the-connection)
67
+ - [Transactions](#transactions)
68
+ - [Filter Options](#filter-options)
69
+ - [Basic Filters](#basic-filters)
70
+ - [Null Checks](#null-checks)
71
+ - [Comparison Operators](#comparison-operators)
72
+ - [List Operations](#list-operations)
73
+ - [String Operations (Case-Sensitive)](#string-operations-case-sensitive)
74
+ - [String Operations (Case-Insensitive)](#string-operations-case-insensitive)
75
+ - [Contributing](#contributing)
76
+ - [License](#license)
77
+ - [Acknowledgements](#acknowledgements)
78
+
79
+ ## Features
80
+
81
+ - Table creation based on Pydantic models
82
+ - CRUD operations (Create, Read, Update, Delete)
83
+ - Basic query building with filtering, ordering, and pagination
84
+ - Transaction support
85
+ - Custom exceptions for better error handling
86
+
87
+ ## Installation
88
+
89
+ You can install SQLiter using whichever method you prefer or is compatible with
90
+ your project setup.
91
+
92
+ With `pip`:
93
+
94
+ ```bash
95
+ pip install sqliter-py
96
+ ```
97
+
98
+ Or `Poetry`:
99
+
100
+ ```bash
101
+ poetry add sqliter-py
102
+ ```
103
+
104
+ Or `uv` which is rapidly becoming my favorite tool for managing projects and
105
+ virtual environments (`uv` is used for developing this project and in the CI):
106
+
107
+ ```bash
108
+ uv add sqliter-py
109
+ ```
110
+
111
+ ## Quick Start
112
+
113
+ Here's a quick example of how to use SQLiter:
114
+
115
+ ```python
116
+ from sqliter import SqliterDB
117
+ from sqliter.model import BaseDBModel
118
+
119
+ # Define your model
120
+ class User(BaseDBModel):
121
+ name: str
122
+ age: int
123
+
124
+ class Meta:
125
+ table_name = "users"
126
+
127
+ # Create a database connection
128
+ db = SqliterDB("example.db")
129
+
130
+ # Create the table
131
+ db.create_table(User)
132
+
133
+ # Insert a record
134
+ user = User(name="John Doe", age=30)
135
+ db.insert(user)
136
+
137
+ # Query records
138
+ results = db.select(User).filter(name="John Doe").fetch_all()
139
+ for user in results:
140
+ print(f"User: {user.name}, Age: {user.age}")
141
+
142
+ # Update a record
143
+ user.age = 31
144
+ db.update(user)
145
+
146
+ # Delete a record
147
+ db.delete(User, "John Doe")
148
+ ```
149
+
150
+ ## Detailed Usage
151
+
152
+ ### Defining Models
153
+
154
+ Models in SQLiter are based on Pydantic's `BaseModel`. You can define your
155
+ models like this:
156
+
157
+ ```python
158
+ from sqliter.model import BaseDBModel
159
+
160
+ class User(BaseDBModel):
161
+ name: str
162
+ age: int
163
+ email: str
164
+
165
+ class Meta:
166
+ table_name = "users"
167
+ primary_key = "name" # Default is "id"
168
+ create_id = False # Set to True to auto-create an ID field
169
+ ```
170
+
171
+ ### Database Operations
172
+
173
+ #### Creating a Connection
174
+
175
+ ```python
176
+ from sqliter import SqliterDB
177
+
178
+ db = SqliterDB("your_database.db")
179
+ ```
180
+
181
+ The default behavior is to automatically commit changes to the database after
182
+ each operation. If you want to disable this behavior, you can set `auto_commit=False`
183
+ when creating the database connection:
184
+
185
+ ```python
186
+ db = SqliterDB("your_database.db", auto_commit=False)
187
+ ```
188
+
189
+ It is then up to you to manually commit changes using the `commit()` method.
190
+ This can be useful when you want to perform multiple operations in a single
191
+ transaction without the overhead of committing after each operation.
192
+
193
+ #### Creating Tables
194
+
195
+ ```python
196
+ db.create_table(User)
197
+ ```
198
+
199
+ #### Inserting Records
200
+
201
+ ```python
202
+ user = User(name="Jane Doe", age=25, email="jane@example.com")
203
+ db.insert(user)
204
+ ```
205
+
206
+ #### Querying Records
207
+
208
+ ```python
209
+ # Fetch all users
210
+ all_users = db.select(User).fetch_all()
211
+
212
+ # Filter users
213
+ young_users = db.select(User).filter(age=25).fetch_all()
214
+
215
+ # Order users
216
+ ordered_users = db.select(User).order("age", direction="DESC").fetch_all()
217
+
218
+ # Limit and offset
219
+ paginated_users = db.select(User).limit(10).offset(20).fetch_all()
220
+ ```
221
+
222
+ See below for more advanced filtering options.
223
+
224
+ #### Updating Records
225
+
226
+ ```python
227
+ user.age = 26
228
+ db.update(user)
229
+ ```
230
+
231
+ #### Deleting Records
232
+
233
+ ```python
234
+ db.delete(User, "Jane Doe")
235
+ ```
236
+
237
+ #### Commit your changes
238
+
239
+ By default, SQLiter will automatically commit changes to the database after each
240
+ operation. If you want to disable this behavior, you can set `auto_commit=False`
241
+ when creating the database connection:
242
+
243
+ ```python
244
+ db = SqliterDB("your_database.db", auto_commit=False)
245
+ ```
246
+
247
+ You can then manually commit changes using the `commit()` method:
248
+
249
+ ```python
250
+ db.commit()
251
+ ```
252
+
253
+ #### Close the Connection
254
+
255
+ When you're done with the database connection, you should close it to release
256
+ resources:
257
+
258
+ ```python
259
+ db.close()
260
+ ```
261
+
262
+ Note that closing the connection will also commit any pending changes, unless
263
+ `auto_commit` is set to `False`.
264
+
265
+ ### Transactions
266
+
267
+ SQLiter supports transactions using Python's context manager:
268
+
269
+ ```python
270
+ with db:
271
+ db.insert(User(name="Alice", age=30, email="alice@example.com"))
272
+ db.insert(User(name="Bob", age=35, email="bob@example.com"))
273
+ # If an exception occurs, the transaction will be rolled back
274
+ ```
275
+
276
+ > [!WARNING]
277
+ > Using the context manager will automatically commit the transaction
278
+ > at the end (unless an exception occurs), regardless of the `auto_commit`
279
+ > setting.
280
+ >
281
+ > the 'close()' method will also be called when the context manager exits, so you
282
+ > do not need to call it manually.
283
+
284
+ ### Filter Options
285
+
286
+ The `filter()` method in SQLiter supports various filter options to query records.
287
+
288
+ #### Basic Filters
289
+
290
+ - `__eq`: Equal to (default if no operator is specified)
291
+ - Example: `name="John"` or `name__eq="John"`
292
+
293
+ #### Null Checks
294
+
295
+ - `__isnull`: Is NULL
296
+ - Example: `email__isnull=True`
297
+ - `__notnull`: Is NOT NULL
298
+ - Example: `email__notnull=True`
299
+
300
+ #### Comparison Operators
301
+
302
+ - `__lt`: Less than
303
+ - Example: `age__lt=30`
304
+ - `__lte`: Less than or equal to
305
+ - Example: `age__lte=30`
306
+ - `__gt`: Greater than
307
+ - Example: `age__gt=30`
308
+ - `__gte`: Greater than or equal to
309
+ - Example: `age__gte=30`
310
+ - `__ne`: Not equal to
311
+ - Example: `status__ne="inactive"`
312
+
313
+ #### List Operations
314
+
315
+ - `__in`: In a list of values
316
+ - Example: `status__in=["active", "pending"]`
317
+ - `__not_in`: Not in a list of values
318
+ - Example: `category__not_in=["archived", "deleted"]`
319
+
320
+ #### String Operations (Case-Sensitive)
321
+
322
+ - `__startswith`: Starts with
323
+ - Example: `name__startswith="A"`
324
+ - `__endswith`: Ends with
325
+ - Example: `email__endswith=".com"`
326
+ - `__contains`: Contains
327
+ - Example: `description__contains="important"`
328
+
329
+ #### String Operations (Case-Insensitive)
330
+
331
+ - `__istartswith`: Starts with (case-insensitive)
332
+ - Example: `name__istartswith="a"`
333
+ - `__iendswith`: Ends with (case-insensitive)
334
+ - Example: `email__iendswith=".COM"`
335
+ - `__icontains`: Contains (case-insensitive)
336
+ - Example: `description__icontains="IMPORTANT"`
337
+
338
+ ## Contributing
339
+
340
+ Contributions are welcome! Please feel free to submit a Pull Request.
341
+
342
+ ## License
343
+
344
+ This project is licensed under the MIT License.
345
+
346
+ ## Acknowledgements
347
+
348
+ SQLiter was initially developed as an experiment to see how helpful ChatGPT and
349
+ Claud AI can be to speed up the development process. The initial version of the
350
+ code was generated by ChatGPT, with subsequent manual/AI refinements and
351
+ improvements.
@@ -0,0 +1,326 @@
1
+ # SQLiter <!-- omit in toc -->
2
+
3
+ ![PyPI - Version](https://img.shields.io/pypi/v/sqliter-py)
4
+ [![Test Suite](https://github.com/seapagan/sqliter-py/actions/workflows/testing.yml/badge.svg)](https://github.com/seapagan/sqliter-py/actions/workflows/testing.yml)
5
+ [![Linting](https://github.com/seapagan/sqliter-py/actions/workflows/linting.yml/badge.svg)](https://github.com/seapagan/sqliter-py/actions/workflows/linting.yml)
6
+ [![Type Checking](https://github.com/seapagan/sqliter-py/actions/workflows/mypy.yml/badge.svg)](https://github.com/seapagan/sqliter-py/actions/workflows/mypy.yml)
7
+ ![PyPI - Python Version](https://img.shields.io/pypi/pyversions/sqliter-py)
8
+
9
+ SQLiter is a lightweight Object-Relational Mapping (ORM) library for SQLite
10
+ databases in Python. It provides a simplified interface for interacting with
11
+ SQLite databases using Pydantic models. The only external run-time dependency
12
+ is Pydantic itself.
13
+
14
+ It does not aim to be a full-fledged ORM like SQLAlchemy, but rather a simple
15
+ and easy-to-use library for basic database operations, especially for small
16
+ projects. It is NOT asynchronous and does not support complex queries (at this
17
+ time).
18
+
19
+ The ideal use case is more for Python CLI tools that need to store data in a
20
+ database-like format without needing to learn SQL or use a full ORM.
21
+
22
+ > [!NOTE]
23
+ > This project is still in the early stages of development and is lacking some
24
+ > planned functionality. Please use with caution.
25
+ >
26
+ > See the [TODO](TODO.md) for planned features and improvements.
27
+
28
+ - [Features](#features)
29
+ - [Installation](#installation)
30
+ - [Quick Start](#quick-start)
31
+ - [Detailed Usage](#detailed-usage)
32
+ - [Defining Models](#defining-models)
33
+ - [Database Operations](#database-operations)
34
+ - [Creating a Connection](#creating-a-connection)
35
+ - [Creating Tables](#creating-tables)
36
+ - [Inserting Records](#inserting-records)
37
+ - [Querying Records](#querying-records)
38
+ - [Updating Records](#updating-records)
39
+ - [Deleting Records](#deleting-records)
40
+ - [Commit your changes](#commit-your-changes)
41
+ - [Close the Connection](#close-the-connection)
42
+ - [Transactions](#transactions)
43
+ - [Filter Options](#filter-options)
44
+ - [Basic Filters](#basic-filters)
45
+ - [Null Checks](#null-checks)
46
+ - [Comparison Operators](#comparison-operators)
47
+ - [List Operations](#list-operations)
48
+ - [String Operations (Case-Sensitive)](#string-operations-case-sensitive)
49
+ - [String Operations (Case-Insensitive)](#string-operations-case-insensitive)
50
+ - [Contributing](#contributing)
51
+ - [License](#license)
52
+ - [Acknowledgements](#acknowledgements)
53
+
54
+ ## Features
55
+
56
+ - Table creation based on Pydantic models
57
+ - CRUD operations (Create, Read, Update, Delete)
58
+ - Basic query building with filtering, ordering, and pagination
59
+ - Transaction support
60
+ - Custom exceptions for better error handling
61
+
62
+ ## Installation
63
+
64
+ You can install SQLiter using whichever method you prefer or is compatible with
65
+ your project setup.
66
+
67
+ With `pip`:
68
+
69
+ ```bash
70
+ pip install sqliter-py
71
+ ```
72
+
73
+ Or `Poetry`:
74
+
75
+ ```bash
76
+ poetry add sqliter-py
77
+ ```
78
+
79
+ Or `uv` which is rapidly becoming my favorite tool for managing projects and
80
+ virtual environments (`uv` is used for developing this project and in the CI):
81
+
82
+ ```bash
83
+ uv add sqliter-py
84
+ ```
85
+
86
+ ## Quick Start
87
+
88
+ Here's a quick example of how to use SQLiter:
89
+
90
+ ```python
91
+ from sqliter import SqliterDB
92
+ from sqliter.model import BaseDBModel
93
+
94
+ # Define your model
95
+ class User(BaseDBModel):
96
+ name: str
97
+ age: int
98
+
99
+ class Meta:
100
+ table_name = "users"
101
+
102
+ # Create a database connection
103
+ db = SqliterDB("example.db")
104
+
105
+ # Create the table
106
+ db.create_table(User)
107
+
108
+ # Insert a record
109
+ user = User(name="John Doe", age=30)
110
+ db.insert(user)
111
+
112
+ # Query records
113
+ results = db.select(User).filter(name="John Doe").fetch_all()
114
+ for user in results:
115
+ print(f"User: {user.name}, Age: {user.age}")
116
+
117
+ # Update a record
118
+ user.age = 31
119
+ db.update(user)
120
+
121
+ # Delete a record
122
+ db.delete(User, "John Doe")
123
+ ```
124
+
125
+ ## Detailed Usage
126
+
127
+ ### Defining Models
128
+
129
+ Models in SQLiter are based on Pydantic's `BaseModel`. You can define your
130
+ models like this:
131
+
132
+ ```python
133
+ from sqliter.model import BaseDBModel
134
+
135
+ class User(BaseDBModel):
136
+ name: str
137
+ age: int
138
+ email: str
139
+
140
+ class Meta:
141
+ table_name = "users"
142
+ primary_key = "name" # Default is "id"
143
+ create_id = False # Set to True to auto-create an ID field
144
+ ```
145
+
146
+ ### Database Operations
147
+
148
+ #### Creating a Connection
149
+
150
+ ```python
151
+ from sqliter import SqliterDB
152
+
153
+ db = SqliterDB("your_database.db")
154
+ ```
155
+
156
+ The default behavior is to automatically commit changes to the database after
157
+ each operation. If you want to disable this behavior, you can set `auto_commit=False`
158
+ when creating the database connection:
159
+
160
+ ```python
161
+ db = SqliterDB("your_database.db", auto_commit=False)
162
+ ```
163
+
164
+ It is then up to you to manually commit changes using the `commit()` method.
165
+ This can be useful when you want to perform multiple operations in a single
166
+ transaction without the overhead of committing after each operation.
167
+
168
+ #### Creating Tables
169
+
170
+ ```python
171
+ db.create_table(User)
172
+ ```
173
+
174
+ #### Inserting Records
175
+
176
+ ```python
177
+ user = User(name="Jane Doe", age=25, email="jane@example.com")
178
+ db.insert(user)
179
+ ```
180
+
181
+ #### Querying Records
182
+
183
+ ```python
184
+ # Fetch all users
185
+ all_users = db.select(User).fetch_all()
186
+
187
+ # Filter users
188
+ young_users = db.select(User).filter(age=25).fetch_all()
189
+
190
+ # Order users
191
+ ordered_users = db.select(User).order("age", direction="DESC").fetch_all()
192
+
193
+ # Limit and offset
194
+ paginated_users = db.select(User).limit(10).offset(20).fetch_all()
195
+ ```
196
+
197
+ See below for more advanced filtering options.
198
+
199
+ #### Updating Records
200
+
201
+ ```python
202
+ user.age = 26
203
+ db.update(user)
204
+ ```
205
+
206
+ #### Deleting Records
207
+
208
+ ```python
209
+ db.delete(User, "Jane Doe")
210
+ ```
211
+
212
+ #### Commit your changes
213
+
214
+ By default, SQLiter will automatically commit changes to the database after each
215
+ operation. If you want to disable this behavior, you can set `auto_commit=False`
216
+ when creating the database connection:
217
+
218
+ ```python
219
+ db = SqliterDB("your_database.db", auto_commit=False)
220
+ ```
221
+
222
+ You can then manually commit changes using the `commit()` method:
223
+
224
+ ```python
225
+ db.commit()
226
+ ```
227
+
228
+ #### Close the Connection
229
+
230
+ When you're done with the database connection, you should close it to release
231
+ resources:
232
+
233
+ ```python
234
+ db.close()
235
+ ```
236
+
237
+ Note that closing the connection will also commit any pending changes, unless
238
+ `auto_commit` is set to `False`.
239
+
240
+ ### Transactions
241
+
242
+ SQLiter supports transactions using Python's context manager:
243
+
244
+ ```python
245
+ with db:
246
+ db.insert(User(name="Alice", age=30, email="alice@example.com"))
247
+ db.insert(User(name="Bob", age=35, email="bob@example.com"))
248
+ # If an exception occurs, the transaction will be rolled back
249
+ ```
250
+
251
+ > [!WARNING]
252
+ > Using the context manager will automatically commit the transaction
253
+ > at the end (unless an exception occurs), regardless of the `auto_commit`
254
+ > setting.
255
+ >
256
+ > the 'close()' method will also be called when the context manager exits, so you
257
+ > do not need to call it manually.
258
+
259
+ ### Filter Options
260
+
261
+ The `filter()` method in SQLiter supports various filter options to query records.
262
+
263
+ #### Basic Filters
264
+
265
+ - `__eq`: Equal to (default if no operator is specified)
266
+ - Example: `name="John"` or `name__eq="John"`
267
+
268
+ #### Null Checks
269
+
270
+ - `__isnull`: Is NULL
271
+ - Example: `email__isnull=True`
272
+ - `__notnull`: Is NOT NULL
273
+ - Example: `email__notnull=True`
274
+
275
+ #### Comparison Operators
276
+
277
+ - `__lt`: Less than
278
+ - Example: `age__lt=30`
279
+ - `__lte`: Less than or equal to
280
+ - Example: `age__lte=30`
281
+ - `__gt`: Greater than
282
+ - Example: `age__gt=30`
283
+ - `__gte`: Greater than or equal to
284
+ - Example: `age__gte=30`
285
+ - `__ne`: Not equal to
286
+ - Example: `status__ne="inactive"`
287
+
288
+ #### List Operations
289
+
290
+ - `__in`: In a list of values
291
+ - Example: `status__in=["active", "pending"]`
292
+ - `__not_in`: Not in a list of values
293
+ - Example: `category__not_in=["archived", "deleted"]`
294
+
295
+ #### String Operations (Case-Sensitive)
296
+
297
+ - `__startswith`: Starts with
298
+ - Example: `name__startswith="A"`
299
+ - `__endswith`: Ends with
300
+ - Example: `email__endswith=".com"`
301
+ - `__contains`: Contains
302
+ - Example: `description__contains="important"`
303
+
304
+ #### String Operations (Case-Insensitive)
305
+
306
+ - `__istartswith`: Starts with (case-insensitive)
307
+ - Example: `name__istartswith="a"`
308
+ - `__iendswith`: Ends with (case-insensitive)
309
+ - Example: `email__iendswith=".COM"`
310
+ - `__icontains`: Contains (case-insensitive)
311
+ - Example: `description__icontains="IMPORTANT"`
312
+
313
+ ## Contributing
314
+
315
+ Contributions are welcome! Please feel free to submit a Pull Request.
316
+
317
+ ## License
318
+
319
+ This project is licensed under the MIT License.
320
+
321
+ ## Acknowledgements
322
+
323
+ SQLiter was initially developed as an experiment to see how helpful ChatGPT and
324
+ Claud AI can be to speed up the development process. The initial version of the
325
+ code was generated by ChatGPT, with subsequent manual/AI refinements and
326
+ improvements.