/usr/share/pyshared/dhm/sql/wrap.py is in python-dhm 0.6-3build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 | # sqlwrap.py
#
# Copyright 2003 Wichert Akkerman <wichert@deephackmode.org>
#
# This file is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# Calculate shared library dependencies
"""DB-API wrappers
The python DB-API 2.0 provides a standard API for accessing SQL databases.
Unfortunately it has a few problems that make it hard to write portable
SQL using code:
1. there is no portable way to access the exception classes; since
they are put in the driver module you always have to know the module
name throughout your code
2. it allows for multiple different parameter styles, and each driver
module only implements one of them.
This module implements some classes that make it easier to write
portable SQL using code. It does this by adding some wrappers around
DB-API which hide the incompatibilties:
- it introduces database classes which always copy the exception class types
into a connection object
- it adds connection objects which can execute a SQL command and return
a cursor with the results, with support for converting between different
parameter styles
"""
__docformat__ = "epytext en"
import dhm.sql.utils
class Server:
"""SQL server.
This is the general SQL server wrapper; it has connect and login
information for a database and can setup database connections
when asked to.
This is an abstract base class; please use a derived class which
overloads the _connect() method with real database connection
code.
@ivar user: username to use when connecting to the database
@type user: string
@ivar password: password to use when connecting to the database
@type password: string
@ivar host: hostname of database server
@type host: string
"""
def __init__(self, user=None, password=None, host=None):
self.user=user
self.password=password
self.host=host
def _connect(self, db):
"""Connect to a database.
This function connects to a database on a SQL server
and returns the connection and the driver module
used for the connection.
@param db: database to connect to
@type db: string
@return: connection object and driver module
@rtype: tuple with Connection instance and driver module
"""
raise NotImplementedError, "Abstract base class used"
def __getitem__(self, key):
return Connection(*self._connect(key))
class PsycoServer(Server):
"""Postgres SQL server using psycopg.
"""
def _connect(self, db):
import psycopg
dbc=psycopg.connect("host=%s dbname=%s user=%s password=%s" %
(self.host, db, self.user, self.password))
return (dbc, psycopg)
class PostgresServer(Server):
"""Postgres SQL server using psycopg.
"""
def _connect(self, db):
import pyPgSQL.PgSQL
dbc=pyPgSQL.PgSQL.connect(user=self.user,
password=self.password,
host=self.host, database=db,
client_encoding="utf-8",
unicode_results=True)
return (dbc, pyPgSQL.PgSQL)
class MysqlServer(Server):
"""MySQL SQL server.
"""
def _connect(self, db):
import MySQLdb
dbc=MySQLdb.connect(user=self.user, passwd=self.password,
host=self.host, db=db)
return (dbc, MySQLdb)
class MssqlServer(Server):
"""MS-SQL server.
"""
def _connect(self, db):
import MSSQL
dbc=MSSQL.connect(self.host, self.user, self.password, db)
return (dbc, MSSQL)
class SybaseServer(Server):
"""Sybase server.
"""
def _connect(self, db):
import Sybase
dbc=Sybase.connect(self.host, self.user, self.password, db)
return (dbc, Sybase)
class Connection:
"""A database connection.
This class can do automatic paramstyle conversion if desired; to
do this simply change the paramstyle instance variable to the
desires paramstyle. Please note that not all conversion types are
implemented.
@ivar paramstyle: DB-API 2.0 paramerstyle expected
@type paramstyle: string
@ivar driverparamstyle: DB-API 2.0 paramerstyle used by the db driver
@type driverparamstyle: string
@ivar dbc: low-level connection object used internally
@type dbc: DB-API 2.0 connection object
"""
def __init__(self, dbc, module):
"""Constructor
@param dbc: low-level connection object used internally
@type dbc: DB-API 2.0 connection object
@param module: SQL driver module
@type module: python module
"""
self.dbc=dbc
self.paramstyle=module.paramstyle
self.driverparamstyle=module.paramstyle
for error in [ "Warning", "Error", "InterfaceError",
"DatabaseError", "DataError",
"OperationalError", "IntegrityError",
"InternalError", "ProgrammingError",
"NotSupportedError" ]:
setattr(self, error, getattr(module, error))
def cursor(self):
"""Return a DB-API cursor.
"""
return self.dbc.cursor()
def execute(self, operation, params=(), paramstyle=None):
"""Execute a SQL command.
@param operation: SQL command to execute
@type operation: string
@param params: parameters for SQL command
@type params: depends on paramstyle used
@param paramstyle: DB-API parameter style to used if not using the
current connection paramstyle
@type paramstyle: string
@return: cursor with results
@rtype: DB-API cursor instance
"""
if paramstyle==None:
paramstyle=self.paramstyle
cursor=self.dbc.cursor()
if params:
cursor.execute(*dhm.sql.utils.ParamStyleConvert(
paramstyle, self.driverparamstyle,
operation, params))
else:
cursor.execute(operation)
return cursor
def query(self, operation, params=(), paramstyle=None):
"""Execute a SQL query and return the results.
This is a convenience wrapper around the execute method
which returns all results directly.
@param operation: SQL command to execute
@type operation: string
@param params: parameters for SQL command
@type params: depends on paramstyle used
@param paramstyle: DB-API parameter style to used if not using the
current connection paramstyle
@type paramstyle: string
@return: list of query results
@rtype: list
"""
c=self.execute(operation, params, paramstyle)
try:
res=c.fetchall()
finally:
c.close()
return res
def close(self):
"""Close the connection to the database.
"""
self.dbc.close()
def commit(self):
"""Commit an open transaction."
"""
self.dbc.commit()
def rollback(self):
"""Roll back an open transaction."
"""
self.dbc.rollback()
class Transaction:
"""Transaction object.
This class makes using transactions a bit simpler: when you
instantiate it it will open a transaction (which is a nop in
python since the DB-API uses implicit transaction). When the
class is garbage collected the transaction is rolled back,
unless it is manually commited by calling commit(). Note that
this relies on the reference counting garbage collection
approach python uses, which will automatically delete the class
instance when it goes out of scope.
@ivar _connection: database connection
@type _connection: Connection instance or DB-API 2.0 complient
connection object
@ivar _active: flag indicating if we are in an active transaction
@type _active: boolean
"""
def __init__(self, connection):
"""Constructor.
@param connection: database connection
@type connection: Connection instance
"""
self._connection=connection
self._active=1
def __del__(self):
assert(not self._active)
def commit(self):
"""Commit the transaction.
"""
self._connection.commit()
self._active=0
def rollback(self):
"""Rollback the transaction.
"""
self._connection.rollback()
self._active=0
def GetServer(driver, **kwargs):
"""Server factory function.
This is a convenience function that returns a Server class
instance for a specific server. The supported types are:
- mysql: return a MysqlServer using the MySQLdb module
- postgres: return a PostgresServer using the psycopg module
@param driver: SQL driver to use
@type driver: string
@return: a server instance
@type: Server class instance
"""
if driver=="mysql":
return MysqlServer(**kwargs)
elif driver=="postgres":
return PostgresServer(**kwargs)
elif driver=="psyco":
return PsycoServer(**kwargs)
elif driver=="mssql":
return MssqlServer(**kwargs)
elif driver=="sybase":
return SybaseServer(**kwargs)
raise KeyError, "Unknown server type"
|