root / pykota / branches / specialauth / pykota / storages / sql.py @ 3179

Revision 3179, 55.3 kB (checked in by jerome, 17 years ago)

Special DB auth seems to work fine with clear text passwords.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1# PyKota
2# -*- coding: ISO-8859-15 -*-
3#
4# PyKota : Print Quotas for CUPS and LPRng
5#
6# (c) 2003, 2004, 2005, 2006, 2007 Jerome Alet <alet@librelogiciel.com>
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 2 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program; if not, write to the Free Software
19# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20#
21# $Id$
22#
23#
24
25from pykota.storage import StorageUser, StorageGroup, StoragePrinter, \
26                           StorageJob, StorageLastJob, StorageUserPQuota, \
27                           StorageGroupPQuota, StorageBillingCode
28
29class SQLStorage :
30    def storageUserFromRecord(self, username, record) :
31        """Returns a StorageUser instance from a database record."""
32        user = StorageUser(self, username)
33        user.ident = record.get("uid", record.get("userid", record.get("id")))
34        user.LimitBy = record.get("limitby") or "quota"
35        user.AccountBalance = record.get("balance")
36        user.LifeTimePaid = record.get("lifetimepaid")
37        user.Email = record.get("email")
38        user.Password = record.get("password")
39        user.Description = self.databaseToUserCharset(record.get("description"))
40        user.OverCharge = record.get("overcharge", 1.0)
41        user.Exists = True
42        return user
43       
44    def storageGroupFromRecord(self, groupname, record) :
45        """Returns a StorageGroup instance from a database record."""
46        group = StorageGroup(self, groupname)
47        group.ident = record.get("id")
48        group.LimitBy = record.get("limitby") or "quota"
49        group.AccountBalance = record.get("balance")
50        group.LifeTimePaid = record.get("lifetimepaid")
51        group.Description = self.databaseToUserCharset(record.get("description"))
52        group.Exists = True
53        return group
54       
55    def storagePrinterFromRecord(self, printername, record) :
56        """Returns a StoragePrinter instance from a database record."""
57        printer = StoragePrinter(self, printername)
58        printer.ident = record.get("id")
59        printer.PricePerJob = record.get("priceperjob") or 0.0
60        printer.PricePerPage = record.get("priceperpage") or 0.0
61        printer.MaxJobSize = record.get("maxjobsize") or 0
62        printer.PassThrough = record.get("passthrough") or 0
63        if printer.PassThrough in (1, "1", "t", "true", "TRUE", "True") :
64            printer.PassThrough = True
65        else :
66            printer.PassThrough = False
67        printer.Description = self.databaseToUserCharset(record.get("description") or "") # TODO : is 'or ""' still needed ?
68        printer.Exists = True
69        return printer
70       
71    def setJobAttributesFromRecord(self, job, record) :   
72        """Sets the attributes of a job from a database record."""
73        job.ident = record.get("id")
74        job.JobId = record.get("jobid")
75        job.PrinterPageCounter = record.get("pagecounter")
76        job.JobSize = record.get("jobsize")
77        job.JobPrice = record.get("jobprice")
78        job.JobAction = record.get("action")
79        job.JobFileName = self.databaseToUserCharset(record.get("filename") or "") 
80        job.JobTitle = self.databaseToUserCharset(record.get("title") or "") 
81        job.JobCopies = record.get("copies")
82        job.JobOptions = self.databaseToUserCharset(record.get("options") or "") 
83        job.JobDate = record.get("jobdate")
84        job.JobHostName = record.get("hostname")
85        job.JobSizeBytes = record.get("jobsizebytes")
86        job.JobMD5Sum = record.get("md5sum")
87        job.JobPages = record.get("pages")
88        job.JobBillingCode = self.databaseToUserCharset(record.get("billingcode") or "")
89        job.PrecomputedJobSize = record.get("precomputedjobsize")
90        job.PrecomputedJobPrice = record.get("precomputedjobprice")
91        job.UserName = self.databaseToUserCharset(record.get("username"))
92        job.PrinterName = self.databaseToUserCharset(record.get("printername"))
93        if job.JobTitle == job.JobFileName == job.JobOptions == "hidden" :
94            (job.JobTitle, job.JobFileName, job.JobOptions) = (_("Hidden because of privacy concerns"),) * 3
95        job.Exists = True
96       
97    def storageJobFromRecord(self, record) :
98        """Returns a StorageJob instance from a database record."""
99        job = StorageJob(self)
100        self.setJobAttributesFromRecord(job, record)
101        return job
102       
103    def storageLastJobFromRecord(self, printer, record) :
104        """Returns a StorageLastJob instance from a database record."""
105        lastjob = StorageLastJob(self, printer)
106        self.setJobAttributesFromRecord(lastjob, record)
107        return lastjob
108       
109    def storageUserPQuotaFromRecord(self, user, printer, record) :
110        """Returns a StorageUserPQuota instance from a database record."""
111        userpquota = StorageUserPQuota(self, user, printer)
112        userpquota.ident = record.get("id")
113        userpquota.PageCounter = record.get("pagecounter")
114        userpquota.LifePageCounter = record.get("lifepagecounter")
115        userpquota.SoftLimit = record.get("softlimit")
116        userpquota.HardLimit = record.get("hardlimit")
117        userpquota.DateLimit = record.get("datelimit")
118        userpquota.WarnCount = record.get("warncount") or 0
119        userpquota.Exists = True
120        return userpquota
121       
122    def storageGroupPQuotaFromRecord(self, group, printer, record) :
123        """Returns a StorageGroupPQuota instance from a database record."""
124        grouppquota = StorageGroupPQuota(self, group, printer)
125        grouppquota.ident = record.get("id")
126        grouppquota.SoftLimit = record.get("softlimit")
127        grouppquota.HardLimit = record.get("hardlimit")
128        grouppquota.DateLimit = record.get("datelimit")
129        result = self.doSearch("SELECT SUM(lifepagecounter) AS lifepagecounter, SUM(pagecounter) AS pagecounter FROM userpquota WHERE printerid=%s AND userid IN (SELECT userid FROM groupsmembers WHERE groupid=%s)" \
130                      % (self.doQuote(printer.ident), self.doQuote(group.ident)))
131        if result :
132            grouppquota.PageCounter = result[0].get("pagecounter") or 0
133            grouppquota.LifePageCounter = result[0].get("lifepagecounter") or 0
134        grouppquota.Exists = True
135        return grouppquota
136       
137    def storageBillingCodeFromRecord(self, billingcode, record) :
138        """Returns a StorageBillingCode instance from a database record."""
139        code = StorageBillingCode(self, billingcode)
140        code.ident = record.get("id")
141        code.Description = self.databaseToUserCharset(record.get("description") or "") # TODO : is 'or ""' still needed ?
142        code.Balance = record.get("balance") or 0.0
143        code.PageCounter = record.get("pagecounter") or 0
144        code.Exists = True
145        return code
146       
147    def createFilter(self, only) :   
148        """Returns the appropriate SQL filter."""
149        if only :
150            expressions = []
151            for (k, v) in only.items() :
152                expressions.append("%s=%s" % (k, self.doQuote(self.userCharsetToDatabase(v))))
153            return " AND ".join(expressions)     
154        return ""       
155       
156    def createOrderBy(self, default, ordering) :   
157        """Creates a suitable ORDER BY statement based on a list of fieldnames prefixed with '+' (ASC) or '-' (DESC)."""
158        statements = []
159        if not ordering :
160            ordering = default
161        for field in ordering :   
162            if field.startswith("-") :   
163                statements.append("%s DESC" % field[1:])
164            elif field.startswith("+") :
165                statements.append("%s ASC" % field[1:])
166            else :   
167                statements.append("%s ASC" % field)
168        return ", ".join(statements)   
169       
170    def extractPrinters(self, extractonly={}, ordering=[]) :
171        """Extracts all printer records."""
172        thefilter = self.createFilter(extractonly)
173        if thefilter :
174            thefilter = "WHERE %s" % thefilter
175        orderby = self.createOrderBy(["+id"], ordering)
176        result = self.doRawSearch("SELECT * FROM printers %(thefilter)s ORDER BY %(orderby)s" % locals())
177        return self.prepareRawResult(result)
178       
179    def extractUsers(self, extractonly={}, ordering=[]) :
180        """Extracts all user records."""
181        thefilter = self.createFilter(extractonly)
182        if thefilter :
183            thefilter = "WHERE %s" % thefilter
184        orderby = self.createOrderBy(["+id"], ordering)
185        result = self.doRawSearch("SELECT * FROM users %(thefilter)s ORDER BY %(orderby)s" % locals())
186        return self.prepareRawResult(result)
187       
188    def extractBillingcodes(self, extractonly={}, ordering=[]) :
189        """Extracts all billing codes records."""
190        thefilter = self.createFilter(extractonly)
191        if thefilter :
192            thefilter = "WHERE %s" % thefilter
193        orderby = self.createOrderBy(["+id"], ordering)
194        result = self.doRawSearch("SELECT * FROM billingcodes %(thefilter)s ORDER BY %(orderby)s" % locals())
195        return self.prepareRawResult(result)
196       
197    def extractGroups(self, extractonly={}, ordering=[]) :
198        """Extracts all group records."""
199        thefilter = self.createFilter(extractonly)
200        if thefilter :
201            thefilter = "WHERE %s" % thefilter
202        orderby = self.createOrderBy(["+groups.id"], ordering)
203        result = self.doRawSearch("SELECT groups.*,COALESCE(SUM(balance), 0) AS balance, COALESCE(SUM(lifetimepaid), 0) as lifetimepaid FROM groups LEFT OUTER JOIN users ON users.id IN (SELECT userid FROM groupsmembers WHERE groupid=groups.id) %(thefilter)s GROUP BY groups.id,groups.groupname,groups.limitby,groups.description ORDER BY %(orderby)s" % locals())
204        return self.prepareRawResult(result)
205       
206    def extractPayments(self, extractonly={}, ordering=[]) :
207        """Extracts all payment records."""
208        startdate = extractonly.get("start")
209        enddate = extractonly.get("end")
210        for limit in ("start", "end") :
211            try :
212                del extractonly[limit]
213            except KeyError :   
214                pass
215        thefilter = self.createFilter(extractonly)
216        if thefilter :
217            thefilter = "AND %s" % thefilter
218        (startdate, enddate) = self.cleanDates(startdate, enddate)
219        if startdate : 
220            thefilter = "%s AND date>=%s" % (thefilter, self.doQuote(startdate))
221        if enddate : 
222            thefilter = "%s AND date<=%s" % (thefilter, self.doQuote(enddate))
223        orderby = self.createOrderBy(["+payments.id"], ordering)
224        result = self.doRawSearch("SELECT username,payments.* FROM users,payments WHERE users.id=payments.userid %(thefilter)s ORDER BY %(orderby)s" % locals())
225        return self.prepareRawResult(result)
226       
227    def extractUpquotas(self, extractonly={}, ordering=[]) :
228        """Extracts all userpquota records."""
229        thefilter = self.createFilter(extractonly)
230        if thefilter :
231            thefilter = "AND %s" % thefilter
232        orderby = self.createOrderBy(["+userpquota.id"], ordering)
233        result = self.doRawSearch("SELECT users.username,printers.printername,userpquota.* FROM users,printers,userpquota WHERE users.id=userpquota.userid AND printers.id=userpquota.printerid %(thefilter)s ORDER BY %(orderby)s" % locals())
234        return self.prepareRawResult(result)
235       
236    def extractGpquotas(self, extractonly={}, ordering=[]) :
237        """Extracts all grouppquota records."""
238        thefilter = self.createFilter(extractonly)
239        if thefilter :
240            thefilter = "AND %s" % thefilter
241        orderby = self.createOrderBy(["+grouppquota.id"], ordering)
242        result = self.doRawSearch("SELECT groups.groupname,printers.printername,grouppquota.*,coalesce(sum(pagecounter), 0) AS pagecounter,coalesce(sum(lifepagecounter), 0) AS lifepagecounter FROM groups,printers,grouppquota,userpquota WHERE groups.id=grouppquota.groupid AND printers.id=grouppquota.printerid AND userpquota.printerid=grouppquota.printerid AND userpquota.userid IN (SELECT userid FROM groupsmembers WHERE groupsmembers.groupid=grouppquota.groupid) %(thefilter)s GROUP BY grouppquota.id,grouppquota.groupid,grouppquota.printerid,grouppquota.softlimit,grouppquota.hardlimit,grouppquota.datelimit,grouppquota.maxjobsize,groups.groupname,printers.printername ORDER BY %(orderby)s" % locals())
243        return self.prepareRawResult(result)
244       
245    def extractUmembers(self, extractonly={}, ordering=[]) :
246        """Extracts all user groups members."""
247        thefilter = self.createFilter(extractonly)
248        if thefilter :
249            thefilter = "AND %s" % thefilter
250        orderby = self.createOrderBy(["+groupsmembers.groupid", "+groupsmembers.userid"], ordering)
251        result = self.doRawSearch("SELECT groups.groupname, users.username, groupsmembers.* FROM groups,users,groupsmembers WHERE users.id=groupsmembers.userid AND groups.id=groupsmembers.groupid %(thefilter)s ORDER BY %(orderby)s" % locals())
252        return self.prepareRawResult(result)
253       
254    def extractPmembers(self, extractonly={}, ordering=[]) :
255        """Extracts all printer groups members."""
256        for (k, v) in extractonly.items() :
257            if k == "pgroupname" :
258                del extractonly[k]
259                extractonly["p1.printername"] = v
260            elif k == "printername" :
261                del extractonly[k]
262                extractonly["p2.printername"] = v
263        thefilter = self.createFilter(extractonly)
264        if thefilter :
265            thefilter = "AND %s" % thefilter
266        orderby = self.createOrderBy(["+printergroupsmembers.groupid", "+printergroupsmembers.printerid"], ordering)
267        result = self.doRawSearch("SELECT p1.printername as pgroupname, p2.printername as printername, printergroupsmembers.* FROM printers p1, printers p2, printergroupsmembers WHERE p1.id=printergroupsmembers.groupid AND p2.id=printergroupsmembers.printerid %(thefilter)s ORDER BY %(orderby)s" % locals())
268        return self.prepareRawResult(result)
269       
270    def extractHistory(self, extractonly={}, ordering=[]) :
271        """Extracts all jobhistory records."""
272        startdate = extractonly.get("start")
273        enddate = extractonly.get("end")
274        for limit in ("start", "end") :
275            try :
276                del extractonly[limit]
277            except KeyError :   
278                pass
279        thefilter = self.createFilter(extractonly)
280        if thefilter :
281            thefilter = "AND %s" % thefilter
282        (startdate, enddate) = self.cleanDates(startdate, enddate)
283        if startdate : 
284            thefilter = "%s AND jobdate>=%s" % (thefilter, self.doQuote(startdate))
285        if enddate : 
286            thefilter = "%s AND jobdate<=%s" % (thefilter, self.doQuote(enddate))
287        orderby = self.createOrderBy(["+jobhistory.id"], ordering)
288        result = self.doRawSearch("SELECT users.username,printers.printername,jobhistory.* FROM users,printers,jobhistory WHERE users.id=jobhistory.userid AND printers.id=jobhistory.printerid %(thefilter)s ORDER BY %(orderby)s" % locals())
289        return self.prepareRawResult(result)
290           
291    def filterNames(self, records, attribute, patterns=None) :
292        """Returns a list of 'attribute' from a list of records.
293       
294           Logs any missing attribute.
295        """   
296        result = []
297        for record in records :
298            attrval = record.get(attribute, [None])
299            if attrval is None :
300                self.tool.printInfo("Object %s has no %s attribute !" % (repr(record), attribute), "error")
301            else :
302                attrval = self.databaseToUserCharset(attrval)
303                if patterns :
304                    if (not isinstance(patterns, type([]))) and (not isinstance(patterns, type(()))) :
305                        patterns = [ patterns ]
306                    if self.tool.matchString(attrval, patterns) :
307                        result.append(attrval)
308                else :   
309                    result.append(attrval)
310        return result   
311               
312    def getAllBillingCodes(self, billingcode=None) :   
313        """Extracts all billing codes or only the billing codes matching the optional parameter."""
314        result = self.doSearch("SELECT billingcode FROM billingcodes")
315        if result :
316            return self.filterNames(result, "billingcode", billingcode)
317        else :   
318            return []
319       
320    def getAllPrintersNames(self, printername=None) :   
321        """Extracts all printer names or only the printers' names matching the optional parameter."""
322        result = self.doSearch("SELECT printername FROM printers")
323        if result :
324            return self.filterNames(result, "printername", printername)
325        else :   
326            return []
327   
328    def getAllUsersNames(self, username=None) :   
329        """Extracts all user names."""
330        result = self.doSearch("SELECT username FROM users")
331        if result :
332            return self.filterNames(result, "username", username)
333        else :   
334            return []
335       
336    def getAllGroupsNames(self, groupname=None) :   
337        """Extracts all group names."""
338        result = self.doSearch("SELECT groupname FROM groups")
339        if result :
340            return self.filterNames(result, "groupname", groupname)
341        else :
342            return []
343       
344    def getUserNbJobsFromHistory(self, user) :
345        """Returns the number of jobs the user has in history."""
346        result = self.doSearch("SELECT COUNT(*) FROM jobhistory WHERE userid=%s" % self.doQuote(user.ident))
347        if result :
348            return result[0]["count"]
349        return 0
350       
351    def getUserFromBackend(self, username) :   
352        """Extracts user information given its name."""
353        result = self.doSearch("SELECT * FROM users WHERE username=%s LIMIT 1"\
354                      % self.doQuote(self.userCharsetToDatabase(username)))
355        if result :
356            return self.storageUserFromRecord(username, result[0])
357        else :   
358            return StorageUser(self, username)
359       
360    def getGroupFromBackend(self, groupname) :   
361        """Extracts group information given its name."""
362        result = self.doSearch("SELECT groups.*,COALESCE(SUM(balance), 0.0) AS balance, COALESCE(SUM(lifetimepaid), 0.0) AS lifetimepaid FROM groups LEFT OUTER JOIN users ON users.id IN (SELECT userid FROM groupsmembers WHERE groupid=groups.id) WHERE groupname=%s GROUP BY groups.id,groups.groupname,groups.limitby,groups.description LIMIT 1" \
363                      % self.doQuote(self.userCharsetToDatabase(groupname)))
364        if result :
365            return self.storageGroupFromRecord(groupname, result[0])
366        else :   
367            return StorageGroup(self, groupname)
368       
369    def getPrinterFromBackend(self, printername) :       
370        """Extracts printer information given its name."""
371        result = self.doSearch("SELECT * FROM printers WHERE printername=%s LIMIT 1" \
372                      % self.doQuote(self.userCharsetToDatabase(printername)))
373        if result :
374            return self.storagePrinterFromRecord(printername, result[0])
375        else :   
376            return StoragePrinter(self, printername)
377       
378    def getBillingCodeFromBackend(self, label) :       
379        """Extracts a billing code information given its name."""
380        result = self.doSearch("SELECT * FROM billingcodes WHERE billingcode=%s LIMIT 1" \
381                      % self.doQuote(self.userCharsetToDatabase(label)))
382        if result :
383            return self.storageBillingCodeFromRecord(label, result[0])
384        else :   
385            return StorageBillingCode(self, label)
386       
387    def getUserPQuotaFromBackend(self, user, printer) :       
388        """Extracts a user print quota."""
389        if printer.Exists and user.Exists :
390            result = self.doSearch("SELECT * FROM userpquota WHERE userid=%s AND printerid=%s;" \
391                          % (self.doQuote(user.ident), self.doQuote(printer.ident)))
392            if result :
393                return self.storageUserPQuotaFromRecord(user, printer, result[0])
394        return StorageUserPQuota(self, user, printer)
395       
396    def getGroupPQuotaFromBackend(self, group, printer) :       
397        """Extracts a group print quota."""
398        if printer.Exists and group.Exists :
399            result = self.doSearch("SELECT * FROM grouppquota WHERE groupid=%s AND printerid=%s" \
400                          % (self.doQuote(group.ident), self.doQuote(printer.ident)))
401            if result :
402                return self.storageGroupPQuotaFromRecord(group, printer, result[0])
403        return StorageGroupPQuota(self, group, printer)
404       
405    def getPrinterLastJobFromBackend(self, printer) :       
406        """Extracts a printer's last job information."""
407        result = self.doSearch("SELECT jobhistory.id, jobid, userid, username, pagecounter, jobsize, jobprice, filename, title, copies, options, hostname, jobdate, md5sum, pages, billingcode, precomputedjobsize, precomputedjobprice FROM jobhistory, users WHERE printerid=%s AND userid=users.id ORDER BY jobdate DESC LIMIT 1" % self.doQuote(printer.ident))
408        if result :
409            return self.storageLastJobFromRecord(printer, result[0])
410        else :   
411            return StorageLastJob(self, printer)
412           
413    def getGroupMembersFromBackend(self, group) :       
414        """Returns the group's members list."""
415        groupmembers = []
416        result = self.doSearch("SELECT * FROM groupsmembers JOIN users ON groupsmembers.userid=users.id WHERE groupid=%s" % self.doQuote(group.ident))
417        if result :
418            for record in result :
419                user = self.storageUserFromRecord(self.databaseToUserCharset(record.get("username")), \
420                                                  record)
421                groupmembers.append(user)
422                self.cacheEntry("USERS", user.Name, user)
423        return groupmembers       
424       
425    def getUserGroupsFromBackend(self, user) :       
426        """Returns the user's groups list."""
427        groups = []
428        result = self.doSearch("SELECT groupname FROM groupsmembers JOIN groups ON groupsmembers.groupid=groups.id WHERE userid=%s" % self.doQuote(user.ident))
429        if result :
430            for record in result :
431                groups.append(self.getGroup(self.databaseToUserCharset(record.get("groupname"))))
432        return groups       
433       
434    def getParentPrintersFromBackend(self, printer) :   
435        """Get all the printer groups this printer is a member of."""
436        pgroups = []
437        result = self.doSearch("SELECT groupid,printername FROM printergroupsmembers JOIN printers ON groupid=id WHERE printerid=%s" % self.doQuote(printer.ident))
438        if result :
439            for record in result :
440                if record["groupid"] != printer.ident : # in case of integrity violation
441                    parentprinter = self.getPrinter(self.databaseToUserCharset(record.get("printername")))
442                    if parentprinter.Exists :
443                        pgroups.append(parentprinter)
444        return pgroups
445       
446    def getMatchingPrinters(self, printerpattern) :
447        """Returns the list of all printers for which name matches a certain pattern."""
448        printers = []
449        # We 'could' do a SELECT printername FROM printers WHERE printername LIKE ...
450        # but we don't because other storages semantics may be different, so every
451        # storage should use fnmatch to match patterns and be storage agnostic
452        result = self.doSearch("SELECT * FROM printers")
453        if result :
454            patterns = printerpattern.split(",")
455            try :
456                patdict = {}.fromkeys(patterns)
457            except AttributeError :   
458                # Python v2.2 or earlier
459                patdict = {}
460                for p in patterns :
461                    patdict[p] = None
462            for record in result :
463                pname = self.databaseToUserCharset(record["printername"])
464                if patdict.has_key(pname) or self.tool.matchString(pname, patterns) :
465                    printer = self.storagePrinterFromRecord(pname, record)
466                    printers.append(printer)
467                    self.cacheEntry("PRINTERS", printer.Name, printer)
468        return printers       
469       
470    def getMatchingUsers(self, userpattern) :
471        """Returns the list of all users for which name matches a certain pattern."""
472        users = []
473        # We 'could' do a SELECT username FROM users WHERE username LIKE ...
474        # but we don't because other storages semantics may be different, so every
475        # storage should use fnmatch to match patterns and be storage agnostic
476        result = self.doSearch("SELECT * FROM users")
477        if result :
478            patterns = userpattern.split(",")
479            try :
480                patdict = {}.fromkeys(patterns)
481            except AttributeError :   
482                # Python v2.2 or earlier
483                patdict = {}
484                for p in patterns :
485                    patdict[p] = None
486            for record in result :
487                uname = self.databaseToUserCharset(record["username"])
488                if patdict.has_key(uname) or self.tool.matchString(uname, patterns) :
489                    user = self.storageUserFromRecord(uname, record)
490                    users.append(user)
491                    self.cacheEntry("USERS", user.Name, user)
492        return users       
493       
494    def getMatchingGroups(self, grouppattern) :
495        """Returns the list of all groups for which name matches a certain pattern."""
496        groups = []
497        # We 'could' do a SELECT groupname FROM groups WHERE groupname LIKE ...
498        # but we don't because other storages semantics may be different, so every
499        # storage should use fnmatch to match patterns and be storage agnostic
500        result = self.doSearch("SELECT groups.*,COALESCE(SUM(balance), 0.0) AS balance, COALESCE(SUM(lifetimepaid), 0.0) AS lifetimepaid FROM groups LEFT OUTER JOIN users ON users.id IN (SELECT userid FROM groupsmembers WHERE groupid=groups.id) GROUP BY groups.id,groups.groupname,groups.limitby,groups.description")
501        if result :
502            patterns = grouppattern.split(",")
503            try :
504                patdict = {}.fromkeys(patterns)
505            except AttributeError :   
506                # Python v2.2 or earlier
507                patdict = {}
508                for p in patterns :
509                    patdict[p] = None
510            for record in result :
511                gname = self.databaseToUserCharset(record["groupname"])
512                if patdict.has_key(gname) or self.tool.matchString(gname, patterns) :
513                    group = self.storageGroupFromRecord(gname, record)
514                    groups.append(group)
515                    self.cacheEntry("GROUPS", group.Name, group)
516        return groups       
517       
518    def getMatchingBillingCodes(self, billingcodepattern) :
519        """Returns the list of all billing codes for which the label matches a certain pattern."""
520        codes = []
521        result = self.doSearch("SELECT * FROM billingcodes")
522        if result :
523            patterns = billingcodepattern.split(",")
524            try :
525                patdict = {}.fromkeys(patterns)
526            except AttributeError :   
527                # Python v2.2 or earlier
528                patdict = {}
529                for p in patterns :
530                    patdict[p] = None
531            for record in result :
532                codename = self.databaseToUserCharset(record["billingcode"])
533                if patdict.has_key(codename) or self.tool.matchString(codename, patterns) :
534                    code = self.storageBillingCodeFromRecord(codename, record)
535                    codes.append(code)
536                    self.cacheEntry("BILLINGCODES", code.BillingCode, code)
537        return codes       
538       
539    def getPrinterUsersAndQuotas(self, printer, names=["*"]) :       
540        """Returns the list of users who uses a given printer, along with their quotas."""
541        usersandquotas = []
542        result = self.doSearch("SELECT users.id as uid,username,description,balance,lifetimepaid,limitby,email,overcharge,userpquota.id,lifepagecounter,pagecounter,softlimit,hardlimit,datelimit,warncount FROM users JOIN userpquota ON users.id=userpquota.userid AND printerid=%s ORDER BY username ASC" % self.doQuote(printer.ident))
543        if result :
544            for record in result :
545                uname = self.databaseToUserCharset(record.get("username"))
546                if self.tool.matchString(uname, names) :
547                    user = self.storageUserFromRecord(uname, record)
548                    userpquota = self.storageUserPQuotaFromRecord(user, printer, record)
549                    usersandquotas.append((user, userpquota))
550                    self.cacheEntry("USERS", user.Name, user)
551                    self.cacheEntry("USERPQUOTAS", "%s@%s" % (user.Name, printer.Name), userpquota)
552        return usersandquotas
553               
554    def getPrinterGroupsAndQuotas(self, printer, names=["*"]) :       
555        """Returns the list of groups which uses a given printer, along with their quotas."""
556        groupsandquotas = []
557        result = self.doSearch("SELECT groupname FROM groups JOIN grouppquota ON groups.id=grouppquota.groupid AND printerid=%s ORDER BY groupname ASC" % self.doQuote(printer.ident))
558        if result :
559            for record in result :
560                gname = self.databaseToUserCharset(record.get("groupname"))
561                if self.tool.matchString(gname, names) :
562                    group = self.getGroup(gname)
563                    grouppquota = self.getGroupPQuota(group, printer)
564                    groupsandquotas.append((group, grouppquota))
565        return groupsandquotas
566       
567    def addPrinter(self, printer) :       
568        """Adds a printer to the quota storage, returns the old value if it already exists."""
569        oldentry = self.getPrinter(printer.Name)
570        if oldentry.Exists :
571            return oldentry
572        self.doModify("INSERT INTO printers (printername, passthrough, maxjobsize, description, priceperpage, priceperjob) VALUES (%s, %s, %s, %s, %s, %s)" \
573                          % (self.doQuote(self.userCharsetToDatabase(printer.Name)), \
574                             self.doQuote((printer.PassThrough and "t") or "f"), \
575                             self.doQuote(printer.MaxJobSize or 0), \
576                             self.doQuote(self.userCharsetToDatabase(printer.Description)), \
577                             self.doQuote(printer.PricePerPage or 0.0), \
578                             self.doQuote(printer.PricePerJob or 0.0)))
579        printer.isDirty = False
580        return None # the entry created doesn't need further modification
581       
582    def addBillingCode(self, bcode) :
583        """Adds a billing code to the quota storage, returns the old value if it already exists."""
584        oldentry = self.getBillingCode(bcode.BillingCode)
585        if oldentry.Exists :
586            return oldentry
587        self.doModify("INSERT INTO billingcodes (billingcode, balance, pagecounter, description) VALUES (%s, %s, %s, %s)" \
588                           % (self.doQuote(self.userCharsetToDatabase(bcode.BillingCode)), 
589                              self.doQuote(bcode.Balance or 0.0), \
590                              self.doQuote(bcode.PageCounter or 0), \
591                              self.doQuote(self.userCharsetToDatabase(bcode.Description))))
592        bcode.isDirty = False
593        return None # the entry created doesn't need further modification
594       
595    def addUser(self, user) :       
596        """Adds a user to the quota storage, returns the old value if it already exists."""
597        oldentry = self.getUser(user.Name)
598        if oldentry.Exists :
599            return oldentry
600        self.doModify("INSERT INTO users (username, limitby, balance, lifetimepaid, email, overcharge, description) VALUES (%s, %s, %s, %s, %s, %s, %s)" % \
601                                     (self.doQuote(self.userCharsetToDatabase(user.Name)), \
602                                      self.doQuote(user.LimitBy or 'quota'), \
603                                      self.doQuote(user.AccountBalance or 0.0), \
604                                      self.doQuote(user.LifeTimePaid or 0.0), \
605                                      self.doQuote(user.Email), \
606                                      self.doQuote(user.OverCharge), \
607                                      self.doQuote(self.userCharsetToDatabase(user.Description))))
608        if user.PaymentsBacklog :
609            for (value, comment) in user.PaymentsBacklog :
610                self.writeNewPayment(user, value, comment)
611            user.PaymentsBacklog = []
612        user.isDirty = False
613        return None # the entry created doesn't need further modification
614       
615    def addGroup(self, group) :       
616        """Adds a group to the quota storage, returns the old value if it already exists."""
617        oldentry = self.getGroup(group.Name)
618        if oldentry.Exists :
619            return oldentry
620        self.doModify("INSERT INTO groups (groupname, limitby, description) VALUES (%s, %s, %s)" % \
621                              (self.doQuote(self.userCharsetToDatabase(group.Name)), \
622                               self.doQuote(group.LimitBy or "quota"), \
623                               self.doQuote(self.userCharsetToDatabase(group.Description))))
624        group.isDirty = False
625        return None # the entry created doesn't need further modification
626
627    def addUserToGroup(self, user, group) :   
628        """Adds an user to a group."""
629        result = self.doSearch("SELECT COUNT(*) AS mexists FROM groupsmembers WHERE groupid=%s AND userid=%s" % (self.doQuote(group.ident), self.doQuote(user.ident)))
630        try :
631            mexists = int(result[0].get("mexists"))
632        except (IndexError, TypeError) :   
633            mexists = 0
634        if not mexists :   
635            self.doModify("INSERT INTO groupsmembers (groupid, userid) VALUES (%s, %s)" % (self.doQuote(group.ident), self.doQuote(user.ident)))
636           
637    def delUserFromGroup(self, user, group) :   
638        """Removes an user from a group."""
639        self.doModify("DELETE FROM groupsmembers WHERE groupid=%s AND userid=%s" % \
640                       (self.doQuote(group.ident), self.doQuote(user.ident)))
641           
642    def addUserPQuota(self, upq) :
643        """Initializes a user print quota on a printer."""
644        oldentry = self.getUserPQuota(upq.User, upq.Printer)
645        if oldentry.Exists :
646            return oldentry
647        self.doModify("INSERT INTO userpquota (userid, printerid, softlimit, hardlimit, warncount, datelimit, pagecounter, lifepagecounter, maxjobsize) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)" \
648                          % (self.doQuote(upq.User.ident), \
649                             self.doQuote(upq.Printer.ident), \
650                             self.doQuote(upq.SoftLimit), \
651                             self.doQuote(upq.HardLimit), \
652                             self.doQuote(upq.WarnCount or 0), \
653                             self.doQuote(upq.DateLimit), \
654                             self.doQuote(upq.PageCounter or 0), \
655                             self.doQuote(upq.LifePageCounter or 0), \
656                             self.doQuote(upq.MaxJobSize)))
657        upq.isDirty = False
658        return None # the entry created doesn't need further modification
659       
660    def addGroupPQuota(self, gpq) :
661        """Initializes a group print quota on a printer."""
662        oldentry = self.getGroupPQuota(gpq.Group, gpq.Printer)
663        if oldentry.Exists :
664            return oldentry
665        self.doModify("INSERT INTO grouppquota (groupid, printerid, softlimit, hardlimit, datelimit, maxjobsize) VALUES (%s, %s, %s, %s, %s, %s)" \
666                          % (self.doQuote(gpq.Group.ident), \
667                             self.doQuote(gpq.Printer.ident), \
668                             self.doQuote(gpq.SoftLimit), \
669                             self.doQuote(gpq.HardLimit), \
670                             self.doQuote(gpq.DateLimit), \
671                             self.doQuote(gpq.MaxJobSize)))
672        gpq.isDirty = False
673        return None # the entry created doesn't need further modification
674       
675    def savePrinter(self, printer) :   
676        """Saves the printer to the database in a single operation."""
677        self.doModify("UPDATE printers SET passthrough=%s, maxjobsize=%s, description=%s, priceperpage=%s, priceperjob=%s WHERE id=%s" \
678                              % (self.doQuote((printer.PassThrough and "t") or "f"), \
679                                 self.doQuote(printer.MaxJobSize or 0), \
680                                 self.doQuote(self.userCharsetToDatabase(printer.Description)), \
681                                 self.doQuote(printer.PricePerPage or 0.0), \
682                                 self.doQuote(printer.PricePerJob or 0.0), \
683                                 self.doQuote(printer.ident)))
684                                 
685    def saveUser(self, user) :       
686        """Saves the user to the database in a single operation."""
687        self.doModify("UPDATE users SET limitby=%s, balance=%s, lifetimepaid=%s, email=%s, overcharge=%s, description=%s WHERE id=%s" \
688                               % (self.doQuote(user.LimitBy or 'quota'), \
689                                  self.doQuote(user.AccountBalance or 0.0), \
690                                  self.doQuote(user.LifeTimePaid or 0.0), \
691                                  self.doQuote(user.Email), \
692                                  self.doQuote(user.OverCharge), \
693                                  self.doQuote(self.userCharsetToDatabase(user.Description)), \
694                                  self.doQuote(user.ident)))
695                                 
696    def saveGroup(self, group) :       
697        """Saves the group to the database in a single operation."""
698        self.doModify("UPDATE groups SET limitby=%s, description=%s WHERE id=%s" \
699                               % (self.doQuote(group.LimitBy or 'quota'), \
700                                  self.doQuote(self.userCharsetToDatabase(group.Description)), \
701                                  self.doQuote(group.ident)))
702       
703    def writeUserPQuotaDateLimit(self, userpquota, datelimit) :   
704        """Sets the date limit permanently for a user print quota."""
705        self.doModify("UPDATE userpquota SET datelimit=%s WHERE id=%s" % (self.doQuote(datelimit), self.doQuote(userpquota.ident)))
706           
707    def writeGroupPQuotaDateLimit(self, grouppquota, datelimit) :   
708        """Sets the date limit permanently for a group print quota."""
709        self.doModify("UPDATE grouppquota SET datelimit=%s WHERE id=%s" % (self.doQuote(datelimit), self.doQuote(grouppquota.ident)))
710       
711    def increaseUserPQuotaPagesCounters(self, userpquota, nbpages) :   
712        """Increase page counters for a user print quota."""
713        self.doModify("UPDATE userpquota SET pagecounter=pagecounter + %s,lifepagecounter=lifepagecounter + %s WHERE id=%s" % (self.doQuote(nbpages), self.doQuote(nbpages), self.doQuote(userpquota.ident)))
714       
715    def saveBillingCode(self, bcode) :   
716        """Saves the billing code to the database."""
717        self.doModify("UPDATE billingcodes SET balance=%s, pagecounter=%s, description=%s WHERE id=%s" \
718                            % (self.doQuote(bcode.Balance or 0.0), \
719                               self.doQuote(bcode.PageCounter or 0), \
720                               self.doQuote(self.userCharsetToDatabase(bcode.Description)), \
721                               self.doQuote(bcode.ident)))
722       
723    def consumeBillingCode(self, bcode, pagecounter, balance) :
724        """Consumes from a billing code."""
725        self.doModify("UPDATE billingcodes SET balance=balance + %s, pagecounter=pagecounter + %s WHERE id=%s" % (self.doQuote(balance), self.doQuote(pagecounter), self.doQuote(bcode.ident)))
726       
727    def refundJob(self, jobident) :   
728        """Marks a job as refunded in the history."""
729        self.doModify("UPDATE jobhistory SET action='REFUND' WHERE id=%s;" % self.doQuote(jobident))
730       
731    def decreaseUserAccountBalance(self, user, amount) :   
732        """Decreases user's account balance from an amount."""
733        self.doModify("UPDATE users SET balance=balance - %s WHERE id=%s" % (self.doQuote(amount), self.doQuote(user.ident)))
734       
735    def writeNewPayment(self, user, amount, comment="") :
736        """Adds a new payment to the payments history."""
737        if user.ident is not None :
738            self.doModify("INSERT INTO payments (userid, amount, description) VALUES (%s, %s, %s)" % (self.doQuote(user.ident), self.doQuote(amount), self.doQuote(self.userCharsetToDatabase(comment))))
739        else :   
740            self.doModify("INSERT INTO payments (userid, amount, description) VALUES ((SELECT id FROM users WHERE username=%s), %s, %s)" % (self.doQuote(self.userCharsetToDatabase(user.Name)), self.doQuote(amount), self.doQuote(self.userCharsetToDatabase(comment))))
741       
742    def writeLastJobSize(self, lastjob, jobsize, jobprice) :       
743        """Sets the last job's size permanently."""
744        self.doModify("UPDATE jobhistory SET jobsize=%s, jobprice=%s WHERE id=%s" % (self.doQuote(jobsize), self.doQuote(jobprice), self.doQuote(lastjob.ident)))
745       
746    def writeJobNew(self, printer, user, jobid, pagecounter, action, jobsize=None, jobprice=None, filename=None, title=None, copies=None, options=None, clienthost=None, jobsizebytes=None, jobmd5sum=None, jobpages=None, jobbilling=None, precomputedsize=None, precomputedprice=None) :
747        """Adds a job in a printer's history."""
748        if self.privacy :   
749            # For legal reasons, we want to hide the title, filename and options
750            title = filename = options = "hidden"
751        filename = self.userCharsetToDatabase(filename)
752        title = self.userCharsetToDatabase(title)
753        options = self.userCharsetToDatabase(options)
754        jobbilling = self.userCharsetToDatabase(jobbilling)
755        if (not self.disablehistory) or (not printer.LastJob.Exists) :
756            if jobsize is not None :
757                self.doModify("INSERT INTO jobhistory (userid, printerid, jobid, pagecounter, action, jobsize, jobprice, filename, title, copies, options, hostname, jobsizebytes, md5sum, pages, billingcode, precomputedjobsize, precomputedjobprice) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)" % (self.doQuote(user.ident), self.doQuote(printer.ident), self.doQuote(jobid), self.doQuote(pagecounter), self.doQuote(action), self.doQuote(jobsize), self.doQuote(jobprice), self.doQuote(filename), self.doQuote(title), self.doQuote(copies), self.doQuote(options), self.doQuote(clienthost), self.doQuote(jobsizebytes), self.doQuote(jobmd5sum), self.doQuote(jobpages), self.doQuote(jobbilling), self.doQuote(precomputedsize), self.doQuote(precomputedprice)))
758            else :   
759                self.doModify("INSERT INTO jobhistory (userid, printerid, jobid, pagecounter, action, filename, title, copies, options, hostname, jobsizebytes, md5sum, pages, billingcode, precomputedjobsize, precomputedjobprice) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)" % (self.doQuote(user.ident), self.doQuote(printer.ident), self.doQuote(jobid), self.doQuote(pagecounter), self.doQuote(action), self.doQuote(filename), self.doQuote(title), self.doQuote(copies), self.doQuote(options), self.doQuote(clienthost), self.doQuote(jobsizebytes), self.doQuote(jobmd5sum), self.doQuote(jobpages), self.doQuote(jobbilling), self.doQuote(precomputedsize), self.doQuote(precomputedprice)))
760        else :       
761            # here we explicitly want to reset jobsize to NULL if needed
762            self.doModify("UPDATE jobhistory SET userid=%s, jobid=%s, pagecounter=%s, action=%s, jobsize=%s, jobprice=%s, filename=%s, title=%s, copies=%s, options=%s, hostname=%s, jobsizebytes=%s, md5sum=%s, pages=%s, billingcode=%s, precomputedjobsize=%s, precomputedjobprice=%s, jobdate=now() WHERE id=%s" % (self.doQuote(user.ident), self.doQuote(jobid), self.doQuote(pagecounter), self.doQuote(action), self.doQuote(jobsize), self.doQuote(jobprice), self.doQuote(filename), self.doQuote(title), self.doQuote(copies), self.doQuote(options), self.doQuote(clienthost), self.doQuote(jobsizebytes), self.doQuote(jobmd5sum), self.doQuote(jobpages), self.doQuote(jobbilling), self.doQuote(precomputedsize), self.doQuote(precomputedprice), self.doQuote(printer.LastJob.ident)))
763           
764    def saveUserPQuota(self, userpquota) :
765        """Saves an user print quota entry."""
766        self.doModify("UPDATE userpquota SET softlimit=%s, hardlimit=%s, warncount=%s, datelimit=%s, pagecounter=%s, lifepagecounter=%s, maxjobsize=%s WHERE id=%s" \
767                              % (self.doQuote(userpquota.SoftLimit), \
768                                 self.doQuote(userpquota.HardLimit), \
769                                 self.doQuote(userpquota.WarnCount or 0), \
770                                 self.doQuote(userpquota.DateLimit), \
771                                 self.doQuote(userpquota.PageCounter or 0), \
772                                 self.doQuote(userpquota.LifePageCounter or 0), \
773                                 self.doQuote(userpquota.MaxJobSize), \
774                                 self.doQuote(userpquota.ident)))
775       
776    def writeUserPQuotaWarnCount(self, userpquota, warncount) :
777        """Sets the warn counter value for a user quota."""
778        self.doModify("UPDATE userpquota SET warncount=%s WHERE id=%s" % (self.doQuote(warncount), self.doQuote(userpquota.ident)))
779       
780    def increaseUserPQuotaWarnCount(self, userpquota) :
781        """Increases the warn counter value for a user quota."""
782        self.doModify("UPDATE userpquota SET warncount=warncount+1 WHERE id=%s" % self.doQuote(userpquota.ident))
783       
784    def saveGroupPQuota(self, grouppquota) :
785        """Saves a group print quota entry."""
786        self.doModify("UPDATE grouppquota SET softlimit=%s, hardlimit=%s, datelimit=%s WHERE id=%s" \
787                              % (self.doQuote(grouppquota.SoftLimit), \
788                                 self.doQuote(grouppquota.HardLimit), \
789                                 self.doQuote(grouppquota.DateLimit), \
790                                 self.doQuote(grouppquota.ident)))
791
792    def writePrinterToGroup(self, pgroup, printer) :
793        """Puts a printer into a printer group."""
794        children = []
795        result = self.doSearch("SELECT printerid FROM printergroupsmembers WHERE groupid=%s" % self.doQuote(pgroup.ident))
796        if result :
797            for record in result :
798                children.append(record.get("printerid")) # TODO : put this into the database integrity rules
799        if printer.ident not in children :       
800            self.doModify("INSERT INTO printergroupsmembers (groupid, printerid) VALUES (%s, %s)" % (self.doQuote(pgroup.ident), self.doQuote(printer.ident)))
801       
802    def removePrinterFromGroup(self, pgroup, printer) :
803        """Removes a printer from a printer group."""
804        self.doModify("DELETE FROM printergroupsmembers WHERE groupid=%s AND printerid=%s" % (self.doQuote(pgroup.ident), self.doQuote(printer.ident)))
805       
806    def retrieveHistory(self, user=None, printer=None, hostname=None, billingcode=None, jobid=None, limit=100, start=None, end=None) :
807        """Retrieves all print jobs for user on printer (or all) between start and end date, limited to first 100 results."""
808        query = "SELECT jobhistory.*,username,printername FROM jobhistory,users,printers WHERE users.id=userid AND printers.id=printerid"
809        where = []
810        if user is not None : # user.ident is None anyway if user doesn't exist
811            where.append("userid=%s" % self.doQuote(user.ident))
812        if printer is not None : # printer.ident is None anyway if printer doesn't exist
813            where.append("printerid=%s" % self.doQuote(printer.ident))
814        if hostname is not None :   
815            where.append("hostname=%s" % self.doQuote(hostname))
816        if billingcode is not None :   
817            where.append("billingcode=%s" % self.doQuote(self.userCharsetToDatabase(billingcode)))
818        if jobid is not None :   
819            where.append("jobid=%s" % self.doQuote(jobid)) # TODO : jobid is text, so self.userCharsetToDatabase(jobid) but do all of them as well.
820        if start is not None :   
821            where.append("jobdate>=%s" % self.doQuote(start))
822        if end is not None :   
823            where.append("jobdate<=%s" % self.doQuote(end))
824        if where :   
825            query += " AND %s" % " AND ".join(where)
826        query += " ORDER BY jobhistory.id DESC"
827        if limit :
828            query += " LIMIT %s" % self.doQuote(int(limit))
829        jobs = []   
830        result = self.doSearch(query)   
831        if result :
832            for fields in result :
833                job = self.storageJobFromRecord(fields)
834                jobs.append(job)
835        return jobs
836       
837    def deleteUser(self, user) :   
838        """Completely deletes an user from the database."""
839        # TODO : What should we do if we delete the last person who used a given printer ?
840        # TODO : we can't reassign the last job to the previous one, because next user would be
841        # TODO : incorrectly charged (overcharged).
842        for q in [ 
843                    "DELETE FROM payments WHERE userid=%s" % self.doQuote(user.ident),
844                    "DELETE FROM groupsmembers WHERE userid=%s" % self.doQuote(user.ident),
845                    "DELETE FROM jobhistory WHERE userid=%s" % self.doQuote(user.ident),
846                    "DELETE FROM userpquota WHERE userid=%s" % self.doQuote(user.ident),
847                    "DELETE FROM users WHERE id=%s" % self.doQuote(user.ident),
848                  ] :
849            self.doModify(q)
850           
851    def multipleQueriesInTransaction(self, queries) :       
852        """Does many modifications in a single transaction."""
853        self.beginTransaction()
854        try :
855            for q in queries :
856                self.doModify(q)
857        except :   
858            self.rollbackTransaction()
859            raise
860        else :   
861            self.commitTransaction()
862           
863    def deleteManyBillingCodes(self, billingcodes) :       
864        """Deletes many billing codes."""
865        codeids = ", ".join(["%s" % self.doQuote(b.ident) for b in billingcodes])
866        if codeids :
867            self.multipleQueriesInTransaction([ 
868                    "DELETE FROM billingcodes WHERE id IN (%s)" % codeids,])
869           
870    def deleteManyUsers(self, users) :       
871        """Deletes many users."""
872        userids = ", ".join(["%s" % self.doQuote(u.ident) for u in users])
873        if userids :
874            self.multipleQueriesInTransaction([ 
875                    "DELETE FROM payments WHERE userid IN (%s)" % userids,
876                    "DELETE FROM groupsmembers WHERE userid IN (%s)" % userids,
877                    "DELETE FROM jobhistory WHERE userid IN (%s)" % userids,
878                    "DELETE FROM userpquota WHERE userid IN (%s)" % userids,
879                    "DELETE FROM users WHERE id IN (%s)" % userids,])
880                   
881    def deleteManyGroups(self, groups) :       
882        """Deletes many groups."""
883        groupids = ", ".join(["%s" % self.doQuote(g.ident) for g in groups])
884        if groupids :
885            self.multipleQueriesInTransaction([ 
886                    "DELETE FROM groupsmembers WHERE groupid IN (%s)" % groupids,
887                    "DELETE FROM grouppquota WHERE groupid IN (%s)" % groupids,
888                    "DELETE FROM groups WHERE id IN (%s)" % groupids,])
889       
890    def deleteManyPrinters(self, printers) :
891        """Deletes many printers."""
892        printerids = ", ".join(["%s" % self.doQuote(p.ident) for p in printers])
893        if printerids :
894            self.multipleQueriesInTransaction([ 
895                    "DELETE FROM printergroupsmembers WHERE groupid IN (%s) OR printerid IN (%s)" % (printerids, printerids),
896                    "DELETE FROM jobhistory WHERE printerid IN (%s)" % printerids,
897                    "DELETE FROM grouppquota WHERE printerid IN (%s)" % printerids,
898                    "DELETE FROM userpquota WHERE printerid IN (%s)" % printerids,
899                    "DELETE FROM printers WHERE id IN (%s)" % printerids,])
900       
901    def deleteManyUserPQuotas(self, printers, users) :       
902        """Deletes many user print quota entries."""
903        printerids = ", ".join(["%s" % self.doQuote(p.ident) for p in printers])
904        userids = ", ".join(["%s" % self.doQuote(u.ident) for u in users])
905        if userids and printerids :
906            self.multipleQueriesInTransaction([ 
907                    "DELETE FROM jobhistory WHERE userid IN (%s) AND printerid IN (%s)" \
908                                 % (userids, printerids),
909                    "DELETE FROM userpquota WHERE userid IN (%s) AND printerid IN (%s)" \
910                                 % (userids, printerids),])
911           
912    def deleteManyGroupPQuotas(self, printers, groups) :
913        """Deletes many group print quota entries."""
914        printerids = ", ".join(["%s" % self.doQuote(p.ident) for p in printers])
915        groupids = ", ".join(["%s" % self.doQuote(g.ident) for g in groups])
916        if groupids and printerids :
917            self.multipleQueriesInTransaction([ 
918                    "DELETE FROM grouppquota WHERE groupid IN (%s) AND printerid IN (%s)" \
919                                 % (groupids, printerids),])
920       
921    def deleteUserPQuota(self, upquota) :   
922        """Completely deletes an user print quota entry from the database."""
923        for q in [ 
924                    "DELETE FROM jobhistory WHERE userid=%s AND printerid=%s" \
925                                 % (self.doQuote(upquota.User.ident), self.doQuote(upquota.Printer.ident)),
926                    "DELETE FROM userpquota WHERE id=%s" % self.doQuote(upquota.ident),
927                  ] :
928            self.doModify(q)
929       
930    def deleteGroupPQuota(self, gpquota) :   
931        """Completely deletes a group print quota entry from the database."""
932        for q in [ 
933                    "DELETE FROM grouppquota WHERE id=%s" % self.doQuote(gpquota.ident),
934                  ] :
935            self.doModify(q)
936       
937    def deleteGroup(self, group) :   
938        """Completely deletes a group from the database."""
939        for q in [
940                   "DELETE FROM groupsmembers WHERE groupid=%s" % self.doQuote(group.ident),
941                   "DELETE FROM grouppquota WHERE groupid=%s" % self.doQuote(group.ident),
942                   "DELETE FROM groups WHERE id=%s" % self.doQuote(group.ident),
943                 ] : 
944            self.doModify(q)
945           
946    def deletePrinter(self, printer) :   
947        """Completely deletes a printer from the database."""
948        for q in [ 
949                    "DELETE FROM printergroupsmembers WHERE groupid=%s OR printerid=%s" % (self.doQuote(printer.ident), self.doQuote(printer.ident)),
950                    "DELETE FROM jobhistory WHERE printerid=%s" % self.doQuote(printer.ident),
951                    "DELETE FROM grouppquota WHERE printerid=%s" % self.doQuote(printer.ident),
952                    "DELETE FROM userpquota WHERE printerid=%s" % self.doQuote(printer.ident),
953                    "DELETE FROM printers WHERE id=%s" % self.doQuote(printer.ident),
954                  ] :
955            self.doModify(q)
956           
957    def deleteBillingCode(self, code) :   
958        """Completely deletes a billing code from the database."""
959        for q in [
960                   "DELETE FROM billingcodes WHERE id=%s" % self.doQuote(code.ident),
961                 ] : 
962            self.doModify(q)
963       
Note: See TracBrowser for help on using the browser.