Skip to content

Commit

Permalink
Merge pull request #25 from ProdByGodfather/24-fix-postgresql-connect…
Browse files Browse the repository at this point in the history
…ions

24 fix postgresql connections
  • Loading branch information
ProdByGodfather authored Dec 7, 2024
2 parents a594cdd + 53feb49 commit ebf5065
Show file tree
Hide file tree
Showing 10 changed files with 174 additions and 382 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ abarorm.egg-info
__pycache__/
**/__pycache__/
m.py
./m.py
./m.py
abarorm/mysql.ex.py
./abarorm/mysql.ex.py
48 changes: 24 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# abarorm

| ![abarorm Logo](https://prodbygodfather.github.io/abarorm/images/logo.png) | **abarorm** is a lightweight and easy-to-use Object-Relational Mapping (ORM) library for SQLite, MySQL, and PostgreSQL databases in Python. It provides a simple and intuitive interface for managing database models and interactions. |
| ![abarorm Logo](https://prodbygodfather.github.io/abarorm/images/logo.png) | **abarorm** is a lightweight and easy-to-use Object-Relational Mapping (ORM) library for SQLite and PostgreSQL databases in Python. It provides a simple and intuitive interface for managing database models and interactions. |
|----------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|

## Features
Expand All @@ -19,6 +19,14 @@
- **New in v3.2.0**: Added `__gte` and `__lte` functionality in the filter section.
- **New in v4.0.0**: Added `__repr__`, `count`, and `to_dict` methods for easier data manipulation and debugging.
- **New in v4.2.3**: Added `first()`, `last()`, `exists()`, and `paginate()` methods to the QuerySet class for more powerful querying capabilities.
- **New in v5.0.0**: Fix `PostgreSQL` Bugs and structure.
- **Transaction Support**: Added support for transactions, allowing multiple operations to be grouped and committed together.
- **Optimized Queries**: Significant performance improvements for complex queries, especially with large datasets.
- **Migration System**: Introduced a built-in migration system for database schema updates, so developers can track changes and apply them incrementally.
- **Field Customization**: Enhanced field types with custom validation rules and hooks for dynamic field properties.
- **Composite Keys**: Added support for composite primary keys, allowing multiple fields to be used as a primary key in a model.
- **Improved QuerySet Methods**: Refined `filter()` and `exclude()` methods for more flexibility and ease of use.
- **Bulk Operations**: Added bulk insert and update methods for more efficient database operations.



Expand All @@ -30,11 +38,7 @@ You can install [**abarorm**](https://pypi.org/project/abarorm/) from PyPI using
```bash
pip install abarorm
```
For MySQL support, you also need to install `mysql-connector-python`:

```bash
pip install mysql-connector-python
```
For PostgreSQL support, you need to install `psycopg2-binary`:
```bash
pip install psycopg2-binary
Expand All @@ -44,32 +48,28 @@ pip install psycopg2-binary
For detailed documentation, examples, and advanced usage, please visit the [official abarorm documentation website](https://prodbygodfather.github.io/abarorm/).

## Database Configuration
Before defining models, you need to set up your database configuration. This involves specifying connection parameters for the database you are using (SQLite, MySQL, or PostgreSQL). Here’s an example of how to configure the database:
Before defining models, you need to set up your database configuration. This involves specifying connection parameters for the database you are using (SQLite and PostgreSQL). Here’s an example of how to configure the database:
```python
# Database configuration
DATABASE_CONFIG = {
'sqlite': {
'db_name': 'example.db', # Name of the SQLite database file
},
'mysql': {
'host': 'localhost',
'user': 'your_mysql_user',
'password': 'your_mysql_password',
'database': 'example_db',
},
'postgresql': {
'host': 'localhost',
'user': 'your_pg_user',
'password': 'your_pg_password',
'database': 'example_db',
'user': 'hoopad',
'password': 'db_password',
'database': 'example_db', # Ensure this matches everywhere
'port': 5432,
}
}
```
## Model Definition
After setting up the database configuration, you can define your models. A model is a representation of a database table. Here’s how to create a model using abarorm:
```python
from abarorm import SQLiteModel, MySQLModel, PostgreSQLModel
from abarorm.fields import CharField, DateTimeField, ForeignKey
from abarorm import SQLiteModel, PostgreSQLModel
from abarorm.fields.sqlite import CharField, DateTimeField, ForeignKey
from abarorm.fields import psql

# Define the Category model for SQLite
class Category(SQLiteModel):
Expand All @@ -82,14 +82,14 @@ class Category(SQLiteModel):
update_time = DateTimeField(auto_now=True) # Automatically set to current datetime


# Define the Post model for MySQL
class Post(MySQLModel):
# Define the Post model for Postgresql
class Post(PostgreSQLModel):
class Meta:
db_config = DATABASE_CONFIG['mysql']
db_config = DATABASE_CONFIG['postgresql']

title = CharField(max_length=100, null=False) # Title of the post, must be unique and not null
create_time = DateTimeField(auto_now=True) # Automatically set to current datetime
category = ForeignKey(to=Category) # Foreign key referring to the Category model
title = psql.CharField(max_length=100, null=False) # Title of the post, must be unique and not null
create_time = psql.DateTimeField(auto_now=True) # Automatically set to current datetime
category = psql.ForeignKey(to=Category) # Foreign key referring to the Category model
```
## CRUD Operations
Now that you have defined your models, you can perform CRUD operations. Here’s a breakdown of each operation:
Expand Down Expand Up @@ -216,6 +216,6 @@ This project is licensed under the [Apache-2.0 License](https://github.com/ProdB
## Acknowledgements

- **Python**: The language used for this project.
- **SQLite & MySQL**: The databases supported by this project.
- **SQLite and Postgresql**: The databases supported by this project.
- **setuptools**: The tool used for packaging and distributing the library.
- **psycopg2-binary**: The PostgreSQL adapter used for connecting to PostgreSQL databases.
1 change: 0 additions & 1 deletion abarorm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@
]

from .sqlite import SQLiteModel
from .mysql import MySQLModel
from .psql import PostgreSQLModel
84 changes: 84 additions & 0 deletions abarorm/fields/psql.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
from typing import Type, Optional
import re

class Field:
def __init__(self, field_type: str, max_length: Optional[int] = None, unique: bool = False,
null: bool = False, default: Optional[str] = None):
self.field_type = field_type
self.max_length = max_length
self.unique = unique
self.null = null
self.default = default

class CharField(Field):
def __init__(self, max_length: int = 255, **kwargs):
super().__init__(field_type='VARCHAR', max_length=max_length, **kwargs) # PostgreSQL uses VARCHAR for strings

class IntegerField(Field):
def __init__(self, **kwargs):
super().__init__(field_type='INTEGER', **kwargs)

class BooleanField(Field):
def __init__(self, default: bool = False, **kwargs):
super().__init__(field_type='BOOLEAN', default=default, **kwargs)

def set_default(self):
return 'TRUE' if self.default else 'FALSE'


class DateTimeField(Field):
def __init__(self, auto_now: bool = False, auto_now_add: Optional[bool] = None, **kwargs):
super().__init__(field_type='TIMESTAMP', **kwargs) # PostgreSQL uses TIMESTAMP for DateTime fields
self.auto_now = auto_now
self.auto_now_add = auto_now_add

class DateField(Field):
def __init__(self, auto_now: bool = False, auto_now_add: Optional[bool] = None, **kwargs):
super().__init__(field_type='DATE', **kwargs) # PostgreSQL uses DATE for date-only fields
self.auto_now = auto_now
self.auto_now_add = auto_now_add

class TimeField(Field):
def __init__(self, **kwargs):
super().__init__(field_type='TIME', **kwargs) # PostgreSQL uses TIME for time-only fields

class ForeignKey(Field):
def __init__(self, to: Type['BaseModel'], on_delete: str = 'CASCADE', **kwargs):
super().__init__(field_type='INTEGER', **kwargs)
self.to = to # This is the related model class
self.on_delete = on_delete # Specifies the behavior when the referenced row is deleted
# PostgreSQL usually uses INTEGER or BIGINT for foreign key references.

class FloatField(Field):
def __init__(self, **kwargs):
super().__init__(field_type='REAL', **kwargs) # PostgreSQL uses REAL for floating point numbers

class DecimalField(Field):
def __init__(self, max_digits: int, decimal_places: int, **kwargs):
super().__init__(field_type='DECIMAL', **kwargs) # PostgreSQL uses DECIMAL/NUMERIC for fixed precision numbers
self.max_digits = max_digits
self.decimal_places = decimal_places

class TextField(Field):
def __init__(self, **kwargs):
super().__init__(field_type='TEXT', **kwargs) # PostgreSQL uses TEXT for long strings

class EmailField(CharField):
EMAIL_REGEX = r'^[\w\.-]+@[\w\.-]+\.\w+$'

def __init__(self, max_length: int = 255, **kwargs):
super().__init__(max_length=max_length, **kwargs)

def validate(self, value: str):
if not re.match(self.EMAIL_REGEX, value):
raise ValueError(f"Invalid email address: {value}")

class URLField(CharField):
URL_REGEX = r'^(https?|ftp)://[^\s/$.?#].[^\s]*$'

def __init__(self, max_length: int = 2048, **kwargs):
super().__init__(max_length=max_length, **kwargs)

def validate(self, value: str):
if not re.match(self.URL_REGEX, value):
raise ValueError(f"Invalid URL: {value}")
File renamed without changes.
Loading

0 comments on commit ebf5065

Please sign in to comment.