The macaron module is the main module of Macaron. It has some module functions needed to use Macaron.
Macaron works with a global Macaron object. The object is generated by macaron module. Typical usage starts with macaronage() and finishes with cleanup(). If you change the database contents, you will need to call bake() (as commit) or rollback().
Using database models are described in Model class reference.
import macaron
# Initializing with autocommit and SQL execution logging
macaron.macaronage("mybook.db", autocommit=True, history=10)
... uses Models ...
# printing out the last five history
for idx in range(0, 5):
print macaron.history[idx]
# finishing macaron
macaron.bake()
macaron.cleanup()
Exception : | RuntimeError – SQL history is not enabled. |
---|---|
Exception : | IndexError – index is lager than history count. |
Return type: | string or list |
Returns history of SQL execution. You can get history like a list (index:0 is latest). It may be useful for debugging your applications.
last_sql = macaron.history[0]
second_last_sql = macaron.history[1]
sqls = macaron.history[0:5]
Wrapper for Cursor#execute().
Parameters: |
|
---|
Initializes macaron. This sets Macaron instance to module global variable _m (don’t access directly). If lazy is True, LazyConnection object is used for connection, which will connect to the DB when using. If autocommit is True, this will commits when this object will be unloaded.
Macaron‘s logging architecture uses logging which is Python standard module. Simply, you can use SQL history, calling macaron.macaronage() with history parameter. It set a logger for recording executions of SQL and you can obtain the SQL and stringfied its parameters through macaron.history.
If you want to use custom logger(s), you call macaron.macaronage() with logger parameter. It set the logger for recording history, too. The class of logger object must be a subclass of :class`logging.Logger`.
If you set both logger and history parameters, Macaron will add handler to your logger object.