Skip to main content

It lets you use the common Python's data structures to build SQLs, and provides a convenient model of result set.

Project description

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

MoSQL — More than SQL

It lets you use the common Python’s data structures to build SQLs, and provides a convenient model of result set.

The main features:

  1. Easy-to-learn — Everything is just plain data structure or SQL keyword. See The SQL Builders.

  2. Convenient — It makes result set more easy to use. See The Model of Result Set.

  3. Secure — It prevents the SQL injection from both identifier and value.

  4. Faster — It just builds the SQLs from Python’s data structure and then send it via the connector.

It is just “More than SQL”.

NOTE: The versions after v0.2 is a new branch and it does not provide backward-compatibility for v0.1.x.

The SQL Builders

>>> from mosql import build
>>> build.select('author', {'email like': '%mosky%@%'})
SELECT * FROM "author" WHERE "email" LIKE '%mosky%@%'

It is very easy to build a query by Python’s data structures and mosql.build.

There is more explanation of the builders — mosql.build.

It also provides mosql.result.Model for result set, and you can use the same way to make queries to database.

The Model of Result Set

Here is a SQL and the result set:

mosky=> select * from detail where person_id in ('mosky', 'andy') order by person_id, key;
 detail_id | person_id |   key   |           val
-----------+-----------+---------+--------------------------
         5 | andy      | email   | andy@gmail.com
         3 | mosky     | address | It is my first address.
         4 | mosky     | address | It is my second address.
         1 | mosky     | email   | mosky.tw@gmail.com
         2 | mosky     | email   | mosky.liu@pinkoi.com
        10 | mosky     | email   | mosky@ubuntu-tw.org
(6 rows)

Then, use the model configured (the module, detail, is in the examples) to do so:

>>> from detail import Detail
>>> for detail in Detail.arrange({'person_id': ('mosky', 'andy')}):
...     print detail
...
{'detail_id': [5],
 'key': 'email',
 'person_id': 'andy',
 'val': ['andy@gmail.com']}
{'detail_id': [3, 4],
 'key': 'address',
 'person_id': 'mosky',
 'val': ['It is my first address.', 'It is my second address.']}
{'detail_id': [1, 2, 10],
 'key': 'email',
 'person_id': 'mosky',
 'val': ['mosky.tw@gmail.com', 'mosky.liu@pinkoi.com', 'mosky@ubuntu-tw.org']}

Here I use arrange for taking advantages from the model configured, so the result sets are grouped into three model instances, but the plain methods, such as select, are also available.

It converts the result set to column-oriented models, and the column can be squashed. The non-list value above is just the squashed column. See mosql.result for more information.

There is more explanation of the model — mosql.result.

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

Uploaded Source

File details

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

File metadata

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

File hashes

Hashes for mosql-0.5.tar.gz
Algorithm Hash digest
SHA256 e119b5daf7eeee27facff73a40b92eada0ec1eefcd622977eae5ee6f7966dbc9
MD5 7e23090d7b7d3799081f645c3786072d
BLAKE2b-256 461759b136adc5bee41c9cfd6ccfcb4744c8ce76b0bb3f1498226bdc25c186fd

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