This release introduces significant improvements in data filtering capabilities within the database. The new `Q` class allows for the creation of complex query filters using logical operators like AND, OR, and NOT. Additionally, refactoring has enhanced code readability and streamlined the handling of conditions in SQL queries.
Added
- **Support for Advanced Filters with the `Q` Class:**
- Introduced the `Q` class, enabling the creation of complex queries by combining various conditions using AND, OR, and NOT operators.
- Capability to construct intricate queries, such as filtering data with multiple `Q` objects using logical AND/OR.
Refactoring
- **Improved Condition Handling in Queries:**
- Refactored the `prepare_where_conditions` method to accept positional arguments.
- Simplified the `_fetch_raw_data` method by leveraging the refactored `prepare_where_conditions` function.
- Refactored the `Q` class and related methods to enhance readability and improve SQL condition management.
Example
python
q1 = Q(name="Alice")
q2 = Q(age__lt=25)
q3 = Q(weight__gte=70)
q4 = Q(name="Bob")
q5 = Q(age__gt=30)
q6 = Q(weight__lte=80)
q = Q(q1 & q2 | q3) | Q(q4 & q5 | q6)
User.filter(q)
This is equivalent to the following SQL WHERE clause:
sql
WHERE (name = 'Alice' AND age < 25 OR weight >= 70) OR (name = 'Bob' AND age > 30 OR weight <= 80)
Features and Roadmap
- [x] Define table schema using Pydantic models
- [x] Basic CRUD operations
- [x] Save data to the database
- [x] Read data from the database
- [x] Update data in the database
- [x] Delete data from the database
- [x] Relationships between tables
- [x] One-to-many
- [x] Create a tables with a foreign key
- [x] Save data with a foreign key
- [x] Read data with a foreign key
- [x] Update data with a foreign key
- [x] Delete data with a foreign key
- [X] Cascade
- [x] Set null
- [x] Restrict
- [x] Set default
- [x] No action
- [x] One-to-one
- [x] Many-to-many
- [x] Unique constraints
- [x] Remove table
- [x] Read all data from the database
- [x] Filter data and retrieve multiple records
- [x] Equal
- [x] Not equal
- [x] Greater than
- [x] Greater than or equal
- [x] Less than
- [x] Less than or equal
- [x] Like (Pattern matching with % and _)
- [x] Not like (Pattern matching with % and _)
- [x] In (List of values)
- [x] Not in (List of values)
- [x] Between (Two values)
- [x] Not between (Two values)
- [x] Q objects to combine filters (AND, OR, NOT)
- [x] Protect against SQL injection
- [x] Order by
- [x] Limit and offset
- [x] Update table schema
- [x] Add new column
- [x] Rename column
- [x] Drop column
- [ ] Custom primary key
- [ ] Functions
- [ ] Aggregate functions
- [ ] String functions
- [ ] Date and time functions
- [ ] Mathematical functions
- [ ] Control flow functions
- [ ] Bulk operations (save, update, delete)
- [ ] Migrations
**Full Changelog**: https://github.com/SpaceShaman/ORMagic/compare/v0.11.0...v0.12.0