root / pykota / trunk / pykota / storages / postgresql.py @ 967

Revision 967, 4.1 kB (checked in by jalet, 21 years ago)

LDAP storage backend's skeleton added. DOESN'T WORK.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1# PyKota
2#
3# PyKota : Print Quotas for CUPS and LPRng
4#
5# (c) 2003 Jerome Alet <alet@librelogiciel.com>
6# This program is free software; you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation; either version 2 of the License, or
9# (at your option) any later version.
10#
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
17# along with this program; if not, write to the Free Software
18# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
19#
20# $Id$
21#
22# $Log$
23# Revision 1.9  2003/04/27 08:04:15  jalet
24# LDAP storage backend's skeleton added. DOESN'T WORK.
25#
26# Revision 1.8  2003/04/23 22:13:57  jalet
27# Preliminary support for LPRng added BUT STILL UNTESTED.
28#
29# Revision 1.7  2003/04/15 11:30:57  jalet
30# More work done on money print charging.
31# Minor bugs corrected.
32# All tools now access to the storage as priviledged users, repykota excepted.
33#
34# Revision 1.6  2003/03/29 13:45:27  jalet
35# GPL paragraphs were incorrectly (from memory) copied into the sources.
36# Two README files were added.
37# Upgrade script for PostgreSQL pre 1.01 schema was added.
38#
39# Revision 1.5  2003/03/22 13:11:33  jalet
40# The port on which the Quota Storage Sever is listening can now
41# be set in the configuration file (see sample).
42# Better error handling if PygreSQL is not installed.
43# Improved documentation.
44# Version number changed to 1.02alpha
45#
46# Revision 1.4  2003/02/17 22:05:50  jalet
47# Storage backend now supports admin and user passwords (untested)
48#
49# Revision 1.3  2003/02/06 14:49:04  jalet
50# edpykota should be ok now
51#
52# Revision 1.2  2003/02/05 22:28:38  jalet
53# More robust storage
54#
55# Revision 1.1  2003/02/05 21:28:17  jalet
56# Initial import into CVS
57#
58#
59#
60
61
62from pykota.storage import PyKotaStorageError
63from pykota.storages import sql
64
65try :
66    import pg
67except ImportError :   
68    import sys
69    # TODO : to translate or not to translate ?
70    raise PyKotaStorageError, "This python version (%s) doesn't seem to have the PygreSQL module installed correctly." % sys.version.split()[0]
71
72class Storage(sql.SQLStorage) :
73    def __init__(self, host, dbname, user, passwd) :
74        """Opens the PostgreSQL database connection."""
75        self.closed = 1
76        try :
77            (host, port) = host.split(":")
78            port = int(port)
79        except ValueError :   
80            port = -1         # Use PostgreSQL's default tcp/ip port (5432).
81       
82        try :
83            self.database = pg.connect(host=host, port=port, dbname=dbname, user=user, passwd=passwd)
84            self.closed = 0
85        except pg.error, msg :
86            raise PyKotaStorageError, msg
87           
88    def __del__(self) :       
89        """Closes the database connection."""
90        if not self.closed :
91            self.database.close()
92            self.closed = 1
93       
94    def doQuery(self, query) :
95        """Does a query."""
96        if type(query) in (type([]), type(())) :
97            query = ";".join(query)
98        query = query.strip()   
99        if not query.endswith(';') :   
100            query += ';'
101        self.database.query("BEGIN;")
102        try :
103            result = self.database.query(query)
104        except pg.error, msg :   
105            self.database.query("ROLLBACK;")
106            raise PyKotaStorageError, msg
107        else :   
108            self.database.query("COMMIT;")
109            return result
110       
111    def doQuote(self, field) :
112        """Quotes a field for use as a string in SQL queries."""
113        if type(field) == type(0) : # TODO : do something safer
114            typ = "decimal"
115        else :   
116            typ = "text"
117        return pg._quote(field, typ)
118       
119    def doParseResult(self, result) :
120        """Returns the result as a list of Python mappings."""
121        if (result is not None) and (result.ntuples() > 0) :
122            return result.dictresult()
123       
Note: See TracBrowser for help on using the browser.