Contact: fumanchu@aminus.org

Log in as guest/dejavu to create tickets

I think I've seen this ORM somewhere before...

Changeset 48

Show
Ignore:
Timestamp:
01/06/05 04:37:31
Author:
fumanchu
Message:

1. Unicode fixed for all SMs. db.AdapterFromDB.coerce_str now defaults to utf8 encoding. So does SM.execute().
2. decimal.Decimal tested for all SMs.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/storage/db.py

    r47 r48  
    349349    def coerce_unicode(self, value, coltype): 
    350350        # You should REALLY check into your DB's encoding and override this. 
    351         return unicode(value, "ASCII") 
     351        return unicode(value, "utf8") 
    352352 
    353353 
    354354# -------------------------- SQL DECOMPILATION -------------------------- # 
    355355 
    356 class ConstWrapper(str): 
     356class ConstWrapper(unicode): 
    357357    """Wraps a constant for use in SQLDecompiler's stack. 
    358358     
     
    362362    """ 
    363363    def __new__(self, basevalue, coerced_value): 
    364         newobj = str.__new__(ConstWrapper, coerced_value) 
     364        newobj = unicode.__new__(ConstWrapper, coerced_value) 
    365365        newobj.basevalue = basevalue 
    366366        return newobj 
    367367     
    368368    def __str__(self): 
    369         return self 
     369        return str(self) 
    370370     
    371371    def __unicode__(self): 
     
    786786            if conn is None: 
    787787                conn = self.connection() 
    788             return conn.query(query
     788            return conn.query(query.encode('utf8')
    789789        except Exception, x: 
    790790            x.args += (query,) 
     
    839839                        try: 
    840840                            consume(unit, key, value, ftype) 
     841                        except UnicodeDecodeError, x: 
     842                            x.reason += "[%s][%s][%s]" % (key, value, ftype) 
     843                            raise x 
    841844                        except Exception, x: 
    842845                            x.args += (key, value, ftype) 
     
    927930            data, col_defs = self.fetch(u"SELECT EXPVAL FROM %s" % table) 
    928931        except: 
    929             # No table was made because no values were present. 
    930             # Feel free to narrow the exception in a subclass. 
    931932            values = None 
    932933        else: 
  • trunk/storage/storemysql.py

    r47 r48  
    11""" 
     2References the MySQLdb package at: 
     3http://sourceforge.net/projects/mysql-python 
     4 
    25From the MySQL manual: 
    36 
     
    1417from dejavu import storage, logic 
    1518from dejavu.storage import db 
     19 
     20 
     21class AdapterToMySQL(db.AdapterToSQL): 
     22     
     23    def coerce_str(self, value): 
     24        return "'" + _mysql.escape_string(value) + "'" 
     25 
     26 
     27class AdapterFromMySQL(db.AdapterFromDB): 
     28     
     29    def coerce_unicode(self, value, coltype): 
     30        return unicode(value, "utf-8") 
    1631 
    1732 
     
    6883            # They also won't truncate trailing spaces like VARCHAR does. 
    6984            if bytes <= 255: 
    70                 return u"VARBINARY(%s)" % bytes 
     85                return u"VARBINARY(%s) CHARACTER SET utf8" % bytes 
    7186            elif bytes < 2 ** 16: 
    7287                return "BLOB" 
     
    85100    identifier_caseless = True 
    86101    typeAdapter = FieldTypeAdapterMySQL() 
     102    toAdapter = AdapterToMySQL() 
     103    fromAdapter = AdapterFromMySQL() 
    87104     
    88105    def __init__(self, name, arena, allOptions={}): 
     
    92109                    "conv", "connect_time", "compress", "named_pipe", 
    93110                    "init_command", "read_default_file", "read_default_group", 
    94                     "cursorclass", "unicode", "client_flag", 
     111                    "cursorclass", "client_flag", 
    95112                    ] 
    96113        self.connargs = dict([(k, v) for k, v in allOptions.iteritems() 
     
    137154     
    138155    def create_database(self): 
     156        # _mysql has create_db and drop_db commands, but they're deprecated. 
    139157        sql = 'CREATE DATABASE %s;' % self.identifier(self.dbname) 
    140158        conn = self._template_conn() 
  • trunk/storage/storeodbc.py

    r47 r48  
    106106        try: 
    107107            cursor = conn.cursor() 
    108             cursor.execute(query
     108            cursor.execute(query.encode('utf8')
    109109            return cursor 
    110110        except Exception, x: 
     
    112112            raise x 
    113113        except dbi.progError, x: 
    114             # Force query to string type 
    115114            x += "\n" + str(query) 
    116115            raise x 
  • trunk/storage/storesqlite.py

    r47 r48  
    7979     
    8080    def execute(self, query, conn=None): 
    81         if conn is None: 
    82             conn = self.connection() 
    8381        try: 
    84             return conn.execute(query) 
     82            if conn is None: 
     83                conn = self.connection() 
     84            return conn.execute(query.encode('utf8')) 
    8585            #           ^^^^^^^ 
    8686        except Exception, x: 
    8787            x.args += (query,) 
     88            # Dereference the connection so that release() is called back. 
     89            conn = None 
    8890            raise x 
    8991     
  • trunk/storage/zoo_fixture.py

    r47 r48  
    66""" 
    77 
    8 import time 
    98import unittest 
    109import threading 
     
    4544                          Admission = "11.75", 
    4645                          ) 
    47          
     46        box.memorize(Biodome) 
     47         
     48        # Animals 
    4849        leopard = zoo.Animal(Name='Leopard', Legs=4) 
    4950        self.assertEqual(leopard.PreviousZoos, None) 
     
    5960        box.memorize(zoo.Animal(Name='Ostrich', Legs=2, PreviousZoos=[])) 
    6061        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) 
    6166         
    6267        millipede = zoo.Animal(Name='Millipede', Legs=1000000) 
     
    6570        box.memorize(millipede) 
    6671         
     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         
    6780        box.flush_all() 
    6881     
     
    7083        box = zoo.arena.new_sandbox() 
    7184         
     85        # Zoos 
    7286        WAP = box.unit(zoo.Zoo, Name='Wild Animal Park') 
    7387        self.assertNotEqual(WAP, None) 
     
    86100        self.assertEqual(float(SDZ.Admission), 0) 
    87101         
     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 
    88111        leopard = box.unit(zoo.Animal, Name='Leopard') 
    89112        self.assertEqual(leopard.Name, 'Leopard') 
     
    107130        self.assertEqual(millipede.PreviousZoos, [WAP.ID]) 
    108131        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         
    109142        box.flush_all() 
    110143     
     
    120153        zoos = [x for x in box.recall(zoo.Zoo)] 
    121154        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
    124157        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
    127160        self.assertEqual(matches(lambda x: x.Legs > 10), 2) 
    128161        self.assertEqual(matches(lambda x: x.Name.startswith('L')), 2) 
    129162        self.assertEqual(matches(lambda x: x.Name.endswith('pede')), 2) 
    130163        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) 
    133165         
    134166        # In operator (containedby) 
     
    146178        self.assertEqual(matches(lambda x: dejavu.icontains(x.Name, 'PEDE')), 2) 
    147179        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) 
    149182        name = 'Lion' 
    150183        self.assertEqual(matches(lambda x: len(x.Name) == len(name)), 3) 
    151184         
    152185        # 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
    154187         
    155188        # Test now(), today(), year() 
     
    163196        # known SM handles, so it will default back to Expr.eval(). 
    164197        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
    166199     
    167200    def test_3_Aggregates(self): 
  • trunk/zoo.py

    r47 r48  
    4343 
    4444class Exhibit(Unit): 
     45    # Make this a string to help test vs unicode. 
     46    Name = UnitProperty(str) 
    4547    ZooID = UnitProperty(int) 
    4648    Animals = UnitProperty(list) 
     49    if decimal: 
     50        Acreage = UnitProperty(decimal.Decimal) 
     51    else: 
     52        Acreage = UnitProperty(float) 
    4753 
    4854associate(Zoo, 'ID', Exhibit, 'ZooID')