#11581 closed enhancement (fixed)
Add an abstract base class for connection classes
Reported by: | Ryan J Ollos | Owned by: | Ryan J Ollos |
---|---|---|---|
Priority: | normal | Milestone: | 1.1.2 |
Component: | database backend | Version: | |
Severity: | normal | Keywords: | |
Cc: | Branch: | ||
Release Notes: | |||
API Changes: |
Added an abstract base class |
||
Internal Changes: |
Description
Discussed in #11580, add an abstract base class for the connection classes: SQLiteConnection
, PostgreSQLConnection
and MySQLConnection
. With this addition, implementations of any new database backends will have an interface to adhere to.
Attachments (0)
Change History (8)
comment:1 by , 11 years ago
Milestone: | 1.0.2 → 1.1.2 |
---|---|
Status: | new → assigned |
comment:2 by , 11 years ago
Initially I used the pre-Python26 pattern of defining a class with methods that raise NotImplementedError
s. However this doesn't prevent a user from instantiating the abstract base class (unless we also define an __init__
method that raises a NotImplementedError
), and it doesn't perform any runtime checks when constructing the derived instance to guarantee that the derived class has implemented all of the abstract methods.
>>> import trac.db.api >>> c = trac.db.api.Connection() >>> c.like() Traceback (most recent call last): File "<stdin>", line 1, in <module> File "trac/db/api.py", line 209, in like raise NotImplementedError NotImplementedError
Using the Python 2.6 AbstractBaseClass module we can enforce that all of the abstractmethod
s have been implemented. If we didn't define like
on the SQLiteConnection
class, the result would be:
>>> import trac.db.sqlite_backend >>> csqlite = trac.db.sqlite_backend.SQLiteConnection('/home/user/Workspace/tracdev/tracdev/db/trac.db') Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: Can't instantiate abstract class SQLiteConnection with abstract methods like
Proposed changes can be found in log:rjollos.git:t11581. In particular, please let me know if you have any better text for the Connection
class method docstrings.
comment:3 by , 11 years ago
I decided to commit changes in #11600 and leave these changes for a few days to see if there is any feedback. Proposed changes have been rebased on the trunk in log:rjollos.git:t11581.3.
comment:4 by , 11 years ago
API Changes: | modified (diff) |
---|---|
Resolution: | → fixed |
Status: | assigned → closed |
Committed to trunk in [12762].
follow-up: 8 comment:5 by , 10 years ago
I'm thinking that the abstract class Connection
should be named ConnectionBase
. ConnectionBase
will provide better name differentiation from ConnectionWrapper
.
Two existing base classes that follow this convention are WikiMacroBase
and CachedPropertyBase
. Several base classes such as Repository
do not follow the convention. Most of the abstract classes can be seen in [12782].
Going forward, should we use the convention that base classes should end in Base
? The detail could be added to TracDev/CodingStyle.
comment:7 by , 10 years ago
API Changes: | modified (diff) |
---|
comment:8 by , 10 years ago
Replying to rjollos:
Going forward, should we use the convention that base classes should end in
Base
? The detail could be added to TracDev/CodingStyle.
This detail was added to the coding style page in TracDev/CodingStyle@19.
I guess these changes should probably go on the trunk.