Changeset 3531
- Timestamp:
- 04/17/10 23:08:15 (15 years ago)
- Location:
- pykota/trunk/pykota
- Files:
-
- 3 modified
Legend:
- Unmodified
- Added
- Removed
-
pykota/trunk/pykota/storages/sqlitestorage.py
r3528 r3531 38 38 """Opens the SQLite database connection.""" 39 39 BaseStorage.__init__(self, pykotatool) 40 40 self.doModify = self.doQuery 41 41 self.tool.logdebug("Trying to open database (dbname=%s)..." % repr(dbname)) 42 42 self.database = sqlite.connect(dbname, isolation_level=None) 43 43 self.cursor = self.database.cursor() 44 44 self.closed = False 45 try : 46 self.doQuery("PRAGMA foreign_keys = True;") 47 except PyKotaStorageError : 48 pass 45 49 self.tool.logdebug("Database opened (dbname=%s)" % repr(dbname)) 46 50 … … 68 72 self.tool.logdebug("Transaction aborted.") 69 73 70 def do RawSearch(self, query) :71 """ Does a raw searchquery."""74 def doQuery(self, query) : 75 """Executes an SQL query.""" 72 76 query = query.strip() 73 77 if not query.endswith(';') : … … 77 81 self.cursor.execute(query) 78 82 except self.database.Error, msg : 83 self.tool.logdebug("Query failed : %s" % repr(msg)) 79 84 raise PyKotaStorageError, repr(msg) 80 else : 81 result = self.cursor.fetchall() 82 return result 85 86 def doRawSearch(self, query) : 87 """Executes a raw search query.""" 88 self.doQuery(query) 89 result = self.cursor.fetchall() 90 return result 83 91 84 92 def doSearch(self, query) : … … 101 109 rows.append(rowdict) 102 110 return rows 103 104 def doModify(self, query) :105 """Does a (possibly multiple) modify query."""106 query = query.strip()107 if not query.endswith(';') :108 query += ';'109 self.querydebug("QUERY : %s" % query)110 try :111 self.cursor.execute(query)112 except self.database.Error, msg :113 self.tool.logdebug("Query failed : %s" % repr(msg))114 raise PyKotaStorageError, repr(msg)115 111 116 112 def doQuote(self, field) : -
pykota/trunk/pykota/storages/sql.py
r3524 r3531 36 36 user = StorageUser(self, username) 37 37 user.ident = record.get("uid", record.get("userid", record.get("id"))) 38 user.LimitBy = record.get("limitby") or "quota"38 user.LimitBy = databaseToUnicode(record.get("limitby") or "quota") 39 39 user.AccountBalance = record.get("balance") 40 40 user.LifeTimePaid = record.get("lifetimepaid") 41 user.Email = record.get("email")41 user.Email = databaseToUnicode(record.get("email")) 42 42 user.Description = databaseToUnicode(record.get("description")) 43 43 user.OverCharge = record.get("overcharge", 1.0) … … 49 49 group = StorageGroup(self, groupname) 50 50 group.ident = record.get("id") 51 group.LimitBy = record.get("limitby") or "quota"51 group.LimitBy = databaseToUnicode(record.get("limitby") or "quota") 52 52 group.AccountBalance = record.get("balance") 53 53 group.LifeTimePaid = record.get("lifetimepaid") … … 85 85 job.JobOptions = databaseToUnicode(record.get("options") or "") 86 86 job.JobDate = record.get("jobdate") 87 job.JobHostName = record.get("hostname")87 job.JobHostName = databaseToUnicode(record.get("hostname")) 88 88 job.JobSizeBytes = record.get("jobsizebytes") 89 89 job.JobMD5Sum = record.get("md5sum") … … 587 587 self.doModify("INSERT INTO users (username, limitby, balance, lifetimepaid, email, overcharge, description) VALUES (%s, %s, %s, %s, %s, %s, %s)" % \ 588 588 (self.doQuote(unicodeToDatabase(user.Name)), \ 589 self.doQuote(u ser.LimitBy or 'quota'), \589 self.doQuote(unicodeToDatabase(user.LimitBy or 'quota')), \ 590 590 self.doQuote(user.AccountBalance or 0.0), \ 591 591 self.doQuote(user.LifeTimePaid or 0.0), \ 592 self.doQuote(u ser.Email), \592 self.doQuote(unicodeToDatabase(user.Email)), \ 593 593 self.doQuote(user.OverCharge), \ 594 594 self.doQuote(unicodeToDatabase(user.Description)))) … … 607 607 self.doModify("INSERT INTO groups (groupname, limitby, description) VALUES (%s, %s, %s)" % \ 608 608 (self.doQuote(unicodeToDatabase(group.Name)), \ 609 self.doQuote( group.LimitBy or "quota"), \609 self.doQuote(unicodeToDatabase(group.LimitBy or "quota")), \ 610 610 self.doQuote(unicodeToDatabase(group.Description)))) 611 611 group.isDirty = False … … 673 673 """Saves the user to the database in a single operation.""" 674 674 self.doModify("UPDATE users SET limitby=%s, balance=%s, lifetimepaid=%s, email=%s, overcharge=%s, description=%s WHERE id=%s" \ 675 % (self.doQuote(u ser.LimitBy or 'quota'), \675 % (self.doQuote(unicodeToDatabase(user.LimitBy or 'quota')), \ 676 676 self.doQuote(user.AccountBalance or 0.0), \ 677 677 self.doQuote(user.LifeTimePaid or 0.0), \ 678 self.doQuote(u ser.Email), \678 self.doQuote(unicodeToDatabase(user.Email)), \ 679 679 self.doQuote(user.OverCharge), \ 680 680 self.doQuote(unicodeToDatabase(user.Description)), \ … … 684 684 """Saves the group to the database in a single operation.""" 685 685 self.doModify("UPDATE groups SET limitby=%s, description=%s WHERE id=%s" \ 686 % (self.doQuote( group.LimitBy or 'quota'), \686 % (self.doQuote(unicodeToDatabase(group.LimitBy or 'quota')), \ 687 687 self.doQuote(unicodeToDatabase(group.Description)), \ 688 688 self.doQuote(group.ident))) … … 736 736 # For legal reasons, we want to hide the title, filename and options 737 737 title = filename = options = "hidden" 738 filename = unicodeToDatabase(filename)739 title = unicodeToDatabase(title)740 options = unicodeToDatabase(options)741 jobbilling = unicodeToDatabase(jobbilling)742 738 if (not self.disablehistory) or (not printer.LastJob.Exists) : 743 739 if jobsize is not None : 744 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))) 740 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)" \ 741 % (self.doQuote(user.ident), 742 self.doQuote(printer.ident), 743 self.doQuote(jobid), 744 self.doQuote(pagecounter), 745 self.doQuote(action), 746 self.doQuote(jobsize), 747 self.doQuote(jobprice), 748 self.doQuote(unicodeToDatabase(filename)), 749 self.doQuote(unicodeToDatabase(title)), 750 self.doQuote(copies), 751 self.doQuote(unicodeToDatabase(options)), 752 self.doQuote(unicodeToDatabase(clienthost)), 753 self.doQuote(jobsizebytes), 754 self.doQuote(jobmd5sum), 755 self.doQuote(jobpages), 756 self.doQuote(unicodeToDatabase(jobbilling)), 757 self.doQuote(precomputedsize), 758 self.doQuote(precomputedprice))) 745 759 else : 746 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 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)" \ 761 % (self.doQuote(user.ident), 762 self.doQuote(printer.ident), 763 self.doQuote(jobid), 764 self.doQuote(pagecounter), 765 self.doQuote(action), 766 self.doQuote(unicodeToDatabase(filename)), 767 self.doQuote(unicodeToDatabase(title)), 768 self.doQuote(copies), 769 self.doQuote(unicodeToDatabase(options)), 770 self.doQuote(unicodeToDatabase(clienthost)), 771 self.doQuote(jobsizebytes), 772 self.doQuote(jobmd5sum), 773 self.doQuote(jobpages), 774 self.doQuote(unicodeToDatabase(jobbilling)), 775 self.doQuote(precomputedsize), 776 self.doQuote(precomputedprice))) 747 777 else : 748 778 # here we explicitly want to reset jobsize to NULL if needed 749 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))) 779 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" \ 780 % (self.doQuote(user.ident), 781 self.doQuote(jobid), 782 self.doQuote(pagecounter), 783 self.doQuote(action), 784 self.doQuote(jobsize), 785 self.doQuote(jobprice), 786 self.doQuote(unicodeToDatabase(filename)), 787 self.doQuote(unicodeToDatabase(title)), 788 self.doQuote(copies), 789 self.doQuote(unicodeToDatabase(options)), 790 self.doQuote(unicodeToDatabase(clienthost)), 791 self.doQuote(jobsizebytes), 792 self.doQuote(jobmd5sum), 793 self.doQuote(jobpages), 794 self.doQuote(unicodeToDatabase(jobbilling)), 795 self.doQuote(precomputedsize), 796 self.doQuote(precomputedprice), 797 self.doQuote(printer.LastJob.ident))) 750 798 751 799 def saveUserPQuota(self, userpquota) : 752 800 """Saves an user print quota entry.""" 753 801 self.doModify("UPDATE userpquota SET softlimit=%s, hardlimit=%s, warncount=%s, datelimit=%s, pagecounter=%s, lifepagecounter=%s, maxjobsize=%s WHERE id=%s" \ 754 % (self.doQuote(userpquota.SoftLimit), \755 self.doQuote(userpquota.HardLimit), \756 self.doQuote(userpquota.WarnCount or 0), \757 self.doQuote(userpquota.DateLimit), \758 self.doQuote(userpquota.PageCounter or 0), \759 self.doQuote(userpquota.LifePageCounter or 0), \760 self.doQuote(userpquota.MaxJobSize), \802 % (self.doQuote(userpquota.SoftLimit), 803 self.doQuote(userpquota.HardLimit), 804 self.doQuote(userpquota.WarnCount or 0), 805 self.doQuote(userpquota.DateLimit), 806 self.doQuote(userpquota.PageCounter or 0), 807 self.doQuote(userpquota.LifePageCounter or 0), 808 self.doQuote(userpquota.MaxJobSize), 761 809 self.doQuote(userpquota.ident))) 762 810 763 811 def writeUserPQuotaWarnCount(self, userpquota, warncount) : 764 812 """Sets the warn counter value for a user quota.""" 765 self.doModify("UPDATE userpquota SET warncount=%s WHERE id=%s" % (self.doQuote(warncount), self.doQuote(userpquota.ident))) 813 self.doModify("UPDATE userpquota SET warncount=%s WHERE id=%s" \ 814 % (self.doQuote(warncount), 815 self.doQuote(userpquota.ident))) 766 816 767 817 def increaseUserPQuotaWarnCount(self, userpquota) : 768 818 """Increases the warn counter value for a user quota.""" 769 self.doModify("UPDATE userpquota SET warncount=warncount+1 WHERE id=%s" % self.doQuote(userpquota.ident)) 819 self.doModify("UPDATE userpquota SET warncount=warncount+1 WHERE id=%s" \ 820 % self.doQuote(userpquota.ident)) 770 821 771 822 def saveGroupPQuota(self, grouppquota) : 772 823 """Saves a group print quota entry.""" 773 824 self.doModify("UPDATE grouppquota SET softlimit=%s, hardlimit=%s, datelimit=%s WHERE id=%s" \ 774 % (self.doQuote(grouppquota.SoftLimit), \775 self.doQuote(grouppquota.HardLimit), \776 self.doQuote(grouppquota.DateLimit), \825 % (self.doQuote(grouppquota.SoftLimit), 826 self.doQuote(grouppquota.HardLimit), 827 self.doQuote(grouppquota.DateLimit), 777 828 self.doQuote(grouppquota.ident))) 778 829 … … 800 851 where.append("printerid=%s" % self.doQuote(printer.ident)) 801 852 if hostname is not None : 802 where.append("hostname=%s" % self.doQuote( hostname))853 where.append("hostname=%s" % self.doQuote(unicodeToDatabase(hostname))) 803 854 if billingcode is not None : 804 855 where.append("billingcode=%s" % self.doQuote(unicodeToDatabase(billingcode))) -
pykota/trunk/pykota/version.py
r3508 r3531 24 24 import time 25 25 26 __version__ = "1.27alpha 8_unofficial"26 __version__ = "1.27alpha9_unofficial" 27 27 28 28 __doc__ = "PyKota : a complete Printing Quota Solution for CUPS."