moskytw / mosql

Build SQL with native Python data structure smoothly.
http://mosql.mosky.tw
MIT License
140 stars 17 forks source link

Context manager implementation #25

Closed uranusjr closed 10 years ago

uranusjr commented 10 years ago

Also see: #24. I didn't implement "unload" because it could be pretty messy when there are nested or even interlaced apply-restore pairs.

With this implementation, users have two choices:

  1. Use the context manager to mix different database engines

    from mosql.query import *
    from mosql import mysql, sqlite
    
    select(...)             # Standard
    with mysql.apply():
       select(...)         # MySQL style
       with sqlite.apply():
           select(...)     # SQLite style
       select(...)         # MySQL again
    select(...)             # Back to standard
  2. If the user only wants to work with one specific database

    # These two lines is equivalent to the old "import mosql.mysql"
    from mosql import mysql
    mysql.load()

To compensate the absence of the "unload" functionality, "load" actually returns a dictionary of attributes that were replaced by it. Users can manually restore things back if they wish to.

from mosql import mysql
backups = mysql.load()

# MySQL style now

import mosql.util
for k in backups:
    setattr(mosql.util, k, backups[k])

# Back to standard

I have also added/modified some documentation and tests.

moskytw commented 10 years ago

This feature is implemented in a different way. I will release it in next version.