comparison roundup/backends/back_postgresql.py @ 1906:f255363e6d97

PostgreSQL backend lands. - that's the postgresql backend in (cleaned up doc, unit testing harness and the backend module itself) - also cleaned up the index maintenance code (actual checks for existence rather than bare-except failure mode)
author Richard Jones <richard@users.sourceforge.net>
date Tue, 11 Nov 2003 11:19:18 +0000
parents f63aa57386b0
children f5c804379c85
comparison
equal deleted inserted replaced
1905:dc43e339e607 1906:f255363e6d97
9 # 9 #
10 10
11 from roundup.backends.rdbms_common import * 11 from roundup.backends.rdbms_common import *
12 from roundup.backends import rdbms_common 12 from roundup.backends import rdbms_common
13 import psycopg 13 import psycopg
14 import os, shutil 14 import os, shutil, popen2
15
16 class Maintenance:
17 """ Database maintenance functions """
18 def db_nuke(self, config):
19 """Clear all database contents and drop database itself"""
20 config.POSTGRESQL_DATABASE['database'] = 'template1'
21 db = Database(config, 'admin')
22 db.conn.set_isolation_level(0)
23 db.sql("DROP DATABASE %s" % config.POSTGRESQL_DBNAME)
24 db.sql("CREATE DATABASE %s" % config.POSTGRESQL_DBNAME)
25 if os.path.exists(config.DATABASE):
26 shutil.rmtree(config.DATABASE)
27 config.POSTGRESQL_DATABASE['database'] = config.POSTGRESQL_DBNAME
28
29 def db_exists(self, config):
30 """Check if database already exists"""
31 try:
32 db = Database(config, 'admin')
33 return 1
34 except:
35 return 0
36 15
37 class Database(Database): 16 class Database(Database):
38 arg = '%s' 17 arg = '%s'
39 18
40 def open_connection(self): 19 def open_connection(self):
56 35
57 def close(self): 36 def close(self):
58 self.conn.close() 37 self.conn.close()
59 38
60 def __repr__(self): 39 def __repr__(self):
61 return '<psycopgroundsql 0x%x>' % id(self) 40 return '<roundpsycopgsql 0x%x>' % id(self)
62 41
63 def sql_fetchone(self): 42 def sql_fetchone(self):
64 return self.cursor.fetchone() 43 return self.cursor.fetchone()
65 44
66 def sql_fetchall(self): 45 def sql_fetchall(self):
67 return self.cursor.fetchall() 46 return self.cursor.fetchall()
68 47
69 def sql_stringquote(self, value): 48 def sql_stringquote(self, value):
70 return psycopg.QuotedString(str(value)) 49 ''' psycopg.QuotedString returns a "buffer" object with the
50 single-quotes around it... '''
51 return str(psycopg.QuotedString(str(value)))[1:-1]
52
53 def sql_index_exists(self, table_name, index_name):
54 sql = 'select count(*) from pg_indexes where ' \
55 'tablename=%s and indexname=%s'%(self.arg, self.arg)
56 self.cursor.execute(sql, (table_name, index_name))
57 return self.cursor.fetchone()[0]
71 58
72 def save_dbschema(self, schema): 59 def save_dbschema(self, schema):
73 s = repr(self.database_schema) 60 s = repr(self.database_schema)
74 self.sql('INSERT INTO schema VALUES (%s)', (s,)) 61 self.sql('INSERT INTO schema VALUES (%s)', (s,))
75 62
136 123
137 if __debug__: 124 if __debug__:
138 print >>hyperdb.DEBUG, 'create_class', (self, sql) 125 print >>hyperdb.DEBUG, 'create_class', (self, sql)
139 126
140 self.cursor.execute(sql) 127 self.cursor.execute(sql)
141
142 # Static methods
143 nuke = Maintenance().db_nuke
144 exists = Maintenance().db_exists
145 128
146 class PsycopgClass: 129 class PsycopgClass:
147 def find(self, **propspec): 130 def find(self, **propspec):
148 """Get the ids of nodes in this class which link to the given nodes.""" 131 """Get the ids of nodes in this class which link to the given nodes."""
149 132
172 if not isinstance(props[prop], hyperdb.Link): 155 if not isinstance(props[prop], hyperdb.Link):
173 continue 156 continue
174 if type(values) is type(''): 157 if type(values) is type(''):
175 allvalues += (values,) 158 allvalues += (values,)
176 where.append('_%s = %s' % (prop, a)) 159 where.append('_%s = %s' % (prop, a))
160 elif values is None:
161 where.append('_%s is NULL'%prop)
177 else: 162 else:
178 allvalues += tuple(values.keys()) 163 allvalues += tuple(values.keys())
179 where.append('_%s in (%s)' % (prop, ','.join([a]*len(values)))) 164 where.append('_%s in (%s)' % (prop, ','.join([a]*len(values))))
180 tables = [] 165 tables = []
181 if where: 166 if where:

Roundup Issue Tracker: http://roundup-tracker.org/