I was looking for a really easy way to organize SQL outside of code. Standard ORMs just seem to have way more functionality then I actually need and object models just seem to kill all that is great with SQL. Additionally, I actually like working with SQL and found little use for proper models in my projects. So thus, all I really needed was a stupid simple python module to communicate with the database and provide an ORM'ish type framework to organize SQL.
StupidDB supports PostgreSQL (psycopg2) and MySQL (MySQLdb). One caveat is that there is no restriction to writing db-specific SQL in your SQL map files. StupidDB provides no abstraction as far as the database layer is concerned. But really, how often are you gonna swap the underlying DB? Plus, with little restriction on the DB layer, you can take advantage of more advanced DB-specific features (analytical functions and stuff).
Place your config file in the root of your application and specify the following arguments:
- db_user=username
- db_password=password
- db_host=hostname
- db_port=port
- db_connectdb=connect_db
- db_type=postgresql/mysql
- sqlmap_path=path to sqlmap directory (optional: by default, assumes a sqlmap directory exists in your application root)
It's really quick and easy with 3 methods to remember. First, instantiate a class:
from StupidDB.stupiddb import StupidDB
stupid = StupidDB(<config_file (optional)>)
# if config path is not provided, StupidDB will just look in the app root
You might need to modify the import statement depending on how your environment is setup.
This encompasses any and all queries meant to return as a list of dicts. As a consumer, the call is as follows:
results = stupid.read(<sqlmap>, <id>, <**kwargs>)
- sqlmap refers to the xml file containing the relevant set of queries (see sqlmap/example.xml)
- id refers to the name of the query being called (see sqlmap/example.xml)
- **kwargs all named parameters that will be used in the query (match in sqlmap is required)
Same as read_single accepts returns a single dict
result = stupid.read_single(<sqlmap>, <id>, <**kwargs>)
This ecompasses all inserts, updates, merges (DML statements, see sqlmap/example.xml)
stupid.write(<sqlmap>, <id>, <**kwargs>)
- The sqlmap file can be contained within subdirectories: ie, <sqlmap_root>/dir1/dir2/map.xml. You can define this in the read/write calls as sqlmap='dir1.dir2.map'. The module will auto detect path seperators and point to the correct place.
And that's it! Have fun...