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

Revision 2147, 5.2 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
25########################################################################
26##                                                                    ##
27## THIS FILE IS HIGHLY EXPERIMENTAL.                                  ##
28##                                                                    ##
29## IT WILL PROVIDE DATABASE INDEPENDANCE IN THE FUTURE.               ##
30##                                                                    ##
31## FOR NOW IT ONLY RECOGNIZES THE POSTGRESQL DBI MODULE.              ##
32##                                                                    ##
33## DON'T USE THIS UNTIL YOU ARE TOLD TO DO SO.                        ##
34##                                                                    ##
35##                                      THANKS !                      ##
36##                                                                    ##
37########################################################################
38
39from pykota.storage import PyKotaStorageError,BaseStorage,StorageObject,StorageUser,StorageGroup,StoragePrinter,StorageJob,StorageLastJob,StorageUserPQuota,StorageGroupPQuota
40from pykota.storages.sql import SQLStorage
41
42try :
43    import _pg
44    import pgdb
45except ImportError :   
46    import sys
47    # TODO : to translate or not to translate ?
48    raise PyKotaStorageError, "This python version (%s) doesn't seem to have the PygreSQL module installed correctly." % sys.version.split()[0]
49
50class Storage(BaseStorage, SQLStorage) :
51    def __init__(self, pykotatool, host, dbname, user, passwd) :
52        """Opens the Database Independant connection."""
53        BaseStorage.__init__(self, pykotatool)
54        try :
55            (host, port) = host.split(":")
56            port = int(port)
57        except ValueError :   
58            port = -1         # Use PostgreSQL's default tcp/ip port (5432).
59       
60        try :
61            self.database = pgdb.connect(host=host, database=dbname, user=user, password=passwd)
62            self.cursor = self.database.cursor()
63        except _pg.error, msg :
64            raise PyKotaStorageError, msg
65        else :   
66            self.closed = 0
67            self.tool.logdebug("Database opened (host=%s, port=%s, dbname=%s, user=%s)" % (host, port, dbname, user))
68           
69    def close(self) :   
70        """Closes the database connection."""
71        if not self.closed :
72            self.cursor.close()
73            self.database.close()
74            self.closed = 1
75            self.tool.logdebug("Database closed.")
76       
77    def beginTransaction(self) :   
78        """Starts a transaction."""
79        # No need for this since transactions begin automatically
80        # self.database.query("BEGIN;")
81        self.tool.logdebug("Transaction begins...")
82       
83    def commitTransaction(self) :   
84        """Commits a transaction."""
85        self.database.commit()
86        self.tool.logdebug("Transaction committed.")
87       
88    def rollbackTransaction(self) :     
89        """Rollbacks a transaction."""
90        self.database.rollback()
91        self.tool.logdebug("Transaction aborted.")
92       
93    def doSearch(self, query) :
94        """Does a search query."""
95        query = query.strip()   
96        if not query.endswith(';') :   
97            query += ';'
98        try :
99            self.tool.logdebug("QUERY : %s" % query)
100            self.cursor.execute(query)
101            result = self.cursor.fetchall()
102        except pgdb.DatabaseError, msg :   
103            raise PyKotaStorageError, msg
104        else :   
105            newresult = []
106            fieldsnames = [desc[0] for desc in self.cursor.description]
107            for record in result :
108                dico = {}
109                for fnumber in range(len(record)) :
110                    dico.update({fieldsnames[fnumber] : record[fnumber]})
111                newresult.append(dico)   
112            return newresult
113           
114    def doModify(self, query) :
115        """Does a (possibly multiple) modify query."""
116        query = query.strip()   
117        if not query.endswith(';') :   
118            query += ';'
119        try :
120            self.tool.logdebug("QUERY : %s" % query)
121            result = self.database.query(query)
122        except pg.error, msg :   
123            raise PyKotaStorageError, msg
124        else :   
125            return result
126           
127    def doQuote(self, field) :
128        """Quotes a field for use as a string in SQL queries."""
129        return pgdb._quote(field)
Note: See TracBrowser for help on using the browser.