horsql
An experimental SQL ORM that operates with Pandas DataFrames, offering the flexibility of dynamic querying without the need for model or schema definitions.
Install
$ pip install horsql
Connect
db = horsql.connect(
database="mydb",
host="localhost",
port=5432,
user="dev",
password="dev",
dialect="postgresql", # or "mysql"
echo=False,
pool_size=5, # defaults to 5
)
Querying
import horsql.operators as o
# Get all users
df = db.public.users.get()
# Get some columns of the users that are +18
df = db.public.users.get(columns=["user_name", "age"], age=o.EqualsOrGreaterThan(18))
# (Aggregation) get the average age by city from Brazil
df = db.public.users.get(columns=["city"], avg=["age"], country="Brazil")
# Distinct columns
df = db.public.users.get(distinct=["last_name"])
# get users that were born between those dates
df = db.public.users.get(birth_date=o.Between(["1998-01-01", "2000-01-01"]))
# get users that it's last_name is Silva or Machado
df = db.public.users.get(last_name=["Silva", "Machado"])
# get users that it's last_name is not Silva or Machado
df = db.public.users.get(last_name=o.Not(["Silva", "Machado"]))
# get users from Brazil or those that have "Silva" as last_name
df = db.public.users.get(
where=o.Or(
country="Brazil",
last_name="Silva"
)
)
# get users from United States OR those that have "Smith" as last_name
# OR (first_name is "Wilian" AND last_name is "Silva")
df = db.public.users.get(
where=o.Or(
country="United States",
last_name="Smith",
o.And(
first_name="Wilian",
last_name="Silva"
)
)
)
# Ordering
df = db.public.users.order_by("age", ascending=True).get()
df = db.public.users.order_by(["age", "country"], ascending=[True, False]).get()
# Limit
df = db.public.users.limit(limit=10).get()
df = db.public.users.order_by("age", ascending=True).limit(limit=10).get()
# Pagination
df = db.public.users.paginate(page=1, page_size=10).get()
df = db.public.users.order_by("age", ascending=True).paginate(page=1, page_size=10).get()
Creating/Updating records in the database
new_user = pd.DataFrame([
{
"user_name": "WilianZilv",
"first_name": "Wilian",
"last_name": "Silva"
}
])
# Create new records based on a dataframe
db.public.users.create(new_user)
# Upsert
db.public.users.create(new_user, on_conflict=["user_name"], update=["city", "country"])
# Updating records
new_user["city"] = "Curitiba"
new_user["country"] = "Brazil"
db.public.users.update(new_user, on_conflict=["user_name"], update=["city", "country"])
# Delete records
db.public.users.delete(user_name="WilianZilv")
Function definitions
Columns = Union[str, List[str]]
# Functions available in the Table object
def get(
self,
columns: Optional[Columns] = None,
distinct: Optional[Columns] = None,
min: Optional[Columns] = None,
max: Optional[Columns] = None,
sum: Optional[Columns] = None,
avg: Optional[Columns] = None,
count: Optional[Columns] = None,
where: Optional[Union[And, Or]] = None,
**and_where,
):
...
def create(
self,
df: pd.DataFrame,
on_conflict: Optional[Columns] = None,
update: Optional[Columns] = None,
commit: bool = True,
):
...
def update(
self,
df: pd.DataFrame,
on_conflict: Columns,
update: Columns,
commit: bool = True,
):
...
def limit(self, limit: int):
...
def paginate(self, page: int, page_size: int):
...
def order_by(
self,
columns: Union[List[str], str],
ascending: Union[List[bool], bool] = []
):
...
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
horsql-0.1.22.tar.gz
(7.5 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file horsql-0.1.22.tar.gz.
File metadata
- Download URL: horsql-0.1.22.tar.gz
- Upload date:
- Size: 7.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/4.0.2 CPython/3.11.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bd75335e732a17ddd35f565cb1fe70c4cae95393e40961610f1cd4af2dc0c9f7
|
|
| MD5 |
077c469812e4608c99e21e6bd1df88f4
|
|
| BLAKE2b-256 |
962b48cfd000920ba5c57aae70010809b21771cab708730cab33350322cdd9e8
|
File details
Details for the file horsql-0.1.22-py3-none-any.whl.
File metadata
- Download URL: horsql-0.1.22-py3-none-any.whl
- Upload date:
- Size: 8.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/4.0.2 CPython/3.11.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
95ef319812a3c27b361f222b3aff0d32865491c74da35f30b1e7cb9bfd929ee6
|
|
| MD5 |
dc0fb7da21eedb13c0abe50e8e0d8094
|
|
| BLAKE2b-256 |
8617321cebf1b79f866ed951c575cce0fcfa1f1aec7785edbdf51b916fc685c1
|