Skip to main content

A zero, 0, weight ORM for sqlite3 database.

Project description

Downloads

SQL30

sql30 is a zero weight ORM for sqlite3 in Python.

It is written using pure python constructs and has no dependency on any other module. There is no requirements.txt in the project in fact. Having zero dependency on any other module makes this package usable on variety of platforms including the ones which have limited or delayed support such as ESX.

Usage

SQL30 is incredibly simple to use.

All it requires is to define your database schema using a simple JSON. Then, pre-defined interface can be used to perform CRUD operations. As is true for any ORM, user doesn't need to know how the operations are translated back to SQL statements for sqlite3.

In the schema,

  • Database filename is provided using the key db_name.
  • Multiple tables can be defined under tables key in JSON.
  • Fields / Columns for each table are added using fields key in JSON. Field name and it's type is provided as key value pair again.
  • Primary Key for each table can be defined using key primary_key

Let's take an example.

Example

Let's say we have to store the reviews of a product in database and we chose sqlite3 for the purpose. Using SQL30, a sample schema can be defined as shown below.

# reviews.py
from sql30 import db

class Reviews(db.Model):
    TABLE = 'reviews'
    PKEY = 'rid'
    DB_SCHEMA = {
        'db_name': 'reviews.db',
        'tables': [
            {
                'name': TABLE,
                'fields': {
                    'rid': 'uuid',
                    'header': 'text',
                    'rating': 'int',
                    'desc': 'text'
                    },
                'primary_key': PKEY
            }]
        }
    VALIDATE_BEFORE_WRITE = True

Now, we can create instance of Review and that will handle CRUD operations to database for us.

>>> import os
>>> import reviews

# delete old database if you want to
>>> os.remove('reviews.db')

# Create ORM layer object instance.
>>> db = reviews.Reviews()

# With this, we can create/write records to db as following.
>>> tbl = 'reviews' # or db.TABLE
>>> db.write(tbl, rid=1, header='good thing', rating=5)
>>> db.write(tbl, rid=2, header='bad thing', rating=1, desc='what a disgusting thing')

# We can read back the records as shown below.
>>> results = db.read(tbl, rid=1)

# Querying over the records is possble as below.
>>> results = db.read(tbl, rating=3, rid=1)

# If we try to add another record with same primary key, it will error out.
>>> db.write(tbl, rid=1, header='good thing', rating=5)

# Updating the record is also possible by providing condition for records and updated values.
>>> where = {'rid': 2}
>>> db.update(tbl, condition=where, header='average item', rating=2)

# Deleteing the records is possble with any level of filtering.
>>> db.remove(tbl, rid=1)

Installation

Fetching / Installing SQL30 is simple. Easiest way to consume sql30 is by installing it from pypi server (https://pypi.org/project/sql30/) by running following command.

pip install sql30

However, if your machine doesn't have access to pypi server, it is easy to get and build a wheel for SQL30 from a machine which has access to pypi server. Following are the steps for the same.

$ virtualenv -p python3 . 
$ mkdir unpacked
$ bin/pip install --target=./unpacked/ sql30
$ cd unpacked
$ zip -r9 ../sql30.egg *
$ cd -
# 

sql30.egg file generated by above steps can now be taken and used with any machine with python3 and sqlite3 (python module) available. An example of the same is shown below. Here sql30.egg is being taken to an ESX Hypervisor with a Python 3.5.6 version and shown to consume egg file.

[root@prom-0505695d9ce:~] PYTHONPATH=sql30.egg python
Python 3.5.6 (default, Feb  2 2019, 01:09:51)
[GCC 4.6.3] on linux
Type "help", "copyright", "credits" or "license" for more information.
No entry for terminal type "screen.xterm-256color";
using dumb terminal settings.
>>> from sql30 import db
>>> import os
>>> os.system('uname -a')
VMkernel prom-0505695d9ce.xyz.test 6.5.0 #1 SMP Release build-13753126 May 19 2019 21:13:25 x86_64 x86_64 x86_64 ESXi
0
>>> class Dummy(db.Model):
...     pass
...
>>> dir(Dummy)
['DB_SCHEMA', 'VALIDATE_BEFORE_WRITE', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_form_constraints', '_get_fields', '_get_schema', '_validate_bfr_write', 'close', 'commit', 'create_table', 'cursor', 'init_db', 'read', 'remove', 'table_exists', 'update', 'write']

Repository

https://github.com/gitvipin/sql30

Requirements

Python 3.4+

Share and enjoy!

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

sql30-0.1.2.tar.gz (5.8 kB view hashes)

Uploaded Source

Built Distribution

sql30-0.1.2-py3-none-any.whl (6.5 kB view hashes)

Uploaded Python 3

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