]>
git.ipfire.org Git - people/jschlag/pbs.git/blob - backend/database.py
81f80aeef39cbcf0e44522b17a1967a91d7c2ab6
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
17 class Connection(object):
19 A lightweight wrapper around MySQLdb DB-API connections.
21 The main value we provide is wrapping rows in a dict/object so that
22 columns can be accessed by name. Typical usage::
24 db = torndb.Connection("localhost", "mydatabase")
25 for article in db.query("SELECT * FROM articles"):
28 Cursors are hidden by the implementation, but other than that, the methods
29 are very similar to the DB-API.
31 We explicitly set the timezone to UTC and the character encoding to
32 UTF-8 on all connections to avoid time zone and encoding errors.
34 def __init__(self
, host
, database
, user
=None, password
=None):
36 self
.database
= database
41 "database" : database
,
43 "password" : password
,
49 logging
.error("Cannot connect to database on %s", self
.host
, exc_info
=True)
56 Closes this database connection.
58 if getattr(self
, "_db", None) is not None:
64 Closes the existing database connection and re-opens it.
68 self
._db
= psycopg2
.connect(**self
._db
_args
)
69 self
._db
.autocommit
= True
71 def query(self
, query
, *parameters
, **kwparameters
):
73 Returns a row list for the given query and parameters.
75 cursor
= self
._cursor
()
77 self
._execute
(cursor
, query
, parameters
, kwparameters
)
78 column_names
= [d
[0] for d
in cursor
.description
]
79 return [Row(itertools
.izip(column_names
, row
)) for row
in cursor
]
83 def get(self
, query
, *parameters
, **kwparameters
):
85 Returns the first row returned for the given query.
87 rows
= self
.query(query
, *parameters
, **kwparameters
)
91 raise Exception("Multiple rows returned for Database.get() query")
95 def execute(self
, query
, *parameters
, **kwparameters
):
97 Executes the given query, returning the lastrowid from the query.
99 return self
.execute_lastrowid(query
, *parameters
, **kwparameters
)
101 def execute_lastrowid(self
, query
, *parameters
, **kwparameters
):
103 Executes the given query, returning the lastrowid from the query.
105 cursor
= self
._cursor
()
107 self
._execute
(cursor
, query
, parameters
, kwparameters
)
108 return cursor
.lastrowid
112 def execute_rowcount(self
, query
, *parameters
, **kwparameters
):
114 Executes the given query, returning the rowcount from the query.
116 cursor
= self
._cursor
()
118 self
._execute
(cursor
, query
, parameters
, kwparameters
)
119 return cursor
.rowcount
123 def executemany(self
, query
, parameters
):
125 Executes the given query against all the given param sequences.
127 We return the lastrowid from the query.
129 return self
.executemany_lastrowid(query
, parameters
)
131 def executemany_lastrowid(self
, query
, parameters
):
133 Executes the given query against all the given param sequences.
135 We return the lastrowid from the query.
137 cursor
= self
._cursor
()
139 cursor
.executemany(query
, parameters
)
140 return cursor
.lastrowid
144 def executemany_rowcount(self
, query
, parameters
):
146 Executes the given query against all the given param sequences.
148 We return the rowcount from the query.
150 cursor
= self
._cursor
()
153 cursor
.executemany(query
, parameters
)
154 return cursor
.rowcount
158 def _ensure_connected(self
):
163 self
._ensure
_connected
()
164 return self
._db
.cursor()
166 def _execute(self
, cursor
, query
, parameters
, kwparameters
):
168 return cursor
.execute(query
, kwparameters
or parameters
)
169 except OperationalError
:
170 logging
.error("Error connecting to database on %s", self
.host
)
174 def transaction(self
):
175 return Transaction(self
)
179 """A dict that allows for object-like property access syntax."""
180 def __getattr__(self
, name
):
184 raise AttributeError(name
)
187 class Transaction(object):
188 def __init__(self
, db
):
191 self
.db
.execute("START TRANSACTION")
196 def __exit__(self
, exctype
, excvalue
, traceback
):
197 if exctype
is not None:
198 self
.db
.execute("ROLLBACK")
200 self
.db
.execute("COMMIT")
203 # Alias some common exceptions
204 IntegrityError
= psycopg2
.IntegrityError
205 OperationalError
= psycopg2
.OperationalError