Show
Ignore:
Timestamp:
03/01/06 00:07:05 (18 years ago)
Author:
jerome
Message:

Optimized pkbcodes like edpykota.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • pykota/trunk/pykota/storages/sql.py

    r2763 r2765  
    532532        return self.getPrinter(printername) 
    533533         
    534     def addBillingCode(self, label) :         
    535         """Adds a billing code to the quota storage, returns it.""" 
    536         self.doModify("INSERT INTO billingcodes (billingcode) VALUES (%s)" % self.doQuote(self.userCharsetToDatabase(label))) 
    537         return self.getBillingCode(label) 
     534    def addBillingCode(self, bcode) : 
     535        """Adds a billing code to the quota storage, returns the old value if it already exists.""" 
     536        try : 
     537            self.doModify("INSERT INTO billingcodes (billingcode, balance, pagecounter, description) VALUES (%s, %s, %s, %s)" \ 
     538                               % (self.doQuote(self.userCharsetToDatabase(bcode.BillingCode)),  
     539                                  self.doQuote(bcode.Balance or 0.0), \ 
     540                                  self.doQuote(bcode.PageCounter or 0), \ 
     541                                  self.doQuote(self.userCharsetToDatabase(bcode.Description)))) 
     542        except PyKotaStorageError :     
     543            # TODO : check if this is an error different from a duplicate insert 
     544            # return the existing entry which has to be modified 
     545            return self.getBillingCode(bcode.BillingCode) 
     546        else :     
     547            bcode.isDirty = False 
     548            return None # the entry created doesn't need further modification 
    538549         
    539550    def addUser(self, user) :         
     
    651662        self.doModify("UPDATE userpquota SET pagecounter=pagecounter + %s,lifepagecounter=lifepagecounter + %s WHERE id=%s" % (self.doQuote(nbpages), self.doQuote(nbpages), self.doQuote(userpquota.ident))) 
    652663        
    653     def saveBillingCode(self, code) :     
     664    def saveBillingCode(self, bcode) :     
    654665        """Saves the billing code to the database.""" 
    655666        self.doModify("UPDATE billingcodes SET balance=%s, pagecounter=%s, description=%s WHERE id=%s" \ 
    656                             % (self.doQuote(code.Balance or 0.0), \ 
    657                                self.doQuote(code.PageCounter or 0), \ 
    658                                self.doQuote(self.userCharsetToDatabase(code.Description)), \ 
    659                                self.doQuote(code.ident))) 
     667                            % (self.doQuote(bcode.Balance or 0.0), \ 
     668                               self.doQuote(bcode.PageCounter or 0), \ 
     669                               self.doQuote(self.userCharsetToDatabase(bcode.Description)), \ 
     670                               self.doQuote(bcode.ident))) 
    660671        
    661     def consumeBillingCode(self, code, pagecounter, balance) : 
     672    def consumeBillingCode(self, bcode, pagecounter, balance) : 
    662673        """Consumes from a billing code.""" 
    663         self.doModify("UPDATE billingcodes SET balance=balance + %s, pagecounter=pagecounter + %s WHERE id=%s" % (self.doQuote(balance), self.doQuote(pagecounter), self.doQuote(code.ident))) 
     674        self.doModify("UPDATE billingcodes SET balance=balance + %s, pagecounter=pagecounter + %s WHERE id=%s" % (self.doQuote(balance), self.doQuote(pagecounter), self.doQuote(bcode.ident))) 
    664675        
    665676    def decreaseUserAccountBalance(self, user, amount) :     
     
    813824            self.commitTransaction() 
    814825             
    815     def deleteManyBillingCodesFromNames(self, billingcodes) :         
    816         """Deletes many billing codes from their names.""" 
    817         codelabels = ", ".join(["%s" % self.doQuote(b) for b in billingcodes]) 
     826    def deleteManyBillingCodes(self, billingcodes) :         
     827        """Deletes many billing codes.""" 
     828        codeids = ", ".join(["%s" % self.doQuote(b.ident) for b in billingcodes]) 
    818829        self.deleteInTransaction([  
    819                     "DELETE FROM billingcodes WHERE billingcode IN (%s)" % codelabels,]) 
    820              
    821     def deleteManyUsersFromNames(self, usernames) :         
    822         """Deletes many users from their names.""" 
    823         usernames = ", ".join(["%s" % self.doQuote(u) for u in usernames]) 
     830                    "DELETE FROM billingcodes WHERE id IN (%s)" % codeids,]) 
     831             
     832    def deleteManyUsers(self, users) :         
     833        """Deletes many users.""" 
     834        userids = ", ".join(["%s" % self.doQuote(u.ident) for u in users]) 
    824835        self.deleteInTransaction([  
    825                     "DELETE FROM payments WHERE userid IN (SELECT id FROM users WHERE username IN %s)" % usernames, 
    826                     "DELETE FROM groupsmembers WHERE userid IN (SELECT id FROM users WHERE username IN %s)" % usernames, 
    827                     "DELETE FROM jobhistory WHERE userid IN (SELECT id FROM users WHERE username IN %s)" % usernames, 
    828                     "DELETE FROM userpquota WHERE userid IN (SELECT id FROM users WHERE username IN %s)" % usernames, 
    829                     "DELETE FROM users WHERE username IN %s" % usernames,]) 
    830          
    831     def deleteManyPrintersFromNames(self, printernames) :         
    832         """Deletes many printers from their names.""" 
    833         printernames = ", ".join(["%s" % self.doQuote(p) for p in printernames]) 
     836                    "DELETE FROM payments WHERE userid IN (%s)" % userids, 
     837                    "DELETE FROM groupsmembers WHERE userid IN (%s)" % userids, 
     838                    "DELETE FROM jobhistory WHERE userid IN (%s)" % userids, 
     839                    "DELETE FROM userpquota WHERE userid IN (%s)" % userids, 
     840                    "DELETE FROM users WHERE id IN (%s)" % userids,]) 
     841                     
     842    def deleteManyGroups(self, groups) :         
     843        """Deletes many groups.""" 
     844        groupids = ", ".join(["%s" % self.doQuote(g.ident) for g in groups]) 
    834845        self.deleteInTransaction([  
    835                     "DELETE FROM printergroupsmembers WHERE groupid=%s OR printerid=%s" % (self.doQuote(printer.ident), self.doQuote(printer.ident)), 
    836                     "DELETE FROM jobhistory WHERE printerid=%s" % self.doQuote(printer.ident), 
    837                     "DELETE FROM grouppquota WHERE printerid=%s" % self.doQuote(printer.ident), 
    838                     "DELETE FROM userpquota WHERE printerid=%s" % self.doQuote(printer.ident), 
    839                     "DELETE FROM printers WHERE id=%s" % self.doQuote(printer.ident),]) 
     846                    "DELETE FROM groupsmembers WHERE groupid IN (%s)" % groupids, 
     847                    "DELETE FROM grouppquota WHERE groupid IN (%s)" % groupids, 
     848                    "DELETE FROM groups WHERE id IN (%s)" % groupids,]) 
     849         
     850    def deleteManyPrinters(self, printers) : 
     851        """Deletes many printers.""" 
     852        printerids = ", ".join(["%s" % self.doQuote(p.ident) for p in printers]) 
     853        self.deleteInTransaction([  
     854                    "DELETE FROM printergroupsmembers WHERE groupid IN (%s) OR printerid IN (%s)" % printerids, 
     855                    "DELETE FROM jobhistory WHERE printerid IN (%s)" % printerids, 
     856                    "DELETE FROM grouppquota WHERE printerid IN (%s)" % printerids, 
     857                    "DELETE FROM userpquota WHERE printerid IN (%s)" % printerids, 
     858                    "DELETE FROM printers WHERE id IN (%s)" % printerids,]) 
    840859         
    841860    def deleteManyUserPQuotas(self, printers, users) :