root / pykota / trunk / pykota / storages / sql.py @ 3292

Revision 3292, 55.1 kB (checked in by jerome, 16 years ago)

Removed the LIMIT SQL keyword to accomodate DB2 which apparently lacks it.
TODO : Only one instance of LIMIT remains in retrieveHistory().

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