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 — No magic syntax. Everything is just plain data structure or SQL keyword.

  2. Convenient — It makes result set more easy to use.

  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 mosql.result.Model to do so: (The detail.py is in the examples.)

>>> 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']}

There are almost same, right?

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.

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

Uploaded Source

File details

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

File metadata

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

File hashes

Hashes for mosql-0.3.tar.gz
Algorithm Hash digest
SHA256 d979dd44dba7fbc067586c7daada671897862c4e696a94d49eaa36f202852c0b
MD5 31abe87d1cf241299010772fd351517a
BLAKE2b-256 b46e1f688bd4c76ec337da825b481975386d2a4e7ffcfa1ced0c3aea93c86b21

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