Changes between Version 37 and Version 38 of TracDev/DatabaseApi
- Timestamp:
- Mar 7, 2023, 8:42:51 AM (14 months ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
TracDev/DatabaseApi
v37 v38 3 3 = Trac SQL Database API 4 4 5 Trac uses a very thin layer on top of the standard [http://www.python.org/peps/pep-0249.html Python Database API 2.0] for interfacing with supported relational database systems such as [http://sqlite.org/ SQLite], [http://www.postgresql.org/ PostgreSQL] or theMySqlDb systems.5 Trac uses a thin layer on top of the standard [http://www.python.org/peps/pep-0249.html Python Database API 2.0] to interface with supported relational database systems such as [http://sqlite.org/ SQLite], [http://www.postgresql.org/ PostgreSQL] and MySqlDb systems. 6 6 7 7 You can find the specifics of the database API in the [source:/trunk/trac/db/ trac.db] package. This package provides: … … 16 16 === API before Trac 0.12 17 17 18 //This is how things used to be for Trac 0.5 - 0.11, and is stillsupported until [milestone:1.1.2]. Its use is discouraged for new code.//18 //This is the style for Trac 0.5 - 0.11 and supported until [milestone:1.1.2]. Its use is discouraged for new code.// 19 19 20 20 Code accessing the database in Trac go through this layer simply by using the {{{Environment}}} method {{{get_db_cnx()}}} 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: … … 36 36 === Trac 0.12 API 37 37 38 //Th e 0.12 style can still be used in 1.0, but for new code we encourage you to focus on Trac version 1.0 and adopt the even simpler 1.0 style. See next sections for details. The 0.12 style was removed in [milestone:1.3.1].//38 //This is the style for Trac 0.12 and deprecated in [milestone:1.3.1]. Its use is discouraged for new code.// 39 39 40 40 As support for Python 2.3 has been dropped, we could make use of decorators to get rid of some of the boilerplate code: … … 60 60 ==== Nested Transactions 61 61 62 With the API described above (since ''0.12'') it is possible to have nested transactions . A nested transaction is best explained by anexample:62 With the API described above (since ''0.12'') it is possible to have nested transactions, as in the following example: 63 63 64 64 {{{#!python … … 93 93 94 94 [=#Trac0.13API] 95 //This style is supported for Trac version 1.0 and later. 96 Starting with [milestone:1.3.1], this is actually the only way, as the deprecated APIs were removed.// 97 98 As we dropped support for Python 2.4, we could simplify the code a bit more by using the `with` keyword and context managers: 95 //This style is supported for Trac version 1.0 and later. Starting with [milestone:1.3.1], this is actually the only way, as the deprecated APIs were removed.// 96 97 As we dropped support for Python 2.4, we simplified the code by using the `with` keyword and context managers: 99 98 100 99 {{{#!python … … 202 201 }}} 203 202 204 Note that dict comprehensions are only available in Python 2.7 205 and Trac < 1.3.1 supports Python 2.6. 203 Note that dict comprehensions are only available in Python 2.7 and Trac < 1.3.1 supports Python 2.6. 206 204 207 205 These short forms also present an additional safety measure as they're only allowing a "SELECT" query to be executed by a read-only connection. Indeed, the same short forms are possible on both `env.db_transaction` and `env.db_query`.