Skip to main content

tiny ORM for SQL-databases

Project description

This package represents a new ORM to work with SQL-syntax databases (for now, only PostgreSQL is supported).

Initializing

To start working, all you need to do is import a package's facade object and initialise it:

import qrookDB.DB as DB
DB = db.DB('postgres', 'db_name', 'username', 'password', format_type='dict')
DB.create_logger(app_name='qrookdb_test', file='app_log.log')
DB.create_data(__name__, in_module=True)

Here we first created a database connection, providing connect parameters (format_type defines the form in which results'll be returned - 'list' and 'dict' supported), then initialized an internal logger to write both in console and file. The 'create_data' function reads database system tables to get info about all user-defined tables, and creates QRTable objects based on this info. Now you can use table names (ones given to them in database) to access to these objects as DB instance fields (also, configuration showed above adds these table names to your current module, so you can use short names: 'books' instead of 'DB.books').

Querying

You can form execute queries using either DB instance or concrete tables (in this case, you don't need to mention in which table to perform queries). To execute any query, use one of 'exec', 'one' and 'all' methods (latter two define how many rows to return from query; 'exec' returns None by default). If forming of the query fails, it won't be executed at all (and will return None if you try); you can use 'get_error' method of the query to get the error description (it will be logged, though). Note: if error occured in the middle of query-building, query will ignore the rest of building proccess.

Select queries examples

op = DB.operators

print(DB.books)
print(books, books.id)

# logical 'and' is used by default for multiple where conditions; 'op' module contains special operators
data = DB.select(books).where(original_publication_year=2000, language_code='eng').    where(id=op.In(470, 490, 485)).all()

# you can add raw-string query parts, but it'll be on your conscience in terms of security   
query = books.select('count(*)').group_by(books.original_publication_year)
data = query.all()

data = DB.select(books, books.id).where('id < 10').order_by(books.id, desc=True).    limit(3).offset(2).all()

# here fields have same name ('id'), but via different tables it'll be ok
# (for data in dict-format, table-names'll be added to keys) 
data = books.select(authors.id, books.id)    .join(books_authors, op.Eq(books_authors.book_id, books.id))    .join(authors, op.Eq(books_authors.author_id, authors.id)).all()
    # .join(books_authors, 'books_authors.book_id = books.id')
data = books.select(books.id).where(id=1).where(bool='or', id=2).all()

# error - trying to select two equal fields;
q = DB.select(events, events.id, events.id).where(id=1)
data = q.all()
print('data is None here:', data, ';	error:', q.get_error())

Update, Insert, Delete queries examples

# if auto_commit is not set, you'll have to commit manually 
ok = DB.delete(events, auto_commit=True).where(id=1).exec()

from datetime import datetime
t = datetime.now().time()
d = datetime.now().date()
ok = DB.update(events, auto_commit=False).set(time=t).where(id=6).exec()
DB.commit()

# other possible variants for values: values([t]), values([d, t])
# other possible variants for returning: returning(events.date, events.time), returning(['date', 'time']), returning('date', 'time')
query = events.insert(events.date, events.time, auto_commit=True).values([[d, t], [None, t]]).returning('*')
data = query.all()

# you can also execute fully-raw queries; if you need to return values, 
use 'config_fields' to define results' names (not necessary for 'list' data format) 
data = DB.exec('select * from get_book_authors(1) as f(id int, name varchar)').config_fields('id', 'name').all()
print(data)

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

qrookDB-1.2.0.7.tar.gz (16.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

qrookDB-1.2.0.7-py3-none-any.whl (19.7 kB view details)

Uploaded Python 3

File details

Details for the file qrookDB-1.2.0.7.tar.gz.

File metadata

  • Download URL: qrookDB-1.2.0.7.tar.gz
  • Upload date:
  • Size: 16.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.24.0 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.6.9

File hashes

Hashes for qrookDB-1.2.0.7.tar.gz
Algorithm Hash digest
SHA256 3374c8510cbd9d49fbcd62b399c8b403e674f6f974ccdf8525dae7a7cc2948fa
MD5 9baa24e79c291de6ff62e22a0fd29145
BLAKE2b-256 5669e614c859231b6dcd731c828175d9538cb2c4d56cc57f7234b8793cda78cf

See more details on using hashes here.

File details

Details for the file qrookDB-1.2.0.7-py3-none-any.whl.

File metadata

  • Download URL: qrookDB-1.2.0.7-py3-none-any.whl
  • Upload date:
  • Size: 19.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.24.0 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.6.9

File hashes

Hashes for qrookDB-1.2.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 80778ccabad2401a0fd5ca91e6311b48c3ab5591a6a379acfe9f8072b210a4ef
MD5 ab4e49da49a2d88e1f12acf0f77c3a3d
BLAKE2b-256 94c6e09d62ff1e0dfefcaab2dc1997180911e9e2b846bbee8013f26c46863582

See more details on using hashes here.

Supported by

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