Changes between Version 26 and Version 27 of TracDev/DatabaseApi
- Timestamp:
- Sep 8, 2012, 1:22:47 PM (12 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
TracDev/DatabaseApi
v26 v27 11 11 12 12 == Accessing the Database == 13 14 The DB API has evolved quite a lot in the recent versions of Trac, and the usage is substantially different whether you're working with Trac 0.11, 0.12 or 1.0 and up. 15 16 13 17 === API before Trac 0.12 18 19 //This is how things used to be for Trac 0.5 - 0.11, and is still supported in Trac 0.12, but in new code this style should be definitely **not** used, as it introduces all the problems that the new styles are solving (see next sections for details). // 14 20 15 21 Code accessing the database in Trac go through this layer simply by using the {{{Environment}}} method {{{get_db_cnx()}}} in order to get a pooled database connection. A database cursor can be obtained from the pooled database connection, and the code uses the cursor in conjunction with SQL statements to implement the behavior. … … 32 38 === Trac 0.12 API 33 39 40 // The 0.12 style can still be used in 1.0, but for new code we encourage you to focus on the Trac version 1.0 and to adopt the even simpler 1.0 style (see next section). // 41 34 42 As we dropped support for Python 2.3, we could make use of decorators to get rid of some of the boilerplate code. 35 43 … … 46 54 }}} 47 55 48 This is slightly simpler and more robustthan the previous API, as not only the `commit()` is performed in any successful case (i.e. the `Execute some SQL statements` code can `return` at any56 This is slightly simpler and **more robust** than the previous API, as not only the `commit()` is performed in any successful case (i.e. the `Execute some SQL statements` code can `return` at any 49 57 place), but a `rollback` will be performed should an exception be triggered in that block. 50 58 … … 87 95 ''Note:'' Uncommited changes of a transaction are only visible to nested transactions in the same thread. Different threads use different database connections and therefore different transactions. 88 96 89 === Trac 0.13dev and 1.0 API #Trac0.13API 97 === Trac 1.0 API #Trac0.13API 98 99 // This style is supported for version 1.0.x, 1.1.x and will be supported beyond as well. // 90 100 91 101 As we dropped support for Python 2.4, we could simplify the code a bit more by using the `with` keyword and context managers: