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

Revision 2147, 4.3 kB (checked in by jerome, 19 years ago)

Removed all references to $Log$

  • 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307, 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        try :
51            self.database = pg.connect(host=host, port=port, dbname=dbname, user=user, passwd=passwd)
52        except PGError, msg :
53            raise PyKotaStorageError, str(msg)
54        else :   
55            self.closed = 0
56            self.tool.logdebug("Database opened (host=%s, port=%s, dbname=%s, user=%s)" % (host, port, dbname, user))
57           
58    def close(self) :   
59        """Closes the database connection."""
60        if not self.closed :
61            self.database.close()
62            self.closed = 1
63            self.tool.logdebug("Database closed.")
64       
65    def beginTransaction(self) :   
66        """Starts a transaction."""
67        self.database.query("BEGIN;")
68        self.tool.logdebug("Transaction begins...")
69       
70    def commitTransaction(self) :   
71        """Commits a transaction."""
72        self.database.query("COMMIT;")
73        self.tool.logdebug("Transaction committed.")
74       
75    def rollbackTransaction(self) :     
76        """Rollbacks a transaction."""
77        self.database.query("ROLLBACK;")
78        self.tool.logdebug("Transaction aborted.")
79       
80    def doRawSearch(self, query) :
81        """Does a raw search query."""
82        query = query.strip()   
83        if not query.endswith(';') :   
84            query += ';'
85        try :
86            self.tool.logdebug("QUERY : %s" % query)
87            result = self.database.query(query)
88        except PGError, msg :   
89            raise PyKotaStorageError, str(msg)
90        else :   
91            return result
92           
93    def doSearch(self, query) :       
94        """Does a search query."""
95        result = self.doRawSearch(query)
96        if (result is not None) and (result.ntuples() > 0) : 
97            return result.dictresult()
98       
99    def doModify(self, query) :
100        """Does a (possibly multiple) modify query."""
101        query = query.strip()   
102        if not query.endswith(';') :   
103            query += ';'
104        try :
105            self.tool.logdebug("QUERY : %s" % query)
106            result = self.database.query(query)
107        except PGError, msg :   
108            raise PyKotaStorageError, str(msg)
109        else :   
110            return result
111           
112    def doQuote(self, field) :
113        """Quotes a field for use as a string in SQL queries."""
114        if type(field) == type(0.0) : 
115            typ = "decimal"
116        elif type(field) == type(0) :   
117            typ = "int"
118        elif type(field) == type(0L) :   
119            typ = "int"
120        else :   
121            typ = "text"
122        return pg._quote(field, typ)
Note: See TracBrowser for help on using the browser.