This file is indexed.

/usr/share/pyshared/dhm/sql/object.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
# SQLObject.py
#
# Copyright 2001, 2002,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

"""SQL object helper

This module implements SQLObject, an convenience class that allows one to
use data stored in a SQL database as if it was a standard python mapping
type.
"""

__docformat__	= "epytext en"

import UserDict

class SQLObjectException(Exception):
	"""SQLObject exception class

	@ivar reason: reason for raising this exception
	@type reason: string
	"""
	def __init__(self, reason):
		"""Constructor.

		@param reason: reason for raising this exception
		@type reason:  string
		"""
		self.reason=reason

	def __str__(self):
		return self.reason


class SQLObject(UserDict.UserDict):
	"""SQL object

	A SQL object acts like a dictionary representing a row in a
	SQL table. It keeps track of changes made (and will not allow
	changes for columns that do not exist) and can efficiently
	update the data in the SQL database when requested.

	@cvar table: name of SQL table containing data for this object
	@type table: string
	@cvar keys:  list of keys uniquely identifying an object
	@type keys:  list of strings
	@cvar attrs: list of columns
	@type attrs: list of strings
	"""
	table	= ""
	keys	= [ ]
	attrs	= [ ]

	def __init__(self, dbc=None, **kwargs):
		"""Constructor

		Extra keyword parameters are used to set initial valus for
		columns/attributes.

		@param dbc: database connection
		@type  dbc: sqlwrap.Connection class
		"""
		UserDict.UserDict.__init__(self)

		self.changes=[]

		cnonkey=0
		ckey=0
		for (key,value) in kwargs.items():
			if key in self.keys:
				ckey+=1
			else:
				cnonkey+=1

			self[key]=value

		for a in self.attrs:
			if not self.has_key(a):
				self[a]=None

		if dbc:
			self.dbc=dbc

			if ckey==len(self.keys) and not cnonkey:
				self.retrieve(dbc)


	def __setitem__(self, key, item):
		if key in self.keys and self.data.has_key(key) and self.data[key]:
			raise SQLObjectException, "cannot change key"

		self.data[key]=item
		if not key in self.changes:
			self.changes.append(key)


	def clear(self):
		"""Clear all data in this object.

		This method clear all data stored in a class instance. This
		can be used to recycle an instance so the cost of creating
		a new instance can be avoided.
		"""
		UserDict.UserDict.clear(self)
		self.changes=[]

		
	def _genwhere(self):
		"""Generate data for a WHERE clause to identify this object.

		This method generates data which can be used to generate
		a WHERE clause to uniquely identify this object in a table. It
		returns a tuple containing a string with the SQL command and a 
		tuple with the data values. This can be fed to the execute
		method for a database connection using the format paramstyle.

		@return: (command,values) tuple
		"""
		cmd=" AND ".join(map(lambda x: x+"=%s", self.keys))
		values=map(lambda x,data=self.data: data[x], self.keys)

		return (cmd,tuple(values))


	def retrieve(self, dbc=None):
		"""Retrieve object from the database.

		It is possible to retrieve the object from another database by
		specifying a different database connection.

		@param dbc: database connection to save 
		@type dbc:  sqlwrap.Connection class
		"""
		if not dbc:
			dbc=self.dbc

		for k in self.keys:
			if not self.data.has_key(k):
				raise SQLObjectException, "Not all keys set"

		(cmd,values)=self._genwhere()
		cursor=dbc.execute("SELECT * FROM %s WHERE %s;" % 
			(self.table, cmd), values, "format")

		res=cursor.fetchone()
		if not res:
			cursor.close()
			raise KeyError

		self.changes=[]
		for i in range(len(cursor.description)):
			self.data[cursor.description[i][0]]=res[i]
		cursor.close()


	def _sql_insert(self, dbc):
		attrs=[]
		vals=[]
		for a in self.attrs:
			if not (self.data.has_key(a) and 
					self.data[a]!=None):
				continue

			attrs.append(a)
			vals.append(self.data[a])

		dbc.execute("INSERT INTO %s (%s) VALUES (%s);" % 
			(self.table, ", ".join(attrs),
			 ",".join(["%s"]*len(vals))), vals, "format")


	def _sql_update(self, dbc):
		set=", ".join(map(lambda x: x+"=%s", self.changes))
		data=tuple(map(lambda x,data=self.data: data[x], self.changes))
		(cmd,values)=self._genwhere()

		dbc.execute("UPDATE %s SET %s WHERE %s;" % 
			(self.table, set, cmd), data+values, "format")


	def update(self, dbc=None):
		"""Commit changes to the database.

		If the key (or one of the keys) has been changed a new row will
		be inserted in the database. The old row will NOT be removed
		though, this has to be done manually.

		It is possible to store the object in another database by
		specifying a different database connection.

		@param dbc: database connection to save 
		@type dbc:  sqlwrap.Connection class
		"""
		if not dbc:
			dbc=self.dbc
			
		if not self.changes:
			return

		kchange=0
		for k in self.keys:
			if k in self.changes:
				kchange+=1

		if kchange or (dbc and dbc!=self.dbc):
			self._sql_insert(dbc)
		else:
			self._sql_update(dbc)

		self.changes=[]