]>
git.ipfire.org Git - people/shoehn/ipfire.org.git/blob - webapp/backend/database.py
4 A lightweight wrapper around psycopg2.
6 Originally part of the Tornado framework. The tornado.database module
7 is slated for removal in Tornado 3.0, and it is now available separately
11 from __future__
import absolute_import
, division
, with_statement
20 class Connection(object):
22 A lightweight wrapper around MySQLdb DB-API connections.
24 The main value we provide is wrapping rows in a dict/object so that
25 columns can be accessed by name. Typical usage::
27 db = torndb.Connection("localhost", "mydatabase")
28 for article in db.query("SELECT * FROM articles"):
31 Cursors are hidden by the implementation, but other than that, the methods
32 are very similar to the DB-API.
34 We explicitly set the timezone to UTC and the character encoding to
35 UTF-8 on all connections to avoid time zone and encoding errors.
37 def __init__(self
, host
, database
, user
=None, password
=None):
39 self
.database
= database
44 "database" : database
,
46 "password" : password
,
52 logging
.error("Cannot connect to database on %s", self
.host
, exc_info
=True)
59 Closes this database connection.
61 if getattr(self
, "_db", None) is not None:
67 Closes the existing database connection and re-opens it.
71 self
._db
= psycopg2
.connect(**self
._db
_args
)
72 self
._db
.autocommit
= True
74 def query(self
, query
, *parameters
, **kwparameters
):
76 Returns a row list for the given query and parameters.
78 cursor
= self
._cursor
()
80 self
._execute
(cursor
, query
, parameters
, kwparameters
)
81 column_names
= [d
[0] for d
in cursor
.description
]
82 return [Row(itertools
.izip(column_names
, row
)) for row
in cursor
]
86 def get(self
, query
, *parameters
, **kwparameters
):
88 Returns the first row returned for the given query.
90 rows
= self
.query(query
, *parameters
, **kwparameters
)
94 raise Exception("Multiple rows returned for Database.get() query")
98 def execute(self
, query
, *parameters
, **kwparameters
):
100 Executes the given query, returning the lastrowid from the query.
102 return self
.execute_lastrowid(query
, *parameters
, **kwparameters
)
104 def execute_lastrowid(self
, query
, *parameters
, **kwparameters
):
106 Executes the given query, returning the lastrowid from the query.
108 cursor
= self
._cursor
()
110 self
._execute
(cursor
, query
, parameters
, kwparameters
)
111 return cursor
.lastrowid
115 def execute_rowcount(self
, query
, *parameters
, **kwparameters
):
117 Executes the given query, returning the rowcount from the query.
119 cursor
= self
._cursor
()
121 self
._execute
(cursor
, query
, parameters
, kwparameters
)
122 return cursor
.rowcount
126 def executemany(self
, query
, parameters
):
128 Executes the given query against all the given param sequences.
130 We return the lastrowid from the query.
132 return self
.executemany_lastrowid(query
, parameters
)
134 def executemany_lastrowid(self
, query
, parameters
):
136 Executes the given query against all the given param sequences.
138 We return the lastrowid from the query.
140 cursor
= self
._cursor
()
142 cursor
.executemany(query
, parameters
)
143 return cursor
.lastrowid
147 def executemany_rowcount(self
, query
, parameters
):
149 Executes the given query against all the given param sequences.
151 We return the rowcount from the query.
153 cursor
= self
._cursor
()
156 cursor
.executemany(query
, parameters
)
157 return cursor
.rowcount
161 def _ensure_connected(self
):
166 self
._ensure
_connected
()
167 return self
._db
.cursor()
169 def _execute(self
, cursor
, query
, parameters
, kwparameters
):
171 return cursor
.execute(query
, kwparameters
or parameters
)
172 except OperationalError
:
173 logging
.error("Error connecting to database on %s", self
.host
)
179 """A dict that allows for object-like property access syntax."""
180 def __getattr__(self
, name
):
184 raise AttributeError(name
)
187 # Alias some common exceptions
188 IntegrityError
= psycopg2
.IntegrityError
189 OperationalError
= psycopg2
.OperationalError