Opened 8 years ago

Last modified 7 years ago

#5487 new task

Work on Oracle backend using cx_Oracle

Reported by: David Avsajanishvili Owned by: anybody
Priority: normal Component: Request-a-Hack
Severity: normal Keywords: database db backend oracle
Cc: David Avsajanishvili Trac Release: 0.11


Oracle Backend for Trac


Oracle database backend needed for Trac. Because there is no such one, I decided to create it. Finally, development of this backend deadlocked because there is a lot of unsolvable or intractable issues. Although, some work performed and, maybe, it will be helpful for other people who will attempt to port Trac to Oracle.

Source of the is created according on by similarity.

More information about Trac DB backens could be found here:



The backend is based on cx_Oracle Python extension module. It has different implementations for different versions of Oracle client software. The extension module could be obtained here:

File attached to this ticked contains patch against Trac distribution, version, which could be obtained from Trac Download Page, or by this direct link:



There are several Oracle-specific issues, which hindered backend development.

Lack of Autoincrement type

There is no built-in "Autoincrement" type in Oracle. Thus additional workarounds became required to simulate it, based on approach described here: A Sequence and a Trigger is added for table having autoincrement field:


    SELECT seq_<<TABLENAME>>.nextval INTO :new.<<COLNAME>> FROM DUAL;

CLOB columns in Indexes not supported

So, all columns participated in index creation are created of type VARCHAR(4000). This adds limitation on length of such columns. Because indexed columns usually have little amount of data, this limitation is not significant in practicle.

CLOB can't be in PK's and length of PK is lilmited

PK columns not only must be created of type VARCHAR instead of CLOB, but their total length must be bounded. This limitation is not significant in practicle, as well.

Different syntax of parametrized SQL

Syntax of parameters in cx_Oracle is different -- ":1, :2, :3" instead of "%s, %s, %s". Therefore a special code is required to translate them:

# Replace %s placeholders with :1, :2, :3...
c = 0
s = stmt
for i in re.finditer("(?<!%)%[sd]", s):
    s = s[:i.start()] + (":%d" % c) + s[i.end():]

Also, the parameters should be passed strictly as list, but in source of Trac often tuple is used. So, cx_Oracle.Cursor was replaced with OracleCursor to convert parameters to list explicitly.

Lack of LIMIT/OFFSET keywords in queries

There is no LIMIT and OFFSET keywords in Oracle -- "rownum" special column is used instead. So, all SQL queries, where these keywords present, must be wrapped in additional SELECT, filtered by "rownum":


Oracle keywords as Table/Column names

In Oracle following names are keywords and could not be used for naming Tables and Columns:

  • size
  • session
  • comment

All places in Trac source code where these names were used have had to change with acceptable names!

Parameter size limitation

Parameters, passed to cx_Oracle.execute() method are limited in length. This method could not be used to INSERT or UPDATE rows with long-data CLOB columns. Actually, following script raised error when initializing database:

INSERT INTO wiki(version,name,time,author,ipnr,text)
SELECT 1+COALESCE(max(version),0),:1,:2,
       'trac','',:3 -- :3 contains long text!
FROM wiki  WHERE name=:4
This deadlocked development! I could not find a reasonable solution for this issue and gave up! It remained only to wait until ORM approach implemented in Trac... But if anybody decides to continue working on Oracle backend, please, feel free to use this stuff.

David Avsajanishvili

Attachments (1)

Trac-0.11.4-OracleBackend.patch (31.0 KB) - added by David Avsajanishvili 8 years ago.
Patch against Trac distribution, version 0.11.4 for supporting Oracle

Download all attachments as: .zip

Change History (3)

Changed 8 years ago by David Avsajanishvili

Patch against Trac distribution, version 0.11.4 for supporting Oracle

comment:1 Changed 7 years ago by Ryan J Ollos

Component: SELECT A HACKRequest-a-Hack
Owner: changed from anonymous to anybody

This might be more appropriate for Edgewall:

comment:2 Changed 7 years ago by Richard Lyders

true, you can not insert a new CLOB directly into a table using INSERT in Oracle (I deal with Oracle 9iR2)... but you can UPDATE a CLOB directly ....

so just create the record with a NULL CLOB (use EMPTY_BLOB())

and then update the CLOB using UPDATE...

see an example here:

Modify Ticket

as new The owner will remain anybody.

Add Comment

E-mail address and name can be saved in the Preferences.

Note: See TracTickets for help on using tickets.