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

Revision 2302, 4.2 kB (checked in by jerome, 19 years ago)

Updated the FSF's address

  • 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20#
21# $Id$
22#
23#
24
25from pykota.storage import PyKotaStorageError,BaseStorage,StorageObject,StorageUser,StorageGroup,StoragePrinter,StorageJob,StorageLastJob,StorageUserPQuota,StorageGroupPQuota
26from pykota.storages.sql import SQLStorage
27
28try :
29    import pg
30except ImportError :   
31    import sys
32    # TODO : to translate or not to translate ?
33    raise PyKotaStorageError, "This python version (%s) doesn't seem to have the PygreSQL module installed correctly." % sys.version.split()[0]
34else :   
35    try :
36        PGError = pg.Error
37    except AttributeError :   
38        PGError = pg.error
39
40class Storage(BaseStorage, SQLStorage) :
41    def __init__(self, pykotatool, host, dbname, user, passwd) :
42        """Opens the PostgreSQL database connection."""
43        BaseStorage.__init__(self, pykotatool)
44        try :
45            (host, port) = host.split(":")
46            port = int(port)
47        except ValueError :   
48            port = -1         # Use PostgreSQL's default tcp/ip port (5432).
49       
50        self.database = pg.connect(host=host, port=port, dbname=dbname, user=user, passwd=passwd)
51        self.closed = 0
52        self.tool.logdebug("Database opened (host=%s, port=%s, dbname=%s, user=%s)" % (host, port, dbname, user))
53           
54    def close(self) :   
55        """Closes the database connection."""
56        if not self.closed :
57            self.database.close()
58            self.closed = 1
59            self.tool.logdebug("Database closed.")
60       
61    def beginTransaction(self) :   
62        """Starts a transaction."""
63        self.database.query("BEGIN;")
64        self.tool.logdebug("Transaction begins...")
65       
66    def commitTransaction(self) :   
67        """Commits a transaction."""
68        self.database.query("COMMIT;")
69        self.tool.logdebug("Transaction committed.")
70       
71    def rollbackTransaction(self) :     
72        """Rollbacks a transaction."""
73        self.database.query("ROLLBACK;")
74        self.tool.logdebug("Transaction aborted.")
75       
76    def doRawSearch(self, query) :
77        """Does a raw search query."""
78        query = query.strip()   
79        if not query.endswith(';') :   
80            query += ';'
81        try :
82            self.tool.logdebug("QUERY : %s" % query)
83            result = self.database.query(query)
84        except PGError, msg :   
85            raise PyKotaStorageError, str(msg)
86        else :   
87            return result
88           
89    def doSearch(self, query) :       
90        """Does a search query."""
91        result = self.doRawSearch(query)
92        if (result is not None) and (result.ntuples() > 0) : 
93            return result.dictresult()
94       
95    def doModify(self, query) :
96        """Does a (possibly multiple) modify query."""
97        query = query.strip()   
98        if not query.endswith(';') :   
99            query += ';'
100        try :
101            self.tool.logdebug("QUERY : %s" % query)
102            result = self.database.query(query)
103        except PGError, msg :   
104            raise PyKotaStorageError, str(msg)
105        else :   
106            return result
107           
108    def doQuote(self, field) :
109        """Quotes a field for use as a string in SQL queries."""
110        if type(field) == type(0.0) : 
111            typ = "decimal"
112        elif type(field) == type(0) :   
113            typ = "int"
114        elif type(field) == type(0L) :   
115            typ = "int"
116        else :   
117            typ = "text"
118        return pg._quote(field, typ)
Note: See TracBrowser for help on using the browser.