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 to the connector.

It is just “More than SQL”.

NOTE: The versions after v0.2 are 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 each result set into column-oriented model. The columns are squashable. The non-list values above are just the squashed columns. 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.2.tar.gz (33.1 kB view details)

Uploaded Source

File details

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

File metadata

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

File hashes

Hashes for mosql-0.5.2.tar.gz
Algorithm Hash digest
SHA256 1755de4fae9efb1d5a47728294b6998d8bb6d808b099e270129d1f5f1d1f5f42
MD5 4487afafa7e10cd053c16c159305daf4
BLAKE2b-256 8d873260cd7cbcd5c16d50756a212e498933f2ded58b4734498daeb560702c36

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