1 #
2 # Copyright (c) 2003 Martynas Sklyzmantas, Andrey Lebedev <andrey@micro.lt>
3 #
4 # This module is free software, and you may redistribute it and/or modify
5 # under the same terms as Python, so long as this copyright message and
6 # disclaimer are retained in their original form.
7 #
8 # psycopg backend for roundup
9 #
11 from roundup import hyperdb, date
12 from roundup.backends import rdbms_common
13 import psycopg
14 import os, shutil, popen2
16 class Database(rdbms_common.Database):
17 arg = '%s'
19 def sql_open_connection(self):
20 db = getattr(self.config, 'POSTGRESQL_DATABASE')
21 try:
22 self.conn = psycopg.connect(**db)
23 except psycopg.OperationalError, message:
24 raise DatabaseError, message
26 self.cursor = self.conn.cursor()
28 try:
29 self.database_schema = self.load_dbschema()
30 except:
31 self.rollback()
32 self.database_schema = {}
33 self.sql("CREATE TABLE schema (schema TEXT)")
34 self.sql("CREATE TABLE ids (name VARCHAR(255), num INT4)")
36 def __repr__(self):
37 return '<roundpsycopgsql 0x%x>' % id(self)
39 def sql_stringquote(self, value):
40 ''' psycopg.QuotedString returns a "buffer" object with the
41 single-quotes around it... '''
42 return str(psycopg.QuotedString(str(value)))[1:-1]
44 def sql_index_exists(self, table_name, index_name):
45 sql = 'select count(*) from pg_indexes where ' \
46 'tablename=%s and indexname=%s'%(self.arg, self.arg)
47 self.cursor.execute(sql, (table_name, index_name))
48 return self.cursor.fetchone()[0]
50 def create_class_table(self, spec):
51 cols, mls = self.determine_columns(spec.properties.items())
52 cols.append('id')
53 cols.append('__retired__')
54 scols = ',' . join(['"%s" VARCHAR(255)' % x for x in cols])
55 sql = 'CREATE TABLE "_%s" (%s)' % (spec.classname, scols)
57 if __debug__:
58 print >>hyperdb.DEBUG, 'create_class', (self, sql)
60 self.cursor.execute(sql)
61 return cols, mls
63 def create_journal_table(self, spec):
64 cols = ',' . join(['"%s" VARCHAR(255)' % x
65 for x in 'nodeid date tag action params' . split()])
66 sql = 'CREATE TABLE "%s__journal" (%s)'%(spec.classname, cols)
68 if __debug__:
69 print >>hyperdb.DEBUG, 'create_class', (self, sql)
71 self.cursor.execute(sql)
73 def create_multilink_table(self, spec, ml):
74 sql = '''CREATE TABLE "%s_%s" (linkid VARCHAR(255),
75 nodeid VARCHAR(255))''' % (spec.classname, ml)
77 if __debug__:
78 print >>hyperdb.DEBUG, 'create_class', (self, sql)
80 self.cursor.execute(sql)
82 class Class(rdbms_common.Class):
83 pass
84 class IssueClass(rdbms_common.IssueClass):
85 pass
86 class FileClass(rdbms_common.FileClass):
87 pass