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.
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 create the connection inside a threadlocal.
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: |
|
---|
Call commit() on the active connection
Call rollback() on the active connection
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: |
|
---|
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: | 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
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
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