root / pykota / trunk / pykota / storages / pgstorage.py @ 1327

Revision 1327, 7.5 kB (checked in by jalet, 20 years ago)

Preliminary work on Relationnal Database Independance via DB-API 2.0

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1# PyKota
2# -*- coding: ISO-8859-15 -*-
3#
4# PyKota : Print Quotas for CUPS and LPRng
5#
6# (c) 2003-2004 Jerome Alet <alet@librelogiciel.com>
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 2 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program; if not, write to the Free Software
19# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
20#
21# $Id$
22#
23# $Log$
24# Revision 1.35  2004/02/02 22:44:16  jalet
25# Preliminary work on Relationnal Database Independance via DB-API 2.0
26#
27# Revision 1.34  2004/01/12 15:12:50  jalet
28# Small fix for history
29#
30# Revision 1.33  2004/01/12 14:44:47  jalet
31# Missing space in SQL query
32#
33# Revision 1.32  2004/01/12 14:35:02  jalet
34# Printing history added to CGI script.
35#
36# Revision 1.31  2004/01/10 09:44:02  jalet
37# Fixed potential accuracy problem if a user printed on several printers at
38# the very same time.
39#
40# Revision 1.30  2004/01/08 16:33:27  jalet
41# Additionnal check to not create a circular printers group.
42#
43# Revision 1.29  2004/01/08 16:24:49  jalet
44# edpykota now supports adding printers to printer groups.
45#
46# Revision 1.28  2004/01/08 14:10:33  jalet
47# Copyright year changed.
48#
49# Revision 1.27  2004/01/06 14:24:59  jalet
50# Printer groups should be cached now, if caching is enabled.
51#
52# Revision 1.26  2003/12/29 14:12:48  uid67467
53# Tries to workaround possible integrity violations when retrieving printer groups
54#
55# Revision 1.25  2003/12/27 16:49:25  uid67467
56# Should be ok now.
57#
58# Revision 1.24  2003/11/29 22:02:14  jalet
59# Don't try to retrieve the user print quota information if current printer
60# doesn't exist.
61#
62# Revision 1.23  2003/11/23 19:01:37  jalet
63# Job price added to history
64#
65# Revision 1.22  2003/11/21 14:28:46  jalet
66# More complete job history.
67#
68# Revision 1.21  2003/11/12 13:06:38  jalet
69# Bug fix wrt no user/group name command line argument to edpykota
70#
71# Revision 1.20  2003/10/09 21:25:26  jalet
72# Multiple printer names or wildcards can be passed on the command line
73# separated with commas.
74# Beta phase.
75#
76# Revision 1.19  2003/10/08 07:01:20  jalet
77# Job history can be disabled.
78# Some typos in README.
79# More messages in setup script.
80#
81# Revision 1.18  2003/10/07 09:07:30  jalet
82# Character encoding added to please latest version of Python
83#
84# Revision 1.17  2003/10/06 13:12:28  jalet
85# More work on caching
86#
87# Revision 1.16  2003/10/03 18:01:49  jalet
88# Nothing interesting...
89#
90# Revision 1.15  2003/10/03 12:27:03  jalet
91# Several optimizations, especially with LDAP backend
92#
93# Revision 1.14  2003/10/03 08:57:55  jalet
94# Caching mechanism now caches all that's cacheable.
95#
96# Revision 1.13  2003/10/02 20:23:18  jalet
97# Storage caching mechanism added.
98#
99# Revision 1.12  2003/08/17 14:20:25  jalet
100# Bug fix by Oleg Biteryakov
101#
102# Revision 1.11  2003/07/29 20:55:17  jalet
103# 1.14 is out !
104#
105# Revision 1.10  2003/07/16 21:53:08  jalet
106# Really big modifications wrt new configuration file's location and content.
107#
108# Revision 1.9  2003/07/14 17:20:15  jalet
109# Bug in postgresql storage when modifying the prices for a printer
110#
111# Revision 1.8  2003/07/14 14:18:17  jalet
112# Wrong documentation strings
113#
114# Revision 1.7  2003/07/09 20:17:07  jalet
115# Email field added to PostgreSQL schema
116#
117# Revision 1.6  2003/07/07 11:49:24  jalet
118# Lots of small fixes with the help of PyChecker
119#
120# Revision 1.5  2003/07/07 08:33:19  jalet
121# Bug fix due to a typo in LDAP code
122#
123# Revision 1.4  2003/06/30 13:54:21  jalet
124# Sorts by user / group name
125#
126# Revision 1.3  2003/06/25 14:10:01  jalet
127# Hey, it may work (edpykota --reset excepted) !
128#
129# Revision 1.2  2003/06/12 21:09:57  jalet
130# wrongly placed code.
131#
132# Revision 1.1  2003/06/10 16:37:54  jalet
133# Deletion of the second user which is not needed anymore.
134# Added a debug configuration field in /etc/pykota.conf
135# All queries can now be sent to the logger in debug mode, this will
136# greatly help improve performance when time for this will come.
137#
138#
139#
140#
141
142from pykota.storage import PyKotaStorageError,BaseStorage,StorageObject,StorageUser,StorageGroup,StoragePrinter,StorageJob,StorageLastJob,StorageUserPQuota,StorageGroupPQuota
143from pykota.storages.sql import SQLStorage
144
145try :
146    import pg
147except ImportError :   
148    import sys
149    # TODO : to translate or not to translate ?
150    raise PyKotaStorageError, "This python version (%s) doesn't seem to have the PygreSQL module installed correctly." % sys.version.split()[0]
151
152class Storage(BaseStorage, SQLStorage) :
153    def __init__(self, pykotatool, host, dbname, user, passwd) :
154        """Opens the PostgreSQL database connection."""
155        BaseStorage.__init__(self, pykotatool)
156        try :
157            (host, port) = host.split(":")
158            port = int(port)
159        except ValueError :   
160            port = -1         # Use PostgreSQL's default tcp/ip port (5432).
161       
162        try :
163            self.database = pg.connect(host=host, port=port, dbname=dbname, user=user, passwd=passwd)
164        except pg.error, msg :
165            raise PyKotaStorageError, msg
166        else :   
167            self.closed = 0
168            self.tool.logdebug("Database opened (host=%s, port=%s, dbname=%s, user=%s)" % (host, port, dbname, user))
169           
170    def close(self) :   
171        """Closes the database connection."""
172        if not self.closed :
173            self.database.close()
174            self.closed = 1
175            self.tool.logdebug("Database closed.")
176       
177    def beginTransaction(self) :   
178        """Starts a transaction."""
179        self.database.query("BEGIN;")
180        self.tool.logdebug("Transaction begins...")
181       
182    def commitTransaction(self) :   
183        """Commits a transaction."""
184        self.database.query("COMMIT;")
185        self.tool.logdebug("Transaction committed.")
186       
187    def rollbackTransaction(self) :     
188        """Rollbacks a transaction."""
189        self.database.query("ROLLBACK;")
190        self.tool.logdebug("Transaction aborted.")
191       
192    def doSearch(self, query) :
193        """Does a search query."""
194        query = query.strip()   
195        if not query.endswith(';') :   
196            query += ';'
197        try :
198            self.tool.logdebug("QUERY : %s" % query)
199            result = self.database.query(query)
200        except pg.error, msg :   
201            raise PyKotaStorageError, msg
202        else :   
203            if (result is not None) and (result.ntuples() > 0) : 
204                return result.dictresult()
205           
206    def doModify(self, query) :
207        """Does a (possibly multiple) modify query."""
208        query = query.strip()   
209        if not query.endswith(';') :   
210            query += ';'
211        try :
212            self.tool.logdebug("QUERY : %s" % query)
213            result = self.database.query(query)
214        except pg.error, msg :   
215            raise PyKotaStorageError, msg
216        else :   
217            return result
218           
219    def doQuote(self, field) :
220        """Quotes a field for use as a string in SQL queries."""
221        if type(field) == type(0.0) : 
222            typ = "decimal"
223        elif type(field) == type(0) :   
224            typ = "int"
225        else :   
226            typ = "text"
227        return pg._quote(field, typ)
Note: See TracBrowser for help on using the browser.