Table Of Contents

Previous topic

Models

Next topic

Model class reference

This Page

Module contents

The macaron module is the main module of Macaron. It has some module functions needed to use Macaron.

Usage

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.

Example

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()

Module attributes

macaron.history
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]

Module methods

macaron.bake()
macaron.cleanup()
macaron.execute(*args, **kw)

Wrapper for Cursor#execute().

macaron.macaronage(dbfile=':memory:', lazy=False, autocommit=False, logger=None, history=-1)
Parameters:
  • dbfile – SQLite database file name.
  • lazy – Uses LazyConnection.
  • autocommit – Commits automatically when closing database.
  • logger (logging.Logger) – Uses for logging SQL execution.
  • history – Sets max count of SQL execution history (0 is unlimited, -1 is disabled). Default: disabled

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.rollback()

Logging

SQL logging

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.

Using custom logger

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.