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

Revision 695, 1.8 kB (checked in by jalet, 21 years ago)

Initial import into CVS

  • 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# You're welcome to redistribute this software under the
7# terms of the GNU General Public Licence version 2.0
8# or, at your option, any higher version.
9#
10# You can read the complete GNU GPL in the file COPYING
11# which should come along with this software, or visit
12# the Free Software Foundation's WEB site http://www.fsf.org
13#
14# $Id$
15#
16# $Log$
17# Revision 1.1  2003/02/05 21:28:17  jalet
18# Initial import into CVS
19#
20#
21#
22
23import pg
24
25from pykota.storage import PyKotaStorageError
26from pykota.storages import sql
27
28class Storage(sql.SQLStorage) :
29    def __init__(self, host, dbname, user) :
30        """Opens the PostgreSQL database connection."""
31        self.closed = 1
32        try :
33            self.database = pg.connect(host=host, dbname=dbname, user=user)
34            self.closed = 0
35        except pg.error, msg :
36            raise PyKotaStorageError, msg
37           
38    def __del__(self) :       
39        """Closes the database connection."""
40        if not self.closed :
41            self.database.close()
42            self.closed = 1
43       
44    def doQuery(self, query) :
45        """Does a query."""
46        try :
47            return self.database.query(query)
48        except pg.error, msg :   
49            raise PyKotaStorageError, msg
50       
51    def doQuote(self, field) :
52        """Quotes a field for use as a string in SQL queries."""
53        if type(field) == type(0) : # TODO : do something safer
54            typ = "decimal"
55        else :   
56            typ = "text"
57        return pg._quote(field, typ)
58       
59    def doParseResult(self, result) :
60        """Returns the result as a Python dictionnary."""
61        try :
62            return result.dictresult()[0]
63        except IndexError :   
64            return None # not found
65       
Note: See TracBrowser for help on using the browser.