Changeset 48
- Timestamp:
- 01/06/05 04:37:31
- Files:
-
- trunk/storage/db.py (modified) (5 diffs)
- trunk/storage/storemysql.py (modified) (6 diffs)
- trunk/storage/storeodbc.py (modified) (2 diffs)
- trunk/storage/storesqlite.py (modified) (1 diff)
- trunk/storage/zoo_fixture.py (modified) (10 diffs)
- trunk/zoo.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/storage/db.py
r47 r48 349 349 def coerce_unicode(self, value, coltype): 350 350 # You should REALLY check into your DB's encoding and override this. 351 return unicode(value, " ASCII")351 return unicode(value, "utf8") 352 352 353 353 354 354 # -------------------------- SQL DECOMPILATION -------------------------- # 355 355 356 class ConstWrapper( str):356 class ConstWrapper(unicode): 357 357 """Wraps a constant for use in SQLDecompiler's stack. 358 358 … … 362 362 """ 363 363 def __new__(self, basevalue, coerced_value): 364 newobj = str.__new__(ConstWrapper, coerced_value)364 newobj = unicode.__new__(ConstWrapper, coerced_value) 365 365 newobj.basevalue = basevalue 366 366 return newobj 367 367 368 368 def __str__(self): 369 return s elf369 return str(self) 370 370 371 371 def __unicode__(self): … … 786 786 if conn is None: 787 787 conn = self.connection() 788 return conn.query(query )788 return conn.query(query.encode('utf8')) 789 789 except Exception, x: 790 790 x.args += (query,) … … 839 839 try: 840 840 consume(unit, key, value, ftype) 841 except UnicodeDecodeError, x: 842 x.reason += "[%s][%s][%s]" % (key, value, ftype) 843 raise x 841 844 except Exception, x: 842 845 x.args += (key, value, ftype) … … 927 930 data, col_defs = self.fetch(u"SELECT EXPVAL FROM %s" % table) 928 931 except: 929 # No table was made because no values were present.930 # Feel free to narrow the exception in a subclass.931 932 values = None 932 933 else: trunk/storage/storemysql.py
r47 r48 1 1 """ 2 References the MySQLdb package at: 3 http://sourceforge.net/projects/mysql-python 4 2 5 From the MySQL manual: 3 6 … … 14 17 from dejavu import storage, logic 15 18 from dejavu.storage import db 19 20 21 class AdapterToMySQL(db.AdapterToSQL): 22 23 def coerce_str(self, value): 24 return "'" + _mysql.escape_string(value) + "'" 25 26 27 class AdapterFromMySQL(db.AdapterFromDB): 28 29 def coerce_unicode(self, value, coltype): 30 return unicode(value, "utf-8") 16 31 17 32 … … 68 83 # They also won't truncate trailing spaces like VARCHAR does. 69 84 if bytes <= 255: 70 return u"VARBINARY(%s) " % bytes85 return u"VARBINARY(%s) CHARACTER SET utf8" % bytes 71 86 elif bytes < 2 ** 16: 72 87 return "BLOB" … … 85 100 identifier_caseless = True 86 101 typeAdapter = FieldTypeAdapterMySQL() 102 toAdapter = AdapterToMySQL() 103 fromAdapter = AdapterFromMySQL() 87 104 88 105 def __init__(self, name, arena, allOptions={}): … … 92 109 "conv", "connect_time", "compress", "named_pipe", 93 110 "init_command", "read_default_file", "read_default_group", 94 "cursorclass", " unicode", "client_flag",111 "cursorclass", "client_flag", 95 112 ] 96 113 self.connargs = dict([(k, v) for k, v in allOptions.iteritems() … … 137 154 138 155 def create_database(self): 156 # _mysql has create_db and drop_db commands, but they're deprecated. 139 157 sql = 'CREATE DATABASE %s;' % self.identifier(self.dbname) 140 158 conn = self._template_conn() trunk/storage/storeodbc.py
r47 r48 106 106 try: 107 107 cursor = conn.cursor() 108 cursor.execute(query )108 cursor.execute(query.encode('utf8')) 109 109 return cursor 110 110 except Exception, x: … … 112 112 raise x 113 113 except dbi.progError, x: 114 # Force query to string type115 114 x += "\n" + str(query) 116 115 raise x trunk/storage/storesqlite.py
r47 r48 79 79 80 80 def execute(self, query, conn=None): 81 if conn is None:82 conn = self.connection()83 81 try: 84 return conn.execute(query) 82 if conn is None: 83 conn = self.connection() 84 return conn.execute(query.encode('utf8')) 85 85 # ^^^^^^^ 86 86 except Exception, x: 87 87 x.args += (query,) 88 # Dereference the connection so that release() is called back. 89 conn = None 88 90 raise x 89 91 trunk/storage/zoo_fixture.py
r47 r48 6 6 """ 7 7 8 import time9 8 import unittest 10 9 import threading … … 45 44 Admission = "11.75", 46 45 ) 47 46 box.memorize(Biodome) 47 48 # Animals 48 49 leopard = zoo.Animal(Name='Leopard', Legs=4) 49 50 self.assertEqual(leopard.PreviousZoos, None) … … 59 60 box.memorize(zoo.Animal(Name='Ostrich', Legs=2, PreviousZoos=[])) 60 61 box.memorize(zoo.Animal(Name='Centipede', Legs=100)) 62 emp = zoo.Animal(Name='Emperor Penguin', Legs=2) 63 box.memorize(emp) 64 adelie = zoo.Animal(Name='Adelie Penguin', Legs=2) 65 box.memorize(adelie) 61 66 62 67 millipede = zoo.Animal(Name='Millipede', Legs=1000000) … … 65 70 box.memorize(millipede) 66 71 72 # Exhibits 73 pe = zoo.Exhibit(Name = 'The Penguin Encounter', 74 ZooID = SDZ.ID, 75 Animals = [emp.ID, adelie.ID], 76 Acreage = "3.21", 77 ) 78 box.memorize(pe) 79 67 80 box.flush_all() 68 81 … … 70 83 box = zoo.arena.new_sandbox() 71 84 85 # Zoos 72 86 WAP = box.unit(zoo.Zoo, Name='Wild Animal Park') 73 87 self.assertNotEqual(WAP, None) … … 86 100 self.assertEqual(float(SDZ.Admission), 0) 87 101 102 Biodome = box.unit(zoo.Zoo, Name = u'Montr\xe9al Biod\xf4me') 103 self.assertNotEqual(Biodome, None) 104 self.assertEqual(Biodome.Name, u'Montr\xe9al Biod\xf4me') 105 self.assertEqual(Biodome.Founded, datetime.date(1992, 6, 19)) 106 self.assertEqual(Biodome.Opens, datetime.time(9, 0, 0)) 107 self.assertEqual(Biodome.LastEscape, None) 108 self.assertEqual(float(Biodome.Admission), 11.75) 109 110 # Animals 88 111 leopard = box.unit(zoo.Animal, Name='Leopard') 89 112 self.assertEqual(leopard.Name, 'Leopard') … … 107 130 self.assertEqual(millipede.PreviousZoos, [WAP.ID]) 108 131 self.assertEqual(millipede.LastEscape, None) 132 133 # Exhibits 134 exes = [x for x in box.recall(zoo.Exhibit)] 135 self.assertEqual(len(exes), 1) 136 pe = exes[0] 137 self.assertEqual(pe.Name, "The Penguin Encounter") 138 self.assertEqual(pe.ZooID, SDZ.ID) 139 self.assertEqual(len(pe.Animals), 2) 140 self.assertEqual(float(pe.Acreage), 3.21) 141 109 142 box.flush_all() 110 143 … … 120 153 zoos = [x for x in box.recall(zoo.Zoo)] 121 154 self.assertEqual(zoos[0].dirty(), False) 122 self.assertEqual(len(zoos), 2)123 self.assertEqual(matches(lambda x: True), 8)155 self.assertEqual(len(zoos), 3) 156 self.assertEqual(matches(lambda x: True), 10) 124 157 self.assertEqual(matches(lambda x: x.Legs == 4), 4) 125 self.assertEqual(matches(lambda x: x.Legs == 2), 1)126 self.assertEqual(matches(lambda x: x.Legs >= 2 and x.Legs < 20), 5)158 self.assertEqual(matches(lambda x: x.Legs == 2), 3) 159 self.assertEqual(matches(lambda x: x.Legs >= 2 and x.Legs < 20), 7) 127 160 self.assertEqual(matches(lambda x: x.Legs > 10), 2) 128 161 self.assertEqual(matches(lambda x: x.Name.startswith('L')), 2) 129 162 self.assertEqual(matches(lambda x: x.Name.endswith('pede')), 2) 130 163 self.assertEqual(matches(lambda x: x.LastEscape != None), 1) 131 self.assertEqual(matches(lambda x: None == x.LastEscape), 7) 132 ## self.assertRaises(ValueError, matches, lambda x: None is x.LastEscape) 164 self.assertEqual(matches(lambda x: None == x.LastEscape), 9) 133 165 134 166 # In operator (containedby) … … 146 178 self.assertEqual(matches(lambda x: dejavu.icontains(x.Name, 'PEDE')), 2) 147 179 self.assertEqual(matches(lambda x: dejavu.icontains(('Lion', 'Banana'), x.Name)), 1) 148 self.assertEqual(matches(lambda x: dejavu.icontainedby(x.Name, ('Lion', 'Bear', 'Leopard'))), 3) 180 f = lambda x: dejavu.icontainedby(x.Name, ('Lion', 'Bear', 'Leopard')) 181 self.assertEqual(matches(f), 3) 149 182 name = 'Lion' 150 183 self.assertEqual(matches(lambda x: len(x.Name) == len(name)), 3) 151 184 152 185 # This broke sometime in 2004. Rev 32 seems to have fixed it. 153 self.assertEqual(matches(lambda x: 'i' in x.Name), 5)186 self.assertEqual(matches(lambda x: 'i' in x.Name), 7) 154 187 155 188 # Test now(), today(), year() … … 163 196 # known SM handles, so it will default back to Expr.eval(). 164 197 self.assertEqual(matches(lambda x: 'p' in x.Name 165 and x.Name.count('e') > 1), 2)198 and x.Name.count('e') > 1), 3) 166 199 167 200 def test_3_Aggregates(self): trunk/zoo.py
r47 r48 43 43 44 44 class Exhibit(Unit): 45 # Make this a string to help test vs unicode. 46 Name = UnitProperty(str) 45 47 ZooID = UnitProperty(int) 46 48 Animals = UnitProperty(list) 49 if decimal: 50 Acreage = UnitProperty(decimal.Decimal) 51 else: 52 Acreage = UnitProperty(float) 47 53 48 54 associate(Zoo, 'ID', Exhibit, 'ZooID')
