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

Revision 952, 4.0 kB (checked in by jalet, 21 years ago)

Preliminary support for LPRng added BUT STILL UNTESTED.

  • 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.8  2003/04/23 22:13:57  jalet
24# Preliminary support for LPRng added BUT STILL UNTESTED.
25#
26# Revision 1.7  2003/04/15 11:30:57  jalet
27# More work done on money print charging.
28# Minor bugs corrected.
29# All tools now access to the storage as priviledged users, repykota excepted.
30#
31# Revision 1.6  2003/03/29 13:45:27  jalet
32# GPL paragraphs were incorrectly (from memory) copied into the sources.
33# Two README files were added.
34# Upgrade script for PostgreSQL pre 1.01 schema was added.
35#
36# Revision 1.5  2003/03/22 13:11:33  jalet
37# The port on which the Quota Storage Sever is listening can now
38# be set in the configuration file (see sample).
39# Better error handling if PygreSQL is not installed.
40# Improved documentation.
41# Version number changed to 1.02alpha
42#
43# Revision 1.4  2003/02/17 22:05:50  jalet
44# Storage backend now supports admin and user passwords (untested)
45#
46# Revision 1.3  2003/02/06 14:49:04  jalet
47# edpykota should be ok now
48#
49# Revision 1.2  2003/02/05 22:28:38  jalet
50# More robust storage
51#
52# Revision 1.1  2003/02/05 21:28:17  jalet
53# Initial import into CVS
54#
55#
56#
57
58try :
59    import pg
60except ImportError :   
61    import sys
62    sys.stderr.write("This python version (%s) doesn't seem to have the PygreSQL module installed correctly.\n" % sys.version.split()[0])
63    raise
64
65from pykota.storage import PyKotaStorageError
66from pykota.storages import sql
67
68class Storage(sql.SQLStorage) :
69    def __init__(self, host, dbname, user, passwd) :
70        """Opens the PostgreSQL database connection."""
71        self.closed = 1
72        try :
73            (host, port) = host.split(":")
74            port = int(port)
75        except ValueError :   
76            port = -1         # Use PostgreSQL's default tcp/ip port (5432).
77       
78        try :
79            self.database = pg.connect(host=host, port=port, dbname=dbname, user=user, passwd=passwd)
80            self.closed = 0
81        except pg.error, msg :
82            raise PyKotaStorageError, msg
83           
84    def __del__(self) :       
85        """Closes the database connection."""
86        if not self.closed :
87            self.database.close()
88            self.closed = 1
89       
90    def doQuery(self, query) :
91        """Does a query."""
92        if type(query) in (type([]), type(())) :
93            query = ";".join(query)
94        query = query.strip()   
95        if not query.endswith(';') :   
96            query += ';'
97        self.database.query("BEGIN;")
98        try :
99            result = self.database.query(query)
100        except pg.error, msg :   
101            self.database.query("ROLLBACK;")
102            raise PyKotaStorageError, msg
103        else :   
104            self.database.query("COMMIT;")
105            return result
106       
107    def doQuote(self, field) :
108        """Quotes a field for use as a string in SQL queries."""
109        if type(field) == type(0) : # TODO : do something safer
110            typ = "decimal"
111        else :   
112            typ = "text"
113        return pg._quote(field, typ)
114       
115    def doParseResult(self, result) :
116        """Returns the result as a list of Python mappings."""
117        if (result is not None) and (result.ntuples() > 0) :
118            return result.dictresult()
119       
Note: See TracBrowser for help on using the browser.