`
huangro
  • 浏览: 327836 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Trac SQL Database API

阅读更多
Trac SQL Database API

Trac uses a very thin layer on top of the standard  Python Database API 2.0 for interfacing with supported relational database systems such as  SQLite or  PostgreSQL.

Note that Trac prior to version 0.9 used the PySQLite APIs directly, and has no support for other database systems. This document describes the thin DB API abstraction layer introduced in version 0.9.

You can find the specifics of the database API in the trac.db package. This package provides:
Simple pooling of database connections
Iterable cursors
Selection of DB modules based of connection URIs.

Accessing the Database

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.
from trac.env import Environment
def myFunc():
    env = Environment('/path/to/projenv')
    db = env.get_db_cnx()
    cursor = db.cursor()
    # Execute some SQL statements
    db.commit()
    return


Note that you should always make sure that db won't get garbage collected while cursor is still used, as the collection will do a rollback and close the cursors (avoid in particular doing cursor = env.get_db_cnx().cursor(), see r8878).

The get_db_cnx method looks at the value of the database configuration option in trac.ini, which should contain a database connection URI. The default value for this option tells Trac to use an SQLite database inside the environment directory:

[trac]
database = sqlite:db/trac.db


The connection URI syntax has been designed to be compatible with that provided by  SQLObject (see also the Wiki page on SQLObject  connections). The only supported URI schemes at this point are sqlite and postgres.

Pooled Connections

The Environment method get_db_cnx() returns a connection from the pool of connections. This connection needs to be returned, and Trac is written so that the return will happen automatically by the garbage collector if the code is written to follow a simple rule. When the garbage collector determines the pooled database connection is no longer being used, it's del method will return the pooled connection to the pool for reuse. If you have set a lexical variable in the function's body to the pooled connection, this typically occurs when the function is returning. In the example above of myFunc it occurs at the return statement since db is a variable local to myFunc

Rules for DB API Usage

Different DB API modules have different ways to pass parameters to the cursors' execute method, and different ways to access query results. To keep the database API as thin as possible, the Trac team has decided to use a relatively common subset in all database code.

Parameter passing

Always use the "format" parameter style, and always with %s (because that's the only type that pyPgSQL supports). Statement parameters always need to be passed into execute as an actual sequence (list or tuple).

So the following statements are okay:
cursor.execute("SELECT author,ipnr,comment FROM wiki WHERE name=%s", [thename])
cursor.execute("SELECT id FROM ticket WHERE time>=%s AND time<=%s", (start, stop))


The following uses are not okay:
cursor.execute("SELECT author,ipnr,comment FROM wiki WHERE name=?", thename)
cursor.execute("SELECT id FROM ticket WHERE time>=%i AND time<=%i", start, stop)


At any cost, try avoiding the use of  string formatting to get values into the SQL statement. The database automatically escapes values you pass using execute() arguments, the same is not true if you use string formatting, opening your code up to  SQL injection attacks.

On the other hand, you must use string formatting to dynamically specify names of tables or columns, i.e. anything that is not a value as such:
cursor.execute("SELECT time FROM %s WHERE name=%%s" % table, (thename,))


Retrieving results

For convenience, cursors returned by the database connection object are iterable after having executed an SQL query. Individual fields in result rows may only be accessed using integer indices:

cursor.execute("SELECT author,ipnr,comment FROM wiki WHERE name=%s", (thename,))
for row in cursor:
    print 'Author: %s (%s)' % (row[0], row[1])
    print row[2]


Accessing fields using the column names is not supported by all database modules, so it should not be used. Automatically unpacking rows into tuples of named variables often provides better readability:
cursor.execute("SELECT author,ipnr,comment FROM wiki WHERE name=%s", (thename,))
for author, ipnr, comment in cursor:
    print 'Author: %s (%s)' % (author, ipnr)
    print comment

Guidelines for SQL Statements

As you may know, support for SQL varies among different database systems. The requirements of Trac are relatively trivial, so we try to stick to a common subset that is supported by the majority of databases.

TODO: Need content
分享到:
评论

相关推荐

    trac api调用测试 c#

    trac api调用测试 c# trac+testlink测试连接用

    Trac插件开发Trac插件开发

    Trac插件开发Trac插件开发Trac插件开发Trac插件开发

    Trac应用于项目管理

    Trac应用于项目管理,经典阐述,TRAC管理首选教材 0. 前言 - 1 - 0.1. 我们遇到了什么问题? - 1 - 0.2. 什么是TRAC? - 2 - 0.3. TRAC可以做什么? - 3 - 0.4. TRAC会带来什么变化? - 3 - 1. 定义角色/活动 - 4 - 1.1...

    trac工具包trac工具包

    trac工具包 安装文件 和说明文档 安装文件 和说明文档

    Apache+Trac配置

    Apache+Trac配置Apache+Trac配置Apache+Trac配置

    Trac用户使用介绍

    Trac用户使用介绍Trac用户使用介绍Trac用户使用介绍

    trac插件开发指南

    trac插件开发指南,用于开发TRAC的插件

    trac汉化安装程序

    trac汉化安装文档和相关需要安装的程序,简单便捷的让你安装trac

    SVN+Trac配置

    很久以来就有的想法,给我们的产品开发...使用过vss,虽说简洁,但功能不完善而且有很多小BUG,所以放弃,在次研究Trac,发现Trac非常符合我的要求:免费、开源、跨平台、轻量级、高度可扩展,下面介绍下svn+trac的配置

    Trac-0.12.2及配套

    Trac-0.12.2及配套 含:Trac Babel Bitten Genshi Python

    trac install file22

    trac install file22

    Trac-1.0.1.zip

    Trac是一个为软件开发项目需要而集成了Wiki和问题跟踪管理系统的应用平台,是一个开源软件应用。Trac以简单的方式建立了一个软件项目管理的Web应用,以帮助开发人员更好地写出高质量的软件;Trac应用力求不影响现有...

    选安装trac截图详解

    精选安装trac截图详解,均有详图描述,轻松安装trac

    SVN+Trac安装笔记

    SVN+Trac安装笔记

    trac使用手册.doc

    Trac使用手册 Trac是一个为软件开发项目需要而集成了Wiki和问题跟踪管理系统的应用平台,是一个开源软件应用。Trac以简单的方式建立了一个软件项目管理的Web应用,以帮助开发人员更好地写出高质量的软件;Trac应用...

    TRAC教程【原创】

    TRAC教程【原创】 非常贴近使用的教材

    Apache+Trac+SVN

    Apache+Trac+SVN安装测试指导

    trac项目管理使用手册

    trac项目管理手册,手把手教会PM如何利用trac工具进行项目管理。内容层次清晰便于理解。

    trac开发环境搭建

    进行TRAC开,整理的资料。包括完整的开发环境说明,各程序的版本等。

    Trac安装包

    Trac安装包,可以与SVN集成,供软件开发人员管理软件版本

Global site tag (gtag.js) - Google Analytics