comparison roundup/backends/back_mysql.py @ 2679:b3f0b7b9d20d

for MySQL connection, password argument is called 'passwd'. Have own version of connection_dict() returning dictionary suitable for MySQL connections. trim trailing spaces; fix vim modeline.
author Alexander Smishlajev <a1s@users.sourceforge.net>
date Sat, 25 Sep 2004 15:42:41 +0000
parents f47ca4541770
children 59ea85d47d34
comparison
equal deleted inserted replaced
2678:599d2eac6be0 2679:b3f0b7b9d20d
37 from roundup.backends import rdbms_common 37 from roundup.backends import rdbms_common
38 import MySQLdb 38 import MySQLdb
39 import os, shutil 39 import os, shutil
40 from MySQLdb.constants import ER 40 from MySQLdb.constants import ER
41 41
42 def connection_dict(config):
43 d = rdbms_common.connection_dict(config, 'db')
44 if d.has_key('password'):
45 d['passwd'] = d['password']
46 del d['password']
47 return d
48
42 def db_nuke(config): 49 def db_nuke(config):
43 """Clear all database contents and drop database itself""" 50 """Clear all database contents and drop database itself"""
44 if db_exists(config): 51 if db_exists(config):
45 kwargs = rdbms_common.connection_dict(config) 52 kwargs = connection_dict(config)
46 conn = MySQLdb.connect(**kwargs) 53 conn = MySQLdb.connect(**kwargs)
47 try: 54 try:
48 conn.select_db(config.RDBMS_NAME) 55 conn.select_db(config.RDBMS_NAME)
49 except: 56 except:
50 # no, it doesn't exist 57 # no, it doesn't exist
68 if os.path.exists(config.DATABASE): 75 if os.path.exists(config.DATABASE):
69 shutil.rmtree(config.DATABASE) 76 shutil.rmtree(config.DATABASE)
70 77
71 def db_create(config): 78 def db_create(config):
72 """Create the database.""" 79 """Create the database."""
73 kwargs = rdbms_common.connection_dict(config) 80 kwargs = connection_dict(config)
74 conn = MySQLdb.connect(**kwargs) 81 conn = MySQLdb.connect(**kwargs)
75 cursor = conn.cursor() 82 cursor = conn.cursor()
76 command = "CREATE DATABASE %s"%config.RDBMS_NAME 83 command = "CREATE DATABASE %s"%config.RDBMS_NAME
77 config.logging.getLogger('hyperdb').info(command) 84 config.logging.getLogger('hyperdb').info(command)
78 cursor.execute(command) 85 cursor.execute(command)
79 conn.commit() 86 conn.commit()
80 conn.close() 87 conn.close()
81 88
82 def db_exists(config): 89 def db_exists(config):
83 """Check if database already exists.""" 90 """Check if database already exists."""
84 kwargs = rdbms_common.connection_dict(config) 91 kwargs = connection_dict(config)
85 conn = MySQLdb.connect(**kwargs) 92 conn = MySQLdb.connect(**kwargs)
86 try: 93 try:
87 try: 94 try:
88 conn.select_db(config.RDBMS_NAME) 95 conn.select_db(config.RDBMS_NAME)
89 except MySQLdb.OperationalError: 96 except MySQLdb.OperationalError:
123 hyperdb.Number : lambda x: x, 130 hyperdb.Number : lambda x: x,
124 hyperdb.Multilink : lambda x: x, # used in journal marshalling 131 hyperdb.Multilink : lambda x: x, # used in journal marshalling
125 } 132 }
126 133
127 def sql_open_connection(self): 134 def sql_open_connection(self):
128 kwargs = rdbms_common.connection_dict(config, 'db') 135 kwargs = connection_dict(self.config)
129 self.config.logging.getLogger('hyperdb').info('open database %r'%( 136 self.config.logging.getLogger('hyperdb').info('open database %r'%(
130 kwargs['db'],)) 137 kwargs['db'],))
131 try: 138 try:
132 conn = MySQLdb.connect(**kwargs) 139 conn = MySQLdb.connect(**kwargs)
133 except MySQLdb.OperationalError, message: 140 except MySQLdb.OperationalError, message:
134 raise DatabaseError, message 141 raise DatabaseError, message
135 cursor = conn.cursor() 142 cursor = conn.cursor()
136 cursor.execute("SET AUTOCOMMIT=OFF") 143 cursor.execute("SET AUTOCOMMIT=OFF")
137 cursor.execute("START TRANSACTION") 144 cursor.execute("START TRANSACTION")
138 return (conn, cursor) 145 return (conn, cursor)
139 146
140 def open_connection(self): 147 def open_connection(self):
141 # make sure the database actually exists 148 # make sure the database actually exists
142 if not db_exists(self.config): 149 if not db_exists(self.config):
143 db_create(self.config) 150 db_create(self.config)
144 151
216 self.sql(sql) 223 self.sql(sql)
217 224
218 if properties.has_key(name): 225 if properties.has_key(name):
219 # re-create and populate the new table 226 # re-create and populate the new table
220 self.create_multilink_table(klass, name) 227 self.create_multilink_table(klass, name)
221 sql = '''insert into %s (linkid, nodeid) values 228 sql = '''insert into %s (linkid, nodeid) values
222 (%s, %s)'''%(tn, self.arg, self.arg) 229 (%s, %s)'''%(tn, self.arg, self.arg)
223 for linkid, nodeid in rows: 230 for linkid, nodeid in rows:
224 self.sql(sql, (int(linkid), int(nodeid))) 231 self.sql(sql, (int(linkid), int(nodeid)))
225 232
226 # figure the column names to fetch 233 # figure the column names to fetch
257 v = date.Date(v) 264 v = date.Date(v)
258 elif isinstance(prop, Interval) and v is not None: 265 elif isinstance(prop, Interval) and v is not None:
259 v = date.Interval(v) 266 v = date.Interval(v)
260 elif isinstance(prop, Password) and v is not None: 267 elif isinstance(prop, Password) and v is not None:
261 v = password.Password(encrypted=v) 268 v = password.Password(encrypted=v)
262 elif (isinstance(prop, Boolean) or 269 elif (isinstance(prop, Boolean) or
263 isinstance(prop, Number)) and v is not None: 270 isinstance(prop, Number)) and v is not None:
264 v = float(v) 271 v = float(v)
265 272
266 # convert to new MySQL data type 273 # convert to new MySQL data type
267 prop = properties[name] 274 prop = properties[name]
321 for nodeid, journaldate, journaltag, action, params in olddata: 328 for nodeid, journaldate, journaltag, action, params in olddata:
322 self.save_journal(cn, cols, nodeid, journaldate, 329 self.save_journal(cn, cols, nodeid, journaldate,
323 journaltag, action, params) 330 journaltag, action, params)
324 331
325 # make sure the normal schema update code doesn't try to 332 # make sure the normal schema update code doesn't try to
326 # change things 333 # change things
327 self.database_schema['tables'][cn] = klass.schema() 334 self.database_schema['tables'][cn] = klass.schema()
328 335
329 def fix_version_2_tables(self): 336 def fix_version_2_tables(self):
330 # Convert journal date column to TIMESTAMP, params column to TEXT 337 # Convert journal date column to TIMESTAMP, params column to TEXT
331 self._convert_journal_tables() 338 self._convert_journal_tables()
377 continue 384 continue
378 index_sql = 'drop index %s on %s'%(index_name, table_name) 385 index_sql = 'drop index %s on %s'%(index_name, table_name)
379 self.sql(index_sql) 386 self.sql(index_sql)
380 387
381 def create_journal_table(self, spec): 388 def create_journal_table(self, spec):
382 ''' create the journal table for a class given the spec and 389 ''' create the journal table for a class given the spec and
383 already-determined cols 390 already-determined cols
384 ''' 391 '''
385 # journal table 392 # journal table
386 cols = ','.join(['%s varchar'%x 393 cols = ','.join(['%s varchar'%x
387 for x in 'nodeid date tag action params'.split()]) 394 for x in 'nodeid date tag action params'.split()])
603 if date_rng.to_value: 610 if date_rng.to_value:
604 where.append('_%s._%s <= %s'%(cn, k, a)) 611 where.append('_%s._%s <= %s'%(cn, k, a))
605 args.append(dc(date_rng.to_value)) 612 args.append(dc(date_rng.to_value))
606 except ValueError: 613 except ValueError:
607 # If range creation fails - ignore that search parameter 614 # If range creation fails - ignore that search parameter
608 pass 615 pass
609 elif isinstance(propclass, Interval): 616 elif isinstance(propclass, Interval):
610 # filter using the __<prop>_int__ column 617 # filter using the __<prop>_int__ column
611 if isinstance(v, type([])): 618 if isinstance(v, type([])):
612 s = ','.join([a for x in v]) 619 s = ','.join([a for x in v])
613 where.append('_%s.__%s_int__ in (%s)'%(cn, k, s)) 620 where.append('_%s.__%s_int__ in (%s)'%(cn, k, s))
622 if date_rng.to_value: 629 if date_rng.to_value:
623 where.append('_%s.__%s_int__ <= %s'%(cn, k, a)) 630 where.append('_%s.__%s_int__ <= %s'%(cn, k, a))
624 args.append(date_rng.to_value.as_seconds()) 631 args.append(date_rng.to_value.as_seconds())
625 except ValueError: 632 except ValueError:
626 # If range creation fails - ignore that search parameter 633 # If range creation fails - ignore that search parameter
627 pass 634 pass
628 else: 635 else:
629 if isinstance(v, type([])): 636 if isinstance(v, type([])):
630 s = ','.join([a for x in v]) 637 s = ','.join([a for x in v])
631 where.append('_%s._%s in (%s)'%(cn, k, s)) 638 where.append('_%s._%s in (%s)'%(cn, k, s))
632 args = args + v 639 args = args + v
741 class IssueClass(MysqlClass, rdbms_common.IssueClass): 748 class IssueClass(MysqlClass, rdbms_common.IssueClass):
742 pass 749 pass
743 class FileClass(MysqlClass, rdbms_common.FileClass): 750 class FileClass(MysqlClass, rdbms_common.FileClass):
744 pass 751 pass
745 752
746 #vim: set et 753 # vim: set et sts=4 sw=4 :

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