Skip to content

Commit d7f2ebe

Browse files
author
Richard Jones
committed
added mysql backend
1 parent bd536c3 commit d7f2ebe

File tree

2 files changed

+106
-6
lines changed

2 files changed

+106
-6
lines changed

roundup/backends/back_mysql.py

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
from roundup.backends.rdbms_common import *
2+
import MySQLdb
3+
from MySQLdb.constants import ER
4+
5+
class Database(Database):
6+
arg = '%s'
7+
8+
def open_connection(self):
9+
db = getattr(self.config, 'MYSQL_DATABASE')
10+
try:
11+
self.conn = MySQLdb.connect(*db)
12+
except MySQLdb.OperationalError, message:
13+
raise DatabaseError, message
14+
15+
self.cursor = self.conn.cursor()
16+
try:
17+
self.database_schema = self.load_dbschema()
18+
except MySQLdb.ProgrammingError, message:
19+
if message[0] != ER.NO_SUCH_TABLE:
20+
raise DatabaseError, message
21+
self.database_schema = {}
22+
self.cursor.execute("CREATE TABLE schema (schema TEXT)")
23+
self.cursor.execute("CREATE TABLE ids (name varchar(255), num INT)")
24+
25+
def close(self):
26+
try:
27+
self.conn.close()
28+
except MySQLdb.OperationalError, message:
29+
raise
30+
31+
def __repr__(self):
32+
return '<myroundsql 0x%x>'%id(self)
33+
34+
def sql_fetchone(self):
35+
return self.cursor.fetchone()
36+
37+
def sql_fetchall(self):
38+
return self.cursor.fetchall()
39+
40+
def save_dbschema(self, schema):
41+
s = repr(self.database_schema)
42+
self.sql('INSERT INTO schema VALUES (%s)', (s,))
43+
44+
def load_dbschema(self):
45+
self.cursor.execute('SELECT schema FROM schema')
46+
return eval(self.cursor.fetchone()[0])
47+
48+
def save_journal(self, classname, cols, nodeid, journaldate,
49+
journaltag, action, params):
50+
params = repr(params)
51+
entry = (nodeid, journaldate, journaltag, action, params)
52+
53+
a = self.arg
54+
sql = 'insert into %s__journal (%s) values (%s,%s,%s,%s,%s)'%(classname,
55+
cols, a, a, a, a, a)
56+
if __debug__:
57+
print >>hyperdb.DEBUG, 'addjournal', (self, sql, entry)
58+
self.cursor.execute(sql, entry)
59+
60+
def load_journal(self, classname, cols, nodeid):
61+
sql = 'select %s from %s__journal where nodeid=%s'%(cols, classname,
62+
self.arg)
63+
if __debug__:
64+
print >>hyperdb.DEBUG, 'getjournal', (self, sql, nodeid)
65+
self.cursor.execute(sql, (nodeid,))
66+
res = []
67+
for nodeid, date_stamp, user, action, params in self.cursor.fetchall():
68+
params = eval(params)
69+
res.append((nodeid, date.Date(date_stamp), user, action, params))
70+
return res
71+
72+
def create_class_table(self, spec):
73+
cols, mls = self.determine_columns(spec.properties.items())
74+
cols.append('id')
75+
cols.append('__retired__')
76+
scols = ',' . join(['`%s` VARCHAR(255)'%x for x in cols])
77+
sql = 'CREATE TABLE `_%s` (%s)'%(spec.classname, scols)
78+
if __debug__:
79+
print >>hyperdb.DEBUG, 'create_class', (self, sql)
80+
self.cursor.execute(sql)
81+
return cols, mls
82+
83+
def create_journal_table(self, spec):
84+
cols = ',' . join(['`%s` VARCHAR(255)'%x
85+
for x in 'nodeid date tag action params' . split()])
86+
sql = 'CREATE TABLE `%s__journal` (%s)'%(spec.classname, cols)
87+
if __debug__:
88+
print >>hyperdb.DEBUG, 'create_class', (self, sql)
89+
self.cursor.execute(sql)
90+
91+
def create_multilink_table(self, spec, ml):
92+
sql = '''CREATE TABLE `%s_%s` (linkid VARCHAR(255),
93+
nodeid VARCHAR(255))'''%(spec.classname, ml)
94+
if __debug__:
95+
print >>hyperdb.DEBUG, 'create_class', (self, sql)
96+
self.cursor.execute(sql)
97+
98+

test/test_db.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
1616
# SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
1717
#
18-
# $Id: test_db.py,v 1.65 2003-01-12 23:53:20 richard Exp $
18+
# $Id: test_db.py,v 1.66 2003-01-13 23:32:12 richard Exp $
1919

2020
import unittest, os, shutil, time
2121

@@ -708,7 +708,8 @@ def setUp(self):
708708
# remove previous test, ignore errors
709709
if os.path.exists(config.DATABASE):
710710
shutil.rmtree(config.DATABASE)
711-
config.MYSQL_DATABASE='mysql@localhost root rootpasswd'.split()
711+
config.MYSQL_DATABASE = ('localhost', 'rounduptest', 'rounduptest',
712+
'rounduptest')
712713
os.makedirs(config.DATABASE + '/files')
713714
self.db = mysql.Database(config, 'admin')
714715
setupSchema(self.db, 1, mysql)
@@ -719,7 +720,8 @@ def setUp(self):
719720
# remove previous test, ignore errors
720721
if os.path.exists(config.DATABASE):
721722
shutil.rmtree(config.DATABASE)
722-
config.MYSQL_DATABASE='mysql@localhost root rootpasswd'.split()
723+
config.MYSQL_DATABASE = ('localhost', 'rounduptest', 'rounduptest',
724+
'rounduptest')
723725
os.makedirs(config.DATABASE + '/files')
724726
db = mysql.Database(config, 'admin')
725727
setupSchema(db, 1, mysql)
@@ -818,9 +820,9 @@ def suite():
818820
# return unittest.TestSuite(l)
819821

820822
from roundup import backends
821-
# if hasattr(backends, 'mysql'):
822-
# l.append(unittest.makeSuite(mysqlDBTestCase, 'test'))
823-
# l.append(unittest.makeSuite(mysqlReadOnlyDBTestCase, 'test'))
823+
if hasattr(backends, 'mysql'):
824+
l.append(unittest.makeSuite(mysqlDBTestCase, 'test'))
825+
l.append(unittest.makeSuite(mysqlReadOnlyDBTestCase, 'test'))
824826
# return unittest.TestSuite(l)
825827

826828
if hasattr(backends, 'gadfly'):

0 commit comments

Comments
 (0)