del net-oscar
[learning-git.git] / pgworksheet_yvesf / pgw / DBConnection.py
blobde5e4395d0d6b199e50cba04a0b0800271387bd1
1 # -*- coding: latin-1; -*-
3 # PgWorksheet - PostgreSQL Front End
4 # http://pgworksheet.projects.postgresql.org/
6 # Copyright © 2004-2005 Henri Michelon & CML http://www.e-cml.org/
8 # This program is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License
10 # as published by the Free Software Foundation; either version 2
11 # of the License, or (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details (read LICENSE.txt).
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 # $Id: DBConnection.py,v 1.20 2005/03/01 16:09:57 hmichelon Exp $
24 import string
25 import sys
26 #from mx import DateTime
27 from pyPgSQL import libpq
28 from pyPgSQL import PgSQL
30 class ConnectionParameter:
31 def __init__(self, name="", user="", password="", db="", local=False, host="", port="5432"):
32 self.name = name
34 self.user = user
35 self.password = password
36 self.db = db
38 self.local = local
40 self.host = host
41 self.port = port
43 def get_dsn(self):
44 dsn = ""
45 if (self.host is not None):
46 dsn = self.host
48 dsn = dsn + ":"
50 if (self.port is not None):
51 dsn = dsn + str(self.port)
53 dsn = dsn + ":"
55 if (self.db is not None):
56 dsn = dsn + self.db
58 dsn = dsn + ":"
60 if (self.user is not None):
61 dsn = dsn + self.user
63 dsn = dsn + ":"
64 if (self.password is not None):
65 dsn = dsn + self.password
67 return dsn
68 def dump(self):
69 print "NAME=%s : user=%s, password=%s, db=%s, local=%s, host=%s, port=%s"%(self.name, self.user, self.password, self.db, self.local, self.host, self.port)
72 DatabaseError = libpq.DatabaseError
74 class DBConnection:
75 """Database connection/deconnection and query execution"""
77 def __init__(self):
78 self.db = None
80 def connect(self, connectionParameter):
81 """Try to connect to a database"""
82 self.connectionParameter = connectionParameter
83 #self.db = MyPgSQL.connect(dsn) #delete see svn history for MyPgSQL.py. i dunno why this was necessary
84 self.db = PgSQL.connect(connectionParameter.get_dsn())
85 self.db.autocommit = 1
86 return self.db
89 def pgversion(self):
90 if (self.is_connected()):
91 v = string.split(str(self.db.version), ',')
92 return v.pop(0)
93 return ''
96 def query(self, sql):
97 """Execute a query and return the corresponding DB-API cursor."""
98 if not self.is_connected() : return None
99 cursor = self.db.cursor()
100 try:
101 cursor.execute("SET CLIENT_ENCODING TO 'UTF-8'")
102 cursor.execute(sql)
103 except libpq.OperationalError, msg:
104 cursor.close()
105 return { 'error' : str(msg), 'notices' : self.db.notices }
106 return { 'cursor' : cursor, 'notices' : self.db.notices }
109 def commit(self):
110 if not self.is_connected() : return
111 self.db.commit()
114 def is_connected(self):
115 return (self.db != None)
118 def disconnect(self):
119 if not self.is_connected() : return
120 self.db.close()
121 self.db = None