Skip to main content

A lightweight ORM for SQLite

Project description

ORM-SQLite

ORM-SQLite is

Quickstart

  1. Define a Pomodora model

    import orm_sqlite  
    
    class Pomodora(orm_sqlite.Model):  
    
        id = orm_sqlite.IntegerField(primary_key=True) # auto-increment
        task = orm_sqlite.StringField()
        interval = orm_sqlite.IntegerField()
    
  2. Create a Database instance

    db = orm_sqlite.Database('example.db')
    
  3. Set it as backend of Pomodora model objects, CRUD operations thus can be performed later

    Pomodora.objects.backend = db
    
  4. Create a Pomodora timer record (primary key is auto-increment) and insert it to backend (auto-commited)

    pomodora = Pomodora({'task': 'do something', 'interval': 25})
    pomodora.save()
    

    which is equivalent to

    pomodora = Pomodora({'task': 'do something', 'interval': 25})
    Pomodora.objects.add(pomodora)   
    
  5. Retrieve all the records in the backend

    Pomodora.objects.all()
    
  6. Retrieve single record by its primary key and update it (auto-commited)

    obj = Pomodora.objects.get(pk=1)
    obj['task'] = 'do something else'
    obj.update()
    

    which is equivalent to

    obj = Pomodora.objects.get(pk=1)
    obj['task'] = 'do something else'
    Pomodora.objects.update(obj)
    
  7. Retrieve single record by its primary key and delete it (auto-commited)

    Pomodora.objects.get(pk=1).delete()
    

    which is equivalent to

    obj = Pomodora.objects.get(pk=1)
    Pomodora.objects.remove(obj)
    
  8. Disconnect the backend

    Pomodora.objects.backend.close()
    

Install ORM-SQLite

$ pip install orm-sqlite

Documentation

Module Database

class orm_sqlite.Database(database[, timeout, detect_types, isolation_level, check_same_thread, factory, cached_statements, uri])
  • connected

    Instance attribute: Whether or not the SQLite database is connected.

  • connection

    Instance attribute: sqlite3.Connection object

  • cursor

    Instance attribute: sqlite3.Cursor object

  • connect()

    Instance method: Connects the SQLite database if disconnected.

  • close()

    Instance method: Disconnects the SQLite database if connected.

  • select(sql, *args, size=None)

    Instance method: Returns query results, a list of sqlite3.Row objects.

  • execute(sql, *args, autocommit=True)

    Instance method: Executes an SQL statement and returns rows affected.

  • commit()

    Instance method: Commits the staged transaction.

Module StringField

class orm_sqlite.StringField(name=None, default='')
  • name

    Instance attribute: Column name. Default: None.

  • type

    Instance attribute: Column type. Default: TEXT.

  • default

    Instance attribute: Column default value. Default: ''.

  • primary_key

    Instance attribute: Whether or not it is the primary key. Default: False.

Module IntegerField

class orm_sqlite.IntegerField(name=None, default=0, primary_key=False)
  • name

    Instance attribute: Column name. Default: None.

  • type

    Instance attribute: Column type. Default: INTEGER.

  • default

    Instance attribute: Column default value. Default: 0.

  • primary_key

    Instance attribute: Whether or not it is the primary key. Default: False.

Module FloatField

class orm_sqlite.StringField(name=None, default=0.0)
  • name

    Instance attribute: Column name. Default: None.

  • type

    Instance attribute: Column type. Default: REAL.

  • default

    Instance attribute: Column default value. Default: 0.0.

  • primary_key

    Instance attribute: Whether or not it is the primary key. Default: False.

Module Model

class orm_sqlite.Model(*args, **kwargs)
  • __table__

    Class attribute: Table.

  • __mappings__

    Class attribute: Object Relational Mappings (ORMs).

  • __primary_key__

    Class attribute: Primary key.

  • __fields__

    Class attribute: Fields except primary key.

  • objects

    Class-only attribute: orm_sqlite.Manager object used to manage corresponding Model objects.

  • exists()

    Class-only method: Whether or not the table exists in the connected database.

  • create()

    Class-only method: Create the table if not exists in the connected database.

  • drop()

    Class-only method: Drop the table if exists in the connected database, objects.backend.commit() needed to confirm. Extremely Dangerous.

  • save()

    Instance method: Inserts Model object to table in the connected database and returns rows affected (1 or -1).

  • update()

    Instance method: Updates Model object from table in the connected database and returns rows affected (1 or -1). Dangerous.

  • delete()

    Instance method: Deletes Model object from table in the connected database and returns rows affected (1 or -1). Dangerous.

  • keys(), values(), items(), copy(), etc. methods inherited from standard dict.

Practical template for customized model:

import orm_sqlite

class MyModel(orm_sqlite.Model):

    id = orm_sqlite.IntegerField(primary_key=True) # auto-increment
    # TODO:   

Module Manager

class orm_sqlite.Manager()
  • backend

    Instance attribute: orm_sqlite.Database object serving as backend of Model objects.

  • all()

    Instance method: Returns all Model objects from table in the connected database.

  • find(filter=None, order_by=None, **extra)

    Instance method: Returns all Model objects satified the conditions from table in the connected database.

  • get(pk)

    Instance method: Returns single Model object by its primary key from table in the connected database.

  • exists(pk)

    Instance method: Whether or not a primary key exists in the table from the connected database.

  • aggregate(self, expression, filter=None)

    Instance method: Returns the result of an aggregate function performed on table in the connected database.

  • add(obj)

    Instance method: Inserts Model object to table in the connected database and returns rows affected (1 or -1).

  • update(obj)

    Instance method: Updates Model object from table in the connected database and returns rows affected (1 or -1). Dangerous.

  • remove(obj)

    Instance method: Deletes Model object from table in the connected database and returns rows affected (1 or -1). Dangerous.

  • clear()

    Instance method: Deletes all Model objects from table in the connected database and returns rows affected (1 or -1), backend.commit() needed to confirm. Extremely Dangerous.

Helper Function

  • root_logger(log_dir='.')

    Returns root logger, a logging.logger object, logging in main module.

  • child_logger(child)

    Returns child logger, a logging.logger object, logging in auxiliary module.

Related Projects

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

ORM-SQLite-0.0.1.tar.gz (7.3 kB view details)

Uploaded Source

Built Distribution

ORM_SQLite-0.0.1-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

Details for the file ORM-SQLite-0.0.1.tar.gz.

File metadata

  • Download URL: ORM-SQLite-0.0.1.tar.gz
  • Upload date:
  • Size: 7.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.7.3 pkginfo/1.6.1 requests/2.23.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.7

File hashes

Hashes for ORM-SQLite-0.0.1.tar.gz
Algorithm Hash digest
SHA256 64b97e33d8ef47d6dfda546daa0576fc9e6dc0c92055e6c17b2647bdc5240846
MD5 a496f613fd94c5ffa793e5070ca31118
BLAKE2b-256 22f9b2f675c2651fa0587b5451007ead132437c651e1dfb27fe37d18e4b60638

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ORM_SQLite-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 8.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.7.3 pkginfo/1.6.1 requests/2.23.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.7

File hashes

Hashes for ORM_SQLite-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b7f3aa24a7a47fa5a0df3617a75b1550e03e8fb3640f867b8eec65dec3c51975
MD5 8203c93cc32269e7510d04054afbbab4
BLAKE2b-256 bc23a6880b19594e4f1381e7c5c7f5dfc0e361814c8d29abea3fd54e1e1b6466

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