def test_build_multiple_insert(self): query = sql.build_insert('table', [{ 'col2': 'col2_data', 'col1': sql.RAW("ENCRYPT('something')") }, { 'col2': 'col2_data', 'col1': sql.RAW("ENCRYPT('something')") }]) expecting = "INSERT INTO table (col1, col2) VALUES (ENCRYPT('something'), 'col2_data'), (ENCRYPT('something'), 'col2_data')" self.assertEqual(query, expecting, 'Got "%s" when expecting "%s"' % (query, expecting))
def logout_player(self, avatar_id): """ Register a player as logged out. """ self.connection.runOperation( sql.build_update('player', dict(last_logout=sql.RAW('now()')), dict(avatar_id=avatar_id)))
def test_build_insert2(self): query = sql.build_insert('table', col2='col2_data', col1=sql.RAW("ENCRYPT('something')")) expecting = "INSERT INTO table (col1, col2) VALUES (ENCRYPT('something'), 'col2_data')" self.assertEqual(query, expecting, 'Got "%s" when expecting "%s"' % (query, expecting))
def test_build_insert_dot_syntax(self): query = sql.build_insert('db.table', { 'col2': 'col2_data', 'col1': sql.RAW("ENCRYPT('something')") }) expecting = "INSERT INTO db.table (col1, col2) VALUES (ENCRYPT('something'), 'col2_data')" self.assertEqual(query, expecting, 'Got "%s" when expecting "%s"' % (query, expecting))
def test_build_select_raw(self): query = sql.build_select('table', { 'col1': sql.RAW("%s = ENCRYPT('something', SUBSTRING(col1,1,2))") }) expecting = "SELECT * FROM table WHERE col1 = ENCRYPT('something', SUBSTRING(col1,1,2))" self.assertEqual(query, expecting, 'Got "%s" when expecting "%s"' % (query, expecting))
def login_player(self, avatar_id, session_id): """ Register a player as logged in. """ self.connection.runOperation( sql.build_update( 'player', dict(session_id=session_id, last_login=sql.RAW('now()')), dict(avatar_id=avatar_id)))
def save(self, obj): """ Save the provided model back into the database. """ obj_type = type(obj).__name__.lower() obj_id = obj.get_id() if (obj_type == 'object'): attribs = dict( name=obj._name, unique_name=('f', 't')[obj._unique_name], owner_id=obj._owner_id, location_id=obj._location_id, ) elif (obj_type == 'verb'): attribs = dict( code=obj._code, filename=obj._filename, owner_id=obj._owner_id, origin_id=obj._origin_id, ability=('f', 't')[obj._ability], method=('f', 't')[obj._method], ) elif (obj_type == 'property'): def check(v): if (v is None): return False elif (v is ""): return False return True attribs = dict( name=obj._name, value=ason.dumps(obj._value) if check(obj._value) else obj._value, owner_id=obj._owner_id, origin_id=obj._origin_id, type=obj._type, ) else: raise RuntimeError( "Don't know how to save an object of type '%s'" % obj_type) if (obj_id): self.connection.runOperation( sql.build_update(obj_type, attribs, dict(id=obj_id))) else: attribs['id'] = sql.RAW('DEFAULT') result = self.connection.runQuery( sql.build_insert(obj_type, attribs) + ' RETURNING id') obj.set_id(result[0]['id']) object_key = '%s-%s' % (obj_type, obj.get_id()) if (object_key not in self.cache): self.cache[object_key] = obj
def is_unique_name(self, key): """ Has the given key been designated as a unique name? """ result = self.connection.runQuery( sql.build_select( 'object', dict(name=sql.RAW(sql.interp('LOWER(%%s) = LOWER(%s)', key)), unique_name=True))) return bool(result)
def get_object(self, key, return_list=False): """ Return the object specified by the provided key. If return_list is True, ambiguous object keys will return a list of matching objects. """ if (isinstance(key, str)): key = key.strip() try: key = int(key) except: pass if (key in ('', 'none', 'None', 'null', 'NULL', None)): return None items = None if (isinstance(key, str)): if (key.startswith('#')): end = key.find("(") if (end == -1): end = key.find(" ") if (end == -1): end = len(key) key = int(key[1:end]) else: items = self.connection.runQuery( sql.build_select('object', name=sql.RAW( sql.interp('LOWER(%%s) = LOWER(%s)', key)))) if (len(items) == 0): if (return_list): return [] else: raise errors.NoSuchObjectError(key) elif (len(items) > 1): if (return_list): return self.instantiate('object', *items) else: raise errors.AmbiguousObjectError(key, items) else: return self.instantiate('object', items[0]) if (isinstance(key, int)): if (key == -1): return None return self.load('object', key) else: raise ValueError("Invalid key type: %r" % repr(key))