File: README.md

package info (click to toggle)
django-prometheus 2.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 568 kB
  • sloc: python: 1,776; sh: 5; makefile: 3
file content (35 lines) | stat: -rw-r--r-- 1,499 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# Adding new database wrapper types

Unfortunately, I don't have the resources to create wrappers for all
database vendors. Doing so should be straightforward, but testing that
it works and maintaining it is a lot of busywork, or is impossible for
me for commercial databases.

This document should be enough for people who wish to implement a new
database wrapper.

## Structure

A database engine in Django requires 3 classes (it really requires 2,
but the 3rd one is required for our purposes):

* A DatabaseFeatures class, which describes what features the database
  supports. For our usage, we can simply extend the existing
  DatabaseFeatures class without any changes.
* A DatabaseWrapper class, which abstracts the interface to the
  database.
* A CursorWrapper class, which abstracts the interface to a cursor. A
  cursor is the object that can execute SQL statements via an open
  connection.

An easy example can be found in the sqlite3 module. Here are a few tips:

* The `self.alias` and `self.vendor` properties are present in all
  DatabaseWrappers.
* The CursorWrapper doesn't have access to the alias and vendor, so we
  generate the class in a function that accepts them as arguments.
* Most methods you overload should just increment a counter, forward
  all arguments to the original method and return the
  result. `execute` and `execute_many` should also wrap the call to
  the parent method in a `try...except` block to increment the
  `errors_total` counter as appropriate.