Changeset 2787

Show
Ignore:
Timestamp:
03/06/06 08:48:54 (18 years ago)
Author:
jerome
Message:

I had to slowdown data creation because PostgreSQL 7.4 (the one I use)
doesn't seem to support subtransactions : we have to check if the entry
already exists before creating it, otherwise our transaction aborts.
Interestingly, an error on INSERT doesn't abort SQLite3 transactions.
Severity : HIGH

Files:
1 modified

Legend:

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

    r2775 r2787  
    536536    def addPrinter(self, printer) :         
    537537        """Adds a printer to the quota storage, returns the old value if it already exists.""" 
    538         try : 
    539             self.doModify("INSERT INTO printers (printername, passthrough, maxjobsize, description, priceperpage, priceperjob) VALUES (%s, %s, %s, %s, %s, %s)" \ 
    540                               % (self.doQuote(self.userCharsetToDatabase(printer.Name)), \ 
    541                                  self.doQuote((printer.PassThrough and "t") or "f"), \ 
    542                                  self.doQuote(printer.MaxJobSize or 0), \ 
    543                                  self.doQuote(self.userCharsetToDatabase(printer.Description)), \ 
    544                                  self.doQuote(printer.PricePerPage or 0.0), \ 
    545                                  self.doQuote(printer.PricePerJob or 0.0))) 
    546         except PyKotaStorageError :     
    547             # TODO : check if this is an error different from a duplicate insert 
    548             # return the existing entry which has to be modified 
    549             return self.getPrinter(printer.Name) 
    550         else :     
    551             printer.isDirty = False 
    552             return None # the entry created doesn't need further modification 
     538        oldentry = self.getPrinter(printer.Name) 
     539        if oldentry.Exists : 
     540            return oldentry 
     541        self.doModify("INSERT INTO printers (printername, passthrough, maxjobsize, description, priceperpage, priceperjob) VALUES (%s, %s, %s, %s, %s, %s)" \ 
     542                          % (self.doQuote(self.userCharsetToDatabase(printer.Name)), \ 
     543                             self.doQuote((printer.PassThrough and "t") or "f"), \ 
     544                             self.doQuote(printer.MaxJobSize or 0), \ 
     545                             self.doQuote(self.userCharsetToDatabase(printer.Description)), \ 
     546                             self.doQuote(printer.PricePerPage or 0.0), \ 
     547                             self.doQuote(printer.PricePerJob or 0.0))) 
     548        printer.isDirty = False 
     549        return None # the entry created doesn't need further modification 
    553550         
    554551    def addBillingCode(self, bcode) : 
    555552        """Adds a billing code to the quota storage, returns the old value if it already exists.""" 
    556         try : 
    557             self.doModify("INSERT INTO billingcodes (billingcode, balance, pagecounter, description) VALUES (%s, %s, %s, %s)" \ 
    558                                % (self.doQuote(self.userCharsetToDatabase(bcode.BillingCode)),  
    559                                   self.doQuote(bcode.Balance or 0.0), \ 
    560                                   self.doQuote(bcode.PageCounter or 0), \ 
    561                                   self.doQuote(self.userCharsetToDatabase(bcode.Description)))) 
    562         except PyKotaStorageError :     
    563             # TODO : check if this is an error different from a duplicate insert 
    564             # return the existing entry which has to be modified 
    565             return self.getBillingCode(bcode.BillingCode) 
    566         else :     
    567             bcode.isDirty = False 
    568             return None # the entry created doesn't need further modification 
     553        oldentry = self.getBillingCode(bcode.BillingCode) 
     554        if oldentry.Exists : 
     555            return oldentry 
     556        self.doModify("INSERT INTO billingcodes (billingcode, balance, pagecounter, description) VALUES (%s, %s, %s, %s)" \ 
     557                           % (self.doQuote(self.userCharsetToDatabase(bcode.BillingCode)),  
     558                              self.doQuote(bcode.Balance or 0.0), \ 
     559                              self.doQuote(bcode.PageCounter or 0), \ 
     560                              self.doQuote(self.userCharsetToDatabase(bcode.Description)))) 
     561        bcode.isDirty = False 
     562        return None # the entry created doesn't need further modification 
    569563         
    570564    def addUser(self, user) :         
    571565        """Adds a user to the quota storage, returns the old value if it already exists.""" 
    572         try : 
    573             self.doModify("INSERT INTO users (username, limitby, balance, lifetimepaid, email, overcharge, description) VALUES (%s, %s, %s, %s, %s, %s, %s)" % \ 
    574                                          (self.doQuote(self.userCharsetToDatabase(user.Name)), \ 
    575                                           self.doQuote(user.LimitBy or 'quota'), \ 
    576                                           self.doQuote(user.AccountBalance or 0.0), \ 
    577                                           self.doQuote(user.LifeTimePaid or 0.0), \ 
    578                                           self.doQuote(user.Email), \ 
    579                                           self.doQuote(user.OverCharge), \ 
    580                                           self.doQuote(self.userCharsetToDatabase(user.Description)))) 
    581         except PyKotaStorageError :     
    582             # TODO : check if this is an error different from a duplicate insert 
    583             # return the existing entry which has to be modified 
    584             return self.getUser(user.Name) 
    585         else :     
    586             if user.PaymentsBacklog : 
    587                 for (value, comment) in user.PaymentsBacklog : 
    588                     self.writeNewPayment(user, value, comment) 
    589                 user.PaymentsBacklog = [] 
    590             user.isDirty = False 
    591             return None # the entry created doesn't need further modification 
     566        oldentry = self.getUser(user.Name) 
     567        if oldentry.Exists : 
     568            return oldentry 
     569        self.doModify("INSERT INTO users (username, limitby, balance, lifetimepaid, email, overcharge, description) VALUES (%s, %s, %s, %s, %s, %s, %s)" % \ 
     570                                     (self.doQuote(self.userCharsetToDatabase(user.Name)), \ 
     571                                      self.doQuote(user.LimitBy or 'quota'), \ 
     572                                      self.doQuote(user.AccountBalance or 0.0), \ 
     573                                      self.doQuote(user.LifeTimePaid or 0.0), \ 
     574                                      self.doQuote(user.Email), \ 
     575                                      self.doQuote(user.OverCharge), \ 
     576                                      self.doQuote(self.userCharsetToDatabase(user.Description)))) 
     577        if user.PaymentsBacklog : 
     578            for (value, comment) in user.PaymentsBacklog : 
     579                self.writeNewPayment(user, value, comment) 
     580            user.PaymentsBacklog = [] 
     581        user.isDirty = False 
     582        return None # the entry created doesn't need further modification 
    592583         
    593584    def addGroup(self, group) :         
    594585        """Adds a group to the quota storage, returns the old value if it already exists.""" 
    595         try : 
    596             self.doModify("INSERT INTO groups (groupname, limitby, description) VALUES (%s, %s, %s)" % \ 
    597                                   (self.doQuote(self.userCharsetToDatabase(group.Name)), \ 
    598                                    self.doQuote(group.LimitBy or "quota"), \ 
    599                                    self.doQuote(self.userCharsetToDatabase(group.Description)))) 
    600         except PyKotaStorageError :     
    601             # TODO : check if this is an error different from a duplicate insert 
    602             # return the existing entry which has to be modified 
    603             return self.getGroup(group.Name) 
    604         else :     
    605             group.isDirty = False 
    606             return None # the entry created doesn't need further modification 
     586        oldentry = self.getGroup(group.Name) 
     587        if oldentry.Exists : 
     588            return oldentry 
     589        self.doModify("INSERT INTO groups (groupname, limitby, description) VALUES (%s, %s, %s)" % \ 
     590                              (self.doQuote(self.userCharsetToDatabase(group.Name)), \ 
     591                               self.doQuote(group.LimitBy or "quota"), \ 
     592                               self.doQuote(self.userCharsetToDatabase(group.Description)))) 
     593        group.isDirty = False 
     594        return None # the entry created doesn't need further modification 
    607595 
    608596    def addUserToGroup(self, user, group) :     
     
    623611    def addUserPQuota(self, upq) : 
    624612        """Initializes a user print quota on a printer.""" 
    625         try : 
    626             self.doModify("INSERT INTO userpquota (userid, printerid, softlimit, hardlimit, warncount, datelimit, pagecounter, lifepagecounter, maxjobsize) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)" \ 
    627                               % (self.doQuote(upq.User.ident), \ 
    628                                  self.doQuote(upq.Printer.ident), \ 
    629                                  self.doQuote(upq.SoftLimit), \ 
    630                                  self.doQuote(upq.HardLimit), \ 
    631                                  self.doQuote(upq.WarnCount), \ 
    632                                  self.doQuote(upq.DateLimit), \ 
    633                                  self.doQuote(upq.PageCounter or 0), \ 
    634                                  self.doQuote(upq.LifePageCounter or 0), \ 
    635                                  self.doQuote(upq.MaxJobSize))) 
    636         except PyKotaStorageError :                          
    637             # TODO : check if this is an error different from a duplicate insert 
    638             # return the existing entry which has to be modified 
    639             return self.getUserPQuota(upq.User, upq.Printer) 
    640         else :     
    641             upq.isDirty = False 
    642             return None # the entry created doesn't need further modification 
     613        oldentry = self.getUserPQuota(upq.User, upq.Printer) 
     614        if oldentry.Exists : 
     615            return oldentry 
     616        self.doModify("INSERT INTO userpquota (userid, printerid, softlimit, hardlimit, warncount, datelimit, pagecounter, lifepagecounter, maxjobsize) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)" \ 
     617                          % (self.doQuote(upq.User.ident), \ 
     618                             self.doQuote(upq.Printer.ident), \ 
     619                             self.doQuote(upq.SoftLimit), \ 
     620                             self.doQuote(upq.HardLimit), \ 
     621                             self.doQuote(upq.WarnCount), \ 
     622                             self.doQuote(upq.DateLimit), \ 
     623                             self.doQuote(upq.PageCounter or 0), \ 
     624                             self.doQuote(upq.LifePageCounter or 0), \ 
     625                             self.doQuote(upq.MaxJobSize))) 
     626        upq.isDirty = False 
     627        return None # the entry created doesn't need further modification 
    643628         
    644629    def addGroupPQuota(self, gpq) : 
    645630        """Initializes a group print quota on a printer.""" 
    646         try : 
    647             self.doModify("INSERT INTO grouppquota (groupid, printerid, softlimit, hardlimit, datelimit, maxjobsize) VALUES (%s, %s, %s, %s, %s, %s)" \ 
    648                               % (self.doQuote(gpq.Group.ident), \ 
    649                                  self.doQuote(gpq.Printer.ident), \ 
    650                                  self.doQuote(gpq.SoftLimit), \ 
    651                                  self.doQuote(gpq.HardLimit), \ 
    652                                  self.doQuote(gpq.DateLimit), \ 
    653                                  self.doQuote(gpq.MaxJobSize))) 
    654         except PyKotaStorageError :                          
    655             # TODO : check if this is an error different from a duplicate insert 
    656             # return the existing entry which has to be modified 
    657             return self.getGroupPQuota(gpq.Group, gpq.Printer) 
    658         else :     
    659             gpq.isDirty = False 
    660             return None # the entry created doesn't need further modification 
     631        oldentry = self.getGroupPQuota(gpq.Group, gpq.Printer) 
     632        if oldentry.Exists : 
     633            return oldentry 
     634        self.doModify("INSERT INTO grouppquota (groupid, printerid, softlimit, hardlimit, datelimit, maxjobsize) VALUES (%s, %s, %s, %s, %s, %s)" \ 
     635                          % (self.doQuote(gpq.Group.ident), \ 
     636                             self.doQuote(gpq.Printer.ident), \ 
     637                             self.doQuote(gpq.SoftLimit), \ 
     638                             self.doQuote(gpq.HardLimit), \ 
     639                             self.doQuote(gpq.DateLimit), \ 
     640                             self.doQuote(gpq.MaxJobSize))) 
     641        gpq.isDirty = False 
     642        return None # the entry created doesn't need further modification 
    661643         
    662644    def savePrinter(self, printer) :