Below the Model level, peewee uses an abstraction for representing the database. The Database is responsible for establishing and closing connections, making queries, and gathering information from the database.
The Database in turn uses another abstraction called an Adapter, which is backend-specific and encapsulates functionality specific to a given db driver. Since there is some difference in column types across database engines, this information also resides in the adapter. The adapter is responsible for smoothing out the quirks of each database driver to provide a consistent interface, for example sqlite uses the question-mark ”?” character for parameter interpolation, while all the other backends use “%s”.
Note
The internals of the Database and BaseAdapter will be of interest to anyone interested in adding support for another database driver.
A high-level api for working with the supported database engines. Database provides a wrapper around some of the functions performed by the Adapter, in addition providing support for:
Parameters: |
|
---|
Establishes a connection to the database
Note
If you initialized with threadlocals=True, then this will store the connection inside a threadlocal, ensuring that connections are not shared across threads.
Closes the connection to the database (if one is open)
Note
If you initialized with threadlocals=True, only a connection local to the calling thread will be closed.
Return type: | a connection to the database, creates one if does not exist |
---|
Return type: | a cursor for executing queries |
---|
Parameters: | autocommit – a boolean value indicating whether to turn on/off autocommit for the current connection |
---|
Return type: | a boolean value indicating whether autocommit is on for the current connection |
---|
Parameters: |
|
---|
Note
You can configure whether queries will automatically commit by using the set_autocommit() and Database.get_autocommit() methods.
Call commit() on the active connection, committing the current transaction
Call rollback() on the active connection, rolling back the current transaction
Decorator that wraps the given function in a single transaction, which, upon success will be committed. If an error is raised inside the function, the transaction will be rolled back and the error will be re-raised.
Parameters: | func – function to decorate |
---|
@database.commit_on_success
def transfer_money(from_acct, to_acct, amt):
from_acct.charge(amt)
to_acct.pay(amt)
return amt
Parameters: |
|
---|---|
Return type: | the primary key of the most recently inserted instance |
Return type: | number of rows affected by the last query |
---|
Parameters: |
|
---|
Parameters: |
|
---|
Parameters: |
---|
Parameters: |
|
---|
Note
Cascading drop tables are not supported at this time, so if a constraint exists that prevents a table being dropped, you will need to handle that in application logic.
Parameters: | sequence_name – name of sequence to create |
---|
Note
only works with database engines that support sequences
Parameters: | sequence_name – name of sequence to drop |
---|
Note
only works with database engines that support sequences
Parameters: | table – the name of table to introspect |
---|---|
Return type: | a list of (index_name, is_unique) tuples |
Warning
Not implemented – implementations exist in subclasses
Return type: | a list of table names in the database |
---|
Warning
Not implemented – implementations exist in subclasses
Rtype boolean: |
---|
The various subclasses of BaseAdapter provide a bridge between the high- level Database abstraction and the underlying python libraries like psycopg2. It also provides a way to unify the pythonic field types with the underlying column types used by the database engine.
The BaseAdapter provides two types of mappings: - mapping between filter operations and their database equivalents - mapping between basic field types and their database column types
The BaseAdapter also is the mechanism used by the Database class to: - handle connections with the database - extract information from the database cursor
A mapping of query operation to SQL
The string used by the driver to interpolate query parameters
Whether the given backend supports sequences
Table names that are reserved by the backend – if encountered in the application a warning will be issued.
Return type: | a dictionary mapping “user-friendly field type” to specific column type, e.g. {'string': 'VARCHAR', 'float': 'REAL', ... } |
---|
Return type: | a dictionary similar to that returned by get_field_types(). |
---|
Provides a mechanism to override any number of field types without having to override all of them.
Parameters: |
|
---|---|
Return type: | a database connection |
Parameters: | conn – a database connection |
---|
Close the given database connection
Parameters: |
|
---|---|
Return type: | a converted value appropriate for the given lookup |
Used as a hook when a specific lookup requires altering the given value, like for example when performing a LIKE query you may need to insert wildcards.
Return type: | most recently inserted primary key |
---|
Return type: | number of rows affected by most recent query |
---|
Subclass of BaseAdapter that works with the “sqlite3” driver
Subclass of BaseAdapter that works with the “MySQLdb” driver
Subclass of BaseAdapter that works with the “psycopg2” driver