Skip to main content

A efficient raw SQL ORM

Project description

Bajra Bajra

bajra

Bajra is a simple ORM API (for MySQL and PostGreSQL) that provide helpers to manage raw SQL in Python applications.

What Bajra can do?

Bajra allows you to write raw SQL and forget about cursors, and it's related data structures like tuples or dicts. Also allows you to write your query by pieces, making the whole thing more readable.

Let's see a few examples!

Fetching results made simple

from bajra import Bajra
from bajra.engines.postgres import PostgreSQLEngine

cater = Bajra(PostgreSQLEngine(dsn="YOUR-DATABASE-DSN"))
rows = cater.fetchall("SELECT name, surname FROM test")  # That command returns a RowResult you can iterate
for row in rows:
    print(row.name)  # You can access to the data with dot notation.
    print(row['name'])  # Also as a dict-like object.

Querying with arguments. No more pain.

from bajra import Bajra
from bajra.engines.postgres import PostgreSQLEngine

cater = Bajra(PostgreSQLEngine(dsn="YOUR-DATABASE-DSN"))
row = cater.fetchone("SELECT name, surname FROM test WHERE name = %s", ("John", ))
print(row.name)  # John
print(row.surname)  # Costa

row = cater.fetchone("SELECT name, surname FROM test WHERE name = %(name)s", {'name': 'John'})
print(row.name)  # John
print(row.surname)  # Costa

row = cater.fetchone("SELECT name, surname FROM test WHERE name = %(name)s", name='John')
print(row.name)  # John
print(row.surname)  # Costa

Querying piece by piece

from bajra import Bajra
from bajra.engines.postgres import PostgreSQLEngine

cater = Bajra(PostgreSQLEngine(dsn="YOUR-DATABASE-DSN"))
query = cater.query("SELECT * FROM test")
query.append("WHERE name = %(name)s", name="John")
row = cater.fetchone(query)
row.name  # John
row.surname  # Costa

Execute all the things.

from bajra import Bajra
from bajra.engines.postgres import PostgreSQLEngine

cater = Bajra(PostgreSQLEngine(dsn="YOUR-DATABASE-DSN"))
query = cater.query("INSERT INTO test (name, surname) VALUES")
query.append("(%(name)s, %(surname)s)", name="Thomas", surname="Jefferson")

result = cater.execute(query)
print(result.lastrowid)  # Last Row ID of the last query. (If the engine supports it.)
print(result.rowcount)  # How many rows retrieve the last query. (If the engine supports it.)
print(result.rownumber)  # Number of rows affected by the last query. (If engine supports it.)

Transactions

from bajra import Bajra
from bajra.engines.postgres import PostgreSQLEngine

cater = Bajra(PostgreSQLEngine(dsn="YOUR-DATABASE-DSN"))
cater.begin()
query = cater.query("INSERT INTO test (name, surname) VALUES")
query.append("(%(name)s, %(surname)s)", name="Marie-Cole", surname="Ross")

result = cater.execute(query)
print(result.lastrowid)  # Last Row ID of the last query. (If the engine supports it.)
print(result.rowcount)  # How many rows retrieve the last query. (If the engine supports it.)
print(result.rownumber)  # Number of rows affected by the last query. (If engine supports it.)

cater.commit()

Transactions within a context manager

from bajra import Bajra
from bajra.engines.postgres import PostgreSQLEngine


def callback_on_exception(_cater, exc_type, exc_value, traceback):
    print(exc_type)  # <class 'psycopg2.ProgrammingError'>


cater = Bajra(PostgreSQLEngine(dsn="YOUR-DATABASE-DSN"))

with cater.transaction(rollback_on_exc=True, callback_exc=callback_on_exception):
    cater.execute("INSERT INTO test (name, surname) VALUES %s, %s", ("Augustus", "Zuma"))

row = cater.fetchone("SELECT * FROM test WHERE name=%s AND surname=%s", ("Augustus", "Zuma"))
print(row)  # None, Bajra has perfomed a rollback due to a exception.

Collaborators Welcome!

If you have an idea you want to contribute in Bajra, clone the repo and raise a pull request with your feature!

Made with love in India

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bajra-0.0.1.tar.gz (10.6 kB view details)

Uploaded Source

Built Distribution

bajra-0.0.1-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

Details for the file bajra-0.0.1.tar.gz.

File metadata

  • Download URL: bajra-0.0.1.tar.gz
  • Upload date:
  • Size: 10.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.1

File hashes

Hashes for bajra-0.0.1.tar.gz
Algorithm Hash digest
SHA256 6911c91257865df97a19312f3c972bd6149c68e9aba40d70d571177755dfb870
MD5 4a7786d0f73da4d3d2e3a912420e0eab
BLAKE2b-256 ae65fef73fee96f5e6cb417061e458f2a05db4251549dbb0d08191e27a51d925

See more details on using hashes here.

File details

Details for the file bajra-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: bajra-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 10.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.1

File hashes

Hashes for bajra-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d7aec783d0476b001cfe3c114763af349b3b915c8bd48ec4e8fc84205e3f758b
MD5 707ab9b6d347ba72ce0b1121f7fae2be
BLAKE2b-256 a4fa4729a868ef6bac3e4b3b648fd5122dbcb00cb1fd6c2cb906b05715235cc1

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page