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.1.6.tar.gz (26.1 kB view details)

Uploaded Source

File details

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

File metadata

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

File hashes

Hashes for mosql-0.1.6.tar.gz
Algorithm Hash digest
SHA256 7d8bb1e89b7a1b645cb27fa4b792a583e6e158ebbe4959ecdbba50c4a9bcd464
MD5 d38f4f6bd73a6afa776f0979ecf739ef
BLAKE2b-256 a34ef6933a5f7888441325ab42dd155a913559e78cc11ce06b9c73b775c3903a

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