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

Revision 915, 3.8 kB (checked in by jalet, 21 years ago)

More work done on money print charging.
Minor bugs corrected.
All tools now access to the storage as priviledged users, repykota excepted.

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