Skip to main content

MoSQL is a lightweight Python library which assists programmer to use SQL.

Project description

The full version of this documentation is at mosql.mosky.tw.

MoSQL — More than SQL

MoSQL is a lightweight Python library which assists programmer to use SQL.

It has two major parts:

  1. An Easy-to-Use Model for the result set.

  2. The SQL Builders which build the SQL strings by the common data types in Python.

An Easy-to-Use Model

I show you an example with this result set:

db=> select * from detail where person_id='mosky';
 detail_id | person_id |   key   |   val
-----------+-----------+---------+---------
         4 | mosky     | address | address
         3 | mosky     | address | ...
        10 | mosky     | email   | email
         6 | mosky     | email   | ...
         1 | mosky     | email   | ...
(5 rows)

The mosql.result.Model act as a proxy of the result set. After configuring, it provides a nice inferface to access the rows.

>>> from my_models import Detail
>>> for detail in Detail.find(person_id='mosky')):
...     print detail
{'person_id': 'mosky', 'detail_id': [3, 4], 'val': ['address', '...'], 'key': 'address'}
{'person_id': 'mosky', 'detail_id': [1, 6, 10], 'val': ['email', '...', '...'], 'key': 'email'}

For simplicity, the Model, which is a dict-like object, is rendered as a dict, and the mosql.result.Column, which is a list-like object, is rendered as a list.

As you see, some of the columns aren’t rendered as lists, because they are the columns grouped. It is the feature Model provides. It is more convenient than using SQL’s group by.

If you want to modify this model, just treat them as a dict or a list. The model will record your changes and let you save the changes at any time.

>>> detail = Detail.find(person_id='mosky', key='email')
>>> detail['val'][0] = 'I changed my email.'
>>> # detail.val[0] = 'I changed my email.' # It also works in 0.1.1 .
>>> detail.save()

Start with MoSQL’s model describes more details about how to use the Model.

The SQL Builders

The above model is based on these SQL builders. For an example:

>>> from mosql.common import select
>>> select('person', {'age >': 18})
'SELECT * FROM person WHERE age > 18'

It converts the common data types in Python into the SQL statements.

You can find more exmaples in mosql.common. If the common builders aren’t enough in your case, it is possible to customize the builder by mosql.util.

Installation

It is easy to install MoSQL with pip:

$ sudo pip install mosql

Or clone the source code from Github:

$ git clone git://github.com/moskytw/mosql.git

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

mosql-0.2.1.tar.gz (27.6 kB view details)

Uploaded Source

File details

Details for the file mosql-0.2.1.tar.gz.

File metadata

  • Download URL: mosql-0.2.1.tar.gz
  • Upload date:
  • Size: 27.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for mosql-0.2.1.tar.gz
Algorithm Hash digest
SHA256 b51b0fde885077086094678039bdc3e9fc03bc354a98f9d6507fa0e6b3125798
MD5 54d29d1d197cc0e86f7c6b83cb4b4a14
BLAKE2b-256 69b803d8973c0d13171dcbfa0446fbccd62daf13092cf0d39da5dd4fcd3af046

See more details on using hashes here.

Provenance

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